libbb: two new functions: wait_for_exitstatus(pid), xfchdir(fd)

Bartosz Golaszewski proposed xfchdir()

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2016-04-01 22:12:44 +02:00
parent 29b33b63d4
commit c4199f22d0
6 changed files with 22 additions and 15 deletions

View File

@ -112,8 +112,7 @@ void FAST_FUNC data_extract_to_command(archive_handle_t *archive_handle)
bb_copyfd_exact_size(archive_handle->src_fd, p[1], -file_header->size);
close(p[1]);
if (safe_waitpid(pid, &status, 0) == -1)
bb_perror_msg_and_die("waitpid");
status = wait_for_exitstatus(pid);
if (WIFEXITED(status) && WEXITSTATUS(status))
bb_error_msg_and_die("'%s' returned status %d",
archive_handle->tar__to_command, WEXITSTATUS(status));

View File

@ -500,6 +500,7 @@ void xsetuid(uid_t uid) FAST_FUNC;
void xsetegid(gid_t egid) FAST_FUNC;
void xseteuid(uid_t euid) FAST_FUNC;
void xchdir(const char *path) FAST_FUNC;
void xfchdir(int fd) FAST_FUNC;
void xchroot(const char *path) FAST_FUNC;
void xsetenv(const char *key, const char *value) FAST_FUNC;
void bb_unsetenv(const char *key) FAST_FUNC;
@ -1021,6 +1022,7 @@ pid_t wait_any_nohang(int *wstat) FAST_FUNC;
* if (rc > 0) bb_error_msg("exit code: %d", rc & 0xff);
*/
int wait4pid(pid_t pid) FAST_FUNC;
int wait_for_exitstatus(pid_t pid) FAST_FUNC;
/* Same as wait4pid(spawn(argv)), but with NOFORK/NOEXEC if configured: */
int spawn_and_wait(char **argv) FAST_FUNC;
/* Does NOT check that applet is NOFORK, just blindly runs it */

View File

@ -315,3 +315,15 @@ int FAST_FUNC wait4pid(pid_t pid)
return WTERMSIG(status) + 0x180;
return 0;
}
// Useful when we do know that pid is valid, and we just want to wait
// for it to exit. Not existing pid is fatal. waitpid() status is not returned.
int FAST_FUNC wait_for_exitstatus(pid_t pid)
{
int exit_status, n;
n = safe_waitpid(pid, &exit_status, 0);
if (n < 0)
bb_perror_msg_and_die("waitpid");
return exit_status;
}

View File

@ -390,6 +390,12 @@ void FAST_FUNC xchdir(const char *path)
bb_perror_msg_and_die("can't change directory to '%s'", path);
}
void FAST_FUNC xfchdir(int fd)
{
if (fchdir(fd))
bb_perror_msg_and_die("fchdir");
}
void FAST_FUNC xchroot(const char *path)
{
if (chroot(path))

View File

@ -255,8 +255,7 @@ static NOINLINE void edir(const char *directory_name)
xsetenv(d->d_name, buf);
}
closedir(dir);
if (fchdir(wdir) == -1)
bb_perror_msg_and_die("fchdir");
xfchdir(wdir);
close(wdir);
}

View File

@ -57,17 +57,6 @@ static void mount_or_die(const char *source, const char *target,
}
}
// TODO: move to libbb
static int wait_for_exitstatus(pid_t pid)
{
int exit_status, n;
n = safe_waitpid(pid, &exit_status, 0);
if (n < 0)
bb_perror_msg_and_die("waitpid");
return exit_status;
}
/*
* Longest possible path to a procfs file used in unshare. Must be able to
* contain the '/proc/' string, the '/ns/user' string which is the longest