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