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