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
40 #ifndef SYS_SIGLIST_DECLARED
41 extern char *sys_siglist
[];
44 /* #define FULL_DEBUG */
46 typedef void (_CALLBACK_
*signal_handler
)(int);
48 /* Defined in process.h which conflicts with the local copy */
51 typedef struct _child_process
63 #define MAX_CHILDREN MAXDESC
67 _DebPrint (char *fmt
, ...)
73 vsprintf (buf
, fmt
, args
);
75 OutputDebugString (buf
);
79 /* Child process management list. */
80 static int child_proc_count
= 0;
81 static child_process child_procs
[MAX_CHILDREN
];
82 static child_process
*dead_child
= NULL
;
84 #define CHILD_ACTIVE(cp) ((cp)->process != NULL)
85 #define DEACTIVATE_CHILD(cp) ((cp)->process = NULL)
87 /* Signal handlers...SIG_DFL == 0 so this is initialized correctly. */
88 static signal_handler sig_handlers
[NSIG
];
90 /* Fake signal implementation to record the SIGCHLD handler. */
92 win32_signal (int sig
, signal_handler handler
)
101 old
= sig_handlers
[sig
];
102 sig_handlers
[sig
] = handler
;
106 /* Find an unused process slot. */
107 static child_process
*
112 if (child_proc_count
== MAX_CHILDREN
)
115 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
116 if (!CHILD_ACTIVE (cp
))
118 return &child_procs
[child_proc_count
++];
121 /* Find a child by pid. */
122 static child_process
*
123 find_child_pid (DWORD pid
)
127 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
128 if (CHILD_ACTIVE (cp
) && pid
== cp
->pid
)
133 /* Find a child by fd. */
134 static child_process
*
135 find_child_fd (int fd
)
139 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
140 if (CHILD_ACTIVE (cp
) && fd
== cp
->fd
)
145 /* Thread proc for child process reader threads
146 The threads just sit in a loop waiting for input
147 When they detect input, they signal the char_avail input to
148 wake up the select emulator
149 When the select emulator processes their input, it pulses
150 char_consumed so that the reader thread goes back to reading. */
152 reader_thread (void *arg
)
157 cp
= (child_process
*)arg
;
159 /* We have to wait for the go-ahead before we can start */
160 if (WaitForSingleObject (cp
->char_consumed
, INFINITE
) != WAIT_OBJECT_0
)
162 /* If something went wrong, quit */
168 /* Use read to get CRLF translation */
169 if (read (cp
->fd
, &cp
->chr
, sizeof (char)) == sizeof (char))
176 DebPrint (("reader_thread.read failed with %lu for fd %ld\n",
177 GetLastError (), cp
->fd
));
182 if (!SetEvent (cp
->char_avail
))
184 DebPrint (("reader_thread.SetEvent failed with %lu for fd %ld\n",
185 GetLastError (), cp
->fd
));
189 /* If the read died, the child has died so let the thread die */
193 /* Wait until our input is acknowledged before reading again */
194 if (WaitForSingleObject (cp
->char_consumed
, INFINITE
) != WAIT_OBJECT_0
)
196 DebPrint (("reader_thread.WaitForSingleObject failed with "
197 "%lu for fd %ld\n", GetLastError (), cp
->fd
));
205 create_child (char *exe
, char *cmdline
, char *env
,
206 PROCESS_INFORMATION
*info
)
211 SECURITY_ATTRIBUTES sec_attrs
;
212 SECURITY_DESCRIPTOR sec_desc
;
220 cp
->char_avail
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
221 if (cp
->char_avail
== NULL
)
224 cp
->char_consumed
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
225 if (cp
->char_consumed
== NULL
)
228 cp
->thrd
= CreateThread (NULL
, 1024, reader_thread
, cp
, 0, &id
);
229 if (cp
->thrd
== NULL
)
230 goto EH_char_consumed
;
232 memset (&start
, 0, sizeof (start
));
233 start
.cb
= sizeof (start
);
236 start
.dwFlags
= STARTF_USESTDHANDLES
| STARTF_USESHOWWINDOW
;
237 start
.wShowWindow
= SW_HIDE
;
239 start
.hStdInput
= GetStdHandle (STD_INPUT_HANDLE
);
240 start
.hStdOutput
= GetStdHandle (STD_OUTPUT_HANDLE
);
241 start
.hStdError
= GetStdHandle (STD_ERROR_HANDLE
);
242 #endif /* HAVE_NTGUI */
244 /* Explicitly specify no security */
245 if (!InitializeSecurityDescriptor (&sec_desc
, SECURITY_DESCRIPTOR_REVISION
))
247 if (!SetSecurityDescriptorDacl (&sec_desc
, TRUE
, NULL
, FALSE
))
249 sec_attrs
.nLength
= sizeof (sec_attrs
);
250 sec_attrs
.lpSecurityDescriptor
= &sec_desc
;
251 sec_attrs
.bInheritHandle
= FALSE
;
253 if (!CreateProcess (exe
, cmdline
, &sec_attrs
, NULL
, TRUE
,
254 CREATE_NEW_PROCESS_GROUP
, env
, NULL
,
257 cp
->process
= info
->hProcess
;
258 cp
->pid
= info
->dwProcessId
;
263 id
= GetLastError ();
266 SetEvent (cp
->char_consumed
);
268 CloseHandle (cp
->char_consumed
);
270 CloseHandle (cp
->char_avail
);
275 /* create_child doesn't know what emacs' file handle will be for waiting
276 on output from the child, so we need to make this additional call
277 to register the handle with the process
278 This way the select emulator knows how to match file handles with
279 entries in child_procs. */
281 register_child (int pid
, int fd
)
285 cp
= find_child_pid (pid
);
288 DebPrint (("register_child unable to find pid %lu\n", pid
));
293 DebPrint (("register_child registered fd %d with pid %lu\n", fd
, pid
));
299 /* Tell the reader thread to start */
300 if (!SetEvent (cp
->char_consumed
))
302 DebPrint (("register_child.SetEvent failed with %lu for fd %ld\n",
303 GetLastError (), cp
->fd
));
307 /* When a process dies its pipe will break so the reader thread will
308 signal failure to the select emulator.
309 The select emulator then calls this routine to clean up.
310 Since the thread signaled failure we can assume it is exiting. */
312 remove_child (child_process
*cp
)
314 /* Reap the thread */
315 if (WaitForSingleObject (cp
->thrd
, INFINITE
) != WAIT_OBJECT_0
)
317 DebPrint (("remove_child.WaitForSingleObject (thread) failed "
318 "with %lu for fd %ld\n", GetLastError (), cp
->fd
));
320 CloseHandle (cp
->thrd
);
321 CloseHandle (cp
->char_consumed
);
322 CloseHandle (cp
->char_avail
);
324 /* Reap the process */
325 if (WaitForSingleObject (cp
->process
, INFINITE
) != WAIT_OBJECT_0
)
327 DebPrint (("remove_child.WaitForSingleObject (process) failed "
328 "with %lu for fd %ld\n", GetLastError (), cp
->fd
));
330 CloseHandle (cp
->process
);
332 DEACTIVATE_CHILD (cp
);
335 /* Wait for any of our existing child processes to die
336 When it does, close its handle
337 Return the pid and fill in the status if non-NULL. */
340 win32_wait (int *status
)
342 DWORD active
, retval
;
344 child_process
*cp
, *cps
[MAX_CHILDREN
];
345 HANDLE wait_hnd
[MAX_CHILDREN
];
348 if (dead_child
!= NULL
)
350 /* We want to wait for a specific child */
351 wait_hnd
[nh
] = dead_child
->process
;
352 cps
[nh
] = dead_child
;
357 for (cp
= child_procs
+(child_proc_count
-1); cp
>= child_procs
; cp
--)
358 if (CHILD_ACTIVE (cp
))
360 wait_hnd
[nh
] = cp
->process
;
368 /* Nothing to wait on, so fail */
373 active
= WaitForMultipleObjects (nh
, wait_hnd
, FALSE
, INFINITE
);
374 if (active
== WAIT_FAILED
)
379 else if (active
== WAIT_TIMEOUT
)
381 /* Should never happen */
385 else if (active
>= WAIT_OBJECT_0
&&
386 active
< WAIT_OBJECT_0
+MAXIMUM_WAIT_OBJECTS
)
388 active
-= WAIT_OBJECT_0
;
390 else if (active
>= WAIT_ABANDONED_0
&&
391 active
< WAIT_ABANDONED_0
+MAXIMUM_WAIT_OBJECTS
)
393 active
-= WAIT_ABANDONED_0
;
396 if (!GetExitCodeProcess (wait_hnd
[active
], &retval
))
398 DebPrint (("Wait.GetExitCodeProcess failed with %lu\n",
402 if (retval
== STILL_ACTIVE
)
404 /* Should never happen */
405 DebPrint (("Wait.WaitForMultipleObjects returned an active process\n"));
410 /* Massage the exit code from the process to match the format expected
411 by the WIFSTOPPED et al macros in syswait.h. Only WIFSIGNALED and
412 WIFEXITED are supported; WIFSTOPPED doesn't make sense under NT. */
414 if (retval
== STATUS_CONTROL_C_EXIT
)
425 else if (synch_process_alive
)
427 synch_process_alive
= 0;
429 /* Report the status of the synchronous process. */
430 if (WIFEXITED (retval
))
431 synch_process_retcode
= WRETCODE (retval
);
432 else if (WIFSIGNALED (retval
))
434 int code
= WTERMSIG (retval
);
439 /* Suppress warning if the table has const char *. */
440 signame
= (char *) sys_siglist
[code
];
445 synch_process_death
= signame
;
447 TerminateThread (cp
->thrd
, 0);
448 CloseHandle (cp
->thrd
);
449 CloseHandle (cp
->char_consumed
);
450 CloseHandle (cp
->char_avail
);
451 CloseHandle (cp
->process
);
452 DEACTIVATE_CHILD (cp
);
458 /* We pass our process ID to our children by setting up an environment
459 variable in their environment. */
460 char ppid_env_var_buffer
[64];
462 /* When a new child process is created we need to register it in our list,
463 so intercept spawn requests. */
465 win32_spawnve (int mode
, char *cmdname
, char **argv
, char **envp
)
467 Lisp_Object program
, full
;
468 char *cmdline
, *env
, *parg
, **targ
;
470 PROCESS_INFORMATION pi
;
472 /* Handle executable names without an executable suffix. */
473 program
= make_string (cmdname
, strlen (cmdname
));
474 if (NILP (Ffile_executable_p (program
)))
480 openp (Vexec_path
, program
, EXEC_SUFFIXES
, &full
, 1);
487 cmdname
= XSTRING (full
)->data
;
491 if (child_proc_count
== MAX_CHILDREN
)
497 /* We don't care about the other modes */
498 if (mode
!= _P_NOWAIT
)
504 /* we have to do some conjuring here to put argv and envp into the
505 form CreateProcess wants... argv needs to be a space separated/null
506 terminated list of parameters, and envp is a null
507 separated/double-null terminated list of parameters.
509 Since I have no idea how large argv and envp are likely to be
510 we figure out list lengths on the fly and allocate them. */
517 arglen
+= strlen (*targ
++) + 1;
519 cmdline
= malloc (arglen
);
529 strcpy (parg
, *targ
);
530 parg
+= strlen (*targ
++);
540 arglen
+= strlen (*targ
++) + 1;
542 sprintf (ppid_env_var_buffer
, "__PARENT_PROCESS_ID=%d",
543 GetCurrentProcessId ());
544 arglen
+= strlen (ppid_env_var_buffer
) + 1;
546 env
= malloc (arglen
);
556 strcpy (parg
, *targ
);
557 parg
+= strlen (*targ
++);
560 strcpy (parg
, ppid_env_var_buffer
);
561 parg
+= strlen (ppid_env_var_buffer
);
565 /* Now create the process. */
566 if (!create_child (cmdname
, cmdline
, env
, &pi
))
572 return pi
.dwProcessId
;
582 /* Emulate the select call
583 Wait for available input on any of the given rfds, or timeout if
584 a timeout is given and no input is detected
585 wfds and efds are not supported and must be NULL. */
588 extern HANDLE keyboard_handle
;
590 extern int proc_buffered_char
[];
593 sys_select (int nfds
, SELECT_TYPE
*rfds
, SELECT_TYPE
*wfds
, SELECT_TYPE
*efds
,
600 child_process
*cp
, *cps
[MAX_CHILDREN
+ 1];
601 HANDLE wait_hnd
[MAX_CHILDREN
+ 1];
603 BOOL keyboardwait
= FALSE
;
604 #endif /* HAVE_NTGUI */
606 /* If the descriptor sets are NULL but timeout isn't, then just Sleep. */
607 if (rfds
== NULL
&& wfds
== NULL
&& efds
== NULL
&& timeout
!= NULL
)
610 Sleep (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000);
612 Sleep ((*timeout
) * 1000);
617 /* Otherwise, we only handle rfds, so fail otherwise. */
618 if (rfds
== NULL
|| wfds
!= NULL
|| efds
!= NULL
)
628 /* Build a list of handles to wait on. */
630 for (i
= 0; i
< nfds
; i
++)
631 if (FD_ISSET (i
, &orfds
))
636 keyboardwait
= TRUE
;
638 /* Handle stdin specially */
639 wait_hnd
[nh
] = keyboard_handle
;
642 #endif /* HAVE_NTGUI */
644 /* Check for any emacs-generated input in the queue since
645 it won't be detected in the wait */
646 if (detect_input_pending ())
654 /* Child process input */
655 cp
= find_child_fd (i
);
659 DebPrint (("select waiting on child %d fd %d\n",
662 wait_hnd
[nh
] = cp
->char_avail
;
668 /* Unable to find something to wait on for this fd, fail */
669 DebPrint (("select unable to find child process "
677 /* Never do this in win32 since we will not get paint messages */
680 /* Nothing to look for, so we didn't find anything */
685 Sleep (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000);
687 Sleep ((*timeout
) * 1000);
691 #endif /* !HAVE_NTGUI */
693 /* Check for immediate return without waiting */
699 If a child process dies while this is waiting, its pipe will break
700 so the reader thread will signal an error condition, thus, the wait
704 timeout_ms
= timeout
? (timeout
->tv_sec
* 1000 + timeout
->tv_usec
/ 1000) : INFINITE
;
706 timeout_ms
= timeout
? *timeout
*1000 : INFINITE
;
709 active
= MsgWaitForMultipleObjects (nh
, wait_hnd
, FALSE
, timeout_ms
,QS_ALLINPUT
);
711 active
= WaitForMultipleObjects (nh
, wait_hnd
, FALSE
, timeout_ms
);
712 #endif /* HAVE_NTGUI */
713 if (active
== WAIT_FAILED
)
715 DebPrint (("select.WaitForMultipleObjects (%d, %lu) failed with %lu\n",
716 nh
, timeout_ms
, GetLastError ()));
717 /* Is there a better error? */
721 else if (active
== WAIT_TIMEOUT
)
726 else if (active
== WAIT_OBJECT_0
+ nh
)
728 /* Keyboard input available */
731 /* This shouldn't be necessary, but apparently just setting the input
732 fd is not good enough for emacs */
733 // read_input_waiting ();
737 #endif /* HAVE_NTGUI */
738 else if (active
>= WAIT_OBJECT_0
&&
739 active
< WAIT_OBJECT_0
+MAXIMUM_WAIT_OBJECTS
)
741 active
-= WAIT_OBJECT_0
;
743 else if (active
>= WAIT_ABANDONED_0
&&
744 active
< WAIT_ABANDONED_0
+MAXIMUM_WAIT_OBJECTS
)
746 active
-= WAIT_ABANDONED_0
;
749 if (cps
[active
] == NULL
)
751 /* Keyboard input available */
755 /* This shouldn't be necessary, but apparently just setting the input
756 fd is not good enough for emacs */
757 read_input_waiting ();
764 /* If status is FALSE the read failed so don't report input */
767 FD_SET (cp
->fd
, rfds
);
768 proc_buffered_char
[cp
->fd
] = cp
->chr
;
773 /* The SIGCHLD handler will do a Wait so we know it won't
774 return until the process is dead
775 We force Wait to only wait for this process to avoid it
776 picking up other children that happen to be dead but that
777 we haven't noticed yet
778 SIG_DFL for SIGCHLD is ignore? */
779 if (sig_handlers
[SIGCHLD
] != SIG_DFL
&&
780 sig_handlers
[SIGCHLD
] != SIG_IGN
)
783 DebPrint (("select calling SIGCHLD handler for pid %d\n",
787 sig_handlers
[SIGCHLD
](SIGCHLD
);
791 /* Clean up the child process entry in the table */
799 Substitute for certain kill () operations
802 win32_kill_process (int pid
, int sig
)
806 /* Only handle signals that will result in the process dying */
807 if (sig
!= SIGINT
&& sig
!= SIGKILL
&& sig
!= SIGQUIT
&& sig
!= SIGHUP
)
813 cp
= find_child_pid (pid
);
816 DebPrint (("win32_kill_process didn't find a child with pid %lu\n", pid
));
823 /* Fake Ctrl-Break. */
824 if (!GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT
, pid
))
826 DebPrint (("win32_kill_process.GenerateConsoleCtrlEvent return %d "
827 "for pid %lu\n", GetLastError (), pid
));
834 /* Kill the process. On Win32 this doesn't kill child processes
835 so it doesn't work very well for shells which is why it's
836 not used in every case. */
837 if (!TerminateProcess (cp
->process
, 0xff))
839 DebPrint (("win32_kill_process.TerminateProcess returned %d "
840 "for pid %lu\n", GetLastError (), pid
));
848 /* If the channel is a pipe this read might block since we don't
849 know how many characters are available, so check and read only
851 We also need to wake up the reader thread once we've read our data. */
853 read_child_output (int fd
, char *buf
, int max
)
860 h
= (HANDLE
)_get_osfhandle (fd
);
861 if (GetFileType (h
) == FILE_TYPE_PIPE
)
863 PeekNamedPipe (h
, NULL
, 0, NULL
, &waiting
, NULL
);
864 to_read
= min (waiting
, (DWORD
)max
);
869 /* Use read to get CRLF translation */
870 nchars
= read (fd
, buf
, to_read
);
872 if (GetFileType (h
) == FILE_TYPE_PIPE
)
874 /* Wake up the reader thread
876 cp
= find_child_fd (fd
);
879 if (!SetEvent (cp
->char_consumed
))
880 DebPrint (("read_child_output.SetEvent failed with "
881 "%lu for fd %ld\n", GetLastError (), fd
));
884 DebPrint (("read_child_output couldn't find a child with fd %d\n",