summaryrefslogtreecommitdiffhomepage
path: root/libbb
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2016-04-02 18:06:24 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2016-04-02 18:06:24 +0200
commit8220399173cf8d25e37059cadac96ac30f94e82a (patch)
treeffe5ae4a783c8ddeceda15f57eae74ee69feebea /libbb
parentc87e81f9440278dd46a3eddd1e0f4773afd46a95 (diff)
nsenter,unshare: share common code; fix a bug of not closing all fds
function old new delta xvfork_parent_waits_and_exits - 64 +64 exec_prog_or_SHELL - 39 +39 unshare_main 873 810 -63 nsenter_main 663 596 -67 ------------------------------------------------------------------------------ (add/remove: 2/0 grow/shrink: 0/2 up/down: 106/-130) Total: -27 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r--libbb/executable.c11
-rw-r--r--libbb/xfuncs_printf.c16
2 files changed, 26 insertions, 1 deletions
diff --git a/libbb/executable.c b/libbb/executable.c
index 85ecc3e6c..05e70312f 100644
--- a/libbb/executable.c
+++ b/libbb/executable.c
@@ -83,10 +83,19 @@ int FAST_FUNC BB_EXECVP(const char *file, char *const argv[])
}
#endif
-int FAST_FUNC BB_EXECVP_or_die(char **argv)
+void FAST_FUNC BB_EXECVP_or_die(char **argv)
{
BB_EXECVP(argv[0], argv);
/* SUSv3-mandated exit codes */
xfunc_error_retval = (errno == ENOENT) ? 127 : 126;
bb_perror_msg_and_die("can't execute '%s'", argv[0]);
}
+
+/* Typical idiom for applets which exec *optional* PROG [ARGS] */
+void FAST_FUNC exec_prog_or_SHELL(char **argv)
+{
+ if (argv[0]) {
+ BB_EXECVP_or_die(argv);
+ }
+ run_shell(getenv("SHELL"), /*login:*/ 1, NULL, NULL);
+}
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index 4aa1b5ce2..e9222f690 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -659,3 +659,19 @@ pid_t FAST_FUNC xfork(void)
return pid;
}
#endif
+
+void FAST_FUNC xvfork_parent_waits_and_exits(void)
+{
+ pid_t pid;
+
+ fflush_all();
+ pid = xvfork();
+ if (pid > 0) {
+ /* Parent */
+ int exit_status = wait_for_exitstatus(pid);
+ if (WIFSIGNALED(exit_status))
+ kill_myself_with_sig(WTERMSIG(exit_status));
+ _exit(WEXITSTATUS(exit_status));
+ }
+ /* Child continues */
+}