From fb0eba706cccd510d99c4c5339a76dd15bc8a628 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Wed, 2 Jan 2008 19:55:04 +0000 Subject: libbb: introduce and use safe_waitpid (loops in EINTR) *: use more approproate (shorter) versions of wait() function old new delta safe_waitpid - 48 +48 wait_any_nohang - 17 +17 send_tree 365 369 +4 processorstop 432 435 +3 text_yank 110 108 -2 make_human_readable_str 202 200 -2 crond_main 1368 1366 -2 handle_sigchld 49 43 -6 reapchild 166 159 -7 custom 260 250 -10 checkscript 191 177 -14 wait_nohang 17 - -17 wait_pid 43 - -43 ------------------------------------------------------------------------------ (add/remove: 2/2 grow/shrink: 2/7 up/down: 72/-103) Total: -31 bytes --- libbb/vfork_daemon_rexec.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'libbb/vfork_daemon_rexec.c') diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c index d6e233ac3..4e6ecde67 100644 --- a/libbb/vfork_daemon_rexec.c +++ b/libbb/vfork_daemon_rexec.c @@ -66,6 +66,21 @@ pid_t xspawn(char **argv) return pid; } +int safe_waitpid(int pid, int *wstat, int options) +{ + int r; + + do + r = waitpid(pid, wstat, options); + while ((r == -1) && (errno == EINTR)); + return r; +} + +int wait_any_nohang(int *wstat) +{ + return safe_waitpid(-1, wstat, WNOHANG); +} + // Wait for the specified child PID to exit, returning child's error return. int wait4pid(int pid) { @@ -76,28 +91,18 @@ int wait4pid(int pid) /* we expect errno to be already set from failed [v]fork/exec */ return -1; } - if (waitpid(pid, &status, 0) == -1) + if (safe_waitpid(pid, &status, 0) == -1) return -1; if (WIFEXITED(status)) return WEXITSTATUS(status); if (WIFSIGNALED(status)) return WTERMSIG(status) + 1000; return 0; -} - -int wait_nohang(int *wstat) -{ - return waitpid(-1, wstat, WNOHANG); -} - -int wait_pid(int *wstat, int pid) -{ - int r; - - do - r = waitpid(pid, wstat, 0); - while ((r == -1) && (errno == EINTR)); - return r; + if (WIFEXITED(status)) + return WEXITSTATUS(status); + if (WIFSIGNALED(status)) + return WTERMSIG(status) + 1000; + return 0; } #if ENABLE_FEATURE_PREFER_APPLETS -- cgit v1.2.3