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.
|
563
|
5 Copyright (C) 1995, 1996, 2000, 2001 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
|
|
26 #include <config.h>
|
|
27 #include "lisp.h"
|
|
28
|
442
|
29 #include "buffer.h"
|
|
30 #include "console-msw.h"
|
428
|
31 #include "hash.h"
|
|
32 #include "lstream.h"
|
442
|
33 #include "nt.h"
|
428
|
34 #include "process.h"
|
|
35 #include "procimpl.h"
|
|
36 #include "sysdep.h"
|
|
37
|
558
|
38 #include "syssignal.h"
|
|
39 #include "sysfile.h"
|
|
40 #include "sysproc.h"
|
428
|
41
|
442
|
42 /* Bound by win32-native.el */
|
|
43 Lisp_Object Qmswindows_construct_process_command_line;
|
|
44
|
428
|
45 /* Arbitrary size limit for code fragments passed to run_in_other_process */
|
|
46 #define FRAGMENT_CODE_SIZE 32
|
|
47
|
|
48 /* Implementation-specific data. Pointed to by Lisp_Process->process_data */
|
|
49 struct nt_process_data
|
|
50 {
|
|
51 HANDLE h_process;
|
442
|
52 DWORD dwProcessId;
|
|
53 HWND hwnd; /* console window */
|
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
|
428
|
67 #define NT_DATA(p) ((struct nt_process_data*)((p)->process_data))
|
|
68
|
|
69 /*-----------------------------------------------------------------------*/
|
|
70 /* Process helpers */
|
|
71 /*-----------------------------------------------------------------------*/
|
|
72
|
|
73 /* This one breaks process abstraction. Prototype is in console-msw.h,
|
|
74 used by select_process method in event-msw.c */
|
|
75 HANDLE
|
440
|
76 get_nt_process_handle (Lisp_Process *p)
|
428
|
77 {
|
|
78 return (NT_DATA (p)->h_process);
|
|
79 }
|
442
|
80
|
|
81 static struct Lisp_Process *
|
|
82 find_process_from_pid (DWORD pid)
|
|
83 {
|
|
84 Lisp_Object tail, proc;
|
|
85
|
|
86 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
|
|
87 {
|
|
88 proc = XCAR (tail);
|
|
89 if (NT_DATA (XPROCESS (proc))->dwProcessId == pid)
|
|
90 return XPROCESS (proc);
|
|
91 }
|
|
92 return 0;
|
|
93 }
|
|
94
|
428
|
95
|
|
96 /*-----------------------------------------------------------------------*/
|
|
97 /* Running remote threads. See Microsoft Systems Journal 1994 Number 5 */
|
|
98 /* Jeffrey Richter, Load Your 32-bit DLL into Another Process's Address..*/
|
|
99 /*-----------------------------------------------------------------------*/
|
|
100
|
|
101 typedef struct
|
|
102 {
|
|
103 HANDLE h_process;
|
|
104 HANDLE h_thread;
|
|
105 LPVOID address;
|
|
106 } process_memory;
|
|
107
|
|
108 /*
|
|
109 * Allocate SIZE bytes in H_PROCESS address space. Fill in PMC used
|
|
110 * further by other routines. Return nonzero if successful.
|
|
111 *
|
|
112 * The memory in other process is allocated by creating a suspended
|
|
113 * thread. Initial stack of that thread is used as the memory
|
|
114 * block. The thread entry point is the routine ExitThread in
|
|
115 * kernel32.dll, so the allocated memory is freed just by resuming the
|
|
116 * thread, which immediately terminates after that.
|
|
117 */
|
|
118
|
|
119 static int
|
|
120 alloc_process_memory (HANDLE h_process, size_t size,
|
|
121 process_memory* pmc)
|
|
122 {
|
|
123 LPTHREAD_START_ROUTINE adr_ExitThread =
|
|
124 (LPTHREAD_START_ROUTINE)
|
|
125 GetProcAddress (GetModuleHandle ("kernel32"), "ExitThread");
|
|
126 DWORD dw_unused;
|
|
127 CONTEXT context;
|
|
128 MEMORY_BASIC_INFORMATION mbi;
|
|
129
|
|
130 pmc->h_process = h_process;
|
|
131 pmc->h_thread = CreateRemoteThread (h_process, NULL, size,
|
|
132 adr_ExitThread, NULL,
|
|
133 CREATE_SUSPENDED, &dw_unused);
|
|
134 if (pmc->h_thread == NULL)
|
|
135 return 0;
|
|
136
|
|
137 /* Get context, for thread's stack pointer */
|
|
138 context.ContextFlags = CONTEXT_CONTROL;
|
|
139 if (!GetThreadContext (pmc->h_thread, &context))
|
|
140 goto failure;
|
|
141
|
|
142 /* Determine base address of the committed range */
|
|
143 if (sizeof(mbi) != VirtualQueryEx (h_process,
|
|
144 #if defined (_X86_)
|
|
145 (LPDWORD)context.Esp - 1,
|
|
146 #elif defined (_ALPHA_)
|
|
147 (LPDWORD)context.IntSp - 1,
|
|
148 #else
|
|
149 #error Unknown processor architecture
|
|
150 #endif
|
|
151 &mbi, sizeof(mbi)))
|
|
152 goto failure;
|
|
153
|
|
154 /* Change the page protection of the allocated memory to executable,
|
|
155 read, and write. */
|
|
156 if (!VirtualProtectEx (h_process, mbi.BaseAddress, size,
|
|
157 PAGE_EXECUTE_READWRITE, &dw_unused))
|
|
158 goto failure;
|
|
159
|
|
160 pmc->address = mbi.BaseAddress;
|
|
161 return 1;
|
|
162
|
|
163 failure:
|
|
164 ResumeThread (pmc->h_thread);
|
|
165 pmc->address = 0;
|
|
166 return 0;
|
|
167 }
|
|
168
|
|
169 static void
|
|
170 free_process_memory (process_memory* pmc)
|
|
171 {
|
|
172 ResumeThread (pmc->h_thread);
|
|
173 }
|
|
174
|
|
175 /*
|
|
176 * Run ROUTINE in the context of process determined by H_PROCESS. The
|
|
177 * routine is passed the address of DATA as parameter. The ROUTINE must
|
|
178 * not be longer than ROUTINE_CODE_SIZE bytes. DATA_SIZE is the size of
|
|
179 * DATA structure.
|
|
180 *
|
|
181 * Note that the code must be positionally independent, and compiled
|
|
182 * without stack checks (they cause implicit calls into CRT so will
|
|
183 * fail). DATA should not refer any data in calling process, as both
|
|
184 * routine and its data are copied into remote process. Size of data
|
|
185 * and code together should not exceed one page (4K on x86 systems).
|
|
186 *
|
|
187 * Return the value returned by ROUTINE, or (DWORD)-1 if call failed.
|
|
188 */
|
|
189 static DWORD
|
|
190 run_in_other_process (HANDLE h_process,
|
|
191 LPTHREAD_START_ROUTINE routine,
|
|
192 LPVOID data, size_t data_size)
|
|
193 {
|
|
194 process_memory pm;
|
442
|
195 const size_t code_size = FRAGMENT_CODE_SIZE;
|
428
|
196 /* Need at most 3 extra bytes of memory, for data alignment */
|
|
197 size_t total_size = code_size + data_size + 3;
|
|
198 LPVOID remote_data;
|
|
199 HANDLE h_thread;
|
|
200 DWORD dw_unused;
|
|
201
|
|
202 /* Allocate memory */
|
|
203 if (!alloc_process_memory (h_process, total_size, &pm))
|
|
204 return (DWORD)-1;
|
|
205
|
|
206 /* Copy code */
|
|
207 if (!WriteProcessMemory (h_process, pm.address, (LPVOID)routine,
|
|
208 code_size, NULL))
|
|
209 goto failure;
|
|
210
|
|
211 /* Copy data */
|
|
212 if (data_size)
|
|
213 {
|
|
214 remote_data = (LPBYTE)pm.address + ((code_size + 4) & ~3);
|
|
215 if (!WriteProcessMemory (h_process, remote_data, data, data_size, NULL))
|
|
216 goto failure;
|
|
217 }
|
|
218 else
|
|
219 remote_data = NULL;
|
|
220
|
|
221 /* Execute the remote copy of code, passing it remote data */
|
|
222 h_thread = CreateRemoteThread (h_process, NULL, 0,
|
|
223 (LPTHREAD_START_ROUTINE) pm.address,
|
|
224 remote_data, 0, &dw_unused);
|
|
225 if (h_thread == NULL)
|
|
226 goto failure;
|
|
227
|
|
228 /* Wait till thread finishes */
|
|
229 WaitForSingleObject (h_thread, INFINITE);
|
|
230
|
|
231 /* Free remote memory */
|
|
232 free_process_memory (&pm);
|
|
233
|
|
234 /* Return thread's exit code */
|
|
235 {
|
|
236 DWORD exit_code;
|
|
237 GetExitCodeThread (h_thread, &exit_code);
|
|
238 CloseHandle (h_thread);
|
|
239 return exit_code;
|
|
240 }
|
|
241
|
|
242 failure:
|
|
243 free_process_memory (&pm);
|
|
244 return (DWORD)-1;
|
|
245 }
|
|
246
|
|
247 /*-----------------------------------------------------------------------*/
|
|
248 /* Sending signals */
|
|
249 /*-----------------------------------------------------------------------*/
|
|
250
|
442
|
251 /* ---------------------------- the NT way ------------------------------- */
|
|
252
|
428
|
253 /*
|
|
254 * We handle the following signals:
|
|
255 *
|
|
256 * SIGKILL, SIGTERM, SIGQUIT, SIGHUP - These four translate to ExitProcess
|
|
257 * executed by the remote process
|
|
258 * SIGINT - The remote process is sent CTRL_BREAK_EVENT
|
|
259 *
|
|
260 * The MSVC5.0 compiler feels free to re-order functions within a
|
|
261 * compilation unit, so we have no way of finding out the size of the
|
|
262 * following functions. Therefore these functions must not be larger than
|
|
263 * FRAGMENT_CODE_SIZE.
|
|
264 */
|
|
265
|
|
266 /*
|
|
267 * Sending SIGKILL
|
|
268 */
|
|
269 typedef struct
|
|
270 {
|
|
271 void (WINAPI *adr_ExitProcess) (UINT);
|
|
272 } sigkill_data;
|
|
273
|
|
274 static DWORD WINAPI
|
|
275 sigkill_proc (sigkill_data* data)
|
|
276 {
|
|
277 (*data->adr_ExitProcess)(255);
|
|
278 return 1;
|
|
279 }
|
|
280
|
|
281 /*
|
|
282 * Sending break or control c
|
|
283 */
|
|
284 typedef struct
|
|
285 {
|
|
286 BOOL (WINAPI *adr_GenerateConsoleCtrlEvent) (DWORD, DWORD);
|
|
287 DWORD event;
|
|
288 } sigint_data;
|
|
289
|
|
290 static DWORD WINAPI
|
|
291 sigint_proc (sigint_data* data)
|
|
292 {
|
|
293 return (*data->adr_GenerateConsoleCtrlEvent) (data->event, 0);
|
|
294 }
|
|
295
|
|
296 /*
|
|
297 * Enabling signals
|
|
298 */
|
|
299 typedef struct
|
|
300 {
|
|
301 BOOL (WINAPI *adr_SetConsoleCtrlHandler) (LPVOID, BOOL);
|
|
302 } sig_enable_data;
|
|
303
|
|
304 static DWORD WINAPI
|
|
305 sig_enable_proc (sig_enable_data* data)
|
|
306 {
|
|
307 (*data->adr_SetConsoleCtrlHandler) (NULL, FALSE);
|
|
308 return 1;
|
|
309 }
|
|
310
|
|
311 /*
|
|
312 * Send signal SIGNO to process H_PROCESS.
|
|
313 * Return nonzero if successful.
|
|
314 */
|
|
315
|
|
316 static int
|
442
|
317 send_signal_the_nt_way (struct nt_process_data *cp, int pid, int signo)
|
428
|
318 {
|
442
|
319 HANDLE h_process;
|
428
|
320 HMODULE h_kernel = GetModuleHandle ("kernel32");
|
442
|
321 int close_process = 0;
|
428
|
322 DWORD retval;
|
|
323
|
|
324 assert (h_kernel != NULL);
|
|
325
|
442
|
326 if (cp)
|
|
327 {
|
|
328 pid = cp->dwProcessId;
|
|
329 h_process = cp->h_process;
|
|
330 }
|
|
331 else
|
|
332 {
|
|
333 close_process = 1;
|
|
334 /* Try to open the process with required privileges */
|
|
335 h_process = OpenProcess (PROCESS_CREATE_THREAD
|
|
336 | PROCESS_QUERY_INFORMATION
|
|
337 | PROCESS_VM_OPERATION
|
|
338 | PROCESS_VM_WRITE,
|
|
339 FALSE, pid);
|
|
340 if (!h_process)
|
|
341 return 0;
|
|
342 }
|
|
343
|
428
|
344 switch (signo)
|
|
345 {
|
|
346 case SIGKILL:
|
|
347 case SIGTERM:
|
|
348 case SIGQUIT:
|
|
349 case SIGHUP:
|
|
350 {
|
|
351 sigkill_data d;
|
442
|
352
|
|
353 d.adr_ExitProcess =
|
|
354 (void (WINAPI *) (UINT)) GetProcAddress (h_kernel, "ExitProcess");
|
428
|
355 assert (d.adr_ExitProcess);
|
|
356 retval = run_in_other_process (h_process,
|
|
357 (LPTHREAD_START_ROUTINE)sigkill_proc,
|
|
358 &d, sizeof (d));
|
|
359 break;
|
|
360 }
|
|
361 case SIGINT:
|
|
362 {
|
|
363 sigint_data d;
|
|
364 d.adr_GenerateConsoleCtrlEvent =
|
442
|
365 (BOOL (WINAPI *) (DWORD, DWORD))
|
428
|
366 GetProcAddress (h_kernel, "GenerateConsoleCtrlEvent");
|
|
367 assert (d.adr_GenerateConsoleCtrlEvent);
|
|
368 d.event = CTRL_C_EVENT;
|
|
369 retval = run_in_other_process (h_process,
|
|
370 (LPTHREAD_START_ROUTINE)sigint_proc,
|
|
371 &d, sizeof (d));
|
|
372 break;
|
|
373 }
|
|
374 default:
|
|
375 assert (0);
|
|
376 }
|
|
377
|
442
|
378 if (close_process)
|
|
379 CloseHandle (h_process);
|
428
|
380 return (int)retval > 0 ? 1 : 0;
|
|
381 }
|
|
382
|
|
383 /*
|
|
384 * Enable CTRL_C_EVENT handling in a new child process
|
|
385 */
|
|
386 static void
|
|
387 enable_child_signals (HANDLE h_process)
|
|
388 {
|
|
389 HMODULE h_kernel = GetModuleHandle ("kernel32");
|
|
390 sig_enable_data d;
|
|
391
|
|
392 assert (h_kernel != NULL);
|
|
393 d.adr_SetConsoleCtrlHandler =
|
442
|
394 (BOOL (WINAPI *) (LPVOID, BOOL))
|
428
|
395 GetProcAddress (h_kernel, "SetConsoleCtrlHandler");
|
|
396 assert (d.adr_SetConsoleCtrlHandler);
|
|
397 run_in_other_process (h_process, (LPTHREAD_START_ROUTINE)sig_enable_proc,
|
|
398 &d, sizeof (d));
|
|
399 }
|
|
400
|
|
401 #pragma warning (default : 4113)
|
|
402
|
442
|
403 /* ---------------------------- the 95 way ------------------------------- */
|
|
404
|
|
405 static BOOL CALLBACK
|
|
406 find_child_console (HWND hwnd, long putada)
|
|
407 {
|
|
408 DWORD thread_id;
|
|
409 DWORD process_id;
|
|
410 struct nt_process_data *cp = (struct nt_process_data *) putada;
|
|
411
|
|
412 thread_id = GetWindowThreadProcessId (hwnd, &process_id);
|
|
413 if (process_id == cp->dwProcessId)
|
|
414 {
|
|
415 char window_class[32];
|
|
416
|
|
417 GetClassName (hwnd, window_class, sizeof (window_class));
|
|
418 if (strcmp (window_class,
|
|
419 mswindows_windows9x_p ()
|
|
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 {
|
|
459 BYTE control_scan_code = (BYTE) MapVirtualKey (VK_CONTROL, 0);
|
|
460 BYTE vk_break_code = VK_CANCEL;
|
|
461 BYTE break_scan_code = (BYTE) MapVirtualKey (vk_break_code, 0);
|
|
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';
|
|
468 break_scan_code = (BYTE) MapVirtualKey (vk_break_code, 0);
|
|
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
|
|
540 if (mswindows_windows9x_p ())
|
|
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. */
|
|
567 PostMessage (cp->hwnd, WM_QUIT, 0xff, 0);
|
|
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
|
|
581 PostMessage (cp->hwnd, WM_CLOSE, 0, 0);
|
|
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 {
|
|
608 return (!mswindows_windows9x_p () && send_signal_the_nt_way (cp, pid, signo))
|
|
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);
|
442
|
642 if (NT_DATA (p)->h_process)
|
|
643 CloseHandle (NT_DATA (p)->h_process);
|
428
|
644 }
|
|
645
|
|
646 /*
|
|
647 * Initialize XEmacs process implementation once
|
|
648 */
|
|
649 static void
|
|
650 nt_init_process (void)
|
|
651 {
|
|
652 /* Initialize winsock */
|
|
653 WSADATA wsa_data;
|
|
654 /* Request Winsock v1.1 Note the order: (minor=1, major=1) */
|
|
655 WSAStartup (MAKEWORD (1,1), &wsa_data);
|
|
656 }
|
|
657
|
563
|
658 DOESNT_RETURN
|
|
659 mswindows_report_process_error (const char *string, Lisp_Object data,
|
|
660 int errnum)
|
|
661 {
|
|
662 report_file_type_error (Qprocess_error, mswindows_lisp_error (errnum),
|
|
663 string, data);
|
|
664 }
|
428
|
665
|
563
|
666 static DOESNT_RETURN
|
|
667 mswindows_report_winsock_error (const char *string, Lisp_Object data,
|
|
668 int errnum)
|
428
|
669 {
|
563
|
670 report_file_type_error (Qnetwork_error, mswindows_lisp_error (errnum),
|
|
671 string, data);
|
442
|
672 }
|
|
673
|
|
674 static void
|
|
675 ensure_console_window_exists (void)
|
|
676 {
|
|
677 if (mswindows_windows9x_p ())
|
|
678 mswindows_hide_console ();
|
|
679 }
|
|
680
|
|
681 int
|
|
682 compare_env (const void *strp1, const void *strp2)
|
|
683 {
|
|
684 const char *str1 = *(const char**)strp1, *str2 = *(const char**)strp2;
|
|
685
|
|
686 while (*str1 && *str2 && *str1 != '=' && *str2 != '=')
|
|
687 {
|
|
688 if ((*str1) > (*str2))
|
|
689 return 1;
|
|
690 else if ((*str1) < (*str2))
|
|
691 return -1;
|
|
692 str1++, str2++;
|
|
693 }
|
|
694
|
|
695 if (*str1 == '=' && *str2 == '=')
|
|
696 return 0;
|
|
697 else if (*str1 == '=')
|
|
698 return -1;
|
|
699 else
|
|
700 return 1;
|
428
|
701 }
|
|
702
|
563
|
703 /*
|
|
704 * Fork off a subprocess. P is a pointer to newly created subprocess
|
|
705 * object. If this function signals, the caller is responsible for
|
|
706 * deleting (and finalizing) the process object.
|
|
707 *
|
|
708 * The method must return PID of the new process, a (positive??? ####) number
|
|
709 * which fits into Lisp_Int. No return value indicates an error, the method
|
|
710 * must signal an error instead.
|
|
711 */
|
|
712
|
428
|
713 static int
|
440
|
714 nt_create_process (Lisp_Process *p,
|
428
|
715 Lisp_Object *argv, int nargv,
|
|
716 Lisp_Object program, Lisp_Object cur_dir)
|
|
717 {
|
442
|
718 /* Synched up with sys_spawnve in FSF 20.6. Significantly different
|
|
719 but still synchable. */
|
430
|
720 HANDLE hmyshove, hmyslurp, hprocin, hprocout, hprocerr;
|
442
|
721 Extbyte *command_line;
|
428
|
722 BOOL do_io, windowed;
|
|
723 char *proc_env;
|
|
724
|
442
|
725 /* No need to DOS-ize the filename; expand-file-name (called prior)
|
|
726 already does this. */
|
|
727
|
428
|
728 /* Find out whether the application is windowed or not */
|
442
|
729 if (xSHGetFileInfoA)
|
|
730 {
|
|
731 /* SHGetFileInfo tends to return ERROR_FILE_NOT_FOUND on most
|
|
732 errors. This leads to bogus error message. */
|
|
733 DWORD image_type;
|
|
734 char *p = strrchr ((char *)XSTRING_DATA (program), '.');
|
|
735 if (p != NULL &&
|
|
736 (stricmp (p, ".exe") == 0 ||
|
|
737 stricmp (p, ".com") == 0 ||
|
|
738 stricmp (p, ".bat") == 0 ||
|
|
739 stricmp (p, ".cmd") == 0))
|
|
740 {
|
|
741 image_type = xSHGetFileInfoA ((char *)XSTRING_DATA (program), 0,NULL,
|
|
742 0, SHGFI_EXETYPE);
|
|
743 }
|
|
744 else
|
|
745 {
|
558
|
746 char progname[PATH_MAX];
|
442
|
747 sprintf (progname, "%s.exe", (char *)XSTRING_DATA (program));
|
|
748 image_type = xSHGetFileInfoA (progname, 0, NULL, 0, SHGFI_EXETYPE);
|
|
749 }
|
|
750 if (image_type == 0)
|
563
|
751 mswindows_report_process_error
|
|
752 ("Error starting",
|
|
753 program,
|
|
754 GetLastError () == ERROR_FILE_NOT_FOUND
|
|
755 ? ERROR_BAD_FORMAT : GetLastError ());
|
442
|
756 windowed = HIWORD (image_type) != 0;
|
|
757 }
|
|
758 else /* NT 3.5; we have no idea so just guess. */
|
|
759 windowed = 0;
|
428
|
760
|
|
761 /* Decide whether to do I/O on process handles, or just mark the
|
|
762 process exited immediately upon successful launching. We do I/O if the
|
|
763 process is a console one, or if it is windowed but windowed_process_io
|
|
764 is non-zero */
|
|
765 do_io = !windowed || windowed_process_io ;
|
|
766
|
|
767 if (do_io)
|
|
768 {
|
|
769 /* Create two unidirectional named pipes */
|
|
770 HANDLE htmp;
|
|
771 SECURITY_ATTRIBUTES sa;
|
|
772
|
|
773 sa.nLength = sizeof(sa);
|
|
774 sa.bInheritHandle = TRUE;
|
|
775 sa.lpSecurityDescriptor = NULL;
|
|
776
|
|
777 CreatePipe (&hprocin, &hmyshove, &sa, 0);
|
|
778 CreatePipe (&hmyslurp, &hprocout, &sa, 0);
|
|
779
|
430
|
780 /* Duplicate the stdout handle for use as stderr */
|
442
|
781 DuplicateHandle(GetCurrentProcess(), hprocout, GetCurrentProcess(),
|
|
782 &hprocerr, 0, TRUE, DUPLICATE_SAME_ACCESS);
|
430
|
783
|
428
|
784 /* Stupid Win32 allows to create a pipe with *both* ends either
|
|
785 inheritable or not. We need process ends inheritable, and local
|
|
786 ends not inheritable. */
|
442
|
787 DuplicateHandle (GetCurrentProcess(), hmyshove, GetCurrentProcess(),
|
|
788 &htmp, 0, FALSE,
|
|
789 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
428
|
790 hmyshove = htmp;
|
442
|
791 DuplicateHandle (GetCurrentProcess(), hmyslurp, GetCurrentProcess(),
|
|
792 &htmp, 0, FALSE,
|
|
793 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
428
|
794 hmyslurp = htmp;
|
|
795 }
|
|
796
|
|
797 /* Convert an argv vector into Win32 style command line by a call to
|
442
|
798 lisp function `mswindows-construct-process-command-line'
|
|
799 (in win32-native.el) */
|
428
|
800 {
|
|
801 int i;
|
|
802 Lisp_Object args_or_ret = Qnil;
|
|
803 struct gcpro gcpro1;
|
|
804
|
|
805 GCPRO1 (args_or_ret);
|
|
806
|
|
807 for (i = 0; i < nargv; ++i)
|
|
808 args_or_ret = Fcons (*argv++, args_or_ret);
|
|
809 args_or_ret = Fnreverse (args_or_ret);
|
|
810 args_or_ret = Fcons (program, args_or_ret);
|
|
811
|
442
|
812 args_or_ret = call1 (Qmswindows_construct_process_command_line,
|
|
813 args_or_ret);
|
428
|
814
|
|
815 if (!STRINGP (args_or_ret))
|
|
816 /* Luser wrote his/her own clever version */
|
442
|
817 invalid_argument
|
|
818 ("Bogus return value from `mswindows-construct-process-command-line'",
|
|
819 args_or_ret);
|
428
|
820
|
442
|
821 LISP_STRING_TO_EXTERNAL (args_or_ret, command_line, Qmswindows_tstr);
|
428
|
822
|
|
823 UNGCPRO; /* args_or_ret */
|
|
824 }
|
|
825
|
|
826 /* Set `proc_env' to a nul-separated array of the strings in
|
|
827 Vprocess_environment terminated by 2 nuls. */
|
|
828
|
|
829 {
|
|
830 char **env;
|
|
831 REGISTER Lisp_Object tem;
|
|
832 REGISTER char **new_env;
|
|
833 REGISTER int new_length = 0, i, new_space;
|
|
834 char *penv;
|
|
835
|
|
836 for (tem = Vprocess_environment;
|
|
837 (CONSP (tem)
|
|
838 && STRINGP (XCAR (tem)));
|
|
839 tem = XCDR (tem))
|
|
840 new_length++;
|
442
|
841
|
|
842 /* FSF adds an extra env var to hold the current process ID of the
|
|
843 Emacs process. Apparently this is used only by emacsserver.c,
|
|
844 which we have superseded to gnuserv.c. (#### Does it work under
|
|
845 MS Windows?)
|
|
846
|
|
847 sprintf (ppid_env_var_buffer, "EM_PARENT_PROCESS_ID=%d",
|
|
848 GetCurrentProcessId ());
|
|
849 arglen += strlen (ppid_env_var_buffer) + 1;
|
|
850 numenv++;
|
|
851 */
|
428
|
852
|
|
853 /* new_length + 1 to include terminating 0. */
|
|
854 env = new_env = alloca_array (char *, new_length + 1);
|
|
855
|
|
856 /* Copy the Vprocess_environment strings into new_env. */
|
|
857 for (tem = Vprocess_environment;
|
|
858 (CONSP (tem)
|
|
859 && STRINGP (XCAR (tem)));
|
|
860 tem = XCDR (tem))
|
|
861 {
|
|
862 char **ep = env;
|
|
863 char *string = (char *) XSTRING_DATA (XCAR (tem));
|
|
864 /* See if this string duplicates any string already in the env.
|
|
865 If so, don't put it in.
|
|
866 When an env var has multiple definitions,
|
|
867 we keep the definition that comes first in process-environment. */
|
|
868 for (; ep != new_env; ep++)
|
|
869 {
|
|
870 char *p = *ep, *q = string;
|
|
871 while (1)
|
|
872 {
|
|
873 if (*q == 0)
|
|
874 /* The string is malformed; might as well drop it. */
|
|
875 goto duplicate;
|
|
876 if (*q != *p)
|
|
877 break;
|
|
878 if (*q == '=')
|
|
879 goto duplicate;
|
|
880 p++, q++;
|
|
881 }
|
|
882 }
|
|
883 *new_env++ = string;
|
|
884 duplicate: ;
|
|
885 }
|
|
886 *new_env = 0;
|
|
887
|
|
888 /* Sort the environment variables */
|
|
889 new_length = new_env - env;
|
|
890 qsort (env, new_length, sizeof (char *), compare_env);
|
|
891
|
|
892 /* Work out how much space to allocate */
|
|
893 new_space = 0;
|
|
894 for (i = 0; i < new_length; i++)
|
|
895 {
|
|
896 new_space += strlen(env[i]) + 1;
|
|
897 }
|
|
898 new_space++;
|
|
899
|
|
900 /* Allocate space and copy variables into it */
|
442
|
901 penv = proc_env = (char*) alloca(new_space);
|
428
|
902 for (i = 0; i < new_length; i++)
|
|
903 {
|
|
904 strcpy(penv, env[i]);
|
|
905 penv += strlen(env[i]) + 1;
|
|
906 }
|
|
907 *penv = 0;
|
|
908 }
|
442
|
909
|
|
910 #if 0
|
|
911 /* #### we need to port this. */
|
|
912 /* On Windows 95, if cmdname is a DOS app, we invoke a helper
|
|
913 application to start it by specifying the helper app as cmdname,
|
|
914 while leaving the real app name as argv[0]. */
|
|
915 if (is_dos_app)
|
|
916 {
|
|
917 cmdname = (char*) alloca (MAXPATHLEN);
|
|
918 if (egetenv ("CMDPROXY"))
|
|
919 strcpy ((char*)cmdname, egetenv ("CMDPROXY"));
|
|
920 else
|
|
921 {
|
|
922 strcpy ((char*)cmdname, XSTRING_DATA (Vinvocation_directory));
|
|
923 strcat ((char*)cmdname, "cmdproxy.exe");
|
|
924 }
|
|
925 }
|
|
926 #endif
|
428
|
927
|
|
928 /* Create process */
|
|
929 {
|
|
930 STARTUPINFO si;
|
|
931 PROCESS_INFORMATION pi;
|
|
932 DWORD err;
|
442
|
933 DWORD flags;
|
428
|
934
|
|
935 xzero (si);
|
|
936 si.dwFlags = STARTF_USESHOWWINDOW;
|
|
937 si.wShowWindow = windowed ? SW_SHOWNORMAL : SW_HIDE;
|
|
938 if (do_io)
|
|
939 {
|
|
940 si.hStdInput = hprocin;
|
|
941 si.hStdOutput = hprocout;
|
430
|
942 si.hStdError = hprocerr;
|
428
|
943 si.dwFlags |= STARTF_USESTDHANDLES;
|
|
944 }
|
|
945
|
442
|
946 flags = CREATE_SUSPENDED;
|
|
947 if (mswindows_windows9x_p ())
|
|
948 flags |= (!NILP (Vmswindows_start_process_share_console)
|
|
949 ? CREATE_NEW_PROCESS_GROUP
|
|
950 : CREATE_NEW_CONSOLE);
|
|
951 else
|
|
952 flags |= CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP;
|
|
953 if (NILP (Vmswindows_start_process_inherit_error_mode))
|
|
954 flags |= CREATE_DEFAULT_ERROR_MODE;
|
|
955
|
|
956 ensure_console_window_exists ();
|
|
957
|
|
958 err = (CreateProcess (NULL, command_line, NULL, NULL, TRUE, flags,
|
428
|
959 proc_env, (char *) XSTRING_DATA (cur_dir), &si, &pi)
|
|
960 ? 0 : GetLastError ());
|
|
961
|
|
962 if (do_io)
|
|
963 {
|
|
964 /* These just have been inherited; we do not need a copy */
|
|
965 CloseHandle (hprocin);
|
|
966 CloseHandle (hprocout);
|
430
|
967 CloseHandle (hprocerr);
|
428
|
968 }
|
|
969
|
|
970 /* Handle process creation failure */
|
|
971 if (err)
|
|
972 {
|
|
973 if (do_io)
|
|
974 {
|
|
975 CloseHandle (hmyshove);
|
|
976 CloseHandle (hmyslurp);
|
|
977 }
|
563
|
978 mswindows_report_process_error
|
|
979 ("Error starting",
|
|
980 program, GetLastError ());
|
428
|
981 }
|
|
982
|
|
983 /* The process started successfully */
|
|
984 if (do_io)
|
|
985 {
|
|
986 NT_DATA(p)->h_process = pi.hProcess;
|
442
|
987 NT_DATA(p)->dwProcessId = pi.dwProcessId;
|
428
|
988 init_process_io_handles (p, (void*)hmyslurp, (void*)hmyshove, 0);
|
|
989 }
|
|
990 else
|
|
991 {
|
|
992 /* Indicate as if the process has exited immediately. */
|
|
993 p->status_symbol = Qexit;
|
|
994 CloseHandle (pi.hProcess);
|
|
995 }
|
|
996
|
442
|
997 if (!windowed)
|
|
998 enable_child_signals (pi.hProcess);
|
|
999
|
428
|
1000 ResumeThread (pi.hThread);
|
|
1001 CloseHandle (pi.hThread);
|
|
1002
|
432
|
1003 return ((int)pi.dwProcessId);
|
428
|
1004 }
|
|
1005 }
|
|
1006
|
|
1007 /*
|
|
1008 * This method is called to update status fields of the process
|
|
1009 * structure. If the process has not existed, this method is expected
|
|
1010 * to do nothing.
|
|
1011 *
|
|
1012 * The method is called only for real child processes.
|
|
1013 */
|
|
1014
|
|
1015 static void
|
440
|
1016 nt_update_status_if_terminated (Lisp_Process* p)
|
428
|
1017 {
|
|
1018 DWORD exit_code;
|
|
1019 if (GetExitCodeProcess (NT_DATA(p)->h_process, &exit_code)
|
|
1020 && exit_code != STILL_ACTIVE)
|
|
1021 {
|
|
1022 p->tick++;
|
|
1023 p->core_dumped = 0;
|
|
1024 /* The exit code can be a code returned by process, or an
|
|
1025 NTSTATUS value. We cannot accurately handle the latter since
|
|
1026 it is a full 32 bit integer */
|
|
1027 if (exit_code & 0xC0000000)
|
|
1028 {
|
|
1029 p->status_symbol = Qsignal;
|
|
1030 p->exit_code = exit_code & 0x1FFFFFFF;
|
|
1031 }
|
|
1032 else
|
|
1033 {
|
|
1034 p->status_symbol = Qexit;
|
|
1035 p->exit_code = exit_code;
|
|
1036 }
|
|
1037 }
|
|
1038 }
|
|
1039
|
|
1040 /*
|
|
1041 * Stuff the entire contents of LSTREAM to the process output pipe
|
|
1042 */
|
|
1043
|
|
1044 /* #### If only this function could be somehow merged with
|
|
1045 unix_send_process... */
|
|
1046
|
|
1047 static void
|
|
1048 nt_send_process (Lisp_Object proc, struct lstream* lstream)
|
|
1049 {
|
432
|
1050 volatile Lisp_Object vol_proc = proc;
|
440
|
1051 Lisp_Process *volatile p = XPROCESS (proc);
|
428
|
1052
|
|
1053 /* use a reasonable-sized buffer (somewhere around the size of the
|
|
1054 stream buffer) so as to avoid inundating the stream with blocked
|
|
1055 data. */
|
442
|
1056 Bufbyte chunkbuf[512];
|
428
|
1057 Bytecount chunklen;
|
|
1058
|
|
1059 while (1)
|
|
1060 {
|
462
|
1061 Lstream_data_count writeret;
|
428
|
1062
|
442
|
1063 chunklen = Lstream_read (lstream, chunkbuf, 512);
|
428
|
1064 if (chunklen <= 0)
|
|
1065 break; /* perhaps should abort() if < 0?
|
|
1066 This should never happen. */
|
|
1067
|
|
1068 /* Lstream_write() will never successfully write less than the
|
|
1069 amount sent in. In the worst case, it just buffers the
|
|
1070 unwritten data. */
|
|
1071 writeret = Lstream_write (XLSTREAM (DATA_OUTSTREAM(p)), chunkbuf,
|
|
1072 chunklen);
|
|
1073 Lstream_flush (XLSTREAM (DATA_OUTSTREAM(p)));
|
|
1074 if (writeret < 0)
|
|
1075 {
|
|
1076 p->status_symbol = Qexit;
|
|
1077 p->exit_code = ERROR_BROKEN_PIPE;
|
|
1078 p->core_dumped = 0;
|
|
1079 p->tick++;
|
|
1080 process_tick++;
|
432
|
1081 deactivate_process (*((Lisp_Object *) (&vol_proc)));
|
442
|
1082 invalid_operation ("Broken pipe error sending to process; closed it",
|
|
1083 p->name);
|
428
|
1084 }
|
|
1085
|
|
1086 {
|
|
1087 int wait_ms = 25;
|
|
1088 while (Lstream_was_blocked_p (XLSTREAM (p->pipe_outstream)))
|
|
1089 {
|
|
1090 /* Buffer is full. Wait, accepting input; that may allow
|
|
1091 the program to finish doing output and read more. */
|
|
1092 Faccept_process_output (Qnil, Qzero, make_int (wait_ms));
|
|
1093 Lstream_flush (XLSTREAM (p->pipe_outstream));
|
|
1094 wait_ms = min (1000, 2 * wait_ms);
|
|
1095 }
|
|
1096 }
|
|
1097 }
|
|
1098 }
|
|
1099
|
|
1100 /*
|
|
1101 * Send a signal number SIGNO to PROCESS.
|
|
1102 * CURRENT_GROUP means send to the process group that currently owns
|
|
1103 * the terminal being used to communicate with PROCESS.
|
|
1104 * This is used for various commands in shell mode.
|
|
1105 * If NOMSG is zero, insert signal-announcements into process's buffers
|
|
1106 * right away.
|
|
1107 *
|
|
1108 * If we can, we try to signal PROCESS by sending control characters
|
|
1109 * down the pty. This allows us to signal inferiors who have changed
|
|
1110 * their uid, for which killpg would return an EPERM error.
|
|
1111 *
|
|
1112 * The method signals an error if the given SIGNO is not valid
|
|
1113 */
|
|
1114
|
|
1115 static void
|
|
1116 nt_kill_child_process (Lisp_Object proc, int signo,
|
|
1117 int current_group, int nomsg)
|
|
1118 {
|
440
|
1119 Lisp_Process *p = XPROCESS (proc);
|
428
|
1120
|
|
1121 /* Signal error if SIGNO cannot be sent */
|
|
1122 validate_signal_number (signo);
|
|
1123
|
|
1124 /* Send signal */
|
442
|
1125 if (!send_signal (NT_DATA (p), 0, signo))
|
|
1126 invalid_operation ("Cannot send signal to process", proc);
|
428
|
1127 }
|
|
1128
|
|
1129 /*
|
442
|
1130 * Kill any process in the system given its PID
|
428
|
1131 *
|
|
1132 * Returns zero if a signal successfully sent, or
|
|
1133 * negative number upon failure
|
|
1134 */
|
|
1135 static int
|
|
1136 nt_kill_process_by_pid (int pid, int signo)
|
|
1137 {
|
442
|
1138 struct Lisp_Process *p;
|
|
1139
|
428
|
1140 /* Signal error if SIGNO cannot be sent */
|
|
1141 validate_signal_number (signo);
|
|
1142
|
442
|
1143 p = find_process_from_pid (pid);
|
|
1144 return send_signal (p ? NT_DATA (p) : 0, pid, signo) ? 0 : -1;
|
428
|
1145 }
|
|
1146
|
|
1147 /*-----------------------------------------------------------------------*/
|
|
1148 /* Sockets connections */
|
|
1149 /*-----------------------------------------------------------------------*/
|
|
1150 #ifdef HAVE_SOCKETS
|
|
1151
|
|
1152 /* #### Hey MS, how long Winsock 2 for '95 will be in beta? */
|
|
1153
|
|
1154 #define SOCK_TIMER_ID 666
|
|
1155 #define XM_SOCKREPLY (WM_USER + 666)
|
|
1156
|
563
|
1157 /* Return 0 for success, or error code */
|
|
1158
|
428
|
1159 static int
|
563
|
1160 get_internet_address (Lisp_Object host, struct sockaddr_in *address)
|
428
|
1161 {
|
|
1162 char buf [MAXGETHOSTSTRUCT];
|
|
1163 HWND hwnd;
|
|
1164 HANDLE hasync;
|
563
|
1165 int errcode = 0;
|
428
|
1166
|
|
1167 address->sin_family = AF_INET;
|
|
1168
|
|
1169 /* First check if HOST is already a numeric address */
|
|
1170 {
|
|
1171 unsigned long inaddr = inet_addr (XSTRING_DATA (host));
|
|
1172 if (inaddr != INADDR_NONE)
|
|
1173 {
|
|
1174 address->sin_addr.s_addr = inaddr;
|
563
|
1175 return 0;
|
428
|
1176 }
|
|
1177 }
|
|
1178
|
|
1179 /* Create a window which will receive completion messages */
|
|
1180 hwnd = CreateWindow ("STATIC", NULL, WS_OVERLAPPED, 0, 0, 1, 1,
|
|
1181 NULL, NULL, NULL, NULL);
|
|
1182 assert (hwnd);
|
|
1183
|
|
1184 /* Post name resolution request */
|
|
1185 hasync = WSAAsyncGetHostByName (hwnd, XM_SOCKREPLY, XSTRING_DATA (host),
|
|
1186 buf, sizeof (buf));
|
|
1187 if (hasync == NULL)
|
563
|
1188 {
|
|
1189 errcode = WSAGetLastError ();
|
|
1190 goto done;
|
|
1191 }
|
428
|
1192
|
|
1193 /* Set a timer to poll for quit every 250 ms */
|
|
1194 SetTimer (hwnd, SOCK_TIMER_ID, 250, NULL);
|
|
1195
|
|
1196 while (1)
|
|
1197 {
|
|
1198 MSG msg;
|
|
1199 GetMessage (&msg, hwnd, 0, 0);
|
|
1200 if (msg.message == XM_SOCKREPLY)
|
|
1201 {
|
|
1202 /* Ok, got an answer */
|
563
|
1203 errcode = WSAGETASYNCERROR (msg.lParam);
|
428
|
1204 goto done;
|
|
1205 }
|
|
1206 else if (msg.message == WM_TIMER && msg.wParam == SOCK_TIMER_ID)
|
|
1207 {
|
|
1208 if (QUITP)
|
|
1209 {
|
|
1210 WSACancelAsyncRequest (hasync);
|
|
1211 KillTimer (hwnd, SOCK_TIMER_ID);
|
|
1212 DestroyWindow (hwnd);
|
|
1213 REALLY_QUIT;
|
|
1214 }
|
|
1215 }
|
|
1216 DispatchMessage (&msg);
|
|
1217 }
|
|
1218
|
|
1219 done:
|
|
1220 KillTimer (hwnd, SOCK_TIMER_ID);
|
|
1221 DestroyWindow (hwnd);
|
563
|
1222 if (!errcode)
|
428
|
1223 {
|
|
1224 /* BUF starts with struct hostent */
|
|
1225 struct hostent* he = (struct hostent*) buf;
|
|
1226 address->sin_addr.s_addr = *(unsigned long*)he->h_addr_list[0];
|
|
1227 }
|
563
|
1228 return errcode;
|
428
|
1229 }
|
|
1230
|
|
1231 static Lisp_Object
|
|
1232 nt_canonicalize_host_name (Lisp_Object host)
|
|
1233 {
|
|
1234 struct sockaddr_in address;
|
|
1235
|
563
|
1236 if (get_internet_address (host, &address)) /* error */
|
428
|
1237 return host;
|
|
1238
|
|
1239 if (address.sin_family == AF_INET)
|
|
1240 return build_string (inet_ntoa (address.sin_addr));
|
|
1241 else
|
|
1242 return host;
|
|
1243 }
|
|
1244
|
|
1245 /* open a TCP network connection to a given HOST/SERVICE. Treated
|
|
1246 exactly like a normal process when reading and writing. Only
|
|
1247 differences are in status display and process deletion. A network
|
|
1248 connection has no PID; you cannot signal it. All you can do is
|
|
1249 deactivate and close it via delete-process */
|
|
1250
|
|
1251 static void
|
442
|
1252 nt_open_network_stream (Lisp_Object name, Lisp_Object host,
|
|
1253 Lisp_Object service,
|
428
|
1254 Lisp_Object protocol, void** vinfd, void** voutfd)
|
|
1255 {
|
442
|
1256 /* !!#### not Mule-ized */
|
428
|
1257 struct sockaddr_in address;
|
|
1258 SOCKET s;
|
|
1259 int port;
|
|
1260 int retval;
|
563
|
1261 int errnum;
|
428
|
1262
|
|
1263 CHECK_STRING (host);
|
|
1264
|
|
1265 if (!EQ (protocol, Qtcp))
|
563
|
1266 invalid_constant ("Unsupported protocol", protocol);
|
428
|
1267
|
|
1268 if (INTP (service))
|
|
1269 port = htons ((unsigned short) XINT (service));
|
|
1270 else
|
|
1271 {
|
|
1272 struct servent *svc_info;
|
|
1273 CHECK_STRING (service);
|
|
1274 svc_info = getservbyname ((char *) XSTRING_DATA (service), "tcp");
|
|
1275 if (svc_info == 0)
|
442
|
1276 invalid_argument ("Unknown service", service);
|
428
|
1277 port = svc_info->s_port;
|
|
1278 }
|
|
1279
|
563
|
1280 retval = get_internet_address (host, &address);
|
|
1281 if (retval)
|
|
1282 mswindows_report_winsock_error ("Getting IP address", host,
|
|
1283 retval);
|
428
|
1284 address.sin_port = port;
|
|
1285
|
|
1286 s = socket (address.sin_family, SOCK_STREAM, 0);
|
|
1287 if (s < 0)
|
563
|
1288 mswindows_report_winsock_error ("Creating socket", name,
|
|
1289 WSAGetLastError ());
|
428
|
1290
|
|
1291 /* We don't want to be blocked on connect */
|
|
1292 {
|
|
1293 unsigned long nonblock = 1;
|
|
1294 ioctlsocket (s, FIONBIO, &nonblock);
|
|
1295 }
|
|
1296
|
|
1297 retval = connect (s, (struct sockaddr *) &address, sizeof (address));
|
|
1298 if (retval != NO_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
|
563
|
1299 {
|
|
1300 errnum = WSAGetLastError ();
|
|
1301 goto connect_failed;
|
|
1302 }
|
|
1303
|
|
1304 #if 0 /* PUTA! I thought getsockopt() was failing, so I created the
|
|
1305 following based on the code in get_internet_address(), but
|
|
1306 it was my own fault down below. Both versions should work. */
|
428
|
1307 /* Wait while connection is established */
|
563
|
1308 {
|
|
1309 HWND hwnd;
|
|
1310
|
|
1311 /* Create a window which will receive completion messages */
|
|
1312 hwnd = CreateWindow ("STATIC", NULL, WS_OVERLAPPED, 0, 0, 1, 1,
|
|
1313 NULL, NULL, NULL, NULL);
|
|
1314 assert (hwnd);
|
|
1315
|
|
1316 /* Post request */
|
|
1317 if (WSAAsyncSelect (s, hwnd, XM_SOCKREPLY, FD_CONNECT))
|
|
1318 {
|
|
1319 errnum = WSAGetLastError ();
|
|
1320 goto done;
|
|
1321 }
|
|
1322
|
|
1323 /* Set a timer to poll for quit every 250 ms */
|
|
1324 SetTimer (hwnd, SOCK_TIMER_ID, 250, NULL);
|
|
1325
|
|
1326 while (1)
|
|
1327 {
|
|
1328 MSG msg;
|
|
1329 GetMessage (&msg, hwnd, 0, 0);
|
|
1330 if (msg.message == XM_SOCKREPLY)
|
|
1331 {
|
|
1332 /* Ok, got an answer */
|
|
1333 errnum = WSAGETASYNCERROR (msg.lParam);
|
|
1334 goto done;
|
|
1335 }
|
|
1336
|
|
1337 else if (msg.message == WM_TIMER && msg.wParam == SOCK_TIMER_ID)
|
|
1338 {
|
|
1339 if (QUITP)
|
|
1340 {
|
|
1341 WSAAsyncSelect (s, hwnd, XM_SOCKREPLY, 0);
|
|
1342 KillTimer (hwnd, SOCK_TIMER_ID);
|
|
1343 DestroyWindow (hwnd);
|
|
1344 REALLY_QUIT;
|
|
1345 }
|
|
1346 }
|
|
1347 DispatchMessage (&msg);
|
|
1348 }
|
|
1349
|
|
1350 done:
|
|
1351 WSAAsyncSelect (s, hwnd, XM_SOCKREPLY, 0);
|
|
1352 KillTimer (hwnd, SOCK_TIMER_ID);
|
|
1353 DestroyWindow (hwnd);
|
|
1354 if (errnum)
|
|
1355 goto connect_failed;
|
|
1356 }
|
|
1357
|
|
1358 #else
|
428
|
1359 while (1)
|
|
1360 {
|
563
|
1361 fd_set fdwriteset, fdexceptset;
|
428
|
1362 struct timeval tv;
|
|
1363 int nsel;
|
|
1364
|
|
1365 if (QUITP)
|
|
1366 {
|
|
1367 closesocket (s);
|
|
1368 REALLY_QUIT;
|
|
1369 }
|
|
1370
|
|
1371 /* Poll for quit every 250 ms */
|
|
1372 tv.tv_sec = 0;
|
|
1373 tv.tv_usec = 250 * 1000;
|
|
1374
|
563
|
1375 FD_ZERO (&fdwriteset);
|
|
1376 FD_SET (s, &fdwriteset);
|
|
1377 FD_ZERO (&fdexceptset);
|
|
1378 FD_SET (s, &fdexceptset);
|
|
1379 nsel = select (0, NULL, &fdwriteset, &fdexceptset, &tv);
|
|
1380
|
|
1381 if (nsel == SOCKET_ERROR)
|
|
1382 {
|
|
1383 errnum = WSAGetLastError ();
|
|
1384 goto connect_failed;
|
|
1385 }
|
428
|
1386
|
|
1387 if (nsel > 0)
|
|
1388 {
|
|
1389 /* Check: was connection successful or not? */
|
563
|
1390 if (FD_ISSET (s, &fdwriteset))
|
|
1391 break;
|
|
1392 else if (FD_ISSET (s, &fdexceptset))
|
|
1393 {
|
|
1394 int store_me_harder = sizeof (errnum);
|
|
1395 /* OK, we finally can get the REAL error code. Any paths
|
|
1396 in this code that lead to a call of WSAGetLastError()
|
|
1397 indicate probable logic failure. */
|
|
1398 if (getsockopt (s, SOL_SOCKET, SO_ERROR, (char *) &errnum,
|
|
1399 &store_me_harder))
|
|
1400 errnum = WSAGetLastError ();
|
|
1401 goto connect_failed;
|
|
1402 }
|
428
|
1403 else
|
563
|
1404 {
|
|
1405 signal_error (Qinternal_error,
|
|
1406 "Porra, esse caralho de um sistema de operacao",
|
|
1407 Qunbound);
|
|
1408 break;
|
|
1409 }
|
428
|
1410 }
|
|
1411 }
|
563
|
1412 #endif
|
428
|
1413
|
|
1414 /* We are connected at this point */
|
|
1415 *vinfd = (void*)s;
|
|
1416 DuplicateHandle (GetCurrentProcess(), (HANDLE)s,
|
|
1417 GetCurrentProcess(), (LPHANDLE)voutfd,
|
|
1418 0, FALSE, DUPLICATE_SAME_ACCESS);
|
|
1419 return;
|
|
1420
|
563
|
1421 connect_failed:
|
|
1422 {
|
|
1423 closesocket (s);
|
|
1424 mswindows_report_winsock_error ("Connection failed",
|
|
1425 list3 (Qunbound, host, service),
|
|
1426 errnum);
|
|
1427 }
|
428
|
1428 }
|
|
1429
|
|
1430 #endif
|
|
1431
|
|
1432 /*-----------------------------------------------------------------------*/
|
|
1433 /* Initialization */
|
|
1434 /*-----------------------------------------------------------------------*/
|
|
1435
|
|
1436 void
|
|
1437 process_type_create_nt (void)
|
|
1438 {
|
|
1439 PROCESS_HAS_METHOD (nt, alloc_process_data);
|
|
1440 PROCESS_HAS_METHOD (nt, finalize_process_data);
|
|
1441 PROCESS_HAS_METHOD (nt, init_process);
|
|
1442 PROCESS_HAS_METHOD (nt, create_process);
|
|
1443 PROCESS_HAS_METHOD (nt, update_status_if_terminated);
|
|
1444 PROCESS_HAS_METHOD (nt, send_process);
|
|
1445 PROCESS_HAS_METHOD (nt, kill_child_process);
|
|
1446 PROCESS_HAS_METHOD (nt, kill_process_by_pid);
|
|
1447 #ifdef HAVE_SOCKETS
|
|
1448 PROCESS_HAS_METHOD (nt, canonicalize_host_name);
|
|
1449 PROCESS_HAS_METHOD (nt, open_network_stream);
|
|
1450 #ifdef HAVE_MULTICAST
|
|
1451 #error I won't do this until '95 has winsock2
|
|
1452 PROCESS_HAS_METHOD (nt, open_multicast_group);
|
|
1453 #endif
|
|
1454 #endif
|
|
1455 }
|
|
1456
|
|
1457 void
|
|
1458 syms_of_process_nt (void)
|
|
1459 {
|
442
|
1460 DEFSYMBOL (Qmswindows_construct_process_command_line);
|
428
|
1461 }
|
|
1462
|
|
1463 void
|
|
1464 vars_of_process_nt (void)
|
|
1465 {
|
442
|
1466 DEFVAR_LISP ("mswindows-start-process-share-console",
|
|
1467 &Vmswindows_start_process_share_console /*
|
|
1468 When nil, new child processes are given a new console.
|
|
1469 When non-nil, they share the Emacs console; this has the limitation of
|
638
|
1470 allowing only one DOS subprocess to run at a time (whether started directly
|
442
|
1471 or indirectly by Emacs), and preventing Emacs from cleanly terminating the
|
|
1472 subprocess group, but may allow Emacs to interrupt a subprocess that doesn't
|
|
1473 otherwise respond to interrupts from Emacs.
|
|
1474 */ );
|
|
1475 Vmswindows_start_process_share_console = Qnil;
|
|
1476
|
|
1477 DEFVAR_LISP ("mswindows-start-process-inherit-error-mode",
|
|
1478 &Vmswindows_start_process_inherit_error_mode /*
|
|
1479 "When nil, new child processes revert to the default error mode.
|
|
1480 When non-nil, they inherit their error mode setting from Emacs, which stops
|
|
1481 them blocking when trying to access unmounted drives etc.
|
|
1482 */ );
|
|
1483 Vmswindows_start_process_inherit_error_mode = Qt;
|
428
|
1484 }
|