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>
|
282
|
38 #ifdef HAVE_SOCKETS
|
|
39 #include <winsock.h>
|
|
40 #endif
|
274
|
41
|
|
42 /* Implemenation-specific data. Pointed to by Lisp_Process->process_data */
|
|
43 struct nt_process_data
|
|
44 {
|
|
45 HANDLE h_process;
|
|
46 };
|
|
47
|
|
48 #define NT_DATA(p) ((struct nt_process_data*)((p)->process_data))
|
|
49
|
|
50 /*-----------------------------------------------------------------------*/
|
|
51 /* Process helpers */
|
|
52 /*-----------------------------------------------------------------------*/
|
|
53
|
276
|
54 /* This one breaks process abstraction. Prototype is in console-msw.h,
|
|
55 used by select_process method in event-msw.c */
|
|
56 HANDLE
|
|
57 get_nt_process_handle (struct Lisp_Process *p)
|
|
58 {
|
|
59 return (NT_DATA (p)->h_process);
|
|
60 }
|
280
|
61
|
|
62 /*-----------------------------------------------------------------------*/
|
|
63 /* Running remote threads. See Microsoft Systems Journal 1994 Number 5 */
|
|
64 /* Jeffrey Richter, Load Your 32-bit DLL into Another Process's Address..*/
|
|
65 /*-----------------------------------------------------------------------*/
|
274
|
66
|
280
|
67 typedef struct
|
|
68 {
|
|
69 HANDLE h_process;
|
|
70 HANDLE h_thread;
|
|
71 LPVOID address;
|
|
72 } process_memory;
|
|
73
|
|
74 /*
|
|
75 * Allocate SIZE bytes in H_PROCESS address space. Fill in PMC used
|
|
76 * further by other routines. Return nonzero if successful.
|
|
77 *
|
|
78 * The memory in other process is allocated by creating a suspended
|
|
79 * thread. Initial stack of that thread is used as the memory
|
|
80 * block. The thread entry point is the routine ExitThread in
|
|
81 * kernel32.dll, so the allocated memory is freed just by resuming the
|
|
82 * thread, which immediately terminates after that.
|
|
83 */
|
|
84
|
|
85 static int
|
|
86 alloc_process_memory (HANDLE h_process, size_t size,
|
|
87 process_memory* pmc)
|
|
88 {
|
|
89 LPTHREAD_START_ROUTINE adr_ExitThread =
|
|
90 (LPTHREAD_START_ROUTINE)
|
|
91 GetProcAddress (GetModuleHandle ("kernel32"), "ExitThread");
|
|
92 DWORD dw_unused;
|
|
93 CONTEXT context;
|
|
94 MEMORY_BASIC_INFORMATION mbi;
|
|
95
|
|
96 pmc->h_process = h_process;
|
|
97 pmc->h_thread = CreateRemoteThread (h_process, NULL, size,
|
|
98 adr_ExitThread, NULL,
|
|
99 CREATE_SUSPENDED, &dw_unused);
|
|
100 if (pmc->h_thread == NULL)
|
|
101 return 0;
|
|
102
|
|
103 /* Get context, for thread's stack pointer */
|
|
104 context.ContextFlags = CONTEXT_CONTROL;
|
|
105 if (!GetThreadContext (pmc->h_thread, &context))
|
|
106 goto failure;
|
|
107
|
|
108 /* Determine base address of the committed range */
|
|
109 if (sizeof(mbi) != VirtualQueryEx (h_process,
|
|
110 #if defined (_X86_)
|
|
111 (LPDWORD)context.Esp - 1,
|
|
112 #elif defined (_ALPHA_)
|
|
113 (LPDWORD)context.IntSp - 1,
|
|
114 #else
|
|
115 #error Unknown processor architecture
|
|
116 #endif
|
|
117 &mbi, sizeof(mbi)))
|
|
118 goto failure;
|
|
119
|
|
120 /* Change the page protection of the allocated memory to executable,
|
|
121 read, and write. */
|
|
122 if (!VirtualProtectEx (h_process, mbi.BaseAddress, size,
|
|
123 PAGE_EXECUTE_READWRITE, &dw_unused))
|
|
124 goto failure;
|
|
125
|
|
126 pmc->address = mbi.BaseAddress;
|
|
127 return 1;
|
|
128
|
|
129 failure:
|
|
130 ResumeThread (pmc->h_thread);
|
|
131 pmc->address = 0;
|
|
132 return 0;
|
|
133 }
|
|
134
|
|
135 static void
|
|
136 free_process_memory (process_memory* pmc)
|
|
137 {
|
|
138 ResumeThread (pmc->h_thread);
|
|
139 }
|
|
140
|
|
141 /*
|
|
142 * Run ROUTINE in the context of process determined by H_PROCESS. The
|
|
143 * routine is passed the address of DATA as parameter. CODE_END is the
|
|
144 * address immediately after ROUTINE's code. DATA_SIZE is the size of
|
|
145 * DATA structure.
|
|
146 *
|
|
147 * Note that the code must be positionally independent, and compiled
|
|
148 * without stack checks (they cause implicit calls into CRT so will
|
|
149 * fail). DATA should not refer any data in calling process, as both
|
|
150 * routine and its data are copied into remote process. Size of data
|
|
151 * and code together should not exceed one page (4K on x86 systems).
|
|
152 *
|
|
153 * Return the value returned by ROUTINE, or (DWORD)-1 if call failed.
|
|
154 */
|
|
155 static DWORD
|
|
156 run_in_other_process (HANDLE h_process,
|
|
157 LPTHREAD_START_ROUTINE routine, LPVOID code_end,
|
|
158 LPVOID data, size_t data_size)
|
|
159 {
|
|
160 process_memory pm;
|
|
161 size_t code_size = (LPBYTE)code_end - (LPBYTE)routine;
|
|
162 /* Need at most 3 extra bytes of memory, for data alignment */
|
|
163 size_t total_size = code_size + data_size + 3;
|
|
164 LPVOID remote_data;
|
|
165 HANDLE h_thread;
|
|
166 DWORD dw_unused;
|
|
167
|
|
168 /* Allocate memory */
|
|
169 if (!alloc_process_memory (h_process, total_size, &pm))
|
|
170 return (DWORD)-1;
|
|
171
|
|
172 /* Copy code */
|
|
173 if (!WriteProcessMemory (h_process, pm.address, (LPVOID)routine,
|
|
174 code_size, NULL))
|
|
175 goto failure;
|
|
176
|
|
177 /* Copy data */
|
|
178 if (data_size)
|
|
179 {
|
|
180 remote_data = (LPBYTE)pm.address + ((code_size + 4) & ~3);
|
|
181 if (!WriteProcessMemory (h_process, remote_data, data, data_size, NULL))
|
|
182 goto failure;
|
|
183 }
|
|
184 else
|
|
185 remote_data = NULL;
|
|
186
|
|
187 /* Execute the remote copy of code, passing it remote data */
|
|
188 h_thread = CreateRemoteThread (h_process, NULL, 0,
|
|
189 (LPTHREAD_START_ROUTINE) pm.address,
|
|
190 remote_data, 0, &dw_unused);
|
|
191 if (h_thread == NULL)
|
|
192 goto failure;
|
|
193
|
|
194 /* Wait till thread finishes */
|
|
195 WaitForSingleObject (h_thread, INFINITE);
|
|
196
|
|
197 /* Free remote memory */
|
|
198 free_process_memory (&pm);
|
|
199
|
|
200 /* Return thread's exit code */
|
|
201 {
|
|
202 DWORD exit_code;
|
|
203 GetExitCodeThread (h_thread, &exit_code);
|
|
204 CloseHandle (h_thread);
|
|
205 return exit_code;
|
|
206 }
|
|
207
|
|
208 failure:
|
|
209 free_process_memory (&pm);
|
|
210 return (DWORD)-1;
|
|
211 }
|
|
212
|
|
213 /*-----------------------------------------------------------------------*/
|
|
214 /* Sending signals */
|
|
215 /*-----------------------------------------------------------------------*/
|
|
216
|
|
217 /*
|
|
218 * We handle the following signals:
|
|
219 *
|
|
220 * SIGKILL, SIGTERM, SIGQUIT - These three translate to ExitProcess
|
|
221 * executed by the remote process
|
|
222 * SIGINT - The remote process is sent CTRL_BREAK_EVENT
|
|
223 */
|
|
224
|
|
225 /*
|
|
226 * Sending SIGKILL
|
|
227 */
|
|
228 typedef struct
|
|
229 {
|
|
230 void (WINAPI *adr_ExitProcess) (UINT);
|
|
231 } sigkill_data;
|
|
232
|
|
233 static DWORD WINAPI
|
|
234 sigkill_proc (sigkill_data* data)
|
|
235 {
|
|
236 (*data->adr_ExitProcess)(255);
|
|
237 return 1;
|
|
238 }
|
|
239
|
|
240 /* Watermark in code space */
|
|
241 static void
|
|
242 sigkill_code_end (void)
|
|
243 {
|
|
244 }
|
|
245
|
|
246 /*
|
|
247 * Sending break or control c
|
|
248 */
|
|
249 typedef struct
|
|
250 {
|
|
251 BOOL (WINAPI *adr_GenerateConsoleCtrlEvent) (DWORD, DWORD);
|
|
252 DWORD event;
|
|
253 } sigint_data;
|
|
254
|
|
255 static DWORD WINAPI
|
|
256 sigint_proc (sigint_data* data)
|
|
257 {
|
|
258 return (*data->adr_GenerateConsoleCtrlEvent) (data->event, 0);
|
|
259 }
|
|
260
|
|
261 /* Watermark in code space */
|
|
262 static void
|
|
263 sigint_code_end (void)
|
|
264 {
|
|
265 }
|
|
266
|
|
267 /*
|
|
268 * Enabling signals
|
|
269 */
|
|
270 typedef struct
|
|
271 {
|
|
272 BOOL (WINAPI *adr_SetConsoleCtrlHandler) (LPVOID, BOOL);
|
|
273 } sig_enable_data;
|
|
274
|
|
275 static DWORD WINAPI
|
|
276 sig_enable_proc (sig_enable_data* data)
|
|
277 {
|
|
278 (*data->adr_SetConsoleCtrlHandler) (NULL, FALSE);
|
|
279 return 1;
|
|
280 }
|
|
281
|
|
282 /* Watermark in code space */
|
|
283 static void
|
|
284 sig_enable_code_end (void)
|
|
285 {
|
|
286 }
|
|
287
|
|
288 /*
|
|
289 * Send signal SIGNO to process H_PROCESS.
|
|
290 * Return nonzero if successful.
|
|
291 */
|
|
292
|
|
293 /* This code assigns a return value of GetProcAddress to function pointers
|
|
294 of many different types. Instead of heavy obscure casts, we just disable
|
|
295 warnings about assignments to different function pointer types. */
|
|
296 #pragma warning (disable : 4113)
|
|
297
|
|
298 static int
|
|
299 send_signal (HANDLE h_process, int signo)
|
|
300 {
|
|
301 HMODULE h_kernel = GetModuleHandle ("kernel32");
|
|
302 DWORD retval;
|
|
303
|
|
304 assert (h_kernel != NULL);
|
|
305
|
|
306 switch (signo)
|
|
307 {
|
|
308 case SIGKILL:
|
|
309 case SIGTERM:
|
|
310 case SIGQUIT:
|
|
311 {
|
|
312 sigkill_data d;
|
|
313 d.adr_ExitProcess = GetProcAddress (h_kernel, "ExitProcess");
|
|
314 assert (d.adr_ExitProcess);
|
|
315 retval = run_in_other_process (h_process,
|
|
316 sigkill_proc, sigkill_code_end,
|
|
317 &d, sizeof (d));
|
|
318 break;
|
|
319 }
|
|
320 case SIGINT:
|
|
321 {
|
|
322 sigint_data d;
|
|
323 d.adr_GenerateConsoleCtrlEvent =
|
|
324 GetProcAddress (h_kernel, "GenerateConsoleCtrlEvent");
|
|
325 assert (d.adr_GenerateConsoleCtrlEvent);
|
|
326 d.event = CTRL_C_EVENT;
|
|
327 retval = run_in_other_process (h_process,
|
|
328 sigint_proc, sigint_code_end,
|
|
329 &d, sizeof (d));
|
|
330 break;
|
|
331 }
|
|
332 default:
|
|
333 assert (0);
|
|
334 }
|
|
335
|
|
336 return (int)retval > 0 ? 1 : 0;
|
|
337 }
|
|
338
|
|
339 /*
|
|
340 * Enable CTRL_C_EVENT handling in a new child process
|
|
341 */
|
|
342 static void
|
|
343 enable_child_signals (HANDLE h_process)
|
|
344 {
|
|
345 HMODULE h_kernel = GetModuleHandle ("kernel32");
|
|
346 sig_enable_data d;
|
|
347
|
|
348 assert (h_kernel != NULL);
|
|
349 d.adr_SetConsoleCtrlHandler =
|
|
350 GetProcAddress (h_kernel, "SetConsoleCtrlHandler");
|
|
351 assert (d.adr_SetConsoleCtrlHandler);
|
|
352 run_in_other_process (h_process,
|
|
353 sig_enable_proc, sig_enable_code_end,
|
|
354 &d, sizeof (d));
|
|
355 }
|
|
356
|
|
357 #pragma warning (default : 4113)
|
|
358
|
|
359 /*
|
|
360 * Signal error if SIGNO is not supported
|
|
361 */
|
|
362 static void
|
|
363 validate_signal_number (int signo)
|
|
364 {
|
|
365 if (signo != SIGKILL && signo != SIGTERM
|
|
366 && signo != SIGQUIT && signo != SIGINT)
|
|
367 error ("Signal number %d not supported", signo);
|
|
368 }
|
|
369
|
274
|
370 /*-----------------------------------------------------------------------*/
|
|
371 /* Process methods */
|
|
372 /*-----------------------------------------------------------------------*/
|
|
373
|
|
374 /*
|
|
375 * Allocate and initialize Lisp_Process->process_data
|
|
376 */
|
|
377
|
|
378 static void
|
|
379 nt_alloc_process_data (struct Lisp_Process *p)
|
|
380 {
|
282
|
381 p->process_data = xnew_and_zero (struct nt_process_data);
|
274
|
382 }
|
|
383
|
|
384 static void
|
|
385 nt_finalize_process_data (struct Lisp_Process *p, int for_disksave)
|
|
386 {
|
|
387 assert (!for_disksave);
|
|
388 if (NT_DATA(p)->h_process)
|
|
389 CloseHandle (NT_DATA(p)->h_process);
|
|
390 }
|
|
391
|
|
392 /*
|
|
393 * Initialize XEmacs process implemenation once
|
|
394 */
|
|
395 static void
|
|
396 nt_init_process (void)
|
|
397 {
|
282
|
398 /* Initialize winsock */
|
|
399 WSADATA wsa_data;
|
|
400 /* Request Winsock v1.1 Note the order: (minor=1, major=1) */
|
|
401 WSAStartup (MAKEWORD (1,1), &wsa_data);
|
274
|
402 }
|
|
403
|
|
404 /*
|
|
405 * Fork off a subprocess. P is a pointer to newly created subprocess
|
|
406 * object. If this function signals, the caller is responsible for
|
|
407 * deleting (and finalizing) the process object.
|
|
408 *
|
|
409 * The method must return PID of the new proces, a (positive??? ####) number
|
|
410 * which fits into Lisp_Int. No return value indicates an error, the method
|
|
411 * must signal an error instead.
|
|
412 */
|
|
413
|
|
414 /* #### This function completely ignores Vprocess_environment */
|
|
415
|
278
|
416 static void
|
|
417 signal_cannot_launch (char* image_file, DWORD err)
|
|
418 {
|
|
419 mswindows_set_errno (err);
|
|
420 error ("Starting \"%s\": %s", image_file, strerror (errno));
|
|
421 }
|
|
422
|
274
|
423 static int
|
|
424 nt_create_process (struct Lisp_Process *p,
|
|
425 char **argv, CONST char *current_dir)
|
|
426 {
|
|
427 HANDLE hmyshove, hmyslurp, hprocin, hprocout;
|
|
428 LPTSTR command_line;
|
278
|
429 BOOL do_io, windowed;
|
|
430
|
|
431 /* Find out whether the application is windowed or not */
|
274
|
432 {
|
278
|
433 /* SHGetFileInfo tends to return ERROR_FILE_NOT_FOUND on most
|
|
434 errors. This leads to bogus error message. */
|
|
435 DWORD image_type = SHGetFileInfo (argv[0], 0, NULL, 0, SHGFI_EXETYPE);
|
|
436 if (image_type == 0)
|
|
437 signal_cannot_launch (argv[0], (GetLastError () == ERROR_FILE_NOT_FOUND
|
|
438 ? ERROR_BAD_FORMAT : GetLastError ()));
|
|
439 windowed = HIWORD (image_type) != 0;
|
|
440 }
|
274
|
441
|
278
|
442 /* Decide whether to do I/O on process handles, or just mark the
|
|
443 process exited immediately upon successful launching. We do I/O if the
|
|
444 process is a console one, or if it is windowed but windowed_process_io
|
|
445 is non-zero */
|
|
446 do_io = !windowed || windowed_process_io ;
|
|
447
|
|
448 if (do_io)
|
|
449 {
|
|
450 /* Create two unidirectional named pipes */
|
|
451 HANDLE htmp;
|
|
452 SECURITY_ATTRIBUTES sa;
|
274
|
453
|
278
|
454 sa.nLength = sizeof(sa);
|
|
455 sa.bInheritHandle = TRUE;
|
|
456 sa.lpSecurityDescriptor = NULL;
|
|
457
|
|
458 CreatePipe (&hprocin, &hmyshove, &sa, 0);
|
|
459 CreatePipe (&hmyslurp, &hprocout, &sa, 0);
|
|
460
|
|
461 /* Stupid Win32 allows to create a pipe with *both* ends either
|
|
462 inheritable or not. We need process ends inheritable, and local
|
|
463 ends not inheritable. */
|
|
464 DuplicateHandle (GetCurrentProcess(), hmyshove, GetCurrentProcess(), &htmp,
|
|
465 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
|
466 hmyshove = htmp;
|
|
467 DuplicateHandle (GetCurrentProcess(), hmyslurp, GetCurrentProcess(), &htmp,
|
|
468 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
|
469 hmyslurp = htmp;
|
|
470 }
|
274
|
471
|
|
472 /* Convert an argv vector into Win32 style command line.
|
|
473
|
|
474 #### This works only for cmd, and not for cygwin bash. Perhaps,
|
|
475 instead of ad-hoc fiddling with different methods for quoting
|
|
476 process arguments in ntproc.c (disgust shudder), this must call a
|
|
477 smart lisp routine. The code here will be a fallback, if the
|
|
478 lisp function is not specified.
|
|
479 */
|
|
480 {
|
|
481 char** thisarg;
|
|
482 size_t size = 1;
|
|
483
|
|
484 for (thisarg = argv; *thisarg; ++thisarg)
|
|
485 size += strlen (*thisarg) + 1;
|
|
486
|
|
487 command_line = alloca_array (char, size);
|
|
488 *command_line = 0;
|
|
489
|
|
490 for (thisarg = argv; *thisarg; ++thisarg)
|
|
491 {
|
|
492 if (thisarg != argv)
|
|
493 strcat (command_line, " ");
|
|
494 strcat (command_line, *thisarg);
|
|
495 }
|
|
496 }
|
|
497
|
|
498 /* Create process */
|
|
499 {
|
|
500 STARTUPINFO si;
|
|
501 PROCESS_INFORMATION pi;
|
|
502 DWORD err;
|
|
503
|
|
504 xzero (si);
|
278
|
505 si.dwFlags = STARTF_USESHOWWINDOW;
|
|
506 si.wShowWindow = windowed ? SW_SHOWNORMAL : SW_HIDE;
|
|
507 if (do_io)
|
274
|
508 {
|
278
|
509 si.hStdInput = hprocin;
|
|
510 si.hStdOutput = hprocout;
|
|
511 si.hStdError = hprocout;
|
|
512 si.dwFlags |= STARTF_USESTDHANDLES;
|
274
|
513 }
|
|
514
|
278
|
515 err = (CreateProcess (NULL, command_line, NULL, NULL, TRUE,
|
280
|
516 CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP
|
|
517 | CREATE_SUSPENDED,
|
278
|
518 NULL, current_dir, &si, &pi)
|
|
519 ? 0 : GetLastError ());
|
|
520
|
|
521 if (do_io)
|
274
|
522 {
|
278
|
523 /* These just have been inherited; we do not need a copy */
|
|
524 CloseHandle (hprocin);
|
|
525 CloseHandle (hprocout);
|
|
526 }
|
|
527
|
|
528 /* Handle process creation failure */
|
|
529 if (err)
|
|
530 {
|
|
531 if (do_io)
|
274
|
532 {
|
278
|
533 CloseHandle (hmyshove);
|
|
534 CloseHandle (hmyslurp);
|
274
|
535 }
|
278
|
536 signal_cannot_launch (argv[0], GetLastError ());
|
|
537 }
|
274
|
538
|
278
|
539 /* The process started successfully */
|
|
540 if (do_io)
|
|
541 {
|
|
542 NT_DATA(p)->h_process = pi.hProcess;
|
|
543 init_process_io_handles (p, (void*)hmyslurp, (void*)hmyshove, 0);
|
274
|
544 }
|
|
545 else
|
|
546 {
|
278
|
547 /* Indicate as if the process has exited immediately. */
|
|
548 p->status_symbol = Qexit;
|
|
549 CloseHandle (pi.hProcess);
|
274
|
550 }
|
|
551
|
280
|
552 if (!windowed)
|
|
553 enable_child_signals (pi.hProcess);
|
|
554
|
|
555 ResumeThread (pi.hThread);
|
278
|
556 CloseHandle (pi.hThread);
|
|
557
|
|
558 /* Hack to support Windows 95 negative pids */
|
274
|
559 return ((int)pi.dwProcessId < 0
|
|
560 ? -(int)pi.dwProcessId : (int)pi.dwProcessId);
|
|
561 }
|
|
562 }
|
|
563
|
|
564 /*
|
|
565 * This method is called to update status fields of the process
|
|
566 * structure. If the process has not existed, this method is expected
|
|
567 * to do nothing.
|
|
568 *
|
|
569 * The method is called only for real child processes.
|
|
570 */
|
|
571
|
|
572 static void
|
|
573 nt_update_status_if_terminated (struct Lisp_Process* p)
|
|
574 {
|
|
575 DWORD exit_code;
|
|
576 if (GetExitCodeProcess (NT_DATA(p)->h_process, &exit_code)
|
|
577 && exit_code != STILL_ACTIVE)
|
|
578 {
|
|
579 p->tick++;
|
|
580 p->core_dumped = 0;
|
|
581 /* The exit code can be a code returned by process, or an
|
|
582 NTSTATUS value. We cannot accurately handle the latter since
|
|
583 it is a full 32 bit integer */
|
|
584 if (exit_code & 0xC0000000)
|
|
585 {
|
|
586 p->status_symbol = Qsignal;
|
|
587 p->exit_code = exit_code & 0x1FFFFFFF;
|
|
588 }
|
|
589 else
|
|
590 {
|
|
591 p->status_symbol = Qexit;
|
|
592 p->exit_code = exit_code;
|
|
593 }
|
|
594 }
|
|
595 }
|
|
596
|
|
597 /*
|
|
598 * Stuff the entire contents of LSTREAM to the process ouptut pipe
|
|
599 */
|
|
600
|
|
601 /* #### If only this function could be somehow merged with
|
|
602 unix_send_process... */
|
|
603
|
|
604 static void
|
|
605 nt_send_process (Lisp_Object proc, struct lstream* lstream)
|
|
606 {
|
|
607 struct Lisp_Process *p = XPROCESS (proc);
|
|
608
|
|
609 /* use a reasonable-sized buffer (somewhere around the size of the
|
|
610 stream buffer) so as to avoid inundating the stream with blocked
|
|
611 data. */
|
|
612 Bufbyte chunkbuf[512];
|
|
613 Bytecount chunklen;
|
|
614
|
|
615 while (1)
|
|
616 {
|
|
617 int writeret;
|
|
618
|
|
619 chunklen = Lstream_read (lstream, chunkbuf, 512);
|
|
620 if (chunklen <= 0)
|
|
621 break; /* perhaps should abort() if < 0?
|
|
622 This should never happen. */
|
|
623
|
|
624 /* Lstream_write() will never successfully write less than the
|
|
625 amount sent in. In the worst case, it just buffers the
|
|
626 unwritten data. */
|
|
627 writeret = Lstream_write (XLSTREAM (DATA_OUTSTREAM(p)), chunkbuf,
|
|
628 chunklen);
|
|
629 if (writeret < 0)
|
|
630 {
|
|
631 p->status_symbol = Qexit;
|
|
632 p->exit_code = ERROR_BROKEN_PIPE;
|
|
633 p->core_dumped = 0;
|
|
634 p->tick++;
|
|
635 process_tick++;
|
|
636 deactivate_process (proc);
|
|
637 error ("Broken pipe error sending to process %s; closed it",
|
|
638 XSTRING_DATA (p->name));
|
|
639 }
|
|
640
|
|
641 while (Lstream_was_blocked_p (XLSTREAM (p->pipe_outstream)))
|
|
642 {
|
|
643 /* Buffer is full. Wait, accepting input; that may allow
|
|
644 the program to finish doing output and read more. */
|
|
645 Faccept_process_output (Qnil, make_int (1), Qnil);
|
|
646 Lstream_flush (XLSTREAM (p->pipe_outstream));
|
|
647 }
|
|
648 }
|
|
649 Lstream_flush (XLSTREAM (DATA_OUTSTREAM(p)));
|
|
650 }
|
|
651
|
280
|
652 /*
|
|
653 * Send a signal number SIGNO to PROCESS.
|
|
654 * CURRENT_GROUP means send to the process group that currently owns
|
|
655 * the terminal being used to communicate with PROCESS.
|
|
656 * This is used for various commands in shell mode.
|
|
657 * If NOMSG is zero, insert signal-announcements into process's buffers
|
|
658 * right away.
|
|
659 *
|
|
660 * If we can, we try to signal PROCESS by sending control characters
|
|
661 * down the pty. This allows us to signal inferiors who have changed
|
|
662 * their uid, for which killpg would return an EPERM error.
|
|
663 *
|
|
664 * The method signals an error if the given SIGNO is not valid
|
|
665 */
|
|
666
|
|
667 static void
|
|
668 nt_kill_child_process (Lisp_Object proc, int signo,
|
|
669 int current_group, int nomsg)
|
|
670 {
|
|
671 struct Lisp_Process *p = XPROCESS (proc);
|
|
672
|
|
673 /* Signal error if SIGNO cannot be sent */
|
|
674 validate_signal_number (signo);
|
|
675
|
|
676 /* Send signal */
|
|
677 if (!send_signal (NT_DATA(p)->h_process, signo))
|
|
678 error ("Cannot send signal to process");
|
|
679 }
|
|
680
|
|
681 /*
|
|
682 * Kill any process in the system given its PID.
|
|
683 *
|
|
684 * Returns zero if a signal successfully sent, or
|
|
685 * negative number upon failure
|
|
686 */
|
|
687 static int
|
|
688 nt_kill_process_by_pid (int pid, int signo)
|
|
689 {
|
|
690 HANDLE h_process;
|
|
691 int send_result;
|
|
692
|
|
693 /* Signal error if SIGNO cannot be sent */
|
|
694 validate_signal_number (signo);
|
|
695
|
|
696 /* Try to open the process with required privileges */
|
|
697 h_process = OpenProcess (PROCESS_CREATE_THREAD
|
|
698 | PROCESS_QUERY_INFORMATION
|
|
699 | PROCESS_VM_OPERATION
|
|
700 | PROCESS_VM_WRITE,
|
|
701 FALSE, pid);
|
|
702 if (h_process == NULL)
|
|
703 return -1;
|
|
704
|
|
705 send_result = send_signal (h_process, signo);
|
|
706
|
|
707 CloseHandle (h_process);
|
|
708
|
|
709 return send_result ? 0 : -1;
|
|
710 }
|
282
|
711
|
|
712 /*-----------------------------------------------------------------------*/
|
|
713 /* Sockets connections */
|
|
714 /*-----------------------------------------------------------------------*/
|
|
715 #ifdef HAVE_SOCKETS
|
280
|
716
|
282
|
717 /* #### Hey MS, how long Winsock 2 for '95 will be in beta? */
|
|
718
|
|
719 #define SOCK_TIMER_ID 666
|
|
720 #define XM_SOCKREPLY (WM_USER + 666)
|
|
721
|
|
722 static int
|
|
723 get_internet_address (Lisp_Object host, struct sockaddr_in *address,
|
|
724 Error_behavior errb)
|
|
725 {
|
|
726 char buf [MAXGETHOSTSTRUCT];
|
|
727 HWND hwnd;
|
|
728 HANDLE hasync;
|
|
729 int success = 0;
|
|
730
|
|
731 address->sin_family = AF_INET;
|
|
732
|
|
733 /* First check if HOST is already a numeric address */
|
|
734 {
|
|
735 unsigned long inaddr = inet_addr (XSTRING_DATA (host));
|
|
736 if (inaddr != INADDR_NONE)
|
|
737 {
|
|
738 address->sin_addr.s_addr = inaddr;
|
|
739 return 1;
|
|
740 }
|
|
741 }
|
|
742
|
|
743 /* Create a window which will receive completion messages */
|
|
744 hwnd = CreateWindow ("STATIC", NULL, WS_OVERLAPPED, 0, 0, 1, 1,
|
|
745 NULL, NULL, NULL, NULL);
|
|
746 assert (hwnd);
|
|
747
|
|
748 /* Post name resolution request */
|
|
749 hasync = WSAAsyncGetHostByName (hwnd, XM_SOCKREPLY, XSTRING_DATA (host),
|
|
750 buf, sizeof (buf));
|
|
751 if (hasync == NULL)
|
|
752 goto done;
|
|
753
|
|
754 /* Set a timer to poll for quit every 250 ms */
|
|
755 SetTimer (hwnd, SOCK_TIMER_ID, 250, NULL);
|
|
756
|
|
757 while (1)
|
|
758 {
|
|
759 MSG msg;
|
|
760 GetMessage (&msg, hwnd, 0, 0);
|
|
761 if (msg.message == XM_SOCKREPLY)
|
|
762 {
|
|
763 /* Ok, got an answer */
|
|
764 if (WSAGETASYNCERROR(msg.lParam) == NO_ERROR)
|
|
765 success = 1;
|
|
766 goto done;
|
|
767 }
|
|
768 else if (msg.message == WM_TIMER && msg.wParam == SOCK_TIMER_ID)
|
|
769 {
|
|
770 if (QUITP)
|
|
771 {
|
|
772 WSACancelAsyncRequest (hasync);
|
|
773 KillTimer (hwnd, SOCK_TIMER_ID);
|
|
774 DestroyWindow (hwnd);
|
|
775 REALLY_QUIT;
|
|
776 }
|
|
777 }
|
|
778 DispatchMessage (&msg);
|
|
779 }
|
|
780
|
|
781 done:
|
|
782 KillTimer (hwnd, SOCK_TIMER_ID);
|
|
783 DestroyWindow (hwnd);
|
|
784 if (success)
|
|
785 {
|
|
786 /* BUF starts with struct hostent */
|
|
787 struct hostent* he = (struct hostent*) buf;
|
|
788 address->sin_addr.s_addr = *(unsigned long*)he->h_addr_list[0];
|
|
789 }
|
|
790 return success;
|
|
791 }
|
|
792
|
|
793 static Lisp_Object
|
|
794 nt_canonicalize_host_name (Lisp_Object host)
|
|
795 {
|
|
796 struct sockaddr_in address;
|
|
797
|
|
798 if (!get_internet_address (host, &address, ERROR_ME_NOT))
|
|
799 return host;
|
|
800
|
|
801 if (address.sin_family == AF_INET)
|
|
802 return build_string (inet_ntoa (address.sin_addr));
|
|
803 else
|
|
804 return host;
|
|
805 }
|
|
806
|
|
807 /* open a TCP network connection to a given HOST/SERVICE. Treated
|
|
808 exactly like a normal process when reading and writing. Only
|
|
809 differences are in status display and process deletion. A network
|
|
810 connection has no PID; you cannot signal it. All you can do is
|
|
811 deactivate and close it via delete-process */
|
|
812
|
|
813 static void
|
|
814 nt_open_network_stream (Lisp_Object name, Lisp_Object host, Lisp_Object service,
|
|
815 Lisp_Object family, void** vinfd, void** voutfd)
|
|
816 {
|
|
817 struct sockaddr_in address;
|
|
818 SOCKET s;
|
|
819 int port;
|
|
820 int retval;
|
|
821
|
|
822 CHECK_STRING (host);
|
|
823
|
|
824 if (!EQ (family, Qtcpip))
|
|
825 error ("Unsupported protocol family \"%s\"",
|
|
826 string_data (symbol_name (XSYMBOL (family))));
|
|
827
|
|
828 if (INTP (service))
|
|
829 port = htons ((unsigned short) XINT (service));
|
|
830 else
|
|
831 {
|
|
832 struct servent *svc_info;
|
|
833 CHECK_STRING (service);
|
|
834 svc_info = getservbyname ((char *) XSTRING_DATA (service), "tcp");
|
|
835 if (svc_info == 0)
|
|
836 error ("Unknown service \"%s\"", XSTRING_DATA (service));
|
|
837 port = svc_info->s_port;
|
|
838 }
|
|
839
|
|
840 get_internet_address (host, &address, ERROR_ME);
|
|
841 address.sin_port = port;
|
|
842
|
|
843 s = socket (address.sin_family, SOCK_STREAM, 0);
|
|
844 if (s < 0)
|
|
845 report_file_error ("error creating socket", list1 (name));
|
|
846
|
|
847 /* We don't want to be blocked on connect */
|
|
848 {
|
|
849 unsigned int nonblock = 1;
|
|
850 ioctlsocket (s, FIONBIO, &nonblock);
|
|
851 }
|
|
852
|
|
853 retval = connect (s, (struct sockaddr *) &address, sizeof (address));
|
|
854 if (retval != NO_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
|
|
855 goto connect_failed;
|
|
856
|
|
857 /* Wait while connection is established */
|
|
858 while (1)
|
|
859 {
|
|
860 fd_set fdset;
|
|
861 struct timeval tv;
|
|
862 int nsel;
|
|
863
|
|
864 if (QUITP)
|
|
865 {
|
|
866 closesocket (s);
|
|
867 REALLY_QUIT;
|
|
868 }
|
|
869
|
|
870 /* Poll for quit every 250 ms */
|
|
871 tv.tv_sec = 0;
|
|
872 tv.tv_usec = 250 * 1000;
|
|
873
|
|
874 FD_ZERO (&fdset);
|
|
875 FD_SET (s, &fdset);
|
|
876 nsel = select (0, NULL, &fdset, &fdset, &tv);
|
|
877
|
|
878 if (nsel > 0)
|
|
879 {
|
|
880 /* Check was connnection successful or not */
|
|
881 tv.tv_usec = 0;
|
|
882 nsel = select (0, NULL, NULL, &fdset, &tv);
|
|
883 if (nsel > 0)
|
|
884 goto connect_failed;
|
|
885 else
|
|
886 break;
|
|
887 }
|
|
888 }
|
|
889
|
|
890 /* We are connected at this point */
|
|
891 *vinfd = (void*)s;
|
|
892 DuplicateHandle (GetCurrentProcess(), (HANDLE)s,
|
|
893 GetCurrentProcess(), (LPHANDLE)voutfd,
|
|
894 0, FALSE, DUPLICATE_SAME_ACCESS);
|
|
895 return;
|
|
896
|
|
897 connect_failed:
|
|
898 closesocket (s);
|
|
899 report_file_error ("connection failed", list2 (host, name));
|
|
900 }
|
|
901
|
|
902 #endif
|
280
|
903
|
274
|
904 /*-----------------------------------------------------------------------*/
|
|
905 /* Initialization */
|
|
906 /*-----------------------------------------------------------------------*/
|
|
907
|
|
908 void
|
|
909 process_type_create_nt (void)
|
|
910 {
|
|
911 PROCESS_HAS_METHOD (nt, alloc_process_data);
|
|
912 PROCESS_HAS_METHOD (nt, finalize_process_data);
|
282
|
913 PROCESS_HAS_METHOD (nt, init_process);
|
274
|
914 PROCESS_HAS_METHOD (nt, create_process);
|
|
915 PROCESS_HAS_METHOD (nt, update_status_if_terminated);
|
|
916 PROCESS_HAS_METHOD (nt, send_process);
|
280
|
917 PROCESS_HAS_METHOD (nt, kill_child_process);
|
|
918 PROCESS_HAS_METHOD (nt, kill_process_by_pid);
|
274
|
919 #ifdef HAVE_SOCKETS
|
|
920 PROCESS_HAS_METHOD (nt, canonicalize_host_name);
|
|
921 PROCESS_HAS_METHOD (nt, open_network_stream);
|
|
922 #ifdef HAVE_MULTICAST
|
282
|
923 #error I won't do this until '95 has winsock2
|
274
|
924 PROCESS_HAS_METHOD (nt, open_multicast_group);
|
|
925 #endif
|
|
926 #endif
|
|
927 }
|
|
928
|
|
929 void
|
|
930 vars_of_process_nt (void)
|
|
931 {
|
|
932 }
|
|
933
|