mirror of
https://github.com/sheumann/hush.git
synced 2025-01-16 09:30:29 +00:00
Use vfork, by vodz
This commit is contained in:
parent
02fcd2d2df
commit
39289b54a7
@ -1,53 +1,20 @@
|
|||||||
/* vi: set sw=4 ts=4: */
|
/* 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>
|
* Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it>
|
||||||
*
|
* rewrite to vfork usage by
|
||||||
* Based on the Debian run-parts program, version 1.15
|
* Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
|
||||||
* Copyright (C) 1996 Jeff Noxon <jeff@router.patch.net>,
|
|
||||||
* Copyright (C) 1996-1999 Guy Maor <maor@debian.org>
|
|
||||||
*
|
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation; either version 2 of the License, or
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
* (at your option) any later version.
|
* (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/types.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
@ -55,6 +22,7 @@
|
|||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#include "libbb.h"
|
#include "libbb.h"
|
||||||
|
|
||||||
@ -86,6 +54,11 @@ extern int run_parts(char **args, const unsigned char test_mode)
|
|||||||
int i;
|
int i;
|
||||||
int exitstatus = 0;
|
int exitstatus = 0;
|
||||||
|
|
||||||
|
#if __GNUC__
|
||||||
|
/* Avoid longjmp clobbering */
|
||||||
|
(void) &i;
|
||||||
|
(void) &exitstatus;
|
||||||
|
#endif
|
||||||
/* scandir() isn't POSIX, but it makes things easy. */
|
/* scandir() isn't POSIX, but it makes things easy. */
|
||||||
entries = scandir(args[0], &namelist, valid_name, alphasort);
|
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)
|
if (test_mode)
|
||||||
printf("run-parts would run %s\n", filename);
|
printf("run-parts would run %s\n", filename);
|
||||||
else {
|
else {
|
||||||
|
/* exec_errno is common vfork variable */
|
||||||
|
volatile int exec_errno = 0;
|
||||||
int result;
|
int result;
|
||||||
int pid;
|
int pid;
|
||||||
|
|
||||||
if ((pid = fork()) < 0) {
|
if ((pid = vfork()) < 0) {
|
||||||
perror_msg_and_die("failed to fork");
|
perror_msg_and_die("failed to fork");
|
||||||
} else if (!pid) {
|
} else if (!pid) {
|
||||||
execv(args[0], args);
|
args[0] = filename;
|
||||||
perror_msg_and_die("failed to exec %s", args[0]);
|
execv(filename, args);
|
||||||
|
exec_errno = errno;
|
||||||
|
_exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
waitpid(pid, &result, 0);
|
waitpid(pid, &result, 0);
|
||||||
|
if(exec_errno) {
|
||||||
|
errno = exec_errno;
|
||||||
|
perror_msg_and_die("failed to exec %s", filename);
|
||||||
|
}
|
||||||
if (WIFEXITED(result) && WEXITSTATUS(result)) {
|
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;
|
exitstatus = 1;
|
||||||
} else if (WIFSIGNALED(result)) {
|
} 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;
|
exitstatus = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user