Commit | Line | Data |
---|---|---|
6cdfb6e6 | 1 | /* Process support for Windows NT port of GNU EMACS. |
22759c72 | 2 | Copyright (C) 1992, 1995 Free Software Foundation, Inc. |
6cdfb6e6 | 3 | |
3b7ad313 EN |
4 | This file is part of GNU Emacs. |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. | |
6cdfb6e6 RS |
20 | |
21 | Drew Bliss Oct 14, 1993 | |
22 | Adapted from alarm.c by Tim Fleehart | |
23 | */ | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <errno.h> | |
28 | #include <io.h> | |
c519b5e1 | 29 | #include <fcntl.h> |
6cdfb6e6 RS |
30 | #include <signal.h> |
31 | ||
c519b5e1 GV |
32 | /* must include CRT headers *before* config.h */ |
33 | #include "config.h" | |
34 | #undef signal | |
35 | #undef wait | |
36 | #undef spawnve | |
37 | #undef select | |
38 | #undef kill | |
39 | ||
6cdfb6e6 RS |
40 | #include <windows.h> |
41 | ||
42 | #include "lisp.h" | |
43 | #include "nt.h" | |
44 | #include "systime.h" | |
3d7eead0 GV |
45 | #include "syswait.h" |
46 | #include "process.h" | |
47 | ||
93fdf2f8 RS |
48 | /* Control whether spawnve quotes arguments as necessary to ensure |
49 | correct parsing by child process. Because not all uses of spawnve | |
50 | are careful about constructing argv arrays, we make this behaviour | |
51 | conditional (off by default). */ | |
52 | Lisp_Object Vwin32_quote_process_args; | |
53 | ||
0ecf7d36 RS |
54 | /* Control whether create_child causes the process' window to be |
55 | hidden. The default is nil. */ | |
56 | Lisp_Object Vwin32_start_process_show_window; | |
57 | ||
817abdf6 KH |
58 | /* Time to sleep before reading from a subprocess output pipe - this |
59 | avoids the inefficiency of frequently reading small amounts of data. | |
60 | This is primarily necessary for handling DOS processes on Windows 95, | |
61 | but is useful for Win32 processes on both Win95 and NT as well. */ | |
62 | Lisp_Object Vwin32_pipe_read_delay; | |
63 | ||
0c04091e RS |
64 | /* Control conversion of upper case file names to lower case. |
65 | nil means no, t means yes. */ | |
66 | Lisp_Object Vwin32_downcase_file_names; | |
67 | ||
57445e59 | 68 | /* Keep track of whether we have already started a DOS program. */ |
817abdf6 KH |
69 | BOOL dos_process_running; |
70 | ||
3d7eead0 GV |
71 | #ifndef SYS_SIGLIST_DECLARED |
72 | extern char *sys_siglist[]; | |
73 | #endif | |
6cdfb6e6 | 74 | |
6cdfb6e6 | 75 | #ifdef EMACSDEBUG |
c519b5e1 | 76 | void _DebPrint (const char *fmt, ...) |
6cdfb6e6 | 77 | { |
c519b5e1 | 78 | char buf[1024]; |
6cdfb6e6 RS |
79 | va_list args; |
80 | ||
81 | va_start (args, fmt); | |
82 | vsprintf (buf, fmt, args); | |
83 | va_end (args); | |
84 | OutputDebugString (buf); | |
85 | } | |
86 | #endif | |
87 | ||
c519b5e1 | 88 | typedef void (_CALLBACK_ *signal_handler)(int); |
6cdfb6e6 RS |
89 | |
90 | /* Signal handlers...SIG_DFL == 0 so this is initialized correctly. */ | |
91 | static signal_handler sig_handlers[NSIG]; | |
92 | ||
93 | /* Fake signal implementation to record the SIGCHLD handler. */ | |
94 | signal_handler | |
c519b5e1 | 95 | sys_signal (int sig, signal_handler handler) |
6cdfb6e6 RS |
96 | { |
97 | signal_handler old; | |
98 | ||
99 | if (sig != SIGCHLD) | |
100 | { | |
101 | errno = EINVAL; | |
102 | return SIG_ERR; | |
103 | } | |
104 | old = sig_handlers[sig]; | |
105 | sig_handlers[sig] = handler; | |
106 | return old; | |
107 | } | |
108 | ||
c519b5e1 GV |
109 | /* Defined in <process.h> which conflicts with the local copy */ |
110 | #define _P_NOWAIT 1 | |
111 | ||
112 | /* Child process management list. */ | |
113 | int child_proc_count = 0; | |
114 | child_process child_procs[ MAX_CHILDREN ]; | |
115 | child_process *dead_child = NULL; | |
116 | ||
117 | DWORD WINAPI reader_thread (void *arg); | |
118 | ||
6cdfb6e6 | 119 | /* Find an unused process slot. */ |
c519b5e1 | 120 | child_process * |
6cdfb6e6 RS |
121 | new_child (void) |
122 | { | |
123 | child_process *cp; | |
c519b5e1 | 124 | DWORD id; |
6cdfb6e6 RS |
125 | |
126 | for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--) | |
127 | if (!CHILD_ACTIVE (cp)) | |
c519b5e1 GV |
128 | goto Initialise; |
129 | if (child_proc_count == MAX_CHILDREN) | |
130 | return NULL; | |
131 | cp = &child_procs[child_proc_count++]; | |
132 | ||
133 | Initialise: | |
134 | memset (cp, 0, sizeof(*cp)); | |
135 | cp->fd = -1; | |
136 | cp->pid = -1; | |
137 | cp->procinfo.hProcess = NULL; | |
138 | cp->status = STATUS_READ_ERROR; | |
139 | ||
140 | /* use manual reset event so that select() will function properly */ | |
141 | cp->char_avail = CreateEvent (NULL, TRUE, FALSE, NULL); | |
142 | if (cp->char_avail) | |
143 | { | |
144 | cp->char_consumed = CreateEvent (NULL, FALSE, FALSE, NULL); | |
145 | if (cp->char_consumed) | |
146 | { | |
147 | cp->thrd = CreateThread (NULL, 1024, reader_thread, cp, 0, &id); | |
148 | if (cp->thrd) | |
149 | return cp; | |
150 | } | |
151 | } | |
152 | delete_child (cp); | |
153 | return NULL; | |
154 | } | |
155 | ||
156 | void | |
157 | delete_child (child_process *cp) | |
158 | { | |
159 | int i; | |
160 | ||
161 | /* Should not be deleting a child that is still needed. */ | |
162 | for (i = 0; i < MAXDESC; i++) | |
163 | if (fd_info[i].cp == cp) | |
164 | abort (); | |
165 | ||
166 | if (!CHILD_ACTIVE (cp)) | |
167 | return; | |
168 | ||
169 | /* reap thread if necessary */ | |
170 | if (cp->thrd) | |
171 | { | |
172 | DWORD rc; | |
173 | ||
174 | if (GetExitCodeThread (cp->thrd, &rc) && rc == STILL_ACTIVE) | |
175 | { | |
176 | /* let the thread exit cleanly if possible */ | |
177 | cp->status = STATUS_READ_ERROR; | |
178 | SetEvent (cp->char_consumed); | |
179 | if (WaitForSingleObject (cp->thrd, 1000) != WAIT_OBJECT_0) | |
180 | { | |
181 | DebPrint (("delete_child.WaitForSingleObject (thread) failed " | |
182 | "with %lu for fd %ld\n", GetLastError (), cp->fd)); | |
183 | TerminateThread (cp->thrd, 0); | |
184 | } | |
185 | } | |
186 | CloseHandle (cp->thrd); | |
187 | cp->thrd = NULL; | |
188 | } | |
189 | if (cp->char_avail) | |
190 | { | |
191 | CloseHandle (cp->char_avail); | |
192 | cp->char_avail = NULL; | |
193 | } | |
194 | if (cp->char_consumed) | |
195 | { | |
196 | CloseHandle (cp->char_consumed); | |
197 | cp->char_consumed = NULL; | |
198 | } | |
199 | ||
200 | /* update child_proc_count (highest numbered slot in use plus one) */ | |
201 | if (cp == child_procs + child_proc_count - 1) | |
202 | { | |
203 | for (i = child_proc_count-1; i >= 0; i--) | |
204 | if (CHILD_ACTIVE (&child_procs[i])) | |
205 | { | |
206 | child_proc_count = i + 1; | |
207 | break; | |
208 | } | |
209 | } | |
210 | if (i < 0) | |
211 | child_proc_count = 0; | |
6cdfb6e6 RS |
212 | } |
213 | ||
214 | /* Find a child by pid. */ | |
215 | static child_process * | |
216 | find_child_pid (DWORD pid) | |
217 | { | |
218 | child_process *cp; | |
c519b5e1 | 219 | |
6cdfb6e6 RS |
220 | for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--) |
221 | if (CHILD_ACTIVE (cp) && pid == cp->pid) | |
222 | return cp; | |
223 | return NULL; | |
224 | } | |
225 | ||
6cdfb6e6 | 226 | |
c519b5e1 GV |
227 | /* Thread proc for child process and socket reader threads. Each thread |
228 | is normally blocked until woken by select() to check for input by | |
229 | reading one char. When the read completes, char_avail is signalled | |
230 | to wake up the select emulator and the thread blocks itself again. */ | |
6cdfb6e6 RS |
231 | DWORD WINAPI |
232 | reader_thread (void *arg) | |
233 | { | |
234 | child_process *cp; | |
235 | ||
236 | /* Our identity */ | |
237 | cp = (child_process *)arg; | |
238 | ||
239 | /* We have to wait for the go-ahead before we can start */ | |
c519b5e1 GV |
240 | if (cp == NULL || |
241 | WaitForSingleObject (cp->char_consumed, INFINITE) != WAIT_OBJECT_0) | |
242 | return 1; | |
243 | ||
6cdfb6e6 RS |
244 | for (;;) |
245 | { | |
c519b5e1 GV |
246 | int rc; |
247 | ||
248 | rc = _sys_read_ahead (cp->fd); | |
249 | ||
250 | /* The name char_avail is a misnomer - it really just means the | |
251 | read-ahead has completed, whether successfully or not. */ | |
6cdfb6e6 RS |
252 | if (!SetEvent (cp->char_avail)) |
253 | { | |
254 | DebPrint (("reader_thread.SetEvent failed with %lu for fd %ld\n", | |
255 | GetLastError (), cp->fd)); | |
c519b5e1 GV |
256 | return 1; |
257 | } | |
258 | ||
259 | if (rc == STATUS_READ_ERROR) | |
260 | return 1; | |
6cdfb6e6 RS |
261 | |
262 | /* If the read died, the child has died so let the thread die */ | |
c519b5e1 | 263 | if (rc == STATUS_READ_FAILED) |
6cdfb6e6 RS |
264 | break; |
265 | ||
266 | /* Wait until our input is acknowledged before reading again */ | |
267 | if (WaitForSingleObject (cp->char_consumed, INFINITE) != WAIT_OBJECT_0) | |
268 | { | |
269 | DebPrint (("reader_thread.WaitForSingleObject failed with " | |
270 | "%lu for fd %ld\n", GetLastError (), cp->fd)); | |
271 | break; | |
272 | } | |
273 | } | |
274 | return 0; | |
275 | } | |
276 | ||
277 | static BOOL | |
278 | create_child (char *exe, char *cmdline, char *env, | |
c519b5e1 | 279 | int * pPid, child_process *cp) |
6cdfb6e6 | 280 | { |
6cdfb6e6 RS |
281 | STARTUPINFO start; |
282 | SECURITY_ATTRIBUTES sec_attrs; | |
283 | SECURITY_DESCRIPTOR sec_desc; | |
284 | ||
c519b5e1 | 285 | if (cp == NULL) abort (); |
6cdfb6e6 RS |
286 | |
287 | memset (&start, 0, sizeof (start)); | |
288 | start.cb = sizeof (start); | |
289 | ||
58d4e829 | 290 | #ifdef HAVE_NTGUI |
0ecf7d36 RS |
291 | if (NILP (Vwin32_start_process_show_window)) |
292 | start.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; | |
293 | else | |
294 | start.dwFlags = STARTF_USESTDHANDLES; | |
58d4e829 GV |
295 | start.wShowWindow = SW_HIDE; |
296 | ||
297 | start.hStdInput = GetStdHandle (STD_INPUT_HANDLE); | |
298 | start.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE); | |
299 | start.hStdError = GetStdHandle (STD_ERROR_HANDLE); | |
300 | #endif /* HAVE_NTGUI */ | |
301 | ||
6cdfb6e6 RS |
302 | /* Explicitly specify no security */ |
303 | if (!InitializeSecurityDescriptor (&sec_desc, SECURITY_DESCRIPTOR_REVISION)) | |
c519b5e1 | 304 | goto EH_Fail; |
6cdfb6e6 | 305 | if (!SetSecurityDescriptorDacl (&sec_desc, TRUE, NULL, FALSE)) |
c519b5e1 | 306 | goto EH_Fail; |
6cdfb6e6 RS |
307 | sec_attrs.nLength = sizeof (sec_attrs); |
308 | sec_attrs.lpSecurityDescriptor = &sec_desc; | |
309 | sec_attrs.bInheritHandle = FALSE; | |
310 | ||
311 | if (!CreateProcess (exe, cmdline, &sec_attrs, NULL, TRUE, | |
c519b5e1 GV |
312 | CREATE_NEW_PROCESS_GROUP, |
313 | env, NULL, | |
314 | &start, &cp->procinfo)) | |
315 | goto EH_Fail; | |
316 | ||
317 | cp->pid = (int) cp->procinfo.dwProcessId; | |
318 | ||
319 | /* Hack for Windows 95, which assigns large (ie negative) pids */ | |
320 | if (cp->pid < 0) | |
321 | cp->pid = -cp->pid; | |
322 | ||
323 | /* pid must fit in a Lisp_Int */ | |
324 | cp->pid = (cp->pid & VALMASK); | |
325 | ||
326 | ||
327 | *pPid = cp->pid; | |
6cdfb6e6 RS |
328 | |
329 | return TRUE; | |
330 | ||
6cdfb6e6 | 331 | EH_Fail: |
c519b5e1 | 332 | DebPrint (("create_child.CreateProcess failed: %ld\n", GetLastError());); |
6cdfb6e6 RS |
333 | return FALSE; |
334 | } | |
335 | ||
336 | /* create_child doesn't know what emacs' file handle will be for waiting | |
337 | on output from the child, so we need to make this additional call | |
338 | to register the handle with the process | |
339 | This way the select emulator knows how to match file handles with | |
340 | entries in child_procs. */ | |
341 | void | |
342 | register_child (int pid, int fd) | |
343 | { | |
344 | child_process *cp; | |
345 | ||
346 | cp = find_child_pid (pid); | |
347 | if (cp == NULL) | |
348 | { | |
349 | DebPrint (("register_child unable to find pid %lu\n", pid)); | |
350 | return; | |
351 | } | |
352 | ||
353 | #ifdef FULL_DEBUG | |
354 | DebPrint (("register_child registered fd %d with pid %lu\n", fd, pid)); | |
355 | #endif | |
356 | ||
357 | cp->fd = fd; | |
6cdfb6e6 | 358 | |
c519b5e1 GV |
359 | /* thread is initially blocked until select is called; set status so |
360 | that select will release thread */ | |
361 | cp->status = STATUS_READ_ACKNOWLEDGED; | |
362 | ||
363 | /* attach child_process to fd_info */ | |
364 | if (fd_info[fd].cp != NULL) | |
6cdfb6e6 | 365 | { |
c519b5e1 GV |
366 | DebPrint (("register_child: fd_info[%d] apparently in use!\n", fd)); |
367 | abort (); | |
6cdfb6e6 | 368 | } |
c519b5e1 GV |
369 | |
370 | fd_info[fd].cp = cp; | |
6cdfb6e6 RS |
371 | } |
372 | ||
373 | /* When a process dies its pipe will break so the reader thread will | |
374 | signal failure to the select emulator. | |
375 | The select emulator then calls this routine to clean up. | |
376 | Since the thread signaled failure we can assume it is exiting. */ | |
377 | static void | |
c519b5e1 | 378 | reap_subprocess (child_process *cp) |
6cdfb6e6 | 379 | { |
c519b5e1 | 380 | if (cp->procinfo.hProcess) |
6cdfb6e6 | 381 | { |
c519b5e1 GV |
382 | /* Reap the process */ |
383 | if (WaitForSingleObject (cp->procinfo.hProcess, INFINITE) != WAIT_OBJECT_0) | |
384 | DebPrint (("reap_subprocess.WaitForSingleObject (process) failed " | |
385 | "with %lu for fd %ld\n", GetLastError (), cp->fd)); | |
386 | CloseHandle (cp->procinfo.hProcess); | |
387 | cp->procinfo.hProcess = NULL; | |
388 | CloseHandle (cp->procinfo.hThread); | |
389 | cp->procinfo.hThread = NULL; | |
817abdf6 KH |
390 | |
391 | /* If this was a DOS process, indicate that it is now safe to | |
57445e59 | 392 | start a new one. */ |
817abdf6 KH |
393 | if (cp->is_dos_process) |
394 | dos_process_running = FALSE; | |
6cdfb6e6 | 395 | } |
c519b5e1 GV |
396 | |
397 | /* For asynchronous children, the child_proc resources will be freed | |
398 | when the last pipe read descriptor is closed; for synchronous | |
399 | children, we must explicitly free the resources now because | |
400 | register_child has not been called. */ | |
401 | if (cp->fd == -1) | |
402 | delete_child (cp); | |
6cdfb6e6 RS |
403 | } |
404 | ||
405 | /* Wait for any of our existing child processes to die | |
406 | When it does, close its handle | |
407 | Return the pid and fill in the status if non-NULL. */ | |
22759c72 | 408 | |
6cdfb6e6 | 409 | int |
c519b5e1 | 410 | sys_wait (int *status) |
6cdfb6e6 RS |
411 | { |
412 | DWORD active, retval; | |
413 | int nh; | |
c519b5e1 | 414 | int pid; |
6cdfb6e6 RS |
415 | child_process *cp, *cps[MAX_CHILDREN]; |
416 | HANDLE wait_hnd[MAX_CHILDREN]; | |
417 | ||
418 | nh = 0; | |
419 | if (dead_child != NULL) | |
420 | { | |
421 | /* We want to wait for a specific child */ | |
c519b5e1 | 422 | wait_hnd[nh] = dead_child->procinfo.hProcess; |
6cdfb6e6 | 423 | cps[nh] = dead_child; |
c519b5e1 | 424 | if (!wait_hnd[nh]) abort (); |
6cdfb6e6 RS |
425 | nh++; |
426 | } | |
427 | else | |
428 | { | |
429 | for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--) | |
c519b5e1 GV |
430 | /* some child_procs might be sockets; ignore them */ |
431 | if (CHILD_ACTIVE (cp) && cp->procinfo.hProcess) | |
6cdfb6e6 | 432 | { |
c519b5e1 | 433 | wait_hnd[nh] = cp->procinfo.hProcess; |
6cdfb6e6 | 434 | cps[nh] = cp; |
c519b5e1 | 435 | if (!wait_hnd[nh]) abort (); |
6cdfb6e6 RS |
436 | nh++; |
437 | } | |
438 | } | |
439 | ||
440 | if (nh == 0) | |
441 | { | |
442 | /* Nothing to wait on, so fail */ | |
443 | errno = ECHILD; | |
444 | return -1; | |
445 | } | |
446 | ||
447 | active = WaitForMultipleObjects (nh, wait_hnd, FALSE, INFINITE); | |
448 | if (active == WAIT_FAILED) | |
449 | { | |
450 | errno = EBADF; | |
451 | return -1; | |
452 | } | |
453 | else if (active == WAIT_TIMEOUT) | |
454 | { | |
455 | /* Should never happen */ | |
456 | errno = EINVAL; | |
457 | return -1; | |
458 | } | |
459 | else if (active >= WAIT_OBJECT_0 && | |
460 | active < WAIT_OBJECT_0+MAXIMUM_WAIT_OBJECTS) | |
461 | { | |
462 | active -= WAIT_OBJECT_0; | |
463 | } | |
464 | else if (active >= WAIT_ABANDONED_0 && | |
465 | active < WAIT_ABANDONED_0+MAXIMUM_WAIT_OBJECTS) | |
466 | { | |
467 | active -= WAIT_ABANDONED_0; | |
468 | } | |
469 | ||
470 | if (!GetExitCodeProcess (wait_hnd[active], &retval)) | |
471 | { | |
472 | DebPrint (("Wait.GetExitCodeProcess failed with %lu\n", | |
473 | GetLastError ())); | |
474 | retval = 1; | |
475 | } | |
476 | if (retval == STILL_ACTIVE) | |
477 | { | |
478 | /* Should never happen */ | |
479 | DebPrint (("Wait.WaitForMultipleObjects returned an active process\n")); | |
480 | errno = EINVAL; | |
481 | return -1; | |
482 | } | |
bc69349b RS |
483 | |
484 | /* Massage the exit code from the process to match the format expected | |
8e6208c5 | 485 | by the WIFSTOPPED et al macros in syswait.h. Only WIFSIGNALED and |
bc69349b RS |
486 | WIFEXITED are supported; WIFSTOPPED doesn't make sense under NT. */ |
487 | ||
488 | if (retval == STATUS_CONTROL_C_EXIT) | |
489 | retval = SIGINT; | |
490 | else | |
491 | retval <<= 8; | |
c519b5e1 | 492 | |
6cdfb6e6 | 493 | cp = cps[active]; |
c519b5e1 GV |
494 | pid = cp->pid; |
495 | #ifdef FULL_DEBUG | |
496 | DebPrint (("Wait signaled with process pid %d\n", cp->pid)); | |
497 | #endif | |
22759c72 | 498 | |
6cdfb6e6 RS |
499 | if (status) |
500 | { | |
22759c72 KH |
501 | *status = retval; |
502 | } | |
503 | else if (synch_process_alive) | |
504 | { | |
505 | synch_process_alive = 0; | |
22759c72 | 506 | |
3d7eead0 GV |
507 | /* Report the status of the synchronous process. */ |
508 | if (WIFEXITED (retval)) | |
509 | synch_process_retcode = WRETCODE (retval); | |
510 | else if (WIFSIGNALED (retval)) | |
511 | { | |
512 | int code = WTERMSIG (retval); | |
513 | char *signame = 0; | |
514 | ||
515 | if (code < NSIG) | |
516 | { | |
517 | /* Suppress warning if the table has const char *. */ | |
518 | signame = (char *) sys_siglist[code]; | |
519 | } | |
520 | if (signame == 0) | |
521 | signame = "unknown"; | |
522 | ||
523 | synch_process_death = signame; | |
524 | } | |
c519b5e1 GV |
525 | |
526 | reap_subprocess (cp); | |
6cdfb6e6 RS |
527 | } |
528 | ||
c519b5e1 | 529 | return pid; |
6cdfb6e6 RS |
530 | } |
531 | ||
817abdf6 KH |
532 | int |
533 | win32_is_dos_binary (char * filename) | |
534 | { | |
535 | IMAGE_DOS_HEADER dos_header; | |
536 | DWORD signature; | |
537 | int fd; | |
538 | int is_dos_binary = FALSE; | |
539 | ||
540 | fd = open (filename, O_RDONLY | O_BINARY, 0); | |
541 | if (fd >= 0) | |
542 | { | |
543 | char * p = strrchr (filename, '.'); | |
544 | ||
545 | /* We can only identify DOS .com programs from the extension. */ | |
546 | if (p && stricmp (p, ".com") == 0) | |
547 | is_dos_binary = TRUE; | |
548 | else if (p && stricmp (p, ".bat") == 0) | |
549 | { | |
550 | /* A DOS shell script - it appears that CreateProcess is happy | |
551 | to accept this (somewhat surprisingly); presumably it looks | |
552 | at COMSPEC to determine what executable to actually invoke. | |
553 | Therefore, we have to do the same here as well. */ | |
554 | p = getenv ("COMSPEC"); | |
555 | if (p) | |
556 | is_dos_binary = win32_is_dos_binary (p); | |
557 | } | |
558 | else | |
559 | { | |
560 | /* Look for DOS .exe signature - if found, we must also check | |
561 | that it isn't really a 16- or 32-bit Windows exe, since | |
562 | both formats start with a DOS program stub. Note that | |
563 | 16-bit Windows executables use the OS/2 1.x format. */ | |
564 | if (read (fd, &dos_header, sizeof (dos_header)) == sizeof (dos_header) | |
565 | && dos_header.e_magic == IMAGE_DOS_SIGNATURE | |
566 | && lseek (fd, dos_header.e_lfanew, SEEK_SET) != -1) | |
567 | { | |
568 | if (read (fd, &signature, sizeof (signature)) != sizeof (signature) | |
569 | || (signature != IMAGE_NT_SIGNATURE && | |
570 | LOWORD (signature) != IMAGE_OS2_SIGNATURE)) | |
571 | is_dos_binary = TRUE; | |
572 | } | |
573 | } | |
574 | close (fd); | |
575 | } | |
576 | ||
577 | return is_dos_binary; | |
578 | } | |
579 | ||
6cdfb6e6 RS |
580 | /* We pass our process ID to our children by setting up an environment |
581 | variable in their environment. */ | |
582 | char ppid_env_var_buffer[64]; | |
583 | ||
584 | /* When a new child process is created we need to register it in our list, | |
585 | so intercept spawn requests. */ | |
586 | int | |
c519b5e1 | 587 | sys_spawnve (int mode, char *cmdname, char **argv, char **envp) |
6cdfb6e6 | 588 | { |
0a4de642 | 589 | Lisp_Object program, full; |
6cdfb6e6 RS |
590 | char *cmdline, *env, *parg, **targ; |
591 | int arglen; | |
c519b5e1 GV |
592 | int pid; |
593 | child_process *cp; | |
817abdf6 | 594 | int is_dos_binary; |
c519b5e1 GV |
595 | |
596 | /* We don't care about the other modes */ | |
597 | if (mode != _P_NOWAIT) | |
598 | { | |
599 | errno = EINVAL; | |
600 | return -1; | |
601 | } | |
0a4de642 RS |
602 | |
603 | /* Handle executable names without an executable suffix. */ | |
604 | program = make_string (cmdname, strlen (cmdname)); | |
605 | if (NILP (Ffile_executable_p (program))) | |
606 | { | |
607 | struct gcpro gcpro1; | |
608 | ||
609 | full = Qnil; | |
610 | GCPRO1 (program); | |
611 | openp (Vexec_path, program, EXEC_SUFFIXES, &full, 1); | |
612 | UNGCPRO; | |
613 | if (NILP (full)) | |
614 | { | |
615 | errno = EINVAL; | |
616 | return -1; | |
617 | } | |
618 | cmdname = XSTRING (full)->data; | |
619 | argv[0] = cmdname; | |
620 | } | |
621 | ||
c519b5e1 GV |
622 | /* make sure cmdname is in DOS format */ |
623 | strcpy (cmdname = alloca (strlen (cmdname) + 1), argv[0]); | |
624 | unixtodos_filename (cmdname); | |
625 | argv[0] = cmdname; | |
817abdf6 KH |
626 | |
627 | /* Check if program is a DOS executable, and if so whether we are | |
628 | allowed to start it. */ | |
629 | is_dos_binary = win32_is_dos_binary (cmdname); | |
57445e59 | 630 | if (is_dos_binary && dos_process_running) |
817abdf6 | 631 | { |
de05443f | 632 | errno = EAGAIN; |
817abdf6 KH |
633 | return -1; |
634 | } | |
6cdfb6e6 RS |
635 | |
636 | /* we have to do some conjuring here to put argv and envp into the | |
637 | form CreateProcess wants... argv needs to be a space separated/null | |
638 | terminated list of parameters, and envp is a null | |
639 | separated/double-null terminated list of parameters. | |
c519b5e1 GV |
640 | |
641 | Additionally, zero-length args and args containing whitespace need | |
642 | to be wrapped in double quotes. Args containing embedded double | |
643 | quotes (as opposed to enclosing quotes, which we leave alone) are | |
644 | usually illegal (most Win32 programs do not implement escaping of | |
645 | double quotes - sad but true, at least for programs compiled with | |
646 | MSVC), but we will escape quotes anyway for those programs that can | |
647 | handle it. The Win32 gcc library from Cygnus doubles quotes to | |
648 | escape them, so we will use that convention. | |
6cdfb6e6 RS |
649 | |
650 | Since I have no idea how large argv and envp are likely to be | |
651 | we figure out list lengths on the fly and allocate them. */ | |
652 | ||
653 | /* do argv... */ | |
654 | arglen = 0; | |
655 | targ = argv; | |
656 | while (*targ) | |
657 | { | |
c519b5e1 GV |
658 | char * p = *targ; |
659 | int add_quotes = 0; | |
660 | ||
661 | if (*p == 0) | |
662 | add_quotes = 1; | |
663 | while (*p) | |
664 | if (*p++ == '"') | |
665 | { | |
666 | /* allow for embedded quotes to be doubled - we won't | |
667 | actually double quotes that aren't embedded though */ | |
668 | arglen++; | |
669 | add_quotes = 1; | |
670 | } | |
671 | else if (*p == ' ' || *p == '\t') | |
672 | add_quotes = 1; | |
673 | if (add_quotes) | |
674 | arglen += 2; | |
6cdfb6e6 RS |
675 | arglen += strlen (*targ++) + 1; |
676 | } | |
c519b5e1 | 677 | cmdline = alloca (arglen); |
6cdfb6e6 RS |
678 | targ = argv; |
679 | parg = cmdline; | |
680 | while (*targ) | |
681 | { | |
c519b5e1 GV |
682 | char * p = *targ; |
683 | int add_quotes = 0; | |
684 | ||
685 | if (*p == 0) | |
686 | add_quotes = 1; | |
93fdf2f8 RS |
687 | |
688 | if (!NILP (Vwin32_quote_process_args)) | |
689 | { | |
690 | /* This is conditional because it sometimes causes more | |
691 | problems than it solves, since argv arrays are not always | |
692 | carefully constructed. M-x grep, for instance, passes the | |
693 | whole command line as one argument, so it becomes | |
694 | impossible to pass a regexp which contains spaces. */ | |
695 | for ( ; *p; p++) | |
696 | if (*p == ' ' || *p == '\t' || *p == '"') | |
697 | add_quotes = 1; | |
698 | } | |
c519b5e1 GV |
699 | if (add_quotes) |
700 | { | |
701 | char * first; | |
702 | char * last; | |
703 | ||
704 | p = *targ; | |
705 | first = p; | |
706 | last = p + strlen (p) - 1; | |
707 | *parg++ = '"'; | |
708 | while (*p) | |
709 | { | |
710 | if (*p == '"' && p > first && p < last) | |
711 | *parg++ = '"'; /* double up embedded quotes only */ | |
712 | *parg++ = *p++; | |
713 | } | |
714 | *parg++ = '"'; | |
715 | } | |
716 | else | |
717 | { | |
718 | strcpy (parg, *targ); | |
719 | parg += strlen (*targ); | |
720 | } | |
6cdfb6e6 | 721 | *parg++ = ' '; |
c519b5e1 | 722 | targ++; |
6cdfb6e6 RS |
723 | } |
724 | *--parg = '\0'; | |
725 | ||
726 | /* and envp... */ | |
727 | arglen = 1; | |
728 | targ = envp; | |
729 | while (*targ) | |
730 | { | |
731 | arglen += strlen (*targ++) + 1; | |
732 | } | |
733 | sprintf (ppid_env_var_buffer, "__PARENT_PROCESS_ID=%d", | |
734 | GetCurrentProcessId ()); | |
735 | arglen += strlen (ppid_env_var_buffer) + 1; | |
736 | ||
c519b5e1 | 737 | env = alloca (arglen); |
6cdfb6e6 RS |
738 | targ = envp; |
739 | parg = env; | |
740 | while (*targ) | |
741 | { | |
742 | strcpy (parg, *targ); | |
743 | parg += strlen (*targ++); | |
744 | *parg++ = '\0'; | |
745 | } | |
746 | strcpy (parg, ppid_env_var_buffer); | |
747 | parg += strlen (ppid_env_var_buffer); | |
748 | *parg++ = '\0'; | |
749 | *parg = '\0'; | |
c519b5e1 GV |
750 | |
751 | cp = new_child (); | |
752 | if (cp == NULL) | |
753 | { | |
754 | errno = EAGAIN; | |
755 | return -1; | |
756 | } | |
6cdfb6e6 RS |
757 | |
758 | /* Now create the process. */ | |
c519b5e1 | 759 | if (!create_child (cmdname, cmdline, env, &pid, cp)) |
6cdfb6e6 | 760 | { |
c519b5e1 | 761 | delete_child (cp); |
6cdfb6e6 | 762 | errno = ENOEXEC; |
c519b5e1 | 763 | return -1; |
6cdfb6e6 | 764 | } |
817abdf6 KH |
765 | |
766 | if (is_dos_binary) | |
767 | { | |
768 | cp->is_dos_process = TRUE; | |
769 | dos_process_running = TRUE; | |
770 | } | |
6cdfb6e6 | 771 | |
c519b5e1 | 772 | return pid; |
6cdfb6e6 RS |
773 | } |
774 | ||
775 | /* Emulate the select call | |
776 | Wait for available input on any of the given rfds, or timeout if | |
777 | a timeout is given and no input is detected | |
778 | wfds and efds are not supported and must be NULL. */ | |
779 | ||
780 | /* From ntterm.c */ | |
781 | extern HANDLE keyboard_handle; | |
782 | /* From process.c */ | |
783 | extern int proc_buffered_char[]; | |
784 | ||
785 | int | |
22759c72 KH |
786 | sys_select (int nfds, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds, |
787 | EMACS_TIME *timeout) | |
6cdfb6e6 RS |
788 | { |
789 | SELECT_TYPE orfds; | |
790 | DWORD timeout_ms; | |
791 | int i, nh, nr; | |
792 | DWORD active; | |
c519b5e1 GV |
793 | child_process *cp; |
794 | HANDLE wait_hnd[MAXDESC]; | |
795 | int fdindex[MAXDESC]; /* mapping from wait handles back to descriptors */ | |
6cdfb6e6 RS |
796 | |
797 | /* If the descriptor sets are NULL but timeout isn't, then just Sleep. */ | |
798 | if (rfds == NULL && wfds == NULL && efds == NULL && timeout != NULL) | |
799 | { | |
22759c72 | 800 | Sleep (timeout->tv_sec * 1000 + timeout->tv_usec / 1000); |
6cdfb6e6 RS |
801 | return 0; |
802 | } | |
803 | ||
804 | /* Otherwise, we only handle rfds, so fail otherwise. */ | |
805 | if (rfds == NULL || wfds != NULL || efds != NULL) | |
806 | { | |
807 | errno = EINVAL; | |
808 | return -1; | |
809 | } | |
810 | ||
811 | orfds = *rfds; | |
812 | FD_ZERO (rfds); | |
813 | nr = 0; | |
814 | ||
815 | /* Build a list of handles to wait on. */ | |
816 | nh = 0; | |
817 | for (i = 0; i < nfds; i++) | |
818 | if (FD_ISSET (i, &orfds)) | |
819 | { | |
820 | if (i == 0) | |
821 | { | |
c519b5e1 GV |
822 | if (keyboard_handle) |
823 | { | |
824 | /* Handle stdin specially */ | |
825 | wait_hnd[nh] = keyboard_handle; | |
826 | fdindex[nh] = i; | |
827 | nh++; | |
828 | } | |
6cdfb6e6 RS |
829 | |
830 | /* Check for any emacs-generated input in the queue since | |
831 | it won't be detected in the wait */ | |
832 | if (detect_input_pending ()) | |
833 | { | |
834 | FD_SET (i, rfds); | |
c519b5e1 | 835 | return 1; |
6cdfb6e6 RS |
836 | } |
837 | } | |
838 | else | |
839 | { | |
c519b5e1 GV |
840 | /* Child process and socket input */ |
841 | cp = fd_info[i].cp; | |
6cdfb6e6 RS |
842 | if (cp) |
843 | { | |
c519b5e1 GV |
844 | int current_status = cp->status; |
845 | ||
846 | if (current_status == STATUS_READ_ACKNOWLEDGED) | |
847 | { | |
848 | /* Tell reader thread which file handle to use. */ | |
849 | cp->fd = i; | |
850 | /* Wake up the reader thread for this process */ | |
851 | cp->status = STATUS_READ_READY; | |
852 | if (!SetEvent (cp->char_consumed)) | |
853 | DebPrint (("nt_select.SetEvent failed with " | |
854 | "%lu for fd %ld\n", GetLastError (), i)); | |
855 | } | |
856 | ||
857 | #ifdef CHECK_INTERLOCK | |
858 | /* slightly crude cross-checking of interlock between threads */ | |
859 | ||
860 | current_status = cp->status; | |
861 | if (WaitForSingleObject (cp->char_avail, 0) == WAIT_OBJECT_0) | |
862 | { | |
863 | /* char_avail has been signalled, so status (which may | |
864 | have changed) should indicate read has completed | |
865 | but has not been acknowledged. */ | |
866 | current_status = cp->status; | |
867 | if (current_status != STATUS_READ_SUCCEEDED && | |
868 | current_status != STATUS_READ_FAILED) | |
869 | DebPrint (("char_avail set, but read not completed: status %d\n", | |
870 | current_status)); | |
871 | } | |
872 | else | |
873 | { | |
874 | /* char_avail has not been signalled, so status should | |
875 | indicate that read is in progress; small possibility | |
876 | that read has completed but event wasn't yet signalled | |
877 | when we tested it (because a context switch occurred | |
878 | or if running on separate CPUs). */ | |
879 | if (current_status != STATUS_READ_READY && | |
880 | current_status != STATUS_READ_IN_PROGRESS && | |
881 | current_status != STATUS_READ_SUCCEEDED && | |
882 | current_status != STATUS_READ_FAILED) | |
883 | DebPrint (("char_avail reset, but read status is bad: %d\n", | |
884 | current_status)); | |
885 | } | |
886 | #endif | |
887 | wait_hnd[nh] = cp->char_avail; | |
888 | fdindex[nh] = i; | |
889 | if (!wait_hnd[nh]) abort (); | |
890 | nh++; | |
6cdfb6e6 RS |
891 | #ifdef FULL_DEBUG |
892 | DebPrint (("select waiting on child %d fd %d\n", | |
893 | cp-child_procs, i)); | |
894 | #endif | |
6cdfb6e6 RS |
895 | } |
896 | else | |
897 | { | |
c519b5e1 GV |
898 | /* Unable to find something to wait on for this fd, skip */ |
899 | DebPrint (("sys_select: fd %ld is invalid! ignoring\n", i)); | |
900 | abort (); | |
6cdfb6e6 RS |
901 | } |
902 | } | |
903 | } | |
904 | ||
905 | /* Nothing to look for, so we didn't find anything */ | |
906 | if (nh == 0) | |
907 | { | |
22759c72 | 908 | if (timeout) |
22759c72 | 909 | Sleep (timeout->tv_sec * 1000 + timeout->tv_usec / 1000); |
6cdfb6e6 RS |
910 | return 0; |
911 | } | |
6cdfb6e6 RS |
912 | |
913 | /* | |
914 | Wait for input | |
915 | If a child process dies while this is waiting, its pipe will break | |
916 | so the reader thread will signal an error condition, thus, the wait | |
917 | will wake up | |
918 | */ | |
22759c72 | 919 | timeout_ms = timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : INFINITE; |
c519b5e1 | 920 | |
6cdfb6e6 | 921 | active = WaitForMultipleObjects (nh, wait_hnd, FALSE, timeout_ms); |
c519b5e1 | 922 | |
6cdfb6e6 RS |
923 | if (active == WAIT_FAILED) |
924 | { | |
925 | DebPrint (("select.WaitForMultipleObjects (%d, %lu) failed with %lu\n", | |
926 | nh, timeout_ms, GetLastError ())); | |
c519b5e1 GV |
927 | /* don't return EBADF - this causes wait_reading_process_input to |
928 | abort; WAIT_FAILED is returned when single-stepping under | |
929 | Windows 95 after switching thread focus in debugger, and | |
930 | possibly at other times. */ | |
931 | errno = EINTR; | |
6cdfb6e6 RS |
932 | return -1; |
933 | } | |
934 | else if (active == WAIT_TIMEOUT) | |
935 | { | |
936 | return 0; | |
937 | } | |
938 | else if (active >= WAIT_OBJECT_0 && | |
939 | active < WAIT_OBJECT_0+MAXIMUM_WAIT_OBJECTS) | |
940 | { | |
941 | active -= WAIT_OBJECT_0; | |
942 | } | |
943 | else if (active >= WAIT_ABANDONED_0 && | |
944 | active < WAIT_ABANDONED_0+MAXIMUM_WAIT_OBJECTS) | |
945 | { | |
946 | active -= WAIT_ABANDONED_0; | |
947 | } | |
6cdfb6e6 | 948 | |
c519b5e1 GV |
949 | /* Loop over all handles after active (now officially documented as |
950 | being the first signalled handle in the array). We do this to | |
951 | ensure fairness, so that all channels with data available will be | |
952 | processed - otherwise higher numbered channels could be starved. */ | |
953 | do | |
6cdfb6e6 | 954 | { |
c519b5e1 GV |
955 | if (fdindex[active] == 0) |
956 | { | |
957 | /* Keyboard input available */ | |
958 | FD_SET (0, rfds); | |
6cdfb6e6 | 959 | nr++; |
c519b5e1 | 960 | } |
6cdfb6e6 | 961 | else |
c519b5e1 GV |
962 | { |
963 | /* must be a socket or pipe */ | |
964 | int current_status; | |
965 | ||
966 | cp = fd_info[ fdindex[active] ].cp; | |
967 | ||
968 | /* Read ahead should have completed, either succeeding or failing. */ | |
969 | FD_SET (fdindex[active], rfds); | |
970 | nr++; | |
971 | current_status = cp->status; | |
972 | if (current_status != STATUS_READ_SUCCEEDED) | |
973 | { | |
974 | if (current_status != STATUS_READ_FAILED) | |
975 | DebPrint (("internal error: subprocess pipe signalled " | |
976 | "at the wrong time (status %d)\n!", current_status)); | |
977 | ||
978 | /* The child_process entry for a socket or pipe will be | |
979 | freed when the last descriptor using it is closed; for | |
980 | pipes, we call the SIGCHLD handler. */ | |
981 | if (fd_info[ fdindex[active] ].flags & FILE_PIPE) | |
982 | { | |
983 | /* The SIGCHLD handler will do a Wait so we know it won't | |
984 | return until the process is dead | |
985 | We force Wait to only wait for this process to avoid it | |
986 | picking up other children that happen to be dead but that | |
987 | we haven't noticed yet | |
988 | SIG_DFL for SIGCHLD is ignore? */ | |
989 | if (sig_handlers[SIGCHLD] != SIG_DFL && | |
990 | sig_handlers[SIGCHLD] != SIG_IGN) | |
991 | { | |
6cdfb6e6 | 992 | #ifdef FULL_DEBUG |
c519b5e1 GV |
993 | DebPrint (("select calling SIGCHLD handler for pid %d\n", |
994 | cp->pid)); | |
6cdfb6e6 | 995 | #endif |
c519b5e1 GV |
996 | dead_child = cp; |
997 | sig_handlers[SIGCHLD] (SIGCHLD); | |
998 | dead_child = NULL; | |
999 | } | |
1000 | ||
1001 | /* Clean up the child process entry in the table */ | |
1002 | reap_subprocess (cp); | |
1003 | } | |
1004 | } | |
1005 | } | |
1006 | ||
1007 | /* Test for input on remaining channels. */ | |
1008 | while (++active < nh) | |
1009 | if (WaitForSingleObject (wait_hnd[active], 0) == WAIT_OBJECT_0) | |
1010 | break; | |
1011 | } while (active < nh); | |
1012 | ||
6cdfb6e6 RS |
1013 | return nr; |
1014 | } | |
1015 | ||
c519b5e1 | 1016 | /* Substitute for certain kill () operations */ |
6cdfb6e6 | 1017 | int |
c519b5e1 | 1018 | sys_kill (int pid, int sig) |
6cdfb6e6 RS |
1019 | { |
1020 | child_process *cp; | |
c519b5e1 GV |
1021 | HANDLE proc_hand; |
1022 | int need_to_free = 0; | |
1023 | int rc = 0; | |
6cdfb6e6 RS |
1024 | |
1025 | /* Only handle signals that will result in the process dying */ | |
1026 | if (sig != SIGINT && sig != SIGKILL && sig != SIGQUIT && sig != SIGHUP) | |
1027 | { | |
1028 | errno = EINVAL; | |
1029 | return -1; | |
1030 | } | |
c519b5e1 | 1031 | |
6cdfb6e6 RS |
1032 | cp = find_child_pid (pid); |
1033 | if (cp == NULL) | |
1034 | { | |
c519b5e1 GV |
1035 | proc_hand = OpenProcess (PROCESS_TERMINATE, 0, pid); |
1036 | if (proc_hand == NULL) | |
1037 | { | |
1038 | errno = EPERM; | |
1039 | return -1; | |
1040 | } | |
1041 | need_to_free = 1; | |
1042 | } | |
1043 | else | |
1044 | { | |
1045 | proc_hand = cp->procinfo.hProcess; | |
1046 | pid = cp->procinfo.dwProcessId; | |
6cdfb6e6 RS |
1047 | } |
1048 | ||
1049 | if (sig == SIGINT) | |
1050 | { | |
c519b5e1 | 1051 | /* Ctrl-Break is NT equivalent of SIGINT. */ |
6cdfb6e6 RS |
1052 | if (!GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, pid)) |
1053 | { | |
c519b5e1 | 1054 | DebPrint (("sys_kill.GenerateConsoleCtrlEvent return %d " |
6cdfb6e6 RS |
1055 | "for pid %lu\n", GetLastError (), pid)); |
1056 | errno = EINVAL; | |
c519b5e1 | 1057 | rc = -1; |
6cdfb6e6 RS |
1058 | } |
1059 | } | |
1060 | else | |
1061 | { | |
1062 | /* Kill the process. On Win32 this doesn't kill child processes | |
8eae7766 RS |
1063 | so it doesn't work very well for shells which is why it's not |
1064 | used in every case. Also, don't try to terminate DOS processes | |
1065 | (on Win95), because this will hang Emacs. */ | |
1066 | if (!(cp && cp->is_dos_process) | |
1067 | && !TerminateProcess (proc_hand, 0xff)) | |
6cdfb6e6 | 1068 | { |
c519b5e1 | 1069 | DebPrint (("sys_kill.TerminateProcess returned %d " |
6cdfb6e6 RS |
1070 | "for pid %lu\n", GetLastError (), pid)); |
1071 | errno = EINVAL; | |
c519b5e1 | 1072 | rc = -1; |
6cdfb6e6 RS |
1073 | } |
1074 | } | |
c519b5e1 GV |
1075 | |
1076 | if (need_to_free) | |
1077 | CloseHandle (proc_hand); | |
1078 | ||
1079 | return rc; | |
6cdfb6e6 RS |
1080 | } |
1081 | ||
c519b5e1 GV |
1082 | extern int report_file_error (char *, Lisp_Object); |
1083 | ||
1084 | /* The following two routines are used to manipulate stdin, stdout, and | |
1085 | stderr of our child processes. | |
1086 | ||
1087 | Assuming that in, out, and err are *not* inheritable, we make them | |
1088 | stdin, stdout, and stderr of the child as follows: | |
1089 | ||
1090 | - Save the parent's current standard handles. | |
1091 | - Set the std handles to inheritable duplicates of the ones being passed in. | |
1092 | (Note that _get_osfhandle() is an io.h procedure that retrieves the | |
1093 | NT file handle for a crt file descriptor.) | |
1094 | - Spawn the child, which inherits in, out, and err as stdin, | |
1095 | stdout, and stderr. (see Spawnve) | |
1096 | - Close the std handles passed to the child. | |
1097 | - Reset the parent's standard handles to the saved handles. | |
1098 | (see reset_standard_handles) | |
1099 | We assume that the caller closes in, out, and err after calling us. */ | |
1100 | ||
1101 | void | |
1102 | prepare_standard_handles (int in, int out, int err, HANDLE handles[3]) | |
6cdfb6e6 | 1103 | { |
c519b5e1 GV |
1104 | HANDLE parent; |
1105 | HANDLE newstdin, newstdout, newstderr; | |
1106 | ||
1107 | parent = GetCurrentProcess (); | |
1108 | ||
1109 | handles[0] = GetStdHandle (STD_INPUT_HANDLE); | |
1110 | handles[1] = GetStdHandle (STD_OUTPUT_HANDLE); | |
1111 | handles[2] = GetStdHandle (STD_ERROR_HANDLE); | |
1112 | ||
1113 | /* make inheritable copies of the new handles */ | |
1114 | if (!DuplicateHandle (parent, | |
1115 | (HANDLE) _get_osfhandle (in), | |
1116 | parent, | |
1117 | &newstdin, | |
1118 | 0, | |
1119 | TRUE, | |
1120 | DUPLICATE_SAME_ACCESS)) | |
1121 | report_file_error ("Duplicating input handle for child", Qnil); | |
6cdfb6e6 | 1122 | |
c519b5e1 GV |
1123 | if (!DuplicateHandle (parent, |
1124 | (HANDLE) _get_osfhandle (out), | |
1125 | parent, | |
1126 | &newstdout, | |
1127 | 0, | |
1128 | TRUE, | |
1129 | DUPLICATE_SAME_ACCESS)) | |
1130 | report_file_error ("Duplicating output handle for child", Qnil); | |
6cdfb6e6 | 1131 | |
c519b5e1 GV |
1132 | if (!DuplicateHandle (parent, |
1133 | (HANDLE) _get_osfhandle (err), | |
1134 | parent, | |
1135 | &newstderr, | |
1136 | 0, | |
1137 | TRUE, | |
1138 | DUPLICATE_SAME_ACCESS)) | |
1139 | report_file_error ("Duplicating error handle for child", Qnil); | |
1140 | ||
1141 | /* and store them as our std handles */ | |
1142 | if (!SetStdHandle (STD_INPUT_HANDLE, newstdin)) | |
1143 | report_file_error ("Changing stdin handle", Qnil); | |
6cdfb6e6 | 1144 | |
c519b5e1 GV |
1145 | if (!SetStdHandle (STD_OUTPUT_HANDLE, newstdout)) |
1146 | report_file_error ("Changing stdout handle", Qnil); | |
1147 | ||
1148 | if (!SetStdHandle (STD_ERROR_HANDLE, newstderr)) | |
1149 | report_file_error ("Changing stderr handle", Qnil); | |
1150 | } | |
1151 | ||
1152 | void | |
1153 | reset_standard_handles (int in, int out, int err, HANDLE handles[3]) | |
1154 | { | |
1155 | /* close the duplicated handles passed to the child */ | |
1156 | CloseHandle (GetStdHandle (STD_INPUT_HANDLE)); | |
1157 | CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE)); | |
1158 | CloseHandle (GetStdHandle (STD_ERROR_HANDLE)); | |
1159 | ||
1160 | /* now restore parent's saved std handles */ | |
1161 | SetStdHandle (STD_INPUT_HANDLE, handles[0]); | |
1162 | SetStdHandle (STD_OUTPUT_HANDLE, handles[1]); | |
1163 | SetStdHandle (STD_ERROR_HANDLE, handles[2]); | |
6cdfb6e6 | 1164 | } |
c519b5e1 | 1165 | |
a11e68d0 RS |
1166 | #ifdef HAVE_SOCKETS |
1167 | ||
1168 | /* To avoid problems with winsock implementations that work over dial-up | |
1169 | connections causing or requiring a connection to exist while Emacs is | |
1170 | running, Emacs no longer automatically loads winsock on startup if it | |
1171 | is present. Instead, it will be loaded when open-network-stream is | |
1172 | first called. | |
1173 | ||
1174 | To allow full control over when winsock is loaded, we provide these | |
1175 | two functions to dynamically load and unload winsock. This allows | |
1176 | dial-up users to only be connected when they actually need to use | |
1177 | socket services. */ | |
1178 | ||
1179 | /* From nt.c */ | |
1180 | extern HANDLE winsock_lib; | |
1181 | extern BOOL term_winsock (void); | |
1182 | extern BOOL init_winsock (int load_now); | |
1183 | ||
1184 | extern Lisp_Object Vsystem_name; | |
1185 | ||
1186 | DEFUN ("win32-has-winsock", Fwin32_has_winsock, Swin32_has_winsock, 0, 1, 0, | |
1187 | "Test for presence of the Windows socket library `winsock'.\n\ | |
1188 | Returns non-nil if winsock support is present, nil otherwise.\n\ | |
1189 | \n\ | |
1190 | If the optional argument LOAD-NOW is non-nil, the winsock library is\n\ | |
1191 | also loaded immediately if not already loaded. If winsock is loaded,\n\ | |
1192 | the winsock local hostname is returned (since this may be different from\n\ | |
1193 | the value of `system-name' and should supplant it), otherwise t is\n\ | |
1194 | returned to indicate winsock support is present.") | |
1195 | (load_now) | |
1196 | Lisp_Object load_now; | |
1197 | { | |
1198 | int have_winsock; | |
1199 | ||
1200 | have_winsock = init_winsock (!NILP (load_now)); | |
1201 | if (have_winsock) | |
1202 | { | |
1203 | if (winsock_lib != NULL) | |
1204 | { | |
1205 | /* Return new value for system-name. The best way to do this | |
1206 | is to call init_system_name, saving and restoring the | |
1207 | original value to avoid side-effects. */ | |
1208 | Lisp_Object orig_hostname = Vsystem_name; | |
1209 | Lisp_Object hostname; | |
1210 | ||
1211 | init_system_name (); | |
1212 | hostname = Vsystem_name; | |
1213 | Vsystem_name = orig_hostname; | |
1214 | return hostname; | |
1215 | } | |
1216 | return Qt; | |
1217 | } | |
1218 | return Qnil; | |
1219 | } | |
1220 | ||
1221 | DEFUN ("win32-unload-winsock", Fwin32_unload_winsock, Swin32_unload_winsock, | |
1222 | 0, 0, 0, | |
1223 | "Unload the Windows socket library `winsock' if loaded.\n\ | |
1224 | This is provided to allow dial-up socket connections to be disconnected\n\ | |
1225 | when no longer needed. Returns nil without unloading winsock if any\n\ | |
1226 | socket connections still exist.") | |
1227 | () | |
1228 | { | |
1229 | return term_winsock () ? Qt : Qnil; | |
1230 | } | |
1231 | ||
1232 | #endif /* HAVE_SOCKETS */ | |
1233 | ||
93fdf2f8 RS |
1234 | \f |
1235 | syms_of_ntproc () | |
1236 | { | |
a11e68d0 RS |
1237 | #ifdef HAVE_SOCKETS |
1238 | defsubr (&Swin32_has_winsock); | |
1239 | defsubr (&Swin32_unload_winsock); | |
1240 | #endif | |
1241 | ||
93fdf2f8 RS |
1242 | DEFVAR_LISP ("win32-quote-process-args", &Vwin32_quote_process_args, |
1243 | "Non-nil enables quoting of process arguments to ensure correct parsing.\n\ | |
1244 | Because Windows does not directly pass argv arrays to child processes,\n\ | |
1245 | programs have to reconstruct the argv array by parsing the command\n\ | |
1246 | line string. For an argument to contain a space, it must be enclosed\n\ | |
1247 | in double quotes or it will be parsed as multiple arguments.\n\ | |
1248 | \n\ | |
1249 | However, the argument list to call-process is not always correctly\n\ | |
1250 | constructed (or arguments have already been quoted), so enabling this\n\ | |
1251 | option may cause unexpected behavior."); | |
1252 | Vwin32_quote_process_args = Qnil; | |
817abdf6 | 1253 | |
0ecf7d36 RS |
1254 | DEFVAR_LISP ("win32-start-process-show-window", |
1255 | &Vwin32_start_process_show_window, | |
1256 | "When nil, processes started via start-process hide their windows.\n\ | |
1257 | When non-nil, they show their window in the method of their choice."); | |
1258 | Vwin32_start_process_show_window = Qnil; | |
1259 | ||
817abdf6 KH |
1260 | DEFVAR_INT ("win32-pipe-read-delay", &Vwin32_pipe_read_delay, |
1261 | "Forced delay before reading subprocess output.\n\ | |
1262 | This is done to improve the buffering of subprocess output, by\n\ | |
1263 | avoiding the inefficiency of frequently reading small amounts of data.\n\ | |
1264 | \n\ | |
1265 | If positive, the value is the number of milliseconds to sleep before\n\ | |
1266 | reading the subprocess output. If negative, the magnitude is the number\n\ | |
1267 | of time slices to wait (effectively boosting the priority of the child\n\ | |
1268 | process temporarily). A value of zero disables waiting entirely."); | |
1269 | Vwin32_pipe_read_delay = 50; | |
0c04091e RS |
1270 | |
1271 | DEFVAR_LISP ("win32-downcase-file-names", &Vwin32_downcase_file_names, | |
1272 | "Non-nil means convert all-upper case file names to lower case.\n\ | |
1273 | This applies when performing completions and file name expansion."); | |
1274 | Vwin32_downcase_file_names = Qnil; | |
93fdf2f8 | 1275 | } |
c519b5e1 | 1276 | /* end of ntproc.c */ |