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