The latest patch from Larry

This commit is contained in:
Eric Andersen 2001-05-21 16:18:22 +00:00
parent 8ae319a322
commit ada18ff0f5
2 changed files with 122 additions and 62 deletions

92
hush.c
View File

@ -365,6 +365,7 @@ static int setup_redirects(struct child_prog *prog, int squirrel[]);
static int pipe_wait(struct pipe *pi); static int pipe_wait(struct pipe *pi);
static int run_list_real(struct pipe *pi); static int run_list_real(struct pipe *pi);
static void pseudo_exec(struct child_prog *child) __attribute__ ((noreturn)); static void pseudo_exec(struct child_prog *child) __attribute__ ((noreturn));
int controlling_tty(int check_pgrp);
static int run_pipe_real(struct pipe *pi); static int run_pipe_real(struct pipe *pi);
/* extended glob support: */ /* extended glob support: */
static int globhack(const char *src, int flags, glob_t *pglob); static int globhack(const char *src, int flags, glob_t *pglob);
@ -576,7 +577,7 @@ static int builtin_fg_bg(struct child_prog *child)
signal(SIGTTOU, SIG_IGN); signal(SIGTTOU, SIG_IGN);
/* suppress messages when run from /linuxrc mag@sysgo.de */ /* suppress messages when run from /linuxrc mag@sysgo.de */
if (tcsetpgrp(0, pi->pgrp) && errno != ENOTTY) if (tcsetpgrp(0, pi->pgrp) && errno != ENOTTY)
perror_msg("tcsetpgrp"); perror_msg("tcsetpgrp-1");
signal(SIGTTOU, SIG_DFL); signal(SIGTTOU, SIG_DFL);
job_list->fg = pi; job_list->fg = pi;
} }
@ -1059,13 +1060,14 @@ static int pipe_wait(struct pipe *pi)
return rcode; return rcode;
} }
/* very simple version for testing */ /* never returns */
static void pseudo_exec(struct child_prog *child) static void pseudo_exec(struct child_prog *child)
{ {
int i, rcode; int i, rcode;
struct built_in_command *x; struct built_in_command *x;
if (child->argv) { if (child->argv) {
for (i=0; is_assignment(child->argv[i]); i++) { for (i=0; is_assignment(child->argv[i]); i++) {
debug_printf("pid %d environment modification: %s\n",getpid(),child->argv[i]);
putenv(strdup(child->argv[i])); putenv(strdup(child->argv[i]));
} }
child->argv+=i; /* XXX this hack isn't so horrible, since we are about child->argv+=i; /* XXX this hack isn't so horrible, since we are about
@ -1125,7 +1127,7 @@ static void pseudo_exec(struct child_prog *child)
#endif #endif
debug_printf("exec of %s\n",child->argv[0]); debug_printf("exec of %s\n",child->argv[0]);
execvp(child->argv[0],child->argv); execvp(child->argv[0],child->argv);
perror("execvp"); perror_msg("couldn't exec: %s",child->argv[0]);
exit(1); exit(1);
} else if (child->group) { } else if (child->group) {
debug_printf("runtime nesting to group\n"); debug_printf("runtime nesting to group\n");
@ -1224,7 +1226,7 @@ static void free_pipe(struct pipe *pi)
have, figure out why and see if a job has completed */ have, figure out why and see if a job has completed */
static void checkjobs() static void checkjobs()
{ {
int status; int status, ctty;
int prognum = 0; int prognum = 0;
struct pipe *pi; struct pipe *pi;
pid_t childpid; pid_t childpid;
@ -1264,8 +1266,31 @@ static void checkjobs()
perror_msg("waitpid"); perror_msg("waitpid");
/* move the shell to the foreground */ /* move the shell to the foreground */
if (tcsetpgrp(0, getpgrp()) && errno != ENOTTY) if (interactive && (ctty=controlling_tty(0))!=-1) {
perror_msg("tcsetpgrp"); if (tcsetpgrp(ctty, getpgrp()))
perror_msg("tcsetpgrp-2");
}
}
/* Figure out our controlling tty, checking in order stderr,
* stdin, and stdout. If check_pgrp is set, also check that
* we belong to the foreground process group associated with
* that tty. The value of ctty is needed in order to call
* tcsetpgrp(ctty, ...); */
int controlling_tty(int check_pgrp)
{
pid_t curpgrp;
int ctty;
if ((curpgrp = tcgetpgrp(ctty = 2)) < 0
&& (curpgrp = tcgetpgrp(ctty = 0)) < 0
&& (curpgrp = tcgetpgrp(ctty = 1)) < 0)
return errno = ENOTTY, -1;
if (check_pgrp && curpgrp != getpgrp())
return errno = EPERM, -1;
return ctty;
} }
/* run_pipe_real() starts all the jobs, but doesn't wait for anything /* run_pipe_real() starts all the jobs, but doesn't wait for anything
@ -1295,17 +1320,11 @@ static int run_pipe_real(struct pipe *pi)
ctty = -1; ctty = -1;
nextin = 0; nextin = 0;
pi->pgrp = 0; pi->pgrp = -1;
/* Check if we are supposed to run in the foreground */ /* Check if we are supposed to run in the foreground */
if (interactive && pi->followup!=PIPE_BG) { if (interactive && pi->followup!=PIPE_BG) {
if ((pi->pgrp = tcgetpgrp(ctty = 2)) < 0 if ((ctty = controlling_tty(pi->pgrp<0)) < 0) return -1;
&& (pi->pgrp = tcgetpgrp(ctty = 0)) < 0
&& (pi->pgrp = tcgetpgrp(ctty = 1)) < 0)
return errno = ENOTTY, -1;
if (pi->pgrp < 0 && pi->pgrp != getpgrp())
return errno = EPERM, -1;
} }
/* Check if this is a simple builtin (not part of a pipe). /* Check if this is a simple builtin (not part of a pipe).
@ -1395,11 +1414,11 @@ static int run_pipe_real(struct pipe *pi)
* and the pipe fd is available for dup'ing. */ * and the pipe fd is available for dup'ing. */
setup_redirects(child,NULL); setup_redirects(child,NULL);
if (pi->followup!=PIPE_BG) { if (interactive && pi->followup!=PIPE_BG) {
/* If we (the child) win the race, put ourselves in the process /* If we (the child) win the race, put ourselves in the process
* group whose leader is the first process in this pipe. */ * group whose leader is the first process in this pipe. */
if (pi->pgrp < 0) { if (pi->pgrp < 0) {
pi->pgrp = child->pid; pi->pgrp = getpid();
} }
if (setpgid(0, pi->pgrp) == 0) { if (setpgid(0, pi->pgrp) == 0) {
signal(SIGTTOU, SIG_IGN); signal(SIGTTOU, SIG_IGN);
@ -1461,13 +1480,15 @@ static int run_list_real(struct pipe *pi)
if (interactive) { if (interactive) {
/* move the new process group into the foreground */ /* move the new process group into the foreground */
/* suppress messages when run from /linuxrc mag@sysgo.de */ /* suppress messages when run from /linuxrc mag@sysgo.de */
/* XXX probably this "0" should come from controlling_tty() */
if (tcsetpgrp(0, pi->pgrp) && errno != ENOTTY) if (tcsetpgrp(0, pi->pgrp) && errno != ENOTTY)
perror_msg("tcsetpgrp"); perror_msg("tcsetpgrp-3");
rcode = pipe_wait(pi); rcode = pipe_wait(pi);
if (tcsetpgrp(0, getpgrp()) && errno != ENOTTY) if (tcsetpgrp(0, getpgrp()) && errno != ENOTTY)
perror_msg("tcsetpgrp"); perror_msg("tcsetpgrp-4");
} else { } else {
rcode = pipe_wait(pi); rcode = pipe_wait(pi);
debug_printf("pipe_wait returned %d\n",rcode);
} }
} }
last_return_code=rcode; last_return_code=rcode;
@ -2484,6 +2505,25 @@ static int parse_file_outer(FILE *f)
return rcode; return rcode;
} }
/* I think Erik wrote this. It looks imperfect at best */
void grab_tty_control(void)
{
pid_t initialpgrp;
do {
initialpgrp = tcgetpgrp(fileno(stderr));
if (initialpgrp < 0) {
error_msg("sh: can't access tty; job control disabled\n");
}
if (initialpgrp == -1)
initialpgrp = getpgrp();
else if (initialpgrp != getpgrp()) {
killpg(initialpgrp, SIGTTIN);
continue;
}
} while (0);
}
int shell_main(int argc, char **argv) int shell_main(int argc, char **argv)
{ {
int opt; int opt;
@ -2507,19 +2547,6 @@ int shell_main(int argc, char **argv)
/* If we get started under a job aware app (like bash /* If we get started under a job aware app (like bash
* for example), make sure we are now in charge so we * for example), make sure we are now in charge so we
* don't fight over who gets the foreground */ * don't fight over who gets the foreground */
do {
pid_t initialpgrp;
initialpgrp = tcgetpgrp(fileno(stderr));
if (initialpgrp < 0) {
error_msg_and_die("sh: can't access tty; job control disabled\n");
}
if (initialpgrp == -1)
initialpgrp = getpgrp();
else if (initialpgrp != getpgrp()) {
killpg(initialpgrp, SIGTTIN);
continue;
}
} while (0);
/* don't pay any attention to this signal; it just confuses /* don't pay any attention to this signal; it just confuses
things and isn't really meant for shells anyway */ things and isn't really meant for shells anyway */
signal(SIGTTOU, SIG_IGN); signal(SIGTTOU, SIG_IGN);
@ -2588,6 +2615,9 @@ int shell_main(int argc, char **argv)
if (interactive) { if (interactive) {
/* Looks like they want an interactive shell */ /* Looks like they want an interactive shell */
fprintf(stdout, "\nhush -- the humble shell v0.01 (testing)\n\n"); fprintf(stdout, "\nhush -- the humble shell v0.01 (testing)\n\n");
grab_tty_control();
}
if (argv[optind]==NULL) {
opt=parse_file_outer(stdin); opt=parse_file_outer(stdin);
goto final_return; goto final_return;
} }

View File

@ -365,6 +365,7 @@ static int setup_redirects(struct child_prog *prog, int squirrel[]);
static int pipe_wait(struct pipe *pi); static int pipe_wait(struct pipe *pi);
static int run_list_real(struct pipe *pi); static int run_list_real(struct pipe *pi);
static void pseudo_exec(struct child_prog *child) __attribute__ ((noreturn)); static void pseudo_exec(struct child_prog *child) __attribute__ ((noreturn));
int controlling_tty(int check_pgrp);
static int run_pipe_real(struct pipe *pi); static int run_pipe_real(struct pipe *pi);
/* extended glob support: */ /* extended glob support: */
static int globhack(const char *src, int flags, glob_t *pglob); static int globhack(const char *src, int flags, glob_t *pglob);
@ -576,7 +577,7 @@ static int builtin_fg_bg(struct child_prog *child)
signal(SIGTTOU, SIG_IGN); signal(SIGTTOU, SIG_IGN);
/* suppress messages when run from /linuxrc mag@sysgo.de */ /* suppress messages when run from /linuxrc mag@sysgo.de */
if (tcsetpgrp(0, pi->pgrp) && errno != ENOTTY) if (tcsetpgrp(0, pi->pgrp) && errno != ENOTTY)
perror_msg("tcsetpgrp"); perror_msg("tcsetpgrp-1");
signal(SIGTTOU, SIG_DFL); signal(SIGTTOU, SIG_DFL);
job_list->fg = pi; job_list->fg = pi;
} }
@ -1059,13 +1060,14 @@ static int pipe_wait(struct pipe *pi)
return rcode; return rcode;
} }
/* very simple version for testing */ /* never returns */
static void pseudo_exec(struct child_prog *child) static void pseudo_exec(struct child_prog *child)
{ {
int i, rcode; int i, rcode;
struct built_in_command *x; struct built_in_command *x;
if (child->argv) { if (child->argv) {
for (i=0; is_assignment(child->argv[i]); i++) { for (i=0; is_assignment(child->argv[i]); i++) {
debug_printf("pid %d environment modification: %s\n",getpid(),child->argv[i]);
putenv(strdup(child->argv[i])); putenv(strdup(child->argv[i]));
} }
child->argv+=i; /* XXX this hack isn't so horrible, since we are about child->argv+=i; /* XXX this hack isn't so horrible, since we are about
@ -1125,7 +1127,7 @@ static void pseudo_exec(struct child_prog *child)
#endif #endif
debug_printf("exec of %s\n",child->argv[0]); debug_printf("exec of %s\n",child->argv[0]);
execvp(child->argv[0],child->argv); execvp(child->argv[0],child->argv);
perror("execvp"); perror_msg("couldn't exec: %s",child->argv[0]);
exit(1); exit(1);
} else if (child->group) { } else if (child->group) {
debug_printf("runtime nesting to group\n"); debug_printf("runtime nesting to group\n");
@ -1224,7 +1226,7 @@ static void free_pipe(struct pipe *pi)
have, figure out why and see if a job has completed */ have, figure out why and see if a job has completed */
static void checkjobs() static void checkjobs()
{ {
int status; int status, ctty;
int prognum = 0; int prognum = 0;
struct pipe *pi; struct pipe *pi;
pid_t childpid; pid_t childpid;
@ -1264,8 +1266,31 @@ static void checkjobs()
perror_msg("waitpid"); perror_msg("waitpid");
/* move the shell to the foreground */ /* move the shell to the foreground */
if (tcsetpgrp(0, getpgrp()) && errno != ENOTTY) if (interactive && (ctty=controlling_tty(0))!=-1) {
perror_msg("tcsetpgrp"); if (tcsetpgrp(ctty, getpgrp()))
perror_msg("tcsetpgrp-2");
}
}
/* Figure out our controlling tty, checking in order stderr,
* stdin, and stdout. If check_pgrp is set, also check that
* we belong to the foreground process group associated with
* that tty. The value of ctty is needed in order to call
* tcsetpgrp(ctty, ...); */
int controlling_tty(int check_pgrp)
{
pid_t curpgrp;
int ctty;
if ((curpgrp = tcgetpgrp(ctty = 2)) < 0
&& (curpgrp = tcgetpgrp(ctty = 0)) < 0
&& (curpgrp = tcgetpgrp(ctty = 1)) < 0)
return errno = ENOTTY, -1;
if (check_pgrp && curpgrp != getpgrp())
return errno = EPERM, -1;
return ctty;
} }
/* run_pipe_real() starts all the jobs, but doesn't wait for anything /* run_pipe_real() starts all the jobs, but doesn't wait for anything
@ -1295,17 +1320,11 @@ static int run_pipe_real(struct pipe *pi)
ctty = -1; ctty = -1;
nextin = 0; nextin = 0;
pi->pgrp = 0; pi->pgrp = -1;
/* Check if we are supposed to run in the foreground */ /* Check if we are supposed to run in the foreground */
if (interactive && pi->followup!=PIPE_BG) { if (interactive && pi->followup!=PIPE_BG) {
if ((pi->pgrp = tcgetpgrp(ctty = 2)) < 0 if ((ctty = controlling_tty(pi->pgrp<0)) < 0) return -1;
&& (pi->pgrp = tcgetpgrp(ctty = 0)) < 0
&& (pi->pgrp = tcgetpgrp(ctty = 1)) < 0)
return errno = ENOTTY, -1;
if (pi->pgrp < 0 && pi->pgrp != getpgrp())
return errno = EPERM, -1;
} }
/* Check if this is a simple builtin (not part of a pipe). /* Check if this is a simple builtin (not part of a pipe).
@ -1395,11 +1414,11 @@ static int run_pipe_real(struct pipe *pi)
* and the pipe fd is available for dup'ing. */ * and the pipe fd is available for dup'ing. */
setup_redirects(child,NULL); setup_redirects(child,NULL);
if (pi->followup!=PIPE_BG) { if (interactive && pi->followup!=PIPE_BG) {
/* If we (the child) win the race, put ourselves in the process /* If we (the child) win the race, put ourselves in the process
* group whose leader is the first process in this pipe. */ * group whose leader is the first process in this pipe. */
if (pi->pgrp < 0) { if (pi->pgrp < 0) {
pi->pgrp = child->pid; pi->pgrp = getpid();
} }
if (setpgid(0, pi->pgrp) == 0) { if (setpgid(0, pi->pgrp) == 0) {
signal(SIGTTOU, SIG_IGN); signal(SIGTTOU, SIG_IGN);
@ -1461,13 +1480,15 @@ static int run_list_real(struct pipe *pi)
if (interactive) { if (interactive) {
/* move the new process group into the foreground */ /* move the new process group into the foreground */
/* suppress messages when run from /linuxrc mag@sysgo.de */ /* suppress messages when run from /linuxrc mag@sysgo.de */
/* XXX probably this "0" should come from controlling_tty() */
if (tcsetpgrp(0, pi->pgrp) && errno != ENOTTY) if (tcsetpgrp(0, pi->pgrp) && errno != ENOTTY)
perror_msg("tcsetpgrp"); perror_msg("tcsetpgrp-3");
rcode = pipe_wait(pi); rcode = pipe_wait(pi);
if (tcsetpgrp(0, getpgrp()) && errno != ENOTTY) if (tcsetpgrp(0, getpgrp()) && errno != ENOTTY)
perror_msg("tcsetpgrp"); perror_msg("tcsetpgrp-4");
} else { } else {
rcode = pipe_wait(pi); rcode = pipe_wait(pi);
debug_printf("pipe_wait returned %d\n",rcode);
} }
} }
last_return_code=rcode; last_return_code=rcode;
@ -2484,6 +2505,25 @@ static int parse_file_outer(FILE *f)
return rcode; return rcode;
} }
/* I think Erik wrote this. It looks imperfect at best */
void grab_tty_control(void)
{
pid_t initialpgrp;
do {
initialpgrp = tcgetpgrp(fileno(stderr));
if (initialpgrp < 0) {
error_msg("sh: can't access tty; job control disabled\n");
}
if (initialpgrp == -1)
initialpgrp = getpgrp();
else if (initialpgrp != getpgrp()) {
killpg(initialpgrp, SIGTTIN);
continue;
}
} while (0);
}
int shell_main(int argc, char **argv) int shell_main(int argc, char **argv)
{ {
int opt; int opt;
@ -2507,19 +2547,6 @@ int shell_main(int argc, char **argv)
/* If we get started under a job aware app (like bash /* If we get started under a job aware app (like bash
* for example), make sure we are now in charge so we * for example), make sure we are now in charge so we
* don't fight over who gets the foreground */ * don't fight over who gets the foreground */
do {
pid_t initialpgrp;
initialpgrp = tcgetpgrp(fileno(stderr));
if (initialpgrp < 0) {
error_msg_and_die("sh: can't access tty; job control disabled\n");
}
if (initialpgrp == -1)
initialpgrp = getpgrp();
else if (initialpgrp != getpgrp()) {
killpg(initialpgrp, SIGTTIN);
continue;
}
} while (0);
/* don't pay any attention to this signal; it just confuses /* don't pay any attention to this signal; it just confuses
things and isn't really meant for shells anyway */ things and isn't really meant for shells anyway */
signal(SIGTTOU, SIG_IGN); signal(SIGTTOU, SIG_IGN);
@ -2588,6 +2615,9 @@ int shell_main(int argc, char **argv)
if (interactive) { if (interactive) {
/* Looks like they want an interactive shell */ /* Looks like they want an interactive shell */
fprintf(stdout, "\nhush -- the humble shell v0.01 (testing)\n\n"); fprintf(stdout, "\nhush -- the humble shell v0.01 (testing)\n\n");
grab_tty_control();
}
if (argv[optind]==NULL) {
opt=parse_file_outer(stdin); opt=parse_file_outer(stdin);
goto final_return; goto final_return;
} }