aboutsummaryrefslogtreecommitdiffstats
path: root/src/process.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/process.c')
-rw-r--r--src/process.c1658
1 files changed, 856 insertions, 802 deletions
diff --git a/src/process.c b/src/process.c
index 5b15ade1122..187627dd85a 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-2013 Free Software 3Copyright (C) 1985-1988, 1993-1996, 1998-1999, 2001-2014
4Foundation, Inc. 4 Free Software Foundation, Inc.
5 5
6This file is part of GNU Emacs. 6This file is part of GNU Emacs.
7 7
@@ -21,8 +21,6 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 21
22#include <config.h> 22#include <config.h>
23 23
24#define PROCESS_INLINE EXTERN_INLINE
25
26#include <stdio.h> 24#include <stdio.h>
27#include <errno.h> 25#include <errno.h>
28#include <sys/types.h> /* Some typedefs are used in sys/file.h. */ 26#include <sys/types.h> /* Some typedefs are used in sys/file.h. */
@@ -78,7 +76,6 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
78#endif 76#endif
79 77
80#ifdef HAVE_RES_INIT 78#ifdef HAVE_RES_INIT
81#include <netinet/in.h>
82#include <arpa/nameser.h> 79#include <arpa/nameser.h>
83#include <resolv.h> 80#include <resolv.h>
84#endif 81#endif
@@ -93,6 +90,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
93 90
94#include <c-ctype.h> 91#include <c-ctype.h>
95#include <sig2str.h> 92#include <sig2str.h>
93#include <verify.h>
96 94
97#endif /* subprocesses */ 95#endif /* subprocesses */
98 96
@@ -124,19 +122,53 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
124#include TERM_HEADER 122#include TERM_HEADER
125#endif /* HAVE_WINDOW_SYSTEM */ 123#endif /* HAVE_WINDOW_SYSTEM */
126 124
127#if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS) 125#ifdef HAVE_GLIB
128#include "xgselect.h" 126#include "xgselect.h"
127#ifndef WINDOWSNT
128#include <glib.h>
129#endif
129#endif 130#endif
130 131
131#ifdef WINDOWSNT 132#ifdef WINDOWSNT
132extern int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *, 133extern int sys_select (int, fd_set *, fd_set *, fd_set *,
133 EMACS_TIME *, void *); 134 struct timespec *, void *);
135#endif
136
137#ifndef SOCK_CLOEXEC
138# define SOCK_CLOEXEC 0
139#endif
140
141#ifndef HAVE_ACCEPT4
142
143/* Emulate GNU/Linux accept4 and socket well enough for this module. */
144
145static int
146close_on_exec (int fd)
147{
148 if (0 <= fd)
149 fcntl (fd, F_SETFD, FD_CLOEXEC);
150 return fd;
151}
152
153static int
154accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags)
155{
156 return close_on_exec (accept (sockfd, addr, addrlen));
157}
158
159static int
160process_socket (int domain, int type, int protocol)
161{
162 return close_on_exec (socket (domain, type, protocol));
163}
164# undef socket
165# define socket(domain, type, protocol) process_socket (domain, type, protocol)
134#endif 166#endif
135 167
136/* Work around GCC 4.7.0 bug with strict overflow checking; see 168/* Work around GCC 4.7.0 bug with strict overflow checking; see
137 <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52904>. 169 <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52904>.
138 These lines can be removed once the GCC bug is fixed. */ 170 These lines can be removed once the GCC bug is fixed. */
139#if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__ 171#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
140# pragma GCC diagnostic ignored "-Wstrict-overflow" 172# pragma GCC diagnostic ignored "-Wstrict-overflow"
141#endif 173#endif
142 174
@@ -146,13 +178,13 @@ Lisp_Object Qcutime, Qpri, Qnice, Qthcount, Qstart, Qvsize, Qrss, Qargs;
146Lisp_Object Quser, Qgroup, Qetime, Qpcpu, Qpmem, Qtime, Qctime; 178Lisp_Object Quser, Qgroup, Qetime, Qpcpu, Qpmem, Qtime, Qctime;
147Lisp_Object QCname, QCtype; 179Lisp_Object QCname, QCtype;
148 180
149/* Non-zero if keyboard input is on hold, zero otherwise. */ 181/* True if keyboard input is on hold, zero otherwise. */
150 182
151static int kbd_is_on_hold; 183static bool kbd_is_on_hold;
152 184
153/* Nonzero means don't run process sentinels. This is used 185/* Nonzero means don't run process sentinels. This is used
154 when exiting. */ 186 when exiting. */
155int inhibit_sentinels; 187bool inhibit_sentinels;
156 188
157#ifdef subprocesses 189#ifdef subprocesses
158 190
@@ -174,6 +206,8 @@ static Lisp_Object QClocal, QCremote, QCcoding;
174static Lisp_Object QCserver, QCnowait, QCnoquery, QCstop; 206static Lisp_Object QCserver, QCnowait, QCnoquery, QCstop;
175static Lisp_Object QCsentinel, QClog, QCoptions, QCplist; 207static Lisp_Object QCsentinel, QClog, QCoptions, QCplist;
176static Lisp_Object Qlast_nonmenu_event; 208static Lisp_Object Qlast_nonmenu_event;
209static Lisp_Object Qinternal_default_process_sentinel;
210static Lisp_Object Qinternal_default_process_filter;
177 211
178#define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork)) 212#define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
179#define NETCONN1_P(p) (EQ (p->type, Qnetwork)) 213#define NETCONN1_P(p) (EQ (p->type, Qnetwork))
@@ -225,7 +259,7 @@ static EMACS_INT update_tick;
225#endif 259#endif
226 260
227#ifdef ADAPTIVE_READ_BUFFERING 261#ifdef ADAPTIVE_READ_BUFFERING
228#define READ_OUTPUT_DELAY_INCREMENT (EMACS_TIME_RESOLUTION / 100) 262#define READ_OUTPUT_DELAY_INCREMENT (TIMESPEC_RESOLUTION / 100)
229#define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5) 263#define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
230#define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7) 264#define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
231 265
@@ -234,9 +268,9 @@ static EMACS_INT update_tick;
234 268
235static int process_output_delay_count; 269static int process_output_delay_count;
236 270
237/* Non-zero if any process has non-nil read_output_skip. */ 271/* True if any process has non-nil read_output_skip. */
238 272
239static int process_output_skip; 273static bool process_output_skip;
240 274
241#else 275#else
242#define process_output_delay_count 0 276#define process_output_delay_count 0
@@ -244,7 +278,7 @@ static int process_output_skip;
244 278
245static void create_process (Lisp_Object, char **, Lisp_Object); 279static void create_process (Lisp_Object, char **, Lisp_Object);
246#ifdef USABLE_SIGIO 280#ifdef USABLE_SIGIO
247static int keyboard_bit_set (SELECT_TYPE *); 281static bool keyboard_bit_set (fd_set *);
248#endif 282#endif
249static void deactivate_process (Lisp_Object); 283static void deactivate_process (Lisp_Object);
250static void status_notify (struct Lisp_Process *); 284static void status_notify (struct Lisp_Process *);
@@ -263,39 +297,39 @@ static void exec_sentinel (Lisp_Object proc, Lisp_Object reason);
263 297
264/* Mask of bits indicating the descriptors that we wait for input on. */ 298/* Mask of bits indicating the descriptors that we wait for input on. */
265 299
266static SELECT_TYPE input_wait_mask; 300static fd_set input_wait_mask;
267 301
268/* Mask that excludes keyboard input descriptor(s). */ 302/* Mask that excludes keyboard input descriptor(s). */
269 303
270static SELECT_TYPE non_keyboard_wait_mask; 304static fd_set non_keyboard_wait_mask;
271 305
272/* Mask that excludes process input descriptor(s). */ 306/* Mask that excludes process input descriptor(s). */
273 307
274static SELECT_TYPE non_process_wait_mask; 308static fd_set non_process_wait_mask;
275 309
276/* Mask for selecting for write. */ 310/* Mask for selecting for write. */
277 311
278static SELECT_TYPE write_mask; 312static fd_set write_mask;
279 313
280#ifdef NON_BLOCKING_CONNECT 314#ifdef NON_BLOCKING_CONNECT
281/* Mask of bits indicating the descriptors that we wait for connect to 315/* Mask of bits indicating the descriptors that we wait for connect to
282 complete on. Once they complete, they are removed from this mask 316 complete on. Once they complete, they are removed from this mask
283 and added to the input_wait_mask and non_keyboard_wait_mask. */ 317 and added to the input_wait_mask and non_keyboard_wait_mask. */
284 318
285static SELECT_TYPE connect_wait_mask; 319static fd_set connect_wait_mask;
286 320
287/* Number of bits set in connect_wait_mask. */ 321/* Number of bits set in connect_wait_mask. */
288static int num_pending_connects; 322static int num_pending_connects;
289#endif /* NON_BLOCKING_CONNECT */ 323#endif /* NON_BLOCKING_CONNECT */
290 324
291/* The largest descriptor currently in use for a process object. */ 325/* The largest descriptor currently in use for a process object; -1 if none. */
292static int max_process_desc; 326static int max_process_desc;
293 327
294/* The largest descriptor currently in use for input. */ 328/* The largest descriptor currently in use for input; -1 if none. */
295static int max_input_desc; 329static int max_input_desc;
296 330
297/* Indexed by descriptor, gives the process (if any) for that descriptor */ 331/* Indexed by descriptor, gives the process (if any) for that descriptor */
298static Lisp_Object chan_process[MAXDESC]; 332static Lisp_Object chan_process[FD_SETSIZE];
299 333
300/* Alist of elements (NAME . PROCESS) */ 334/* Alist of elements (NAME . PROCESS) */
301static Lisp_Object Vprocess_alist; 335static Lisp_Object Vprocess_alist;
@@ -306,18 +340,18 @@ static Lisp_Object Vprocess_alist;
306 output from the process is to read at least one char. 340 output from the process is to read at least one char.
307 Always -1 on systems that support FIONREAD. */ 341 Always -1 on systems that support FIONREAD. */
308 342
309static int proc_buffered_char[MAXDESC]; 343static int proc_buffered_char[FD_SETSIZE];
310 344
311/* Table of `struct coding-system' for each process. */ 345/* Table of `struct coding-system' for each process. */
312static struct coding_system *proc_decode_coding_system[MAXDESC]; 346static struct coding_system *proc_decode_coding_system[FD_SETSIZE];
313static struct coding_system *proc_encode_coding_system[MAXDESC]; 347static struct coding_system *proc_encode_coding_system[FD_SETSIZE];
314 348
315#ifdef DATAGRAM_SOCKETS 349#ifdef DATAGRAM_SOCKETS
316/* Table of `partner address' for datagram sockets. */ 350/* Table of `partner address' for datagram sockets. */
317static struct sockaddr_and_len { 351static struct sockaddr_and_len {
318 struct sockaddr *sa; 352 struct sockaddr *sa;
319 int len; 353 int len;
320} datagram_address[MAXDESC]; 354} datagram_address[FD_SETSIZE];
321#define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0) 355#define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
322#define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0) 356#define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
323#else 357#else
@@ -325,6 +359,12 @@ static struct sockaddr_and_len {
325#define DATAGRAM_CONN_P(proc) (0) 359#define DATAGRAM_CONN_P(proc) (0)
326#endif 360#endif
327 361
362/* FOR_EACH_PROCESS (LIST_VAR, PROC_VAR) followed by a statement is
363 a `for' loop which iterates over processes from Vprocess_alist. */
364
365#define FOR_EACH_PROCESS(list_var, proc_var) \
366 FOR_EACH_ALIST_VALUE (Vprocess_alist, list_var, proc_var)
367
328/* These setters are used only in this file, so they can be private. */ 368/* These setters are used only in this file, so they can be private. */
329static void 369static void
330pset_buffer (struct Lisp_Process *p, Lisp_Object val) 370pset_buffer (struct Lisp_Process *p, Lisp_Object val)
@@ -359,7 +399,7 @@ pset_encoding_buf (struct Lisp_Process *p, Lisp_Object val)
359static void 399static void
360pset_filter (struct Lisp_Process *p, Lisp_Object val) 400pset_filter (struct Lisp_Process *p, Lisp_Object val)
361{ 401{
362 p->filter = val; 402 p->filter = NILP (val) ? Qinternal_default_process_filter : val;
363} 403}
364static void 404static void
365pset_log (struct Lisp_Process *p, Lisp_Object val) 405pset_log (struct Lisp_Process *p, Lisp_Object val)
@@ -384,7 +424,7 @@ pset_plist (struct Lisp_Process *p, Lisp_Object val)
384static void 424static void
385pset_sentinel (struct Lisp_Process *p, Lisp_Object val) 425pset_sentinel (struct Lisp_Process *p, Lisp_Object val)
386{ 426{
387 p->sentinel = val; 427 p->sentinel = NILP (val) ? Qinternal_default_process_sentinel : val;
388} 428}
389static void 429static void
390pset_status (struct Lisp_Process *p, Lisp_Object val) 430pset_status (struct Lisp_Process *p, Lisp_Object val)
@@ -416,7 +456,7 @@ static struct fd_callback_data
416#define FOR_READ 1 456#define FOR_READ 1
417#define FOR_WRITE 2 457#define FOR_WRITE 2
418 int condition; /* mask of the defines above. */ 458 int condition; /* mask of the defines above. */
419} fd_callback_info[MAXDESC]; 459} fd_callback_info[FD_SETSIZE];
420 460
421 461
422/* Add a file descriptor FD to be monitored for when read is possible. 462/* Add a file descriptor FD to be monitored for when read is possible.
@@ -425,7 +465,7 @@ static struct fd_callback_data
425void 465void
426add_read_fd (int fd, fd_callback func, void *data) 466add_read_fd (int fd, fd_callback func, void *data)
427{ 467{
428 eassert (fd < MAXDESC); 468 eassert (fd < FD_SETSIZE);
429 add_keyboard_wait_descriptor (fd); 469 add_keyboard_wait_descriptor (fd);
430 470
431 fd_callback_info[fd].func = func; 471 fd_callback_info[fd].func = func;
@@ -438,7 +478,7 @@ add_read_fd (int fd, fd_callback func, void *data)
438void 478void
439delete_read_fd (int fd) 479delete_read_fd (int fd)
440{ 480{
441 eassert (fd < MAXDESC); 481 eassert (fd < FD_SETSIZE);
442 delete_keyboard_wait_descriptor (fd); 482 delete_keyboard_wait_descriptor (fd);
443 483
444 fd_callback_info[fd].condition &= ~FOR_READ; 484 fd_callback_info[fd].condition &= ~FOR_READ;
@@ -455,7 +495,7 @@ delete_read_fd (int fd)
455void 495void
456add_write_fd (int fd, fd_callback func, void *data) 496add_write_fd (int fd, fd_callback func, void *data)
457{ 497{
458 eassert (fd < MAXDESC); 498 eassert (fd < FD_SETSIZE);
459 FD_SET (fd, &write_mask); 499 FD_SET (fd, &write_mask);
460 if (fd > max_input_desc) 500 if (fd > max_input_desc)
461 max_input_desc = fd; 501 max_input_desc = fd;
@@ -465,29 +505,35 @@ add_write_fd (int fd, fd_callback func, void *data)
465 fd_callback_info[fd].condition |= FOR_WRITE; 505 fd_callback_info[fd].condition |= FOR_WRITE;
466} 506}
467 507
508/* FD is no longer an input descriptor; update max_input_desc accordingly. */
509
510static void
511delete_input_desc (int fd)
512{
513 if (fd == max_input_desc)
514 {
515 do
516 fd--;
517 while (0 <= fd && ! (FD_ISSET (fd, &input_wait_mask)
518 || FD_ISSET (fd, &write_mask)));
519
520 max_input_desc = fd;
521 }
522}
523
468/* Stop monitoring file descriptor FD for when write is possible. */ 524/* Stop monitoring file descriptor FD for when write is possible. */
469 525
470void 526void
471delete_write_fd (int fd) 527delete_write_fd (int fd)
472{ 528{
473 int lim = max_input_desc; 529 eassert (fd < FD_SETSIZE);
474
475 eassert (fd < MAXDESC);
476 FD_CLR (fd, &write_mask); 530 FD_CLR (fd, &write_mask);
477 fd_callback_info[fd].condition &= ~FOR_WRITE; 531 fd_callback_info[fd].condition &= ~FOR_WRITE;
478 if (fd_callback_info[fd].condition == 0) 532 if (fd_callback_info[fd].condition == 0)
479 { 533 {
480 fd_callback_info[fd].func = 0; 534 fd_callback_info[fd].func = 0;
481 fd_callback_info[fd].data = 0; 535 fd_callback_info[fd].data = 0;
482 536 delete_input_desc (fd);
483 if (fd == max_input_desc)
484 for (fd = lim; fd >= 0; fd--)
485 if (FD_ISSET (fd, &input_wait_mask) || FD_ISSET (fd, &write_mask))
486 {
487 max_input_desc = fd;
488 break;
489 }
490
491 } 537 }
492} 538}
493 539
@@ -527,7 +573,7 @@ status_convert (int w)
527 and store them individually through the three pointers. */ 573 and store them individually through the three pointers. */
528 574
529static void 575static void
530decode_status (Lisp_Object l, Lisp_Object *symbol, int *code, int *coredump) 576decode_status (Lisp_Object l, Lisp_Object *symbol, int *code, bool *coredump)
531{ 577{
532 Lisp_Object tem; 578 Lisp_Object tem;
533 579
@@ -554,7 +600,8 @@ status_message (struct Lisp_Process *p)
554{ 600{
555 Lisp_Object status = p->status; 601 Lisp_Object status = p->status;
556 Lisp_Object symbol; 602 Lisp_Object symbol;
557 int code, coredump; 603 int code;
604 bool coredump;
558 Lisp_Object string, string2; 605 Lisp_Object string, string2;
559 606
560 decode_status (status, &symbol, &code, &coredump); 607 decode_status (status, &symbol, &code, &coredump);
@@ -604,19 +651,16 @@ status_message (struct Lisp_Process *p)
604 return Fcopy_sequence (Fsymbol_name (symbol)); 651 return Fcopy_sequence (Fsymbol_name (symbol));
605} 652}
606 653
607#ifdef HAVE_PTYS 654enum { PTY_NAME_SIZE = 24 };
608
609/* The file name of the pty opened by allocate_pty. */
610static char pty_name[24];
611 655
612/* Open an available pty, returning a file descriptor. 656/* Open an available pty, returning a file descriptor.
613 Return -1 on failure. 657 Store into PTY_NAME the file name of the terminal corresponding to the pty.
614 The file name of the terminal corresponding to the pty 658 Return -1 on failure. */
615 is left in the variable pty_name. */
616 659
617static int 660static int
618allocate_pty (void) 661allocate_pty (char pty_name[PTY_NAME_SIZE])
619{ 662{
663#ifdef HAVE_PTYS
620 int fd; 664 int fd;
621 665
622#ifdef PTY_ITERATION 666#ifdef PTY_ITERATION
@@ -641,6 +685,15 @@ allocate_pty (void)
641 685
642 if (fd >= 0) 686 if (fd >= 0)
643 { 687 {
688#ifdef PTY_OPEN
689 /* Set FD's close-on-exec flag. This is needed even if
690 PT_OPEN calls posix_openpt with O_CLOEXEC, since POSIX
691 doesn't require support for that combination.
692 Multithreaded platforms where posix_openpt ignores
693 O_CLOEXEC (or where PTY_OPEN doesn't call posix_openpt)
694 have a race condition between the PTY_OPEN and here. */
695 fcntl (fd, F_SETFD, FD_CLOEXEC);
696#endif
644 /* check to make certain that both sides are available 697 /* check to make certain that both sides are available
645 this avoids a nasty yet stupid bug in rlogins */ 698 this avoids a nasty yet stupid bug in rlogins */
646#ifdef PTY_TTY_NAME_SPRINTF 699#ifdef PTY_TTY_NAME_SPRINTF
@@ -661,9 +714,9 @@ allocate_pty (void)
661 return fd; 714 return fd;
662 } 715 }
663 } 716 }
717#endif /* HAVE_PTYS */
664 return -1; 718 return -1;
665} 719}
666#endif /* HAVE_PTYS */
667 720
668static Lisp_Object 721static Lisp_Object
669make_process (Lisp_Object name) 722make_process (Lisp_Object name)
@@ -683,6 +736,8 @@ make_process (Lisp_Object name)
683 non-Lisp data, so do it only for slots which should not be zero. */ 736 non-Lisp data, so do it only for slots which should not be zero. */
684 p->infd = -1; 737 p->infd = -1;
685 p->outfd = -1; 738 p->outfd = -1;
739 for (i = 0; i < PROCESS_OPEN_FDS; i++)
740 p->open_fd[i] = -1;
686 741
687#ifdef HAVE_GNUTLS 742#ifdef HAVE_GNUTLS
688 p->gnutls_initstage = GNUTLS_STAGE_EMPTY; 743 p->gnutls_initstage = GNUTLS_STAGE_EMPTY;
@@ -699,6 +754,8 @@ make_process (Lisp_Object name)
699 } 754 }
700 name = name1; 755 name = name1;
701 pset_name (p, name); 756 pset_name (p, name);
757 pset_sentinel (p, Qinternal_default_process_sentinel);
758 pset_filter (p, Qinternal_default_process_filter);
702 XSETPROCESS (val, p); 759 XSETPROCESS (val, p);
703 Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist); 760 Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
704 return val; 761 return val;
@@ -755,13 +812,14 @@ get_process (register Lisp_Object name)
755 else 812 else
756 obj = name; 813 obj = name;
757 814
758 /* Now obj should be either a buffer object or a process object. 815 /* Now obj should be either a buffer object or a process object. */
759 */
760 if (BUFFERP (obj)) 816 if (BUFFERP (obj))
761 { 817 {
818 if (NILP (BVAR (XBUFFER (obj), name)))
819 error ("Attempt to get process for a dead buffer");
762 proc = Fget_buffer_process (obj); 820 proc = Fget_buffer_process (obj);
763 if (NILP (proc)) 821 if (NILP (proc))
764 error ("Buffer %s has no process", SDATA (BVAR (XBUFFER (obj), name))); 822 error ("Buffer %s has no process", SDATA (BVAR (XBUFFER (obj), name)));
765 } 823 }
766 else 824 else
767 { 825 {
@@ -777,13 +835,17 @@ get_process (register Lisp_Object name)
777 treated by the SIGCHLD handler and waitpid has been invoked on them; 835 treated by the SIGCHLD handler and waitpid has been invoked on them;
778 otherwise they might fill up the kernel's process table. 836 otherwise they might fill up the kernel's process table.
779 837
780 Some processes created by call-process are also put onto this list. */ 838 Some processes created by call-process are also put onto this list.
839
840 Members of this list are (process-ID . filename) pairs. The
841 process-ID is a number; the filename, if a string, is a file that
842 needs to be removed after the process exits. */
781static Lisp_Object deleted_pid_list; 843static Lisp_Object deleted_pid_list;
782 844
783void 845void
784record_deleted_pid (pid_t pid) 846record_deleted_pid (pid_t pid, Lisp_Object filename)
785{ 847{
786 deleted_pid_list = Fcons (make_fixnum_or_float (pid), 848 deleted_pid_list = Fcons (Fcons (make_fixnum_or_float (pid), filename),
787 /* GC treated elements set to nil. */ 849 /* GC treated elements set to nil. */
788 Fdelq (Qnil, deleted_pid_list)); 850 Fdelq (Qnil, deleted_pid_list));
789 851
@@ -803,7 +865,7 @@ nil, indicating the current buffer's process. */)
803 p->raw_status_new = 0; 865 p->raw_status_new = 0;
804 if (NETCONN1_P (p) || SERIALCONN1_P (p)) 866 if (NETCONN1_P (p) || SERIALCONN1_P (p))
805 { 867 {
806 pset_status (p, Fcons (Qexit, Fcons (make_number (0), Qnil))); 868 pset_status (p, list2 (Qexit, make_number (0)));
807 p->tick = ++process_tick; 869 p->tick = ++process_tick;
808 status_notify (p); 870 status_notify (p);
809 redisplay_preserve_echo_area (13); 871 redisplay_preserve_echo_area (13);
@@ -811,7 +873,7 @@ nil, indicating the current buffer's process. */)
811 else 873 else
812 { 874 {
813 if (p->alive) 875 if (p->alive)
814 record_kill_process (p); 876 record_kill_process (p, Qnil);
815 877
816 if (p->infd >= 0) 878 if (p->infd >= 0)
817 { 879 {
@@ -960,7 +1022,7 @@ Return BUFFER. */)
960DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer, 1022DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer,
961 1, 1, 0, 1023 1, 1, 0,
962 doc: /* Return the buffer PROCESS is associated with. 1024 doc: /* Return the buffer PROCESS is associated with.
963Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */) 1025The default process filter inserts output from PROCESS into this buffer. */)
964 (register Lisp_Object process) 1026 (register Lisp_Object process)
965{ 1027{
966 CHECK_PROCESS (process); 1028 CHECK_PROCESS (process);
@@ -978,16 +1040,16 @@ DEFUN ("process-mark", Fprocess_mark, Sprocess_mark,
978 1040
979DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter, 1041DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter,
980 2, 2, 0, 1042 2, 2, 0,
981 doc: /* Give PROCESS the filter function FILTER; nil means no filter. 1043 doc: /* Give PROCESS the filter function FILTER; nil means default.
982A value of t means stop accepting output from the process. 1044A value of t means stop accepting output from the process.
983 1045
984When a process has a filter, its buffer is not used for output. 1046When a process has a non-default filter, its buffer is not used for output.
985Instead, each time it does output, the entire string of output is 1047Instead, each time it does output, the entire string of output is
986passed to the filter. 1048passed to the filter.
987 1049
988The filter gets two arguments: the process and the string of output. 1050The filter gets two arguments: the process and the string of output.
989The string argument is normally a multibyte string, except: 1051The string argument is normally a multibyte string, except:
990- if the process' input coding system is no-conversion or raw-text, 1052- if the process's input coding system is no-conversion or raw-text,
991 it is a unibyte string (the non-converted input), or else 1053 it is a unibyte string (the non-converted input), or else
992- if `default-enable-multibyte-characters' is nil, it is a unibyte 1054- if `default-enable-multibyte-characters' is nil, it is a unibyte
993 string (the result of converting the decoded input multibyte 1055 string (the result of converting the decoded input multibyte
@@ -999,7 +1061,7 @@ The string argument is normally a multibyte string, except:
999 CHECK_PROCESS (process); 1061 CHECK_PROCESS (process);
1000 p = XPROCESS (process); 1062 p = XPROCESS (process);
1001 1063
1002 /* Don't signal an error if the process' input file descriptor 1064 /* Don't signal an error if the process's input file descriptor
1003 is closed. This could make debugging Lisp more difficult, 1065 is closed. This could make debugging Lisp more difficult,
1004 for example when doing something like 1066 for example when doing something like
1005 1067
@@ -1007,6 +1069,9 @@ The string argument is normally a multibyte string, except:
1007 (debug) 1069 (debug)
1008 (set-process-filter process ...) */ 1070 (set-process-filter process ...) */
1009 1071
1072 if (NILP (filter))
1073 filter = Qinternal_default_process_filter;
1074
1010 if (p->infd >= 0) 1075 if (p->infd >= 0)
1011 { 1076 {
1012 if (EQ (filter, Qt) && !EQ (p->status, Qlisten)) 1077 if (EQ (filter, Qt) && !EQ (p->status, Qlisten))
@@ -1032,7 +1097,7 @@ The string argument is normally a multibyte string, except:
1032 1097
1033DEFUN ("process-filter", Fprocess_filter, Sprocess_filter, 1098DEFUN ("process-filter", Fprocess_filter, Sprocess_filter,
1034 1, 1, 0, 1099 1, 1, 0,
1035 doc: /* Returns the filter function of PROCESS; nil if none. 1100 doc: /* Return the filter function of PROCESS.
1036See `set-process-filter' for more info on filter functions. */) 1101See `set-process-filter' for more info on filter functions. */)
1037 (register Lisp_Object process) 1102 (register Lisp_Object process)
1038{ 1103{
@@ -1042,7 +1107,7 @@ See `set-process-filter' for more info on filter functions. */)
1042 1107
1043DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel, 1108DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel,
1044 2, 2, 0, 1109 2, 2, 0,
1045 doc: /* Give PROCESS the sentinel SENTINEL; nil for none. 1110 doc: /* Give PROCESS the sentinel SENTINEL; nil for default.
1046The sentinel is called as a function when the process changes state. 1111The sentinel is called as a function when the process changes state.
1047It gets two arguments: the process, and a string describing the change. */) 1112It gets two arguments: the process, and a string describing the change. */)
1048 (register Lisp_Object process, Lisp_Object sentinel) 1113 (register Lisp_Object process, Lisp_Object sentinel)
@@ -1052,6 +1117,9 @@ It gets two arguments: the process, and a string describing the change. */)
1052 CHECK_PROCESS (process); 1117 CHECK_PROCESS (process);
1053 p = XPROCESS (process); 1118 p = XPROCESS (process);
1054 1119
1120 if (NILP (sentinel))
1121 sentinel = Qinternal_default_process_sentinel;
1122
1055 pset_sentinel (p, sentinel); 1123 pset_sentinel (p, sentinel);
1056 if (NETCONN1_P (p) || SERIALCONN1_P (p)) 1124 if (NETCONN1_P (p) || SERIALCONN1_P (p))
1057 pset_childp (p, Fplist_put (p->childp, QCsentinel, sentinel)); 1125 pset_childp (p, Fplist_put (p->childp, QCsentinel, sentinel));
@@ -1060,7 +1128,7 @@ It gets two arguments: the process, and a string describing the change. */)
1060 1128
1061DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel, 1129DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
1062 1, 1, 0, 1130 1, 1, 0,
1063 doc: /* Return the sentinel of PROCESS; nil if none. 1131 doc: /* Return the sentinel of PROCESS.
1064See `set-process-sentinel' for more info on sentinels. */) 1132See `set-process-sentinel' for more info on sentinels. */)
1065 (register Lisp_Object process) 1133 (register Lisp_Object process)
1066{ 1134{
@@ -1071,15 +1139,18 @@ See `set-process-sentinel' for more info on sentinels. */)
1071DEFUN ("set-process-window-size", Fset_process_window_size, 1139DEFUN ("set-process-window-size", Fset_process_window_size,
1072 Sset_process_window_size, 3, 3, 0, 1140 Sset_process_window_size, 3, 3, 0,
1073 doc: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */) 1141 doc: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1074 (register Lisp_Object process, Lisp_Object height, Lisp_Object width) 1142 (Lisp_Object process, Lisp_Object height, Lisp_Object width)
1075{ 1143{
1076 CHECK_PROCESS (process); 1144 CHECK_PROCESS (process);
1077 CHECK_RANGED_INTEGER (height, 0, INT_MAX); 1145
1078 CHECK_RANGED_INTEGER (width, 0, INT_MAX); 1146 /* All known platforms store window sizes as 'unsigned short'. */
1147 CHECK_RANGED_INTEGER (height, 0, USHRT_MAX);
1148 CHECK_RANGED_INTEGER (width, 0, USHRT_MAX);
1079 1149
1080 if (XPROCESS (process)->infd < 0 1150 if (XPROCESS (process)->infd < 0
1081 || set_window_size (XPROCESS (process)->infd, 1151 || (set_window_size (XPROCESS (process)->infd,
1082 XINT (height), XINT (width)) <= 0) 1152 XINT (height), XINT (width))
1153 < 0))
1083 return Qnil; 1154 return Qnil;
1084 else 1155 else
1085 return Qt; 1156 return Qt;
@@ -1162,11 +1233,11 @@ list of keywords. */)
1162 if ((!NETCONN_P (process) && !SERIALCONN_P (process)) || EQ (key, Qt)) 1233 if ((!NETCONN_P (process) && !SERIALCONN_P (process)) || EQ (key, Qt))
1163 return contact; 1234 return contact;
1164 if (NILP (key) && NETCONN_P (process)) 1235 if (NILP (key) && NETCONN_P (process))
1165 return Fcons (Fplist_get (contact, QChost), 1236 return list2 (Fplist_get (contact, QChost),
1166 Fcons (Fplist_get (contact, QCservice), Qnil)); 1237 Fplist_get (contact, QCservice));
1167 if (NILP (key) && SERIALCONN_P (process)) 1238 if (NILP (key) && SERIALCONN_P (process))
1168 return Fcons (Fplist_get (contact, QCport), 1239 return list2 (Fplist_get (contact, QCport),
1169 Fcons (Fplist_get (contact, QCspeed), Qnil)); 1240 Fplist_get (contact, QCspeed));
1170 return Fplist_get (contact, key); 1241 return Fplist_get (contact, key);
1171} 1242}
1172 1243
@@ -1289,7 +1360,7 @@ Returns nil if format of ADDRESS is invalid. */)
1289} 1360}
1290 1361
1291DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0, 1362DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
1292 doc: /* Return a list of all processes. */) 1363 doc: /* Return a list of all processes that are Emacs sub-processes. */)
1293 (void) 1364 (void)
1294{ 1365{
1295 return Fmapcar (Qcdr, Vprocess_alist); 1366 return Fmapcar (Qcdr, Vprocess_alist);
@@ -1297,7 +1368,7 @@ DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
1297 1368
1298/* Starting asynchronous inferior processes. */ 1369/* Starting asynchronous inferior processes. */
1299 1370
1300static Lisp_Object start_process_unwind (Lisp_Object proc); 1371static void start_process_unwind (Lisp_Object proc);
1301 1372
1302DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0, 1373DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0,
1303 doc: /* Start a program in a subprocess. Return the process object for it. 1374 doc: /* Start a program in a subprocess. Return the process object for it.
@@ -1339,22 +1410,9 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1339 function. The argument list is protected by the caller, so all 1410 function. The argument list is protected by the caller, so all
1340 we really have to worry about is buffer. */ 1411 we really have to worry about is buffer. */
1341 { 1412 {
1342 struct gcpro gcpro1, gcpro2; 1413 struct gcpro gcpro1;
1343 1414 GCPRO1 (buffer);
1344 current_dir = BVAR (current_buffer, directory); 1415 current_dir = encode_current_directory ();
1345
1346 GCPRO2 (buffer, current_dir);
1347
1348 current_dir = Funhandled_file_name_directory (current_dir);
1349 if (NILP (current_dir))
1350 /* If the file name handler says that current_dir is unreachable, use
1351 a sensible default. */
1352 current_dir = build_string ("~/");
1353 current_dir = expand_and_dir_to_file (current_dir, Qnil);
1354 if (NILP (Ffile_accessible_directory_p (current_dir)))
1355 report_file_error ("Setting current directory",
1356 Fcons (BVAR (current_buffer, directory), Qnil));
1357
1358 UNGCPRO; 1416 UNGCPRO;
1359 } 1417 }
1360 1418
@@ -1377,8 +1435,8 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1377 pset_plist (XPROCESS (proc), Qnil); 1435 pset_plist (XPROCESS (proc), Qnil);
1378 pset_type (XPROCESS (proc), Qreal); 1436 pset_type (XPROCESS (proc), Qreal);
1379 pset_buffer (XPROCESS (proc), buffer); 1437 pset_buffer (XPROCESS (proc), buffer);
1380 pset_sentinel (XPROCESS (proc), Qnil); 1438 pset_sentinel (XPROCESS (proc), Qinternal_default_process_sentinel);
1381 pset_filter (XPROCESS (proc), Qnil); 1439 pset_filter (XPROCESS (proc), Qinternal_default_process_filter);
1382 pset_command (XPROCESS (proc), Flist (nargs - 2, args + 2)); 1440 pset_command (XPROCESS (proc), Flist (nargs - 2, args + 2));
1383 1441
1384#ifdef HAVE_GNUTLS 1442#ifdef HAVE_GNUTLS
@@ -1472,10 +1530,11 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1472 1530
1473 tem = Qnil; 1531 tem = Qnil;
1474 GCPRO4 (name, program, buffer, current_dir); 1532 GCPRO4 (name, program, buffer, current_dir);
1475 openp (Vexec_path, program, Vexec_suffixes, &tem, make_number (X_OK)); 1533 openp (Vexec_path, program, Vexec_suffixes, &tem,
1534 make_number (X_OK), false);
1476 UNGCPRO; 1535 UNGCPRO;
1477 if (NILP (tem)) 1536 if (NILP (tem))
1478 report_file_error ("Searching for program", Fcons (program, Qnil)); 1537 report_file_error ("Searching for program", program);
1479 tem = Fexpand_file_name (tem, Qnil); 1538 tem = Fexpand_file_name (tem, Qnil);
1480 } 1539 }
1481 else 1540 else
@@ -1498,7 +1557,7 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1498 1557
1499 /* Encode the file name and put it in NEW_ARGV. 1558 /* Encode the file name and put it in NEW_ARGV.
1500 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. */
1501 tem = Fcons (ENCODE_FILE (tem), Qnil); 1560 tem = list1 (ENCODE_FILE (tem));
1502 1561
1503 /* Here we encode arguments by the coding system used for sending 1562 /* Here we encode arguments by the coding system used for sending
1504 data to the process. We don't support using different coding 1563 data to the process. We don't support using different coding
@@ -1546,7 +1605,7 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1546 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
1547 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
1548 remove it from the process list. */ 1607 remove it from the process list. */
1549static Lisp_Object 1608static void
1550start_process_unwind (Lisp_Object proc) 1609start_process_unwind (Lisp_Object proc)
1551{ 1610{
1552 if (!PROCESSP (proc)) 1611 if (!PROCESSP (proc))
@@ -1556,41 +1615,60 @@ start_process_unwind (Lisp_Object proc)
1556 -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. */
1557 if (XPROCESS (proc)->pid <= 0 && XPROCESS (proc)->pid != -2) 1616 if (XPROCESS (proc)->pid <= 0 && XPROCESS (proc)->pid != -2)
1558 remove_process (proc); 1617 remove_process (proc);
1559
1560 return Qnil;
1561} 1618}
1562 1619
1620/* If *FD_ADDR is nonnegative, close it, and mark it as closed. */
1621
1563static void 1622static void
1564create_process_1 (struct atimer *timer) 1623close_process_fd (int *fd_addr)
1565{ 1624{
1566 /* Nothing to do. */ 1625 int fd = *fd_addr;
1626 if (0 <= fd)
1627 {
1628 *fd_addr = -1;
1629 emacs_close (fd);
1630 }
1567} 1631}
1568 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);
1569 1651
1570static void 1652static void
1571create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir) 1653create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1572{ 1654{
1655 struct Lisp_Process *p = XPROCESS (process);
1573 int inchannel, outchannel; 1656 int inchannel, outchannel;
1574 pid_t pid; 1657 pid_t pid;
1575 int sv[2]; 1658 int vfork_errno;
1576#ifndef WINDOWSNT 1659 int forkin, forkout;
1577 int wait_child_setup[2]; 1660 bool pty_flag = 0;
1578#endif 1661 char pty_name[PTY_NAME_SIZE];
1579 sigset_t blocked; 1662 Lisp_Object lisp_pty_name = Qnil;
1580 /* Use volatile to protect variables from being clobbered by vfork. */
1581 volatile int forkin, forkout;
1582 volatile int pty_flag = 0;
1583 volatile Lisp_Object lisp_pty_name = Qnil;
1584 volatile Lisp_Object encoded_current_dir;
1585 1663
1586 inchannel = outchannel = -1; 1664 inchannel = outchannel = -1;
1587 1665
1588#ifdef HAVE_PTYS
1589 if (!NILP (Vprocess_connection_type)) 1666 if (!NILP (Vprocess_connection_type))
1590 outchannel = inchannel = allocate_pty (); 1667 outchannel = inchannel = allocate_pty (pty_name);
1591 1668
1592 if (inchannel >= 0) 1669 if (inchannel >= 0)
1593 { 1670 {
1671 p->open_fd[READ_FROM_SUBPROCESS] = inchannel;
1594#if ! defined (USG) || defined (USG_SUBTTY_WORKS) 1672#if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1595 /* 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,
1596 then close it and reopen it in the child. */ 1674 then close it and reopen it in the child. */
@@ -1599,6 +1677,7 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1599 forkout = forkin = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0); 1677 forkout = forkin = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0);
1600 if (forkin < 0) 1678 if (forkin < 0)
1601 report_file_error ("Opening pty", Qnil); 1679 report_file_error ("Opening pty", Qnil);
1680 p->open_fd[SUBPROCESS_STDIN] = forkin;
1602#else 1681#else
1603 forkin = forkout = -1; 1682 forkin = forkout = -1;
1604#endif /* not USG, or USG_SUBTTY_WORKS */ 1683#endif /* not USG, or USG_SUBTTY_WORKS */
@@ -1606,60 +1685,36 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1606 lisp_pty_name = build_string (pty_name); 1685 lisp_pty_name = build_string (pty_name);
1607 } 1686 }
1608 else 1687 else
1609#endif /* HAVE_PTYS */
1610 { 1688 {
1611 int tem; 1689 if (emacs_pipe (p->open_fd + SUBPROCESS_STDIN) != 0
1612 tem = pipe (sv); 1690 || emacs_pipe (p->open_fd + READ_FROM_SUBPROCESS) != 0)
1613 if (tem < 0)
1614 report_file_error ("Creating pipe", Qnil); 1691 report_file_error ("Creating pipe", Qnil);
1615 inchannel = sv[0]; 1692 forkin = p->open_fd[SUBPROCESS_STDIN];
1616 forkout = sv[1]; 1693 outchannel = p->open_fd[WRITE_TO_SUBPROCESS];
1617 tem = pipe (sv); 1694 inchannel = p->open_fd[READ_FROM_SUBPROCESS];
1618 if (tem < 0) 1695 forkout = p->open_fd[SUBPROCESS_STDOUT];
1619 {
1620 emacs_close (inchannel);
1621 emacs_close (forkout);
1622 report_file_error ("Creating pipe", Qnil);
1623 }
1624 outchannel = sv[1];
1625 forkin = sv[0];
1626 } 1696 }
1627 1697
1628#ifndef WINDOWSNT 1698#ifndef WINDOWSNT
1629 { 1699 if (emacs_pipe (p->open_fd + READ_FROM_EXEC_MONITOR) != 0)
1630 int tem; 1700 report_file_error ("Creating pipe", Qnil);
1631
1632 tem = pipe (wait_child_setup);
1633 if (tem < 0)
1634 report_file_error ("Creating pipe", Qnil);
1635 tem = fcntl (wait_child_setup[1], F_GETFD, 0);
1636 if (tem >= 0)
1637 tem = fcntl (wait_child_setup[1], F_SETFD, tem | FD_CLOEXEC);
1638 if (tem < 0)
1639 {
1640 emacs_close (wait_child_setup[0]);
1641 emacs_close (wait_child_setup[1]);
1642 report_file_error ("Setting file descriptor flags", Qnil);
1643 }
1644 }
1645#endif 1701#endif
1646 1702
1647 fcntl (inchannel, F_SETFL, O_NONBLOCK); 1703 fcntl (inchannel, F_SETFL, O_NONBLOCK);
1648 fcntl (outchannel, F_SETFL, O_NONBLOCK); 1704 fcntl (outchannel, F_SETFL, O_NONBLOCK);
1649 1705
1650 /* Record this as an active process, with its channels. 1706 /* Record this as an active process, with its channels. */
1651 As a result, child_setup will close Emacs's side of the pipes. */
1652 chan_process[inchannel] = process; 1707 chan_process[inchannel] = process;
1653 XPROCESS (process)->infd = inchannel; 1708 p->infd = inchannel;
1654 XPROCESS (process)->outfd = outchannel; 1709 p->outfd = outchannel;
1655 1710
1656 /* Previously we recorded the tty descriptor used in the subprocess. 1711 /* Previously we recorded the tty descriptor used in the subprocess.
1657 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
1658 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
1659 more portable (see USG_SUBTTY_WORKS above). */ 1714 more portable (see USG_SUBTTY_WORKS above). */
1660 1715
1661 XPROCESS (process)->pty_flag = pty_flag; 1716 p->pty_flag = pty_flag;
1662 pset_status (XPROCESS (process), Qrun); 1717 pset_status (p, Qrun);
1663 1718
1664 FD_SET (inchannel, &input_wait_mask); 1719 FD_SET (inchannel, &input_wait_mask);
1665 FD_SET (inchannel, &non_keyboard_wait_mask); 1720 FD_SET (inchannel, &non_keyboard_wait_mask);
@@ -1669,18 +1724,31 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1669 /* This may signal an error. */ 1724 /* This may signal an error. */
1670 setup_process_coding_systems (process); 1725 setup_process_coding_systems (process);
1671 1726
1672 encoded_current_dir = ENCODE_FILE (current_dir);
1673
1674 block_input (); 1727 block_input ();
1675 1728 block_child_signal ();
1676 /* Block SIGCHLD until we have a chance to store the new fork's
1677 pid in its process structure. */
1678 sigemptyset (&blocked);
1679 sigaddset (&blocked, SIGCHLD);
1680 pthread_sigmask (SIG_BLOCK, &blocked, 0);
1681 1729
1682#ifndef WINDOWSNT 1730#ifndef WINDOWSNT
1683 pid = vfork (); 1731 /* vfork, and prevent local vars from being clobbered by the vfork. */
1732 {
1733 Lisp_Object volatile current_dir_volatile = current_dir;
1734 Lisp_Object volatile lisp_pty_name_volatile = lisp_pty_name;
1735 char **volatile new_argv_volatile = new_argv;
1736 int volatile forkin_volatile = forkin;
1737 int volatile forkout_volatile = forkout;
1738 struct Lisp_Process *p_volatile = p;
1739
1740 pid = vfork ();
1741
1742 current_dir = current_dir_volatile;
1743 lisp_pty_name = lisp_pty_name_volatile;
1744 new_argv = new_argv_volatile;
1745 forkin = forkin_volatile;
1746 forkout = forkout_volatile;
1747 p = p_volatile;
1748
1749 pty_flag = p->pty_flag;
1750 }
1751
1684 if (pid == 0) 1752 if (pid == 0)
1685#endif /* not WINDOWSNT */ 1753#endif /* not WINDOWSNT */
1686 { 1754 {
@@ -1709,7 +1777,7 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1709 tcgetattr (xforkin, &t); 1777 tcgetattr (xforkin, &t);
1710 t.c_lflag = LDISC1; 1778 t.c_lflag = LDISC1;
1711 if (tcsetattr (xforkin, TCSANOW, &t) < 0) 1779 if (tcsetattr (xforkin, TCSANOW, &t) < 0)
1712 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39); 1780 emacs_perror ("create_process/tcsetattr LDISC1");
1713 } 1781 }
1714#else 1782#else
1715#if defined (NTTYDISC) && defined (TIOCSETD) 1783#if defined (NTTYDISC) && defined (TIOCSETD)
@@ -1748,18 +1816,16 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1748 if (pty_flag) 1816 if (pty_flag)
1749 { 1817 {
1750 1818
1751 /* I wonder if emacs_close (emacs_open (pty_name, ...)) 1819 /* I wonder if emacs_close (emacs_open (SSDATA (lisp_pty_name), ...))
1752 would work? */ 1820 would work? */
1753 if (xforkin >= 0) 1821 if (xforkin >= 0)
1754 emacs_close (xforkin); 1822 emacs_close (xforkin);
1755 xforkout = xforkin = emacs_open (pty_name, O_RDWR, 0); 1823 xforkout = xforkin = emacs_open (SSDATA (lisp_pty_name), O_RDWR, 0);
1756 1824
1757 if (xforkin < 0) 1825 if (xforkin < 0)
1758 { 1826 {
1759 emacs_write (1, "Couldn't open the pty terminal ", 31); 1827 emacs_perror (SSDATA (lisp_pty_name));
1760 emacs_write (1, pty_name, strlen (pty_name)); 1828 _exit (EXIT_CANCELED);
1761 emacs_write (1, "\n", 1);
1762 _exit (1);
1763 } 1829 }
1764 1830
1765 } 1831 }
@@ -1771,116 +1837,83 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1771 SETUP_SLAVE_PTY; 1837 SETUP_SLAVE_PTY;
1772 } 1838 }
1773#endif /* SETUP_SLAVE_PTY */ 1839#endif /* SETUP_SLAVE_PTY */
1774#ifdef AIX
1775 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
1776 Now reenable it in the child, so it will die when we want it to. */
1777 if (pty_flag)
1778 signal (SIGHUP, SIG_DFL);
1779#endif
1780#endif /* HAVE_PTYS */ 1840#endif /* HAVE_PTYS */
1781 1841
1782 signal (SIGINT, SIG_DFL); 1842 signal (SIGINT, SIG_DFL);
1783 signal (SIGQUIT, SIG_DFL); 1843 signal (SIGQUIT, SIG_DFL);
1844#ifdef SIGPROF
1845 signal (SIGPROF, SIG_DFL);
1846#endif
1784 1847
1785 /* Emacs ignores SIGPIPE, but the child should not. */ 1848 /* Emacs ignores SIGPIPE, but the child should not. */
1786 signal (SIGPIPE, SIG_DFL); 1849 signal (SIGPIPE, SIG_DFL);
1787 1850
1788 /* Stop blocking signals in the child. */ 1851 /* Stop blocking SIGCHLD in the child. */
1789 pthread_sigmask (SIG_SETMASK, &empty_mask, 0); 1852 unblock_child_signal ();
1790 1853
1791 if (pty_flag) 1854 if (pty_flag)
1792 child_setup_tty (xforkout); 1855 child_setup_tty (xforkout);
1793#ifdef WINDOWSNT 1856#ifdef WINDOWSNT
1794 pid = child_setup (xforkin, xforkout, xforkout, 1857 pid = child_setup (xforkin, xforkout, xforkout, new_argv, 1, current_dir);
1795 new_argv, 1, encoded_current_dir);
1796#else /* not WINDOWSNT */ 1858#else /* not WINDOWSNT */
1797 emacs_close (wait_child_setup[0]); 1859 child_setup (xforkin, xforkout, xforkout, new_argv, 1, current_dir);
1798 child_setup (xforkin, xforkout, xforkout,
1799 new_argv, 1, encoded_current_dir);
1800#endif /* not WINDOWSNT */ 1860#endif /* not WINDOWSNT */
1801 } 1861 }
1802 1862
1803 /* Back in the parent process. */ 1863 /* Back in the parent process. */
1804 1864
1805 XPROCESS (process)->pid = pid; 1865 vfork_errno = errno;
1806 if (0 <= pid) 1866 p->pid = pid;
1807 XPROCESS (process)->alive = 1; 1867 if (pid >= 0)
1868 p->alive = 1;
1808 1869
1809 /* Stop blocking signals in the parent. */ 1870 /* Stop blocking in the parent. */
1810 pthread_sigmask (SIG_SETMASK, &empty_mask, 0); 1871 unblock_child_signal ();
1811 unblock_input (); 1872 unblock_input ();
1812 1873
1813 if (pid < 0) 1874 if (pid < 0)
1814 { 1875 report_file_errno ("Doing vfork", Qnil, vfork_errno);
1815 if (forkin >= 0)
1816 emacs_close (forkin);
1817 if (forkin != forkout && forkout >= 0)
1818 emacs_close (forkout);
1819 }
1820 else 1876 else
1821 { 1877 {
1822 /* vfork succeeded. */ 1878 /* vfork succeeded. */
1823 1879
1880 /* Close the pipe ends that the child uses, or the child's pty. */
1881 close_process_fd (&p->open_fd[SUBPROCESS_STDIN]);
1882 close_process_fd (&p->open_fd[SUBPROCESS_STDOUT]);
1883
1824#ifdef WINDOWSNT 1884#ifdef WINDOWSNT
1825 register_child (pid, inchannel); 1885 register_child (pid, inchannel);
1826#endif /* WINDOWSNT */ 1886#endif /* WINDOWSNT */
1827 1887
1828 /* If the subfork execv fails, and it exits, 1888 pset_tty_name (p, lisp_pty_name);
1829 this close hangs. I don't know why.
1830 So have an interrupt jar it loose. */
1831 {
1832 struct atimer *timer;
1833 EMACS_TIME offset = make_emacs_time (1, 0);
1834
1835 stop_polling ();
1836 timer = start_atimer (ATIMER_RELATIVE, offset, create_process_1, 0);
1837
1838 if (forkin >= 0)
1839 emacs_close (forkin);
1840
1841 cancel_atimer (timer);
1842 start_polling ();
1843 }
1844
1845 if (forkin != forkout && forkout >= 0)
1846 emacs_close (forkout);
1847
1848 pset_tty_name (XPROCESS (process), lisp_pty_name);
1849 1889
1850#ifndef WINDOWSNT 1890#ifndef WINDOWSNT
1851 /* Wait for child_setup to complete in case that vfork is 1891 /* Wait for child_setup to complete in case that vfork is
1852 actually defined as fork. The descriptor wait_child_setup[1] 1892 actually defined as fork. The descriptor
1893 XPROCESS (proc)->open_fd[EXEC_MONITOR_OUTPUT]
1853 of a pipe is closed at the child side either by close-on-exec 1894 of a pipe is closed at the child side either by close-on-exec
1854 on successful execve or the _exit call in child_setup. */ 1895 on successful execve or the _exit call in child_setup. */
1855 { 1896 {
1856 char dummy; 1897 char dummy;
1857 1898
1858 emacs_close (wait_child_setup[1]); 1899 close_process_fd (&p->open_fd[EXEC_MONITOR_OUTPUT]);
1859 emacs_read (wait_child_setup[0], &dummy, 1); 1900 emacs_read (p->open_fd[READ_FROM_EXEC_MONITOR], &dummy, 1);
1860 emacs_close (wait_child_setup[0]); 1901 close_process_fd (&p->open_fd[READ_FROM_EXEC_MONITOR]);
1861 } 1902 }
1862#endif 1903#endif
1863 } 1904 }
1864
1865 /* Now generate the error if vfork failed. */
1866 if (pid < 0)
1867 report_file_error ("Doing vfork", Qnil);
1868} 1905}
1869 1906
1870void 1907static void
1871create_pty (Lisp_Object process) 1908create_pty (Lisp_Object process)
1872{ 1909{
1873 int inchannel, outchannel; 1910 struct Lisp_Process *p = XPROCESS (process);
1874 int pty_flag = 0; 1911 char pty_name[PTY_NAME_SIZE];
1875 1912 int pty_fd = NILP (Vprocess_connection_type) ? -1 : allocate_pty (pty_name);
1876 inchannel = outchannel = -1;
1877
1878#ifdef HAVE_PTYS
1879 if (!NILP (Vprocess_connection_type))
1880 outchannel = inchannel = allocate_pty ();
1881 1913
1882 if (inchannel >= 0) 1914 if (pty_fd >= 0)
1883 { 1915 {
1916 p->open_fd[SUBPROCESS_STDIN] = pty_fd;
1884#if ! defined (USG) || defined (USG_SUBTTY_WORKS) 1917#if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1885 /* On most USG systems it does not work to open the pty's tty here, 1918 /* On most USG systems it does not work to open the pty's tty here,
1886 then close it and reopen it in the child. */ 1919 then close it and reopen it in the child. */
@@ -1889,6 +1922,7 @@ create_pty (Lisp_Object process)
1889 int forkout = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0); 1922 int forkout = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0);
1890 if (forkout < 0) 1923 if (forkout < 0)
1891 report_file_error ("Opening pty", Qnil); 1924 report_file_error ("Opening pty", Qnil);
1925 p->open_fd[WRITE_TO_SUBPROCESS] = forkout;
1892#if defined (DONT_REOPEN_PTY) 1926#if defined (DONT_REOPEN_PTY)
1893 /* In the case that vfork is defined as fork, the parent process 1927 /* In the case that vfork is defined as fork, the parent process
1894 (Emacs) may send some data before the child process completes 1928 (Emacs) may send some data before the child process completes
@@ -1896,47 +1930,43 @@ create_pty (Lisp_Object process)
1896 child_setup_tty (forkout); 1930 child_setup_tty (forkout);
1897#endif /* DONT_REOPEN_PTY */ 1931#endif /* DONT_REOPEN_PTY */
1898#endif /* not USG, or USG_SUBTTY_WORKS */ 1932#endif /* not USG, or USG_SUBTTY_WORKS */
1899 pty_flag = 1;
1900 }
1901#endif /* HAVE_PTYS */
1902 1933
1903 fcntl (inchannel, F_SETFL, O_NONBLOCK); 1934 fcntl (pty_fd, F_SETFL, O_NONBLOCK);
1904 fcntl (outchannel, F_SETFL, O_NONBLOCK);
1905 1935
1906 /* Record this as an active process, with its channels. 1936 /* Record this as an active process, with its channels.
1907 As a result, child_setup will close Emacs's side of the pipes. */ 1937 As a result, child_setup will close Emacs's side of the pipes. */
1908 chan_process[inchannel] = process; 1938 chan_process[pty_fd] = process;
1909 XPROCESS (process)->infd = inchannel; 1939 p->infd = pty_fd;
1910 XPROCESS (process)->outfd = outchannel; 1940 p->outfd = pty_fd;
1911 1941
1912 /* Previously we recorded the tty descriptor used in the subprocess. 1942 /* Previously we recorded the tty descriptor used in the subprocess.
1913 It was only used for getting the foreground tty process, so now 1943 It was only used for getting the foreground tty process, so now
1914 we just reopen the device (see emacs_get_tty_pgrp) as this is 1944 we just reopen the device (see emacs_get_tty_pgrp) as this is
1915 more portable (see USG_SUBTTY_WORKS above). */ 1945 more portable (see USG_SUBTTY_WORKS above). */
1916 1946
1917 XPROCESS (process)->pty_flag = pty_flag; 1947 p->pty_flag = 1;
1918 pset_status (XPROCESS (process), Qrun); 1948 pset_status (p, Qrun);
1919 setup_process_coding_systems (process); 1949 setup_process_coding_systems (process);
1920 1950
1921 FD_SET (inchannel, &input_wait_mask); 1951 FD_SET (pty_fd, &input_wait_mask);
1922 FD_SET (inchannel, &non_keyboard_wait_mask); 1952 FD_SET (pty_fd, &non_keyboard_wait_mask);
1923 if (inchannel > max_process_desc) 1953 if (pty_fd > max_process_desc)
1924 max_process_desc = inchannel; 1954 max_process_desc = pty_fd;
1925 1955
1926 XPROCESS (process)->pid = -2; 1956 pset_tty_name (p, build_string (pty_name));
1927#ifdef HAVE_PTYS 1957 }
1928 if (pty_flag) 1958
1929 pset_tty_name (XPROCESS (process), build_string (pty_name)); 1959 p->pid = -2;
1930 else
1931#endif
1932 pset_tty_name (XPROCESS (process), Qnil);
1933} 1960}
1934 1961
1935 1962
1936/* Convert an internal struct sockaddr to a lisp object (vector or string). 1963/* Convert an internal struct sockaddr to a lisp object (vector or string).
1937 The address family of sa is not included in the result. */ 1964 The address family of sa is not included in the result. */
1938 1965
1939static Lisp_Object 1966#ifndef WINDOWSNT
1967static
1968#endif
1969Lisp_Object
1940conv_sockaddr_to_lisp (struct sockaddr *sa, int len) 1970conv_sockaddr_to_lisp (struct sockaddr *sa, int len)
1941{ 1971{
1942 Lisp_Object address; 1972 Lisp_Object address;
@@ -2154,7 +2184,7 @@ Returns nil upon error setting address, ADDRESS otherwise. */)
2154 channel = XPROCESS (process)->infd; 2184 channel = XPROCESS (process)->infd;
2155 2185
2156 len = get_lisp_to_sockaddr_size (address, &family); 2186 len = get_lisp_to_sockaddr_size (address, &family);
2157 if (datagram_address[channel].len != len) 2187 if (len == 0 || datagram_address[channel].len != len)
2158 return Qnil; 2188 return Qnil;
2159 conv_lisp_to_sockaddr (family, address, datagram_address[channel].sa, len); 2189 conv_lisp_to_sockaddr (family, address, datagram_address[channel].sa, len);
2160 return address; 2190 return address;
@@ -2289,8 +2319,12 @@ set_socket_option (int s, Lisp_Object opt, Lisp_Object val)
2289 } 2319 }
2290 2320
2291 if (ret < 0) 2321 if (ret < 0)
2292 report_file_error ("Cannot set network option", 2322 {
2293 Fcons (opt, Fcons (val, Qnil))); 2323 int setsockopt_errno = errno;
2324 report_file_errno ("Cannot set network option", list2 (opt, val),
2325 setsockopt_errno);
2326 }
2327
2294 return (1 << sopt->optbit); 2328 return (1 << sopt->optbit);
2295} 2329}
2296 2330
@@ -2422,16 +2456,6 @@ usage: (serial-process-configure &rest ARGS) */)
2422 return Qnil; 2456 return Qnil;
2423} 2457}
2424 2458
2425/* Used by make-serial-process to recover from errors. */
2426static Lisp_Object
2427make_serial_process_unwind (Lisp_Object proc)
2428{
2429 if (!PROCESSP (proc))
2430 emacs_abort ();
2431 remove_process (proc);
2432 return Qnil;
2433}
2434
2435DEFUN ("make-serial-process", Fmake_serial_process, Smake_serial_process, 2459DEFUN ("make-serial-process", Fmake_serial_process, Smake_serial_process,
2436 0, MANY, 0, 2460 0, MANY, 0,
2437 doc: /* Create and return a serial port process. 2461 doc: /* Create and return a serial port process.
@@ -2513,7 +2537,7 @@ usage: (make-serial-process &rest ARGS) */)
2513 struct gcpro gcpro1; 2537 struct gcpro gcpro1;
2514 Lisp_Object name, buffer; 2538 Lisp_Object name, buffer;
2515 Lisp_Object tem, val; 2539 Lisp_Object tem, val;
2516 ptrdiff_t specpdl_count = -1; 2540 ptrdiff_t specpdl_count;
2517 2541
2518 if (nargs == 0) 2542 if (nargs == 0)
2519 return Qnil; 2543 return Qnil;
@@ -2537,10 +2561,11 @@ usage: (make-serial-process &rest ARGS) */)
2537 CHECK_STRING (name); 2561 CHECK_STRING (name);
2538 proc = make_process (name); 2562 proc = make_process (name);
2539 specpdl_count = SPECPDL_INDEX (); 2563 specpdl_count = SPECPDL_INDEX ();
2540 record_unwind_protect (make_serial_process_unwind, proc); 2564 record_unwind_protect (remove_process, proc);
2541 p = XPROCESS (proc); 2565 p = XPROCESS (proc);
2542 2566
2543 fd = serial_open (SSDATA (port)); 2567 fd = serial_open (port);
2568 p->open_fd[SUBPROCESS_STDIN] = fd;
2544 p->infd = fd; 2569 p->infd = fd;
2545 p->outfd = fd; 2570 p->outfd = fd;
2546 if (fd > max_process_desc) 2571 if (fd > max_process_desc)
@@ -2563,7 +2588,7 @@ usage: (make-serial-process &rest ARGS) */)
2563 p->kill_without_query = 1; 2588 p->kill_without_query = 1;
2564 if (tem = Fplist_get (contact, QCstop), !NILP (tem)) 2589 if (tem = Fplist_get (contact, QCstop), !NILP (tem))
2565 pset_command (p, Qt); 2590 pset_command (p, Qt);
2566 p->pty_flag = 0; 2591 eassert (! p->pty_flag);
2567 2592
2568 if (!EQ (p->command, Qt)) 2593 if (!EQ (p->command, Qt))
2569 { 2594 {
@@ -2733,7 +2758,7 @@ client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
2733is the server process, CLIENT is the new process for the connection, 2758is the server process, CLIENT is the new process for the connection,
2734and MESSAGE is a string. 2759and MESSAGE is a string.
2735 2760
2736:plist PLIST -- Install PLIST as the new process' initial plist. 2761:plist PLIST -- Install PLIST as the new process's initial plist.
2737 2762
2738:server QLEN -- if QLEN is non-nil, create a server process for the 2763:server QLEN -- if QLEN is non-nil, create a server process for the
2739specified FAMILY, SERVICE, and connection type (stream or datagram). 2764specified FAMILY, SERVICE, and connection type (stream or datagram).
@@ -2763,21 +2788,21 @@ When a client connection is accepted, a new network process is created
2763for the connection with the following parameters: 2788for the connection with the following parameters:
2764 2789
2765- The client's process name is constructed by concatenating the server 2790- The client's process name is constructed by concatenating the server
2766process' NAME and a client identification string. 2791process's NAME and a client identification string.
2767- If the FILTER argument is non-nil, the client process will not get a 2792- If the FILTER argument is non-nil, the client process will not get a
2768separate process buffer; otherwise, the client's process buffer is a newly 2793separate process buffer; otherwise, the client's process buffer is a newly
2769created buffer named after the server process' BUFFER name or process 2794created buffer named after the server process's BUFFER name or process
2770NAME concatenated with the client identification string. 2795NAME concatenated with the client identification string.
2771- The connection type and the process filter and sentinel parameters are 2796- The connection type and the process filter and sentinel parameters are
2772inherited from the server process' TYPE, FILTER and SENTINEL. 2797inherited from the server process's TYPE, FILTER and SENTINEL.
2773- The client process' contact info is set according to the client's 2798- The client process's contact info is set according to the client's
2774addressing information (typically an IP address and a port number). 2799addressing information (typically an IP address and a port number).
2775- The client process' plist is initialized from the server's plist. 2800- The client process's plist is initialized from the server's plist.
2776 2801
2777Notice that the FILTER and SENTINEL args are never used directly by 2802Notice that the FILTER and SENTINEL args are never used directly by
2778the server process. Also, the BUFFER argument is not used directly by 2803the server process. Also, the BUFFER argument is not used directly by
2779the server process, but via the optional :log function, accepted (and 2804the server process, but via the optional :log function, accepted (and
2780failed) connections may be logged in the server process' buffer. 2805failed) connections may be logged in the server process's buffer.
2781 2806
2782The original argument list, modified with the actual connection 2807The original argument list, modified with the actual connection
2783information, is available via the `process-contact' function. 2808information, is available via the `process-contact' function.
@@ -2819,8 +2844,9 @@ usage: (make-network-process &rest ARGS) */)
2819 Lisp_Object tem; 2844 Lisp_Object tem;
2820 Lisp_Object name, buffer, host, service, address; 2845 Lisp_Object name, buffer, host, service, address;
2821 Lisp_Object filter, sentinel; 2846 Lisp_Object filter, sentinel;
2822 int is_non_blocking_client = 0; 2847 bool is_non_blocking_client = 0;
2823 int is_server = 0, backlog = 5; 2848 bool is_server = 0;
2849 int backlog = 5;
2824 int socktype; 2850 int socktype;
2825 int family = -1; 2851 int family = -1;
2826 2852
@@ -2972,7 +2998,7 @@ usage: (make-network-process &rest ARGS) */)
2972#ifdef POLL_FOR_INPUT 2998#ifdef POLL_FOR_INPUT
2973 if (socktype != SOCK_DGRAM) 2999 if (socktype != SOCK_DGRAM)
2974 { 3000 {
2975 record_unwind_protect (unwind_stop_other_atimers, Qnil); 3001 record_unwind_protect_void (run_all_atimers);
2976 bind_polling_period (10); 3002 bind_polling_period (10);
2977 } 3003 }
2978#endif 3004#endif
@@ -3104,7 +3130,8 @@ usage: (make-network-process &rest ARGS) */)
3104 retry_connect: 3130 retry_connect:
3105#endif 3131#endif
3106 3132
3107 s = socket (lres->ai_family, lres->ai_socktype, lres->ai_protocol); 3133 s = socket (lres->ai_family, lres->ai_socktype | SOCK_CLOEXEC,
3134 lres->ai_protocol);
3108 if (s < 0) 3135 if (s < 0)
3109 { 3136 {
3110 xerrno = errno; 3137 xerrno = errno;
@@ -3131,7 +3158,7 @@ usage: (make-network-process &rest ARGS) */)
3131#endif 3158#endif
3132 3159
3133 /* Make us close S if quit. */ 3160 /* Make us close S if quit. */
3134 record_unwind_protect (close_file_unwind, make_number (s)); 3161 record_unwind_protect_int (close_file_unwind, s);
3135 3162
3136 /* Parse network options in the arg list. 3163 /* Parse network options in the arg list.
3137 We simply ignore anything which isn't a known option (including other keywords). 3164 We simply ignore anything which isn't a known option (including other keywords).
@@ -3211,7 +3238,7 @@ usage: (make-network-process &rest ARGS) */)
3211 wait for completion is pselect(). */ 3238 wait for completion is pselect(). */
3212 int sc; 3239 int sc;
3213 socklen_t len; 3240 socklen_t len;
3214 SELECT_TYPE fdset; 3241 fd_set fdset;
3215 retry_select: 3242 retry_select:
3216 FD_ZERO (&fdset); 3243 FD_ZERO (&fdset);
3217 FD_SET (s, &fdset); 3244 FD_SET (s, &fdset);
@@ -3222,18 +3249,17 @@ usage: (make-network-process &rest ARGS) */)
3222 if (errno == EINTR) 3249 if (errno == EINTR)
3223 goto retry_select; 3250 goto retry_select;
3224 else 3251 else
3225 report_file_error ("select failed", Qnil); 3252 report_file_error ("Failed select", Qnil);
3226 } 3253 }
3227 eassert (sc > 0); 3254 eassert (sc > 0);
3228 3255
3229 len = sizeof xerrno; 3256 len = sizeof xerrno;
3230 eassert (FD_ISSET (s, &fdset)); 3257 eassert (FD_ISSET (s, &fdset));
3231 if (getsockopt (s, SOL_SOCKET, SO_ERROR, &xerrno, &len) == -1) 3258 if (getsockopt (s, SOL_SOCKET, SO_ERROR, &xerrno, &len) < 0)
3232 report_file_error ("getsockopt failed", Qnil); 3259 report_file_error ("Failed getsockopt", Qnil);
3233 if (xerrno) 3260 if (xerrno)
3234 errno = xerrno, report_file_error ("error during connect", Qnil); 3261 report_file_errno ("Failed connect", Qnil, xerrno);
3235 else 3262 break;
3236 break;
3237 } 3263 }
3238#endif /* !WINDOWSNT */ 3264#endif /* !WINDOWSNT */
3239 3265
@@ -3267,7 +3293,8 @@ usage: (make-network-process &rest ARGS) */)
3267 { 3293 {
3268 int rfamily, rlen; 3294 int rfamily, rlen;
3269 rlen = get_lisp_to_sockaddr_size (remote, &rfamily); 3295 rlen = get_lisp_to_sockaddr_size (remote, &rfamily);
3270 if (rfamily == lres->ai_family && rlen == lres->ai_addrlen) 3296 if (rlen != 0 && rfamily == lres->ai_family
3297 && rlen == lres->ai_addrlen)
3271 conv_lisp_to_sockaddr (rfamily, remote, 3298 conv_lisp_to_sockaddr (rfamily, remote,
3272 datagram_address[s].sa, rlen); 3299 datagram_address[s].sa, rlen);
3273 } 3300 }
@@ -3301,12 +3328,6 @@ usage: (make-network-process &rest ARGS) */)
3301 } 3328 }
3302#endif 3329#endif
3303 3330
3304 /* Discard the unwind protect for closing S, if any. */
3305 specpdl_ptr = specpdl + count1;
3306
3307 /* Unwind bind_polling_period and request_sigio. */
3308 unbind_to (count, Qnil);
3309
3310 if (s < 0) 3331 if (s < 0)
3311 { 3332 {
3312 /* If non-blocking got this far - and failed - assume non-blocking is 3333 /* If non-blocking got this far - and failed - assume non-blocking is
@@ -3316,11 +3337,10 @@ usage: (make-network-process &rest ARGS) */)
3316 if (is_non_blocking_client) 3337 if (is_non_blocking_client)
3317 return Qnil; 3338 return Qnil;
3318 3339
3319 errno = xerrno; 3340 report_file_errno ((is_server
3320 if (is_server) 3341 ? "make server process failed"
3321 report_file_error ("make server process failed", contact); 3342 : "make client process failed"),
3322 else 3343 contact, xerrno);
3323 report_file_error ("make client process failed", contact);
3324 } 3344 }
3325 3345
3326 inch = s; 3346 inch = s;
@@ -3349,8 +3369,17 @@ usage: (make-network-process &rest ARGS) */)
3349 if ((tem = Fplist_get (contact, QCstop), !NILP (tem))) 3369 if ((tem = Fplist_get (contact, QCstop), !NILP (tem)))
3350 pset_command (p, Qt); 3370 pset_command (p, Qt);
3351 p->pid = 0; 3371 p->pid = 0;
3372
3373 p->open_fd[SUBPROCESS_STDIN] = inch;
3352 p->infd = inch; 3374 p->infd = inch;
3353 p->outfd = outch; 3375 p->outfd = outch;
3376
3377 /* Discard the unwind protect for closing S, if any. */
3378 specpdl_ptr = specpdl + count1;
3379
3380 /* Unwind bind_polling_period and request_sigio. */
3381 unbind_to (count, Qnil);
3382
3354 if (is_server && socktype != SOCK_DGRAM) 3383 if (is_server && socktype != SOCK_DGRAM)
3355 pset_status (p, Qlisten); 3384 pset_status (p, Qlisten);
3356 3385
@@ -3483,26 +3512,25 @@ usage: (make-network-process &rest ARGS) */)
3483} 3512}
3484 3513
3485 3514
3486#if defined (HAVE_NET_IF_H) 3515#ifdef HAVE_NET_IF_H
3487 3516
3488#ifdef SIOCGIFCONF 3517#ifdef SIOCGIFCONF
3489DEFUN ("network-interface-list", Fnetwork_interface_list, Snetwork_interface_list, 0, 0, 0, 3518static Lisp_Object
3490 doc: /* Return an alist of all network interfaces and their network address. 3519network_interface_list (void)
3491Each element is a cons, the car of which is a string containing the
3492interface name, and the cdr is the network address in internal
3493format; see the description of ADDRESS in `make-network-process'. */)
3494 (void)
3495{ 3520{
3496 struct ifconf ifconf; 3521 struct ifconf ifconf;
3497 struct ifreq *ifreq; 3522 struct ifreq *ifreq;
3498 void *buf = NULL; 3523 void *buf = NULL;
3499 ptrdiff_t buf_size = 512; 3524 ptrdiff_t buf_size = 512;
3500 int s, i; 3525 int s;
3501 Lisp_Object res; 3526 Lisp_Object res;
3527 ptrdiff_t count;
3502 3528
3503 s = socket (AF_INET, SOCK_STREAM, 0); 3529 s = socket (AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
3504 if (s < 0) 3530 if (s < 0)
3505 return Qnil; 3531 return Qnil;
3532 count = SPECPDL_INDEX ();
3533 record_unwind_protect_int (close_file_unwind, s);
3506 3534
3507 do 3535 do
3508 { 3536 {
@@ -3511,16 +3539,14 @@ format; see the description of ADDRESS in `make-network-process'. */)
3511 ifconf.ifc_len = buf_size; 3539 ifconf.ifc_len = buf_size;
3512 if (ioctl (s, SIOCGIFCONF, &ifconf)) 3540 if (ioctl (s, SIOCGIFCONF, &ifconf))
3513 { 3541 {
3514 close (s); 3542 emacs_close (s);
3515 xfree (buf); 3543 xfree (buf);
3516 return Qnil; 3544 return Qnil;
3517 } 3545 }
3518 } 3546 }
3519 while (ifconf.ifc_len == buf_size); 3547 while (ifconf.ifc_len == buf_size);
3520 3548
3521 close (s); 3549 res = unbind_to (count, Qnil);
3522
3523 res = Qnil;
3524 ifreq = ifconf.ifc_req; 3550 ifreq = ifconf.ifc_req;
3525 while ((char *) ifreq < (char *) ifconf.ifc_req + ifconf.ifc_len) 3551 while ((char *) ifreq < (char *) ifconf.ifc_req + ifconf.ifc_len)
3526 { 3552 {
@@ -3535,7 +3561,6 @@ format; see the description of ADDRESS in `make-network-process'. */)
3535 int len = sizeof (*ifreq); 3561 int len = sizeof (*ifreq);
3536#endif 3562#endif
3537 char namebuf[sizeof (ifq->ifr_name) + 1]; 3563 char namebuf[sizeof (ifq->ifr_name) + 1];
3538 i += len;
3539 ifreq = (struct ifreq *) ((char *) ifreq + len); 3564 ifreq = (struct ifreq *) ((char *) ifreq + len);
3540 3565
3541 if (ifq->ifr_addr.sa_family != AF_INET) 3566 if (ifq->ifr_addr.sa_family != AF_INET)
@@ -3633,19 +3658,15 @@ static const struct ifflag_def ifflag_table[] = {
3633 { 0, 0 } 3658 { 0, 0 }
3634}; 3659};
3635 3660
3636DEFUN ("network-interface-info", Fnetwork_interface_info, Snetwork_interface_info, 1, 1, 0, 3661static Lisp_Object
3637 doc: /* Return information about network interface named IFNAME. 3662network_interface_info (Lisp_Object ifname)
3638The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3639where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3640NETMASK is the layer 3 network mask, HWADDR is the layer 2 address, and
3641FLAGS is the current flags of the interface. */)
3642 (Lisp_Object ifname)
3643{ 3663{
3644 struct ifreq rq; 3664 struct ifreq rq;
3645 Lisp_Object res = Qnil; 3665 Lisp_Object res = Qnil;
3646 Lisp_Object elt; 3666 Lisp_Object elt;
3647 int s; 3667 int s;
3648 int any = 0; 3668 bool any = 0;
3669 ptrdiff_t count;
3649#if (! (defined SIOCGIFHWADDR && defined HAVE_STRUCT_IFREQ_IFR_HWADDR) \ 3670#if (! (defined SIOCGIFHWADDR && defined HAVE_STRUCT_IFREQ_IFR_HWADDR) \
3650 && defined HAVE_GETIFADDRS && defined LLADDR) 3671 && defined HAVE_GETIFADDRS && defined LLADDR)
3651 struct ifaddrs *ifap; 3672 struct ifaddrs *ifap;
@@ -3657,9 +3678,11 @@ FLAGS is the current flags of the interface. */)
3657 error ("interface name too long"); 3678 error ("interface name too long");
3658 strcpy (rq.ifr_name, SSDATA (ifname)); 3679 strcpy (rq.ifr_name, SSDATA (ifname));
3659 3680
3660 s = socket (AF_INET, SOCK_STREAM, 0); 3681 s = socket (AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
3661 if (s < 0) 3682 if (s < 0)
3662 return Qnil; 3683 return Qnil;
3684 count = SPECPDL_INDEX ();
3685 record_unwind_protect_int (close_file_unwind, s);
3663 3686
3664 elt = Qnil; 3687 elt = Qnil;
3665#if defined (SIOCGIFFLAGS) && defined (HAVE_STRUCT_IFREQ_IFR_FLAGS) 3688#if defined (SIOCGIFFLAGS) && defined (HAVE_STRUCT_IFREQ_IFR_FLAGS)
@@ -3705,7 +3728,9 @@ FLAGS is the current flags of the interface. */)
3705 3728
3706 any = 1; 3729 any = 1;
3707 for (n = 0; n < 6; n++) 3730 for (n = 0; n < 6; n++)
3708 p->contents[n] = make_number (((unsigned char *)&rq.ifr_hwaddr.sa_data[0])[n]); 3731 p->contents[n] = make_number (((unsigned char *)
3732 &rq.ifr_hwaddr.sa_data[0])
3733 [n]);
3709 elt = Fcons (make_number (rq.ifr_hwaddr.sa_family), hwaddr); 3734 elt = Fcons (make_number (rq.ifr_hwaddr.sa_family), hwaddr);
3710 } 3735 }
3711#elif defined (HAVE_GETIFADDRS) && defined (LLADDR) 3736#elif defined (HAVE_GETIFADDRS) && defined (LLADDR)
@@ -3776,29 +3801,64 @@ FLAGS is the current flags of the interface. */)
3776#endif 3801#endif
3777 res = Fcons (elt, res); 3802 res = Fcons (elt, res);
3778 3803
3779 close (s); 3804 return unbind_to (count, any ? res : Qnil);
3805}
3806#endif /* !SIOCGIFADDR && !SIOCGIFHWADDR && !SIOCGIFFLAGS */
3807#endif /* defined (HAVE_NET_IF_H) */
3780 3808
3781 return any ? res : Qnil; 3809DEFUN ("network-interface-list", Fnetwork_interface_list,
3810 Snetwork_interface_list, 0, 0, 0,
3811 doc: /* Return an alist of all network interfaces and their network address.
3812Each element is a cons, the car of which is a string containing the
3813interface name, and the cdr is the network address in internal
3814format; see the description of ADDRESS in `make-network-process'.
3815
3816If the information is not available, return nil. */)
3817 (void)
3818{
3819#if (defined HAVE_NET_IF_H && defined SIOCGIFCONF) || defined WINDOWSNT
3820 return network_interface_list ();
3821#else
3822 return Qnil;
3823#endif
3782} 3824}
3825
3826DEFUN ("network-interface-info", Fnetwork_interface_info,
3827 Snetwork_interface_info, 1, 1, 0,
3828 doc: /* Return information about network interface named IFNAME.
3829The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3830where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3831NETMASK is the layer 3 network mask, HWADDR is the layer 2 address, and
3832FLAGS is the current flags of the interface.
3833
3834Data that is unavailable is returned as nil. */)
3835 (Lisp_Object ifname)
3836{
3837#if ((defined HAVE_NET_IF_H \
3838 && (defined SIOCGIFADDR || defined SIOCGIFHWADDR \
3839 || defined SIOCGIFFLAGS)) \
3840 || defined WINDOWSNT)
3841 return network_interface_info (ifname);
3842#else
3843 return Qnil;
3783#endif 3844#endif
3784#endif /* defined (HAVE_NET_IF_H) */ 3845}
3846
3785 3847
3786/* Turn off input and output for process PROC. */ 3848/* Turn off input and output for process PROC. */
3787 3849
3788static void 3850static void
3789deactivate_process (Lisp_Object proc) 3851deactivate_process (Lisp_Object proc)
3790{ 3852{
3791 register int inchannel, outchannel; 3853 int inchannel;
3792 register struct Lisp_Process *p = XPROCESS (proc); 3854 struct Lisp_Process *p = XPROCESS (proc);
3855 int i;
3793 3856
3794#ifdef HAVE_GNUTLS 3857#ifdef HAVE_GNUTLS
3795 /* Delete GnuTLS structures in PROC, if any. */ 3858 /* Delete GnuTLS structures in PROC, if any. */
3796 emacs_gnutls_deinit (proc); 3859 emacs_gnutls_deinit (proc);
3797#endif /* HAVE_GNUTLS */ 3860#endif /* HAVE_GNUTLS */
3798 3861
3799 inchannel = p->infd;
3800 outchannel = p->outfd;
3801
3802#ifdef ADAPTIVE_READ_BUFFERING 3862#ifdef ADAPTIVE_READ_BUFFERING
3803 if (p->read_output_delay > 0) 3863 if (p->read_output_delay > 0)
3804 { 3864 {
@@ -3809,14 +3869,14 @@ deactivate_process (Lisp_Object proc)
3809 } 3869 }
3810#endif 3870#endif
3811 3871
3872 /* Beware SIGCHLD hereabouts. */
3873
3874 for (i = 0; i < PROCESS_OPEN_FDS; i++)
3875 close_process_fd (&p->open_fd[i]);
3876
3877 inchannel = p->infd;
3812 if (inchannel >= 0) 3878 if (inchannel >= 0)
3813 { 3879 {
3814 /* Beware SIGCHLD hereabouts. */
3815 flush_pending_output (inchannel);
3816 emacs_close (inchannel);
3817 if (outchannel >= 0 && outchannel != inchannel)
3818 emacs_close (outchannel);
3819
3820 p->infd = -1; 3880 p->infd = -1;
3821 p->outfd = -1; 3881 p->outfd = -1;
3822#ifdef DATAGRAM_SOCKETS 3882#ifdef DATAGRAM_SOCKETS
@@ -3841,13 +3901,14 @@ deactivate_process (Lisp_Object proc)
3841#endif 3901#endif
3842 if (inchannel == max_process_desc) 3902 if (inchannel == max_process_desc)
3843 { 3903 {
3844 int i;
3845 /* We just closed the highest-numbered process input descriptor, 3904 /* We just closed the highest-numbered process input descriptor,
3846 so recompute the highest-numbered one now. */ 3905 so recompute the highest-numbered one now. */
3847 max_process_desc = 0; 3906 int i = inchannel;
3848 for (i = 0; i < MAXDESC; i++) 3907 do
3849 if (!NILP (chan_process[i])) 3908 i--;
3850 max_process_desc = i; 3909 while (0 <= i && NILP (chan_process[i]));
3910
3911 max_process_desc = i;
3851 } 3912 }
3852 } 3913 }
3853} 3914}
@@ -3856,7 +3917,7 @@ deactivate_process (Lisp_Object proc)
3856DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output, 3917DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output,
3857 0, 4, 0, 3918 0, 4, 0,
3858 doc: /* Allow any pending output from subprocesses to be read by Emacs. 3919 doc: /* Allow any pending output from subprocesses to be read by Emacs.
3859It is read into the process' buffers or given to their filter functions. 3920It is given to their filter functions.
3860Non-nil arg PROCESS means do not return until some output has been received 3921Non-nil arg PROCESS means do not return until some output has been received
3861from PROCESS. 3922from PROCESS.
3862 3923
@@ -3899,7 +3960,7 @@ Return non-nil if we received any output before the timeout expired. */)
3899 { 3960 {
3900 if (INTEGERP (seconds)) 3961 if (INTEGERP (seconds))
3901 { 3962 {
3902 if (0 < XINT (seconds)) 3963 if (XINT (seconds) > 0)
3903 { 3964 {
3904 secs = XINT (seconds); 3965 secs = XINT (seconds);
3905 nsecs = 0; 3966 nsecs = 0;
@@ -3907,11 +3968,11 @@ Return non-nil if we received any output before the timeout expired. */)
3907 } 3968 }
3908 else if (FLOATP (seconds)) 3969 else if (FLOATP (seconds))
3909 { 3970 {
3910 if (0 < XFLOAT_DATA (seconds)) 3971 if (XFLOAT_DATA (seconds) > 0)
3911 { 3972 {
3912 EMACS_TIME t = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (seconds)); 3973 struct timespec t = dtotimespec (XFLOAT_DATA (seconds));
3913 secs = min (EMACS_SECS (t), WAIT_READING_MAX); 3974 secs = min (t.tv_sec, WAIT_READING_MAX);
3914 nsecs = EMACS_NSECS (t); 3975 nsecs = t.tv_nsec;
3915 } 3976 }
3916 } 3977 }
3917 else 3978 else
@@ -3931,7 +3992,7 @@ Return non-nil if we received any output before the timeout expired. */)
3931 3992
3932/* Accept a connection for server process SERVER on CHANNEL. */ 3993/* Accept a connection for server process SERVER on CHANNEL. */
3933 3994
3934static int connect_counter = 0; 3995static EMACS_INT connect_counter = 0;
3935 3996
3936static void 3997static void
3937server_accept_connection (Lisp_Object server, int channel) 3998server_accept_connection (Lisp_Object server, int channel)
@@ -3952,8 +4013,9 @@ server_accept_connection (Lisp_Object server, int channel)
3952#endif 4013#endif
3953 } saddr; 4014 } saddr;
3954 socklen_t len = sizeof saddr; 4015 socklen_t len = sizeof saddr;
4016 ptrdiff_t count;
3955 4017
3956 s = accept (channel, &saddr.sa, &len); 4018 s = accept4 (channel, &saddr.sa, &len, SOCK_CLOEXEC);
3957 4019
3958 if (s < 0) 4020 if (s < 0)
3959 { 4021 {
@@ -3974,6 +4036,9 @@ server_accept_connection (Lisp_Object server, int channel)
3974 return; 4036 return;
3975 } 4037 }
3976 4038
4039 count = SPECPDL_INDEX ();
4040 record_unwind_protect_int (close_file_unwind, s);
4041
3977 connect_counter++; 4042 connect_counter++;
3978 4043
3979 /* Setup a new process to handle the connection. */ 4044 /* Setup a new process to handle the connection. */
@@ -4037,7 +4102,8 @@ server_accept_connection (Lisp_Object server, int channel)
4037 process name of the server process concatenated with the caller 4102 process name of the server process concatenated with the caller
4038 identification. */ 4103 identification. */
4039 4104
4040 if (!NILP (ps->filter) && !EQ (ps->filter, Qt)) 4105 if (!(EQ (ps->filter, Qinternal_default_process_filter)
4106 || EQ (ps->filter, Qt)))
4041 buffer = Qnil; 4107 buffer = Qnil;
4042 else 4108 else
4043 { 4109 {
@@ -4089,6 +4155,11 @@ server_accept_connection (Lisp_Object server, int channel)
4089 pset_filter (p, ps->filter); 4155 pset_filter (p, ps->filter);
4090 pset_command (p, Qnil); 4156 pset_command (p, Qnil);
4091 p->pid = 0; 4157 p->pid = 0;
4158
4159 /* Discard the unwind protect for closing S. */
4160 specpdl_ptr = specpdl + count;
4161
4162 p->open_fd[SUBPROCESS_STDIN] = s;
4092 p->infd = s; 4163 p->infd = s;
4093 p->outfd = s; 4164 p->outfd = s;
4094 pset_status (p, Qrun); 4165 pset_status (p, Qrun);
@@ -4106,7 +4177,7 @@ server_accept_connection (Lisp_Object server, int channel)
4106 /* Setup coding system for new process based on server process. 4177 /* Setup coding system for new process based on server process.
4107 This seems to be the proper thing to do, as the coding system 4178 This seems to be the proper thing to do, as the coding system
4108 of the new process should reflect the settings at the time the 4179 of the new process should reflect the settings at the time the
4109 server socket was opened; not the current settings. */ 4180 server socket was opened; not the current settings. */
4110 4181
4111 pset_decode_coding_system (p, ps->decode_coding_system); 4182 pset_decode_coding_system (p, ps->decode_coding_system);
4112 pset_encode_coding_system (p, ps->encode_coding_system); 4183 pset_encode_coding_system (p, ps->encode_coding_system);
@@ -4125,11 +4196,10 @@ server_accept_connection (Lisp_Object server, int channel)
4125 (STRINGP (host) ? host : build_string ("-")), 4196 (STRINGP (host) ? host : build_string ("-")),
4126 build_string ("\n"))); 4197 build_string ("\n")));
4127 4198
4128 if (!NILP (p->sentinel)) 4199 exec_sentinel (proc,
4129 exec_sentinel (proc, 4200 concat3 (build_string ("open from "),
4130 concat3 (build_string ("open from "), 4201 (STRINGP (host) ? host : build_string ("-")),
4131 (STRINGP (host) ? host : build_string ("-")), 4202 build_string ("\n")));
4132 build_string ("\n")));
4133} 4203}
4134 4204
4135/* This variable is different from waiting_for_input in keyboard.c. 4205/* This variable is different from waiting_for_input in keyboard.c.
@@ -4143,11 +4213,10 @@ server_accept_connection (Lisp_Object server, int channel)
4143 when not inside wait_reading_process_output. */ 4213 when not inside wait_reading_process_output. */
4144static int waiting_for_user_input_p; 4214static int waiting_for_user_input_p;
4145 4215
4146static Lisp_Object 4216static void
4147wait_reading_process_output_unwind (Lisp_Object data) 4217wait_reading_process_output_unwind (int data)
4148{ 4218{
4149 waiting_for_user_input_p = XINT (data); 4219 waiting_for_user_input_p = data;
4150 return Qnil;
4151} 4220}
4152 4221
4153/* This is here so breakpoints can be put on it. */ 4222/* This is here so breakpoints can be put on it. */
@@ -4186,7 +4255,7 @@ wait_reading_process_output_1 (void)
4186 process. The return value is true if we read some input from 4255 process. The return value is true if we read some input from
4187 that process. 4256 that process.
4188 4257
4189 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC 4258 If JUST_WAIT_PROC is nonzero, handle only output from WAIT_PROC
4190 (suspending output from other processes). A negative value 4259 (suspending output from other processes). A negative value
4191 means don't run any timers either. 4260 means don't run any timers either.
4192 4261
@@ -4194,22 +4263,23 @@ wait_reading_process_output_1 (void)
4194 received input from that process before the timeout elapsed. 4263 received input from that process before the timeout elapsed.
4195 Otherwise, return true if we received input from any process. */ 4264 Otherwise, return true if we received input from any process. */
4196 4265
4197int 4266bool
4198wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, 4267wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4199 bool do_display, 4268 bool do_display,
4200 Lisp_Object wait_for_cell, 4269 Lisp_Object wait_for_cell,
4201 struct Lisp_Process *wait_proc, int just_wait_proc) 4270 struct Lisp_Process *wait_proc, int just_wait_proc)
4202{ 4271{
4203 register int channel, nfds; 4272 int channel, nfds;
4204 SELECT_TYPE Available; 4273 fd_set Available;
4205 SELECT_TYPE Writeok; 4274 fd_set Writeok;
4206 int check_write; 4275 bool check_write;
4207 int check_delay, no_avail; 4276 int check_delay;
4277 bool no_avail;
4208 int xerrno; 4278 int xerrno;
4209 Lisp_Object proc; 4279 Lisp_Object proc;
4210 EMACS_TIME timeout, end_time; 4280 struct timespec timeout, end_time;
4211 int wait_channel = -1; 4281 int wait_channel = -1;
4212 int got_some_input = 0; 4282 bool got_some_input = 0;
4213 ptrdiff_t count = SPECPDL_INDEX (); 4283 ptrdiff_t count = SPECPDL_INDEX ();
4214 4284
4215 FD_ZERO (&Available); 4285 FD_ZERO (&Available);
@@ -4224,8 +4294,8 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4224 if (wait_proc != NULL) 4294 if (wait_proc != NULL)
4225 wait_channel = wait_proc->infd; 4295 wait_channel = wait_proc->infd;
4226 4296
4227 record_unwind_protect (wait_reading_process_output_unwind, 4297 record_unwind_protect_int (wait_reading_process_output_unwind,
4228 make_number (waiting_for_user_input_p)); 4298 waiting_for_user_input_p);
4229 waiting_for_user_input_p = read_kbd; 4299 waiting_for_user_input_p = read_kbd;
4230 4300
4231 if (time_limit < 0) 4301 if (time_limit < 0)
@@ -4238,15 +4308,15 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4238 4308
4239 /* Since we may need to wait several times, 4309 /* Since we may need to wait several times,
4240 compute the absolute time to return at. */ 4310 compute the absolute time to return at. */
4241 if (time_limit || 0 < nsecs) 4311 if (time_limit || nsecs > 0)
4242 { 4312 {
4243 timeout = make_emacs_time (time_limit, nsecs); 4313 timeout = make_timespec (time_limit, nsecs);
4244 end_time = add_emacs_time (current_emacs_time (), timeout); 4314 end_time = timespec_add (current_timespec (), timeout);
4245 } 4315 }
4246 4316
4247 while (1) 4317 while (1)
4248 { 4318 {
4249 int timeout_reduced_for_timers = 0; 4319 bool timeout_reduced_for_timers = 0;
4250 4320
4251 /* If calling from keyboard input, do not quit 4321 /* If calling from keyboard input, do not quit
4252 since we want to return C-g as an input character. 4322 since we want to return C-g as an input character.
@@ -4260,26 +4330,26 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4260 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell))) 4330 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
4261 break; 4331 break;
4262 4332
4263 /* Compute time from now till when time limit is up */ 4333 /* Compute time from now till when time limit is up. */
4264 /* Exit if already run out */ 4334 /* Exit if already run out. */
4265 if (nsecs < 0) 4335 if (nsecs < 0)
4266 { 4336 {
4267 /* A negative timeout means 4337 /* A negative timeout means
4268 gobble output available now 4338 gobble output available now
4269 but don't wait at all. */ 4339 but don't wait at all. */
4270 4340
4271 timeout = make_emacs_time (0, 0); 4341 timeout = make_timespec (0, 0);
4272 } 4342 }
4273 else if (time_limit || 0 < nsecs) 4343 else if (time_limit || nsecs > 0)
4274 { 4344 {
4275 EMACS_TIME now = current_emacs_time (); 4345 struct timespec now = current_timespec ();
4276 if (EMACS_TIME_LE (end_time, now)) 4346 if (timespec_cmp (end_time, now) <= 0)
4277 break; 4347 break;
4278 timeout = sub_emacs_time (end_time, now); 4348 timeout = timespec_sub (end_time, now);
4279 } 4349 }
4280 else 4350 else
4281 { 4351 {
4282 timeout = make_emacs_time (100000, 0); 4352 timeout = make_timespec (100000, 0);
4283 } 4353 }
4284 4354
4285 /* Normally we run timers here. 4355 /* Normally we run timers here.
@@ -4289,7 +4359,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4289 if (NILP (wait_for_cell) 4359 if (NILP (wait_for_cell)
4290 && just_wait_proc >= 0) 4360 && just_wait_proc >= 0)
4291 { 4361 {
4292 EMACS_TIME timer_delay; 4362 struct timespec timer_delay;
4293 4363
4294 do 4364 do
4295 { 4365 {
@@ -4322,11 +4392,11 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4322 break; 4392 break;
4323 4393
4324 /* A negative timeout means do not wait at all. */ 4394 /* A negative timeout means do not wait at all. */
4325 if (0 <= nsecs) 4395 if (nsecs >= 0)
4326 { 4396 {
4327 if (EMACS_TIME_VALID_P (timer_delay)) 4397 if (timespec_valid_p (timer_delay))
4328 { 4398 {
4329 if (EMACS_TIME_LT (timer_delay, timeout)) 4399 if (timespec_cmp (timer_delay, timeout) < 0)
4330 { 4400 {
4331 timeout = timer_delay; 4401 timeout = timer_delay;
4332 timeout_reduced_for_timers = 1; 4402 timeout_reduced_for_timers = 1;
@@ -4355,8 +4425,8 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4355 timeout to get our attention. */ 4425 timeout to get our attention. */
4356 if (update_tick != process_tick) 4426 if (update_tick != process_tick)
4357 { 4427 {
4358 SELECT_TYPE Atemp; 4428 fd_set Atemp;
4359 SELECT_TYPE Ctemp; 4429 fd_set Ctemp;
4360 4430
4361 if (kbd_on_hold_p ()) 4431 if (kbd_on_hold_p ())
4362 FD_ZERO (&Atemp); 4432 FD_ZERO (&Atemp);
@@ -4364,7 +4434,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4364 Atemp = input_wait_mask; 4434 Atemp = input_wait_mask;
4365 Ctemp = write_mask; 4435 Ctemp = write_mask;
4366 4436
4367 timeout = make_emacs_time (0, 0); 4437 timeout = make_timespec (0, 0);
4368 if ((pselect (max (max_process_desc, max_input_desc) + 1, 4438 if ((pselect (max (max_process_desc, max_input_desc) + 1,
4369 &Atemp, 4439 &Atemp,
4370#ifdef NON_BLOCKING_CONNECT 4440#ifdef NON_BLOCKING_CONNECT
@@ -4391,7 +4461,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4391 && ! EQ (wait_proc->status, Qrun) 4461 && ! EQ (wait_proc->status, Qrun)
4392 && ! EQ (wait_proc->status, Qconnect)) 4462 && ! EQ (wait_proc->status, Qconnect))
4393 { 4463 {
4394 int nread, total_nread = 0; 4464 bool read_some_bytes = 0;
4395 4465
4396 clear_waiting_for_input (); 4466 clear_waiting_for_input ();
4397 XSETPROCESS (proc, wait_proc); 4467 XSETPROCESS (proc, wait_proc);
@@ -4399,16 +4469,13 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4399 /* Read data from the process, until we exhaust it. */ 4469 /* Read data from the process, until we exhaust it. */
4400 while (wait_proc->infd >= 0) 4470 while (wait_proc->infd >= 0)
4401 { 4471 {
4402 nread = read_process_output (proc, wait_proc->infd); 4472 int nread = read_process_output (proc, wait_proc->infd);
4403 4473
4404 if (nread == 0) 4474 if (nread == 0)
4405 break; 4475 break;
4406 4476
4407 if (0 < nread) 4477 if (nread > 0)
4408 { 4478 got_some_input = read_some_bytes = 1;
4409 total_nread += nread;
4410 got_some_input = 1;
4411 }
4412 else if (nread == -1 && (errno == EIO || errno == EAGAIN)) 4479 else if (nread == -1 && (errno == EIO || errno == EAGAIN))
4413 break; 4480 break;
4414#ifdef EWOULDBLOCK 4481#ifdef EWOULDBLOCK
@@ -4416,7 +4483,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4416 break; 4483 break;
4417#endif 4484#endif
4418 } 4485 }
4419 if (total_nread > 0 && do_display) 4486 if (read_some_bytes && do_display)
4420 redisplay_preserve_echo_area (10); 4487 redisplay_preserve_echo_area (10);
4421 4488
4422 break; 4489 break;
@@ -4489,8 +4556,8 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4489 Vprocess_adaptive_read_buffering is nil. */ 4556 Vprocess_adaptive_read_buffering is nil. */
4490 if (process_output_skip && check_delay > 0) 4557 if (process_output_skip && check_delay > 0)
4491 { 4558 {
4492 int nsecs = EMACS_NSECS (timeout); 4559 int nsecs = timeout.tv_nsec;
4493 if (EMACS_SECS (timeout) > 0 || nsecs > READ_OUTPUT_DELAY_MAX) 4560 if (timeout.tv_sec > 0 || nsecs > READ_OUTPUT_DELAY_MAX)
4494 nsecs = READ_OUTPUT_DELAY_MAX; 4561 nsecs = READ_OUTPUT_DELAY_MAX;
4495 for (channel = 0; check_delay > 0 && channel <= max_process_desc; channel++) 4562 for (channel = 0; check_delay > 0 && channel <= max_process_desc; channel++)
4496 { 4563 {
@@ -4510,21 +4577,21 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4510 nsecs = XPROCESS (proc)->read_output_delay; 4577 nsecs = XPROCESS (proc)->read_output_delay;
4511 } 4578 }
4512 } 4579 }
4513 timeout = make_emacs_time (0, nsecs); 4580 timeout = make_timespec (0, nsecs);
4514 process_output_skip = 0; 4581 process_output_skip = 0;
4515 } 4582 }
4516#endif 4583#endif
4517 4584
4518#if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS) 4585#if defined (HAVE_NS)
4519 nfds = xg_select 4586 nfds = ns_select
4520#elif defined (HAVE_NS) 4587#elif defined (HAVE_GLIB)
4521 nfds = ns_select 4588 nfds = xg_select
4522#else 4589#else
4523 nfds = pselect 4590 nfds = pselect
4524#endif 4591#endif
4525 (max (max_process_desc, max_input_desc) + 1, 4592 (max (max_process_desc, max_input_desc) + 1,
4526 &Available, 4593 &Available,
4527 (check_write ? &Writeok : (SELECT_TYPE *)0), 4594 (check_write ? &Writeok : 0),
4528 NULL, &timeout, NULL); 4595 NULL, &timeout, NULL);
4529 4596
4530#ifdef HAVE_GNUTLS 4597#ifdef HAVE_GNUTLS
@@ -4542,12 +4609,12 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4542 the gnutls library -- 2.12.14 has been confirmed 4609 the gnutls library -- 2.12.14 has been confirmed
4543 to need it. See 4610 to need it. See
4544 http://comments.gmane.org/gmane.emacs.devel/145074 */ 4611 http://comments.gmane.org/gmane.emacs.devel/145074 */
4545 for (channel = 0; channel < MAXDESC; ++channel) 4612 for (channel = 0; channel < FD_SETSIZE; ++channel)
4546 if (! NILP (chan_process[channel])) 4613 if (! NILP (chan_process[channel]))
4547 { 4614 {
4548 struct Lisp_Process *p = 4615 struct Lisp_Process *p =
4549 XPROCESS (chan_process[channel]); 4616 XPROCESS (chan_process[channel]);
4550 if (p && p->gnutls_p && p->infd 4617 if (p && p->gnutls_p && p->gnutls_state && p->infd
4551 && ((emacs_gnutls_record_check_pending 4618 && ((emacs_gnutls_record_check_pending
4552 (p->gnutls_state)) 4619 (p->gnutls_state))
4553 > 0)) 4620 > 0))
@@ -4561,6 +4628,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4561 { 4628 {
4562 /* Check this specific channel. */ 4629 /* Check this specific channel. */
4563 if (wait_proc->gnutls_p /* Check for valid process. */ 4630 if (wait_proc->gnutls_p /* Check for valid process. */
4631 && wait_proc->gnutls_state
4564 /* Do we have pending data? */ 4632 /* Do we have pending data? */
4565 && ((emacs_gnutls_record_check_pending 4633 && ((emacs_gnutls_record_check_pending
4566 (wait_proc->gnutls_state)) 4634 (wait_proc->gnutls_state))
@@ -4591,22 +4659,9 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4591 if (xerrno == EINTR) 4659 if (xerrno == EINTR)
4592 no_avail = 1; 4660 no_avail = 1;
4593 else if (xerrno == EBADF) 4661 else if (xerrno == EBADF)
4594 { 4662 emacs_abort ();
4595#ifdef AIX
4596 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4597 the child's closure of the pts gives the parent a SIGHUP, and
4598 the ptc file descriptor is automatically closed,
4599 yielding EBADF here or at select() call above.
4600 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4601 in m/ibmrt-aix.h), and here we just ignore the select error.
4602 Cleanup occurs c/o status_notify after SIGCHLD. */
4603 no_avail = 1; /* Cannot depend on values returned */
4604#else
4605 emacs_abort ();
4606#endif
4607 }
4608 else 4663 else
4609 error ("select error: %s", emacs_strerror (xerrno)); 4664 report_file_errno ("Failed select", Qnil, xerrno);
4610 } 4665 }
4611 4666
4612 if (no_avail) 4667 if (no_avail)
@@ -4624,7 +4679,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4624 unsigned old_timers_run = timers_run; 4679 unsigned old_timers_run = timers_run;
4625 struct buffer *old_buffer = current_buffer; 4680 struct buffer *old_buffer = current_buffer;
4626 Lisp_Object old_window = selected_window; 4681 Lisp_Object old_window = selected_window;
4627 int leave = 0; 4682 bool leave = 0;
4628 4683
4629 if (detect_input_pending_run_timers (do_display)) 4684 if (detect_input_pending_run_timers (do_display))
4630 { 4685 {
@@ -4833,7 +4888,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4833#else 4888#else
4834 { 4889 {
4835 struct sockaddr pname; 4890 struct sockaddr pname;
4836 int pnamelen = sizeof (pname); 4891 socklen_t pnamelen = sizeof (pname);
4837 4892
4838 /* If connection failed, getpeername will fail. */ 4893 /* If connection failed, getpeername will fail. */
4839 xerrno = 0; 4894 xerrno = 0;
@@ -4868,8 +4923,8 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4868 } 4923 }
4869 } 4924 }
4870#endif /* NON_BLOCKING_CONNECT */ 4925#endif /* NON_BLOCKING_CONNECT */
4871 } /* end for each file descriptor */ 4926 } /* End for each file descriptor. */
4872 } /* end while exit conditions not met */ 4927 } /* End while exit conditions not met. */
4873 4928
4874 unbind_to (count, Qnil); 4929 unbind_to (count, Qnil);
4875 4930
@@ -4904,6 +4959,11 @@ read_process_output_error_handler (Lisp_Object error_val)
4904 return Qt; 4959 return Qt;
4905} 4960}
4906 4961
4962static void
4963read_and_dispose_of_process_output (struct Lisp_Process *p, char *chars,
4964 ssize_t nbytes,
4965 struct coding_system *coding);
4966
4907/* Read pending output from the process channel, 4967/* Read pending output from the process channel,
4908 starting with our buffered-ahead character if we have one. 4968 starting with our buffered-ahead character if we have one.
4909 Yield number of decoded characters read. 4969 Yield number of decoded characters read.
@@ -4920,9 +4980,7 @@ read_process_output (Lisp_Object proc, register int channel)
4920{ 4980{
4921 register ssize_t nbytes; 4981 register ssize_t nbytes;
4922 char *chars; 4982 char *chars;
4923 register Lisp_Object outstream;
4924 register struct Lisp_Process *p = XPROCESS (proc); 4983 register struct Lisp_Process *p = XPROCESS (proc);
4925 register ptrdiff_t opoint;
4926 struct coding_system *coding = proc_decode_coding_system[channel]; 4984 struct coding_system *coding = proc_decode_coding_system[channel];
4927 int carryover = p->decoding_carryover; 4985 int carryover = p->decoding_carryover;
4928 int readmax = 4096; 4986 int readmax = 4096;
@@ -4945,14 +5003,14 @@ read_process_output (Lisp_Object proc, register int channel)
4945 else 5003 else
4946#endif 5004#endif
4947 { 5005 {
4948 int buffered = 0 <= proc_buffered_char[channel]; 5006 bool buffered = proc_buffered_char[channel] >= 0;
4949 if (buffered) 5007 if (buffered)
4950 { 5008 {
4951 chars[carryover] = proc_buffered_char[channel]; 5009 chars[carryover] = proc_buffered_char[channel];
4952 proc_buffered_char[channel] = -1; 5010 proc_buffered_char[channel] = -1;
4953 } 5011 }
4954#ifdef HAVE_GNUTLS 5012#ifdef HAVE_GNUTLS
4955 if (p->gnutls_p) 5013 if (p->gnutls_p && p->gnutls_state)
4956 nbytes = emacs_gnutls_read (p, chars + carryover + buffered, 5014 nbytes = emacs_gnutls_read (p, chars + carryover + buffered,
4957 readmax - buffered); 5015 readmax - buffered);
4958 else 5016 else
@@ -5010,122 +5068,144 @@ read_process_output (Lisp_Object proc, register int channel)
5010 friends don't expect current-buffer to be changed from under them. */ 5068 friends don't expect current-buffer to be changed from under them. */
5011 record_unwind_current_buffer (); 5069 record_unwind_current_buffer ();
5012 5070
5013 /* Read and dispose of the process output. */ 5071 read_and_dispose_of_process_output (p, chars, nbytes, coding);
5014 outstream = p->filter; 5072
5015 if (!NILP (outstream)) 5073 /* Handling the process output should not deactivate the mark. */
5016 { 5074 Vdeactivate_mark = odeactivate;
5017 Lisp_Object text; 5075
5018 bool outer_running_asynch_code = running_asynch_code; 5076 unbind_to (count, Qnil);
5019 int waiting = waiting_for_user_input_p; 5077 return nbytes;
5078}
5079
5080static void
5081read_and_dispose_of_process_output (struct Lisp_Process *p, char *chars,
5082 ssize_t nbytes,
5083 struct coding_system *coding)
5084{
5085 Lisp_Object outstream = p->filter;
5086 Lisp_Object text;
5087 bool outer_running_asynch_code = running_asynch_code;
5088 int waiting = waiting_for_user_input_p;
5020 5089
5021 /* No need to gcpro these, because all we do with them later 5090 /* No need to gcpro these, because all we do with them later
5022 is test them for EQness, and none of them should be a string. */ 5091 is test them for EQness, and none of them should be a string. */
5023#if 0 5092#if 0
5024 Lisp_Object obuffer, okeymap; 5093 Lisp_Object obuffer, okeymap;
5025 XSETBUFFER (obuffer, current_buffer); 5094 XSETBUFFER (obuffer, current_buffer);
5026 okeymap = BVAR (current_buffer, keymap); 5095 okeymap = BVAR (current_buffer, keymap);
5027#endif 5096#endif
5028 5097
5029 /* We inhibit quit here instead of just catching it so that 5098 /* We inhibit quit here instead of just catching it so that
5030 hitting ^G when a filter happens to be running won't screw 5099 hitting ^G when a filter happens to be running won't screw
5031 it up. */ 5100 it up. */
5032 specbind (Qinhibit_quit, Qt); 5101 specbind (Qinhibit_quit, Qt);
5033 specbind (Qlast_nonmenu_event, Qt); 5102 specbind (Qlast_nonmenu_event, Qt);
5034
5035 /* In case we get recursively called,
5036 and we already saved the match data nonrecursively,
5037 save the same match data in safely recursive fashion. */
5038 if (outer_running_asynch_code)
5039 {
5040 Lisp_Object tem;
5041 /* Don't clobber the CURRENT match data, either! */
5042 tem = Fmatch_data (Qnil, Qnil, Qnil);
5043 restore_search_regs ();
5044 record_unwind_save_match_data ();
5045 Fset_match_data (tem, Qt);
5046 }
5047 5103
5048 /* For speed, if a search happens within this code, 5104 /* In case we get recursively called,
5049 save the match data in a special nonrecursive fashion. */ 5105 and we already saved the match data nonrecursively,
5050 running_asynch_code = 1; 5106 save the same match data in safely recursive fashion. */
5107 if (outer_running_asynch_code)
5108 {
5109 Lisp_Object tem;
5110 /* Don't clobber the CURRENT match data, either! */
5111 tem = Fmatch_data (Qnil, Qnil, Qnil);
5112 restore_search_regs ();
5113 record_unwind_save_match_data ();
5114 Fset_match_data (tem, Qt);
5115 }
5051 5116
5052 decode_coding_c_string (coding, (unsigned char *) chars, nbytes, Qt); 5117 /* For speed, if a search happens within this code,
5053 text = coding->dst_object; 5118 save the match data in a special nonrecursive fashion. */
5054 Vlast_coding_system_used = CODING_ID_NAME (coding->id); 5119 running_asynch_code = 1;
5055 /* A new coding system might be found. */
5056 if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
5057 {
5058 pset_decode_coding_system (p, Vlast_coding_system_used);
5059 5120
5060 /* Don't call setup_coding_system for 5121 decode_coding_c_string (coding, (unsigned char *) chars, nbytes, Qt);
5061 proc_decode_coding_system[channel] here. It is done in 5122 text = coding->dst_object;
5062 detect_coding called via decode_coding above. */ 5123 Vlast_coding_system_used = CODING_ID_NAME (coding->id);
5124 /* A new coding system might be found. */
5125 if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
5126 {
5127 pset_decode_coding_system (p, Vlast_coding_system_used);
5063 5128
5064 /* If a coding system for encoding is not yet decided, we set 5129 /* Don't call setup_coding_system for
5065 it as the same as coding-system for decoding. 5130 proc_decode_coding_system[channel] here. It is done in
5131 detect_coding called via decode_coding above. */
5066 5132
5067 But, before doing that we must check if 5133 /* If a coding system for encoding is not yet decided, we set
5068 proc_encode_coding_system[p->outfd] surely points to a 5134 it as the same as coding-system for decoding.
5069 valid memory because p->outfd will be changed once EOF is
5070 sent to the process. */
5071 if (NILP (p->encode_coding_system)
5072 && proc_encode_coding_system[p->outfd])
5073 {
5074 pset_encode_coding_system
5075 (p, coding_inherit_eol_type (Vlast_coding_system_used, Qnil));
5076 setup_coding_system (p->encode_coding_system,
5077 proc_encode_coding_system[p->outfd]);
5078 }
5079 }
5080 5135
5081 if (coding->carryover_bytes > 0) 5136 But, before doing that we must check if
5137 proc_encode_coding_system[p->outfd] surely points to a
5138 valid memory because p->outfd will be changed once EOF is
5139 sent to the process. */
5140 if (NILP (p->encode_coding_system)
5141 && proc_encode_coding_system[p->outfd])
5082 { 5142 {
5083 if (SCHARS (p->decoding_buf) < coding->carryover_bytes) 5143 pset_encode_coding_system
5084 pset_decoding_buf (p, make_uninit_string (coding->carryover_bytes)); 5144 (p, coding_inherit_eol_type (Vlast_coding_system_used, Qnil));
5085 memcpy (SDATA (p->decoding_buf), coding->carryover, 5145 setup_coding_system (p->encode_coding_system,
5086 coding->carryover_bytes); 5146 proc_encode_coding_system[p->outfd]);
5087 p->decoding_carryover = coding->carryover_bytes;
5088 } 5147 }
5089 if (SBYTES (text) > 0) 5148 }
5090 /* FIXME: It's wrong to wrap or not based on debug-on-error, and 5149
5091 sometimes it's simply wrong to wrap (e.g. when called from 5150 if (coding->carryover_bytes > 0)
5092 accept-process-output). */ 5151 {
5093 internal_condition_case_1 (read_process_output_call, 5152 if (SCHARS (p->decoding_buf) < coding->carryover_bytes)
5094 Fcons (outstream, 5153 pset_decoding_buf (p, make_uninit_string (coding->carryover_bytes));
5095 Fcons (proc, Fcons (text, Qnil))), 5154 memcpy (SDATA (p->decoding_buf), coding->carryover,
5096 !NILP (Vdebug_on_error) ? Qnil : Qerror, 5155 coding->carryover_bytes);
5097 read_process_output_error_handler); 5156 p->decoding_carryover = coding->carryover_bytes;
5098 5157 }
5099 /* If we saved the match data nonrecursively, restore it now. */ 5158 if (SBYTES (text) > 0)
5100 restore_search_regs (); 5159 /* FIXME: It's wrong to wrap or not based on debug-on-error, and
5101 running_asynch_code = outer_running_asynch_code; 5160 sometimes it's simply wrong to wrap (e.g. when called from
5161 accept-process-output). */
5162 internal_condition_case_1 (read_process_output_call,
5163 list3 (outstream, make_lisp_proc (p), text),
5164 !NILP (Vdebug_on_error) ? Qnil : Qerror,
5165 read_process_output_error_handler);
5166
5167 /* If we saved the match data nonrecursively, restore it now. */
5168 restore_search_regs ();
5169 running_asynch_code = outer_running_asynch_code;
5102 5170
5103 /* Restore waiting_for_user_input_p as it was 5171 /* Restore waiting_for_user_input_p as it was
5104 when we were called, in case the filter clobbered it. */ 5172 when we were called, in case the filter clobbered it. */
5105 waiting_for_user_input_p = waiting; 5173 waiting_for_user_input_p = waiting;
5106 5174
5107#if 0 /* Call record_asynch_buffer_change unconditionally, 5175#if 0 /* Call record_asynch_buffer_change unconditionally,
5108 because we might have changed minor modes or other things 5176 because we might have changed minor modes or other things
5109 that affect key bindings. */ 5177 that affect key bindings. */
5110 if (! EQ (Fcurrent_buffer (), obuffer) 5178 if (! EQ (Fcurrent_buffer (), obuffer)
5111 || ! EQ (current_buffer->keymap, okeymap)) 5179 || ! EQ (current_buffer->keymap, okeymap))
5112#endif 5180#endif
5113 /* But do it only if the caller is actually going to read events. 5181 /* But do it only if the caller is actually going to read events.
5114 Otherwise there's no need to make him wake up, and it could 5182 Otherwise there's no need to make him wake up, and it could
5115 cause trouble (for example it would make sit_for return). */ 5183 cause trouble (for example it would make sit_for return). */
5116 if (waiting_for_user_input_p == -1) 5184 if (waiting_for_user_input_p == -1)
5117 record_asynch_buffer_change (); 5185 record_asynch_buffer_change ();
5118 } 5186}
5187
5188DEFUN ("internal-default-process-filter", Finternal_default_process_filter,
5189 Sinternal_default_process_filter, 2, 2, 0,
5190 doc: /* Function used as default process filter.
5191This inserts the process's output into its buffer, if there is one.
5192Otherwise it discards the output. */)
5193 (Lisp_Object proc, Lisp_Object text)
5194{
5195 struct Lisp_Process *p;
5196 ptrdiff_t opoint;
5197
5198 CHECK_PROCESS (proc);
5199 p = XPROCESS (proc);
5200 CHECK_STRING (text);
5119 5201
5120 /* If no filter, write into buffer if it isn't dead. */ 5202 if (!NILP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
5121 else if (!NILP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
5122 { 5203 {
5123 Lisp_Object old_read_only; 5204 Lisp_Object old_read_only;
5124 ptrdiff_t old_begv, old_zv; 5205 ptrdiff_t old_begv, old_zv;
5125 ptrdiff_t old_begv_byte, old_zv_byte; 5206 ptrdiff_t old_begv_byte, old_zv_byte;
5126 ptrdiff_t before, before_byte; 5207 ptrdiff_t before, before_byte;
5127 ptrdiff_t opoint_byte; 5208 ptrdiff_t opoint_byte;
5128 Lisp_Object text;
5129 struct buffer *b; 5209 struct buffer *b;
5130 5210
5131 Fset_buffer (p->buffer); 5211 Fset_buffer (p->buffer);
@@ -5139,15 +5219,10 @@ read_process_output (Lisp_Object proc, register int channel)
5139 5219
5140 bset_read_only (current_buffer, Qnil); 5220 bset_read_only (current_buffer, Qnil);
5141 5221
5142 /* Insert new output into buffer 5222 /* Insert new output into buffer at the current end-of-output
5143 at the current end-of-output marker, 5223 marker, thus preserving logical ordering of input and output. */
5144 thus preserving logical ordering of input and output. */
5145 if (XMARKER (p->mark)->buffer) 5224 if (XMARKER (p->mark)->buffer)
5146 SET_PT_BOTH (clip_to_bounds (BEGV, 5225 set_point_from_marker (p->mark);
5147 marker_position (p->mark), ZV),
5148 clip_to_bounds (BEGV_BYTE,
5149 marker_byte_position (p->mark),
5150 ZV_BYTE));
5151 else 5226 else
5152 SET_PT_BOTH (ZV, ZV_BYTE); 5227 SET_PT_BOTH (ZV, ZV_BYTE);
5153 before = PT; 5228 before = PT;
@@ -5158,31 +5233,6 @@ read_process_output (Lisp_Object proc, register int channel)
5158 if (! (BEGV <= PT && PT <= ZV)) 5233 if (! (BEGV <= PT && PT <= ZV))
5159 Fwiden (); 5234 Fwiden ();
5160 5235
5161 decode_coding_c_string (coding, (unsigned char *) chars, nbytes, Qt);
5162 text = coding->dst_object;
5163 Vlast_coding_system_used = CODING_ID_NAME (coding->id);
5164 /* A new coding system might be found. See the comment in the
5165 similar code in the previous `if' block. */
5166 if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
5167 {
5168 pset_decode_coding_system (p, Vlast_coding_system_used);
5169 if (NILP (p->encode_coding_system)
5170 && proc_encode_coding_system[p->outfd])
5171 {
5172 pset_encode_coding_system
5173 (p, coding_inherit_eol_type (Vlast_coding_system_used, Qnil));
5174 setup_coding_system (p->encode_coding_system,
5175 proc_encode_coding_system[p->outfd]);
5176 }
5177 }
5178 if (coding->carryover_bytes > 0)
5179 {
5180 if (SCHARS (p->decoding_buf) < coding->carryover_bytes)
5181 pset_decoding_buf (p, make_uninit_string (coding->carryover_bytes));
5182 memcpy (SDATA (p->decoding_buf), coding->carryover,
5183 coding->carryover_bytes);
5184 p->decoding_carryover = coding->carryover_bytes;
5185 }
5186 /* Adjust the multibyteness of TEXT to that of the buffer. */ 5236 /* Adjust the multibyteness of TEXT to that of the buffer. */
5187 if (NILP (BVAR (current_buffer, enable_multibyte_characters)) 5237 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
5188 != ! STRING_MULTIBYTE (text)) 5238 != ! STRING_MULTIBYTE (text))
@@ -5203,7 +5253,7 @@ read_process_output (Lisp_Object proc, register int channel)
5203 else 5253 else
5204 set_marker_both (p->mark, p->buffer, PT, PT_BYTE); 5254 set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
5205 5255
5206 update_mode_lines++; 5256 update_mode_lines = 23;
5207 5257
5208 /* Make sure opoint and the old restrictions 5258 /* Make sure opoint and the old restrictions
5209 float ahead of any new text just as point would. */ 5259 float ahead of any new text just as point would. */
@@ -5227,18 +5277,13 @@ read_process_output (Lisp_Object proc, register int channel)
5227 if (old_begv != BEGV || old_zv != ZV) 5277 if (old_begv != BEGV || old_zv != ZV)
5228 Fnarrow_to_region (make_number (old_begv), make_number (old_zv)); 5278 Fnarrow_to_region (make_number (old_begv), make_number (old_zv));
5229 5279
5230
5231 bset_read_only (current_buffer, old_read_only); 5280 bset_read_only (current_buffer, old_read_only);
5232 SET_PT_BOTH (opoint, opoint_byte); 5281 SET_PT_BOTH (opoint, opoint_byte);
5233 } 5282 }
5234 /* Handling the process output should not deactivate the mark. */ 5283 return Qnil;
5235 Vdeactivate_mark = odeactivate;
5236
5237 unbind_to (count, Qnil);
5238 return nbytes;
5239} 5284}
5240 5285
5241/* Sending data to subprocess */ 5286/* Sending data to subprocess. */
5242 5287
5243/* In send_process, when a write fails temporarily, 5288/* In send_process, when a write fails temporarily,
5244 wait_reading_process_output is called. It may execute user code, 5289 wait_reading_process_output is called. It may execute user code,
@@ -5261,7 +5306,7 @@ read_process_output (Lisp_Object proc, register int channel)
5261 5306
5262static void 5307static void
5263write_queue_push (struct Lisp_Process *p, Lisp_Object input_obj, 5308write_queue_push (struct Lisp_Process *p, Lisp_Object input_obj,
5264 const char *buf, ptrdiff_t len, int front) 5309 const char *buf, ptrdiff_t len, bool front)
5265{ 5310{
5266 ptrdiff_t offset; 5311 ptrdiff_t offset;
5267 Lisp_Object entry, obj; 5312 Lisp_Object entry, obj;
@@ -5282,14 +5327,14 @@ write_queue_push (struct Lisp_Process *p, Lisp_Object input_obj,
5282 if (front) 5327 if (front)
5283 pset_write_queue (p, Fcons (entry, p->write_queue)); 5328 pset_write_queue (p, Fcons (entry, p->write_queue));
5284 else 5329 else
5285 pset_write_queue (p, nconc2 (p->write_queue, Fcons (entry, Qnil))); 5330 pset_write_queue (p, nconc2 (p->write_queue, list1 (entry)));
5286} 5331}
5287 5332
5288/* Remove the first element in the write_queue of process P, put its 5333/* Remove the first element in the write_queue of process P, put its
5289 contents in OBJ, BUF and LEN, and return non-zero. If the 5334 contents in OBJ, BUF and LEN, and return true. If the
5290 write_queue is empty, return zero. */ 5335 write_queue is empty, return false. */
5291 5336
5292static int 5337static bool
5293write_queue_pop (struct Lisp_Process *p, Lisp_Object *obj, 5338write_queue_pop (struct Lisp_Process *p, Lisp_Object *obj,
5294 const char **buf, ptrdiff_t *len) 5339 const char **buf, ptrdiff_t *len)
5295{ 5340{
@@ -5452,20 +5497,20 @@ send_process (Lisp_Object proc, const char *buf, ptrdiff_t len,
5452 rv = sendto (outfd, cur_buf, cur_len, 5497 rv = sendto (outfd, cur_buf, cur_len,
5453 0, datagram_address[outfd].sa, 5498 0, datagram_address[outfd].sa,
5454 datagram_address[outfd].len); 5499 datagram_address[outfd].len);
5455 if (0 <= rv) 5500 if (rv >= 0)
5456 written = rv; 5501 written = rv;
5457 else if (errno == EMSGSIZE) 5502 else if (errno == EMSGSIZE)
5458 report_file_error ("sending datagram", Fcons (proc, Qnil)); 5503 report_file_error ("Sending datagram", proc);
5459 } 5504 }
5460 else 5505 else
5461#endif 5506#endif
5462 { 5507 {
5463#ifdef HAVE_GNUTLS 5508#ifdef HAVE_GNUTLS
5464 if (p->gnutls_p) 5509 if (p->gnutls_p && p->gnutls_state)
5465 written = emacs_gnutls_write (p, cur_buf, cur_len); 5510 written = emacs_gnutls_write (p, cur_buf, cur_len);
5466 else 5511 else
5467#endif 5512#endif
5468 written = emacs_write (outfd, cur_buf, cur_len); 5513 written = emacs_write_sig (outfd, cur_buf, cur_len);
5469 rv = (written ? 0 : -1); 5514 rv = (written ? 0 : -1);
5470#ifdef ADAPTIVE_READ_BUFFERING 5515#ifdef ADAPTIVE_READ_BUFFERING
5471 if (p->read_output_delay > 0 5516 if (p->read_output_delay > 0
@@ -5532,7 +5577,7 @@ send_process (Lisp_Object proc, const char *buf, ptrdiff_t len,
5532 } 5577 }
5533 else 5578 else
5534 /* This is a real error. */ 5579 /* This is a real error. */
5535 report_file_error ("writing to process", Fcons (proc, Qnil)); 5580 report_file_error ("Writing to process", proc);
5536 } 5581 }
5537 cur_buf += written; 5582 cur_buf += written;
5538 cur_len -= written; 5583 cur_len -= written;
@@ -5650,7 +5695,7 @@ return t unconditionally. */)
5650 If CURRENT_GROUP is lambda, that means send to the process group 5695 If CURRENT_GROUP is lambda, that means send to the process group
5651 that currently owns the terminal, but only if it is NOT the shell itself. 5696 that currently owns the terminal, but only if it is NOT the shell itself.
5652 5697
5653 If NOMSG is zero, insert signal-announcements into process's buffers 5698 If NOMSG is false, insert signal-announcements into process's buffers
5654 right away. 5699 right away.
5655 5700
5656 If we can, we try to signal PROCESS by sending control characters 5701 If we can, we try to signal PROCESS by sending control characters
@@ -5659,12 +5704,12 @@ return t unconditionally. */)
5659 5704
5660static void 5705static void
5661process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group, 5706process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group,
5662 int nomsg) 5707 bool nomsg)
5663{ 5708{
5664 Lisp_Object proc; 5709 Lisp_Object proc;
5665 register struct Lisp_Process *p; 5710 struct Lisp_Process *p;
5666 pid_t gid; 5711 pid_t gid;
5667 int no_pgrp = 0; 5712 bool no_pgrp = 0;
5668 5713
5669 proc = get_process (process); 5714 proc = get_process (process);
5670 p = XPROCESS (proc); 5715 p = XPROCESS (proc);
@@ -5758,10 +5803,9 @@ process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group,
5758 return; 5803 return;
5759 } 5804 }
5760 5805
5761 switch (signo)
5762 {
5763#ifdef SIGCONT 5806#ifdef SIGCONT
5764 case SIGCONT: 5807 if (signo == SIGCONT)
5808 {
5765 p->raw_status_new = 0; 5809 p->raw_status_new = 0;
5766 pset_status (p, Qrun); 5810 pset_status (p, Qrun);
5767 p->tick = ++process_tick; 5811 p->tick = ++process_tick;
@@ -5770,14 +5814,8 @@ process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group,
5770 status_notify (NULL); 5814 status_notify (NULL);
5771 redisplay_preserve_echo_area (13); 5815 redisplay_preserve_echo_area (13);
5772 } 5816 }
5773 break;
5774#endif /* ! defined (SIGCONT) */
5775 case SIGINT:
5776 case SIGQUIT:
5777 case SIGKILL:
5778 flush_pending_output (p->infd);
5779 break;
5780 } 5817 }
5818#endif
5781 5819
5782 /* If we don't have process groups, send the signal to the immediate 5820 /* If we don't have process groups, send the signal to the immediate
5783 subprocess. That isn't really right, but it's better than any 5821 subprocess. That isn't really right, but it's better than any
@@ -6000,13 +6038,16 @@ process has been transmitted to the serial port. */)
6000 (Lisp_Object process) 6038 (Lisp_Object process)
6001{ 6039{
6002 Lisp_Object proc; 6040 Lisp_Object proc;
6003 struct coding_system *coding; 6041 struct coding_system *coding = NULL;
6042 int outfd;
6004 6043
6005 if (DATAGRAM_CONN_P (process)) 6044 if (DATAGRAM_CONN_P (process))
6006 return process; 6045 return process;
6007 6046
6008 proc = get_process (process); 6047 proc = get_process (process);
6009 coding = proc_encode_coding_system[XPROCESS (proc)->outfd]; 6048 outfd = XPROCESS (proc)->outfd;
6049 if (outfd >= 0)
6050 coding = proc_encode_coding_system[outfd];
6010 6051
6011 /* Make sure the process is really alive. */ 6052 /* Make sure the process is really alive. */
6012 if (XPROCESS (proc)->raw_status_new) 6053 if (XPROCESS (proc)->raw_status_new)
@@ -6014,7 +6055,7 @@ process has been transmitted to the serial port. */)
6014 if (! EQ (XPROCESS (proc)->status, Qrun)) 6055 if (! EQ (XPROCESS (proc)->status, Qrun))
6015 error ("Process %s not running", SDATA (XPROCESS (proc)->name)); 6056 error ("Process %s not running", SDATA (XPROCESS (proc)->name));
6016 6057
6017 if (CODING_REQUIRE_FLUSHING (coding)) 6058 if (coding && CODING_REQUIRE_FLUSHING (coding))
6018 { 6059 {
6019 coding->mode |= CODING_MODE_LAST_BLOCK; 6060 coding->mode |= CODING_MODE_LAST_BLOCK;
6020 send_process (proc, "", 0, Qnil); 6061 send_process (proc, "", 0, Qnil);
@@ -6026,42 +6067,45 @@ process has been transmitted to the serial port. */)
6026 { 6067 {
6027#ifndef WINDOWSNT 6068#ifndef WINDOWSNT
6028 if (tcdrain (XPROCESS (proc)->outfd) != 0) 6069 if (tcdrain (XPROCESS (proc)->outfd) != 0)
6029 error ("tcdrain() failed: %s", emacs_strerror (errno)); 6070 report_file_error ("Failed tcdrain", Qnil);
6030#endif /* not WINDOWSNT */ 6071#endif /* not WINDOWSNT */
6031 /* Do nothing on Windows because writes are blocking. */ 6072 /* Do nothing on Windows because writes are blocking. */
6032 } 6073 }
6033 else 6074 else
6034 { 6075 {
6035 int old_outfd, new_outfd; 6076 struct Lisp_Process *p = XPROCESS (proc);
6077 int old_outfd = p->outfd;
6078 int new_outfd;
6036 6079
6037#ifdef HAVE_SHUTDOWN 6080#ifdef HAVE_SHUTDOWN
6038 /* If this is a network connection, or socketpair is used 6081 /* If this is a network connection, or socketpair is used
6039 for communication with the subprocess, call shutdown to cause EOF. 6082 for communication with the subprocess, call shutdown to cause EOF.
6040 (In some old system, shutdown to socketpair doesn't work. 6083 (In some old system, shutdown to socketpair doesn't work.
6041 Then we just can't win.) */ 6084 Then we just can't win.) */
6042 if (EQ (XPROCESS (proc)->type, Qnetwork) 6085 if (EQ (p->type, Qnetwork)
6043 || XPROCESS (proc)->outfd == XPROCESS (proc)->infd) 6086 || p->infd == old_outfd)
6044 shutdown (XPROCESS (proc)->outfd, 1); 6087 shutdown (old_outfd, 1);
6045 /* In case of socketpair, outfd == infd, so don't close it. */ 6088#endif
6046 if (XPROCESS (proc)->outfd != XPROCESS (proc)->infd) 6089 close_process_fd (&p->open_fd[WRITE_TO_SUBPROCESS]);
6047 emacs_close (XPROCESS (proc)->outfd);
6048#else /* not HAVE_SHUTDOWN */
6049 emacs_close (XPROCESS (proc)->outfd);
6050#endif /* not HAVE_SHUTDOWN */
6051 new_outfd = emacs_open (NULL_DEVICE, O_WRONLY, 0); 6090 new_outfd = emacs_open (NULL_DEVICE, O_WRONLY, 0);
6052 if (new_outfd < 0) 6091 if (new_outfd < 0)
6053 emacs_abort (); 6092 report_file_error ("Opening null device", Qnil);
6054 old_outfd = XPROCESS (proc)->outfd; 6093 p->open_fd[WRITE_TO_SUBPROCESS] = new_outfd;
6094 p->outfd = new_outfd;
6055 6095
6056 if (!proc_encode_coding_system[new_outfd]) 6096 if (!proc_encode_coding_system[new_outfd])
6057 proc_encode_coding_system[new_outfd] 6097 proc_encode_coding_system[new_outfd]
6058 = xmalloc (sizeof (struct coding_system)); 6098 = xmalloc (sizeof (struct coding_system));
6059 *proc_encode_coding_system[new_outfd] 6099 if (old_outfd >= 0)
6060 = *proc_encode_coding_system[old_outfd]; 6100 {
6061 memset (proc_encode_coding_system[old_outfd], 0, 6101 *proc_encode_coding_system[new_outfd]
6062 sizeof (struct coding_system)); 6102 = *proc_encode_coding_system[old_outfd];
6063 6103 memset (proc_encode_coding_system[old_outfd], 0,
6064 XPROCESS (proc)->outfd = new_outfd; 6104 sizeof (struct coding_system));
6105 }
6106 else
6107 setup_coding_system (p->encode_coding_system,
6108 proc_encode_coding_system[new_outfd]);
6065 } 6109 }
6066 return process; 6110 return process;
6067} 6111}
@@ -6092,6 +6136,13 @@ process has been transmitted to the serial port. */)
6092 might inadvertently reap a GTK-created process that happened to 6136 might inadvertently reap a GTK-created process that happened to
6093 have the same process ID. */ 6137 have the same process ID. */
6094 6138
6139/* LIB_CHILD_HANDLER is a SIGCHLD handler that Emacs calls while doing
6140 its own SIGCHLD handling. On POSIXish systems, glib needs this to
6141 keep track of its own children. GNUstep is similar. */
6142
6143static void dummy_handler (int sig) {}
6144static signal_handler_t volatile lib_child_handler;
6145
6095/* Handle a SIGCHLD signal by looking for known child processes of 6146/* Handle a SIGCHLD signal by looking for known child processes of
6096 Emacs whose status have changed. For each one found, record its 6147 Emacs whose status have changed. For each one found, record its
6097 new status. 6148 new status.
@@ -6120,7 +6171,7 @@ process has been transmitted to the serial port. */)
6120static void 6171static void
6121handle_child_signal (int sig) 6172handle_child_signal (int sig)
6122{ 6173{
6123 Lisp_Object tail; 6174 Lisp_Object tail, proc;
6124 6175
6125 /* Find the process that signaled us, and record its status. */ 6176 /* Find the process that signaled us, and record its status. */
6126 6177
@@ -6131,7 +6182,11 @@ handle_child_signal (int sig)
6131 bool all_pids_are_fixnums 6182 bool all_pids_are_fixnums
6132 = (MOST_NEGATIVE_FIXNUM <= TYPE_MINIMUM (pid_t) 6183 = (MOST_NEGATIVE_FIXNUM <= TYPE_MINIMUM (pid_t)
6133 && TYPE_MAXIMUM (pid_t) <= MOST_POSITIVE_FIXNUM); 6184 && TYPE_MAXIMUM (pid_t) <= MOST_POSITIVE_FIXNUM);
6134 Lisp_Object xpid = XCAR (tail); 6185 Lisp_Object head = XCAR (tail);
6186 Lisp_Object xpid;
6187 if (! CONSP (head))
6188 continue;
6189 xpid = XCAR (head);
6135 if (all_pids_are_fixnums ? INTEGERP (xpid) : NUMBERP (xpid)) 6190 if (all_pids_are_fixnums ? INTEGERP (xpid) : NUMBERP (xpid))
6136 { 6191 {
6137 pid_t deleted_pid; 6192 pid_t deleted_pid;
@@ -6140,18 +6195,22 @@ handle_child_signal (int sig)
6140 else 6195 else
6141 deleted_pid = XFLOAT_DATA (xpid); 6196 deleted_pid = XFLOAT_DATA (xpid);
6142 if (child_status_changed (deleted_pid, 0, 0)) 6197 if (child_status_changed (deleted_pid, 0, 0))
6143 XSETCAR (tail, Qnil); 6198 {
6199 if (STRINGP (XCDR (head)))
6200 unlink (SSDATA (XCDR (head)));
6201 XSETCAR (tail, Qnil);
6202 }
6144 } 6203 }
6145 } 6204 }
6146 6205
6147 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */ 6206 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6148 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) 6207 FOR_EACH_PROCESS (tail, proc)
6149 { 6208 {
6150 Lisp_Object proc = XCDR (XCAR (tail));
6151 struct Lisp_Process *p = XPROCESS (proc); 6209 struct Lisp_Process *p = XPROCESS (proc);
6152 int status; 6210 int status;
6153 6211
6154 if (p->alive && child_status_changed (p->pid, &status, WUNTRACED)) 6212 if (p->alive
6213 && child_status_changed (p->pid, &status, WUNTRACED | WCONTINUED))
6155 { 6214 {
6156 /* Change the status of the process that was found. */ 6215 /* Change the status of the process that was found. */
6157 p->tick = ++process_tick; 6216 p->tick = ++process_tick;
@@ -6161,7 +6220,7 @@ handle_child_signal (int sig)
6161 /* If process has terminated, stop waiting for its output. */ 6220 /* If process has terminated, stop waiting for its output. */
6162 if (WIFSIGNALED (status) || WIFEXITED (status)) 6221 if (WIFSIGNALED (status) || WIFEXITED (status))
6163 { 6222 {
6164 int clear_desc_flag = 0; 6223 bool clear_desc_flag = 0;
6165 p->alive = 0; 6224 p->alive = 0;
6166 if (p->infd >= 0) 6225 if (p->infd >= 0)
6167 clear_desc_flag = 1; 6226 clear_desc_flag = 1;
@@ -6175,6 +6234,13 @@ handle_child_signal (int sig)
6175 } 6234 }
6176 } 6235 }
6177 } 6236 }
6237
6238 lib_child_handler (sig);
6239#ifdef NS_IMPL_GNUSTEP
6240 /* NSTask in GNUstep sets its child handler each time it is called.
6241 So we must re-set ours. */
6242 catch_child_signal();
6243#endif
6178} 6244}
6179 6245
6180static void 6246static void
@@ -6185,13 +6251,6 @@ deliver_child_signal (int sig)
6185 6251
6186 6252
6187static Lisp_Object 6253static Lisp_Object
6188exec_sentinel_unwind (Lisp_Object data)
6189{
6190 pset_sentinel (XPROCESS (XCAR (data)), XCDR (data));
6191 return Qnil;
6192}
6193
6194static Lisp_Object
6195exec_sentinel_error_handler (Lisp_Object error_val) 6254exec_sentinel_error_handler (Lisp_Object error_val)
6196{ 6255{
6197 cmd_error_internal (error_val, "error in process sentinel: "); 6256 cmd_error_internal (error_val, "error in process sentinel: ");
@@ -6228,13 +6287,7 @@ exec_sentinel (Lisp_Object proc, Lisp_Object reason)
6228 record_unwind_current_buffer (); 6287 record_unwind_current_buffer ();
6229 6288
6230 sentinel = p->sentinel; 6289 sentinel = p->sentinel;
6231 if (NILP (sentinel))
6232 return;
6233 6290
6234 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6235 assure that it gets restored no matter how the sentinel exits. */
6236 pset_sentinel (p, Qnil);
6237 record_unwind_protect (exec_sentinel_unwind, Fcons (proc, sentinel));
6238 /* Inhibit quit so that random quits don't screw up a running filter. */ 6291 /* Inhibit quit so that random quits don't screw up a running filter. */
6239 specbind (Qinhibit_quit, Qt); 6292 specbind (Qinhibit_quit, Qt);
6240 specbind (Qlast_nonmenu_event, Qt); /* Why? --Stef */ 6293 specbind (Qlast_nonmenu_event, Qt); /* Why? --Stef */
@@ -6256,8 +6309,7 @@ exec_sentinel (Lisp_Object proc, Lisp_Object reason)
6256 running_asynch_code = 1; 6309 running_asynch_code = 1;
6257 6310
6258 internal_condition_case_1 (read_process_output_call, 6311 internal_condition_case_1 (read_process_output_call,
6259 Fcons (sentinel, 6312 list3 (sentinel, proc, reason),
6260 Fcons (proc, Fcons (reason, Qnil))),
6261 !NILP (Vdebug_on_error) ? Qnil : Qerror, 6313 !NILP (Vdebug_on_error) ? Qnil : Qerror,
6262 exec_sentinel_error_handler); 6314 exec_sentinel_error_handler);
6263 6315
@@ -6292,7 +6344,7 @@ exec_sentinel (Lisp_Object proc, Lisp_Object reason)
6292static void 6344static void
6293status_notify (struct Lisp_Process *deleting_process) 6345status_notify (struct Lisp_Process *deleting_process)
6294{ 6346{
6295 register Lisp_Object proc, buffer; 6347 register Lisp_Object proc;
6296 Lisp_Object tail, msg; 6348 Lisp_Object tail, msg;
6297 struct gcpro gcpro1, gcpro2; 6349 struct gcpro gcpro1, gcpro2;
6298 6350
@@ -6308,13 +6360,10 @@ status_notify (struct Lisp_Process *deleting_process)
6308 that we run, we get called again to handle their status changes. */ 6360 that we run, we get called again to handle their status changes. */
6309 update_tick = process_tick; 6361 update_tick = process_tick;
6310 6362
6311 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) 6363 FOR_EACH_PROCESS (tail, proc)
6312 { 6364 {
6313 Lisp_Object symbol; 6365 Lisp_Object symbol;
6314 register struct Lisp_Process *p; 6366 register struct Lisp_Process *p = XPROCESS (proc);
6315
6316 proc = Fcdr (XCAR (tail));
6317 p = XPROCESS (proc);
6318 6367
6319 if (p->tick != p->update_tick) 6368 if (p->tick != p->update_tick)
6320 { 6369 {
@@ -6330,8 +6379,6 @@ status_notify (struct Lisp_Process *deleting_process)
6330 && p != deleting_process 6379 && p != deleting_process
6331 && read_process_output (proc, p->infd) > 0); 6380 && read_process_output (proc, p->infd) > 0);
6332 6381
6333 buffer = p->buffer;
6334
6335 /* Get the text to use for the message. */ 6382 /* Get the text to use for the message. */
6336 if (p->raw_status_new) 6383 if (p->raw_status_new)
6337 update_status (p); 6384 update_status (p);
@@ -6352,66 +6399,84 @@ status_notify (struct Lisp_Process *deleting_process)
6352 } 6399 }
6353 6400
6354 /* The actions above may have further incremented p->tick. 6401 /* The actions above may have further incremented p->tick.
6355 So set p->update_tick again 6402 So set p->update_tick again so that an error in the sentinel will
6356 so that an error in the sentinel will not cause 6403 not cause this code to be run again. */
6357 this code to be run again. */
6358 p->update_tick = p->tick; 6404 p->update_tick = p->tick;
6359 /* Now output the message suitably. */ 6405 /* Now output the message suitably. */
6360 if (!NILP (p->sentinel)) 6406 exec_sentinel (proc, msg);
6361 exec_sentinel (proc, msg);
6362 /* Don't bother with a message in the buffer
6363 when a process becomes runnable. */
6364 else if (!EQ (symbol, Qrun) && !NILP (buffer))
6365 {
6366 Lisp_Object tem;
6367 struct buffer *old = current_buffer;
6368 ptrdiff_t opoint, opoint_byte;
6369 ptrdiff_t before, before_byte;
6370
6371 /* Avoid error if buffer is deleted
6372 (probably that's why the process is dead, too) */
6373 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
6374 continue;
6375 Fset_buffer (buffer);
6376
6377 opoint = PT;
6378 opoint_byte = PT_BYTE;
6379 /* Insert new output into buffer
6380 at the current end-of-output marker,
6381 thus preserving logical ordering of input and output. */
6382 if (XMARKER (p->mark)->buffer)
6383 Fgoto_char (p->mark);
6384 else
6385 SET_PT_BOTH (ZV, ZV_BYTE);
6386
6387 before = PT;
6388 before_byte = PT_BYTE;
6389
6390 tem = BVAR (current_buffer, read_only);
6391 bset_read_only (current_buffer, Qnil);
6392 insert_string ("\nProcess ");
6393 { /* FIXME: temporary kludge */
6394 Lisp_Object tem2 = p->name; Finsert (1, &tem2); }
6395 insert_string (" ");
6396 Finsert (1, &msg);
6397 bset_read_only (current_buffer, tem);
6398 set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
6399
6400 if (opoint >= before)
6401 SET_PT_BOTH (opoint + (PT - before),
6402 opoint_byte + (PT_BYTE - before_byte));
6403 else
6404 SET_PT_BOTH (opoint, opoint_byte);
6405
6406 set_buffer_internal (old);
6407 }
6408 } 6407 }
6409 } /* end for */ 6408 } /* end for */
6410 6409
6411 update_mode_lines++; /* in case buffers use %s in mode-line-format */ 6410 update_mode_lines = 24; /* In case buffers use %s in mode-line-format. */
6412 UNGCPRO; 6411 UNGCPRO;
6413} 6412}
6414 6413
6414DEFUN ("internal-default-process-sentinel", Finternal_default_process_sentinel,
6415 Sinternal_default_process_sentinel, 2, 2, 0,
6416 doc: /* Function used as default sentinel for processes.
6417This inserts a status message into the process's buffer, if there is one. */)
6418 (Lisp_Object proc, Lisp_Object msg)
6419{
6420 Lisp_Object buffer, symbol;
6421 struct Lisp_Process *p;
6422 CHECK_PROCESS (proc);
6423 p = XPROCESS (proc);
6424 buffer = p->buffer;
6425 symbol = p->status;
6426 if (CONSP (symbol))
6427 symbol = XCAR (symbol);
6428
6429 if (!EQ (symbol, Qrun) && !NILP (buffer))
6430 {
6431 Lisp_Object tem;
6432 struct buffer *old = current_buffer;
6433 ptrdiff_t opoint, opoint_byte;
6434 ptrdiff_t before, before_byte;
6435
6436 /* Avoid error if buffer is deleted
6437 (probably that's why the process is dead, too). */
6438 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
6439 return Qnil;
6440 Fset_buffer (buffer);
6441
6442 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
6443 msg = (code_convert_string_norecord
6444 (msg, Vlocale_coding_system, 1));
6445
6446 opoint = PT;
6447 opoint_byte = PT_BYTE;
6448 /* Insert new output into buffer
6449 at the current end-of-output marker,
6450 thus preserving logical ordering of input and output. */
6451 if (XMARKER (p->mark)->buffer)
6452 Fgoto_char (p->mark);
6453 else
6454 SET_PT_BOTH (ZV, ZV_BYTE);
6455
6456 before = PT;
6457 before_byte = PT_BYTE;
6458
6459 tem = BVAR (current_buffer, read_only);
6460 bset_read_only (current_buffer, Qnil);
6461 insert_string ("\nProcess ");
6462 { /* FIXME: temporary kludge. */
6463 Lisp_Object tem2 = p->name; Finsert (1, &tem2); }
6464 insert_string (" ");
6465 Finsert (1, &msg);
6466 bset_read_only (current_buffer, tem);
6467 set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
6468
6469 if (opoint >= before)
6470 SET_PT_BOTH (opoint + (PT - before),
6471 opoint_byte + (PT_BYTE - before_byte));
6472 else
6473 SET_PT_BOTH (opoint, opoint_byte);
6474
6475 set_buffer_internal (old);
6476 }
6477 return Qnil;
6478}
6479
6415 6480
6416DEFUN ("set-process-coding-system", Fset_process_coding_system, 6481DEFUN ("set-process-coding-system", Fset_process_coding_system,
6417 Sset_process_coding_system, 1, 3, 0, 6482 Sset_process_coding_system, 1, 3, 0,
@@ -6504,10 +6569,10 @@ delete_gpm_wait_descriptor (int desc)
6504 6569
6505# ifdef USABLE_SIGIO 6570# ifdef USABLE_SIGIO
6506 6571
6507/* Return nonzero if *MASK has a bit set 6572/* Return true if *MASK has a bit set
6508 that corresponds to one of the keyboard input descriptors. */ 6573 that corresponds to one of the keyboard input descriptors. */
6509 6574
6510static int 6575static bool
6511keyboard_bit_set (fd_set *mask) 6576keyboard_bit_set (fd_set *mask)
6512{ 6577{
6513 int fd; 6578 int fd;
@@ -6524,8 +6589,8 @@ keyboard_bit_set (fd_set *mask)
6524#else /* not subprocesses */ 6589#else /* not subprocesses */
6525 6590
6526/* Defined on msdos.c. */ 6591/* Defined on msdos.c. */
6527extern int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *, 6592extern int sys_select (int, fd_set *, fd_set *, fd_set *,
6528 EMACS_TIME *, void *); 6593 struct timespec *, void *);
6529 6594
6530/* Implementation of wait_reading_process_output, assuming that there 6595/* Implementation of wait_reading_process_output, assuming that there
6531 are no subprocesses. Used only by the MS-DOS build. 6596 are no subprocesses. Used only by the MS-DOS build.
@@ -6557,14 +6622,14 @@ extern int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *,
6557 6622
6558 Return true if we received input from any process. */ 6623 Return true if we received input from any process. */
6559 6624
6560int 6625bool
6561wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, 6626wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6562 bool do_display, 6627 bool do_display,
6563 Lisp_Object wait_for_cell, 6628 Lisp_Object wait_for_cell,
6564 struct Lisp_Process *wait_proc, int just_wait_proc) 6629 struct Lisp_Process *wait_proc, int just_wait_proc)
6565{ 6630{
6566 register int nfds; 6631 register int nfds;
6567 EMACS_TIME end_time, timeout; 6632 struct timespec end_time, timeout;
6568 6633
6569 if (time_limit < 0) 6634 if (time_limit < 0)
6570 { 6635 {
@@ -6575,10 +6640,10 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6575 time_limit = TYPE_MAXIMUM (time_t); 6640 time_limit = TYPE_MAXIMUM (time_t);
6576 6641
6577 /* What does time_limit really mean? */ 6642 /* What does time_limit really mean? */
6578 if (time_limit || 0 < nsecs) 6643 if (time_limit || nsecs > 0)
6579 { 6644 {
6580 timeout = make_emacs_time (time_limit, nsecs); 6645 timeout = make_timespec (time_limit, nsecs);
6581 end_time = add_emacs_time (current_emacs_time (), timeout); 6646 end_time = timespec_add (current_timespec (), timeout);
6582 } 6647 }
6583 6648
6584 /* Turn off periodic alarms (in case they are in use) 6649 /* Turn off periodic alarms (in case they are in use)
@@ -6589,8 +6654,8 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6589 6654
6590 while (1) 6655 while (1)
6591 { 6656 {
6592 int timeout_reduced_for_timers = 0; 6657 bool timeout_reduced_for_timers = 0;
6593 SELECT_TYPE waitchannels; 6658 fd_set waitchannels;
6594 int xerrno; 6659 int xerrno;
6595 6660
6596 /* If calling from keyboard input, do not quit 6661 /* If calling from keyboard input, do not quit
@@ -6603,26 +6668,26 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6603 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell))) 6668 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
6604 break; 6669 break;
6605 6670
6606 /* Compute time from now till when time limit is up */ 6671 /* Compute time from now till when time limit is up. */
6607 /* Exit if already run out */ 6672 /* Exit if already run out. */
6608 if (nsecs < 0) 6673 if (nsecs < 0)
6609 { 6674 {
6610 /* A negative timeout means 6675 /* A negative timeout means
6611 gobble output available now 6676 gobble output available now
6612 but don't wait at all. */ 6677 but don't wait at all. */
6613 6678
6614 timeout = make_emacs_time (0, 0); 6679 timeout = make_timespec (0, 0);
6615 } 6680 }
6616 else if (time_limit || 0 < nsecs) 6681 else if (time_limit || nsecs > 0)
6617 { 6682 {
6618 EMACS_TIME now = current_emacs_time (); 6683 struct timespec now = current_timespec ();
6619 if (EMACS_TIME_LE (end_time, now)) 6684 if (timespec_cmp (end_time, now) <= 0)
6620 break; 6685 break;
6621 timeout = sub_emacs_time (end_time, now); 6686 timeout = timespec_sub (end_time, now);
6622 } 6687 }
6623 else 6688 else
6624 { 6689 {
6625 timeout = make_emacs_time (100000, 0); 6690 timeout = make_timespec (100000, 0);
6626 } 6691 }
6627 6692
6628 /* If our caller will not immediately handle keyboard events, 6693 /* If our caller will not immediately handle keyboard events,
@@ -6631,7 +6696,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6631 call timer_delay on their own.) */ 6696 call timer_delay on their own.) */
6632 if (NILP (wait_for_cell)) 6697 if (NILP (wait_for_cell))
6633 { 6698 {
6634 EMACS_TIME timer_delay; 6699 struct timespec timer_delay;
6635 6700
6636 do 6701 do
6637 { 6702 {
@@ -6651,9 +6716,9 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6651 && requeued_events_pending_p ()) 6716 && requeued_events_pending_p ())
6652 break; 6717 break;
6653 6718
6654 if (EMACS_TIME_VALID_P (timer_delay) && 0 <= nsecs) 6719 if (timespec_valid_p (timer_delay) && nsecs >= 0)
6655 { 6720 {
6656 if (EMACS_TIME_LT (timer_delay, timeout)) 6721 if (timespec_cmp (timer_delay, timeout) < 0)
6657 { 6722 {
6658 timeout = timer_delay; 6723 timeout = timer_delay;
6659 timeout_reduced_for_timers = 1; 6724 timeout_reduced_for_timers = 1;
@@ -6706,7 +6771,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6706 if (xerrno == EINTR) 6771 if (xerrno == EINTR)
6707 FD_ZERO (&waitchannels); 6772 FD_ZERO (&waitchannels);
6708 else 6773 else
6709 error ("select error: %s", emacs_strerror (xerrno)); 6774 report_file_errno ("Failed select", Qnil, xerrno);
6710 } 6775 }
6711 6776
6712 /* Check for keyboard input */ 6777 /* Check for keyboard input */
@@ -6772,16 +6837,9 @@ void
6772delete_keyboard_wait_descriptor (int desc) 6837delete_keyboard_wait_descriptor (int desc)
6773{ 6838{
6774#ifdef subprocesses 6839#ifdef subprocesses
6775 int fd;
6776 int lim = max_input_desc;
6777
6778 FD_CLR (desc, &input_wait_mask); 6840 FD_CLR (desc, &input_wait_mask);
6779 FD_CLR (desc, &non_process_wait_mask); 6841 FD_CLR (desc, &non_process_wait_mask);
6780 6842 delete_input_desc (desc);
6781 if (desc == max_input_desc)
6782 for (fd = 0; fd < lim; fd++)
6783 if (FD_ISSET (fd, &input_wait_mask) || FD_ISSET (fd, &write_mask))
6784 max_input_desc = fd;
6785#endif 6843#endif
6786} 6844}
6787 6845
@@ -6802,9 +6860,8 @@ setup_process_coding_systems (Lisp_Object process)
6802 if (!proc_decode_coding_system[inch]) 6860 if (!proc_decode_coding_system[inch])
6803 proc_decode_coding_system[inch] = xmalloc (sizeof (struct coding_system)); 6861 proc_decode_coding_system[inch] = xmalloc (sizeof (struct coding_system));
6804 coding_system = p->decode_coding_system; 6862 coding_system = p->decode_coding_system;
6805 if (! NILP (p->filter)) 6863 if (EQ (p->filter, Qinternal_default_process_filter)
6806 ; 6864 && BUFFERP (p->buffer))
6807 else if (BUFFERP (p->buffer))
6808 { 6865 {
6809 if (NILP (BVAR (XBUFFER (p->buffer), enable_multibyte_characters))) 6866 if (NILP (BVAR (XBUFFER (p->buffer), enable_multibyte_characters)))
6810 coding_system = raw_text_coding_system (coding_system); 6867 coding_system = raw_text_coding_system (coding_system);
@@ -6818,32 +6875,6 @@ setup_process_coding_systems (Lisp_Object process)
6818#endif 6875#endif
6819} 6876}
6820 6877
6821/* Close all descriptors currently in use for communication
6822 with subprocess. This is used in a newly-forked subprocess
6823 to get rid of irrelevant descriptors. */
6824
6825void
6826close_process_descs (void)
6827{
6828#ifndef DOS_NT
6829 int i;
6830 for (i = 0; i < MAXDESC; i++)
6831 {
6832 Lisp_Object process;
6833 process = chan_process[i];
6834 if (!NILP (process))
6835 {
6836 int in = XPROCESS (process)->infd;
6837 int out = XPROCESS (process)->outfd;
6838 if (in >= 0)
6839 emacs_close (in);
6840 if (out >= 0 && in != out)
6841 emacs_close (out);
6842 }
6843 }
6844#endif
6845}
6846
6847DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0, 6878DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
6848 doc: /* Return the (or a) process associated with BUFFER. 6879 doc: /* Return the (or a) process associated with BUFFER.
6849BUFFER may be a buffer or the name of one. */) 6880BUFFER may be a buffer or the name of one. */)
@@ -6856,12 +6887,9 @@ BUFFER may be a buffer or the name of one. */)
6856 buf = Fget_buffer (buffer); 6887 buf = Fget_buffer (buffer);
6857 if (NILP (buf)) return Qnil; 6888 if (NILP (buf)) return Qnil;
6858 6889
6859 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) 6890 FOR_EACH_PROCESS (tail, proc)
6860 { 6891 if (EQ (XPROCESS (proc)->buffer, buf))
6861 proc = Fcdr (XCAR (tail)); 6892 return proc;
6862 if (PROCESSP (proc) && EQ (XPROCESS (proc)->buffer, buf))
6863 return proc;
6864 }
6865#endif /* subprocesses */ 6893#endif /* subprocesses */
6866 return Qnil; 6894 return Qnil;
6867} 6895}
@@ -6894,18 +6922,14 @@ kill_buffer_processes (Lisp_Object buffer)
6894#ifdef subprocesses 6922#ifdef subprocesses
6895 Lisp_Object tail, proc; 6923 Lisp_Object tail, proc;
6896 6924
6897 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) 6925 FOR_EACH_PROCESS (tail, proc)
6898 { 6926 if (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer))
6899 proc = XCDR (XCAR (tail)); 6927 {
6900 if (PROCESSP (proc) 6928 if (NETCONN_P (proc) || SERIALCONN_P (proc))
6901 && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer))) 6929 Fdelete_process (proc);
6902 { 6930 else if (XPROCESS (proc)->infd >= 0)
6903 if (NETCONN_P (proc) || SERIALCONN_P (proc)) 6931 process_send_signal (proc, SIGHUP, Qnil, 1);
6904 Fdelete_process (proc); 6932 }
6905 else if (XPROCESS (proc)->infd >= 0)
6906 process_send_signal (proc, SIGHUP, Qnil, 1);
6907 }
6908 }
6909#else /* subprocesses */ 6933#else /* subprocesses */
6910 /* Since we have no subprocesses, this does nothing. */ 6934 /* Since we have no subprocesses, this does nothing. */
6911#endif /* subprocesses */ 6935#endif /* subprocesses */
@@ -6913,7 +6937,7 @@ kill_buffer_processes (Lisp_Object buffer)
6913 6937
6914DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, 6938DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p,
6915 Swaiting_for_user_input_p, 0, 0, 0, 6939 Swaiting_for_user_input_p, 0, 0, 0,
6916 doc: /* Returns non-nil if Emacs is waiting for input from the user. 6940 doc: /* Return non-nil if Emacs is waiting for input from the user.
6917This is intended for use by asynchronous process output filters and sentinels. */) 6941This is intended for use by asynchronous process output filters and sentinels. */)
6918 (void) 6942 (void)
6919{ 6943{
@@ -6940,9 +6964,9 @@ unhold_keyboard_input (void)
6940 kbd_is_on_hold = 0; 6964 kbd_is_on_hold = 0;
6941} 6965}
6942 6966
6943/* Return non-zero if keyboard input is on hold, zero otherwise. */ 6967/* Return true if keyboard input is on hold, zero otherwise. */
6944 6968
6945int 6969bool
6946kbd_on_hold_p (void) 6970kbd_on_hold_p (void)
6947{ 6971{
6948 return kbd_is_on_hold; 6972 return kbd_is_on_hold;
@@ -7019,6 +7043,31 @@ integer or floating point values.
7019 return system_process_attributes (pid); 7043 return system_process_attributes (pid);
7020} 7044}
7021 7045
7046/* Arrange to catch SIGCHLD if this hasn't already been arranged.
7047 Invoke this after init_process_emacs, and after glib and/or GNUstep
7048 futz with the SIGCHLD handler, but before Emacs forks any children.
7049 This function's caller should block SIGCHLD. */
7050
7051#ifndef NS_IMPL_GNUSTEP
7052static
7053#endif
7054void
7055catch_child_signal (void)
7056{
7057 struct sigaction action, old_action;
7058 emacs_sigaction_init (&action, deliver_child_signal);
7059 block_child_signal ();
7060 sigaction (SIGCHLD, &action, &old_action);
7061 eassert (! (old_action.sa_flags & SA_SIGINFO));
7062
7063 if (old_action.sa_handler != deliver_child_signal)
7064 lib_child_handler
7065 = (old_action.sa_handler == SIG_DFL || old_action.sa_handler == SIG_IGN
7066 ? dummy_handler
7067 : old_action.sa_handler);
7068 unblock_child_signal ();
7069}
7070
7022 7071
7023/* This is not called "init_process" because that is the name of a 7072/* This is not called "init_process" because that is the name of a
7024 Mach system call, so it would cause problems on Darwin systems. */ 7073 Mach system call, so it would cause problems on Darwin systems. */
@@ -7034,16 +7083,21 @@ init_process_emacs (void)
7034 if (! noninteractive || initialized) 7083 if (! noninteractive || initialized)
7035#endif 7084#endif
7036 { 7085 {
7037 struct sigaction action; 7086#if defined HAVE_GLIB && !defined WINDOWSNT
7038 emacs_sigaction_init (&action, deliver_child_signal); 7087 /* Tickle glib's child-handling code. Ask glib to wait for Emacs itself;
7039 sigaction (SIGCHLD, &action, 0); 7088 this should always fail, but is enough to initialize glib's
7089 private SIGCHLD handler, allowing catch_child_signal to copy
7090 it into lib_child_handler. */
7091 g_source_unref (g_child_watch_source_new (getpid ()));
7092#endif
7093 catch_child_signal ();
7040 } 7094 }
7041 7095
7042 FD_ZERO (&input_wait_mask); 7096 FD_ZERO (&input_wait_mask);
7043 FD_ZERO (&non_keyboard_wait_mask); 7097 FD_ZERO (&non_keyboard_wait_mask);
7044 FD_ZERO (&non_process_wait_mask); 7098 FD_ZERO (&non_process_wait_mask);
7045 FD_ZERO (&write_mask); 7099 FD_ZERO (&write_mask);
7046 max_process_desc = 0; 7100 max_process_desc = max_input_desc = -1;
7047 memset (fd_callback_info, 0, sizeof (fd_callback_info)); 7101 memset (fd_callback_info, 0, sizeof (fd_callback_info));
7048 7102
7049#ifdef NON_BLOCKING_CONNECT 7103#ifdef NON_BLOCKING_CONNECT
@@ -7065,7 +7119,7 @@ init_process_emacs (void)
7065 7119
7066 Vprocess_alist = Qnil; 7120 Vprocess_alist = Qnil;
7067 deleted_pid_list = Qnil; 7121 deleted_pid_list = Qnil;
7068 for (i = 0; i < MAXDESC; i++) 7122 for (i = 0; i < FD_SETSIZE; i++)
7069 { 7123 {
7070 chan_process[i] = Qnil; 7124 chan_process[i] = Qnil;
7071 proc_buffered_char[i] = -1; 7125 proc_buffered_char[i] = -1;
@@ -7219,6 +7273,10 @@ syms_of_process (void)
7219 DEFSYM (Qcutime, "cutime"); 7273 DEFSYM (Qcutime, "cutime");
7220 DEFSYM (Qcstime, "cstime"); 7274 DEFSYM (Qcstime, "cstime");
7221 DEFSYM (Qctime, "ctime"); 7275 DEFSYM (Qctime, "ctime");
7276 DEFSYM (Qinternal_default_process_sentinel,
7277 "internal-default-process-sentinel");
7278 DEFSYM (Qinternal_default_process_filter,
7279 "internal-default-process-filter");
7222 DEFSYM (Qpri, "pri"); 7280 DEFSYM (Qpri, "pri");
7223 DEFSYM (Qnice, "nice"); 7281 DEFSYM (Qnice, "nice");
7224 DEFSYM (Qthcount, "thcount"); 7282 DEFSYM (Qthcount, "thcount");
@@ -7289,14 +7347,8 @@ The variable takes effect when `start-process' is called. */);
7289 defsubr (&Sset_network_process_option); 7347 defsubr (&Sset_network_process_option);
7290 defsubr (&Smake_network_process); 7348 defsubr (&Smake_network_process);
7291 defsubr (&Sformat_network_address); 7349 defsubr (&Sformat_network_address);
7292#if defined (HAVE_NET_IF_H)
7293#ifdef SIOCGIFCONF
7294 defsubr (&Snetwork_interface_list); 7350 defsubr (&Snetwork_interface_list);
7295#endif
7296#if defined (SIOCGIFADDR) || defined (SIOCGIFHWADDR) || defined (SIOCGIFFLAGS)
7297 defsubr (&Snetwork_interface_info); 7351 defsubr (&Snetwork_interface_info);
7298#endif
7299#endif /* defined (HAVE_NET_IF_H) */
7300#ifdef DATAGRAM_SOCKETS 7352#ifdef DATAGRAM_SOCKETS
7301 defsubr (&Sprocess_datagram_address); 7353 defsubr (&Sprocess_datagram_address);
7302 defsubr (&Sset_process_datagram_address); 7354 defsubr (&Sset_process_datagram_address);
@@ -7314,6 +7366,8 @@ The variable takes effect when `start-process' is called. */);
7314 defsubr (&Ssignal_process); 7366 defsubr (&Ssignal_process);
7315 defsubr (&Swaiting_for_user_input_p); 7367 defsubr (&Swaiting_for_user_input_p);
7316 defsubr (&Sprocess_type); 7368 defsubr (&Sprocess_type);
7369 defsubr (&Sinternal_default_process_sentinel);
7370 defsubr (&Sinternal_default_process_filter);
7317 defsubr (&Sset_process_coding_system); 7371 defsubr (&Sset_process_coding_system);
7318 defsubr (&Sprocess_coding_system); 7372 defsubr (&Sprocess_coding_system);
7319 defsubr (&Sset_process_filter_multibyte); 7373 defsubr (&Sset_process_filter_multibyte);