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 /* Control whether create_child causes the process' window to be
55 hidden. The default is nil. */
56 Lisp_Object Vwin32_start_process_show_window
;
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
;
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
;
68 /* Keep track of whether we have already started a DOS program. */
69 BOOL dos_process_running
;
71 #ifndef SYS_SIGLIST_DECLARED
72 extern char *sys_siglist
[];
76 void _DebPrint (const char *fmt
, ...)
82 vsprintf (buf
, fmt
, args
);
84 OutputDebugString (buf
);
88 typedef void (_CALLBACK_
*signal_handler
)(int);
90 /* Signal handlers...SIG_DFL == 0 so this is initialized correctly. */
91 static signal_handler sig_handlers
[NSIG
];
93 /* Fake signal implementation to record the SIGCHLD handler. */
95 sys_signal (int sig
, signal_handler handler
)
104 old
= sig_handlers
[sig
];
105 sig_handlers
[sig
] = handler
;
109 /* Defined in <process.h> which conflicts with the local copy */
112 /* Child process management list. */
113 int child_proc_count
= 0;
114 child_process child_procs
[ MAX_CHILDREN
];
115 child_process
*dead_child
= NULL
;
117 DWORD WINAPI
reader_thread (void *arg
);
119 /* Find an unused process slot. */
126 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
127 if (!CHILD_ACTIVE (cp
))
129 if (child_proc_count
== MAX_CHILDREN
)
131 cp
= &child_procs
[child_proc_count
++];
134 memset (cp
, 0, sizeof(*cp
));
137 cp
->procinfo
.hProcess
= NULL
;
138 cp
->status
= STATUS_READ_ERROR
;
140 /* use manual reset event so that select() will function properly */
141 cp
->char_avail
= CreateEvent (NULL
, TRUE
, FALSE
, NULL
);
144 cp
->char_consumed
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
145 if (cp
->char_consumed
)
147 cp
->thrd
= CreateThread (NULL
, 1024, reader_thread
, cp
, 0, &id
);
157 delete_child (child_process
*cp
)
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
)
166 if (!CHILD_ACTIVE (cp
))
169 /* reap thread if necessary */
174 if (GetExitCodeThread (cp
->thrd
, &rc
) && rc
== STILL_ACTIVE
)
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
)
181 DebPrint (("delete_child.WaitForSingleObject (thread) failed "
182 "with %lu for fd %ld\n", GetLastError (), cp
->fd
));
183 TerminateThread (cp
->thrd
, 0);
186 CloseHandle (cp
->thrd
);
191 CloseHandle (cp
->char_avail
);
192 cp
->char_avail
= NULL
;
194 if (cp
->char_consumed
)
196 CloseHandle (cp
->char_consumed
);
197 cp
->char_consumed
= NULL
;
200 /* update child_proc_count (highest numbered slot in use plus one) */
201 if (cp
== child_procs
+ child_proc_count
- 1)
203 for (i
= child_proc_count
-1; i
>= 0; i
--)
204 if (CHILD_ACTIVE (&child_procs
[i
]))
206 child_proc_count
= i
+ 1;
211 child_proc_count
= 0;
214 /* Find a child by pid. */
215 static child_process
*
216 find_child_pid (DWORD pid
)
220 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
221 if (CHILD_ACTIVE (cp
) && pid
== cp
->pid
)
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. */
232 reader_thread (void *arg
)
237 cp
= (child_process
*)arg
;
239 /* We have to wait for the go-ahead before we can start */
241 WaitForSingleObject (cp
->char_consumed
, INFINITE
) != WAIT_OBJECT_0
)
248 rc
= _sys_read_ahead (cp
->fd
);
250 /* The name char_avail is a misnomer - it really just means the
251 read-ahead has completed, whether successfully or not. */
252 if (!SetEvent (cp
->char_avail
))
254 DebPrint (("reader_thread.SetEvent failed with %lu for fd %ld\n",
255 GetLastError (), cp
->fd
));
259 if (rc
== STATUS_READ_ERROR
)
262 /* If the read died, the child has died so let the thread die */
263 if (rc
== STATUS_READ_FAILED
)
266 /* Wait until our input is acknowledged before reading again */
267 if (WaitForSingleObject (cp
->char_consumed
, INFINITE
) != WAIT_OBJECT_0
)
269 DebPrint (("reader_thread.WaitForSingleObject failed with "
270 "%lu for fd %ld\n", GetLastError (), cp
->fd
));
278 create_child (char *exe
, char *cmdline
, char *env
,
279 int * pPid
, child_process
*cp
)
282 SECURITY_ATTRIBUTES sec_attrs
;
283 SECURITY_DESCRIPTOR sec_desc
;
285 if (cp
== NULL
) abort ();
287 memset (&start
, 0, sizeof (start
));
288 start
.cb
= sizeof (start
);
291 if (NILP (Vwin32_start_process_show_window
))
292 start
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
294 start
.dwFlags
= STARTF_USESTDHANDLES
;
295 start
.wShowWindow
= SW_HIDE
;
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 */
302 /* Explicitly specify no security */
303 if (!InitializeSecurityDescriptor (&sec_desc
, SECURITY_DESCRIPTOR_REVISION
))
305 if (!SetSecurityDescriptorDacl (&sec_desc
, TRUE
, NULL
, FALSE
))
307 sec_attrs
.nLength
= sizeof (sec_attrs
);
308 sec_attrs
.lpSecurityDescriptor
= &sec_desc
;
309 sec_attrs
.bInheritHandle
= FALSE
;
311 if (!CreateProcess (exe
, cmdline
, &sec_attrs
, NULL
, TRUE
,
312 CREATE_NEW_PROCESS_GROUP
,
314 &start
, &cp
->procinfo
))
317 cp
->pid
= (int) cp
->procinfo
.dwProcessId
;
319 /* Hack for Windows 95, which assigns large (ie negative) pids */
323 /* pid must fit in a Lisp_Int */
324 cp
->pid
= (cp
->pid
& VALMASK
);
332 DebPrint (("create_child.CreateProcess failed: %ld\n", GetLastError()););
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. */
342 register_child (int pid
, int fd
)
346 cp
= find_child_pid (pid
);
349 DebPrint (("register_child unable to find pid %lu\n", pid
));
354 DebPrint (("register_child registered fd %d with pid %lu\n", fd
, pid
));
359 /* thread is initially blocked until select is called; set status so
360 that select will release thread */
361 cp
->status
= STATUS_READ_ACKNOWLEDGED
;
363 /* attach child_process to fd_info */
364 if (fd_info
[fd
].cp
!= NULL
)
366 DebPrint (("register_child: fd_info[%d] apparently in use!\n", fd
));
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. */
378 reap_subprocess (child_process
*cp
)
380 if (cp
->procinfo
.hProcess
)
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
;
391 /* If this was a DOS process, indicate that it is now safe to
393 if (cp
->is_dos_process
)
394 dos_process_running
= FALSE
;
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. */
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. */
410 sys_wait (int *status
)
412 DWORD active
, retval
;
415 child_process
*cp
, *cps
[MAX_CHILDREN
];
416 HANDLE wait_hnd
[MAX_CHILDREN
];
419 if (dead_child
!= NULL
)
421 /* We want to wait for a specific child */
422 wait_hnd
[nh
] = dead_child
->procinfo
.hProcess
;
423 cps
[nh
] = dead_child
;
424 if (!wait_hnd
[nh
]) abort ();
429 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
430 /* some child_procs might be sockets; ignore them */
431 if (CHILD_ACTIVE (cp
) && cp
->procinfo
.hProcess
)
433 wait_hnd
[nh
] = cp
->procinfo
.hProcess
;
435 if (!wait_hnd
[nh
]) abort ();
442 /* Nothing to wait on, so fail */
447 active
= WaitForMultipleObjects (nh
, wait_hnd
, FALSE
, INFINITE
);
448 if (active
== WAIT_FAILED
)
453 else if (active
== WAIT_TIMEOUT
)
455 /* Should never happen */
459 else if (active
>= WAIT_OBJECT_0
&&
460 active
< WAIT_OBJECT_0
+MAXIMUM_WAIT_OBJECTS
)
462 active
-= WAIT_OBJECT_0
;
464 else if (active
>= WAIT_ABANDONED_0
&&
465 active
< WAIT_ABANDONED_0
+MAXIMUM_WAIT_OBJECTS
)
467 active
-= WAIT_ABANDONED_0
;
470 if (!GetExitCodeProcess (wait_hnd
[active
], &retval
))
472 DebPrint (("Wait.GetExitCodeProcess failed with %lu\n",
476 if (retval
== STILL_ACTIVE
)
478 /* Should never happen */
479 DebPrint (("Wait.WaitForMultipleObjects returned an active process\n"));
484 /* Massage the exit code from the process to match the format expected
485 by the WIFSTOPPED et al macros in syswait.h. Only WIFSIGNALED and
486 WIFEXITED are supported; WIFSTOPPED doesn't make sense under NT. */
488 if (retval
== STATUS_CONTROL_C_EXIT
)
496 DebPrint (("Wait signaled with process pid %d\n", cp
->pid
));
503 else if (synch_process_alive
)
505 synch_process_alive
= 0;
507 /* Report the status of the synchronous process. */
508 if (WIFEXITED (retval
))
509 synch_process_retcode
= WRETCODE (retval
);
510 else if (WIFSIGNALED (retval
))
512 int code
= WTERMSIG (retval
);
517 /* Suppress warning if the table has const char *. */
518 signame
= (char *) sys_siglist
[code
];
523 synch_process_death
= signame
;
526 reap_subprocess (cp
);
533 win32_is_dos_binary (char * filename
)
535 IMAGE_DOS_HEADER dos_header
;
538 int is_dos_binary
= FALSE
;
540 fd
= open (filename
, O_RDONLY
| O_BINARY
, 0);
543 char * p
= strrchr (filename
, '.');
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)
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");
556 is_dos_binary
= win32_is_dos_binary (p
);
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)
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
;
577 return is_dos_binary
;
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];
584 /* When a new child process is created we need to register it in our list,
585 so intercept spawn requests. */
587 sys_spawnve (int mode
, char *cmdname
, char **argv
, char **envp
)
589 Lisp_Object program
, full
;
590 char *cmdline
, *env
, *parg
, **targ
;
596 /* We don't care about the other modes */
597 if (mode
!= _P_NOWAIT
)
603 /* Handle executable names without an executable suffix. */
604 program
= make_string (cmdname
, strlen (cmdname
));
605 if (NILP (Ffile_executable_p (program
)))
611 openp (Vexec_path
, program
, EXEC_SUFFIXES
, &full
, 1);
618 cmdname
= XSTRING (full
)->data
;
622 /* make sure cmdname is in DOS format */
623 strcpy (cmdname
= alloca (strlen (cmdname
) + 1), argv
[0]);
624 unixtodos_filename (cmdname
);
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
);
630 if (is_dos_binary
&& dos_process_running
)
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.
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.
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. */
666 /* allow for embedded quotes to be doubled - we won't
667 actually double quotes that aren't embedded though */
671 else if (*p
== ' ' || *p
== '\t')
675 arglen
+= strlen (*targ
++) + 1;
677 cmdline
= alloca (arglen
);
688 if (!NILP (Vwin32_quote_process_args
))
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. */
696 if (*p
== ' ' || *p
== '\t' || *p
== '"')
706 last
= p
+ strlen (p
) - 1;
710 if (*p
== '"' && p
> first
&& p
< last
)
711 *parg
++ = '"'; /* double up embedded quotes only */
718 strcpy (parg
, *targ
);
719 parg
+= strlen (*targ
);
731 arglen
+= strlen (*targ
++) + 1;
733 sprintf (ppid_env_var_buffer
, "__PARENT_PROCESS_ID=%d",
734 GetCurrentProcessId ());
735 arglen
+= strlen (ppid_env_var_buffer
) + 1;
737 env
= alloca (arglen
);
742 strcpy (parg
, *targ
);
743 parg
+= strlen (*targ
++);
746 strcpy (parg
, ppid_env_var_buffer
);
747 parg
+= strlen (ppid_env_var_buffer
);
758 /* Now create the process. */
759 if (!create_child (cmdname
, cmdline
, env
, &pid
, cp
))
768 cp
->is_dos_process
= TRUE
;
769 dos_process_running
= TRUE
;
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. */
781 extern HANDLE keyboard_handle
;
783 extern int proc_buffered_char
[];
786 sys_select (int nfds
, SELECT_TYPE
*rfds
, SELECT_TYPE
*wfds
, SELECT_TYPE
*efds
,
794 HANDLE wait_hnd
[MAXDESC
];
795 int fdindex
[MAXDESC
]; /* mapping from wait handles back to descriptors */
797 /* If the descriptor sets are NULL but timeout isn't, then just Sleep. */
798 if (rfds
== NULL
&& wfds
== NULL
&& efds
== NULL
&& timeout
!= NULL
)
800 Sleep (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000);
804 /* Otherwise, we only handle rfds, so fail otherwise. */
805 if (rfds
== NULL
|| wfds
!= NULL
|| efds
!= NULL
)
815 /* Build a list of handles to wait on. */
817 for (i
= 0; i
< nfds
; i
++)
818 if (FD_ISSET (i
, &orfds
))
824 /* Handle stdin specially */
825 wait_hnd
[nh
] = keyboard_handle
;
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 ())
840 /* Child process and socket input */
844 int current_status
= cp
->status
;
846 if (current_status
== STATUS_READ_ACKNOWLEDGED
)
848 /* Tell reader thread which file handle to use. */
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
));
857 #ifdef CHECK_INTERLOCK
858 /* slightly crude cross-checking of interlock between threads */
860 current_status
= cp
->status
;
861 if (WaitForSingleObject (cp
->char_avail
, 0) == WAIT_OBJECT_0
)
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",
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",
887 wait_hnd
[nh
] = cp
->char_avail
;
889 if (!wait_hnd
[nh
]) abort ();
892 DebPrint (("select waiting on child %d fd %d\n",
898 /* Unable to find something to wait on for this fd, skip */
899 DebPrint (("sys_select: fd %ld is invalid! ignoring\n", i
));
905 /* Nothing to look for, so we didn't find anything */
909 Sleep (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000);
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
919 timeout_ms
= timeout
? (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000) : INFINITE
;
921 active
= WaitForMultipleObjects (nh
, wait_hnd
, FALSE
, timeout_ms
);
923 if (active
== WAIT_FAILED
)
925 DebPrint (("select.WaitForMultipleObjects (%d, %lu) failed with %lu\n",
926 nh
, timeout_ms
, GetLastError ()));
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. */
934 else if (active
== WAIT_TIMEOUT
)
938 else if (active
>= WAIT_OBJECT_0
&&
939 active
< WAIT_OBJECT_0
+MAXIMUM_WAIT_OBJECTS
)
941 active
-= WAIT_OBJECT_0
;
943 else if (active
>= WAIT_ABANDONED_0
&&
944 active
< WAIT_ABANDONED_0
+MAXIMUM_WAIT_OBJECTS
)
946 active
-= WAIT_ABANDONED_0
;
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. */
955 if (fdindex
[active
] == 0)
957 /* Keyboard input available */
963 /* must be a socket or pipe */
966 cp
= fd_info
[ fdindex
[active
] ].cp
;
968 /* Read ahead should have completed, either succeeding or failing. */
969 FD_SET (fdindex
[active
], rfds
);
971 current_status
= cp
->status
;
972 if (current_status
!= STATUS_READ_SUCCEEDED
)
974 if (current_status
!= STATUS_READ_FAILED
)
975 DebPrint (("internal error: subprocess pipe signalled "
976 "at the wrong time (status %d)\n!", current_status
));
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
)
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
)
993 DebPrint (("select calling SIGCHLD handler for pid %d\n",
997 sig_handlers
[SIGCHLD
] (SIGCHLD
);
1001 /* Clean up the child process entry in the table */
1002 reap_subprocess (cp
);
1007 /* Test for input on remaining channels. */
1008 while (++active
< nh
)
1009 if (WaitForSingleObject (wait_hnd
[active
], 0) == WAIT_OBJECT_0
)
1011 } while (active
< nh
);
1016 /* Substitute for certain kill () operations */
1018 sys_kill (int pid
, int sig
)
1022 int need_to_free
= 0;
1025 /* Only handle signals that will result in the process dying */
1026 if (sig
!= SIGINT
&& sig
!= SIGKILL
&& sig
!= SIGQUIT
&& sig
!= SIGHUP
)
1032 cp
= find_child_pid (pid
);
1035 proc_hand
= OpenProcess (PROCESS_TERMINATE
, 0, pid
);
1036 if (proc_hand
== NULL
)
1045 proc_hand
= cp
->procinfo
.hProcess
;
1046 pid
= cp
->procinfo
.dwProcessId
;
1051 /* Ctrl-Break is NT equivalent of SIGINT. */
1052 if (!GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, pid
))
1054 DebPrint (("sys_kill.GenerateConsoleCtrlEvent return %d "
1055 "for pid %lu\n", GetLastError (), pid
));
1062 /* Kill the process. On Win32 this doesn't kill child processes
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))
1069 DebPrint (("sys_kill.TerminateProcess returned %d "
1070 "for pid %lu\n", GetLastError (), pid
));
1077 CloseHandle (proc_hand
);
1082 extern int report_file_error (char *, Lisp_Object
);
1084 /* The following two routines are used to manipulate stdin, stdout, and
1085 stderr of our child processes.
1087 Assuming that in, out, and err are *not* inheritable, we make them
1088 stdin, stdout, and stderr of the child as follows:
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. */
1102 prepare_standard_handles (int in
, int out
, int err
, HANDLE handles
[3])
1105 HANDLE newstdin
, newstdout
, newstderr
;
1107 parent
= GetCurrentProcess ();
1109 handles
[0] = GetStdHandle (STD_INPUT_HANDLE
);
1110 handles
[1] = GetStdHandle (STD_OUTPUT_HANDLE
);
1111 handles
[2] = GetStdHandle (STD_ERROR_HANDLE
);
1113 /* make inheritable copies of the new handles */
1114 if (!DuplicateHandle (parent
,
1115 (HANDLE
) _get_osfhandle (in
),
1120 DUPLICATE_SAME_ACCESS
))
1121 report_file_error ("Duplicating input handle for child", Qnil
);
1123 if (!DuplicateHandle (parent
,
1124 (HANDLE
) _get_osfhandle (out
),
1129 DUPLICATE_SAME_ACCESS
))
1130 report_file_error ("Duplicating output handle for child", Qnil
);
1132 if (!DuplicateHandle (parent
,
1133 (HANDLE
) _get_osfhandle (err
),
1138 DUPLICATE_SAME_ACCESS
))
1139 report_file_error ("Duplicating error handle for child", Qnil
);
1141 /* and store them as our std handles */
1142 if (!SetStdHandle (STD_INPUT_HANDLE
, newstdin
))
1143 report_file_error ("Changing stdin handle", Qnil
);
1145 if (!SetStdHandle (STD_OUTPUT_HANDLE
, newstdout
))
1146 report_file_error ("Changing stdout handle", Qnil
);
1148 if (!SetStdHandle (STD_ERROR_HANDLE
, newstderr
))
1149 report_file_error ("Changing stderr handle", Qnil
);
1153 reset_standard_handles (int in
, int out
, int err
, HANDLE handles
[3])
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
));
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]);
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
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
1180 extern HANDLE winsock_lib
;
1181 extern BOOL
term_winsock (void);
1182 extern BOOL
init_winsock (int load_now
);
1184 extern Lisp_Object Vsystem_name
;
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\
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.")
1196 Lisp_Object load_now
;
1200 have_winsock
= init_winsock (!NILP (load_now
));
1203 if (winsock_lib
!= NULL
)
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
;
1211 init_system_name ();
1212 hostname
= Vsystem_name
;
1213 Vsystem_name
= orig_hostname
;
1221 DEFUN ("win32-unload-winsock", Fwin32_unload_winsock
, Swin32_unload_winsock
,
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.")
1229 return term_winsock () ? Qt
: Qnil
;
1232 #endif /* HAVE_SOCKETS */
1238 defsubr (&Swin32_has_winsock
);
1239 defsubr (&Swin32_unload_winsock
);
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\
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
;
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
;
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\
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;
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
;
1276 /* end of ntproc.c */