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