1 /* Process support for Windows NT port of GNU EMACS.
2 Copyright (C) 1992, 1995 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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)
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.
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.
21 Drew Bliss Oct 14, 1993
22 Adapted from alarm.c by Tim Fleehart
32 /* must include CRT headers *before* config.h */
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
;
54 /* Time to sleep before reading from a subprocess output pipe - this
55 avoids the inefficiency of frequently reading small amounts of data.
56 This is primarily necessary for handling DOS processes on Windows 95,
57 but is useful for Win32 processes on both Win95 and NT as well. */
58 Lisp_Object Vwin32_pipe_read_delay
;
60 /* Control conversion of upper case file names to lower case.
61 nil means no, t means yes. */
62 Lisp_Object Vwin32_downcase_file_names
;
64 /* Keep track of whether we have already started a DOS program, and
65 whether we can run them in the first place. */
66 BOOL can_run_dos_process
;
67 BOOL dos_process_running
;
69 #ifndef SYS_SIGLIST_DECLARED
70 extern char *sys_siglist
[];
74 void _DebPrint (const char *fmt
, ...)
80 vsprintf (buf
, fmt
, args
);
82 OutputDebugString (buf
);
86 typedef void (_CALLBACK_
*signal_handler
)(int);
88 /* Signal handlers...SIG_DFL == 0 so this is initialized correctly. */
89 static signal_handler sig_handlers
[NSIG
];
91 /* Fake signal implementation to record the SIGCHLD handler. */
93 sys_signal (int sig
, signal_handler handler
)
102 old
= sig_handlers
[sig
];
103 sig_handlers
[sig
] = handler
;
107 /* Defined in <process.h> which conflicts with the local copy */
110 /* Child process management list. */
111 int child_proc_count
= 0;
112 child_process child_procs
[ MAX_CHILDREN
];
113 child_process
*dead_child
= NULL
;
115 DWORD WINAPI
reader_thread (void *arg
);
117 /* Find an unused process slot. */
124 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
125 if (!CHILD_ACTIVE (cp
))
127 if (child_proc_count
== MAX_CHILDREN
)
129 cp
= &child_procs
[child_proc_count
++];
132 memset (cp
, 0, sizeof(*cp
));
135 cp
->procinfo
.hProcess
= NULL
;
136 cp
->status
= STATUS_READ_ERROR
;
138 /* use manual reset event so that select() will function properly */
139 cp
->char_avail
= CreateEvent (NULL
, TRUE
, FALSE
, NULL
);
142 cp
->char_consumed
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
143 if (cp
->char_consumed
)
145 cp
->thrd
= CreateThread (NULL
, 1024, reader_thread
, cp
, 0, &id
);
155 delete_child (child_process
*cp
)
159 /* Should not be deleting a child that is still needed. */
160 for (i
= 0; i
< MAXDESC
; i
++)
161 if (fd_info
[i
].cp
== cp
)
164 if (!CHILD_ACTIVE (cp
))
167 /* reap thread if necessary */
172 if (GetExitCodeThread (cp
->thrd
, &rc
) && rc
== STILL_ACTIVE
)
174 /* let the thread exit cleanly if possible */
175 cp
->status
= STATUS_READ_ERROR
;
176 SetEvent (cp
->char_consumed
);
177 if (WaitForSingleObject (cp
->thrd
, 1000) != WAIT_OBJECT_0
)
179 DebPrint (("delete_child.WaitForSingleObject (thread) failed "
180 "with %lu for fd %ld\n", GetLastError (), cp
->fd
));
181 TerminateThread (cp
->thrd
, 0);
184 CloseHandle (cp
->thrd
);
189 CloseHandle (cp
->char_avail
);
190 cp
->char_avail
= NULL
;
192 if (cp
->char_consumed
)
194 CloseHandle (cp
->char_consumed
);
195 cp
->char_consumed
= NULL
;
198 /* update child_proc_count (highest numbered slot in use plus one) */
199 if (cp
== child_procs
+ child_proc_count
- 1)
201 for (i
= child_proc_count
-1; i
>= 0; i
--)
202 if (CHILD_ACTIVE (&child_procs
[i
]))
204 child_proc_count
= i
+ 1;
209 child_proc_count
= 0;
212 /* Find a child by pid. */
213 static child_process
*
214 find_child_pid (DWORD pid
)
218 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
219 if (CHILD_ACTIVE (cp
) && pid
== cp
->pid
)
225 /* Thread proc for child process and socket reader threads. Each thread
226 is normally blocked until woken by select() to check for input by
227 reading one char. When the read completes, char_avail is signalled
228 to wake up the select emulator and the thread blocks itself again. */
230 reader_thread (void *arg
)
235 cp
= (child_process
*)arg
;
237 /* We have to wait for the go-ahead before we can start */
239 WaitForSingleObject (cp
->char_consumed
, INFINITE
) != WAIT_OBJECT_0
)
246 rc
= _sys_read_ahead (cp
->fd
);
248 /* The name char_avail is a misnomer - it really just means the
249 read-ahead has completed, whether successfully or not. */
250 if (!SetEvent (cp
->char_avail
))
252 DebPrint (("reader_thread.SetEvent failed with %lu for fd %ld\n",
253 GetLastError (), cp
->fd
));
257 if (rc
== STATUS_READ_ERROR
)
260 /* If the read died, the child has died so let the thread die */
261 if (rc
== STATUS_READ_FAILED
)
264 /* Wait until our input is acknowledged before reading again */
265 if (WaitForSingleObject (cp
->char_consumed
, INFINITE
) != WAIT_OBJECT_0
)
267 DebPrint (("reader_thread.WaitForSingleObject failed with "
268 "%lu for fd %ld\n", GetLastError (), cp
->fd
));
276 create_child (char *exe
, char *cmdline
, char *env
,
277 int * pPid
, child_process
*cp
)
280 SECURITY_ATTRIBUTES sec_attrs
;
281 SECURITY_DESCRIPTOR sec_desc
;
283 if (cp
== NULL
) abort ();
285 memset (&start
, 0, sizeof (start
));
286 start
.cb
= sizeof (start
);
289 start
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
290 start
.wShowWindow
= SW_HIDE
;
292 start
.hStdInput
= GetStdHandle (STD_INPUT_HANDLE
);
293 start
.hStdOutput
= GetStdHandle (STD_OUTPUT_HANDLE
);
294 start
.hStdError
= GetStdHandle (STD_ERROR_HANDLE
);
295 #endif /* HAVE_NTGUI */
297 /* Explicitly specify no security */
298 if (!InitializeSecurityDescriptor (&sec_desc
, SECURITY_DESCRIPTOR_REVISION
))
300 if (!SetSecurityDescriptorDacl (&sec_desc
, TRUE
, NULL
, FALSE
))
302 sec_attrs
.nLength
= sizeof (sec_attrs
);
303 sec_attrs
.lpSecurityDescriptor
= &sec_desc
;
304 sec_attrs
.bInheritHandle
= FALSE
;
306 if (!CreateProcess (exe
, cmdline
, &sec_attrs
, NULL
, TRUE
,
307 CREATE_NEW_PROCESS_GROUP
,
309 &start
, &cp
->procinfo
))
312 cp
->pid
= (int) cp
->procinfo
.dwProcessId
;
314 /* Hack for Windows 95, which assigns large (ie negative) pids */
318 /* pid must fit in a Lisp_Int */
319 cp
->pid
= (cp
->pid
& VALMASK
);
327 DebPrint (("create_child.CreateProcess failed: %ld\n", GetLastError()););
331 /* create_child doesn't know what emacs' file handle will be for waiting
332 on output from the child, so we need to make this additional call
333 to register the handle with the process
334 This way the select emulator knows how to match file handles with
335 entries in child_procs. */
337 register_child (int pid
, int fd
)
341 cp
= find_child_pid (pid
);
344 DebPrint (("register_child unable to find pid %lu\n", pid
));
349 DebPrint (("register_child registered fd %d with pid %lu\n", fd
, pid
));
354 /* thread is initially blocked until select is called; set status so
355 that select will release thread */
356 cp
->status
= STATUS_READ_ACKNOWLEDGED
;
358 /* attach child_process to fd_info */
359 if (fd_info
[fd
].cp
!= NULL
)
361 DebPrint (("register_child: fd_info[%d] apparently in use!\n", fd
));
368 /* When a process dies its pipe will break so the reader thread will
369 signal failure to the select emulator.
370 The select emulator then calls this routine to clean up.
371 Since the thread signaled failure we can assume it is exiting. */
373 reap_subprocess (child_process
*cp
)
375 if (cp
->procinfo
.hProcess
)
377 /* Reap the process */
378 if (WaitForSingleObject (cp
->procinfo
.hProcess
, INFINITE
) != WAIT_OBJECT_0
)
379 DebPrint (("reap_subprocess.WaitForSingleObject (process) failed "
380 "with %lu for fd %ld\n", GetLastError (), cp
->fd
));
381 CloseHandle (cp
->procinfo
.hProcess
);
382 cp
->procinfo
.hProcess
= NULL
;
383 CloseHandle (cp
->procinfo
.hThread
);
384 cp
->procinfo
.hThread
= NULL
;
386 /* If this was a DOS process, indicate that it is now safe to
388 if (cp
->is_dos_process
)
389 dos_process_running
= FALSE
;
392 /* For asynchronous children, the child_proc resources will be freed
393 when the last pipe read descriptor is closed; for synchronous
394 children, we must explicitly free the resources now because
395 register_child has not been called. */
400 /* Wait for any of our existing child processes to die
401 When it does, close its handle
402 Return the pid and fill in the status if non-NULL. */
405 sys_wait (int *status
)
407 DWORD active
, retval
;
410 child_process
*cp
, *cps
[MAX_CHILDREN
];
411 HANDLE wait_hnd
[MAX_CHILDREN
];
414 if (dead_child
!= NULL
)
416 /* We want to wait for a specific child */
417 wait_hnd
[nh
] = dead_child
->procinfo
.hProcess
;
418 cps
[nh
] = dead_child
;
419 if (!wait_hnd
[nh
]) abort ();
424 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
425 /* some child_procs might be sockets; ignore them */
426 if (CHILD_ACTIVE (cp
) && cp
->procinfo
.hProcess
)
428 wait_hnd
[nh
] = cp
->procinfo
.hProcess
;
430 if (!wait_hnd
[nh
]) abort ();
437 /* Nothing to wait on, so fail */
442 active
= WaitForMultipleObjects (nh
, wait_hnd
, FALSE
, INFINITE
);
443 if (active
== WAIT_FAILED
)
448 else if (active
== WAIT_TIMEOUT
)
450 /* Should never happen */
454 else if (active
>= WAIT_OBJECT_0
&&
455 active
< WAIT_OBJECT_0
+MAXIMUM_WAIT_OBJECTS
)
457 active
-= WAIT_OBJECT_0
;
459 else if (active
>= WAIT_ABANDONED_0
&&
460 active
< WAIT_ABANDONED_0
+MAXIMUM_WAIT_OBJECTS
)
462 active
-= WAIT_ABANDONED_0
;
465 if (!GetExitCodeProcess (wait_hnd
[active
], &retval
))
467 DebPrint (("Wait.GetExitCodeProcess failed with %lu\n",
471 if (retval
== STILL_ACTIVE
)
473 /* Should never happen */
474 DebPrint (("Wait.WaitForMultipleObjects returned an active process\n"));
479 /* Massage the exit code from the process to match the format expected
480 by the WIFSTOPPED et al macros in syswait.h. Only WIFSIGNALED and
481 WIFEXITED are supported; WIFSTOPPED doesn't make sense under NT. */
483 if (retval
== STATUS_CONTROL_C_EXIT
)
491 DebPrint (("Wait signaled with process pid %d\n", cp
->pid
));
498 else if (synch_process_alive
)
500 synch_process_alive
= 0;
502 /* Report the status of the synchronous process. */
503 if (WIFEXITED (retval
))
504 synch_process_retcode
= WRETCODE (retval
);
505 else if (WIFSIGNALED (retval
))
507 int code
= WTERMSIG (retval
);
512 /* Suppress warning if the table has const char *. */
513 signame
= (char *) sys_siglist
[code
];
518 synch_process_death
= signame
;
521 reap_subprocess (cp
);
528 win32_is_dos_binary (char * filename
)
530 IMAGE_DOS_HEADER dos_header
;
533 int is_dos_binary
= FALSE
;
535 fd
= open (filename
, O_RDONLY
| O_BINARY
, 0);
538 char * p
= strrchr (filename
, '.');
540 /* We can only identify DOS .com programs from the extension. */
541 if (p
&& stricmp (p
, ".com") == 0)
542 is_dos_binary
= TRUE
;
543 else if (p
&& stricmp (p
, ".bat") == 0)
545 /* A DOS shell script - it appears that CreateProcess is happy
546 to accept this (somewhat surprisingly); presumably it looks
547 at COMSPEC to determine what executable to actually invoke.
548 Therefore, we have to do the same here as well. */
549 p
= getenv ("COMSPEC");
551 is_dos_binary
= win32_is_dos_binary (p
);
555 /* Look for DOS .exe signature - if found, we must also check
556 that it isn't really a 16- or 32-bit Windows exe, since
557 both formats start with a DOS program stub. Note that
558 16-bit Windows executables use the OS/2 1.x format. */
559 if (read (fd
, &dos_header
, sizeof (dos_header
)) == sizeof (dos_header
)
560 && dos_header
.e_magic
== IMAGE_DOS_SIGNATURE
561 && lseek (fd
, dos_header
.e_lfanew
, SEEK_SET
) != -1)
563 if (read (fd
, &signature
, sizeof (signature
)) != sizeof (signature
)
564 || (signature
!= IMAGE_NT_SIGNATURE
&&
565 LOWORD (signature
) != IMAGE_OS2_SIGNATURE
))
566 is_dos_binary
= TRUE
;
572 return is_dos_binary
;
575 /* We pass our process ID to our children by setting up an environment
576 variable in their environment. */
577 char ppid_env_var_buffer
[64];
579 /* When a new child process is created we need to register it in our list,
580 so intercept spawn requests. */
582 sys_spawnve (int mode
, char *cmdname
, char **argv
, char **envp
)
584 Lisp_Object program
, full
;
585 char *cmdline
, *env
, *parg
, **targ
;
591 /* We don't care about the other modes */
592 if (mode
!= _P_NOWAIT
)
598 /* Handle executable names without an executable suffix. */
599 program
= make_string (cmdname
, strlen (cmdname
));
600 if (NILP (Ffile_executable_p (program
)))
606 openp (Vexec_path
, program
, EXEC_SUFFIXES
, &full
, 1);
613 cmdname
= XSTRING (full
)->data
;
617 /* make sure cmdname is in DOS format */
618 strcpy (cmdname
= alloca (strlen (cmdname
) + 1), argv
[0]);
619 unixtodos_filename (cmdname
);
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
&& (!can_run_dos_process
|| dos_process_running
))
627 errno
= (can_run_dos_process
) ? EAGAIN
: EINVAL
;
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.
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.
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. */
661 /* allow for embedded quotes to be doubled - we won't
662 actually double quotes that aren't embedded though */
666 else if (*p
== ' ' || *p
== '\t')
670 arglen
+= strlen (*targ
++) + 1;
672 cmdline
= alloca (arglen
);
683 if (!NILP (Vwin32_quote_process_args
))
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. */
691 if (*p
== ' ' || *p
== '\t' || *p
== '"')
701 last
= p
+ strlen (p
) - 1;
705 if (*p
== '"' && p
> first
&& p
< last
)
706 *parg
++ = '"'; /* double up embedded quotes only */
713 strcpy (parg
, *targ
);
714 parg
+= strlen (*targ
);
726 arglen
+= strlen (*targ
++) + 1;
728 sprintf (ppid_env_var_buffer
, "__PARENT_PROCESS_ID=%d",
729 GetCurrentProcessId ());
730 arglen
+= strlen (ppid_env_var_buffer
) + 1;
732 env
= alloca (arglen
);
737 strcpy (parg
, *targ
);
738 parg
+= strlen (*targ
++);
741 strcpy (parg
, ppid_env_var_buffer
);
742 parg
+= strlen (ppid_env_var_buffer
);
753 /* Now create the process. */
754 if (!create_child (cmdname
, cmdline
, env
, &pid
, cp
))
763 cp
->is_dos_process
= TRUE
;
764 dos_process_running
= TRUE
;
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. */
776 extern HANDLE keyboard_handle
;
778 extern int proc_buffered_char
[];
781 sys_select (int nfds
, SELECT_TYPE
*rfds
, SELECT_TYPE
*wfds
, SELECT_TYPE
*efds
,
789 HANDLE wait_hnd
[MAXDESC
];
790 int fdindex
[MAXDESC
]; /* mapping from wait handles back to descriptors */
792 /* If the descriptor sets are NULL but timeout isn't, then just Sleep. */
793 if (rfds
== NULL
&& wfds
== NULL
&& efds
== NULL
&& timeout
!= NULL
)
795 Sleep (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000);
799 /* Otherwise, we only handle rfds, so fail otherwise. */
800 if (rfds
== NULL
|| wfds
!= NULL
|| efds
!= NULL
)
810 /* Build a list of handles to wait on. */
812 for (i
= 0; i
< nfds
; i
++)
813 if (FD_ISSET (i
, &orfds
))
819 /* Handle stdin specially */
820 wait_hnd
[nh
] = keyboard_handle
;
825 /* Check for any emacs-generated input in the queue since
826 it won't be detected in the wait */
827 if (detect_input_pending ())
835 /* Child process and socket input */
839 int current_status
= cp
->status
;
841 if (current_status
== STATUS_READ_ACKNOWLEDGED
)
843 /* Tell reader thread which file handle to use. */
845 /* Wake up the reader thread for this process */
846 cp
->status
= STATUS_READ_READY
;
847 if (!SetEvent (cp
->char_consumed
))
848 DebPrint (("nt_select.SetEvent failed with "
849 "%lu for fd %ld\n", GetLastError (), i
));
852 #ifdef CHECK_INTERLOCK
853 /* slightly crude cross-checking of interlock between threads */
855 current_status
= cp
->status
;
856 if (WaitForSingleObject (cp
->char_avail
, 0) == WAIT_OBJECT_0
)
858 /* char_avail has been signalled, so status (which may
859 have changed) should indicate read has completed
860 but has not been acknowledged. */
861 current_status
= cp
->status
;
862 if (current_status
!= STATUS_READ_SUCCEEDED
&&
863 current_status
!= STATUS_READ_FAILED
)
864 DebPrint (("char_avail set, but read not completed: status %d\n",
869 /* char_avail has not been signalled, so status should
870 indicate that read is in progress; small possibility
871 that read has completed but event wasn't yet signalled
872 when we tested it (because a context switch occurred
873 or if running on separate CPUs). */
874 if (current_status
!= STATUS_READ_READY
&&
875 current_status
!= STATUS_READ_IN_PROGRESS
&&
876 current_status
!= STATUS_READ_SUCCEEDED
&&
877 current_status
!= STATUS_READ_FAILED
)
878 DebPrint (("char_avail reset, but read status is bad: %d\n",
882 wait_hnd
[nh
] = cp
->char_avail
;
884 if (!wait_hnd
[nh
]) abort ();
887 DebPrint (("select waiting on child %d fd %d\n",
893 /* Unable to find something to wait on for this fd, skip */
894 DebPrint (("sys_select: fd %ld is invalid! ignoring\n", i
));
900 /* Nothing to look for, so we didn't find anything */
904 Sleep (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000);
910 If a child process dies while this is waiting, its pipe will break
911 so the reader thread will signal an error condition, thus, the wait
914 timeout_ms
= timeout
? (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000) : INFINITE
;
916 active
= WaitForMultipleObjects (nh
, wait_hnd
, FALSE
, timeout_ms
);
918 if (active
== WAIT_FAILED
)
920 DebPrint (("select.WaitForMultipleObjects (%d, %lu) failed with %lu\n",
921 nh
, timeout_ms
, GetLastError ()));
922 /* don't return EBADF - this causes wait_reading_process_input to
923 abort; WAIT_FAILED is returned when single-stepping under
924 Windows 95 after switching thread focus in debugger, and
925 possibly at other times. */
929 else if (active
== WAIT_TIMEOUT
)
933 else if (active
>= WAIT_OBJECT_0
&&
934 active
< WAIT_OBJECT_0
+MAXIMUM_WAIT_OBJECTS
)
936 active
-= WAIT_OBJECT_0
;
938 else if (active
>= WAIT_ABANDONED_0
&&
939 active
< WAIT_ABANDONED_0
+MAXIMUM_WAIT_OBJECTS
)
941 active
-= WAIT_ABANDONED_0
;
944 /* Loop over all handles after active (now officially documented as
945 being the first signalled handle in the array). We do this to
946 ensure fairness, so that all channels with data available will be
947 processed - otherwise higher numbered channels could be starved. */
950 if (fdindex
[active
] == 0)
952 /* Keyboard input available */
958 /* must be a socket or pipe */
961 cp
= fd_info
[ fdindex
[active
] ].cp
;
963 /* Read ahead should have completed, either succeeding or failing. */
964 FD_SET (fdindex
[active
], rfds
);
966 current_status
= cp
->status
;
967 if (current_status
!= STATUS_READ_SUCCEEDED
)
969 if (current_status
!= STATUS_READ_FAILED
)
970 DebPrint (("internal error: subprocess pipe signalled "
971 "at the wrong time (status %d)\n!", current_status
));
973 /* The child_process entry for a socket or pipe will be
974 freed when the last descriptor using it is closed; for
975 pipes, we call the SIGCHLD handler. */
976 if (fd_info
[ fdindex
[active
] ].flags
& FILE_PIPE
)
978 /* The SIGCHLD handler will do a Wait so we know it won't
979 return until the process is dead
980 We force Wait to only wait for this process to avoid it
981 picking up other children that happen to be dead but that
982 we haven't noticed yet
983 SIG_DFL for SIGCHLD is ignore? */
984 if (sig_handlers
[SIGCHLD
] != SIG_DFL
&&
985 sig_handlers
[SIGCHLD
] != SIG_IGN
)
988 DebPrint (("select calling SIGCHLD handler for pid %d\n",
992 sig_handlers
[SIGCHLD
] (SIGCHLD
);
996 /* Clean up the child process entry in the table */
997 reap_subprocess (cp
);
1002 /* Test for input on remaining channels. */
1003 while (++active
< nh
)
1004 if (WaitForSingleObject (wait_hnd
[active
], 0) == WAIT_OBJECT_0
)
1006 } while (active
< nh
);
1011 /* Substitute for certain kill () operations */
1013 sys_kill (int pid
, int sig
)
1017 int need_to_free
= 0;
1020 /* Only handle signals that will result in the process dying */
1021 if (sig
!= SIGINT
&& sig
!= SIGKILL
&& sig
!= SIGQUIT
&& sig
!= SIGHUP
)
1027 cp
= find_child_pid (pid
);
1030 proc_hand
= OpenProcess (PROCESS_TERMINATE
, 0, pid
);
1031 if (proc_hand
== NULL
)
1040 proc_hand
= cp
->procinfo
.hProcess
;
1041 pid
= cp
->procinfo
.dwProcessId
;
1046 /* Ctrl-Break is NT equivalent of SIGINT. */
1047 if (!GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, pid
))
1049 DebPrint (("sys_kill.GenerateConsoleCtrlEvent return %d "
1050 "for pid %lu\n", GetLastError (), pid
));
1057 /* Kill the process. On Win32 this doesn't kill child processes
1058 so it doesn't work very well for shells which is why it's not
1059 used in every case. Also, don't try to terminate DOS processes
1060 (on Win95), because this will hang Emacs. */
1061 if (!(cp
&& cp
->is_dos_process
)
1062 && !TerminateProcess (proc_hand
, 0xff))
1064 DebPrint (("sys_kill.TerminateProcess returned %d "
1065 "for pid %lu\n", GetLastError (), pid
));
1072 CloseHandle (proc_hand
);
1077 extern int report_file_error (char *, Lisp_Object
);
1079 /* The following two routines are used to manipulate stdin, stdout, and
1080 stderr of our child processes.
1082 Assuming that in, out, and err are *not* inheritable, we make them
1083 stdin, stdout, and stderr of the child as follows:
1085 - Save the parent's current standard handles.
1086 - Set the std handles to inheritable duplicates of the ones being passed in.
1087 (Note that _get_osfhandle() is an io.h procedure that retrieves the
1088 NT file handle for a crt file descriptor.)
1089 - Spawn the child, which inherits in, out, and err as stdin,
1090 stdout, and stderr. (see Spawnve)
1091 - Close the std handles passed to the child.
1092 - Reset the parent's standard handles to the saved handles.
1093 (see reset_standard_handles)
1094 We assume that the caller closes in, out, and err after calling us. */
1097 prepare_standard_handles (int in
, int out
, int err
, HANDLE handles
[3])
1100 HANDLE newstdin
, newstdout
, newstderr
;
1102 parent
= GetCurrentProcess ();
1104 handles
[0] = GetStdHandle (STD_INPUT_HANDLE
);
1105 handles
[1] = GetStdHandle (STD_OUTPUT_HANDLE
);
1106 handles
[2] = GetStdHandle (STD_ERROR_HANDLE
);
1108 /* make inheritable copies of the new handles */
1109 if (!DuplicateHandle (parent
,
1110 (HANDLE
) _get_osfhandle (in
),
1115 DUPLICATE_SAME_ACCESS
))
1116 report_file_error ("Duplicating input handle for child", Qnil
);
1118 if (!DuplicateHandle (parent
,
1119 (HANDLE
) _get_osfhandle (out
),
1124 DUPLICATE_SAME_ACCESS
))
1125 report_file_error ("Duplicating output handle for child", Qnil
);
1127 if (!DuplicateHandle (parent
,
1128 (HANDLE
) _get_osfhandle (err
),
1133 DUPLICATE_SAME_ACCESS
))
1134 report_file_error ("Duplicating error handle for child", Qnil
);
1136 /* and store them as our std handles */
1137 if (!SetStdHandle (STD_INPUT_HANDLE
, newstdin
))
1138 report_file_error ("Changing stdin handle", Qnil
);
1140 if (!SetStdHandle (STD_OUTPUT_HANDLE
, newstdout
))
1141 report_file_error ("Changing stdout handle", Qnil
);
1143 if (!SetStdHandle (STD_ERROR_HANDLE
, newstderr
))
1144 report_file_error ("Changing stderr handle", Qnil
);
1148 reset_standard_handles (int in
, int out
, int err
, HANDLE handles
[3])
1150 /* close the duplicated handles passed to the child */
1151 CloseHandle (GetStdHandle (STD_INPUT_HANDLE
));
1152 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE
));
1153 CloseHandle (GetStdHandle (STD_ERROR_HANDLE
));
1155 /* now restore parent's saved std handles */
1156 SetStdHandle (STD_INPUT_HANDLE
, handles
[0]);
1157 SetStdHandle (STD_OUTPUT_HANDLE
, handles
[1]);
1158 SetStdHandle (STD_ERROR_HANDLE
, handles
[2]);
1163 /* To avoid problems with winsock implementations that work over dial-up
1164 connections causing or requiring a connection to exist while Emacs is
1165 running, Emacs no longer automatically loads winsock on startup if it
1166 is present. Instead, it will be loaded when open-network-stream is
1169 To allow full control over when winsock is loaded, we provide these
1170 two functions to dynamically load and unload winsock. This allows
1171 dial-up users to only be connected when they actually need to use
1175 extern HANDLE winsock_lib
;
1176 extern BOOL
term_winsock (void);
1177 extern BOOL
init_winsock (int load_now
);
1179 extern Lisp_Object Vsystem_name
;
1181 DEFUN ("win32-has-winsock", Fwin32_has_winsock
, Swin32_has_winsock
, 0, 1, 0,
1182 "Test for presence of the Windows socket library `winsock'.\n\
1183 Returns non-nil if winsock support is present, nil otherwise.\n\
1185 If the optional argument LOAD-NOW is non-nil, the winsock library is\n\
1186 also loaded immediately if not already loaded. If winsock is loaded,\n\
1187 the winsock local hostname is returned (since this may be different from\n\
1188 the value of `system-name' and should supplant it), otherwise t is\n\
1189 returned to indicate winsock support is present.")
1191 Lisp_Object load_now
;
1195 have_winsock
= init_winsock (!NILP (load_now
));
1198 if (winsock_lib
!= NULL
)
1200 /* Return new value for system-name. The best way to do this
1201 is to call init_system_name, saving and restoring the
1202 original value to avoid side-effects. */
1203 Lisp_Object orig_hostname
= Vsystem_name
;
1204 Lisp_Object hostname
;
1206 init_system_name ();
1207 hostname
= Vsystem_name
;
1208 Vsystem_name
= orig_hostname
;
1216 DEFUN ("win32-unload-winsock", Fwin32_unload_winsock
, Swin32_unload_winsock
,
1218 "Unload the Windows socket library `winsock' if loaded.\n\
1219 This is provided to allow dial-up socket connections to be disconnected\n\
1220 when no longer needed. Returns nil without unloading winsock if any\n\
1221 socket connections still exist.")
1224 return term_winsock () ? Qt
: Qnil
;
1227 #endif /* HAVE_SOCKETS */
1233 defsubr (&Swin32_has_winsock
);
1234 defsubr (&Swin32_unload_winsock
);
1237 DEFVAR_LISP ("win32-quote-process-args", &Vwin32_quote_process_args
,
1238 "Non-nil enables quoting of process arguments to ensure correct parsing.\n\
1239 Because Windows does not directly pass argv arrays to child processes,\n\
1240 programs have to reconstruct the argv array by parsing the command\n\
1241 line string. For an argument to contain a space, it must be enclosed\n\
1242 in double quotes or it will be parsed as multiple arguments.\n\
1244 However, the argument list to call-process is not always correctly\n\
1245 constructed (or arguments have already been quoted), so enabling this\n\
1246 option may cause unexpected behavior.");
1247 Vwin32_quote_process_args
= Qnil
;
1249 DEFVAR_INT ("win32-pipe-read-delay", &Vwin32_pipe_read_delay
,
1250 "Forced delay before reading subprocess output.\n\
1251 This is done to improve the buffering of subprocess output, by\n\
1252 avoiding the inefficiency of frequently reading small amounts of data.\n\
1254 If positive, the value is the number of milliseconds to sleep before\n\
1255 reading the subprocess output. If negative, the magnitude is the number\n\
1256 of time slices to wait (effectively boosting the priority of the child\n\
1257 process temporarily). A value of zero disables waiting entirely.");
1258 Vwin32_pipe_read_delay
= 50;
1260 DEFVAR_LISP ("win32-downcase-file-names", &Vwin32_downcase_file_names
,
1261 "Non-nil means convert all-upper case file names to lower case.\n\
1262 This applies when performing completions and file name expansion.");
1263 Vwin32_downcase_file_names
= Qnil
;
1265 /* end of ntproc.c */