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