aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2016-04-01 22:12:44 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2016-04-01 22:12:44 +0200
commitc4199f22d0f7793b70db51c01783f0d45afce3d4 (patch)
treed54b8df23749d5ea06dc5effd5ba111b8e2cf52f
parent29b33b63d49be88200f794d832450a4c71e85a5e (diff)
downloadbusybox-w32-c4199f22d0f7793b70db51c01783f0d45afce3d4.tar.gz
busybox-w32-c4199f22d0f7793b70db51c01783f0d45afce3d4.tar.bz2
busybox-w32-c4199f22d0f7793b70db51c01783f0d45afce3d4.zip
libbb: two new functions: wait_for_exitstatus(pid), xfchdir(fd)
Bartosz Golaszewski proposed xfchdir() Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--archival/libarchive/data_extract_to_command.c3
-rw-r--r--include/libbb.h2
-rw-r--r--libbb/xfuncs.c12
-rw-r--r--libbb/xfuncs_printf.c6
-rw-r--r--runit/chpst.c3
-rw-r--r--util-linux/unshare.c11
6 files changed, 22 insertions, 15 deletions
diff --git a/archival/libarchive/data_extract_to_command.c b/archival/libarchive/data_extract_to_command.c
index 6f5317a0e..5d8769382 100644
--- a/archival/libarchive/data_extract_to_command.c
+++ b/archival/libarchive/data_extract_to_command.c
@@ -112,8 +112,7 @@ void FAST_FUNC data_extract_to_command(archive_handle_t *archive_handle)
112 bb_copyfd_exact_size(archive_handle->src_fd, p[1], -file_header->size); 112 bb_copyfd_exact_size(archive_handle->src_fd, p[1], -file_header->size);
113 close(p[1]); 113 close(p[1]);
114 114
115 if (safe_waitpid(pid, &status, 0) == -1) 115 status = wait_for_exitstatus(pid);
116 bb_perror_msg_and_die("waitpid");
117 if (WIFEXITED(status) && WEXITSTATUS(status)) 116 if (WIFEXITED(status) && WEXITSTATUS(status))
118 bb_error_msg_and_die("'%s' returned status %d", 117 bb_error_msg_and_die("'%s' returned status %d",
119 archive_handle->tar__to_command, WEXITSTATUS(status)); 118 archive_handle->tar__to_command, WEXITSTATUS(status));
diff --git a/include/libbb.h b/include/libbb.h
index 98d788402..5b4280e34 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -500,6 +500,7 @@ void xsetuid(uid_t uid) FAST_FUNC;
500void xsetegid(gid_t egid) FAST_FUNC; 500void xsetegid(gid_t egid) FAST_FUNC;
501void xseteuid(uid_t euid) FAST_FUNC; 501void xseteuid(uid_t euid) FAST_FUNC;
502void xchdir(const char *path) FAST_FUNC; 502void xchdir(const char *path) FAST_FUNC;
503void xfchdir(int fd) FAST_FUNC;
503void xchroot(const char *path) FAST_FUNC; 504void xchroot(const char *path) FAST_FUNC;
504void xsetenv(const char *key, const char *value) FAST_FUNC; 505void xsetenv(const char *key, const char *value) FAST_FUNC;
505void bb_unsetenv(const char *key) FAST_FUNC; 506void bb_unsetenv(const char *key) FAST_FUNC;
@@ -1021,6 +1022,7 @@ pid_t wait_any_nohang(int *wstat) FAST_FUNC;
1021 * if (rc > 0) bb_error_msg("exit code: %d", rc & 0xff); 1022 * if (rc > 0) bb_error_msg("exit code: %d", rc & 0xff);
1022 */ 1023 */
1023int wait4pid(pid_t pid) FAST_FUNC; 1024int wait4pid(pid_t pid) FAST_FUNC;
1025int wait_for_exitstatus(pid_t pid) FAST_FUNC;
1024/* Same as wait4pid(spawn(argv)), but with NOFORK/NOEXEC if configured: */ 1026/* Same as wait4pid(spawn(argv)), but with NOFORK/NOEXEC if configured: */
1025int spawn_and_wait(char **argv) FAST_FUNC; 1027int spawn_and_wait(char **argv) FAST_FUNC;
1026/* Does NOT check that applet is NOFORK, just blindly runs it */ 1028/* Does NOT check that applet is NOFORK, just blindly runs it */
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index 206edb4a0..3f9a84ad4 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -315,3 +315,15 @@ int FAST_FUNC wait4pid(pid_t pid)
315 return WTERMSIG(status) + 0x180; 315 return WTERMSIG(status) + 0x180;
316 return 0; 316 return 0;
317} 317}
318
319// Useful when we do know that pid is valid, and we just want to wait
320// for it to exit. Not existing pid is fatal. waitpid() status is not returned.
321int FAST_FUNC wait_for_exitstatus(pid_t pid)
322{
323 int exit_status, n;
324
325 n = safe_waitpid(pid, &exit_status, 0);
326 if (n < 0)
327 bb_perror_msg_and_die("waitpid");
328 return exit_status;
329}
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index 73488908d..4aa1b5ce2 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -390,6 +390,12 @@ void FAST_FUNC xchdir(const char *path)
390 bb_perror_msg_and_die("can't change directory to '%s'", path); 390 bb_perror_msg_and_die("can't change directory to '%s'", path);
391} 391}
392 392
393void FAST_FUNC xfchdir(int fd)
394{
395 if (fchdir(fd))
396 bb_perror_msg_and_die("fchdir");
397}
398
393void FAST_FUNC xchroot(const char *path) 399void FAST_FUNC xchroot(const char *path)
394{ 400{
395 if (chroot(path)) 401 if (chroot(path))
diff --git a/runit/chpst.c b/runit/chpst.c
index 301cdd08a..7fe5151db 100644
--- a/runit/chpst.c
+++ b/runit/chpst.c
@@ -255,8 +255,7 @@ static NOINLINE void edir(const char *directory_name)
255 xsetenv(d->d_name, buf); 255 xsetenv(d->d_name, buf);
256 } 256 }
257 closedir(dir); 257 closedir(dir);
258 if (fchdir(wdir) == -1) 258 xfchdir(wdir);
259 bb_perror_msg_and_die("fchdir");
260 close(wdir); 259 close(wdir);
261} 260}
262 261
diff --git a/util-linux/unshare.c b/util-linux/unshare.c
index f1a9cdf19..b8cd4676a 100644
--- a/util-linux/unshare.c
+++ b/util-linux/unshare.c
@@ -57,17 +57,6 @@ static void mount_or_die(const char *source, const char *target,
57 } 57 }
58} 58}
59 59
60// TODO: move to libbb
61static int wait_for_exitstatus(pid_t pid)
62{
63 int exit_status, n;
64
65 n = safe_waitpid(pid, &exit_status, 0);
66 if (n < 0)
67 bb_perror_msg_and_die("waitpid");
68 return exit_status;
69}
70
71/* 60/*
72 * Longest possible path to a procfs file used in unshare. Must be able to 61 * Longest possible path to a procfs file used in unshare. Must be able to
73 * contain the '/proc/' string, the '/ns/user' string which is the longest 62 * contain the '/proc/' string, the '/ns/user' string which is the longest