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
41 #ifndef SYS_SIGLIST_DECLARED
42 extern char *sys_siglist
[];
45 /* #define FULL_DEBUG */
47 typedef void (_CALLBACK_
*signal_handler
)(int);
49 /* Defined in process.h which conflicts with the local copy */
52 typedef struct _child_process
64 #define MAX_CHILDREN MAXDESC
68 _DebPrint (char *fmt
, ...)
74 vsprintf (buf
, fmt
, args
);
76 OutputDebugString (buf
);
80 /* Child process management list. */
81 static int child_proc_count
= 0;
82 static child_process child_procs
[MAX_CHILDREN
];
83 static child_process
*dead_child
= NULL
;
85 #define CHILD_ACTIVE(cp) ((cp)->process != NULL)
86 #define DEACTIVATE_CHILD(cp) ((cp)->process = NULL)
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 win32_signal (int sig
, signal_handler handler
)
102 old
= sig_handlers
[sig
];
103 sig_handlers
[sig
] = handler
;
107 /* Find an unused process slot. */
108 static child_process
*
113 if (child_proc_count
== MAX_CHILDREN
)
116 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
117 if (!CHILD_ACTIVE (cp
))
119 return &child_procs
[child_proc_count
++];
122 /* Find a child by pid. */
123 static child_process
*
124 find_child_pid (DWORD pid
)
128 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
129 if (CHILD_ACTIVE (cp
) && pid
== cp
->pid
)
134 /* Find a child by fd. */
135 static child_process
*
136 find_child_fd (int fd
)
140 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
141 if (CHILD_ACTIVE (cp
) && fd
== cp
->fd
)
146 /* Thread proc for child process reader threads
147 The threads just sit in a loop waiting for input
148 When they detect input, they signal the char_avail input to
149 wake up the select emulator
150 When the select emulator processes their input, it pulses
151 char_consumed so that the reader thread goes back to reading. */
153 reader_thread (void *arg
)
158 cp
= (child_process
*)arg
;
160 /* We have to wait for the go-ahead before we can start */
161 if (WaitForSingleObject (cp
->char_consumed
, INFINITE
) != WAIT_OBJECT_0
)
163 /* If something went wrong, quit */
169 /* Use read to get CRLF translation */
170 if (read (cp
->fd
, &cp
->chr
, sizeof (char)) == sizeof (char))
177 DebPrint (("reader_thread.read failed with %lu for fd %ld\n",
178 GetLastError (), cp
->fd
));
183 if (!SetEvent (cp
->char_avail
))
185 DebPrint (("reader_thread.SetEvent failed with %lu for fd %ld\n",
186 GetLastError (), cp
->fd
));
190 /* If the read died, the child has died so let the thread die */
194 /* Wait until our input is acknowledged before reading again */
195 if (WaitForSingleObject (cp
->char_consumed
, INFINITE
) != WAIT_OBJECT_0
)
197 DebPrint (("reader_thread.WaitForSingleObject failed with "
198 "%lu for fd %ld\n", GetLastError (), cp
->fd
));
206 create_child (char *exe
, char *cmdline
, char *env
,
207 PROCESS_INFORMATION
*info
)
212 SECURITY_ATTRIBUTES sec_attrs
;
213 SECURITY_DESCRIPTOR sec_desc
;
221 cp
->char_avail
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
222 if (cp
->char_avail
== NULL
)
225 cp
->char_consumed
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
226 if (cp
->char_consumed
== NULL
)
229 cp
->thrd
= CreateThread (NULL
, 1024, reader_thread
, cp
, 0, &id
);
230 if (cp
->thrd
== NULL
)
231 goto EH_char_consumed
;
233 memset (&start
, 0, sizeof (start
));
234 start
.cb
= sizeof (start
);
237 start
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
238 start
.wShowWindow
= SW_HIDE
;
240 start
.hStdInput
= GetStdHandle (STD_INPUT_HANDLE
);
241 start
.hStdOutput
= GetStdHandle (STD_OUTPUT_HANDLE
);
242 start
.hStdError
= GetStdHandle (STD_ERROR_HANDLE
);
243 #endif /* HAVE_NTGUI */
245 /* Explicitly specify no security */
246 if (!InitializeSecurityDescriptor (&sec_desc
, SECURITY_DESCRIPTOR_REVISION
))
248 if (!SetSecurityDescriptorDacl (&sec_desc
, TRUE
, NULL
, FALSE
))
250 sec_attrs
.nLength
= sizeof (sec_attrs
);
251 sec_attrs
.lpSecurityDescriptor
= &sec_desc
;
252 sec_attrs
.bInheritHandle
= FALSE
;
254 if (!CreateProcess (exe
, cmdline
, &sec_attrs
, NULL
, TRUE
,
255 CREATE_NEW_PROCESS_GROUP
, env
, NULL
,
258 cp
->process
= info
->hProcess
;
259 cp
->pid
= info
->dwProcessId
;
264 id
= GetLastError ();
267 SetEvent (cp
->char_consumed
);
269 CloseHandle (cp
->char_consumed
);
271 CloseHandle (cp
->char_avail
);
276 /* create_child doesn't know what emacs' file handle will be for waiting
277 on output from the child, so we need to make this additional call
278 to register the handle with the process
279 This way the select emulator knows how to match file handles with
280 entries in child_procs. */
282 register_child (int pid
, int fd
)
286 cp
= find_child_pid (pid
);
289 DebPrint (("register_child unable to find pid %lu\n", pid
));
294 DebPrint (("register_child registered fd %d with pid %lu\n", fd
, pid
));
300 /* Tell the reader thread to start */
301 if (!SetEvent (cp
->char_consumed
))
303 DebPrint (("register_child.SetEvent failed with %lu for fd %ld\n",
304 GetLastError (), cp
->fd
));
308 /* When a process dies its pipe will break so the reader thread will
309 signal failure to the select emulator.
310 The select emulator then calls this routine to clean up.
311 Since the thread signaled failure we can assume it is exiting. */
313 remove_child (child_process
*cp
)
315 /* Reap the thread */
316 if (WaitForSingleObject (cp
->thrd
, INFINITE
) != WAIT_OBJECT_0
)
318 DebPrint (("remove_child.WaitForSingleObject (thread) failed "
319 "with %lu for fd %ld\n", GetLastError (), cp
->fd
));
321 CloseHandle (cp
->thrd
);
322 CloseHandle (cp
->char_consumed
);
323 CloseHandle (cp
->char_avail
);
325 /* Reap the process */
326 if (WaitForSingleObject (cp
->process
, INFINITE
) != WAIT_OBJECT_0
)
328 DebPrint (("remove_child.WaitForSingleObject (process) failed "
329 "with %lu for fd %ld\n", GetLastError (), cp
->fd
));
331 CloseHandle (cp
->process
);
333 DEACTIVATE_CHILD (cp
);
336 /* Wait for any of our existing child processes to die
337 When it does, close its handle
338 Return the pid and fill in the status if non-NULL. */
341 win32_wait (int *status
)
343 DWORD active
, retval
;
345 child_process
*cp
, *cps
[MAX_CHILDREN
];
346 HANDLE wait_hnd
[MAX_CHILDREN
];
349 if (dead_child
!= NULL
)
351 /* We want to wait for a specific child */
352 wait_hnd
[nh
] = dead_child
->process
;
353 cps
[nh
] = dead_child
;
358 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
359 if (CHILD_ACTIVE (cp
))
361 wait_hnd
[nh
] = cp
->process
;
369 /* Nothing to wait on, so fail */
374 active
= WaitForMultipleObjects (nh
, wait_hnd
, FALSE
, INFINITE
);
375 if (active
== WAIT_FAILED
)
380 else if (active
== WAIT_TIMEOUT
)
382 /* Should never happen */
386 else if (active
>= WAIT_OBJECT_0
&&
387 active
< WAIT_OBJECT_0
+MAXIMUM_WAIT_OBJECTS
)
389 active
-= WAIT_OBJECT_0
;
391 else if (active
>= WAIT_ABANDONED_0
&&
392 active
< WAIT_ABANDONED_0
+MAXIMUM_WAIT_OBJECTS
)
394 active
-= WAIT_ABANDONED_0
;
397 if (!GetExitCodeProcess (wait_hnd
[active
], &retval
))
399 DebPrint (("Wait.GetExitCodeProcess failed with %lu\n",
403 if (retval
== STILL_ACTIVE
)
405 /* Should never happen */
406 DebPrint (("Wait.WaitForMultipleObjects returned an active process\n"));
411 /* Massage the exit code from the process to match the format expected
412 by the WIFSTOPPED et al macros in syswait.h. Only WIFSIGNALED and
413 WIFEXITED are supported; WIFSTOPPED doesn't make sense under NT. */
415 if (retval
== STATUS_CONTROL_C_EXIT
)
426 else if (synch_process_alive
)
428 synch_process_alive
= 0;
430 /* Report the status of the synchronous process. */
431 if (WIFEXITED (retval
))
432 synch_process_retcode
= WRETCODE (retval
);
433 else if (WIFSIGNALED (retval
))
435 int code
= WTERMSIG (retval
);
440 /* Suppress warning if the table has const char *. */
441 signame
= (char *) sys_siglist
[code
];
446 synch_process_death
= signame
;
448 TerminateThread (cp
->thrd
, 0);
449 CloseHandle (cp
->thrd
);
450 CloseHandle (cp
->char_consumed
);
451 CloseHandle (cp
->char_avail
);
452 CloseHandle (cp
->process
);
453 DEACTIVATE_CHILD (cp
);
459 /* We pass our process ID to our children by setting up an environment
460 variable in their environment. */
461 char ppid_env_var_buffer
[64];
463 /* When a new child process is created we need to register it in our list,
464 so intercept spawn requests. */
466 win32_spawnve (int mode
, char *cmdname
, char **argv
, char **envp
)
468 Lisp_Object program
, full
;
469 char *cmdline
, *env
, *parg
, **targ
;
471 PROCESS_INFORMATION pi
;
473 /* Handle executable names without an executable suffix. */
474 program
= make_string (cmdname
, strlen (cmdname
));
475 if (NILP (Ffile_executable_p (program
)))
481 openp (Vexec_path
, program
, EXEC_SUFFIXES
, &full
, 1);
488 cmdname
= XSTRING (full
)->data
;
492 if (child_proc_count
== MAX_CHILDREN
)
498 /* We don't care about the other modes */
499 if (mode
!= _P_NOWAIT
)
505 /* we have to do some conjuring here to put argv and envp into the
506 form CreateProcess wants... argv needs to be a space separated/null
507 terminated list of parameters, and envp is a null
508 separated/double-null terminated list of parameters.
510 Since I have no idea how large argv and envp are likely to be
511 we figure out list lengths on the fly and allocate them. */
518 arglen
+= strlen (*targ
++) + 1;
520 cmdline
= malloc (arglen
);
530 strcpy (parg
, *targ
);
531 parg
+= strlen (*targ
++);
541 arglen
+= strlen (*targ
++) + 1;
543 sprintf (ppid_env_var_buffer
, "__PARENT_PROCESS_ID=%d",
544 GetCurrentProcessId ());
545 arglen
+= strlen (ppid_env_var_buffer
) + 1;
547 env
= malloc (arglen
);
557 strcpy (parg
, *targ
);
558 parg
+= strlen (*targ
++);
561 strcpy (parg
, ppid_env_var_buffer
);
562 parg
+= strlen (ppid_env_var_buffer
);
566 /* Now create the process. */
567 if (!create_child (cmdname
, cmdline
, env
, &pi
))
573 return pi
.dwProcessId
;
583 /* Emulate the select call
584 Wait for available input on any of the given rfds, or timeout if
585 a timeout is given and no input is detected
586 wfds and efds are not supported and must be NULL. */
589 extern HANDLE keyboard_handle
;
591 extern int proc_buffered_char
[];
594 sys_select (int nfds
, SELECT_TYPE
*rfds
, SELECT_TYPE
*wfds
, SELECT_TYPE
*efds
,
601 child_process
*cp
, *cps
[MAX_CHILDREN
+ 1];
602 HANDLE wait_hnd
[MAX_CHILDREN
+ 1];
604 BOOL keyboardwait
= FALSE
;
605 #endif /* HAVE_NTGUI */
607 /* If the descriptor sets are NULL but timeout isn't, then just Sleep. */
608 if (rfds
== NULL
&& wfds
== NULL
&& efds
== NULL
&& timeout
!= NULL
)
611 Sleep (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000);
613 Sleep ((*timeout
) * 1000);
618 /* Otherwise, we only handle rfds, so fail otherwise. */
619 if (rfds
== NULL
|| wfds
!= NULL
|| efds
!= NULL
)
629 /* Build a list of handles to wait on. */
631 for (i
= 0; i
< nfds
; i
++)
632 if (FD_ISSET (i
, &orfds
))
637 keyboardwait
= TRUE
;
639 /* Handle stdin specially */
640 wait_hnd
[nh
] = keyboard_handle
;
643 #endif /* HAVE_NTGUI */
645 /* Check for any emacs-generated input in the queue since
646 it won't be detected in the wait */
647 if (detect_input_pending ())
655 /* Child process input */
656 cp
= find_child_fd (i
);
660 DebPrint (("select waiting on child %d fd %d\n",
663 wait_hnd
[nh
] = cp
->char_avail
;
669 /* Unable to find something to wait on for this fd, fail */
670 DebPrint (("select unable to find child process "
678 /* Never do this in win32 since we will not get paint messages */
681 /* Nothing to look for, so we didn't find anything */
686 Sleep (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000);
688 Sleep ((*timeout
) * 1000);
692 #endif /* !HAVE_NTGUI */
694 /* Check for immediate return without waiting */
700 If a child process dies while this is waiting, its pipe will break
701 so the reader thread will signal an error condition, thus, the wait
705 timeout_ms
= timeout
? (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000) : INFINITE
;
707 timeout_ms
= timeout
? *timeout
*1000 : INFINITE
;
710 active
= MsgWaitForMultipleObjects (nh
, wait_hnd
, FALSE
, timeout_ms
,QS_ALLINPUT
);
712 active
= WaitForMultipleObjects (nh
, wait_hnd
, FALSE
, timeout_ms
);
713 #endif /* HAVE_NTGUI */
714 if (active
== WAIT_FAILED
)
716 DebPrint (("select.WaitForMultipleObjects (%d, %lu) failed with %lu\n",
717 nh
, timeout_ms
, GetLastError ()));
718 /* Is there a better error? */
722 else if (active
== WAIT_TIMEOUT
)
727 else if (active
== WAIT_OBJECT_0
+ nh
)
729 /* Keyboard input available */
732 /* This shouldn't be necessary, but apparently just setting the input
733 fd is not good enough for emacs */
734 // read_input_waiting ();
738 #endif /* HAVE_NTGUI */
739 else if (active
>= WAIT_OBJECT_0
&&
740 active
< WAIT_OBJECT_0
+MAXIMUM_WAIT_OBJECTS
)
742 active
-= WAIT_OBJECT_0
;
744 else if (active
>= WAIT_ABANDONED_0
&&
745 active
< WAIT_ABANDONED_0
+MAXIMUM_WAIT_OBJECTS
)
747 active
-= WAIT_ABANDONED_0
;
750 if (cps
[active
] == NULL
)
752 /* Keyboard input available */
756 /* This shouldn't be necessary, but apparently just setting the input
757 fd is not good enough for emacs */
758 read_input_waiting ();
765 /* If status is FALSE the read failed so don't report input */
768 FD_SET (cp
->fd
, rfds
);
769 proc_buffered_char
[cp
->fd
] = cp
->chr
;
774 /* The SIGCHLD handler will do a Wait so we know it won't
775 return until the process is dead
776 We force Wait to only wait for this process to avoid it
777 picking up other children that happen to be dead but that
778 we haven't noticed yet
779 SIG_DFL for SIGCHLD is ignore? */
780 if (sig_handlers
[SIGCHLD
] != SIG_DFL
&&
781 sig_handlers
[SIGCHLD
] != SIG_IGN
)
784 DebPrint (("select calling SIGCHLD handler for pid %d\n",
788 sig_handlers
[SIGCHLD
](SIGCHLD
);
792 /* Clean up the child process entry in the table */
800 Substitute for certain kill () operations
803 win32_kill_process (int pid
, int sig
)
807 /* Only handle signals that will result in the process dying */
808 if (sig
!= SIGINT
&& sig
!= SIGKILL
&& sig
!= SIGQUIT
&& sig
!= SIGHUP
)
814 cp
= find_child_pid (pid
);
817 DebPrint (("win32_kill_process didn't find a child with pid %lu\n", pid
));
824 /* Fake Ctrl-Break. */
825 if (!GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, pid
))
827 DebPrint (("win32_kill_process.GenerateConsoleCtrlEvent return %d "
828 "for pid %lu\n", GetLastError (), pid
));
835 /* Kill the process. On Win32 this doesn't kill child processes
836 so it doesn't work very well for shells which is why it's
837 not used in every case. */
838 if (!TerminateProcess (cp
->process
, 0xff))
840 DebPrint (("win32_kill_process.TerminateProcess returned %d "
841 "for pid %lu\n", GetLastError (), pid
));
849 /* If the channel is a pipe this read might block since we don't
850 know how many characters are available, so check and read only
852 We also need to wake up the reader thread once we've read our data. */
854 read_child_output (int fd
, char *buf
, int max
)
861 h
= (HANDLE
)_get_osfhandle (fd
);
862 if (GetFileType (h
) == FILE_TYPE_PIPE
)
864 PeekNamedPipe (h
, NULL
, 0, NULL
, &waiting
, NULL
);
865 to_read
= min (waiting
, (DWORD
)max
);
870 /* Use read to get CRLF translation */
871 nchars
= read (fd
, buf
, to_read
);
873 if (GetFileType (h
) == FILE_TYPE_PIPE
)
875 /* Wake up the reader thread
877 cp
= find_child_fd (fd
);
880 if (!SetEvent (cp
->char_consumed
))
881 DebPrint (("read_child_output.SetEvent failed with "
882 "%lu for fd %ld\n", GetLastError (), fd
));
885 DebPrint (("read_child_output couldn't find a child with fd %d\n",