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