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"
|
278
|
33 #include "sysdep.h"
|
274
|
34
|
|
35 #include <windows.h>
|
278
|
36 #include <shellapi.h>
|
274
|
37
|
|
38 /* Implemenation-specific data. Pointed to by Lisp_Process->process_data */
|
|
39 struct nt_process_data
|
|
40 {
|
|
41 HANDLE h_process;
|
|
42 };
|
|
43
|
|
44 #define NT_DATA(p) ((struct nt_process_data*)((p)->process_data))
|
|
45
|
|
46 /*-----------------------------------------------------------------------*/
|
|
47 /* Process helpers */
|
|
48 /*-----------------------------------------------------------------------*/
|
|
49
|
276
|
50 /* This one breaks process abstraction. Prototype is in console-msw.h,
|
|
51 used by select_process method in event-msw.c */
|
|
52 HANDLE
|
|
53 get_nt_process_handle (struct Lisp_Process *p)
|
|
54 {
|
|
55 return (NT_DATA (p)->h_process);
|
|
56 }
|
274
|
57
|
|
58 /*-----------------------------------------------------------------------*/
|
|
59 /* Process methods */
|
|
60 /*-----------------------------------------------------------------------*/
|
|
61
|
|
62 /*
|
|
63 * Allocate and initialize Lisp_Process->process_data
|
|
64 */
|
|
65
|
|
66 static void
|
|
67 nt_alloc_process_data (struct Lisp_Process *p)
|
|
68 {
|
|
69 p->process_data = xnew (struct nt_process_data);
|
|
70 }
|
|
71
|
|
72 #if 0 /* #### Need this method? */
|
|
73 /*
|
|
74 * Mark any Lisp objects in Lisp_Process->process_data
|
|
75 */
|
|
76
|
|
77 static void
|
|
78 nt_mark_process_data (struct Lisp_Process *proc,
|
|
79 void (*markobj) (Lisp_Object))
|
|
80 {
|
|
81 }
|
|
82 #endif
|
|
83
|
|
84 static void
|
|
85 nt_finalize_process_data (struct Lisp_Process *p, int for_disksave)
|
|
86 {
|
|
87 assert (!for_disksave);
|
|
88 if (NT_DATA(p)->h_process)
|
|
89 CloseHandle (NT_DATA(p)->h_process);
|
|
90 }
|
|
91
|
|
92 #if 0 /* #### Need this method? */
|
|
93 /*
|
|
94 * Initialize XEmacs process implemenation once
|
|
95 */
|
|
96
|
|
97 static void
|
|
98 nt_init_process (void)
|
|
99 {
|
|
100 }
|
|
101 #endif
|
|
102
|
|
103 #if 0 /* #### Need this method? */
|
|
104 /*
|
|
105 * Initialize any process local data. This is called when newly
|
|
106 * created process is connected to real OS file handles. The
|
|
107 * handles are generally represented by void* type, but are
|
|
108 * of type HANDLE for Win32
|
|
109 */
|
|
110
|
|
111 static void
|
|
112 nt_init_process_io_handles (struct Lisp_Process *p, void* in, void* out, int flags)
|
|
113 {
|
|
114 }
|
|
115 #endif
|
|
116
|
|
117 /*
|
|
118 * Fork off a subprocess. P is a pointer to newly created subprocess
|
|
119 * object. If this function signals, the caller is responsible for
|
|
120 * deleting (and finalizing) the process object.
|
|
121 *
|
|
122 * The method must return PID of the new proces, a (positive??? ####) number
|
|
123 * which fits into Lisp_Int. No return value indicates an error, the method
|
|
124 * must signal an error instead.
|
|
125 */
|
|
126
|
|
127 /* #### This function completely ignores Vprocess_environment */
|
|
128
|
278
|
129 static void
|
|
130 signal_cannot_launch (char* image_file, DWORD err)
|
|
131 {
|
|
132 mswindows_set_errno (err);
|
|
133 error ("Starting \"%s\": %s", image_file, strerror (errno));
|
|
134 }
|
|
135
|
274
|
136 static int
|
|
137 nt_create_process (struct Lisp_Process *p,
|
|
138 char **argv, CONST char *current_dir)
|
|
139 {
|
|
140 HANDLE hmyshove, hmyslurp, hprocin, hprocout;
|
|
141 LPTSTR command_line;
|
278
|
142 BOOL do_io, windowed;
|
|
143
|
|
144 /* Find out whether the application is windowed or not */
|
274
|
145 {
|
278
|
146 /* SHGetFileInfo tends to return ERROR_FILE_NOT_FOUND on most
|
|
147 errors. This leads to bogus error message. */
|
|
148 DWORD image_type = SHGetFileInfo (argv[0], 0, NULL, 0, SHGFI_EXETYPE);
|
|
149 if (image_type == 0)
|
|
150 signal_cannot_launch (argv[0], (GetLastError () == ERROR_FILE_NOT_FOUND
|
|
151 ? ERROR_BAD_FORMAT : GetLastError ()));
|
|
152 windowed = HIWORD (image_type) != 0;
|
|
153 }
|
274
|
154
|
278
|
155 /* Decide whether to do I/O on process handles, or just mark the
|
|
156 process exited immediately upon successful launching. We do I/O if the
|
|
157 process is a console one, or if it is windowed but windowed_process_io
|
|
158 is non-zero */
|
|
159 do_io = !windowed || windowed_process_io ;
|
|
160
|
|
161 if (do_io)
|
|
162 {
|
|
163 /* Create two unidirectional named pipes */
|
|
164 HANDLE htmp;
|
|
165 SECURITY_ATTRIBUTES sa;
|
274
|
166
|
278
|
167 sa.nLength = sizeof(sa);
|
|
168 sa.bInheritHandle = TRUE;
|
|
169 sa.lpSecurityDescriptor = NULL;
|
|
170
|
|
171 CreatePipe (&hprocin, &hmyshove, &sa, 0);
|
|
172 CreatePipe (&hmyslurp, &hprocout, &sa, 0);
|
|
173
|
|
174 /* Stupid Win32 allows to create a pipe with *both* ends either
|
|
175 inheritable or not. We need process ends inheritable, and local
|
|
176 ends not inheritable. */
|
|
177 DuplicateHandle (GetCurrentProcess(), hmyshove, GetCurrentProcess(), &htmp,
|
|
178 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
|
179 hmyshove = htmp;
|
|
180 DuplicateHandle (GetCurrentProcess(), hmyslurp, GetCurrentProcess(), &htmp,
|
|
181 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
|
|
182 hmyslurp = htmp;
|
|
183 }
|
274
|
184
|
|
185 /* Convert an argv vector into Win32 style command line.
|
|
186
|
|
187 #### This works only for cmd, and not for cygwin bash. Perhaps,
|
|
188 instead of ad-hoc fiddling with different methods for quoting
|
|
189 process arguments in ntproc.c (disgust shudder), this must call a
|
|
190 smart lisp routine. The code here will be a fallback, if the
|
|
191 lisp function is not specified.
|
|
192 */
|
|
193 {
|
|
194 char** thisarg;
|
|
195 size_t size = 1;
|
|
196
|
|
197 for (thisarg = argv; *thisarg; ++thisarg)
|
|
198 size += strlen (*thisarg) + 1;
|
|
199
|
|
200 command_line = alloca_array (char, size);
|
|
201 *command_line = 0;
|
|
202
|
|
203 for (thisarg = argv; *thisarg; ++thisarg)
|
|
204 {
|
|
205 if (thisarg != argv)
|
|
206 strcat (command_line, " ");
|
|
207 strcat (command_line, *thisarg);
|
|
208 }
|
|
209 }
|
|
210
|
|
211 /* Create process */
|
|
212 {
|
|
213 STARTUPINFO si;
|
|
214 PROCESS_INFORMATION pi;
|
|
215 DWORD err;
|
|
216
|
|
217 xzero (si);
|
278
|
218 si.dwFlags = STARTF_USESHOWWINDOW;
|
|
219 si.wShowWindow = windowed ? SW_SHOWNORMAL : SW_HIDE;
|
|
220 if (do_io)
|
274
|
221 {
|
278
|
222 si.hStdInput = hprocin;
|
|
223 si.hStdOutput = hprocout;
|
|
224 si.hStdError = hprocout;
|
|
225 si.dwFlags |= STARTF_USESTDHANDLES;
|
274
|
226 }
|
|
227
|
278
|
228 err = (CreateProcess (NULL, command_line, NULL, NULL, TRUE,
|
|
229 CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP,
|
|
230 NULL, current_dir, &si, &pi)
|
|
231 ? 0 : GetLastError ());
|
|
232
|
|
233 if (do_io)
|
274
|
234 {
|
278
|
235 /* These just have been inherited; we do not need a copy */
|
|
236 CloseHandle (hprocin);
|
|
237 CloseHandle (hprocout);
|
|
238 }
|
|
239
|
|
240 /* Handle process creation failure */
|
|
241 if (err)
|
|
242 {
|
|
243 if (do_io)
|
274
|
244 {
|
278
|
245 CloseHandle (hmyshove);
|
|
246 CloseHandle (hmyslurp);
|
274
|
247 }
|
278
|
248 signal_cannot_launch (argv[0], GetLastError ());
|
|
249 }
|
274
|
250
|
278
|
251 /* The process started successfully */
|
|
252 if (do_io)
|
|
253 {
|
|
254 NT_DATA(p)->h_process = pi.hProcess;
|
|
255 init_process_io_handles (p, (void*)hmyslurp, (void*)hmyshove, 0);
|
274
|
256 }
|
|
257 else
|
|
258 {
|
278
|
259 /* Indicate as if the process has exited immediately. */
|
|
260 p->status_symbol = Qexit;
|
|
261 CloseHandle (pi.hProcess);
|
274
|
262 }
|
|
263
|
278
|
264 CloseHandle (pi.hThread);
|
|
265
|
|
266 /* Hack to support Windows 95 negative pids */
|
274
|
267 return ((int)pi.dwProcessId < 0
|
|
268 ? -(int)pi.dwProcessId : (int)pi.dwProcessId);
|
|
269 }
|
|
270 }
|
|
271
|
|
272 /*
|
|
273 * This method is called to update status fields of the process
|
|
274 * structure. If the process has not existed, this method is expected
|
|
275 * to do nothing.
|
|
276 *
|
|
277 * The method is called only for real child processes.
|
|
278 */
|
|
279
|
|
280 static void
|
|
281 nt_update_status_if_terminated (struct Lisp_Process* p)
|
|
282 {
|
|
283 DWORD exit_code;
|
|
284 if (GetExitCodeProcess (NT_DATA(p)->h_process, &exit_code)
|
|
285 && exit_code != STILL_ACTIVE)
|
|
286 {
|
|
287 p->tick++;
|
|
288 p->core_dumped = 0;
|
|
289 /* The exit code can be a code returned by process, or an
|
|
290 NTSTATUS value. We cannot accurately handle the latter since
|
|
291 it is a full 32 bit integer */
|
|
292 if (exit_code & 0xC0000000)
|
|
293 {
|
|
294 p->status_symbol = Qsignal;
|
|
295 p->exit_code = exit_code & 0x1FFFFFFF;
|
|
296 }
|
|
297 else
|
|
298 {
|
|
299 p->status_symbol = Qexit;
|
|
300 p->exit_code = exit_code;
|
|
301 }
|
|
302 }
|
|
303 }
|
|
304
|
|
305 /*
|
|
306 * Stuff the entire contents of LSTREAM to the process ouptut pipe
|
|
307 */
|
|
308
|
|
309 /* #### If only this function could be somehow merged with
|
|
310 unix_send_process... */
|
|
311
|
|
312 static void
|
|
313 nt_send_process (Lisp_Object proc, struct lstream* lstream)
|
|
314 {
|
|
315 struct Lisp_Process *p = XPROCESS (proc);
|
|
316
|
|
317 /* use a reasonable-sized buffer (somewhere around the size of the
|
|
318 stream buffer) so as to avoid inundating the stream with blocked
|
|
319 data. */
|
|
320 Bufbyte chunkbuf[512];
|
|
321 Bytecount chunklen;
|
|
322
|
|
323 while (1)
|
|
324 {
|
|
325 int writeret;
|
|
326
|
|
327 chunklen = Lstream_read (lstream, chunkbuf, 512);
|
|
328 if (chunklen <= 0)
|
|
329 break; /* perhaps should abort() if < 0?
|
|
330 This should never happen. */
|
|
331
|
|
332 /* Lstream_write() will never successfully write less than the
|
|
333 amount sent in. In the worst case, it just buffers the
|
|
334 unwritten data. */
|
|
335 writeret = Lstream_write (XLSTREAM (DATA_OUTSTREAM(p)), chunkbuf,
|
|
336 chunklen);
|
|
337 if (writeret < 0)
|
|
338 {
|
|
339 p->status_symbol = Qexit;
|
|
340 p->exit_code = ERROR_BROKEN_PIPE;
|
|
341 p->core_dumped = 0;
|
|
342 p->tick++;
|
|
343 process_tick++;
|
|
344 deactivate_process (proc);
|
|
345 error ("Broken pipe error sending to process %s; closed it",
|
|
346 XSTRING_DATA (p->name));
|
|
347 }
|
|
348
|
|
349 while (Lstream_was_blocked_p (XLSTREAM (p->pipe_outstream)))
|
|
350 {
|
|
351 /* Buffer is full. Wait, accepting input; that may allow
|
|
352 the program to finish doing output and read more. */
|
|
353 Faccept_process_output (Qnil, make_int (1), Qnil);
|
|
354 Lstream_flush (XLSTREAM (p->pipe_outstream));
|
|
355 }
|
|
356 }
|
|
357 Lstream_flush (XLSTREAM (DATA_OUTSTREAM(p)));
|
|
358 }
|
|
359
|
|
360 /*-----------------------------------------------------------------------*/
|
|
361 /* Initialization */
|
|
362 /*-----------------------------------------------------------------------*/
|
|
363
|
|
364 void
|
|
365 process_type_create_nt (void)
|
|
366 {
|
|
367 PROCESS_HAS_METHOD (nt, alloc_process_data);
|
|
368 PROCESS_HAS_METHOD (nt, finalize_process_data);
|
|
369 /* PROCESS_HAS_METHOD (nt, mark_process_data); */
|
|
370 /* PROCESS_HAS_METHOD (nt, init_process); */
|
|
371 /* PROCESS_HAS_METHOD (nt, init_process_io_handles); */
|
|
372 PROCESS_HAS_METHOD (nt, create_process);
|
|
373 PROCESS_HAS_METHOD (nt, update_status_if_terminated);
|
|
374 PROCESS_HAS_METHOD (nt, send_process);
|
|
375 /* PROCESS_HAS_METHOD (nt, kill_child_process); */
|
|
376 /* PROCESS_HAS_METHOD (nt, kill_process_by_pid); */
|
|
377 #if 0 /* Yet todo */
|
|
378 #ifdef HAVE_SOCKETS
|
|
379 PROCESS_HAS_METHOD (nt, canonicalize_host_name);
|
|
380 PROCESS_HAS_METHOD (nt, open_network_stream);
|
|
381 #ifdef HAVE_MULTICAST
|
|
382 PROCESS_HAS_METHOD (nt, open_multicast_group);
|
|
383 #endif
|
|
384 #endif
|
|
385 #endif
|
|
386 }
|
|
387
|
|
388 void
|
|
389 vars_of_process_nt (void)
|
|
390 {
|
|
391 }
|
|
392
|