aboutsummaryrefslogtreecommitdiffstats
path: root/src/process.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/process.c')
-rw-r--r--src/process.c3163
1 files changed, 1476 insertions, 1687 deletions
diff --git a/src/process.c b/src/process.c
index 4d59ff0d452..20f84990d6f 100644
--- a/src/process.c
+++ b/src/process.c
@@ -1,7 +1,7 @@
1/* Asynchronous subprocess control for GNU Emacs. 1/* Asynchronous subprocess control for GNU Emacs.
2 2
3Copyright (C) 1985-1988, 1993-1996, 1998-1999, 2001-2012 3Copyright (C) 1985-1988, 1993-1996, 1998-1999, 2001-2013 Free Software
4 Free Software Foundation, Inc. 4Foundation, Inc.
5 5
6This file is part of GNU Emacs. 6This file is part of GNU Emacs.
7 7
@@ -20,15 +20,14 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 20
21 21
22#include <config.h> 22#include <config.h>
23#include <signal.h> 23
24#define PROCESS_INLINE EXTERN_INLINE
25
24#include <stdio.h> 26#include <stdio.h>
25#include <errno.h> 27#include <errno.h>
26#include <setjmp.h>
27#include <sys/types.h> /* Some typedefs are used in sys/file.h. */ 28#include <sys/types.h> /* Some typedefs are used in sys/file.h. */
28#include <sys/file.h> 29#include <sys/file.h>
29#include <sys/stat.h> 30#include <sys/stat.h>
30#include <setjmp.h>
31
32#include <unistd.h> 31#include <unistd.h>
33#include <fcntl.h> 32#include <fcntl.h>
34 33
@@ -73,8 +72,12 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
73#include <bsdtty.h> 72#include <bsdtty.h>
74#endif 73#endif
75 74
75#ifdef USG5_4
76# include <sys/stream.h>
77# include <sys/stropts.h>
78#endif
79
76#ifdef HAVE_RES_INIT 80#ifdef HAVE_RES_INIT
77#include <netinet/in.h>
78#include <arpa/nameser.h> 81#include <arpa/nameser.h>
79#include <resolv.h> 82#include <resolv.h>
80#endif 83#endif
@@ -87,14 +90,18 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
87#include <pty.h> 90#include <pty.h>
88#endif 91#endif
89 92
93#include <c-ctype.h>
94#include <sig2str.h>
95#include <verify.h>
96
90#endif /* subprocesses */ 97#endif /* subprocesses */
91 98
92#include "systime.h" 99#include "systime.h"
93#include "systty.h" 100#include "systty.h"
94 101
95#include "window.h" 102#include "window.h"
96#include "buffer.h"
97#include "character.h" 103#include "character.h"
104#include "buffer.h"
98#include "coding.h" 105#include "coding.h"
99#include "process.h" 106#include "process.h"
100#include "frame.h" 107#include "frame.h"
@@ -113,17 +120,57 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
113#include "gnutls.h" 120#include "gnutls.h"
114#endif 121#endif
115 122
116#if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS) 123#ifdef HAVE_WINDOW_SYSTEM
124#include TERM_HEADER
125#endif /* HAVE_WINDOW_SYSTEM */
126
127#ifdef HAVE_GLIB
117#include "xgselect.h" 128#include "xgselect.h"
129#ifndef WINDOWSNT
130#include <glib.h>
131#endif
118#endif 132#endif
119#ifdef HAVE_NS 133
120#include "nsterm.h" 134#ifdef WINDOWSNT
135extern int sys_select (int, fd_set *, fd_set *, fd_set *,
136 struct timespec *, void *);
137#endif
138
139#ifndef SOCK_CLOEXEC
140# define SOCK_CLOEXEC 0
141#endif
142
143#ifndef HAVE_ACCEPT4
144
145/* Emulate GNU/Linux accept4 and socket well enough for this module. */
146
147static int
148close_on_exec (int fd)
149{
150 if (0 <= fd)
151 fcntl (fd, F_SETFD, FD_CLOEXEC);
152 return fd;
153}
154
155static int
156accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags)
157{
158 return close_on_exec (accept (sockfd, addr, addrlen));
159}
160
161static int
162process_socket (int domain, int type, int protocol)
163{
164 return close_on_exec (socket (domain, type, protocol));
165}
166# undef socket
167# define socket(domain, type, protocol) process_socket (domain, type, protocol)
121#endif 168#endif
122 169
123/* Work around GCC 4.7.0 bug with strict overflow checking; see 170/* Work around GCC 4.7.0 bug with strict overflow checking; see
124 <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52904>. 171 <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52904>.
125 These lines can be removed once the GCC bug is fixed. */ 172 These lines can be removed once the GCC bug is fixed. */
126#if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__ 173#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
127# pragma GCC diagnostic ignored "-Wstrict-overflow" 174# pragma GCC diagnostic ignored "-Wstrict-overflow"
128#endif 175#endif
129 176
@@ -133,13 +180,13 @@ Lisp_Object Qcutime, Qpri, Qnice, Qthcount, Qstart, Qvsize, Qrss, Qargs;
133Lisp_Object Quser, Qgroup, Qetime, Qpcpu, Qpmem, Qtime, Qctime; 180Lisp_Object Quser, Qgroup, Qetime, Qpcpu, Qpmem, Qtime, Qctime;
134Lisp_Object QCname, QCtype; 181Lisp_Object QCname, QCtype;
135 182
136/* Non-zero if keyboard input is on hold, zero otherwise. */ 183/* True if keyboard input is on hold, zero otherwise. */
137 184
138static int kbd_is_on_hold; 185static bool kbd_is_on_hold;
139 186
140/* Nonzero means don't run process sentinels. This is used 187/* Nonzero means don't run process sentinels. This is used
141 when exiting. */ 188 when exiting. */
142int inhibit_sentinels; 189bool inhibit_sentinels;
143 190
144#ifdef subprocesses 191#ifdef subprocesses
145 192
@@ -161,25 +208,13 @@ static Lisp_Object QClocal, QCremote, QCcoding;
161static Lisp_Object QCserver, QCnowait, QCnoquery, QCstop; 208static Lisp_Object QCserver, QCnowait, QCnoquery, QCstop;
162static Lisp_Object QCsentinel, QClog, QCoptions, QCplist; 209static Lisp_Object QCsentinel, QClog, QCoptions, QCplist;
163static Lisp_Object Qlast_nonmenu_event; 210static Lisp_Object Qlast_nonmenu_event;
164/* QCfamily is declared and initialized in xfaces.c, 211static Lisp_Object Qinternal_default_process_sentinel;
165 QCfilter in keyboard.c. */ 212static Lisp_Object Qinternal_default_process_filter;
166extern Lisp_Object QCfamily, QCfilter;
167
168/* Qexit is declared and initialized in eval.c. */
169
170/* QCfamily is defined in xfaces.c. */
171extern Lisp_Object QCfamily;
172/* QCfilter is defined in keyboard.c. */
173extern Lisp_Object QCfilter;
174 213
175#define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork)) 214#define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
176#define NETCONN1_P(p) (EQ ((p)->type, Qnetwork)) 215#define NETCONN1_P(p) (EQ (p->type, Qnetwork))
177#define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial)) 216#define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial))
178#define SERIALCONN1_P(p) (EQ ((p)->type, Qserial)) 217#define SERIALCONN1_P(p) (EQ (p->type, Qserial))
179
180#ifndef HAVE_H_ERRNO
181extern int h_errno;
182#endif
183 218
184/* Number of events of change of status of a process. */ 219/* Number of events of change of status of a process. */
185static EMACS_INT process_tick; 220static EMACS_INT process_tick;
@@ -196,11 +231,9 @@ static EMACS_INT update_tick;
196#ifndef NON_BLOCKING_CONNECT 231#ifndef NON_BLOCKING_CONNECT
197#ifdef HAVE_SELECT 232#ifdef HAVE_SELECT
198#if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX) 233#if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
199#if defined (O_NONBLOCK) || defined (O_NDELAY)
200#if defined (EWOULDBLOCK) || defined (EINPROGRESS) 234#if defined (EWOULDBLOCK) || defined (EINPROGRESS)
201#define NON_BLOCKING_CONNECT 235#define NON_BLOCKING_CONNECT
202#endif /* EWOULDBLOCK || EINPROGRESS */ 236#endif /* EWOULDBLOCK || EINPROGRESS */
203#endif /* O_NONBLOCK || O_NDELAY */
204#endif /* HAVE_GETPEERNAME || GNU_LINUX */ 237#endif /* HAVE_GETPEERNAME || GNU_LINUX */
205#endif /* HAVE_SELECT */ 238#endif /* HAVE_SELECT */
206#endif /* NON_BLOCKING_CONNECT */ 239#endif /* NON_BLOCKING_CONNECT */
@@ -211,30 +244,24 @@ static EMACS_INT update_tick;
211 "non-destructive" select. So we require either native select, 244 "non-destructive" select. So we require either native select,
212 or emulation of select using FIONREAD. */ 245 or emulation of select using FIONREAD. */
213 246
214#ifdef BROKEN_DATAGRAM_SOCKETS 247#ifndef BROKEN_DATAGRAM_SOCKETS
215#undef DATAGRAM_SOCKETS 248# if defined HAVE_SELECT || defined USABLE_FIONREAD
216#else 249# if defined HAVE_SENDTO && defined HAVE_RECVFROM && defined EMSGSIZE
217#ifndef DATAGRAM_SOCKETS 250# define DATAGRAM_SOCKETS
218#if defined (HAVE_SELECT) || defined (FIONREAD) 251# endif
219#if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE) 252# endif
220#define DATAGRAM_SOCKETS 253#endif
221#endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
222#endif /* HAVE_SELECT || FIONREAD */
223#endif /* DATAGRAM_SOCKETS */
224#endif /* BROKEN_DATAGRAM_SOCKETS */
225 254
226#if defined HAVE_LOCAL_SOCKETS && defined DATAGRAM_SOCKETS 255#if defined HAVE_LOCAL_SOCKETS && defined DATAGRAM_SOCKETS
227# define HAVE_SEQPACKET 256# define HAVE_SEQPACKET
228#endif 257#endif
229 258
230#if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING) 259#if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
231#ifdef EMACS_HAS_USECS
232#define ADAPTIVE_READ_BUFFERING 260#define ADAPTIVE_READ_BUFFERING
233#endif 261#endif
234#endif
235 262
236#ifdef ADAPTIVE_READ_BUFFERING 263#ifdef ADAPTIVE_READ_BUFFERING
237#define READ_OUTPUT_DELAY_INCREMENT 10000 264#define READ_OUTPUT_DELAY_INCREMENT (TIMESPEC_RESOLUTION / 100)
238#define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5) 265#define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
239#define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7) 266#define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
240 267
@@ -243,27 +270,27 @@ static EMACS_INT update_tick;
243 270
244static int process_output_delay_count; 271static int process_output_delay_count;
245 272
246/* Non-zero if any process has non-nil read_output_skip. */ 273/* True if any process has non-nil read_output_skip. */
247 274
248static int process_output_skip; 275static bool process_output_skip;
249 276
250#else 277#else
251#define process_output_delay_count 0 278#define process_output_delay_count 0
252#endif 279#endif
253 280
254static Lisp_Object Fget_process (Lisp_Object);
255static void create_process (Lisp_Object, char **, Lisp_Object); 281static void create_process (Lisp_Object, char **, Lisp_Object);
256#ifdef SIGIO 282#ifdef USABLE_SIGIO
257static int keyboard_bit_set (SELECT_TYPE *); 283static bool keyboard_bit_set (fd_set *);
258#endif 284#endif
259static void deactivate_process (Lisp_Object); 285static void deactivate_process (Lisp_Object);
260static void status_notify (struct Lisp_Process *); 286static void status_notify (struct Lisp_Process *);
261static int read_process_output (Lisp_Object, int); 287static int read_process_output (Lisp_Object, int);
288static void handle_child_signal (int);
262static void create_pty (Lisp_Object); 289static void create_pty (Lisp_Object);
263 290
264/* If we support a window system, turn on the code to poll periodically 291/* If we support a window system, turn on the code to poll periodically
265 to detect C-g. It isn't actually used when doing interrupt input. */ 292 to detect C-g. It isn't actually used when doing interrupt input. */
266#if defined (HAVE_WINDOW_SYSTEM) && !defined (USE_ASYNC_EVENTS) 293#ifdef HAVE_WINDOW_SYSTEM
267#define POLL_FOR_INPUT 294#define POLL_FOR_INPUT
268#endif 295#endif
269 296
@@ -272,39 +299,39 @@ static void exec_sentinel (Lisp_Object proc, Lisp_Object reason);
272 299
273/* Mask of bits indicating the descriptors that we wait for input on. */ 300/* Mask of bits indicating the descriptors that we wait for input on. */
274 301
275static SELECT_TYPE input_wait_mask; 302static fd_set input_wait_mask;
276 303
277/* Mask that excludes keyboard input descriptor(s). */ 304/* Mask that excludes keyboard input descriptor(s). */
278 305
279static SELECT_TYPE non_keyboard_wait_mask; 306static fd_set non_keyboard_wait_mask;
280 307
281/* Mask that excludes process input descriptor(s). */ 308/* Mask that excludes process input descriptor(s). */
282 309
283static SELECT_TYPE non_process_wait_mask; 310static fd_set non_process_wait_mask;
284 311
285/* Mask for selecting for write. */ 312/* Mask for selecting for write. */
286 313
287static SELECT_TYPE write_mask; 314static fd_set write_mask;
288 315
289#ifdef NON_BLOCKING_CONNECT 316#ifdef NON_BLOCKING_CONNECT
290/* Mask of bits indicating the descriptors that we wait for connect to 317/* Mask of bits indicating the descriptors that we wait for connect to
291 complete on. Once they complete, they are removed from this mask 318 complete on. Once they complete, they are removed from this mask
292 and added to the input_wait_mask and non_keyboard_wait_mask. */ 319 and added to the input_wait_mask and non_keyboard_wait_mask. */
293 320
294static SELECT_TYPE connect_wait_mask; 321static fd_set connect_wait_mask;
295 322
296/* Number of bits set in connect_wait_mask. */ 323/* Number of bits set in connect_wait_mask. */
297static int num_pending_connects; 324static int num_pending_connects;
298#endif /* NON_BLOCKING_CONNECT */ 325#endif /* NON_BLOCKING_CONNECT */
299 326
300/* The largest descriptor currently in use for a process object. */ 327/* The largest descriptor currently in use for a process object; -1 if none. */
301static int max_process_desc; 328static int max_process_desc;
302 329
303/* The largest descriptor currently in use for input. */ 330/* The largest descriptor currently in use for input; -1 if none. */
304static int max_input_desc; 331static int max_input_desc;
305 332
306/* Indexed by descriptor, gives the process (if any) for that descriptor */ 333/* Indexed by descriptor, gives the process (if any) for that descriptor */
307static Lisp_Object chan_process[MAXDESC]; 334static Lisp_Object chan_process[FD_SETSIZE];
308 335
309/* Alist of elements (NAME . PROCESS) */ 336/* Alist of elements (NAME . PROCESS) */
310static Lisp_Object Vprocess_alist; 337static Lisp_Object Vprocess_alist;
@@ -315,18 +342,18 @@ static Lisp_Object Vprocess_alist;
315 output from the process is to read at least one char. 342 output from the process is to read at least one char.
316 Always -1 on systems that support FIONREAD. */ 343 Always -1 on systems that support FIONREAD. */
317 344
318static int proc_buffered_char[MAXDESC]; 345static int proc_buffered_char[FD_SETSIZE];
319 346
320/* Table of `struct coding-system' for each process. */ 347/* Table of `struct coding-system' for each process. */
321static struct coding_system *proc_decode_coding_system[MAXDESC]; 348static struct coding_system *proc_decode_coding_system[FD_SETSIZE];
322static struct coding_system *proc_encode_coding_system[MAXDESC]; 349static struct coding_system *proc_encode_coding_system[FD_SETSIZE];
323 350
324#ifdef DATAGRAM_SOCKETS 351#ifdef DATAGRAM_SOCKETS
325/* Table of `partner address' for datagram sockets. */ 352/* Table of `partner address' for datagram sockets. */
326static struct sockaddr_and_len { 353static struct sockaddr_and_len {
327 struct sockaddr *sa; 354 struct sockaddr *sa;
328 int len; 355 int len;
329} datagram_address[MAXDESC]; 356} datagram_address[FD_SETSIZE];
330#define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0) 357#define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
331#define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0) 358#define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
332#else 359#else
@@ -334,8 +361,93 @@ static struct sockaddr_and_len {
334#define DATAGRAM_CONN_P(proc) (0) 361#define DATAGRAM_CONN_P(proc) (0)
335#endif 362#endif
336 363
337/* Maximum number of bytes to send to a pty without an eof. */ 364/* FOR_EACH_PROCESS (LIST_VAR, PROC_VAR) followed by a statement is
338static int pty_max_bytes; 365 a `for' loop which iterates over processes from Vprocess_alist. */
366
367#define FOR_EACH_PROCESS(list_var, proc_var) \
368 FOR_EACH_ALIST_VALUE (Vprocess_alist, list_var, proc_var)
369
370/* These setters are used only in this file, so they can be private. */
371static void
372pset_buffer (struct Lisp_Process *p, Lisp_Object val)
373{
374 p->buffer = val;
375}
376static void
377pset_command (struct Lisp_Process *p, Lisp_Object val)
378{
379 p->command = val;
380}
381static void
382pset_decode_coding_system (struct Lisp_Process *p, Lisp_Object val)
383{
384 p->decode_coding_system = val;
385}
386static void
387pset_decoding_buf (struct Lisp_Process *p, Lisp_Object val)
388{
389 p->decoding_buf = val;
390}
391static void
392pset_encode_coding_system (struct Lisp_Process *p, Lisp_Object val)
393{
394 p->encode_coding_system = val;
395}
396static void
397pset_encoding_buf (struct Lisp_Process *p, Lisp_Object val)
398{
399 p->encoding_buf = val;
400}
401static void
402pset_filter (struct Lisp_Process *p, Lisp_Object val)
403{
404 p->filter = NILP (val) ? Qinternal_default_process_filter : val;
405}
406static void
407pset_log (struct Lisp_Process *p, Lisp_Object val)
408{
409 p->log = val;
410}
411static void
412pset_mark (struct Lisp_Process *p, Lisp_Object val)
413{
414 p->mark = val;
415}
416static void
417pset_name (struct Lisp_Process *p, Lisp_Object val)
418{
419 p->name = val;
420}
421static void
422pset_plist (struct Lisp_Process *p, Lisp_Object val)
423{
424 p->plist = val;
425}
426static void
427pset_sentinel (struct Lisp_Process *p, Lisp_Object val)
428{
429 p->sentinel = NILP (val) ? Qinternal_default_process_sentinel : val;
430}
431static void
432pset_status (struct Lisp_Process *p, Lisp_Object val)
433{
434 p->status = val;
435}
436static void
437pset_tty_name (struct Lisp_Process *p, Lisp_Object val)
438{
439 p->tty_name = val;
440}
441static void
442pset_type (struct Lisp_Process *p, Lisp_Object val)
443{
444 p->type = val;
445}
446static void
447pset_write_queue (struct Lisp_Process *p, Lisp_Object val)
448{
449 p->write_queue = val;
450}
339 451
340 452
341 453
@@ -346,7 +458,7 @@ static struct fd_callback_data
346#define FOR_READ 1 458#define FOR_READ 1
347#define FOR_WRITE 2 459#define FOR_WRITE 2
348 int condition; /* mask of the defines above. */ 460 int condition; /* mask of the defines above. */
349} fd_callback_info[MAXDESC]; 461} fd_callback_info[FD_SETSIZE];
350 462
351 463
352/* Add a file descriptor FD to be monitored for when read is possible. 464/* Add a file descriptor FD to be monitored for when read is possible.
@@ -355,7 +467,7 @@ static struct fd_callback_data
355void 467void
356add_read_fd (int fd, fd_callback func, void *data) 468add_read_fd (int fd, fd_callback func, void *data)
357{ 469{
358 xassert (fd < MAXDESC); 470 eassert (fd < FD_SETSIZE);
359 add_keyboard_wait_descriptor (fd); 471 add_keyboard_wait_descriptor (fd);
360 472
361 fd_callback_info[fd].func = func; 473 fd_callback_info[fd].func = func;
@@ -368,7 +480,7 @@ add_read_fd (int fd, fd_callback func, void *data)
368void 480void
369delete_read_fd (int fd) 481delete_read_fd (int fd)
370{ 482{
371 xassert (fd < MAXDESC); 483 eassert (fd < FD_SETSIZE);
372 delete_keyboard_wait_descriptor (fd); 484 delete_keyboard_wait_descriptor (fd);
373 485
374 fd_callback_info[fd].condition &= ~FOR_READ; 486 fd_callback_info[fd].condition &= ~FOR_READ;
@@ -385,7 +497,7 @@ delete_read_fd (int fd)
385void 497void
386add_write_fd (int fd, fd_callback func, void *data) 498add_write_fd (int fd, fd_callback func, void *data)
387{ 499{
388 xassert (fd < MAXDESC); 500 eassert (fd < FD_SETSIZE);
389 FD_SET (fd, &write_mask); 501 FD_SET (fd, &write_mask);
390 if (fd > max_input_desc) 502 if (fd > max_input_desc)
391 max_input_desc = fd; 503 max_input_desc = fd;
@@ -395,29 +507,35 @@ add_write_fd (int fd, fd_callback func, void *data)
395 fd_callback_info[fd].condition |= FOR_WRITE; 507 fd_callback_info[fd].condition |= FOR_WRITE;
396} 508}
397 509
510/* FD is no longer an input descriptor; update max_input_desc accordingly. */
511
512static void
513delete_input_desc (int fd)
514{
515 if (fd == max_input_desc)
516 {
517 do
518 fd--;
519 while (0 <= fd && ! (FD_ISSET (fd, &input_wait_mask)
520 || FD_ISSET (fd, &write_mask)));
521
522 max_input_desc = fd;
523 }
524}
525
398/* Stop monitoring file descriptor FD for when write is possible. */ 526/* Stop monitoring file descriptor FD for when write is possible. */
399 527
400void 528void
401delete_write_fd (int fd) 529delete_write_fd (int fd)
402{ 530{
403 int lim = max_input_desc; 531 eassert (fd < FD_SETSIZE);
404
405 xassert (fd < MAXDESC);
406 FD_CLR (fd, &write_mask); 532 FD_CLR (fd, &write_mask);
407 fd_callback_info[fd].condition &= ~FOR_WRITE; 533 fd_callback_info[fd].condition &= ~FOR_WRITE;
408 if (fd_callback_info[fd].condition == 0) 534 if (fd_callback_info[fd].condition == 0)
409 { 535 {
410 fd_callback_info[fd].func = 0; 536 fd_callback_info[fd].func = 0;
411 fd_callback_info[fd].data = 0; 537 fd_callback_info[fd].data = 0;
412 538 delete_input_desc (fd);
413 if (fd == max_input_desc)
414 for (fd = lim; fd >= 0; fd--)
415 if (FD_ISSET (fd, &input_wait_mask) || FD_ISSET (fd, &write_mask))
416 {
417 max_input_desc = fd;
418 break;
419 }
420
421 } 539 }
422} 540}
423 541
@@ -431,7 +549,7 @@ static void
431update_status (struct Lisp_Process *p) 549update_status (struct Lisp_Process *p)
432{ 550{
433 eassert (p->raw_status_new); 551 eassert (p->raw_status_new);
434 p->status = status_convert (p->raw_status); 552 pset_status (p, status_convert (p->raw_status));
435 p->raw_status_new = 0; 553 p->raw_status_new = 0;
436} 554}
437 555
@@ -444,7 +562,7 @@ status_convert (int w)
444 if (WIFSTOPPED (w)) 562 if (WIFSTOPPED (w))
445 return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil)); 563 return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil));
446 else if (WIFEXITED (w)) 564 else if (WIFEXITED (w))
447 return Fcons (Qexit, Fcons (make_number (WRETCODE (w)), 565 return Fcons (Qexit, Fcons (make_number (WEXITSTATUS (w)),
448 WCOREDUMP (w) ? Qt : Qnil)); 566 WCOREDUMP (w) ? Qt : Qnil));
449 else if (WIFSIGNALED (w)) 567 else if (WIFSIGNALED (w))
450 return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)), 568 return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)),
@@ -457,7 +575,7 @@ status_convert (int w)
457 and store them individually through the three pointers. */ 575 and store them individually through the three pointers. */
458 576
459static void 577static void
460decode_status (Lisp_Object l, Lisp_Object *symbol, int *code, int *coredump) 578decode_status (Lisp_Object l, Lisp_Object *symbol, int *code, bool *coredump)
461{ 579{
462 Lisp_Object tem; 580 Lisp_Object tem;
463 581
@@ -484,14 +602,15 @@ status_message (struct Lisp_Process *p)
484{ 602{
485 Lisp_Object status = p->status; 603 Lisp_Object status = p->status;
486 Lisp_Object symbol; 604 Lisp_Object symbol;
487 int code, coredump; 605 int code;
606 bool coredump;
488 Lisp_Object string, string2; 607 Lisp_Object string, string2;
489 608
490 decode_status (status, &symbol, &code, &coredump); 609 decode_status (status, &symbol, &code, &coredump);
491 610
492 if (EQ (symbol, Qsignal) || EQ (symbol, Qstop)) 611 if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
493 { 612 {
494 char *signame; 613 char const *signame;
495 synchronize_system_messages_locale (); 614 synchronize_system_messages_locale ();
496 signame = strsignal (code); 615 signame = strsignal (code);
497 if (signame == 0) 616 if (signame == 0)
@@ -500,7 +619,7 @@ status_message (struct Lisp_Process *p)
500 { 619 {
501 int c1, c2; 620 int c1, c2;
502 621
503 string = make_unibyte_string (signame, strlen (signame)); 622 string = build_unibyte_string (signame);
504 if (! NILP (Vlocale_coding_system)) 623 if (! NILP (Vlocale_coding_system))
505 string = (code_convert_string_norecord 624 string = (code_convert_string_norecord
506 (string, Vlocale_coding_system, 0)); 625 (string, Vlocale_coding_system, 0));
@@ -534,19 +653,16 @@ status_message (struct Lisp_Process *p)
534 return Fcopy_sequence (Fsymbol_name (symbol)); 653 return Fcopy_sequence (Fsymbol_name (symbol));
535} 654}
536 655
537#ifdef HAVE_PTYS 656enum { PTY_NAME_SIZE = 24 };
538
539/* The file name of the pty opened by allocate_pty. */
540static char pty_name[24];
541 657
542/* Open an available pty, returning a file descriptor. 658/* Open an available pty, returning a file descriptor.
543 Return -1 on failure. 659 Store into PTY_NAME the file name of the terminal corresponding to the pty.
544 The file name of the terminal corresponding to the pty 660 Return -1 on failure. */
545 is left in the variable pty_name. */
546 661
547static int 662static int
548allocate_pty (void) 663allocate_pty (char pty_name[PTY_NAME_SIZE])
549{ 664{
665#ifdef HAVE_PTYS
550 int fd; 666 int fd;
551 667
552#ifdef PTY_ITERATION 668#ifdef PTY_ITERATION
@@ -566,34 +682,20 @@ allocate_pty (void)
566#ifdef PTY_OPEN 682#ifdef PTY_OPEN
567 PTY_OPEN; 683 PTY_OPEN;
568#else /* no PTY_OPEN */ 684#else /* no PTY_OPEN */
569 { 685 fd = emacs_open (pty_name, O_RDWR | O_NONBLOCK, 0);
570 { /* Some systems name their pseudoterminals so that there are gaps in
571 the usual sequence - for example, on HP9000/S700 systems, there
572 are no pseudoterminals with names ending in 'f'. So we wait for
573 three failures in a row before deciding that we've reached the
574 end of the ptys. */
575 int failed_count = 0;
576 struct stat stb;
577
578 if (stat (pty_name, &stb) < 0)
579 {
580 failed_count++;
581 if (failed_count >= 3)
582 return -1;
583 }
584 else
585 failed_count = 0;
586 }
587# ifdef O_NONBLOCK
588 fd = emacs_open (pty_name, O_RDWR | O_NONBLOCK, 0);
589# else
590 fd = emacs_open (pty_name, O_RDWR | O_NDELAY, 0);
591# endif
592 }
593#endif /* no PTY_OPEN */ 686#endif /* no PTY_OPEN */
594 687
595 if (fd >= 0) 688 if (fd >= 0)
596 { 689 {
690#ifdef PTY_OPEN
691 /* Set FD's close-on-exec flag. This is needed even if
692 PT_OPEN calls posix_openpt with O_CLOEXEC, since POSIX
693 doesn't require support for that combination.
694 Multithreaded platforms where posix_openpt ignores
695 O_CLOEXEC (or where PTY_OPEN doesn't call posix_openpt)
696 have a race condition between the PTY_OPEN and here. */
697 fcntl (fd, F_SETFD, FD_CLOEXEC);
698#endif
597 /* check to make certain that both sides are available 699 /* check to make certain that both sides are available
598 this avoids a nasty yet stupid bug in rlogins */ 700 this avoids a nasty yet stupid bug in rlogins */
599#ifdef PTY_TTY_NAME_SPRINTF 701#ifdef PTY_TTY_NAME_SPRINTF
@@ -601,7 +703,7 @@ allocate_pty (void)
601#else 703#else
602 sprintf (pty_name, "/dev/tty%c%x", c, i); 704 sprintf (pty_name, "/dev/tty%c%x", c, i);
603#endif /* no PTY_TTY_NAME_SPRINTF */ 705#endif /* no PTY_TTY_NAME_SPRINTF */
604 if (access (pty_name, 6) != 0) 706 if (faccessat (AT_FDCWD, pty_name, R_OK | W_OK, AT_EACCESS) != 0)
605 { 707 {
606 emacs_close (fd); 708 emacs_close (fd);
607# ifndef __sgi 709# ifndef __sgi
@@ -614,9 +716,9 @@ allocate_pty (void)
614 return fd; 716 return fd;
615 } 717 }
616 } 718 }
719#endif /* HAVE_PTYS */
617 return -1; 720 return -1;
618} 721}
619#endif /* HAVE_PTYS */
620 722
621static Lisp_Object 723static Lisp_Object
622make_process (Lisp_Object name) 724make_process (Lisp_Object name)
@@ -627,34 +729,20 @@ make_process (Lisp_Object name)
627 printmax_t i; 729 printmax_t i;
628 730
629 p = allocate_process (); 731 p = allocate_process ();
732 /* Initialize Lisp data. Note that allocate_process initializes all
733 Lisp data to nil, so do it only for slots which should not be nil. */
734 pset_status (p, Qrun);
735 pset_mark (p, Fmake_marker ());
630 736
737 /* Initialize non-Lisp data. Note that allocate_process zeroes out all
738 non-Lisp data, so do it only for slots which should not be zero. */
631 p->infd = -1; 739 p->infd = -1;
632 p->outfd = -1; 740 p->outfd = -1;
633 p->tick = 0; 741 for (i = 0; i < PROCESS_OPEN_FDS; i++)
634 p->update_tick = 0; 742 p->open_fd[i] = -1;
635 p->pid = 0;
636 p->pty_flag = 0;
637 p->raw_status_new = 0;
638 p->status = Qrun;
639 p->mark = Fmake_marker ();
640 p->kill_without_query = 0;
641
642#ifdef ADAPTIVE_READ_BUFFERING
643 p->adaptive_read_buffering = 0;
644 p->read_output_delay = 0;
645 p->read_output_skip = 0;
646#endif
647 743
648#ifdef HAVE_GNUTLS 744#ifdef HAVE_GNUTLS
649 p->gnutls_initstage = GNUTLS_STAGE_EMPTY; 745 p->gnutls_initstage = GNUTLS_STAGE_EMPTY;
650 /* Default log level. */
651 p->gnutls_log_level = 0;
652 /* GnuTLS handshakes attempted for this connection. */
653 p->gnutls_handshakes_tried = 0;
654 p->gnutls_p = 0;
655 p->gnutls_state = NULL;
656 p->gnutls_x509_cred = NULL;
657 p->gnutls_anon_cred = NULL;
658#endif 746#endif
659 747
660 /* If name is already in use, modify it until it is unused. */ 748 /* If name is already in use, modify it until it is unused. */
@@ -664,11 +752,12 @@ make_process (Lisp_Object name)
664 { 752 {
665 tem = Fget_process (name1); 753 tem = Fget_process (name1);
666 if (NILP (tem)) break; 754 if (NILP (tem)) break;
667 sprintf (suffix, "<%"pMd">", i); 755 name1 = concat2 (name, make_formatted_string (suffix, "<%"pMd">", i));
668 name1 = concat2 (name, build_string (suffix));
669 } 756 }
670 name = name1; 757 name = name1;
671 p->name = name; 758 pset_name (p, name);
759 pset_sentinel (p, Qinternal_default_process_sentinel);
760 pset_filter (p, Qinternal_default_process_filter);
672 XSETPROCESS (val, p); 761 XSETPROCESS (val, p);
673 Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist); 762 Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
674 return val; 763 return val;
@@ -742,14 +831,26 @@ get_process (register Lisp_Object name)
742} 831}
743 832
744 833
745#ifdef SIGCHLD
746/* Fdelete_process promises to immediately forget about the process, but in 834/* Fdelete_process promises to immediately forget about the process, but in
747 reality, Emacs needs to remember those processes until they have been 835 reality, Emacs needs to remember those processes until they have been
748 treated by sigchld_handler; otherwise this handler would consider the 836 treated by the SIGCHLD handler and waitpid has been invoked on them;
749 process as being synchronous and say that the synchronous process is 837 otherwise they might fill up the kernel's process table.
750 dead. */ 838
839 Some processes created by call-process are also put onto this list.
840
841 Members of this list are (process-ID . filename) pairs. The
842 process-ID is a number; the filename, if a string, is a file that
843 needs to be removed after the process exits. */
751static Lisp_Object deleted_pid_list; 844static Lisp_Object deleted_pid_list;
752#endif 845
846void
847record_deleted_pid (pid_t pid, Lisp_Object filename)
848{
849 deleted_pid_list = Fcons (Fcons (make_fixnum_or_float (pid), filename),
850 /* GC treated elements set to nil. */
851 Fdelq (Qnil, deleted_pid_list));
852
853}
753 854
754DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0, 855DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0,
755 doc: /* Delete PROCESS: kill it and forget about it immediately. 856 doc: /* Delete PROCESS: kill it and forget about it immediately.
@@ -765,37 +866,27 @@ nil, indicating the current buffer's process. */)
765 p->raw_status_new = 0; 866 p->raw_status_new = 0;
766 if (NETCONN1_P (p) || SERIALCONN1_P (p)) 867 if (NETCONN1_P (p) || SERIALCONN1_P (p))
767 { 868 {
768 p->status = Fcons (Qexit, Fcons (make_number (0), Qnil)); 869 pset_status (p, list2 (Qexit, make_number (0)));
769 p->tick = ++process_tick; 870 p->tick = ++process_tick;
770 status_notify (p); 871 status_notify (p);
771 redisplay_preserve_echo_area (13); 872 redisplay_preserve_echo_area (13);
772 } 873 }
773 else if (p->infd >= 0) 874 else
774 { 875 {
775#ifdef SIGCHLD 876 if (p->alive)
776 Lisp_Object symbol; 877 record_kill_process (p, Qnil);
777 pid_t pid = p->pid; 878
778 879 if (p->infd >= 0)
779 /* No problem storing the pid here, as it is still in Vprocess_alist. */
780 deleted_pid_list = Fcons (make_fixnum_or_float (pid),
781 /* GC treated elements set to nil. */
782 Fdelq (Qnil, deleted_pid_list));
783 /* If the process has already signaled, remove it from the list. */
784 if (p->raw_status_new)
785 update_status (p);
786 symbol = p->status;
787 if (CONSP (p->status))
788 symbol = XCAR (p->status);
789 if (EQ (symbol, Qsignal) || EQ (symbol, Qexit))
790 deleted_pid_list
791 = Fdelete (make_fixnum_or_float (pid), deleted_pid_list);
792 else
793#endif
794 { 880 {
795 Fkill_process (process, Qnil); 881 /* Update P's status, since record_kill_process will make the
796 /* Do this now, since remove_process will make sigchld_handler do nothing. */ 882 SIGCHLD handler update deleted_pid_list, not *P. */
797 p->status 883 Lisp_Object symbol;
798 = Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil)); 884 if (p->raw_status_new)
885 update_status (p);
886 symbol = CONSP (p->status) ? XCAR (p->status) : p->status;
887 if (! (EQ (symbol, Qsignal) || EQ (symbol, Qexit)))
888 pset_status (p, list2 (Qsignal, make_number (SIGKILL)));
889
799 p->tick = ++process_tick; 890 p->tick = ++process_tick;
800 status_notify (p); 891 status_notify (p);
801 redisplay_preserve_echo_area (13); 892 redisplay_preserve_echo_area (13);
@@ -922,9 +1013,9 @@ Return BUFFER. */)
922 if (!NILP (buffer)) 1013 if (!NILP (buffer))
923 CHECK_BUFFER (buffer); 1014 CHECK_BUFFER (buffer);
924 p = XPROCESS (process); 1015 p = XPROCESS (process);
925 p->buffer = buffer; 1016 pset_buffer (p, buffer);
926 if (NETCONN1_P (p) || SERIALCONN1_P (p)) 1017 if (NETCONN1_P (p) || SERIALCONN1_P (p))
927 p->childp = Fplist_put (p->childp, QCbuffer, buffer); 1018 pset_childp (p, Fplist_put (p->childp, QCbuffer, buffer));
928 setup_process_coding_systems (process); 1019 setup_process_coding_systems (process);
929 return buffer; 1020 return buffer;
930} 1021}
@@ -950,10 +1041,10 @@ DEFUN ("process-mark", Fprocess_mark, Sprocess_mark,
950 1041
951DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter, 1042DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter,
952 2, 2, 0, 1043 2, 2, 0,
953 doc: /* Give PROCESS the filter function FILTER; nil means no filter. 1044 doc: /* Give PROCESS the filter function FILTER; nil means default.
954A value of t means stop accepting output from the process. 1045A value of t means stop accepting output from the process.
955 1046
956When a process has a filter, its buffer is not used for output. 1047When a process has a non-default filter, its buffer is not used for output.
957Instead, each time it does output, the entire string of output is 1048Instead, each time it does output, the entire string of output is
958passed to the filter. 1049passed to the filter.
959 1050
@@ -979,6 +1070,9 @@ The string argument is normally a multibyte string, except:
979 (debug) 1070 (debug)
980 (set-process-filter process ...) */ 1071 (set-process-filter process ...) */
981 1072
1073 if (NILP (filter))
1074 filter = Qinternal_default_process_filter;
1075
982 if (p->infd >= 0) 1076 if (p->infd >= 0)
983 { 1077 {
984 if (EQ (filter, Qt) && !EQ (p->status, Qlisten)) 1078 if (EQ (filter, Qt) && !EQ (p->status, Qlisten))
@@ -995,16 +1089,16 @@ The string argument is normally a multibyte string, except:
995 } 1089 }
996 } 1090 }
997 1091
998 p->filter = filter; 1092 pset_filter (p, filter);
999 if (NETCONN1_P (p) || SERIALCONN1_P (p)) 1093 if (NETCONN1_P (p) || SERIALCONN1_P (p))
1000 p->childp = Fplist_put (p->childp, QCfilter, filter); 1094 pset_childp (p, Fplist_put (p->childp, QCfilter, filter));
1001 setup_process_coding_systems (process); 1095 setup_process_coding_systems (process);
1002 return filter; 1096 return filter;
1003} 1097}
1004 1098
1005DEFUN ("process-filter", Fprocess_filter, Sprocess_filter, 1099DEFUN ("process-filter", Fprocess_filter, Sprocess_filter,
1006 1, 1, 0, 1100 1, 1, 0,
1007 doc: /* Returns the filter function of PROCESS; nil if none. 1101 doc: /* Return the filter function of PROCESS.
1008See `set-process-filter' for more info on filter functions. */) 1102See `set-process-filter' for more info on filter functions. */)
1009 (register Lisp_Object process) 1103 (register Lisp_Object process)
1010{ 1104{
@@ -1014,7 +1108,7 @@ See `set-process-filter' for more info on filter functions. */)
1014 1108
1015DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel, 1109DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel,
1016 2, 2, 0, 1110 2, 2, 0,
1017 doc: /* Give PROCESS the sentinel SENTINEL; nil for none. 1111 doc: /* Give PROCESS the sentinel SENTINEL; nil for default.
1018The sentinel is called as a function when the process changes state. 1112The sentinel is called as a function when the process changes state.
1019It gets two arguments: the process, and a string describing the change. */) 1113It gets two arguments: the process, and a string describing the change. */)
1020 (register Lisp_Object process, Lisp_Object sentinel) 1114 (register Lisp_Object process, Lisp_Object sentinel)
@@ -1024,15 +1118,18 @@ It gets two arguments: the process, and a string describing the change. */)
1024 CHECK_PROCESS (process); 1118 CHECK_PROCESS (process);
1025 p = XPROCESS (process); 1119 p = XPROCESS (process);
1026 1120
1027 p->sentinel = sentinel; 1121 if (NILP (sentinel))
1122 sentinel = Qinternal_default_process_sentinel;
1123
1124 pset_sentinel (p, sentinel);
1028 if (NETCONN1_P (p) || SERIALCONN1_P (p)) 1125 if (NETCONN1_P (p) || SERIALCONN1_P (p))
1029 p->childp = Fplist_put (p->childp, QCsentinel, sentinel); 1126 pset_childp (p, Fplist_put (p->childp, QCsentinel, sentinel));
1030 return sentinel; 1127 return sentinel;
1031} 1128}
1032 1129
1033DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel, 1130DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
1034 1, 1, 0, 1131 1, 1, 0,
1035 doc: /* Return the sentinel of PROCESS; nil if none. 1132 doc: /* Return the sentinel of PROCESS.
1036See `set-process-sentinel' for more info on sentinels. */) 1133See `set-process-sentinel' for more info on sentinels. */)
1037 (register Lisp_Object process) 1134 (register Lisp_Object process)
1038{ 1135{
@@ -1043,15 +1140,18 @@ See `set-process-sentinel' for more info on sentinels. */)
1043DEFUN ("set-process-window-size", Fset_process_window_size, 1140DEFUN ("set-process-window-size", Fset_process_window_size,
1044 Sset_process_window_size, 3, 3, 0, 1141 Sset_process_window_size, 3, 3, 0,
1045 doc: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */) 1142 doc: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1046 (register Lisp_Object process, Lisp_Object height, Lisp_Object width) 1143 (Lisp_Object process, Lisp_Object height, Lisp_Object width)
1047{ 1144{
1048 CHECK_PROCESS (process); 1145 CHECK_PROCESS (process);
1049 CHECK_RANGED_INTEGER (0, height, INT_MAX); 1146
1050 CHECK_RANGED_INTEGER (0, width, INT_MAX); 1147 /* All known platforms store window sizes as 'unsigned short'. */
1148 CHECK_RANGED_INTEGER (height, 0, USHRT_MAX);
1149 CHECK_RANGED_INTEGER (width, 0, USHRT_MAX);
1051 1150
1052 if (XPROCESS (process)->infd < 0 1151 if (XPROCESS (process)->infd < 0
1053 || set_window_size (XPROCESS (process)->infd, 1152 || (set_window_size (XPROCESS (process)->infd,
1054 XINT (height), XINT (width)) <= 0) 1153 XINT (height), XINT (width))
1154 < 0))
1055 return Qnil; 1155 return Qnil;
1056 else 1156 else
1057 return Qt; 1157 return Qt;
@@ -1107,10 +1207,6 @@ DEFUN ("process-query-on-exit-flag",
1107 return (XPROCESS (process)->kill_without_query ? Qnil : Qt); 1207 return (XPROCESS (process)->kill_without_query ? Qnil : Qt);
1108} 1208}
1109 1209
1110#ifdef DATAGRAM_SOCKETS
1111static Lisp_Object Fprocess_datagram_address (Lisp_Object);
1112#endif
1113
1114DEFUN ("process-contact", Fprocess_contact, Sprocess_contact, 1210DEFUN ("process-contact", Fprocess_contact, Sprocess_contact,
1115 1, 2, 0, 1211 1, 2, 0,
1116 doc: /* Return the contact info of PROCESS; t for a real child. 1212 doc: /* Return the contact info of PROCESS; t for a real child.
@@ -1138,11 +1234,11 @@ list of keywords. */)
1138 if ((!NETCONN_P (process) && !SERIALCONN_P (process)) || EQ (key, Qt)) 1234 if ((!NETCONN_P (process) && !SERIALCONN_P (process)) || EQ (key, Qt))
1139 return contact; 1235 return contact;
1140 if (NILP (key) && NETCONN_P (process)) 1236 if (NILP (key) && NETCONN_P (process))
1141 return Fcons (Fplist_get (contact, QChost), 1237 return list2 (Fplist_get (contact, QChost),
1142 Fcons (Fplist_get (contact, QCservice), Qnil)); 1238 Fplist_get (contact, QCservice));
1143 if (NILP (key) && SERIALCONN_P (process)) 1239 if (NILP (key) && SERIALCONN_P (process))
1144 return Fcons (Fplist_get (contact, QCport), 1240 return list2 (Fplist_get (contact, QCport),
1145 Fcons (Fplist_get (contact, QCspeed), Qnil)); 1241 Fplist_get (contact, QCspeed));
1146 return Fplist_get (contact, key); 1242 return Fplist_get (contact, key);
1147} 1243}
1148 1244
@@ -1163,7 +1259,7 @@ DEFUN ("set-process-plist", Fset_process_plist, Sset_process_plist,
1163 CHECK_PROCESS (process); 1259 CHECK_PROCESS (process);
1164 CHECK_LIST (plist); 1260 CHECK_LIST (plist);
1165 1261
1166 XPROCESS (process)->plist = plist; 1262 pset_plist (XPROCESS (process), plist);
1167 return plist; 1263 return plist;
1168} 1264}
1169 1265
@@ -1265,7 +1361,7 @@ Returns nil if format of ADDRESS is invalid. */)
1265} 1361}
1266 1362
1267DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0, 1363DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
1268 doc: /* Return a list of all processes. */) 1364 doc: /* Return a list of all processes that are Emacs sub-processes. */)
1269 (void) 1365 (void)
1270{ 1366{
1271 return Fmapcar (Qcdr, Vprocess_alist); 1367 return Fmapcar (Qcdr, Vprocess_alist);
@@ -1273,7 +1369,7 @@ DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
1273 1369
1274/* Starting asynchronous inferior processes. */ 1370/* Starting asynchronous inferior processes. */
1275 1371
1276static Lisp_Object start_process_unwind (Lisp_Object proc); 1372static void start_process_unwind (Lisp_Object proc);
1277 1373
1278DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0, 1374DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0,
1279 doc: /* Start a program in a subprocess. Return the process object for it. 1375 doc: /* Start a program in a subprocess. Return the process object for it.
@@ -1315,22 +1411,9 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1315 function. The argument list is protected by the caller, so all 1411 function. The argument list is protected by the caller, so all
1316 we really have to worry about is buffer. */ 1412 we really have to worry about is buffer. */
1317 { 1413 {
1318 struct gcpro gcpro1, gcpro2; 1414 struct gcpro gcpro1;
1319 1415 GCPRO1 (buffer);
1320 current_dir = BVAR (current_buffer, directory); 1416 current_dir = encode_current_directory ();
1321
1322 GCPRO2 (buffer, current_dir);
1323
1324 current_dir = Funhandled_file_name_directory (current_dir);
1325 if (NILP (current_dir))
1326 /* If the file name handler says that current_dir is unreachable, use
1327 a sensible default. */
1328 current_dir = build_string ("~/");
1329 current_dir = expand_and_dir_to_file (current_dir, Qnil);
1330 if (NILP (Ffile_accessible_directory_p (current_dir)))
1331 report_file_error ("Setting current directory",
1332 Fcons (BVAR (current_buffer, directory), Qnil));
1333
1334 UNGCPRO; 1417 UNGCPRO;
1335 } 1418 }
1336 1419
@@ -1349,18 +1432,18 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1349 itself; it's all taken care of here. */ 1432 itself; it's all taken care of here. */
1350 record_unwind_protect (start_process_unwind, proc); 1433 record_unwind_protect (start_process_unwind, proc);
1351 1434
1352 XPROCESS (proc)->childp = Qt; 1435 pset_childp (XPROCESS (proc), Qt);
1353 XPROCESS (proc)->plist = Qnil; 1436 pset_plist (XPROCESS (proc), Qnil);
1354 XPROCESS (proc)->type = Qreal; 1437 pset_type (XPROCESS (proc), Qreal);
1355 XPROCESS (proc)->buffer = buffer; 1438 pset_buffer (XPROCESS (proc), buffer);
1356 XPROCESS (proc)->sentinel = Qnil; 1439 pset_sentinel (XPROCESS (proc), Qinternal_default_process_sentinel);
1357 XPROCESS (proc)->filter = Qnil; 1440 pset_filter (XPROCESS (proc), Qinternal_default_process_filter);
1358 XPROCESS (proc)->command = Flist (nargs - 2, args + 2); 1441 pset_command (XPROCESS (proc), Flist (nargs - 2, args + 2));
1359 1442
1360#ifdef HAVE_GNUTLS 1443#ifdef HAVE_GNUTLS
1361 /* AKA GNUTLS_INITSTAGE(proc). */ 1444 /* AKA GNUTLS_INITSTAGE(proc). */
1362 XPROCESS (proc)->gnutls_initstage = GNUTLS_STAGE_EMPTY; 1445 XPROCESS (proc)->gnutls_initstage = GNUTLS_STAGE_EMPTY;
1363 XPROCESS (proc)->gnutls_cred_type = Qnil; 1446 pset_gnutls_cred_type (XPROCESS (proc), Qnil);
1364#endif 1447#endif
1365 1448
1366#ifdef ADAPTIVE_READ_BUFFERING 1449#ifdef ADAPTIVE_READ_BUFFERING
@@ -1388,7 +1471,7 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1388 val = Vcoding_system_for_read; 1471 val = Vcoding_system_for_read;
1389 if (NILP (val)) 1472 if (NILP (val))
1390 { 1473 {
1391 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); 1474 args2 = alloca ((nargs + 1) * sizeof *args2);
1392 args2[0] = Qstart_process; 1475 args2[0] = Qstart_process;
1393 for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; 1476 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
1394 GCPRO2 (proc, current_dir); 1477 GCPRO2 (proc, current_dir);
@@ -1400,14 +1483,14 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1400 else if (CONSP (Vdefault_process_coding_system)) 1483 else if (CONSP (Vdefault_process_coding_system))
1401 val = XCAR (Vdefault_process_coding_system); 1484 val = XCAR (Vdefault_process_coding_system);
1402 } 1485 }
1403 XPROCESS (proc)->decode_coding_system = val; 1486 pset_decode_coding_system (XPROCESS (proc), val);
1404 1487
1405 val = Vcoding_system_for_write; 1488 val = Vcoding_system_for_write;
1406 if (NILP (val)) 1489 if (NILP (val))
1407 { 1490 {
1408 if (EQ (coding_systems, Qt)) 1491 if (EQ (coding_systems, Qt))
1409 { 1492 {
1410 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); 1493 args2 = alloca ((nargs + 1) * sizeof *args2);
1411 args2[0] = Qstart_process; 1494 args2[0] = Qstart_process;
1412 for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; 1495 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
1413 GCPRO2 (proc, current_dir); 1496 GCPRO2 (proc, current_dir);
@@ -1420,7 +1503,7 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1420 else if (CONSP (Vdefault_process_coding_system)) 1503 else if (CONSP (Vdefault_process_coding_system))
1421 val = XCDR (Vdefault_process_coding_system); 1504 val = XCDR (Vdefault_process_coding_system);
1422 } 1505 }
1423 XPROCESS (proc)->encode_coding_system = val; 1506 pset_encode_coding_system (XPROCESS (proc), val);
1424 /* Note: At this moment, the above coding system may leave 1507 /* Note: At this moment, the above coding system may leave
1425 text-conversion or eol-conversion unspecified. They will be 1508 text-conversion or eol-conversion unspecified. They will be
1426 decided after we read output from the process and decode it by 1509 decided after we read output from the process and decode it by
@@ -1429,9 +1512,9 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1429 } 1512 }
1430 1513
1431 1514
1432 XPROCESS (proc)->decoding_buf = empty_unibyte_string; 1515 pset_decoding_buf (XPROCESS (proc), empty_unibyte_string);
1433 XPROCESS (proc)->decoding_carryover = 0; 1516 XPROCESS (proc)->decoding_carryover = 0;
1434 XPROCESS (proc)->encoding_buf = empty_unibyte_string; 1517 pset_encoding_buf (XPROCESS (proc), empty_unibyte_string);
1435 1518
1436 XPROCESS (proc)->inherit_coding_system_flag 1519 XPROCESS (proc)->inherit_coding_system_flag
1437 = !(NILP (buffer) || !inherit_process_coding_system); 1520 = !(NILP (buffer) || !inherit_process_coding_system);
@@ -1451,7 +1534,7 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1451 openp (Vexec_path, program, Vexec_suffixes, &tem, make_number (X_OK)); 1534 openp (Vexec_path, program, Vexec_suffixes, &tem, make_number (X_OK));
1452 UNGCPRO; 1535 UNGCPRO;
1453 if (NILP (tem)) 1536 if (NILP (tem))
1454 report_file_error ("Searching for program", Fcons (program, Qnil)); 1537 report_file_error ("Searching for program", program);
1455 tem = Fexpand_file_name (tem, Qnil); 1538 tem = Fexpand_file_name (tem, Qnil);
1456 } 1539 }
1457 else 1540 else
@@ -1474,7 +1557,7 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1474 1557
1475 /* Encode the file name and put it in NEW_ARGV. 1558 /* Encode the file name and put it in NEW_ARGV.
1476 That's where the child will use it to execute the program. */ 1559 That's where the child will use it to execute the program. */
1477 tem = Fcons (ENCODE_FILE (tem), Qnil); 1560 tem = list1 (ENCODE_FILE (tem));
1478 1561
1479 /* Here we encode arguments by the coding system used for sending 1562 /* Here we encode arguments by the coding system used for sending
1480 data to the process. We don't support using different coding 1563 data to the process. We don't support using different coding
@@ -1501,7 +1584,7 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1501 1584
1502 /* Now that everything is encoded we can collect the strings into 1585 /* Now that everything is encoded we can collect the strings into
1503 NEW_ARGV. */ 1586 NEW_ARGV. */
1504 new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *)); 1587 new_argv = alloca ((nargs - 1) * sizeof *new_argv);
1505 new_argv[nargs - 2] = 0; 1588 new_argv[nargs - 2] = 0;
1506 1589
1507 for (i = nargs - 2; i-- != 0; ) 1590 for (i = nargs - 2; i-- != 0; )
@@ -1522,442 +1605,321 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1522 PROC doesn't have its pid set, then we know someone has signaled 1605 PROC doesn't have its pid set, then we know someone has signaled
1523 an error and the process wasn't started successfully, so we should 1606 an error and the process wasn't started successfully, so we should
1524 remove it from the process list. */ 1607 remove it from the process list. */
1525static Lisp_Object 1608static void
1526start_process_unwind (Lisp_Object proc) 1609start_process_unwind (Lisp_Object proc)
1527{ 1610{
1528 if (!PROCESSP (proc)) 1611 if (!PROCESSP (proc))
1529 abort (); 1612 emacs_abort ();
1530 1613
1531 /* Was PROC started successfully? 1614 /* Was PROC started successfully?
1532 -2 is used for a pty with no process, eg for gdb. */ 1615 -2 is used for a pty with no process, eg for gdb. */
1533 if (XPROCESS (proc)->pid <= 0 && XPROCESS (proc)->pid != -2) 1616 if (XPROCESS (proc)->pid <= 0 && XPROCESS (proc)->pid != -2)
1534 remove_process (proc); 1617 remove_process (proc);
1535
1536 return Qnil;
1537} 1618}
1538 1619
1620/* If *FD_ADDR is nonnegative, close it, and mark it as closed. */
1621
1539static void 1622static void
1540create_process_1 (struct atimer *timer) 1623close_process_fd (int *fd_addr)
1541{ 1624{
1542 /* Nothing to do. */ 1625 int fd = *fd_addr;
1626 if (0 <= fd)
1627 {
1628 *fd_addr = -1;
1629 emacs_close (fd);
1630 }
1543} 1631}
1544 1632
1633/* Indexes of file descriptors in open_fds. */
1634enum
1635 {
1636 /* The pipe from Emacs to its subprocess. */
1637 SUBPROCESS_STDIN,
1638 WRITE_TO_SUBPROCESS,
1639
1640 /* The main pipe from the subprocess to Emacs. */
1641 READ_FROM_SUBPROCESS,
1642 SUBPROCESS_STDOUT,
1643
1644 /* The pipe from the subprocess to Emacs that is closed when the
1645 subprocess execs. */
1646 READ_FROM_EXEC_MONITOR,
1647 EXEC_MONITOR_OUTPUT
1648 };
1649
1650verify (PROCESS_OPEN_FDS == EXEC_MONITOR_OUTPUT + 1);
1545 1651
1546static void 1652static void
1547create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir) 1653create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1548{ 1654{
1655 struct Lisp_Process *p = XPROCESS (process);
1549 int inchannel, outchannel; 1656 int inchannel, outchannel;
1550 pid_t pid; 1657 pid_t pid;
1551 int sv[2]; 1658 int vfork_errno;
1552#if !defined (WINDOWSNT) && defined (FD_CLOEXEC) 1659 int forkin, forkout;
1553 int wait_child_setup[2]; 1660 bool pty_flag = 0;
1554#endif 1661 char pty_name[PTY_NAME_SIZE];
1555 sigset_t procmask; 1662 Lisp_Object lisp_pty_name = Qnil;
1556 sigset_t blocked;
1557 struct sigaction sigint_action;
1558 struct sigaction sigquit_action;
1559 struct sigaction sigpipe_action;
1560#ifdef AIX
1561 struct sigaction sighup_action;
1562#endif
1563 /* Use volatile to protect variables from being clobbered by longjmp. */
1564 volatile int forkin, forkout;
1565 volatile int pty_flag = 0;
1566#ifndef USE_CRT_DLL
1567 extern char **environ;
1568#endif
1569 1663
1570 inchannel = outchannel = -1; 1664 inchannel = outchannel = -1;
1571 1665
1572#ifdef HAVE_PTYS
1573 if (!NILP (Vprocess_connection_type)) 1666 if (!NILP (Vprocess_connection_type))
1574 outchannel = inchannel = allocate_pty (); 1667 outchannel = inchannel = allocate_pty (pty_name);
1575 1668
1576 if (inchannel >= 0) 1669 if (inchannel >= 0)
1577 { 1670 {
1671 p->open_fd[READ_FROM_SUBPROCESS] = inchannel;
1578#if ! defined (USG) || defined (USG_SUBTTY_WORKS) 1672#if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1579 /* On most USG systems it does not work to open the pty's tty here, 1673 /* On most USG systems it does not work to open the pty's tty here,
1580 then close it and reopen it in the child. */ 1674 then close it and reopen it in the child. */
1581#ifdef O_NOCTTY
1582 /* Don't let this terminal become our controlling terminal 1675 /* Don't let this terminal become our controlling terminal
1583 (in case we don't have one). */ 1676 (in case we don't have one). */
1584 forkout = forkin = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0); 1677 forkout = forkin = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0);
1585#else
1586 forkout = forkin = emacs_open (pty_name, O_RDWR, 0);
1587#endif
1588 if (forkin < 0) 1678 if (forkin < 0)
1589 report_file_error ("Opening pty", Qnil); 1679 report_file_error ("Opening pty", Qnil);
1680 p->open_fd[SUBPROCESS_STDIN] = forkin;
1590#else 1681#else
1591 forkin = forkout = -1; 1682 forkin = forkout = -1;
1592#endif /* not USG, or USG_SUBTTY_WORKS */ 1683#endif /* not USG, or USG_SUBTTY_WORKS */
1593 pty_flag = 1; 1684 pty_flag = 1;
1685 lisp_pty_name = build_string (pty_name);
1594 } 1686 }
1595 else 1687 else
1596#endif /* HAVE_PTYS */
1597 { 1688 {
1598 int tem; 1689 if (emacs_pipe (p->open_fd + SUBPROCESS_STDIN) != 0
1599 tem = pipe (sv); 1690 || emacs_pipe (p->open_fd + READ_FROM_SUBPROCESS) != 0)
1600 if (tem < 0)
1601 report_file_error ("Creating pipe", Qnil); 1691 report_file_error ("Creating pipe", Qnil);
1602 inchannel = sv[0]; 1692 forkin = p->open_fd[SUBPROCESS_STDIN];
1603 forkout = sv[1]; 1693 outchannel = p->open_fd[WRITE_TO_SUBPROCESS];
1604 tem = pipe (sv); 1694 inchannel = p->open_fd[READ_FROM_SUBPROCESS];
1605 if (tem < 0) 1695 forkout = p->open_fd[SUBPROCESS_STDOUT];
1606 {
1607 emacs_close (inchannel);
1608 emacs_close (forkout);
1609 report_file_error ("Creating pipe", Qnil);
1610 }
1611 outchannel = sv[1];
1612 forkin = sv[0];
1613 } 1696 }
1614 1697
1615#if !defined (WINDOWSNT) && defined (FD_CLOEXEC) 1698#ifndef WINDOWSNT
1616 { 1699 if (emacs_pipe (p->open_fd + READ_FROM_EXEC_MONITOR) != 0)
1617 int tem; 1700 report_file_error ("Creating pipe", Qnil);
1618
1619 tem = pipe (wait_child_setup);
1620 if (tem < 0)
1621 report_file_error ("Creating pipe", Qnil);
1622 tem = fcntl (wait_child_setup[1], F_GETFD, 0);
1623 if (tem >= 0)
1624 tem = fcntl (wait_child_setup[1], F_SETFD, tem | FD_CLOEXEC);
1625 if (tem < 0)
1626 {
1627 emacs_close (wait_child_setup[0]);
1628 emacs_close (wait_child_setup[1]);
1629 report_file_error ("Setting file descriptor flags", Qnil);
1630 }
1631 }
1632#endif 1701#endif
1633 1702
1634#ifdef O_NONBLOCK
1635 fcntl (inchannel, F_SETFL, O_NONBLOCK); 1703 fcntl (inchannel, F_SETFL, O_NONBLOCK);
1636 fcntl (outchannel, F_SETFL, O_NONBLOCK); 1704 fcntl (outchannel, F_SETFL, O_NONBLOCK);
1637#else
1638#ifdef O_NDELAY
1639 fcntl (inchannel, F_SETFL, O_NDELAY);
1640 fcntl (outchannel, F_SETFL, O_NDELAY);
1641#endif
1642#endif
1643 1705
1644 /* Record this as an active process, with its channels. 1706 /* Record this as an active process, with its channels. */
1645 As a result, child_setup will close Emacs's side of the pipes. */
1646 chan_process[inchannel] = process; 1707 chan_process[inchannel] = process;
1647 XPROCESS (process)->infd = inchannel; 1708 p->infd = inchannel;
1648 XPROCESS (process)->outfd = outchannel; 1709 p->outfd = outchannel;
1649 1710
1650 /* Previously we recorded the tty descriptor used in the subprocess. 1711 /* Previously we recorded the tty descriptor used in the subprocess.
1651 It was only used for getting the foreground tty process, so now 1712 It was only used for getting the foreground tty process, so now
1652 we just reopen the device (see emacs_get_tty_pgrp) as this is 1713 we just reopen the device (see emacs_get_tty_pgrp) as this is
1653 more portable (see USG_SUBTTY_WORKS above). */ 1714 more portable (see USG_SUBTTY_WORKS above). */
1654 1715
1655 XPROCESS (process)->pty_flag = pty_flag; 1716 p->pty_flag = pty_flag;
1656 XPROCESS (process)->status = Qrun; 1717 pset_status (p, Qrun);
1657
1658 /* Delay interrupts until we have a chance to store
1659 the new fork's pid in its process structure */
1660 sigemptyset (&blocked);
1661#ifdef SIGCHLD
1662 sigaddset (&blocked, SIGCHLD);
1663#endif
1664#ifdef HAVE_WORKING_VFORK
1665 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1666 this sets the parent's signal handlers as well as the child's.
1667 So delay all interrupts whose handlers the child might munge,
1668 and record the current handlers so they can be restored later. */
1669 sigaddset (&blocked, SIGINT ); sigaction (SIGINT , 0, &sigint_action );
1670 sigaddset (&blocked, SIGQUIT); sigaction (SIGQUIT, 0, &sigquit_action);
1671 sigaddset (&blocked, SIGPIPE); sigaction (SIGPIPE, 0, &sigpipe_action);
1672#ifdef AIX
1673 sigaddset (&blocked, SIGHUP ); sigaction (SIGHUP , 0, &sighup_action );
1674#endif
1675#endif /* HAVE_WORKING_VFORK */
1676 pthread_sigmask (SIG_BLOCK, &blocked, &procmask);
1677 1718
1678 FD_SET (inchannel, &input_wait_mask); 1719 FD_SET (inchannel, &input_wait_mask);
1679 FD_SET (inchannel, &non_keyboard_wait_mask); 1720 FD_SET (inchannel, &non_keyboard_wait_mask);
1680 if (inchannel > max_process_desc) 1721 if (inchannel > max_process_desc)
1681 max_process_desc = inchannel; 1722 max_process_desc = inchannel;
1682 1723
1683 /* Until we store the proper pid, enable sigchld_handler 1724 /* This may signal an error. */
1684 to recognize an unknown pid as standing for this process.
1685 It is very important not to let this `marker' value stay
1686 in the table after this function has returned; if it does
1687 it might cause call-process to hang and subsequent asynchronous
1688 processes to get their return values scrambled. */
1689 XPROCESS (process)->pid = -1;
1690
1691 /* This must be called after the above line because it may signal an
1692 error. */
1693 setup_process_coding_systems (process); 1725 setup_process_coding_systems (process);
1694 1726
1695 BLOCK_INPUT; 1727 block_input ();
1728 block_child_signal ();
1696 1729
1730#ifndef WINDOWSNT
1731 /* vfork, and prevent local vars from being clobbered by the vfork. */
1697 { 1732 {
1698 /* child_setup must clobber environ on systems with true vfork. 1733 Lisp_Object volatile current_dir_volatile = current_dir;
1699 Protect it from permanent change. */ 1734 Lisp_Object volatile lisp_pty_name_volatile = lisp_pty_name;
1700 char **save_environ = environ; 1735 char **volatile new_argv_volatile = new_argv;
1701 volatile Lisp_Object encoded_current_dir = ENCODE_FILE (current_dir); 1736 int volatile forkin_volatile = forkin;
1737 int volatile forkout_volatile = forkout;
1738 struct Lisp_Process *p_volatile = p;
1702 1739
1703#ifndef WINDOWSNT
1704 pid = vfork (); 1740 pid = vfork ();
1705 if (pid == 0)
1706#endif /* not WINDOWSNT */
1707 {
1708 int xforkin = forkin;
1709 int xforkout = forkout;
1710 1741
1711#if 0 /* This was probably a mistake--it duplicates code later on, 1742 current_dir = current_dir_volatile;
1712 but fails to handle all the cases. */ 1743 lisp_pty_name = lisp_pty_name_volatile;
1713 /* Make sure SIGCHLD is not blocked in the child. */ 1744 new_argv = new_argv_volatile;
1714 sigsetmask (SIGEMPTYMASK); 1745 forkin = forkin_volatile;
1715#endif 1746 forkout = forkout_volatile;
1747 p = p_volatile;
1748
1749 pty_flag = p->pty_flag;
1750 }
1716 1751
1717 /* Make the pty be the controlling terminal of the process. */ 1752 if (pid == 0)
1753#endif /* not WINDOWSNT */
1754 {
1755 int xforkin = forkin;
1756 int xforkout = forkout;
1757
1758 /* Make the pty be the controlling terminal of the process. */
1718#ifdef HAVE_PTYS 1759#ifdef HAVE_PTYS
1719 /* First, disconnect its current controlling terminal. */ 1760 /* First, disconnect its current controlling terminal. */
1720#ifdef HAVE_SETSID 1761 /* We tried doing setsid only if pty_flag, but it caused
1721 /* We tried doing setsid only if pty_flag, but it caused 1762 process_set_signal to fail on SGI when using a pipe. */
1722 process_set_signal to fail on SGI when using a pipe. */ 1763 setsid ();
1723 setsid (); 1764 /* Make the pty's terminal the controlling terminal. */
1724 /* Make the pty's terminal the controlling terminal. */ 1765 if (pty_flag && xforkin >= 0)
1725 if (pty_flag && xforkin >= 0) 1766 {
1726 {
1727#ifdef TIOCSCTTY 1767#ifdef TIOCSCTTY
1728 /* We ignore the return value 1768 /* We ignore the return value
1729 because faith@cs.unc.edu says that is necessary on Linux. */ 1769 because faith@cs.unc.edu says that is necessary on Linux. */
1730 ioctl (xforkin, TIOCSCTTY, 0); 1770 ioctl (xforkin, TIOCSCTTY, 0);
1731#endif 1771#endif
1732 } 1772 }
1733#else /* not HAVE_SETSID */
1734#ifdef USG
1735 /* It's very important to call setpgrp here and no time
1736 afterwards. Otherwise, we lose our controlling tty which
1737 is set when we open the pty. */
1738 setpgrp ();
1739#endif /* USG */
1740#endif /* not HAVE_SETSID */
1741#if defined (LDISC1) 1773#if defined (LDISC1)
1742 if (pty_flag && xforkin >= 0) 1774 if (pty_flag && xforkin >= 0)
1743 { 1775 {
1744 struct termios t; 1776 struct termios t;
1745 tcgetattr (xforkin, &t); 1777 tcgetattr (xforkin, &t);
1746 t.c_lflag = LDISC1; 1778 t.c_lflag = LDISC1;
1747 if (tcsetattr (xforkin, TCSANOW, &t) < 0) 1779 if (tcsetattr (xforkin, TCSANOW, &t) < 0)
1748 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39); 1780 emacs_perror ("create_process/tcsetattr LDISC1");
1749 } 1781 }
1750#else 1782#else
1751#if defined (NTTYDISC) && defined (TIOCSETD) 1783#if defined (NTTYDISC) && defined (TIOCSETD)
1752 if (pty_flag && xforkin >= 0) 1784 if (pty_flag && xforkin >= 0)
1753 { 1785 {
1754 /* Use new line discipline. */ 1786 /* Use new line discipline. */
1755 int ldisc = NTTYDISC; 1787 int ldisc = NTTYDISC;
1756 ioctl (xforkin, TIOCSETD, &ldisc); 1788 ioctl (xforkin, TIOCSETD, &ldisc);
1757 } 1789 }
1758#endif 1790#endif
1759#endif 1791#endif
1760#ifdef TIOCNOTTY 1792#ifdef TIOCNOTTY
1761 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you 1793 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1762 can do TIOCSPGRP only to the process's controlling tty. */ 1794 can do TIOCSPGRP only to the process's controlling tty. */
1763 if (pty_flag) 1795 if (pty_flag)
1764 { 1796 {
1765 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here? 1797 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1766 I can't test it since I don't have 4.3. */ 1798 I can't test it since I don't have 4.3. */
1767 int j = emacs_open ("/dev/tty", O_RDWR, 0); 1799 int j = emacs_open ("/dev/tty", O_RDWR, 0);
1768 if (j >= 0) 1800 if (j >= 0)
1769 { 1801 {
1770 ioctl (j, TIOCNOTTY, 0); 1802 ioctl (j, TIOCNOTTY, 0);
1771 emacs_close (j); 1803 emacs_close (j);
1772 } 1804 }
1773#ifndef USG 1805 }
1774 /* In order to get a controlling terminal on some versions
1775 of BSD, it is necessary to put the process in pgrp 0
1776 before it opens the terminal. */
1777#ifdef HAVE_SETPGID
1778 setpgid (0, 0);
1779#else
1780 setpgrp (0, 0);
1781#endif
1782#endif
1783 }
1784#endif /* TIOCNOTTY */ 1806#endif /* TIOCNOTTY */
1785 1807
1786#if !defined (DONT_REOPEN_PTY) 1808#if !defined (DONT_REOPEN_PTY)
1787/*** There is a suggestion that this ought to be a 1809/*** There is a suggestion that this ought to be a
1788 conditional on TIOCSPGRP, 1810 conditional on TIOCSPGRP, or !defined TIOCSCTTY.
1789 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
1790 Trying the latter gave the wrong results on Debian GNU/Linux 1.1; 1811 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
1791 that system does seem to need this code, even though 1812 that system does seem to need this code, even though
1792 both HAVE_SETSID and TIOCSCTTY are defined. */ 1813 both TIOCSCTTY is defined. */
1793 /* Now close the pty (if we had it open) and reopen it. 1814 /* Now close the pty (if we had it open) and reopen it.
1794 This makes the pty the controlling terminal of the subprocess. */ 1815 This makes the pty the controlling terminal of the subprocess. */
1795 if (pty_flag) 1816 if (pty_flag)
1796 { 1817 {
1797 1818
1798 /* I wonder if emacs_close (emacs_open (pty_name, ...)) 1819 /* I wonder if emacs_close (emacs_open (SSDATA (lisp_pty_name), ...))
1799 would work? */ 1820 would work? */
1800 if (xforkin >= 0) 1821 if (xforkin >= 0)
1801 emacs_close (xforkin); 1822 emacs_close (xforkin);
1802 xforkout = xforkin = emacs_open (pty_name, O_RDWR, 0); 1823 xforkout = xforkin = emacs_open (SSDATA (lisp_pty_name), O_RDWR, 0);
1803 1824
1804 if (xforkin < 0) 1825 if (xforkin < 0)
1805 { 1826 {
1806 emacs_write (1, "Couldn't open the pty terminal ", 31); 1827 emacs_perror (SSDATA (lisp_pty_name));
1807 emacs_write (1, pty_name, strlen (pty_name)); 1828 _exit (EXIT_CANCELED);
1808 emacs_write (1, "\n", 1); 1829 }
1809 _exit (1);
1810 }
1811 1830
1812 } 1831 }
1813#endif /* not DONT_REOPEN_PTY */ 1832#endif /* not DONT_REOPEN_PTY */
1814 1833
1815#ifdef SETUP_SLAVE_PTY 1834#ifdef SETUP_SLAVE_PTY
1816 if (pty_flag) 1835 if (pty_flag)
1817 { 1836 {
1818 SETUP_SLAVE_PTY; 1837 SETUP_SLAVE_PTY;
1819 } 1838 }
1820#endif /* SETUP_SLAVE_PTY */ 1839#endif /* SETUP_SLAVE_PTY */
1821#ifdef AIX
1822 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
1823 Now reenable it in the child, so it will die when we want it to. */
1824 if (pty_flag)
1825 signal (SIGHUP, SIG_DFL);
1826#endif
1827#endif /* HAVE_PTYS */ 1840#endif /* HAVE_PTYS */
1828 1841
1829 signal (SIGINT, SIG_DFL); 1842 signal (SIGINT, SIG_DFL);
1830 signal (SIGQUIT, SIG_DFL); 1843 signal (SIGQUIT, SIG_DFL);
1831 /* GConf causes us to ignore SIGPIPE, make sure it is restored
1832 in the child. */
1833 signal (SIGPIPE, SIG_DFL);
1834 1844
1835 /* Stop blocking signals in the child. */ 1845 /* Emacs ignores SIGPIPE, but the child should not. */
1836 pthread_sigmask (SIG_SETMASK, &procmask, 0); 1846 signal (SIGPIPE, SIG_DFL);
1837 1847
1838 if (pty_flag) 1848 /* Stop blocking SIGCHLD in the child. */
1839 child_setup_tty (xforkout); 1849 unblock_child_signal ();
1850
1851 if (pty_flag)
1852 child_setup_tty (xforkout);
1840#ifdef WINDOWSNT 1853#ifdef WINDOWSNT
1841 pid = child_setup (xforkin, xforkout, xforkout, 1854 pid = child_setup (xforkin, xforkout, xforkout, new_argv, 1, current_dir);
1842 new_argv, 1, encoded_current_dir);
1843#else /* not WINDOWSNT */ 1855#else /* not WINDOWSNT */
1844#ifdef FD_CLOEXEC 1856 child_setup (xforkin, xforkout, xforkout, new_argv, 1, current_dir);
1845 emacs_close (wait_child_setup[0]);
1846#endif
1847 child_setup (xforkin, xforkout, xforkout,
1848 new_argv, 1, encoded_current_dir);
1849#endif /* not WINDOWSNT */ 1857#endif /* not WINDOWSNT */
1850 } 1858 }
1851 environ = save_environ;
1852 }
1853 1859
1854 UNBLOCK_INPUT; 1860 /* Back in the parent process. */
1861
1862 vfork_errno = errno;
1863 p->pid = pid;
1864 if (pid >= 0)
1865 p->alive = 1;
1866
1867 /* Stop blocking in the parent. */
1868 unblock_child_signal ();
1869 unblock_input ();
1855 1870
1856 /* This runs in the Emacs process. */
1857 if (pid < 0) 1871 if (pid < 0)
1858 { 1872 report_file_errno ("Doing vfork", Qnil, vfork_errno);
1859 if (forkin >= 0)
1860 emacs_close (forkin);
1861 if (forkin != forkout && forkout >= 0)
1862 emacs_close (forkout);
1863 }
1864 else 1873 else
1865 { 1874 {
1866 /* vfork succeeded. */ 1875 /* vfork succeeded. */
1867 XPROCESS (process)->pid = pid; 1876
1877 /* Close the pipe ends that the child uses, or the child's pty. */
1878 close_process_fd (&p->open_fd[SUBPROCESS_STDIN]);
1879 close_process_fd (&p->open_fd[SUBPROCESS_STDOUT]);
1868 1880
1869#ifdef WINDOWSNT 1881#ifdef WINDOWSNT
1870 register_child (pid, inchannel); 1882 register_child (pid, inchannel);
1871#endif /* WINDOWSNT */ 1883#endif /* WINDOWSNT */
1872 1884
1873 /* If the subfork execv fails, and it exits, 1885 pset_tty_name (p, lisp_pty_name);
1874 this close hangs. I don't know why.
1875 So have an interrupt jar it loose. */
1876 {
1877 struct atimer *timer;
1878 EMACS_TIME offset;
1879
1880 stop_polling ();
1881 EMACS_SET_SECS_USECS (offset, 1, 0);
1882 timer = start_atimer (ATIMER_RELATIVE, offset, create_process_1, 0);
1883
1884 if (forkin >= 0)
1885 emacs_close (forkin);
1886
1887 cancel_atimer (timer);
1888 start_polling ();
1889 }
1890
1891 if (forkin != forkout && forkout >= 0)
1892 emacs_close (forkout);
1893
1894#ifdef HAVE_PTYS
1895 if (pty_flag)
1896 XPROCESS (process)->tty_name = build_string (pty_name);
1897 else
1898#endif
1899 XPROCESS (process)->tty_name = Qnil;
1900 1886
1901#if !defined (WINDOWSNT) && defined (FD_CLOEXEC) 1887#ifndef WINDOWSNT
1902 /* Wait for child_setup to complete in case that vfork is 1888 /* Wait for child_setup to complete in case that vfork is
1903 actually defined as fork. The descriptor wait_child_setup[1] 1889 actually defined as fork. The descriptor
1890 XPROCESS (proc)->open_fd[EXEC_MONITOR_OUTPUT]
1904 of a pipe is closed at the child side either by close-on-exec 1891 of a pipe is closed at the child side either by close-on-exec
1905 on successful execvp or the _exit call in child_setup. */ 1892 on successful execve or the _exit call in child_setup. */
1906 { 1893 {
1907 char dummy; 1894 char dummy;
1908 1895
1909 emacs_close (wait_child_setup[1]); 1896 close_process_fd (&p->open_fd[EXEC_MONITOR_OUTPUT]);
1910 emacs_read (wait_child_setup[0], &dummy, 1); 1897 emacs_read (p->open_fd[READ_FROM_EXEC_MONITOR], &dummy, 1);
1911 emacs_close (wait_child_setup[0]); 1898 close_process_fd (&p->open_fd[READ_FROM_EXEC_MONITOR]);
1912 } 1899 }
1913#endif 1900#endif
1914 } 1901 }
1915
1916 /* Restore the signal state whether vfork succeeded or not.
1917 (We will signal an error, below, if it failed.) */
1918#ifdef HAVE_WORKING_VFORK
1919 /* Restore the parent's signal handlers. */
1920 sigaction (SIGINT, &sigint_action, 0);
1921 sigaction (SIGQUIT, &sigquit_action, 0);
1922 sigaction (SIGPIPE, &sigpipe_action, 0);
1923#ifdef AIX
1924 sigaction (SIGHUP, &sighup_action, 0);
1925#endif
1926#endif /* HAVE_WORKING_VFORK */
1927 /* Stop blocking signals in the parent. */
1928 pthread_sigmask (SIG_SETMASK, &procmask, 0);
1929
1930 /* Now generate the error if vfork failed. */
1931 if (pid < 0)
1932 report_file_error ("Doing vfork", Qnil);
1933} 1902}
1934 1903
1935void 1904static void
1936create_pty (Lisp_Object process) 1905create_pty (Lisp_Object process)
1937{ 1906{
1938 int inchannel, outchannel; 1907 struct Lisp_Process *p = XPROCESS (process);
1939 int pty_flag = 0; 1908 char pty_name[PTY_NAME_SIZE];
1940 1909 int pty_fd = NILP (Vprocess_connection_type) ? -1 : allocate_pty (pty_name);
1941 inchannel = outchannel = -1;
1942
1943#ifdef HAVE_PTYS
1944 if (!NILP (Vprocess_connection_type))
1945 outchannel = inchannel = allocate_pty ();
1946 1910
1947 if (inchannel >= 0) 1911 if (pty_fd >= 0)
1948 { 1912 {
1913 p->open_fd[SUBPROCESS_STDIN] = pty_fd;
1949#if ! defined (USG) || defined (USG_SUBTTY_WORKS) 1914#if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1950 /* On most USG systems it does not work to open the pty's tty here, 1915 /* On most USG systems it does not work to open the pty's tty here,
1951 then close it and reopen it in the child. */ 1916 then close it and reopen it in the child. */
1952#ifdef O_NOCTTY
1953 /* Don't let this terminal become our controlling terminal 1917 /* Don't let this terminal become our controlling terminal
1954 (in case we don't have one). */ 1918 (in case we don't have one). */
1955 int forkout = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0); 1919 int forkout = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0);
1956#else
1957 int forkout = emacs_open (pty_name, O_RDWR, 0);
1958#endif
1959 if (forkout < 0) 1920 if (forkout < 0)
1960 report_file_error ("Opening pty", Qnil); 1921 report_file_error ("Opening pty", Qnil);
1922 p->open_fd[WRITE_TO_SUBPROCESS] = forkout;
1961#if defined (DONT_REOPEN_PTY) 1923#if defined (DONT_REOPEN_PTY)
1962 /* In the case that vfork is defined as fork, the parent process 1924 /* In the case that vfork is defined as fork, the parent process
1963 (Emacs) may send some data before the child process completes 1925 (Emacs) may send some data before the child process completes
@@ -1965,47 +1927,33 @@ create_pty (Lisp_Object process)
1965 child_setup_tty (forkout); 1927 child_setup_tty (forkout);
1966#endif /* DONT_REOPEN_PTY */ 1928#endif /* DONT_REOPEN_PTY */
1967#endif /* not USG, or USG_SUBTTY_WORKS */ 1929#endif /* not USG, or USG_SUBTTY_WORKS */
1968 pty_flag = 1;
1969 }
1970#endif /* HAVE_PTYS */
1971 1930
1972#ifdef O_NONBLOCK 1931 fcntl (pty_fd, F_SETFL, O_NONBLOCK);
1973 fcntl (inchannel, F_SETFL, O_NONBLOCK);
1974 fcntl (outchannel, F_SETFL, O_NONBLOCK);
1975#else
1976#ifdef O_NDELAY
1977 fcntl (inchannel, F_SETFL, O_NDELAY);
1978 fcntl (outchannel, F_SETFL, O_NDELAY);
1979#endif
1980#endif
1981 1932
1982 /* Record this as an active process, with its channels. 1933 /* Record this as an active process, with its channels.
1983 As a result, child_setup will close Emacs's side of the pipes. */ 1934 As a result, child_setup will close Emacs's side of the pipes. */
1984 chan_process[inchannel] = process; 1935 chan_process[pty_fd] = process;
1985 XPROCESS (process)->infd = inchannel; 1936 p->infd = pty_fd;
1986 XPROCESS (process)->outfd = outchannel; 1937 p->outfd = pty_fd;
1987 1938
1988 /* Previously we recorded the tty descriptor used in the subprocess. 1939 /* Previously we recorded the tty descriptor used in the subprocess.
1989 It was only used for getting the foreground tty process, so now 1940 It was only used for getting the foreground tty process, so now
1990 we just reopen the device (see emacs_get_tty_pgrp) as this is 1941 we just reopen the device (see emacs_get_tty_pgrp) as this is
1991 more portable (see USG_SUBTTY_WORKS above). */ 1942 more portable (see USG_SUBTTY_WORKS above). */
1992 1943
1993 XPROCESS (process)->pty_flag = pty_flag; 1944 p->pty_flag = 1;
1994 XPROCESS (process)->status = Qrun; 1945 pset_status (p, Qrun);
1995 setup_process_coding_systems (process); 1946 setup_process_coding_systems (process);
1996 1947
1997 FD_SET (inchannel, &input_wait_mask); 1948 FD_SET (pty_fd, &input_wait_mask);
1998 FD_SET (inchannel, &non_keyboard_wait_mask); 1949 FD_SET (pty_fd, &non_keyboard_wait_mask);
1999 if (inchannel > max_process_desc) 1950 if (pty_fd > max_process_desc)
2000 max_process_desc = inchannel; 1951 max_process_desc = pty_fd;
2001 1952
2002 XPROCESS (process)->pid = -2; 1953 pset_tty_name (p, build_string (pty_name));
2003#ifdef HAVE_PTYS 1954 }
2004 if (pty_flag) 1955
2005 XPROCESS (process)->tty_name = build_string (pty_name); 1956 p->pid = -2;
2006 else
2007#endif
2008 XPROCESS (process)->tty_name = Qnil;
2009} 1957}
2010 1958
2011 1959
@@ -2230,7 +2178,7 @@ Returns nil upon error setting address, ADDRESS otherwise. */)
2230 channel = XPROCESS (process)->infd; 2178 channel = XPROCESS (process)->infd;
2231 2179
2232 len = get_lisp_to_sockaddr_size (address, &family); 2180 len = get_lisp_to_sockaddr_size (address, &family);
2233 if (datagram_address[channel].len != len) 2181 if (len == 0 || datagram_address[channel].len != len)
2234 return Qnil; 2182 return Qnil;
2235 conv_lisp_to_sockaddr (family, address, datagram_address[channel].sa, len); 2183 conv_lisp_to_sockaddr (family, address, datagram_address[channel].sa, len);
2236 return address; 2184 return address;
@@ -2365,8 +2313,12 @@ set_socket_option (int s, Lisp_Object opt, Lisp_Object val)
2365 } 2313 }
2366 2314
2367 if (ret < 0) 2315 if (ret < 0)
2368 report_file_error ("Cannot set network option", 2316 {
2369 Fcons (opt, Fcons (val, Qnil))); 2317 int setsockopt_errno = errno;
2318 report_file_errno ("Cannot set network option", list2 (opt, val),
2319 setsockopt_errno);
2320 }
2321
2370 return (1 << sopt->optbit); 2322 return (1 << sopt->optbit);
2371} 2323}
2372 2324
@@ -2394,7 +2346,7 @@ OPTION is not a supported option, return nil instead; otherwise return t. */)
2394 2346
2395 if (set_socket_option (s, option, value)) 2347 if (set_socket_option (s, option, value))
2396 { 2348 {
2397 p->childp = Fplist_put (p->childp, option, value); 2349 pset_childp (p, Fplist_put (p->childp, option, value));
2398 return Qt; 2350 return Qt;
2399 } 2351 }
2400 2352
@@ -2498,16 +2450,6 @@ usage: (serial-process-configure &rest ARGS) */)
2498 return Qnil; 2450 return Qnil;
2499} 2451}
2500 2452
2501/* Used by make-serial-process to recover from errors. */
2502static Lisp_Object
2503make_serial_process_unwind (Lisp_Object proc)
2504{
2505 if (!PROCESSP (proc))
2506 abort ();
2507 remove_process (proc);
2508 return Qnil;
2509}
2510
2511DEFUN ("make-serial-process", Fmake_serial_process, Smake_serial_process, 2453DEFUN ("make-serial-process", Fmake_serial_process, Smake_serial_process,
2512 0, MANY, 0, 2454 0, MANY, 0,
2513 doc: /* Create and return a serial port process. 2455 doc: /* Create and return a serial port process.
@@ -2589,7 +2531,7 @@ usage: (make-serial-process &rest ARGS) */)
2589 struct gcpro gcpro1; 2531 struct gcpro gcpro1;
2590 Lisp_Object name, buffer; 2532 Lisp_Object name, buffer;
2591 Lisp_Object tem, val; 2533 Lisp_Object tem, val;
2592 ptrdiff_t specpdl_count = -1; 2534 ptrdiff_t specpdl_count;
2593 2535
2594 if (nargs == 0) 2536 if (nargs == 0)
2595 return Qnil; 2537 return Qnil;
@@ -2613,10 +2555,11 @@ usage: (make-serial-process &rest ARGS) */)
2613 CHECK_STRING (name); 2555 CHECK_STRING (name);
2614 proc = make_process (name); 2556 proc = make_process (name);
2615 specpdl_count = SPECPDL_INDEX (); 2557 specpdl_count = SPECPDL_INDEX ();
2616 record_unwind_protect (make_serial_process_unwind, proc); 2558 record_unwind_protect (remove_process, proc);
2617 p = XPROCESS (proc); 2559 p = XPROCESS (proc);
2618 2560
2619 fd = serial_open (SSDATA (port)); 2561 fd = serial_open (port);
2562 p->open_fd[SUBPROCESS_STDIN] = fd;
2620 p->infd = fd; 2563 p->infd = fd;
2621 p->outfd = fd; 2564 p->outfd = fd;
2622 if (fd > max_process_desc) 2565 if (fd > max_process_desc)
@@ -2627,19 +2570,19 @@ usage: (make-serial-process &rest ARGS) */)
2627 if (NILP (buffer)) 2570 if (NILP (buffer))
2628 buffer = name; 2571 buffer = name;
2629 buffer = Fget_buffer_create (buffer); 2572 buffer = Fget_buffer_create (buffer);
2630 p->buffer = buffer; 2573 pset_buffer (p, buffer);
2631 2574
2632 p->childp = contact; 2575 pset_childp (p, contact);
2633 p->plist = Fcopy_sequence (Fplist_get (contact, QCplist)); 2576 pset_plist (p, Fcopy_sequence (Fplist_get (contact, QCplist)));
2634 p->type = Qserial; 2577 pset_type (p, Qserial);
2635 p->sentinel = Fplist_get (contact, QCsentinel); 2578 pset_sentinel (p, Fplist_get (contact, QCsentinel));
2636 p->filter = Fplist_get (contact, QCfilter); 2579 pset_filter (p, Fplist_get (contact, QCfilter));
2637 p->log = Qnil; 2580 pset_log (p, Qnil);
2638 if (tem = Fplist_get (contact, QCnoquery), !NILP (tem)) 2581 if (tem = Fplist_get (contact, QCnoquery), !NILP (tem))
2639 p->kill_without_query = 1; 2582 p->kill_without_query = 1;
2640 if (tem = Fplist_get (contact, QCstop), !NILP (tem)) 2583 if (tem = Fplist_get (contact, QCstop), !NILP (tem))
2641 p->command = Qt; 2584 pset_command (p, Qt);
2642 p->pty_flag = 0; 2585 eassert (! p->pty_flag);
2643 2586
2644 if (!EQ (p->command, Qt)) 2587 if (!EQ (p->command, Qt))
2645 { 2588 {
@@ -2670,7 +2613,7 @@ usage: (make-serial-process &rest ARGS) */)
2670 else if ((!NILP (buffer) && NILP (BVAR (XBUFFER (buffer), enable_multibyte_characters))) 2613 else if ((!NILP (buffer) && NILP (BVAR (XBUFFER (buffer), enable_multibyte_characters)))
2671 || (NILP (buffer) && NILP (BVAR (&buffer_defaults, enable_multibyte_characters)))) 2614 || (NILP (buffer) && NILP (BVAR (&buffer_defaults, enable_multibyte_characters))))
2672 val = Qnil; 2615 val = Qnil;
2673 p->decode_coding_system = val; 2616 pset_decode_coding_system (p, val);
2674 2617
2675 val = Qnil; 2618 val = Qnil;
2676 if (!NILP (tem)) 2619 if (!NILP (tem))
@@ -2684,12 +2627,12 @@ usage: (make-serial-process &rest ARGS) */)
2684 else if ((!NILP (buffer) && NILP (BVAR (XBUFFER (buffer), enable_multibyte_characters))) 2627 else if ((!NILP (buffer) && NILP (BVAR (XBUFFER (buffer), enable_multibyte_characters)))
2685 || (NILP (buffer) && NILP (BVAR (&buffer_defaults, enable_multibyte_characters)))) 2628 || (NILP (buffer) && NILP (BVAR (&buffer_defaults, enable_multibyte_characters))))
2686 val = Qnil; 2629 val = Qnil;
2687 p->encode_coding_system = val; 2630 pset_encode_coding_system (p, val);
2688 2631
2689 setup_process_coding_systems (proc); 2632 setup_process_coding_systems (proc);
2690 p->decoding_buf = empty_unibyte_string; 2633 pset_decoding_buf (p, empty_unibyte_string);
2691 p->decoding_carryover = 0; 2634 p->decoding_carryover = 0;
2692 p->encoding_buf = empty_unibyte_string; 2635 pset_encoding_buf (p, empty_unibyte_string);
2693 p->inherit_coding_system_flag 2636 p->inherit_coding_system_flag
2694 = !(!NILP (tem) || NILP (buffer) || !inherit_process_coding_system); 2637 = !(!NILP (tem) || NILP (buffer) || !inherit_process_coding_system);
2695 2638
@@ -2895,8 +2838,9 @@ usage: (make-network-process &rest ARGS) */)
2895 Lisp_Object tem; 2838 Lisp_Object tem;
2896 Lisp_Object name, buffer, host, service, address; 2839 Lisp_Object name, buffer, host, service, address;
2897 Lisp_Object filter, sentinel; 2840 Lisp_Object filter, sentinel;
2898 int is_non_blocking_client = 0; 2841 bool is_non_blocking_client = 0;
2899 int is_server = 0, backlog = 5; 2842 bool is_server = 0;
2843 int backlog = 5;
2900 int socktype; 2844 int socktype;
2901 int family = -1; 2845 int family = -1;
2902 2846
@@ -2933,13 +2877,9 @@ usage: (make-network-process &rest ARGS) */)
2933 { 2877 {
2934 /* Don't support network sockets when non-blocking mode is 2878 /* Don't support network sockets when non-blocking mode is
2935 not available, since a blocked Emacs is not useful. */ 2879 not available, since a blocked Emacs is not useful. */
2936#if !defined (O_NONBLOCK) && !defined (O_NDELAY)
2937 error ("Network servers not supported");
2938#else
2939 is_server = 1; 2880 is_server = 1;
2940 if (TYPE_RANGED_INTEGERP (int, tem)) 2881 if (TYPE_RANGED_INTEGERP (int, tem))
2941 backlog = XINT (tem); 2882 backlog = XINT (tem);
2942#endif
2943 } 2883 }
2944 2884
2945 /* Make QCaddress an alias for :local (server) or :remote (client). */ 2885 /* Make QCaddress an alias for :local (server) or :remote (client). */
@@ -3037,7 +2977,9 @@ usage: (make-network-process &rest ARGS) */)
3037 CHECK_STRING (service); 2977 CHECK_STRING (service);
3038 memset (&address_un, 0, sizeof address_un); 2978 memset (&address_un, 0, sizeof address_un);
3039 address_un.sun_family = AF_LOCAL; 2979 address_un.sun_family = AF_LOCAL;
3040 strncpy (address_un.sun_path, SSDATA (service), sizeof address_un.sun_path); 2980 if (sizeof address_un.sun_path <= SBYTES (service))
2981 error ("Service name too long");
2982 strcpy (address_un.sun_path, SSDATA (service));
3041 ai.ai_addr = (struct sockaddr *) &address_un; 2983 ai.ai_addr = (struct sockaddr *) &address_un;
3042 ai.ai_addrlen = sizeof address_un; 2984 ai.ai_addrlen = sizeof address_un;
3043 goto open_socket; 2985 goto open_socket;
@@ -3050,7 +2992,7 @@ usage: (make-network-process &rest ARGS) */)
3050#ifdef POLL_FOR_INPUT 2992#ifdef POLL_FOR_INPUT
3051 if (socktype != SOCK_DGRAM) 2993 if (socktype != SOCK_DGRAM)
3052 { 2994 {
3053 record_unwind_protect (unwind_stop_other_atimers, Qnil); 2995 record_unwind_protect_void (run_all_atimers);
3054 bind_polling_period (10); 2996 bind_polling_period (10);
3055 } 2997 }
3056#endif 2998#endif
@@ -3182,7 +3124,8 @@ usage: (make-network-process &rest ARGS) */)
3182 retry_connect: 3124 retry_connect:
3183#endif 3125#endif
3184 3126
3185 s = socket (lres->ai_family, lres->ai_socktype, lres->ai_protocol); 3127 s = socket (lres->ai_family, lres->ai_socktype | SOCK_CLOEXEC,
3128 lres->ai_protocol);
3186 if (s < 0) 3129 if (s < 0)
3187 { 3130 {
3188 xerrno = errno; 3131 xerrno = errno;
@@ -3197,11 +3140,7 @@ usage: (make-network-process &rest ARGS) */)
3197#ifdef NON_BLOCKING_CONNECT 3140#ifdef NON_BLOCKING_CONNECT
3198 if (is_non_blocking_client) 3141 if (is_non_blocking_client)
3199 { 3142 {
3200#ifdef O_NONBLOCK
3201 ret = fcntl (s, F_SETFL, O_NONBLOCK); 3143 ret = fcntl (s, F_SETFL, O_NONBLOCK);
3202#else
3203 ret = fcntl (s, F_SETFL, O_NDELAY);
3204#endif
3205 if (ret < 0) 3144 if (ret < 0)
3206 { 3145 {
3207 xerrno = errno; 3146 xerrno = errno;
@@ -3213,7 +3152,7 @@ usage: (make-network-process &rest ARGS) */)
3213#endif 3152#endif
3214 3153
3215 /* Make us close S if quit. */ 3154 /* Make us close S if quit. */
3216 record_unwind_protect (close_file_unwind, make_number (s)); 3155 record_unwind_protect_int (close_file_unwind, s);
3217 3156
3218 /* Parse network options in the arg list. 3157 /* Parse network options in the arg list.
3219 We simply ignore anything which isn't a known option (including other keywords). 3158 We simply ignore anything which isn't a known option (including other keywords).
@@ -3290,33 +3229,31 @@ usage: (make-network-process &rest ARGS) */)
3290 { 3229 {
3291 /* Unlike most other syscalls connect() cannot be called 3230 /* Unlike most other syscalls connect() cannot be called
3292 again. (That would return EALREADY.) The proper way to 3231 again. (That would return EALREADY.) The proper way to
3293 wait for completion is select(). */ 3232 wait for completion is pselect(). */
3294 int sc; 3233 int sc;
3295 socklen_t len; 3234 socklen_t len;
3296 SELECT_TYPE fdset; 3235 fd_set fdset;
3297 retry_select: 3236 retry_select:
3298 FD_ZERO (&fdset); 3237 FD_ZERO (&fdset);
3299 FD_SET (s, &fdset); 3238 FD_SET (s, &fdset);
3300 QUIT; 3239 QUIT;
3301 sc = select (s + 1, (SELECT_TYPE *)0, &fdset, (SELECT_TYPE *)0, 3240 sc = pselect (s + 1, NULL, &fdset, NULL, NULL, NULL);
3302 (EMACS_TIME *)0);
3303 if (sc == -1) 3241 if (sc == -1)
3304 { 3242 {
3305 if (errno == EINTR) 3243 if (errno == EINTR)
3306 goto retry_select; 3244 goto retry_select;
3307 else 3245 else
3308 report_file_error ("select failed", Qnil); 3246 report_file_error ("Failed select", Qnil);
3309 } 3247 }
3310 eassert (sc > 0); 3248 eassert (sc > 0);
3311 3249
3312 len = sizeof xerrno; 3250 len = sizeof xerrno;
3313 eassert (FD_ISSET (s, &fdset)); 3251 eassert (FD_ISSET (s, &fdset));
3314 if (getsockopt (s, SOL_SOCKET, SO_ERROR, &xerrno, &len) == -1) 3252 if (getsockopt (s, SOL_SOCKET, SO_ERROR, &xerrno, &len) < 0)
3315 report_file_error ("getsockopt failed", Qnil); 3253 report_file_error ("Failed getsockopt", Qnil);
3316 if (xerrno) 3254 if (xerrno)
3317 errno = xerrno, report_file_error ("error during connect", Qnil); 3255 report_file_errno ("Failed connect", Qnil, xerrno);
3318 else 3256 break;
3319 break;
3320 } 3257 }
3321#endif /* !WINDOWSNT */ 3258#endif /* !WINDOWSNT */
3322 3259
@@ -3339,8 +3276,8 @@ usage: (make-network-process &rest ARGS) */)
3339 if (socktype == SOCK_DGRAM) 3276 if (socktype == SOCK_DGRAM)
3340 { 3277 {
3341 if (datagram_address[s].sa) 3278 if (datagram_address[s].sa)
3342 abort (); 3279 emacs_abort ();
3343 datagram_address[s].sa = (struct sockaddr *) xmalloc (lres->ai_addrlen); 3280 datagram_address[s].sa = xmalloc (lres->ai_addrlen);
3344 datagram_address[s].len = lres->ai_addrlen; 3281 datagram_address[s].len = lres->ai_addrlen;
3345 if (is_server) 3282 if (is_server)
3346 { 3283 {
@@ -3350,7 +3287,8 @@ usage: (make-network-process &rest ARGS) */)
3350 { 3287 {
3351 int rfamily, rlen; 3288 int rfamily, rlen;
3352 rlen = get_lisp_to_sockaddr_size (remote, &rfamily); 3289 rlen = get_lisp_to_sockaddr_size (remote, &rfamily);
3353 if (rfamily == lres->ai_family && rlen == lres->ai_addrlen) 3290 if (rlen != 0 && rfamily == lres->ai_family
3291 && rlen == lres->ai_addrlen)
3354 conv_lisp_to_sockaddr (rfamily, remote, 3292 conv_lisp_to_sockaddr (rfamily, remote,
3355 datagram_address[s].sa, rlen); 3293 datagram_address[s].sa, rlen);
3356 } 3294 }
@@ -3378,18 +3316,12 @@ usage: (make-network-process &rest ARGS) */)
3378#ifdef HAVE_GETADDRINFO 3316#ifdef HAVE_GETADDRINFO
3379 if (res != &ai) 3317 if (res != &ai)
3380 { 3318 {
3381 BLOCK_INPUT; 3319 block_input ();
3382 freeaddrinfo (res); 3320 freeaddrinfo (res);
3383 UNBLOCK_INPUT; 3321 unblock_input ();
3384 } 3322 }
3385#endif 3323#endif
3386 3324
3387 /* Discard the unwind protect for closing S, if any. */
3388 specpdl_ptr = specpdl + count1;
3389
3390 /* Unwind bind_polling_period and request_sigio. */
3391 unbind_to (count, Qnil);
3392
3393 if (s < 0) 3325 if (s < 0)
3394 { 3326 {
3395 /* If non-blocking got this far - and failed - assume non-blocking is 3327 /* If non-blocking got this far - and failed - assume non-blocking is
@@ -3399,11 +3331,10 @@ usage: (make-network-process &rest ARGS) */)
3399 if (is_non_blocking_client) 3331 if (is_non_blocking_client)
3400 return Qnil; 3332 return Qnil;
3401 3333
3402 errno = xerrno; 3334 report_file_errno ((is_server
3403 if (is_server) 3335 ? "make server process failed"
3404 report_file_error ("make server process failed", contact); 3336 : "make client process failed"),
3405 else 3337 contact, xerrno);
3406 report_file_error ("make client process failed", contact);
3407 } 3338 }
3408 3339
3409 inch = s; 3340 inch = s;
@@ -3415,33 +3346,36 @@ usage: (make-network-process &rest ARGS) */)
3415 3346
3416 chan_process[inch] = proc; 3347 chan_process[inch] = proc;
3417 3348
3418#ifdef O_NONBLOCK
3419 fcntl (inch, F_SETFL, O_NONBLOCK); 3349 fcntl (inch, F_SETFL, O_NONBLOCK);
3420#else
3421#ifdef O_NDELAY
3422 fcntl (inch, F_SETFL, O_NDELAY);
3423#endif
3424#endif
3425 3350
3426 p = XPROCESS (proc); 3351 p = XPROCESS (proc);
3427 3352
3428 p->childp = contact; 3353 pset_childp (p, contact);
3429 p->plist = Fcopy_sequence (Fplist_get (contact, QCplist)); 3354 pset_plist (p, Fcopy_sequence (Fplist_get (contact, QCplist)));
3430 p->type = Qnetwork; 3355 pset_type (p, Qnetwork);
3431 3356
3432 p->buffer = buffer; 3357 pset_buffer (p, buffer);
3433 p->sentinel = sentinel; 3358 pset_sentinel (p, sentinel);
3434 p->filter = filter; 3359 pset_filter (p, filter);
3435 p->log = Fplist_get (contact, QClog); 3360 pset_log (p, Fplist_get (contact, QClog));
3436 if (tem = Fplist_get (contact, QCnoquery), !NILP (tem)) 3361 if (tem = Fplist_get (contact, QCnoquery), !NILP (tem))
3437 p->kill_without_query = 1; 3362 p->kill_without_query = 1;
3438 if ((tem = Fplist_get (contact, QCstop), !NILP (tem))) 3363 if ((tem = Fplist_get (contact, QCstop), !NILP (tem)))
3439 p->command = Qt; 3364 pset_command (p, Qt);
3440 p->pid = 0; 3365 p->pid = 0;
3366
3367 p->open_fd[SUBPROCESS_STDIN] = inch;
3441 p->infd = inch; 3368 p->infd = inch;
3442 p->outfd = outch; 3369 p->outfd = outch;
3370
3371 /* Discard the unwind protect for closing S, if any. */
3372 specpdl_ptr = specpdl + count1;
3373
3374 /* Unwind bind_polling_period and request_sigio. */
3375 unbind_to (count, Qnil);
3376
3443 if (is_server && socktype != SOCK_DGRAM) 3377 if (is_server && socktype != SOCK_DGRAM)
3444 p->status = Qlisten; 3378 pset_status (p, Qlisten);
3445 3379
3446 /* Make the process marker point into the process buffer (if any). */ 3380 /* Make the process marker point into the process buffer (if any). */
3447 if (BUFFERP (buffer)) 3381 if (BUFFERP (buffer))
@@ -3455,7 +3389,7 @@ usage: (make-network-process &rest ARGS) */)
3455 /* We may get here if connect did succeed immediately. However, 3389 /* We may get here if connect did succeed immediately. However,
3456 in that case, we still need to signal this like a non-blocking 3390 in that case, we still need to signal this like a non-blocking
3457 connection. */ 3391 connection. */
3458 p->status = Qconnect; 3392 pset_status (p, Qconnect);
3459 if (!FD_ISSET (inch, &connect_wait_mask)) 3393 if (!FD_ISSET (inch, &connect_wait_mask))
3460 { 3394 {
3461 FD_SET (inch, &connect_wait_mask); 3395 FD_SET (inch, &connect_wait_mask);
@@ -3522,7 +3456,7 @@ usage: (make-network-process &rest ARGS) */)
3522 else 3456 else
3523 val = Qnil; 3457 val = Qnil;
3524 } 3458 }
3525 p->decode_coding_system = val; 3459 pset_decode_coding_system (p, val);
3526 3460
3527 if (!NILP (tem)) 3461 if (!NILP (tem))
3528 { 3462 {
@@ -3556,13 +3490,13 @@ usage: (make-network-process &rest ARGS) */)
3556 else 3490 else
3557 val = Qnil; 3491 val = Qnil;
3558 } 3492 }
3559 p->encode_coding_system = val; 3493 pset_encode_coding_system (p, val);
3560 } 3494 }
3561 setup_process_coding_systems (proc); 3495 setup_process_coding_systems (proc);
3562 3496
3563 p->decoding_buf = empty_unibyte_string; 3497 pset_decoding_buf (p, empty_unibyte_string);
3564 p->decoding_carryover = 0; 3498 p->decoding_carryover = 0;
3565 p->encoding_buf = empty_unibyte_string; 3499 pset_encoding_buf (p, empty_unibyte_string);
3566 3500
3567 p->inherit_coding_system_flag 3501 p->inherit_coding_system_flag
3568 = !(!NILP (tem) || NILP (buffer) || !inherit_process_coding_system); 3502 = !(!NILP (tem) || NILP (buffer) || !inherit_process_coding_system);
@@ -3586,12 +3520,15 @@ format; see the description of ADDRESS in `make-network-process'. */)
3586 struct ifreq *ifreq; 3520 struct ifreq *ifreq;
3587 void *buf = NULL; 3521 void *buf = NULL;
3588 ptrdiff_t buf_size = 512; 3522 ptrdiff_t buf_size = 512;
3589 int s, i; 3523 int s;
3590 Lisp_Object res; 3524 Lisp_Object res;
3525 ptrdiff_t count;
3591 3526
3592 s = socket (AF_INET, SOCK_STREAM, 0); 3527 s = socket (AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
3593 if (s < 0) 3528 if (s < 0)
3594 return Qnil; 3529 return Qnil;
3530 count = SPECPDL_INDEX ();
3531 record_unwind_protect_int (close_file_unwind, s);
3595 3532
3596 do 3533 do
3597 { 3534 {
@@ -3600,16 +3537,14 @@ format; see the description of ADDRESS in `make-network-process'. */)
3600 ifconf.ifc_len = buf_size; 3537 ifconf.ifc_len = buf_size;
3601 if (ioctl (s, SIOCGIFCONF, &ifconf)) 3538 if (ioctl (s, SIOCGIFCONF, &ifconf))
3602 { 3539 {
3603 close (s); 3540 emacs_close (s);
3604 xfree (buf); 3541 xfree (buf);
3605 return Qnil; 3542 return Qnil;
3606 } 3543 }
3607 } 3544 }
3608 while (ifconf.ifc_len == buf_size); 3545 while (ifconf.ifc_len == buf_size);
3609 3546
3610 close (s); 3547 res = unbind_to (count, Qnil);
3611
3612 res = Qnil;
3613 ifreq = ifconf.ifc_req; 3548 ifreq = ifconf.ifc_req;
3614 while ((char *) ifreq < (char *) ifconf.ifc_req + ifconf.ifc_len) 3549 while ((char *) ifreq < (char *) ifconf.ifc_req + ifconf.ifc_len)
3615 { 3550 {
@@ -3624,7 +3559,6 @@ format; see the description of ADDRESS in `make-network-process'. */)
3624 int len = sizeof (*ifreq); 3559 int len = sizeof (*ifreq);
3625#endif 3560#endif
3626 char namebuf[sizeof (ifq->ifr_name) + 1]; 3561 char namebuf[sizeof (ifq->ifr_name) + 1];
3627 i += len;
3628 ifreq = (struct ifreq *) ((char *) ifreq + len); 3562 ifreq = (struct ifreq *) ((char *) ifreq + len);
3629 3563
3630 if (ifq->ifr_addr.sa_family != AF_INET) 3564 if (ifq->ifr_addr.sa_family != AF_INET)
@@ -3734,7 +3668,8 @@ FLAGS is the current flags of the interface. */)
3734 Lisp_Object res = Qnil; 3668 Lisp_Object res = Qnil;
3735 Lisp_Object elt; 3669 Lisp_Object elt;
3736 int s; 3670 int s;
3737 int any = 0; 3671 bool any = 0;
3672 ptrdiff_t count;
3738#if (! (defined SIOCGIFHWADDR && defined HAVE_STRUCT_IFREQ_IFR_HWADDR) \ 3673#if (! (defined SIOCGIFHWADDR && defined HAVE_STRUCT_IFREQ_IFR_HWADDR) \
3739 && defined HAVE_GETIFADDRS && defined LLADDR) 3674 && defined HAVE_GETIFADDRS && defined LLADDR)
3740 struct ifaddrs *ifap; 3675 struct ifaddrs *ifap;
@@ -3742,12 +3677,15 @@ FLAGS is the current flags of the interface. */)
3742 3677
3743 CHECK_STRING (ifname); 3678 CHECK_STRING (ifname);
3744 3679
3745 memset (rq.ifr_name, 0, sizeof rq.ifr_name); 3680 if (sizeof rq.ifr_name <= SBYTES (ifname))
3746 strncpy (rq.ifr_name, SSDATA (ifname), sizeof (rq.ifr_name)); 3681 error ("interface name too long");
3682 strcpy (rq.ifr_name, SSDATA (ifname));
3747 3683
3748 s = socket (AF_INET, SOCK_STREAM, 0); 3684 s = socket (AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
3749 if (s < 0) 3685 if (s < 0)
3750 return Qnil; 3686 return Qnil;
3687 count = SPECPDL_INDEX ();
3688 record_unwind_protect_int (close_file_unwind, s);
3751 3689
3752 elt = Qnil; 3690 elt = Qnil;
3753#if defined (SIOCGIFFLAGS) && defined (HAVE_STRUCT_IFREQ_IFR_FLAGS) 3691#if defined (SIOCGIFFLAGS) && defined (HAVE_STRUCT_IFREQ_IFR_FLAGS)
@@ -3864,9 +3802,7 @@ FLAGS is the current flags of the interface. */)
3864#endif 3802#endif
3865 res = Fcons (elt, res); 3803 res = Fcons (elt, res);
3866 3804
3867 close (s); 3805 return unbind_to (count, any ? res : Qnil);
3868
3869 return any ? res : Qnil;
3870} 3806}
3871#endif 3807#endif
3872#endif /* defined (HAVE_NET_IF_H) */ 3808#endif /* defined (HAVE_NET_IF_H) */
@@ -3876,17 +3812,15 @@ FLAGS is the current flags of the interface. */)
3876static void 3812static void
3877deactivate_process (Lisp_Object proc) 3813deactivate_process (Lisp_Object proc)
3878{ 3814{
3879 register int inchannel, outchannel; 3815 int inchannel;
3880 register struct Lisp_Process *p = XPROCESS (proc); 3816 struct Lisp_Process *p = XPROCESS (proc);
3817 int i;
3881 3818
3882#ifdef HAVE_GNUTLS 3819#ifdef HAVE_GNUTLS
3883 /* Delete GnuTLS structures in PROC, if any. */ 3820 /* Delete GnuTLS structures in PROC, if any. */
3884 emacs_gnutls_deinit (proc); 3821 emacs_gnutls_deinit (proc);
3885#endif /* HAVE_GNUTLS */ 3822#endif /* HAVE_GNUTLS */
3886 3823
3887 inchannel = p->infd;
3888 outchannel = p->outfd;
3889
3890#ifdef ADAPTIVE_READ_BUFFERING 3824#ifdef ADAPTIVE_READ_BUFFERING
3891 if (p->read_output_delay > 0) 3825 if (p->read_output_delay > 0)
3892 { 3826 {
@@ -3897,14 +3831,14 @@ deactivate_process (Lisp_Object proc)
3897 } 3831 }
3898#endif 3832#endif
3899 3833
3834 /* Beware SIGCHLD hereabouts. */
3835
3836 for (i = 0; i < PROCESS_OPEN_FDS; i++)
3837 close_process_fd (&p->open_fd[i]);
3838
3839 inchannel = p->infd;
3900 if (inchannel >= 0) 3840 if (inchannel >= 0)
3901 { 3841 {
3902 /* Beware SIGCHLD hereabouts. */
3903 flush_pending_output (inchannel);
3904 emacs_close (inchannel);
3905 if (outchannel >= 0 && outchannel != inchannel)
3906 emacs_close (outchannel);
3907
3908 p->infd = -1; 3842 p->infd = -1;
3909 p->outfd = -1; 3843 p->outfd = -1;
3910#ifdef DATAGRAM_SOCKETS 3844#ifdef DATAGRAM_SOCKETS
@@ -3924,18 +3858,19 @@ deactivate_process (Lisp_Object proc)
3924 FD_CLR (inchannel, &connect_wait_mask); 3858 FD_CLR (inchannel, &connect_wait_mask);
3925 FD_CLR (inchannel, &write_mask); 3859 FD_CLR (inchannel, &write_mask);
3926 if (--num_pending_connects < 0) 3860 if (--num_pending_connects < 0)
3927 abort (); 3861 emacs_abort ();
3928 } 3862 }
3929#endif 3863#endif
3930 if (inchannel == max_process_desc) 3864 if (inchannel == max_process_desc)
3931 { 3865 {
3932 int i;
3933 /* We just closed the highest-numbered process input descriptor, 3866 /* We just closed the highest-numbered process input descriptor,
3934 so recompute the highest-numbered one now. */ 3867 so recompute the highest-numbered one now. */
3935 max_process_desc = 0; 3868 int i = inchannel;
3936 for (i = 0; i < MAXDESC; i++) 3869 do
3937 if (!NILP (chan_process[i])) 3870 i--;
3938 max_process_desc = i; 3871 while (0 <= i && NILP (chan_process[i]));
3872
3873 max_process_desc = i;
3939 } 3874 }
3940 } 3875 }
3941} 3876}
@@ -3960,7 +3895,8 @@ If JUST-THIS-ONE is an integer, don't run any timers either.
3960Return non-nil if we received any output before the timeout expired. */) 3895Return non-nil if we received any output before the timeout expired. */)
3961 (register Lisp_Object process, Lisp_Object seconds, Lisp_Object millisec, Lisp_Object just_this_one) 3896 (register Lisp_Object process, Lisp_Object seconds, Lisp_Object millisec, Lisp_Object just_this_one)
3962{ 3897{
3963 int secs = -1, usecs = 0; 3898 intmax_t secs;
3899 int nsecs;
3964 3900
3965 if (! NILP (process)) 3901 if (! NILP (process))
3966 CHECK_PROCESS (process); 3902 CHECK_PROCESS (process);
@@ -3979,17 +3915,36 @@ Return non-nil if we received any output before the timeout expired. */)
3979 } 3915 }
3980 } 3916 }
3981 3917
3918 secs = 0;
3919 nsecs = -1;
3920
3982 if (!NILP (seconds)) 3921 if (!NILP (seconds))
3983 { 3922 {
3984 double duration = extract_float (seconds); 3923 if (INTEGERP (seconds))
3985 if (0 < duration) 3924 {
3986 duration_to_sec_usec (duration, &secs, &usecs); 3925 if (XINT (seconds) > 0)
3926 {
3927 secs = XINT (seconds);
3928 nsecs = 0;
3929 }
3930 }
3931 else if (FLOATP (seconds))
3932 {
3933 if (XFLOAT_DATA (seconds) > 0)
3934 {
3935 struct timespec t = dtotimespec (XFLOAT_DATA (seconds));
3936 secs = min (t.tv_sec, WAIT_READING_MAX);
3937 nsecs = t.tv_nsec;
3938 }
3939 }
3940 else
3941 wrong_type_argument (Qnumberp, seconds);
3987 } 3942 }
3988 else if (!NILP (process)) 3943 else if (! NILP (process))
3989 secs = 0; 3944 nsecs = 0;
3990 3945
3991 return 3946 return
3992 (wait_reading_process_output (secs, usecs, 0, 0, 3947 (wait_reading_process_output (secs, nsecs, 0, 0,
3993 Qnil, 3948 Qnil,
3994 !NILP (process) ? XPROCESS (process) : NULL, 3949 !NILP (process) ? XPROCESS (process) : NULL,
3995 NILP (just_this_one) ? 0 : 3950 NILP (just_this_one) ? 0 :
@@ -3999,7 +3954,7 @@ Return non-nil if we received any output before the timeout expired. */)
3999 3954
4000/* Accept a connection for server process SERVER on CHANNEL. */ 3955/* Accept a connection for server process SERVER on CHANNEL. */
4001 3956
4002static int connect_counter = 0; 3957static EMACS_INT connect_counter = 0;
4003 3958
4004static void 3959static void
4005server_accept_connection (Lisp_Object server, int channel) 3960server_accept_connection (Lisp_Object server, int channel)
@@ -4020,8 +3975,9 @@ server_accept_connection (Lisp_Object server, int channel)
4020#endif 3975#endif
4021 } saddr; 3976 } saddr;
4022 socklen_t len = sizeof saddr; 3977 socklen_t len = sizeof saddr;
3978 ptrdiff_t count;
4023 3979
4024 s = accept (channel, &saddr.sa, &len); 3980 s = accept4 (channel, &saddr.sa, &len, SOCK_CLOEXEC);
4025 3981
4026 if (s < 0) 3982 if (s < 0)
4027 { 3983 {
@@ -4042,6 +3998,9 @@ server_accept_connection (Lisp_Object server, int channel)
4042 return; 3998 return;
4043 } 3999 }
4044 4000
4001 count = SPECPDL_INDEX ();
4002 record_unwind_protect_int (close_file_unwind, s);
4003
4045 connect_counter++; 4004 connect_counter++;
4046 4005
4047 /* Setup a new process to handle the connection. */ 4006 /* Setup a new process to handle the connection. */
@@ -4105,7 +4064,8 @@ server_accept_connection (Lisp_Object server, int channel)
4105 process name of the server process concatenated with the caller 4064 process name of the server process concatenated with the caller
4106 identification. */ 4065 identification. */
4107 4066
4108 if (!NILP (ps->filter) && !EQ (ps->filter, Qt)) 4067 if (!(EQ (ps->filter, Qinternal_default_process_filter)
4068 || EQ (ps->filter, Qt)))
4109 buffer = Qnil; 4069 buffer = Qnil;
4110 else 4070 else
4111 { 4071 {
@@ -4129,13 +4089,7 @@ server_accept_connection (Lisp_Object server, int channel)
4129 4089
4130 chan_process[s] = proc; 4090 chan_process[s] = proc;
4131 4091
4132#ifdef O_NONBLOCK
4133 fcntl (s, F_SETFL, O_NONBLOCK); 4092 fcntl (s, F_SETFL, O_NONBLOCK);
4134#else
4135#ifdef O_NDELAY
4136 fcntl (s, F_SETFL, O_NDELAY);
4137#endif
4138#endif
4139 4093
4140 p = XPROCESS (proc); 4094 p = XPROCESS (proc);
4141 4095
@@ -4154,18 +4108,23 @@ server_accept_connection (Lisp_Object server, int channel)
4154 conv_sockaddr_to_lisp (&saddr.sa, len)); 4108 conv_sockaddr_to_lisp (&saddr.sa, len));
4155#endif 4109#endif
4156 4110
4157 p->childp = contact; 4111 pset_childp (p, contact);
4158 p->plist = Fcopy_sequence (ps->plist); 4112 pset_plist (p, Fcopy_sequence (ps->plist));
4159 p->type = Qnetwork; 4113 pset_type (p, Qnetwork);
4160 4114
4161 p->buffer = buffer; 4115 pset_buffer (p, buffer);
4162 p->sentinel = ps->sentinel; 4116 pset_sentinel (p, ps->sentinel);
4163 p->filter = ps->filter; 4117 pset_filter (p, ps->filter);
4164 p->command = Qnil; 4118 pset_command (p, Qnil);
4165 p->pid = 0; 4119 p->pid = 0;
4120
4121 /* Discard the unwind protect for closing S. */
4122 specpdl_ptr = specpdl + count;
4123
4124 p->open_fd[SUBPROCESS_STDIN] = s;
4166 p->infd = s; 4125 p->infd = s;
4167 p->outfd = s; 4126 p->outfd = s;
4168 p->status = Qrun; 4127 pset_status (p, Qrun);
4169 4128
4170 /* Client processes for accepted connections are not stopped initially. */ 4129 /* Client processes for accepted connections are not stopped initially. */
4171 if (!EQ (p->filter, Qt)) 4130 if (!EQ (p->filter, Qt))
@@ -4180,15 +4139,15 @@ server_accept_connection (Lisp_Object server, int channel)
4180 /* Setup coding system for new process based on server process. 4139 /* Setup coding system for new process based on server process.
4181 This seems to be the proper thing to do, as the coding system 4140 This seems to be the proper thing to do, as the coding system
4182 of the new process should reflect the settings at the time the 4141 of the new process should reflect the settings at the time the
4183 server socket was opened; not the current settings. */ 4142 server socket was opened; not the current settings. */
4184 4143
4185 p->decode_coding_system = ps->decode_coding_system; 4144 pset_decode_coding_system (p, ps->decode_coding_system);
4186 p->encode_coding_system = ps->encode_coding_system; 4145 pset_encode_coding_system (p, ps->encode_coding_system);
4187 setup_process_coding_systems (proc); 4146 setup_process_coding_systems (proc);
4188 4147
4189 p->decoding_buf = empty_unibyte_string; 4148 pset_decoding_buf (p, empty_unibyte_string);
4190 p->decoding_carryover = 0; 4149 p->decoding_carryover = 0;
4191 p->encoding_buf = empty_unibyte_string; 4150 pset_encoding_buf (p, empty_unibyte_string);
4192 4151
4193 p->inherit_coding_system_flag 4152 p->inherit_coding_system_flag
4194 = (NILP (buffer) ? 0 : ps->inherit_coding_system_flag); 4153 = (NILP (buffer) ? 0 : ps->inherit_coding_system_flag);
@@ -4199,11 +4158,10 @@ server_accept_connection (Lisp_Object server, int channel)
4199 (STRINGP (host) ? host : build_string ("-")), 4158 (STRINGP (host) ? host : build_string ("-")),
4200 build_string ("\n"))); 4159 build_string ("\n")));
4201 4160
4202 if (!NILP (p->sentinel)) 4161 exec_sentinel (proc,
4203 exec_sentinel (proc, 4162 concat3 (build_string ("open from "),
4204 concat3 (build_string ("open from "), 4163 (STRINGP (host) ? host : build_string ("-")),
4205 (STRINGP (host) ? host : build_string ("-")), 4164 build_string ("\n")));
4206 build_string ("\n")));
4207} 4165}
4208 4166
4209/* This variable is different from waiting_for_input in keyboard.c. 4167/* This variable is different from waiting_for_input in keyboard.c.
@@ -4217,11 +4175,10 @@ server_accept_connection (Lisp_Object server, int channel)
4217 when not inside wait_reading_process_output. */ 4175 when not inside wait_reading_process_output. */
4218static int waiting_for_user_input_p; 4176static int waiting_for_user_input_p;
4219 4177
4220static Lisp_Object 4178static void
4221wait_reading_process_output_unwind (Lisp_Object data) 4179wait_reading_process_output_unwind (int data)
4222{ 4180{
4223 waiting_for_user_input_p = XINT (data); 4181 waiting_for_user_input_p = data;
4224 return Qnil;
4225} 4182}
4226 4183
4227/* This is here so breakpoints can be put on it. */ 4184/* This is here so breakpoints can be put on it. */
@@ -4230,42 +4187,27 @@ wait_reading_process_output_1 (void)
4230{ 4187{
4231} 4188}
4232 4189
4233/* Use a wrapper around select to work around a bug in gdb 5.3.
4234 Normally, the wrapper is optimized away by inlining.
4235
4236 If emacs is stopped inside select, the gdb backtrace doesn't
4237 show the function which called select, so it is practically
4238 impossible to step through wait_reading_process_output. */
4239
4240#ifndef select
4241static inline int
4242select_wrapper (int n, fd_set *rfd, fd_set *wfd, fd_set *xfd, struct timeval *tmo)
4243{
4244 return select (n, rfd, wfd, xfd, tmo);
4245}
4246#define select select_wrapper
4247#endif
4248
4249/* Read and dispose of subprocess output while waiting for timeout to 4190/* Read and dispose of subprocess output while waiting for timeout to
4250 elapse and/or keyboard input to be available. 4191 elapse and/or keyboard input to be available.
4251 4192
4252 TIME_LIMIT is: 4193 TIME_LIMIT is:
4253 timeout in seconds, or 4194 timeout in seconds
4254 zero for no limit, or 4195 If negative, gobble data immediately available but don't wait for any.
4255 -1 means gobble data immediately available but don't wait for any.
4256 4196
4257 MICROSECS is: 4197 NSECS is:
4258 an additional duration to wait, measured in microseconds. 4198 an additional duration to wait, measured in nanoseconds
4259 If this is nonzero and time_limit is 0, then the timeout 4199 If TIME_LIMIT is zero, then:
4260 consists of MICROSECS only. 4200 If NSECS == 0, there is no limit.
4201 If NSECS > 0, the timeout consists of NSECS only.
4202 If NSECS < 0, gobble data immediately, as if TIME_LIMIT were negative.
4261 4203
4262 READ_KBD is a lisp value: 4204 READ_KBD is:
4263 0 to ignore keyboard input, or 4205 0 to ignore keyboard input, or
4264 1 to return when input is available, or 4206 1 to return when input is available, or
4265 -1 meaning caller will actually read the input, so don't throw to 4207 -1 meaning caller will actually read the input, so don't throw to
4266 the quit handler, or 4208 the quit handler, or
4267 4209
4268 DO_DISPLAY != 0 means redisplay should be done to show subprocess 4210 DO_DISPLAY means redisplay should be done to show subprocess
4269 output that arrives. 4211 output that arrives.
4270 4212
4271 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil 4213 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
@@ -4275,7 +4217,7 @@ select_wrapper (int n, fd_set *rfd, fd_set *wfd, fd_set *xfd, struct timeval *tm
4275 process. The return value is true if we read some input from 4217 process. The return value is true if we read some input from
4276 that process. 4218 that process.
4277 4219
4278 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC 4220 If JUST_WAIT_PROC is nonzero, handle only output from WAIT_PROC
4279 (suspending output from other processes). A negative value 4221 (suspending output from other processes). A negative value
4280 means don't run any timers either. 4222 means don't run any timers either.
4281 4223
@@ -4283,86 +4225,93 @@ select_wrapper (int n, fd_set *rfd, fd_set *wfd, fd_set *xfd, struct timeval *tm
4283 received input from that process before the timeout elapsed. 4225 received input from that process before the timeout elapsed.
4284 Otherwise, return true if we received input from any process. */ 4226 Otherwise, return true if we received input from any process. */
4285 4227
4286int 4228bool
4287wait_reading_process_output (int time_limit, int microsecs, int read_kbd, 4229wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4288 int do_display, 4230 bool do_display,
4289 Lisp_Object wait_for_cell, 4231 Lisp_Object wait_for_cell,
4290 struct Lisp_Process *wait_proc, int just_wait_proc) 4232 struct Lisp_Process *wait_proc, int just_wait_proc)
4291{ 4233{
4292 register int channel, nfds; 4234 int channel, nfds;
4293 SELECT_TYPE Available; 4235 fd_set Available;
4294 SELECT_TYPE Writeok; 4236 fd_set Writeok;
4295 int check_write; 4237 bool check_write;
4296 int check_delay, no_avail; 4238 int check_delay;
4239 bool no_avail;
4297 int xerrno; 4240 int xerrno;
4298 Lisp_Object proc; 4241 Lisp_Object proc;
4299 EMACS_TIME timeout, end_time; 4242 struct timespec timeout, end_time;
4300 int wait_channel = -1; 4243 int wait_channel = -1;
4301 int got_some_input = 0; 4244 bool got_some_input = 0;
4302 ptrdiff_t count = SPECPDL_INDEX (); 4245 ptrdiff_t count = SPECPDL_INDEX ();
4303 4246
4304 FD_ZERO (&Available); 4247 FD_ZERO (&Available);
4305 FD_ZERO (&Writeok); 4248 FD_ZERO (&Writeok);
4306 4249
4307 if (time_limit == 0 && microsecs == 0 && wait_proc && !NILP (Vinhibit_quit) 4250 if (time_limit == 0 && nsecs == 0 && wait_proc && !NILP (Vinhibit_quit)
4308 && !(CONSP (wait_proc->status) && EQ (XCAR (wait_proc->status), Qexit))) 4251 && !(CONSP (wait_proc->status)
4309 message ("Blocking call to accept-process-output with quit inhibited!!"); 4252 && EQ (XCAR (wait_proc->status), Qexit)))
4253 message1 ("Blocking call to accept-process-output with quit inhibited!!");
4310 4254
4311 /* If wait_proc is a process to watch, set wait_channel accordingly. */ 4255 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4312 if (wait_proc != NULL) 4256 if (wait_proc != NULL)
4313 wait_channel = wait_proc->infd; 4257 wait_channel = wait_proc->infd;
4314 4258
4315 record_unwind_protect (wait_reading_process_output_unwind, 4259 record_unwind_protect_int (wait_reading_process_output_unwind,
4316 make_number (waiting_for_user_input_p)); 4260 waiting_for_user_input_p);
4317 waiting_for_user_input_p = read_kbd; 4261 waiting_for_user_input_p = read_kbd;
4318 4262
4263 if (time_limit < 0)
4264 {
4265 time_limit = 0;
4266 nsecs = -1;
4267 }
4268 else if (TYPE_MAXIMUM (time_t) < time_limit)
4269 time_limit = TYPE_MAXIMUM (time_t);
4270
4319 /* Since we may need to wait several times, 4271 /* Since we may need to wait several times,
4320 compute the absolute time to return at. */ 4272 compute the absolute time to return at. */
4321 if (time_limit || microsecs) 4273 if (time_limit || nsecs > 0)
4322 { 4274 {
4323 EMACS_GET_TIME (end_time); 4275 timeout = make_timespec (time_limit, nsecs);
4324 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs); 4276 end_time = timespec_add (current_timespec (), timeout);
4325 EMACS_ADD_TIME (end_time, end_time, timeout);
4326 } 4277 }
4327 4278
4328 while (1) 4279 while (1)
4329 { 4280 {
4330 int timeout_reduced_for_timers = 0; 4281 bool timeout_reduced_for_timers = 0;
4331 4282
4332 /* If calling from keyboard input, do not quit 4283 /* If calling from keyboard input, do not quit
4333 since we want to return C-g as an input character. 4284 since we want to return C-g as an input character.
4334 Otherwise, do pending quit if requested. */ 4285 Otherwise, do pending quit if requested. */
4335 if (read_kbd >= 0) 4286 if (read_kbd >= 0)
4336 QUIT; 4287 QUIT;
4337#ifdef SYNC_INPUT 4288 else if (pending_signals)
4338 else
4339 process_pending_signals (); 4289 process_pending_signals ();
4340#endif
4341 4290
4342 /* Exit now if the cell we're waiting for became non-nil. */ 4291 /* Exit now if the cell we're waiting for became non-nil. */
4343 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell))) 4292 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
4344 break; 4293 break;
4345 4294
4346 /* Compute time from now till when time limit is up */ 4295 /* Compute time from now till when time limit is up. */
4347 /* Exit if already run out */ 4296 /* Exit if already run out. */
4348 if (time_limit == -1) 4297 if (nsecs < 0)
4349 { 4298 {
4350 /* -1 specified for timeout means 4299 /* A negative timeout means
4351 gobble output available now 4300 gobble output available now
4352 but don't wait at all. */ 4301 but don't wait at all. */
4353 4302
4354 EMACS_SET_SECS_USECS (timeout, 0, 0); 4303 timeout = make_timespec (0, 0);
4355 } 4304 }
4356 else if (time_limit || microsecs) 4305 else if (time_limit || nsecs > 0)
4357 { 4306 {
4358 EMACS_GET_TIME (timeout); 4307 struct timespec now = current_timespec ();
4359 EMACS_SUB_TIME (timeout, end_time, timeout); 4308 if (timespec_cmp (end_time, now) <= 0)
4360 if (EMACS_TIME_NEG_P (timeout))
4361 break; 4309 break;
4310 timeout = timespec_sub (end_time, now);
4362 } 4311 }
4363 else 4312 else
4364 { 4313 {
4365 EMACS_SET_SECS_USECS (timeout, 100000, 0); 4314 timeout = make_timespec (100000, 0);
4366 } 4315 }
4367 4316
4368 /* Normally we run timers here. 4317 /* Normally we run timers here.
@@ -4372,11 +4321,11 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4372 if (NILP (wait_for_cell) 4321 if (NILP (wait_for_cell)
4373 && just_wait_proc >= 0) 4322 && just_wait_proc >= 0)
4374 { 4323 {
4375 EMACS_TIME timer_delay; 4324 struct timespec timer_delay;
4376 4325
4377 do 4326 do
4378 { 4327 {
4379 int old_timers_run = timers_run; 4328 unsigned old_timers_run = timers_run;
4380 struct buffer *old_buffer = current_buffer; 4329 struct buffer *old_buffer = current_buffer;
4381 Lisp_Object old_window = selected_window; 4330 Lisp_Object old_window = selected_window;
4382 4331
@@ -4404,21 +4353,22 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4404 && requeued_events_pending_p ()) 4353 && requeued_events_pending_p ())
4405 break; 4354 break;
4406 4355
4407 if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1) 4356 /* A negative timeout means do not wait at all. */
4357 if (nsecs >= 0)
4408 { 4358 {
4409 EMACS_TIME difference; 4359 if (timespec_valid_p (timer_delay))
4410 EMACS_SUB_TIME (difference, timer_delay, timeout);
4411 if (EMACS_TIME_NEG_P (difference))
4412 { 4360 {
4413 timeout = timer_delay; 4361 if (timespec_cmp (timer_delay, timeout) < 0)
4414 timeout_reduced_for_timers = 1; 4362 {
4363 timeout = timer_delay;
4364 timeout_reduced_for_timers = 1;
4365 }
4366 }
4367 else
4368 {
4369 /* This is so a breakpoint can be put here. */
4370 wait_reading_process_output_1 ();
4415 } 4371 }
4416 }
4417 /* If time_limit is -1, we are not going to wait at all. */
4418 else if (time_limit != -1)
4419 {
4420 /* This is so a breakpoint can be put here. */
4421 wait_reading_process_output_1 ();
4422 } 4372 }
4423 } 4373 }
4424 4374
@@ -4437,8 +4387,8 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4437 timeout to get our attention. */ 4387 timeout to get our attention. */
4438 if (update_tick != process_tick) 4388 if (update_tick != process_tick)
4439 { 4389 {
4440 SELECT_TYPE Atemp; 4390 fd_set Atemp;
4441 SELECT_TYPE Ctemp; 4391 fd_set Ctemp;
4442 4392
4443 if (kbd_on_hold_p ()) 4393 if (kbd_on_hold_p ())
4444 FD_ZERO (&Atemp); 4394 FD_ZERO (&Atemp);
@@ -4446,15 +4396,15 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4446 Atemp = input_wait_mask; 4396 Atemp = input_wait_mask;
4447 Ctemp = write_mask; 4397 Ctemp = write_mask;
4448 4398
4449 EMACS_SET_SECS_USECS (timeout, 0, 0); 4399 timeout = make_timespec (0, 0);
4450 if ((select (max (max_process_desc, max_input_desc) + 1, 4400 if ((pselect (max (max_process_desc, max_input_desc) + 1,
4451 &Atemp, 4401 &Atemp,
4452#ifdef NON_BLOCKING_CONNECT 4402#ifdef NON_BLOCKING_CONNECT
4453 (num_pending_connects > 0 ? &Ctemp : (SELECT_TYPE *)0), 4403 (num_pending_connects > 0 ? &Ctemp : NULL),
4454#else 4404#else
4455 (SELECT_TYPE *)0, 4405 NULL,
4456#endif 4406#endif
4457 (SELECT_TYPE *)0, &timeout) 4407 NULL, &timeout, NULL)
4458 <= 0)) 4408 <= 0))
4459 { 4409 {
4460 /* It's okay for us to do this and then continue with 4410 /* It's okay for us to do this and then continue with
@@ -4473,7 +4423,7 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4473 && ! EQ (wait_proc->status, Qrun) 4423 && ! EQ (wait_proc->status, Qrun)
4474 && ! EQ (wait_proc->status, Qconnect)) 4424 && ! EQ (wait_proc->status, Qconnect))
4475 { 4425 {
4476 int nread, total_nread = 0; 4426 bool read_some_bytes = 0;
4477 4427
4478 clear_waiting_for_input (); 4428 clear_waiting_for_input ();
4479 XSETPROCESS (proc, wait_proc); 4429 XSETPROCESS (proc, wait_proc);
@@ -4481,30 +4431,21 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4481 /* Read data from the process, until we exhaust it. */ 4431 /* Read data from the process, until we exhaust it. */
4482 while (wait_proc->infd >= 0) 4432 while (wait_proc->infd >= 0)
4483 { 4433 {
4484 nread = read_process_output (proc, wait_proc->infd); 4434 int nread = read_process_output (proc, wait_proc->infd);
4485 4435
4486 if (nread == 0) 4436 if (nread == 0)
4487 break; 4437 break;
4488 4438
4489 if (0 < nread) 4439 if (nread > 0)
4490 { 4440 got_some_input = read_some_bytes = 1;
4491 total_nread += nread; 4441 else if (nread == -1 && (errno == EIO || errno == EAGAIN))
4492 got_some_input = 1;
4493 }
4494#ifdef EIO
4495 else if (nread == -1 && EIO == errno)
4496 break;
4497#endif
4498#ifdef EAGAIN
4499 else if (nread == -1 && EAGAIN == errno)
4500 break; 4442 break;
4501#endif
4502#ifdef EWOULDBLOCK 4443#ifdef EWOULDBLOCK
4503 else if (nread == -1 && EWOULDBLOCK == errno) 4444 else if (nread == -1 && EWOULDBLOCK == errno)
4504 break; 4445 break;
4505#endif 4446#endif
4506 } 4447 }
4507 if (total_nread > 0 && do_display) 4448 if (read_some_bytes && do_display)
4508 redisplay_preserve_echo_area (10); 4449 redisplay_preserve_echo_area (10);
4509 4450
4510 break; 4451 break;
@@ -4577,9 +4518,9 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4577 Vprocess_adaptive_read_buffering is nil. */ 4518 Vprocess_adaptive_read_buffering is nil. */
4578 if (process_output_skip && check_delay > 0) 4519 if (process_output_skip && check_delay > 0)
4579 { 4520 {
4580 int usecs = EMACS_USECS (timeout); 4521 int nsecs = timeout.tv_nsec;
4581 if (EMACS_SECS (timeout) > 0 || usecs > READ_OUTPUT_DELAY_MAX) 4522 if (timeout.tv_sec > 0 || nsecs > READ_OUTPUT_DELAY_MAX)
4582 usecs = READ_OUTPUT_DELAY_MAX; 4523 nsecs = READ_OUTPUT_DELAY_MAX;
4583 for (channel = 0; check_delay > 0 && channel <= max_process_desc; channel++) 4524 for (channel = 0; check_delay > 0 && channel <= max_process_desc; channel++)
4584 { 4525 {
4585 proc = chan_process[channel]; 4526 proc = chan_process[channel];
@@ -4594,25 +4535,26 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4594 continue; 4535 continue;
4595 FD_CLR (channel, &Available); 4536 FD_CLR (channel, &Available);
4596 XPROCESS (proc)->read_output_skip = 0; 4537 XPROCESS (proc)->read_output_skip = 0;
4597 if (XPROCESS (proc)->read_output_delay < usecs) 4538 if (XPROCESS (proc)->read_output_delay < nsecs)
4598 usecs = XPROCESS (proc)->read_output_delay; 4539 nsecs = XPROCESS (proc)->read_output_delay;
4599 } 4540 }
4600 } 4541 }
4601 EMACS_SET_SECS_USECS (timeout, 0, usecs); 4542 timeout = make_timespec (0, nsecs);
4602 process_output_skip = 0; 4543 process_output_skip = 0;
4603 } 4544 }
4604#endif 4545#endif
4605#if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS) 4546
4606 nfds = xg_select 4547#if defined (HAVE_NS)
4607#elif defined (HAVE_NS) 4548 nfds = ns_select
4608 nfds = ns_select 4549#elif defined (HAVE_GLIB)
4550 nfds = xg_select
4609#else 4551#else
4610 nfds = select 4552 nfds = pselect
4611#endif 4553#endif
4612 (max (max_process_desc, max_input_desc) + 1, 4554 (max (max_process_desc, max_input_desc) + 1,
4613 &Available, 4555 &Available,
4614 (check_write ? &Writeok : (SELECT_TYPE *)0), 4556 (check_write ? &Writeok : 0),
4615 (SELECT_TYPE *)0, &timeout); 4557 NULL, &timeout, NULL);
4616 4558
4617#ifdef HAVE_GNUTLS 4559#ifdef HAVE_GNUTLS
4618 /* GnuTLS buffers data internally. In lowat mode it leaves 4560 /* GnuTLS buffers data internally. In lowat mode it leaves
@@ -4629,7 +4571,7 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4629 the gnutls library -- 2.12.14 has been confirmed 4571 the gnutls library -- 2.12.14 has been confirmed
4630 to need it. See 4572 to need it. See
4631 http://comments.gmane.org/gmane.emacs.devel/145074 */ 4573 http://comments.gmane.org/gmane.emacs.devel/145074 */
4632 for (channel = 0; channel < MAXDESC; ++channel) 4574 for (channel = 0; channel < FD_SETSIZE; ++channel)
4633 if (! NILP (chan_process[channel])) 4575 if (! NILP (chan_process[channel]))
4634 { 4576 {
4635 struct Lisp_Process *p = 4577 struct Lisp_Process *p =
@@ -4670,30 +4612,17 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4670 /* If we woke up due to SIGWINCH, actually change size now. */ 4612 /* If we woke up due to SIGWINCH, actually change size now. */
4671 do_pending_window_change (0); 4613 do_pending_window_change (0);
4672 4614
4673 if (time_limit && nfds == 0 && ! timeout_reduced_for_timers) 4615 if ((time_limit || nsecs) && nfds == 0 && ! timeout_reduced_for_timers)
4674 /* We wanted the full specified time, so return now. */ 4616 /* We waited the full specified time, so return now. */
4675 break; 4617 break;
4676 if (nfds < 0) 4618 if (nfds < 0)
4677 { 4619 {
4678 if (xerrno == EINTR) 4620 if (xerrno == EINTR)
4679 no_avail = 1; 4621 no_avail = 1;
4680 else if (xerrno == EBADF) 4622 else if (xerrno == EBADF)
4681 { 4623 emacs_abort ();
4682#ifdef AIX
4683 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4684 the child's closure of the pts gives the parent a SIGHUP, and
4685 the ptc file descriptor is automatically closed,
4686 yielding EBADF here or at select() call above.
4687 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4688 in m/ibmrt-aix.h), and here we just ignore the select error.
4689 Cleanup occurs c/o status_notify after SIGCLD. */
4690 no_avail = 1; /* Cannot depend on values returned */
4691#else
4692 abort ();
4693#endif
4694 }
4695 else 4624 else
4696 error ("select error: %s", emacs_strerror (xerrno)); 4625 report_file_errno ("Failed select", Qnil, xerrno);
4697 } 4626 }
4698 4627
4699 if (no_avail) 4628 if (no_avail)
@@ -4702,31 +4631,16 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4702 check_write = 0; 4631 check_write = 0;
4703 } 4632 }
4704 4633
4705#if 0 /* When polling is used, interrupt_input is 0,
4706 so get_input_pending should read the input.
4707 So this should not be needed. */
4708 /* If we are using polling for input,
4709 and we see input available, make it get read now.
4710 Otherwise it might not actually get read for a second.
4711 And on hpux, since we turn off polling in wait_reading_process_output,
4712 it might never get read at all if we don't spend much time
4713 outside of wait_reading_process_output. */
4714 if (read_kbd && interrupt_input
4715 && keyboard_bit_set (&Available)
4716 && input_polling_used ())
4717 kill (getpid (), SIGALRM);
4718#endif
4719
4720 /* Check for keyboard input */ 4634 /* Check for keyboard input */
4721 /* If there is any, return immediately 4635 /* If there is any, return immediately
4722 to give it higher priority than subprocesses */ 4636 to give it higher priority than subprocesses */
4723 4637
4724 if (read_kbd != 0) 4638 if (read_kbd != 0)
4725 { 4639 {
4726 int old_timers_run = timers_run; 4640 unsigned old_timers_run = timers_run;
4727 struct buffer *old_buffer = current_buffer; 4641 struct buffer *old_buffer = current_buffer;
4728 Lisp_Object old_window = selected_window; 4642 Lisp_Object old_window = selected_window;
4729 int leave = 0; 4643 bool leave = 0;
4730 4644
4731 if (detect_input_pending_run_timers (do_display)) 4645 if (detect_input_pending_run_timers (do_display))
4732 { 4646 {
@@ -4772,7 +4686,7 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4772 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell))) 4686 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
4773 break; 4687 break;
4774 4688
4775#ifdef SIGIO 4689#ifdef USABLE_SIGIO
4776 /* If we think we have keyboard input waiting, but didn't get SIGIO, 4690 /* If we think we have keyboard input waiting, but didn't get SIGIO,
4777 go read it. This can happen with X on BSD after logging out. 4691 go read it. This can happen with X on BSD after logging out.
4778 In that case, there really is no input and no SIGIO, 4692 In that case, there really is no input and no SIGIO,
@@ -4780,7 +4694,7 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4780 4694
4781 if (read_kbd && interrupt_input 4695 if (read_kbd && interrupt_input
4782 && keyboard_bit_set (&Available) && ! noninteractive) 4696 && keyboard_bit_set (&Available) && ! noninteractive)
4783 kill (getpid (), SIGIO); 4697 handle_input_available_signal (SIGIO);
4784#endif 4698#endif
4785 4699
4786 if (! wait_proc) 4700 if (! wait_proc)
@@ -4798,15 +4712,13 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4798 for (channel = 0; channel <= max_input_desc; ++channel) 4712 for (channel = 0; channel <= max_input_desc; ++channel)
4799 { 4713 {
4800 struct fd_callback_data *d = &fd_callback_info[channel]; 4714 struct fd_callback_data *d = &fd_callback_info[channel];
4801 if (FD_ISSET (channel, &Available) 4715 if (d->func
4802 && d->func != 0 4716 && ((d->condition & FOR_READ
4803 && (d->condition & FOR_READ) != 0) 4717 && FD_ISSET (channel, &Available))
4804 d->func (channel, d->data, 1); 4718 || (d->condition & FOR_WRITE
4805 if (FD_ISSET (channel, &write_mask) 4719 && FD_ISSET (channel, &write_mask))))
4806 && d->func != 0 4720 d->func (channel, d->data);
4807 && (d->condition & FOR_WRITE) != 0) 4721 }
4808 d->func (channel, d->data, 0);
4809 }
4810 4722
4811 for (channel = 0; channel <= max_process_desc; channel++) 4723 for (channel = 0; channel <= max_process_desc; channel++)
4812 { 4724 {
@@ -4822,7 +4734,7 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4822 if (wait_channel == channel) 4734 if (wait_channel == channel)
4823 { 4735 {
4824 wait_channel = -1; 4736 wait_channel = -1;
4825 time_limit = -1; 4737 nsecs = -1;
4826 got_some_input = 1; 4738 got_some_input = 1;
4827 } 4739 }
4828 proc = chan_process[channel]; 4740 proc = chan_process[channel];
@@ -4855,23 +4767,17 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4855 else if (nread == -1 && errno == EWOULDBLOCK) 4767 else if (nread == -1 && errno == EWOULDBLOCK)
4856 ; 4768 ;
4857#endif 4769#endif
4858 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
4859 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
4860#ifdef O_NONBLOCK
4861 else if (nread == -1 && errno == EAGAIN)
4862 ;
4863#else
4864#ifdef O_NDELAY
4865 else if (nread == -1 && errno == EAGAIN) 4770 else if (nread == -1 && errno == EAGAIN)
4866 ; 4771 ;
4772#ifdef WINDOWSNT
4773 /* FIXME: Is this special case still needed? */
4867 /* Note that we cannot distinguish between no input 4774 /* Note that we cannot distinguish between no input
4868 available now and a closed pipe. 4775 available now and a closed pipe.
4869 With luck, a closed pipe will be accompanied by 4776 With luck, a closed pipe will be accompanied by
4870 subprocess termination and SIGCHLD. */ 4777 subprocess termination and SIGCHLD. */
4871 else if (nread == 0 && !NETCONN_P (proc) && !SERIALCONN_P (proc)) 4778 else if (nread == 0 && !NETCONN_P (proc) && !SERIALCONN_P (proc))
4872 ; 4779 ;
4873#endif /* O_NDELAY */ 4780#endif
4874#endif /* O_NONBLOCK */
4875#ifdef HAVE_PTYS 4781#ifdef HAVE_PTYS
4876 /* On some OSs with ptys, when the process on one end of 4782 /* On some OSs with ptys, when the process on one end of
4877 a pty exits, the other end gets an error reading with 4783 a pty exits, the other end gets an error reading with
@@ -4879,11 +4785,7 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4879 Therefore, if we get an error reading and errno = 4785 Therefore, if we get an error reading and errno =
4880 EIO, just continue, because the child process has 4786 EIO, just continue, because the child process has
4881 exited and should clean itself up soon (e.g. when we 4787 exited and should clean itself up soon (e.g. when we
4882 get a SIGCHLD). 4788 get a SIGCHLD). */
4883
4884 However, it has been known to happen that the SIGCHLD
4885 got lost. So raise the signal again just in case.
4886 It can't hurt. */
4887 else if (nread == -1 && errno == EIO) 4789 else if (nread == -1 && errno == EIO)
4888 { 4790 {
4889 struct Lisp_Process *p = XPROCESS (proc); 4791 struct Lisp_Process *p = XPROCESS (proc);
@@ -4895,22 +4797,18 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4895 4797
4896 if (p->pid == -2) 4798 if (p->pid == -2)
4897 { 4799 {
4898 /* If the EIO occurs on a pty, sigchld_handler's 4800 /* If the EIO occurs on a pty, the SIGCHLD handler's
4899 wait3() will not find the process object to 4801 waitpid call will not find the process object to
4900 delete. Do it here. */ 4802 delete. Do it here. */
4901 p->tick = ++process_tick; 4803 p->tick = ++process_tick;
4902 p->status = Qfailed; 4804 pset_status (p, Qfailed);
4903 } 4805 }
4904 else
4905 kill (getpid (), SIGCHLD);
4906 } 4806 }
4907#endif /* HAVE_PTYS */ 4807#endif /* HAVE_PTYS */
4908 /* If we can detect process termination, don't consider the 4808 /* If we can detect process termination, don't consider the
4909 process gone just because its pipe is closed. */ 4809 process gone just because its pipe is closed. */
4910#ifdef SIGCHLD
4911 else if (nread == 0 && !NETCONN_P (proc) && !SERIALCONN_P (proc)) 4810 else if (nread == 0 && !NETCONN_P (proc) && !SERIALCONN_P (proc))
4912 ; 4811 ;
4913#endif
4914 else 4812 else
4915 { 4813 {
4916 /* Preserve status of processes already terminated. */ 4814 /* Preserve status of processes already terminated. */
@@ -4919,8 +4817,8 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4919 if (XPROCESS (proc)->raw_status_new) 4817 if (XPROCESS (proc)->raw_status_new)
4920 update_status (XPROCESS (proc)); 4818 update_status (XPROCESS (proc));
4921 if (EQ (XPROCESS (proc)->status, Qrun)) 4819 if (EQ (XPROCESS (proc)->status, Qrun))
4922 XPROCESS (proc)->status 4820 pset_status (XPROCESS (proc),
4923 = Fcons (Qexit, Fcons (make_number (256), Qnil)); 4821 list2 (Qexit, make_number (256)));
4924 } 4822 }
4925 } 4823 }
4926#ifdef NON_BLOCKING_CONNECT 4824#ifdef NON_BLOCKING_CONNECT
@@ -4932,7 +4830,7 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4932 FD_CLR (channel, &connect_wait_mask); 4830 FD_CLR (channel, &connect_wait_mask);
4933 FD_CLR (channel, &write_mask); 4831 FD_CLR (channel, &write_mask);
4934 if (--num_pending_connects < 0) 4832 if (--num_pending_connects < 0)
4935 abort (); 4833 emacs_abort ();
4936 4834
4937 proc = chan_process[channel]; 4835 proc = chan_process[channel];
4938 if (NILP (proc)) 4836 if (NILP (proc))
@@ -4968,12 +4866,12 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4968 if (xerrno) 4866 if (xerrno)
4969 { 4867 {
4970 p->tick = ++process_tick; 4868 p->tick = ++process_tick;
4971 p->status = Fcons (Qfailed, Fcons (make_number (xerrno), Qnil)); 4869 pset_status (p, list2 (Qfailed, make_number (xerrno)));
4972 deactivate_process (proc); 4870 deactivate_process (proc);
4973 } 4871 }
4974 else 4872 else
4975 { 4873 {
4976 p->status = Qrun; 4874 pset_status (p, Qrun);
4977 /* Execute the sentinel here. If we had relied on 4875 /* Execute the sentinel here. If we had relied on
4978 status_notify to do it later, it will read input 4876 status_notify to do it later, it will read input
4979 from the process before calling the sentinel. */ 4877 from the process before calling the sentinel. */
@@ -4986,8 +4884,8 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4986 } 4884 }
4987 } 4885 }
4988#endif /* NON_BLOCKING_CONNECT */ 4886#endif /* NON_BLOCKING_CONNECT */
4989 } /* end for each file descriptor */ 4887 } /* End for each file descriptor. */
4990 } /* end while exit conditions not met */ 4888 } /* End while exit conditions not met. */
4991 4889
4992 unbind_to (count, Qnil); 4890 unbind_to (count, Qnil);
4993 4891
@@ -5022,6 +4920,11 @@ read_process_output_error_handler (Lisp_Object error_val)
5022 return Qt; 4920 return Qt;
5023} 4921}
5024 4922
4923static void
4924read_and_dispose_of_process_output (struct Lisp_Process *p, char *chars,
4925 ssize_t nbytes,
4926 struct coding_system *coding);
4927
5025/* Read pending output from the process channel, 4928/* Read pending output from the process channel,
5026 starting with our buffered-ahead character if we have one. 4929 starting with our buffered-ahead character if we have one.
5027 Yield number of decoded characters read. 4930 Yield number of decoded characters read.
@@ -5038,16 +4941,14 @@ read_process_output (Lisp_Object proc, register int channel)
5038{ 4941{
5039 register ssize_t nbytes; 4942 register ssize_t nbytes;
5040 char *chars; 4943 char *chars;
5041 register Lisp_Object outstream;
5042 register struct Lisp_Process *p = XPROCESS (proc); 4944 register struct Lisp_Process *p = XPROCESS (proc);
5043 register ptrdiff_t opoint;
5044 struct coding_system *coding = proc_decode_coding_system[channel]; 4945 struct coding_system *coding = proc_decode_coding_system[channel];
5045 int carryover = p->decoding_carryover; 4946 int carryover = p->decoding_carryover;
5046 int readmax = 4096; 4947 int readmax = 4096;
5047 ptrdiff_t count = SPECPDL_INDEX (); 4948 ptrdiff_t count = SPECPDL_INDEX ();
5048 Lisp_Object odeactivate; 4949 Lisp_Object odeactivate;
5049 4950
5050 chars = (char *) alloca (carryover + readmax); 4951 chars = alloca (carryover + readmax);
5051 if (carryover) 4952 if (carryover)
5052 /* See the comment above. */ 4953 /* See the comment above. */
5053 memcpy (chars, SDATA (p->decoding_buf), carryover); 4954 memcpy (chars, SDATA (p->decoding_buf), carryover);
@@ -5063,7 +4964,7 @@ read_process_output (Lisp_Object proc, register int channel)
5063 else 4964 else
5064#endif 4965#endif
5065 { 4966 {
5066 int buffered = 0 <= proc_buffered_char[channel]; 4967 bool buffered = proc_buffered_char[channel] >= 0;
5067 if (buffered) 4968 if (buffered)
5068 { 4969 {
5069 chars[carryover] = proc_buffered_char[channel]; 4970 chars[carryover] = proc_buffered_char[channel];
@@ -5126,124 +5027,144 @@ read_process_output (Lisp_Object proc, register int channel)
5126 /* There's no good reason to let process filters change the current 5027 /* There's no good reason to let process filters change the current
5127 buffer, and many callers of accept-process-output, sit-for, and 5028 buffer, and many callers of accept-process-output, sit-for, and
5128 friends don't expect current-buffer to be changed from under them. */ 5029 friends don't expect current-buffer to be changed from under them. */
5129 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ()); 5030 record_unwind_current_buffer ();
5130 5031
5131 /* Read and dispose of the process output. */ 5032 read_and_dispose_of_process_output (p, chars, nbytes, coding);
5132 outstream = p->filter; 5033
5133 if (!NILP (outstream)) 5034 /* Handling the process output should not deactivate the mark. */
5134 { 5035 Vdeactivate_mark = odeactivate;
5135 Lisp_Object text; 5036
5136 int outer_running_asynch_code = running_asynch_code; 5037 unbind_to (count, Qnil);
5137 int waiting = waiting_for_user_input_p; 5038 return nbytes;
5039}
5040
5041static void
5042read_and_dispose_of_process_output (struct Lisp_Process *p, char *chars,
5043 ssize_t nbytes,
5044 struct coding_system *coding)
5045{
5046 Lisp_Object outstream = p->filter;
5047 Lisp_Object text;
5048 bool outer_running_asynch_code = running_asynch_code;
5049 int waiting = waiting_for_user_input_p;
5138 5050
5139 /* No need to gcpro these, because all we do with them later 5051 /* No need to gcpro these, because all we do with them later
5140 is test them for EQness, and none of them should be a string. */ 5052 is test them for EQness, and none of them should be a string. */
5141#if 0 5053#if 0
5142 Lisp_Object obuffer, okeymap; 5054 Lisp_Object obuffer, okeymap;
5143 XSETBUFFER (obuffer, current_buffer); 5055 XSETBUFFER (obuffer, current_buffer);
5144 okeymap = BVAR (current_buffer, keymap); 5056 okeymap = BVAR (current_buffer, keymap);
5145#endif 5057#endif
5146 5058
5147 /* We inhibit quit here instead of just catching it so that 5059 /* We inhibit quit here instead of just catching it so that
5148 hitting ^G when a filter happens to be running won't screw 5060 hitting ^G when a filter happens to be running won't screw
5149 it up. */ 5061 it up. */
5150 specbind (Qinhibit_quit, Qt); 5062 specbind (Qinhibit_quit, Qt);
5151 specbind (Qlast_nonmenu_event, Qt); 5063 specbind (Qlast_nonmenu_event, Qt);
5152
5153 /* In case we get recursively called,
5154 and we already saved the match data nonrecursively,
5155 save the same match data in safely recursive fashion. */
5156 if (outer_running_asynch_code)
5157 {
5158 Lisp_Object tem;
5159 /* Don't clobber the CURRENT match data, either! */
5160 tem = Fmatch_data (Qnil, Qnil, Qnil);
5161 restore_search_regs ();
5162 record_unwind_save_match_data ();
5163 Fset_match_data (tem, Qt);
5164 }
5165 5064
5166 /* For speed, if a search happens within this code, 5065 /* In case we get recursively called,
5167 save the match data in a special nonrecursive fashion. */ 5066 and we already saved the match data nonrecursively,
5168 running_asynch_code = 1; 5067 save the same match data in safely recursive fashion. */
5068 if (outer_running_asynch_code)
5069 {
5070 Lisp_Object tem;
5071 /* Don't clobber the CURRENT match data, either! */
5072 tem = Fmatch_data (Qnil, Qnil, Qnil);
5073 restore_search_regs ();
5074 record_unwind_save_match_data ();
5075 Fset_match_data (tem, Qt);
5076 }
5169 5077
5170 decode_coding_c_string (coding, (unsigned char *) chars, nbytes, Qt); 5078 /* For speed, if a search happens within this code,
5171 text = coding->dst_object; 5079 save the match data in a special nonrecursive fashion. */
5172 Vlast_coding_system_used = CODING_ID_NAME (coding->id); 5080 running_asynch_code = 1;
5173 /* A new coding system might be found. */
5174 if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
5175 {
5176 p->decode_coding_system = Vlast_coding_system_used;
5177 5081
5178 /* Don't call setup_coding_system for 5082 decode_coding_c_string (coding, (unsigned char *) chars, nbytes, Qt);
5179 proc_decode_coding_system[channel] here. It is done in 5083 text = coding->dst_object;
5180 detect_coding called via decode_coding above. */ 5084 Vlast_coding_system_used = CODING_ID_NAME (coding->id);
5085 /* A new coding system might be found. */
5086 if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
5087 {
5088 pset_decode_coding_system (p, Vlast_coding_system_used);
5181 5089
5182 /* If a coding system for encoding is not yet decided, we set 5090 /* Don't call setup_coding_system for
5183 it as the same as coding-system for decoding. 5091 proc_decode_coding_system[channel] here. It is done in
5092 detect_coding called via decode_coding above. */
5184 5093
5185 But, before doing that we must check if 5094 /* If a coding system for encoding is not yet decided, we set
5186 proc_encode_coding_system[p->outfd] surely points to a 5095 it as the same as coding-system for decoding.
5187 valid memory because p->outfd will be changed once EOF is
5188 sent to the process. */
5189 if (NILP (p->encode_coding_system)
5190 && proc_encode_coding_system[p->outfd])
5191 {
5192 p->encode_coding_system
5193 = coding_inherit_eol_type (Vlast_coding_system_used, Qnil);
5194 setup_coding_system (p->encode_coding_system,
5195 proc_encode_coding_system[p->outfd]);
5196 }
5197 }
5198 5096
5199 if (coding->carryover_bytes > 0) 5097 But, before doing that we must check if
5098 proc_encode_coding_system[p->outfd] surely points to a
5099 valid memory because p->outfd will be changed once EOF is
5100 sent to the process. */
5101 if (NILP (p->encode_coding_system)
5102 && proc_encode_coding_system[p->outfd])
5200 { 5103 {
5201 if (SCHARS (p->decoding_buf) < coding->carryover_bytes) 5104 pset_encode_coding_system
5202 p->decoding_buf = make_uninit_string (coding->carryover_bytes); 5105 (p, coding_inherit_eol_type (Vlast_coding_system_used, Qnil));
5203 memcpy (SDATA (p->decoding_buf), coding->carryover, 5106 setup_coding_system (p->encode_coding_system,
5204 coding->carryover_bytes); 5107 proc_encode_coding_system[p->outfd]);
5205 p->decoding_carryover = coding->carryover_bytes;
5206 } 5108 }
5207 if (SBYTES (text) > 0) 5109 }
5208 /* FIXME: It's wrong to wrap or not based on debug-on-error, and 5110
5209 sometimes it's simply wrong to wrap (e.g. when called from 5111 if (coding->carryover_bytes > 0)
5210 accept-process-output). */ 5112 {
5211 internal_condition_case_1 (read_process_output_call, 5113 if (SCHARS (p->decoding_buf) < coding->carryover_bytes)
5212 Fcons (outstream, 5114 pset_decoding_buf (p, make_uninit_string (coding->carryover_bytes));
5213 Fcons (proc, Fcons (text, Qnil))), 5115 memcpy (SDATA (p->decoding_buf), coding->carryover,
5214 !NILP (Vdebug_on_error) ? Qnil : Qerror, 5116 coding->carryover_bytes);
5215 read_process_output_error_handler); 5117 p->decoding_carryover = coding->carryover_bytes;
5216 5118 }
5217 /* If we saved the match data nonrecursively, restore it now. */ 5119 if (SBYTES (text) > 0)
5218 restore_search_regs (); 5120 /* FIXME: It's wrong to wrap or not based on debug-on-error, and
5219 running_asynch_code = outer_running_asynch_code; 5121 sometimes it's simply wrong to wrap (e.g. when called from
5122 accept-process-output). */
5123 internal_condition_case_1 (read_process_output_call,
5124 list3 (outstream, make_lisp_proc (p), text),
5125 !NILP (Vdebug_on_error) ? Qnil : Qerror,
5126 read_process_output_error_handler);
5220 5127
5221 /* Restore waiting_for_user_input_p as it was 5128 /* If we saved the match data nonrecursively, restore it now. */
5222 when we were called, in case the filter clobbered it. */ 5129 restore_search_regs ();
5223 waiting_for_user_input_p = waiting; 5130 running_asynch_code = outer_running_asynch_code;
5131
5132 /* Restore waiting_for_user_input_p as it was
5133 when we were called, in case the filter clobbered it. */
5134 waiting_for_user_input_p = waiting;
5224 5135
5225#if 0 /* Call record_asynch_buffer_change unconditionally, 5136#if 0 /* Call record_asynch_buffer_change unconditionally,
5226 because we might have changed minor modes or other things 5137 because we might have changed minor modes or other things
5227 that affect key bindings. */ 5138 that affect key bindings. */
5228 if (! EQ (Fcurrent_buffer (), obuffer) 5139 if (! EQ (Fcurrent_buffer (), obuffer)
5229 || ! EQ (current_buffer->keymap, okeymap)) 5140 || ! EQ (current_buffer->keymap, okeymap))
5230#endif 5141#endif
5231 /* But do it only if the caller is actually going to read events. 5142 /* But do it only if the caller is actually going to read events.
5232 Otherwise there's no need to make him wake up, and it could 5143 Otherwise there's no need to make him wake up, and it could
5233 cause trouble (for example it would make sit_for return). */ 5144 cause trouble (for example it would make sit_for return). */
5234 if (waiting_for_user_input_p == -1) 5145 if (waiting_for_user_input_p == -1)
5235 record_asynch_buffer_change (); 5146 record_asynch_buffer_change ();
5236 } 5147}
5148
5149DEFUN ("internal-default-process-filter", Finternal_default_process_filter,
5150 Sinternal_default_process_filter, 2, 2, 0,
5151 doc: /* Function used as default process filter. */)
5152 (Lisp_Object proc, Lisp_Object text)
5153{
5154 struct Lisp_Process *p;
5155 ptrdiff_t opoint;
5156
5157 CHECK_PROCESS (proc);
5158 p = XPROCESS (proc);
5159 CHECK_STRING (text);
5237 5160
5238 /* If no filter, write into buffer if it isn't dead. */ 5161 if (!NILP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
5239 else if (!NILP (p->buffer) && !NILP (BVAR (XBUFFER (p->buffer), name)))
5240 { 5162 {
5241 Lisp_Object old_read_only; 5163 Lisp_Object old_read_only;
5242 ptrdiff_t old_begv, old_zv; 5164 ptrdiff_t old_begv, old_zv;
5243 ptrdiff_t old_begv_byte, old_zv_byte; 5165 ptrdiff_t old_begv_byte, old_zv_byte;
5244 ptrdiff_t before, before_byte; 5166 ptrdiff_t before, before_byte;
5245 ptrdiff_t opoint_byte; 5167 ptrdiff_t opoint_byte;
5246 Lisp_Object text;
5247 struct buffer *b; 5168 struct buffer *b;
5248 5169
5249 Fset_buffer (p->buffer); 5170 Fset_buffer (p->buffer);
@@ -5255,15 +5176,12 @@ read_process_output (Lisp_Object proc, register int channel)
5255 old_begv_byte = BEGV_BYTE; 5176 old_begv_byte = BEGV_BYTE;
5256 old_zv_byte = ZV_BYTE; 5177 old_zv_byte = ZV_BYTE;
5257 5178
5258 BVAR (current_buffer, read_only) = Qnil; 5179 bset_read_only (current_buffer, Qnil);
5259 5180
5260 /* Insert new output into buffer 5181 /* Insert new output into buffer at the current end-of-output
5261 at the current end-of-output marker, 5182 marker, thus preserving logical ordering of input and output. */
5262 thus preserving logical ordering of input and output. */
5263 if (XMARKER (p->mark)->buffer) 5183 if (XMARKER (p->mark)->buffer)
5264 SET_PT_BOTH (clip_to_bounds (BEGV, marker_position (p->mark), ZV), 5184 set_point_from_marker (p->mark);
5265 clip_to_bounds (BEGV_BYTE, marker_byte_position (p->mark),
5266 ZV_BYTE));
5267 else 5185 else
5268 SET_PT_BOTH (ZV, ZV_BYTE); 5186 SET_PT_BOTH (ZV, ZV_BYTE);
5269 before = PT; 5187 before = PT;
@@ -5274,31 +5192,6 @@ read_process_output (Lisp_Object proc, register int channel)
5274 if (! (BEGV <= PT && PT <= ZV)) 5192 if (! (BEGV <= PT && PT <= ZV))
5275 Fwiden (); 5193 Fwiden ();
5276 5194
5277 decode_coding_c_string (coding, (unsigned char *) chars, nbytes, Qt);
5278 text = coding->dst_object;
5279 Vlast_coding_system_used = CODING_ID_NAME (coding->id);
5280 /* A new coding system might be found. See the comment in the
5281 similar code in the previous `if' block. */
5282 if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
5283 {
5284 p->decode_coding_system = Vlast_coding_system_used;
5285 if (NILP (p->encode_coding_system)
5286 && proc_encode_coding_system[p->outfd])
5287 {
5288 p->encode_coding_system
5289 = coding_inherit_eol_type (Vlast_coding_system_used, Qnil);
5290 setup_coding_system (p->encode_coding_system,
5291 proc_encode_coding_system[p->outfd]);
5292 }
5293 }
5294 if (coding->carryover_bytes > 0)
5295 {
5296 if (SCHARS (p->decoding_buf) < coding->carryover_bytes)
5297 p->decoding_buf = make_uninit_string (coding->carryover_bytes);
5298 memcpy (SDATA (p->decoding_buf), coding->carryover,
5299 coding->carryover_bytes);
5300 p->decoding_carryover = coding->carryover_bytes;
5301 }
5302 /* Adjust the multibyteness of TEXT to that of the buffer. */ 5195 /* Adjust the multibyteness of TEXT to that of the buffer. */
5303 if (NILP (BVAR (current_buffer, enable_multibyte_characters)) 5196 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
5304 != ! STRING_MULTIBYTE (text)) 5197 != ! STRING_MULTIBYTE (text))
@@ -5343,32 +5236,84 @@ read_process_output (Lisp_Object proc, register int channel)
5343 if (old_begv != BEGV || old_zv != ZV) 5236 if (old_begv != BEGV || old_zv != ZV)
5344 Fnarrow_to_region (make_number (old_begv), make_number (old_zv)); 5237 Fnarrow_to_region (make_number (old_begv), make_number (old_zv));
5345 5238
5346 5239 bset_read_only (current_buffer, old_read_only);
5347 BVAR (current_buffer, read_only) = old_read_only;
5348 SET_PT_BOTH (opoint, opoint_byte); 5240 SET_PT_BOTH (opoint, opoint_byte);
5349 } 5241 }
5350 /* Handling the process output should not deactivate the mark. */ 5242 return Qnil;
5351 Vdeactivate_mark = odeactivate;
5352
5353 unbind_to (count, Qnil);
5354 return nbytes;
5355} 5243}
5356 5244
5357/* Sending data to subprocess */ 5245/* Sending data to subprocess. */
5358 5246
5359static jmp_buf send_process_frame; 5247/* In send_process, when a write fails temporarily,
5360static Lisp_Object process_sent_to; 5248 wait_reading_process_output is called. It may execute user code,
5249 e.g. timers, that attempts to write new data to the same process.
5250 We must ensure that data is sent in the right order, and not
5251 interspersed half-completed with other writes (Bug#10815). This is
5252 handled by the write_queue element of struct process. It is a list
5253 with each entry having the form
5361 5254
5362#ifndef FORWARD_SIGNAL_TO_MAIN_THREAD 5255 (string . (offset . length))
5363static void send_process_trap (int) NO_RETURN; 5256
5364#endif 5257 where STRING is a lisp string, OFFSET is the offset into the
5258 string's byte sequence from which we should begin to send, and
5259 LENGTH is the number of bytes left to send. */
5260
5261/* Create a new entry in write_queue.
5262 INPUT_OBJ should be a buffer, string Qt, or Qnil.
5263 BUF is a pointer to the string sequence of the input_obj or a C
5264 string in case of Qt or Qnil. */
5365 5265
5366static void 5266static void
5367send_process_trap (int ignore) 5267write_queue_push (struct Lisp_Process *p, Lisp_Object input_obj,
5268 const char *buf, ptrdiff_t len, bool front)
5269{
5270 ptrdiff_t offset;
5271 Lisp_Object entry, obj;
5272
5273 if (STRINGP (input_obj))
5274 {
5275 offset = buf - SSDATA (input_obj);
5276 obj = input_obj;
5277 }
5278 else
5279 {
5280 offset = 0;
5281 obj = make_unibyte_string (buf, len);
5282 }
5283
5284 entry = Fcons (obj, Fcons (make_number (offset), make_number (len)));
5285
5286 if (front)
5287 pset_write_queue (p, Fcons (entry, p->write_queue));
5288 else
5289 pset_write_queue (p, nconc2 (p->write_queue, list1 (entry)));
5290}
5291
5292/* Remove the first element in the write_queue of process P, put its
5293 contents in OBJ, BUF and LEN, and return true. If the
5294 write_queue is empty, return false. */
5295
5296static bool
5297write_queue_pop (struct Lisp_Process *p, Lisp_Object *obj,
5298 const char **buf, ptrdiff_t *len)
5368{ 5299{
5369 SIGNAL_THREAD_CHECK (SIGPIPE); 5300 Lisp_Object entry, offset_length;
5370 sigunblock (sigmask (SIGPIPE)); 5301 ptrdiff_t offset;
5371 longjmp (send_process_frame, 1); 5302
5303 if (NILP (p->write_queue))
5304 return 0;
5305
5306 entry = XCAR (p->write_queue);
5307 pset_write_queue (p, XCDR (p->write_queue));
5308
5309 *obj = XCAR (entry);
5310 offset_length = XCDR (entry);
5311
5312 *len = XINT (XCDR (offset_length));
5313 offset = XINT (XCAR (offset_length));
5314 *buf = SSDATA (*obj) + offset;
5315
5316 return 1;
5372} 5317}
5373 5318
5374/* Send some data to process PROC. 5319/* Send some data to process PROC.
@@ -5382,17 +5327,12 @@ send_process_trap (int ignore)
5382 This function can evaluate Lisp code and can garbage collect. */ 5327 This function can evaluate Lisp code and can garbage collect. */
5383 5328
5384static void 5329static void
5385send_process (volatile Lisp_Object proc, const char *volatile buf, 5330send_process (Lisp_Object proc, const char *buf, ptrdiff_t len,
5386 volatile ptrdiff_t len, volatile Lisp_Object object) 5331 Lisp_Object object)
5387{ 5332{
5388 /* Use volatile to protect variables from being clobbered by longjmp. */
5389 struct Lisp_Process *p = XPROCESS (proc); 5333 struct Lisp_Process *p = XPROCESS (proc);
5390 ssize_t rv; 5334 ssize_t rv;
5391 struct coding_system *coding; 5335 struct coding_system *coding;
5392 struct gcpro gcpro1;
5393 void (*volatile old_sigpipe) (int);
5394
5395 GCPRO1 (object);
5396 5336
5397 if (p->raw_status_new) 5337 if (p->raw_status_new)
5398 update_status (p); 5338 update_status (p);
@@ -5409,8 +5349,8 @@ send_process (volatile Lisp_Object proc, const char *volatile buf,
5409 && !NILP (BVAR (XBUFFER (object), enable_multibyte_characters))) 5349 && !NILP (BVAR (XBUFFER (object), enable_multibyte_characters)))
5410 || EQ (object, Qt)) 5350 || EQ (object, Qt))
5411 { 5351 {
5412 p->encode_coding_system 5352 pset_encode_coding_system
5413 = complement_process_encoding_system (p->encode_coding_system); 5353 (p, complement_process_encoding_system (p->encode_coding_system));
5414 if (!EQ (Vlast_coding_system_used, p->encode_coding_system)) 5354 if (!EQ (Vlast_coding_system_used, p->encode_coding_system))
5415 { 5355 {
5416 /* The coding system for encoding was changed to raw-text 5356 /* The coding system for encoding was changed to raw-text
@@ -5486,158 +5426,123 @@ send_process (volatile Lisp_Object proc, const char *volatile buf,
5486 buf = SSDATA (object); 5426 buf = SSDATA (object);
5487 } 5427 }
5488 5428
5489 if (pty_max_bytes == 0) 5429 /* If there is already data in the write_queue, put the new data
5490 { 5430 in the back of queue. Otherwise, ignore it. */
5491#if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON) 5431 if (!NILP (p->write_queue))
5492 pty_max_bytes = fpathconf (p->outfd, _PC_MAX_CANON); 5432 write_queue_push (p, object, buf, len, 0);
5493 if (pty_max_bytes < 0)
5494 pty_max_bytes = 250;
5495#else
5496 pty_max_bytes = 250;
5497#endif
5498 /* Deduct one, to leave space for the eof. */
5499 pty_max_bytes--;
5500 }
5501 5433
5502 /* 2000-09-21: Emacs 20.7, sparc-sun-solaris-2.6, GCC 2.95.2, 5434 do /* while !NILP (p->write_queue) */
5503 CFLAGS="-g -O": The value of the parameter `proc' is clobbered
5504 when returning with longjmp despite being declared volatile. */
5505 if (!setjmp (send_process_frame))
5506 { 5435 {
5507 p = XPROCESS (proc); /* Repair any setjmp clobbering. */ 5436 ptrdiff_t cur_len = -1;
5437 const char *cur_buf;
5438 Lisp_Object cur_object;
5508 5439
5509 process_sent_to = proc; 5440 /* If write_queue is empty, ignore it. */
5510 while (len > 0) 5441 if (!write_queue_pop (p, &cur_object, &cur_buf, &cur_len))
5511 { 5442 {
5512 ptrdiff_t this = len; 5443 cur_len = len;
5444 cur_buf = buf;
5445 cur_object = object;
5446 }
5513 5447
5448 while (cur_len > 0)
5449 {
5514 /* Send this batch, using one or more write calls. */ 5450 /* Send this batch, using one or more write calls. */
5515 while (this > 0) 5451 ptrdiff_t written = 0;
5516 { 5452 int outfd = p->outfd;
5517 ptrdiff_t written = 0;
5518 int outfd = p->outfd;
5519 old_sigpipe = (void (*) (int)) signal (SIGPIPE, send_process_trap);
5520#ifdef DATAGRAM_SOCKETS 5453#ifdef DATAGRAM_SOCKETS
5521 if (DATAGRAM_CHAN_P (outfd)) 5454 if (DATAGRAM_CHAN_P (outfd))
5522 { 5455 {
5523 rv = sendto (outfd, buf, this, 5456 rv = sendto (outfd, cur_buf, cur_len,
5524 0, datagram_address[outfd].sa, 5457 0, datagram_address[outfd].sa,
5525 datagram_address[outfd].len); 5458 datagram_address[outfd].len);
5526 if (0 <= rv) 5459 if (rv >= 0)
5527 written = rv; 5460 written = rv;
5528 else if (errno == EMSGSIZE) 5461 else if (errno == EMSGSIZE)
5529 { 5462 report_file_error ("Sending datagram", proc);
5530 signal (SIGPIPE, old_sigpipe); 5463 }
5531 report_file_error ("sending datagram", 5464 else
5532 Fcons (proc, Qnil));
5533 }
5534 }
5535 else
5536#endif 5465#endif
5537 { 5466 {
5538#ifdef HAVE_GNUTLS 5467#ifdef HAVE_GNUTLS
5539 if (p->gnutls_p) 5468 if (p->gnutls_p)
5540 written = emacs_gnutls_write (p, buf, this); 5469 written = emacs_gnutls_write (p, cur_buf, cur_len);
5541 else 5470 else
5542#endif 5471#endif
5543 written = emacs_write (outfd, buf, this); 5472 written = emacs_write_sig (outfd, cur_buf, cur_len);
5544 rv = (written ? 0 : -1); 5473 rv = (written ? 0 : -1);
5545#ifdef ADAPTIVE_READ_BUFFERING 5474#ifdef ADAPTIVE_READ_BUFFERING
5546 if (p->read_output_delay > 0 5475 if (p->read_output_delay > 0
5547 && p->adaptive_read_buffering == 1) 5476 && p->adaptive_read_buffering == 1)
5548 { 5477 {
5549 p->read_output_delay = 0; 5478 p->read_output_delay = 0;
5550 process_output_delay_count--; 5479 process_output_delay_count--;
5551 p->read_output_skip = 0; 5480 p->read_output_skip = 0;
5552 }
5553#endif
5554 } 5481 }
5555 signal (SIGPIPE, old_sigpipe); 5482#endif
5483 }
5556 5484
5557 if (rv < 0) 5485 if (rv < 0)
5558 { 5486 {
5559 if (0 5487 if (errno == EAGAIN
5560#ifdef EWOULDBLOCK 5488#ifdef EWOULDBLOCK
5561 || errno == EWOULDBLOCK 5489 || errno == EWOULDBLOCK
5562#endif 5490#endif
5563#ifdef EAGAIN 5491 )
5564 || errno == EAGAIN 5492 /* Buffer is full. Wait, accepting input;
5565#endif 5493 that may allow the program
5566 ) 5494 to finish doing output and read more. */
5567 /* Buffer is full. Wait, accepting input; 5495 {
5568 that may allow the program
5569 to finish doing output and read more. */
5570 {
5571 ptrdiff_t offset = 0;
5572
5573#ifdef BROKEN_PTY_READ_AFTER_EAGAIN 5496#ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5574 /* A gross hack to work around a bug in FreeBSD. 5497 /* A gross hack to work around a bug in FreeBSD.
5575 In the following sequence, read(2) returns 5498 In the following sequence, read(2) returns
5576 bogus data: 5499 bogus data:
5577 5500
5578 write(2) 1022 bytes 5501 write(2) 1022 bytes
5579 write(2) 954 bytes, get EAGAIN 5502 write(2) 954 bytes, get EAGAIN
5580 read(2) 1024 bytes in process_read_output 5503 read(2) 1024 bytes in process_read_output
5581 read(2) 11 bytes in process_read_output 5504 read(2) 11 bytes in process_read_output
5582 5505
5583 That is, read(2) returns more bytes than have 5506 That is, read(2) returns more bytes than have
5584 ever been written successfully. The 1033 bytes 5507 ever been written successfully. The 1033 bytes
5585 read are the 1022 bytes written successfully 5508 read are the 1022 bytes written successfully
5586 after processing (for example with CRs added if 5509 after processing (for example with CRs added if
5587 the terminal is set up that way which it is 5510 the terminal is set up that way which it is
5588 here). The same bytes will be seen again in a 5511 here). The same bytes will be seen again in a
5589 later read(2), without the CRs. */ 5512 later read(2), without the CRs. */
5590 5513
5591 if (errno == EAGAIN) 5514 if (errno == EAGAIN)
5592 { 5515 {
5593 int flags = FWRITE; 5516 int flags = FWRITE;
5594 ioctl (p->outfd, TIOCFLUSH, &flags); 5517 ioctl (p->outfd, TIOCFLUSH, &flags);
5595 } 5518 }
5596#endif /* BROKEN_PTY_READ_AFTER_EAGAIN */ 5519#endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5597 5520
5598 /* Running filters might relocate buffers or strings. 5521 /* Put what we should have written in wait_queue. */
5599 Arrange to relocate BUF. */ 5522 write_queue_push (p, cur_object, cur_buf, cur_len, 1);
5600 if (BUFFERP (object)) 5523 wait_reading_process_output (0, 20 * 1000 * 1000,
5601 offset = BUF_PTR_BYTE_POS (XBUFFER (object), 5524 0, 0, Qnil, NULL, 0);
5602 (unsigned char *) buf); 5525 /* Reread queue, to see what is left. */
5603 else if (STRINGP (object)) 5526 break;
5604 offset = buf - SSDATA (object); 5527 }
5605 5528 else if (errno == EPIPE)
5606#ifdef EMACS_HAS_USECS 5529 {
5607 wait_reading_process_output (0, 20000, 0, 0, Qnil, NULL, 0); 5530 p->raw_status_new = 0;
5608#else 5531 pset_status (p, list2 (Qexit, make_number (256)));
5609 wait_reading_process_output (1, 0, 0, 0, Qnil, NULL, 0); 5532 p->tick = ++process_tick;
5610#endif 5533 deactivate_process (proc);
5611 5534 error ("process %s no longer connected to pipe; closed it",
5612 if (BUFFERP (object)) 5535 SDATA (p->name));
5613 buf = (char *) BUF_BYTE_ADDRESS (XBUFFER (object),
5614 offset);
5615 else if (STRINGP (object))
5616 buf = offset + SSDATA (object);
5617 }
5618 else
5619 /* This is a real error. */
5620 report_file_error ("writing to process", Fcons (proc, Qnil));
5621 } 5536 }
5622 buf += written; 5537 else
5623 len -= written; 5538 /* This is a real error. */
5624 this -= written; 5539 report_file_error ("Writing to process", proc);
5625 } 5540 }
5541 cur_buf += written;
5542 cur_len -= written;
5626 } 5543 }
5627 } 5544 }
5628 else 5545 while (!NILP (p->write_queue));
5629 {
5630 signal (SIGPIPE, old_sigpipe);
5631 proc = process_sent_to;
5632 p = XPROCESS (proc);
5633 p->raw_status_new = 0;
5634 p->status = Fcons (Qexit, Fcons (make_number (256), Qnil));
5635 p->tick = ++process_tick;
5636 deactivate_process (proc);
5637 error ("SIGPIPE raised on process %s; closed it", SDATA (p->name));
5638 }
5639
5640 UNGCPRO;
5641} 5546}
5642 5547
5643DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region, 5548DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region,
@@ -5651,19 +5556,19 @@ it is sent in several bunches. This may happen even for shorter regions.
5651Output from processes can arrive in between bunches. */) 5556Output from processes can arrive in between bunches. */)
5652 (Lisp_Object process, Lisp_Object start, Lisp_Object end) 5557 (Lisp_Object process, Lisp_Object start, Lisp_Object end)
5653{ 5558{
5654 Lisp_Object proc; 5559 Lisp_Object proc = get_process (process);
5655 ptrdiff_t start1, end1; 5560 ptrdiff_t start_byte, end_byte;
5656 5561
5657 proc = get_process (process);
5658 validate_region (&start, &end); 5562 validate_region (&start, &end);
5659 5563
5564 start_byte = CHAR_TO_BYTE (XINT (start));
5565 end_byte = CHAR_TO_BYTE (XINT (end));
5566
5660 if (XINT (start) < GPT && XINT (end) > GPT) 5567 if (XINT (start) < GPT && XINT (end) > GPT)
5661 move_gap (XINT (start)); 5568 move_gap_both (XINT (start), start_byte);
5662 5569
5663 start1 = CHAR_TO_BYTE (XINT (start)); 5570 send_process (proc, (char *) BYTE_POS_ADDR (start_byte),
5664 end1 = CHAR_TO_BYTE (XINT (end)); 5571 end_byte - start_byte, Fcurrent_buffer ());
5665 send_process (proc, (char *) BYTE_POS_ADDR (start1), end1 - start1,
5666 Fcurrent_buffer ());
5667 5572
5668 return Qnil; 5573 return Qnil;
5669} 5574}
@@ -5749,21 +5654,21 @@ return t unconditionally. */)
5749 If CURRENT_GROUP is lambda, that means send to the process group 5654 If CURRENT_GROUP is lambda, that means send to the process group
5750 that currently owns the terminal, but only if it is NOT the shell itself. 5655 that currently owns the terminal, but only if it is NOT the shell itself.
5751 5656
5752 If NOMSG is zero, insert signal-announcements into process's buffers 5657 If NOMSG is false, insert signal-announcements into process's buffers
5753 right away. 5658 right away.
5754 5659
5755 If we can, we try to signal PROCESS by sending control characters 5660 If we can, we try to signal PROCESS by sending control characters
5756 down the pty. This allows us to signal inferiors who have changed 5661 down the pty. This allows us to signal inferiors who have changed
5757 their uid, for which killpg would return an EPERM error. */ 5662 their uid, for which kill would return an EPERM error. */
5758 5663
5759static void 5664static void
5760process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group, 5665process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group,
5761 int nomsg) 5666 bool nomsg)
5762{ 5667{
5763 Lisp_Object proc; 5668 Lisp_Object proc;
5764 register struct Lisp_Process *p; 5669 struct Lisp_Process *p;
5765 pid_t gid; 5670 pid_t gid;
5766 int no_pgrp = 0; 5671 bool no_pgrp = 0;
5767 5672
5768 proc = get_process (process); 5673 proc = get_process (process);
5769 p = XPROCESS (proc); 5674 p = XPROCESS (proc);
@@ -5857,26 +5762,19 @@ process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group,
5857 return; 5762 return;
5858 } 5763 }
5859 5764
5860 switch (signo)
5861 {
5862#ifdef SIGCONT 5765#ifdef SIGCONT
5863 case SIGCONT: 5766 if (signo == SIGCONT)
5767 {
5864 p->raw_status_new = 0; 5768 p->raw_status_new = 0;
5865 p->status = Qrun; 5769 pset_status (p, Qrun);
5866 p->tick = ++process_tick; 5770 p->tick = ++process_tick;
5867 if (!nomsg) 5771 if (!nomsg)
5868 { 5772 {
5869 status_notify (NULL); 5773 status_notify (NULL);
5870 redisplay_preserve_echo_area (13); 5774 redisplay_preserve_echo_area (13);
5871 } 5775 }
5872 break;
5873#endif /* ! defined (SIGCONT) */
5874 case SIGINT:
5875 case SIGQUIT:
5876 case SIGKILL:
5877 flush_pending_output (p->infd);
5878 break;
5879 } 5776 }
5777#endif
5880 5778
5881 /* If we don't have process groups, send the signal to the immediate 5779 /* If we don't have process groups, send the signal to the immediate
5882 subprocess. That isn't really right, but it's better than any 5780 subprocess. That isn't really right, but it's better than any
@@ -5892,7 +5790,7 @@ process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group,
5892 if (!NILP (current_group)) 5790 if (!NILP (current_group))
5893 { 5791 {
5894 if (ioctl (p->infd, TIOCSIGSEND, signo) == -1) 5792 if (ioctl (p->infd, TIOCSIGSEND, signo) == -1)
5895 EMACS_KILLPG (gid, signo); 5793 kill (-gid, signo);
5896 } 5794 }
5897 else 5795 else
5898 { 5796 {
@@ -5900,7 +5798,7 @@ process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group,
5900 kill (gid, signo); 5798 kill (gid, signo);
5901 } 5799 }
5902#else /* ! defined (TIOCSIGSEND) */ 5800#else /* ! defined (TIOCSIGSEND) */
5903 EMACS_KILLPG (gid, signo); 5801 kill (-gid, signo);
5904#endif /* ! defined (TIOCSIGSEND) */ 5802#endif /* ! defined (TIOCSIGSEND) */
5905} 5803}
5906 5804
@@ -5958,7 +5856,7 @@ traffic. */)
5958 FD_CLR (p->infd, &input_wait_mask); 5856 FD_CLR (p->infd, &input_wait_mask);
5959 FD_CLR (p->infd, &non_keyboard_wait_mask); 5857 FD_CLR (p->infd, &non_keyboard_wait_mask);
5960 } 5858 }
5961 p->command = Qt; 5859 pset_command (p, Qt);
5962 return process; 5860 return process;
5963 } 5861 }
5964#ifndef SIGTSTP 5862#ifndef SIGTSTP
@@ -5994,7 +5892,7 @@ traffic. */)
5994 tcflush (p->infd, TCIFLUSH); 5892 tcflush (p->infd, TCIFLUSH);
5995#endif /* not WINDOWSNT */ 5893#endif /* not WINDOWSNT */
5996 } 5894 }
5997 p->command = Qnil; 5895 pset_command (p, Qnil);
5998 return process; 5896 return process;
5999 } 5897 }
6000#ifdef SIGCONT 5898#ifdef SIGCONT
@@ -6005,6 +5903,27 @@ traffic. */)
6005 return process; 5903 return process;
6006} 5904}
6007 5905
5906/* Return the integer value of the signal whose abbreviation is ABBR,
5907 or a negative number if there is no such signal. */
5908static int
5909abbr_to_signal (char const *name)
5910{
5911 int i, signo;
5912 char sigbuf[20]; /* Large enough for all valid signal abbreviations. */
5913
5914 if (!strncmp (name, "SIG", 3) || !strncmp (name, "sig", 3))
5915 name += 3;
5916
5917 for (i = 0; i < sizeof sigbuf; i++)
5918 {
5919 sigbuf[i] = c_toupper (name[i]);
5920 if (! sigbuf[i])
5921 return str2sig (sigbuf, &signo) == 0 ? signo : -1;
5922 }
5923
5924 return -1;
5925}
5926
6008DEFUN ("signal-process", Fsignal_process, Ssignal_process, 5927DEFUN ("signal-process", Fsignal_process, Ssignal_process,
6009 2, 2, "sProcess (name or number): \nnSignal code: ", 5928 2, 2, "sProcess (name or number): \nnSignal code: ",
6010 doc: /* Send PROCESS the signal with code SIGCODE. 5929 doc: /* Send PROCESS the signal with code SIGCODE.
@@ -6015,6 +5934,7 @@ SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6015 (Lisp_Object process, Lisp_Object sigcode) 5934 (Lisp_Object process, Lisp_Object sigcode)
6016{ 5935{
6017 pid_t pid; 5936 pid_t pid;
5937 int signo;
6018 5938
6019 if (STRINGP (process)) 5939 if (STRINGP (process))
6020 { 5940 {
@@ -6044,12 +5964,11 @@ SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6044 error ("Cannot signal process %s", SDATA (XPROCESS (process)->name)); 5964 error ("Cannot signal process %s", SDATA (XPROCESS (process)->name));
6045 } 5965 }
6046 5966
6047#define parse_signal(NAME, VALUE) \
6048 else if (!xstrcasecmp (name, NAME)) \
6049 XSETINT (sigcode, VALUE)
6050
6051 if (INTEGERP (sigcode)) 5967 if (INTEGERP (sigcode))
6052 CHECK_TYPE_RANGED_INTEGER (int, sigcode); 5968 {
5969 CHECK_TYPE_RANGED_INTEGER (int, sigcode);
5970 signo = XINT (sigcode);
5971 }
6053 else 5972 else
6054 { 5973 {
6055 char *name; 5974 char *name;
@@ -6057,108 +5976,12 @@ SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6057 CHECK_SYMBOL (sigcode); 5976 CHECK_SYMBOL (sigcode);
6058 name = SSDATA (SYMBOL_NAME (sigcode)); 5977 name = SSDATA (SYMBOL_NAME (sigcode));
6059 5978
6060 if (!strncmp (name, "SIG", 3) || !strncmp (name, "sig", 3)) 5979 signo = abbr_to_signal (name);
6061 name += 3; 5980 if (signo < 0)
6062
6063 if (0)
6064 ;
6065#ifdef SIGUSR1
6066 parse_signal ("usr1", SIGUSR1);
6067#endif
6068#ifdef SIGUSR2
6069 parse_signal ("usr2", SIGUSR2);
6070#endif
6071#ifdef SIGTERM
6072 parse_signal ("term", SIGTERM);
6073#endif
6074#ifdef SIGHUP
6075 parse_signal ("hup", SIGHUP);
6076#endif
6077#ifdef SIGINT
6078 parse_signal ("int", SIGINT);
6079#endif
6080#ifdef SIGQUIT
6081 parse_signal ("quit", SIGQUIT);
6082#endif
6083#ifdef SIGILL
6084 parse_signal ("ill", SIGILL);
6085#endif
6086#ifdef SIGABRT
6087 parse_signal ("abrt", SIGABRT);
6088#endif
6089#ifdef SIGEMT
6090 parse_signal ("emt", SIGEMT);
6091#endif
6092#ifdef SIGKILL
6093 parse_signal ("kill", SIGKILL);
6094#endif
6095#ifdef SIGFPE
6096 parse_signal ("fpe", SIGFPE);
6097#endif
6098#ifdef SIGBUS
6099 parse_signal ("bus", SIGBUS);
6100#endif
6101#ifdef SIGSEGV
6102 parse_signal ("segv", SIGSEGV);
6103#endif
6104#ifdef SIGSYS
6105 parse_signal ("sys", SIGSYS);
6106#endif
6107#ifdef SIGPIPE
6108 parse_signal ("pipe", SIGPIPE);
6109#endif
6110#ifdef SIGALRM
6111 parse_signal ("alrm", SIGALRM);
6112#endif
6113#ifdef SIGURG
6114 parse_signal ("urg", SIGURG);
6115#endif
6116#ifdef SIGSTOP
6117 parse_signal ("stop", SIGSTOP);
6118#endif
6119#ifdef SIGTSTP
6120 parse_signal ("tstp", SIGTSTP);
6121#endif
6122#ifdef SIGCONT
6123 parse_signal ("cont", SIGCONT);
6124#endif
6125#ifdef SIGCHLD
6126 parse_signal ("chld", SIGCHLD);
6127#endif
6128#ifdef SIGTTIN
6129 parse_signal ("ttin", SIGTTIN);
6130#endif
6131#ifdef SIGTTOU
6132 parse_signal ("ttou", SIGTTOU);
6133#endif
6134#ifdef SIGIO
6135 parse_signal ("io", SIGIO);
6136#endif
6137#ifdef SIGXCPU
6138 parse_signal ("xcpu", SIGXCPU);
6139#endif
6140#ifdef SIGXFSZ
6141 parse_signal ("xfsz", SIGXFSZ);
6142#endif
6143#ifdef SIGVTALRM
6144 parse_signal ("vtalrm", SIGVTALRM);
6145#endif
6146#ifdef SIGPROF
6147 parse_signal ("prof", SIGPROF);
6148#endif
6149#ifdef SIGWINCH
6150 parse_signal ("winch", SIGWINCH);
6151#endif
6152#ifdef SIGINFO
6153 parse_signal ("info", SIGINFO);
6154#endif
6155 else
6156 error ("Undefined signal name %s", name); 5981 error ("Undefined signal name %s", name);
6157 } 5982 }
6158 5983
6159#undef parse_signal 5984 return make_number (kill (pid, signo));
6160
6161 return make_number (kill (pid, XINT (sigcode)));
6162} 5985}
6163 5986
6164DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0, 5987DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0,
@@ -6200,13 +6023,14 @@ process has been transmitted to the serial port. */)
6200 { 6023 {
6201#ifndef WINDOWSNT 6024#ifndef WINDOWSNT
6202 if (tcdrain (XPROCESS (proc)->outfd) != 0) 6025 if (tcdrain (XPROCESS (proc)->outfd) != 0)
6203 error ("tcdrain() failed: %s", emacs_strerror (errno)); 6026 report_file_error ("Failed tcdrain", Qnil);
6204#endif /* not WINDOWSNT */ 6027#endif /* not WINDOWSNT */
6205 /* Do nothing on Windows because writes are blocking. */ 6028 /* Do nothing on Windows because writes are blocking. */
6206 } 6029 }
6207 else 6030 else
6208 { 6031 {
6209 int old_outfd, new_outfd; 6032 int old_outfd = XPROCESS (proc)->outfd;
6033 int new_outfd;
6210 6034
6211#ifdef HAVE_SHUTDOWN 6035#ifdef HAVE_SHUTDOWN
6212 /* If this is a network connection, or socketpair is used 6036 /* If this is a network connection, or socketpair is used
@@ -6214,36 +6038,63 @@ process has been transmitted to the serial port. */)
6214 (In some old system, shutdown to socketpair doesn't work. 6038 (In some old system, shutdown to socketpair doesn't work.
6215 Then we just can't win.) */ 6039 Then we just can't win.) */
6216 if (EQ (XPROCESS (proc)->type, Qnetwork) 6040 if (EQ (XPROCESS (proc)->type, Qnetwork)
6217 || XPROCESS (proc)->outfd == XPROCESS (proc)->infd) 6041 || XPROCESS (proc)->infd == old_outfd)
6218 shutdown (XPROCESS (proc)->outfd, 1); 6042 shutdown (old_outfd, 1);
6219 /* In case of socketpair, outfd == infd, so don't close it. */ 6043#endif
6220 if (XPROCESS (proc)->outfd != XPROCESS (proc)->infd) 6044 close_process_fd (&XPROCESS (proc)->open_fd[WRITE_TO_SUBPROCESS]);
6221 emacs_close (XPROCESS (proc)->outfd);
6222#else /* not HAVE_SHUTDOWN */
6223 emacs_close (XPROCESS (proc)->outfd);
6224#endif /* not HAVE_SHUTDOWN */
6225 new_outfd = emacs_open (NULL_DEVICE, O_WRONLY, 0); 6045 new_outfd = emacs_open (NULL_DEVICE, O_WRONLY, 0);
6226 if (new_outfd < 0) 6046 if (new_outfd < 0)
6227 abort (); 6047 report_file_error ("Opening null device", Qnil);
6228 old_outfd = XPROCESS (proc)->outfd; 6048 XPROCESS (proc)->open_fd[WRITE_TO_SUBPROCESS] = new_outfd;
6049 XPROCESS (proc)->outfd = new_outfd;
6229 6050
6230 if (!proc_encode_coding_system[new_outfd]) 6051 if (!proc_encode_coding_system[new_outfd])
6231 proc_encode_coding_system[new_outfd] 6052 proc_encode_coding_system[new_outfd]
6232 = (struct coding_system *) xmalloc (sizeof (struct coding_system)); 6053 = xmalloc (sizeof (struct coding_system));
6233 memcpy (proc_encode_coding_system[new_outfd], 6054 *proc_encode_coding_system[new_outfd]
6234 proc_encode_coding_system[old_outfd], 6055 = *proc_encode_coding_system[old_outfd];
6235 sizeof (struct coding_system));
6236 memset (proc_encode_coding_system[old_outfd], 0, 6056 memset (proc_encode_coding_system[old_outfd], 0,
6237 sizeof (struct coding_system)); 6057 sizeof (struct coding_system));
6238
6239 XPROCESS (proc)->outfd = new_outfd;
6240 } 6058 }
6241 return process; 6059 return process;
6242} 6060}
6243 6061
6244/* On receipt of a signal that a child status has changed, loop asking 6062/* The main Emacs thread records child processes in three places:
6245 about children with changed statuses until the system says there 6063
6246 are no more. 6064 - Vprocess_alist, for asynchronous subprocesses, which are child
6065 processes visible to Lisp.
6066
6067 - deleted_pid_list, for child processes invisible to Lisp,
6068 typically because of delete-process. These are recorded so that
6069 the processes can be reaped when they exit, so that the operating
6070 system's process table is not cluttered by zombies.
6071
6072 - the local variable PID in Fcall_process, call_process_cleanup and
6073 call_process_kill, for synchronous subprocesses.
6074 record_unwind_protect is used to make sure this process is not
6075 forgotten: if the user interrupts call-process and the child
6076 process refuses to exit immediately even with two C-g's,
6077 call_process_kill adds PID's contents to deleted_pid_list before
6078 returning.
6079
6080 The main Emacs thread invokes waitpid only on child processes that
6081 it creates and that have not been reaped. This avoid races on
6082 platforms such as GTK, where other threads create their own
6083 subprocesses which the main thread should not reap. For example,
6084 if the main thread attempted to reap an already-reaped child, it
6085 might inadvertently reap a GTK-created process that happened to
6086 have the same process ID. */
6087
6088/* LIB_CHILD_HANDLER is a SIGCHLD handler that Emacs calls while doing
6089 its own SIGCHLD handling. On POSIXish systems, glib needs this to
6090 keep track of its own children. GNUstep is similar. */
6091
6092static void dummy_handler (int sig) {}
6093static signal_handler_t volatile lib_child_handler;
6094
6095/* Handle a SIGCHLD signal by looking for known child processes of
6096 Emacs whose status have changed. For each one found, record its
6097 new status.
6247 6098
6248 All we do is change the status; we do not run sentinels or print 6099 All we do is change the status; we do not run sentinels or print
6249 notifications. That is saved for the next time keyboard input is 6100 notifications. That is saved for the next time keyboard input is
@@ -6266,153 +6117,87 @@ process has been transmitted to the serial port. */)
6266 ** Malloc WARNING: This should never call malloc either directly or 6117 ** Malloc WARNING: This should never call malloc either directly or
6267 indirectly; if it does, that is a bug */ 6118 indirectly; if it does, that is a bug */
6268 6119
6269#ifdef SIGCHLD
6270static void 6120static void
6271sigchld_handler (int signo) 6121handle_child_signal (int sig)
6272{ 6122{
6273 int old_errno = errno; 6123 Lisp_Object tail, proc;
6274 Lisp_Object proc;
6275 struct Lisp_Process *p;
6276 6124
6277 SIGNAL_THREAD_CHECK (signo); 6125 /* Find the process that signaled us, and record its status. */
6278 6126
6279 while (1) 6127 /* The process can have been deleted by Fdelete_process, or have
6128 been started asynchronously by Fcall_process. */
6129 for (tail = deleted_pid_list; CONSP (tail); tail = XCDR (tail))
6280 { 6130 {
6281 pid_t pid; 6131 bool all_pids_are_fixnums
6282 int w; 6132 = (MOST_NEGATIVE_FIXNUM <= TYPE_MINIMUM (pid_t)
6283 Lisp_Object tail; 6133 && TYPE_MAXIMUM (pid_t) <= MOST_POSITIVE_FIXNUM);
6284 6134 Lisp_Object head = XCAR (tail);
6285#ifdef WNOHANG 6135 Lisp_Object xpid;
6286#ifndef WUNTRACED 6136 if (! CONSP (head))
6287#define WUNTRACED 0 6137 continue;
6288#endif /* no WUNTRACED */ 6138 xpid = XCAR (head);
6289 /* Keep trying to get a status until we get a definitive result. */ 6139 if (all_pids_are_fixnums ? INTEGERP (xpid) : NUMBERP (xpid))
6290 do
6291 {
6292 errno = 0;
6293 pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
6294 }
6295 while (pid < 0 && errno == EINTR);
6296
6297 if (pid <= 0)
6298 {
6299 /* PID == 0 means no processes found, PID == -1 means a real
6300 failure. We have done all our job, so return. */
6301
6302 errno = old_errno;
6303 return;
6304 }
6305#else
6306 pid = wait (&w);
6307#endif /* no WNOHANG */
6308
6309 /* Find the process that signaled us, and record its status. */
6310
6311 /* The process can have been deleted by Fdelete_process. */
6312 for (tail = deleted_pid_list; CONSP (tail); tail = XCDR (tail))
6313 { 6140 {
6314 Lisp_Object xpid = XCAR (tail); 6141 pid_t deleted_pid;
6315 if ((INTEGERP (xpid) && pid == XINT (xpid)) 6142 if (INTEGERP (xpid))
6316 || (FLOATP (xpid) && pid == XFLOAT_DATA (xpid))) 6143 deleted_pid = XINT (xpid);
6144 else
6145 deleted_pid = XFLOAT_DATA (xpid);
6146 if (child_status_changed (deleted_pid, 0, 0))
6317 { 6147 {
6148 if (STRINGP (XCDR (head)))
6149 unlink (SSDATA (XCDR (head)));
6318 XSETCAR (tail, Qnil); 6150 XSETCAR (tail, Qnil);
6319 goto sigchld_end_of_loop;
6320 } 6151 }
6321 } 6152 }
6153 }
6322 6154
6323 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */ 6155 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6324 p = 0; 6156 FOR_EACH_PROCESS (tail, proc)
6325 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) 6157 {
6326 { 6158 struct Lisp_Process *p = XPROCESS (proc);
6327 proc = XCDR (XCAR (tail)); 6159 int status;
6328 p = XPROCESS (proc);
6329 if (EQ (p->type, Qreal) && p->pid == pid)
6330 break;
6331 p = 0;
6332 }
6333
6334 /* Look for an asynchronous process whose pid hasn't been filled
6335 in yet. */
6336 if (p == 0)
6337 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
6338 {
6339 proc = XCDR (XCAR (tail));
6340 p = XPROCESS (proc);
6341 if (p->pid == -1)
6342 break;
6343 p = 0;
6344 }
6345 6160
6346 /* Change the status of the process that was found. */ 6161 if (p->alive
6347 if (p != 0) 6162 && child_status_changed (p->pid, &status, WUNTRACED | WCONTINUED))
6348 { 6163 {
6349 int clear_desc_flag = 0; 6164 /* Change the status of the process that was found. */
6350
6351 p->tick = ++process_tick; 6165 p->tick = ++process_tick;
6352 p->raw_status = w; 6166 p->raw_status = status;
6353 p->raw_status_new = 1; 6167 p->raw_status_new = 1;
6354 6168
6355 /* If process has terminated, stop waiting for its output. */ 6169 /* If process has terminated, stop waiting for its output. */
6356 if ((WIFSIGNALED (w) || WIFEXITED (w)) 6170 if (WIFSIGNALED (status) || WIFEXITED (status))
6357 && p->infd >= 0)
6358 clear_desc_flag = 1;
6359
6360 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6361 if (clear_desc_flag)
6362 { 6171 {
6363 FD_CLR (p->infd, &input_wait_mask); 6172 bool clear_desc_flag = 0;
6364 FD_CLR (p->infd, &non_keyboard_wait_mask); 6173 p->alive = 0;
6365 } 6174 if (p->infd >= 0)
6366 6175 clear_desc_flag = 1;
6367 /* Tell wait_reading_process_output that it needs to wake up and
6368 look around. */
6369 if (input_available_clear_time)
6370 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
6371 }
6372 6176
6373 /* There was no asynchronous process found for that pid: we have 6177 /* clear_desc_flag avoids a compiler bug in Microsoft C. */
6374 a synchronous process. */ 6178 if (clear_desc_flag)
6375 else 6179 {
6376 { 6180 FD_CLR (p->infd, &input_wait_mask);
6377 synch_process_alive = 0; 6181 FD_CLR (p->infd, &non_keyboard_wait_mask);
6378 6182 }
6379 /* Report the status of the synchronous process. */ 6183 }
6380 if (WIFEXITED (w))
6381 synch_process_retcode = WRETCODE (w);
6382 else if (WIFSIGNALED (w))
6383 synch_process_termsig = WTERMSIG (w);
6384
6385 /* Tell wait_reading_process_output that it needs to wake up and
6386 look around. */
6387 if (input_available_clear_time)
6388 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
6389 } 6184 }
6390
6391 sigchld_end_of_loop:
6392 ;
6393
6394 /* On some systems, we must return right away.
6395 If any more processes want to signal us, we will
6396 get another signal.
6397 Otherwise (on systems that have WNOHANG), loop around
6398 to use up all the processes that have something to tell us. */
6399#if (defined WINDOWSNT \
6400 || (defined USG && !defined GNU_LINUX \
6401 && !(defined HPUX && defined WNOHANG)))
6402 errno = old_errno;
6403 return;
6404#endif /* USG, but not HPUX with WNOHANG */
6405 } 6185 }
6186
6187 lib_child_handler (sig);
6188#ifdef NS_IMPL_GNUSTEP
6189 /* NSTask in GNUStep sets its child handler each time it is called.
6190 So we must re-set ours. */
6191 catch_child_signal();
6192#endif
6406} 6193}
6407#endif /* SIGCHLD */
6408
6409 6194
6410static Lisp_Object 6195static void
6411exec_sentinel_unwind (Lisp_Object data) 6196deliver_child_signal (int sig)
6412{ 6197{
6413 XPROCESS (XCAR (data))->sentinel = XCDR (data); 6198 deliver_process_signal (sig, handle_child_signal);
6414 return Qnil;
6415} 6199}
6200
6416 6201
6417static Lisp_Object 6202static Lisp_Object
6418exec_sentinel_error_handler (Lisp_Object error_val) 6203exec_sentinel_error_handler (Lisp_Object error_val)
@@ -6428,9 +6213,9 @@ static void
6428exec_sentinel (Lisp_Object proc, Lisp_Object reason) 6213exec_sentinel (Lisp_Object proc, Lisp_Object reason)
6429{ 6214{
6430 Lisp_Object sentinel, odeactivate; 6215 Lisp_Object sentinel, odeactivate;
6431 register struct Lisp_Process *p = XPROCESS (proc); 6216 struct Lisp_Process *p = XPROCESS (proc);
6432 ptrdiff_t count = SPECPDL_INDEX (); 6217 ptrdiff_t count = SPECPDL_INDEX ();
6433 int outer_running_asynch_code = running_asynch_code; 6218 bool outer_running_asynch_code = running_asynch_code;
6434 int waiting = waiting_for_user_input_p; 6219 int waiting = waiting_for_user_input_p;
6435 6220
6436 if (inhibit_sentinels) 6221 if (inhibit_sentinels)
@@ -6448,16 +6233,10 @@ exec_sentinel (Lisp_Object proc, Lisp_Object reason)
6448 /* There's no good reason to let sentinels change the current 6233 /* There's no good reason to let sentinels change the current
6449 buffer, and many callers of accept-process-output, sit-for, and 6234 buffer, and many callers of accept-process-output, sit-for, and
6450 friends don't expect current-buffer to be changed from under them. */ 6235 friends don't expect current-buffer to be changed from under them. */
6451 record_unwind_protect (set_buffer_if_live, Fcurrent_buffer ()); 6236 record_unwind_current_buffer ();
6452 6237
6453 sentinel = p->sentinel; 6238 sentinel = p->sentinel;
6454 if (NILP (sentinel))
6455 return;
6456 6239
6457 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6458 assure that it gets restored no matter how the sentinel exits. */
6459 p->sentinel = Qnil;
6460 record_unwind_protect (exec_sentinel_unwind, Fcons (proc, sentinel));
6461 /* Inhibit quit so that random quits don't screw up a running filter. */ 6240 /* Inhibit quit so that random quits don't screw up a running filter. */
6462 specbind (Qinhibit_quit, Qt); 6241 specbind (Qinhibit_quit, Qt);
6463 specbind (Qlast_nonmenu_event, Qt); /* Why? --Stef */ 6242 specbind (Qlast_nonmenu_event, Qt); /* Why? --Stef */
@@ -6479,8 +6258,7 @@ exec_sentinel (Lisp_Object proc, Lisp_Object reason)
6479 running_asynch_code = 1; 6258 running_asynch_code = 1;
6480 6259
6481 internal_condition_case_1 (read_process_output_call, 6260 internal_condition_case_1 (read_process_output_call,
6482 Fcons (sentinel, 6261 list3 (sentinel, proc, reason),
6483 Fcons (proc, Fcons (reason, Qnil))),
6484 !NILP (Vdebug_on_error) ? Qnil : Qerror, 6262 !NILP (Vdebug_on_error) ? Qnil : Qerror,
6485 exec_sentinel_error_handler); 6263 exec_sentinel_error_handler);
6486 6264
@@ -6515,7 +6293,7 @@ exec_sentinel (Lisp_Object proc, Lisp_Object reason)
6515static void 6293static void
6516status_notify (struct Lisp_Process *deleting_process) 6294status_notify (struct Lisp_Process *deleting_process)
6517{ 6295{
6518 register Lisp_Object proc, buffer; 6296 register Lisp_Object proc;
6519 Lisp_Object tail, msg; 6297 Lisp_Object tail, msg;
6520 struct gcpro gcpro1, gcpro2; 6298 struct gcpro gcpro1, gcpro2;
6521 6299
@@ -6531,13 +6309,10 @@ status_notify (struct Lisp_Process *deleting_process)
6531 that we run, we get called again to handle their status changes. */ 6309 that we run, we get called again to handle their status changes. */
6532 update_tick = process_tick; 6310 update_tick = process_tick;
6533 6311
6534 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) 6312 FOR_EACH_PROCESS (tail, proc)
6535 { 6313 {
6536 Lisp_Object symbol; 6314 Lisp_Object symbol;
6537 register struct Lisp_Process *p; 6315 register struct Lisp_Process *p = XPROCESS (proc);
6538
6539 proc = Fcdr (XCAR (tail));
6540 p = XPROCESS (proc);
6541 6316
6542 if (p->tick != p->update_tick) 6317 if (p->tick != p->update_tick)
6543 { 6318 {
@@ -6553,8 +6328,6 @@ status_notify (struct Lisp_Process *deleting_process)
6553 && p != deleting_process 6328 && p != deleting_process
6554 && read_process_output (proc, p->infd) > 0); 6329 && read_process_output (proc, p->infd) > 0);
6555 6330
6556 buffer = p->buffer;
6557
6558 /* Get the text to use for the message. */ 6331 /* Get the text to use for the message. */
6559 if (p->raw_status_new) 6332 if (p->raw_status_new)
6560 update_status (p); 6333 update_status (p);
@@ -6575,65 +6348,83 @@ status_notify (struct Lisp_Process *deleting_process)
6575 } 6348 }
6576 6349
6577 /* The actions above may have further incremented p->tick. 6350 /* The actions above may have further incremented p->tick.
6578 So set p->update_tick again 6351 So set p->update_tick again so that an error in the sentinel will
6579 so that an error in the sentinel will not cause 6352 not cause this code to be run again. */
6580 this code to be run again. */
6581 p->update_tick = p->tick; 6353 p->update_tick = p->tick;
6582 /* Now output the message suitably. */ 6354 /* Now output the message suitably. */
6583 if (!NILP (p->sentinel)) 6355 exec_sentinel (proc, msg);
6584 exec_sentinel (proc, msg);
6585 /* Don't bother with a message in the buffer
6586 when a process becomes runnable. */
6587 else if (!EQ (symbol, Qrun) && !NILP (buffer))
6588 {
6589 Lisp_Object tem;
6590 struct buffer *old = current_buffer;
6591 ptrdiff_t opoint, opoint_byte;
6592 ptrdiff_t before, before_byte;
6593
6594 /* Avoid error if buffer is deleted
6595 (probably that's why the process is dead, too) */
6596 if (NILP (BVAR (XBUFFER (buffer), name)))
6597 continue;
6598 Fset_buffer (buffer);
6599
6600 opoint = PT;
6601 opoint_byte = PT_BYTE;
6602 /* Insert new output into buffer
6603 at the current end-of-output marker,
6604 thus preserving logical ordering of input and output. */
6605 if (XMARKER (p->mark)->buffer)
6606 Fgoto_char (p->mark);
6607 else
6608 SET_PT_BOTH (ZV, ZV_BYTE);
6609
6610 before = PT;
6611 before_byte = PT_BYTE;
6612
6613 tem = BVAR (current_buffer, read_only);
6614 BVAR (current_buffer, read_only) = Qnil;
6615 insert_string ("\nProcess ");
6616 Finsert (1, &p->name);
6617 insert_string (" ");
6618 Finsert (1, &msg);
6619 BVAR (current_buffer, read_only) = tem;
6620 set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
6621
6622 if (opoint >= before)
6623 SET_PT_BOTH (opoint + (PT - before),
6624 opoint_byte + (PT_BYTE - before_byte));
6625 else
6626 SET_PT_BOTH (opoint, opoint_byte);
6627
6628 set_buffer_internal (old);
6629 }
6630 } 6356 }
6631 } /* end for */ 6357 } /* end for */
6632 6358
6633 update_mode_lines++; /* in case buffers use %s in mode-line-format */ 6359 update_mode_lines++; /* In case buffers use %s in mode-line-format. */
6634 UNGCPRO; 6360 UNGCPRO;
6635} 6361}
6636 6362
6363DEFUN ("internal-default-process-sentinel", Finternal_default_process_sentinel,
6364 Sinternal_default_process_sentinel, 2, 2, 0,
6365 doc: /* Function used as default sentinel for processes. */)
6366 (Lisp_Object proc, Lisp_Object msg)
6367{
6368 Lisp_Object buffer, symbol;
6369 struct Lisp_Process *p;
6370 CHECK_PROCESS (proc);
6371 p = XPROCESS (proc);
6372 buffer = p->buffer;
6373 symbol = p->status;
6374 if (CONSP (symbol))
6375 symbol = XCAR (symbol);
6376
6377 if (!EQ (symbol, Qrun) && !NILP (buffer))
6378 {
6379 Lisp_Object tem;
6380 struct buffer *old = current_buffer;
6381 ptrdiff_t opoint, opoint_byte;
6382 ptrdiff_t before, before_byte;
6383
6384 /* Avoid error if buffer is deleted
6385 (probably that's why the process is dead, too). */
6386 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
6387 return Qnil;
6388 Fset_buffer (buffer);
6389
6390 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
6391 msg = (code_convert_string_norecord
6392 (msg, Vlocale_coding_system, 1));
6393
6394 opoint = PT;
6395 opoint_byte = PT_BYTE;
6396 /* Insert new output into buffer
6397 at the current end-of-output marker,
6398 thus preserving logical ordering of input and output. */
6399 if (XMARKER (p->mark)->buffer)
6400 Fgoto_char (p->mark);
6401 else
6402 SET_PT_BOTH (ZV, ZV_BYTE);
6403
6404 before = PT;
6405 before_byte = PT_BYTE;
6406
6407 tem = BVAR (current_buffer, read_only);
6408 bset_read_only (current_buffer, Qnil);
6409 insert_string ("\nProcess ");
6410 { /* FIXME: temporary kludge. */
6411 Lisp_Object tem2 = p->name; Finsert (1, &tem2); }
6412 insert_string (" ");
6413 Finsert (1, &msg);
6414 bset_read_only (current_buffer, tem);
6415 set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
6416
6417 if (opoint >= before)
6418 SET_PT_BOTH (opoint + (PT - before),
6419 opoint_byte + (PT_BYTE - before_byte));
6420 else
6421 SET_PT_BOTH (opoint, opoint_byte);
6422
6423 set_buffer_internal (old);
6424 }
6425 return Qnil;
6426}
6427
6637 6428
6638DEFUN ("set-process-coding-system", Fset_process_coding_system, 6429DEFUN ("set-process-coding-system", Fset_process_coding_system,
6639 Sset_process_coding_system, 1, 3, 0, 6430 Sset_process_coding_system, 1, 3, 0,
@@ -6653,8 +6444,8 @@ encode subprocess input. */)
6653 Fcheck_coding_system (decoding); 6444 Fcheck_coding_system (decoding);
6654 Fcheck_coding_system (encoding); 6445 Fcheck_coding_system (encoding);
6655 encoding = coding_inherit_eol_type (encoding, Qnil); 6446 encoding = coding_inherit_eol_type (encoding, Qnil);
6656 p->decode_coding_system = decoding; 6447 pset_decode_coding_system (p, decoding);
6657 p->encode_coding_system = encoding; 6448 pset_encode_coding_system (p, encoding);
6658 setup_process_coding_systems (process); 6449 setup_process_coding_systems (process);
6659 6450
6660 return Qnil; 6451 return Qnil;
@@ -6684,7 +6475,8 @@ suppressed. */)
6684 CHECK_PROCESS (process); 6475 CHECK_PROCESS (process);
6685 p = XPROCESS (process); 6476 p = XPROCESS (process);
6686 if (NILP (flag)) 6477 if (NILP (flag))
6687 p->decode_coding_system = raw_text_coding_system (p->decode_coding_system); 6478 pset_decode_coding_system
6479 (p, raw_text_coding_system (p->decode_coding_system));
6688 setup_process_coding_systems (process); 6480 setup_process_coding_systems (process);
6689 6481
6690 return Qnil; 6482 return Qnil;
@@ -6723,12 +6515,12 @@ delete_gpm_wait_descriptor (int desc)
6723 6515
6724# endif 6516# endif
6725 6517
6726# ifdef SIGIO 6518# ifdef USABLE_SIGIO
6727 6519
6728/* Return nonzero if *MASK has a bit set 6520/* Return true if *MASK has a bit set
6729 that corresponds to one of the keyboard input descriptors. */ 6521 that corresponds to one of the keyboard input descriptors. */
6730 6522
6731static int 6523static bool
6732keyboard_bit_set (fd_set *mask) 6524keyboard_bit_set (fd_set *mask)
6733{ 6525{
6734 int fd; 6526 int fd;
@@ -6745,20 +6537,26 @@ keyboard_bit_set (fd_set *mask)
6745#else /* not subprocesses */ 6537#else /* not subprocesses */
6746 6538
6747/* Defined on msdos.c. */ 6539/* Defined on msdos.c. */
6748extern int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *, 6540extern int sys_select (int, fd_set *, fd_set *, fd_set *,
6749 EMACS_TIME *); 6541 struct timespec *, void *);
6750 6542
6751/* Implementation of wait_reading_process_output, assuming that there 6543/* Implementation of wait_reading_process_output, assuming that there
6752 are no subprocesses. Used only by the MS-DOS build. 6544 are no subprocesses. Used only by the MS-DOS build.
6753 6545
6754 Wait for timeout to elapse and/or keyboard input to be available. 6546 Wait for timeout to elapse and/or keyboard input to be available.
6755 6547
6756 time_limit is: 6548 TIME_LIMIT is:
6757 timeout in seconds, or 6549 timeout in seconds
6758 zero for no limit, or 6550 If negative, gobble data immediately available but don't wait for any.
6759 -1 means gobble data immediately available but don't wait for any.
6760 6551
6761 read_kbd is a Lisp_Object: 6552 NSECS is:
6553 an additional duration to wait, measured in nanoseconds
6554 If TIME_LIMIT is zero, then:
6555 If NSECS == 0, there is no limit.
6556 If NSECS > 0, the timeout consists of NSECS only.
6557 If NSECS < 0, gobble data immediately, as if TIME_LIMIT were negative.
6558
6559 READ_KBD is:
6762 0 to ignore keyboard input, or 6560 0 to ignore keyboard input, or
6763 1 to return when input is available, or 6561 1 to return when input is available, or
6764 -1 means caller will actually read the input, so don't throw to 6562 -1 means caller will actually read the input, so don't throw to
@@ -6767,28 +6565,33 @@ extern int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *,
6767 see full version for other parameters. We know that wait_proc will 6565 see full version for other parameters. We know that wait_proc will
6768 always be NULL, since `subprocesses' isn't defined. 6566 always be NULL, since `subprocesses' isn't defined.
6769 6567
6770 do_display != 0 means redisplay should be done to show subprocess 6568 DO_DISPLAY means redisplay should be done to show subprocess
6771 output that arrives. 6569 output that arrives.
6772 6570
6773 Return true if we received input from any process. */ 6571 Return true if we received input from any process. */
6774 6572
6775int 6573bool
6776wait_reading_process_output (int time_limit, int microsecs, int read_kbd, 6574wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6777 int do_display, 6575 bool do_display,
6778 Lisp_Object wait_for_cell, 6576 Lisp_Object wait_for_cell,
6779 struct Lisp_Process *wait_proc, int just_wait_proc) 6577 struct Lisp_Process *wait_proc, int just_wait_proc)
6780{ 6578{
6781 register int nfds; 6579 register int nfds;
6782 EMACS_TIME end_time, timeout; 6580 struct timespec end_time, timeout;
6783 SELECT_TYPE waitchannels; 6581
6784 int xerrno; 6582 if (time_limit < 0)
6583 {
6584 time_limit = 0;
6585 nsecs = -1;
6586 }
6587 else if (TYPE_MAXIMUM (time_t) < time_limit)
6588 time_limit = TYPE_MAXIMUM (time_t);
6785 6589
6786 /* What does time_limit really mean? */ 6590 /* What does time_limit really mean? */
6787 if (time_limit || microsecs) 6591 if (time_limit || nsecs > 0)
6788 { 6592 {
6789 EMACS_GET_TIME (end_time); 6593 timeout = make_timespec (time_limit, nsecs);
6790 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs); 6594 end_time = timespec_add (current_timespec (), timeout);
6791 EMACS_ADD_TIME (end_time, end_time, timeout);
6792 } 6595 }
6793 6596
6794 /* Turn off periodic alarms (in case they are in use) 6597 /* Turn off periodic alarms (in case they are in use)
@@ -6799,7 +6602,9 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
6799 6602
6800 while (1) 6603 while (1)
6801 { 6604 {
6802 int timeout_reduced_for_timers = 0; 6605 bool timeout_reduced_for_timers = 0;
6606 fd_set waitchannels;
6607 int xerrno;
6803 6608
6804 /* If calling from keyboard input, do not quit 6609 /* If calling from keyboard input, do not quit
6805 since we want to return C-g as an input character. 6610 since we want to return C-g as an input character.
@@ -6811,26 +6616,26 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
6811 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell))) 6616 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
6812 break; 6617 break;
6813 6618
6814 /* Compute time from now till when time limit is up */ 6619 /* Compute time from now till when time limit is up. */
6815 /* Exit if already run out */ 6620 /* Exit if already run out. */
6816 if (time_limit == -1) 6621 if (nsecs < 0)
6817 { 6622 {
6818 /* -1 specified for timeout means 6623 /* A negative timeout means
6819 gobble output available now 6624 gobble output available now
6820 but don't wait at all. */ 6625 but don't wait at all. */
6821 6626
6822 EMACS_SET_SECS_USECS (timeout, 0, 0); 6627 timeout = make_timespec (0, 0);
6823 } 6628 }
6824 else if (time_limit || microsecs) 6629 else if (time_limit || nsecs > 0)
6825 { 6630 {
6826 EMACS_GET_TIME (timeout); 6631 struct timespec now = current_timespec ();
6827 EMACS_SUB_TIME (timeout, end_time, timeout); 6632 if (timespec_cmp (end_time, now) <= 0)
6828 if (EMACS_TIME_NEG_P (timeout))
6829 break; 6633 break;
6634 timeout = timespec_sub (end_time, now);
6830 } 6635 }
6831 else 6636 else
6832 { 6637 {
6833 EMACS_SET_SECS_USECS (timeout, 100000, 0); 6638 timeout = make_timespec (100000, 0);
6834 } 6639 }
6835 6640
6836 /* If our caller will not immediately handle keyboard events, 6641 /* If our caller will not immediately handle keyboard events,
@@ -6839,11 +6644,11 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
6839 call timer_delay on their own.) */ 6644 call timer_delay on their own.) */
6840 if (NILP (wait_for_cell)) 6645 if (NILP (wait_for_cell))
6841 { 6646 {
6842 EMACS_TIME timer_delay; 6647 struct timespec timer_delay;
6843 6648
6844 do 6649 do
6845 { 6650 {
6846 int old_timers_run = timers_run; 6651 unsigned old_timers_run = timers_run;
6847 timer_delay = timer_check (); 6652 timer_delay = timer_check ();
6848 if (timers_run != old_timers_run && do_display) 6653 if (timers_run != old_timers_run && do_display)
6849 /* We must retry, since a timer may have requeued itself 6654 /* We must retry, since a timer may have requeued itself
@@ -6859,11 +6664,9 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
6859 && requeued_events_pending_p ()) 6664 && requeued_events_pending_p ())
6860 break; 6665 break;
6861 6666
6862 if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1) 6667 if (timespec_valid_p (timer_delay) && nsecs >= 0)
6863 { 6668 {
6864 EMACS_TIME difference; 6669 if (timespec_cmp (timer_delay, timeout) < 0)
6865 EMACS_SUB_TIME (difference, timer_delay, timeout);
6866 if (EMACS_TIME_NEG_P (difference))
6867 { 6670 {
6868 timeout = timer_delay; 6671 timeout = timer_delay;
6869 timeout_reduced_for_timers = 1; 6672 timeout_reduced_for_timers = 1;
@@ -6876,13 +6679,6 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
6876 if (read_kbd < 0) 6679 if (read_kbd < 0)
6877 set_waiting_for_input (&timeout); 6680 set_waiting_for_input (&timeout);
6878 6681
6879 /* Wait till there is something to do. */
6880
6881 if (! read_kbd && NILP (wait_for_cell))
6882 FD_ZERO (&waitchannels);
6883 else
6884 FD_SET (0, &waitchannels);
6885
6886 /* If a frame has been newly mapped and needs updating, 6682 /* If a frame has been newly mapped and needs updating,
6887 reprocess its display stuff. */ 6683 reprocess its display stuff. */
6888 if (frame_garbaged && do_display) 6684 if (frame_garbaged && do_display)
@@ -6893,14 +6689,16 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
6893 set_waiting_for_input (&timeout); 6689 set_waiting_for_input (&timeout);
6894 } 6690 }
6895 6691
6692 /* Wait till there is something to do. */
6693 FD_ZERO (&waitchannels);
6896 if (read_kbd && detect_input_pending ()) 6694 if (read_kbd && detect_input_pending ())
6695 nfds = 0;
6696 else
6897 { 6697 {
6898 nfds = 0; 6698 if (read_kbd || !NILP (wait_for_cell))
6899 FD_ZERO (&waitchannels); 6699 FD_SET (0, &waitchannels);
6700 nfds = pselect (1, &waitchannels, NULL, NULL, &timeout, NULL);
6900 } 6701 }
6901 else
6902 nfds = select (1, &waitchannels, (SELECT_TYPE *)0, (SELECT_TYPE *)0,
6903 &timeout);
6904 6702
6905 xerrno = errno; 6703 xerrno = errno;
6906 6704
@@ -6910,7 +6708,7 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
6910 /* If we woke up due to SIGWINCH, actually change size now. */ 6708 /* If we woke up due to SIGWINCH, actually change size now. */
6911 do_pending_window_change (0); 6709 do_pending_window_change (0);
6912 6710
6913 if (time_limit && nfds == 0 && ! timeout_reduced_for_timers) 6711 if ((time_limit || nsecs) && nfds == 0 && ! timeout_reduced_for_timers)
6914 /* We waited the full specified time, so return now. */ 6712 /* We waited the full specified time, so return now. */
6915 break; 6713 break;
6916 6714
@@ -6921,7 +6719,7 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
6921 if (xerrno == EINTR) 6719 if (xerrno == EINTR)
6922 FD_ZERO (&waitchannels); 6720 FD_ZERO (&waitchannels);
6923 else 6721 else
6924 error ("select error: %s", emacs_strerror (xerrno)); 6722 report_file_errno ("Failed select", Qnil, xerrno);
6925 } 6723 }
6926 6724
6927 /* Check for keyboard input */ 6725 /* Check for keyboard input */
@@ -6987,16 +6785,9 @@ void
6987delete_keyboard_wait_descriptor (int desc) 6785delete_keyboard_wait_descriptor (int desc)
6988{ 6786{
6989#ifdef subprocesses 6787#ifdef subprocesses
6990 int fd;
6991 int lim = max_input_desc;
6992
6993 FD_CLR (desc, &input_wait_mask); 6788 FD_CLR (desc, &input_wait_mask);
6994 FD_CLR (desc, &non_process_wait_mask); 6789 FD_CLR (desc, &non_process_wait_mask);
6995 6790 delete_input_desc (desc);
6996 if (desc == max_input_desc)
6997 for (fd = 0; fd < lim; fd++)
6998 if (FD_ISSET (fd, &input_wait_mask) || FD_ISSET (fd, &write_mask))
6999 max_input_desc = fd;
7000#endif 6791#endif
7001} 6792}
7002 6793
@@ -7015,12 +6806,10 @@ setup_process_coding_systems (Lisp_Object process)
7015 return; 6806 return;
7016 6807
7017 if (!proc_decode_coding_system[inch]) 6808 if (!proc_decode_coding_system[inch])
7018 proc_decode_coding_system[inch] 6809 proc_decode_coding_system[inch] = xmalloc (sizeof (struct coding_system));
7019 = (struct coding_system *) xmalloc (sizeof (struct coding_system));
7020 coding_system = p->decode_coding_system; 6810 coding_system = p->decode_coding_system;
7021 if (! NILP (p->filter)) 6811 if (EQ (p->filter, Qinternal_default_process_filter)
7022 ; 6812 && BUFFERP (p->buffer))
7023 else if (BUFFERP (p->buffer))
7024 { 6813 {
7025 if (NILP (BVAR (XBUFFER (p->buffer), enable_multibyte_characters))) 6814 if (NILP (BVAR (XBUFFER (p->buffer), enable_multibyte_characters)))
7026 coding_system = raw_text_coding_system (coding_system); 6815 coding_system = raw_text_coding_system (coding_system);
@@ -7028,39 +6817,12 @@ setup_process_coding_systems (Lisp_Object process)
7028 setup_coding_system (coding_system, proc_decode_coding_system[inch]); 6817 setup_coding_system (coding_system, proc_decode_coding_system[inch]);
7029 6818
7030 if (!proc_encode_coding_system[outch]) 6819 if (!proc_encode_coding_system[outch])
7031 proc_encode_coding_system[outch] 6820 proc_encode_coding_system[outch] = xmalloc (sizeof (struct coding_system));
7032 = (struct coding_system *) xmalloc (sizeof (struct coding_system));
7033 setup_coding_system (p->encode_coding_system, 6821 setup_coding_system (p->encode_coding_system,
7034 proc_encode_coding_system[outch]); 6822 proc_encode_coding_system[outch]);
7035#endif 6823#endif
7036} 6824}
7037 6825
7038/* Close all descriptors currently in use for communication
7039 with subprocess. This is used in a newly-forked subprocess
7040 to get rid of irrelevant descriptors. */
7041
7042void
7043close_process_descs (void)
7044{
7045#ifndef DOS_NT
7046 int i;
7047 for (i = 0; i < MAXDESC; i++)
7048 {
7049 Lisp_Object process;
7050 process = chan_process[i];
7051 if (!NILP (process))
7052 {
7053 int in = XPROCESS (process)->infd;
7054 int out = XPROCESS (process)->outfd;
7055 if (in >= 0)
7056 emacs_close (in);
7057 if (out >= 0 && in != out)
7058 emacs_close (out);
7059 }
7060 }
7061#endif
7062}
7063
7064DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0, 6826DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
7065 doc: /* Return the (or a) process associated with BUFFER. 6827 doc: /* Return the (or a) process associated with BUFFER.
7066BUFFER may be a buffer or the name of one. */) 6828BUFFER may be a buffer or the name of one. */)
@@ -7073,12 +6835,9 @@ BUFFER may be a buffer or the name of one. */)
7073 buf = Fget_buffer (buffer); 6835 buf = Fget_buffer (buffer);
7074 if (NILP (buf)) return Qnil; 6836 if (NILP (buf)) return Qnil;
7075 6837
7076 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) 6838 FOR_EACH_PROCESS (tail, proc)
7077 { 6839 if (EQ (XPROCESS (proc)->buffer, buf))
7078 proc = Fcdr (XCAR (tail)); 6840 return proc;
7079 if (PROCESSP (proc) && EQ (XPROCESS (proc)->buffer, buf))
7080 return proc;
7081 }
7082#endif /* subprocesses */ 6841#endif /* subprocesses */
7083 return Qnil; 6842 return Qnil;
7084} 6843}
@@ -7111,18 +6870,14 @@ kill_buffer_processes (Lisp_Object buffer)
7111#ifdef subprocesses 6870#ifdef subprocesses
7112 Lisp_Object tail, proc; 6871 Lisp_Object tail, proc;
7113 6872
7114 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) 6873 FOR_EACH_PROCESS (tail, proc)
7115 { 6874 if (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer))
7116 proc = XCDR (XCAR (tail)); 6875 {
7117 if (PROCESSP (proc) 6876 if (NETCONN_P (proc) || SERIALCONN_P (proc))
7118 && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer))) 6877 Fdelete_process (proc);
7119 { 6878 else if (XPROCESS (proc)->infd >= 0)
7120 if (NETCONN_P (proc) || SERIALCONN_P (proc)) 6879 process_send_signal (proc, SIGHUP, Qnil, 1);
7121 Fdelete_process (proc); 6880 }
7122 else if (XPROCESS (proc)->infd >= 0)
7123 process_send_signal (proc, SIGHUP, Qnil, 1);
7124 }
7125 }
7126#else /* subprocesses */ 6881#else /* subprocesses */
7127 /* Since we have no subprocesses, this does nothing. */ 6882 /* Since we have no subprocesses, this does nothing. */
7128#endif /* subprocesses */ 6883#endif /* subprocesses */
@@ -7130,7 +6885,7 @@ kill_buffer_processes (Lisp_Object buffer)
7130 6885
7131DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, 6886DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p,
7132 Swaiting_for_user_input_p, 0, 0, 0, 6887 Swaiting_for_user_input_p, 0, 0, 0,
7133 doc: /* Returns non-nil if Emacs is waiting for input from the user. 6888 doc: /* Return non-nil if Emacs is waiting for input from the user.
7134This is intended for use by asynchronous process output filters and sentinels. */) 6889This is intended for use by asynchronous process output filters and sentinels. */)
7135 (void) 6890 (void)
7136{ 6891{
@@ -7157,9 +6912,9 @@ unhold_keyboard_input (void)
7157 kbd_is_on_hold = 0; 6912 kbd_is_on_hold = 0;
7158} 6913}
7159 6914
7160/* Return non-zero if keyboard input is on hold, zero otherwise. */ 6915/* Return true if keyboard input is on hold, zero otherwise. */
7161 6916
7162int 6917bool
7163kbd_on_hold_p (void) 6918kbd_on_hold_p (void)
7164{ 6919{
7165 return kbd_is_on_hold; 6920 return kbd_is_on_hold;
@@ -7213,19 +6968,20 @@ integer or floating point values.
7213 majflt -- number of major page faults (number) 6968 majflt -- number of major page faults (number)
7214 cminflt -- cumulative number of minor page faults (number) 6969 cminflt -- cumulative number of minor page faults (number)
7215 cmajflt -- cumulative number of major page faults (number) 6970 cmajflt -- cumulative number of major page faults (number)
7216 utime -- user time used by the process, in the (HIGH LOW USEC) format 6971 utime -- user time used by the process, in (current-time) format,
7217 stime -- system time used by the process, in the (HIGH LOW USEC) format 6972 which is a list of integers (HIGH LOW USEC PSEC)
7218 time -- sum of utime and stime, in the (HIGH LOW USEC) format 6973 stime -- system time used by the process (current-time)
7219 cutime -- user time used by the process and its children, (HIGH LOW USEC) 6974 time -- sum of utime and stime (current-time)
7220 cstime -- system time used by the process and its children, (HIGH LOW USEC) 6975 cutime -- user time used by the process and its children (current-time)
7221 ctime -- sum of cutime and cstime, in the (HIGH LOW USEC) format 6976 cstime -- system time used by the process and its children (current-time)
6977 ctime -- sum of cutime and cstime (current-time)
7222 pri -- priority of the process (number) 6978 pri -- priority of the process (number)
7223 nice -- nice value of the process (number) 6979 nice -- nice value of the process (number)
7224 thcount -- process thread count (number) 6980 thcount -- process thread count (number)
7225 start -- time the process started, in the (HIGH LOW USEC) format 6981 start -- time the process started (current-time)
7226 vsize -- virtual memory size of the process in KB's (number) 6982 vsize -- virtual memory size of the process in KB's (number)
7227 rss -- resident set size of the process in KB's (number) 6983 rss -- resident set size of the process in KB's (number)
7228 etime -- elapsed time the process is running, in (HIGH LOW USEC) format 6984 etime -- elapsed time the process is running, in (HIGH LOW USEC PSEC) format
7229 pcpu -- percents of CPU time used by the process (floating-point number) 6985 pcpu -- percents of CPU time used by the process (floating-point number)
7230 pmem -- percents of total physical memory used by process's resident set 6986 pmem -- percents of total physical memory used by process's resident set
7231 (floating-point number) 6987 (floating-point number)
@@ -7235,27 +6991,61 @@ integer or floating point values.
7235 return system_process_attributes (pid); 6991 return system_process_attributes (pid);
7236} 6992}
7237 6993
6994/* Arrange to catch SIGCHLD if this hasn't already been arranged.
6995 Invoke this after init_process_emacs, and after glib and/or GNUstep
6996 futz with the SIGCHLD handler, but before Emacs forks any children.
6997 This function's caller should block SIGCHLD. */
6998
6999#ifndef NS_IMPL_GNUSTEP
7000static
7001#endif
7002void
7003catch_child_signal (void)
7004{
7005 struct sigaction action, old_action;
7006 emacs_sigaction_init (&action, deliver_child_signal);
7007 block_child_signal ();
7008 sigaction (SIGCHLD, &action, &old_action);
7009 eassert (! (old_action.sa_flags & SA_SIGINFO));
7010
7011 if (old_action.sa_handler != deliver_child_signal)
7012 lib_child_handler
7013 = (old_action.sa_handler == SIG_DFL || old_action.sa_handler == SIG_IGN
7014 ? dummy_handler
7015 : old_action.sa_handler);
7016 unblock_child_signal ();
7017}
7018
7238 7019
7020/* This is not called "init_process" because that is the name of a
7021 Mach system call, so it would cause problems on Darwin systems. */
7239void 7022void
7240init_process (void) 7023init_process_emacs (void)
7241{ 7024{
7242#ifdef subprocesses 7025#ifdef subprocesses
7243 register int i; 7026 register int i;
7244 7027
7245 inhibit_sentinels = 0; 7028 inhibit_sentinels = 0;
7246 7029
7247#ifdef SIGCHLD
7248#ifndef CANNOT_DUMP 7030#ifndef CANNOT_DUMP
7249 if (! noninteractive || initialized) 7031 if (! noninteractive || initialized)
7250#endif 7032#endif
7251 signal (SIGCHLD, sigchld_handler); 7033 {
7252#endif 7034#if defined HAVE_GLIB && !defined WINDOWSNT
7035 /* Tickle glib's child-handling code. Ask glib to wait for Emacs itself;
7036 this should always fail, but is enough to initialize glib's
7037 private SIGCHLD handler, allowing catch_child_signal to copy
7038 it into lib_child_handler. */
7039 g_source_unref (g_child_watch_source_new (getpid ()));
7040#endif
7041 catch_child_signal ();
7042 }
7253 7043
7254 FD_ZERO (&input_wait_mask); 7044 FD_ZERO (&input_wait_mask);
7255 FD_ZERO (&non_keyboard_wait_mask); 7045 FD_ZERO (&non_keyboard_wait_mask);
7256 FD_ZERO (&non_process_wait_mask); 7046 FD_ZERO (&non_process_wait_mask);
7257 FD_ZERO (&write_mask); 7047 FD_ZERO (&write_mask);
7258 max_process_desc = 0; 7048 max_process_desc = max_input_desc = -1;
7259 memset (fd_callback_info, 0, sizeof (fd_callback_info)); 7049 memset (fd_callback_info, 0, sizeof (fd_callback_info));
7260 7050
7261#ifdef NON_BLOCKING_CONNECT 7051#ifdef NON_BLOCKING_CONNECT
@@ -7276,10 +7066,8 @@ init_process (void)
7276#endif 7066#endif
7277 7067
7278 Vprocess_alist = Qnil; 7068 Vprocess_alist = Qnil;
7279#ifdef SIGCHLD
7280 deleted_pid_list = Qnil; 7069 deleted_pid_list = Qnil;
7281#endif 7070 for (i = 0; i < FD_SETSIZE; i++)
7282 for (i = 0; i < MAXDESC; i++)
7283 { 7071 {
7284 chan_process[i] = Qnil; 7072 chan_process[i] = Qnil;
7285 proc_buffered_char[i] = -1; 7073 proc_buffered_char[i] = -1;
@@ -7316,9 +7104,7 @@ init_process (void)
7316#ifdef HAVE_GETSOCKNAME 7104#ifdef HAVE_GETSOCKNAME
7317 ADD_SUBFEATURE (QCservice, Qt); 7105 ADD_SUBFEATURE (QCservice, Qt);
7318#endif 7106#endif
7319#if defined (O_NONBLOCK) || defined (O_NDELAY)
7320 ADD_SUBFEATURE (QCserver, Qt); 7107 ADD_SUBFEATURE (QCserver, Qt);
7321#endif
7322 7108
7323 for (sopt = socket_options; sopt->name; sopt++) 7109 for (sopt = socket_options; sopt->name; sopt++)
7324 subfeatures = pure_cons (intern_c_string (sopt->name), subfeatures); 7110 subfeatures = pure_cons (intern_c_string (sopt->name), subfeatures);
@@ -7334,8 +7120,7 @@ init_process (void)
7334 char const *release = (STRINGP (Voperating_system_release) 7120 char const *release = (STRINGP (Voperating_system_release)
7335 ? SSDATA (Voperating_system_release) 7121 ? SSDATA (Voperating_system_release)
7336 : 0); 7122 : 0);
7337 if (!release || !release[0] || (release[0] < MIN_PTY_KERNEL_VERSION 7123 if (!release || !release[0] || (release[0] < '7' && release[1] == '.')) {
7338 && release[1] == '.')) {
7339 Vprocess_connection_type = Qnil; 7124 Vprocess_connection_type = Qnil;
7340 } 7125 }
7341 } 7126 }
@@ -7408,9 +7193,7 @@ syms_of_process (void)
7408 DEFSYM (Qlast_nonmenu_event, "last-nonmenu-event"); 7193 DEFSYM (Qlast_nonmenu_event, "last-nonmenu-event");
7409 7194
7410 staticpro (&Vprocess_alist); 7195 staticpro (&Vprocess_alist);
7411#ifdef SIGCHLD
7412 staticpro (&deleted_pid_list); 7196 staticpro (&deleted_pid_list);
7413#endif
7414 7197
7415#endif /* subprocesses */ 7198#endif /* subprocesses */
7416 7199
@@ -7438,6 +7221,10 @@ syms_of_process (void)
7438 DEFSYM (Qcutime, "cutime"); 7221 DEFSYM (Qcutime, "cutime");
7439 DEFSYM (Qcstime, "cstime"); 7222 DEFSYM (Qcstime, "cstime");
7440 DEFSYM (Qctime, "ctime"); 7223 DEFSYM (Qctime, "ctime");
7224 DEFSYM (Qinternal_default_process_sentinel,
7225 "internal-default-process-sentinel");
7226 DEFSYM (Qinternal_default_process_filter,
7227 "internal-default-process-filter");
7441 DEFSYM (Qpri, "pri"); 7228 DEFSYM (Qpri, "pri");
7442 DEFSYM (Qnice, "nice"); 7229 DEFSYM (Qnice, "nice");
7443 DEFSYM (Qthcount, "thcount"); 7230 DEFSYM (Qthcount, "thcount");
@@ -7533,6 +7320,8 @@ The variable takes effect when `start-process' is called. */);
7533 defsubr (&Ssignal_process); 7320 defsubr (&Ssignal_process);
7534 defsubr (&Swaiting_for_user_input_p); 7321 defsubr (&Swaiting_for_user_input_p);
7535 defsubr (&Sprocess_type); 7322 defsubr (&Sprocess_type);
7323 defsubr (&Sinternal_default_process_sentinel);
7324 defsubr (&Sinternal_default_process_filter);
7536 defsubr (&Sset_process_coding_system); 7325 defsubr (&Sset_process_coding_system);
7537 defsubr (&Sprocess_coding_system); 7326 defsubr (&Sprocess_coding_system);
7538 defsubr (&Sset_process_filter_multibyte); 7327 defsubr (&Sset_process_filter_multibyte);