aboutsummaryrefslogtreecommitdiffstats
path: root/src/process.c
diff options
context:
space:
mode:
authorXue Fuqiao2013-09-04 08:39:34 +0800
committerXue Fuqiao2013-09-04 08:39:34 +0800
commitadf2fc4a01efe77d73cd52bc9173914ed56ff531 (patch)
treea5a280a5554a7bffeaf94fccae29fa3ac1a5d066 /src/process.c
parent63191d9f2043d2e67657e85a7b3842805dd1dad6 (diff)
parent38726039b77db432989fed106c88e9f1aa463281 (diff)
downloademacs-adf2fc4a01efe77d73cd52bc9173914ed56ff531.tar.gz
emacs-adf2fc4a01efe77d73cd52bc9173914ed56ff531.zip
Merge from mainline.
Diffstat (limited to 'src/process.c')
-rw-r--r--src/process.c520
1 files changed, 264 insertions, 256 deletions
diff --git a/src/process.c b/src/process.c
index d87a1803fe2..20f84990d6f 100644
--- a/src/process.c
+++ b/src/process.c
@@ -92,6 +92,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
92 92
93#include <c-ctype.h> 93#include <c-ctype.h>
94#include <sig2str.h> 94#include <sig2str.h>
95#include <verify.h>
95 96
96#endif /* subprocesses */ 97#endif /* subprocesses */
97 98
@@ -131,8 +132,8 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
131#endif 132#endif
132 133
133#ifdef WINDOWSNT 134#ifdef WINDOWSNT
134extern int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *, 135extern int sys_select (int, fd_set *, fd_set *, fd_set *,
135 EMACS_TIME *, void *); 136 struct timespec *, void *);
136#endif 137#endif
137 138
138#ifndef SOCK_CLOEXEC 139#ifndef SOCK_CLOEXEC
@@ -260,7 +261,7 @@ static EMACS_INT update_tick;
260#endif 261#endif
261 262
262#ifdef ADAPTIVE_READ_BUFFERING 263#ifdef ADAPTIVE_READ_BUFFERING
263#define READ_OUTPUT_DELAY_INCREMENT (EMACS_TIME_RESOLUTION / 100) 264#define READ_OUTPUT_DELAY_INCREMENT (TIMESPEC_RESOLUTION / 100)
264#define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5) 265#define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
265#define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7) 266#define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
266 267
@@ -279,7 +280,7 @@ static bool process_output_skip;
279 280
280static void create_process (Lisp_Object, char **, Lisp_Object); 281static void create_process (Lisp_Object, char **, Lisp_Object);
281#ifdef USABLE_SIGIO 282#ifdef USABLE_SIGIO
282static bool keyboard_bit_set (SELECT_TYPE *); 283static bool keyboard_bit_set (fd_set *);
283#endif 284#endif
284static void deactivate_process (Lisp_Object); 285static void deactivate_process (Lisp_Object);
285static void status_notify (struct Lisp_Process *); 286static void status_notify (struct Lisp_Process *);
@@ -298,39 +299,39 @@ static void exec_sentinel (Lisp_Object proc, Lisp_Object reason);
298 299
299/* Mask of bits indicating the descriptors that we wait for input on. */ 300/* Mask of bits indicating the descriptors that we wait for input on. */
300 301
301static SELECT_TYPE input_wait_mask; 302static fd_set input_wait_mask;
302 303
303/* Mask that excludes keyboard input descriptor(s). */ 304/* Mask that excludes keyboard input descriptor(s). */
304 305
305static SELECT_TYPE non_keyboard_wait_mask; 306static fd_set non_keyboard_wait_mask;
306 307
307/* Mask that excludes process input descriptor(s). */ 308/* Mask that excludes process input descriptor(s). */
308 309
309static SELECT_TYPE non_process_wait_mask; 310static fd_set non_process_wait_mask;
310 311
311/* Mask for selecting for write. */ 312/* Mask for selecting for write. */
312 313
313static SELECT_TYPE write_mask; 314static fd_set write_mask;
314 315
315#ifdef NON_BLOCKING_CONNECT 316#ifdef NON_BLOCKING_CONNECT
316/* Mask of bits indicating the descriptors that we wait for connect to 317/* Mask of bits indicating the descriptors that we wait for connect to
317 complete on. Once they complete, they are removed from this mask 318 complete on. Once they complete, they are removed from this mask
318 and added to the input_wait_mask and non_keyboard_wait_mask. */ 319 and added to the input_wait_mask and non_keyboard_wait_mask. */
319 320
320static SELECT_TYPE connect_wait_mask; 321static fd_set connect_wait_mask;
321 322
322/* Number of bits set in connect_wait_mask. */ 323/* Number of bits set in connect_wait_mask. */
323static int num_pending_connects; 324static int num_pending_connects;
324#endif /* NON_BLOCKING_CONNECT */ 325#endif /* NON_BLOCKING_CONNECT */
325 326
326/* The largest descriptor currently in use for a process object. */ 327/* The largest descriptor currently in use for a process object; -1 if none. */
327static int max_process_desc; 328static int max_process_desc;
328 329
329/* The largest descriptor currently in use for input. */ 330/* The largest descriptor currently in use for input; -1 if none. */
330static int max_input_desc; 331static int max_input_desc;
331 332
332/* Indexed by descriptor, gives the process (if any) for that descriptor */ 333/* Indexed by descriptor, gives the process (if any) for that descriptor */
333static Lisp_Object chan_process[MAXDESC]; 334static Lisp_Object chan_process[FD_SETSIZE];
334 335
335/* Alist of elements (NAME . PROCESS) */ 336/* Alist of elements (NAME . PROCESS) */
336static Lisp_Object Vprocess_alist; 337static Lisp_Object Vprocess_alist;
@@ -341,18 +342,18 @@ static Lisp_Object Vprocess_alist;
341 output from the process is to read at least one char. 342 output from the process is to read at least one char.
342 Always -1 on systems that support FIONREAD. */ 343 Always -1 on systems that support FIONREAD. */
343 344
344static int proc_buffered_char[MAXDESC]; 345static int proc_buffered_char[FD_SETSIZE];
345 346
346/* Table of `struct coding-system' for each process. */ 347/* Table of `struct coding-system' for each process. */
347static struct coding_system *proc_decode_coding_system[MAXDESC]; 348static struct coding_system *proc_decode_coding_system[FD_SETSIZE];
348static struct coding_system *proc_encode_coding_system[MAXDESC]; 349static struct coding_system *proc_encode_coding_system[FD_SETSIZE];
349 350
350#ifdef DATAGRAM_SOCKETS 351#ifdef DATAGRAM_SOCKETS
351/* Table of `partner address' for datagram sockets. */ 352/* Table of `partner address' for datagram sockets. */
352static struct sockaddr_and_len { 353static struct sockaddr_and_len {
353 struct sockaddr *sa; 354 struct sockaddr *sa;
354 int len; 355 int len;
355} datagram_address[MAXDESC]; 356} datagram_address[FD_SETSIZE];
356#define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0) 357#define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
357#define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0) 358#define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
358#else 359#else
@@ -360,6 +361,12 @@ static struct sockaddr_and_len {
360#define DATAGRAM_CONN_P(proc) (0) 361#define DATAGRAM_CONN_P(proc) (0)
361#endif 362#endif
362 363
364/* FOR_EACH_PROCESS (LIST_VAR, PROC_VAR) followed by a statement is
365 a `for' loop which iterates over processes from Vprocess_alist. */
366
367#define FOR_EACH_PROCESS(list_var, proc_var) \
368 FOR_EACH_ALIST_VALUE (Vprocess_alist, list_var, proc_var)
369
363/* These setters are used only in this file, so they can be private. */ 370/* These setters are used only in this file, so they can be private. */
364static void 371static void
365pset_buffer (struct Lisp_Process *p, Lisp_Object val) 372pset_buffer (struct Lisp_Process *p, Lisp_Object val)
@@ -451,7 +458,7 @@ static struct fd_callback_data
451#define FOR_READ 1 458#define FOR_READ 1
452#define FOR_WRITE 2 459#define FOR_WRITE 2
453 int condition; /* mask of the defines above. */ 460 int condition; /* mask of the defines above. */
454} fd_callback_info[MAXDESC]; 461} fd_callback_info[FD_SETSIZE];
455 462
456 463
457/* Add a file descriptor FD to be monitored for when read is possible. 464/* Add a file descriptor FD to be monitored for when read is possible.
@@ -460,7 +467,7 @@ static struct fd_callback_data
460void 467void
461add_read_fd (int fd, fd_callback func, void *data) 468add_read_fd (int fd, fd_callback func, void *data)
462{ 469{
463 eassert (fd < MAXDESC); 470 eassert (fd < FD_SETSIZE);
464 add_keyboard_wait_descriptor (fd); 471 add_keyboard_wait_descriptor (fd);
465 472
466 fd_callback_info[fd].func = func; 473 fd_callback_info[fd].func = func;
@@ -473,7 +480,7 @@ add_read_fd (int fd, fd_callback func, void *data)
473void 480void
474delete_read_fd (int fd) 481delete_read_fd (int fd)
475{ 482{
476 eassert (fd < MAXDESC); 483 eassert (fd < FD_SETSIZE);
477 delete_keyboard_wait_descriptor (fd); 484 delete_keyboard_wait_descriptor (fd);
478 485
479 fd_callback_info[fd].condition &= ~FOR_READ; 486 fd_callback_info[fd].condition &= ~FOR_READ;
@@ -490,7 +497,7 @@ delete_read_fd (int fd)
490void 497void
491add_write_fd (int fd, fd_callback func, void *data) 498add_write_fd (int fd, fd_callback func, void *data)
492{ 499{
493 eassert (fd < MAXDESC); 500 eassert (fd < FD_SETSIZE);
494 FD_SET (fd, &write_mask); 501 FD_SET (fd, &write_mask);
495 if (fd > max_input_desc) 502 if (fd > max_input_desc)
496 max_input_desc = fd; 503 max_input_desc = fd;
@@ -500,29 +507,35 @@ add_write_fd (int fd, fd_callback func, void *data)
500 fd_callback_info[fd].condition |= FOR_WRITE; 507 fd_callback_info[fd].condition |= FOR_WRITE;
501} 508}
502 509
510/* FD is no longer an input descriptor; update max_input_desc accordingly. */
511
512static void
513delete_input_desc (int fd)
514{
515 if (fd == max_input_desc)
516 {
517 do
518 fd--;
519 while (0 <= fd && ! (FD_ISSET (fd, &input_wait_mask)
520 || FD_ISSET (fd, &write_mask)));
521
522 max_input_desc = fd;
523 }
524}
525
503/* Stop monitoring file descriptor FD for when write is possible. */ 526/* Stop monitoring file descriptor FD for when write is possible. */
504 527
505void 528void
506delete_write_fd (int fd) 529delete_write_fd (int fd)
507{ 530{
508 int lim = max_input_desc; 531 eassert (fd < FD_SETSIZE);
509
510 eassert (fd < MAXDESC);
511 FD_CLR (fd, &write_mask); 532 FD_CLR (fd, &write_mask);
512 fd_callback_info[fd].condition &= ~FOR_WRITE; 533 fd_callback_info[fd].condition &= ~FOR_WRITE;
513 if (fd_callback_info[fd].condition == 0) 534 if (fd_callback_info[fd].condition == 0)
514 { 535 {
515 fd_callback_info[fd].func = 0; 536 fd_callback_info[fd].func = 0;
516 fd_callback_info[fd].data = 0; 537 fd_callback_info[fd].data = 0;
517 538 delete_input_desc (fd);
518 if (fd == max_input_desc)
519 for (fd = lim; fd >= 0; fd--)
520 if (FD_ISSET (fd, &input_wait_mask) || FD_ISSET (fd, &write_mask))
521 {
522 max_input_desc = fd;
523 break;
524 }
525
526 } 539 }
527} 540}
528 541
@@ -674,6 +687,15 @@ allocate_pty (char pty_name[PTY_NAME_SIZE])
674 687
675 if (fd >= 0) 688 if (fd >= 0)
676 { 689 {
690#ifdef PTY_OPEN
691 /* Set FD's close-on-exec flag. This is needed even if
692 PT_OPEN calls posix_openpt with O_CLOEXEC, since POSIX
693 doesn't require support for that combination.
694 Multithreaded platforms where posix_openpt ignores
695 O_CLOEXEC (or where PTY_OPEN doesn't call posix_openpt)
696 have a race condition between the PTY_OPEN and here. */
697 fcntl (fd, F_SETFD, FD_CLOEXEC);
698#endif
677 /* check to make certain that both sides are available 699 /* check to make certain that both sides are available
678 this avoids a nasty yet stupid bug in rlogins */ 700 this avoids a nasty yet stupid bug in rlogins */
679#ifdef PTY_TTY_NAME_SPRINTF 701#ifdef PTY_TTY_NAME_SPRINTF
@@ -716,6 +738,8 @@ make_process (Lisp_Object name)
716 non-Lisp data, so do it only for slots which should not be zero. */ 738 non-Lisp data, so do it only for slots which should not be zero. */
717 p->infd = -1; 739 p->infd = -1;
718 p->outfd = -1; 740 p->outfd = -1;
741 for (i = 0; i < PROCESS_OPEN_FDS; i++)
742 p->open_fd[i] = -1;
719 743
720#ifdef HAVE_GNUTLS 744#ifdef HAVE_GNUTLS
721 p->gnutls_initstage = GNUTLS_STAGE_EMPTY; 745 p->gnutls_initstage = GNUTLS_STAGE_EMPTY;
@@ -812,13 +836,17 @@ get_process (register Lisp_Object name)
812 treated by the SIGCHLD handler and waitpid has been invoked on them; 836 treated by the SIGCHLD handler and waitpid has been invoked on them;
813 otherwise they might fill up the kernel's process table. 837 otherwise they might fill up the kernel's process table.
814 838
815 Some processes created by call-process are also put onto this list. */ 839 Some processes created by call-process are also put onto this list.
840
841 Members of this list are (process-ID . filename) pairs. The
842 process-ID is a number; the filename, if a string, is a file that
843 needs to be removed after the process exits. */
816static Lisp_Object deleted_pid_list; 844static Lisp_Object deleted_pid_list;
817 845
818void 846void
819record_deleted_pid (pid_t pid) 847record_deleted_pid (pid_t pid, Lisp_Object filename)
820{ 848{
821 deleted_pid_list = Fcons (make_fixnum_or_float (pid), 849 deleted_pid_list = Fcons (Fcons (make_fixnum_or_float (pid), filename),
822 /* GC treated elements set to nil. */ 850 /* GC treated elements set to nil. */
823 Fdelq (Qnil, deleted_pid_list)); 851 Fdelq (Qnil, deleted_pid_list));
824 852
@@ -846,7 +874,7 @@ nil, indicating the current buffer's process. */)
846 else 874 else
847 { 875 {
848 if (p->alive) 876 if (p->alive)
849 record_kill_process (p); 877 record_kill_process (p, Qnil);
850 878
851 if (p->infd >= 0) 879 if (p->infd >= 0)
852 { 880 {
@@ -1112,15 +1140,18 @@ See `set-process-sentinel' for more info on sentinels. */)
1112DEFUN ("set-process-window-size", Fset_process_window_size, 1140DEFUN ("set-process-window-size", Fset_process_window_size,
1113 Sset_process_window_size, 3, 3, 0, 1141 Sset_process_window_size, 3, 3, 0,
1114 doc: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */) 1142 doc: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1115 (register Lisp_Object process, Lisp_Object height, Lisp_Object width) 1143 (Lisp_Object process, Lisp_Object height, Lisp_Object width)
1116{ 1144{
1117 CHECK_PROCESS (process); 1145 CHECK_PROCESS (process);
1118 CHECK_RANGED_INTEGER (height, 0, INT_MAX); 1146
1119 CHECK_RANGED_INTEGER (width, 0, INT_MAX); 1147 /* All known platforms store window sizes as 'unsigned short'. */
1148 CHECK_RANGED_INTEGER (height, 0, USHRT_MAX);
1149 CHECK_RANGED_INTEGER (width, 0, USHRT_MAX);
1120 1150
1121 if (XPROCESS (process)->infd < 0 1151 if (XPROCESS (process)->infd < 0
1122 || set_window_size (XPROCESS (process)->infd, 1152 || (set_window_size (XPROCESS (process)->infd,
1123 XINT (height), XINT (width)) <= 0) 1153 XINT (height), XINT (width))
1154 < 0))
1124 return Qnil; 1155 return Qnil;
1125 else 1156 else
1126 return Qt; 1157 return Qt;
@@ -1380,22 +1411,9 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1380 function. The argument list is protected by the caller, so all 1411 function. The argument list is protected by the caller, so all
1381 we really have to worry about is buffer. */ 1412 we really have to worry about is buffer. */
1382 { 1413 {
1383 struct gcpro gcpro1, gcpro2; 1414 struct gcpro gcpro1;
1384 1415 GCPRO1 (buffer);
1385 current_dir = BVAR (current_buffer, directory); 1416 current_dir = encode_current_directory ();
1386
1387 GCPRO2 (buffer, current_dir);
1388
1389 current_dir = Funhandled_file_name_directory (current_dir);
1390 if (NILP (current_dir))
1391 /* If the file name handler says that current_dir is unreachable, use
1392 a sensible default. */
1393 current_dir = build_string ("~/");
1394 current_dir = expand_and_dir_to_file (current_dir, Qnil);
1395 if (NILP (Ffile_accessible_directory_p (current_dir)))
1396 report_file_error ("Setting current directory",
1397 BVAR (current_buffer, directory));
1398
1399 UNGCPRO; 1417 UNGCPRO;
1400 } 1418 }
1401 1419
@@ -1599,22 +1617,49 @@ start_process_unwind (Lisp_Object proc)
1599 remove_process (proc); 1617 remove_process (proc);
1600} 1618}
1601 1619
1620/* If *FD_ADDR is nonnegative, close it, and mark it as closed. */
1621
1622static void
1623close_process_fd (int *fd_addr)
1624{
1625 int fd = *fd_addr;
1626 if (0 <= fd)
1627 {
1628 *fd_addr = -1;
1629 emacs_close (fd);
1630 }
1631}
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);
1602 1651
1603static void 1652static void
1604create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir) 1653create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1605{ 1654{
1655 struct Lisp_Process *p = XPROCESS (process);
1606 int inchannel, outchannel; 1656 int inchannel, outchannel;
1607 pid_t pid; 1657 pid_t pid;
1608 int vfork_errno; 1658 int vfork_errno;
1609 int sv[2];
1610#ifndef WINDOWSNT
1611 int wait_child_setup[2];
1612#endif
1613 int forkin, forkout; 1659 int forkin, forkout;
1614 bool pty_flag = 0; 1660 bool pty_flag = 0;
1615 char pty_name[PTY_NAME_SIZE]; 1661 char pty_name[PTY_NAME_SIZE];
1616 Lisp_Object lisp_pty_name = Qnil; 1662 Lisp_Object lisp_pty_name = Qnil;
1617 Lisp_Object encoded_current_dir;
1618 1663
1619 inchannel = outchannel = -1; 1664 inchannel = outchannel = -1;
1620 1665
@@ -1623,6 +1668,7 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1623 1668
1624 if (inchannel >= 0) 1669 if (inchannel >= 0)
1625 { 1670 {
1671 p->open_fd[READ_FROM_SUBPROCESS] = inchannel;
1626#if ! defined (USG) || defined (USG_SUBTTY_WORKS) 1672#if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1627 /* 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,
1628 then close it and reopen it in the child. */ 1674 then close it and reopen it in the child. */
@@ -1631,6 +1677,7 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1631 forkout = forkin = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0); 1677 forkout = forkin = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0);
1632 if (forkin < 0) 1678 if (forkin < 0)
1633 report_file_error ("Opening pty", Qnil); 1679 report_file_error ("Opening pty", Qnil);
1680 p->open_fd[SUBPROCESS_STDIN] = forkin;
1634#else 1681#else
1635 forkin = forkout = -1; 1682 forkin = forkout = -1;
1636#endif /* not USG, or USG_SUBTTY_WORKS */ 1683#endif /* not USG, or USG_SUBTTY_WORKS */
@@ -1639,23 +1686,17 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1639 } 1686 }
1640 else 1687 else
1641 { 1688 {
1642 if (emacs_pipe (sv) != 0) 1689 if (emacs_pipe (p->open_fd + SUBPROCESS_STDIN) != 0
1690 || emacs_pipe (p->open_fd + READ_FROM_SUBPROCESS) != 0)
1643 report_file_error ("Creating pipe", Qnil); 1691 report_file_error ("Creating pipe", Qnil);
1644 inchannel = sv[0]; 1692 forkin = p->open_fd[SUBPROCESS_STDIN];
1645 forkout = sv[1]; 1693 outchannel = p->open_fd[WRITE_TO_SUBPROCESS];
1646 if (emacs_pipe (sv) != 0) 1694 inchannel = p->open_fd[READ_FROM_SUBPROCESS];
1647 { 1695 forkout = p->open_fd[SUBPROCESS_STDOUT];
1648 int pipe_errno = errno;
1649 emacs_close (inchannel);
1650 emacs_close (forkout);
1651 report_file_errno ("Creating pipe", Qnil, pipe_errno);
1652 }
1653 outchannel = sv[1];
1654 forkin = sv[0];
1655 } 1696 }
1656 1697
1657#ifndef WINDOWSNT 1698#ifndef WINDOWSNT
1658 if (emacs_pipe (wait_child_setup) != 0) 1699 if (emacs_pipe (p->open_fd + READ_FROM_EXEC_MONITOR) != 0)
1659 report_file_error ("Creating pipe", Qnil); 1700 report_file_error ("Creating pipe", Qnil);
1660#endif 1701#endif
1661 1702
@@ -1664,16 +1705,16 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1664 1705
1665 /* Record this as an active process, with its channels. */ 1706 /* Record this as an active process, with its channels. */
1666 chan_process[inchannel] = process; 1707 chan_process[inchannel] = process;
1667 XPROCESS (process)->infd = inchannel; 1708 p->infd = inchannel;
1668 XPROCESS (process)->outfd = outchannel; 1709 p->outfd = outchannel;
1669 1710
1670 /* Previously we recorded the tty descriptor used in the subprocess. 1711 /* Previously we recorded the tty descriptor used in the subprocess.
1671 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
1672 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
1673 more portable (see USG_SUBTTY_WORKS above). */ 1714 more portable (see USG_SUBTTY_WORKS above). */
1674 1715
1675 XPROCESS (process)->pty_flag = pty_flag; 1716 p->pty_flag = pty_flag;
1676 pset_status (XPROCESS (process), Qrun); 1717 pset_status (p, Qrun);
1677 1718
1678 FD_SET (inchannel, &input_wait_mask); 1719 FD_SET (inchannel, &input_wait_mask);
1679 FD_SET (inchannel, &non_keyboard_wait_mask); 1720 FD_SET (inchannel, &non_keyboard_wait_mask);
@@ -1683,35 +1724,29 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1683 /* This may signal an error. */ 1724 /* This may signal an error. */
1684 setup_process_coding_systems (process); 1725 setup_process_coding_systems (process);
1685 1726
1686 encoded_current_dir = ENCODE_FILE (current_dir);
1687
1688 block_input (); 1727 block_input ();
1689 block_child_signal (); 1728 block_child_signal ();
1690 1729
1691#ifndef WINDOWSNT 1730#ifndef WINDOWSNT
1692 /* vfork, and prevent local vars from being clobbered by the vfork. */ 1731 /* vfork, and prevent local vars from being clobbered by the vfork. */
1693 { 1732 {
1694 Lisp_Object volatile encoded_current_dir_volatile = encoded_current_dir; 1733 Lisp_Object volatile current_dir_volatile = current_dir;
1695 Lisp_Object volatile lisp_pty_name_volatile = lisp_pty_name; 1734 Lisp_Object volatile lisp_pty_name_volatile = lisp_pty_name;
1696 Lisp_Object volatile process_volatile = process;
1697 char **volatile new_argv_volatile = new_argv; 1735 char **volatile new_argv_volatile = new_argv;
1698 int volatile forkin_volatile = forkin; 1736 int volatile forkin_volatile = forkin;
1699 int volatile forkout_volatile = forkout; 1737 int volatile forkout_volatile = forkout;
1700 int volatile wait_child_setup_0_volatile = wait_child_setup[0]; 1738 struct Lisp_Process *p_volatile = p;
1701 int volatile wait_child_setup_1_volatile = wait_child_setup[1];
1702 1739
1703 pid = vfork (); 1740 pid = vfork ();
1704 1741
1705 encoded_current_dir = encoded_current_dir_volatile; 1742 current_dir = current_dir_volatile;
1706 lisp_pty_name = lisp_pty_name_volatile; 1743 lisp_pty_name = lisp_pty_name_volatile;
1707 process = process_volatile;
1708 new_argv = new_argv_volatile; 1744 new_argv = new_argv_volatile;
1709 forkin = forkin_volatile; 1745 forkin = forkin_volatile;
1710 forkout = forkout_volatile; 1746 forkout = forkout_volatile;
1711 wait_child_setup[0] = wait_child_setup_0_volatile; 1747 p = p_volatile;
1712 wait_child_setup[1] = wait_child_setup_1_volatile;
1713 1748
1714 pty_flag = XPROCESS (process)->pty_flag; 1749 pty_flag = p->pty_flag;
1715 } 1750 }
1716 1751
1717 if (pid == 0) 1752 if (pid == 0)
@@ -1816,53 +1851,51 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1816 if (pty_flag) 1851 if (pty_flag)
1817 child_setup_tty (xforkout); 1852 child_setup_tty (xforkout);
1818#ifdef WINDOWSNT 1853#ifdef WINDOWSNT
1819 pid = child_setup (xforkin, xforkout, xforkout, 1854 pid = child_setup (xforkin, xforkout, xforkout, new_argv, 1, current_dir);
1820 new_argv, 1, encoded_current_dir);
1821#else /* not WINDOWSNT */ 1855#else /* not WINDOWSNT */
1822 child_setup (xforkin, xforkout, xforkout, 1856 child_setup (xforkin, xforkout, xforkout, new_argv, 1, current_dir);
1823 new_argv, 1, encoded_current_dir);
1824#endif /* not WINDOWSNT */ 1857#endif /* not WINDOWSNT */
1825 } 1858 }
1826 1859
1827 /* Back in the parent process. */ 1860 /* Back in the parent process. */
1828 1861
1829 vfork_errno = errno; 1862 vfork_errno = errno;
1830 XPROCESS (process)->pid = pid; 1863 p->pid = pid;
1831 if (pid >= 0) 1864 if (pid >= 0)
1832 XPROCESS (process)->alive = 1; 1865 p->alive = 1;
1833 1866
1834 /* Stop blocking in the parent. */ 1867 /* Stop blocking in the parent. */
1835 unblock_child_signal (); 1868 unblock_child_signal ();
1836 unblock_input (); 1869 unblock_input ();
1837 1870
1838 if (forkin >= 0)
1839 emacs_close (forkin);
1840 if (forkin != forkout && forkout >= 0)
1841 emacs_close (forkout);
1842
1843 if (pid < 0) 1871 if (pid < 0)
1844 report_file_errno ("Doing vfork", Qnil, vfork_errno); 1872 report_file_errno ("Doing vfork", Qnil, vfork_errno);
1845 else 1873 else
1846 { 1874 {
1847 /* vfork succeeded. */ 1875 /* vfork succeeded. */
1848 1876
1877 /* Close the pipe ends that the child uses, or the child's pty. */
1878 close_process_fd (&p->open_fd[SUBPROCESS_STDIN]);
1879 close_process_fd (&p->open_fd[SUBPROCESS_STDOUT]);
1880
1849#ifdef WINDOWSNT 1881#ifdef WINDOWSNT
1850 register_child (pid, inchannel); 1882 register_child (pid, inchannel);
1851#endif /* WINDOWSNT */ 1883#endif /* WINDOWSNT */
1852 1884
1853 pset_tty_name (XPROCESS (process), lisp_pty_name); 1885 pset_tty_name (p, lisp_pty_name);
1854 1886
1855#ifndef WINDOWSNT 1887#ifndef WINDOWSNT
1856 /* Wait for child_setup to complete in case that vfork is 1888 /* Wait for child_setup to complete in case that vfork is
1857 actually defined as fork. The descriptor wait_child_setup[1] 1889 actually defined as fork. The descriptor
1890 XPROCESS (proc)->open_fd[EXEC_MONITOR_OUTPUT]
1858 of a pipe is closed at the child side either by close-on-exec 1891 of a pipe is closed at the child side either by close-on-exec
1859 on successful execve or the _exit call in child_setup. */ 1892 on successful execve or the _exit call in child_setup. */
1860 { 1893 {
1861 char dummy; 1894 char dummy;
1862 1895
1863 emacs_close (wait_child_setup[1]); 1896 close_process_fd (&p->open_fd[EXEC_MONITOR_OUTPUT]);
1864 emacs_read (wait_child_setup[0], &dummy, 1); 1897 emacs_read (p->open_fd[READ_FROM_EXEC_MONITOR], &dummy, 1);
1865 emacs_close (wait_child_setup[0]); 1898 close_process_fd (&p->open_fd[READ_FROM_EXEC_MONITOR]);
1866 } 1899 }
1867#endif 1900#endif
1868 } 1901 }
@@ -1871,16 +1904,13 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1871static void 1904static void
1872create_pty (Lisp_Object process) 1905create_pty (Lisp_Object process)
1873{ 1906{
1907 struct Lisp_Process *p = XPROCESS (process);
1874 char pty_name[PTY_NAME_SIZE]; 1908 char pty_name[PTY_NAME_SIZE];
1875 int inchannel, outchannel; 1909 int pty_fd = NILP (Vprocess_connection_type) ? -1 : allocate_pty (pty_name);
1876 1910
1877 inchannel = outchannel = -1; 1911 if (pty_fd >= 0)
1878
1879 if (!NILP (Vprocess_connection_type))
1880 outchannel = inchannel = allocate_pty (pty_name);
1881
1882 if (inchannel >= 0)
1883 { 1912 {
1913 p->open_fd[SUBPROCESS_STDIN] = pty_fd;
1884#if ! defined (USG) || defined (USG_SUBTTY_WORKS) 1914#if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1885 /* On most USG systems it does not work to open the pty's tty here, 1915 /* On most USG systems it does not work to open the pty's tty here,
1886 then close it and reopen it in the child. */ 1916 then close it and reopen it in the child. */
@@ -1889,6 +1919,7 @@ create_pty (Lisp_Object process)
1889 int forkout = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0); 1919 int forkout = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0);
1890 if (forkout < 0) 1920 if (forkout < 0)
1891 report_file_error ("Opening pty", Qnil); 1921 report_file_error ("Opening pty", Qnil);
1922 p->open_fd[WRITE_TO_SUBPROCESS] = forkout;
1892#if defined (DONT_REOPEN_PTY) 1923#if defined (DONT_REOPEN_PTY)
1893 /* In the case that vfork is defined as fork, the parent process 1924 /* In the case that vfork is defined as fork, the parent process
1894 (Emacs) may send some data before the child process completes 1925 (Emacs) may send some data before the child process completes
@@ -1897,33 +1928,32 @@ create_pty (Lisp_Object process)
1897#endif /* DONT_REOPEN_PTY */ 1928#endif /* DONT_REOPEN_PTY */
1898#endif /* not USG, or USG_SUBTTY_WORKS */ 1929#endif /* not USG, or USG_SUBTTY_WORKS */
1899 1930
1900 fcntl (inchannel, F_SETFL, O_NONBLOCK); 1931 fcntl (pty_fd, F_SETFL, O_NONBLOCK);
1901 fcntl (outchannel, F_SETFL, O_NONBLOCK);
1902 1932
1903 /* Record this as an active process, with its channels. 1933 /* Record this as an active process, with its channels.
1904 As a result, child_setup will close Emacs's side of the pipes. */ 1934 As a result, child_setup will close Emacs's side of the pipes. */
1905 chan_process[inchannel] = process; 1935 chan_process[pty_fd] = process;
1906 XPROCESS (process)->infd = inchannel; 1936 p->infd = pty_fd;
1907 XPROCESS (process)->outfd = outchannel; 1937 p->outfd = pty_fd;
1908 1938
1909 /* Previously we recorded the tty descriptor used in the subprocess. 1939 /* Previously we recorded the tty descriptor used in the subprocess.
1910 It was only used for getting the foreground tty process, so now 1940 It was only used for getting the foreground tty process, so now
1911 we just reopen the device (see emacs_get_tty_pgrp) as this is 1941 we just reopen the device (see emacs_get_tty_pgrp) as this is
1912 more portable (see USG_SUBTTY_WORKS above). */ 1942 more portable (see USG_SUBTTY_WORKS above). */
1913 1943
1914 XPROCESS (process)->pty_flag = 1; 1944 p->pty_flag = 1;
1915 pset_status (XPROCESS (process), Qrun); 1945 pset_status (p, Qrun);
1916 setup_process_coding_systems (process); 1946 setup_process_coding_systems (process);
1917 1947
1918 FD_SET (inchannel, &input_wait_mask); 1948 FD_SET (pty_fd, &input_wait_mask);
1919 FD_SET (inchannel, &non_keyboard_wait_mask); 1949 FD_SET (pty_fd, &non_keyboard_wait_mask);
1920 if (inchannel > max_process_desc) 1950 if (pty_fd > max_process_desc)
1921 max_process_desc = inchannel; 1951 max_process_desc = pty_fd;
1922 1952
1923 pset_tty_name (XPROCESS (process), build_string (pty_name)); 1953 pset_tty_name (p, build_string (pty_name));
1924 } 1954 }
1925 1955
1926 XPROCESS (process)->pid = -2; 1956 p->pid = -2;
1927} 1957}
1928 1958
1929 1959
@@ -2529,6 +2559,7 @@ usage: (make-serial-process &rest ARGS) */)
2529 p = XPROCESS (proc); 2559 p = XPROCESS (proc);
2530 2560
2531 fd = serial_open (port); 2561 fd = serial_open (port);
2562 p->open_fd[SUBPROCESS_STDIN] = fd;
2532 p->infd = fd; 2563 p->infd = fd;
2533 p->outfd = fd; 2564 p->outfd = fd;
2534 if (fd > max_process_desc) 2565 if (fd > max_process_desc)
@@ -3201,7 +3232,7 @@ usage: (make-network-process &rest ARGS) */)
3201 wait for completion is pselect(). */ 3232 wait for completion is pselect(). */
3202 int sc; 3233 int sc;
3203 socklen_t len; 3234 socklen_t len;
3204 SELECT_TYPE fdset; 3235 fd_set fdset;
3205 retry_select: 3236 retry_select:
3206 FD_ZERO (&fdset); 3237 FD_ZERO (&fdset);
3207 FD_SET (s, &fdset); 3238 FD_SET (s, &fdset);
@@ -3291,12 +3322,6 @@ usage: (make-network-process &rest ARGS) */)
3291 } 3322 }
3292#endif 3323#endif
3293 3324
3294 /* Discard the unwind protect for closing S, if any. */
3295 specpdl_ptr = specpdl + count1;
3296
3297 /* Unwind bind_polling_period and request_sigio. */
3298 unbind_to (count, Qnil);
3299
3300 if (s < 0) 3325 if (s < 0)
3301 { 3326 {
3302 /* If non-blocking got this far - and failed - assume non-blocking is 3327 /* If non-blocking got this far - and failed - assume non-blocking is
@@ -3338,8 +3363,17 @@ usage: (make-network-process &rest ARGS) */)
3338 if ((tem = Fplist_get (contact, QCstop), !NILP (tem))) 3363 if ((tem = Fplist_get (contact, QCstop), !NILP (tem)))
3339 pset_command (p, Qt); 3364 pset_command (p, Qt);
3340 p->pid = 0; 3365 p->pid = 0;
3366
3367 p->open_fd[SUBPROCESS_STDIN] = inch;
3341 p->infd = inch; 3368 p->infd = inch;
3342 p->outfd = outch; 3369 p->outfd = outch;
3370
3371 /* Discard the unwind protect for closing S, if any. */
3372 specpdl_ptr = specpdl + count1;
3373
3374 /* Unwind bind_polling_period and request_sigio. */
3375 unbind_to (count, Qnil);
3376
3343 if (is_server && socktype != SOCK_DGRAM) 3377 if (is_server && socktype != SOCK_DGRAM)
3344 pset_status (p, Qlisten); 3378 pset_status (p, Qlisten);
3345 3379
@@ -3778,17 +3812,15 @@ FLAGS is the current flags of the interface. */)
3778static void 3812static void
3779deactivate_process (Lisp_Object proc) 3813deactivate_process (Lisp_Object proc)
3780{ 3814{
3781 register int inchannel, outchannel; 3815 int inchannel;
3782 register struct Lisp_Process *p = XPROCESS (proc); 3816 struct Lisp_Process *p = XPROCESS (proc);
3817 int i;
3783 3818
3784#ifdef HAVE_GNUTLS 3819#ifdef HAVE_GNUTLS
3785 /* Delete GnuTLS structures in PROC, if any. */ 3820 /* Delete GnuTLS structures in PROC, if any. */
3786 emacs_gnutls_deinit (proc); 3821 emacs_gnutls_deinit (proc);
3787#endif /* HAVE_GNUTLS */ 3822#endif /* HAVE_GNUTLS */
3788 3823
3789 inchannel = p->infd;
3790 outchannel = p->outfd;
3791
3792#ifdef ADAPTIVE_READ_BUFFERING 3824#ifdef ADAPTIVE_READ_BUFFERING
3793 if (p->read_output_delay > 0) 3825 if (p->read_output_delay > 0)
3794 { 3826 {
@@ -3799,14 +3831,14 @@ deactivate_process (Lisp_Object proc)
3799 } 3831 }
3800#endif 3832#endif
3801 3833
3834 /* Beware SIGCHLD hereabouts. */
3835
3836 for (i = 0; i < PROCESS_OPEN_FDS; i++)
3837 close_process_fd (&p->open_fd[i]);
3838
3839 inchannel = p->infd;
3802 if (inchannel >= 0) 3840 if (inchannel >= 0)
3803 { 3841 {
3804 /* Beware SIGCHLD hereabouts. */
3805 flush_pending_output (inchannel);
3806 emacs_close (inchannel);
3807 if (outchannel >= 0 && outchannel != inchannel)
3808 emacs_close (outchannel);
3809
3810 p->infd = -1; 3842 p->infd = -1;
3811 p->outfd = -1; 3843 p->outfd = -1;
3812#ifdef DATAGRAM_SOCKETS 3844#ifdef DATAGRAM_SOCKETS
@@ -3831,13 +3863,14 @@ deactivate_process (Lisp_Object proc)
3831#endif 3863#endif
3832 if (inchannel == max_process_desc) 3864 if (inchannel == max_process_desc)
3833 { 3865 {
3834 int i;
3835 /* We just closed the highest-numbered process input descriptor, 3866 /* We just closed the highest-numbered process input descriptor,
3836 so recompute the highest-numbered one now. */ 3867 so recompute the highest-numbered one now. */
3837 max_process_desc = 0; 3868 int i = inchannel;
3838 for (i = 0; i < MAXDESC; i++) 3869 do
3839 if (!NILP (chan_process[i])) 3870 i--;
3840 max_process_desc = i; 3871 while (0 <= i && NILP (chan_process[i]));
3872
3873 max_process_desc = i;
3841 } 3874 }
3842 } 3875 }
3843} 3876}
@@ -3899,9 +3932,9 @@ Return non-nil if we received any output before the timeout expired. */)
3899 { 3932 {
3900 if (XFLOAT_DATA (seconds) > 0) 3933 if (XFLOAT_DATA (seconds) > 0)
3901 { 3934 {
3902 EMACS_TIME t = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (seconds)); 3935 struct timespec t = dtotimespec (XFLOAT_DATA (seconds));
3903 secs = min (EMACS_SECS (t), WAIT_READING_MAX); 3936 secs = min (t.tv_sec, WAIT_READING_MAX);
3904 nsecs = EMACS_NSECS (t); 3937 nsecs = t.tv_nsec;
3905 } 3938 }
3906 } 3939 }
3907 else 3940 else
@@ -4088,6 +4121,7 @@ server_accept_connection (Lisp_Object server, int channel)
4088 /* Discard the unwind protect for closing S. */ 4121 /* Discard the unwind protect for closing S. */
4089 specpdl_ptr = specpdl + count; 4122 specpdl_ptr = specpdl + count;
4090 4123
4124 p->open_fd[SUBPROCESS_STDIN] = s;
4091 p->infd = s; 4125 p->infd = s;
4092 p->outfd = s; 4126 p->outfd = s;
4093 pset_status (p, Qrun); 4127 pset_status (p, Qrun);
@@ -4198,14 +4232,14 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4198 struct Lisp_Process *wait_proc, int just_wait_proc) 4232 struct Lisp_Process *wait_proc, int just_wait_proc)
4199{ 4233{
4200 int channel, nfds; 4234 int channel, nfds;
4201 SELECT_TYPE Available; 4235 fd_set Available;
4202 SELECT_TYPE Writeok; 4236 fd_set Writeok;
4203 bool check_write; 4237 bool check_write;
4204 int check_delay; 4238 int check_delay;
4205 bool no_avail; 4239 bool no_avail;
4206 int xerrno; 4240 int xerrno;
4207 Lisp_Object proc; 4241 Lisp_Object proc;
4208 EMACS_TIME timeout, end_time; 4242 struct timespec timeout, end_time;
4209 int wait_channel = -1; 4243 int wait_channel = -1;
4210 bool got_some_input = 0; 4244 bool got_some_input = 0;
4211 ptrdiff_t count = SPECPDL_INDEX (); 4245 ptrdiff_t count = SPECPDL_INDEX ();
@@ -4238,8 +4272,8 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4238 compute the absolute time to return at. */ 4272 compute the absolute time to return at. */
4239 if (time_limit || nsecs > 0) 4273 if (time_limit || nsecs > 0)
4240 { 4274 {
4241 timeout = make_emacs_time (time_limit, nsecs); 4275 timeout = make_timespec (time_limit, nsecs);
4242 end_time = add_emacs_time (current_emacs_time (), timeout); 4276 end_time = timespec_add (current_timespec (), timeout);
4243 } 4277 }
4244 4278
4245 while (1) 4279 while (1)
@@ -4266,18 +4300,18 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4266 gobble output available now 4300 gobble output available now
4267 but don't wait at all. */ 4301 but don't wait at all. */
4268 4302
4269 timeout = make_emacs_time (0, 0); 4303 timeout = make_timespec (0, 0);
4270 } 4304 }
4271 else if (time_limit || nsecs > 0) 4305 else if (time_limit || nsecs > 0)
4272 { 4306 {
4273 EMACS_TIME now = current_emacs_time (); 4307 struct timespec now = current_timespec ();
4274 if (EMACS_TIME_LE (end_time, now)) 4308 if (timespec_cmp (end_time, now) <= 0)
4275 break; 4309 break;
4276 timeout = sub_emacs_time (end_time, now); 4310 timeout = timespec_sub (end_time, now);
4277 } 4311 }
4278 else 4312 else
4279 { 4313 {
4280 timeout = make_emacs_time (100000, 0); 4314 timeout = make_timespec (100000, 0);
4281 } 4315 }
4282 4316
4283 /* Normally we run timers here. 4317 /* Normally we run timers here.
@@ -4287,7 +4321,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4287 if (NILP (wait_for_cell) 4321 if (NILP (wait_for_cell)
4288 && just_wait_proc >= 0) 4322 && just_wait_proc >= 0)
4289 { 4323 {
4290 EMACS_TIME timer_delay; 4324 struct timespec timer_delay;
4291 4325
4292 do 4326 do
4293 { 4327 {
@@ -4322,9 +4356,9 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4322 /* A negative timeout means do not wait at all. */ 4356 /* A negative timeout means do not wait at all. */
4323 if (nsecs >= 0) 4357 if (nsecs >= 0)
4324 { 4358 {
4325 if (EMACS_TIME_VALID_P (timer_delay)) 4359 if (timespec_valid_p (timer_delay))
4326 { 4360 {
4327 if (EMACS_TIME_LT (timer_delay, timeout)) 4361 if (timespec_cmp (timer_delay, timeout) < 0)
4328 { 4362 {
4329 timeout = timer_delay; 4363 timeout = timer_delay;
4330 timeout_reduced_for_timers = 1; 4364 timeout_reduced_for_timers = 1;
@@ -4353,8 +4387,8 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4353 timeout to get our attention. */ 4387 timeout to get our attention. */
4354 if (update_tick != process_tick) 4388 if (update_tick != process_tick)
4355 { 4389 {
4356 SELECT_TYPE Atemp; 4390 fd_set Atemp;
4357 SELECT_TYPE Ctemp; 4391 fd_set Ctemp;
4358 4392
4359 if (kbd_on_hold_p ()) 4393 if (kbd_on_hold_p ())
4360 FD_ZERO (&Atemp); 4394 FD_ZERO (&Atemp);
@@ -4362,7 +4396,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4362 Atemp = input_wait_mask; 4396 Atemp = input_wait_mask;
4363 Ctemp = write_mask; 4397 Ctemp = write_mask;
4364 4398
4365 timeout = make_emacs_time (0, 0); 4399 timeout = make_timespec (0, 0);
4366 if ((pselect (max (max_process_desc, max_input_desc) + 1, 4400 if ((pselect (max (max_process_desc, max_input_desc) + 1,
4367 &Atemp, 4401 &Atemp,
4368#ifdef NON_BLOCKING_CONNECT 4402#ifdef NON_BLOCKING_CONNECT
@@ -4484,8 +4518,8 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4484 Vprocess_adaptive_read_buffering is nil. */ 4518 Vprocess_adaptive_read_buffering is nil. */
4485 if (process_output_skip && check_delay > 0) 4519 if (process_output_skip && check_delay > 0)
4486 { 4520 {
4487 int nsecs = EMACS_NSECS (timeout); 4521 int nsecs = timeout.tv_nsec;
4488 if (EMACS_SECS (timeout) > 0 || nsecs > READ_OUTPUT_DELAY_MAX) 4522 if (timeout.tv_sec > 0 || nsecs > READ_OUTPUT_DELAY_MAX)
4489 nsecs = READ_OUTPUT_DELAY_MAX; 4523 nsecs = READ_OUTPUT_DELAY_MAX;
4490 for (channel = 0; check_delay > 0 && channel <= max_process_desc; channel++) 4524 for (channel = 0; check_delay > 0 && channel <= max_process_desc; channel++)
4491 { 4525 {
@@ -4505,7 +4539,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4505 nsecs = XPROCESS (proc)->read_output_delay; 4539 nsecs = XPROCESS (proc)->read_output_delay;
4506 } 4540 }
4507 } 4541 }
4508 timeout = make_emacs_time (0, nsecs); 4542 timeout = make_timespec (0, nsecs);
4509 process_output_skip = 0; 4543 process_output_skip = 0;
4510 } 4544 }
4511#endif 4545#endif
@@ -4519,7 +4553,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4519#endif 4553#endif
4520 (max (max_process_desc, max_input_desc) + 1, 4554 (max (max_process_desc, max_input_desc) + 1,
4521 &Available, 4555 &Available,
4522 (check_write ? &Writeok : (SELECT_TYPE *)0), 4556 (check_write ? &Writeok : 0),
4523 NULL, &timeout, NULL); 4557 NULL, &timeout, NULL);
4524 4558
4525#ifdef HAVE_GNUTLS 4559#ifdef HAVE_GNUTLS
@@ -4537,7 +4571,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4537 the gnutls library -- 2.12.14 has been confirmed 4571 the gnutls library -- 2.12.14 has been confirmed
4538 to need it. See 4572 to need it. See
4539 http://comments.gmane.org/gmane.emacs.devel/145074 */ 4573 http://comments.gmane.org/gmane.emacs.devel/145074 */
4540 for (channel = 0; channel < MAXDESC; ++channel) 4574 for (channel = 0; channel < FD_SETSIZE; ++channel)
4541 if (! NILP (chan_process[channel])) 4575 if (! NILP (chan_process[channel]))
4542 { 4576 {
4543 struct Lisp_Process *p = 4577 struct Lisp_Process *p =
@@ -5144,15 +5178,10 @@ DEFUN ("internal-default-process-filter", Finternal_default_process_filter,
5144 5178
5145 bset_read_only (current_buffer, Qnil); 5179 bset_read_only (current_buffer, Qnil);
5146 5180
5147 /* Insert new output into buffer 5181 /* Insert new output into buffer at the current end-of-output
5148 at the current end-of-output marker, 5182 marker, thus preserving logical ordering of input and output. */
5149 thus preserving logical ordering of input and output. */
5150 if (XMARKER (p->mark)->buffer) 5183 if (XMARKER (p->mark)->buffer)
5151 SET_PT_BOTH (clip_to_bounds (BEGV, 5184 set_point_from_marker (p->mark);
5152 marker_position (p->mark), ZV),
5153 clip_to_bounds (BEGV_BYTE,
5154 marker_byte_position (p->mark),
5155 ZV_BYTE));
5156 else 5185 else
5157 SET_PT_BOTH (ZV, ZV_BYTE); 5186 SET_PT_BOTH (ZV, ZV_BYTE);
5158 before = PT; 5187 before = PT;
@@ -5733,10 +5762,9 @@ process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group,
5733 return; 5762 return;
5734 } 5763 }
5735 5764
5736 switch (signo)
5737 {
5738#ifdef SIGCONT 5765#ifdef SIGCONT
5739 case SIGCONT: 5766 if (signo == SIGCONT)
5767 {
5740 p->raw_status_new = 0; 5768 p->raw_status_new = 0;
5741 pset_status (p, Qrun); 5769 pset_status (p, Qrun);
5742 p->tick = ++process_tick; 5770 p->tick = ++process_tick;
@@ -5745,14 +5773,8 @@ process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group,
5745 status_notify (NULL); 5773 status_notify (NULL);
5746 redisplay_preserve_echo_area (13); 5774 redisplay_preserve_echo_area (13);
5747 } 5775 }
5748 break;
5749#endif /* ! defined (SIGCONT) */
5750 case SIGINT:
5751 case SIGQUIT:
5752 case SIGKILL:
5753 flush_pending_output (p->infd);
5754 break;
5755 } 5776 }
5777#endif
5756 5778
5757 /* If we don't have process groups, send the signal to the immediate 5779 /* If we don't have process groups, send the signal to the immediate
5758 subprocess. That isn't really right, but it's better than any 5780 subprocess. That isn't really right, but it's better than any
@@ -6007,7 +6029,8 @@ process has been transmitted to the serial port. */)
6007 } 6029 }
6008 else 6030 else
6009 { 6031 {
6010 int old_outfd, new_outfd; 6032 int old_outfd = XPROCESS (proc)->outfd;
6033 int new_outfd;
6011 6034
6012#ifdef HAVE_SHUTDOWN 6035#ifdef HAVE_SHUTDOWN
6013 /* If this is a network connection, or socketpair is used 6036 /* If this is a network connection, or socketpair is used
@@ -6015,18 +6038,15 @@ process has been transmitted to the serial port. */)
6015 (In some old system, shutdown to socketpair doesn't work. 6038 (In some old system, shutdown to socketpair doesn't work.
6016 Then we just can't win.) */ 6039 Then we just can't win.) */
6017 if (EQ (XPROCESS (proc)->type, Qnetwork) 6040 if (EQ (XPROCESS (proc)->type, Qnetwork)
6018 || XPROCESS (proc)->outfd == XPROCESS (proc)->infd) 6041 || XPROCESS (proc)->infd == old_outfd)
6019 shutdown (XPROCESS (proc)->outfd, 1); 6042 shutdown (old_outfd, 1);
6020 /* In case of socketpair, outfd == infd, so don't close it. */ 6043#endif
6021 if (XPROCESS (proc)->outfd != XPROCESS (proc)->infd) 6044 close_process_fd (&XPROCESS (proc)->open_fd[WRITE_TO_SUBPROCESS]);
6022 emacs_close (XPROCESS (proc)->outfd);
6023#else /* not HAVE_SHUTDOWN */
6024 emacs_close (XPROCESS (proc)->outfd);
6025#endif /* not HAVE_SHUTDOWN */
6026 new_outfd = emacs_open (NULL_DEVICE, O_WRONLY, 0); 6045 new_outfd = emacs_open (NULL_DEVICE, O_WRONLY, 0);
6027 if (new_outfd < 0) 6046 if (new_outfd < 0)
6028 emacs_abort (); 6047 report_file_error ("Opening null device", Qnil);
6029 old_outfd = XPROCESS (proc)->outfd; 6048 XPROCESS (proc)->open_fd[WRITE_TO_SUBPROCESS] = new_outfd;
6049 XPROCESS (proc)->outfd = new_outfd;
6030 6050
6031 if (!proc_encode_coding_system[new_outfd]) 6051 if (!proc_encode_coding_system[new_outfd])
6032 proc_encode_coding_system[new_outfd] 6052 proc_encode_coding_system[new_outfd]
@@ -6035,8 +6055,6 @@ process has been transmitted to the serial port. */)
6035 = *proc_encode_coding_system[old_outfd]; 6055 = *proc_encode_coding_system[old_outfd];
6036 memset (proc_encode_coding_system[old_outfd], 0, 6056 memset (proc_encode_coding_system[old_outfd], 0,
6037 sizeof (struct coding_system)); 6057 sizeof (struct coding_system));
6038
6039 XPROCESS (proc)->outfd = new_outfd;
6040 } 6058 }
6041 return process; 6059 return process;
6042} 6060}
@@ -6102,7 +6120,7 @@ static signal_handler_t volatile lib_child_handler;
6102static void 6120static void
6103handle_child_signal (int sig) 6121handle_child_signal (int sig)
6104{ 6122{
6105 Lisp_Object tail; 6123 Lisp_Object tail, proc;
6106 6124
6107 /* Find the process that signaled us, and record its status. */ 6125 /* Find the process that signaled us, and record its status. */
6108 6126
@@ -6113,7 +6131,11 @@ handle_child_signal (int sig)
6113 bool all_pids_are_fixnums 6131 bool all_pids_are_fixnums
6114 = (MOST_NEGATIVE_FIXNUM <= TYPE_MINIMUM (pid_t) 6132 = (MOST_NEGATIVE_FIXNUM <= TYPE_MINIMUM (pid_t)
6115 && TYPE_MAXIMUM (pid_t) <= MOST_POSITIVE_FIXNUM); 6133 && TYPE_MAXIMUM (pid_t) <= MOST_POSITIVE_FIXNUM);
6116 Lisp_Object xpid = XCAR (tail); 6134 Lisp_Object head = XCAR (tail);
6135 Lisp_Object xpid;
6136 if (! CONSP (head))
6137 continue;
6138 xpid = XCAR (head);
6117 if (all_pids_are_fixnums ? INTEGERP (xpid) : NUMBERP (xpid)) 6139 if (all_pids_are_fixnums ? INTEGERP (xpid) : NUMBERP (xpid))
6118 { 6140 {
6119 pid_t deleted_pid; 6141 pid_t deleted_pid;
@@ -6122,14 +6144,17 @@ handle_child_signal (int sig)
6122 else 6144 else
6123 deleted_pid = XFLOAT_DATA (xpid); 6145 deleted_pid = XFLOAT_DATA (xpid);
6124 if (child_status_changed (deleted_pid, 0, 0)) 6146 if (child_status_changed (deleted_pid, 0, 0))
6125 XSETCAR (tail, Qnil); 6147 {
6148 if (STRINGP (XCDR (head)))
6149 unlink (SSDATA (XCDR (head)));
6150 XSETCAR (tail, Qnil);
6151 }
6126 } 6152 }
6127 } 6153 }
6128 6154
6129 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */ 6155 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6130 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) 6156 FOR_EACH_PROCESS (tail, proc)
6131 { 6157 {
6132 Lisp_Object proc = XCDR (XCAR (tail));
6133 struct Lisp_Process *p = XPROCESS (proc); 6158 struct Lisp_Process *p = XPROCESS (proc);
6134 int status; 6159 int status;
6135 6160
@@ -6284,13 +6309,10 @@ status_notify (struct Lisp_Process *deleting_process)
6284 that we run, we get called again to handle their status changes. */ 6309 that we run, we get called again to handle their status changes. */
6285 update_tick = process_tick; 6310 update_tick = process_tick;
6286 6311
6287 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) 6312 FOR_EACH_PROCESS (tail, proc)
6288 { 6313 {
6289 Lisp_Object symbol; 6314 Lisp_Object symbol;
6290 register struct Lisp_Process *p; 6315 register struct Lisp_Process *p = XPROCESS (proc);
6291
6292 proc = Fcdr (XCAR (tail));
6293 p = XPROCESS (proc);
6294 6316
6295 if (p->tick != p->update_tick) 6317 if (p->tick != p->update_tick)
6296 { 6318 {
@@ -6515,8 +6537,8 @@ keyboard_bit_set (fd_set *mask)
6515#else /* not subprocesses */ 6537#else /* not subprocesses */
6516 6538
6517/* Defined on msdos.c. */ 6539/* Defined on msdos.c. */
6518extern int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *, 6540extern int sys_select (int, fd_set *, fd_set *, fd_set *,
6519 EMACS_TIME *, void *); 6541 struct timespec *, void *);
6520 6542
6521/* Implementation of wait_reading_process_output, assuming that there 6543/* Implementation of wait_reading_process_output, assuming that there
6522 are no subprocesses. Used only by the MS-DOS build. 6544 are no subprocesses. Used only by the MS-DOS build.
@@ -6555,7 +6577,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6555 struct Lisp_Process *wait_proc, int just_wait_proc) 6577 struct Lisp_Process *wait_proc, int just_wait_proc)
6556{ 6578{
6557 register int nfds; 6579 register int nfds;
6558 EMACS_TIME end_time, timeout; 6580 struct timespec end_time, timeout;
6559 6581
6560 if (time_limit < 0) 6582 if (time_limit < 0)
6561 { 6583 {
@@ -6568,8 +6590,8 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6568 /* What does time_limit really mean? */ 6590 /* What does time_limit really mean? */
6569 if (time_limit || nsecs > 0) 6591 if (time_limit || nsecs > 0)
6570 { 6592 {
6571 timeout = make_emacs_time (time_limit, nsecs); 6593 timeout = make_timespec (time_limit, nsecs);
6572 end_time = add_emacs_time (current_emacs_time (), timeout); 6594 end_time = timespec_add (current_timespec (), timeout);
6573 } 6595 }
6574 6596
6575 /* Turn off periodic alarms (in case they are in use) 6597 /* Turn off periodic alarms (in case they are in use)
@@ -6581,7 +6603,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6581 while (1) 6603 while (1)
6582 { 6604 {
6583 bool timeout_reduced_for_timers = 0; 6605 bool timeout_reduced_for_timers = 0;
6584 SELECT_TYPE waitchannels; 6606 fd_set waitchannels;
6585 int xerrno; 6607 int xerrno;
6586 6608
6587 /* If calling from keyboard input, do not quit 6609 /* If calling from keyboard input, do not quit
@@ -6602,18 +6624,18 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6602 gobble output available now 6624 gobble output available now
6603 but don't wait at all. */ 6625 but don't wait at all. */
6604 6626
6605 timeout = make_emacs_time (0, 0); 6627 timeout = make_timespec (0, 0);
6606 } 6628 }
6607 else if (time_limit || nsecs > 0) 6629 else if (time_limit || nsecs > 0)
6608 { 6630 {
6609 EMACS_TIME now = current_emacs_time (); 6631 struct timespec now = current_timespec ();
6610 if (EMACS_TIME_LE (end_time, now)) 6632 if (timespec_cmp (end_time, now) <= 0)
6611 break; 6633 break;
6612 timeout = sub_emacs_time (end_time, now); 6634 timeout = timespec_sub (end_time, now);
6613 } 6635 }
6614 else 6636 else
6615 { 6637 {
6616 timeout = make_emacs_time (100000, 0); 6638 timeout = make_timespec (100000, 0);
6617 } 6639 }
6618 6640
6619 /* If our caller will not immediately handle keyboard events, 6641 /* If our caller will not immediately handle keyboard events,
@@ -6622,7 +6644,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6622 call timer_delay on their own.) */ 6644 call timer_delay on their own.) */
6623 if (NILP (wait_for_cell)) 6645 if (NILP (wait_for_cell))
6624 { 6646 {
6625 EMACS_TIME timer_delay; 6647 struct timespec timer_delay;
6626 6648
6627 do 6649 do
6628 { 6650 {
@@ -6642,9 +6664,9 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6642 && requeued_events_pending_p ()) 6664 && requeued_events_pending_p ())
6643 break; 6665 break;
6644 6666
6645 if (EMACS_TIME_VALID_P (timer_delay) && nsecs >= 0) 6667 if (timespec_valid_p (timer_delay) && nsecs >= 0)
6646 { 6668 {
6647 if (EMACS_TIME_LT (timer_delay, timeout)) 6669 if (timespec_cmp (timer_delay, timeout) < 0)
6648 { 6670 {
6649 timeout = timer_delay; 6671 timeout = timer_delay;
6650 timeout_reduced_for_timers = 1; 6672 timeout_reduced_for_timers = 1;
@@ -6763,16 +6785,9 @@ void
6763delete_keyboard_wait_descriptor (int desc) 6785delete_keyboard_wait_descriptor (int desc)
6764{ 6786{
6765#ifdef subprocesses 6787#ifdef subprocesses
6766 int fd;
6767 int lim = max_input_desc;
6768
6769 FD_CLR (desc, &input_wait_mask); 6788 FD_CLR (desc, &input_wait_mask);
6770 FD_CLR (desc, &non_process_wait_mask); 6789 FD_CLR (desc, &non_process_wait_mask);
6771 6790 delete_input_desc (desc);
6772 if (desc == max_input_desc)
6773 for (fd = 0; fd < lim; fd++)
6774 if (FD_ISSET (fd, &input_wait_mask) || FD_ISSET (fd, &write_mask))
6775 max_input_desc = fd;
6776#endif 6791#endif
6777} 6792}
6778 6793
@@ -6820,12 +6835,9 @@ BUFFER may be a buffer or the name of one. */)
6820 buf = Fget_buffer (buffer); 6835 buf = Fget_buffer (buffer);
6821 if (NILP (buf)) return Qnil; 6836 if (NILP (buf)) return Qnil;
6822 6837
6823 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) 6838 FOR_EACH_PROCESS (tail, proc)
6824 { 6839 if (EQ (XPROCESS (proc)->buffer, buf))
6825 proc = Fcdr (XCAR (tail)); 6840 return proc;
6826 if (PROCESSP (proc) && EQ (XPROCESS (proc)->buffer, buf))
6827 return proc;
6828 }
6829#endif /* subprocesses */ 6841#endif /* subprocesses */
6830 return Qnil; 6842 return Qnil;
6831} 6843}
@@ -6858,18 +6870,14 @@ kill_buffer_processes (Lisp_Object buffer)
6858#ifdef subprocesses 6870#ifdef subprocesses
6859 Lisp_Object tail, proc; 6871 Lisp_Object tail, proc;
6860 6872
6861 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) 6873 FOR_EACH_PROCESS (tail, proc)
6862 { 6874 if (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer))
6863 proc = XCDR (XCAR (tail)); 6875 {
6864 if (PROCESSP (proc) 6876 if (NETCONN_P (proc) || SERIALCONN_P (proc))
6865 && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer))) 6877 Fdelete_process (proc);
6866 { 6878 else if (XPROCESS (proc)->infd >= 0)
6867 if (NETCONN_P (proc) || SERIALCONN_P (proc)) 6879 process_send_signal (proc, SIGHUP, Qnil, 1);
6868 Fdelete_process (proc); 6880 }
6869 else if (XPROCESS (proc)->infd >= 0)
6870 process_send_signal (proc, SIGHUP, Qnil, 1);
6871 }
6872 }
6873#else /* subprocesses */ 6881#else /* subprocesses */
6874 /* Since we have no subprocesses, this does nothing. */ 6882 /* Since we have no subprocesses, this does nothing. */
6875#endif /* subprocesses */ 6883#endif /* subprocesses */
@@ -7037,7 +7045,7 @@ init_process_emacs (void)
7037 FD_ZERO (&non_keyboard_wait_mask); 7045 FD_ZERO (&non_keyboard_wait_mask);
7038 FD_ZERO (&non_process_wait_mask); 7046 FD_ZERO (&non_process_wait_mask);
7039 FD_ZERO (&write_mask); 7047 FD_ZERO (&write_mask);
7040 max_process_desc = 0; 7048 max_process_desc = max_input_desc = -1;
7041 memset (fd_callback_info, 0, sizeof (fd_callback_info)); 7049 memset (fd_callback_info, 0, sizeof (fd_callback_info));
7042 7050
7043#ifdef NON_BLOCKING_CONNECT 7051#ifdef NON_BLOCKING_CONNECT
@@ -7059,7 +7067,7 @@ init_process_emacs (void)
7059 7067
7060 Vprocess_alist = Qnil; 7068 Vprocess_alist = Qnil;
7061 deleted_pid_list = Qnil; 7069 deleted_pid_list = Qnil;
7062 for (i = 0; i < MAXDESC; i++) 7070 for (i = 0; i < FD_SETSIZE; i++)
7063 { 7071 {
7064 chan_process[i] = Qnil; 7072 chan_process[i] = Qnil;
7065 proc_buffered_char[i] = -1; 7073 proc_buffered_char[i] = -1;