jobs.c revision 223024
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3436150Scharnier#if 0 3536150Scharnierstatic char sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95"; 3636150Scharnier#endif 371556Srgrimes#endif /* not lint */ 3899110Sobrien#include <sys/cdefs.h> 3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/jobs.c 223024 2011-06-12 23:06:04Z jilles $"); 401556Srgrimes 41213775Sjhb#include <sys/ioctl.h> 42213775Sjhb#include <sys/param.h> 43213775Sjhb#include <sys/resource.h> 44213775Sjhb#include <sys/time.h> 45213775Sjhb#include <sys/wait.h> 46213775Sjhb#include <errno.h> 4717987Speter#include <fcntl.h> 48213775Sjhb#include <paths.h> 4917987Speter#include <signal.h> 50213925Sjilles#include <stddef.h> 51213775Sjhb#include <stdlib.h> 5217987Speter#include <unistd.h> 5317987Speter 541556Srgrimes#include "shell.h" 551556Srgrimes#if JOBS 5617987Speter#include <termios.h> 571556Srgrimes#undef CEOF /* syntax.h redefines this */ 581556Srgrimes#endif 5917987Speter#include "redir.h" 6017987Speter#include "show.h" 611556Srgrimes#include "main.h" 621556Srgrimes#include "parser.h" 631556Srgrimes#include "nodes.h" 641556Srgrimes#include "jobs.h" 651556Srgrimes#include "options.h" 661556Srgrimes#include "trap.h" 671556Srgrimes#include "syntax.h" 681556Srgrimes#include "input.h" 691556Srgrimes#include "output.h" 701556Srgrimes#include "memalloc.h" 711556Srgrimes#include "error.h" 721556Srgrimes#include "mystring.h" 73223024Sjilles#include "var.h" 741556Srgrimes 751556Srgrimes 76213760Sobrienstatic struct job *jobtab; /* array of jobs */ 77213760Sobrienstatic int njobs; /* size of array */ 7828346SsteveMKINIT pid_t backgndpid = -1; /* pid of last background process */ 79209600SjillesMKINIT struct job *bgjob = NULL; /* last background process */ 801556Srgrimes#if JOBS 81213760Sobrienstatic struct job *jobmru; /* most recently used job list */ 82213760Sobrienstatic pid_t initialpgrp; /* pgrp of shell on invocation */ 831556Srgrimes#endif 8438536Scracauerint in_waitcmd = 0; /* are we in waitcmd()? */ 8538950Scracauerint in_dowait = 0; /* are we in dowait()? */ 8638536Scracauervolatile sig_atomic_t breakwaitcmd = 0; /* should wait be terminated? */ 8799762Stjrstatic int ttyfd = -1; 881556Srgrimes 8920425Ssteve#if JOBS 90213811Sobrienstatic void restartjob(struct job *); 9120425Ssteve#endif 92213811Sobrienstatic void freejob(struct job *); 93213811Sobrienstatic struct job *getjob(char *); 94213811Sobrienstatic pid_t dowait(int, struct job *); 95213811Sobrienstatic pid_t waitproc(int, int *); 96213811Sobrienstatic void checkzombies(void); 97213811Sobrienstatic void cmdtxt(union node *); 98213811Sobrienstatic void cmdputs(const char *); 9997659Stjr#if JOBS 100213811Sobrienstatic void setcurjob(struct job *); 101213811Sobrienstatic void deljob(struct job *); 102213811Sobrienstatic struct job *getcurjob(struct job *); 10397659Stjr#endif 104216217Sjillesstatic void printjobcmd(struct job *); 105216217Sjillesstatic void showjob(struct job *, int); 1061556Srgrimes 1071556Srgrimes 1081556Srgrimes/* 1091556Srgrimes * Turn job control on and off. 1101556Srgrimes */ 1111556Srgrimes 1121556SrgrimesMKINIT int jobctl; 1131556Srgrimes 11420425Ssteve#if JOBS 1151556Srgrimesvoid 11690111Simpsetjobctl(int on) 11717987Speter{ 11899762Stjr int i; 1191556Srgrimes 1201556Srgrimes if (on == jobctl || rootshell == 0) 1211556Srgrimes return; 1221556Srgrimes if (on) { 12399762Stjr if (ttyfd != -1) 12499762Stjr close(ttyfd); 12599762Stjr if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) { 12699762Stjr i = 0; 12799762Stjr while (i <= 2 && !isatty(i)) 12899762Stjr i++; 129109927Stjr if (i > 2 || (ttyfd = fcntl(i, F_DUPFD, 10)) < 0) 13099762Stjr goto out; 13199762Stjr } 132109927Stjr if (ttyfd < 10) { 133109927Stjr /* 134109927Stjr * Keep our TTY file descriptor out of the way of 135109927Stjr * the user's redirections. 136109927Stjr */ 137109927Stjr if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) { 138109927Stjr close(ttyfd); 139109927Stjr ttyfd = -1; 140109927Stjr goto out; 141109927Stjr } 142109927Stjr close(ttyfd); 143109927Stjr ttyfd = i; 144109927Stjr } 145103223Snectar if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) { 14699762Stjr close(ttyfd); 14799762Stjr ttyfd = -1; 14899762Stjr goto out; 14999762Stjr } 1501556Srgrimes do { /* while we are in the background */ 15199762Stjr initialpgrp = tcgetpgrp(ttyfd); 15220425Ssteve if (initialpgrp < 0) { 153199629Sjillesout: out2fmt_flush("sh: can't access tty; job control turned off\n"); 1541556Srgrimes mflag = 0; 1551556Srgrimes return; 1561556Srgrimes } 157216400Sjilles if (initialpgrp != getpgrp()) { 158216400Sjilles kill(0, SIGTTIN); 1591556Srgrimes continue; 1601556Srgrimes } 1611556Srgrimes } while (0); 1621556Srgrimes setsignal(SIGTSTP); 1631556Srgrimes setsignal(SIGTTOU); 1641556Srgrimes setsignal(SIGTTIN); 16517987Speter setpgid(0, rootpid); 16699762Stjr tcsetpgrp(ttyfd, rootpid); 1671556Srgrimes } else { /* turning job control off */ 16817987Speter setpgid(0, initialpgrp); 16999762Stjr tcsetpgrp(ttyfd, initialpgrp); 17099762Stjr close(ttyfd); 17199762Stjr ttyfd = -1; 1721556Srgrimes setsignal(SIGTSTP); 1731556Srgrimes setsignal(SIGTTOU); 1741556Srgrimes setsignal(SIGTTIN); 1751556Srgrimes } 1761556Srgrimes jobctl = on; 1771556Srgrimes} 17820425Ssteve#endif 1791556Srgrimes 1801556Srgrimes 1811556Srgrimes#if JOBS 18217987Speterint 18390111Simpfgcmd(int argc __unused, char **argv) 18417987Speter{ 1851556Srgrimes struct job *jp; 186100308Stjr pid_t pgrp; 1871556Srgrimes int status; 1881556Srgrimes 1891556Srgrimes jp = getjob(argv[1]); 1901556Srgrimes if (jp->jobctl == 0) 1911556Srgrimes error("job not created under job control"); 192216217Sjilles printjobcmd(jp); 19396933Stjr flushout(&output); 1941556Srgrimes pgrp = jp->ps[0].pid; 19599762Stjr tcsetpgrp(ttyfd, pgrp); 1961556Srgrimes restartjob(jp); 197100305Stjr jp->foreground = 1; 1981556Srgrimes INTOFF; 19945916Scracauer status = waitforjob(jp, (int *)NULL); 2001556Srgrimes INTON; 2011556Srgrimes return status; 2021556Srgrimes} 2031556Srgrimes 2041556Srgrimes 20517987Speterint 20690111Simpbgcmd(int argc, char **argv) 20717987Speter{ 2081556Srgrimes struct job *jp; 2091556Srgrimes 2101556Srgrimes do { 2111556Srgrimes jp = getjob(*++argv); 2121556Srgrimes if (jp->jobctl == 0) 2131556Srgrimes error("job not created under job control"); 21496933Stjr if (jp->state == JOBDONE) 21596933Stjr continue; 2161556Srgrimes restartjob(jp); 217100305Stjr jp->foreground = 0; 218216400Sjilles out1fmt("[%td] ", jp - jobtab + 1); 219216217Sjilles printjobcmd(jp); 2201556Srgrimes } while (--argc > 1); 2211556Srgrimes return 0; 2221556Srgrimes} 2231556Srgrimes 2241556Srgrimes 225213811Sobrienstatic void 22690111Simprestartjob(struct job *jp) 22717987Speter{ 2281556Srgrimes struct procstat *ps; 2291556Srgrimes int i; 2301556Srgrimes 2311556Srgrimes if (jp->state == JOBDONE) 2321556Srgrimes return; 23397660Stjr setcurjob(jp); 2341556Srgrimes INTOFF; 235216400Sjilles kill(-jp->ps[0].pid, SIGCONT); 2361556Srgrimes for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 23726104Ssteve if (WIFSTOPPED(ps->status)) { 2381556Srgrimes ps->status = -1; 2391556Srgrimes jp->state = 0; 2401556Srgrimes } 2411556Srgrimes } 2421556Srgrimes INTON; 2431556Srgrimes} 2441556Srgrimes#endif 2451556Srgrimes 2461556Srgrimes 2471556Srgrimesint 24897669Stjrjobscmd(int argc, char *argv[]) 24917987Speter{ 25097669Stjr char *id; 251163085Sstefanf int ch, mode; 25297669Stjr 25397669Stjr optind = optreset = 1; 254100663Stjr opterr = 0; 255163085Sstefanf mode = SHOWJOBS_DEFAULT; 256163085Sstefanf while ((ch = getopt(argc, argv, "lps")) != -1) { 25797669Stjr switch (ch) { 25897669Stjr case 'l': 259163085Sstefanf mode = SHOWJOBS_VERBOSE; 26097669Stjr break; 261163085Sstefanf case 'p': 262163085Sstefanf mode = SHOWJOBS_PGIDS; 263163085Sstefanf break; 26497669Stjr case 's': 265163085Sstefanf mode = SHOWJOBS_PIDS; 26697669Stjr break; 26797669Stjr case '?': 26897669Stjr default: 26997669Stjr error("unknown option: -%c", optopt); 27097669Stjr } 27197669Stjr } 27297669Stjr argc -= optind; 27397669Stjr argv += optind; 27497669Stjr 27597669Stjr if (argc == 0) 276163085Sstefanf showjobs(0, mode); 27797669Stjr else 27897669Stjr while ((id = *argv++) != NULL) 279216217Sjilles showjob(getjob(id), mode); 28097669Stjr 28197669Stjr return (0); 2821556Srgrimes} 2831556Srgrimes 284213811Sobrienstatic void 285216217Sjillesprintjobcmd(struct job *jp) 28697663Stjr{ 287216217Sjilles struct procstat *ps; 288216217Sjilles int i; 289216217Sjilles 290216217Sjilles for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 291216217Sjilles out1str(ps->cmd); 292216217Sjilles if (i > 0) 293216217Sjilles out1str(" | "); 294216217Sjilles } 295216217Sjilles out1c('\n'); 296216217Sjilles} 297216217Sjilles 298216217Sjillesstatic void 299216217Sjillesshowjob(struct job *jp, int mode) 300216217Sjilles{ 30197663Stjr char s[64]; 302216217Sjilles char statestr[64]; 30397663Stjr struct procstat *ps; 30497669Stjr struct job *j; 30597669Stjr int col, curr, i, jobno, prev, procno; 30697669Stjr char c; 3071556Srgrimes 308163085Sstefanf procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs; 30997663Stjr jobno = jp - jobtab + 1; 31097669Stjr curr = prev = 0; 31197669Stjr#if JOBS 31297669Stjr if ((j = getcurjob(NULL)) != NULL) { 31397669Stjr curr = j - jobtab + 1; 31497669Stjr if ((j = getcurjob(j)) != NULL) 31597669Stjr prev = j - jobtab + 1; 31697669Stjr } 31797669Stjr#endif 318216217Sjilles ps = jp->ps + jp->nprocs - 1; 319216217Sjilles if (jp->state == 0) { 320216217Sjilles strcpy(statestr, "Running"); 321216217Sjilles#if JOBS 322216217Sjilles } else if (jp->state == JOBSTOPPED) { 323216217Sjilles while (!WIFSTOPPED(ps->status) && ps > jp->ps) 324216217Sjilles ps--; 325216217Sjilles if (WIFSTOPPED(ps->status)) 326216217Sjilles i = WSTOPSIG(ps->status); 327216217Sjilles else 328216217Sjilles i = -1; 329216217Sjilles if (i > 0 && i < sys_nsig && sys_siglist[i]) 330216217Sjilles strcpy(statestr, sys_siglist[i]); 331216217Sjilles else 332216217Sjilles strcpy(statestr, "Suspended"); 333216217Sjilles#endif 334216217Sjilles } else if (WIFEXITED(ps->status)) { 335216217Sjilles if (WEXITSTATUS(ps->status) == 0) 336216217Sjilles strcpy(statestr, "Done"); 337216217Sjilles else 338216220Sjilles fmtstr(statestr, 64, "Done(%d)", 339216217Sjilles WEXITSTATUS(ps->status)); 340216217Sjilles } else { 341216217Sjilles i = WTERMSIG(ps->status); 342216217Sjilles if (i > 0 && i < sys_nsig && sys_siglist[i]) 343216217Sjilles strcpy(statestr, sys_siglist[i]); 344216217Sjilles else 345216217Sjilles fmtstr(statestr, 64, "Signal %d", i); 346216217Sjilles if (WCOREDUMP(ps->status)) 347216217Sjilles strcat(statestr, " (core dumped)"); 348216217Sjilles } 349216217Sjilles 35097663Stjr for (ps = jp->ps ; ; ps++) { /* for each process */ 351163085Sstefanf if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) { 352216199Sjilles out1fmt("%d\n", (int)ps->pid); 35397669Stjr goto skip; 35497669Stjr } 355216217Sjilles if (mode != SHOWJOBS_VERBOSE && ps != jp->ps) 35697669Stjr goto skip; 35797819Stjr if (jobno == curr && ps == jp->ps) 35897669Stjr c = '+'; 35997819Stjr else if (jobno == prev && ps == jp->ps) 36097669Stjr c = '-'; 36197669Stjr else 36297669Stjr c = ' '; 36397663Stjr if (ps == jp->ps) 36497669Stjr fmtstr(s, 64, "[%d] %c ", jobno, c); 36597663Stjr else 36697816Stjr fmtstr(s, 64, " %c ", c); 36797663Stjr out1str(s); 36897663Stjr col = strlen(s); 369163085Sstefanf if (mode == SHOWJOBS_VERBOSE) { 370100308Stjr fmtstr(s, 64, "%d ", (int)ps->pid); 37197669Stjr out1str(s); 37297669Stjr col += strlen(s); 37397669Stjr } 374216217Sjilles if (ps == jp->ps) { 375216217Sjilles out1str(statestr); 376216217Sjilles col += strlen(statestr); 37797663Stjr } 37897663Stjr do { 37997663Stjr out1c(' '); 38097663Stjr col++; 38197663Stjr } while (col < 30); 382216217Sjilles if (mode == SHOWJOBS_VERBOSE) { 383216217Sjilles out1str(ps->cmd); 384216217Sjilles out1c('\n'); 385216217Sjilles } else 386216217Sjilles printjobcmd(jp); 38797669Stjrskip: if (--procno <= 0) 38897663Stjr break; 38997663Stjr } 39097663Stjr} 39197663Stjr 3921556Srgrimes/* 3931556Srgrimes * Print a list of jobs. If "change" is nonzero, only print jobs whose 3941556Srgrimes * statuses have changed since the last call to showjobs. 3951556Srgrimes * 3961556Srgrimes * If the shell is interrupted in the process of creating a job, the 3971556Srgrimes * result may be a job structure containing zero processes. Such structures 3981556Srgrimes * will be freed here. 3991556Srgrimes */ 4001556Srgrimes 4011556Srgrimesvoid 402163085Sstefanfshowjobs(int change, int mode) 40317987Speter{ 4041556Srgrimes int jobno; 4051556Srgrimes struct job *jp; 4061556Srgrimes 4071556Srgrimes TRACE(("showjobs(%d) called\n", change)); 408208489Sjilles checkzombies(); 4091556Srgrimes for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) { 4101556Srgrimes if (! jp->used) 4111556Srgrimes continue; 4121556Srgrimes if (jp->nprocs == 0) { 4131556Srgrimes freejob(jp); 4141556Srgrimes continue; 4151556Srgrimes } 4161556Srgrimes if (change && ! jp->changed) 4171556Srgrimes continue; 418216217Sjilles showjob(jp, mode); 4191556Srgrimes jp->changed = 0; 420209600Sjilles /* Hack: discard jobs for which $! has not been referenced 421209600Sjilles * in interactive mode when they terminate. 422209600Sjilles */ 423209600Sjilles if (jp->state == JOBDONE && !jp->remembered && 424209600Sjilles (iflag || jp != bgjob)) { 4251556Srgrimes freejob(jp); 4261556Srgrimes } 4271556Srgrimes } 4281556Srgrimes} 4291556Srgrimes 4301556Srgrimes 4311556Srgrimes/* 4321556Srgrimes * Mark a job structure as unused. 4331556Srgrimes */ 4341556Srgrimes 435213811Sobrienstatic void 43690111Simpfreejob(struct job *jp) 43790111Simp{ 4381556Srgrimes struct procstat *ps; 4391556Srgrimes int i; 4401556Srgrimes 4411556Srgrimes INTOFF; 442209600Sjilles if (bgjob == jp) 443209600Sjilles bgjob = NULL; 4441556Srgrimes for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { 4451556Srgrimes if (ps->cmd != nullstr) 4461556Srgrimes ckfree(ps->cmd); 4471556Srgrimes } 4481556Srgrimes if (jp->ps != &jp->ps0) 4491556Srgrimes ckfree(jp->ps); 4501556Srgrimes jp->used = 0; 4511556Srgrimes#if JOBS 45297659Stjr deljob(jp); 4531556Srgrimes#endif 4541556Srgrimes INTON; 4551556Srgrimes} 4561556Srgrimes 4571556Srgrimes 4581556Srgrimes 4591556Srgrimesint 46090111Simpwaitcmd(int argc, char **argv) 46117987Speter{ 4621556Srgrimes struct job *job; 46326104Ssteve int status, retval; 4641556Srgrimes struct job *jp; 4651556Srgrimes 4661556Srgrimes if (argc > 1) { 4671556Srgrimes job = getjob(argv[1]); 4681556Srgrimes } else { 4691556Srgrimes job = NULL; 4701556Srgrimes } 47138536Scracauer 47238536Scracauer /* 47338536Scracauer * Loop until a process is terminated or stopped, or a SIGINT is 47438536Scracauer * received. 47538536Scracauer */ 47638536Scracauer 47738521Scracauer in_waitcmd++; 47838536Scracauer do { 4791556Srgrimes if (job != NULL) { 4801556Srgrimes if (job->state) { 4811556Srgrimes status = job->ps[job->nprocs - 1].status; 48226104Ssteve if (WIFEXITED(status)) 48326104Ssteve retval = WEXITSTATUS(status); 4841556Srgrimes#if JOBS 48526104Ssteve else if (WIFSTOPPED(status)) 48626104Ssteve retval = WSTOPSIG(status) + 128; 4871556Srgrimes#endif 4881556Srgrimes else 48926104Ssteve retval = WTERMSIG(status) + 128; 490209600Sjilles if (! iflag || ! job->changed) 4911556Srgrimes freejob(job); 492209600Sjilles else { 493209600Sjilles job->remembered = 0; 494209600Sjilles if (job == bgjob) 495209600Sjilles bgjob = NULL; 496209600Sjilles } 49738536Scracauer in_waitcmd--; 49826104Ssteve return retval; 4991556Srgrimes } 5001556Srgrimes } else { 501209600Sjilles for (jp = jobtab ; jp < jobtab + njobs; jp++) 502209600Sjilles if (jp->used && jp->state == JOBDONE) { 503209600Sjilles if (! iflag || ! jp->changed) 504209600Sjilles freejob(jp); 505209600Sjilles else { 506209600Sjilles jp->remembered = 0; 507209600Sjilles if (jp == bgjob) 508209600Sjilles bgjob = NULL; 509209600Sjilles } 510209600Sjilles } 5111556Srgrimes for (jp = jobtab ; ; jp++) { 5121556Srgrimes if (jp >= jobtab + njobs) { /* no running procs */ 51338536Scracauer in_waitcmd--; 5141556Srgrimes return 0; 5151556Srgrimes } 5161556Srgrimes if (jp->used && jp->state == 0) 5171556Srgrimes break; 5181556Srgrimes } 5191556Srgrimes } 52038521Scracauer } while (dowait(1, (struct job *)NULL) != -1); 52138521Scracauer in_waitcmd--; 52238521Scracauer 52338521Scracauer return 0; 5241556Srgrimes} 5251556Srgrimes 5261556Srgrimes 5271556Srgrimes 52817987Speterint 52990111Simpjobidcmd(int argc __unused, char **argv) 53017987Speter{ 5311556Srgrimes struct job *jp; 5321556Srgrimes int i; 5331556Srgrimes 5341556Srgrimes jp = getjob(argv[1]); 5351556Srgrimes for (i = 0 ; i < jp->nprocs ; ) { 536100308Stjr out1fmt("%d", (int)jp->ps[i].pid); 5371556Srgrimes out1c(++i < jp->nprocs? ' ' : '\n'); 5381556Srgrimes } 5391556Srgrimes return 0; 5401556Srgrimes} 5411556Srgrimes 5421556Srgrimes 5431556Srgrimes 5441556Srgrimes/* 5451556Srgrimes * Convert a job name to a job structure. 5461556Srgrimes */ 5471556Srgrimes 548213811Sobrienstatic struct job * 54990111Simpgetjob(char *name) 55090111Simp{ 5511556Srgrimes int jobno; 55297688Stjr struct job *found, *jp; 553100308Stjr pid_t pid; 5541556Srgrimes int i; 5551556Srgrimes 5561556Srgrimes if (name == NULL) { 5571556Srgrimes#if JOBS 55897659Stjrcurrentjob: if ((jp = getcurjob(NULL)) == NULL) 5591556Srgrimes error("No current job"); 56097659Stjr return (jp); 5611556Srgrimes#else 5621556Srgrimes error("No current job"); 5631556Srgrimes#endif 5641556Srgrimes } else if (name[0] == '%') { 5651556Srgrimes if (is_digit(name[1])) { 5661556Srgrimes jobno = number(name + 1); 5671556Srgrimes if (jobno > 0 && jobno <= njobs 5681556Srgrimes && jobtab[jobno - 1].used != 0) 5691556Srgrimes return &jobtab[jobno - 1]; 5701556Srgrimes#if JOBS 5711556Srgrimes } else if (name[1] == '%' && name[2] == '\0') { 5721556Srgrimes goto currentjob; 57397688Stjr } else if (name[1] == '+' && name[2] == '\0') { 57497688Stjr goto currentjob; 57597688Stjr } else if (name[1] == '-' && name[2] == '\0') { 57697688Stjr if ((jp = getcurjob(NULL)) == NULL || 57797688Stjr (jp = getcurjob(jp)) == NULL) 57897688Stjr error("No previous job"); 57997688Stjr return (jp); 5801556Srgrimes#endif 58197688Stjr } else if (name[1] == '?') { 58297688Stjr found = NULL; 58397688Stjr for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 58497688Stjr if (jp->used && jp->nprocs > 0 58597688Stjr && strstr(jp->ps[0].cmd, name + 2) != NULL) { 58697688Stjr if (found) 58797688Stjr error("%s: ambiguous", name); 58897688Stjr found = jp; 58997688Stjr } 59097688Stjr } 59197688Stjr if (found != NULL) 59297688Stjr return (found); 5931556Srgrimes } else { 59497688Stjr found = NULL; 5951556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 5961556Srgrimes if (jp->used && jp->nprocs > 0 5971556Srgrimes && prefix(name + 1, jp->ps[0].cmd)) { 5981556Srgrimes if (found) 5991556Srgrimes error("%s: ambiguous", name); 6001556Srgrimes found = jp; 6011556Srgrimes } 6021556Srgrimes } 6031556Srgrimes if (found) 6041556Srgrimes return found; 6051556Srgrimes } 6061556Srgrimes } else if (is_number(name)) { 607100308Stjr pid = (pid_t)number(name); 6081556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 6091556Srgrimes if (jp->used && jp->nprocs > 0 6101556Srgrimes && jp->ps[jp->nprocs - 1].pid == pid) 6111556Srgrimes return jp; 6121556Srgrimes } 6131556Srgrimes } 6141556Srgrimes error("No such job: %s", name); 61517987Speter /*NOTREACHED*/ 61617987Speter return NULL; 6171556Srgrimes} 6181556Srgrimes 6191556Srgrimes 620216629Sjillespid_t 621216629Sjillesgetjobpgrp(char *name) 622216629Sjilles{ 623216629Sjilles struct job *jp; 6241556Srgrimes 625216629Sjilles jp = getjob(name); 626216629Sjilles return -jp->ps[0].pid; 627216629Sjilles} 628216629Sjilles 6291556Srgrimes/* 6301556Srgrimes * Return a new job structure, 6311556Srgrimes */ 6321556Srgrimes 6331556Srgrimesstruct job * 63490111Simpmakejob(union node *node __unused, int nprocs) 63517987Speter{ 6361556Srgrimes int i; 6371556Srgrimes struct job *jp; 6381556Srgrimes 6391556Srgrimes for (i = njobs, jp = jobtab ; ; jp++) { 6401556Srgrimes if (--i < 0) { 6411556Srgrimes INTOFF; 6421556Srgrimes if (njobs == 0) { 6431556Srgrimes jobtab = ckmalloc(4 * sizeof jobtab[0]); 64497664Stjr#if JOBS 64597659Stjr jobmru = NULL; 64697664Stjr#endif 6471556Srgrimes } else { 6481556Srgrimes jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 64917987Speter memcpy(jp, jobtab, njobs * sizeof jp[0]); 65097659Stjr#if JOBS 65197659Stjr /* Relocate `next' pointers and list head */ 65299760Stjr if (jobmru != NULL) 65399760Stjr jobmru = &jp[jobmru - jobtab]; 65497659Stjr for (i = 0; i < njobs; i++) 65597659Stjr if (jp[i].next != NULL) 65697659Stjr jp[i].next = &jp[jp[i].next - 65797659Stjr jobtab]; 65897659Stjr#endif 659209600Sjilles if (bgjob != NULL) 660209600Sjilles bgjob = &jp[bgjob - jobtab]; 66120425Ssteve /* Relocate `ps' pointers */ 66220425Ssteve for (i = 0; i < njobs; i++) 66320425Ssteve if (jp[i].ps == &jobtab[i].ps0) 66420425Ssteve jp[i].ps = &jp[i].ps0; 6651556Srgrimes ckfree(jobtab); 6661556Srgrimes jobtab = jp; 6671556Srgrimes } 6681556Srgrimes jp = jobtab + njobs; 6691556Srgrimes for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 6701556Srgrimes INTON; 6711556Srgrimes break; 6721556Srgrimes } 6731556Srgrimes if (jp->used == 0) 6741556Srgrimes break; 6751556Srgrimes } 6761556Srgrimes INTOFF; 6771556Srgrimes jp->state = 0; 6781556Srgrimes jp->used = 1; 6791556Srgrimes jp->changed = 0; 6801556Srgrimes jp->nprocs = 0; 681100305Stjr jp->foreground = 0; 682209600Sjilles jp->remembered = 0; 6831556Srgrimes#if JOBS 6841556Srgrimes jp->jobctl = jobctl; 68597659Stjr jp->next = NULL; 6861556Srgrimes#endif 6871556Srgrimes if (nprocs > 1) { 6881556Srgrimes jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 6891556Srgrimes } else { 6901556Srgrimes jp->ps = &jp->ps0; 6911556Srgrimes } 6921556Srgrimes INTON; 693213775Sjhb TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs, 69417987Speter jp - jobtab + 1)); 6951556Srgrimes return jp; 6968855Srgrimes} 6971556Srgrimes 69897659Stjr#if JOBS 699213811Sobrienstatic void 70097659Stjrsetcurjob(struct job *cj) 70197659Stjr{ 70297659Stjr struct job *jp, *prev; 7031556Srgrimes 70497659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 70597659Stjr if (jp == cj) { 70697659Stjr if (prev != NULL) 70797659Stjr prev->next = jp->next; 70897659Stjr else 70997659Stjr jobmru = jp->next; 71097659Stjr jp->next = jobmru; 71197659Stjr jobmru = cj; 71297659Stjr return; 71397659Stjr } 71497659Stjr } 71597659Stjr cj->next = jobmru; 71697659Stjr jobmru = cj; 71797659Stjr} 71897659Stjr 719213811Sobrienstatic void 72097659Stjrdeljob(struct job *j) 72197659Stjr{ 72297659Stjr struct job *jp, *prev; 72397659Stjr 72497659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 72597659Stjr if (jp == j) { 72697659Stjr if (prev != NULL) 72797659Stjr prev->next = jp->next; 72897659Stjr else 72997659Stjr jobmru = jp->next; 73097659Stjr return; 73197659Stjr } 73297659Stjr } 73397659Stjr} 73497659Stjr 7351556Srgrimes/* 73697659Stjr * Return the most recently used job that isn't `nj', and preferably one 73797659Stjr * that is stopped. 73897659Stjr */ 739213811Sobrienstatic struct job * 74097659Stjrgetcurjob(struct job *nj) 74197659Stjr{ 74297659Stjr struct job *jp; 74397659Stjr 74497659Stjr /* Try to find a stopped one.. */ 74597659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 74697659Stjr if (jp->used && jp != nj && jp->state == JOBSTOPPED) 74797659Stjr return (jp); 74897659Stjr /* Otherwise the most recently used job that isn't `nj' */ 74997659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 75097659Stjr if (jp->used && jp != nj) 75197659Stjr return (jp); 75297659Stjr 75397659Stjr return (NULL); 75497659Stjr} 75597659Stjr 75697659Stjr#endif 75797659Stjr 75897659Stjr/* 7591556Srgrimes * Fork of a subshell. If we are doing job control, give the subshell its 7601556Srgrimes * own process group. Jp is a job structure that the job is to be added to. 7611556Srgrimes * N is the command that will be evaluated by the child. Both jp and n may 7621556Srgrimes * be NULL. The mode parameter can be one of the following: 7631556Srgrimes * FORK_FG - Fork off a foreground process. 7641556Srgrimes * FORK_BG - Fork off a background process. 7651556Srgrimes * FORK_NOJOB - Like FORK_FG, but don't give the process its own 7661556Srgrimes * process group even if job control is on. 7671556Srgrimes * 7681556Srgrimes * When job control is turned off, background processes have their standard 7691556Srgrimes * input redirected to /dev/null (except for the second and later processes 7701556Srgrimes * in a pipeline). 7711556Srgrimes */ 7721556Srgrimes 773100308Stjrpid_t 77490111Simpforkshell(struct job *jp, union node *n, int mode) 77517987Speter{ 776100308Stjr pid_t pid; 777100308Stjr pid_t pgrp; 7781556Srgrimes 779213775Sjhb TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n, 78017987Speter mode)); 7811556Srgrimes INTOFF; 782216208Sjilles if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0)) 783208489Sjilles checkzombies(); 784112341Stjr flushall(); 7851556Srgrimes pid = fork(); 7861556Srgrimes if (pid == -1) { 7871556Srgrimes TRACE(("Fork failed, errno=%d\n", errno)); 7881556Srgrimes INTON; 78953891Scracauer error("Cannot fork: %s", strerror(errno)); 7901556Srgrimes } 7911556Srgrimes if (pid == 0) { 7921556Srgrimes struct job *p; 7931556Srgrimes int wasroot; 7941556Srgrimes int i; 7951556Srgrimes 796100308Stjr TRACE(("Child shell %d\n", (int)getpid())); 7971556Srgrimes wasroot = rootshell; 7981556Srgrimes rootshell = 0; 799200998Sjilles handler = &main_handler; 8001556Srgrimes closescript(); 8011556Srgrimes INTON; 802223024Sjilles forcelocal = 0; 8031556Srgrimes clear_traps(); 8041556Srgrimes#if JOBS 8051556Srgrimes jobctl = 0; /* do job control only in root shell */ 8061556Srgrimes if (wasroot && mode != FORK_NOJOB && mflag) { 8071556Srgrimes if (jp == NULL || jp->nprocs == 0) 8081556Srgrimes pgrp = getpid(); 8091556Srgrimes else 8101556Srgrimes pgrp = jp->ps[0].pid; 81121352Ssteve if (setpgid(0, pgrp) == 0 && mode == FORK_FG) { 8121556Srgrimes /*** this causes superfluous TIOCSPGRPS ***/ 81399762Stjr if (tcsetpgrp(ttyfd, pgrp) < 0) 81420425Ssteve error("tcsetpgrp failed, errno=%d", errno); 8151556Srgrimes } 8161556Srgrimes setsignal(SIGTSTP); 8171556Srgrimes setsignal(SIGTTOU); 8181556Srgrimes } else if (mode == FORK_BG) { 8191556Srgrimes ignoresig(SIGINT); 8201556Srgrimes ignoresig(SIGQUIT); 8211556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 8221556Srgrimes ! fd0_redirected_p ()) { 8231556Srgrimes close(0); 82469793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 825222684Sjilles error("cannot open %s: %s", 82669793Sobrien _PATH_DEVNULL, strerror(errno)); 8271556Srgrimes } 8281556Srgrimes } 8291556Srgrimes#else 8301556Srgrimes if (mode == FORK_BG) { 8311556Srgrimes ignoresig(SIGINT); 8321556Srgrimes ignoresig(SIGQUIT); 8331556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 8341556Srgrimes ! fd0_redirected_p ()) { 8351556Srgrimes close(0); 83669793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 837222684Sjilles error("cannot open %s: %s", 83869793Sobrien _PATH_DEVNULL, strerror(errno)); 8391556Srgrimes } 8401556Srgrimes } 8411556Srgrimes#endif 842102051Stjr INTOFF; 843102051Stjr for (i = njobs, p = jobtab ; --i >= 0 ; p++) 844102051Stjr if (p->used) 845102051Stjr freejob(p); 846102051Stjr INTON; 8471556Srgrimes if (wasroot && iflag) { 8481556Srgrimes setsignal(SIGINT); 8491556Srgrimes setsignal(SIGQUIT); 8501556Srgrimes setsignal(SIGTERM); 8511556Srgrimes } 8521556Srgrimes return pid; 8531556Srgrimes } 8541556Srgrimes if (rootshell && mode != FORK_NOJOB && mflag) { 8551556Srgrimes if (jp == NULL || jp->nprocs == 0) 8561556Srgrimes pgrp = pid; 8571556Srgrimes else 8581556Srgrimes pgrp = jp->ps[0].pid; 85917987Speter setpgid(pid, pgrp); 8601556Srgrimes } 861209600Sjilles if (mode == FORK_BG) { 862209600Sjilles if (bgjob != NULL && bgjob->state == JOBDONE && 863209600Sjilles !bgjob->remembered && !iflag) 864209600Sjilles freejob(bgjob); 8651556Srgrimes backgndpid = pid; /* set $! */ 866209600Sjilles bgjob = jp; 867209600Sjilles } 8681556Srgrimes if (jp) { 8691556Srgrimes struct procstat *ps = &jp->ps[jp->nprocs++]; 8701556Srgrimes ps->pid = pid; 8711556Srgrimes ps->status = -1; 8721556Srgrimes ps->cmd = nullstr; 8731556Srgrimes if (iflag && rootshell && n) 8741556Srgrimes ps->cmd = commandtext(n); 875100305Stjr jp->foreground = mode == FORK_FG; 87697659Stjr#if JOBS 87797659Stjr setcurjob(jp); 87897659Stjr#endif 8791556Srgrimes } 8801556Srgrimes INTON; 881100308Stjr TRACE(("In parent shell: child = %d\n", (int)pid)); 8821556Srgrimes return pid; 8831556Srgrimes} 8841556Srgrimes 8851556Srgrimes 8861556Srgrimes 8871556Srgrimes/* 8881556Srgrimes * Wait for job to finish. 8891556Srgrimes * 8901556Srgrimes * Under job control we have the problem that while a child process is 8911556Srgrimes * running interrupts generated by the user are sent to the child but not 8921556Srgrimes * to the shell. This means that an infinite loop started by an inter- 8931556Srgrimes * active user may be hard to kill. With job control turned off, an 8941556Srgrimes * interactive user may place an interactive program inside a loop. If 8951556Srgrimes * the interactive program catches interrupts, the user doesn't want 8961556Srgrimes * these interrupts to also abort the loop. The approach we take here 8971556Srgrimes * is to have the shell ignore interrupt signals while waiting for a 89846684Skris * foreground process to terminate, and then send itself an interrupt 8991556Srgrimes * signal if the child process was terminated by an interrupt signal. 9001556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then 9011556Srgrimes * exit on interrupt; unless these processes terminate themselves by 9021556Srgrimes * sending a signal to themselves (instead of calling exit) they will 9031556Srgrimes * confuse this approach. 9041556Srgrimes */ 9051556Srgrimes 9061556Srgrimesint 90790111Simpwaitforjob(struct job *jp, int *origstatus) 90845916Scracauer{ 9091556Srgrimes#if JOBS 910100308Stjr pid_t mypgrp = getpgrp(); 911208881Sjilles int propagate_int = jp->jobctl && jp->foreground; 9121556Srgrimes#endif 9131556Srgrimes int status; 9141556Srgrimes int st; 9151556Srgrimes 9161556Srgrimes INTOFF; 917213775Sjhb TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1)); 91838950Scracauer while (jp->state == 0) 91938950Scracauer if (dowait(1, jp) == -1) 92038950Scracauer dotrap(); 9211556Srgrimes#if JOBS 9221556Srgrimes if (jp->jobctl) { 92399762Stjr if (tcsetpgrp(ttyfd, mypgrp) < 0) 92420425Ssteve error("tcsetpgrp failed, errno=%d\n", errno); 9251556Srgrimes } 9261556Srgrimes if (jp->state == JOBSTOPPED) 92797659Stjr setcurjob(jp); 9281556Srgrimes#endif 9291556Srgrimes status = jp->ps[jp->nprocs - 1].status; 93045916Scracauer if (origstatus != NULL) 93145916Scracauer *origstatus = status; 9321556Srgrimes /* convert to 8 bits */ 93326104Ssteve if (WIFEXITED(status)) 93426104Ssteve st = WEXITSTATUS(status); 9351556Srgrimes#if JOBS 93626104Ssteve else if (WIFSTOPPED(status)) 93726104Ssteve st = WSTOPSIG(status) + 128; 9381556Srgrimes#endif 9391556Srgrimes else 94026104Ssteve st = WTERMSIG(status) + 128; 9411556Srgrimes if (! JOBS || jp->state == JOBDONE) 9421556Srgrimes freejob(jp); 94338521Scracauer if (int_pending()) { 944216400Sjilles if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT) 94538521Scracauer CLEAR_PENDING_INT; 94638521Scracauer } 947208881Sjilles#if JOBS 948208881Sjilles else if (rootshell && iflag && propagate_int && 949208881Sjilles WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 950208881Sjilles kill(getpid(), SIGINT); 951208881Sjilles#endif 9521556Srgrimes INTON; 9531556Srgrimes return st; 9541556Srgrimes} 9551556Srgrimes 9561556Srgrimes 9571556Srgrimes 9581556Srgrimes/* 9591556Srgrimes * Wait for a process to terminate. 9601556Srgrimes */ 9611556Srgrimes 962213811Sobrienstatic pid_t 96390111Simpdowait(int block, struct job *job) 96417987Speter{ 965100308Stjr pid_t pid; 9661556Srgrimes int status; 9671556Srgrimes struct procstat *sp; 9681556Srgrimes struct job *jp; 9691556Srgrimes struct job *thisjob; 9701556Srgrimes int done; 9711556Srgrimes int stopped; 97226104Ssteve int sig; 973216217Sjilles int coredump; 9741556Srgrimes 97538950Scracauer in_dowait++; 9761556Srgrimes TRACE(("dowait(%d) called\n", block)); 9771556Srgrimes do { 9781556Srgrimes pid = waitproc(block, &status); 979100308Stjr TRACE(("wait returns %d, status=%d\n", (int)pid, status)); 98072086Scracauer } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) || 981125501Scracauer (pid > 0 && WIFSTOPPED(status) && !iflag)); 98238950Scracauer in_dowait--; 983153417Smaxim if (pid == -1 && errno == ECHILD && job != NULL) 984153417Smaxim job->state = JOBDONE; 98538521Scracauer if (breakwaitcmd != 0) { 98638521Scracauer breakwaitcmd = 0; 987138312Smaxim if (pid <= 0) 988138312Smaxim return -1; 98938521Scracauer } 9901556Srgrimes if (pid <= 0) 9911556Srgrimes return pid; 9921556Srgrimes INTOFF; 9931556Srgrimes thisjob = NULL; 9941556Srgrimes for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 995216208Sjilles if (jp->used && jp->nprocs > 0) { 9961556Srgrimes done = 1; 9971556Srgrimes stopped = 1; 9981556Srgrimes for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 9991556Srgrimes if (sp->pid == -1) 10001556Srgrimes continue; 10011556Srgrimes if (sp->pid == pid) { 100226104Ssteve TRACE(("Changing status of proc %d from 0x%x to 0x%x\n", 1003100308Stjr (int)pid, sp->status, 1004100308Stjr status)); 10051556Srgrimes sp->status = status; 10061556Srgrimes thisjob = jp; 10071556Srgrimes } 10081556Srgrimes if (sp->status == -1) 10091556Srgrimes stopped = 0; 101026104Ssteve else if (WIFSTOPPED(sp->status)) 10111556Srgrimes done = 0; 10121556Srgrimes } 10131556Srgrimes if (stopped) { /* stopped or done */ 10141556Srgrimes int state = done? JOBDONE : JOBSTOPPED; 10151556Srgrimes if (jp->state != state) { 1016213775Sjhb TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 10171556Srgrimes jp->state = state; 1018209600Sjilles if (jp != job) { 1019209600Sjilles if (done && !jp->remembered && 1020209600Sjilles !iflag && jp != bgjob) 1021209600Sjilles freejob(jp); 10221556Srgrimes#if JOBS 1023209600Sjilles else if (done) 1024209600Sjilles deljob(jp); 10251556Srgrimes#endif 1026209600Sjilles } 10271556Srgrimes } 10281556Srgrimes } 10291556Srgrimes } 10301556Srgrimes } 10311556Srgrimes INTON; 1032216217Sjilles if (!thisjob || thisjob->state == 0) 1033216217Sjilles ; 1034216217Sjilles else if ((!rootshell || !iflag || thisjob == job) && 1035216217Sjilles thisjob->foreground && thisjob->state != JOBSTOPPED) { 1036216217Sjilles sig = 0; 1037216217Sjilles coredump = 0; 1038216217Sjilles for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++) 1039216217Sjilles if (WIFSIGNALED(sp->status)) { 1040216217Sjilles sig = WTERMSIG(sp->status); 1041216217Sjilles coredump = WCOREDUMP(sp->status); 1042216217Sjilles } 1043216217Sjilles if (sig > 0 && sig != SIGINT && sig != SIGPIPE) { 1044216217Sjilles if (sig < sys_nsig && sys_siglist[sig]) 1045218105Sjilles out2str(sys_siglist[sig]); 104626104Ssteve else 1047218105Sjilles outfmt(out2, "Signal %d", sig); 1048216217Sjilles if (coredump) 1049218105Sjilles out2str(" (core dumped)"); 1050218105Sjilles out2c('\n'); 1051218105Sjilles flushout(out2); 10521556Srgrimes } 10531556Srgrimes } else { 1054109627Stjr TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job)); 1055216217Sjilles thisjob->changed = 1; 10561556Srgrimes } 10571556Srgrimes return pid; 10581556Srgrimes} 10591556Srgrimes 10601556Srgrimes 10611556Srgrimes 10621556Srgrimes/* 10631556Srgrimes * Do a wait system call. If job control is compiled in, we accept 10641556Srgrimes * stopped processes. If block is zero, we return a value of zero 10651556Srgrimes * rather than blocking. 10661556Srgrimes */ 1067213811Sobrienstatic pid_t 106890111Simpwaitproc(int block, int *status) 106917987Speter{ 10701556Srgrimes int flags; 10711556Srgrimes 10721556Srgrimes#if JOBS 10731556Srgrimes flags = WUNTRACED; 10741556Srgrimes#else 10751556Srgrimes flags = 0; 10761556Srgrimes#endif 10771556Srgrimes if (block == 0) 10781556Srgrimes flags |= WNOHANG; 10791556Srgrimes return wait3(status, flags, (struct rusage *)NULL); 10801556Srgrimes} 10811556Srgrimes 10821556Srgrimes/* 10831556Srgrimes * return 1 if there are stopped jobs, otherwise 0 10841556Srgrimes */ 10851556Srgrimesint job_warning = 0; 10861556Srgrimesint 108790111Simpstoppedjobs(void) 10881556Srgrimes{ 108925222Ssteve int jobno; 109025222Ssteve struct job *jp; 10911556Srgrimes 10921556Srgrimes if (job_warning) 10931556Srgrimes return (0); 10941556Srgrimes for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 10951556Srgrimes if (jp->used == 0) 10961556Srgrimes continue; 10971556Srgrimes if (jp->state == JOBSTOPPED) { 1098199629Sjilles out2fmt_flush("You have stopped jobs.\n"); 10991556Srgrimes job_warning = 2; 11001556Srgrimes return (1); 11011556Srgrimes } 11021556Srgrimes } 11031556Srgrimes 11041556Srgrimes return (0); 11051556Srgrimes} 11061556Srgrimes 1107208489Sjilles 1108213811Sobrienstatic void 1109208489Sjillescheckzombies(void) 1110208489Sjilles{ 1111208489Sjilles while (njobs > 0 && dowait(0, NULL) > 0) 1112208489Sjilles ; 1113208489Sjilles} 1114208489Sjilles 1115208489Sjilles 1116209600Sjillesint 1117209600Sjillesbackgndpidset(void) 1118209600Sjilles{ 1119209600Sjilles return backgndpid != -1; 1120209600Sjilles} 1121209600Sjilles 1122209600Sjilles 1123209600Sjillespid_t 1124209600Sjillesbackgndpidval(void) 1125209600Sjilles{ 1126223024Sjilles if (bgjob != NULL && !forcelocal) 1127209600Sjilles bgjob->remembered = 1; 1128209600Sjilles return backgndpid; 1129209600Sjilles} 1130209600Sjilles 11311556Srgrimes/* 11321556Srgrimes * Return a string identifying a command (to be printed by the 11331556Srgrimes * jobs command. 11341556Srgrimes */ 11351556Srgrimes 1136213760Sobrienstatic char *cmdnextc; 1137213760Sobrienstatic int cmdnleft; 11381556Srgrimes#define MAXCMDTEXT 200 11391556Srgrimes 11401556Srgrimeschar * 114190111Simpcommandtext(union node *n) 114290111Simp{ 11431556Srgrimes char *name; 11441556Srgrimes 11451556Srgrimes cmdnextc = name = ckmalloc(MAXCMDTEXT); 11461556Srgrimes cmdnleft = MAXCMDTEXT - 4; 11471556Srgrimes cmdtxt(n); 11481556Srgrimes *cmdnextc = '\0'; 11491556Srgrimes return name; 11501556Srgrimes} 11511556Srgrimes 11521556Srgrimes 1153213811Sobrienstatic void 115490111Simpcmdtxt(union node *n) 115590111Simp{ 11561556Srgrimes union node *np; 11571556Srgrimes struct nodelist *lp; 1158201053Sjilles const char *p; 11591556Srgrimes int i; 11601556Srgrimes char s[2]; 11611556Srgrimes 11621556Srgrimes if (n == NULL) 11631556Srgrimes return; 11641556Srgrimes switch (n->type) { 11651556Srgrimes case NSEMI: 11661556Srgrimes cmdtxt(n->nbinary.ch1); 11671556Srgrimes cmdputs("; "); 11681556Srgrimes cmdtxt(n->nbinary.ch2); 11691556Srgrimes break; 11701556Srgrimes case NAND: 11711556Srgrimes cmdtxt(n->nbinary.ch1); 11721556Srgrimes cmdputs(" && "); 11731556Srgrimes cmdtxt(n->nbinary.ch2); 11741556Srgrimes break; 11751556Srgrimes case NOR: 11761556Srgrimes cmdtxt(n->nbinary.ch1); 11771556Srgrimes cmdputs(" || "); 11781556Srgrimes cmdtxt(n->nbinary.ch2); 11791556Srgrimes break; 11801556Srgrimes case NPIPE: 11811556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 11821556Srgrimes cmdtxt(lp->n); 11831556Srgrimes if (lp->next) 11841556Srgrimes cmdputs(" | "); 11851556Srgrimes } 11861556Srgrimes break; 11871556Srgrimes case NSUBSHELL: 11881556Srgrimes cmdputs("("); 11891556Srgrimes cmdtxt(n->nredir.n); 11901556Srgrimes cmdputs(")"); 11911556Srgrimes break; 11921556Srgrimes case NREDIR: 11931556Srgrimes case NBACKGND: 11941556Srgrimes cmdtxt(n->nredir.n); 11951556Srgrimes break; 11961556Srgrimes case NIF: 11971556Srgrimes cmdputs("if "); 11981556Srgrimes cmdtxt(n->nif.test); 11991556Srgrimes cmdputs("; then "); 12001556Srgrimes cmdtxt(n->nif.ifpart); 12011556Srgrimes cmdputs("..."); 12021556Srgrimes break; 12031556Srgrimes case NWHILE: 12041556Srgrimes cmdputs("while "); 12051556Srgrimes goto until; 12061556Srgrimes case NUNTIL: 12071556Srgrimes cmdputs("until "); 12081556Srgrimesuntil: 12091556Srgrimes cmdtxt(n->nbinary.ch1); 12101556Srgrimes cmdputs("; do "); 12111556Srgrimes cmdtxt(n->nbinary.ch2); 12121556Srgrimes cmdputs("; done"); 12131556Srgrimes break; 12141556Srgrimes case NFOR: 12151556Srgrimes cmdputs("for "); 12161556Srgrimes cmdputs(n->nfor.var); 12171556Srgrimes cmdputs(" in ..."); 12181556Srgrimes break; 12191556Srgrimes case NCASE: 12201556Srgrimes cmdputs("case "); 12211556Srgrimes cmdputs(n->ncase.expr->narg.text); 12221556Srgrimes cmdputs(" in ..."); 12231556Srgrimes break; 12241556Srgrimes case NDEFUN: 12251556Srgrimes cmdputs(n->narg.text); 12261556Srgrimes cmdputs("() ..."); 12271556Srgrimes break; 12281556Srgrimes case NCMD: 12291556Srgrimes for (np = n->ncmd.args ; np ; np = np->narg.next) { 12301556Srgrimes cmdtxt(np); 12311556Srgrimes if (np->narg.next) 12321556Srgrimes cmdputs(" "); 12331556Srgrimes } 12341556Srgrimes for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 12351556Srgrimes cmdputs(" "); 12361556Srgrimes cmdtxt(np); 12371556Srgrimes } 12381556Srgrimes break; 12391556Srgrimes case NARG: 12401556Srgrimes cmdputs(n->narg.text); 12411556Srgrimes break; 12421556Srgrimes case NTO: 12431556Srgrimes p = ">"; i = 1; goto redir; 12441556Srgrimes case NAPPEND: 12451556Srgrimes p = ">>"; i = 1; goto redir; 12461556Srgrimes case NTOFD: 12471556Srgrimes p = ">&"; i = 1; goto redir; 124896922Stjr case NCLOBBER: 124996922Stjr p = ">|"; i = 1; goto redir; 12501556Srgrimes case NFROM: 12511556Srgrimes p = "<"; i = 0; goto redir; 125266612Sbrian case NFROMTO: 125366612Sbrian p = "<>"; i = 0; goto redir; 12541556Srgrimes case NFROMFD: 12551556Srgrimes p = "<&"; i = 0; goto redir; 12561556Srgrimesredir: 12571556Srgrimes if (n->nfile.fd != i) { 12581556Srgrimes s[0] = n->nfile.fd + '0'; 12591556Srgrimes s[1] = '\0'; 12601556Srgrimes cmdputs(s); 12611556Srgrimes } 12621556Srgrimes cmdputs(p); 12631556Srgrimes if (n->type == NTOFD || n->type == NFROMFD) { 126499634Stjr if (n->ndup.dupfd >= 0) 126599634Stjr s[0] = n->ndup.dupfd + '0'; 126699634Stjr else 126799634Stjr s[0] = '-'; 12681556Srgrimes s[1] = '\0'; 12691556Srgrimes cmdputs(s); 12701556Srgrimes } else { 12711556Srgrimes cmdtxt(n->nfile.fname); 12721556Srgrimes } 12731556Srgrimes break; 12741556Srgrimes case NHERE: 12751556Srgrimes case NXHERE: 12761556Srgrimes cmdputs("<<..."); 12771556Srgrimes break; 12781556Srgrimes default: 12791556Srgrimes cmdputs("???"); 12801556Srgrimes break; 12811556Srgrimes } 12821556Srgrimes} 12831556Srgrimes 12841556Srgrimes 12851556Srgrimes 1286213811Sobrienstatic void 1287201053Sjillescmdputs(const char *s) 128890111Simp{ 1289201053Sjilles const char *p; 1290201053Sjilles char *q; 129125222Ssteve char c; 12921556Srgrimes int subtype = 0; 12931556Srgrimes 12941556Srgrimes if (cmdnleft <= 0) 12951556Srgrimes return; 12961556Srgrimes p = s; 12971556Srgrimes q = cmdnextc; 12981556Srgrimes while ((c = *p++) != '\0') { 12991556Srgrimes if (c == CTLESC) 13001556Srgrimes *q++ = *p++; 13011556Srgrimes else if (c == CTLVAR) { 13021556Srgrimes *q++ = '$'; 13031556Srgrimes if (--cmdnleft > 0) 13041556Srgrimes *q++ = '{'; 13051556Srgrimes subtype = *p++; 1306216246Sjilles if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0) 1307216246Sjilles *q++ = '#'; 13081556Srgrimes } else if (c == '=' && subtype != 0) { 1309216246Sjilles *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL]; 1310216246Sjilles if (*q) 1311216246Sjilles q++; 1312216246Sjilles else 1313216246Sjilles cmdnleft++; 1314216246Sjilles if (((subtype & VSTYPE) == VSTRIMLEFTMAX || 1315216246Sjilles (subtype & VSTYPE) == VSTRIMRIGHTMAX) && 1316216246Sjilles --cmdnleft > 0) 1317216246Sjilles *q = q[-1], q++; 13181556Srgrimes subtype = 0; 13191556Srgrimes } else if (c == CTLENDVAR) { 13201556Srgrimes *q++ = '}'; 1321216246Sjilles } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) { 1322216246Sjilles cmdnleft -= 5; 1323216246Sjilles if (cmdnleft > 0) { 1324216246Sjilles *q++ = '$'; 1325216246Sjilles *q++ = '('; 1326216246Sjilles *q++ = '.'; 1327216246Sjilles *q++ = '.'; 1328216246Sjilles *q++ = '.'; 1329216246Sjilles *q++ = ')'; 1330216246Sjilles } 1331216246Sjilles } else if (c == CTLARI) { 1332216246Sjilles cmdnleft -= 2; 1333216246Sjilles if (cmdnleft > 0) { 1334216246Sjilles *q++ = '$'; 1335216246Sjilles *q++ = '('; 1336216246Sjilles *q++ = '('; 1337216246Sjilles } 1338216246Sjilles p++; 1339216246Sjilles } else if (c == CTLENDARI) { 1340216246Sjilles if (--cmdnleft > 0) { 1341216246Sjilles *q++ = ')'; 1342216246Sjilles *q++ = ')'; 1343216246Sjilles } 1344216246Sjilles } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND) 1345216246Sjilles cmdnleft++; /* ignore */ 13461556Srgrimes else 13471556Srgrimes *q++ = c; 13481556Srgrimes if (--cmdnleft <= 0) { 13491556Srgrimes *q++ = '.'; 13501556Srgrimes *q++ = '.'; 13511556Srgrimes *q++ = '.'; 13521556Srgrimes break; 13531556Srgrimes } 13541556Srgrimes } 13551556Srgrimes cmdnextc = q; 13561556Srgrimes} 1357