diff options
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 |