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