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