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 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 later
11 GNU Emacs 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 for
16 You should have received a copy of the GNU General Public License along
17 with GNU Emacs; see the file COPYING. If not, write to the Free Software
18 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 Drew Bliss Oct 14, 1993
21 Adapted from alarm.c by Tim Fleehart
38 /* #define FULL_DEBUG */
40 typedef void (_CALLBACK_
*signal_handler
)(int);
42 /* Defined in process.h which conflicts with the local copy */
45 typedef struct _child_process
57 #define MAX_CHILDREN MAXDESC
61 _DebPrint (char *fmt
, ...)
67 vsprintf (buf
, fmt
, args
);
69 OutputDebugString (buf
);
73 /* Child process management list. */
74 static int child_proc_count
= 0;
75 static child_process child_procs
[MAX_CHILDREN
];
76 static child_process
*dead_child
= NULL
;
78 #define CHILD_ACTIVE(cp) ((cp)->process != NULL)
79 #define DEACTIVATE_CHILD(cp) ((cp)->process = NULL)
81 /* Signal handlers...SIG_DFL == 0 so this is initialized correctly. */
82 static signal_handler sig_handlers
[NSIG
];
84 /* Fake signal implementation to record the SIGCHLD handler. */
86 win32_signal (int sig
, signal_handler handler
)
95 old
= sig_handlers
[sig
];
96 sig_handlers
[sig
] = handler
;
100 /* Find an unused process slot. */
101 static child_process
*
106 if (child_proc_count
== MAX_CHILDREN
)
109 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
110 if (!CHILD_ACTIVE (cp
))
112 return &child_procs
[child_proc_count
++];
115 /* Find a child by pid. */
116 static child_process
*
117 find_child_pid (DWORD pid
)
121 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
122 if (CHILD_ACTIVE (cp
) && pid
== cp
->pid
)
127 /* Find a child by fd. */
128 static child_process
*
129 find_child_fd (int fd
)
133 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
134 if (CHILD_ACTIVE (cp
) && fd
== cp
->fd
)
139 /* Thread proc for child process reader threads
140 The threads just sit in a loop waiting for input
141 When they detect input, they signal the char_avail input to
142 wake up the select emulator
143 When the select emulator processes their input, it pulses
144 char_consumed so that the reader thread goes back to reading. */
146 reader_thread (void *arg
)
151 cp
= (child_process
*)arg
;
153 /* We have to wait for the go-ahead before we can start */
154 if (WaitForSingleObject (cp
->char_consumed
, INFINITE
) != WAIT_OBJECT_0
)
156 /* If something went wrong, quit */
162 /* Use read to get CRLF translation */
163 if (read (cp
->fd
, &cp
->chr
, sizeof (char)) == sizeof (char))
170 DebPrint (("reader_thread.read failed with %lu for fd %ld\n",
171 GetLastError (), cp
->fd
));
176 if (!SetEvent (cp
->char_avail
))
178 DebPrint (("reader_thread.SetEvent failed with %lu for fd %ld\n",
179 GetLastError (), cp
->fd
));
183 /* If the read died, the child has died so let the thread die */
187 /* Wait until our input is acknowledged before reading again */
188 if (WaitForSingleObject (cp
->char_consumed
, INFINITE
) != WAIT_OBJECT_0
)
190 DebPrint (("reader_thread.WaitForSingleObject failed with "
191 "%lu for fd %ld\n", GetLastError (), cp
->fd
));
199 create_child (char *exe
, char *cmdline
, char *env
,
200 PROCESS_INFORMATION
*info
)
205 SECURITY_ATTRIBUTES sec_attrs
;
206 SECURITY_DESCRIPTOR sec_desc
;
214 cp
->char_avail
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
215 if (cp
->char_avail
== NULL
)
218 cp
->char_consumed
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
219 if (cp
->char_consumed
== NULL
)
222 cp
->thrd
= CreateThread (NULL
, 1024, reader_thread
, cp
, 0, &id
);
223 if (cp
->thrd
== NULL
)
224 goto EH_char_consumed
;
226 memset (&start
, 0, sizeof (start
));
227 start
.cb
= sizeof (start
);
229 /* Explicitly specify no security */
230 if (!InitializeSecurityDescriptor (&sec_desc
, SECURITY_DESCRIPTOR_REVISION
))
232 if (!SetSecurityDescriptorDacl (&sec_desc
, TRUE
, NULL
, FALSE
))
234 sec_attrs
.nLength
= sizeof (sec_attrs
);
235 sec_attrs
.lpSecurityDescriptor
= &sec_desc
;
236 sec_attrs
.bInheritHandle
= FALSE
;
238 if (!CreateProcess (exe
, cmdline
, &sec_attrs
, NULL
, TRUE
,
239 CREATE_NEW_PROCESS_GROUP
, env
, NULL
,
242 cp
->process
= info
->hProcess
;
243 cp
->pid
= info
->dwProcessId
;
248 id
= GetLastError ();
251 SetEvent (cp
->char_consumed
);
253 CloseHandle (cp
->char_consumed
);
255 CloseHandle (cp
->char_avail
);
260 /* create_child doesn't know what emacs' file handle will be for waiting
261 on output from the child, so we need to make this additional call
262 to register the handle with the process
263 This way the select emulator knows how to match file handles with
264 entries in child_procs. */
266 register_child (int pid
, int fd
)
270 cp
= find_child_pid (pid
);
273 DebPrint (("register_child unable to find pid %lu\n", pid
));
278 DebPrint (("register_child registered fd %d with pid %lu\n", fd
, pid
));
284 /* Tell the reader thread to start */
285 if (!SetEvent (cp
->char_consumed
))
287 DebPrint (("register_child.SetEvent failed with %lu for fd %ld\n",
288 GetLastError (), cp
->fd
));
292 /* When a process dies its pipe will break so the reader thread will
293 signal failure to the select emulator.
294 The select emulator then calls this routine to clean up.
295 Since the thread signaled failure we can assume it is exiting. */
297 remove_child (child_process
*cp
)
299 /* Reap the thread */
300 if (WaitForSingleObject (cp
->thrd
, INFINITE
) != WAIT_OBJECT_0
)
302 DebPrint (("remove_child.WaitForSingleObject (thread) failed "
303 "with %lu for fd %ld\n", GetLastError (), cp
->fd
));
305 CloseHandle (cp
->thrd
);
306 CloseHandle (cp
->char_consumed
);
307 CloseHandle (cp
->char_avail
);
309 /* Reap the process */
310 if (WaitForSingleObject (cp
->process
, INFINITE
) != WAIT_OBJECT_0
)
312 DebPrint (("remove_child.WaitForSingleObject (process) failed "
313 "with %lu for fd %ld\n", GetLastError (), cp
->fd
));
315 CloseHandle (cp
->process
);
317 DEACTIVATE_CHILD (cp
);
320 /* Wait for any of our existing child processes to die
321 When it does, close its handle
322 Return the pid and fill in the status if non-NULL. */
324 /* From callproc.c */
325 extern int synch_process_alive
;
326 extern int synch_process_retcode
;
329 win32_wait (int *status
)
331 DWORD active
, retval
;
333 child_process
*cp
, *cps
[MAX_CHILDREN
];
334 HANDLE wait_hnd
[MAX_CHILDREN
];
337 if (dead_child
!= NULL
)
339 /* We want to wait for a specific child */
340 wait_hnd
[nh
] = dead_child
->process
;
341 cps
[nh
] = dead_child
;
346 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
347 if (CHILD_ACTIVE (cp
))
349 wait_hnd
[nh
] = cp
->process
;
357 /* Nothing to wait on, so fail */
362 active
= WaitForMultipleObjects (nh
, wait_hnd
, FALSE
, INFINITE
);
363 if (active
== WAIT_FAILED
)
368 else if (active
== WAIT_TIMEOUT
)
370 /* Should never happen */
374 else if (active
>= WAIT_OBJECT_0
&&
375 active
< WAIT_OBJECT_0
+MAXIMUM_WAIT_OBJECTS
)
377 active
-= WAIT_OBJECT_0
;
379 else if (active
>= WAIT_ABANDONED_0
&&
380 active
< WAIT_ABANDONED_0
+MAXIMUM_WAIT_OBJECTS
)
382 active
-= WAIT_ABANDONED_0
;
385 if (!GetExitCodeProcess (wait_hnd
[active
], &retval
))
387 DebPrint (("Wait.GetExitCodeProcess failed with %lu\n",
391 if (retval
== STILL_ACTIVE
)
393 /* Should never happen */
394 DebPrint (("Wait.WaitForMultipleObjects returned an active process\n"));
399 /* Massage the exit code from the process to match the format expected
400 by the WIFSTOPPED et al macros in syswait.h. Only WIFSIGNALLED and
401 WIFEXITED are supported; WIFSTOPPED doesn't make sense under NT. */
403 if (retval
== STATUS_CONTROL_C_EXIT
)
414 else if (synch_process_alive
)
416 synch_process_alive
= 0;
417 synch_process_retcode
= retval
;
419 TerminateThread (cp
->thrd
, 0);
420 CloseHandle (cp
->thrd
);
421 CloseHandle (cp
->char_consumed
);
422 CloseHandle (cp
->char_avail
);
423 CloseHandle (cp
->process
);
424 DEACTIVATE_CHILD (cp
);
430 /* We pass our process ID to our children by setting up an environment
431 variable in their environment. */
432 char ppid_env_var_buffer
[64];
434 /* When a new child process is created we need to register it in our list,
435 so intercept spawn requests. */
437 win32_spawnve (int mode
, char *cmdname
, char **argv
, char **envp
)
439 Lisp_Object program
, full
;
440 char *cmdline
, *env
, *parg
, **targ
;
442 PROCESS_INFORMATION pi
;
444 /* Handle executable names without an executable suffix. */
445 program
= make_string (cmdname
, strlen (cmdname
));
446 if (NILP (Ffile_executable_p (program
)))
452 openp (Vexec_path
, program
, EXEC_SUFFIXES
, &full
, 1);
459 cmdname
= XSTRING (full
)->data
;
463 if (child_proc_count
== MAX_CHILDREN
)
469 /* We don't care about the other modes */
470 if (mode
!= _P_NOWAIT
)
476 /* we have to do some conjuring here to put argv and envp into the
477 form CreateProcess wants... argv needs to be a space separated/null
478 terminated list of parameters, and envp is a null
479 separated/double-null terminated list of parameters.
481 Since I have no idea how large argv and envp are likely to be
482 we figure out list lengths on the fly and allocate them. */
489 arglen
+= strlen (*targ
++) + 1;
491 cmdline
= malloc (arglen
);
501 strcpy (parg
, *targ
);
502 parg
+= strlen (*targ
++);
512 arglen
+= strlen (*targ
++) + 1;
514 sprintf (ppid_env_var_buffer
, "__PARENT_PROCESS_ID=%d",
515 GetCurrentProcessId ());
516 arglen
+= strlen (ppid_env_var_buffer
) + 1;
518 env
= malloc (arglen
);
528 strcpy (parg
, *targ
);
529 parg
+= strlen (*targ
++);
532 strcpy (parg
, ppid_env_var_buffer
);
533 parg
+= strlen (ppid_env_var_buffer
);
537 /* Now create the process. */
538 if (!create_child (cmdname
, cmdline
, env
, &pi
))
544 return pi
.dwProcessId
;
554 /* Emulate the select call
555 Wait for available input on any of the given rfds, or timeout if
556 a timeout is given and no input is detected
557 wfds and efds are not supported and must be NULL. */
560 extern HANDLE keyboard_handle
;
562 extern int proc_buffered_char
[];
565 sys_select (int nfds
, SELECT_TYPE
*rfds
, SELECT_TYPE
*wfds
, SELECT_TYPE
*efds
,
572 child_process
*cp
, *cps
[MAX_CHILDREN
];
573 HANDLE wait_hnd
[MAX_CHILDREN
];
575 /* If the descriptor sets are NULL but timeout isn't, then just Sleep. */
576 if (rfds
== NULL
&& wfds
== NULL
&& efds
== NULL
&& timeout
!= NULL
)
579 Sleep (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000);
581 Sleep ((*timeout
) * 1000);
586 /* Otherwise, we only handle rfds, so fail otherwise. */
587 if (rfds
== NULL
|| wfds
!= NULL
|| efds
!= NULL
)
597 /* Build a list of handles to wait on. */
599 for (i
= 0; i
< nfds
; i
++)
600 if (FD_ISSET (i
, &orfds
))
604 /* Handle stdin specially */
605 wait_hnd
[nh
] = keyboard_handle
;
609 /* Check for any emacs-generated input in the queue since
610 it won't be detected in the wait */
611 if (detect_input_pending ())
619 /* Child process input */
620 cp
= find_child_fd (i
);
624 DebPrint (("select waiting on child %d fd %d\n",
627 wait_hnd
[nh
] = cp
->char_avail
;
633 /* Unable to find something to wait on for this fd, fail */
634 DebPrint (("select unable to find child process "
642 /* Nothing to look for, so we didn't find anything */
647 Sleep (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000);
649 Sleep ((*timeout
) * 1000);
654 /* Check for immediate return without waiting */
660 If a child process dies while this is waiting, its pipe will break
661 so the reader thread will signal an error condition, thus, the wait
665 timeout_ms
= timeout
? (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000) : INFINITE
;
667 timeout_ms
= timeout
? *timeout
*1000 : INFINITE
;
669 active
= WaitForMultipleObjects (nh
, wait_hnd
, FALSE
, timeout_ms
);
670 if (active
== WAIT_FAILED
)
672 DebPrint (("select.WaitForMultipleObjects (%d, %lu) failed with %lu\n",
673 nh
, timeout_ms
, GetLastError ()));
674 /* Is there a better error? */
678 else if (active
== WAIT_TIMEOUT
)
682 else if (active
>= WAIT_OBJECT_0
&&
683 active
< WAIT_OBJECT_0
+MAXIMUM_WAIT_OBJECTS
)
685 active
-= WAIT_OBJECT_0
;
687 else if (active
>= WAIT_ABANDONED_0
&&
688 active
< WAIT_ABANDONED_0
+MAXIMUM_WAIT_OBJECTS
)
690 active
-= WAIT_ABANDONED_0
;
693 if (cps
[active
] == NULL
)
695 /* Keyboard input available */
699 /* This shouldn't be necessary, but apparently just setting the input
700 fd is not good enough for emacs */
701 read_input_waiting ();
708 /* If status is FALSE the read failed so don't report input */
711 FD_SET (cp
->fd
, rfds
);
712 proc_buffered_char
[cp
->fd
] = cp
->chr
;
717 /* The SIGCHLD handler will do a Wait so we know it won't
718 return until the process is dead
719 We force Wait to only wait for this process to avoid it
720 picking up other children that happen to be dead but that
721 we haven't noticed yet
722 SIG_DFL for SIGCHLD is ignore? */
723 if (sig_handlers
[SIGCHLD
] != SIG_DFL
&&
724 sig_handlers
[SIGCHLD
] != SIG_IGN
)
727 DebPrint (("select calling SIGCHLD handler for pid %d\n",
731 sig_handlers
[SIGCHLD
](SIGCHLD
);
735 /* Clean up the child process entry in the table */
743 Substitute for certain kill () operations
746 win32_kill_process (int pid
, int sig
)
750 /* Only handle signals that will result in the process dying */
751 if (sig
!= SIGINT
&& sig
!= SIGKILL
&& sig
!= SIGQUIT
&& sig
!= SIGHUP
)
757 cp
= find_child_pid (pid
);
760 DebPrint (("win32_kill_process didn't find a child with pid %lu\n", pid
));
767 /* Fake Ctrl-Break. */
768 if (!GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, pid
))
770 DebPrint (("win32_kill_process.GenerateConsoleCtrlEvent return %d "
771 "for pid %lu\n", GetLastError (), pid
));
778 /* Kill the process. On Win32 this doesn't kill child processes
779 so it doesn't work very well for shells which is why it's
780 not used in every case. */
781 if (!TerminateProcess (cp
->process
, 0xff))
783 DebPrint (("win32_kill_process.TerminateProcess returned %d "
784 "for pid %lu\n", GetLastError (), pid
));
792 /* If the channel is a pipe this read might block since we don't
793 know how many characters are available, so check and read only
795 We also need to wake up the reader thread once we've read our data. */
797 read_child_output (int fd
, char *buf
, int max
)
804 h
= (HANDLE
)_get_osfhandle (fd
);
805 if (GetFileType (h
) == FILE_TYPE_PIPE
)
807 PeekNamedPipe (h
, NULL
, 0, NULL
, &waiting
, NULL
);
808 to_read
= min (waiting
, (DWORD
)max
);
813 /* Use read to get CRLF translation */
814 nchars
= read (fd
, buf
, to_read
);
816 if (GetFileType (h
) == FILE_TYPE_PIPE
)
818 /* Wake up the reader thread
820 cp
= find_child_fd (fd
);
823 if (!SetEvent (cp
->char_consumed
))
824 DebPrint (("read_child_output.SetEvent failed with "
825 "%lu for fd %ld\n", GetLastError (), fd
));
828 DebPrint (("read_child_output couldn't find a child with fd %d\n",