*: introduce and use xfork() and xvfork()
function old new delta
launch_helper 170 169 -1
setup_heredoc 312 302 -10
handle_dir_common 367 354 -13
expand_vars_to_list 2456 2443 -13
open_transformer 89 74 -15
data_extract_to_command 439 423 -16
do_ipaddr 1406 1389 -17
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/7 up/down: 0/-85) Total: -85 bytes
Signed-off-by: Pascal Bellard <pascal.bellard@ads-lu.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
diff --git a/archival/libunarchive/data_extract_to_command.c b/archival/libunarchive/data_extract_to_command.c
index eb09439..95f5bc8 100644
--- a/archival/libunarchive/data_extract_to_command.c
+++ b/archival/libunarchive/data_extract_to_command.c
@@ -82,11 +82,8 @@
memset(tar_env, 0, sizeof(tar_env));
xpipe(p);
- pid = BB_MMU ? fork() : vfork();
- switch (pid) {
- case -1:
- bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork");
- case 0:
+ pid = BB_MMU ? xfork() : xvfork();
+ if (pid == 0) {
/* Child */
/* str2env(tar_env, TAR_FILETYPE, "f"); - parent should do it once */
oct2env(tar_env, TAR_MODE, file_header->mode);
diff --git a/archival/libunarchive/open_transformer.c b/archival/libunarchive/open_transformer.c
index 47c13e6..cba049f 100644
--- a/archival/libunarchive/open_transformer.c
+++ b/archival/libunarchive/open_transformer.c
@@ -19,19 +19,9 @@
int pid;
xpiped_pair(fd_pipe);
-
-#if BB_MMU
- pid = fork();
- if (pid == -1)
- bb_perror_msg_and_die("vfork" + 1);
-#else
- pid = vfork();
- if (pid == -1)
- bb_perror_msg_and_die("vfork");
-#endif
-
+ pid = BB_MMU ? xfork() : xvfork();
if (pid == 0) {
- /* child process */
+ /* Child */
close(fd_pipe.rd); /* we don't want to read from the parent */
// FIXME: error check?
#if BB_MMU
diff --git a/archival/tar.c b/archival/tar.c
index f49fb12..9dd7453 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -514,9 +514,7 @@
(void) &zip_exec;
# endif
- gzipPid = vfork();
- if (gzipPid < 0)
- bb_perror_msg_and_die("vfork");
+ gzipPid = xvfork();
if (gzipPid == 0) {
/* child */
diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c
index 3ded758..665f38f 100644
--- a/debianutils/start_stop_daemon.c
+++ b/debianutils/start_stop_daemon.c
@@ -409,9 +409,7 @@
/* DAEMON_DEVNULL_STDIO is superfluous -
* it's always done by bb_daemonize() */
#else
- pid_t pid = vfork();
- if (pid < 0) /* error */
- bb_perror_msg_and_die("vfork");
+ pid_t pid = xvfork();
if (pid != 0) {
/* parent */
/* why _exit? the child may have changed the stack,
diff --git a/e2fsprogs/old_e2fsprogs/fsck.c b/e2fsprogs/old_e2fsprogs/fsck.c
index dc029b6..2a66d72 100644
--- a/e2fsprogs/old_e2fsprogs/fsck.c
+++ b/e2fsprogs/old_e2fsprogs/fsck.c
@@ -612,7 +612,7 @@
if (noexecute)
pid = -1;
else if ((pid = fork()) < 0) {
- perror("fork");
+ perror("vfork"+1);
return errno;
} else if (pid == 0) {
if (!interactive)
diff --git a/include/libbb.h b/include/libbb.h
index 4b6699f..e2a8322 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -841,6 +841,19 @@
#endif
int BB_EXECVP_or_die(char **argv) NORETURN FAST_FUNC;
+/* xvfork() can't be a _function_, return after vfork mangles stack
+ * in the parent. It must be a macro. */
+#define xvfork() \
+({ \
+ pid_t bb__xvfork_pid = vfork(); \
+ if (bb__xvfork_pid < 0) \
+ bb_perror_msg_and_die("vfork"); \
+ bb__xvfork_pid; \
+})
+#if BB_MMU
+pid_t xfork(void) FAST_FUNC;
+#endif
+
/* NOMMU friendy fork+exec: */
pid_t spawn(char **argv) FAST_FUNC;
pid_t xspawn(char **argv) FAST_FUNC;
@@ -886,7 +899,7 @@
* Both of the above will redirect fd 0,1,2 to /dev/null and drop ctty
* (will do setsid()).
*
- * fork_or_rexec(argv) = bare-bones "fork" on MMU,
+ * fork_or_rexec(argv) = bare-bones fork on MMU,
* "vfork + re-exec ourself" on NOMMU. No fd redirection, no setsid().
* On MMU ignores argv.
*
@@ -902,19 +915,19 @@
DAEMON_ONLY_SANITIZE = 8, /* internal use */
};
#if BB_MMU
- pid_t fork_or_rexec(void) FAST_FUNC;
enum { re_execed = 0 };
-# define fork_or_rexec(argv) fork_or_rexec()
+# define fork_or_rexec(argv) xfork()
# define bb_daemonize_or_rexec(flags, argv) bb_daemonize_or_rexec(flags)
# define bb_daemonize(flags) bb_daemonize_or_rexec(flags, bogus)
#else
+ extern bool re_execed;
void re_exec(char **argv) NORETURN FAST_FUNC;
pid_t fork_or_rexec(char **argv) FAST_FUNC;
- extern bool re_execed;
int BUG_fork_is_unavailable_on_nommu(void) FAST_FUNC;
int BUG_daemon_is_unavailable_on_nommu(void) FAST_FUNC;
void BUG_bb_daemonize_is_unavailable_on_nommu(void) FAST_FUNC;
# define fork() BUG_fork_is_unavailable_on_nommu()
+# define xfork() BUG_fork_is_unavailable_on_nommu()
# define daemon(a,b) BUG_daemon_is_unavailable_on_nommu()
# define bb_daemonize(a) BUG_bb_daemonize_is_unavailable_on_nommu()
#endif
diff --git a/init/bootchartd.c b/init/bootchartd.c
index 42b98c8..b3e08af 100644
--- a/init/bootchartd.c
+++ b/init/bootchartd.c
@@ -427,9 +427,7 @@
}
if (cmd == CMD_START && argv[2]) { /* "start PROG ARGS" */
- pid_t pid = vfork();
- if (pid < 0)
- bb_perror_msg_and_die("vfork");
+ pid_t pid = xvfork();
if (pid == 0) { /* child */
argv += 2;
execvp(argv[0], argv);
diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c
index 8102ea2..5c2c529 100644
--- a/libbb/vfork_daemon_rexec.c
+++ b/libbb/vfork_daemon_rexec.c
@@ -224,26 +224,12 @@
/* Maybe we are already re-execed and come here again? */
if (re_execed)
return 0;
- pid = vfork();
- if (pid < 0) /* wtf? */
- bb_perror_msg_and_die("vfork");
+ pid = xvfork();
if (pid) /* parent */
return pid;
/* child - re-exec ourself */
re_exec(argv);
}
-#else
-/* Dance around (void)...*/
-#undef fork_or_rexec
-pid_t FAST_FUNC fork_or_rexec(void)
-{
- pid_t pid;
- pid = fork();
- if (pid < 0) /* wtf? */
- bb_perror_msg_and_die("fork");
- return pid;
-}
-#define fork_or_rexec(argv) fork_or_rexec()
#endif
/* Due to a #define in libbb.h on MMU systems we actually have 1 argument -
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index f021493..7069a7c 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -589,3 +589,14 @@
/* variant = 10x */
buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80;
}
+
+#if BB_MMU
+pid_t FAST_FUNC xfork(void)
+{
+ pid_t pid;
+ pid = fork();
+ if (pid < 0) /* wtf? */
+ bb_perror_msg_and_die("vfork"+1);
+ return pid;
+}
+#endif
diff --git a/mailutils/mail.c b/mailutils/mail.c
index 5eb99e1..bcd3583 100644
--- a/mailutils/mail.c
+++ b/mailutils/mail.c
@@ -54,9 +54,7 @@
+ (1 << SIGALRM)
, signal_handler);
- G.helper_pid = vfork();
- if (G.helper_pid < 0)
- bb_perror_msg_and_die("vfork");
+ G.helper_pid = xvfork();
i = (!G.helper_pid) * 2; // for parent:0, for child:2
close(pipes[i + 1]); // 1 or 3 - closing one write end
diff --git a/miscutils/conspy.c b/miscutils/conspy.c
index 3f341ce..509a0f2 100644
--- a/miscutils/conspy.c
+++ b/miscutils/conspy.c
@@ -309,10 +309,7 @@
static NOINLINE void start_shell_in_child(const char* tty_name)
{
- int pid = vfork();
- if (pid < 0) {
- bb_perror_msg_and_die("vfork");
- }
+ int pid = xvfork();
if (pid == 0) {
struct termios termchild;
char *shell = getenv("SHELL");
diff --git a/miscutils/crontab.c b/miscutils/crontab.c
index 5557bc4..b8a5abc 100644
--- a/miscutils/crontab.c
+++ b/miscutils/crontab.c
@@ -20,10 +20,8 @@
static void edit_file(const struct passwd *pas, const char *file)
{
const char *ptr;
- int pid = vfork();
+ int pid = xvfork();
- if (pid < 0) /* failure */
- bb_perror_msg_and_die("vfork");
if (pid) { /* parent */
wait4pid(pid);
return;
@@ -51,9 +49,7 @@
pid_t pid;
char c;
- pid = vfork();
- if (pid < 0) /* ERROR */
- bb_perror_msg_and_die("vfork");
+ pid = xvfork();
if (pid) { /* PARENT */
if (wait4pid(pid) == 0) {
/* exitcode 0: child says it can read */
diff --git a/miscutils/time.c b/miscutils/time.c
index 5cfbcef..9facc36 100644
--- a/miscutils/time.c
+++ b/miscutils/time.c
@@ -372,9 +372,7 @@
void (*quit_signal)(int);
resp->elapsed_ms = monotonic_ms();
- pid = vfork();
- if (pid < 0)
- bb_perror_msg_and_die("vfork");
+ pid = xvfork();
if (pid == 0) {
/* Child */
BB_EXECVP_or_die((char**)cmd);
diff --git a/miscutils/timeout.c b/miscutils/timeout.c
index f6e655a..48b8d8f 100644
--- a/miscutils/timeout.c
+++ b/miscutils/timeout.c
@@ -71,9 +71,7 @@
sv1 = argv[optind];
sv2 = argv[optind + 1];
#endif
- pid = vfork();
- if (pid < 0)
- bb_perror_msg_and_die("vfork");
+ pid = xvfork();
if (pid == 0) {
/* Child: spawn grandchild and exit */
parent = getppid();
diff --git a/networking/ftpd.c b/networking/ftpd.c
index c63b931..e8cae0a 100644
--- a/networking/ftpd.c
+++ b/networking/ftpd.c
@@ -632,10 +632,7 @@
xpiped_pair(outfd);
/*fflush_all(); - so far we dont use stdio on output */
- pid = BB_MMU ? fork() : vfork();
- if (pid < 0)
- bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork");
-
+ pid = BB_MMU ? xfork() : xvfork();
if (pid == 0) {
/* child */
#if !BB_MMU
diff --git a/networking/ifupdown.c b/networking/ifupdown.c
index 1bab2c5..69c56e8 100644
--- a/networking/ifupdown.c
+++ b/networking/ifupdown.c
@@ -1041,12 +1041,10 @@
xpiped_pair(outfd);
fflush_all();
- pid = vfork();
+ pid = xvfork();
- switch (pid) {
- case -1: /* failure */
- bb_perror_msg_and_die("vfork");
- case 0: /* child */
+ if (pid == 0) {
+ /* Child */
/* NB: close _first_, then move fds! */
close(infd.wr);
close(outfd.rd);
diff --git a/networking/inetd.c b/networking/inetd.c
index 2b0e006..7030062 100644
--- a/networking/inetd.c
+++ b/networking/inetd.c
@@ -1271,7 +1271,7 @@
pid = vfork();
if (pid < 0) { /* fork error */
- bb_perror_msg("fork");
+ bb_perror_msg("vfork"+1);
sleep(1);
restore_sigmask(&omask);
maybe_close(accepted_fd);
diff --git a/networking/nc.c b/networking/nc.c
index 5fd8bd7..0dacaf1 100644
--- a/networking/nc.c
+++ b/networking/nc.c
@@ -216,10 +216,8 @@
if (execparam) {
pid_t pid;
/* With more than one -l, repeatedly act as server */
- if (do_listen > 1 && (pid = vfork()) != 0) {
- /* parent or error */
- if (pid < 0)
- bb_perror_msg_and_die("vfork");
+ if (do_listen > 1 && (pid = xvfork()) != 0) {
+ /* parent */
/* prevent zombies */
signal(SIGCHLD, SIG_IGN);
close(cfd);
diff --git a/scripts/basic/docproc.c b/scripts/basic/docproc.c
index ef51812..50ef371 100644
--- a/scripts/basic/docproc.c
+++ b/scripts/basic/docproc.c
@@ -86,7 +86,7 @@
fflush(stdout);
switch(pid=fork()) {
case -1:
- perror("fork");
+ perror("vfork"+1);
exit(1);
case 0:
rflen = strlen(getenv("SRCTREE"));
diff --git a/shell/hush.c b/shell/hush.c
index 29ff3c4..831443e 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -3208,15 +3208,11 @@
#if !BB_MMU
to_free = NULL;
#endif
- pid = vfork();
- if (pid < 0)
- bb_perror_msg_and_die("vfork");
+ pid = xvfork();
if (pid == 0) {
/* child */
disable_restore_tty_pgrp_on_exit();
- pid = BB_MMU ? fork() : vfork();
- if (pid < 0)
- bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork");
+ pid = BB_MMU ? xfork() : xvfork();
if (pid != 0)
_exit(0);
/* grandchild */
@@ -4450,7 +4446,7 @@
argv_expanded = NULL;
if (command->pid < 0) { /* [v]fork failed */
/* Clearly indicate, was it fork or vfork */
- bb_perror_msg(BB_MMU ? "fork" : "vfork");
+ bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork");
} else {
pi->alive_cmds++;
#if ENABLE_HUSH_JOB
@@ -5586,10 +5582,7 @@
# endif
xpipe(channel);
- pid = BB_MMU ? fork() : vfork();
- if (pid < 0)
- bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork");
-
+ pid = BB_MMU ? xfork() : xvfork();
if (pid == 0) { /* child */
disable_restore_tty_pgrp_on_exit();
/* Process substitution is not considered to be usual
diff --git a/util-linux/script.c b/util-linux/script.c
index d9a62fb..c23117c 100644
--- a/util-linux/script.c
+++ b/util-linux/script.c
@@ -88,10 +88,7 @@
/* TODO: SIGWINCH? pass window size changes down to slave? */
- child_pid = vfork();
- if (child_pid < 0) {
- bb_perror_msg_and_die("vfork");
- }
+ child_pid = xvfork();
if (child_pid) {
/* parent */