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