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