aboutsummaryrefslogtreecommitdiffstats
path: root/src/process.c
diff options
context:
space:
mode:
authorJoakim Verona2012-11-06 23:01:10 +0100
committerJoakim Verona2012-11-06 23:01:10 +0100
commit643b1893347ee5c32f6174d0f76a55d723060123 (patch)
tree06847c9375f32e91446bfdbe9aa342cb232be90f /src/process.c
parent9c809558c18410acf9b90529e183d192afca3d2c (diff)
parentb6b3b29458fefcf7f31a0b14d6484cf262db2e54 (diff)
downloademacs-643b1893347ee5c32f6174d0f76a55d723060123.tar.gz
emacs-643b1893347ee5c32f6174d0f76a55d723060123.zip
upstream
Diffstat (limited to 'src/process.c')
-rw-r--r--src/process.c221
1 files changed, 103 insertions, 118 deletions
diff --git a/src/process.c b/src/process.c
index 307e82819d6..43f0239d301 100644
--- a/src/process.c
+++ b/src/process.c
@@ -130,6 +130,10 @@ extern int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *,
130 EMACS_TIME *, void *); 130 EMACS_TIME *, void *);
131#endif 131#endif
132 132
133/* This is for DOS_NT ports. FIXME: Remove this old portability cruft
134 by having DOS_NT ports implement waitpid instead of wait. Nowadays
135 POSIXish hosts all define waitpid, WNOHANG, and WUNTRACED, as these
136 have been standard since POSIX.1-1988. */
133#ifndef WNOHANG 137#ifndef WNOHANG
134# undef waitpid 138# undef waitpid
135# define waitpid(pid, status, options) wait (status) 139# define waitpid(pid, status, options) wait (status)
@@ -795,9 +799,8 @@ get_process (register Lisp_Object name)
795#ifdef SIGCHLD 799#ifdef SIGCHLD
796/* Fdelete_process promises to immediately forget about the process, but in 800/* Fdelete_process promises to immediately forget about the process, but in
797 reality, Emacs needs to remember those processes until they have been 801 reality, Emacs needs to remember those processes until they have been
798 treated by the SIGCHLD handler; otherwise this handler would consider the 802 treated by the SIGCHLD handler and waitpid has been invoked on them;
799 process as being synchronous and say that the synchronous process is 803 otherwise they might fill up the kernel's process table. */
800 dead. */
801static Lisp_Object deleted_pid_list; 804static Lisp_Object deleted_pid_list;
802#endif 805#endif
803 806
@@ -1704,16 +1707,7 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1704 if (inchannel > max_process_desc) 1707 if (inchannel > max_process_desc)
1705 max_process_desc = inchannel; 1708 max_process_desc = inchannel;
1706 1709
1707 /* Until we store the proper pid, enable the SIGCHLD handler 1710 /* This may signal an error. */
1708 to recognize an unknown pid as standing for this process.
1709 It is very important not to let this `marker' value stay
1710 in the table after this function has returned; if it does
1711 it might cause call-process to hang and subsequent asynchronous
1712 processes to get their return values scrambled. */
1713 XPROCESS (process)->pid = -1;
1714
1715 /* This must be called after the above line because it may signal an
1716 error. */
1717 setup_process_coding_systems (process); 1711 setup_process_coding_systems (process);
1718 1712
1719 encoded_current_dir = ENCODE_FILE (current_dir); 1713 encoded_current_dir = ENCODE_FILE (current_dir);
@@ -1745,7 +1739,6 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1745 /* Make the pty be the controlling terminal of the process. */ 1739 /* Make the pty be the controlling terminal of the process. */
1746#ifdef HAVE_PTYS 1740#ifdef HAVE_PTYS
1747 /* First, disconnect its current controlling terminal. */ 1741 /* First, disconnect its current controlling terminal. */
1748#ifdef HAVE_SETSID
1749 /* We tried doing setsid only if pty_flag, but it caused 1742 /* We tried doing setsid only if pty_flag, but it caused
1750 process_set_signal to fail on SGI when using a pipe. */ 1743 process_set_signal to fail on SGI when using a pipe. */
1751 setsid (); 1744 setsid ();
@@ -1758,14 +1751,6 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1758 ioctl (xforkin, TIOCSCTTY, 0); 1751 ioctl (xforkin, TIOCSCTTY, 0);
1759#endif 1752#endif
1760 } 1753 }
1761#else /* not HAVE_SETSID */
1762#ifdef USG
1763 /* It's very important to call setpgrp here and no time
1764 afterwards. Otherwise, we lose our controlling tty which
1765 is set when we open the pty. */
1766 setpgrp ();
1767#endif /* USG */
1768#endif /* not HAVE_SETSID */
1769#if defined (LDISC1) 1754#if defined (LDISC1)
1770 if (pty_flag && xforkin >= 0) 1755 if (pty_flag && xforkin >= 0)
1771 { 1756 {
@@ -1798,26 +1783,15 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1798 ioctl (j, TIOCNOTTY, 0); 1783 ioctl (j, TIOCNOTTY, 0);
1799 emacs_close (j); 1784 emacs_close (j);
1800 } 1785 }
1801#ifndef USG
1802 /* In order to get a controlling terminal on some versions
1803 of BSD, it is necessary to put the process in pgrp 0
1804 before it opens the terminal. */
1805#ifdef HAVE_SETPGID
1806 setpgid (0, 0);
1807#else
1808 setpgrp (0, 0);
1809#endif
1810#endif
1811 } 1786 }
1812#endif /* TIOCNOTTY */ 1787#endif /* TIOCNOTTY */
1813 1788
1814#if !defined (DONT_REOPEN_PTY) 1789#if !defined (DONT_REOPEN_PTY)
1815/*** There is a suggestion that this ought to be a 1790/*** There is a suggestion that this ought to be a
1816 conditional on TIOCSPGRP, 1791 conditional on TIOCSPGRP, or !defined TIOCSCTTY.
1817 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
1818 Trying the latter gave the wrong results on Debian GNU/Linux 1.1; 1792 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
1819 that system does seem to need this code, even though 1793 that system does seem to need this code, even though
1820 both HAVE_SETSID and TIOCSCTTY are defined. */ 1794 both TIOCSCTTY is defined. */
1821 /* Now close the pty (if we had it open) and reopen it. 1795 /* Now close the pty (if we had it open) and reopen it.
1822 This makes the pty the controlling terminal of the subprocess. */ 1796 This makes the pty the controlling terminal of the subprocess. */
1823 if (pty_flag) 1797 if (pty_flag)
@@ -1886,6 +1860,8 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1886#endif 1860#endif
1887 1861
1888 XPROCESS (process)->pid = pid; 1862 XPROCESS (process)->pid = pid;
1863 if (0 <= pid)
1864 XPROCESS (process)->alive = 1;
1889 1865
1890 /* Stop blocking signals in the parent. */ 1866 /* Stop blocking signals in the parent. */
1891#ifdef SIGCHLD 1867#ifdef SIGCHLD
@@ -4443,7 +4419,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4443 if (EMACS_TIME_LT (timer_delay, timeout)) 4419 if (EMACS_TIME_LT (timer_delay, timeout))
4444 { 4420 {
4445 timeout = timer_delay; 4421 timeout = timer_delay;
4446 timeout_reduced_for_timers = 1; 4422 timeout_reduced_for_timers = 1;
4447 } 4423 }
4448 } 4424 }
4449 else 4425 else
@@ -6279,9 +6255,35 @@ process has been transmitted to the serial port. */)
6279 return process; 6255 return process;
6280} 6256}
6281 6257
6282/* On receipt of a signal that a child status has changed, loop asking 6258/* If the status of the process DESIRED has changed, return true and
6283 about children with changed statuses until the system says there 6259 set *STATUS to its exit status; otherwise, return false.
6284 are no more. 6260 If HAVE is nonnegative, assume that HAVE = waitpid (HAVE, STATUS, ...)
6261 has already been invoked, and do not invoke waitpid again. */
6262
6263static bool
6264process_status_retrieved (pid_t desired, pid_t have, int *status)
6265{
6266 if (have < 0)
6267 {
6268 /* Invoke waitpid only with a known process ID; do not invoke
6269 waitpid with a nonpositive argument. Otherwise, Emacs might
6270 reap an unwanted process by mistake. For example, invoking
6271 waitpid (-1, ...) can mess up glib by reaping glib's subprocesses,
6272 so that another thread running glib won't find them. */
6273 do
6274 have = waitpid (desired, status, WNOHANG | WUNTRACED);
6275 while (have < 0 && errno == EINTR);
6276 }
6277
6278 return have == desired;
6279}
6280
6281/* If PID is nonnegative, the child process PID with wait status W has
6282 changed its status; record this and return true.
6283
6284 If PID is negative, ignore W, and look for known child processes
6285 of Emacs whose status have changed. For each one found, record its new
6286 status.
6285 6287
6286 All we do is change the status; we do not run sentinels or print 6288 All we do is change the status; we do not run sentinels or print
6287 notifications. That is saved for the next time keyboard input is 6289 notifications. That is saved for the next time keyboard input is
@@ -6304,13 +6306,23 @@ process has been transmitted to the serial port. */)
6304 ** Malloc WARNING: This should never call malloc either directly or 6306 ** Malloc WARNING: This should never call malloc either directly or
6305 indirectly; if it does, that is a bug */ 6307 indirectly; if it does, that is a bug */
6306 6308
6307/* Record the changed status of the child process PID with wait status W. */
6308void 6309void
6309record_child_status_change (pid_t pid, int w) 6310record_child_status_change (pid_t pid, int w)
6310{ 6311{
6311#ifdef SIGCHLD 6312#ifdef SIGCHLD
6312 Lisp_Object proc; 6313
6313 struct Lisp_Process *p; 6314# ifdef WNOHANG
6315 /* On POSIXish hosts, record at most one child only if we already
6316 know one child that has exited. */
6317 bool record_at_most_one_child = 0 <= pid;
6318# else
6319 /* On DOS_NT (the only porting target that lacks WNOHANG),
6320 record the status of at most one child process, since the SIGCHLD
6321 handler must return right away. If any more processes want to
6322 signal us, we will get another signal. */
6323 bool record_at_most_one_child = 1;
6324# endif
6325
6314 Lisp_Object tail; 6326 Lisp_Object tail;
6315 6327
6316 /* Find the process that signaled us, and record its status. */ 6328 /* Find the process that signaled us, and record its status. */
@@ -6318,68 +6330,69 @@ record_child_status_change (pid_t pid, int w)
6318 /* The process can have been deleted by Fdelete_process. */ 6330 /* The process can have been deleted by Fdelete_process. */
6319 for (tail = deleted_pid_list; CONSP (tail); tail = XCDR (tail)) 6331 for (tail = deleted_pid_list; CONSP (tail); tail = XCDR (tail))
6320 { 6332 {
6333 bool all_pids_are_fixnums
6334 = (MOST_NEGATIVE_FIXNUM <= TYPE_MINIMUM (pid_t)
6335 && TYPE_MAXIMUM (pid_t) <= MOST_POSITIVE_FIXNUM);
6321 Lisp_Object xpid = XCAR (tail); 6336 Lisp_Object xpid = XCAR (tail);
6322 if ((INTEGERP (xpid) && pid == XINT (xpid)) 6337 if (all_pids_are_fixnums ? INTEGERP (xpid) : NUMBERP (xpid))
6323 || (FLOATP (xpid) && pid == XFLOAT_DATA (xpid)))
6324 { 6338 {
6325 XSETCAR (tail, Qnil); 6339 pid_t deleted_pid;
6326 return; 6340 if (INTEGERP (xpid))
6341 deleted_pid = XINT (xpid);
6342 else
6343 deleted_pid = XFLOAT_DATA (xpid);
6344 if (process_status_retrieved (deleted_pid, pid, &w))
6345 {
6346 XSETCAR (tail, Qnil);
6347 if (record_at_most_one_child)
6348 return;
6349 }
6327 } 6350 }
6328 } 6351 }
6329 6352
6330 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */ 6353 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6331 p = 0;
6332 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) 6354 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
6333 { 6355 {
6334 proc = XCDR (XCAR (tail)); 6356 Lisp_Object proc = XCDR (XCAR (tail));
6335 p = XPROCESS (proc); 6357 struct Lisp_Process *p = XPROCESS (proc);
6336 if (EQ (p->type, Qreal) && p->pid == pid) 6358 if (p->alive && process_status_retrieved (p->pid, pid, &w))
6337 break; 6359 {
6338 p = 0; 6360 /* Change the status of the process that was found. */
6339 } 6361 p->tick = ++process_tick;
6340 6362 p->raw_status = w;
6341 /* Look for an asynchronous process whose pid hasn't been filled 6363 p->raw_status_new = 1;
6342 in yet. */
6343 if (! p)
6344 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
6345 {
6346 proc = XCDR (XCAR (tail));
6347 p = XPROCESS (proc);
6348 if (p->pid == -1)
6349 break;
6350 p = 0;
6351 }
6352 6364
6353 /* Change the status of the process that was found. */ 6365 /* If process has terminated, stop waiting for its output. */
6354 if (p) 6366 if (WIFSIGNALED (w) || WIFEXITED (w))
6355 { 6367 {
6356 int clear_desc_flag = 0; 6368 int clear_desc_flag = 0;
6369 p->alive = 0;
6370 if (p->infd >= 0)
6371 clear_desc_flag = 1;
6357 6372
6358 p->tick = ++process_tick; 6373 /* clear_desc_flag avoids a compiler bug in Microsoft C. */
6359 p->raw_status = w; 6374 if (clear_desc_flag)
6360 p->raw_status_new = 1; 6375 {
6376 FD_CLR (p->infd, &input_wait_mask);
6377 FD_CLR (p->infd, &non_keyboard_wait_mask);
6378 }
6379 }
6361 6380
6362 /* If process has terminated, stop waiting for its output. */ 6381 /* Tell wait_reading_process_output that it needs to wake up and
6363 if ((WIFSIGNALED (w) || WIFEXITED (w)) 6382 look around. */
6364 && p->infd >= 0) 6383 if (input_available_clear_time)
6365 clear_desc_flag = 1; 6384 *input_available_clear_time = make_emacs_time (0, 0);
6366 6385
6367 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */ 6386 if (record_at_most_one_child)
6368 if (clear_desc_flag) 6387 return;
6369 {
6370 FD_CLR (p->infd, &input_wait_mask);
6371 FD_CLR (p->infd, &non_keyboard_wait_mask);
6372 } 6388 }
6373
6374 /* Tell wait_reading_process_output that it needs to wake up and
6375 look around. */
6376 if (input_available_clear_time)
6377 *input_available_clear_time = make_emacs_time (0, 0);
6378 } 6389 }
6379 /* There was no asynchronous process found for that pid: we have 6390
6380 a synchronous process. */ 6391 if (0 <= pid)
6381 else
6382 { 6392 {
6393 /* The caller successfully waited for a pid but no asynchronous
6394 process was found for it, so this is a synchronous process. */
6395
6383 synch_process_alive = 0; 6396 synch_process_alive = 0;
6384 6397
6385 /* Report the status of the synchronous process. */ 6398 /* Report the status of the synchronous process. */
@@ -6398,38 +6411,10 @@ record_child_status_change (pid_t pid, int w)
6398 6411
6399#ifdef SIGCHLD 6412#ifdef SIGCHLD
6400 6413
6401/* On some systems, the SIGCHLD handler must return right away. If
6402 any more processes want to signal us, we will get another signal.
6403 Otherwise, loop around to use up all the processes that have
6404 something to tell us. */
6405#if (defined WINDOWSNT \
6406 || (defined USG && !defined GNU_LINUX \
6407 && !(defined HPUX && defined WNOHANG)))
6408enum { CAN_HANDLE_MULTIPLE_CHILDREN = 0 };
6409#else
6410enum { CAN_HANDLE_MULTIPLE_CHILDREN = 1 };
6411#endif
6412
6413static void 6414static void
6414handle_child_signal (int sig) 6415handle_child_signal (int sig)
6415{ 6416{
6416 do 6417 record_child_status_change (-1, 0);
6417 {
6418 pid_t pid;
6419 int status;
6420
6421 do
6422 pid = waitpid (-1, &status, WNOHANG | WUNTRACED);
6423 while (pid < 0 && errno == EINTR);
6424
6425 /* PID == 0 means no processes found, PID == -1 means a real failure.
6426 Either way, we have done all our job. */
6427 if (pid <= 0)
6428 break;
6429
6430 record_child_status_change (pid, status);
6431 }
6432 while (CAN_HANDLE_MULTIPLE_CHILDREN);
6433} 6418}
6434 6419
6435static void 6420static void