annotate src/process-unix.c @ 5915:1af53d35dd53

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