diff options
| author | Paul Eggert | 2012-12-03 13:42:12 -0800 |
|---|---|---|
| committer | Paul Eggert | 2012-12-03 13:42:12 -0800 |
| commit | bb5f74ee84398a56435baa2ef15e12d8a35e5e03 (patch) | |
| tree | 4bb59aacd4d69b873154ce576e51fe8efe345150 /src/sysdep.c | |
| parent | bc9dbce6ee527ded152682c98f5f82e42c33dde9 (diff) | |
| download | emacs-bb5f74ee84398a56435baa2ef15e12d8a35e5e03.tar.gz emacs-bb5f74ee84398a56435baa2ef15e12d8a35e5e03.zip | |
Don't let call-process be a zombie factory.
Fixing this bug required some cleanup of the signal-handling code.
As a side effect, this change also fixes a longstanding rare race
condition whereby Emacs could mistakenly kill unrelated processes,
and it fixes a bug where a second C-g does not kill a recalcitrant
synchronous process in GNU/Linux and similar platforms.
The patch should also fix the last vestiges of Bug#9488,
a bug which has mostly been fixed on the trunk by other changes.
* callproc.c, process.h (synch_process_alive, synch_process_death)
(synch_process_termsig, sync_process_retcode):
Remove. All uses removed, to simplify analysis and so that
less consing is done inside critical sections.
* callproc.c (call_process_exited): Remove. All uses replaced
with !synch_process_pid.
* callproc.c (synch_process_pid, synch_process_fd): New static vars.
These take the role of what used to be in unwind-protect arg.
All uses changed.
(block_child_signal, unblock_child_signal):
New functions, to avoid races that could kill innocent-victim processes.
(call_process_kill, call_process_cleanup, Fcall_process): Use them.
(call_process_kill): Record killed processes as deleted, so that
zombies do not clutter up the system. Do this inside a critical
section, to avoid a race that would allow the clutter.
(call_process_cleanup): Fix code so that the second C-g works again
on common platforms such as GNU/Linux.
(Fcall_process): Create the child process in a critical section,
to fix a race condition. If creating an asynchronous process,
record it as deleted so that zombies do not clutter up the system.
Do unwind-protect for WINDOWSNT too, as that's simpler in the
light of these changes. Omit unnecessary call to emacs_close
before failure, as the unwind-protect code does that.
* callproc.c (call_process_cleanup):
* w32proc.c (waitpid): Simplify now that synch_process_alive is gone.
* process.c (record_deleted_pid): New function, containing
code refactored out of Fdelete_process.
(Fdelete_process): Use it.
(process_status_retrieved): Remove. All callers changed to use
child_status_change.
(record_child_status_change): Remove, folding its contents into ...
(handle_child_signal): ... this signal handler. Now, this
function is purely a handler for SIGCHLD, and is not called after
a synchronous waitpid returns; the synchronous code is moved to
wait_for_termination. There is no need to worry about reaping
more than one child now.
* sysdep.c (get_child_status, child_status_changed): New functions.
(wait_for_termination): Now takes int * status and bool
interruptible arguments, too. Do not record child status change;
that's now the caller's responsibility. All callers changed.
Reimplement in terms of get_child_status.
(wait_for_termination_1, interruptible_wait_for_termination):
Remove. All callers changed to use wait_for_termination.
* syswait.h: Include <stdbool.h>, for bool.
(record_child_status_change, interruptible_wait_for_termination):
Remove decls.
(record_deleted_pid, child_status_changed): New decls.
(wait_for_termination): Adjust to API changes noted above.
Fixes: debbugs:12980
Diffstat (limited to 'src/sysdep.c')
| -rw-r--r-- | src/sysdep.c | 80 |
1 files changed, 52 insertions, 28 deletions
diff --git a/src/sysdep.c b/src/sysdep.c index 1a3834f0379..7068a4f0977 100644 --- a/src/sysdep.c +++ b/src/sysdep.c | |||
| @@ -266,45 +266,71 @@ init_baud_rate (int fd) | |||
| 266 | 266 | ||
| 267 | #ifndef MSDOS | 267 | #ifndef MSDOS |
| 268 | 268 | ||
| 269 | static void | 269 | /* Wait for the subprocess with process id CHILD to terminate or change status. |
| 270 | wait_for_termination_1 (pid_t pid, int interruptible) | 270 | CHILD must be a child process that has not been reaped. |
| 271 | If STATUS is non-null, store the waitpid-style exit status into *STATUS | ||
| 272 | and tell wait_reading_process_output that it needs to look around. | ||
| 273 | Use waitpid-style OPTIONS when waiting. | ||
| 274 | If INTERRUPTIBLE, this function is interruptible by a signal. | ||
| 275 | |||
| 276 | Return CHILD if successful, 0 if no status is available; | ||
| 277 | the latter is possible only when options & NOHANG. */ | ||
| 278 | static pid_t | ||
| 279 | get_child_status (pid_t child, int *status, int options, bool interruptible) | ||
| 271 | { | 280 | { |
| 272 | while (1) | 281 | pid_t pid; |
| 282 | |||
| 283 | /* Invoke waitpid only with a known process ID; do not invoke | ||
| 284 | waitpid with a nonpositive argument. Otherwise, Emacs might | ||
| 285 | reap an unwanted process by mistake. For example, invoking | ||
| 286 | waitpid (-1, ...) can mess up glib by reaping glib's subprocesses, | ||
| 287 | so that another thread running glib won't find them. */ | ||
| 288 | eassert (0 < child); | ||
| 289 | |||
| 290 | while ((pid = waitpid (child, status, options)) < 0) | ||
| 273 | { | 291 | { |
| 274 | int status; | 292 | /* CHILD must be a child process that has not been reaped, and |
| 275 | int wait_result = waitpid (pid, &status, 0); | 293 | STATUS and OPTIONS must be valid. */ |
| 276 | if (wait_result < 0) | 294 | eassert (errno == EINTR); |
| 277 | { | ||
| 278 | if (errno != EINTR) | ||
| 279 | break; | ||
| 280 | } | ||
| 281 | else | ||
| 282 | { | ||
| 283 | record_child_status_change (wait_result, status); | ||
| 284 | break; | ||
| 285 | } | ||
| 286 | 295 | ||
| 287 | /* Note: the MS-Windows emulation of waitpid calls QUIT | 296 | /* Note: the MS-Windows emulation of waitpid calls QUIT |
| 288 | internally. */ | 297 | internally. */ |
| 289 | if (interruptible) | 298 | if (interruptible) |
| 290 | QUIT; | 299 | QUIT; |
| 291 | } | 300 | } |
| 292 | } | ||
| 293 | 301 | ||
| 294 | /* Wait for subprocess with process id `pid' to terminate and | 302 | /* If successful and status is requested, tell wait_reading_process_output |
| 295 | make sure it will get eliminated (not remain forever as a zombie) */ | 303 | that it needs to wake up and look around. */ |
| 304 | if (pid && status && input_available_clear_time) | ||
| 305 | *input_available_clear_time = make_emacs_time (0, 0); | ||
| 296 | 306 | ||
| 307 | return pid; | ||
| 308 | } | ||
| 309 | |||
| 310 | /* Wait for the subprocess with process id CHILD to terminate. | ||
| 311 | CHILD must be a child process that has not been reaped. | ||
| 312 | If STATUS is non-null, store the waitpid-style exit status into *STATUS | ||
| 313 | and tell wait_reading_process_output that it needs to look around. | ||
| 314 | If INTERRUPTIBLE, this function is interruptible by a signal. */ | ||
| 297 | void | 315 | void |
| 298 | wait_for_termination (pid_t pid) | 316 | wait_for_termination (pid_t child, int *status, bool interruptible) |
| 299 | { | 317 | { |
| 300 | wait_for_termination_1 (pid, 0); | 318 | get_child_status (child, status, 0, interruptible); |
| 301 | } | 319 | } |
| 302 | 320 | ||
| 303 | /* Like the above, but allow keyboard interruption. */ | 321 | /* Report whether the subprocess with process id CHILD has changed status. |
| 304 | void | 322 | Termination counts as a change of status. |
| 305 | interruptible_wait_for_termination (pid_t pid) | 323 | CHILD must be a child process that has not been reaped. |
| 324 | If STATUS is non-null, store the waitpid-style exit status into *STATUS | ||
| 325 | and tell wait_reading_process_output that it needs to look around. | ||
| 326 | Use waitpid-style OPTIONS to check status, but do not wait. | ||
| 327 | |||
| 328 | Return CHILD if successful, 0 if no status is available because | ||
| 329 | the process's state has not changed. */ | ||
| 330 | pid_t | ||
| 331 | child_status_changed (pid_t child, int *status, int options) | ||
| 306 | { | 332 | { |
| 307 | wait_for_termination_1 (pid, 1); | 333 | return get_child_status (child, status, WNOHANG | options, 0); |
| 308 | } | 334 | } |
| 309 | 335 | ||
| 310 | /* | 336 | /* |
| @@ -454,6 +480,7 @@ sys_subshell (void) | |||
| 454 | char oldwd[MAXPATHLEN+1]; /* Fixed length is safe on MSDOS. */ | 480 | char oldwd[MAXPATHLEN+1]; /* Fixed length is safe on MSDOS. */ |
| 455 | #endif | 481 | #endif |
| 456 | pid_t pid; | 482 | pid_t pid; |
| 483 | int status; | ||
| 457 | struct save_signal saved_handlers[5]; | 484 | struct save_signal saved_handlers[5]; |
| 458 | Lisp_Object dir; | 485 | Lisp_Object dir; |
| 459 | unsigned char *volatile str_volatile = 0; | 486 | unsigned char *volatile str_volatile = 0; |
| @@ -491,7 +518,6 @@ sys_subshell (void) | |||
| 491 | #ifdef DOS_NT | 518 | #ifdef DOS_NT |
| 492 | pid = 0; | 519 | pid = 0; |
| 493 | save_signal_handlers (saved_handlers); | 520 | save_signal_handlers (saved_handlers); |
| 494 | synch_process_alive = 1; | ||
| 495 | #else | 521 | #else |
| 496 | pid = vfork (); | 522 | pid = vfork (); |
| 497 | if (pid == -1) | 523 | if (pid == -1) |
| @@ -560,14 +586,12 @@ sys_subshell (void) | |||
| 560 | /* Do this now if we did not do it before. */ | 586 | /* Do this now if we did not do it before. */ |
| 561 | #ifndef MSDOS | 587 | #ifndef MSDOS |
| 562 | save_signal_handlers (saved_handlers); | 588 | save_signal_handlers (saved_handlers); |
| 563 | synch_process_alive = 1; | ||
| 564 | #endif | 589 | #endif |
| 565 | 590 | ||
| 566 | #ifndef DOS_NT | 591 | #ifndef DOS_NT |
| 567 | wait_for_termination (pid); | 592 | wait_for_termination (pid, &status, 0); |
| 568 | #endif | 593 | #endif |
| 569 | restore_signal_handlers (saved_handlers); | 594 | restore_signal_handlers (saved_handlers); |
| 570 | synch_process_alive = 0; | ||
| 571 | } | 595 | } |
| 572 | 596 | ||
| 573 | static void | 597 | static void |