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