428
|
1 /* Asynchronous subprocess implementation for Win32
|
|
2 Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994, 1995
|
|
3 Free Software Foundation, Inc.
|
|
4 Copyright (C) 1995 Sun Microsystems, Inc.
|
771
|
5 Copyright (C) 1995, 1996, 2000, 2001, 2002 Ben Wing.
|
428
|
6
|
|
7 This file is part of XEmacs.
|
|
8
|
|
9 XEmacs is free software; you can redistribute it and/or modify it
|
|
10 under the terms of the GNU General Public License as published by the
|
|
11 Free Software Foundation; either version 2, or (at your option) any
|
|
12 later version.
|
|
13
|
|
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
17 for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with XEmacs; see the file COPYING. If not, write to
|
|
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 Boston, MA 02111-1307, USA. */
|
|
23
|
|
24 /* Written by Kirill M. Katsnelson <kkm@kis.ru>, April 1998 */
|
|
25
|
771
|
26 /* Mule-ized as of 8-6-00 */
|
|
27
|
428
|
28 #include <config.h>
|
|
29 #include "lisp.h"
|
|
30
|
442
|
31 #include "console-msw.h"
|
428
|
32 #include "hash.h"
|
|
33 #include "lstream.h"
|
|
34 #include "process.h"
|
|
35 #include "procimpl.h"
|
|
36
|
558
|
37 #include "sysfile.h"
|
|
38 #include "sysproc.h"
|
859
|
39 #include "syssignal.h"
|
428
|
40
|
442
|
41 /* Bound by win32-native.el */
|
|
42 Lisp_Object Qmswindows_construct_process_command_line;
|
|
43
|
428
|
44 /* Arbitrary size limit for code fragments passed to run_in_other_process */
|
|
45 #define FRAGMENT_CODE_SIZE 32
|
|
46
|
|
47 /* Implementation-specific data. Pointed to by Lisp_Process->process_data */
|
|
48 struct nt_process_data
|
|
49 {
|
|
50 HANDLE h_process;
|
442
|
51 DWORD dwProcessId;
|
|
52 HWND hwnd; /* console window */
|
853
|
53 int selected_for_exit_notify;
|
428
|
54 };
|
|
55
|
442
|
56 /* Control whether create_child causes the process to inherit Emacs'
|
|
57 console window, or be given a new one of its own. The default is
|
|
58 nil, to allow multiple DOS programs to run on Win95. Having separate
|
|
59 consoles also allows Emacs to cleanly terminate process groups. */
|
|
60 Lisp_Object Vmswindows_start_process_share_console;
|
|
61
|
|
62 /* Control whether create_child cause the process to inherit Emacs'
|
|
63 error mode setting. The default is t, to minimize the possibility of
|
|
64 subprocesses blocking when accessing unmounted drives. */
|
|
65 Lisp_Object Vmswindows_start_process_inherit_error_mode;
|
|
66
|
771
|
67 #define NT_DATA(p) ((struct nt_process_data *)((p)->process_data))
|
428
|
68
|
|
69 /*-----------------------------------------------------------------------*/
|
|
70 /* Process helpers */
|
|
71 /*-----------------------------------------------------------------------*/
|
|
72
|
853
|
73 /* These break process abstraction. Prototypes in console-msw.h,
|
|
74 used by select_process method in event-msw.c.
|
|
75
|
|
76 If called the first time on a process, return the process handle, so we
|
|
77 can select on it and receive exit notification. "First time only" so we
|
|
78 don't select the same process multiple times if someone turns off and on
|
|
79 the receipt of process data. */
|
|
80
|
|
81 HANDLE
|
|
82 get_nt_process_handle_only_first_time (Lisp_Process *p)
|
|
83 {
|
|
84 if (NT_DATA (p)->selected_for_exit_notify)
|
|
85 return INVALID_HANDLE_VALUE;
|
|
86 NT_DATA (p)->selected_for_exit_notify = 1;
|
|
87 return (NT_DATA (p)->h_process);
|
|
88 }
|
|
89
|
428
|
90 HANDLE
|
440
|
91 get_nt_process_handle (Lisp_Process *p)
|
428
|
92 {
|
|
93 return (NT_DATA (p)->h_process);
|
|
94 }
|
442
|
95
|
|
96 static struct Lisp_Process *
|
|
97 find_process_from_pid (DWORD pid)
|
|
98 {
|
|
99 Lisp_Object tail, proc;
|
|
100
|
|
101 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
|
|
102 {
|
|
103 proc = XCAR (tail);
|
|
104 if (NT_DATA (XPROCESS (proc))->dwProcessId == pid)
|
|
105 return XPROCESS (proc);
|
|
106 }
|
|
107 return 0;
|
|
108 }
|
|
109
|
428
|
110
|
|
111 /*-----------------------------------------------------------------------*/
|
|
112 /* Running remote threads. See Microsoft Systems Journal 1994 Number 5 */
|
|
113 /* Jeffrey Richter, Load Your 32-bit DLL into Another Process's Address..*/
|
|
114 /*-----------------------------------------------------------------------*/
|
|
115
|
|
116 typedef struct
|
|
117 {
|
|
118 HANDLE h_process;
|
|
119 HANDLE h_thread;
|
|
120 LPVOID address;
|
|
121 } process_memory;
|
|
122
|
|
123 /*
|
|
124 * Allocate SIZE bytes in H_PROCESS address space. Fill in PMC used
|
|
125 * further by other routines. Return nonzero if successful.
|
|
126 *
|
|
127 * The memory in other process is allocated by creating a suspended
|
|
128 * thread. Initial stack of that thread is used as the memory
|
|
129 * block. The thread entry point is the routine ExitThread in
|
854
|
130 * kernel32.dll, so the allocated memory is freed just by resuming the
|
428
|
131 * thread, which immediately terminates after that.
|
|
132 */
|
|
133
|
854
|
134 static int
|
665
|
135 alloc_process_memory (HANDLE h_process, Bytecount size,
|
771
|
136 process_memory *pmc)
|
428
|
137 {
|
|
138 LPTHREAD_START_ROUTINE adr_ExitThread =
|
|
139 (LPTHREAD_START_ROUTINE)
|
771
|
140 GetProcAddress (qxeGetModuleHandle (XETEXT ("kernel32")), "ExitThread");
|
428
|
141 DWORD dw_unused;
|
|
142 CONTEXT context;
|
|
143 MEMORY_BASIC_INFORMATION mbi;
|
|
144
|
|
145 pmc->h_process = h_process;
|
|
146 pmc->h_thread = CreateRemoteThread (h_process, NULL, size,
|
771
|
147 adr_ExitThread, NULL,
|
|
148 CREATE_SUSPENDED, &dw_unused);
|
428
|
149 if (pmc->h_thread == NULL)
|
|
150 return 0;
|
|
151
|
|
152 /* Get context, for thread's stack pointer */
|
|
153 context.ContextFlags = CONTEXT_CONTROL;
|
|
154 if (!GetThreadContext (pmc->h_thread, &context))
|
|
155 goto failure;
|
|
156
|
|
157 /* Determine base address of the committed range */
|
|
158 if (sizeof(mbi) != VirtualQueryEx (h_process,
|
|
159 #if defined (_X86_)
|
|
160 (LPDWORD)context.Esp - 1,
|
|
161 #elif defined (_ALPHA_)
|
|
162 (LPDWORD)context.IntSp - 1,
|
|
163 #else
|
|
164 #error Unknown processor architecture
|
|
165 #endif
|
|
166 &mbi, sizeof(mbi)))
|
|
167 goto failure;
|
|
168
|
|
169 /* Change the page protection of the allocated memory to executable,
|
|
170 read, and write. */
|
|
171 if (!VirtualProtectEx (h_process, mbi.BaseAddress, size,
|
|
172 PAGE_EXECUTE_READWRITE, &dw_unused))
|
|
173 goto failure;
|
|
174
|
|
175 pmc->address = mbi.BaseAddress;
|
|
176 return 1;
|
|
177
|
|
178 failure:
|
|
179 ResumeThread (pmc->h_thread);
|
|
180 pmc->address = 0;
|
|
181 return 0;
|
|
182 }
|
|
183
|
|
184 static void
|
771
|
185 free_process_memory (process_memory *pmc)
|
428
|
186 {
|
|
187 ResumeThread (pmc->h_thread);
|
|
188 }
|
|
189
|
|
190 /*
|
|
191 * Run ROUTINE in the context of process determined by H_PROCESS. The
|
|
192 * routine is passed the address of DATA as parameter. The ROUTINE must
|
|
193 * not be longer than ROUTINE_CODE_SIZE bytes. DATA_SIZE is the size of
|
|
194 * DATA structure.
|
|
195 *
|
|
196 * Note that the code must be positionally independent, and compiled
|
|
197 * without stack checks (they cause implicit calls into CRT so will
|
|
198 * fail). DATA should not refer any data in calling process, as both
|
|
199 * routine and its data are copied into remote process. Size of data
|
|
200 * and code together should not exceed one page (4K on x86 systems).
|
|
201 *
|
|
202 * Return the value returned by ROUTINE, or (DWORD)-1 if call failed.
|
|
203 */
|
|
204 static DWORD
|
|
205 run_in_other_process (HANDLE h_process,
|
|
206 LPTHREAD_START_ROUTINE routine,
|
665
|
207 LPVOID data, Bytecount data_size)
|
428
|
208 {
|
|
209 process_memory pm;
|
665
|
210 const Bytecount code_size = FRAGMENT_CODE_SIZE;
|
428
|
211 /* Need at most 3 extra bytes of memory, for data alignment */
|
665
|
212 Bytecount total_size = code_size + data_size + 3;
|
428
|
213 LPVOID remote_data;
|
|
214 HANDLE h_thread;
|
|
215 DWORD dw_unused;
|
|
216
|
|
217 /* Allocate memory */
|
|
218 if (!alloc_process_memory (h_process, total_size, &pm))
|
|
219 return (DWORD)-1;
|
|
220
|
|
221 /* Copy code */
|
|
222 if (!WriteProcessMemory (h_process, pm.address, (LPVOID)routine,
|
|
223 code_size, NULL))
|
|
224 goto failure;
|
|
225
|
|
226 /* Copy data */
|
|
227 if (data_size)
|
|
228 {
|
|
229 remote_data = (LPBYTE)pm.address + ((code_size + 4) & ~3);
|
|
230 if (!WriteProcessMemory (h_process, remote_data, data, data_size, NULL))
|
|
231 goto failure;
|
|
232 }
|
|
233 else
|
|
234 remote_data = NULL;
|
|
235
|
|
236 /* Execute the remote copy of code, passing it remote data */
|
|
237 h_thread = CreateRemoteThread (h_process, NULL, 0,
|
|
238 (LPTHREAD_START_ROUTINE) pm.address,
|
|
239 remote_data, 0, &dw_unused);
|
|
240 if (h_thread == NULL)
|
|
241 goto failure;
|
|
242
|
|
243 /* Wait till thread finishes */
|
|
244 WaitForSingleObject (h_thread, INFINITE);
|
|
245
|
|
246 /* Free remote memory */
|
|
247 free_process_memory (&pm);
|
|
248
|
|
249 /* Return thread's exit code */
|
|
250 {
|
|
251 DWORD exit_code;
|
|
252 GetExitCodeThread (h_thread, &exit_code);
|
|
253 CloseHandle (h_thread);
|
|
254 return exit_code;
|
|
255 }
|
|
256
|
|
257 failure:
|
|
258 free_process_memory (&pm);
|
|
259 return (DWORD)-1;
|
|
260 }
|
|
261
|
|
262 /*-----------------------------------------------------------------------*/
|
|
263 /* Sending signals */
|
|
264 /*-----------------------------------------------------------------------*/
|
|
265
|
442
|
266 /* ---------------------------- the NT way ------------------------------- */
|
|
267
|
428
|
268 /*
|
|
269 * We handle the following signals:
|
|
270 *
|
|
271 * SIGKILL, SIGTERM, SIGQUIT, SIGHUP - These four translate to ExitProcess
|
|
272 * executed by the remote process
|
|
273 * SIGINT - The remote process is sent CTRL_BREAK_EVENT
|
|
274 *
|
|
275 * The MSVC5.0 compiler feels free to re-order functions within a
|
|
276 * compilation unit, so we have no way of finding out the size of the
|
|
277 * following functions. Therefore these functions must not be larger than
|
|
278 * FRAGMENT_CODE_SIZE.
|
|
279 */
|
|
280
|
|
281 /*
|
|
282 * Sending SIGKILL
|
|
283 */
|
|
284 typedef struct
|
|
285 {
|
|
286 void (WINAPI *adr_ExitProcess) (UINT);
|
|
287 } sigkill_data;
|
|
288
|
|
289 static DWORD WINAPI
|
771
|
290 sigkill_proc (sigkill_data *data)
|
428
|
291 {
|
|
292 (*data->adr_ExitProcess)(255);
|
|
293 return 1;
|
|
294 }
|
|
295
|
|
296 /*
|
|
297 * Sending break or control c
|
|
298 */
|
|
299 typedef struct
|
|
300 {
|
|
301 BOOL (WINAPI *adr_GenerateConsoleCtrlEvent) (DWORD, DWORD);
|
|
302 DWORD event;
|
|
303 } sigint_data;
|
|
304
|
|
305 static DWORD WINAPI
|
771
|
306 sigint_proc (sigint_data *data)
|
428
|
307 {
|
|
308 return (*data->adr_GenerateConsoleCtrlEvent) (data->event, 0);
|
|
309 }
|
|
310
|
|
311 /*
|
|
312 * Enabling signals
|
|
313 */
|
|
314 typedef struct
|
|
315 {
|
|
316 BOOL (WINAPI *adr_SetConsoleCtrlHandler) (LPVOID, BOOL);
|
|
317 } sig_enable_data;
|
|
318
|
|
319 static DWORD WINAPI
|
771
|
320 sig_enable_proc (sig_enable_data *data)
|
428
|
321 {
|
|
322 (*data->adr_SetConsoleCtrlHandler) (NULL, FALSE);
|
|
323 return 1;
|
|
324 }
|
|
325
|
|
326 /*
|
|
327 * Send signal SIGNO to process H_PROCESS.
|
|
328 * Return nonzero if successful.
|
|
329 */
|
|
330
|
|
331 static int
|
442
|
332 send_signal_the_nt_way (struct nt_process_data *cp, int pid, int signo)
|
428
|
333 {
|
442
|
334 HANDLE h_process;
|
771
|
335 HMODULE h_kernel = qxeGetModuleHandle (XETEXT ("kernel32"));
|
442
|
336 int close_process = 0;
|
428
|
337 DWORD retval;
|
854
|
338
|
428
|
339 assert (h_kernel != NULL);
|
854
|
340
|
442
|
341 if (cp)
|
|
342 {
|
|
343 pid = cp->dwProcessId;
|
|
344 h_process = cp->h_process;
|
|
345 }
|
|
346 else
|
|
347 {
|
|
348 close_process = 1;
|
|
349 /* Try to open the process with required privileges */
|
|
350 h_process = OpenProcess (PROCESS_CREATE_THREAD
|
854
|
351 | PROCESS_QUERY_INFORMATION
|
442
|
352 | PROCESS_VM_OPERATION
|
|
353 | PROCESS_VM_WRITE,
|
|
354 FALSE, pid);
|
|
355 if (!h_process)
|
|
356 return 0;
|
|
357 }
|
|
358
|
428
|
359 switch (signo)
|
|
360 {
|
|
361 case SIGKILL:
|
|
362 case SIGTERM:
|
|
363 case SIGQUIT:
|
|
364 case SIGHUP:
|
|
365 {
|
|
366 sigkill_data d;
|
442
|
367
|
|
368 d.adr_ExitProcess =
|
|
369 (void (WINAPI *) (UINT)) GetProcAddress (h_kernel, "ExitProcess");
|
428
|
370 assert (d.adr_ExitProcess);
|
854
|
371 retval = run_in_other_process (h_process,
|
771
|
372 (LPTHREAD_START_ROUTINE) sigkill_proc,
|
428
|
373 &d, sizeof (d));
|
|
374 break;
|
|
375 }
|
|
376 case SIGINT:
|
|
377 {
|
|
378 sigint_data d;
|
|
379 d.adr_GenerateConsoleCtrlEvent =
|
442
|
380 (BOOL (WINAPI *) (DWORD, DWORD))
|
428
|
381 GetProcAddress (h_kernel, "GenerateConsoleCtrlEvent");
|
|
382 assert (d.adr_GenerateConsoleCtrlEvent);
|
|
383 d.event = CTRL_C_EVENT;
|
854
|
384 retval = run_in_other_process (h_process,
|
771
|
385 (LPTHREAD_START_ROUTINE) sigint_proc,
|
428
|
386 &d, sizeof (d));
|
|
387 break;
|
|
388 }
|
|
389 default:
|
|
390 assert (0);
|
|
391 }
|
|
392
|
442
|
393 if (close_process)
|
|
394 CloseHandle (h_process);
|
428
|
395 return (int)retval > 0 ? 1 : 0;
|
|
396 }
|
|
397
|
|
398 /*
|
|
399 * Enable CTRL_C_EVENT handling in a new child process
|
|
400 */
|
|
401 static void
|
|
402 enable_child_signals (HANDLE h_process)
|
|
403 {
|
771
|
404 HMODULE h_kernel = qxeGetModuleHandle (XETEXT ("kernel32"));
|
428
|
405 sig_enable_data d;
|
854
|
406
|
428
|
407 assert (h_kernel != NULL);
|
|
408 d.adr_SetConsoleCtrlHandler =
|
442
|
409 (BOOL (WINAPI *) (LPVOID, BOOL))
|
428
|
410 GetProcAddress (h_kernel, "SetConsoleCtrlHandler");
|
|
411 assert (d.adr_SetConsoleCtrlHandler);
|
|
412 run_in_other_process (h_process, (LPTHREAD_START_ROUTINE)sig_enable_proc,
|
|
413 &d, sizeof (d));
|
|
414 }
|
854
|
415
|
442
|
416 /* ---------------------------- the 95 way ------------------------------- */
|
|
417
|
|
418 static BOOL CALLBACK
|
|
419 find_child_console (HWND hwnd, long putada)
|
|
420 {
|
|
421 DWORD thread_id;
|
|
422 DWORD process_id;
|
|
423 struct nt_process_data *cp = (struct nt_process_data *) putada;
|
|
424
|
|
425 thread_id = GetWindowThreadProcessId (hwnd, &process_id);
|
|
426 if (process_id == cp->dwProcessId)
|
|
427 {
|
771
|
428 Extbyte window_class[32];
|
442
|
429
|
771
|
430 /* GetClassNameA to avoid problems with Unicode return values */
|
|
431 GetClassNameA (hwnd, window_class, sizeof (window_class));
|
442
|
432 if (strcmp (window_class,
|
771
|
433 mswindows_windows9x_p
|
442
|
434 ? "tty"
|
|
435 : "ConsoleWindowClass") == 0)
|
|
436 {
|
|
437 cp->hwnd = hwnd;
|
|
438 return FALSE;
|
|
439 }
|
|
440 }
|
|
441 /* keep looking */
|
|
442 return TRUE;
|
|
443 }
|
|
444
|
|
445 static int
|
|
446 send_signal_the_95_way (struct nt_process_data *cp, int pid, int signo)
|
|
447 {
|
|
448 HANDLE h_process;
|
|
449 int close_process = 0;
|
|
450 int rc = 1;
|
854
|
451
|
442
|
452 if (cp)
|
|
453 {
|
|
454 pid = cp->dwProcessId;
|
|
455 h_process = cp->h_process;
|
|
456
|
|
457 /* Try to locate console window for process. */
|
|
458 EnumWindows (find_child_console, (LPARAM) cp);
|
|
459 }
|
|
460 else
|
|
461 {
|
|
462 close_process = 1;
|
|
463 /* Try to open the process with required privileges */
|
|
464 h_process = OpenProcess (PROCESS_TERMINATE, FALSE, pid);
|
|
465 if (!h_process)
|
|
466 return 0;
|
|
467 }
|
854
|
468
|
442
|
469 if (signo == SIGINT)
|
|
470 {
|
|
471 if (NILP (Vmswindows_start_process_share_console) && cp && cp->hwnd)
|
|
472 {
|
771
|
473 BYTE control_scan_code = (BYTE) MapVirtualKeyA (VK_CONTROL, 0);
|
442
|
474 BYTE vk_break_code = VK_CANCEL;
|
771
|
475 BYTE break_scan_code = (BYTE) MapVirtualKeyA (vk_break_code, 0);
|
442
|
476 HWND foreground_window;
|
|
477
|
|
478 if (break_scan_code == 0)
|
|
479 {
|
|
480 /* Fake Ctrl-C if we can't manage Ctrl-Break. */
|
|
481 vk_break_code = 'C';
|
771
|
482 break_scan_code = (BYTE) MapVirtualKeyA (vk_break_code, 0);
|
442
|
483 }
|
|
484
|
|
485 foreground_window = GetForegroundWindow ();
|
|
486 if (foreground_window)
|
|
487 {
|
|
488 /* NT 5.0, and apparently also Windows 98, will not allow
|
|
489 a Window to be set to foreground directly without the
|
|
490 user's involvement. The workaround is to attach
|
|
491 ourselves to the thread that owns the foreground
|
|
492 window, since that is the only thread that can set the
|
|
493 foreground window. */
|
|
494 DWORD foreground_thread, child_thread;
|
|
495 foreground_thread =
|
|
496 GetWindowThreadProcessId (foreground_window, NULL);
|
|
497 if (foreground_thread == GetCurrentThreadId ()
|
|
498 || !AttachThreadInput (GetCurrentThreadId (),
|
|
499 foreground_thread, TRUE))
|
|
500 foreground_thread = 0;
|
|
501
|
|
502 child_thread = GetWindowThreadProcessId (cp->hwnd, NULL);
|
|
503 if (child_thread == GetCurrentThreadId ()
|
|
504 || !AttachThreadInput (GetCurrentThreadId (),
|
|
505 child_thread, TRUE))
|
|
506 child_thread = 0;
|
|
507
|
|
508 /* Set the foreground window to the child. */
|
|
509 if (SetForegroundWindow (cp->hwnd))
|
|
510 {
|
|
511 /* Generate keystrokes as if user had typed Ctrl-Break or
|
|
512 Ctrl-C. */
|
|
513 keybd_event (VK_CONTROL, control_scan_code, 0, 0);
|
|
514 keybd_event (vk_break_code, break_scan_code,
|
|
515 (vk_break_code == 'C' ? 0 : KEYEVENTF_EXTENDEDKEY), 0);
|
|
516 keybd_event (vk_break_code, break_scan_code,
|
|
517 (vk_break_code == 'C' ? 0 : KEYEVENTF_EXTENDEDKEY)
|
|
518 | KEYEVENTF_KEYUP, 0);
|
|
519 keybd_event (VK_CONTROL, control_scan_code,
|
|
520 KEYEVENTF_KEYUP, 0);
|
|
521
|
|
522 /* Sleep for a bit to give time for Emacs frame to respond
|
|
523 to focus change events (if Emacs was active app). */
|
|
524 Sleep (100);
|
|
525
|
|
526 SetForegroundWindow (foreground_window);
|
|
527 }
|
|
528 /* Detach from the foreground and child threads now that
|
|
529 the foreground switching is over. */
|
|
530 if (foreground_thread)
|
|
531 AttachThreadInput (GetCurrentThreadId (),
|
|
532 foreground_thread, FALSE);
|
|
533 if (child_thread)
|
|
534 AttachThreadInput (GetCurrentThreadId (),
|
|
535 child_thread, FALSE);
|
|
536 }
|
|
537 }
|
|
538 /* Ctrl-Break is NT equivalent of SIGINT. */
|
|
539 else if (!GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, pid))
|
|
540 {
|
|
541 #if 0 /* FSF Emacs */
|
|
542 DebPrint (("sys_kill.GenerateConsoleCtrlEvent return %d "
|
|
543 "for pid %lu\n", GetLastError (), pid));
|
|
544 errno = EINVAL;
|
|
545 #endif
|
|
546 rc = 0;
|
|
547 }
|
|
548 }
|
|
549 else
|
|
550 {
|
|
551 if (NILP (Vmswindows_start_process_share_console) && cp && cp->hwnd)
|
|
552 {
|
|
553 #if 1
|
771
|
554 if (mswindows_windows9x_p)
|
442
|
555 {
|
|
556 /*
|
|
557 Another possibility is to try terminating the VDM out-right by
|
|
558 calling the Shell VxD (id 0x17) V86 interface, function #4
|
|
559 "SHELL_Destroy_VM", ie.
|
|
560
|
|
561 mov edx,4
|
|
562 mov ebx,vm_handle
|
|
563 call shellapi
|
|
564
|
|
565 First need to determine the current VM handle, and then arrange for
|
|
566 the shellapi call to be made from the system vm (by using
|
|
567 Switch_VM_and_callback).
|
|
568
|
|
569 Could try to invoke DestroyVM through CallVxD.
|
|
570
|
|
571 */
|
|
572 #if 0
|
|
573 /* On Win95, posting WM_QUIT causes the 16-bit subsystem
|
|
574 to hang when cmdproxy is used in conjunction with
|
|
575 command.com for an interactive shell. Posting
|
|
576 WM_CLOSE pops up a dialog that, when Yes is selected,
|
|
577 does the same thing. TerminateProcess is also less
|
|
578 than ideal in that subprocesses tend to stick around
|
|
579 until the machine is shutdown, but at least it
|
|
580 doesn't freeze the 16-bit subsystem. */
|
800
|
581 qxePostMessage (cp->hwnd, WM_QUIT, 0xff, 0);
|
442
|
582 #endif
|
|
583 if (!TerminateProcess (h_process, 0xff))
|
|
584 {
|
|
585 #if 0 /* FSF Emacs */
|
|
586 DebPrint (("sys_kill.TerminateProcess returned %d "
|
|
587 "for pid %lu\n", GetLastError (), pid));
|
|
588 errno = EINVAL;
|
|
589 #endif
|
|
590 rc = 0;
|
|
591 }
|
|
592 }
|
|
593 else
|
|
594 #endif
|
800
|
595 qxePostMessage (cp->hwnd, WM_CLOSE, 0, 0);
|
442
|
596 }
|
|
597 /* Kill the process. On W32 this doesn't kill child processes
|
|
598 so it doesn't work very well for shells which is why it's not
|
|
599 used in every case. */
|
|
600 else if (!TerminateProcess (h_process, 0xff))
|
|
601 {
|
|
602 #if 0 /* FSF Emacs */
|
|
603 DebPrint (("sys_kill.TerminateProcess returned %d "
|
|
604 "for pid %lu\n", GetLastError (), pid));
|
|
605 errno = EINVAL;
|
|
606 #endif
|
|
607 rc = 0;
|
|
608 }
|
|
609 }
|
|
610
|
|
611 if (close_process)
|
|
612 CloseHandle (h_process);
|
|
613
|
|
614 return rc;
|
|
615 }
|
|
616
|
|
617 /* -------------------------- all-OS functions ---------------------------- */
|
|
618
|
|
619 static int
|
|
620 send_signal (struct nt_process_data *cp, int pid, int signo)
|
|
621 {
|
771
|
622 return (!mswindows_windows9x_p && send_signal_the_nt_way (cp, pid, signo))
|
442
|
623 || send_signal_the_95_way (cp, pid, signo);
|
|
624 }
|
|
625
|
428
|
626 /*
|
|
627 * Signal error if SIGNO is not supported
|
|
628 */
|
|
629 static void
|
|
630 validate_signal_number (int signo)
|
|
631 {
|
|
632 if (signo != SIGKILL && signo != SIGTERM
|
|
633 && signo != SIGQUIT && signo != SIGINT
|
|
634 && signo != SIGHUP)
|
563
|
635 invalid_constant ("Signal number not supported", make_int (signo));
|
428
|
636 }
|
854
|
637
|
428
|
638 /*-----------------------------------------------------------------------*/
|
|
639 /* Process methods */
|
|
640 /*-----------------------------------------------------------------------*/
|
|
641
|
|
642 /*
|
|
643 * Allocate and initialize Lisp_Process->process_data
|
|
644 */
|
|
645
|
|
646 static void
|
440
|
647 nt_alloc_process_data (Lisp_Process *p)
|
428
|
648 {
|
|
649 p->process_data = xnew_and_zero (struct nt_process_data);
|
|
650 }
|
|
651
|
|
652 static void
|
440
|
653 nt_finalize_process_data (Lisp_Process *p, int for_disksave)
|
428
|
654 {
|
|
655 assert (!for_disksave);
|
791
|
656 /* If it's still in the list of processes we are waiting on delete
|
|
657 it. */
|
|
658 mswindows_unwait_process (p);
|
442
|
659 if (NT_DATA (p)->h_process)
|
|
660 CloseHandle (NT_DATA (p)->h_process);
|
428
|
661 }
|
|
662
|
|
663 /*
|
|
664 * Initialize XEmacs process implementation once
|
|
665 */
|
|
666 static void
|
|
667 nt_init_process (void)
|
|
668 {
|
|
669 /* Initialize winsock */
|
|
670 WSADATA wsa_data;
|
|
671 /* Request Winsock v1.1 Note the order: (minor=1, major=1) */
|
|
672 WSAStartup (MAKEWORD (1,1), &wsa_data);
|
|
673 }
|
|
674
|
853
|
675 /*
|
|
676 * Fork off a subprocess. P is a pointer to newly created subprocess
|
|
677 * object. If this function signals, the caller is responsible for
|
|
678 * deleting (and finalizing) the process object.
|
|
679 *
|
|
680 * The method must return PID of the new process, a (positive??? ####) number
|
|
681 * which fits into Lisp_Int. No return value indicates an error, the method
|
|
682 * must signal an error instead.
|
|
683 */
|
|
684
|
563
|
685 static DOESNT_RETURN
|
|
686 mswindows_report_winsock_error (const char *string, Lisp_Object data,
|
|
687 int errnum)
|
428
|
688 {
|
563
|
689 report_file_type_error (Qnetwork_error, mswindows_lisp_error (errnum),
|
|
690 string, data);
|
442
|
691 }
|
|
692
|
|
693 static void
|
|
694 ensure_console_window_exists (void)
|
|
695 {
|
771
|
696 if (mswindows_windows9x_p)
|
442
|
697 mswindows_hide_console ();
|
|
698 }
|
|
699
|
|
700 int
|
771
|
701 mswindows_compare_env (const void *strp1, const void *strp2)
|
442
|
702 {
|
867
|
703 const Ibyte *str1 = *(const Ibyte **)strp1,
|
|
704 *str2 = *(const Ibyte **)strp2;
|
442
|
705
|
|
706 while (*str1 && *str2 && *str1 != '=' && *str2 != '=')
|
|
707 {
|
|
708 if ((*str1) > (*str2))
|
|
709 return 1;
|
|
710 else if ((*str1) < (*str2))
|
|
711 return -1;
|
|
712 str1++, str2++;
|
|
713 }
|
|
714
|
|
715 if (*str1 == '=' && *str2 == '=')
|
|
716 return 0;
|
|
717 else if (*str1 == '=')
|
|
718 return -1;
|
|
719 else
|
|
720 return 1;
|
428
|
721 }
|
|
722
|
563
|
723 /*
|
|
724 * Fork off a subprocess. P is a pointer to newly created subprocess
|
|
725 * object. If this function signals, the caller is responsible for
|
|
726 * deleting (and finalizing) the process object.
|
|
727 *
|
|
728 * The method must return PID of the new process, a (positive??? ####) number
|
|
729 * which fits into Lisp_Int. No return value indicates an error, the method
|
|
730 * must signal an error instead.
|
|
731 */
|
|
732
|
428
|
733 static int
|
440
|
734 nt_create_process (Lisp_Process *p,
|
428
|
735 Lisp_Object *argv, int nargv,
|
853
|
736 Lisp_Object program, Lisp_Object cur_dir,
|
|
737 int separate_err)
|
428
|
738 {
|
442
|
739 /* Synched up with sys_spawnve in FSF 20.6. Significantly different
|
|
740 but still synchable. */
|
853
|
741 HANDLE hmyshove, hmyslurp, hmyslurp_err, hprocin, hprocout, hprocerr;
|
442
|
742 Extbyte *command_line;
|
428
|
743 BOOL do_io, windowed;
|
771
|
744 Extbyte *proc_env;
|
428
|
745
|
442
|
746 /* No need to DOS-ize the filename; expand-file-name (called prior)
|
|
747 already does this. */
|
|
748
|
428
|
749 /* Find out whether the application is windowed or not */
|
771
|
750 {
|
|
751 /* SHGetFileInfo tends to return ERROR_FILE_NOT_FOUND on most
|
|
752 errors. This leads to bogus error message. */
|
|
753 DWORD image_type;
|
1204
|
754 if (mswindows_is_executable (XSTRING_DATA (program)))
|
771
|
755 {
|
|
756 Extbyte *progext;
|
|
757 LISP_STRING_TO_TSTR (program, progext);
|
|
758 image_type = qxeSHGetFileInfo (progext, 0, NULL, 0, SHGFI_EXETYPE);
|
|
759 }
|
|
760 else
|
|
761 {
|
|
762 DECLARE_EISTRING (progext);
|
|
763 eicpy_lstr (progext, program);
|
|
764 eicat_c (progext, ".exe");
|
|
765 eito_external (progext, Qmswindows_tstr);
|
|
766 image_type = qxeSHGetFileInfo (eiextdata (progext), 0, NULL, 0,
|
|
767 SHGFI_EXETYPE);
|
|
768 }
|
|
769 if (image_type == 0)
|
|
770 mswindows_report_process_error
|
|
771 ("Determining executable file type",
|
|
772 program,
|
|
773 GetLastError () == ERROR_FILE_NOT_FOUND
|
|
774 ? ERROR_BAD_FORMAT : GetLastError ());
|
|
775 windowed = HIWORD (image_type) != 0;
|
|
776 }
|
|
777
|
428
|
778
|
|
779 /* Decide whether to do I/O on process handles, or just mark the
|
|
780 process exited immediately upon successful launching. We do I/O if the
|
|
781 process is a console one, or if it is windowed but windowed_process_io
|
|
782 is non-zero */
|
|
783 do_io = !windowed || windowed_process_io ;
|
854
|
784
|
428
|
785 if (do_io)
|
|
786 {
|
|
787 /* Create two unidirectional named pipes */
|
|
788 HANDLE htmp;
|
|
789 SECURITY_ATTRIBUTES sa;
|
|
790
|
826
|
791 sa.nLength = sizeof (sa);
|
428
|
792 sa.bInheritHandle = TRUE;
|
|
793 sa.lpSecurityDescriptor = NULL;
|
|
794
|
|
795 CreatePipe (&hprocin, &hmyshove, &sa, 0);
|
|
796 CreatePipe (&hmyslurp, &hprocout, &sa, 0);
|
|
797
|
853
|
798 if (separate_err)
|
|
799 CreatePipe (&hmyslurp_err, &hprocerr, &sa, 0);
|
|
800 else
|
|
801 /* Duplicate the stdout handle for use as stderr */
|
|
802 DuplicateHandle(GetCurrentProcess(), hprocout, GetCurrentProcess(),
|
|
803 &hprocerr, 0, TRUE, DUPLICATE_SAME_ACCESS);
|
430
|
804
|
428
|
805 /* Stupid Win32 allows to create a pipe with *both* ends either
|
|
806 inheritable or not. We need process ends inheritable, and local
|
|
807 ends not inheritable. */
|
442
|
808 DuplicateHandle (GetCurrentProcess(), hmyshove, GetCurrentProcess(),
|
|
809 &htmp, 0, FALSE,
|
|
810 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
428
|
811 hmyshove = htmp;
|
442
|
812 DuplicateHandle (GetCurrentProcess(), hmyslurp, GetCurrentProcess(),
|
|
813 &htmp, 0, FALSE,
|
|
814 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
428
|
815 hmyslurp = htmp;
|
853
|
816 if (separate_err)
|
|
817 {
|
|
818 DuplicateHandle (GetCurrentProcess(), hmyslurp_err,
|
|
819 GetCurrentProcess(), &htmp, 0, FALSE,
|
|
820 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
|
821 hmyslurp_err = htmp;
|
|
822 }
|
428
|
823 }
|
|
824
|
|
825 /* Convert an argv vector into Win32 style command line by a call to
|
442
|
826 lisp function `mswindows-construct-process-command-line'
|
|
827 (in win32-native.el) */
|
428
|
828 {
|
|
829 int i;
|
|
830 Lisp_Object args_or_ret = Qnil;
|
|
831 struct gcpro gcpro1;
|
|
832
|
|
833 GCPRO1 (args_or_ret);
|
|
834
|
|
835 for (i = 0; i < nargv; ++i)
|
|
836 args_or_ret = Fcons (*argv++, args_or_ret);
|
|
837 args_or_ret = Fnreverse (args_or_ret);
|
|
838 args_or_ret = Fcons (program, args_or_ret);
|
|
839
|
442
|
840 args_or_ret = call1 (Qmswindows_construct_process_command_line,
|
|
841 args_or_ret);
|
428
|
842
|
|
843 if (!STRINGP (args_or_ret))
|
|
844 /* Luser wrote his/her own clever version */
|
442
|
845 invalid_argument
|
|
846 ("Bogus return value from `mswindows-construct-process-command-line'",
|
|
847 args_or_ret);
|
428
|
848
|
771
|
849 LISP_STRING_TO_TSTR (args_or_ret, command_line);
|
428
|
850
|
|
851 UNGCPRO; /* args_or_ret */
|
|
852 }
|
|
853
|
|
854 /* Set `proc_env' to a nul-separated array of the strings in
|
|
855 Vprocess_environment terminated by 2 nuls. */
|
771
|
856
|
428
|
857 {
|
867
|
858 Ibyte **env;
|
428
|
859 REGISTER Lisp_Object tem;
|
867
|
860 REGISTER Ibyte **new_env;
|
771
|
861 REGISTER int new_length = 0, i;
|
854
|
862
|
428
|
863 for (tem = Vprocess_environment;
|
|
864 (CONSP (tem)
|
|
865 && STRINGP (XCAR (tem)));
|
|
866 tem = XCDR (tem))
|
|
867 new_length++;
|
442
|
868
|
|
869 /* FSF adds an extra env var to hold the current process ID of the
|
|
870 Emacs process. Apparently this is used only by emacsserver.c,
|
771
|
871 which we have superseded by gnuserv.c. (#### Does it work under
|
442
|
872 MS Windows?)
|
|
873
|
854
|
874 sprintf (ppid_env_var_buffer, "EM_PARENT_PROCESS_ID=%d",
|
442
|
875 GetCurrentProcessId ());
|
|
876 arglen += strlen (ppid_env_var_buffer) + 1;
|
|
877 numenv++;
|
|
878 */
|
854
|
879
|
428
|
880 /* new_length + 1 to include terminating 0. */
|
867
|
881 env = new_env = alloca_array (Ibyte *, new_length + 1);
|
854
|
882
|
428
|
883 /* Copy the Vprocess_environment strings into new_env. */
|
|
884 for (tem = Vprocess_environment;
|
|
885 (CONSP (tem)
|
|
886 && STRINGP (XCAR (tem)));
|
|
887 tem = XCDR (tem))
|
|
888 {
|
867
|
889 Ibyte **ep = env;
|
|
890 Ibyte *string = XSTRING_DATA (XCAR (tem));
|
428
|
891 /* See if this string duplicates any string already in the env.
|
|
892 If so, don't put it in.
|
|
893 When an env var has multiple definitions,
|
|
894 we keep the definition that comes first in process-environment. */
|
|
895 for (; ep != new_env; ep++)
|
|
896 {
|
867
|
897 Ibyte *p = *ep, *q = string;
|
428
|
898 while (1)
|
|
899 {
|
|
900 if (*q == 0)
|
|
901 /* The string is malformed; might as well drop it. */
|
|
902 goto duplicate;
|
|
903 if (*q != *p)
|
|
904 break;
|
|
905 if (*q == '=')
|
|
906 goto duplicate;
|
|
907 p++, q++;
|
|
908 }
|
|
909 }
|
|
910 *new_env++ = string;
|
|
911 duplicate: ;
|
|
912 }
|
|
913 *new_env = 0;
|
854
|
914
|
428
|
915 /* Sort the environment variables */
|
|
916 new_length = new_env - env;
|
867
|
917 qsort (env, new_length, sizeof (Ibyte *), mswindows_compare_env);
|
771
|
918
|
|
919 {
|
|
920 DECLARE_EISTRING (envout);
|
|
921
|
|
922 for (i = 0; i < new_length; i++)
|
|
923 {
|
1204
|
924 eicat_raw (envout, env[i], qxestrlen (env[i]));
|
|
925 eicat_raw (envout, (Ibyte *) "\0", 1);
|
771
|
926 }
|
|
927
|
1204
|
928 eicat_raw (envout, (Ibyte *) "\0", 1);
|
771
|
929 eito_external (envout, Qmswindows_tstr);
|
|
930 proc_env = eiextdata (envout);
|
|
931 }
|
428
|
932 }
|
442
|
933
|
|
934 #if 0
|
|
935 /* #### we need to port this. */
|
|
936 /* On Windows 95, if cmdname is a DOS app, we invoke a helper
|
|
937 application to start it by specifying the helper app as cmdname,
|
|
938 while leaving the real app name as argv[0]. */
|
|
939 if (is_dos_app)
|
|
940 {
|
867
|
941 cmdname = (Ibyte *) ALLOCA (PATH_MAX);
|
442
|
942 if (egetenv ("CMDPROXY"))
|
771
|
943 qxestrcpy (cmdname, egetenv ("CMDPROXY"));
|
442
|
944 else
|
|
945 {
|
771
|
946 qxestrcpy (cmdname, XSTRING_DATA (Vinvocation_directory));
|
867
|
947 qxestrcat (cmdname, (Ibyte *) "cmdproxy.exe");
|
442
|
948 }
|
|
949 }
|
|
950 #endif
|
854
|
951
|
428
|
952 /* Create process */
|
|
953 {
|
771
|
954 STARTUPINFOW si;
|
428
|
955 PROCESS_INFORMATION pi;
|
|
956 DWORD err;
|
442
|
957 DWORD flags;
|
428
|
958
|
|
959 xzero (si);
|
|
960 si.dwFlags = STARTF_USESHOWWINDOW;
|
|
961 si.wShowWindow = windowed ? SW_SHOWNORMAL : SW_HIDE;
|
|
962 if (do_io)
|
|
963 {
|
|
964 si.hStdInput = hprocin;
|
|
965 si.hStdOutput = hprocout;
|
430
|
966 si.hStdError = hprocerr;
|
428
|
967 si.dwFlags |= STARTF_USESTDHANDLES;
|
|
968 }
|
|
969
|
442
|
970 flags = CREATE_SUSPENDED;
|
771
|
971 if (mswindows_windows9x_p)
|
442
|
972 flags |= (!NILP (Vmswindows_start_process_share_console)
|
|
973 ? CREATE_NEW_PROCESS_GROUP
|
|
974 : CREATE_NEW_CONSOLE);
|
|
975 else
|
|
976 flags |= CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP;
|
|
977 if (NILP (Vmswindows_start_process_inherit_error_mode))
|
|
978 flags |= CREATE_DEFAULT_ERROR_MODE;
|
|
979
|
|
980 ensure_console_window_exists ();
|
|
981
|
771
|
982 {
|
|
983 Extbyte *curdirext;
|
|
984
|
|
985 LISP_STRING_TO_TSTR (cur_dir, curdirext);
|
|
986
|
|
987 err = (qxeCreateProcess (NULL, command_line, NULL, NULL, TRUE,
|
|
988 (XEUNICODE_P ?
|
|
989 flags | CREATE_UNICODE_ENVIRONMENT :
|
|
990 flags), proc_env,
|
|
991 curdirext, &si, &pi)
|
|
992 ? 0 : GetLastError ());
|
|
993 }
|
428
|
994
|
|
995 if (do_io)
|
|
996 {
|
|
997 /* These just have been inherited; we do not need a copy */
|
|
998 CloseHandle (hprocin);
|
|
999 CloseHandle (hprocout);
|
430
|
1000 CloseHandle (hprocerr);
|
428
|
1001 }
|
854
|
1002
|
428
|
1003 /* Handle process creation failure */
|
|
1004 if (err)
|
|
1005 {
|
|
1006 if (do_io)
|
|
1007 {
|
|
1008 CloseHandle (hmyshove);
|
|
1009 CloseHandle (hmyslurp);
|
853
|
1010 if (separate_err)
|
|
1011 CloseHandle (hmyslurp_err);
|
428
|
1012 }
|
563
|
1013 mswindows_report_process_error
|
|
1014 ("Error starting",
|
|
1015 program, GetLastError ());
|
428
|
1016 }
|
|
1017
|
|
1018 /* The process started successfully */
|
|
1019 if (do_io)
|
|
1020 {
|
|
1021 NT_DATA(p)->h_process = pi.hProcess;
|
442
|
1022 NT_DATA(p)->dwProcessId = pi.dwProcessId;
|
853
|
1023 init_process_io_handles (p, (void *) hmyslurp, (void *) hmyshove,
|
|
1024 separate_err ? (void *) hmyslurp_err
|
|
1025 : (void *) -1, 0);
|
428
|
1026 }
|
|
1027 else
|
|
1028 {
|
|
1029 /* Indicate as if the process has exited immediately. */
|
|
1030 p->status_symbol = Qexit;
|
|
1031 CloseHandle (pi.hProcess);
|
|
1032 }
|
|
1033
|
442
|
1034 if (!windowed)
|
|
1035 enable_child_signals (pi.hProcess);
|
|
1036
|
428
|
1037 ResumeThread (pi.hThread);
|
|
1038 CloseHandle (pi.hThread);
|
|
1039
|
432
|
1040 return ((int)pi.dwProcessId);
|
428
|
1041 }
|
|
1042 }
|
|
1043
|
854
|
1044 /*
|
428
|
1045 * This method is called to update status fields of the process
|
|
1046 * structure. If the process has not existed, this method is expected
|
|
1047 * to do nothing.
|
|
1048 *
|
854
|
1049 * The method is called only for real child processes.
|
428
|
1050 */
|
|
1051
|
|
1052 static void
|
771
|
1053 nt_update_status_if_terminated (Lisp_Process *p)
|
428
|
1054 {
|
|
1055 DWORD exit_code;
|
|
1056 if (GetExitCodeProcess (NT_DATA(p)->h_process, &exit_code)
|
|
1057 && exit_code != STILL_ACTIVE)
|
|
1058 {
|
|
1059 p->tick++;
|
|
1060 p->core_dumped = 0;
|
|
1061 /* The exit code can be a code returned by process, or an
|
|
1062 NTSTATUS value. We cannot accurately handle the latter since
|
|
1063 it is a full 32 bit integer */
|
|
1064 if (exit_code & 0xC0000000)
|
|
1065 {
|
|
1066 p->status_symbol = Qsignal;
|
|
1067 p->exit_code = exit_code & 0x1FFFFFFF;
|
|
1068 }
|
|
1069 else
|
|
1070 {
|
|
1071 p->status_symbol = Qexit;
|
|
1072 p->exit_code = exit_code;
|
|
1073 }
|
|
1074 }
|
|
1075 }
|
|
1076
|
|
1077 /*
|
|
1078 * Stuff the entire contents of LSTREAM to the process output pipe
|
|
1079 */
|
|
1080
|
|
1081 /* #### If only this function could be somehow merged with
|
|
1082 unix_send_process... */
|
|
1083
|
|
1084 static void
|
771
|
1085 nt_send_process (Lisp_Object proc, struct lstream *lstream)
|
428
|
1086 {
|
432
|
1087 volatile Lisp_Object vol_proc = proc;
|
440
|
1088 Lisp_Process *volatile p = XPROCESS (proc);
|
428
|
1089
|
|
1090 /* use a reasonable-sized buffer (somewhere around the size of the
|
|
1091 stream buffer) so as to avoid inundating the stream with blocked
|
|
1092 data. */
|
867
|
1093 Ibyte chunkbuf[512];
|
428
|
1094 Bytecount chunklen;
|
|
1095
|
|
1096 while (1)
|
|
1097 {
|
771
|
1098 int writeret;
|
428
|
1099
|
442
|
1100 chunklen = Lstream_read (lstream, chunkbuf, 512);
|
428
|
1101 if (chunklen <= 0)
|
|
1102 break; /* perhaps should abort() if < 0?
|
|
1103 This should never happen. */
|
|
1104
|
|
1105 /* Lstream_write() will never successfully write less than the
|
|
1106 amount sent in. In the worst case, it just buffers the
|
|
1107 unwritten data. */
|
771
|
1108 writeret = Lstream_write (XLSTREAM (DATA_OUTSTREAM (p)), chunkbuf,
|
428
|
1109 chunklen);
|
771
|
1110 Lstream_flush (XLSTREAM (DATA_OUTSTREAM (p)));
|
428
|
1111 if (writeret < 0)
|
|
1112 {
|
|
1113 p->status_symbol = Qexit;
|
|
1114 p->exit_code = ERROR_BROKEN_PIPE;
|
|
1115 p->core_dumped = 0;
|
|
1116 p->tick++;
|
|
1117 process_tick++;
|
432
|
1118 deactivate_process (*((Lisp_Object *) (&vol_proc)));
|
442
|
1119 invalid_operation ("Broken pipe error sending to process; closed it",
|
|
1120 p->name);
|
428
|
1121 }
|
|
1122
|
|
1123 {
|
|
1124 int wait_ms = 25;
|
|
1125 while (Lstream_was_blocked_p (XLSTREAM (p->pipe_outstream)))
|
|
1126 {
|
|
1127 /* Buffer is full. Wait, accepting input; that may allow
|
|
1128 the program to finish doing output and read more. */
|
|
1129 Faccept_process_output (Qnil, Qzero, make_int (wait_ms));
|
|
1130 Lstream_flush (XLSTREAM (p->pipe_outstream));
|
|
1131 wait_ms = min (1000, 2 * wait_ms);
|
|
1132 }
|
|
1133 }
|
|
1134 }
|
|
1135 }
|
|
1136
|
|
1137 /*
|
|
1138 * Send a signal number SIGNO to PROCESS.
|
|
1139 * CURRENT_GROUP means send to the process group that currently owns
|
|
1140 * the terminal being used to communicate with PROCESS.
|
|
1141 * This is used for various commands in shell mode.
|
|
1142 * If NOMSG is zero, insert signal-announcements into process's buffers
|
|
1143 * right away.
|
|
1144 *
|
|
1145 * If we can, we try to signal PROCESS by sending control characters
|
|
1146 * down the pty. This allows us to signal inferiors who have changed
|
|
1147 * their uid, for which killpg would return an EPERM error.
|
|
1148 *
|
|
1149 * The method signals an error if the given SIGNO is not valid
|
|
1150 */
|
|
1151
|
|
1152 static void
|
|
1153 nt_kill_child_process (Lisp_Object proc, int signo,
|
|
1154 int current_group, int nomsg)
|
|
1155 {
|
440
|
1156 Lisp_Process *p = XPROCESS (proc);
|
428
|
1157
|
|
1158 /* Signal error if SIGNO cannot be sent */
|
|
1159 validate_signal_number (signo);
|
|
1160
|
|
1161 /* Send signal */
|
442
|
1162 if (!send_signal (NT_DATA (p), 0, signo))
|
|
1163 invalid_operation ("Cannot send signal to process", proc);
|
428
|
1164 }
|
|
1165
|
|
1166 /*
|
442
|
1167 * Kill any process in the system given its PID
|
428
|
1168 *
|
|
1169 * Returns zero if a signal successfully sent, or
|
|
1170 * negative number upon failure
|
|
1171 */
|
|
1172 static int
|
|
1173 nt_kill_process_by_pid (int pid, int signo)
|
|
1174 {
|
442
|
1175 struct Lisp_Process *p;
|
|
1176
|
428
|
1177 /* Signal error if SIGNO cannot be sent */
|
|
1178 validate_signal_number (signo);
|
|
1179
|
442
|
1180 p = find_process_from_pid (pid);
|
|
1181 return send_signal (p ? NT_DATA (p) : 0, pid, signo) ? 0 : -1;
|
428
|
1182 }
|
|
1183
|
|
1184 /*-----------------------------------------------------------------------*/
|
|
1185 /* Sockets connections */
|
|
1186 /*-----------------------------------------------------------------------*/
|
|
1187 #ifdef HAVE_SOCKETS
|
|
1188
|
|
1189 /* #### Hey MS, how long Winsock 2 for '95 will be in beta? */
|
|
1190
|
|
1191 #define SOCK_TIMER_ID 666
|
|
1192 #define XM_SOCKREPLY (WM_USER + 666)
|
|
1193
|
563
|
1194 /* Return 0 for success, or error code */
|
|
1195
|
428
|
1196 static int
|
563
|
1197 get_internet_address (Lisp_Object host, struct sockaddr_in *address)
|
428
|
1198 {
|
771
|
1199 Char_Binary buf[MAXGETHOSTSTRUCT];
|
428
|
1200 HWND hwnd;
|
|
1201 HANDLE hasync;
|
563
|
1202 int errcode = 0;
|
428
|
1203
|
|
1204 address->sin_family = AF_INET;
|
|
1205
|
|
1206 /* First check if HOST is already a numeric address */
|
|
1207 {
|
1204
|
1208 Extbyte *hostext;
|
|
1209 unsigned long inaddr;
|
|
1210
|
|
1211 LISP_STRING_TO_EXTERNAL (host, hostext, Qmswindows_host_name_encoding);
|
|
1212 inaddr = inet_addr (hostext);
|
428
|
1213 if (inaddr != INADDR_NONE)
|
|
1214 {
|
|
1215 address->sin_addr.s_addr = inaddr;
|
563
|
1216 return 0;
|
428
|
1217 }
|
|
1218 }
|
|
1219
|
|
1220 /* Create a window which will receive completion messages */
|
771
|
1221 hwnd = qxeCreateWindow (XETEXT ("STATIC"), NULL, WS_OVERLAPPED, 0, 0, 1, 1,
|
|
1222 NULL, NULL, NULL, NULL);
|
428
|
1223 assert (hwnd);
|
|
1224
|
|
1225 /* Post name resolution request */
|
771
|
1226 {
|
|
1227 Extbyte *hostext;
|
|
1228
|
|
1229 LISP_STRING_TO_EXTERNAL (host, hostext, Qmswindows_host_name_encoding);
|
|
1230
|
|
1231 hasync = WSAAsyncGetHostByName (hwnd, XM_SOCKREPLY, hostext,
|
|
1232 buf, sizeof (buf));
|
|
1233 if (hasync == NULL)
|
|
1234 {
|
|
1235 errcode = WSAGetLastError ();
|
|
1236 goto done;
|
|
1237 }
|
|
1238 }
|
428
|
1239
|
|
1240 /* Set a timer to poll for quit every 250 ms */
|
|
1241 SetTimer (hwnd, SOCK_TIMER_ID, 250, NULL);
|
|
1242
|
|
1243 while (1)
|
|
1244 {
|
|
1245 MSG msg;
|
800
|
1246 qxeGetMessage (&msg, hwnd, 0, 0);
|
428
|
1247 if (msg.message == XM_SOCKREPLY)
|
|
1248 {
|
|
1249 /* Ok, got an answer */
|
563
|
1250 errcode = WSAGETASYNCERROR (msg.lParam);
|
428
|
1251 goto done;
|
|
1252 }
|
|
1253 else if (msg.message == WM_TIMER && msg.wParam == SOCK_TIMER_ID)
|
|
1254 {
|
|
1255 if (QUITP)
|
|
1256 {
|
|
1257 WSACancelAsyncRequest (hasync);
|
|
1258 KillTimer (hwnd, SOCK_TIMER_ID);
|
|
1259 DestroyWindow (hwnd);
|
853
|
1260 QUIT;
|
428
|
1261 }
|
|
1262 }
|
800
|
1263 qxeDispatchMessage (&msg);
|
428
|
1264 }
|
|
1265
|
|
1266 done:
|
|
1267 KillTimer (hwnd, SOCK_TIMER_ID);
|
|
1268 DestroyWindow (hwnd);
|
563
|
1269 if (!errcode)
|
428
|
1270 {
|
|
1271 /* BUF starts with struct hostent */
|
771
|
1272 struct hostent *he = (struct hostent *) buf;
|
|
1273 address->sin_addr.s_addr = * (unsigned long *) he->h_addr_list[0];
|
428
|
1274 }
|
563
|
1275 return errcode;
|
428
|
1276 }
|
|
1277
|
|
1278 static Lisp_Object
|
|
1279 nt_canonicalize_host_name (Lisp_Object host)
|
|
1280 {
|
|
1281 struct sockaddr_in address;
|
|
1282
|
563
|
1283 if (get_internet_address (host, &address)) /* error */
|
428
|
1284 return host;
|
|
1285
|
|
1286 if (address.sin_family == AF_INET)
|
|
1287 return build_string (inet_ntoa (address.sin_addr));
|
|
1288 else
|
|
1289 return host;
|
|
1290 }
|
|
1291
|
|
1292 /* open a TCP network connection to a given HOST/SERVICE. Treated
|
|
1293 exactly like a normal process when reading and writing. Only
|
|
1294 differences are in status display and process deletion. A network
|
|
1295 connection has no PID; you cannot signal it. All you can do is
|
|
1296 deactivate and close it via delete-process */
|
|
1297
|
|
1298 static void
|
442
|
1299 nt_open_network_stream (Lisp_Object name, Lisp_Object host,
|
|
1300 Lisp_Object service,
|
771
|
1301 Lisp_Object protocol, void **vinfd, void **voutfd)
|
428
|
1302 {
|
|
1303 struct sockaddr_in address;
|
|
1304 SOCKET s;
|
|
1305 int port;
|
|
1306 int retval;
|
563
|
1307 int errnum;
|
428
|
1308
|
|
1309 CHECK_STRING (host);
|
|
1310
|
|
1311 if (!EQ (protocol, Qtcp))
|
563
|
1312 invalid_constant ("Unsupported protocol", protocol);
|
428
|
1313
|
|
1314 if (INTP (service))
|
|
1315 port = htons ((unsigned short) XINT (service));
|
|
1316 else
|
|
1317 {
|
|
1318 struct servent *svc_info;
|
771
|
1319 Extbyte *servext;
|
|
1320
|
428
|
1321 CHECK_STRING (service);
|
771
|
1322 LISP_STRING_TO_EXTERNAL (service, servext,
|
|
1323 Qmswindows_service_name_encoding);
|
|
1324
|
|
1325 svc_info = getservbyname (servext, "tcp");
|
428
|
1326 if (svc_info == 0)
|
442
|
1327 invalid_argument ("Unknown service", service);
|
428
|
1328 port = svc_info->s_port;
|
|
1329 }
|
|
1330
|
563
|
1331 retval = get_internet_address (host, &address);
|
|
1332 if (retval)
|
|
1333 mswindows_report_winsock_error ("Getting IP address", host,
|
|
1334 retval);
|
428
|
1335 address.sin_port = port;
|
|
1336
|
|
1337 s = socket (address.sin_family, SOCK_STREAM, 0);
|
|
1338 if (s < 0)
|
563
|
1339 mswindows_report_winsock_error ("Creating socket", name,
|
|
1340 WSAGetLastError ());
|
428
|
1341
|
|
1342 /* We don't want to be blocked on connect */
|
|
1343 {
|
|
1344 unsigned long nonblock = 1;
|
|
1345 ioctlsocket (s, FIONBIO, &nonblock);
|
|
1346 }
|
854
|
1347
|
428
|
1348 retval = connect (s, (struct sockaddr *) &address, sizeof (address));
|
|
1349 if (retval != NO_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
|
563
|
1350 {
|
|
1351 errnum = WSAGetLastError ();
|
|
1352 goto connect_failed;
|
|
1353 }
|
|
1354
|
|
1355 #if 0 /* PUTA! I thought getsockopt() was failing, so I created the
|
|
1356 following based on the code in get_internet_address(), but
|
|
1357 it was my own fault down below. Both versions should work. */
|
428
|
1358 /* Wait while connection is established */
|
563
|
1359 {
|
|
1360 HWND hwnd;
|
|
1361
|
|
1362 /* Create a window which will receive completion messages */
|
771
|
1363 hwnd = qxeCreateWindow (XETEXT ("STATIC"), NULL, WS_OVERLAPPED, 0, 0, 1, 1,
|
|
1364 NULL, NULL, NULL, NULL);
|
563
|
1365 assert (hwnd);
|
|
1366
|
|
1367 /* Post request */
|
|
1368 if (WSAAsyncSelect (s, hwnd, XM_SOCKREPLY, FD_CONNECT))
|
|
1369 {
|
|
1370 errnum = WSAGetLastError ();
|
|
1371 goto done;
|
|
1372 }
|
|
1373
|
|
1374 /* Set a timer to poll for quit every 250 ms */
|
|
1375 SetTimer (hwnd, SOCK_TIMER_ID, 250, NULL);
|
|
1376
|
|
1377 while (1)
|
|
1378 {
|
|
1379 MSG msg;
|
|
1380 GetMessage (&msg, hwnd, 0, 0);
|
|
1381 if (msg.message == XM_SOCKREPLY)
|
|
1382 {
|
|
1383 /* Ok, got an answer */
|
|
1384 errnum = WSAGETASYNCERROR (msg.lParam);
|
|
1385 goto done;
|
|
1386 }
|
|
1387
|
|
1388 else if (msg.message == WM_TIMER && msg.wParam == SOCK_TIMER_ID)
|
|
1389 {
|
|
1390 if (QUITP)
|
|
1391 {
|
|
1392 WSAAsyncSelect (s, hwnd, XM_SOCKREPLY, 0);
|
|
1393 KillTimer (hwnd, SOCK_TIMER_ID);
|
|
1394 DestroyWindow (hwnd);
|
853
|
1395 QUIT;
|
563
|
1396 }
|
|
1397 }
|
|
1398 DispatchMessage (&msg);
|
|
1399 }
|
|
1400
|
|
1401 done:
|
|
1402 WSAAsyncSelect (s, hwnd, XM_SOCKREPLY, 0);
|
|
1403 KillTimer (hwnd, SOCK_TIMER_ID);
|
|
1404 DestroyWindow (hwnd);
|
|
1405 if (errnum)
|
|
1406 goto connect_failed;
|
|
1407 }
|
|
1408
|
|
1409 #else
|
428
|
1410 while (1)
|
|
1411 {
|
563
|
1412 fd_set fdwriteset, fdexceptset;
|
428
|
1413 struct timeval tv;
|
|
1414 int nsel;
|
|
1415
|
|
1416 if (QUITP)
|
|
1417 {
|
|
1418 closesocket (s);
|
853
|
1419 QUIT;
|
428
|
1420 }
|
|
1421
|
|
1422 /* Poll for quit every 250 ms */
|
|
1423 tv.tv_sec = 0;
|
|
1424 tv.tv_usec = 250 * 1000;
|
|
1425
|
563
|
1426 FD_ZERO (&fdwriteset);
|
|
1427 FD_SET (s, &fdwriteset);
|
|
1428 FD_ZERO (&fdexceptset);
|
|
1429 FD_SET (s, &fdexceptset);
|
|
1430 nsel = select (0, NULL, &fdwriteset, &fdexceptset, &tv);
|
|
1431
|
|
1432 if (nsel == SOCKET_ERROR)
|
|
1433 {
|
|
1434 errnum = WSAGetLastError ();
|
|
1435 goto connect_failed;
|
|
1436 }
|
428
|
1437
|
|
1438 if (nsel > 0)
|
|
1439 {
|
|
1440 /* Check: was connection successful or not? */
|
563
|
1441 if (FD_ISSET (s, &fdwriteset))
|
|
1442 break;
|
|
1443 else if (FD_ISSET (s, &fdexceptset))
|
|
1444 {
|
|
1445 int store_me_harder = sizeof (errnum);
|
|
1446 /* OK, we finally can get the REAL error code. Any paths
|
|
1447 in this code that lead to a call of WSAGetLastError()
|
|
1448 indicate probable logic failure. */
|
|
1449 if (getsockopt (s, SOL_SOCKET, SO_ERROR, (char *) &errnum,
|
|
1450 &store_me_harder))
|
|
1451 errnum = WSAGetLastError ();
|
|
1452 goto connect_failed;
|
|
1453 }
|
428
|
1454 else
|
563
|
1455 {
|
|
1456 signal_error (Qinternal_error,
|
|
1457 "Porra, esse caralho de um sistema de operacao",
|
|
1458 Qunbound);
|
|
1459 break;
|
|
1460 }
|
428
|
1461 }
|
|
1462 }
|
563
|
1463 #endif
|
428
|
1464
|
|
1465 /* We are connected at this point */
|
771
|
1466 *vinfd = (void *)s;
|
428
|
1467 DuplicateHandle (GetCurrentProcess(), (HANDLE)s,
|
|
1468 GetCurrentProcess(), (LPHANDLE)voutfd,
|
|
1469 0, FALSE, DUPLICATE_SAME_ACCESS);
|
|
1470 return;
|
|
1471
|
563
|
1472 connect_failed:
|
|
1473 {
|
|
1474 closesocket (s);
|
|
1475 mswindows_report_winsock_error ("Connection failed",
|
|
1476 list3 (Qunbound, host, service),
|
|
1477 errnum);
|
|
1478 }
|
428
|
1479 }
|
|
1480
|
|
1481 #endif
|
771
|
1482
|
|
1483
|
|
1484 DEFUN ("mswindows-set-process-priority", Fmswindows_set_process_priority, 2, 2, "", /*
|
|
1485 Set the priority of PROCESS to PRIORITY.
|
|
1486 If PROCESS is nil, the priority of Emacs is changed, otherwise the
|
|
1487 priority of the process whose pid is PROCESS is changed.
|
|
1488 PRIORITY should be one of the symbols high, normal, or low;
|
|
1489 any other symbol will be interpreted as normal.
|
|
1490
|
|
1491 If successful, the return value is t, otherwise nil.
|
|
1492 */
|
|
1493 (process, priority))
|
|
1494 {
|
|
1495 HANDLE proc_handle = GetCurrentProcess ();
|
|
1496 DWORD priority_class = NORMAL_PRIORITY_CLASS;
|
|
1497 Lisp_Object result = Qnil;
|
|
1498
|
|
1499 CHECK_SYMBOL (priority);
|
|
1500
|
|
1501 if (!NILP (process))
|
|
1502 {
|
|
1503 DWORD pid;
|
|
1504 struct Lisp_Process *p = 0;
|
|
1505
|
|
1506 if (PROCESSP (process))
|
|
1507 {
|
|
1508 CHECK_LIVE_PROCESS (process);
|
|
1509 p = XPROCESS (process);
|
|
1510 pid = NT_DATA (p)->dwProcessId;
|
|
1511 }
|
|
1512 else
|
|
1513 {
|
|
1514 CHECK_INT (process);
|
|
1515
|
|
1516 /* Allow pid to be an internally generated one, or one obtained
|
|
1517 externally. This is necessary because real pids on Win95 are
|
|
1518 negative. */
|
|
1519
|
|
1520 pid = XINT (process);
|
|
1521 p = find_process_from_pid (pid);
|
|
1522 if (p != NULL)
|
|
1523 pid = NT_DATA (p)->dwProcessId;
|
|
1524 }
|
|
1525
|
|
1526 /* #### Should we be using the existing process handle from NT_DATA(p)?
|
|
1527 Will we fail if we open it a second time? */
|
|
1528 proc_handle = OpenProcess (PROCESS_SET_INFORMATION, FALSE, pid);
|
|
1529 }
|
|
1530
|
|
1531 if (EQ (priority, Qhigh))
|
|
1532 priority_class = HIGH_PRIORITY_CLASS;
|
|
1533 else if (EQ (priority, Qlow))
|
|
1534 priority_class = IDLE_PRIORITY_CLASS;
|
|
1535
|
|
1536 if (proc_handle != NULL)
|
|
1537 {
|
|
1538 if (SetPriorityClass (proc_handle, priority_class))
|
|
1539 result = Qt;
|
|
1540 if (!NILP (process))
|
|
1541 CloseHandle (proc_handle);
|
|
1542 }
|
|
1543
|
|
1544 return result;
|
|
1545 }
|
|
1546
|
428
|
1547
|
|
1548 /*-----------------------------------------------------------------------*/
|
|
1549 /* Initialization */
|
|
1550 /*-----------------------------------------------------------------------*/
|
|
1551
|
|
1552 void
|
|
1553 process_type_create_nt (void)
|
|
1554 {
|
|
1555 PROCESS_HAS_METHOD (nt, alloc_process_data);
|
|
1556 PROCESS_HAS_METHOD (nt, finalize_process_data);
|
|
1557 PROCESS_HAS_METHOD (nt, init_process);
|
|
1558 PROCESS_HAS_METHOD (nt, create_process);
|
|
1559 PROCESS_HAS_METHOD (nt, update_status_if_terminated);
|
|
1560 PROCESS_HAS_METHOD (nt, send_process);
|
|
1561 PROCESS_HAS_METHOD (nt, kill_child_process);
|
|
1562 PROCESS_HAS_METHOD (nt, kill_process_by_pid);
|
|
1563 #ifdef HAVE_SOCKETS
|
|
1564 PROCESS_HAS_METHOD (nt, canonicalize_host_name);
|
|
1565 PROCESS_HAS_METHOD (nt, open_network_stream);
|
|
1566 #ifdef HAVE_MULTICAST
|
|
1567 #error I won't do this until '95 has winsock2
|
|
1568 PROCESS_HAS_METHOD (nt, open_multicast_group);
|
|
1569 #endif
|
|
1570 #endif
|
|
1571 }
|
|
1572
|
|
1573 void
|
|
1574 syms_of_process_nt (void)
|
|
1575 {
|
771
|
1576 DEFSUBR (Fmswindows_set_process_priority);
|
442
|
1577 DEFSYMBOL (Qmswindows_construct_process_command_line);
|
428
|
1578 }
|
|
1579
|
|
1580 void
|
|
1581 vars_of_process_nt (void)
|
|
1582 {
|
442
|
1583 DEFVAR_LISP ("mswindows-start-process-share-console",
|
|
1584 &Vmswindows_start_process_share_console /*
|
|
1585 When nil, new child processes are given a new console.
|
|
1586 When non-nil, they share the Emacs console; this has the limitation of
|
638
|
1587 allowing only one DOS subprocess to run at a time (whether started directly
|
442
|
1588 or indirectly by Emacs), and preventing Emacs from cleanly terminating the
|
|
1589 subprocess group, but may allow Emacs to interrupt a subprocess that doesn't
|
|
1590 otherwise respond to interrupts from Emacs.
|
|
1591 */ );
|
|
1592 Vmswindows_start_process_share_console = Qnil;
|
|
1593
|
|
1594 DEFVAR_LISP ("mswindows-start-process-inherit-error-mode",
|
|
1595 &Vmswindows_start_process_inherit_error_mode /*
|
|
1596 "When nil, new child processes revert to the default error mode.
|
|
1597 When non-nil, they inherit their error mode setting from Emacs, which stops
|
|
1598 them blocking when trying to access unmounted drives etc.
|
|
1599 */ );
|
|
1600 Vmswindows_start_process_inherit_error_mode = Qt;
|
428
|
1601 }
|