274
|
1 /* Asynchronous subprocess implemenation for Win32
|
|
2 Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994, 1995
|
|
3 Free Software Foundation, Inc.
|
|
4 Copyright (C) 1995 Sun Microsystems, Inc.
|
|
5 Copyright (C) 1995, 1996 Ben Wing.
|
|
6
|
|
7 This file is part of XEmacs.
|
|
8
|
|
9 XEmacs is free software; you can redistribute it and/or modify it
|
|
10 under the terms of the GNU General Public License as published by the
|
|
11 Free Software Foundation; either version 2, or (at your option) any
|
|
12 later version.
|
|
13
|
|
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
17 for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with XEmacs; see the file COPYING. If not, write to
|
|
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 Boston, MA 02111-1307, USA. */
|
|
23
|
|
24 /* Written by Kirill M. Katsnelson <kkm@kis.ru>, April 1998 */
|
|
25
|
|
26 #include <config.h>
|
|
27 #include "lisp.h"
|
|
28
|
|
29 #include "hash.h"
|
|
30 #include "lstream.h"
|
|
31 #include "process.h"
|
|
32 #include "procimpl.h"
|
|
33
|
|
34 #include <windows.h>
|
|
35
|
|
36 /* Implemenation-specific data. Pointed to by Lisp_Process->process_data */
|
|
37 struct nt_process_data
|
|
38 {
|
|
39 HANDLE h_process;
|
|
40 };
|
|
41
|
|
42 #define NT_DATA(p) ((struct nt_process_data*)((p)->process_data))
|
|
43
|
|
44 /*-----------------------------------------------------------------------*/
|
|
45 /* Process helpers */
|
|
46 /*-----------------------------------------------------------------------*/
|
|
47
|
276
|
48 /* This one breaks process abstraction. Prototype is in console-msw.h,
|
|
49 used by select_process method in event-msw.c */
|
|
50 HANDLE
|
|
51 get_nt_process_handle (struct Lisp_Process *p)
|
|
52 {
|
|
53 return (NT_DATA (p)->h_process);
|
|
54 }
|
274
|
55
|
|
56 /*-----------------------------------------------------------------------*/
|
|
57 /* Process methods */
|
|
58 /*-----------------------------------------------------------------------*/
|
|
59
|
|
60 /*
|
|
61 * Allocate and initialize Lisp_Process->process_data
|
|
62 */
|
|
63
|
|
64 static void
|
|
65 nt_alloc_process_data (struct Lisp_Process *p)
|
|
66 {
|
|
67 p->process_data = xnew (struct nt_process_data);
|
|
68 }
|
|
69
|
|
70 #if 0 /* #### Need this method? */
|
|
71 /*
|
|
72 * Mark any Lisp objects in Lisp_Process->process_data
|
|
73 */
|
|
74
|
|
75 static void
|
|
76 nt_mark_process_data (struct Lisp_Process *proc,
|
|
77 void (*markobj) (Lisp_Object))
|
|
78 {
|
|
79 }
|
|
80 #endif
|
|
81
|
|
82 static void
|
|
83 nt_finalize_process_data (struct Lisp_Process *p, int for_disksave)
|
|
84 {
|
|
85 assert (!for_disksave);
|
|
86 if (NT_DATA(p)->h_process)
|
|
87 CloseHandle (NT_DATA(p)->h_process);
|
|
88 }
|
|
89
|
|
90 #if 0 /* #### Need this method? */
|
|
91 /*
|
|
92 * Initialize XEmacs process implemenation once
|
|
93 */
|
|
94
|
|
95 static void
|
|
96 nt_init_process (void)
|
|
97 {
|
|
98 }
|
|
99 #endif
|
|
100
|
|
101 #if 0 /* #### Need this method? */
|
|
102 /*
|
|
103 * Initialize any process local data. This is called when newly
|
|
104 * created process is connected to real OS file handles. The
|
|
105 * handles are generally represented by void* type, but are
|
|
106 * of type HANDLE for Win32
|
|
107 */
|
|
108
|
|
109 static void
|
|
110 nt_init_process_io_handles (struct Lisp_Process *p, void* in, void* out, int flags)
|
|
111 {
|
|
112 }
|
|
113 #endif
|
|
114
|
|
115 /*
|
|
116 * Fork off a subprocess. P is a pointer to newly created subprocess
|
|
117 * object. If this function signals, the caller is responsible for
|
|
118 * deleting (and finalizing) the process object.
|
|
119 *
|
|
120 * The method must return PID of the new proces, a (positive??? ####) number
|
|
121 * which fits into Lisp_Int. No return value indicates an error, the method
|
|
122 * must signal an error instead.
|
|
123 */
|
|
124
|
|
125 /* #### This function completely ignores Vprocess_environment */
|
|
126
|
|
127 static int
|
|
128 nt_create_process (struct Lisp_Process *p,
|
|
129 char **argv, CONST char *current_dir)
|
|
130 {
|
|
131 HANDLE hmyshove, hmyslurp, hprocin, hprocout;
|
|
132 LPTSTR command_line;
|
|
133
|
|
134 /* Create two unidirectional named pipes */
|
|
135 {
|
|
136 HANDLE htmp;
|
|
137 SECURITY_ATTRIBUTES sa;
|
|
138
|
|
139 sa.nLength = sizeof(sa);
|
|
140 sa.bInheritHandle = TRUE;
|
|
141 sa.lpSecurityDescriptor = NULL;
|
|
142
|
|
143 CreatePipe (&hprocin, &hmyshove, &sa, 0);
|
|
144 CreatePipe (&hmyslurp, &hprocout, &sa, 0);
|
|
145
|
|
146 /* Stupid Win32 allows to create a pipe with *both* ends either
|
|
147 inheritable or not. We need process ends inheritable, and local
|
|
148 ends not inheritable. */
|
|
149 /* #### Perhaps even stupider me does not know how to do this better */
|
|
150 DuplicateHandle (GetCurrentProcess(), hmyshove, GetCurrentProcess(), &htmp,
|
|
151 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
|
152 hmyshove = htmp;
|
|
153 DuplicateHandle (GetCurrentProcess(), hmyslurp, GetCurrentProcess(), &htmp,
|
|
154 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
|
155 hmyslurp = htmp;
|
|
156 }
|
|
157
|
|
158 /* Convert an argv vector into Win32 style command line.
|
|
159
|
|
160 #### This works only for cmd, and not for cygwin bash. Perhaps,
|
|
161 instead of ad-hoc fiddling with different methods for quoting
|
|
162 process arguments in ntproc.c (disgust shudder), this must call a
|
|
163 smart lisp routine. The code here will be a fallback, if the
|
|
164 lisp function is not specified.
|
|
165 */
|
|
166 {
|
|
167 char** thisarg;
|
|
168 size_t size = 1;
|
|
169
|
|
170 for (thisarg = argv; *thisarg; ++thisarg)
|
|
171 size += strlen (*thisarg) + 1;
|
|
172
|
|
173 command_line = alloca_array (char, size);
|
|
174 *command_line = 0;
|
|
175
|
|
176 for (thisarg = argv; *thisarg; ++thisarg)
|
|
177 {
|
|
178 if (thisarg != argv)
|
|
179 strcat (command_line, " ");
|
|
180 strcat (command_line, *thisarg);
|
|
181 }
|
|
182 }
|
|
183
|
|
184 /* Create process */
|
|
185 {
|
|
186 STARTUPINFO si;
|
|
187 PROCESS_INFORMATION pi;
|
|
188 DWORD err;
|
|
189 BOOL windowed;
|
|
190
|
|
191 xzero (si);
|
|
192 si.hStdInput = hprocin;
|
|
193 si.hStdOutput = hprocout;
|
|
194 si.hStdError = hprocout;
|
|
195 si.wShowWindow = SW_HIDE;
|
|
196 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
|
197
|
|
198 err = (CreateProcess (NULL, command_line, NULL, NULL, TRUE,
|
|
199 CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP | CREATE_SUSPENDED,
|
|
200 NULL, current_dir, &si, &pi)
|
|
201 ? 0 : GetLastError ());
|
|
202
|
|
203 CloseHandle (hprocin);
|
|
204 CloseHandle (hprocout);
|
|
205
|
|
206 /* See if we succeeded with process creation */
|
|
207 if (err)
|
|
208 {
|
|
209 process_error__One_of_those_nasty_uses_for_goto_statement:
|
|
210 CloseHandle (hmyshove);
|
|
211 CloseHandle (hmyslurp);
|
|
212 error ("Cannot start \"%s\": error code was %lu", argv[0], err);
|
|
213 }
|
|
214
|
|
215 /* Determine if the new process is a windowed one */
|
|
216 windowed = WaitForInputIdle (pi.hProcess, 100) == WAIT_TIMEOUT;
|
|
217 if (windowed)
|
|
218 {
|
|
219 /* We restart windowed process fire-and forget style, and
|
|
220 indicate successful process creation, just as if the
|
|
221 process ended instantly upon launching */
|
|
222 CloseHandle (hmyshove);
|
|
223 CloseHandle (hmyslurp);
|
|
224 /* TerminateProcess is safe becuase the process is not yet
|
|
225 running */
|
|
226 TerminateProcess (pi.hProcess, 0);
|
|
227 si.dwFlags = STARTF_USESHOWWINDOW;
|
|
228 si.wShowWindow = SW_SHOWNORMAL;
|
|
229 if (!CreateProcess (NULL, command_line, NULL, NULL, FALSE,
|
|
230 DETACHED_PROCESS , NULL, current_dir, &si, &pi))
|
|
231 {
|
|
232 err = GetLastError ();
|
|
233 goto process_error__One_of_those_nasty_uses_for_goto_statement;
|
|
234 }
|
|
235
|
|
236 /* We just launched a windowed process. Fake it as if a
|
|
237 process launched has already ended */
|
|
238 p->status_symbol = Qexit;
|
|
239
|
|
240 /* Get rid of process and thread handles */
|
|
241 CloseHandle (pi.hThread);
|
|
242 CloseHandle (pi.hProcess);
|
|
243 }
|
|
244 else
|
|
245 {
|
|
246 /* Just started a console subprocess */
|
|
247
|
|
248 NT_DATA(p)->h_process = pi.hProcess;
|
|
249
|
|
250 init_process_io_handles (p, (void*)hmyslurp, (void*)hmyshove, 0);
|
|
251
|
|
252 /* We created it suspended. Resume the only thread */
|
|
253 ResumeThread (pi.hThread);
|
|
254 CloseHandle (pi.hThread);
|
|
255 }
|
|
256
|
|
257 return ((int)pi.dwProcessId < 0
|
|
258 ? -(int)pi.dwProcessId : (int)pi.dwProcessId);
|
|
259 }
|
|
260 }
|
|
261
|
|
262 /*
|
|
263 * This method is called to update status fields of the process
|
|
264 * structure. If the process has not existed, this method is expected
|
|
265 * to do nothing.
|
|
266 *
|
|
267 * The method is called only for real child processes.
|
|
268 */
|
|
269
|
|
270 static void
|
|
271 nt_update_status_if_terminated (struct Lisp_Process* p)
|
|
272 {
|
|
273 DWORD exit_code;
|
|
274 if (GetExitCodeProcess (NT_DATA(p)->h_process, &exit_code)
|
|
275 && exit_code != STILL_ACTIVE)
|
|
276 {
|
|
277 p->tick++;
|
|
278 p->core_dumped = 0;
|
|
279 /* The exit code can be a code returned by process, or an
|
|
280 NTSTATUS value. We cannot accurately handle the latter since
|
|
281 it is a full 32 bit integer */
|
|
282 if (exit_code & 0xC0000000)
|
|
283 {
|
|
284 p->status_symbol = Qsignal;
|
|
285 p->exit_code = exit_code & 0x1FFFFFFF;
|
|
286 }
|
|
287 else
|
|
288 {
|
|
289 p->status_symbol = Qexit;
|
|
290 p->exit_code = exit_code;
|
|
291 }
|
|
292 }
|
|
293 }
|
|
294
|
|
295 /*
|
|
296 * Stuff the entire contents of LSTREAM to the process ouptut pipe
|
|
297 */
|
|
298
|
|
299 /* #### If only this function could be somehow merged with
|
|
300 unix_send_process... */
|
|
301
|
|
302 static void
|
|
303 nt_send_process (Lisp_Object proc, struct lstream* lstream)
|
|
304 {
|
|
305 struct Lisp_Process *p = XPROCESS (proc);
|
|
306
|
|
307 /* use a reasonable-sized buffer (somewhere around the size of the
|
|
308 stream buffer) so as to avoid inundating the stream with blocked
|
|
309 data. */
|
|
310 Bufbyte chunkbuf[512];
|
|
311 Bytecount chunklen;
|
|
312
|
|
313 while (1)
|
|
314 {
|
|
315 int writeret;
|
|
316
|
|
317 chunklen = Lstream_read (lstream, chunkbuf, 512);
|
|
318 if (chunklen <= 0)
|
|
319 break; /* perhaps should abort() if < 0?
|
|
320 This should never happen. */
|
|
321
|
|
322 /* Lstream_write() will never successfully write less than the
|
|
323 amount sent in. In the worst case, it just buffers the
|
|
324 unwritten data. */
|
|
325 writeret = Lstream_write (XLSTREAM (DATA_OUTSTREAM(p)), chunkbuf,
|
|
326 chunklen);
|
|
327 if (writeret < 0)
|
|
328 {
|
|
329 p->status_symbol = Qexit;
|
|
330 p->exit_code = ERROR_BROKEN_PIPE;
|
|
331 p->core_dumped = 0;
|
|
332 p->tick++;
|
|
333 process_tick++;
|
|
334 deactivate_process (proc);
|
|
335 error ("Broken pipe error sending to process %s; closed it",
|
|
336 XSTRING_DATA (p->name));
|
|
337 }
|
|
338
|
|
339 while (Lstream_was_blocked_p (XLSTREAM (p->pipe_outstream)))
|
|
340 {
|
|
341 /* Buffer is full. Wait, accepting input; that may allow
|
|
342 the program to finish doing output and read more. */
|
|
343 Faccept_process_output (Qnil, make_int (1), Qnil);
|
|
344 Lstream_flush (XLSTREAM (p->pipe_outstream));
|
|
345 }
|
|
346 }
|
|
347 Lstream_flush (XLSTREAM (DATA_OUTSTREAM(p)));
|
|
348 }
|
|
349
|
|
350 /*-----------------------------------------------------------------------*/
|
|
351 /* Initialization */
|
|
352 /*-----------------------------------------------------------------------*/
|
|
353
|
|
354 void
|
|
355 process_type_create_nt (void)
|
|
356 {
|
|
357 PROCESS_HAS_METHOD (nt, alloc_process_data);
|
|
358 PROCESS_HAS_METHOD (nt, finalize_process_data);
|
|
359 /* PROCESS_HAS_METHOD (nt, mark_process_data); */
|
|
360 /* PROCESS_HAS_METHOD (nt, init_process); */
|
|
361 /* PROCESS_HAS_METHOD (nt, init_process_io_handles); */
|
|
362 PROCESS_HAS_METHOD (nt, create_process);
|
|
363 PROCESS_HAS_METHOD (nt, update_status_if_terminated);
|
|
364 PROCESS_HAS_METHOD (nt, send_process);
|
|
365 /* PROCESS_HAS_METHOD (nt, kill_child_process); */
|
|
366 /* PROCESS_HAS_METHOD (nt, kill_process_by_pid); */
|
|
367 #if 0 /* Yet todo */
|
|
368 #ifdef HAVE_SOCKETS
|
|
369 PROCESS_HAS_METHOD (nt, canonicalize_host_name);
|
|
370 PROCESS_HAS_METHOD (nt, open_network_stream);
|
|
371 #ifdef HAVE_MULTICAST
|
|
372 PROCESS_HAS_METHOD (nt, open_multicast_group);
|
|
373 #endif
|
|
374 #endif
|
|
375 #endif
|
|
376 }
|
|
377
|
|
378 void
|
|
379 vars_of_process_nt (void)
|
|
380 {
|
|
381 }
|
|
382
|