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