428
|
1 /* Asynchronous subprocess implementation for UNIX
|
|
2 Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994, 1995
|
|
3 Free Software Foundation, Inc.
|
|
4 Copyright (C) 1995 Sun Microsystems, Inc.
|
1330
|
5 Copyright (C) 1995, 1996, 2001, 2002, 2003 Ben Wing.
|
428
|
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
|
771
|
24 /* Mule-ized as of 6-14-00 */
|
428
|
25
|
|
26 /* This file has been split into process.c and process-unix.c by
|
|
27 Kirill M. Katsnelson <kkm@kis.ru>, so please bash him and not
|
|
28 the original author(s) */
|
|
29
|
440
|
30 /* The IPv6 support is derived from the code for GNU Emacs-20.3
|
|
31 written by Wolfgang S. Rupprecht */
|
|
32
|
428
|
33 #include <config.h>
|
|
34
|
|
35 #if !defined (NO_SUBPROCESSES)
|
|
36
|
|
37 /* The entire file is within this conditional */
|
|
38
|
|
39 #include "lisp.h"
|
|
40
|
|
41 #include "buffer.h"
|
|
42 #include "events.h"
|
|
43 #include "frame.h"
|
|
44 #include "hash.h"
|
|
45 #include "lstream.h"
|
|
46 #include "opaque.h"
|
|
47 #include "process.h"
|
|
48 #include "procimpl.h"
|
|
49 #include "sysdep.h"
|
|
50 #include "window.h"
|
|
51 #include "file-coding.h"
|
|
52
|
|
53 #include <setjmp.h>
|
853
|
54 #include "sysdir.h"
|
428
|
55 #include "sysfile.h"
|
|
56 #include "sysproc.h"
|
859
|
57 #include "syssignal.h"
|
428
|
58 #include "systime.h"
|
|
59 #include "systty.h"
|
|
60 #include "syswait.h"
|
|
61
|
442
|
62 #ifdef HPUX
|
|
63 #include <grp.h> /* See grantpt fixups for HPUX below. */
|
|
64 #endif
|
428
|
65
|
502
|
66 #if defined (HAVE_GETADDRINFO) && defined (HAVE_GETNAMEINFO)
|
|
67 #define USE_GETADDRINFO
|
|
68 #endif
|
|
69
|
|
70
|
428
|
71 /*
|
|
72 * Implementation-specific data. Pointed to by Lisp_Process->process_data
|
|
73 */
|
|
74
|
|
75 struct unix_process_data
|
|
76 {
|
|
77 /* Non-0 if this is really a ToolTalk channel. */
|
|
78 int connected_via_filedesc_p;
|
|
79 /* Descriptor by which we read from this process. -1 for dead process */
|
|
80 int infd;
|
853
|
81 /* Descriptor by which we read stderr from this process. -1 for
|
|
82 dead process */
|
|
83 int errfd;
|
428
|
84 /* Descriptor for the tty which this process is using.
|
|
85 -1 if we didn't record it (on some systems, there's no need). */
|
|
86 int subtty;
|
|
87 /* Non-false if communicating through a pty. */
|
|
88 char pty_flag;
|
|
89 };
|
853
|
90 #define UNIX_DATA(p) ((struct unix_process_data*) ((p)->process_data))
|
428
|
91
|
|
92
|
|
93
|
|
94 /**********************************************************************/
|
|
95 /* Static helper routines */
|
|
96 /**********************************************************************/
|
|
97
|
|
98 static SIGTYPE
|
|
99 close_safely_handler (int signo)
|
|
100 {
|
|
101 EMACS_REESTABLISH_SIGNAL (signo, close_safely_handler);
|
|
102 SIGRETURN;
|
|
103 }
|
|
104
|
|
105 static void
|
|
106 close_safely (int fd)
|
|
107 {
|
|
108 stop_interrupts ();
|
613
|
109 set_timeout_signal (SIGALRM, close_safely_handler);
|
428
|
110 alarm (1);
|
771
|
111 retry_close (fd);
|
428
|
112 alarm (0);
|
|
113 start_interrupts ();
|
|
114 }
|
|
115
|
|
116 static void
|
|
117 close_descriptor_pair (int in, int out)
|
|
118 {
|
|
119 if (in >= 0)
|
771
|
120 retry_close (in);
|
428
|
121 if (out != in && out >= 0)
|
771
|
122 retry_close (out);
|
428
|
123 }
|
|
124
|
|
125 /* Close all descriptors currently in use for communication
|
|
126 with subprocess. This is used in a newly-forked subprocess
|
|
127 to get rid of irrelevant descriptors. */
|
|
128
|
|
129 static int
|
853
|
130 close_process_descs_mapfun (const void *key, void *contents, void *arg)
|
428
|
131 {
|
853
|
132 Lisp_Object proc = VOID_TO_LISP (contents);
|
|
133 USID vaffan, culo;
|
|
134
|
|
135 event_stream_delete_io_streams (XPROCESS (proc)->pipe_instream,
|
|
136 XPROCESS (proc)->pipe_outstream,
|
|
137 XPROCESS (proc)->pipe_errstream,
|
|
138 &vaffan, &culo);
|
428
|
139 return 0;
|
|
140 }
|
|
141
|
|
142 void
|
|
143 close_process_descs (void)
|
|
144 {
|
|
145 maphash (close_process_descs_mapfun, usid_to_process, 0);
|
|
146 }
|
|
147
|
|
148 /* connect to an existing file descriptor. This is very similar to
|
|
149 open-network-stream except that it assumes that the connection has
|
|
150 already been initialized. It is currently used for ToolTalk
|
|
151 communication. */
|
|
152
|
|
153 /* This function used to be visible on the Lisp level, but there is no
|
|
154 real point in doing that. Here is the doc string:
|
|
155
|
442
|
156 "Connect to an existing file descriptor.
|
|
157 Return a subprocess-object to represent the connection.
|
|
158 Input and output work as for subprocesses; `delete-process' closes it.
|
|
159 Args are NAME BUFFER INFD OUTFD.
|
|
160 NAME is name for process. It is modified if necessary to make it unique.
|
|
161 BUFFER is the buffer (or buffer-name) to associate with the process.
|
|
162 Process output goes at end of that buffer, unless you specify
|
|
163 an output stream or filter function to handle the output.
|
|
164 BUFFER may also be nil, meaning that this process is not associated
|
|
165 with any buffer.
|
|
166 INFD and OUTFD specify the file descriptors to use for input and
|
428
|
167 output, respectively."
|
|
168 */
|
|
169
|
|
170 Lisp_Object
|
|
171 connect_to_file_descriptor (Lisp_Object name, Lisp_Object buffer,
|
|
172 Lisp_Object infd, Lisp_Object outfd)
|
|
173 {
|
|
174 /* This function can GC */
|
|
175 Lisp_Object proc;
|
|
176 int inch;
|
|
177
|
|
178 CHECK_STRING (name);
|
|
179 CHECK_INT (infd);
|
|
180 CHECK_INT (outfd);
|
|
181
|
|
182 inch = XINT (infd);
|
442
|
183 if (get_process_from_usid (FD_TO_USID (inch)))
|
|
184 invalid_operation ("There is already a process connected to fd", infd);
|
428
|
185 if (!NILP (buffer))
|
|
186 buffer = Fget_buffer_create (buffer);
|
|
187 proc = make_process_internal (name);
|
|
188
|
|
189 XPROCESS (proc)->pid = Fcons (infd, name);
|
|
190 XPROCESS (proc)->buffer = buffer;
|
853
|
191 init_process_io_handles (XPROCESS (proc), (void *) inch,
|
|
192 (void *) XINT (outfd), (void *) -1, 0);
|
428
|
193 UNIX_DATA (XPROCESS (proc))->connected_via_filedesc_p = 1;
|
|
194
|
853
|
195 event_stream_select_process (XPROCESS (proc), 1, 1);
|
428
|
196
|
|
197 return proc;
|
|
198 }
|
|
199
|
442
|
200 static int allocate_pty_the_old_fashioned_way (void);
|
|
201
|
|
202 /* The file name of the (slave) pty opened by allocate_pty(). */
|
|
203 #ifndef MAX_PTYNAME_LEN
|
|
204 #define MAX_PTYNAME_LEN 64
|
|
205 #endif
|
867
|
206 static Ibyte pty_name[MAX_PTYNAME_LEN];
|
428
|
207
|
|
208 /* Open an available pty, returning a file descriptor.
|
|
209 Return -1 on failure.
|
|
210 The file name of the terminal corresponding to the pty
|
442
|
211 is left in the variable `pty_name'. */
|
428
|
212
|
|
213 static int
|
|
214 allocate_pty (void)
|
|
215 {
|
442
|
216 /* Unix98 standardized grantpt, unlockpt, and ptsname, but not the
|
|
217 functions required to open a master pty in the first place :-(
|
|
218
|
|
219 Modern Unix systems all seems to have convenience methods to open
|
|
220 a master pty fd in one function call, but there is little
|
|
221 agreement on how to do it.
|
|
222
|
|
223 allocate_pty() tries all the different known easy ways of opening
|
|
224 a pty. In case of failure, we resort to the old BSD-style pty
|
|
225 grovelling code in allocate_pty_the_old_fashioned_way(). */
|
|
226 int master_fd = -1;
|
771
|
227 const Extbyte *slave_name = NULL;
|
867
|
228 const CIbyte *clone = NULL;
|
|
229 static const CIbyte * const clones[] =
|
771
|
230 /* Different pty master clone devices */
|
442
|
231 {
|
|
232 "/dev/ptmx", /* Various systems */
|
|
233 "/dev/ptm/clone", /* HPUX */
|
|
234 "/dev/ptc", /* AIX */
|
|
235 "/dev/ptmx_bsd" /* Tru64 */
|
|
236 };
|
|
237
|
|
238 #ifdef HAVE_GETPT /* glibc */
|
|
239 master_fd = getpt ();
|
|
240 if (master_fd >= 0)
|
|
241 goto have_master;
|
|
242 #endif /* HAVE_GETPT */
|
|
243
|
|
244
|
|
245 #if defined(HAVE_OPENPTY) /* BSD, Tru64, glibc */
|
|
246 {
|
|
247 int slave_fd = -1;
|
|
248 int rc;
|
|
249 EMACS_BLOCK_SIGNAL (SIGCHLD);
|
|
250 rc = openpty (&master_fd, &slave_fd, NULL, NULL, NULL);
|
|
251 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
|
|
252 if (rc == 0)
|
|
253 {
|
|
254 slave_name = ttyname (slave_fd);
|
771
|
255 retry_close (slave_fd);
|
442
|
256 goto have_slave_name;
|
|
257 }
|
|
258 else
|
|
259 {
|
|
260 if (master_fd >= 0)
|
771
|
261 retry_close (master_fd);
|
442
|
262 if (slave_fd >= 0)
|
771
|
263 retry_close (slave_fd);
|
442
|
264 }
|
|
265 }
|
|
266 #endif /* HAVE_OPENPTY */
|
|
267
|
|
268 #if defined(HAVE__GETPTY) && defined (O_NDELAY) /* SGI */
|
|
269 master_fd = -1;
|
|
270 EMACS_BLOCK_SIGNAL (SIGCHLD);
|
|
271 slave_name = _getpty (&master_fd, O_RDWR | O_NDELAY, 0600, 0);
|
|
272 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
|
|
273 if (master_fd >= 0 && slave_name != NULL)
|
|
274 goto have_slave_name;
|
|
275 #endif /* HAVE__GETPTY */
|
|
276
|
|
277 /* Master clone devices are available on most systems */
|
|
278 {
|
|
279 int i;
|
|
280 for (i = 0; i < countof (clones); i++)
|
|
281 {
|
|
282 clone = clones[i];
|
867
|
283 master_fd = qxe_open ((Ibyte *) clone,
|
771
|
284 O_RDWR | O_NONBLOCK | OPEN_BINARY, 0);
|
442
|
285 if (master_fd >= 0)
|
|
286 goto have_master;
|
|
287 }
|
|
288 clone = NULL;
|
|
289 }
|
|
290
|
|
291 goto lose;
|
|
292
|
|
293 have_master:
|
|
294
|
|
295 #if defined (HAVE_PTSNAME)
|
|
296 slave_name = ptsname (master_fd);
|
|
297 if (slave_name)
|
|
298 goto have_slave_name;
|
|
299 #endif
|
|
300
|
|
301 /* AIX docs say to use ttyname, not ptsname, to get slave_name */
|
|
302 if (clone
|
|
303 && !strcmp (clone, "/dev/ptc")
|
|
304 && (slave_name = ttyname (master_fd)) != NULL)
|
|
305 goto have_slave_name;
|
|
306
|
|
307 goto lose;
|
|
308
|
|
309 have_slave_name:
|
771
|
310 {
|
867
|
311 Ibyte *slaveint;
|
771
|
312
|
|
313 EXTERNAL_TO_C_STRING (slave_name, slaveint, Qfile_name);
|
|
314 qxestrncpy (pty_name, slaveint, sizeof (pty_name));
|
|
315 }
|
|
316
|
442
|
317 pty_name[sizeof (pty_name) - 1] = '\0';
|
|
318 setup_pty (master_fd);
|
|
319
|
|
320 /* We jump through some hoops to frob the pty.
|
|
321 It's not obvious that checking the return code here is useful. */
|
|
322
|
|
323 /* "The grantpt() function will fail if it is unable to successfully
|
|
324 invoke the setuid root program. It may also fail if the
|
|
325 application has installed a signal handler to catch SIGCHLD
|
|
326 signals." */
|
|
327 #if defined (HAVE_GRANTPT) || defined (HAVE_UNLOCKPT)
|
|
328 EMACS_BLOCK_SIGNAL (SIGCHLD);
|
|
329
|
|
330 #if defined (HAVE_GRANTPT)
|
|
331 grantpt (master_fd);
|
|
332 #ifdef HPUX
|
|
333 /* grantpt() behavior on some versions of HP-UX differs from what's
|
|
334 specified in the man page: the group of the slave PTY is set to
|
|
335 the user's primary group, and we fix that. */
|
|
336 {
|
|
337 struct group *tty_group = getgrnam ("tty");
|
|
338 if (tty_group != NULL)
|
771
|
339 {
|
|
340 Extbyte *ptyout;
|
|
341
|
|
342 C_STRING_TO_EXTERNAL (pty_name, ptyout, Qfile_name);
|
|
343 chown (ptyout, (uid_t) -1, tty_group->gr_gid);
|
|
344 }
|
442
|
345 }
|
|
346 #endif /* HPUX has broken grantpt() */
|
|
347 #endif /* HAVE_GRANTPT */
|
|
348
|
|
349 #if defined (HAVE_UNLOCKPT)
|
|
350 unlockpt (master_fd);
|
|
351 #endif
|
|
352
|
|
353 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
|
|
354 #endif /* HAVE_GRANTPT || HAVE_UNLOCKPT */
|
|
355
|
|
356 return master_fd;
|
|
357
|
|
358 lose:
|
|
359 if (master_fd >= 0)
|
771
|
360 retry_close (master_fd);
|
442
|
361 return allocate_pty_the_old_fashioned_way ();
|
|
362 }
|
|
363
|
|
364 /* This function tries to allocate a pty by iterating through file
|
|
365 pairs with names like /dev/ptyp1 and /dev/ttyp1. */
|
|
366 static int
|
|
367 allocate_pty_the_old_fashioned_way (void)
|
|
368 {
|
428
|
369 struct stat stb;
|
|
370
|
|
371 /* Some systems name their pseudoterminals so that there are gaps in
|
|
372 the usual sequence - for example, on HP9000/S700 systems, there
|
|
373 are no pseudoterminals with names ending in 'f'. So we wait for
|
|
374 three failures in a row before deciding that we've reached the
|
|
375 end of the ptys. */
|
|
376 int failed_count = 0;
|
|
377 int fd;
|
|
378 int i;
|
|
379 int c;
|
|
380
|
|
381 #ifdef PTY_ITERATION
|
|
382 PTY_ITERATION
|
|
383 #else
|
442
|
384 # ifndef FIRST_PTY_LETTER
|
|
385 # define FIRST_PTY_LETTER 'p'
|
|
386 # endif
|
428
|
387 for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
|
|
388 for (i = 0; i < 16; i++)
|
442
|
389 #endif /* PTY_ITERATION */
|
|
390
|
428
|
391 {
|
|
392 #ifdef PTY_NAME_SPRINTF
|
|
393 PTY_NAME_SPRINTF
|
|
394 #else
|
771
|
395 qxesprintf (pty_name, "/dev/pty%c%x", c, i);
|
428
|
396 #endif /* no PTY_NAME_SPRINTF */
|
|
397
|
771
|
398 if (qxe_stat (pty_name, &stb) < 0)
|
428
|
399 {
|
442
|
400 if (++failed_count >= 3)
|
428
|
401 return -1;
|
|
402 }
|
|
403 else
|
|
404 failed_count = 0;
|
771
|
405 fd = qxe_open (pty_name, O_RDWR | O_NONBLOCK | OPEN_BINARY, 0);
|
428
|
406
|
|
407 if (fd >= 0)
|
|
408 {
|
|
409 #ifdef PTY_TTY_NAME_SPRINTF
|
|
410 PTY_TTY_NAME_SPRINTF
|
|
411 #else
|
771
|
412 qxesprintf (pty_name, "/dev/tty%c%x", c, i);
|
428
|
413 #endif /* no PTY_TTY_NAME_SPRINTF */
|
771
|
414 if (qxe_access (pty_name, R_OK | W_OK) == 0)
|
428
|
415 {
|
442
|
416 setup_pty (fd);
|
|
417 return fd;
|
428
|
418 }
|
771
|
419 retry_close (fd);
|
428
|
420 }
|
442
|
421 } /* iteration */
|
428
|
422 return -1;
|
|
423 }
|
|
424
|
|
425 static int
|
|
426 create_bidirectional_pipe (int *inchannel, int *outchannel,
|
|
427 volatile int *forkin, volatile int *forkout)
|
|
428 {
|
|
429 int sv[2];
|
|
430
|
|
431 #ifdef SKTPAIR
|
|
432 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) < 0)
|
|
433 return -1;
|
|
434 *outchannel = *inchannel = sv[0];
|
|
435 *forkout = *forkin = sv[1];
|
|
436 #else /* not SKTPAIR */
|
|
437 int temp;
|
|
438 temp = pipe (sv);
|
|
439 if (temp < 0) return -1;
|
|
440 *inchannel = sv[0];
|
|
441 *forkout = sv[1];
|
|
442 temp = pipe (sv);
|
|
443 if (temp < 0) return -1;
|
|
444 *outchannel = sv[1];
|
|
445 *forkin = sv[0];
|
|
446 #endif /* not SKTPAIR */
|
|
447 return 0;
|
|
448 }
|
|
449
|
|
450
|
|
451 #ifdef HAVE_SOCKETS
|
|
452
|
502
|
453 #ifndef USE_GETADDRINFO
|
428
|
454 static int
|
|
455 get_internet_address (Lisp_Object host, struct sockaddr_in *address,
|
578
|
456 Error_Behavior errb)
|
428
|
457 {
|
|
458 struct hostent *host_info_ptr = NULL;
|
|
459 #ifdef TRY_AGAIN
|
|
460 int count = 0;
|
|
461 #endif
|
|
462
|
|
463 xzero (*address);
|
|
464
|
|
465 while (1)
|
|
466 {
|
771
|
467 Extbyte *hostext;
|
|
468
|
428
|
469 #ifdef TRY_AGAIN
|
|
470 if (count++ > 10) break;
|
|
471 h_errno = 0;
|
|
472 #endif
|
771
|
473
|
1204
|
474 LISP_STRING_TO_EXTERNAL (host, hostext, Qunix_host_name_encoding);
|
771
|
475
|
428
|
476 /* Some systems can't handle SIGIO/SIGALARM in gethostbyname. */
|
|
477 slow_down_interrupts ();
|
771
|
478 host_info_ptr = gethostbyname (hostext);
|
428
|
479 speed_up_interrupts ();
|
|
480 #ifdef TRY_AGAIN
|
|
481 if (! (host_info_ptr == 0 && h_errno == TRY_AGAIN))
|
|
482 #endif
|
|
483 break;
|
|
484 Fsleep_for (make_int (1));
|
|
485 }
|
|
486 if (host_info_ptr)
|
|
487 {
|
|
488 address->sin_family = host_info_ptr->h_addrtype;
|
502
|
489 memcpy (&address->sin_addr, host_info_ptr->h_addr,
|
|
490 host_info_ptr->h_length);
|
428
|
491 }
|
|
492 else
|
|
493 {
|
|
494 IN_ADDR numeric_addr;
|
1204
|
495 Extbyte *hostext;
|
|
496
|
428
|
497 /* Attempt to interpret host as numeric inet address */
|
1204
|
498 LISP_STRING_TO_EXTERNAL (host, hostext, Qunix_host_name_encoding);
|
|
499 numeric_addr = inet_addr (hostext);
|
428
|
500 if (NUMERIC_ADDR_ERROR)
|
|
501 {
|
563
|
502 maybe_signal_error (Qio_error, "Unknown host", host,
|
1204
|
503 Qprocess, errb);
|
428
|
504 return 0;
|
|
505 }
|
|
506
|
|
507 /* There was some broken code here that called strlen() here
|
|
508 on (char *) &numeric_addr and even sometimes accessed
|
|
509 uninitialized data. */
|
|
510 address->sin_family = AF_INET;
|
|
511 * (IN_ADDR *) &address->sin_addr = numeric_addr;
|
|
512 }
|
|
513
|
|
514 return 1;
|
|
515 }
|
502
|
516 #endif /* !USE_GETADDRINFO */
|
428
|
517
|
|
518 static void
|
853
|
519 set_socket_nonblocking_maybe (int fd, int port, const char *proto)
|
428
|
520 {
|
|
521 #ifdef PROCESS_IO_BLOCKING
|
|
522 Lisp_Object tail;
|
|
523
|
|
524 for (tail = network_stream_blocking_port_list; CONSP (tail); tail = XCDR (tail))
|
|
525 {
|
|
526 Lisp_Object tail_port = XCAR (tail);
|
|
527
|
|
528 if (STRINGP (tail_port))
|
|
529 {
|
|
530 struct servent *svc_info;
|
771
|
531 Extbyte *tailportext;
|
|
532
|
428
|
533 CHECK_STRING (tail_port);
|
771
|
534 TO_EXTERNAL_FORMAT (LISP_STRING, tail_port, C_STRING_ALLOCA,
|
|
535 tailportext, Qnative);
|
|
536
|
|
537 svc_info = getservbyname (tailportext, proto);
|
428
|
538 if ((svc_info != 0) && (svc_info->s_port == port))
|
|
539 break;
|
|
540 else
|
|
541 continue;
|
|
542 }
|
|
543 else if (INTP (tail_port) && (htons ((unsigned short) XINT (tail_port)) == port))
|
|
544 break;
|
|
545 }
|
|
546
|
|
547 if (!CONSP (tail))
|
|
548 {
|
|
549 set_descriptor_non_blocking (fd);
|
|
550 }
|
|
551 #else
|
|
552 set_descriptor_non_blocking (fd);
|
|
553 #endif /* PROCESS_IO_BLOCKING */
|
|
554 }
|
|
555
|
|
556 #endif /* HAVE_SOCKETS */
|
|
557
|
|
558 /* Compute the Lisp form of the process status from
|
|
559 the numeric status that was returned by `wait'. */
|
|
560
|
|
561 static void
|
440
|
562 update_status_from_wait_code (Lisp_Process *p, int *w_fmh)
|
428
|
563 {
|
|
564 /* C compiler lossage when attempting to pass w directly */
|
|
565 int w = *w_fmh;
|
|
566
|
|
567 if (WIFSTOPPED (w))
|
|
568 {
|
|
569 p->status_symbol = Qstop;
|
|
570 p->exit_code = WSTOPSIG (w);
|
|
571 p->core_dumped = 0;
|
|
572 }
|
|
573 else if (WIFEXITED (w))
|
|
574 {
|
|
575 p->status_symbol = Qexit;
|
|
576 p->exit_code = WEXITSTATUS (w);
|
|
577 p->core_dumped = 0;
|
|
578 }
|
|
579 else if (WIFSIGNALED (w))
|
|
580 {
|
|
581 p->status_symbol = Qsignal;
|
|
582 p->exit_code = WTERMSIG (w);
|
|
583 p->core_dumped = WCOREDUMP (w);
|
|
584 }
|
|
585 else
|
|
586 {
|
|
587 p->status_symbol = Qrun;
|
|
588 p->exit_code = 0;
|
|
589 }
|
|
590 }
|
|
591
|
|
592 #ifdef SIGCHLD
|
|
593
|
|
594 #define MAX_EXITED_PROCESSES 1000
|
|
595 static volatile pid_t exited_processes[MAX_EXITED_PROCESSES];
|
|
596 static volatile int exited_processes_status[MAX_EXITED_PROCESSES];
|
|
597 static volatile int exited_processes_index;
|
|
598
|
|
599 static volatile int sigchld_happened;
|
|
600
|
|
601 /* On receipt of a signal that a child status has changed,
|
|
602 loop asking about children with changed statuses until
|
|
603 the system says there are no more. All we do is record
|
|
604 the processes and wait status.
|
|
605
|
|
606 This function could be called from within the SIGCHLD
|
|
607 handler, so it must be completely reentrant. When
|
|
608 not called from a SIGCHLD handler, BLOCK_SIGCHLD should
|
|
609 be non-zero so that SIGCHLD is blocked while this
|
|
610 function is running. (This is necessary so avoid
|
|
611 race conditions with the SIGCHLD_HAPPENED flag). */
|
|
612
|
|
613 static void
|
|
614 record_exited_processes (int block_sigchld)
|
|
615 {
|
|
616 if (!sigchld_happened)
|
|
617 {
|
|
618 return;
|
|
619 }
|
|
620
|
|
621 #ifdef EMACS_BLOCK_SIGNAL
|
|
622 if (block_sigchld)
|
|
623 EMACS_BLOCK_SIGNAL (SIGCHLD);
|
|
624 #endif
|
|
625
|
|
626 while (sigchld_happened)
|
|
627 {
|
|
628 int pid;
|
|
629 int w;
|
|
630
|
|
631 /* Keep trying to get a status until we get a definitive result. */
|
|
632 do
|
|
633 {
|
|
634 errno = 0;
|
|
635 #ifdef WNOHANG
|
|
636 # ifndef WUNTRACED
|
|
637 # define WUNTRACED 0
|
|
638 # endif /* not WUNTRACED */
|
|
639 # ifdef HAVE_WAITPID
|
|
640 pid = waitpid ((pid_t) -1, &w, WNOHANG | WUNTRACED);
|
|
641 # else
|
|
642 pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
|
|
643 # endif
|
|
644 #else /* not WNOHANG */
|
|
645 pid = wait (&w);
|
|
646 #endif /* not WNOHANG */
|
|
647 }
|
|
648 while (pid <= 0 && errno == EINTR);
|
|
649
|
|
650 if (pid <= 0)
|
|
651 break;
|
|
652
|
|
653 if (exited_processes_index < MAX_EXITED_PROCESSES)
|
|
654 {
|
|
655 exited_processes[exited_processes_index] = pid;
|
|
656 exited_processes_status[exited_processes_index] = w;
|
|
657 exited_processes_index++;
|
|
658 }
|
|
659
|
|
660 /* On systems with WNOHANG, we just ignore the number
|
|
661 of times that SIGCHLD was signalled, and keep looping
|
|
662 until there are no more processes to wait on. If we
|
|
663 don't have WNOHANG, we have to rely on the count in
|
|
664 SIGCHLD_HAPPENED. */
|
|
665 #ifndef WNOHANG
|
|
666 sigchld_happened--;
|
|
667 #endif /* not WNOHANG */
|
|
668 }
|
|
669
|
|
670 sigchld_happened = 0;
|
|
671
|
|
672 if (block_sigchld)
|
|
673 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
|
|
674 }
|
|
675
|
|
676 /* For any processes that have changed status and are recorded
|
440
|
677 and such, update the corresponding Lisp_Process.
|
428
|
678 We separate this from record_exited_processes() so that
|
|
679 we never have to call this function from within a signal
|
|
680 handler. We block SIGCHLD in case record_exited_processes()
|
|
681 is called from a signal handler. */
|
|
682
|
|
683 /** USG WARNING: Although it is not obvious from the documentation
|
|
684 in signal(2), on a USG system the SIGCLD handler MUST NOT call
|
|
685 signal() before executing at least one wait(), otherwise the handler
|
|
686 will be called again, resulting in an infinite loop. The relevant
|
|
687 portion of the documentation reads "SIGCLD signals will be queued
|
|
688 and the signal-catching function will be continually reentered until
|
|
689 the queue is empty". Invoking signal() causes the kernel to reexamine
|
|
690 the SIGCLD queue. Fred Fish, UniSoft Systems Inc.
|
|
691
|
|
692 (Note that now this only applies in SYS V Release 2 and before.
|
|
693 On SYS V Release 3, we use sigset() to set the signal handler for
|
|
694 the first time, and so we don't have to reestablish the signal handler
|
|
695 in the handler below. On SYS V Release 4, we don't get this weirdo
|
|
696 behavior when we use sigaction(), which we do use.) */
|
|
697
|
|
698 static SIGTYPE
|
|
699 sigchld_handler (int signo)
|
|
700 {
|
|
701 #ifdef OBNOXIOUS_SYSV_SIGCLD_BEHAVIOR
|
|
702 int old_errno = errno;
|
|
703
|
|
704 sigchld_happened++;
|
|
705 record_exited_processes (0);
|
|
706 errno = old_errno;
|
|
707 #else
|
|
708 sigchld_happened++;
|
|
709 #endif
|
|
710 #ifdef HAVE_UNIXOID_EVENT_LOOP
|
|
711 signal_fake_event ();
|
|
712 #endif
|
|
713 /* WARNING - must come after wait3() for USG systems */
|
|
714 EMACS_REESTABLISH_SIGNAL (signo, sigchld_handler);
|
|
715 SIGRETURN;
|
|
716 }
|
|
717
|
|
718 #endif /* SIGCHLD */
|
|
719
|
|
720 #ifdef SIGNALS_VIA_CHARACTERS
|
|
721 /* Get signal character to send to process if SIGNALS_VIA_CHARACTERS */
|
|
722
|
|
723 static int
|
|
724 process_signal_char (int tty_fd, int signo)
|
|
725 {
|
|
726 /* If it's not a tty, pray that these default values work */
|
853
|
727 if (! isatty (tty_fd))
|
|
728 {
|
428
|
729 #define CNTL(ch) (037 & (ch))
|
853
|
730 switch (signo)
|
|
731 {
|
|
732 case SIGINT: return CNTL ('C');
|
|
733 case SIGQUIT: return CNTL ('\\');
|
428
|
734 #ifdef SIGTSTP
|
853
|
735 case SIGTSTP: return CNTL ('Z');
|
428
|
736 #endif
|
853
|
737 }
|
|
738 }
|
428
|
739
|
|
740 #ifdef HAVE_TERMIOS
|
|
741 /* TERMIOS is the latest and bestest, and seems most likely to work.
|
|
742 If the system has it, use it. */
|
|
743 {
|
|
744 struct termios t;
|
|
745 tcgetattr (tty_fd, &t);
|
|
746 switch (signo)
|
|
747 {
|
|
748 case SIGINT: return t.c_cc[VINTR];
|
|
749 case SIGQUIT: return t.c_cc[VQUIT];
|
|
750 #if defined(SIGTSTP) && defined(VSUSP)
|
|
751 case SIGTSTP: return t.c_cc[VSUSP];
|
|
752 #endif
|
|
753 }
|
|
754 }
|
|
755
|
|
756 # elif defined (TIOCGLTC) && defined (TIOCGETC) /* not HAVE_TERMIOS */
|
|
757 {
|
|
758 /* On Berkeley descendants, the following IOCTL's retrieve the
|
|
759 current control characters. */
|
|
760 struct tchars c;
|
|
761 struct ltchars lc;
|
|
762 switch (signo)
|
|
763 {
|
|
764 case SIGINT: ioctl (tty_fd, TIOCGETC, &c); return c.t_intrc;
|
|
765 case SIGQUIT: ioctl (tty_fd, TIOCGETC, &c); return c.t_quitc;
|
|
766 # ifdef SIGTSTP
|
|
767 case SIGTSTP: ioctl (tty_fd, TIOCGLTC, &lc); return lc.t_suspc;
|
|
768 # endif /* SIGTSTP */
|
|
769 }
|
|
770 }
|
|
771
|
|
772 # elif defined (TCGETA) /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
|
|
773 {
|
|
774 /* On SYSV descendants, the TCGETA ioctl retrieves the current
|
|
775 control characters. */
|
|
776 struct termio t;
|
|
777 ioctl (tty_fd, TCGETA, &t);
|
|
778 switch (signo) {
|
|
779 case SIGINT: return t.c_cc[VINTR];
|
|
780 case SIGQUIT: return t.c_cc[VQUIT];
|
|
781 # ifdef SIGTSTP
|
|
782 case SIGTSTP: return t.c_cc[VSWTCH];
|
|
783 # endif /* SIGTSTP */
|
|
784 }
|
|
785 }
|
|
786 # else /* ! defined (TCGETA) */
|
|
787 #error ERROR! Using SIGNALS_VIA_CHARACTERS, but not HAVE_TERMIOS || (TIOCGLTC && TIOCGETC) || TCGETA
|
|
788 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
|
|
789 you'd better be using one of the alternatives above! */
|
|
790 # endif /* ! defined (TCGETA) */
|
|
791 return '\0';
|
|
792 }
|
|
793 #endif /* SIGNALS_VIA_CHARACTERS */
|
|
794
|
|
795
|
|
796
|
|
797
|
|
798 /**********************************************************************/
|
|
799 /* Process implementation methods */
|
|
800 /**********************************************************************/
|
|
801
|
|
802 /*
|
|
803 * Allocate and initialize Lisp_Process->process_data
|
|
804 */
|
|
805
|
|
806 static void
|
440
|
807 unix_alloc_process_data (Lisp_Process *p)
|
428
|
808 {
|
|
809 p->process_data = xnew (struct unix_process_data);
|
|
810
|
1204
|
811 UNIX_DATA (p)->connected_via_filedesc_p = 0;
|
|
812 UNIX_DATA (p)->infd = -1;
|
|
813 UNIX_DATA (p)->errfd = -1;
|
|
814 UNIX_DATA (p)->subtty = -1;
|
|
815 UNIX_DATA (p)->pty_flag = 0;
|
428
|
816 }
|
|
817
|
|
818 /*
|
|
819 * Initialize XEmacs process implementation once
|
|
820 */
|
|
821
|
|
822 #ifdef SIGCHLD
|
|
823 static void
|
|
824 unix_init_process (void)
|
|
825 {
|
|
826 if (! noninteractive || initialized)
|
613
|
827 EMACS_SIGNAL (SIGCHLD, sigchld_handler);
|
428
|
828 }
|
|
829 #endif /* SIGCHLD */
|
|
830
|
|
831 /*
|
|
832 * Initialize any process local data. This is called when newly
|
|
833 * created process is connected to real OS file handles. The
|
|
834 * handles are generally represented by void* type, but are
|
442
|
835 * of type int (file descriptors) for UNIX.
|
428
|
836 */
|
|
837
|
|
838 static void
|
853
|
839 unix_init_process_io_handles (Lisp_Process *p, void *in, void *out, void *err,
|
|
840 int flags)
|
|
841 {
|
|
842 UNIX_DATA(p)->infd = (int) in;
|
|
843 UNIX_DATA(p)->errfd = (int) err;
|
|
844 }
|
|
845
|
|
846 /* Move the file descriptor FD so that its number is not less than MIN. *
|
|
847 The original file descriptor remains open. */
|
|
848 static int
|
|
849 relocate_fd (int fd, int min)
|
|
850 {
|
|
851 if (fd >= min)
|
|
852 return fd;
|
|
853 else
|
|
854 {
|
|
855 int newfd = dup (fd);
|
|
856 if (newfd == -1)
|
|
857 {
|
867
|
858 Ibyte *errmess;
|
853
|
859 GET_STRERROR (errmess, errno);
|
|
860 stderr_out ("Error while setting up child: %s\n", errmess);
|
|
861 _exit (1);
|
|
862 }
|
|
863 return relocate_fd (newfd, min);
|
|
864 }
|
|
865 }
|
|
866
|
|
867 /* This is the last thing run in a newly forked inferior process.
|
|
868 Copy descriptors IN, OUT and ERR
|
|
869 as descriptors STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO.
|
|
870 Initialize inferior's priority, pgrp, connected dir and environment.
|
|
871 then exec another program based on new_argv.
|
|
872
|
|
873 XEmacs: We've removed the SET_PGRP argument because it's already
|
|
874 done by the callers of child_setup.
|
|
875
|
|
876 CURRENT_DIR is an elisp string giving the path of the current
|
|
877 directory the subprocess should have. Since we can't really signal
|
|
878 a decent error from within the child (#### not quite correct in
|
|
879 XEmacs?), this should be verified as an executable directory by the
|
|
880 parent. */
|
|
881
|
|
882 static void
|
867
|
883 child_setup (int in, int out, int err, Ibyte **new_argv,
|
853
|
884 Lisp_Object current_dir)
|
428
|
885 {
|
867
|
886 Ibyte **env;
|
|
887 Ibyte *pwd;
|
853
|
888
|
|
889 #ifdef SET_EMACS_PRIORITY
|
|
890 if (emacs_priority != 0)
|
|
891 nice (- emacs_priority);
|
|
892 #endif
|
|
893
|
|
894 #if !defined (NO_SUBPROCESSES)
|
|
895 /* Close Emacs's descriptors that this process should not have. */
|
|
896 close_process_descs ();
|
|
897 #endif /* not NO_SUBPROCESSES */
|
|
898 close_load_descs ();
|
|
899
|
|
900 /* [[Note that use of alloca is always safe here. It's obvious for systems
|
|
901 that do not have true vfork or that have true (stack) alloca.
|
|
902 If using vfork and C_ALLOCA it is safe because that changes
|
|
903 the superior's static variables as if the superior had done alloca
|
|
904 and will be cleaned up in the usual way.]] -- irrelevant because
|
|
905 XEmacs does not use vfork. */
|
|
906 {
|
|
907 REGISTER Bytecount i;
|
|
908
|
|
909 i = XSTRING_LENGTH (current_dir);
|
867
|
910 pwd = alloca_array (Ibyte, i + 6);
|
853
|
911 memcpy (pwd, "PWD=", 4);
|
|
912 memcpy (pwd + 4, XSTRING_DATA (current_dir), i);
|
|
913 i += 4;
|
|
914 if (!IS_DIRECTORY_SEP (pwd[i - 1]))
|
|
915 pwd[i++] = DIRECTORY_SEP;
|
|
916 pwd[i] = 0;
|
|
917
|
|
918 /* [[We can't signal an Elisp error here; we're in a vfork. Since
|
|
919 the callers check the current directory before forking, this
|
|
920 should only return an error if the directory's permissions
|
|
921 are changed between the check and this chdir, but we should
|
|
922 at least check.]] -- irrelevant because XEmacs does not use vfork. */
|
|
923 if (qxe_chdir (pwd + 4) < 0)
|
|
924 {
|
|
925 /* Don't report the chdir error, or ange-ftp.el doesn't work. */
|
|
926 /* (FSFmacs does _exit (errno) here.) */
|
|
927 pwd = 0;
|
|
928 }
|
|
929 else
|
|
930 {
|
|
931 /* Strip trailing "/". Cretinous *[]&@$#^%@#$% Un*x */
|
|
932 /* leave "//" (from FSF) */
|
|
933 while (i > 6 && IS_DIRECTORY_SEP (pwd[i - 1]))
|
|
934 pwd[--i] = 0;
|
|
935 }
|
|
936 }
|
|
937
|
|
938 /* Set `env' to a vector of the strings in Vprocess_environment. */
|
|
939 /* + 2 to include PWD and terminating 0. */
|
867
|
940 env = alloca_array (Ibyte *, XINT (Flength (Vprocess_environment)) + 2);
|
853
|
941 {
|
|
942 REGISTER Lisp_Object tail;
|
867
|
943 Ibyte **new_env = env;
|
853
|
944
|
|
945 /* If we have a PWD envvar and we know the real current directory,
|
|
946 pass one down, but with corrected value. */
|
|
947 if (pwd && egetenv ("PWD"))
|
|
948 *new_env++ = pwd;
|
|
949
|
|
950 /* Copy the Vprocess_environment strings into new_env. */
|
|
951 for (tail = Vprocess_environment;
|
|
952 CONSP (tail) && STRINGP (XCAR (tail));
|
|
953 tail = XCDR (tail))
|
|
954 {
|
867
|
955 Ibyte **ep = env;
|
|
956 Ibyte *envvar = XSTRING_DATA (XCAR (tail));
|
853
|
957
|
|
958 /* See if envvar duplicates any string already in the env.
|
|
959 If so, don't put it in.
|
|
960 When an env var has multiple definitions,
|
|
961 we keep the definition that comes first in process-environment. */
|
|
962 for (; ep != new_env; ep++)
|
|
963 {
|
867
|
964 Ibyte *p = *ep, *q = envvar;
|
853
|
965 while (1)
|
|
966 {
|
|
967 if (*q == 0)
|
|
968 /* The string is malformed; might as well drop it. */
|
|
969 goto duplicate;
|
|
970 if (*q != *p)
|
|
971 break;
|
|
972 if (*q == '=')
|
|
973 goto duplicate;
|
|
974 p++, q++;
|
|
975 }
|
|
976 }
|
867
|
977 if (pwd && !qxestrncmp ((Ibyte *) "PWD=", envvar, 4))
|
853
|
978 {
|
|
979 *new_env++ = pwd;
|
|
980 pwd = 0;
|
|
981 }
|
|
982 else
|
|
983 *new_env++ = envvar;
|
|
984
|
|
985 duplicate: ;
|
|
986 }
|
|
987
|
|
988 *new_env = 0;
|
|
989 }
|
|
990
|
|
991 /* Make sure that in, out, and err are not actually already in
|
|
992 descriptors zero, one, or two; this could happen if Emacs is
|
|
993 started with its standard in, out, or error closed, as might
|
|
994 happen under X. */
|
|
995 in = relocate_fd (in, 3);
|
|
996 out = relocate_fd (out, 3);
|
|
997 err = relocate_fd (err, 3);
|
|
998
|
|
999 /* Set the standard input/output channels of the new process. */
|
|
1000 retry_close (STDIN_FILENO);
|
|
1001 retry_close (STDOUT_FILENO);
|
|
1002 retry_close (STDERR_FILENO);
|
|
1003
|
|
1004 dup2 (in, STDIN_FILENO);
|
|
1005 dup2 (out, STDOUT_FILENO);
|
|
1006 dup2 (err, STDERR_FILENO);
|
|
1007
|
|
1008 retry_close (in);
|
|
1009 retry_close (out);
|
|
1010 retry_close (err);
|
|
1011
|
1015
|
1012 /* Close non-process-related file descriptors. It would be cleaner to
|
932
|
1013 close just the ones that need to be, but the following brute
|
1015
|
1014 force approach is certainly effective, and not too slow. */
|
932
|
1015
|
|
1016 {
|
|
1017 int fd;
|
1015
|
1018
|
|
1019 for (fd = 3; fd < MAXDESC; fd++)
|
932
|
1020 retry_close (fd);
|
|
1021 }
|
|
1022
|
853
|
1023 /* we've wrapped execve; it translates its arguments */
|
|
1024 qxe_execve (new_argv[0], new_argv, env);
|
|
1025
|
|
1026 stdout_out ("Can't exec program %s\n", new_argv[0]);
|
|
1027 _exit (1);
|
428
|
1028 }
|
|
1029
|
|
1030 /*
|
|
1031 * Fork off a subprocess. P is a pointer to a newly created subprocess
|
|
1032 * object. If this function signals, the caller is responsible for
|
|
1033 * deleting (and finalizing) the process object.
|
|
1034 *
|
|
1035 * The method must return PID of the new process, a (positive??? ####) number
|
|
1036 * which fits into Lisp_Int. No return value indicates an error, the method
|
|
1037 * must signal an error instead.
|
|
1038 */
|
|
1039
|
|
1040 static int
|
440
|
1041 unix_create_process (Lisp_Process *p,
|
428
|
1042 Lisp_Object *argv, int nargv,
|
853
|
1043 Lisp_Object program, Lisp_Object cur_dir,
|
|
1044 int separate_err)
|
428
|
1045 {
|
|
1046 int pid;
|
|
1047 int inchannel = -1;
|
|
1048 int outchannel = -1;
|
853
|
1049 int errchannel = -1;
|
428
|
1050 /* Use volatile to protect variables from being clobbered by longjmp. */
|
|
1051 volatile int forkin = -1;
|
|
1052 volatile int forkout = -1;
|
853
|
1053 volatile int forkerr = -1;
|
428
|
1054 volatile int pty_flag = 0;
|
|
1055
|
|
1056 if (!NILP (Vprocess_connection_type))
|
|
1057 {
|
|
1058 /* find a new pty, open the master side, return the opened
|
|
1059 file handle, and store the name of the corresponding slave
|
|
1060 side in global variable pty_name. */
|
|
1061 outchannel = inchannel = allocate_pty ();
|
|
1062 }
|
|
1063
|
535
|
1064 if (inchannel >= 0) /* We successfully allocated a pty. */
|
428
|
1065 {
|
|
1066 /* You're "supposed" to now open the slave in the child.
|
|
1067 On some systems, we can open it here; this allows for
|
|
1068 better error checking. */
|
|
1069 #if !defined(USG)
|
|
1070 /* On USG systems it does not work to open the pty's tty here
|
|
1071 and then close and reopen it in the child. */
|
853
|
1072 # ifdef O_NOCTTY
|
428
|
1073 /* Don't let this terminal become our controlling terminal
|
|
1074 (in case we don't have one). */
|
771
|
1075 forkout = forkin = qxe_open (pty_name,
|
|
1076 O_RDWR | O_NOCTTY | OPEN_BINARY, 0);
|
853
|
1077 # else
|
771
|
1078 forkout = forkin = qxe_open (pty_name, O_RDWR | OPEN_BINARY, 0);
|
853
|
1079 # endif
|
428
|
1080 if (forkin < 0)
|
|
1081 goto io_failure;
|
|
1082 #endif /* not USG */
|
853
|
1083 UNIX_DATA (p)->pty_flag = pty_flag = 1;
|
428
|
1084 }
|
|
1085 else
|
|
1086 if (create_bidirectional_pipe (&inchannel, &outchannel,
|
|
1087 &forkin, &forkout) < 0)
|
|
1088 goto io_failure;
|
|
1089
|
853
|
1090 if (separate_err)
|
|
1091 {
|
|
1092 int sv[2];
|
854
|
1093
|
853
|
1094 if (pipe (sv) < 0)
|
|
1095 goto io_failure;
|
|
1096 forkerr = sv[1];
|
|
1097 errchannel = sv[0];
|
|
1098 }
|
854
|
1099
|
428
|
1100 #if 0
|
|
1101 /* Replaced by close_process_descs */
|
|
1102 set_exclusive_use (inchannel);
|
|
1103 set_exclusive_use (outchannel);
|
|
1104 #endif
|
|
1105
|
|
1106 set_descriptor_non_blocking (inchannel);
|
1192
|
1107 set_descriptor_non_blocking (outchannel);
|
853
|
1108 if (errchannel >= 0)
|
|
1109 set_descriptor_non_blocking (errchannel);
|
428
|
1110
|
|
1111 /* Record this as an active process, with its channels.
|
|
1112 As a result, child_setup will close Emacs's side of the pipes. */
|
853
|
1113 init_process_io_handles (p, (void *) inchannel, (void *) outchannel,
|
|
1114 (void *) errchannel,
|
428
|
1115 pty_flag ? STREAM_PTY_FLUSHING : 0);
|
|
1116 /* Record the tty descriptor used in the subprocess. */
|
853
|
1117 UNIX_DATA (p)->subtty = forkin;
|
428
|
1118
|
|
1119 {
|
|
1120 pid = fork ();
|
|
1121 if (pid == 0)
|
|
1122 {
|
|
1123 /**** Now we're in the child process ****/
|
|
1124 int xforkin = forkin;
|
|
1125 int xforkout = forkout;
|
853
|
1126 int xforkerr = forkerr;
|
428
|
1127
|
1015
|
1128 /* Checking for quit in the child is bad because that will
|
|
1129 cause I/O, and that, in turn, can confuse the X connection. */
|
|
1130 begin_dont_check_for_quit();
|
|
1131
|
442
|
1132 /* Disconnect the current controlling terminal, pursuant to
|
|
1133 making the pty be the controlling terminal of the process.
|
|
1134 Also put us in our own process group. */
|
|
1135
|
|
1136 disconnect_controlling_terminal ();
|
|
1137
|
|
1138 if (pty_flag)
|
428
|
1139 {
|
|
1140 /* Open the pty connection and make the pty's terminal
|
|
1141 our controlling terminal.
|
|
1142
|
|
1143 On systems with TIOCSCTTY, we just use it to set
|
|
1144 the controlling terminal. On other systems, the
|
|
1145 first TTY we open becomes the controlling terminal.
|
|
1146 So, we end up with four possibilities:
|
|
1147
|
|
1148 (1) on USG and TIOCSCTTY systems, we open the pty
|
|
1149 and use TIOCSCTTY.
|
|
1150 (2) on other USG systems, we just open the pty.
|
|
1151 (3) on non-USG systems with TIOCSCTTY, we
|
|
1152 just use TIOCSCTTY. (On non-USG systems, we
|
|
1153 already opened the pty in the parent process.)
|
|
1154 (4) on non-USG systems without TIOCSCTTY, we
|
|
1155 close the pty and reopen it.
|
|
1156
|
|
1157 This would be cleaner if we didn't open the pty
|
|
1158 in the parent process, but doing it that way
|
|
1159 makes it possible to trap error conditions.
|
|
1160 It's harder to convey an error from the child
|
|
1161 process, and I don't feel like messing with
|
|
1162 this now. */
|
|
1163
|
|
1164 /* There was some weirdo, probably wrong,
|
|
1165 conditionalization on RTU and UNIPLUS here.
|
|
1166 I deleted it. So sue me. */
|
|
1167
|
|
1168 /* SunOS has TIOCSCTTY but the close/open method
|
|
1169 also works. */
|
|
1170
|
853
|
1171 #if defined (USG) || !defined (TIOCSCTTY)
|
428
|
1172 /* Now close the pty (if we had it open) and reopen it.
|
|
1173 This makes the pty the controlling terminal of the
|
|
1174 subprocess. */
|
853
|
1175 /* I wonder if retry_close (qxe_open (pty_name, ...)) would
|
|
1176 work? */
|
428
|
1177 if (xforkin >= 0)
|
771
|
1178 retry_close (xforkin);
|
|
1179 xforkout = xforkin = qxe_open (pty_name, O_RDWR | OPEN_BINARY, 0);
|
428
|
1180 if (xforkin < 0)
|
|
1181 {
|
771
|
1182 retry_write (1, "Couldn't open the pty terminal ", 31);
|
|
1183 retry_write (1, pty_name, qxestrlen (pty_name));
|
|
1184 retry_write (1, "\n", 1);
|
428
|
1185 _exit (1);
|
|
1186 }
|
853
|
1187 #endif /* USG or not TIOCSCTTY */
|
428
|
1188
|
|
1189 /* Miscellaneous setup required for some systems.
|
|
1190 Must be done before using tc* functions on xforkin.
|
|
1191 This guarantees that isatty(xforkin) is true. */
|
|
1192
|
853
|
1193 #if defined (HAVE_ISASTREAM) && defined (I_PUSH)
|
442
|
1194 if (isastream (xforkin))
|
|
1195 {
|
853
|
1196 # if defined (I_FIND)
|
|
1197 # define stream_module_pushed(fd, module) (ioctl (fd, I_FIND, module) == 1)
|
|
1198 # else
|
|
1199 # define stream_module_pushed(fd, module) 0
|
|
1200 # endif
|
442
|
1201 if (! stream_module_pushed (xforkin, "ptem"))
|
|
1202 ioctl (xforkin, I_PUSH, "ptem");
|
|
1203 if (! stream_module_pushed (xforkin, "ldterm"))
|
|
1204 ioctl (xforkin, I_PUSH, "ldterm");
|
|
1205 if (! stream_module_pushed (xforkin, "ttcompat"))
|
|
1206 ioctl (xforkin, I_PUSH, "ttcompat");
|
|
1207 }
|
853
|
1208 #endif /* defined (HAVE_ISASTREAM) && defined (I_PUSH) */
|
428
|
1209
|
853
|
1210 #ifdef TIOCSCTTY
|
428
|
1211 /* We ignore the return value
|
|
1212 because faith@cs.unc.edu says that is necessary on Linux. */
|
|
1213 assert (isatty (xforkin));
|
|
1214 ioctl (xforkin, TIOCSCTTY, 0);
|
853
|
1215 #endif /* TIOCSCTTY */
|
428
|
1216
|
|
1217 /* Change the line discipline. */
|
|
1218
|
853
|
1219 #if defined (HAVE_TERMIOS) && defined (LDISC1)
|
428
|
1220 {
|
|
1221 struct termios t;
|
|
1222 assert (isatty (xforkin));
|
|
1223 tcgetattr (xforkin, &t);
|
|
1224 t.c_lflag = LDISC1;
|
|
1225 if (tcsetattr (xforkin, TCSANOW, &t) < 0)
|
|
1226 perror ("create_process/tcsetattr LDISC1 failed\n");
|
|
1227 }
|
853
|
1228 #elif defined (NTTYDISC) && defined (TIOCSETD)
|
428
|
1229 {
|
|
1230 /* Use new line discipline. TIOCSETD is accepted and
|
|
1231 ignored on Sys5.4 systems with ttcompat. */
|
|
1232 int ldisc = NTTYDISC;
|
|
1233 assert (isatty (xforkin));
|
|
1234 ioctl (xforkin, TIOCSETD, &ldisc);
|
|
1235 }
|
853
|
1236 #endif /* TIOCSETD & NTTYDISC */
|
428
|
1237
|
|
1238 /* Make our process group be the foreground group
|
|
1239 of our new controlling terminal. */
|
|
1240
|
|
1241 {
|
442
|
1242 pid_t piddly = EMACS_GET_PROCESS_GROUP ();
|
428
|
1243 EMACS_SET_TTY_PROCESS_GROUP (xforkin, &piddly);
|
|
1244 }
|
|
1245
|
|
1246 /* On AIX, we've disabled SIGHUP above once we start a
|
|
1247 child on a pty. Now reenable it in the child, so it
|
|
1248 will die when we want it to.
|
|
1249 JV: This needs to be done ALWAYS as we might have inherited
|
|
1250 a SIG_IGN handling from our parent (nohup) and we are in new
|
|
1251 process group.
|
|
1252 */
|
613
|
1253 EMACS_SIGNAL (SIGHUP, SIG_DFL);
|
428
|
1254
|
535
|
1255 /* Set up the terminal characteristics of the pty. */
|
|
1256 child_setup_tty (xforkout);
|
|
1257 } /* if (pty_flag) */
|
428
|
1258
|
|
1259
|
613
|
1260 EMACS_SIGNAL (SIGINT, SIG_DFL);
|
|
1261 EMACS_SIGNAL (SIGQUIT, SIG_DFL);
|
428
|
1262
|
|
1263 {
|
867
|
1264 Ibyte **new_argv = alloca_array (Ibyte *, nargv + 2);
|
428
|
1265 int i;
|
|
1266
|
|
1267 /* Nothing below here GCs so our string pointers shouldn't move. */
|
771
|
1268 new_argv[0] = XSTRING_DATA (program);
|
428
|
1269 for (i = 0; i < nargv; i++)
|
|
1270 {
|
|
1271 CHECK_STRING (argv[i]);
|
771
|
1272 new_argv[i + 1] = XSTRING_DATA (argv[i]);
|
428
|
1273 }
|
|
1274 new_argv[i + 1] = 0;
|
|
1275
|
853
|
1276 child_setup (xforkin, xforkout, separate_err ? xforkerr : xforkout,
|
|
1277 new_argv, cur_dir);
|
428
|
1278 }
|
|
1279
|
|
1280 } /**** End of child code ****/
|
|
1281
|
|
1282 /**** Back in parent process ****/
|
|
1283 }
|
|
1284
|
|
1285 if (pid < 0)
|
|
1286 {
|
853
|
1287 /* Note: The caller set up an unwind-protect to automatically delete
|
|
1288 the process if we fail. This will correctly deselect and close
|
|
1289 inchannel, outchannel, and errchannel. */
|
442
|
1290 int save_errno = errno;
|
428
|
1291 close_descriptor_pair (forkin, forkout);
|
853
|
1292 if (separate_err)
|
|
1293 retry_close (forkerr);
|
442
|
1294 errno = save_errno;
|
563
|
1295 report_process_error ("Doing fork", Qunbound);
|
428
|
1296 }
|
|
1297
|
|
1298 /* #### dmoore - why is this commented out, otherwise we leave
|
|
1299 subtty = forkin, but then we close forkin just below. */
|
853
|
1300 /* UNIX_DATA (p)->subtty = -1; */
|
428
|
1301
|
|
1302 /* If the subfork execv fails, and it exits,
|
|
1303 this close hangs. I don't know why.
|
|
1304 So have an interrupt jar it loose. */
|
|
1305 if (forkin >= 0)
|
|
1306 close_safely (forkin);
|
|
1307 if (forkin != forkout && forkout >= 0)
|
771
|
1308 retry_close (forkout);
|
853
|
1309 if (separate_err)
|
|
1310 retry_close (forkerr);
|
428
|
1311
|
1204
|
1312 p->tty_name = pty_flag ? build_intstring (pty_name) : Qnil;
|
428
|
1313
|
|
1314 /* Notice that SIGCHLD was not blocked. (This is not possible on
|
|
1315 some systems.) No biggie if SIGCHLD occurs right around the
|
|
1316 time that this call happens, because SIGCHLD() does not actually
|
|
1317 deselect the process (that doesn't occur until the next time
|
|
1318 we're waiting for an event, when status_notify() is called). */
|
|
1319 return pid;
|
|
1320
|
853
|
1321 io_failure:
|
428
|
1322 {
|
|
1323 int save_errno = errno;
|
|
1324 close_descriptor_pair (forkin, forkout);
|
|
1325 close_descriptor_pair (inchannel, outchannel);
|
853
|
1326 close_descriptor_pair (forkerr, errchannel);
|
428
|
1327 errno = save_errno;
|
563
|
1328 report_process_error ("Opening pty or pipe", Qunbound);
|
1204
|
1329 RETURN_NOT_REACHED (0);
|
428
|
1330 }
|
|
1331 }
|
|
1332
|
|
1333 /* Return nonzero if this process is a ToolTalk connection. */
|
|
1334
|
|
1335 static int
|
440
|
1336 unix_tooltalk_connection_p (Lisp_Process *p)
|
428
|
1337 {
|
853
|
1338 return UNIX_DATA (p)->connected_via_filedesc_p;
|
428
|
1339 }
|
|
1340
|
|
1341 /* This is called to set process' virtual terminal size */
|
|
1342
|
|
1343 static int
|
853
|
1344 unix_set_window_size (Lisp_Process *p, int cols, int rows)
|
428
|
1345 {
|
853
|
1346 return set_window_size (UNIX_DATA (p)->infd, cols, rows);
|
428
|
1347 }
|
|
1348
|
|
1349 /*
|
|
1350 * This method is called to update status fields of the process
|
|
1351 * structure. If the process has not existed, this method is
|
|
1352 * expected to do nothing.
|
|
1353 *
|
|
1354 * The method is called only for real child processes.
|
|
1355 */
|
|
1356
|
|
1357 #ifdef HAVE_WAITPID
|
|
1358 static void
|
853
|
1359 unix_update_status_if_terminated (Lisp_Process *p)
|
428
|
1360 {
|
|
1361 int w;
|
|
1362 #ifdef SIGCHLD
|
|
1363 EMACS_BLOCK_SIGNAL (SIGCHLD);
|
|
1364 #endif
|
|
1365 if (waitpid (XINT (p->pid), &w, WNOHANG) == XINT (p->pid))
|
|
1366 {
|
|
1367 p->tick++;
|
|
1368 update_status_from_wait_code (p, &w);
|
|
1369 }
|
|
1370 #ifdef SIGCHLD
|
|
1371 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
|
|
1372 #endif
|
|
1373 }
|
|
1374 #endif
|
|
1375
|
|
1376 /*
|
|
1377 * Update status of all exited processes. Called when SIGCLD has signaled.
|
|
1378 */
|
|
1379
|
|
1380 #ifdef SIGCHLD
|
|
1381 static void
|
|
1382 unix_reap_exited_processes (void)
|
|
1383 {
|
|
1384 int i;
|
440
|
1385 Lisp_Process *p;
|
428
|
1386
|
|
1387 #ifndef OBNOXIOUS_SYSV_SIGCLD_BEHAVIOR
|
|
1388 record_exited_processes (1);
|
|
1389 #endif
|
|
1390
|
|
1391 if (exited_processes_index <= 0)
|
|
1392 {
|
|
1393 return;
|
|
1394 }
|
|
1395
|
853
|
1396 #ifdef EMACS_BLOCK_SIGNAL
|
428
|
1397 EMACS_BLOCK_SIGNAL (SIGCHLD);
|
|
1398 #endif
|
|
1399 for (i = 0; i < exited_processes_index; i++)
|
|
1400 {
|
|
1401 int pid = exited_processes[i];
|
|
1402 int w = exited_processes_status[i];
|
|
1403
|
|
1404 /* Find the process that signaled us, and record its status. */
|
|
1405
|
|
1406 p = 0;
|
|
1407 {
|
|
1408 Lisp_Object tail;
|
|
1409 LIST_LOOP (tail, Vprocess_list)
|
|
1410 {
|
|
1411 Lisp_Object proc = XCAR (tail);
|
|
1412 p = XPROCESS (proc);
|
|
1413 if (INTP (p->pid) && XINT (p->pid) == pid)
|
|
1414 break;
|
|
1415 p = 0;
|
|
1416 }
|
|
1417 }
|
|
1418
|
|
1419 if (p)
|
|
1420 {
|
|
1421 /* Change the status of the process that was found. */
|
|
1422 p->tick++;
|
|
1423 process_tick++;
|
|
1424 update_status_from_wait_code (p, &w);
|
|
1425
|
|
1426 /* If process has terminated, stop waiting for its output. */
|
|
1427 if (WIFSIGNALED (w) || WIFEXITED (w))
|
|
1428 {
|
853
|
1429 if (!NILP (p->pipe_instream))
|
428
|
1430 {
|
|
1431 /* We can't just call event_stream->unselect_process_cb (p)
|
|
1432 here, because that calls XtRemoveInput, which is not
|
|
1433 necessarily reentrant, so we can't call this at interrupt
|
|
1434 level.
|
|
1435 */
|
|
1436 }
|
|
1437 }
|
|
1438 }
|
853
|
1439 #ifdef NEED_SYNC_PROCESS_CODE
|
428
|
1440 else
|
|
1441 {
|
|
1442 /* There was no asynchronous process found for that id. Check
|
|
1443 if we have a synchronous process. Only set sync process status
|
|
1444 if there is one, so we work OK with the waitpid() call in
|
|
1445 wait_for_termination(). */
|
|
1446 if (synch_process_alive != 0)
|
|
1447 { /* Set the global sync process status variables. */
|
|
1448 synch_process_alive = 0;
|
|
1449
|
|
1450 /* Report the status of the synchronous process. */
|
|
1451 if (WIFEXITED (w))
|
|
1452 synch_process_retcode = WEXITSTATUS (w);
|
|
1453 else if (WIFSIGNALED (w))
|
|
1454 synch_process_death = signal_name (WTERMSIG (w));
|
|
1455 }
|
|
1456 }
|
853
|
1457 #endif /* NEED_SYNC_PROCESS_CODE */
|
428
|
1458 }
|
|
1459
|
|
1460 exited_processes_index = 0;
|
|
1461
|
|
1462 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
|
|
1463 }
|
|
1464 #endif /* SIGCHLD */
|
|
1465
|
|
1466 /*
|
|
1467 * Stuff the entire contents of LSTREAM to the process output pipe
|
|
1468 */
|
|
1469
|
|
1470 static JMP_BUF send_process_frame;
|
|
1471
|
|
1472 static SIGTYPE
|
|
1473 send_process_trap (int signum)
|
|
1474 {
|
|
1475 EMACS_REESTABLISH_SIGNAL (signum, send_process_trap);
|
|
1476 EMACS_UNBLOCK_SIGNAL (signum);
|
|
1477 LONGJMP (send_process_frame, 1);
|
|
1478 }
|
|
1479
|
|
1480 static void
|
853
|
1481 unix_send_process (Lisp_Object proc, struct lstream *lstream)
|
428
|
1482 {
|
1111
|
1483 /* See comment lisp.h circa line 787 */
|
|
1484 SIGTYPE (*VOLATILE_IF_NOT_CPP old_sigpipe) (int) = 0;
|
|
1485 VOLATILE_IF_NOT_CPP Lisp_Object vol_proc = proc;
|
|
1486 Lisp_Process *VOLATILE_IF_NOT_CPP p = XPROCESS (proc);
|
428
|
1487
|
442
|
1488 /* #### JV: layering violation?
|
|
1489
|
|
1490 This function knows too much about the relation between the encoding
|
|
1491 stream (DATA_OUTSTREAM) and the actual output stream p->output_stream.
|
|
1492
|
|
1493 If encoding streams properly forwarded all calls, we could simply
|
|
1494 use DATA_OUTSTREAM everywhere. */
|
|
1495
|
428
|
1496 if (!SETJMP (send_process_frame))
|
|
1497 {
|
|
1498 /* use a reasonable-sized buffer (somewhere around the size of the
|
|
1499 stream buffer) so as to avoid inundating the stream with blocked
|
|
1500 data. */
|
867
|
1501 Ibyte chunkbuf[512];
|
428
|
1502 Bytecount chunklen;
|
|
1503
|
|
1504 while (1)
|
|
1505 {
|
771
|
1506 int writeret;
|
428
|
1507
|
|
1508 chunklen = Lstream_read (lstream, chunkbuf, 512);
|
|
1509 if (chunklen <= 0)
|
|
1510 break; /* perhaps should abort() if < 0?
|
|
1511 This should never happen. */
|
|
1512 old_sigpipe =
|
613
|
1513 (SIGTYPE (*) (int)) EMACS_SIGNAL (SIGPIPE, send_process_trap);
|
428
|
1514 /* Lstream_write() will never successfully write less than
|
|
1515 the amount sent in. In the worst case, it just buffers
|
|
1516 the unwritten data. */
|
800
|
1517 writeret = Lstream_write (XLSTREAM (DATA_OUTSTREAM (p)), chunkbuf,
|
428
|
1518 chunklen);
|
563
|
1519 {
|
|
1520 int save_errno = errno;
|
613
|
1521 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
563
|
1522 errno = save_errno;
|
|
1523 if (writeret < 0)
|
|
1524 /* This is a real error. Blocking errors are handled
|
|
1525 specially inside of the filedesc stream. */
|
|
1526 report_process_error ("writing to process", proc);
|
|
1527 }
|
428
|
1528 while (Lstream_was_blocked_p (XLSTREAM (p->pipe_outstream)))
|
|
1529 {
|
|
1530 /* Buffer is full. Wait, accepting input;
|
|
1531 that may allow the program
|
|
1532 to finish doing output and read more. */
|
1111
|
1533 Faccept_process_output (Qnil, make_int (1), Qnil);
|
442
|
1534 /* It could have *really* finished, deleting the process */
|
|
1535 if (NILP(p->pipe_outstream))
|
|
1536 return;
|
428
|
1537 old_sigpipe =
|
613
|
1538 (SIGTYPE (*) (int)) EMACS_SIGNAL (SIGPIPE, send_process_trap);
|
428
|
1539 Lstream_flush (XLSTREAM (p->pipe_outstream));
|
613
|
1540 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
1541 }
|
|
1542 }
|
|
1543 }
|
|
1544 else
|
|
1545 { /* We got here from a longjmp() from the SIGPIPE handler */
|
613
|
1546 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
1547 /* Close the file lstream so we don't attempt to write to it further */
|
|
1548 /* #### There is controversy over whether this might cause fd leakage */
|
|
1549 /* my tests say no. -slb */
|
|
1550 XLSTREAM (p->pipe_outstream)->flags &= ~LSTREAM_FL_IS_OPEN;
|
898
|
1551 XLSTREAM (p->coding_outstream)->flags &= ~LSTREAM_FL_IS_OPEN;
|
428
|
1552 p->status_symbol = Qexit;
|
|
1553 p->exit_code = 256; /* #### SIGPIPE ??? */
|
|
1554 p->core_dumped = 0;
|
|
1555 p->tick++;
|
|
1556 process_tick++;
|
898
|
1557 deactivate_process (vol_proc);
|
442
|
1558 invalid_operation ("SIGPIPE raised on process; closed it", p->name);
|
428
|
1559 }
|
|
1560
|
613
|
1561 old_sigpipe = (SIGTYPE (*) (int)) EMACS_SIGNAL (SIGPIPE, send_process_trap);
|
800
|
1562 Lstream_flush (XLSTREAM (DATA_OUTSTREAM (p)));
|
613
|
1563 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
1564 }
|
|
1565
|
|
1566 /*
|
|
1567 * Send EOF to the process. The default implementation simply
|
|
1568 * closes the output stream. The method must return 0 to call
|
|
1569 * the default implementation, or 1 if it has taken all care about
|
|
1570 * sending EOF to the process.
|
|
1571 */
|
|
1572
|
|
1573 static int
|
|
1574 unix_process_send_eof (Lisp_Object proc)
|
|
1575 {
|
|
1576 if (!UNIX_DATA (XPROCESS (proc))->pty_flag)
|
|
1577 return 0;
|
|
1578
|
|
1579 /* #### get_eof_char simply doesn't return the correct character
|
|
1580 here. Maybe it is needed to determine the right eof
|
|
1581 character in init_process_io_handles but here it simply screws
|
|
1582 things up. */
|
|
1583 #if 0
|
867
|
1584 Ibyte eof_char = get_eof_char (XPROCESS (proc));
|
428
|
1585 send_process (proc, Qnil, &eof_char, 0, 1);
|
|
1586 #else
|
867
|
1587 send_process (proc, Qnil, (const Ibyte *) "\004", 0, 1);
|
428
|
1588 #endif
|
|
1589 return 1;
|
|
1590 }
|
|
1591
|
|
1592 /*
|
|
1593 * Called before the process is deactivated. The process object
|
|
1594 * is not immediately finalized, just undergoes a transition to
|
|
1595 * inactive state.
|
|
1596 *
|
|
1597 * The return value is a unique stream ID, as returned by
|
853
|
1598 * event_stream_delete_io_streams
|
428
|
1599 *
|
853
|
1600 * In the lack of this method, only event_stream_delete_io_streams
|
428
|
1601 * is called on both I/O streams of the process.
|
|
1602 *
|
|
1603 * The UNIX version guards this by ignoring possible SIGPIPE.
|
|
1604 */
|
|
1605
|
853
|
1606 static void
|
|
1607 unix_deactivate_process (Lisp_Process *p,
|
|
1608 USID *in_usid,
|
|
1609 USID *err_usid)
|
428
|
1610 {
|
|
1611 SIGTYPE (*old_sigpipe) (int) = 0;
|
|
1612
|
|
1613 if (UNIX_DATA(p)->infd >= 0)
|
|
1614 flush_pending_output (UNIX_DATA(p)->infd);
|
853
|
1615 if (UNIX_DATA(p)->errfd >= 0)
|
|
1616 flush_pending_output (UNIX_DATA(p)->errfd);
|
428
|
1617
|
|
1618 /* closing the outstream could result in SIGPIPE, so ignore it. */
|
613
|
1619 old_sigpipe = (SIGTYPE (*) (int)) EMACS_SIGNAL (SIGPIPE, SIG_IGN);
|
853
|
1620 event_stream_delete_io_streams (p->pipe_instream, p->pipe_outstream,
|
|
1621 p->pipe_errstream, in_usid, err_usid);
|
613
|
1622 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
1623
|
|
1624 UNIX_DATA(p)->infd = -1;
|
853
|
1625 UNIX_DATA(p)->errfd = -1;
|
428
|
1626 }
|
|
1627
|
442
|
1628 /* If the subtty field of the process data is not filled in, do so now. */
|
|
1629 static void
|
1204
|
1630 try_to_initialize_subtty (Lisp_Process *p)
|
442
|
1631 {
|
1204
|
1632 struct unix_process_data *upd = UNIX_DATA (p);
|
442
|
1633 if (upd->pty_flag
|
444
|
1634 && (upd->subtty == -1 || ! isatty (upd->subtty))
|
1204
|
1635 && STRINGP (p->tty_name))
|
|
1636 upd->subtty = qxe_open (XSTRING_DATA (p->tty_name), O_RDWR, 0);
|
442
|
1637 }
|
|
1638
|
|
1639 /* Send signal number SIGNO to PROCESS.
|
428
|
1640 CURRENT_GROUP means send to the process group that currently owns
|
|
1641 the terminal being used to communicate with PROCESS.
|
|
1642 This is used for various commands in shell mode.
|
|
1643 If NOMSG is zero, insert signal-announcements into process's buffers
|
|
1644 right away.
|
|
1645
|
|
1646 If we can, we try to signal PROCESS by sending control characters
|
|
1647 down the pty. This allows us to signal inferiors who have changed
|
442
|
1648 their uid, for which killpg would return an EPERM error,
|
|
1649 or processes running on other machines via remote login.
|
428
|
1650
|
442
|
1651 The method signals an error if the given SIGNO is not valid. */
|
428
|
1652
|
|
1653 static void
|
|
1654 unix_kill_child_process (Lisp_Object proc, int signo,
|
|
1655 int current_group, int nomsg)
|
|
1656 {
|
442
|
1657 pid_t pgid = -1;
|
440
|
1658 Lisp_Process *p = XPROCESS (proc);
|
442
|
1659 struct unix_process_data *d = UNIX_DATA (p);
|
428
|
1660
|
|
1661 switch (signo)
|
|
1662 {
|
|
1663 #ifdef SIGCONT
|
|
1664 case SIGCONT:
|
|
1665 p->status_symbol = Qrun;
|
|
1666 p->exit_code = 0;
|
|
1667 p->tick++;
|
|
1668 process_tick++;
|
|
1669 if (!nomsg)
|
|
1670 status_notify ();
|
|
1671 break;
|
|
1672 #endif /* ! defined (SIGCONT) */
|
|
1673 case SIGINT:
|
|
1674 case SIGQUIT:
|
|
1675 case SIGKILL:
|
442
|
1676 flush_pending_output (d->infd);
|
853
|
1677 flush_pending_output (d->errfd);
|
428
|
1678 break;
|
|
1679 }
|
|
1680
|
442
|
1681 if (! d->pty_flag)
|
|
1682 current_group = 0;
|
|
1683
|
|
1684 /* If current_group is true, we want to send a signal to the
|
|
1685 foreground process group of the terminal our child process is
|
|
1686 running on. You would think that would be easy.
|
|
1687
|
|
1688 The BSD people invented the TIOCPGRP ioctl to get the foreground
|
|
1689 process group of a tty. That, combined with killpg, gives us
|
|
1690 what we want.
|
|
1691
|
|
1692 However, the POSIX standards people, in their infinite wisdom,
|
|
1693 have seen fit to only allow this for processes which have the
|
|
1694 terminal as controlling terminal, which doesn't apply to us.
|
|
1695
|
|
1696 Sooo..., we have to do something non-standard. The ioctls
|
|
1697 TIOCSIGNAL, TIOCSIG, and TIOCSIGSEND send the signal directly on
|
|
1698 many systems. POSIX tcgetpgrp(), since it is *documented* as not
|
|
1699 doing what we want, is actually less likely to work than the BSD
|
|
1700 ioctl TIOCGPGRP it is supposed to obsolete. Sometimes we have to
|
|
1701 use TIOCGPGRP on the master end, sometimes the slave end
|
|
1702 (probably an AIX bug). So we better get a fd for the slave if we
|
444
|
1703 haven't got it yet.
|
|
1704
|
|
1705 Anal operating systems like SGI Irix and Compaq Tru64 adhere
|
|
1706 strictly to the letter of the law, so our hack doesn't work.
|
|
1707 The following fragment from an Irix header file is suggestive:
|
|
1708
|
|
1709 #ifdef __notdef__
|
|
1710 // this is not currently supported
|
|
1711 #define TIOCSIGNAL (tIOC|31) // pty: send signal to slave
|
|
1712 #endif
|
|
1713
|
|
1714 On those systems where none of our tricks work, we just fall back
|
|
1715 to the non-current_group behavior and kill the process group of
|
|
1716 the child.
|
|
1717 */
|
442
|
1718 if (current_group)
|
428
|
1719 {
|
1204
|
1720 try_to_initialize_subtty (p);
|
442
|
1721
|
|
1722 #ifdef SIGNALS_VIA_CHARACTERS
|
|
1723 /* If possible, send signals to the entire pgrp
|
|
1724 by sending an input character to it. */
|
|
1725 {
|
867
|
1726 Ibyte sigchar = process_signal_char (d->subtty, signo);
|
442
|
1727 if (sigchar)
|
|
1728 {
|
853
|
1729 send_process (proc, Qnil, &sigchar, 0, 1);
|
442
|
1730 return;
|
|
1731 }
|
|
1732 }
|
|
1733 #endif /* SIGNALS_VIA_CHARACTERS */
|
|
1734
|
|
1735 #ifdef TIOCGPGRP
|
|
1736 if (pgid == -1)
|
|
1737 ioctl (d->infd, TIOCGPGRP, &pgid); /* BSD */
|
|
1738 if (pgid == -1 && d->subtty != -1)
|
|
1739 ioctl (d->subtty, TIOCGPGRP, &pgid); /* Only this works on AIX! */
|
|
1740 #endif /* TIOCGPGRP */
|
|
1741
|
|
1742 if (pgid == -1)
|
428
|
1743 {
|
442
|
1744 /* Many systems provide an ioctl to send a signal directly */
|
|
1745 #ifdef TIOCSIGNAL /* Solaris, HP-UX */
|
|
1746 if (ioctl (d->infd, TIOCSIGNAL, signo) != -1)
|
|
1747 return;
|
|
1748 #endif /* TIOCSIGNAL */
|
|
1749
|
|
1750 #ifdef TIOCSIG /* BSD */
|
|
1751 if (ioctl (d->infd, TIOCSIG, signo) != -1)
|
|
1752 return;
|
|
1753 #endif /* TIOCSIG */
|
428
|
1754 }
|
442
|
1755 } /* current_group */
|
428
|
1756
|
442
|
1757 if (pgid == -1)
|
|
1758 /* Either current_group is 0, or we failed to get the foreground
|
|
1759 process group using the trickery above. So we fall back to
|
|
1760 sending the signal to the process group of our child process.
|
|
1761 Since this is often a shell that ignores signals like SIGINT,
|
|
1762 the shell's subprocess is killed, which is the desired effect.
|
|
1763 The process group of p->pid is always p->pid, since it was
|
|
1764 created as a process group leader. */
|
|
1765 pgid = XINT (p->pid);
|
|
1766
|
|
1767 /* Finally send the signal. */
|
|
1768 if (EMACS_KILLPG (pgid, signo) == -1)
|
458
|
1769 {
|
|
1770 /* It's not an error if our victim is already dead.
|
462
|
1771 And we can't rely on the result of killing a zombie, since
|
|
1772 XPG 4.2 requires that killing a zombie fail with ESRCH,
|
|
1773 while FIPS 151-2 requires that it succeeds! */
|
458
|
1774 #ifdef ESRCH
|
|
1775 if (errno != ESRCH)
|
|
1776 #endif
|
563
|
1777 signal_ferror_with_frob (Qio_error, lisp_strerror (errno),
|
|
1778 "kill (pgid=%ld, signo=%ld) failed",
|
|
1779 (long) pgid, (long) signo);
|
458
|
1780 }
|
428
|
1781 }
|
|
1782
|
442
|
1783 /* Send signal SIGCODE to any process in the system given its PID.
|
|
1784 Return zero if successful, a negative number upon failure. */
|
428
|
1785
|
|
1786 static int
|
|
1787 unix_kill_process_by_pid (int pid, int sigcode)
|
|
1788 {
|
|
1789 return kill (pid, sigcode);
|
|
1790 }
|
|
1791
|
442
|
1792 /* Canonicalize host name HOST, and return its canonical form.
|
|
1793 The default implementation just takes HOST for a canonical name. */
|
428
|
1794
|
|
1795 #ifdef HAVE_SOCKETS
|
|
1796 static Lisp_Object
|
|
1797 unix_canonicalize_host_name (Lisp_Object host)
|
|
1798 {
|
502
|
1799 #ifdef USE_GETADDRINFO
|
440
|
1800 struct addrinfo hints, *res;
|
|
1801 static char addrbuf[NI_MAXHOST];
|
|
1802 Lisp_Object canonname;
|
|
1803 int retval;
|
|
1804 char *ext_host;
|
|
1805
|
|
1806 xzero (hints);
|
|
1807 hints.ai_flags = AI_CANONNAME;
|
724
|
1808 #ifdef IPV6_CANONICALIZE
|
440
|
1809 hints.ai_family = AF_UNSPEC;
|
724
|
1810 #else
|
|
1811 hints.ai_family = PF_INET;
|
|
1812 #endif
|
440
|
1813 hints.ai_socktype = SOCK_STREAM;
|
|
1814 hints.ai_protocol = 0;
|
442
|
1815 LISP_STRING_TO_EXTERNAL (host, ext_host, Qnative);
|
440
|
1816 retval = getaddrinfo (ext_host, NULL, &hints, &res);
|
|
1817 if (retval != 0)
|
|
1818 {
|
867
|
1819 CIbyte *gai_err;
|
440
|
1820
|
855
|
1821 EXTERNAL_TO_C_STRING (gai_strerror (retval), gai_err,
|
771
|
1822 Qstrerror_encoding);
|
855
|
1823 maybe_signal_error (Qio_error, gai_err, host,
|
793
|
1824 Qprocess, ERROR_ME_DEBUG_WARN);
|
440
|
1825 canonname = host;
|
|
1826 }
|
|
1827 else
|
|
1828 {
|
|
1829 int gni = getnameinfo (res->ai_addr, res->ai_addrlen,
|
|
1830 addrbuf, sizeof(addrbuf),
|
|
1831 NULL, 0, NI_NUMERICHOST);
|
|
1832 canonname = gni ? host : build_ext_string (addrbuf, Qnative);
|
|
1833
|
|
1834 freeaddrinfo (res);
|
|
1835 }
|
|
1836
|
|
1837 return canonname;
|
502
|
1838 #else /* ! USE_GETADDRINFO */
|
428
|
1839 struct sockaddr_in address;
|
|
1840
|
|
1841 if (!get_internet_address (host, &address, ERROR_ME_NOT))
|
|
1842 return host;
|
|
1843
|
|
1844 if (address.sin_family == AF_INET)
|
|
1845 return build_string (inet_ntoa (address.sin_addr));
|
|
1846 else
|
|
1847 /* #### any clue what to do here? */
|
|
1848 return host;
|
502
|
1849 #endif /* ! USE_GETADDRINFO */
|
428
|
1850 }
|
|
1851
|
442
|
1852 /* Open a TCP network connection to a given HOST/SERVICE.
|
|
1853 Treated exactly like a normal process when reading and writing.
|
|
1854 Only differences are in status display and process deletion.
|
|
1855 A network connection has no PID; you cannot signal it. All you can
|
|
1856 do is deactivate and close it via delete-process. */
|
428
|
1857
|
|
1858 static void
|
502
|
1859 unix_open_network_stream (Lisp_Object name, Lisp_Object host,
|
|
1860 Lisp_Object service, Lisp_Object protocol,
|
|
1861 void **vinfd, void **voutfd)
|
428
|
1862 {
|
|
1863 int inch;
|
|
1864 int outch;
|
502
|
1865 volatile int s = -1;
|
428
|
1866 volatile int port;
|
|
1867 volatile int retry = 0;
|
502
|
1868 volatile int xerrno = 0;
|
|
1869 volatile int failed_connect = 0;
|
428
|
1870 int retval;
|
|
1871
|
|
1872 CHECK_STRING (host);
|
|
1873
|
|
1874 if (!EQ (protocol, Qtcp) && !EQ (protocol, Qudp))
|
563
|
1875 invalid_constant ("Unsupported protocol", protocol);
|
428
|
1876
|
440
|
1877 {
|
502
|
1878 #ifdef USE_GETADDRINFO
|
|
1879
|
440
|
1880 struct addrinfo hints, *res;
|
|
1881 struct addrinfo * volatile lres;
|
771
|
1882 Extbyte *portstring;
|
|
1883 Extbyte *ext_host;
|
|
1884 Extbyte portbuf[128];
|
440
|
1885 /*
|
|
1886 * Caution: service can either be a string or int.
|
|
1887 * Convert to a C string for later use by getaddrinfo.
|
|
1888 */
|
|
1889 if (INTP (service))
|
|
1890 {
|
|
1891 snprintf (portbuf, sizeof (portbuf), "%ld", (long) XINT (service));
|
|
1892 portstring = portbuf;
|
|
1893 port = htons ((unsigned short) XINT (service));
|
|
1894 }
|
|
1895 else
|
|
1896 {
|
|
1897 CHECK_STRING (service);
|
771
|
1898 LISP_STRING_TO_EXTERNAL (service, portstring,
|
|
1899 Qunix_service_name_encoding);
|
440
|
1900 port = 0;
|
|
1901 }
|
|
1902
|
|
1903 xzero (hints);
|
|
1904 hints.ai_flags = 0;
|
|
1905 hints.ai_family = AF_UNSPEC;
|
|
1906 if (EQ (protocol, Qtcp))
|
|
1907 hints.ai_socktype = SOCK_STREAM;
|
|
1908 else /* EQ (protocol, Qudp) */
|
|
1909 hints.ai_socktype = SOCK_DGRAM;
|
|
1910 hints.ai_protocol = 0;
|
771
|
1911 LISP_STRING_TO_EXTERNAL (host, ext_host, Qunix_host_name_encoding);
|
440
|
1912 retval = getaddrinfo (ext_host, portstring, &hints, &res);
|
|
1913 if (retval != 0)
|
|
1914 {
|
867
|
1915 CIbyte *gai_err;
|
440
|
1916
|
855
|
1917 EXTERNAL_TO_C_STRING (gai_strerror (retval), gai_err,
|
771
|
1918 Qstrerror_encoding);
|
855
|
1919 signal_error (Qio_error, gai_err, list2 (host, service));
|
440
|
1920 }
|
|
1921
|
|
1922 /* address loop */
|
|
1923 for (lres = res; lres ; lres = lres->ai_next)
|
|
1924
|
502
|
1925 #else /* !USE_GETADDRINFO */
|
440
|
1926
|
|
1927 struct sockaddr_in address;
|
502
|
1928 volatile int i;
|
440
|
1929
|
|
1930 if (INTP (service))
|
|
1931 port = htons ((unsigned short) XINT (service));
|
|
1932 else
|
|
1933 {
|
|
1934 struct servent *svc_info;
|
771
|
1935 Extbyte *servext;
|
|
1936
|
440
|
1937 CHECK_STRING (service);
|
771
|
1938 LISP_STRING_TO_EXTERNAL (service, servext,
|
|
1939 Qunix_service_name_encoding);
|
440
|
1940
|
|
1941 if (EQ (protocol, Qtcp))
|
771
|
1942 svc_info = getservbyname (servext, "tcp");
|
440
|
1943 else /* EQ (protocol, Qudp) */
|
771
|
1944 svc_info = getservbyname (servext, "udp");
|
428
|
1945
|
440
|
1946 if (svc_info == 0)
|
442
|
1947 invalid_argument ("Unknown service", service);
|
440
|
1948 port = svc_info->s_port;
|
|
1949 }
|
428
|
1950
|
440
|
1951 get_internet_address (host, &address, ERROR_ME);
|
|
1952 address.sin_port = port;
|
428
|
1953
|
502
|
1954 /* use a trivial address loop */
|
|
1955 for (i = 0; i < 1; i++)
|
|
1956
|
|
1957 #endif /* !USE_GETADDRINFO */
|
|
1958 {
|
|
1959 #ifdef USE_GETADDRINFO
|
|
1960 int family = lres->ai_family;
|
|
1961 #else
|
|
1962 int family = address.sin_family;
|
|
1963 #endif
|
|
1964
|
|
1965 if (EQ (protocol, Qtcp))
|
|
1966 s = socket (family, SOCK_STREAM, 0);
|
|
1967 else /* EQ (protocol, Qudp) */
|
|
1968 s = socket (family, SOCK_DGRAM, 0);
|
|
1969
|
|
1970 if (s < 0)
|
|
1971 {
|
|
1972 xerrno = errno;
|
|
1973 failed_connect = 0;
|
|
1974 continue;
|
|
1975 }
|
|
1976
|
|
1977 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
|
1978 /* Slow down polling. Some kernels have a bug which causes retrying
|
|
1979 connect to fail after a connect. (Note that the entire purpose
|
|
1980 for this code is a very old comment concerning an Ultrix bug that
|
|
1981 requires this code. We used to do this ALWAYS despite this!
|
|
1982 This messes up C-g out of connect() in a big way. So instead we
|
|
1983 just assume that anyone who sees such a kernel bug will define
|
|
1984 this constant, which for now is only defined under Ultrix.) --ben
|
|
1985 */
|
|
1986 slow_down_interrupts ();
|
|
1987 #endif
|
|
1988
|
|
1989 loop:
|
|
1990
|
|
1991 /* A system call interrupted with a SIGALRM or SIGIO comes back
|
|
1992 here, with can_break_system_calls reset to 0. */
|
|
1993 SETJMP (break_system_call_jump);
|
|
1994 if (QUITP)
|
|
1995 {
|
|
1996 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
|
1997 speed_up_interrupts ();
|
|
1998 #endif
|
853
|
1999 QUIT;
|
502
|
2000 /* In case something really weird happens ... */
|
|
2001 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
|
2002 slow_down_interrupts ();
|
|
2003 #endif
|
|
2004 }
|
|
2005
|
|
2006 /* Break out of connect with a signal (it isn't otherwise possible).
|
|
2007 Thus you don't get screwed with a hung network. */
|
|
2008 can_break_system_calls = 1;
|
|
2009
|
|
2010 #ifdef USE_GETADDRINFO
|
|
2011 retval = connect (s, lres->ai_addr, lres->ai_addrlen);
|
|
2012 #else
|
|
2013 retval = connect (s, (struct sockaddr *) &address, sizeof (address));
|
|
2014 #endif
|
|
2015 can_break_system_calls = 0;
|
|
2016 if (retval == -1 && errno != EISCONN)
|
|
2017 {
|
|
2018 xerrno = errno;
|
859
|
2019
|
|
2020 if (errno == EINTR || errno == EINPROGRESS || errno == EALREADY)
|
502
|
2021 goto loop;
|
|
2022 if (errno == EADDRINUSE && retry < 20)
|
|
2023 {
|
|
2024 #ifdef __FreeBSD__
|
|
2025 /* A delay here is needed on some FreeBSD systems,
|
|
2026 and it is harmless, since this retrying takes
|
|
2027 time anyway and should be infrequent.
|
|
2028 `sleep-for' allowed for quitting this loop with
|
|
2029 interrupts slowed down so it can't be used
|
|
2030 here. Async timers should already be disabled
|
|
2031 at this point so we can use `sleep'.
|
|
2032
|
|
2033 (Again, this was not conditionalized on FreeBSD.
|
854
|
2034 Let's not mess up systems without the problem. --ben)
|
502
|
2035 */
|
|
2036 sleep (1);
|
|
2037 #endif
|
|
2038 retry++;
|
|
2039 goto loop;
|
|
2040 }
|
|
2041
|
|
2042 failed_connect = 1;
|
771
|
2043 retry_close (s);
|
502
|
2044 s = -1;
|
|
2045
|
|
2046 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
|
2047 speed_up_interrupts ();
|
|
2048 #endif
|
|
2049
|
|
2050 continue;
|
|
2051 }
|
|
2052
|
|
2053 #ifdef USE_GETADDRINFO
|
|
2054 if (port == 0)
|
|
2055 {
|
|
2056 int gni;
|
|
2057 char servbuf[NI_MAXSERV];
|
|
2058
|
|
2059 if (EQ (protocol, Qtcp))
|
|
2060 gni = getnameinfo (lres->ai_addr, lres->ai_addrlen,
|
|
2061 NULL, 0, servbuf, sizeof(servbuf),
|
|
2062 NI_NUMERICSERV);
|
|
2063 else /* EQ (protocol, Qudp) */
|
|
2064 gni = getnameinfo (lres->ai_addr, lres->ai_addrlen,
|
|
2065 NULL, 0, servbuf, sizeof(servbuf),
|
|
2066 NI_NUMERICSERV | NI_DGRAM);
|
|
2067
|
|
2068 if (gni == 0)
|
|
2069 port = strtol (servbuf, NULL, 10);
|
|
2070 }
|
|
2071
|
|
2072 break;
|
|
2073 #endif /* USE_GETADDRINFO */
|
|
2074 } /* address loop */
|
|
2075
|
|
2076 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
|
2077 speed_up_interrupts ();
|
|
2078 #endif
|
|
2079
|
|
2080 #ifdef USE_GETADDRINFO
|
|
2081 freeaddrinfo (res);
|
|
2082 #endif
|
428
|
2083
|
440
|
2084 if (s < 0)
|
502
|
2085 {
|
|
2086 errno = xerrno;
|
428
|
2087
|
502
|
2088 if (failed_connect)
|
563
|
2089 report_network_error ("connection failed", list3 (Qunbound, host,
|
|
2090 name));
|
502
|
2091 else
|
563
|
2092 report_network_error ("error creating socket", name);
|
440
|
2093 }
|
|
2094 }
|
428
|
2095
|
|
2096 inch = s;
|
|
2097 outch = dup (s);
|
|
2098 if (outch < 0)
|
|
2099 {
|
563
|
2100 int save_errno = errno;
|
771
|
2101 retry_close (s); /* this used to be leaked; from Kyle Jones */
|
563
|
2102 errno = save_errno;
|
|
2103 report_network_error ("error duplicating socket", name);
|
428
|
2104 }
|
|
2105
|
|
2106 set_socket_nonblocking_maybe (inch, port, "tcp");
|
|
2107
|
502
|
2108 *vinfd = (void *) inch;
|
|
2109 *voutfd = (void *) outch;
|
428
|
2110 }
|
|
2111
|
|
2112
|
|
2113 #ifdef HAVE_MULTICAST
|
|
2114
|
442
|
2115 /* Didier Verna <didier@xemacs.org> Nov. 28 1997.
|
428
|
2116
|
|
2117 This function is similar to open-network-stream-internal, but provides a
|
|
2118 mean to open an UDP multicast connection instead of a TCP one. Like in the
|
|
2119 TCP case, the multicast connection will be seen as a sub-process,
|
|
2120
|
|
2121 Some notes:
|
|
2122 - Normally, we should use sendto and recvfrom with non connected
|
|
2123 sockets. The current code doesn't allow us to do this. In the future, it
|
|
2124 would be a good idea to extend the process data structure in order to deal
|
|
2125 properly with the different types network connections.
|
|
2126 - For the same reason, when leaving a multicast group, it is better to make
|
|
2127 a setsockopt - IP_DROP_MEMBERSHIP before closing the descriptors.
|
|
2128 Unfortunately, this can't be done here because delete_process doesn't know
|
|
2129 about the kind of connection we have. However, this is not such an
|
|
2130 important issue.
|
|
2131 */
|
|
2132
|
|
2133 static void
|
442
|
2134 unix_open_multicast_group (Lisp_Object name, Lisp_Object dest,
|
853
|
2135 Lisp_Object port, Lisp_Object ttl, void **vinfd,
|
|
2136 void **voutfd)
|
428
|
2137 {
|
|
2138 struct ip_mreq imr;
|
|
2139 struct sockaddr_in sa;
|
|
2140 struct protoent *udp;
|
|
2141 int ws, rs;
|
|
2142 int theport;
|
|
2143 unsigned char thettl;
|
|
2144 int one = 1; /* For REUSEADDR */
|
|
2145 int ret;
|
|
2146 volatile int retry = 0;
|
|
2147
|
|
2148 CHECK_STRING (dest);
|
|
2149
|
|
2150 CHECK_NATNUM (port);
|
|
2151 theport = htons ((unsigned short) XINT (port));
|
|
2152
|
|
2153 CHECK_NATNUM (ttl);
|
|
2154 thettl = (unsigned char) XINT (ttl);
|
|
2155
|
|
2156 if ((udp = getprotobyname ("udp")) == NULL)
|
563
|
2157 invalid_operation ("No info available for UDP protocol", Qunbound);
|
428
|
2158
|
|
2159 /* Init the sockets. Yes, I need 2 sockets. I couldn't duplicate one. */
|
|
2160 if ((rs = socket (PF_INET, SOCK_DGRAM, udp->p_proto)) < 0)
|
563
|
2161 report_network_error ("error creating socket", name);
|
428
|
2162 if ((ws = socket (PF_INET, SOCK_DGRAM, udp->p_proto)) < 0)
|
|
2163 {
|
563
|
2164 int save_errno = errno;
|
771
|
2165 retry_close (rs);
|
563
|
2166 errno = save_errno;
|
|
2167 report_network_error ("error creating socket", name);
|
428
|
2168 }
|
|
2169
|
|
2170 /* This will be used for both sockets */
|
|
2171 memset (&sa, 0, sizeof(sa));
|
|
2172 sa.sin_family = AF_INET;
|
|
2173 sa.sin_port = theport;
|
671
|
2174 sa.sin_addr.s_addr = inet_addr ((char *) XSTRING_DATA (dest));
|
428
|
2175
|
|
2176 /* Socket configuration for reading ------------------------ */
|
|
2177
|
|
2178 /* Multiple connections from the same machine. This must be done before
|
|
2179 bind. If it fails, it shouldn't be fatal. The only consequence is that
|
|
2180 people won't be able to connect twice from the same machine. */
|
|
2181 if (setsockopt (rs, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof (one))
|
|
2182 < 0)
|
|
2183 warn_when_safe (Qmulticast, Qwarning, "Cannot reuse socket address");
|
|
2184
|
|
2185 /* bind socket name */
|
|
2186 if (bind (rs, (struct sockaddr *)&sa, sizeof(sa)))
|
|
2187 {
|
563
|
2188 int save_errno = errno;
|
771
|
2189 retry_close (rs);
|
|
2190 retry_close (ws);
|
563
|
2191 errno = save_errno;
|
|
2192 report_network_error ("error binding socket", list3 (Qunbound, name,
|
|
2193 port));
|
428
|
2194 }
|
|
2195
|
|
2196 /* join multicast group */
|
671
|
2197 imr.imr_multiaddr.s_addr = inet_addr ((char *) XSTRING_DATA (dest));
|
428
|
2198 imr.imr_interface.s_addr = htonl (INADDR_ANY);
|
|
2199 if (setsockopt (rs, IPPROTO_IP, IP_ADD_MEMBERSHIP,
|
442
|
2200 &imr, sizeof (struct ip_mreq)) < 0)
|
428
|
2201 {
|
563
|
2202 int save_errno = errno;
|
771
|
2203 retry_close (ws);
|
|
2204 retry_close (rs);
|
563
|
2205 errno = save_errno;
|
|
2206 report_network_error ("error adding membership", list3 (Qunbound, name,
|
|
2207 dest));
|
428
|
2208 }
|
|
2209
|
|
2210 /* Socket configuration for writing ----------------------- */
|
|
2211
|
|
2212 /* Normally, there's no 'connect' in multicast, since we prefer to use
|
|
2213 'sendto' and 'recvfrom'. However, in order to handle this connection in
|
|
2214 the process-like way it is done for TCP, we must be able to use 'write'
|
|
2215 instead of 'sendto'. Consequently, we 'connect' this socket. */
|
|
2216
|
|
2217 /* See open-network-stream-internal for comments on this part of the code */
|
502
|
2218 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
428
|
2219 slow_down_interrupts ();
|
502
|
2220 #endif
|
428
|
2221
|
|
2222 loop:
|
|
2223
|
|
2224 /* A system call interrupted with a SIGALRM or SIGIO comes back
|
|
2225 here, with can_break_system_calls reset to 0. */
|
|
2226 SETJMP (break_system_call_jump);
|
|
2227 if (QUITP)
|
|
2228 {
|
502
|
2229 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
428
|
2230 speed_up_interrupts ();
|
502
|
2231 #endif
|
853
|
2232 QUIT;
|
428
|
2233 /* In case something really weird happens ... */
|
502
|
2234 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
428
|
2235 slow_down_interrupts ();
|
502
|
2236 #endif
|
428
|
2237 }
|
|
2238
|
|
2239 /* Break out of connect with a signal (it isn't otherwise possible).
|
|
2240 Thus you don't get screwed with a hung network. */
|
|
2241 can_break_system_calls = 1;
|
|
2242 ret = connect (ws, (struct sockaddr *) &sa, sizeof (sa));
|
|
2243 can_break_system_calls = 0;
|
|
2244 if (ret == -1 && errno != EISCONN)
|
|
2245 {
|
|
2246 int xerrno = errno;
|
|
2247
|
859
|
2248 if (errno == EINTR || errno == EINPROGRESS || errno == EALREADY)
|
428
|
2249 goto loop;
|
|
2250 if (errno == EADDRINUSE && retry < 20)
|
|
2251 {
|
859
|
2252 #ifdef __FreeBSD__
|
428
|
2253 /* A delay here is needed on some FreeBSD systems,
|
|
2254 and it is harmless, since this retrying takes time anyway
|
|
2255 and should be infrequent.
|
|
2256 `sleep-for' allowed for quitting this loop with interrupts
|
|
2257 slowed down so it can't be used here. Async timers should
|
|
2258 already be disabled at this point so we can use `sleep'. */
|
|
2259 sleep (1);
|
859
|
2260 #endif
|
428
|
2261 retry++;
|
|
2262 goto loop;
|
|
2263 }
|
|
2264
|
771
|
2265 retry_close (rs);
|
|
2266 retry_close (ws);
|
502
|
2267 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
428
|
2268 speed_up_interrupts ();
|
502
|
2269 #endif
|
428
|
2270
|
|
2271 errno = xerrno;
|
563
|
2272 report_network_error ("error connecting socket", list3 (Qunbound, name,
|
|
2273 port));
|
428
|
2274 }
|
|
2275
|
502
|
2276 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
428
|
2277 speed_up_interrupts ();
|
502
|
2278 #endif
|
428
|
2279
|
|
2280 /* scope */
|
|
2281 if (setsockopt (ws, IPPROTO_IP, IP_MULTICAST_TTL,
|
442
|
2282 &thettl, sizeof (thettl)) < 0)
|
428
|
2283 {
|
563
|
2284 int save_errno = errno;
|
771
|
2285 retry_close (rs);
|
|
2286 retry_close (ws);
|
563
|
2287 errno = save_errno;
|
|
2288 report_network_error ("error setting ttl", list3 (Qunbound, name, ttl));
|
428
|
2289 }
|
|
2290
|
|
2291 set_socket_nonblocking_maybe (rs, theport, "udp");
|
|
2292
|
|
2293 *vinfd = (void*)rs;
|
|
2294 *voutfd = (void*)ws;
|
|
2295 }
|
|
2296
|
|
2297 #endif /* HAVE_MULTICAST */
|
|
2298
|
|
2299 #endif /* HAVE_SOCKETS */
|
|
2300
|
|
2301
|
|
2302 /**********************************************************************/
|
|
2303 /* Initialization */
|
|
2304 /**********************************************************************/
|
|
2305
|
|
2306 void
|
|
2307 process_type_create_unix (void)
|
|
2308 {
|
|
2309 PROCESS_HAS_METHOD (unix, alloc_process_data);
|
|
2310 #ifdef SIGCHLD
|
|
2311 PROCESS_HAS_METHOD (unix, init_process);
|
|
2312 PROCESS_HAS_METHOD (unix, reap_exited_processes);
|
|
2313 #endif
|
|
2314 PROCESS_HAS_METHOD (unix, init_process_io_handles);
|
|
2315 PROCESS_HAS_METHOD (unix, create_process);
|
|
2316 PROCESS_HAS_METHOD (unix, tooltalk_connection_p);
|
|
2317 PROCESS_HAS_METHOD (unix, set_window_size);
|
|
2318 #ifdef HAVE_WAITPID
|
|
2319 PROCESS_HAS_METHOD (unix, update_status_if_terminated);
|
|
2320 #endif
|
|
2321 PROCESS_HAS_METHOD (unix, send_process);
|
|
2322 PROCESS_HAS_METHOD (unix, process_send_eof);
|
|
2323 PROCESS_HAS_METHOD (unix, deactivate_process);
|
|
2324 PROCESS_HAS_METHOD (unix, kill_child_process);
|
|
2325 PROCESS_HAS_METHOD (unix, kill_process_by_pid);
|
|
2326 #ifdef HAVE_SOCKETS
|
|
2327 PROCESS_HAS_METHOD (unix, canonicalize_host_name);
|
|
2328 PROCESS_HAS_METHOD (unix, open_network_stream);
|
|
2329 #ifdef HAVE_MULTICAST
|
|
2330 PROCESS_HAS_METHOD (unix, open_multicast_group);
|
|
2331 #endif
|
|
2332 #endif
|
|
2333 }
|
|
2334
|
|
2335 void
|
|
2336 vars_of_process_unix (void)
|
|
2337 {
|
|
2338 Fprovide (intern ("unix-processes"));
|
|
2339 }
|
|
2340
|
|
2341 #endif /* !defined (NO_SUBPROCESSES) */
|