mirror of
https://github.com/sheumann/hush.git
synced 2024-12-23 05:29:58 +00:00
Change vfork calls to use vfork_and_run operation with GNO-compatible semantics
This commit is contained in:
parent
e61b4040ec
commit
16c89af688
3
build
3
build
@ -52,4 +52,5 @@ libbb/parse_mode.c \
|
||||
libbb/messages.c \
|
||||
libbb/appletlib.c \
|
||||
libbb/get_last_path_component.c \
|
||||
libbb/mempcpy.c
|
||||
libbb/mempcpy.c \
|
||||
libbb/vfork_and_run.c
|
||||
|
@ -1035,6 +1035,10 @@ void bb_sanitize_stdio(void) FAST_FUNC;
|
||||
int sanitize_env_if_suid(void) FAST_FUNC;
|
||||
|
||||
|
||||
pid_t vfork_and_run(void (*fn)(void*) NORETURN, void *arg);
|
||||
pid_t xvfork_and_run(void (*fn)(void*) NORETURN, void *arg);
|
||||
|
||||
|
||||
char* single_argv(char **argv) FAST_FUNC;
|
||||
extern const char *const bb_argv_dash[]; /* "-", NULL */
|
||||
extern const char *opt_complementary;
|
||||
|
24
libbb/vfork_and_run.c
Normal file
24
libbb/vfork_and_run.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include <unistd.h>
|
||||
#include "libbb.h"
|
||||
|
||||
/* Like vfork, but calls fn(arg) in the child instead of returning.
|
||||
* This is designed to match the semantics of GNO's fork2 call.
|
||||
*/
|
||||
pid_t vfork_and_run(void (*fn)(void*) NORETURN, void *arg) {
|
||||
pid_t pid = vfork();
|
||||
|
||||
if (pid == 0) {
|
||||
fn(arg);
|
||||
}
|
||||
|
||||
return pid;
|
||||
}
|
||||
|
||||
pid_t xvfork_and_run(void (*fn)(void*) NORETURN, void *arg) {
|
||||
pid_t pid = vfork_and_run(fn, arg);
|
||||
|
||||
if (pid < 0)
|
||||
bb_perror_msg_and_die("vfork");
|
||||
|
||||
return pid;
|
||||
}
|
186
shell/hush.c
186
shell/hush.c
@ -5827,6 +5827,13 @@ static void parse_and_run_file(FILE *f)
|
||||
}
|
||||
|
||||
#if ENABLE_HUSH_TICK
|
||||
static void xforked_child(void *) NORETURN;
|
||||
struct child_args2 {
|
||||
int *channel;
|
||||
const char *s;
|
||||
char ***to_free_p;
|
||||
};
|
||||
|
||||
static FILE *generate_stream_from_string(const char *s, pid_t *pid_p)
|
||||
{
|
||||
pid_t pid;
|
||||
@ -5836,8 +5843,39 @@ static FILE *generate_stream_from_string(const char *s, pid_t *pid_p)
|
||||
# endif
|
||||
|
||||
xpipe(channel);
|
||||
pid = BB_MMU ? xfork() : xvfork();
|
||||
|
||||
struct child_args2 args_struct = {
|
||||
channel,
|
||||
s,
|
||||
&to_free,
|
||||
};
|
||||
pid = BB_MMU ? xfork() : xvfork_and_run(xforked_child, &args_struct);
|
||||
#if BB_MMU
|
||||
if (pid == 0) { /* child */
|
||||
xforked_child(&args_struct);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* parent */
|
||||
*pid_p = pid;
|
||||
# if ENABLE_HUSH_FAST
|
||||
G.count_SIGCHLD++;
|
||||
//bb_error_msg("[%d] fork in generate_stream_from_string:"
|
||||
// " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d",
|
||||
// getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
|
||||
# endif
|
||||
enable_restore_tty_pgrp_on_exit();
|
||||
# if !BB_MMU
|
||||
free(to_free);
|
||||
# endif
|
||||
close(channel[1]);
|
||||
close_on_exec_on(channel[0]);
|
||||
return xfdopen_for_read(channel[0]);
|
||||
}
|
||||
|
||||
static void xforked_child(void *args_struct) {
|
||||
struct child_args2 *args = (struct child_args2 *)args_struct;
|
||||
|
||||
disable_restore_tty_pgrp_on_exit();
|
||||
/* Process substitution is not considered to be usual
|
||||
* 'command execution'.
|
||||
@ -5849,8 +5887,8 @@ static FILE *generate_stream_from_string(const char *s, pid_t *pid_p)
|
||||
+ (1 << SIGTTOU)
|
||||
, SIG_IGN);
|
||||
CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */
|
||||
close(channel[0]); /* NB: close _first_, then move fd! */
|
||||
xmove_fd(channel[1], 1);
|
||||
close(args->channel[0]); /* NB: close _first_, then move fd! */
|
||||
xmove_fd(args->channel[1], 1);
|
||||
/* Prevent it from trying to handle ctrl-z etc */
|
||||
IF_HUSH_JOB(G.run_list_level = 1;)
|
||||
/* Awful hack for `trap` or $(trap).
|
||||
@ -5887,9 +5925,9 @@ static FILE *generate_stream_from_string(const char *s, pid_t *pid_p)
|
||||
*
|
||||
* Our solution: ONLY bare $(trap) or `trap` is special.
|
||||
*/
|
||||
s = skip_whitespace(s);
|
||||
if (strncmp(s, "trap", 4) == 0
|
||||
&& skip_whitespace(s + 4)[0] == '\0'
|
||||
args->s = skip_whitespace(args->s);
|
||||
if (strncmp(args->s, "trap", 4) == 0
|
||||
&& skip_whitespace(args->s + 4)[0] == '\0'
|
||||
) {
|
||||
static const char *const argv[] = { NULL, NULL };
|
||||
builtin_trap((char**)argv);
|
||||
@ -5905,30 +5943,14 @@ static FILE *generate_stream_from_string(const char *s, pid_t *pid_p)
|
||||
* huge=`cat BIG` # was blocking here forever
|
||||
* echo OK
|
||||
*/
|
||||
re_execute_shell(&to_free,
|
||||
s,
|
||||
re_execute_shell(args->to_free_p,
|
||||
args->s,
|
||||
G.global_argv[0],
|
||||
G.global_argv + 1,
|
||||
NULL);
|
||||
# endif
|
||||
}
|
||||
|
||||
/* parent */
|
||||
*pid_p = pid;
|
||||
# if ENABLE_HUSH_FAST
|
||||
G.count_SIGCHLD++;
|
||||
//bb_error_msg("[%d] fork in generate_stream_from_string:"
|
||||
// " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d",
|
||||
// getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
|
||||
# endif
|
||||
enable_restore_tty_pgrp_on_exit();
|
||||
# if !BB_MMU
|
||||
free(to_free);
|
||||
# endif
|
||||
close(channel[1]);
|
||||
close_on_exec_on(channel[0]);
|
||||
return xfdopen_for_read(channel[0]);
|
||||
}
|
||||
|
||||
/* Return code is exit status of the process that is run. */
|
||||
static int process_command_subs(o_string *dest, const char *s)
|
||||
@ -6926,6 +6948,17 @@ static int redirect_and_varexp_helper(char ***new_env_p,
|
||||
}
|
||||
return rcode;
|
||||
}
|
||||
|
||||
static void forked_child(void *) NORETURN;
|
||||
struct child_args {
|
||||
struct pipe **pi_p;
|
||||
int *next_infd_p;
|
||||
struct fd_pair *pipefds_p;
|
||||
struct command **command_p;
|
||||
volatile nommu_save_t *nommu_save_p;
|
||||
char ***argv_expanded_p;
|
||||
};
|
||||
|
||||
static NOINLINE int run_pipe(struct pipe *pi)
|
||||
{
|
||||
static const char *const null_ptr = NULL;
|
||||
@ -7191,52 +7224,21 @@ static NOINLINE int run_pipe(struct pipe *pi)
|
||||
if (cmd_no < pi->num_cmds)
|
||||
xpiped_pair(pipefds);
|
||||
|
||||
command->pid = BB_MMU ? fork() : vfork();
|
||||
if (!command->pid) { /* child */
|
||||
#if ENABLE_HUSH_JOB
|
||||
disable_restore_tty_pgrp_on_exit();
|
||||
CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */
|
||||
struct child_args args_struct = {
|
||||
&pi,
|
||||
&next_infd,
|
||||
&pipefds,
|
||||
&command,
|
||||
&nommu_save,
|
||||
&argv_expanded,
|
||||
};
|
||||
|
||||
/* Every child adds itself to new process group
|
||||
* with pgid == pid_of_first_child_in_pipe */
|
||||
if (G.run_list_level == 1 && G_interactive_fd) {
|
||||
pid_t pgrp;
|
||||
pgrp = pi->pgrp;
|
||||
if (pgrp < 0) /* true for 1st process only */
|
||||
pgrp = getpid();
|
||||
if (setpgid(0, pgrp) == 0
|
||||
&& pi->followup != PIPE_BG
|
||||
&& G_saved_tty_pgrp /* we have ctty */
|
||||
) {
|
||||
/* We do it in *every* child, not just first,
|
||||
* to avoid races */
|
||||
tcsetpgrp(G_interactive_fd, pgrp);
|
||||
}
|
||||
command->pid = BB_MMU ? fork() : vfork_and_run(forked_child, &args_struct);
|
||||
#if BB_MMU
|
||||
if (!command->pid) { /* child */
|
||||
forked_child(&args_struct);
|
||||
}
|
||||
#endif
|
||||
if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) {
|
||||
/* 1st cmd in backgrounded pipe
|
||||
* should have its stdin /dev/null'ed */
|
||||
close(0);
|
||||
if (open(bb_dev_null, O_RDONLY))
|
||||
xopen("/", O_RDONLY);
|
||||
} else {
|
||||
xmove_fd(next_infd, 0);
|
||||
}
|
||||
xmove_fd(pipefds.wr, 1);
|
||||
if (pipefds.rd > 1)
|
||||
close(pipefds.rd);
|
||||
/* Like bash, explicit redirects override pipes,
|
||||
* and the pipe fd is available for dup'ing. */
|
||||
if (setup_redirects(command, NULL))
|
||||
_exit(1);
|
||||
|
||||
/* Stores to nommu_save list of env vars putenv'ed
|
||||
* (NOMMU, on MMU we don't need that) */
|
||||
/* cast away volatility... */
|
||||
pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded);
|
||||
/* pseudo_exec() does not return */
|
||||
}
|
||||
|
||||
/* parent or error */
|
||||
#if ENABLE_HUSH_FAST
|
||||
@ -7284,6 +7286,56 @@ static NOINLINE int run_pipe(struct pipe *pi)
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
static void forked_child(void *args_struct) {
|
||||
struct child_args *args = (struct child_args *)args_struct;
|
||||
|
||||
#if ENABLE_HUSH_JOB
|
||||
disable_restore_tty_pgrp_on_exit();
|
||||
CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */
|
||||
|
||||
/* Every child adds itself to new process group
|
||||
* with pgid == pid_of_first_child_in_pipe */
|
||||
if (G.run_list_level == 1 && G_interactive_fd) {
|
||||
pid_t pgrp;
|
||||
pgrp = (*args->pi_p)->pgrp;
|
||||
if (pgrp < 0) /* true for 1st process only */
|
||||
pgrp = getpid();
|
||||
if (setpgid(0, pgrp) == 0
|
||||
&& (*args->pi_p)->followup != PIPE_BG
|
||||
&& G_saved_tty_pgrp /* we have ctty */
|
||||
) {
|
||||
/* We do it in *every* child, not just first,
|
||||
* to avoid races */
|
||||
tcsetpgrp(G_interactive_fd, pgrp);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if ((*args->pi_p)->alive_cmds == 0 && (*args->pi_p)->followup == PIPE_BG) {
|
||||
/* 1st cmd in backgrounded pipe
|
||||
* should have its stdin /dev/null'ed */
|
||||
close(0);
|
||||
if (open(bb_dev_null, O_RDONLY))
|
||||
xopen("/", O_RDONLY);
|
||||
} else {
|
||||
xmove_fd(*args->next_infd_p, 0);
|
||||
}
|
||||
xmove_fd(args->pipefds_p->wr, 1);
|
||||
if (args->pipefds_p->rd > 1)
|
||||
close(args->pipefds_p->rd);
|
||||
/* Like bash, explicit redirects override pipes,
|
||||
* and the pipe fd is available for dup'ing. */
|
||||
if (setup_redirects(*args->command_p, NULL))
|
||||
_exit(1);
|
||||
|
||||
/* Stores to nommu_save list of env vars putenv'ed
|
||||
* (NOMMU, on MMU we don't need that) */
|
||||
/* cast away volatility... */
|
||||
pseudo_exec((nommu_save_t*) args->nommu_save_p, *args->command_p, *args->argv_expanded_p);
|
||||
/* pseudo_exec() does not return */
|
||||
}
|
||||
|
||||
|
||||
/* NB: called by pseudo_exec, and therefore must not modify any
|
||||
* global data until exec/_exit (we can be a child after vfork!) */
|
||||
static int run_list(struct pipe *pi)
|
||||
|
Loading…
Reference in New Issue
Block a user