Use vfork, by vodz

This commit is contained in:
Glenn L McGrath 2002-11-15 22:18:01 +00:00
parent 02fcd2d2df
commit 39289b54a7

View File

@ -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 <emanuele.aina@tiscali.it>
*
* Based on the Debian run-parts program, version 1.15
* Copyright (C) 1996 Jeff Noxon <jeff@router.patch.net>,
* Copyright (C) 1996-1999 Guy Maor <maor@debian.org>
*
* rewrite to vfork usage by
* Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
*
* 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 <sys/types.h>
#include <sys/wait.h>
@ -55,6 +22,7 @@
#include <dirent.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#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;
}
}