From 39289b54a74a37e485adac39dec17938ac568b3c Mon Sep 17 00:00:00 2001 From: Glenn L McGrath Date: Fri, 15 Nov 2002 22:18:01 +0000 Subject: [PATCH] Use vfork, by vodz --- libbb/run_parts.c | 64 ++++++++++++++++------------------------------- 1 file changed, 22 insertions(+), 42 deletions(-) diff --git a/libbb/run_parts.c b/libbb/run_parts.c index bf906a399..8399a6afb 100644 --- a/libbb/run_parts.c +++ b/libbb/run_parts.c @@ -1,53 +1,20 @@ /* vi: set sw=4 ts=4: */ /* - * Mini run-parts implementation for busybox + * run command from specified directory * * * Copyright (C) 2001 by Emanuele Aina - * - * Based on the Debian run-parts program, version 1.15 - * Copyright (C) 1996 Jeff Noxon , - * Copyright (C) 1996-1999 Guy Maor - * + * rewrite to vfork usage by + * Copyright (C) 2002 by Vladimir Oleynik * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - * 02111-1307 USA * */ -/* This is my first attempt to write a program in C (well, this is my first - * attempt to write a program! :-) . */ - -/* This piece of code is heavily based on the original version of run-parts, - * taken from debian-utils. I've only removed the long options and a the - * report mode. As the original run-parts support only long options, I've - * broken compatibility because the BusyBox policy doesn't allow them. - * The supported options are: - * -t test. Print the name of the files to be executed, without - * execute them. - * -a ARG argument. Pass ARG as an argument the program executed. It can - * be repeated to pass multiple arguments. - * -u MASK umask. Set the umask of the program executed to MASK. */ - -/* TODO - * done - convert calls to error in perror... and remove error() - * done - convert malloc/realloc to their x... counterparts - * done - remove catch_sigchld - * done - use bb's concat_path_file() - * done - declare run_parts_main() as extern and any other function as static? - */ #include #include @@ -55,6 +22,7 @@ #include #include #include +#include #include "libbb.h" @@ -86,6 +54,11 @@ extern int run_parts(char **args, const unsigned char test_mode) int i; int exitstatus = 0; +#if __GNUC__ + /* Avoid longjmp clobbering */ + (void) &i; + (void) &exitstatus; +#endif /* scandir() isn't POSIX, but it makes things easy. */ entries = scandir(args[0], &namelist, valid_name, alphasort); @@ -104,23 +77,30 @@ extern int run_parts(char **args, const unsigned char test_mode) if (test_mode) printf("run-parts would run %s\n", filename); else { + /* exec_errno is common vfork variable */ + volatile int exec_errno = 0; int result; int pid; - if ((pid = fork()) < 0) { + if ((pid = vfork()) < 0) { perror_msg_and_die("failed to fork"); } else if (!pid) { - execv(args[0], args); - perror_msg_and_die("failed to exec %s", args[0]); + args[0] = filename; + execv(filename, args); + exec_errno = errno; + _exit(1); } waitpid(pid, &result, 0); - + if(exec_errno) { + errno = exec_errno; + perror_msg_and_die("failed to exec %s", filename); + } if (WIFEXITED(result) && WEXITSTATUS(result)) { - perror_msg("%s exited with return code %d", args[0], WEXITSTATUS(result)); + perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result)); exitstatus = 1; } else if (WIFSIGNALED(result)) { - perror_msg("%s exited because of uncaught signal %d", args[0], WTERMSIG(result)); + perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result)); exitstatus = 1; } }