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
|
|
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 #ifndef CANNOT_DUMP
|
|
827 if (! noninteractive || initialized)
|
|
828 #endif
|
613
|
829 EMACS_SIGNAL (SIGCHLD, sigchld_handler);
|
428
|
830 }
|
|
831 #endif /* SIGCHLD */
|
|
832
|
|
833 /*
|
|
834 * Initialize any process local data. This is called when newly
|
|
835 * created process is connected to real OS file handles. The
|
|
836 * handles are generally represented by void* type, but are
|
442
|
837 * of type int (file descriptors) for UNIX.
|
428
|
838 */
|
|
839
|
|
840 static void
|
853
|
841 unix_init_process_io_handles (Lisp_Process *p, void *in, void *out, void *err,
|
|
842 int flags)
|
|
843 {
|
|
844 UNIX_DATA(p)->infd = (int) in;
|
|
845 UNIX_DATA(p)->errfd = (int) err;
|
|
846 }
|
|
847
|
|
848 /* Move the file descriptor FD so that its number is not less than MIN. *
|
|
849 The original file descriptor remains open. */
|
|
850 static int
|
|
851 relocate_fd (int fd, int min)
|
|
852 {
|
|
853 if (fd >= min)
|
|
854 return fd;
|
|
855 else
|
|
856 {
|
|
857 int newfd = dup (fd);
|
|
858 if (newfd == -1)
|
|
859 {
|
867
|
860 Ibyte *errmess;
|
853
|
861 GET_STRERROR (errmess, errno);
|
|
862 stderr_out ("Error while setting up child: %s\n", errmess);
|
|
863 _exit (1);
|
|
864 }
|
|
865 return relocate_fd (newfd, min);
|
|
866 }
|
|
867 }
|
|
868
|
|
869 /* This is the last thing run in a newly forked inferior process.
|
|
870 Copy descriptors IN, OUT and ERR
|
|
871 as descriptors STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO.
|
|
872 Initialize inferior's priority, pgrp, connected dir and environment.
|
|
873 then exec another program based on new_argv.
|
|
874
|
|
875 XEmacs: We've removed the SET_PGRP argument because it's already
|
|
876 done by the callers of child_setup.
|
|
877
|
|
878 CURRENT_DIR is an elisp string giving the path of the current
|
|
879 directory the subprocess should have. Since we can't really signal
|
|
880 a decent error from within the child (#### not quite correct in
|
|
881 XEmacs?), this should be verified as an executable directory by the
|
|
882 parent. */
|
|
883
|
|
884 static void
|
867
|
885 child_setup (int in, int out, int err, Ibyte **new_argv,
|
853
|
886 Lisp_Object current_dir)
|
428
|
887 {
|
867
|
888 Ibyte **env;
|
|
889 Ibyte *pwd;
|
853
|
890
|
|
891 #ifdef SET_EMACS_PRIORITY
|
|
892 if (emacs_priority != 0)
|
|
893 nice (- emacs_priority);
|
|
894 #endif
|
|
895
|
|
896 #if !defined (NO_SUBPROCESSES)
|
|
897 /* Close Emacs's descriptors that this process should not have. */
|
|
898 close_process_descs ();
|
|
899 #endif /* not NO_SUBPROCESSES */
|
|
900 close_load_descs ();
|
|
901
|
|
902 /* [[Note that use of alloca is always safe here. It's obvious for systems
|
|
903 that do not have true vfork or that have true (stack) alloca.
|
|
904 If using vfork and C_ALLOCA it is safe because that changes
|
|
905 the superior's static variables as if the superior had done alloca
|
|
906 and will be cleaned up in the usual way.]] -- irrelevant because
|
|
907 XEmacs does not use vfork. */
|
|
908 {
|
|
909 REGISTER Bytecount i;
|
|
910
|
|
911 i = XSTRING_LENGTH (current_dir);
|
867
|
912 pwd = alloca_array (Ibyte, i + 6);
|
853
|
913 memcpy (pwd, "PWD=", 4);
|
|
914 memcpy (pwd + 4, XSTRING_DATA (current_dir), i);
|
|
915 i += 4;
|
|
916 if (!IS_DIRECTORY_SEP (pwd[i - 1]))
|
|
917 pwd[i++] = DIRECTORY_SEP;
|
|
918 pwd[i] = 0;
|
|
919
|
|
920 /* [[We can't signal an Elisp error here; we're in a vfork. Since
|
|
921 the callers check the current directory before forking, this
|
|
922 should only return an error if the directory's permissions
|
|
923 are changed between the check and this chdir, but we should
|
|
924 at least check.]] -- irrelevant because XEmacs does not use vfork. */
|
|
925 if (qxe_chdir (pwd + 4) < 0)
|
|
926 {
|
|
927 /* Don't report the chdir error, or ange-ftp.el doesn't work. */
|
|
928 /* (FSFmacs does _exit (errno) here.) */
|
|
929 pwd = 0;
|
|
930 }
|
|
931 else
|
|
932 {
|
|
933 /* Strip trailing "/". Cretinous *[]&@$#^%@#$% Un*x */
|
|
934 /* leave "//" (from FSF) */
|
|
935 while (i > 6 && IS_DIRECTORY_SEP (pwd[i - 1]))
|
|
936 pwd[--i] = 0;
|
|
937 }
|
|
938 }
|
|
939
|
|
940 /* Set `env' to a vector of the strings in Vprocess_environment. */
|
|
941 /* + 2 to include PWD and terminating 0. */
|
867
|
942 env = alloca_array (Ibyte *, XINT (Flength (Vprocess_environment)) + 2);
|
853
|
943 {
|
|
944 REGISTER Lisp_Object tail;
|
867
|
945 Ibyte **new_env = env;
|
853
|
946
|
|
947 /* If we have a PWD envvar and we know the real current directory,
|
|
948 pass one down, but with corrected value. */
|
|
949 if (pwd && egetenv ("PWD"))
|
|
950 *new_env++ = pwd;
|
|
951
|
|
952 /* Copy the Vprocess_environment strings into new_env. */
|
|
953 for (tail = Vprocess_environment;
|
|
954 CONSP (tail) && STRINGP (XCAR (tail));
|
|
955 tail = XCDR (tail))
|
|
956 {
|
867
|
957 Ibyte **ep = env;
|
|
958 Ibyte *envvar = XSTRING_DATA (XCAR (tail));
|
853
|
959
|
|
960 /* See if envvar duplicates any string already in the env.
|
|
961 If so, don't put it in.
|
|
962 When an env var has multiple definitions,
|
|
963 we keep the definition that comes first in process-environment. */
|
|
964 for (; ep != new_env; ep++)
|
|
965 {
|
867
|
966 Ibyte *p = *ep, *q = envvar;
|
853
|
967 while (1)
|
|
968 {
|
|
969 if (*q == 0)
|
|
970 /* The string is malformed; might as well drop it. */
|
|
971 goto duplicate;
|
|
972 if (*q != *p)
|
|
973 break;
|
|
974 if (*q == '=')
|
|
975 goto duplicate;
|
|
976 p++, q++;
|
|
977 }
|
|
978 }
|
867
|
979 if (pwd && !qxestrncmp ((Ibyte *) "PWD=", envvar, 4))
|
853
|
980 {
|
|
981 *new_env++ = pwd;
|
|
982 pwd = 0;
|
|
983 }
|
|
984 else
|
|
985 *new_env++ = envvar;
|
|
986
|
|
987 duplicate: ;
|
|
988 }
|
|
989
|
|
990 *new_env = 0;
|
|
991 }
|
|
992
|
|
993 /* Make sure that in, out, and err are not actually already in
|
|
994 descriptors zero, one, or two; this could happen if Emacs is
|
|
995 started with its standard in, out, or error closed, as might
|
|
996 happen under X. */
|
|
997 in = relocate_fd (in, 3);
|
|
998 out = relocate_fd (out, 3);
|
|
999 err = relocate_fd (err, 3);
|
|
1000
|
|
1001 /* Set the standard input/output channels of the new process. */
|
|
1002 retry_close (STDIN_FILENO);
|
|
1003 retry_close (STDOUT_FILENO);
|
|
1004 retry_close (STDERR_FILENO);
|
|
1005
|
|
1006 dup2 (in, STDIN_FILENO);
|
|
1007 dup2 (out, STDOUT_FILENO);
|
|
1008 dup2 (err, STDERR_FILENO);
|
|
1009
|
|
1010 retry_close (in);
|
|
1011 retry_close (out);
|
|
1012 retry_close (err);
|
|
1013
|
1015
|
1014 /* Close non-process-related file descriptors. It would be cleaner to
|
932
|
1015 close just the ones that need to be, but the following brute
|
1015
|
1016 force approach is certainly effective, and not too slow. */
|
932
|
1017
|
|
1018 {
|
|
1019 int fd;
|
1015
|
1020
|
|
1021 for (fd = 3; fd < MAXDESC; fd++)
|
932
|
1022 retry_close (fd);
|
|
1023 }
|
|
1024
|
853
|
1025 /* we've wrapped execve; it translates its arguments */
|
|
1026 qxe_execve (new_argv[0], new_argv, env);
|
|
1027
|
|
1028 stdout_out ("Can't exec program %s\n", new_argv[0]);
|
|
1029 _exit (1);
|
428
|
1030 }
|
|
1031
|
|
1032 /*
|
|
1033 * Fork off a subprocess. P is a pointer to a newly created subprocess
|
|
1034 * object. If this function signals, the caller is responsible for
|
|
1035 * deleting (and finalizing) the process object.
|
|
1036 *
|
|
1037 * The method must return PID of the new process, a (positive??? ####) number
|
|
1038 * which fits into Lisp_Int. No return value indicates an error, the method
|
|
1039 * must signal an error instead.
|
|
1040 */
|
|
1041
|
|
1042 static int
|
440
|
1043 unix_create_process (Lisp_Process *p,
|
428
|
1044 Lisp_Object *argv, int nargv,
|
853
|
1045 Lisp_Object program, Lisp_Object cur_dir,
|
|
1046 int separate_err)
|
428
|
1047 {
|
|
1048 int pid;
|
|
1049 int inchannel = -1;
|
|
1050 int outchannel = -1;
|
853
|
1051 int errchannel = -1;
|
428
|
1052 /* Use volatile to protect variables from being clobbered by longjmp. */
|
|
1053 volatile int forkin = -1;
|
|
1054 volatile int forkout = -1;
|
853
|
1055 volatile int forkerr = -1;
|
428
|
1056 volatile int pty_flag = 0;
|
|
1057
|
|
1058 if (!NILP (Vprocess_connection_type))
|
|
1059 {
|
|
1060 /* find a new pty, open the master side, return the opened
|
|
1061 file handle, and store the name of the corresponding slave
|
|
1062 side in global variable pty_name. */
|
|
1063 outchannel = inchannel = allocate_pty ();
|
|
1064 }
|
|
1065
|
535
|
1066 if (inchannel >= 0) /* We successfully allocated a pty. */
|
428
|
1067 {
|
|
1068 /* You're "supposed" to now open the slave in the child.
|
|
1069 On some systems, we can open it here; this allows for
|
|
1070 better error checking. */
|
|
1071 #if !defined(USG)
|
|
1072 /* On USG systems it does not work to open the pty's tty here
|
|
1073 and then close and reopen it in the child. */
|
853
|
1074 # ifdef O_NOCTTY
|
428
|
1075 /* Don't let this terminal become our controlling terminal
|
|
1076 (in case we don't have one). */
|
771
|
1077 forkout = forkin = qxe_open (pty_name,
|
|
1078 O_RDWR | O_NOCTTY | OPEN_BINARY, 0);
|
853
|
1079 # else
|
771
|
1080 forkout = forkin = qxe_open (pty_name, O_RDWR | OPEN_BINARY, 0);
|
853
|
1081 # endif
|
428
|
1082 if (forkin < 0)
|
|
1083 goto io_failure;
|
|
1084 #endif /* not USG */
|
853
|
1085 UNIX_DATA (p)->pty_flag = pty_flag = 1;
|
428
|
1086 }
|
|
1087 else
|
|
1088 if (create_bidirectional_pipe (&inchannel, &outchannel,
|
|
1089 &forkin, &forkout) < 0)
|
|
1090 goto io_failure;
|
|
1091
|
853
|
1092 if (separate_err)
|
|
1093 {
|
|
1094 int sv[2];
|
854
|
1095
|
853
|
1096 if (pipe (sv) < 0)
|
|
1097 goto io_failure;
|
|
1098 forkerr = sv[1];
|
|
1099 errchannel = sv[0];
|
|
1100 }
|
854
|
1101
|
428
|
1102 #if 0
|
|
1103 /* Replaced by close_process_descs */
|
|
1104 set_exclusive_use (inchannel);
|
|
1105 set_exclusive_use (outchannel);
|
|
1106 #endif
|
|
1107
|
|
1108 set_descriptor_non_blocking (inchannel);
|
1192
|
1109 set_descriptor_non_blocking (outchannel);
|
853
|
1110 if (errchannel >= 0)
|
|
1111 set_descriptor_non_blocking (errchannel);
|
428
|
1112
|
|
1113 /* Record this as an active process, with its channels.
|
|
1114 As a result, child_setup will close Emacs's side of the pipes. */
|
853
|
1115 init_process_io_handles (p, (void *) inchannel, (void *) outchannel,
|
|
1116 (void *) errchannel,
|
428
|
1117 pty_flag ? STREAM_PTY_FLUSHING : 0);
|
|
1118 /* Record the tty descriptor used in the subprocess. */
|
853
|
1119 UNIX_DATA (p)->subtty = forkin;
|
428
|
1120
|
|
1121 {
|
|
1122 pid = fork ();
|
|
1123 if (pid == 0)
|
|
1124 {
|
|
1125 /**** Now we're in the child process ****/
|
|
1126 int xforkin = forkin;
|
|
1127 int xforkout = forkout;
|
853
|
1128 int xforkerr = forkerr;
|
428
|
1129
|
1015
|
1130 /* Checking for quit in the child is bad because that will
|
|
1131 cause I/O, and that, in turn, can confuse the X connection. */
|
|
1132 begin_dont_check_for_quit();
|
|
1133
|
442
|
1134 /* Disconnect the current controlling terminal, pursuant to
|
|
1135 making the pty be the controlling terminal of the process.
|
|
1136 Also put us in our own process group. */
|
|
1137
|
|
1138 disconnect_controlling_terminal ();
|
|
1139
|
|
1140 if (pty_flag)
|
428
|
1141 {
|
|
1142 /* Open the pty connection and make the pty's terminal
|
|
1143 our controlling terminal.
|
|
1144
|
|
1145 On systems with TIOCSCTTY, we just use it to set
|
|
1146 the controlling terminal. On other systems, the
|
|
1147 first TTY we open becomes the controlling terminal.
|
|
1148 So, we end up with four possibilities:
|
|
1149
|
|
1150 (1) on USG and TIOCSCTTY systems, we open the pty
|
|
1151 and use TIOCSCTTY.
|
|
1152 (2) on other USG systems, we just open the pty.
|
|
1153 (3) on non-USG systems with TIOCSCTTY, we
|
|
1154 just use TIOCSCTTY. (On non-USG systems, we
|
|
1155 already opened the pty in the parent process.)
|
|
1156 (4) on non-USG systems without TIOCSCTTY, we
|
|
1157 close the pty and reopen it.
|
|
1158
|
|
1159 This would be cleaner if we didn't open the pty
|
|
1160 in the parent process, but doing it that way
|
|
1161 makes it possible to trap error conditions.
|
|
1162 It's harder to convey an error from the child
|
|
1163 process, and I don't feel like messing with
|
|
1164 this now. */
|
|
1165
|
|
1166 /* There was some weirdo, probably wrong,
|
|
1167 conditionalization on RTU and UNIPLUS here.
|
|
1168 I deleted it. So sue me. */
|
|
1169
|
|
1170 /* SunOS has TIOCSCTTY but the close/open method
|
|
1171 also works. */
|
|
1172
|
853
|
1173 #if defined (USG) || !defined (TIOCSCTTY)
|
428
|
1174 /* Now close the pty (if we had it open) and reopen it.
|
|
1175 This makes the pty the controlling terminal of the
|
|
1176 subprocess. */
|
853
|
1177 /* I wonder if retry_close (qxe_open (pty_name, ...)) would
|
|
1178 work? */
|
428
|
1179 if (xforkin >= 0)
|
771
|
1180 retry_close (xforkin);
|
|
1181 xforkout = xforkin = qxe_open (pty_name, O_RDWR | OPEN_BINARY, 0);
|
428
|
1182 if (xforkin < 0)
|
|
1183 {
|
771
|
1184 retry_write (1, "Couldn't open the pty terminal ", 31);
|
|
1185 retry_write (1, pty_name, qxestrlen (pty_name));
|
|
1186 retry_write (1, "\n", 1);
|
428
|
1187 _exit (1);
|
|
1188 }
|
853
|
1189 #endif /* USG or not TIOCSCTTY */
|
428
|
1190
|
|
1191 /* Miscellaneous setup required for some systems.
|
|
1192 Must be done before using tc* functions on xforkin.
|
|
1193 This guarantees that isatty(xforkin) is true. */
|
|
1194
|
853
|
1195 #if defined (HAVE_ISASTREAM) && defined (I_PUSH)
|
442
|
1196 if (isastream (xforkin))
|
|
1197 {
|
853
|
1198 # if defined (I_FIND)
|
|
1199 # define stream_module_pushed(fd, module) (ioctl (fd, I_FIND, module) == 1)
|
|
1200 # else
|
|
1201 # define stream_module_pushed(fd, module) 0
|
|
1202 # endif
|
442
|
1203 if (! stream_module_pushed (xforkin, "ptem"))
|
|
1204 ioctl (xforkin, I_PUSH, "ptem");
|
|
1205 if (! stream_module_pushed (xforkin, "ldterm"))
|
|
1206 ioctl (xforkin, I_PUSH, "ldterm");
|
|
1207 if (! stream_module_pushed (xforkin, "ttcompat"))
|
|
1208 ioctl (xforkin, I_PUSH, "ttcompat");
|
|
1209 }
|
853
|
1210 #endif /* defined (HAVE_ISASTREAM) && defined (I_PUSH) */
|
428
|
1211
|
853
|
1212 #ifdef TIOCSCTTY
|
428
|
1213 /* We ignore the return value
|
|
1214 because faith@cs.unc.edu says that is necessary on Linux. */
|
|
1215 assert (isatty (xforkin));
|
|
1216 ioctl (xforkin, TIOCSCTTY, 0);
|
853
|
1217 #endif /* TIOCSCTTY */
|
428
|
1218
|
|
1219 /* Change the line discipline. */
|
|
1220
|
853
|
1221 #if defined (HAVE_TERMIOS) && defined (LDISC1)
|
428
|
1222 {
|
|
1223 struct termios t;
|
|
1224 assert (isatty (xforkin));
|
|
1225 tcgetattr (xforkin, &t);
|
|
1226 t.c_lflag = LDISC1;
|
|
1227 if (tcsetattr (xforkin, TCSANOW, &t) < 0)
|
|
1228 perror ("create_process/tcsetattr LDISC1 failed\n");
|
|
1229 }
|
853
|
1230 #elif defined (NTTYDISC) && defined (TIOCSETD)
|
428
|
1231 {
|
|
1232 /* Use new line discipline. TIOCSETD is accepted and
|
|
1233 ignored on Sys5.4 systems with ttcompat. */
|
|
1234 int ldisc = NTTYDISC;
|
|
1235 assert (isatty (xforkin));
|
|
1236 ioctl (xforkin, TIOCSETD, &ldisc);
|
|
1237 }
|
853
|
1238 #endif /* TIOCSETD & NTTYDISC */
|
428
|
1239
|
|
1240 /* Make our process group be the foreground group
|
|
1241 of our new controlling terminal. */
|
|
1242
|
|
1243 {
|
442
|
1244 pid_t piddly = EMACS_GET_PROCESS_GROUP ();
|
428
|
1245 EMACS_SET_TTY_PROCESS_GROUP (xforkin, &piddly);
|
|
1246 }
|
|
1247
|
|
1248 /* On AIX, we've disabled SIGHUP above once we start a
|
|
1249 child on a pty. Now reenable it in the child, so it
|
|
1250 will die when we want it to.
|
|
1251 JV: This needs to be done ALWAYS as we might have inherited
|
|
1252 a SIG_IGN handling from our parent (nohup) and we are in new
|
|
1253 process group.
|
|
1254 */
|
613
|
1255 EMACS_SIGNAL (SIGHUP, SIG_DFL);
|
428
|
1256
|
535
|
1257 /* Set up the terminal characteristics of the pty. */
|
|
1258 child_setup_tty (xforkout);
|
|
1259 } /* if (pty_flag) */
|
428
|
1260
|
|
1261
|
613
|
1262 EMACS_SIGNAL (SIGINT, SIG_DFL);
|
|
1263 EMACS_SIGNAL (SIGQUIT, SIG_DFL);
|
428
|
1264
|
|
1265 {
|
867
|
1266 Ibyte **new_argv = alloca_array (Ibyte *, nargv + 2);
|
428
|
1267 int i;
|
|
1268
|
|
1269 /* Nothing below here GCs so our string pointers shouldn't move. */
|
771
|
1270 new_argv[0] = XSTRING_DATA (program);
|
428
|
1271 for (i = 0; i < nargv; i++)
|
|
1272 {
|
|
1273 CHECK_STRING (argv[i]);
|
771
|
1274 new_argv[i + 1] = XSTRING_DATA (argv[i]);
|
428
|
1275 }
|
|
1276 new_argv[i + 1] = 0;
|
|
1277
|
853
|
1278 child_setup (xforkin, xforkout, separate_err ? xforkerr : xforkout,
|
|
1279 new_argv, cur_dir);
|
428
|
1280 }
|
|
1281
|
|
1282 } /**** End of child code ****/
|
|
1283
|
|
1284 /**** Back in parent process ****/
|
|
1285 }
|
|
1286
|
|
1287 if (pid < 0)
|
|
1288 {
|
853
|
1289 /* Note: The caller set up an unwind-protect to automatically delete
|
|
1290 the process if we fail. This will correctly deselect and close
|
|
1291 inchannel, outchannel, and errchannel. */
|
442
|
1292 int save_errno = errno;
|
428
|
1293 close_descriptor_pair (forkin, forkout);
|
853
|
1294 if (separate_err)
|
|
1295 retry_close (forkerr);
|
442
|
1296 errno = save_errno;
|
563
|
1297 report_process_error ("Doing fork", Qunbound);
|
428
|
1298 }
|
|
1299
|
|
1300 /* #### dmoore - why is this commented out, otherwise we leave
|
|
1301 subtty = forkin, but then we close forkin just below. */
|
853
|
1302 /* UNIX_DATA (p)->subtty = -1; */
|
428
|
1303
|
|
1304 /* If the subfork execv fails, and it exits,
|
|
1305 this close hangs. I don't know why.
|
|
1306 So have an interrupt jar it loose. */
|
|
1307 if (forkin >= 0)
|
|
1308 close_safely (forkin);
|
|
1309 if (forkin != forkout && forkout >= 0)
|
771
|
1310 retry_close (forkout);
|
853
|
1311 if (separate_err)
|
|
1312 retry_close (forkerr);
|
428
|
1313
|
1204
|
1314 p->tty_name = pty_flag ? build_intstring (pty_name) : Qnil;
|
428
|
1315
|
|
1316 /* Notice that SIGCHLD was not blocked. (This is not possible on
|
|
1317 some systems.) No biggie if SIGCHLD occurs right around the
|
|
1318 time that this call happens, because SIGCHLD() does not actually
|
|
1319 deselect the process (that doesn't occur until the next time
|
|
1320 we're waiting for an event, when status_notify() is called). */
|
|
1321 return pid;
|
|
1322
|
853
|
1323 io_failure:
|
428
|
1324 {
|
|
1325 int save_errno = errno;
|
|
1326 close_descriptor_pair (forkin, forkout);
|
|
1327 close_descriptor_pair (inchannel, outchannel);
|
853
|
1328 close_descriptor_pair (forkerr, errchannel);
|
428
|
1329 errno = save_errno;
|
563
|
1330 report_process_error ("Opening pty or pipe", Qunbound);
|
1204
|
1331 RETURN_NOT_REACHED (0);
|
428
|
1332 }
|
|
1333 }
|
|
1334
|
|
1335 /* Return nonzero if this process is a ToolTalk connection. */
|
|
1336
|
|
1337 static int
|
440
|
1338 unix_tooltalk_connection_p (Lisp_Process *p)
|
428
|
1339 {
|
853
|
1340 return UNIX_DATA (p)->connected_via_filedesc_p;
|
428
|
1341 }
|
|
1342
|
|
1343 /* This is called to set process' virtual terminal size */
|
|
1344
|
|
1345 static int
|
853
|
1346 unix_set_window_size (Lisp_Process *p, int cols, int rows)
|
428
|
1347 {
|
853
|
1348 return set_window_size (UNIX_DATA (p)->infd, cols, rows);
|
428
|
1349 }
|
|
1350
|
|
1351 /*
|
|
1352 * This method is called to update status fields of the process
|
|
1353 * structure. If the process has not existed, this method is
|
|
1354 * expected to do nothing.
|
|
1355 *
|
|
1356 * The method is called only for real child processes.
|
|
1357 */
|
|
1358
|
|
1359 #ifdef HAVE_WAITPID
|
|
1360 static void
|
853
|
1361 unix_update_status_if_terminated (Lisp_Process *p)
|
428
|
1362 {
|
|
1363 int w;
|
|
1364 #ifdef SIGCHLD
|
|
1365 EMACS_BLOCK_SIGNAL (SIGCHLD);
|
|
1366 #endif
|
|
1367 if (waitpid (XINT (p->pid), &w, WNOHANG) == XINT (p->pid))
|
|
1368 {
|
|
1369 p->tick++;
|
|
1370 update_status_from_wait_code (p, &w);
|
|
1371 }
|
|
1372 #ifdef SIGCHLD
|
|
1373 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
|
|
1374 #endif
|
|
1375 }
|
|
1376 #endif
|
|
1377
|
|
1378 /*
|
|
1379 * Update status of all exited processes. Called when SIGCLD has signaled.
|
|
1380 */
|
|
1381
|
|
1382 #ifdef SIGCHLD
|
|
1383 static void
|
|
1384 unix_reap_exited_processes (void)
|
|
1385 {
|
|
1386 int i;
|
440
|
1387 Lisp_Process *p;
|
428
|
1388
|
|
1389 #ifndef OBNOXIOUS_SYSV_SIGCLD_BEHAVIOR
|
|
1390 record_exited_processes (1);
|
|
1391 #endif
|
|
1392
|
|
1393 if (exited_processes_index <= 0)
|
|
1394 {
|
|
1395 return;
|
|
1396 }
|
|
1397
|
853
|
1398 #ifdef EMACS_BLOCK_SIGNAL
|
428
|
1399 EMACS_BLOCK_SIGNAL (SIGCHLD);
|
|
1400 #endif
|
|
1401 for (i = 0; i < exited_processes_index; i++)
|
|
1402 {
|
|
1403 int pid = exited_processes[i];
|
|
1404 int w = exited_processes_status[i];
|
|
1405
|
|
1406 /* Find the process that signaled us, and record its status. */
|
|
1407
|
|
1408 p = 0;
|
|
1409 {
|
|
1410 Lisp_Object tail;
|
|
1411 LIST_LOOP (tail, Vprocess_list)
|
|
1412 {
|
|
1413 Lisp_Object proc = XCAR (tail);
|
|
1414 p = XPROCESS (proc);
|
|
1415 if (INTP (p->pid) && XINT (p->pid) == pid)
|
|
1416 break;
|
|
1417 p = 0;
|
|
1418 }
|
|
1419 }
|
|
1420
|
|
1421 if (p)
|
|
1422 {
|
|
1423 /* Change the status of the process that was found. */
|
|
1424 p->tick++;
|
|
1425 process_tick++;
|
|
1426 update_status_from_wait_code (p, &w);
|
|
1427
|
|
1428 /* If process has terminated, stop waiting for its output. */
|
|
1429 if (WIFSIGNALED (w) || WIFEXITED (w))
|
|
1430 {
|
853
|
1431 if (!NILP (p->pipe_instream))
|
428
|
1432 {
|
|
1433 /* We can't just call event_stream->unselect_process_cb (p)
|
|
1434 here, because that calls XtRemoveInput, which is not
|
|
1435 necessarily reentrant, so we can't call this at interrupt
|
|
1436 level.
|
|
1437 */
|
|
1438 }
|
|
1439 }
|
|
1440 }
|
853
|
1441 #ifdef NEED_SYNC_PROCESS_CODE
|
428
|
1442 else
|
|
1443 {
|
|
1444 /* There was no asynchronous process found for that id. Check
|
|
1445 if we have a synchronous process. Only set sync process status
|
|
1446 if there is one, so we work OK with the waitpid() call in
|
|
1447 wait_for_termination(). */
|
|
1448 if (synch_process_alive != 0)
|
|
1449 { /* Set the global sync process status variables. */
|
|
1450 synch_process_alive = 0;
|
|
1451
|
|
1452 /* Report the status of the synchronous process. */
|
|
1453 if (WIFEXITED (w))
|
|
1454 synch_process_retcode = WEXITSTATUS (w);
|
|
1455 else if (WIFSIGNALED (w))
|
|
1456 synch_process_death = signal_name (WTERMSIG (w));
|
|
1457 }
|
|
1458 }
|
853
|
1459 #endif /* NEED_SYNC_PROCESS_CODE */
|
428
|
1460 }
|
|
1461
|
|
1462 exited_processes_index = 0;
|
|
1463
|
|
1464 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
|
|
1465 }
|
|
1466 #endif /* SIGCHLD */
|
|
1467
|
|
1468 /*
|
|
1469 * Stuff the entire contents of LSTREAM to the process output pipe
|
|
1470 */
|
|
1471
|
|
1472 static JMP_BUF send_process_frame;
|
|
1473
|
|
1474 static SIGTYPE
|
|
1475 send_process_trap (int signum)
|
|
1476 {
|
|
1477 EMACS_REESTABLISH_SIGNAL (signum, send_process_trap);
|
|
1478 EMACS_UNBLOCK_SIGNAL (signum);
|
|
1479 LONGJMP (send_process_frame, 1);
|
|
1480 }
|
|
1481
|
|
1482 static void
|
853
|
1483 unix_send_process (Lisp_Object proc, struct lstream *lstream)
|
428
|
1484 {
|
1111
|
1485 /* See comment lisp.h circa line 787 */
|
|
1486 SIGTYPE (*VOLATILE_IF_NOT_CPP old_sigpipe) (int) = 0;
|
|
1487 VOLATILE_IF_NOT_CPP Lisp_Object vol_proc = proc;
|
|
1488 Lisp_Process *VOLATILE_IF_NOT_CPP p = XPROCESS (proc);
|
428
|
1489
|
442
|
1490 /* #### JV: layering violation?
|
|
1491
|
|
1492 This function knows too much about the relation between the encoding
|
|
1493 stream (DATA_OUTSTREAM) and the actual output stream p->output_stream.
|
|
1494
|
|
1495 If encoding streams properly forwarded all calls, we could simply
|
|
1496 use DATA_OUTSTREAM everywhere. */
|
|
1497
|
428
|
1498 if (!SETJMP (send_process_frame))
|
|
1499 {
|
|
1500 /* use a reasonable-sized buffer (somewhere around the size of the
|
|
1501 stream buffer) so as to avoid inundating the stream with blocked
|
|
1502 data. */
|
867
|
1503 Ibyte chunkbuf[512];
|
428
|
1504 Bytecount chunklen;
|
|
1505
|
|
1506 while (1)
|
|
1507 {
|
771
|
1508 int writeret;
|
428
|
1509
|
|
1510 chunklen = Lstream_read (lstream, chunkbuf, 512);
|
|
1511 if (chunklen <= 0)
|
|
1512 break; /* perhaps should abort() if < 0?
|
|
1513 This should never happen. */
|
|
1514 old_sigpipe =
|
613
|
1515 (SIGTYPE (*) (int)) EMACS_SIGNAL (SIGPIPE, send_process_trap);
|
428
|
1516 /* Lstream_write() will never successfully write less than
|
|
1517 the amount sent in. In the worst case, it just buffers
|
|
1518 the unwritten data. */
|
800
|
1519 writeret = Lstream_write (XLSTREAM (DATA_OUTSTREAM (p)), chunkbuf,
|
428
|
1520 chunklen);
|
563
|
1521 {
|
|
1522 int save_errno = errno;
|
613
|
1523 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
563
|
1524 errno = save_errno;
|
|
1525 if (writeret < 0)
|
|
1526 /* This is a real error. Blocking errors are handled
|
|
1527 specially inside of the filedesc stream. */
|
|
1528 report_process_error ("writing to process", proc);
|
|
1529 }
|
428
|
1530 while (Lstream_was_blocked_p (XLSTREAM (p->pipe_outstream)))
|
|
1531 {
|
|
1532 /* Buffer is full. Wait, accepting input;
|
|
1533 that may allow the program
|
|
1534 to finish doing output and read more. */
|
1111
|
1535 Faccept_process_output (Qnil, make_int (1), Qnil);
|
442
|
1536 /* It could have *really* finished, deleting the process */
|
|
1537 if (NILP(p->pipe_outstream))
|
|
1538 return;
|
428
|
1539 old_sigpipe =
|
613
|
1540 (SIGTYPE (*) (int)) EMACS_SIGNAL (SIGPIPE, send_process_trap);
|
428
|
1541 Lstream_flush (XLSTREAM (p->pipe_outstream));
|
613
|
1542 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
1543 }
|
|
1544 }
|
|
1545 }
|
|
1546 else
|
|
1547 { /* We got here from a longjmp() from the SIGPIPE handler */
|
613
|
1548 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
1549 /* Close the file lstream so we don't attempt to write to it further */
|
|
1550 /* #### There is controversy over whether this might cause fd leakage */
|
|
1551 /* my tests say no. -slb */
|
|
1552 XLSTREAM (p->pipe_outstream)->flags &= ~LSTREAM_FL_IS_OPEN;
|
898
|
1553 XLSTREAM (p->coding_outstream)->flags &= ~LSTREAM_FL_IS_OPEN;
|
428
|
1554 p->status_symbol = Qexit;
|
|
1555 p->exit_code = 256; /* #### SIGPIPE ??? */
|
|
1556 p->core_dumped = 0;
|
|
1557 p->tick++;
|
|
1558 process_tick++;
|
898
|
1559 deactivate_process (vol_proc);
|
442
|
1560 invalid_operation ("SIGPIPE raised on process; closed it", p->name);
|
428
|
1561 }
|
|
1562
|
613
|
1563 old_sigpipe = (SIGTYPE (*) (int)) EMACS_SIGNAL (SIGPIPE, send_process_trap);
|
800
|
1564 Lstream_flush (XLSTREAM (DATA_OUTSTREAM (p)));
|
613
|
1565 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
1566 }
|
|
1567
|
|
1568 /*
|
|
1569 * Send EOF to the process. The default implementation simply
|
|
1570 * closes the output stream. The method must return 0 to call
|
|
1571 * the default implementation, or 1 if it has taken all care about
|
|
1572 * sending EOF to the process.
|
|
1573 */
|
|
1574
|
|
1575 static int
|
|
1576 unix_process_send_eof (Lisp_Object proc)
|
|
1577 {
|
|
1578 if (!UNIX_DATA (XPROCESS (proc))->pty_flag)
|
|
1579 return 0;
|
|
1580
|
|
1581 /* #### get_eof_char simply doesn't return the correct character
|
|
1582 here. Maybe it is needed to determine the right eof
|
|
1583 character in init_process_io_handles but here it simply screws
|
|
1584 things up. */
|
|
1585 #if 0
|
867
|
1586 Ibyte eof_char = get_eof_char (XPROCESS (proc));
|
428
|
1587 send_process (proc, Qnil, &eof_char, 0, 1);
|
|
1588 #else
|
867
|
1589 send_process (proc, Qnil, (const Ibyte *) "\004", 0, 1);
|
428
|
1590 #endif
|
|
1591 return 1;
|
|
1592 }
|
|
1593
|
|
1594 /*
|
|
1595 * Called before the process is deactivated. The process object
|
|
1596 * is not immediately finalized, just undergoes a transition to
|
|
1597 * inactive state.
|
|
1598 *
|
|
1599 * The return value is a unique stream ID, as returned by
|
853
|
1600 * event_stream_delete_io_streams
|
428
|
1601 *
|
853
|
1602 * In the lack of this method, only event_stream_delete_io_streams
|
428
|
1603 * is called on both I/O streams of the process.
|
|
1604 *
|
|
1605 * The UNIX version guards this by ignoring possible SIGPIPE.
|
|
1606 */
|
|
1607
|
853
|
1608 static void
|
|
1609 unix_deactivate_process (Lisp_Process *p,
|
|
1610 USID *in_usid,
|
|
1611 USID *err_usid)
|
428
|
1612 {
|
|
1613 SIGTYPE (*old_sigpipe) (int) = 0;
|
|
1614
|
|
1615 if (UNIX_DATA(p)->infd >= 0)
|
|
1616 flush_pending_output (UNIX_DATA(p)->infd);
|
853
|
1617 if (UNIX_DATA(p)->errfd >= 0)
|
|
1618 flush_pending_output (UNIX_DATA(p)->errfd);
|
428
|
1619
|
|
1620 /* closing the outstream could result in SIGPIPE, so ignore it. */
|
613
|
1621 old_sigpipe = (SIGTYPE (*) (int)) EMACS_SIGNAL (SIGPIPE, SIG_IGN);
|
853
|
1622 event_stream_delete_io_streams (p->pipe_instream, p->pipe_outstream,
|
|
1623 p->pipe_errstream, in_usid, err_usid);
|
613
|
1624 EMACS_SIGNAL (SIGPIPE, old_sigpipe);
|
428
|
1625
|
|
1626 UNIX_DATA(p)->infd = -1;
|
853
|
1627 UNIX_DATA(p)->errfd = -1;
|
428
|
1628 }
|
|
1629
|
442
|
1630 /* If the subtty field of the process data is not filled in, do so now. */
|
|
1631 static void
|
1204
|
1632 try_to_initialize_subtty (Lisp_Process *p)
|
442
|
1633 {
|
1204
|
1634 struct unix_process_data *upd = UNIX_DATA (p);
|
442
|
1635 if (upd->pty_flag
|
444
|
1636 && (upd->subtty == -1 || ! isatty (upd->subtty))
|
1204
|
1637 && STRINGP (p->tty_name))
|
|
1638 upd->subtty = qxe_open (XSTRING_DATA (p->tty_name), O_RDWR, 0);
|
442
|
1639 }
|
|
1640
|
|
1641 /* Send signal number SIGNO to PROCESS.
|
428
|
1642 CURRENT_GROUP means send to the process group that currently owns
|
|
1643 the terminal being used to communicate with PROCESS.
|
|
1644 This is used for various commands in shell mode.
|
|
1645 If NOMSG is zero, insert signal-announcements into process's buffers
|
|
1646 right away.
|
|
1647
|
|
1648 If we can, we try to signal PROCESS by sending control characters
|
|
1649 down the pty. This allows us to signal inferiors who have changed
|
442
|
1650 their uid, for which killpg would return an EPERM error,
|
|
1651 or processes running on other machines via remote login.
|
428
|
1652
|
442
|
1653 The method signals an error if the given SIGNO is not valid. */
|
428
|
1654
|
|
1655 static void
|
|
1656 unix_kill_child_process (Lisp_Object proc, int signo,
|
|
1657 int current_group, int nomsg)
|
|
1658 {
|
442
|
1659 pid_t pgid = -1;
|
440
|
1660 Lisp_Process *p = XPROCESS (proc);
|
442
|
1661 struct unix_process_data *d = UNIX_DATA (p);
|
428
|
1662
|
|
1663 switch (signo)
|
|
1664 {
|
|
1665 #ifdef SIGCONT
|
|
1666 case SIGCONT:
|
|
1667 p->status_symbol = Qrun;
|
|
1668 p->exit_code = 0;
|
|
1669 p->tick++;
|
|
1670 process_tick++;
|
|
1671 if (!nomsg)
|
|
1672 status_notify ();
|
|
1673 break;
|
|
1674 #endif /* ! defined (SIGCONT) */
|
|
1675 case SIGINT:
|
|
1676 case SIGQUIT:
|
|
1677 case SIGKILL:
|
442
|
1678 flush_pending_output (d->infd);
|
853
|
1679 flush_pending_output (d->errfd);
|
428
|
1680 break;
|
|
1681 }
|
|
1682
|
442
|
1683 if (! d->pty_flag)
|
|
1684 current_group = 0;
|
|
1685
|
|
1686 /* If current_group is true, we want to send a signal to the
|
|
1687 foreground process group of the terminal our child process is
|
|
1688 running on. You would think that would be easy.
|
|
1689
|
|
1690 The BSD people invented the TIOCPGRP ioctl to get the foreground
|
|
1691 process group of a tty. That, combined with killpg, gives us
|
|
1692 what we want.
|
|
1693
|
|
1694 However, the POSIX standards people, in their infinite wisdom,
|
|
1695 have seen fit to only allow this for processes which have the
|
|
1696 terminal as controlling terminal, which doesn't apply to us.
|
|
1697
|
|
1698 Sooo..., we have to do something non-standard. The ioctls
|
|
1699 TIOCSIGNAL, TIOCSIG, and TIOCSIGSEND send the signal directly on
|
|
1700 many systems. POSIX tcgetpgrp(), since it is *documented* as not
|
|
1701 doing what we want, is actually less likely to work than the BSD
|
|
1702 ioctl TIOCGPGRP it is supposed to obsolete. Sometimes we have to
|
|
1703 use TIOCGPGRP on the master end, sometimes the slave end
|
|
1704 (probably an AIX bug). So we better get a fd for the slave if we
|
444
|
1705 haven't got it yet.
|
|
1706
|
|
1707 Anal operating systems like SGI Irix and Compaq Tru64 adhere
|
|
1708 strictly to the letter of the law, so our hack doesn't work.
|
|
1709 The following fragment from an Irix header file is suggestive:
|
|
1710
|
|
1711 #ifdef __notdef__
|
|
1712 // this is not currently supported
|
|
1713 #define TIOCSIGNAL (tIOC|31) // pty: send signal to slave
|
|
1714 #endif
|
|
1715
|
|
1716 On those systems where none of our tricks work, we just fall back
|
|
1717 to the non-current_group behavior and kill the process group of
|
|
1718 the child.
|
|
1719 */
|
442
|
1720 if (current_group)
|
428
|
1721 {
|
1204
|
1722 try_to_initialize_subtty (p);
|
442
|
1723
|
|
1724 #ifdef SIGNALS_VIA_CHARACTERS
|
|
1725 /* If possible, send signals to the entire pgrp
|
|
1726 by sending an input character to it. */
|
|
1727 {
|
867
|
1728 Ibyte sigchar = process_signal_char (d->subtty, signo);
|
442
|
1729 if (sigchar)
|
|
1730 {
|
853
|
1731 send_process (proc, Qnil, &sigchar, 0, 1);
|
442
|
1732 return;
|
|
1733 }
|
|
1734 }
|
|
1735 #endif /* SIGNALS_VIA_CHARACTERS */
|
|
1736
|
|
1737 #ifdef TIOCGPGRP
|
|
1738 if (pgid == -1)
|
|
1739 ioctl (d->infd, TIOCGPGRP, &pgid); /* BSD */
|
|
1740 if (pgid == -1 && d->subtty != -1)
|
|
1741 ioctl (d->subtty, TIOCGPGRP, &pgid); /* Only this works on AIX! */
|
|
1742 #endif /* TIOCGPGRP */
|
|
1743
|
|
1744 if (pgid == -1)
|
428
|
1745 {
|
442
|
1746 /* Many systems provide an ioctl to send a signal directly */
|
|
1747 #ifdef TIOCSIGNAL /* Solaris, HP-UX */
|
|
1748 if (ioctl (d->infd, TIOCSIGNAL, signo) != -1)
|
|
1749 return;
|
|
1750 #endif /* TIOCSIGNAL */
|
|
1751
|
|
1752 #ifdef TIOCSIG /* BSD */
|
|
1753 if (ioctl (d->infd, TIOCSIG, signo) != -1)
|
|
1754 return;
|
|
1755 #endif /* TIOCSIG */
|
428
|
1756 }
|
442
|
1757 } /* current_group */
|
428
|
1758
|
442
|
1759 if (pgid == -1)
|
|
1760 /* Either current_group is 0, or we failed to get the foreground
|
|
1761 process group using the trickery above. So we fall back to
|
|
1762 sending the signal to the process group of our child process.
|
|
1763 Since this is often a shell that ignores signals like SIGINT,
|
|
1764 the shell's subprocess is killed, which is the desired effect.
|
|
1765 The process group of p->pid is always p->pid, since it was
|
|
1766 created as a process group leader. */
|
|
1767 pgid = XINT (p->pid);
|
|
1768
|
|
1769 /* Finally send the signal. */
|
|
1770 if (EMACS_KILLPG (pgid, signo) == -1)
|
458
|
1771 {
|
|
1772 /* It's not an error if our victim is already dead.
|
462
|
1773 And we can't rely on the result of killing a zombie, since
|
|
1774 XPG 4.2 requires that killing a zombie fail with ESRCH,
|
|
1775 while FIPS 151-2 requires that it succeeds! */
|
458
|
1776 #ifdef ESRCH
|
|
1777 if (errno != ESRCH)
|
|
1778 #endif
|
563
|
1779 signal_ferror_with_frob (Qio_error, lisp_strerror (errno),
|
|
1780 "kill (pgid=%ld, signo=%ld) failed",
|
|
1781 (long) pgid, (long) signo);
|
458
|
1782 }
|
428
|
1783 }
|
|
1784
|
442
|
1785 /* Send signal SIGCODE to any process in the system given its PID.
|
|
1786 Return zero if successful, a negative number upon failure. */
|
428
|
1787
|
|
1788 static int
|
|
1789 unix_kill_process_by_pid (int pid, int sigcode)
|
|
1790 {
|
|
1791 return kill (pid, sigcode);
|
|
1792 }
|
|
1793
|
442
|
1794 /* Canonicalize host name HOST, and return its canonical form.
|
|
1795 The default implementation just takes HOST for a canonical name. */
|
428
|
1796
|
|
1797 #ifdef HAVE_SOCKETS
|
|
1798 static Lisp_Object
|
|
1799 unix_canonicalize_host_name (Lisp_Object host)
|
|
1800 {
|
502
|
1801 #ifdef USE_GETADDRINFO
|
440
|
1802 struct addrinfo hints, *res;
|
|
1803 static char addrbuf[NI_MAXHOST];
|
|
1804 Lisp_Object canonname;
|
|
1805 int retval;
|
|
1806 char *ext_host;
|
|
1807
|
|
1808 xzero (hints);
|
|
1809 hints.ai_flags = AI_CANONNAME;
|
724
|
1810 #ifdef IPV6_CANONICALIZE
|
440
|
1811 hints.ai_family = AF_UNSPEC;
|
724
|
1812 #else
|
|
1813 hints.ai_family = PF_INET;
|
|
1814 #endif
|
440
|
1815 hints.ai_socktype = SOCK_STREAM;
|
|
1816 hints.ai_protocol = 0;
|
442
|
1817 LISP_STRING_TO_EXTERNAL (host, ext_host, Qnative);
|
440
|
1818 retval = getaddrinfo (ext_host, NULL, &hints, &res);
|
|
1819 if (retval != 0)
|
|
1820 {
|
867
|
1821 CIbyte *gai_err;
|
440
|
1822
|
855
|
1823 EXTERNAL_TO_C_STRING (gai_strerror (retval), gai_err,
|
771
|
1824 Qstrerror_encoding);
|
855
|
1825 maybe_signal_error (Qio_error, gai_err, host,
|
793
|
1826 Qprocess, ERROR_ME_DEBUG_WARN);
|
440
|
1827 canonname = host;
|
|
1828 }
|
|
1829 else
|
|
1830 {
|
|
1831 int gni = getnameinfo (res->ai_addr, res->ai_addrlen,
|
|
1832 addrbuf, sizeof(addrbuf),
|
|
1833 NULL, 0, NI_NUMERICHOST);
|
|
1834 canonname = gni ? host : build_ext_string (addrbuf, Qnative);
|
|
1835
|
|
1836 freeaddrinfo (res);
|
|
1837 }
|
|
1838
|
|
1839 return canonname;
|
502
|
1840 #else /* ! USE_GETADDRINFO */
|
428
|
1841 struct sockaddr_in address;
|
|
1842
|
|
1843 if (!get_internet_address (host, &address, ERROR_ME_NOT))
|
|
1844 return host;
|
|
1845
|
|
1846 if (address.sin_family == AF_INET)
|
|
1847 return build_string (inet_ntoa (address.sin_addr));
|
|
1848 else
|
|
1849 /* #### any clue what to do here? */
|
|
1850 return host;
|
502
|
1851 #endif /* ! USE_GETADDRINFO */
|
428
|
1852 }
|
|
1853
|
442
|
1854 /* Open a TCP network connection to a given HOST/SERVICE.
|
|
1855 Treated exactly like a normal process when reading and writing.
|
|
1856 Only differences are in status display and process deletion.
|
|
1857 A network connection has no PID; you cannot signal it. All you can
|
|
1858 do is deactivate and close it via delete-process. */
|
428
|
1859
|
|
1860 static void
|
502
|
1861 unix_open_network_stream (Lisp_Object name, Lisp_Object host,
|
|
1862 Lisp_Object service, Lisp_Object protocol,
|
|
1863 void **vinfd, void **voutfd)
|
428
|
1864 {
|
|
1865 int inch;
|
|
1866 int outch;
|
502
|
1867 volatile int s = -1;
|
428
|
1868 volatile int port;
|
|
1869 volatile int retry = 0;
|
502
|
1870 volatile int xerrno = 0;
|
|
1871 volatile int failed_connect = 0;
|
428
|
1872 int retval;
|
|
1873
|
|
1874 CHECK_STRING (host);
|
|
1875
|
|
1876 if (!EQ (protocol, Qtcp) && !EQ (protocol, Qudp))
|
563
|
1877 invalid_constant ("Unsupported protocol", protocol);
|
428
|
1878
|
440
|
1879 {
|
502
|
1880 #ifdef USE_GETADDRINFO
|
|
1881
|
440
|
1882 struct addrinfo hints, *res;
|
|
1883 struct addrinfo * volatile lres;
|
771
|
1884 Extbyte *portstring;
|
|
1885 Extbyte *ext_host;
|
|
1886 Extbyte portbuf[128];
|
440
|
1887 /*
|
|
1888 * Caution: service can either be a string or int.
|
|
1889 * Convert to a C string for later use by getaddrinfo.
|
|
1890 */
|
|
1891 if (INTP (service))
|
|
1892 {
|
|
1893 snprintf (portbuf, sizeof (portbuf), "%ld", (long) XINT (service));
|
|
1894 portstring = portbuf;
|
|
1895 port = htons ((unsigned short) XINT (service));
|
|
1896 }
|
|
1897 else
|
|
1898 {
|
|
1899 CHECK_STRING (service);
|
771
|
1900 LISP_STRING_TO_EXTERNAL (service, portstring,
|
|
1901 Qunix_service_name_encoding);
|
440
|
1902 port = 0;
|
|
1903 }
|
|
1904
|
|
1905 xzero (hints);
|
|
1906 hints.ai_flags = 0;
|
|
1907 hints.ai_family = AF_UNSPEC;
|
|
1908 if (EQ (protocol, Qtcp))
|
|
1909 hints.ai_socktype = SOCK_STREAM;
|
|
1910 else /* EQ (protocol, Qudp) */
|
|
1911 hints.ai_socktype = SOCK_DGRAM;
|
|
1912 hints.ai_protocol = 0;
|
771
|
1913 LISP_STRING_TO_EXTERNAL (host, ext_host, Qunix_host_name_encoding);
|
440
|
1914 retval = getaddrinfo (ext_host, portstring, &hints, &res);
|
|
1915 if (retval != 0)
|
|
1916 {
|
867
|
1917 CIbyte *gai_err;
|
440
|
1918
|
855
|
1919 EXTERNAL_TO_C_STRING (gai_strerror (retval), gai_err,
|
771
|
1920 Qstrerror_encoding);
|
855
|
1921 signal_error (Qio_error, gai_err, list2 (host, service));
|
440
|
1922 }
|
|
1923
|
|
1924 /* address loop */
|
|
1925 for (lres = res; lres ; lres = lres->ai_next)
|
|
1926
|
502
|
1927 #else /* !USE_GETADDRINFO */
|
440
|
1928
|
|
1929 struct sockaddr_in address;
|
502
|
1930 volatile int i;
|
440
|
1931
|
|
1932 if (INTP (service))
|
|
1933 port = htons ((unsigned short) XINT (service));
|
|
1934 else
|
|
1935 {
|
|
1936 struct servent *svc_info;
|
771
|
1937 Extbyte *servext;
|
|
1938
|
440
|
1939 CHECK_STRING (service);
|
771
|
1940 LISP_STRING_TO_EXTERNAL (service, servext,
|
|
1941 Qunix_service_name_encoding);
|
440
|
1942
|
|
1943 if (EQ (protocol, Qtcp))
|
771
|
1944 svc_info = getservbyname (servext, "tcp");
|
440
|
1945 else /* EQ (protocol, Qudp) */
|
771
|
1946 svc_info = getservbyname (servext, "udp");
|
428
|
1947
|
440
|
1948 if (svc_info == 0)
|
442
|
1949 invalid_argument ("Unknown service", service);
|
440
|
1950 port = svc_info->s_port;
|
|
1951 }
|
428
|
1952
|
440
|
1953 get_internet_address (host, &address, ERROR_ME);
|
|
1954 address.sin_port = port;
|
428
|
1955
|
502
|
1956 /* use a trivial address loop */
|
|
1957 for (i = 0; i < 1; i++)
|
|
1958
|
|
1959 #endif /* !USE_GETADDRINFO */
|
|
1960 {
|
|
1961 #ifdef USE_GETADDRINFO
|
|
1962 int family = lres->ai_family;
|
|
1963 #else
|
|
1964 int family = address.sin_family;
|
|
1965 #endif
|
|
1966
|
|
1967 if (EQ (protocol, Qtcp))
|
|
1968 s = socket (family, SOCK_STREAM, 0);
|
|
1969 else /* EQ (protocol, Qudp) */
|
|
1970 s = socket (family, SOCK_DGRAM, 0);
|
|
1971
|
|
1972 if (s < 0)
|
|
1973 {
|
|
1974 xerrno = errno;
|
|
1975 failed_connect = 0;
|
|
1976 continue;
|
|
1977 }
|
|
1978
|
|
1979 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
|
1980 /* Slow down polling. Some kernels have a bug which causes retrying
|
|
1981 connect to fail after a connect. (Note that the entire purpose
|
|
1982 for this code is a very old comment concerning an Ultrix bug that
|
|
1983 requires this code. We used to do this ALWAYS despite this!
|
|
1984 This messes up C-g out of connect() in a big way. So instead we
|
|
1985 just assume that anyone who sees such a kernel bug will define
|
|
1986 this constant, which for now is only defined under Ultrix.) --ben
|
|
1987 */
|
|
1988 slow_down_interrupts ();
|
|
1989 #endif
|
|
1990
|
|
1991 loop:
|
|
1992
|
|
1993 /* A system call interrupted with a SIGALRM or SIGIO comes back
|
|
1994 here, with can_break_system_calls reset to 0. */
|
|
1995 SETJMP (break_system_call_jump);
|
|
1996 if (QUITP)
|
|
1997 {
|
|
1998 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
|
1999 speed_up_interrupts ();
|
|
2000 #endif
|
853
|
2001 QUIT;
|
502
|
2002 /* In case something really weird happens ... */
|
|
2003 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
|
2004 slow_down_interrupts ();
|
|
2005 #endif
|
|
2006 }
|
|
2007
|
|
2008 /* Break out of connect with a signal (it isn't otherwise possible).
|
|
2009 Thus you don't get screwed with a hung network. */
|
|
2010 can_break_system_calls = 1;
|
|
2011
|
|
2012 #ifdef USE_GETADDRINFO
|
|
2013 retval = connect (s, lres->ai_addr, lres->ai_addrlen);
|
|
2014 #else
|
|
2015 retval = connect (s, (struct sockaddr *) &address, sizeof (address));
|
|
2016 #endif
|
|
2017 can_break_system_calls = 0;
|
|
2018 if (retval == -1 && errno != EISCONN)
|
|
2019 {
|
|
2020 xerrno = errno;
|
859
|
2021
|
|
2022 if (errno == EINTR || errno == EINPROGRESS || errno == EALREADY)
|
502
|
2023 goto loop;
|
|
2024 if (errno == EADDRINUSE && retry < 20)
|
|
2025 {
|
|
2026 #ifdef __FreeBSD__
|
|
2027 /* A delay here is needed on some FreeBSD systems,
|
|
2028 and it is harmless, since this retrying takes
|
|
2029 time anyway and should be infrequent.
|
|
2030 `sleep-for' allowed for quitting this loop with
|
|
2031 interrupts slowed down so it can't be used
|
|
2032 here. Async timers should already be disabled
|
|
2033 at this point so we can use `sleep'.
|
|
2034
|
|
2035 (Again, this was not conditionalized on FreeBSD.
|
854
|
2036 Let's not mess up systems without the problem. --ben)
|
502
|
2037 */
|
|
2038 sleep (1);
|
|
2039 #endif
|
|
2040 retry++;
|
|
2041 goto loop;
|
|
2042 }
|
|
2043
|
|
2044 failed_connect = 1;
|
771
|
2045 retry_close (s);
|
502
|
2046 s = -1;
|
|
2047
|
|
2048 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
|
2049 speed_up_interrupts ();
|
|
2050 #endif
|
|
2051
|
|
2052 continue;
|
|
2053 }
|
|
2054
|
|
2055 #ifdef USE_GETADDRINFO
|
|
2056 if (port == 0)
|
|
2057 {
|
|
2058 int gni;
|
|
2059 char servbuf[NI_MAXSERV];
|
|
2060
|
|
2061 if (EQ (protocol, Qtcp))
|
|
2062 gni = getnameinfo (lres->ai_addr, lres->ai_addrlen,
|
|
2063 NULL, 0, servbuf, sizeof(servbuf),
|
|
2064 NI_NUMERICSERV);
|
|
2065 else /* EQ (protocol, Qudp) */
|
|
2066 gni = getnameinfo (lres->ai_addr, lres->ai_addrlen,
|
|
2067 NULL, 0, servbuf, sizeof(servbuf),
|
|
2068 NI_NUMERICSERV | NI_DGRAM);
|
|
2069
|
|
2070 if (gni == 0)
|
|
2071 port = strtol (servbuf, NULL, 10);
|
|
2072 }
|
|
2073
|
|
2074 break;
|
|
2075 #endif /* USE_GETADDRINFO */
|
|
2076 } /* address loop */
|
|
2077
|
|
2078 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
|
2079 speed_up_interrupts ();
|
|
2080 #endif
|
|
2081
|
|
2082 #ifdef USE_GETADDRINFO
|
|
2083 freeaddrinfo (res);
|
|
2084 #endif
|
428
|
2085
|
440
|
2086 if (s < 0)
|
502
|
2087 {
|
|
2088 errno = xerrno;
|
428
|
2089
|
502
|
2090 if (failed_connect)
|
563
|
2091 report_network_error ("connection failed", list3 (Qunbound, host,
|
|
2092 name));
|
502
|
2093 else
|
563
|
2094 report_network_error ("error creating socket", name);
|
440
|
2095 }
|
|
2096 }
|
428
|
2097
|
|
2098 inch = s;
|
|
2099 outch = dup (s);
|
|
2100 if (outch < 0)
|
|
2101 {
|
563
|
2102 int save_errno = errno;
|
771
|
2103 retry_close (s); /* this used to be leaked; from Kyle Jones */
|
563
|
2104 errno = save_errno;
|
|
2105 report_network_error ("error duplicating socket", name);
|
428
|
2106 }
|
|
2107
|
|
2108 set_socket_nonblocking_maybe (inch, port, "tcp");
|
|
2109
|
502
|
2110 *vinfd = (void *) inch;
|
|
2111 *voutfd = (void *) outch;
|
428
|
2112 }
|
|
2113
|
|
2114
|
|
2115 #ifdef HAVE_MULTICAST
|
|
2116
|
442
|
2117 /* Didier Verna <didier@xemacs.org> Nov. 28 1997.
|
428
|
2118
|
|
2119 This function is similar to open-network-stream-internal, but provides a
|
|
2120 mean to open an UDP multicast connection instead of a TCP one. Like in the
|
|
2121 TCP case, the multicast connection will be seen as a sub-process,
|
|
2122
|
|
2123 Some notes:
|
|
2124 - Normally, we should use sendto and recvfrom with non connected
|
|
2125 sockets. The current code doesn't allow us to do this. In the future, it
|
|
2126 would be a good idea to extend the process data structure in order to deal
|
|
2127 properly with the different types network connections.
|
|
2128 - For the same reason, when leaving a multicast group, it is better to make
|
|
2129 a setsockopt - IP_DROP_MEMBERSHIP before closing the descriptors.
|
|
2130 Unfortunately, this can't be done here because delete_process doesn't know
|
|
2131 about the kind of connection we have. However, this is not such an
|
|
2132 important issue.
|
|
2133 */
|
|
2134
|
|
2135 static void
|
442
|
2136 unix_open_multicast_group (Lisp_Object name, Lisp_Object dest,
|
853
|
2137 Lisp_Object port, Lisp_Object ttl, void **vinfd,
|
|
2138 void **voutfd)
|
428
|
2139 {
|
|
2140 struct ip_mreq imr;
|
|
2141 struct sockaddr_in sa;
|
|
2142 struct protoent *udp;
|
|
2143 int ws, rs;
|
|
2144 int theport;
|
|
2145 unsigned char thettl;
|
|
2146 int one = 1; /* For REUSEADDR */
|
|
2147 int ret;
|
|
2148 volatile int retry = 0;
|
|
2149
|
|
2150 CHECK_STRING (dest);
|
|
2151
|
|
2152 CHECK_NATNUM (port);
|
|
2153 theport = htons ((unsigned short) XINT (port));
|
|
2154
|
|
2155 CHECK_NATNUM (ttl);
|
|
2156 thettl = (unsigned char) XINT (ttl);
|
|
2157
|
|
2158 if ((udp = getprotobyname ("udp")) == NULL)
|
563
|
2159 invalid_operation ("No info available for UDP protocol", Qunbound);
|
428
|
2160
|
|
2161 /* Init the sockets. Yes, I need 2 sockets. I couldn't duplicate one. */
|
|
2162 if ((rs = socket (PF_INET, SOCK_DGRAM, udp->p_proto)) < 0)
|
563
|
2163 report_network_error ("error creating socket", name);
|
428
|
2164 if ((ws = socket (PF_INET, SOCK_DGRAM, udp->p_proto)) < 0)
|
|
2165 {
|
563
|
2166 int save_errno = errno;
|
771
|
2167 retry_close (rs);
|
563
|
2168 errno = save_errno;
|
|
2169 report_network_error ("error creating socket", name);
|
428
|
2170 }
|
|
2171
|
|
2172 /* This will be used for both sockets */
|
|
2173 memset (&sa, 0, sizeof(sa));
|
|
2174 sa.sin_family = AF_INET;
|
|
2175 sa.sin_port = theport;
|
671
|
2176 sa.sin_addr.s_addr = inet_addr ((char *) XSTRING_DATA (dest));
|
428
|
2177
|
|
2178 /* Socket configuration for reading ------------------------ */
|
|
2179
|
|
2180 /* Multiple connections from the same machine. This must be done before
|
|
2181 bind. If it fails, it shouldn't be fatal. The only consequence is that
|
|
2182 people won't be able to connect twice from the same machine. */
|
|
2183 if (setsockopt (rs, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof (one))
|
|
2184 < 0)
|
|
2185 warn_when_safe (Qmulticast, Qwarning, "Cannot reuse socket address");
|
|
2186
|
|
2187 /* bind socket name */
|
|
2188 if (bind (rs, (struct sockaddr *)&sa, sizeof(sa)))
|
|
2189 {
|
563
|
2190 int save_errno = errno;
|
771
|
2191 retry_close (rs);
|
|
2192 retry_close (ws);
|
563
|
2193 errno = save_errno;
|
|
2194 report_network_error ("error binding socket", list3 (Qunbound, name,
|
|
2195 port));
|
428
|
2196 }
|
|
2197
|
|
2198 /* join multicast group */
|
671
|
2199 imr.imr_multiaddr.s_addr = inet_addr ((char *) XSTRING_DATA (dest));
|
428
|
2200 imr.imr_interface.s_addr = htonl (INADDR_ANY);
|
|
2201 if (setsockopt (rs, IPPROTO_IP, IP_ADD_MEMBERSHIP,
|
442
|
2202 &imr, sizeof (struct ip_mreq)) < 0)
|
428
|
2203 {
|
563
|
2204 int save_errno = errno;
|
771
|
2205 retry_close (ws);
|
|
2206 retry_close (rs);
|
563
|
2207 errno = save_errno;
|
|
2208 report_network_error ("error adding membership", list3 (Qunbound, name,
|
|
2209 dest));
|
428
|
2210 }
|
|
2211
|
|
2212 /* Socket configuration for writing ----------------------- */
|
|
2213
|
|
2214 /* Normally, there's no 'connect' in multicast, since we prefer to use
|
|
2215 'sendto' and 'recvfrom'. However, in order to handle this connection in
|
|
2216 the process-like way it is done for TCP, we must be able to use 'write'
|
|
2217 instead of 'sendto'. Consequently, we 'connect' this socket. */
|
|
2218
|
|
2219 /* See open-network-stream-internal for comments on this part of the code */
|
502
|
2220 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
428
|
2221 slow_down_interrupts ();
|
502
|
2222 #endif
|
428
|
2223
|
|
2224 loop:
|
|
2225
|
|
2226 /* A system call interrupted with a SIGALRM or SIGIO comes back
|
|
2227 here, with can_break_system_calls reset to 0. */
|
|
2228 SETJMP (break_system_call_jump);
|
|
2229 if (QUITP)
|
|
2230 {
|
502
|
2231 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
428
|
2232 speed_up_interrupts ();
|
502
|
2233 #endif
|
853
|
2234 QUIT;
|
428
|
2235 /* In case something really weird happens ... */
|
502
|
2236 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
428
|
2237 slow_down_interrupts ();
|
502
|
2238 #endif
|
428
|
2239 }
|
|
2240
|
|
2241 /* Break out of connect with a signal (it isn't otherwise possible).
|
|
2242 Thus you don't get screwed with a hung network. */
|
|
2243 can_break_system_calls = 1;
|
|
2244 ret = connect (ws, (struct sockaddr *) &sa, sizeof (sa));
|
|
2245 can_break_system_calls = 0;
|
|
2246 if (ret == -1 && errno != EISCONN)
|
|
2247 {
|
|
2248 int xerrno = errno;
|
|
2249
|
859
|
2250 if (errno == EINTR || errno == EINPROGRESS || errno == EALREADY)
|
428
|
2251 goto loop;
|
|
2252 if (errno == EADDRINUSE && retry < 20)
|
|
2253 {
|
859
|
2254 #ifdef __FreeBSD__
|
428
|
2255 /* A delay here is needed on some FreeBSD systems,
|
|
2256 and it is harmless, since this retrying takes time anyway
|
|
2257 and should be infrequent.
|
|
2258 `sleep-for' allowed for quitting this loop with interrupts
|
|
2259 slowed down so it can't be used here. Async timers should
|
|
2260 already be disabled at this point so we can use `sleep'. */
|
|
2261 sleep (1);
|
859
|
2262 #endif
|
428
|
2263 retry++;
|
|
2264 goto loop;
|
|
2265 }
|
|
2266
|
771
|
2267 retry_close (rs);
|
|
2268 retry_close (ws);
|
502
|
2269 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
428
|
2270 speed_up_interrupts ();
|
502
|
2271 #endif
|
428
|
2272
|
|
2273 errno = xerrno;
|
563
|
2274 report_network_error ("error connecting socket", list3 (Qunbound, name,
|
|
2275 port));
|
428
|
2276 }
|
|
2277
|
502
|
2278 #ifdef CONNECT_NEEDS_SLOWED_INTERRUPTS
|
428
|
2279 speed_up_interrupts ();
|
502
|
2280 #endif
|
428
|
2281
|
|
2282 /* scope */
|
|
2283 if (setsockopt (ws, IPPROTO_IP, IP_MULTICAST_TTL,
|
442
|
2284 &thettl, sizeof (thettl)) < 0)
|
428
|
2285 {
|
563
|
2286 int save_errno = errno;
|
771
|
2287 retry_close (rs);
|
|
2288 retry_close (ws);
|
563
|
2289 errno = save_errno;
|
|
2290 report_network_error ("error setting ttl", list3 (Qunbound, name, ttl));
|
428
|
2291 }
|
|
2292
|
|
2293 set_socket_nonblocking_maybe (rs, theport, "udp");
|
|
2294
|
|
2295 *vinfd = (void*)rs;
|
|
2296 *voutfd = (void*)ws;
|
|
2297 }
|
|
2298
|
|
2299 #endif /* HAVE_MULTICAST */
|
|
2300
|
|
2301 #endif /* HAVE_SOCKETS */
|
|
2302
|
|
2303
|
|
2304 /**********************************************************************/
|
|
2305 /* Initialization */
|
|
2306 /**********************************************************************/
|
|
2307
|
|
2308 void
|
|
2309 process_type_create_unix (void)
|
|
2310 {
|
|
2311 PROCESS_HAS_METHOD (unix, alloc_process_data);
|
|
2312 #ifdef SIGCHLD
|
|
2313 PROCESS_HAS_METHOD (unix, init_process);
|
|
2314 PROCESS_HAS_METHOD (unix, reap_exited_processes);
|
|
2315 #endif
|
|
2316 PROCESS_HAS_METHOD (unix, init_process_io_handles);
|
|
2317 PROCESS_HAS_METHOD (unix, create_process);
|
|
2318 PROCESS_HAS_METHOD (unix, tooltalk_connection_p);
|
|
2319 PROCESS_HAS_METHOD (unix, set_window_size);
|
|
2320 #ifdef HAVE_WAITPID
|
|
2321 PROCESS_HAS_METHOD (unix, update_status_if_terminated);
|
|
2322 #endif
|
|
2323 PROCESS_HAS_METHOD (unix, send_process);
|
|
2324 PROCESS_HAS_METHOD (unix, process_send_eof);
|
|
2325 PROCESS_HAS_METHOD (unix, deactivate_process);
|
|
2326 PROCESS_HAS_METHOD (unix, kill_child_process);
|
|
2327 PROCESS_HAS_METHOD (unix, kill_process_by_pid);
|
|
2328 #ifdef HAVE_SOCKETS
|
|
2329 PROCESS_HAS_METHOD (unix, canonicalize_host_name);
|
|
2330 PROCESS_HAS_METHOD (unix, open_network_stream);
|
|
2331 #ifdef HAVE_MULTICAST
|
|
2332 PROCESS_HAS_METHOD (unix, open_multicast_group);
|
|
2333 #endif
|
|
2334 #endif
|
|
2335 }
|
|
2336
|
|
2337 void
|
|
2338 vars_of_process_unix (void)
|
|
2339 {
|
|
2340 Fprovide (intern ("unix-processes"));
|
|
2341 }
|
|
2342
|
|
2343 #endif /* !defined (NO_SUBPROCESSES) */
|