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