diff options
| author | Paul Eggert | 2012-11-23 14:20:31 -0800 |
|---|---|---|
| committer | Paul Eggert | 2012-11-23 14:20:31 -0800 |
| commit | 6d4e8f62e93b575a1da2cd2b4abeb9dce56e1e52 (patch) | |
| tree | 2d3564ca26405db0e887bea037dcc175d85cc753 /src/process.c | |
| parent | 002c019c34eeb1cad4ce8f5ae721b1cdf22f0946 (diff) | |
| download | emacs-6d4e8f62e93b575a1da2cd2b4abeb9dce56e1e52.tar.gz emacs-6d4e8f62e93b575a1da2cd2b4abeb9dce56e1e52.zip | |
Fix a race condition with glib (Bug#8855).
This is a backport from the trunk, consisting of:
2012-11-17 Eli Zaretskii <eliz@gnu.org>
* nt/inc/sys/wait.h: New file, with prototype of waitpid and
definitions of macros it needs.
* nt/inc/ms-w32.h (wait): Don't define, 'wait' is not used anymore.
(sys_wait): Remove prototype.
* nt/config.nt (HAVE_SYS_WAIT_H): Define to 1.
* src/w32proc.c (create_child): Don't clip the PID of the child
process to fit into an Emacs integer, as this is no longer a
restriction.
(waitpid): Rename from sys_wait. Emulate a Posix 'waitpid' by
reaping only the process specified by PID argument, if that is
positive. Use PID instead of dead_child to know which process to
reap. Wait for the child to die only if WNOHANG is not in
OPTIONS.
(sys_select): Don't set dead_child.
* src/sysdep.c (wait_for_termination_1): Remove the WINDOWSNT portion,
as it is no longer needed.
* src/process.c (waitpid, WUNTRACED) [!WNOHANG]: Remove definitions,
no longer needed.
(record_child_status_change): Remove the setting of
record_at_most_one_child for the !WNOHANG case.
2012-11-03 Paul Eggert <eggert@cs.ucla.edu>
Fix a race condition that causes Emacs to mess up glib (Bug#8855).
This is a backport from the trunk.
The symptom is a diagnostic "GLib-WARNING **: In call to
g_spawn_sync(), exit status of a child process was requested but
SIGCHLD action was set to SIG_IGN and ECHILD was received by
waitpid(), so exit status can't be returned." The diagnostic
is partly wrong, as the SIGCHLD action is not set to SIG_IGN.
The real bug is a race condition between Emacs and glib: Emacs
does a waitpid (-1, ...) and reaps glib's subprocess by mistake,
so that glib can't find it. Work around the bug by invoking
waitpid only on subprocesses that Emacs itself creates.
* src/process.c (create_process, record_child_status_change):
Don't use special value -1 in pid field, as the caller now must
know the pid rather than having the callee infer it. The
inference was sometimes incorrect anyway, due to another race.
(create_process): Set new 'alive' member if child is created.
(process_status_retrieved): New function.
(record_child_status_change): Use it.
Accept negative 1st argument, which means to wait for the
processes that Emacs already knows about. Move special-case code
for DOS_NT (which lacks WNOHANG) here, from caller. Keep track of
processes that have already been waited for, by testing and
clearing new 'alive' member.
(CAN_HANDLE_MULTIPLE_CHILDREN): Remove, as record_child_status_change
now does this internally.
(handle_child_signal): Let record_child_status_change do all
the work, since we do not want to reap all exited child processes,
only the child processes that Emacs itself created.
* src/process.h (Lisp_Process): New boolean member 'alive'.
Diffstat (limited to 'src/process.c')
| -rw-r--r-- | src/process.c | 191 |
1 files changed, 88 insertions, 103 deletions
diff --git a/src/process.c b/src/process.c index 77e99ead01f..c095d13293b 100644 --- a/src/process.c +++ b/src/process.c | |||
| @@ -130,14 +130,6 @@ 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 | #ifndef WNOHANG | ||
| 134 | # undef waitpid | ||
| 135 | # define waitpid(pid, status, options) wait (status) | ||
| 136 | #endif | ||
| 137 | #ifndef WUNTRACED | ||
| 138 | # define WUNTRACED 0 | ||
| 139 | #endif | ||
| 140 | |||
| 141 | /* Work around GCC 4.7.0 bug with strict overflow checking; see | 133 | /* Work around GCC 4.7.0 bug with strict overflow checking; see |
| 142 | <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52904>. | 134 | <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52904>. |
| 143 | These lines can be removed once the GCC bug is fixed. */ | 135 | These lines can be removed once the GCC bug is fixed. */ |
| @@ -795,9 +787,8 @@ get_process (register Lisp_Object name) | |||
| 795 | #ifdef SIGCHLD | 787 | #ifdef SIGCHLD |
| 796 | /* Fdelete_process promises to immediately forget about the process, but in | 788 | /* Fdelete_process promises to immediately forget about the process, but in |
| 797 | reality, Emacs needs to remember those processes until they have been | 789 | reality, Emacs needs to remember those processes until they have been |
| 798 | treated by the SIGCHLD handler; otherwise this handler would consider the | 790 | treated by the SIGCHLD handler and waitpid has been invoked on them; |
| 799 | process as being synchronous and say that the synchronous process is | 791 | otherwise they might fill up the kernel's process table. */ |
| 800 | dead. */ | ||
| 801 | static Lisp_Object deleted_pid_list; | 792 | static Lisp_Object deleted_pid_list; |
| 802 | #endif | 793 | #endif |
| 803 | 794 | ||
| @@ -1704,16 +1695,7 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir) | |||
| 1704 | if (inchannel > max_process_desc) | 1695 | if (inchannel > max_process_desc) |
| 1705 | max_process_desc = inchannel; | 1696 | max_process_desc = inchannel; |
| 1706 | 1697 | ||
| 1707 | /* Until we store the proper pid, enable the SIGCHLD handler | 1698 | /* 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); | 1699 | setup_process_coding_systems (process); |
| 1718 | 1700 | ||
| 1719 | encoded_current_dir = ENCODE_FILE (current_dir); | 1701 | encoded_current_dir = ENCODE_FILE (current_dir); |
| @@ -1880,6 +1862,8 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir) | |||
| 1880 | #endif | 1862 | #endif |
| 1881 | 1863 | ||
| 1882 | XPROCESS (process)->pid = pid; | 1864 | XPROCESS (process)->pid = pid; |
| 1865 | if (0 <= pid) | ||
| 1866 | XPROCESS (process)->alive = 1; | ||
| 1883 | 1867 | ||
| 1884 | /* Stop blocking signals in the parent. */ | 1868 | /* Stop blocking signals in the parent. */ |
| 1885 | #ifdef SIGCHLD | 1869 | #ifdef SIGCHLD |
| @@ -6273,9 +6257,35 @@ process has been transmitted to the serial port. */) | |||
| 6273 | return process; | 6257 | return process; |
| 6274 | } | 6258 | } |
| 6275 | 6259 | ||
| 6276 | /* On receipt of a signal that a child status has changed, loop asking | 6260 | /* If the status of the process DESIRED has changed, return true and |
| 6277 | about children with changed statuses until the system says there | 6261 | set *STATUS to its exit status; otherwise, return false. |
| 6278 | are no more. | 6262 | If HAVE is nonnegative, assume that HAVE = waitpid (HAVE, STATUS, ...) |
| 6263 | has already been invoked, and do not invoke waitpid again. */ | ||
| 6264 | |||
| 6265 | static bool | ||
| 6266 | process_status_retrieved (pid_t desired, pid_t have, int *status) | ||
| 6267 | { | ||
| 6268 | if (have < 0) | ||
| 6269 | { | ||
| 6270 | /* Invoke waitpid only with a known process ID; do not invoke | ||
| 6271 | waitpid with a nonpositive argument. Otherwise, Emacs might | ||
| 6272 | reap an unwanted process by mistake. For example, invoking | ||
| 6273 | waitpid (-1, ...) can mess up glib by reaping glib's subprocesses, | ||
| 6274 | so that another thread running glib won't find them. */ | ||
| 6275 | do | ||
| 6276 | have = waitpid (desired, status, WNOHANG | WUNTRACED); | ||
| 6277 | while (have < 0 && errno == EINTR); | ||
| 6278 | } | ||
| 6279 | |||
| 6280 | return have == desired; | ||
| 6281 | } | ||
| 6282 | |||
| 6283 | /* If PID is nonnegative, the child process PID with wait status W has | ||
| 6284 | changed its status; record this and return true. | ||
| 6285 | |||
| 6286 | If PID is negative, ignore W, and look for known child processes | ||
| 6287 | of Emacs whose status have changed. For each one found, record its new | ||
| 6288 | status. | ||
| 6279 | 6289 | ||
| 6280 | All we do is change the status; we do not run sentinels or print | 6290 | All we do is change the status; we do not run sentinels or print |
| 6281 | notifications. That is saved for the next time keyboard input is | 6291 | notifications. That is saved for the next time keyboard input is |
| @@ -6298,13 +6308,15 @@ process has been transmitted to the serial port. */) | |||
| 6298 | ** Malloc WARNING: This should never call malloc either directly or | 6308 | ** Malloc WARNING: This should never call malloc either directly or |
| 6299 | indirectly; if it does, that is a bug */ | 6309 | indirectly; if it does, that is a bug */ |
| 6300 | 6310 | ||
| 6301 | /* Record the changed status of the child process PID with wait status W. */ | ||
| 6302 | void | 6311 | void |
| 6303 | record_child_status_change (pid_t pid, int w) | 6312 | record_child_status_change (pid_t pid, int w) |
| 6304 | { | 6313 | { |
| 6305 | #ifdef SIGCHLD | 6314 | #ifdef SIGCHLD |
| 6306 | Lisp_Object proc; | 6315 | |
| 6307 | struct Lisp_Process *p; | 6316 | /* On POSIXish hosts, record at most one child only if we already |
| 6317 | know one child that has exited. */ | ||
| 6318 | bool record_at_most_one_child = 0 <= pid; | ||
| 6319 | |||
| 6308 | Lisp_Object tail; | 6320 | Lisp_Object tail; |
| 6309 | 6321 | ||
| 6310 | /* Find the process that signaled us, and record its status. */ | 6322 | /* Find the process that signaled us, and record its status. */ |
| @@ -6312,68 +6324,69 @@ record_child_status_change (pid_t pid, int w) | |||
| 6312 | /* The process can have been deleted by Fdelete_process. */ | 6324 | /* The process can have been deleted by Fdelete_process. */ |
| 6313 | for (tail = deleted_pid_list; CONSP (tail); tail = XCDR (tail)) | 6325 | for (tail = deleted_pid_list; CONSP (tail); tail = XCDR (tail)) |
| 6314 | { | 6326 | { |
| 6327 | bool all_pids_are_fixnums | ||
| 6328 | = (MOST_NEGATIVE_FIXNUM <= TYPE_MINIMUM (pid_t) | ||
| 6329 | && TYPE_MAXIMUM (pid_t) <= MOST_POSITIVE_FIXNUM); | ||
| 6315 | Lisp_Object xpid = XCAR (tail); | 6330 | Lisp_Object xpid = XCAR (tail); |
| 6316 | if ((INTEGERP (xpid) && pid == XINT (xpid)) | 6331 | if (all_pids_are_fixnums ? INTEGERP (xpid) : NUMBERP (xpid)) |
| 6317 | || (FLOATP (xpid) && pid == XFLOAT_DATA (xpid))) | ||
| 6318 | { | 6332 | { |
| 6319 | XSETCAR (tail, Qnil); | 6333 | pid_t deleted_pid; |
| 6320 | return; | 6334 | if (INTEGERP (xpid)) |
| 6335 | deleted_pid = XINT (xpid); | ||
| 6336 | else | ||
| 6337 | deleted_pid = XFLOAT_DATA (xpid); | ||
| 6338 | if (process_status_retrieved (deleted_pid, pid, &w)) | ||
| 6339 | { | ||
| 6340 | XSETCAR (tail, Qnil); | ||
| 6341 | if (record_at_most_one_child) | ||
| 6342 | return; | ||
| 6343 | } | ||
| 6321 | } | 6344 | } |
| 6322 | } | 6345 | } |
| 6323 | 6346 | ||
| 6324 | /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */ | 6347 | /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */ |
| 6325 | p = 0; | ||
| 6326 | for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) | 6348 | for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) |
| 6327 | { | 6349 | { |
| 6328 | proc = XCDR (XCAR (tail)); | 6350 | Lisp_Object proc = XCDR (XCAR (tail)); |
| 6329 | p = XPROCESS (proc); | 6351 | struct Lisp_Process *p = XPROCESS (proc); |
| 6330 | if (EQ (p->type, Qreal) && p->pid == pid) | 6352 | if (p->alive && process_status_retrieved (p->pid, pid, &w)) |
| 6331 | break; | 6353 | { |
| 6332 | p = 0; | 6354 | /* Change the status of the process that was found. */ |
| 6333 | } | 6355 | p->tick = ++process_tick; |
| 6334 | 6356 | p->raw_status = w; | |
| 6335 | /* Look for an asynchronous process whose pid hasn't been filled | 6357 | p->raw_status_new = 1; |
| 6336 | in yet. */ | ||
| 6337 | if (! p) | ||
| 6338 | for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail)) | ||
| 6339 | { | ||
| 6340 | proc = XCDR (XCAR (tail)); | ||
| 6341 | p = XPROCESS (proc); | ||
| 6342 | if (p->pid == -1) | ||
| 6343 | break; | ||
| 6344 | p = 0; | ||
| 6345 | } | ||
| 6346 | 6358 | ||
| 6347 | /* Change the status of the process that was found. */ | 6359 | /* If process has terminated, stop waiting for its output. */ |
| 6348 | if (p) | 6360 | if (WIFSIGNALED (w) || WIFEXITED (w)) |
| 6349 | { | 6361 | { |
| 6350 | int clear_desc_flag = 0; | 6362 | int clear_desc_flag = 0; |
| 6363 | p->alive = 0; | ||
| 6364 | if (p->infd >= 0) | ||
| 6365 | clear_desc_flag = 1; | ||
| 6351 | 6366 | ||
| 6352 | p->tick = ++process_tick; | 6367 | /* clear_desc_flag avoids a compiler bug in Microsoft C. */ |
| 6353 | p->raw_status = w; | 6368 | if (clear_desc_flag) |
| 6354 | p->raw_status_new = 1; | 6369 | { |
| 6370 | FD_CLR (p->infd, &input_wait_mask); | ||
| 6371 | FD_CLR (p->infd, &non_keyboard_wait_mask); | ||
| 6372 | } | ||
| 6373 | } | ||
| 6355 | 6374 | ||
| 6356 | /* If process has terminated, stop waiting for its output. */ | 6375 | /* Tell wait_reading_process_output that it needs to wake up and |
| 6357 | if ((WIFSIGNALED (w) || WIFEXITED (w)) | 6376 | look around. */ |
| 6358 | && p->infd >= 0) | 6377 | if (input_available_clear_time) |
| 6359 | clear_desc_flag = 1; | 6378 | *input_available_clear_time = make_emacs_time (0, 0); |
| 6360 | 6379 | ||
| 6361 | /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */ | 6380 | if (record_at_most_one_child) |
| 6362 | if (clear_desc_flag) | 6381 | return; |
| 6363 | { | ||
| 6364 | FD_CLR (p->infd, &input_wait_mask); | ||
| 6365 | FD_CLR (p->infd, &non_keyboard_wait_mask); | ||
| 6366 | } | 6382 | } |
| 6367 | |||
| 6368 | /* Tell wait_reading_process_output that it needs to wake up and | ||
| 6369 | look around. */ | ||
| 6370 | if (input_available_clear_time) | ||
| 6371 | *input_available_clear_time = make_emacs_time (0, 0); | ||
| 6372 | } | 6383 | } |
| 6373 | /* There was no asynchronous process found for that pid: we have | 6384 | |
| 6374 | a synchronous process. */ | 6385 | if (0 <= pid) |
| 6375 | else | ||
| 6376 | { | 6386 | { |
| 6387 | /* The caller successfully waited for a pid but no asynchronous | ||
| 6388 | process was found for it, so this is a synchronous process. */ | ||
| 6389 | |||
| 6377 | synch_process_alive = 0; | 6390 | synch_process_alive = 0; |
| 6378 | 6391 | ||
| 6379 | /* Report the status of the synchronous process. */ | 6392 | /* Report the status of the synchronous process. */ |
| @@ -6392,38 +6405,10 @@ record_child_status_change (pid_t pid, int w) | |||
| 6392 | 6405 | ||
| 6393 | #ifdef SIGCHLD | 6406 | #ifdef SIGCHLD |
| 6394 | 6407 | ||
| 6395 | /* On some systems, the SIGCHLD handler must return right away. If | ||
| 6396 | any more processes want to signal us, we will get another signal. | ||
| 6397 | Otherwise, loop around to use up all the processes that have | ||
| 6398 | something to tell us. */ | ||
| 6399 | #if (defined WINDOWSNT \ | ||
| 6400 | || (defined USG && !defined GNU_LINUX \ | ||
| 6401 | && !(defined HPUX && defined WNOHANG))) | ||
| 6402 | enum { CAN_HANDLE_MULTIPLE_CHILDREN = 0 }; | ||
| 6403 | #else | ||
| 6404 | enum { CAN_HANDLE_MULTIPLE_CHILDREN = 1 }; | ||
| 6405 | #endif | ||
| 6406 | |||
| 6407 | static void | 6408 | static void |
| 6408 | handle_child_signal (int sig) | 6409 | handle_child_signal (int sig) |
| 6409 | { | 6410 | { |
| 6410 | do | 6411 | record_child_status_change (-1, 0); |
| 6411 | { | ||
| 6412 | pid_t pid; | ||
| 6413 | int status; | ||
| 6414 | |||
| 6415 | do | ||
| 6416 | pid = waitpid (-1, &status, WNOHANG | WUNTRACED); | ||
| 6417 | while (pid < 0 && errno == EINTR); | ||
| 6418 | |||
| 6419 | /* PID == 0 means no processes found, PID == -1 means a real failure. | ||
| 6420 | Either way, we have done all our job. */ | ||
| 6421 | if (pid <= 0) | ||
| 6422 | break; | ||
| 6423 | |||
| 6424 | record_child_status_change (pid, status); | ||
| 6425 | } | ||
| 6426 | while (CAN_HANDLE_MULTIPLE_CHILDREN); | ||
| 6427 | } | 6412 | } |
| 6428 | 6413 | ||
| 6429 | static void | 6414 | static void |