[BusyBox] another hush patch
larry at doolittle.boa.org
larry at doolittle.boa.org
Sat May 19 00:33:02 UTC 2001
This fixes two honest-to-goodness bugs, and starts to pull some of
the job control work into cleanly packaged routines. I also
tagged all those perror_msg("tcsetpgrp") messages, so we can
more readily focus on problems if any more leak through.
Unfortunately, this patch is based on CVS revision 1.29, before
the latest Vladimir/Andersen commit. I don't think the work
overlaps, although you might have to fight patch(1) to merge
the two efforts.
- Larry
--- /home/larry/cvs/busybox/hush.c Wed May 16 20:01:09 2001
+++ hush.c Fri May 18 23:28:58 2001
@@ -554,7 +554,7 @@
signal(SIGTTOU, SIG_IGN);
/* suppress messages when run from /linuxrc mag at sysgo.de */
if (tcsetpgrp(0, pi->pgrp) && errno != ENOTTY)
- perror_msg("tcsetpgrp");
+ perror_msg("tcsetpgrp-1");
signal(SIGTTOU, SIG_DFL);
job_list->fg = pi;
}
@@ -1263,7 +1263,28 @@
/* move the shell to the foreground */
if (tcsetpgrp(0, getpgrp()) && errno != ENOTTY)
- perror_msg("tcsetpgrp");
+ 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
@@ -1293,17 +1314,11 @@
ctty = -1;
nextin = 0;
- pi->pgrp = 0;
+ pi->pgrp = -1;
/* Check if we are supposed to run in the foreground */
if (interactive && pi->followup!=PIPE_BG) {
- if ((pi->pgrp = tcgetpgrp(ctty = 2)) < 0
- && (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;
+ if ((ctty = controlling_tty(pi->pgrp<0)) < 0) return -1;
}
/* Check if this is a simple builtin (not part of a pipe).
@@ -1393,11 +1408,11 @@
* and the pipe fd is available for dup'ing. */
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
* group whose leader is the first process in this pipe. */
if (pi->pgrp < 0) {
- pi->pgrp = child->pid;
+ pi->pgrp = getpid();
}
if (setpgid(0, pi->pgrp) == 0) {
signal(SIGTTOU, SIG_IGN);
@@ -1459,11 +1474,12 @@
if (interactive) {
/* move the new process group into the foreground */
/* suppress messages when run from /linuxrc mag at sysgo.de */
+ /* XXX probably this "0" should come from controlling_tty() */
if (tcsetpgrp(0, pi->pgrp) && errno != ENOTTY)
- perror_msg("tcsetpgrp");
+ perror_msg("tcsetpgrp-3");
rcode = pipe_wait(pi);
if (tcsetpgrp(0, getpgrp()) && errno != ENOTTY)
- perror_msg("tcsetpgrp");
+ perror_msg("tcsetpgrp-4");
} else {
rcode = pipe_wait(pi);
}
@@ -2500,6 +2516,25 @@
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 opt;
@@ -2525,19 +2560,6 @@
/* If we get started under a job aware app (like bash
* for example), make sure we are now in charge so we
* 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
things and isn't really meant for shells anyway */
signal(SIGTTOU, SIG_IGN);
@@ -2602,6 +2624,7 @@
if (interactive) {
/* Looks like they want an interactive shell */
fprintf(stdout, "\nhush -- the humble shell v0.01 (testing)\n\n");
+ grab_tty_control();
opt=parse_file_outer(stdin);
goto final_return;
}
More information about the busybox
mailing list