274
|
1 /* Asynchronous subprocess implemenation 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.
|
|
5 Copyright (C) 1995, 1996 Ben Wing.
|
|
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
|
|
29 #include "hash.h"
|
|
30 #include "lstream.h"
|
|
31 #include "process.h"
|
|
32 #include "procimpl.h"
|
278
|
33 #include "sysdep.h"
|
274
|
34
|
|
35 #include <windows.h>
|
278
|
36 #include <shellapi.h>
|
280
|
37 #include <signal.h>
|
274
|
38
|
|
39 /* Implemenation-specific data. Pointed to by Lisp_Process->process_data */
|
|
40 struct nt_process_data
|
|
41 {
|
|
42 HANDLE h_process;
|
|
43 };
|
|
44
|
|
45 #define NT_DATA(p) ((struct nt_process_data*)((p)->process_data))
|
|
46
|
|
47 /*-----------------------------------------------------------------------*/
|
|
48 /* Process helpers */
|
|
49 /*-----------------------------------------------------------------------*/
|
|
50
|
276
|
51 /* This one breaks process abstraction. Prototype is in console-msw.h,
|
|
52 used by select_process method in event-msw.c */
|
|
53 HANDLE
|
|
54 get_nt_process_handle (struct Lisp_Process *p)
|
|
55 {
|
|
56 return (NT_DATA (p)->h_process);
|
|
57 }
|
280
|
58
|
|
59 /*-----------------------------------------------------------------------*/
|
|
60 /* Running remote threads. See Microsoft Systems Journal 1994 Number 5 */
|
|
61 /* Jeffrey Richter, Load Your 32-bit DLL into Another Process's Address..*/
|
|
62 /*-----------------------------------------------------------------------*/
|
274
|
63
|
280
|
64 typedef struct
|
|
65 {
|
|
66 HANDLE h_process;
|
|
67 HANDLE h_thread;
|
|
68 LPVOID address;
|
|
69 } process_memory;
|
|
70
|
|
71 /*
|
|
72 * Allocate SIZE bytes in H_PROCESS address space. Fill in PMC used
|
|
73 * further by other routines. Return nonzero if successful.
|
|
74 *
|
|
75 * The memory in other process is allocated by creating a suspended
|
|
76 * thread. Initial stack of that thread is used as the memory
|
|
77 * block. The thread entry point is the routine ExitThread in
|
|
78 * kernel32.dll, so the allocated memory is freed just by resuming the
|
|
79 * thread, which immediately terminates after that.
|
|
80 */
|
|
81
|
|
82 static int
|
|
83 alloc_process_memory (HANDLE h_process, size_t size,
|
|
84 process_memory* pmc)
|
|
85 {
|
|
86 LPTHREAD_START_ROUTINE adr_ExitThread =
|
|
87 (LPTHREAD_START_ROUTINE)
|
|
88 GetProcAddress (GetModuleHandle ("kernel32"), "ExitThread");
|
|
89 DWORD dw_unused;
|
|
90 CONTEXT context;
|
|
91 MEMORY_BASIC_INFORMATION mbi;
|
|
92
|
|
93 pmc->h_process = h_process;
|
|
94 pmc->h_thread = CreateRemoteThread (h_process, NULL, size,
|
|
95 adr_ExitThread, NULL,
|
|
96 CREATE_SUSPENDED, &dw_unused);
|
|
97 if (pmc->h_thread == NULL)
|
|
98 return 0;
|
|
99
|
|
100 /* Get context, for thread's stack pointer */
|
|
101 context.ContextFlags = CONTEXT_CONTROL;
|
|
102 if (!GetThreadContext (pmc->h_thread, &context))
|
|
103 goto failure;
|
|
104
|
|
105 /* Determine base address of the committed range */
|
|
106 if (sizeof(mbi) != VirtualQueryEx (h_process,
|
|
107 #if defined (_X86_)
|
|
108 (LPDWORD)context.Esp - 1,
|
|
109 #elif defined (_ALPHA_)
|
|
110 (LPDWORD)context.IntSp - 1,
|
|
111 #else
|
|
112 #error Unknown processor architecture
|
|
113 #endif
|
|
114 &mbi, sizeof(mbi)))
|
|
115 goto failure;
|
|
116
|
|
117 /* Change the page protection of the allocated memory to executable,
|
|
118 read, and write. */
|
|
119 if (!VirtualProtectEx (h_process, mbi.BaseAddress, size,
|
|
120 PAGE_EXECUTE_READWRITE, &dw_unused))
|
|
121 goto failure;
|
|
122
|
|
123 pmc->address = mbi.BaseAddress;
|
|
124 return 1;
|
|
125
|
|
126 failure:
|
|
127 ResumeThread (pmc->h_thread);
|
|
128 pmc->address = 0;
|
|
129 return 0;
|
|
130 }
|
|
131
|
|
132 static void
|
|
133 free_process_memory (process_memory* pmc)
|
|
134 {
|
|
135 ResumeThread (pmc->h_thread);
|
|
136 }
|
|
137
|
|
138 /*
|
|
139 * Run ROUTINE in the context of process determined by H_PROCESS. The
|
|
140 * routine is passed the address of DATA as parameter. CODE_END is the
|
|
141 * address immediately after ROUTINE's code. DATA_SIZE is the size of
|
|
142 * DATA structure.
|
|
143 *
|
|
144 * Note that the code must be positionally independent, and compiled
|
|
145 * without stack checks (they cause implicit calls into CRT so will
|
|
146 * fail). DATA should not refer any data in calling process, as both
|
|
147 * routine and its data are copied into remote process. Size of data
|
|
148 * and code together should not exceed one page (4K on x86 systems).
|
|
149 *
|
|
150 * Return the value returned by ROUTINE, or (DWORD)-1 if call failed.
|
|
151 */
|
|
152 static DWORD
|
|
153 run_in_other_process (HANDLE h_process,
|
|
154 LPTHREAD_START_ROUTINE routine, LPVOID code_end,
|
|
155 LPVOID data, size_t data_size)
|
|
156 {
|
|
157 process_memory pm;
|
|
158 size_t code_size = (LPBYTE)code_end - (LPBYTE)routine;
|
|
159 /* Need at most 3 extra bytes of memory, for data alignment */
|
|
160 size_t total_size = code_size + data_size + 3;
|
|
161 LPVOID remote_data;
|
|
162 HANDLE h_thread;
|
|
163 DWORD dw_unused;
|
|
164
|
|
165 /* Allocate memory */
|
|
166 if (!alloc_process_memory (h_process, total_size, &pm))
|
|
167 return (DWORD)-1;
|
|
168
|
|
169 /* Copy code */
|
|
170 if (!WriteProcessMemory (h_process, pm.address, (LPVOID)routine,
|
|
171 code_size, NULL))
|
|
172 goto failure;
|
|
173
|
|
174 /* Copy data */
|
|
175 if (data_size)
|
|
176 {
|
|
177 remote_data = (LPBYTE)pm.address + ((code_size + 4) & ~3);
|
|
178 if (!WriteProcessMemory (h_process, remote_data, data, data_size, NULL))
|
|
179 goto failure;
|
|
180 }
|
|
181 else
|
|
182 remote_data = NULL;
|
|
183
|
|
184 /* Execute the remote copy of code, passing it remote data */
|
|
185 h_thread = CreateRemoteThread (h_process, NULL, 0,
|
|
186 (LPTHREAD_START_ROUTINE) pm.address,
|
|
187 remote_data, 0, &dw_unused);
|
|
188 if (h_thread == NULL)
|
|
189 goto failure;
|
|
190
|
|
191 /* Wait till thread finishes */
|
|
192 WaitForSingleObject (h_thread, INFINITE);
|
|
193
|
|
194 /* Free remote memory */
|
|
195 free_process_memory (&pm);
|
|
196
|
|
197 /* Return thread's exit code */
|
|
198 {
|
|
199 DWORD exit_code;
|
|
200 GetExitCodeThread (h_thread, &exit_code);
|
|
201 CloseHandle (h_thread);
|
|
202 return exit_code;
|
|
203 }
|
|
204
|
|
205 failure:
|
|
206 free_process_memory (&pm);
|
|
207 return (DWORD)-1;
|
|
208 }
|
|
209
|
|
210 /*-----------------------------------------------------------------------*/
|
|
211 /* Sending signals */
|
|
212 /*-----------------------------------------------------------------------*/
|
|
213
|
|
214 /*
|
|
215 * We handle the following signals:
|
|
216 *
|
|
217 * SIGKILL, SIGTERM, SIGQUIT - These three translate to ExitProcess
|
|
218 * executed by the remote process
|
|
219 * SIGINT - The remote process is sent CTRL_BREAK_EVENT
|
|
220 */
|
|
221
|
|
222 /*
|
|
223 * Sending SIGKILL
|
|
224 */
|
|
225 typedef struct
|
|
226 {
|
|
227 void (WINAPI *adr_ExitProcess) (UINT);
|
|
228 } sigkill_data;
|
|
229
|
|
230 static DWORD WINAPI
|
|
231 sigkill_proc (sigkill_data* data)
|
|
232 {
|
|
233 (*data->adr_ExitProcess)(255);
|
|
234 return 1;
|
|
235 }
|
|
236
|
|
237 /* Watermark in code space */
|
|
238 static void
|
|
239 sigkill_code_end (void)
|
|
240 {
|
|
241 }
|
|
242
|
|
243 /*
|
|
244 * Sending break or control c
|
|
245 */
|
|
246 typedef struct
|
|
247 {
|
|
248 BOOL (WINAPI *adr_GenerateConsoleCtrlEvent) (DWORD, DWORD);
|
|
249 DWORD event;
|
|
250 } sigint_data;
|
|
251
|
|
252 static DWORD WINAPI
|
|
253 sigint_proc (sigint_data* data)
|
|
254 {
|
|
255 return (*data->adr_GenerateConsoleCtrlEvent) (data->event, 0);
|
|
256 }
|
|
257
|
|
258 /* Watermark in code space */
|
|
259 static void
|
|
260 sigint_code_end (void)
|
|
261 {
|
|
262 }
|
|
263
|
|
264 /*
|
|
265 * Enabling signals
|
|
266 */
|
|
267 typedef struct
|
|
268 {
|
|
269 BOOL (WINAPI *adr_SetConsoleCtrlHandler) (LPVOID, BOOL);
|
|
270 } sig_enable_data;
|
|
271
|
|
272 static DWORD WINAPI
|
|
273 sig_enable_proc (sig_enable_data* data)
|
|
274 {
|
|
275 (*data->adr_SetConsoleCtrlHandler) (NULL, FALSE);
|
|
276 return 1;
|
|
277 }
|
|
278
|
|
279 /* Watermark in code space */
|
|
280 static void
|
|
281 sig_enable_code_end (void)
|
|
282 {
|
|
283 }
|
|
284
|
|
285 /*
|
|
286 * Send signal SIGNO to process H_PROCESS.
|
|
287 * Return nonzero if successful.
|
|
288 */
|
|
289
|
|
290 /* This code assigns a return value of GetProcAddress to function pointers
|
|
291 of many different types. Instead of heavy obscure casts, we just disable
|
|
292 warnings about assignments to different function pointer types. */
|
|
293 #pragma warning (disable : 4113)
|
|
294
|
|
295 static int
|
|
296 send_signal (HANDLE h_process, int signo)
|
|
297 {
|
|
298 HMODULE h_kernel = GetModuleHandle ("kernel32");
|
|
299 DWORD retval;
|
|
300
|
|
301 assert (h_kernel != NULL);
|
|
302
|
|
303 switch (signo)
|
|
304 {
|
|
305 case SIGKILL:
|
|
306 case SIGTERM:
|
|
307 case SIGQUIT:
|
|
308 {
|
|
309 sigkill_data d;
|
|
310 d.adr_ExitProcess = GetProcAddress (h_kernel, "ExitProcess");
|
|
311 assert (d.adr_ExitProcess);
|
|
312 retval = run_in_other_process (h_process,
|
|
313 sigkill_proc, sigkill_code_end,
|
|
314 &d, sizeof (d));
|
|
315 break;
|
|
316 }
|
|
317 case SIGINT:
|
|
318 {
|
|
319 sigint_data d;
|
|
320 d.adr_GenerateConsoleCtrlEvent =
|
|
321 GetProcAddress (h_kernel, "GenerateConsoleCtrlEvent");
|
|
322 assert (d.adr_GenerateConsoleCtrlEvent);
|
|
323 d.event = CTRL_C_EVENT;
|
|
324 retval = run_in_other_process (h_process,
|
|
325 sigint_proc, sigint_code_end,
|
|
326 &d, sizeof (d));
|
|
327 break;
|
|
328 }
|
|
329 default:
|
|
330 assert (0);
|
|
331 }
|
|
332
|
|
333 return (int)retval > 0 ? 1 : 0;
|
|
334 }
|
|
335
|
|
336 /*
|
|
337 * Enable CTRL_C_EVENT handling in a new child process
|
|
338 */
|
|
339 static void
|
|
340 enable_child_signals (HANDLE h_process)
|
|
341 {
|
|
342 HMODULE h_kernel = GetModuleHandle ("kernel32");
|
|
343 sig_enable_data d;
|
|
344
|
|
345 assert (h_kernel != NULL);
|
|
346 d.adr_SetConsoleCtrlHandler =
|
|
347 GetProcAddress (h_kernel, "SetConsoleCtrlHandler");
|
|
348 assert (d.adr_SetConsoleCtrlHandler);
|
|
349 run_in_other_process (h_process,
|
|
350 sig_enable_proc, sig_enable_code_end,
|
|
351 &d, sizeof (d));
|
|
352 }
|
|
353
|
|
354 #pragma warning (default : 4113)
|
|
355
|
|
356 /*
|
|
357 * Signal error if SIGNO is not supported
|
|
358 */
|
|
359 static void
|
|
360 validate_signal_number (int signo)
|
|
361 {
|
|
362 if (signo != SIGKILL && signo != SIGTERM
|
|
363 && signo != SIGQUIT && signo != SIGINT)
|
|
364 error ("Signal number %d not supported", signo);
|
|
365 }
|
|
366
|
274
|
367 /*-----------------------------------------------------------------------*/
|
|
368 /* Process methods */
|
|
369 /*-----------------------------------------------------------------------*/
|
|
370
|
|
371 /*
|
|
372 * Allocate and initialize Lisp_Process->process_data
|
|
373 */
|
|
374
|
|
375 static void
|
|
376 nt_alloc_process_data (struct Lisp_Process *p)
|
|
377 {
|
|
378 p->process_data = xnew (struct nt_process_data);
|
|
379 }
|
|
380
|
|
381 #if 0 /* #### Need this method? */
|
|
382 /*
|
|
383 * Mark any Lisp objects in Lisp_Process->process_data
|
|
384 */
|
|
385
|
|
386 static void
|
|
387 nt_mark_process_data (struct Lisp_Process *proc,
|
|
388 void (*markobj) (Lisp_Object))
|
|
389 {
|
|
390 }
|
|
391 #endif
|
|
392
|
|
393 static void
|
|
394 nt_finalize_process_data (struct Lisp_Process *p, int for_disksave)
|
|
395 {
|
|
396 assert (!for_disksave);
|
|
397 if (NT_DATA(p)->h_process)
|
|
398 CloseHandle (NT_DATA(p)->h_process);
|
|
399 }
|
|
400
|
|
401 #if 0 /* #### Need this method? */
|
|
402 /*
|
|
403 * Initialize XEmacs process implemenation once
|
|
404 */
|
|
405
|
|
406 static void
|
|
407 nt_init_process (void)
|
|
408 {
|
|
409 }
|
|
410 #endif
|
|
411
|
|
412 #if 0 /* #### Need this method? */
|
|
413 /*
|
|
414 * Initialize any process local data. This is called when newly
|
|
415 * created process is connected to real OS file handles. The
|
|
416 * handles are generally represented by void* type, but are
|
|
417 * of type HANDLE for Win32
|
|
418 */
|
|
419
|
|
420 static void
|
|
421 nt_init_process_io_handles (struct Lisp_Process *p, void* in, void* out, int flags)
|
|
422 {
|
|
423 }
|
|
424 #endif
|
|
425
|
|
426 /*
|
|
427 * Fork off a subprocess. P is a pointer to newly created subprocess
|
|
428 * object. If this function signals, the caller is responsible for
|
|
429 * deleting (and finalizing) the process object.
|
|
430 *
|
|
431 * The method must return PID of the new proces, a (positive??? ####) number
|
|
432 * which fits into Lisp_Int. No return value indicates an error, the method
|
|
433 * must signal an error instead.
|
|
434 */
|
|
435
|
|
436 /* #### This function completely ignores Vprocess_environment */
|
|
437
|
278
|
438 static void
|
|
439 signal_cannot_launch (char* image_file, DWORD err)
|
|
440 {
|
|
441 mswindows_set_errno (err);
|
|
442 error ("Starting \"%s\": %s", image_file, strerror (errno));
|
|
443 }
|
|
444
|
274
|
445 static int
|
|
446 nt_create_process (struct Lisp_Process *p,
|
|
447 char **argv, CONST char *current_dir)
|
|
448 {
|
|
449 HANDLE hmyshove, hmyslurp, hprocin, hprocout;
|
|
450 LPTSTR command_line;
|
278
|
451 BOOL do_io, windowed;
|
|
452
|
|
453 /* Find out whether the application is windowed or not */
|
274
|
454 {
|
278
|
455 /* SHGetFileInfo tends to return ERROR_FILE_NOT_FOUND on most
|
|
456 errors. This leads to bogus error message. */
|
|
457 DWORD image_type = SHGetFileInfo (argv[0], 0, NULL, 0, SHGFI_EXETYPE);
|
|
458 if (image_type == 0)
|
|
459 signal_cannot_launch (argv[0], (GetLastError () == ERROR_FILE_NOT_FOUND
|
|
460 ? ERROR_BAD_FORMAT : GetLastError ()));
|
|
461 windowed = HIWORD (image_type) != 0;
|
|
462 }
|
274
|
463
|
278
|
464 /* Decide whether to do I/O on process handles, or just mark the
|
|
465 process exited immediately upon successful launching. We do I/O if the
|
|
466 process is a console one, or if it is windowed but windowed_process_io
|
|
467 is non-zero */
|
|
468 do_io = !windowed || windowed_process_io ;
|
|
469
|
|
470 if (do_io)
|
|
471 {
|
|
472 /* Create two unidirectional named pipes */
|
|
473 HANDLE htmp;
|
|
474 SECURITY_ATTRIBUTES sa;
|
274
|
475
|
278
|
476 sa.nLength = sizeof(sa);
|
|
477 sa.bInheritHandle = TRUE;
|
|
478 sa.lpSecurityDescriptor = NULL;
|
|
479
|
|
480 CreatePipe (&hprocin, &hmyshove, &sa, 0);
|
|
481 CreatePipe (&hmyslurp, &hprocout, &sa, 0);
|
|
482
|
|
483 /* Stupid Win32 allows to create a pipe with *both* ends either
|
|
484 inheritable or not. We need process ends inheritable, and local
|
|
485 ends not inheritable. */
|
|
486 DuplicateHandle (GetCurrentProcess(), hmyshove, GetCurrentProcess(), &htmp,
|
|
487 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
|
488 hmyshove = htmp;
|
|
489 DuplicateHandle (GetCurrentProcess(), hmyslurp, GetCurrentProcess(), &htmp,
|
|
490 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
|
491 hmyslurp = htmp;
|
|
492 }
|
274
|
493
|
|
494 /* Convert an argv vector into Win32 style command line.
|
|
495
|
|
496 #### This works only for cmd, and not for cygwin bash. Perhaps,
|
|
497 instead of ad-hoc fiddling with different methods for quoting
|
|
498 process arguments in ntproc.c (disgust shudder), this must call a
|
|
499 smart lisp routine. The code here will be a fallback, if the
|
|
500 lisp function is not specified.
|
|
501 */
|
|
502 {
|
|
503 char** thisarg;
|
|
504 size_t size = 1;
|
|
505
|
|
506 for (thisarg = argv; *thisarg; ++thisarg)
|
|
507 size += strlen (*thisarg) + 1;
|
|
508
|
|
509 command_line = alloca_array (char, size);
|
|
510 *command_line = 0;
|
|
511
|
|
512 for (thisarg = argv; *thisarg; ++thisarg)
|
|
513 {
|
|
514 if (thisarg != argv)
|
|
515 strcat (command_line, " ");
|
|
516 strcat (command_line, *thisarg);
|
|
517 }
|
|
518 }
|
|
519
|
|
520 /* Create process */
|
|
521 {
|
|
522 STARTUPINFO si;
|
|
523 PROCESS_INFORMATION pi;
|
|
524 DWORD err;
|
|
525
|
|
526 xzero (si);
|
278
|
527 si.dwFlags = STARTF_USESHOWWINDOW;
|
|
528 si.wShowWindow = windowed ? SW_SHOWNORMAL : SW_HIDE;
|
|
529 if (do_io)
|
274
|
530 {
|
278
|
531 si.hStdInput = hprocin;
|
|
532 si.hStdOutput = hprocout;
|
|
533 si.hStdError = hprocout;
|
|
534 si.dwFlags |= STARTF_USESTDHANDLES;
|
274
|
535 }
|
|
536
|
278
|
537 err = (CreateProcess (NULL, command_line, NULL, NULL, TRUE,
|
280
|
538 CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP
|
|
539 | CREATE_SUSPENDED,
|
278
|
540 NULL, current_dir, &si, &pi)
|
|
541 ? 0 : GetLastError ());
|
|
542
|
|
543 if (do_io)
|
274
|
544 {
|
278
|
545 /* These just have been inherited; we do not need a copy */
|
|
546 CloseHandle (hprocin);
|
|
547 CloseHandle (hprocout);
|
|
548 }
|
|
549
|
|
550 /* Handle process creation failure */
|
|
551 if (err)
|
|
552 {
|
|
553 if (do_io)
|
274
|
554 {
|
278
|
555 CloseHandle (hmyshove);
|
|
556 CloseHandle (hmyslurp);
|
274
|
557 }
|
278
|
558 signal_cannot_launch (argv[0], GetLastError ());
|
|
559 }
|
274
|
560
|
278
|
561 /* The process started successfully */
|
|
562 if (do_io)
|
|
563 {
|
|
564 NT_DATA(p)->h_process = pi.hProcess;
|
|
565 init_process_io_handles (p, (void*)hmyslurp, (void*)hmyshove, 0);
|
274
|
566 }
|
|
567 else
|
|
568 {
|
278
|
569 /* Indicate as if the process has exited immediately. */
|
|
570 p->status_symbol = Qexit;
|
|
571 CloseHandle (pi.hProcess);
|
274
|
572 }
|
|
573
|
280
|
574 if (!windowed)
|
|
575 enable_child_signals (pi.hProcess);
|
|
576
|
|
577 ResumeThread (pi.hThread);
|
278
|
578 CloseHandle (pi.hThread);
|
|
579
|
|
580 /* Hack to support Windows 95 negative pids */
|
274
|
581 return ((int)pi.dwProcessId < 0
|
|
582 ? -(int)pi.dwProcessId : (int)pi.dwProcessId);
|
|
583 }
|
|
584 }
|
|
585
|
|
586 /*
|
|
587 * This method is called to update status fields of the process
|
|
588 * structure. If the process has not existed, this method is expected
|
|
589 * to do nothing.
|
|
590 *
|
|
591 * The method is called only for real child processes.
|
|
592 */
|
|
593
|
|
594 static void
|
|
595 nt_update_status_if_terminated (struct Lisp_Process* p)
|
|
596 {
|
|
597 DWORD exit_code;
|
|
598 if (GetExitCodeProcess (NT_DATA(p)->h_process, &exit_code)
|
|
599 && exit_code != STILL_ACTIVE)
|
|
600 {
|
|
601 p->tick++;
|
|
602 p->core_dumped = 0;
|
|
603 /* The exit code can be a code returned by process, or an
|
|
604 NTSTATUS value. We cannot accurately handle the latter since
|
|
605 it is a full 32 bit integer */
|
|
606 if (exit_code & 0xC0000000)
|
|
607 {
|
|
608 p->status_symbol = Qsignal;
|
|
609 p->exit_code = exit_code & 0x1FFFFFFF;
|
|
610 }
|
|
611 else
|
|
612 {
|
|
613 p->status_symbol = Qexit;
|
|
614 p->exit_code = exit_code;
|
|
615 }
|
|
616 }
|
|
617 }
|
|
618
|
|
619 /*
|
|
620 * Stuff the entire contents of LSTREAM to the process ouptut pipe
|
|
621 */
|
|
622
|
|
623 /* #### If only this function could be somehow merged with
|
|
624 unix_send_process... */
|
|
625
|
|
626 static void
|
|
627 nt_send_process (Lisp_Object proc, struct lstream* lstream)
|
|
628 {
|
|
629 struct Lisp_Process *p = XPROCESS (proc);
|
|
630
|
|
631 /* use a reasonable-sized buffer (somewhere around the size of the
|
|
632 stream buffer) so as to avoid inundating the stream with blocked
|
|
633 data. */
|
|
634 Bufbyte chunkbuf[512];
|
|
635 Bytecount chunklen;
|
|
636
|
|
637 while (1)
|
|
638 {
|
|
639 int writeret;
|
|
640
|
|
641 chunklen = Lstream_read (lstream, chunkbuf, 512);
|
|
642 if (chunklen <= 0)
|
|
643 break; /* perhaps should abort() if < 0?
|
|
644 This should never happen. */
|
|
645
|
|
646 /* Lstream_write() will never successfully write less than the
|
|
647 amount sent in. In the worst case, it just buffers the
|
|
648 unwritten data. */
|
|
649 writeret = Lstream_write (XLSTREAM (DATA_OUTSTREAM(p)), chunkbuf,
|
|
650 chunklen);
|
|
651 if (writeret < 0)
|
|
652 {
|
|
653 p->status_symbol = Qexit;
|
|
654 p->exit_code = ERROR_BROKEN_PIPE;
|
|
655 p->core_dumped = 0;
|
|
656 p->tick++;
|
|
657 process_tick++;
|
|
658 deactivate_process (proc);
|
|
659 error ("Broken pipe error sending to process %s; closed it",
|
|
660 XSTRING_DATA (p->name));
|
|
661 }
|
|
662
|
|
663 while (Lstream_was_blocked_p (XLSTREAM (p->pipe_outstream)))
|
|
664 {
|
|
665 /* Buffer is full. Wait, accepting input; that may allow
|
|
666 the program to finish doing output and read more. */
|
|
667 Faccept_process_output (Qnil, make_int (1), Qnil);
|
|
668 Lstream_flush (XLSTREAM (p->pipe_outstream));
|
|
669 }
|
|
670 }
|
|
671 Lstream_flush (XLSTREAM (DATA_OUTSTREAM(p)));
|
|
672 }
|
|
673
|
280
|
674 /*
|
|
675 * Send a signal number SIGNO to PROCESS.
|
|
676 * CURRENT_GROUP means send to the process group that currently owns
|
|
677 * the terminal being used to communicate with PROCESS.
|
|
678 * This is used for various commands in shell mode.
|
|
679 * If NOMSG is zero, insert signal-announcements into process's buffers
|
|
680 * right away.
|
|
681 *
|
|
682 * If we can, we try to signal PROCESS by sending control characters
|
|
683 * down the pty. This allows us to signal inferiors who have changed
|
|
684 * their uid, for which killpg would return an EPERM error.
|
|
685 *
|
|
686 * The method signals an error if the given SIGNO is not valid
|
|
687 */
|
|
688
|
|
689 static void
|
|
690 nt_kill_child_process (Lisp_Object proc, int signo,
|
|
691 int current_group, int nomsg)
|
|
692 {
|
|
693 struct Lisp_Process *p = XPROCESS (proc);
|
|
694
|
|
695 /* Signal error if SIGNO cannot be sent */
|
|
696 validate_signal_number (signo);
|
|
697
|
|
698 /* Send signal */
|
|
699 if (!send_signal (NT_DATA(p)->h_process, signo))
|
|
700 error ("Cannot send signal to process");
|
|
701 }
|
|
702
|
|
703 /*
|
|
704 * Kill any process in the system given its PID.
|
|
705 *
|
|
706 * Returns zero if a signal successfully sent, or
|
|
707 * negative number upon failure
|
|
708 */
|
|
709 static int
|
|
710 nt_kill_process_by_pid (int pid, int signo)
|
|
711 {
|
|
712 HANDLE h_process;
|
|
713 int send_result;
|
|
714
|
|
715 /* Signal error if SIGNO cannot be sent */
|
|
716 validate_signal_number (signo);
|
|
717
|
|
718 /* Try to open the process with required privileges */
|
|
719 h_process = OpenProcess (PROCESS_CREATE_THREAD
|
|
720 | PROCESS_QUERY_INFORMATION
|
|
721 | PROCESS_VM_OPERATION
|
|
722 | PROCESS_VM_WRITE,
|
|
723 FALSE, pid);
|
|
724 if (h_process == NULL)
|
|
725 return -1;
|
|
726
|
|
727 send_result = send_signal (h_process, signo);
|
|
728
|
|
729 CloseHandle (h_process);
|
|
730
|
|
731 return send_result ? 0 : -1;
|
|
732 }
|
|
733
|
|
734
|
274
|
735 /*-----------------------------------------------------------------------*/
|
|
736 /* Initialization */
|
|
737 /*-----------------------------------------------------------------------*/
|
|
738
|
|
739 void
|
|
740 process_type_create_nt (void)
|
|
741 {
|
|
742 PROCESS_HAS_METHOD (nt, alloc_process_data);
|
|
743 PROCESS_HAS_METHOD (nt, finalize_process_data);
|
|
744 /* PROCESS_HAS_METHOD (nt, mark_process_data); */
|
|
745 /* PROCESS_HAS_METHOD (nt, init_process); */
|
|
746 /* PROCESS_HAS_METHOD (nt, init_process_io_handles); */
|
|
747 PROCESS_HAS_METHOD (nt, create_process);
|
|
748 PROCESS_HAS_METHOD (nt, update_status_if_terminated);
|
|
749 PROCESS_HAS_METHOD (nt, send_process);
|
280
|
750 PROCESS_HAS_METHOD (nt, kill_child_process);
|
|
751 PROCESS_HAS_METHOD (nt, kill_process_by_pid);
|
274
|
752 #if 0 /* Yet todo */
|
|
753 #ifdef HAVE_SOCKETS
|
|
754 PROCESS_HAS_METHOD (nt, canonicalize_host_name);
|
|
755 PROCESS_HAS_METHOD (nt, open_network_stream);
|
|
756 #ifdef HAVE_MULTICAST
|
|
757 PROCESS_HAS_METHOD (nt, open_multicast_group);
|
|
758 #endif
|
|
759 #endif
|
|
760 #endif
|
|
761 }
|
|
762
|
|
763 void
|
|
764 vars_of_process_nt (void)
|
|
765 {
|
|
766 }
|
|
767
|