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