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