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