annotate src/process-unix.c @ 558:ed498ef2108b

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