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