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