annotate src/w32proc.c @ 14472:8979515dac09 libc-960203 libc-960204 libc-960205 libc-960206 libc-960207 libc-960208 libc-960209 libc-960210

Add cygwin32; Put eabi .sdata2/.sbss2 into read-only memory
author Michael Meissner <gnu@the-meissners.org>
date Sat, 03 Feb 1996 01:03:27 +0000
parents ee40177f6c68
children 86a1f426871e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1 /* Process support for Windows NT port of GNU EMACS.
11388
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
2 Copyright (C) 1992, 1995 Free Software Foundation, Inc.
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
3
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
4 This file is part of GNU Emacs.
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
6 GNU Emacs is free software; you can redistribute it and/or modify
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
7 it under the terms of the GNU General Public License as published by
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
8 the Free Software Foundation; either version 2, or (at your option)
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
9 any later version.
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
11 GNU Emacs is distributed in the hope that it will be useful,
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
14 GNU General Public License for more details.
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
16 You should have received a copy of the GNU General Public License
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
17 along with GNU Emacs; see the file COPYING. If not, write to
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
19 Boston, MA 02111-1307, USA.
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 Drew Bliss Oct 14, 1993
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22 Adapted from alarm.c by Tim Fleehart
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23 */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24
12183
47685fb0fbd1 Include config.h before stdio.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11388
diff changeset
25 #include <config.h>
47685fb0fbd1 Include config.h before stdio.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 11388
diff changeset
26
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 #include <stdio.h>
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28 #include <stdlib.h>
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 #include <errno.h>
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30 #include <io.h>
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 #include <signal.h>
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33 #include <windows.h>
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35 #include "lisp.h"
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36 #include "nt.h"
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37 #include "systime.h"
13931
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
38 #include "syswait.h"
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
39 #include "process.h"
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
40
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
41 #ifndef SYS_SIGLIST_DECLARED
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
42 extern char *sys_siglist[];
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
43 #endif
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 /* #define FULL_DEBUG */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 typedef void (_CALLBACK_ *signal_handler)(int);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 /* Defined in process.h which conflicts with the local copy */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50 #define _P_NOWAIT 1
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 typedef struct _child_process
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 int fd;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 HANDLE char_avail;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56 HANDLE char_consumed;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57 char chr;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 BOOL status;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 HANDLE process;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 DWORD pid;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 HANDLE thrd;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 } child_process;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 #define MAX_CHILDREN MAXDESC
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 #ifdef EMACSDEBUG
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 void _CRTAPI1
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 _DebPrint (char *fmt, ...)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70 char buf[256];
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
71 va_list args;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73 va_start (args, fmt);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 vsprintf (buf, fmt, args);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 va_end (args);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76 OutputDebugString (buf);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 #endif
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
80 /* Child process management list. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81 static int child_proc_count = 0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 static child_process child_procs[MAX_CHILDREN];
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83 static child_process *dead_child = NULL;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85 #define CHILD_ACTIVE(cp) ((cp)->process != NULL)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86 #define DEACTIVATE_CHILD(cp) ((cp)->process = NULL)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88 /* Signal handlers...SIG_DFL == 0 so this is initialized correctly. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89 static signal_handler sig_handlers[NSIG];
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91 /* Fake signal implementation to record the SIGCHLD handler. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92 signal_handler
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93 win32_signal (int sig, signal_handler handler)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95 signal_handler old;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
97 if (sig != SIGCHLD)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 errno = EINVAL;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 return SIG_ERR;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 old = sig_handlers[sig];
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103 sig_handlers[sig] = handler;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104 return old;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 /* Find an unused process slot. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 static child_process *
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 new_child (void)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111 child_process *cp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 if (child_proc_count == MAX_CHILDREN)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 return NULL;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116 for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 if (!CHILD_ACTIVE (cp))
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 return cp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 return &child_procs[child_proc_count++];
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 /* Find a child by pid. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 static child_process *
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124 find_child_pid (DWORD pid)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 child_process *cp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128 for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 if (CHILD_ACTIVE (cp) && pid == cp->pid)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 return cp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131 return NULL;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
134 /* Find a child by fd. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
135 static child_process *
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136 find_child_fd (int fd)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138 child_process *cp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 if (CHILD_ACTIVE (cp) && fd == cp->fd)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 return cp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 return NULL;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 /* Thread proc for child process reader threads
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 The threads just sit in a loop waiting for input
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 When they detect input, they signal the char_avail input to
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 wake up the select emulator
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 When the select emulator processes their input, it pulses
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 char_consumed so that the reader thread goes back to reading. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 DWORD WINAPI
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 reader_thread (void *arg)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 child_process *cp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 /* Our identity */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 cp = (child_process *)arg;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 /* We have to wait for the go-ahead before we can start */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 if (WaitForSingleObject (cp->char_consumed, INFINITE) != WAIT_OBJECT_0)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 return 0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163 /* If something went wrong, quit */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 if (!cp->status)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 return 0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 for (;;)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 /* Use read to get CRLF translation */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 if (read (cp->fd, &cp->chr, sizeof (char)) == sizeof (char))
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 cp->status = TRUE;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 else
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 #ifdef FULL_DEBUG
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 DebPrint (("reader_thread.read failed with %lu for fd %ld\n",
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178 GetLastError (), cp->fd));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 #endif
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 cp->status = FALSE;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183 if (!SetEvent (cp->char_avail))
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 DebPrint (("reader_thread.SetEvent failed with %lu for fd %ld\n",
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186 GetLastError (), cp->fd));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 break;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190 /* If the read died, the child has died so let the thread die */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191 if (!cp->status)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 break;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 /* Wait until our input is acknowledged before reading again */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 if (WaitForSingleObject (cp->char_consumed, INFINITE) != WAIT_OBJECT_0)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 DebPrint (("reader_thread.WaitForSingleObject failed with "
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 "%lu for fd %ld\n", GetLastError (), cp->fd));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 break;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 return 0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 static BOOL
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206 create_child (char *exe, char *cmdline, char *env,
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 PROCESS_INFORMATION *info)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 child_process *cp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 DWORD id;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211 STARTUPINFO start;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 SECURITY_ATTRIBUTES sec_attrs;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 SECURITY_DESCRIPTOR sec_desc;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215 cp = new_child ();
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216 if (cp == NULL)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 goto EH_Fail;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 cp->fd = -1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221 cp->char_avail = CreateEvent (NULL, FALSE, FALSE, NULL);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
222 if (cp->char_avail == NULL)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223 goto EH_Fail;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 cp->char_consumed = CreateEvent (NULL, FALSE, FALSE, NULL);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 if (cp->char_consumed == NULL)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 goto EH_char_avail;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229 cp->thrd = CreateThread (NULL, 1024, reader_thread, cp, 0, &id);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 if (cp->thrd == NULL)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 goto EH_char_consumed;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 memset (&start, 0, sizeof (start));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234 start.cb = sizeof (start);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235
13425
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
236 #ifdef HAVE_NTGUI
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
237 start.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
238 start.wShowWindow = SW_HIDE;
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
239
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
240 start.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
241 start.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
242 start.hStdError = GetStdHandle (STD_ERROR_HANDLE);
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
243 #endif /* HAVE_NTGUI */
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
244
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 /* Explicitly specify no security */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246 if (!InitializeSecurityDescriptor (&sec_desc, SECURITY_DESCRIPTOR_REVISION))
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
247 goto EH_thrd;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 if (!SetSecurityDescriptorDacl (&sec_desc, TRUE, NULL, FALSE))
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
249 goto EH_thrd;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 sec_attrs.nLength = sizeof (sec_attrs);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 sec_attrs.lpSecurityDescriptor = &sec_desc;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 sec_attrs.bInheritHandle = FALSE;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 if (!CreateProcess (exe, cmdline, &sec_attrs, NULL, TRUE,
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 CREATE_NEW_PROCESS_GROUP, env, NULL,
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256 &start, info))
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257 goto EH_thrd;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258 cp->process = info->hProcess;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
259 cp->pid = info->dwProcessId;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
260
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
261 return TRUE;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
262
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263 EH_thrd:
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 id = GetLastError ();
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 cp->status = FALSE;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
267 SetEvent (cp->char_consumed);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
268 EH_char_consumed:
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
269 CloseHandle (cp->char_consumed);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270 EH_char_avail:
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271 CloseHandle (cp->char_avail);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
272 EH_Fail:
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 return FALSE;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276 /* create_child doesn't know what emacs' file handle will be for waiting
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277 on output from the child, so we need to make this additional call
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
278 to register the handle with the process
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279 This way the select emulator knows how to match file handles with
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280 entries in child_procs. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
281 void
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
282 register_child (int pid, int fd)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
283 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
284 child_process *cp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
285
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
286 cp = find_child_pid (pid);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
287 if (cp == NULL)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
288 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
289 DebPrint (("register_child unable to find pid %lu\n", pid));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
290 return;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
291 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
292
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
293 #ifdef FULL_DEBUG
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
294 DebPrint (("register_child registered fd %d with pid %lu\n", fd, pid));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
295 #endif
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
296
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
297 cp->fd = fd;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
298 cp->status = TRUE;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
299
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
300 /* Tell the reader thread to start */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
301 if (!SetEvent (cp->char_consumed))
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
302 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
303 DebPrint (("register_child.SetEvent failed with %lu for fd %ld\n",
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
304 GetLastError (), cp->fd));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
305 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
306 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
307
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
308 /* When a process dies its pipe will break so the reader thread will
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
309 signal failure to the select emulator.
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
310 The select emulator then calls this routine to clean up.
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
311 Since the thread signaled failure we can assume it is exiting. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
312 static void
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
313 remove_child (child_process *cp)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
314 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
315 /* Reap the thread */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
316 if (WaitForSingleObject (cp->thrd, INFINITE) != WAIT_OBJECT_0)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
317 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
318 DebPrint (("remove_child.WaitForSingleObject (thread) failed "
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
319 "with %lu for fd %ld\n", GetLastError (), cp->fd));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
320 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
321 CloseHandle (cp->thrd);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
322 CloseHandle (cp->char_consumed);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
323 CloseHandle (cp->char_avail);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
324
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
325 /* Reap the process */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
326 if (WaitForSingleObject (cp->process, INFINITE) != WAIT_OBJECT_0)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
327 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
328 DebPrint (("remove_child.WaitForSingleObject (process) failed "
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
329 "with %lu for fd %ld\n", GetLastError (), cp->fd));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
330 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
331 CloseHandle (cp->process);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
332
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
333 DEACTIVATE_CHILD (cp);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
335
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
336 /* Wait for any of our existing child processes to die
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337 When it does, close its handle
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338 Return the pid and fill in the status if non-NULL. */
11388
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
339
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
340 int
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
341 win32_wait (int *status)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
342 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
343 DWORD active, retval;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
344 int nh;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
345 child_process *cp, *cps[MAX_CHILDREN];
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
346 HANDLE wait_hnd[MAX_CHILDREN];
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
347
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
348 nh = 0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
349 if (dead_child != NULL)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
350 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
351 /* We want to wait for a specific child */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
352 wait_hnd[nh] = dead_child->process;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
353 cps[nh] = dead_child;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
354 nh++;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
355 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
356 else
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
357 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
358 for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
359 if (CHILD_ACTIVE (cp))
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361 wait_hnd[nh] = cp->process;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
362 cps[nh] = cp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363 nh++;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
364 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
365 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
366
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
367 if (nh == 0)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
368 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
369 /* Nothing to wait on, so fail */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
370 errno = ECHILD;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
371 return -1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
372 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
373
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
374 active = WaitForMultipleObjects (nh, wait_hnd, FALSE, INFINITE);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
375 if (active == WAIT_FAILED)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
376 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
377 errno = EBADF;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
378 return -1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
379 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
380 else if (active == WAIT_TIMEOUT)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
381 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
382 /* Should never happen */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
383 errno = EINVAL;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
384 return -1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
385 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
386 else if (active >= WAIT_OBJECT_0 &&
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
387 active < WAIT_OBJECT_0+MAXIMUM_WAIT_OBJECTS)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
388 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
389 active -= WAIT_OBJECT_0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
390 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
391 else if (active >= WAIT_ABANDONED_0 &&
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
392 active < WAIT_ABANDONED_0+MAXIMUM_WAIT_OBJECTS)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
393 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
394 active -= WAIT_ABANDONED_0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
395 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
396
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
397 if (!GetExitCodeProcess (wait_hnd[active], &retval))
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
398 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
399 DebPrint (("Wait.GetExitCodeProcess failed with %lu\n",
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
400 GetLastError ()));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
401 retval = 1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
402 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
403 if (retval == STILL_ACTIVE)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
404 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
405 /* Should never happen */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
406 DebPrint (("Wait.WaitForMultipleObjects returned an active process\n"));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
407 errno = EINVAL;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
408 return -1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
409 }
12325
aa6fc4e97a28 (win32_wait): Massage retval into what is expected in Unix.
Richard M. Stallman <rms@gnu.org>
parents: 12239
diff changeset
410
aa6fc4e97a28 (win32_wait): Massage retval into what is expected in Unix.
Richard M. Stallman <rms@gnu.org>
parents: 12239
diff changeset
411 /* Massage the exit code from the process to match the format expected
14036
621a575db6f7 Comment fixes.
Karl Heuer <kwzh@gnu.org>
parents: 13931
diff changeset
412 by the WIFSTOPPED et al macros in syswait.h. Only WIFSIGNALED and
12325
aa6fc4e97a28 (win32_wait): Massage retval into what is expected in Unix.
Richard M. Stallman <rms@gnu.org>
parents: 12239
diff changeset
413 WIFEXITED are supported; WIFSTOPPED doesn't make sense under NT. */
aa6fc4e97a28 (win32_wait): Massage retval into what is expected in Unix.
Richard M. Stallman <rms@gnu.org>
parents: 12239
diff changeset
414
aa6fc4e97a28 (win32_wait): Massage retval into what is expected in Unix.
Richard M. Stallman <rms@gnu.org>
parents: 12239
diff changeset
415 if (retval == STATUS_CONTROL_C_EXIT)
aa6fc4e97a28 (win32_wait): Massage retval into what is expected in Unix.
Richard M. Stallman <rms@gnu.org>
parents: 12239
diff changeset
416 retval = SIGINT;
aa6fc4e97a28 (win32_wait): Massage retval into what is expected in Unix.
Richard M. Stallman <rms@gnu.org>
parents: 12239
diff changeset
417 else
aa6fc4e97a28 (win32_wait): Massage retval into what is expected in Unix.
Richard M. Stallman <rms@gnu.org>
parents: 12239
diff changeset
418 retval <<= 8;
aa6fc4e97a28 (win32_wait): Massage retval into what is expected in Unix.
Richard M. Stallman <rms@gnu.org>
parents: 12239
diff changeset
419
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
420 cp = cps[active];
11388
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
421
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
422 if (status)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
423 {
11388
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
424 *status = retval;
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
425 }
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
426 else if (synch_process_alive)
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
427 {
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
428 synch_process_alive = 0;
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
429
13931
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
430 /* Report the status of the synchronous process. */
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
431 if (WIFEXITED (retval))
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
432 synch_process_retcode = WRETCODE (retval);
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
433 else if (WIFSIGNALED (retval))
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
434 {
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
435 int code = WTERMSIG (retval);
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
436 char *signame = 0;
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
437
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
438 if (code < NSIG)
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
439 {
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
440 /* Suppress warning if the table has const char *. */
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
441 signame = (char *) sys_siglist[code];
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
442 }
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
443 if (signame == 0)
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
444 signame = "unknown";
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
445
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
446 synch_process_death = signame;
c80fb27c183b (win32_wait): fixed bad synch_process_retcode
Geoff Voelker <voelker@cs.washington.edu>
parents: 13425
diff changeset
447 }
11388
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
448 TerminateThread (cp->thrd, 0);
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
449 CloseHandle (cp->thrd);
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
450 CloseHandle (cp->char_consumed);
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
451 CloseHandle (cp->char_avail);
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
452 CloseHandle (cp->process);
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
453 DEACTIVATE_CHILD (cp);
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
454 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
455
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
456 return cp->pid;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
457 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
458
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
459 /* We pass our process ID to our children by setting up an environment
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
460 variable in their environment. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
461 char ppid_env_var_buffer[64];
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
462
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
463 /* When a new child process is created we need to register it in our list,
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
464 so intercept spawn requests. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
465 int
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
466 win32_spawnve (int mode, char *cmdname, char **argv, char **envp)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
467 {
12239
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
468 Lisp_Object program, full;
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
469 char *cmdline, *env, *parg, **targ;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
470 int arglen;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
471 PROCESS_INFORMATION pi;
12239
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
472
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
473 /* Handle executable names without an executable suffix. */
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
474 program = make_string (cmdname, strlen (cmdname));
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
475 if (NILP (Ffile_executable_p (program)))
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
476 {
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
477 struct gcpro gcpro1;
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
478
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
479 full = Qnil;
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
480 GCPRO1 (program);
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
481 openp (Vexec_path, program, EXEC_SUFFIXES, &full, 1);
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
482 UNGCPRO;
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
483 if (NILP (full))
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
484 {
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
485 errno = EINVAL;
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
486 return -1;
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
487 }
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
488 cmdname = XSTRING (full)->data;
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
489 argv[0] = cmdname;
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
490 }
ff7738cdbd99 (win32_spawnve): Accept program names without executable suffixes.
Richard M. Stallman <rms@gnu.org>
parents: 12183
diff changeset
491
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
492 if (child_proc_count == MAX_CHILDREN)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
493 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
494 errno = EAGAIN;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
495 return -1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
496 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
497
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
498 /* We don't care about the other modes */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
499 if (mode != _P_NOWAIT)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
500 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
501 errno = EINVAL;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
502 return -1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
503 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
504
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
505 /* we have to do some conjuring here to put argv and envp into the
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
506 form CreateProcess wants... argv needs to be a space separated/null
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
507 terminated list of parameters, and envp is a null
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
508 separated/double-null terminated list of parameters.
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
509
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
510 Since I have no idea how large argv and envp are likely to be
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
511 we figure out list lengths on the fly and allocate them. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
512
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
513 /* do argv... */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
514 arglen = 0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
515 targ = argv;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
516 while (*targ)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
517 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
518 arglen += strlen (*targ++) + 1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
519 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
520 cmdline = malloc (arglen);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
521 if (cmdline == NULL)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
522 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
523 errno = ENOMEM;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
524 goto EH_Fail;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
525 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
526 targ = argv;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
527 parg = cmdline;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
528 while (*targ)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
529 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
530 strcpy (parg, *targ);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
531 parg += strlen (*targ++);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
532 *parg++ = ' ';
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
533 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
534 *--parg = '\0';
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
535
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
536 /* and envp... */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
537 arglen = 1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
538 targ = envp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
539 while (*targ)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
540 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
541 arglen += strlen (*targ++) + 1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
542 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
543 sprintf (ppid_env_var_buffer, "__PARENT_PROCESS_ID=%d",
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
544 GetCurrentProcessId ());
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
545 arglen += strlen (ppid_env_var_buffer) + 1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
546
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
547 env = malloc (arglen);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
548 if (env == NULL)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
549 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
550 errno = ENOMEM;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
551 goto EH_cmdline;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
552 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
553 targ = envp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
554 parg = env;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
555 while (*targ)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
556 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
557 strcpy (parg, *targ);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
558 parg += strlen (*targ++);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
559 *parg++ = '\0';
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
560 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
561 strcpy (parg, ppid_env_var_buffer);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
562 parg += strlen (ppid_env_var_buffer);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
563 *parg++ = '\0';
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
564 *parg = '\0';
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
565
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
566 /* Now create the process. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
567 if (!create_child (cmdname, cmdline, env, &pi))
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
568 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
569 errno = ENOEXEC;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
570 goto EH_env;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
571 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
572
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
573 return pi.dwProcessId;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
574
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
575 EH_env:
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
576 free (env);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
577 EH_cmdline:
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
578 free (cmdline);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
579 EH_Fail:
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
580 return -1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
581 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
582
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
583 /* Emulate the select call
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
584 Wait for available input on any of the given rfds, or timeout if
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
585 a timeout is given and no input is detected
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
586 wfds and efds are not supported and must be NULL. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
587
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
588 /* From ntterm.c */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
589 extern HANDLE keyboard_handle;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
590 /* From process.c */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
591 extern int proc_buffered_char[];
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
592
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
593 int
11388
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
594 sys_select (int nfds, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
595 EMACS_TIME *timeout)
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
596 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
597 SELECT_TYPE orfds;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
598 DWORD timeout_ms;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
599 int i, nh, nr;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
600 DWORD active;
13425
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
601 child_process *cp, *cps[MAX_CHILDREN + 1];
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
602 HANDLE wait_hnd[MAX_CHILDREN + 1];
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
603 #ifdef HAVE_NTGUI1
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
604 BOOL keyboardwait = FALSE ;
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
605 #endif /* HAVE_NTGUI */
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
606
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
607 /* If the descriptor sets are NULL but timeout isn't, then just Sleep. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
608 if (rfds == NULL && wfds == NULL && efds == NULL && timeout != NULL)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
609 {
11388
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
610 #ifdef HAVE_TIMEVAL
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
611 Sleep (timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
612 #else
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
613 Sleep ((*timeout) * 1000);
11388
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
614 #endif
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
615 return 0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
616 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
617
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
618 /* Otherwise, we only handle rfds, so fail otherwise. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
619 if (rfds == NULL || wfds != NULL || efds != NULL)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
620 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
621 errno = EINVAL;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
622 return -1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
623 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
624
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
625 orfds = *rfds;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
626 FD_ZERO (rfds);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
627 nr = 0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
628
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
629 /* Build a list of handles to wait on. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
630 nh = 0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
631 for (i = 0; i < nfds; i++)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
632 if (FD_ISSET (i, &orfds))
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
633 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
634 if (i == 0)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
635 {
13425
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
636 #ifdef HAVE_NTGUI1
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
637 keyboardwait = TRUE ;
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
638 #else
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
639 /* Handle stdin specially */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
640 wait_hnd[nh] = keyboard_handle;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
641 cps[nh] = NULL;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
642 nh++;
13425
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
643 #endif /* HAVE_NTGUI */
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
644
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
645 /* Check for any emacs-generated input in the queue since
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
646 it won't be detected in the wait */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
647 if (detect_input_pending ())
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
648 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
649 FD_SET (i, rfds);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
650 nr++;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
651 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
652 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
653 else
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
654 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
655 /* Child process input */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
656 cp = find_child_fd (i);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
657 if (cp)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
658 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
659 #ifdef FULL_DEBUG
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
660 DebPrint (("select waiting on child %d fd %d\n",
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
661 cp-child_procs, i));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
662 #endif
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
663 wait_hnd[nh] = cp->char_avail;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
664 cps[nh] = cp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
665 nh++;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
666 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
667 else
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
668 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
669 /* Unable to find something to wait on for this fd, fail */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
670 DebPrint (("select unable to find child process "
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
671 "for fd %ld\n", i));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
672 nh = 0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
673 break;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
674 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
675 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
676 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
677
13425
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
678 /* Never do this in win32 since we will not get paint messages */
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
679
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
680 #ifndef HAVE_NTGUI1
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
681 /* Nothing to look for, so we didn't find anything */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
682 if (nh == 0)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
683 {
11388
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
684 if (timeout)
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
685 #ifdef HAVE_TIMEVAL
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
686 Sleep (timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
687 #else
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
688 Sleep ((*timeout) * 1000);
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
689 #endif
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
690 return 0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
691 }
13425
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
692 #endif /* !HAVE_NTGUI */
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
693
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
694 /* Check for immediate return without waiting */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
695 if (nr > 0)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
696 return nr;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
697
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
698 /*
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
699 Wait for input
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
700 If a child process dies while this is waiting, its pipe will break
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
701 so the reader thread will signal an error condition, thus, the wait
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
702 will wake up
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
703 */
11388
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
704 #ifdef HAVE_TIMEVAL
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
705 timeout_ms = timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : INFINITE;
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
706 #else
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
707 timeout_ms = timeout ? *timeout*1000 : INFINITE;
11388
96fa39ad9403 (win32_wait): Reap synchronous subprocesses, and place
Karl Heuer <kwzh@gnu.org>
parents: 9907
diff changeset
708 #endif
13425
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
709 #ifdef HAVE_NTGUI1
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
710 active = MsgWaitForMultipleObjects (nh, wait_hnd, FALSE, timeout_ms,QS_ALLINPUT);
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
711 #else
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
712 active = WaitForMultipleObjects (nh, wait_hnd, FALSE, timeout_ms);
13425
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
713 #endif /* HAVE_NTGUI */
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
714 if (active == WAIT_FAILED)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
715 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
716 DebPrint (("select.WaitForMultipleObjects (%d, %lu) failed with %lu\n",
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
717 nh, timeout_ms, GetLastError ()));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
718 /* Is there a better error? */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
719 errno = EBADF;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
720 return -1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
721 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
722 else if (active == WAIT_TIMEOUT)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
723 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
724 return 0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
725 }
13425
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
726 #ifdef HAVE_NTGUI1
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
727 else if (active == WAIT_OBJECT_0 + nh)
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
728 {
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
729 /* Keyboard input available */
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
730 FD_SET (0, rfds);
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
731
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
732 /* This shouldn't be necessary, but apparently just setting the input
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
733 fd is not good enough for emacs */
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
734 // read_input_waiting ();
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
735
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
736 return (1) ;
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
737 }
b6eacb7da9f6 [HAVE_NTGUI] (create_child): Pass handles to children.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12325
diff changeset
738 #endif /* HAVE_NTGUI */
9907
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
739 else if (active >= WAIT_OBJECT_0 &&
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
740 active < WAIT_OBJECT_0+MAXIMUM_WAIT_OBJECTS)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
741 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
742 active -= WAIT_OBJECT_0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
743 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
744 else if (active >= WAIT_ABANDONED_0 &&
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
745 active < WAIT_ABANDONED_0+MAXIMUM_WAIT_OBJECTS)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
746 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
747 active -= WAIT_ABANDONED_0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
748 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
749
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
750 if (cps[active] == NULL)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
751 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
752 /* Keyboard input available */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
753 FD_SET (0, rfds);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
754 nr++;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
755
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
756 /* This shouldn't be necessary, but apparently just setting the input
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
757 fd is not good enough for emacs */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
758 read_input_waiting ();
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
759 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
760 else
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
761 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
762 /* Child process */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
763 cp = cps[active];
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
764
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
765 /* If status is FALSE the read failed so don't report input */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
766 if (cp->status)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
767 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
768 FD_SET (cp->fd, rfds);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
769 proc_buffered_char[cp->fd] = cp->chr;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
770 nr++;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
771 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
772 else
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
773 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
774 /* The SIGCHLD handler will do a Wait so we know it won't
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
775 return until the process is dead
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
776 We force Wait to only wait for this process to avoid it
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
777 picking up other children that happen to be dead but that
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
778 we haven't noticed yet
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
779 SIG_DFL for SIGCHLD is ignore? */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
780 if (sig_handlers[SIGCHLD] != SIG_DFL &&
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
781 sig_handlers[SIGCHLD] != SIG_IGN)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
782 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
783 #ifdef FULL_DEBUG
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
784 DebPrint (("select calling SIGCHLD handler for pid %d\n",
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
785 cp->pid));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
786 #endif
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
787 dead_child = cp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
788 sig_handlers[SIGCHLD](SIGCHLD);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
789 dead_child = NULL;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
790 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
791
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
792 /* Clean up the child process entry in the table */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
793 remove_child (cp);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
794 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
795 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
796 return nr;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
797 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
798
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
799 /*
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
800 Substitute for certain kill () operations
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
801 */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
802 int
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
803 win32_kill_process (int pid, int sig)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
804 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
805 child_process *cp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
806
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
807 /* Only handle signals that will result in the process dying */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
808 if (sig != SIGINT && sig != SIGKILL && sig != SIGQUIT && sig != SIGHUP)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
809 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
810 errno = EINVAL;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
811 return -1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
812 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
813
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
814 cp = find_child_pid (pid);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
815 if (cp == NULL)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
816 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
817 DebPrint (("win32_kill_process didn't find a child with pid %lu\n", pid));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
818 errno = ECHILD;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
819 return -1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
820 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
821
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
822 if (sig == SIGINT)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
823 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
824 /* Fake Ctrl-Break. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
825 if (!GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, pid))
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
826 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
827 DebPrint (("win32_kill_process.GenerateConsoleCtrlEvent return %d "
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
828 "for pid %lu\n", GetLastError (), pid));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
829 errno = EINVAL;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
830 return -1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
831 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
832 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
833 else
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
834 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
835 /* Kill the process. On Win32 this doesn't kill child processes
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
836 so it doesn't work very well for shells which is why it's
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
837 not used in every case. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
838 if (!TerminateProcess (cp->process, 0xff))
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
839 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
840 DebPrint (("win32_kill_process.TerminateProcess returned %d "
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
841 "for pid %lu\n", GetLastError (), pid));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
842 errno = EINVAL;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
843 return -1;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
844 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
845 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
846 return 0;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
847 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
848
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
849 /* If the channel is a pipe this read might block since we don't
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
850 know how many characters are available, so check and read only
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
851 what's there
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
852 We also need to wake up the reader thread once we've read our data. */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
853 int
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
854 read_child_output (int fd, char *buf, int max)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
855 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
856 HANDLE h;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
857 int to_read, nchars;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
858 DWORD waiting;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
859 child_process *cp;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
860
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
861 h = (HANDLE)_get_osfhandle (fd);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
862 if (GetFileType (h) == FILE_TYPE_PIPE)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
863 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
864 PeekNamedPipe (h, NULL, 0, NULL, &waiting, NULL);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
865 to_read = min (waiting, (DWORD)max);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
866 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
867 else
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
868 to_read = max;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
869
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
870 /* Use read to get CRLF translation */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
871 nchars = read (fd, buf, to_read);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
872
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
873 if (GetFileType (h) == FILE_TYPE_PIPE)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
874 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
875 /* Wake up the reader thread
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
876 for this process */
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
877 cp = find_child_fd (fd);
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
878 if (cp)
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
879 {
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
880 if (!SetEvent (cp->char_consumed))
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
881 DebPrint (("read_child_output.SetEvent failed with "
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
882 "%lu for fd %ld\n", GetLastError (), fd));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
883 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
884 else
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
885 DebPrint (("read_child_output couldn't find a child with fd %d\n",
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
886 fd));
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
887 }
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
888
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
889 return nchars;
2e5a14f7c44e Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
890 }