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.
|
442
|
5 Copyright (C) 1995, 1996, 2000 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)
|
442
|
621 invalid_argument ("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
|
|
658 /*
|
|
659 * Fork off a subprocess. P is a pointer to newly created subprocess
|
|
660 * object. If this function signals, the caller is responsible for
|
|
661 * deleting (and finalizing) the process object.
|
|
662 *
|
|
663 * The method must return PID of the new process, a (positive??? ####) number
|
|
664 * which fits into Lisp_Int. No return value indicates an error, the method
|
|
665 * must signal an error instead.
|
|
666 */
|
|
667
|
|
668 static void
|
|
669 signal_cannot_launch (Lisp_Object image_file, DWORD err)
|
|
670 {
|
|
671 mswindows_set_errno (err);
|
442
|
672 report_file_error ("Error starting", image_file);
|
|
673 }
|
|
674
|
|
675 static void
|
|
676 ensure_console_window_exists (void)
|
|
677 {
|
|
678 if (mswindows_windows9x_p ())
|
|
679 mswindows_hide_console ();
|
|
680 }
|
|
681
|
|
682 int
|
|
683 compare_env (const void *strp1, const void *strp2)
|
|
684 {
|
|
685 const char *str1 = *(const char**)strp1, *str2 = *(const char**)strp2;
|
|
686
|
|
687 while (*str1 && *str2 && *str1 != '=' && *str2 != '=')
|
|
688 {
|
|
689 if ((*str1) > (*str2))
|
|
690 return 1;
|
|
691 else if ((*str1) < (*str2))
|
|
692 return -1;
|
|
693 str1++, str2++;
|
|
694 }
|
|
695
|
|
696 if (*str1 == '=' && *str2 == '=')
|
|
697 return 0;
|
|
698 else if (*str1 == '=')
|
|
699 return -1;
|
|
700 else
|
|
701 return 1;
|
428
|
702 }
|
|
703
|
|
704 static int
|
440
|
705 nt_create_process (Lisp_Process *p,
|
428
|
706 Lisp_Object *argv, int nargv,
|
|
707 Lisp_Object program, Lisp_Object cur_dir)
|
|
708 {
|
442
|
709 /* Synched up with sys_spawnve in FSF 20.6. Significantly different
|
|
710 but still synchable. */
|
430
|
711 HANDLE hmyshove, hmyslurp, hprocin, hprocout, hprocerr;
|
442
|
712 Extbyte *command_line;
|
428
|
713 BOOL do_io, windowed;
|
|
714 char *proc_env;
|
|
715
|
442
|
716 /* No need to DOS-ize the filename; expand-file-name (called prior)
|
|
717 already does this. */
|
|
718
|
428
|
719 /* Find out whether the application is windowed or not */
|
442
|
720 if (xSHGetFileInfoA)
|
|
721 {
|
|
722 /* SHGetFileInfo tends to return ERROR_FILE_NOT_FOUND on most
|
|
723 errors. This leads to bogus error message. */
|
|
724 DWORD image_type;
|
|
725 char *p = strrchr ((char *)XSTRING_DATA (program), '.');
|
|
726 if (p != NULL &&
|
|
727 (stricmp (p, ".exe") == 0 ||
|
|
728 stricmp (p, ".com") == 0 ||
|
|
729 stricmp (p, ".bat") == 0 ||
|
|
730 stricmp (p, ".cmd") == 0))
|
|
731 {
|
|
732 image_type = xSHGetFileInfoA ((char *)XSTRING_DATA (program), 0,NULL,
|
|
733 0, SHGFI_EXETYPE);
|
|
734 }
|
|
735 else
|
|
736 {
|
558
|
737 char progname[PATH_MAX];
|
442
|
738 sprintf (progname, "%s.exe", (char *)XSTRING_DATA (program));
|
|
739 image_type = xSHGetFileInfoA (progname, 0, NULL, 0, SHGFI_EXETYPE);
|
|
740 }
|
|
741 if (image_type == 0)
|
|
742 signal_cannot_launch (program, (GetLastError () == ERROR_FILE_NOT_FOUND
|
|
743 ? ERROR_BAD_FORMAT : GetLastError ()));
|
|
744 windowed = HIWORD (image_type) != 0;
|
|
745 }
|
|
746 else /* NT 3.5; we have no idea so just guess. */
|
|
747 windowed = 0;
|
428
|
748
|
|
749 /* Decide whether to do I/O on process handles, or just mark the
|
|
750 process exited immediately upon successful launching. We do I/O if the
|
|
751 process is a console one, or if it is windowed but windowed_process_io
|
|
752 is non-zero */
|
|
753 do_io = !windowed || windowed_process_io ;
|
|
754
|
|
755 if (do_io)
|
|
756 {
|
|
757 /* Create two unidirectional named pipes */
|
|
758 HANDLE htmp;
|
|
759 SECURITY_ATTRIBUTES sa;
|
|
760
|
|
761 sa.nLength = sizeof(sa);
|
|
762 sa.bInheritHandle = TRUE;
|
|
763 sa.lpSecurityDescriptor = NULL;
|
|
764
|
|
765 CreatePipe (&hprocin, &hmyshove, &sa, 0);
|
|
766 CreatePipe (&hmyslurp, &hprocout, &sa, 0);
|
|
767
|
430
|
768 /* Duplicate the stdout handle for use as stderr */
|
442
|
769 DuplicateHandle(GetCurrentProcess(), hprocout, GetCurrentProcess(),
|
|
770 &hprocerr, 0, TRUE, DUPLICATE_SAME_ACCESS);
|
430
|
771
|
428
|
772 /* Stupid Win32 allows to create a pipe with *both* ends either
|
|
773 inheritable or not. We need process ends inheritable, and local
|
|
774 ends not inheritable. */
|
442
|
775 DuplicateHandle (GetCurrentProcess(), hmyshove, GetCurrentProcess(),
|
|
776 &htmp, 0, FALSE,
|
|
777 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
428
|
778 hmyshove = htmp;
|
442
|
779 DuplicateHandle (GetCurrentProcess(), hmyslurp, GetCurrentProcess(),
|
|
780 &htmp, 0, FALSE,
|
|
781 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
428
|
782 hmyslurp = htmp;
|
|
783 }
|
|
784
|
|
785 /* Convert an argv vector into Win32 style command line by a call to
|
442
|
786 lisp function `mswindows-construct-process-command-line'
|
|
787 (in win32-native.el) */
|
428
|
788 {
|
|
789 int i;
|
|
790 Lisp_Object args_or_ret = Qnil;
|
|
791 struct gcpro gcpro1;
|
|
792
|
|
793 GCPRO1 (args_or_ret);
|
|
794
|
|
795 for (i = 0; i < nargv; ++i)
|
|
796 args_or_ret = Fcons (*argv++, args_or_ret);
|
|
797 args_or_ret = Fnreverse (args_or_ret);
|
|
798 args_or_ret = Fcons (program, args_or_ret);
|
|
799
|
442
|
800 args_or_ret = call1 (Qmswindows_construct_process_command_line,
|
|
801 args_or_ret);
|
428
|
802
|
|
803 if (!STRINGP (args_or_ret))
|
|
804 /* Luser wrote his/her own clever version */
|
442
|
805 invalid_argument
|
|
806 ("Bogus return value from `mswindows-construct-process-command-line'",
|
|
807 args_or_ret);
|
428
|
808
|
442
|
809 LISP_STRING_TO_EXTERNAL (args_or_ret, command_line, Qmswindows_tstr);
|
428
|
810
|
|
811 UNGCPRO; /* args_or_ret */
|
|
812 }
|
|
813
|
|
814 /* Set `proc_env' to a nul-separated array of the strings in
|
|
815 Vprocess_environment terminated by 2 nuls. */
|
|
816
|
|
817 {
|
|
818 char **env;
|
|
819 REGISTER Lisp_Object tem;
|
|
820 REGISTER char **new_env;
|
|
821 REGISTER int new_length = 0, i, new_space;
|
|
822 char *penv;
|
|
823
|
|
824 for (tem = Vprocess_environment;
|
|
825 (CONSP (tem)
|
|
826 && STRINGP (XCAR (tem)));
|
|
827 tem = XCDR (tem))
|
|
828 new_length++;
|
442
|
829
|
|
830 /* FSF adds an extra env var to hold the current process ID of the
|
|
831 Emacs process. Apparently this is used only by emacsserver.c,
|
|
832 which we have superseded to gnuserv.c. (#### Does it work under
|
|
833 MS Windows?)
|
|
834
|
|
835 sprintf (ppid_env_var_buffer, "EM_PARENT_PROCESS_ID=%d",
|
|
836 GetCurrentProcessId ());
|
|
837 arglen += strlen (ppid_env_var_buffer) + 1;
|
|
838 numenv++;
|
|
839 */
|
428
|
840
|
|
841 /* new_length + 1 to include terminating 0. */
|
|
842 env = new_env = alloca_array (char *, new_length + 1);
|
|
843
|
|
844 /* Copy the Vprocess_environment strings into new_env. */
|
|
845 for (tem = Vprocess_environment;
|
|
846 (CONSP (tem)
|
|
847 && STRINGP (XCAR (tem)));
|
|
848 tem = XCDR (tem))
|
|
849 {
|
|
850 char **ep = env;
|
|
851 char *string = (char *) XSTRING_DATA (XCAR (tem));
|
|
852 /* See if this string duplicates any string already in the env.
|
|
853 If so, don't put it in.
|
|
854 When an env var has multiple definitions,
|
|
855 we keep the definition that comes first in process-environment. */
|
|
856 for (; ep != new_env; ep++)
|
|
857 {
|
|
858 char *p = *ep, *q = string;
|
|
859 while (1)
|
|
860 {
|
|
861 if (*q == 0)
|
|
862 /* The string is malformed; might as well drop it. */
|
|
863 goto duplicate;
|
|
864 if (*q != *p)
|
|
865 break;
|
|
866 if (*q == '=')
|
|
867 goto duplicate;
|
|
868 p++, q++;
|
|
869 }
|
|
870 }
|
|
871 *new_env++ = string;
|
|
872 duplicate: ;
|
|
873 }
|
|
874 *new_env = 0;
|
|
875
|
|
876 /* Sort the environment variables */
|
|
877 new_length = new_env - env;
|
|
878 qsort (env, new_length, sizeof (char *), compare_env);
|
|
879
|
|
880 /* Work out how much space to allocate */
|
|
881 new_space = 0;
|
|
882 for (i = 0; i < new_length; i++)
|
|
883 {
|
|
884 new_space += strlen(env[i]) + 1;
|
|
885 }
|
|
886 new_space++;
|
|
887
|
|
888 /* Allocate space and copy variables into it */
|
442
|
889 penv = proc_env = (char*) alloca(new_space);
|
428
|
890 for (i = 0; i < new_length; i++)
|
|
891 {
|
|
892 strcpy(penv, env[i]);
|
|
893 penv += strlen(env[i]) + 1;
|
|
894 }
|
|
895 *penv = 0;
|
|
896 }
|
442
|
897
|
|
898 #if 0
|
|
899 /* #### we need to port this. */
|
|
900 /* On Windows 95, if cmdname is a DOS app, we invoke a helper
|
|
901 application to start it by specifying the helper app as cmdname,
|
|
902 while leaving the real app name as argv[0]. */
|
|
903 if (is_dos_app)
|
|
904 {
|
|
905 cmdname = (char*) alloca (MAXPATHLEN);
|
|
906 if (egetenv ("CMDPROXY"))
|
|
907 strcpy ((char*)cmdname, egetenv ("CMDPROXY"));
|
|
908 else
|
|
909 {
|
|
910 strcpy ((char*)cmdname, XSTRING_DATA (Vinvocation_directory));
|
|
911 strcat ((char*)cmdname, "cmdproxy.exe");
|
|
912 }
|
|
913 }
|
|
914 #endif
|
428
|
915
|
|
916 /* Create process */
|
|
917 {
|
|
918 STARTUPINFO si;
|
|
919 PROCESS_INFORMATION pi;
|
|
920 DWORD err;
|
442
|
921 DWORD flags;
|
428
|
922
|
|
923 xzero (si);
|
|
924 si.dwFlags = STARTF_USESHOWWINDOW;
|
|
925 si.wShowWindow = windowed ? SW_SHOWNORMAL : SW_HIDE;
|
|
926 if (do_io)
|
|
927 {
|
|
928 si.hStdInput = hprocin;
|
|
929 si.hStdOutput = hprocout;
|
430
|
930 si.hStdError = hprocerr;
|
428
|
931 si.dwFlags |= STARTF_USESTDHANDLES;
|
|
932 }
|
|
933
|
442
|
934 flags = CREATE_SUSPENDED;
|
|
935 if (mswindows_windows9x_p ())
|
|
936 flags |= (!NILP (Vmswindows_start_process_share_console)
|
|
937 ? CREATE_NEW_PROCESS_GROUP
|
|
938 : CREATE_NEW_CONSOLE);
|
|
939 else
|
|
940 flags |= CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP;
|
|
941 if (NILP (Vmswindows_start_process_inherit_error_mode))
|
|
942 flags |= CREATE_DEFAULT_ERROR_MODE;
|
|
943
|
|
944 ensure_console_window_exists ();
|
|
945
|
|
946 err = (CreateProcess (NULL, command_line, NULL, NULL, TRUE, flags,
|
428
|
947 proc_env, (char *) XSTRING_DATA (cur_dir), &si, &pi)
|
|
948 ? 0 : GetLastError ());
|
|
949
|
|
950 if (do_io)
|
|
951 {
|
|
952 /* These just have been inherited; we do not need a copy */
|
|
953 CloseHandle (hprocin);
|
|
954 CloseHandle (hprocout);
|
430
|
955 CloseHandle (hprocerr);
|
428
|
956 }
|
|
957
|
|
958 /* Handle process creation failure */
|
|
959 if (err)
|
|
960 {
|
|
961 if (do_io)
|
|
962 {
|
|
963 CloseHandle (hmyshove);
|
|
964 CloseHandle (hmyslurp);
|
|
965 }
|
|
966 signal_cannot_launch (program, GetLastError ());
|
|
967 }
|
|
968
|
|
969 /* The process started successfully */
|
|
970 if (do_io)
|
|
971 {
|
|
972 NT_DATA(p)->h_process = pi.hProcess;
|
442
|
973 NT_DATA(p)->dwProcessId = pi.dwProcessId;
|
428
|
974 init_process_io_handles (p, (void*)hmyslurp, (void*)hmyshove, 0);
|
|
975 }
|
|
976 else
|
|
977 {
|
|
978 /* Indicate as if the process has exited immediately. */
|
|
979 p->status_symbol = Qexit;
|
|
980 CloseHandle (pi.hProcess);
|
|
981 }
|
|
982
|
442
|
983 if (!windowed)
|
|
984 enable_child_signals (pi.hProcess);
|
|
985
|
428
|
986 ResumeThread (pi.hThread);
|
|
987 CloseHandle (pi.hThread);
|
|
988
|
432
|
989 return ((int)pi.dwProcessId);
|
428
|
990 }
|
|
991 }
|
|
992
|
|
993 /*
|
|
994 * This method is called to update status fields of the process
|
|
995 * structure. If the process has not existed, this method is expected
|
|
996 * to do nothing.
|
|
997 *
|
|
998 * The method is called only for real child processes.
|
|
999 */
|
|
1000
|
|
1001 static void
|
440
|
1002 nt_update_status_if_terminated (Lisp_Process* p)
|
428
|
1003 {
|
|
1004 DWORD exit_code;
|
|
1005 if (GetExitCodeProcess (NT_DATA(p)->h_process, &exit_code)
|
|
1006 && exit_code != STILL_ACTIVE)
|
|
1007 {
|
|
1008 p->tick++;
|
|
1009 p->core_dumped = 0;
|
|
1010 /* The exit code can be a code returned by process, or an
|
|
1011 NTSTATUS value. We cannot accurately handle the latter since
|
|
1012 it is a full 32 bit integer */
|
|
1013 if (exit_code & 0xC0000000)
|
|
1014 {
|
|
1015 p->status_symbol = Qsignal;
|
|
1016 p->exit_code = exit_code & 0x1FFFFFFF;
|
|
1017 }
|
|
1018 else
|
|
1019 {
|
|
1020 p->status_symbol = Qexit;
|
|
1021 p->exit_code = exit_code;
|
|
1022 }
|
|
1023 }
|
|
1024 }
|
|
1025
|
|
1026 /*
|
|
1027 * Stuff the entire contents of LSTREAM to the process output pipe
|
|
1028 */
|
|
1029
|
|
1030 /* #### If only this function could be somehow merged with
|
|
1031 unix_send_process... */
|
|
1032
|
|
1033 static void
|
|
1034 nt_send_process (Lisp_Object proc, struct lstream* lstream)
|
|
1035 {
|
432
|
1036 volatile Lisp_Object vol_proc = proc;
|
440
|
1037 Lisp_Process *volatile p = XPROCESS (proc);
|
428
|
1038
|
|
1039 /* use a reasonable-sized buffer (somewhere around the size of the
|
|
1040 stream buffer) so as to avoid inundating the stream with blocked
|
|
1041 data. */
|
442
|
1042 Bufbyte chunkbuf[512];
|
428
|
1043 Bytecount chunklen;
|
|
1044
|
|
1045 while (1)
|
|
1046 {
|
462
|
1047 Lstream_data_count writeret;
|
428
|
1048
|
442
|
1049 chunklen = Lstream_read (lstream, chunkbuf, 512);
|
428
|
1050 if (chunklen <= 0)
|
|
1051 break; /* perhaps should abort() if < 0?
|
|
1052 This should never happen. */
|
|
1053
|
|
1054 /* Lstream_write() will never successfully write less than the
|
|
1055 amount sent in. In the worst case, it just buffers the
|
|
1056 unwritten data. */
|
|
1057 writeret = Lstream_write (XLSTREAM (DATA_OUTSTREAM(p)), chunkbuf,
|
|
1058 chunklen);
|
|
1059 Lstream_flush (XLSTREAM (DATA_OUTSTREAM(p)));
|
|
1060 if (writeret < 0)
|
|
1061 {
|
|
1062 p->status_symbol = Qexit;
|
|
1063 p->exit_code = ERROR_BROKEN_PIPE;
|
|
1064 p->core_dumped = 0;
|
|
1065 p->tick++;
|
|
1066 process_tick++;
|
432
|
1067 deactivate_process (*((Lisp_Object *) (&vol_proc)));
|
442
|
1068 invalid_operation ("Broken pipe error sending to process; closed it",
|
|
1069 p->name);
|
428
|
1070 }
|
|
1071
|
|
1072 {
|
|
1073 int wait_ms = 25;
|
|
1074 while (Lstream_was_blocked_p (XLSTREAM (p->pipe_outstream)))
|
|
1075 {
|
|
1076 /* Buffer is full. Wait, accepting input; that may allow
|
|
1077 the program to finish doing output and read more. */
|
|
1078 Faccept_process_output (Qnil, Qzero, make_int (wait_ms));
|
|
1079 Lstream_flush (XLSTREAM (p->pipe_outstream));
|
|
1080 wait_ms = min (1000, 2 * wait_ms);
|
|
1081 }
|
|
1082 }
|
|
1083 }
|
|
1084 }
|
|
1085
|
|
1086 /*
|
|
1087 * Send a signal number SIGNO to PROCESS.
|
|
1088 * CURRENT_GROUP means send to the process group that currently owns
|
|
1089 * the terminal being used to communicate with PROCESS.
|
|
1090 * This is used for various commands in shell mode.
|
|
1091 * If NOMSG is zero, insert signal-announcements into process's buffers
|
|
1092 * right away.
|
|
1093 *
|
|
1094 * If we can, we try to signal PROCESS by sending control characters
|
|
1095 * down the pty. This allows us to signal inferiors who have changed
|
|
1096 * their uid, for which killpg would return an EPERM error.
|
|
1097 *
|
|
1098 * The method signals an error if the given SIGNO is not valid
|
|
1099 */
|
|
1100
|
|
1101 static void
|
|
1102 nt_kill_child_process (Lisp_Object proc, int signo,
|
|
1103 int current_group, int nomsg)
|
|
1104 {
|
440
|
1105 Lisp_Process *p = XPROCESS (proc);
|
428
|
1106
|
|
1107 /* Signal error if SIGNO cannot be sent */
|
|
1108 validate_signal_number (signo);
|
|
1109
|
|
1110 /* Send signal */
|
442
|
1111 if (!send_signal (NT_DATA (p), 0, signo))
|
|
1112 invalid_operation ("Cannot send signal to process", proc);
|
428
|
1113 }
|
|
1114
|
|
1115 /*
|
442
|
1116 * Kill any process in the system given its PID
|
428
|
1117 *
|
|
1118 * Returns zero if a signal successfully sent, or
|
|
1119 * negative number upon failure
|
|
1120 */
|
|
1121 static int
|
|
1122 nt_kill_process_by_pid (int pid, int signo)
|
|
1123 {
|
442
|
1124 struct Lisp_Process *p;
|
|
1125
|
428
|
1126 /* Signal error if SIGNO cannot be sent */
|
|
1127 validate_signal_number (signo);
|
|
1128
|
442
|
1129 p = find_process_from_pid (pid);
|
|
1130 return send_signal (p ? NT_DATA (p) : 0, pid, signo) ? 0 : -1;
|
428
|
1131 }
|
|
1132
|
|
1133 /*-----------------------------------------------------------------------*/
|
|
1134 /* Sockets connections */
|
|
1135 /*-----------------------------------------------------------------------*/
|
|
1136 #ifdef HAVE_SOCKETS
|
|
1137
|
|
1138 /* #### Hey MS, how long Winsock 2 for '95 will be in beta? */
|
|
1139
|
|
1140 #define SOCK_TIMER_ID 666
|
|
1141 #define XM_SOCKREPLY (WM_USER + 666)
|
|
1142
|
|
1143 static int
|
|
1144 get_internet_address (Lisp_Object host, struct sockaddr_in *address,
|
|
1145 Error_behavior errb)
|
|
1146 {
|
|
1147 char buf [MAXGETHOSTSTRUCT];
|
|
1148 HWND hwnd;
|
|
1149 HANDLE hasync;
|
|
1150 int success = 0;
|
|
1151
|
|
1152 address->sin_family = AF_INET;
|
|
1153
|
|
1154 /* First check if HOST is already a numeric address */
|
|
1155 {
|
|
1156 unsigned long inaddr = inet_addr (XSTRING_DATA (host));
|
|
1157 if (inaddr != INADDR_NONE)
|
|
1158 {
|
|
1159 address->sin_addr.s_addr = inaddr;
|
|
1160 return 1;
|
|
1161 }
|
|
1162 }
|
|
1163
|
|
1164 /* Create a window which will receive completion messages */
|
|
1165 hwnd = CreateWindow ("STATIC", NULL, WS_OVERLAPPED, 0, 0, 1, 1,
|
|
1166 NULL, NULL, NULL, NULL);
|
|
1167 assert (hwnd);
|
|
1168
|
|
1169 /* Post name resolution request */
|
|
1170 hasync = WSAAsyncGetHostByName (hwnd, XM_SOCKREPLY, XSTRING_DATA (host),
|
|
1171 buf, sizeof (buf));
|
|
1172 if (hasync == NULL)
|
|
1173 goto done;
|
|
1174
|
|
1175 /* Set a timer to poll for quit every 250 ms */
|
|
1176 SetTimer (hwnd, SOCK_TIMER_ID, 250, NULL);
|
|
1177
|
|
1178 while (1)
|
|
1179 {
|
|
1180 MSG msg;
|
|
1181 GetMessage (&msg, hwnd, 0, 0);
|
|
1182 if (msg.message == XM_SOCKREPLY)
|
|
1183 {
|
|
1184 /* Ok, got an answer */
|
|
1185 if (WSAGETASYNCERROR(msg.lParam) == NO_ERROR)
|
|
1186 success = 1;
|
432
|
1187 else
|
|
1188 {
|
|
1189 warn_when_safe(Qstream, Qwarning,
|
|
1190 "cannot get IP address for host \"%s\"",
|
|
1191 XSTRING_DATA (host));
|
|
1192 }
|
428
|
1193 goto done;
|
|
1194 }
|
|
1195 else if (msg.message == WM_TIMER && msg.wParam == SOCK_TIMER_ID)
|
|
1196 {
|
|
1197 if (QUITP)
|
|
1198 {
|
|
1199 WSACancelAsyncRequest (hasync);
|
|
1200 KillTimer (hwnd, SOCK_TIMER_ID);
|
|
1201 DestroyWindow (hwnd);
|
|
1202 REALLY_QUIT;
|
|
1203 }
|
|
1204 }
|
|
1205 DispatchMessage (&msg);
|
|
1206 }
|
|
1207
|
|
1208 done:
|
|
1209 KillTimer (hwnd, SOCK_TIMER_ID);
|
|
1210 DestroyWindow (hwnd);
|
|
1211 if (success)
|
|
1212 {
|
|
1213 /* BUF starts with struct hostent */
|
|
1214 struct hostent* he = (struct hostent*) buf;
|
|
1215 address->sin_addr.s_addr = *(unsigned long*)he->h_addr_list[0];
|
|
1216 }
|
|
1217 return success;
|
|
1218 }
|
|
1219
|
|
1220 static Lisp_Object
|
|
1221 nt_canonicalize_host_name (Lisp_Object host)
|
|
1222 {
|
|
1223 struct sockaddr_in address;
|
|
1224
|
|
1225 if (!get_internet_address (host, &address, ERROR_ME_NOT))
|
|
1226 return host;
|
|
1227
|
|
1228 if (address.sin_family == AF_INET)
|
|
1229 return build_string (inet_ntoa (address.sin_addr));
|
|
1230 else
|
|
1231 return host;
|
|
1232 }
|
|
1233
|
|
1234 /* open a TCP network connection to a given HOST/SERVICE. Treated
|
|
1235 exactly like a normal process when reading and writing. Only
|
|
1236 differences are in status display and process deletion. A network
|
|
1237 connection has no PID; you cannot signal it. All you can do is
|
|
1238 deactivate and close it via delete-process */
|
|
1239
|
|
1240 static void
|
442
|
1241 nt_open_network_stream (Lisp_Object name, Lisp_Object host,
|
|
1242 Lisp_Object service,
|
428
|
1243 Lisp_Object protocol, void** vinfd, void** voutfd)
|
|
1244 {
|
442
|
1245 /* !!#### not Mule-ized */
|
428
|
1246 struct sockaddr_in address;
|
|
1247 SOCKET s;
|
|
1248 int port;
|
|
1249 int retval;
|
|
1250
|
|
1251 CHECK_STRING (host);
|
|
1252
|
|
1253 if (!EQ (protocol, Qtcp))
|
442
|
1254 invalid_argument ("Unsupported protocol", protocol);
|
428
|
1255
|
|
1256 if (INTP (service))
|
|
1257 port = htons ((unsigned short) XINT (service));
|
|
1258 else
|
|
1259 {
|
|
1260 struct servent *svc_info;
|
|
1261 CHECK_STRING (service);
|
|
1262 svc_info = getservbyname ((char *) XSTRING_DATA (service), "tcp");
|
|
1263 if (svc_info == 0)
|
442
|
1264 invalid_argument ("Unknown service", service);
|
428
|
1265 port = svc_info->s_port;
|
|
1266 }
|
|
1267
|
|
1268 get_internet_address (host, &address, ERROR_ME);
|
|
1269 address.sin_port = port;
|
|
1270
|
|
1271 s = socket (address.sin_family, SOCK_STREAM, 0);
|
|
1272 if (s < 0)
|
|
1273 report_file_error ("error creating socket", list1 (name));
|
|
1274
|
|
1275 /* We don't want to be blocked on connect */
|
|
1276 {
|
|
1277 unsigned long nonblock = 1;
|
|
1278 ioctlsocket (s, FIONBIO, &nonblock);
|
|
1279 }
|
|
1280
|
|
1281 retval = connect (s, (struct sockaddr *) &address, sizeof (address));
|
|
1282 if (retval != NO_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
|
|
1283 goto connect_failed;
|
|
1284 /* Wait while connection is established */
|
|
1285 while (1)
|
|
1286 {
|
|
1287 fd_set fdset;
|
|
1288 struct timeval tv;
|
|
1289 int nsel;
|
|
1290
|
|
1291 if (QUITP)
|
|
1292 {
|
|
1293 closesocket (s);
|
|
1294 REALLY_QUIT;
|
|
1295 }
|
|
1296
|
|
1297 /* Poll for quit every 250 ms */
|
|
1298 tv.tv_sec = 0;
|
|
1299 tv.tv_usec = 250 * 1000;
|
|
1300
|
|
1301 FD_ZERO (&fdset);
|
|
1302 FD_SET (s, &fdset);
|
|
1303 nsel = select (0, NULL, &fdset, &fdset, &tv);
|
|
1304
|
|
1305 if (nsel > 0)
|
|
1306 {
|
|
1307 /* Check: was connection successful or not? */
|
|
1308 tv.tv_usec = 0;
|
|
1309 nsel = select (0, NULL, NULL, &fdset, &tv);
|
|
1310 if (nsel > 0)
|
|
1311 goto connect_failed;
|
|
1312 else
|
|
1313 break;
|
|
1314 }
|
|
1315 }
|
|
1316
|
|
1317 /* We are connected at this point */
|
|
1318 *vinfd = (void*)s;
|
|
1319 DuplicateHandle (GetCurrentProcess(), (HANDLE)s,
|
|
1320 GetCurrentProcess(), (LPHANDLE)voutfd,
|
|
1321 0, FALSE, DUPLICATE_SAME_ACCESS);
|
|
1322 return;
|
|
1323
|
|
1324 connect_failed:
|
|
1325 closesocket (s);
|
442
|
1326 if (INTP (service))
|
|
1327 {
|
|
1328 warn_when_safe (Qstream, Qwarning,
|
|
1329 "failure to open network stream to host \"%s\" for service \"%d\"",
|
|
1330 XSTRING_DATA (host),
|
|
1331 (unsigned short) XINT (service));
|
|
1332 }
|
|
1333 else
|
|
1334 {
|
|
1335 warn_when_safe (Qstream, Qwarning,
|
|
1336 "failure to open network stream to host \"%s\" for service \"%s\"",
|
|
1337 XSTRING_DATA (host),
|
|
1338 XSTRING_DATA (service));
|
|
1339 }
|
428
|
1340 report_file_error ("connection failed", list2 (host, name));
|
|
1341 }
|
|
1342
|
|
1343 #endif
|
|
1344
|
|
1345 /*-----------------------------------------------------------------------*/
|
|
1346 /* Initialization */
|
|
1347 /*-----------------------------------------------------------------------*/
|
|
1348
|
|
1349 void
|
|
1350 process_type_create_nt (void)
|
|
1351 {
|
|
1352 PROCESS_HAS_METHOD (nt, alloc_process_data);
|
|
1353 PROCESS_HAS_METHOD (nt, finalize_process_data);
|
|
1354 PROCESS_HAS_METHOD (nt, init_process);
|
|
1355 PROCESS_HAS_METHOD (nt, create_process);
|
|
1356 PROCESS_HAS_METHOD (nt, update_status_if_terminated);
|
|
1357 PROCESS_HAS_METHOD (nt, send_process);
|
|
1358 PROCESS_HAS_METHOD (nt, kill_child_process);
|
|
1359 PROCESS_HAS_METHOD (nt, kill_process_by_pid);
|
|
1360 #ifdef HAVE_SOCKETS
|
|
1361 PROCESS_HAS_METHOD (nt, canonicalize_host_name);
|
|
1362 PROCESS_HAS_METHOD (nt, open_network_stream);
|
|
1363 #ifdef HAVE_MULTICAST
|
|
1364 #error I won't do this until '95 has winsock2
|
|
1365 PROCESS_HAS_METHOD (nt, open_multicast_group);
|
|
1366 #endif
|
|
1367 #endif
|
|
1368 }
|
|
1369
|
|
1370 void
|
|
1371 syms_of_process_nt (void)
|
|
1372 {
|
442
|
1373 DEFSYMBOL (Qmswindows_construct_process_command_line);
|
428
|
1374 }
|
|
1375
|
|
1376 void
|
|
1377 vars_of_process_nt (void)
|
|
1378 {
|
442
|
1379 DEFVAR_LISP ("mswindows-start-process-share-console",
|
|
1380 &Vmswindows_start_process_share_console /*
|
|
1381 When nil, new child processes are given a new console.
|
|
1382 When non-nil, they share the Emacs console; this has the limitation of
|
|
1383 allowing only only DOS subprocess to run at a time (whether started directly
|
|
1384 or indirectly by Emacs), and preventing Emacs from cleanly terminating the
|
|
1385 subprocess group, but may allow Emacs to interrupt a subprocess that doesn't
|
|
1386 otherwise respond to interrupts from Emacs.
|
|
1387 */ );
|
|
1388 Vmswindows_start_process_share_console = Qnil;
|
|
1389
|
|
1390 DEFVAR_LISP ("mswindows-start-process-inherit-error-mode",
|
|
1391 &Vmswindows_start_process_inherit_error_mode /*
|
|
1392 "When nil, new child processes revert to the default error mode.
|
|
1393 When non-nil, they inherit their error mode setting from Emacs, which stops
|
|
1394 them blocking when trying to access unmounted drives etc.
|
|
1395 */ );
|
|
1396 Vmswindows_start_process_inherit_error_mode = Qt;
|
428
|
1397 }
|