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