📄 proc.c
字号:
Char **v; struct command *t;{ register struct process *pp; okpcntl(); ++v; do { pp = pfind(*v); pstart(pp, 0); } while (*v && *++v);}/* * %... & - builtin - put the job into the background */void/*ARGSUSED*/dobg1(v, t) Char **v; struct command *t;{ register struct process *pp; pp = pfind(v[0]); pstart(pp, 0);}/* * dostop - builtin - stop the job */void/*ARGSUSED*/dostop(v, t) Char **v; struct command *t;{ pkill(++v, SIGSTOP);}/* * dokill - builtin - superset of kill (1) */void/*ARGSUSED*/dokill(v, t) Char **v; struct command *t;{ register int signum = SIGTERM; register char *name; v++; if (v[0] && v[0][0] == '-') { if (v[0][1] == 'l') { for (signum = 1; signum < NSIG; signum++) { (void) fprintf(cshout, "%s ", sys_signame[signum]); if (signum == NSIG / 2) (void) fputc('\n', cshout); } (void) fputc('\n', cshout); return; } if (Isdigit(v[0][1])) { signum = atoi(short2str(v[0] + 1)); if (signum < 0 || signum > NSIG) stderror(ERR_NAME | ERR_BADSIG); } else { name = short2str(&v[0][1]); if (!strncasecmp(name, "sig", 3)) name += 3; for (signum = 1; signum < NSIG; signum++) if (!strcasecmp(sys_signame[signum], name)) break; if (signum == NSIG) { setname(vis_str(&v[0][1])); stderror(ERR_NAME | ERR_UNKSIG); } } v++; } pkill(v, signum);}static voidpkill(v, signum) Char **v; int signum;{ register struct process *pp, *np; register int jobflags = 0; int pid, err1 = 0; sigset_t omask; Char *cp; omask = sigmask(SIGCHLD); if (setintr) omask |= sigmask(SIGINT); omask = sigblock(omask) & ~omask; gflag = 0, tglob(v); if (gflag) { v = globall(v); if (v == 0) stderror(ERR_NAME | ERR_NOMATCH); } else { v = gargv = saveblk(v); trim(v); } while (v && (cp = *v)) { if (*cp == '%') { np = pp = pfind(cp); do jobflags |= np->p_flags; while ((np = np->p_friends) != pp); switch (signum) { case SIGSTOP: case SIGTSTP: case SIGTTIN: case SIGTTOU: if ((jobflags & PRUNNING) == 0) { (void) fprintf(csherr, "%s: Already suspended\n", vis_str(cp)); err1++; goto cont; } break; /* * suspend a process, kill -CONT %, then type jobs; the shell * says it is suspended, but it is running; thanks jaap.. */ case SIGCONT: pstart(pp, 0); goto cont; } if (killpg((pid_t) pp->p_jobid, signum) < 0) { (void) fprintf(csherr, "%s: %s\n", vis_str(cp), strerror(errno)); err1++; } if (signum == SIGTERM || signum == SIGHUP) (void) killpg((pid_t) pp->p_jobid, SIGCONT); } else if (!(Isdigit(*cp) || *cp == '-')) stderror(ERR_NAME | ERR_JOBARGS); else { pid = atoi(short2str(cp)); if (kill((pid_t) pid, signum) < 0) { (void) fprintf(csherr, "%d: %s\n", pid, strerror(errno)); err1++; goto cont; } if (signum == SIGTERM || signum == SIGHUP) (void) kill((pid_t) pid, SIGCONT); }cont: v++; } if (gargv) blkfree(gargv), gargv = 0; (void) sigsetmask(omask); if (err1) stderror(ERR_SILENT);}/* * pstart - start the job in foreground/background */voidpstart(pp, foregnd) register struct process *pp; int foregnd;{ register struct process *np; sigset_t omask; long jobflags = 0; omask = sigblock(sigmask(SIGCHLD)); np = pp; do { jobflags |= np->p_flags; if (np->p_flags & (PRUNNING | PSTOPPED)) { np->p_flags |= PRUNNING; np->p_flags &= ~PSTOPPED; if (foregnd) np->p_flags |= PFOREGND; else np->p_flags &= ~PFOREGND; } } while ((np = np->p_friends) != pp); if (!foregnd) pclrcurr(pp); (void) pprint(pp, foregnd ? NAME | JOBDIR : NUMBER | NAME | AMPERSAND); if (foregnd) (void) tcsetpgrp(FSHTTY, pp->p_jobid); if (jobflags & PSTOPPED) (void) killpg((pid_t) pp->p_jobid, SIGCONT); (void) sigsetmask(omask);}voidpanystop(neednl) bool neednl;{ register struct process *pp; chkstop = 2; for (pp = proclist.p_next; pp; pp = pp->p_next) if (pp->p_flags & PSTOPPED) stderror(ERR_STOPPED, neednl ? "\n" : "");}struct process *pfind(cp) Char *cp;{ register struct process *pp, *np; if (cp == 0 || cp[1] == 0 || eq(cp, STRcent2) || eq(cp, STRcentplus)) { if (pcurrent == NULL) stderror(ERR_NAME | ERR_JOBCUR); return (pcurrent); } if (eq(cp, STRcentminus) || eq(cp, STRcenthash)) { if (pprevious == NULL) stderror(ERR_NAME | ERR_JOBPREV); return (pprevious); } if (Isdigit(cp[1])) { int idx = atoi(short2str(cp + 1)); for (pp = proclist.p_next; pp; pp = pp->p_next) if (pp->p_index == idx && pp->p_pid == pp->p_jobid) return (pp); stderror(ERR_NAME | ERR_NOSUCHJOB); } np = NULL; for (pp = proclist.p_next; pp; pp = pp->p_next) if (pp->p_pid == pp->p_jobid) { if (cp[1] == '?') { register Char *dp; for (dp = pp->p_command; *dp; dp++) { if (*dp != cp[2]) continue; if (prefix(cp + 2, dp)) goto match; } } else if (prefix(cp + 1, pp->p_command)) { match: if (np) stderror(ERR_NAME | ERR_AMBIG); np = pp; } } if (np) return (np); stderror(ERR_NAME | cp[1] == '?' ? ERR_JOBPAT : ERR_NOSUCHJOB); /* NOTREACHED */ return (0);}/* * pgetcurr - find most recent job that is not pp, preferably stopped */static struct process *pgetcurr(pp) register struct process *pp;{ register struct process *np; register struct process *xp = NULL; for (np = proclist.p_next; np; np = np->p_next) if (np != pcurrent && np != pp && np->p_pid && np->p_pid == np->p_jobid) { if (np->p_flags & PSTOPPED) return (np); if (xp == NULL) xp = np; } return (xp);}/* * donotify - flag the job so as to report termination asynchronously */void/*ARGSUSED*/donotify(v, t) Char **v; struct command *t;{ register struct process *pp; pp = pfind(*++v); pp->p_flags |= PNOTIFY;}/* * Do the fork and whatever should be done in the child side that * should not be done if we are not forking at all (like for simple builtin's) * Also do everything that needs any signals fiddled with in the parent side * * Wanttty tells whether process and/or tty pgrps are to be manipulated: * -1: leave tty alone; inherit pgrp from parent * 0: already have tty; manipulate process pgrps only * 1: want to claim tty; manipulate process and tty pgrps * It is usually just the value of tpgrp. */intpfork(t, wanttty) struct command *t; /* command we are forking for */ int wanttty;{ register int pid; bool ignint = 0; int pgrp; sigset_t omask; /* * A child will be uninterruptible only under very special conditions. * Remember that the semantics of '&' is implemented by disconnecting the * process from the tty so signals do not need to ignored just for '&'. * Thus signals are set to default action for children unless: we have had * an "onintr -" (then specifically ignored) we are not playing with * signals (inherit action) */ if (setintr) ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT)) || (gointr && eq(gointr, STRminus)); /* * Check for maximum nesting of 16 processes to avoid Forking loops */ if (child == 16) stderror(ERR_NESTING, 16); /* * Hold SIGCHLD until we have the process installed in our table. */ omask = sigblock(sigmask(SIGCHLD)); while ((pid = fork()) < 0) if (setintr == 0) (void) sleep(FORKSLEEP); else { (void) sigsetmask(omask); stderror(ERR_NOPROC); } if (pid == 0) { settimes(); pgrp = pcurrjob ? pcurrjob->p_jobid : getpid(); pflushall(); pcurrjob = NULL; child++; if (setintr) { setintr = 0; /* until I think otherwise */ /* * Children just get blown away on SIGINT, SIGQUIT unless "onintr * -" seen. */ (void) signal(SIGINT, ignint ? SIG_IGN : SIG_DFL); (void) signal(SIGQUIT, ignint ? SIG_IGN : SIG_DFL); if (wanttty >= 0) { /* make stoppable */ (void) signal(SIGTSTP, SIG_DFL); (void) signal(SIGTTIN, SIG_DFL); (void) signal(SIGTTOU, SIG_DFL); } (void) signal(SIGTERM, parterm); } else if (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT)) { (void) signal(SIGINT, SIG_IGN); (void) signal(SIGQUIT, SIG_IGN); } pgetty(wanttty, pgrp); /* * Nohup and nice apply only to NODE_COMMAND's but it would be nice * (?!?) if you could say "nohup (foo;bar)" Then the parser would have * to know about nice/nohup/time */ if (t->t_dflg & F_NOHUP) (void) signal(SIGHUP, SIG_IGN); if (t->t_dflg & F_NICE) (void) setpriority(PRIO_PROCESS, 0, t->t_nice); } else { if (wanttty >= 0) (void) setpgid(pid, pcurrjob ? pcurrjob->p_jobid : pid); palloc(pid, t); (void) sigsetmask(omask); } return (pid);}static voidokpcntl(){ if (tpgrp == -1) stderror(ERR_JOBCONTROL); if (tpgrp == 0) stderror(ERR_JOBCTRLSUB);}/* * if we don't have vfork(), things can still go in the wrong order * resulting in the famous 'Stopped (tty output)'. But some systems * don't permit the setpgid() call, (these are more recent secure * systems such as ibm's aix). Then we'd rather print an error message * than hang the shell! * I am open to suggestions how to fix that. */voidpgetty(wanttty, pgrp) int wanttty, pgrp;{ sigset_t omask = 0; /* * christos: I am blocking the tty signals till I've set things * correctly.... */ if (wanttty > 0) omask = sigblock(sigmask(SIGTSTP)|sigmask(SIGTTIN)|sigmask(SIGTTOU)); /* * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de> * Don't check for tpgrp >= 0 so even non-interactive shells give * background jobs process groups Same for the comparison in the other part * of the #ifdef */ if (wanttty >= 0) if (setpgid(0, pgrp) == -1) { (void) fprintf(csherr, "csh: setpgid error.\n"); xexit(0); } if (wanttty > 0) { (void) tcsetpgrp(FSHTTY, pgrp); (void) sigsetmask(omask); } if (tpgrp > 0) tpgrp = 0; /* gave tty away */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -