jobs.c revision 216246
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 216246 2010-12-06 23:49:27Z 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" 731556Srgrimes 741556Srgrimes 75213760Sobrienstatic struct job *jobtab; /* array of jobs */ 76213760Sobrienstatic int njobs; /* size of array */ 7728346SsteveMKINIT pid_t backgndpid = -1; /* pid of last background process */ 78209600SjillesMKINIT struct job *bgjob = NULL; /* last background process */ 791556Srgrimes#if JOBS 80213760Sobrienstatic struct job *jobmru; /* most recently used job list */ 81213760Sobrienstatic pid_t initialpgrp; /* pgrp of shell on invocation */ 821556Srgrimes#endif 8338536Scracauerint in_waitcmd = 0; /* are we in waitcmd()? */ 8438950Scracauerint in_dowait = 0; /* are we in dowait()? */ 8538536Scracauervolatile sig_atomic_t breakwaitcmd = 0; /* should wait be terminated? */ 8699762Stjrstatic int ttyfd = -1; 871556Srgrimes 8820425Ssteve#if JOBS 89213811Sobrienstatic void restartjob(struct job *); 9020425Ssteve#endif 91213811Sobrienstatic void freejob(struct job *); 92213811Sobrienstatic struct job *getjob(char *); 93213811Sobrienstatic pid_t dowait(int, struct job *); 94213811Sobrienstatic pid_t waitproc(int, int *); 95213811Sobrienstatic void checkzombies(void); 96213811Sobrienstatic void cmdtxt(union node *); 97213811Sobrienstatic void cmdputs(const char *); 9897659Stjr#if JOBS 99213811Sobrienstatic void setcurjob(struct job *); 100213811Sobrienstatic void deljob(struct job *); 101213811Sobrienstatic struct job *getcurjob(struct job *); 10297659Stjr#endif 103216217Sjillesstatic void printjobcmd(struct job *); 104216217Sjillesstatic void showjob(struct job *, int); 1051556Srgrimes 1061556Srgrimes 1071556Srgrimes/* 1081556Srgrimes * Turn job control on and off. 1091556Srgrimes */ 1101556Srgrimes 1111556SrgrimesMKINIT int jobctl; 1121556Srgrimes 11320425Ssteve#if JOBS 1141556Srgrimesvoid 11590111Simpsetjobctl(int on) 11617987Speter{ 11799762Stjr int i; 1181556Srgrimes 1191556Srgrimes if (on == jobctl || rootshell == 0) 1201556Srgrimes return; 1211556Srgrimes if (on) { 12299762Stjr if (ttyfd != -1) 12399762Stjr close(ttyfd); 12499762Stjr if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) { 12599762Stjr i = 0; 12699762Stjr while (i <= 2 && !isatty(i)) 12799762Stjr i++; 128109927Stjr if (i > 2 || (ttyfd = fcntl(i, F_DUPFD, 10)) < 0) 12999762Stjr goto out; 13099762Stjr } 131109927Stjr if (ttyfd < 10) { 132109927Stjr /* 133109927Stjr * Keep our TTY file descriptor out of the way of 134109927Stjr * the user's redirections. 135109927Stjr */ 136109927Stjr if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) { 137109927Stjr close(ttyfd); 138109927Stjr ttyfd = -1; 139109927Stjr goto out; 140109927Stjr } 141109927Stjr close(ttyfd); 142109927Stjr ttyfd = i; 143109927Stjr } 144103223Snectar if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) { 14599762Stjr close(ttyfd); 14699762Stjr ttyfd = -1; 14799762Stjr goto out; 14899762Stjr } 1491556Srgrimes do { /* while we are in the background */ 15099762Stjr initialpgrp = tcgetpgrp(ttyfd); 15120425Ssteve if (initialpgrp < 0) { 152199629Sjillesout: out2fmt_flush("sh: can't access tty; job control turned off\n"); 1531556Srgrimes mflag = 0; 1541556Srgrimes return; 1551556Srgrimes } 1561556Srgrimes if (initialpgrp == -1) 15717987Speter initialpgrp = getpgrp(); 15817987Speter else if (initialpgrp != getpgrp()) { 15999762Stjr killpg(0, SIGTTIN); 1601556Srgrimes continue; 1611556Srgrimes } 1621556Srgrimes } while (0); 1631556Srgrimes setsignal(SIGTSTP); 1641556Srgrimes setsignal(SIGTTOU); 1651556Srgrimes setsignal(SIGTTIN); 16617987Speter setpgid(0, rootpid); 16799762Stjr tcsetpgrp(ttyfd, rootpid); 1681556Srgrimes } else { /* turning job control off */ 16917987Speter setpgid(0, initialpgrp); 17099762Stjr tcsetpgrp(ttyfd, initialpgrp); 17199762Stjr close(ttyfd); 17299762Stjr ttyfd = -1; 1731556Srgrimes setsignal(SIGTSTP); 1741556Srgrimes setsignal(SIGTTOU); 1751556Srgrimes setsignal(SIGTTIN); 1761556Srgrimes } 1771556Srgrimes jobctl = on; 1781556Srgrimes} 17920425Ssteve#endif 1801556Srgrimes 1811556Srgrimes 1821556Srgrimes#ifdef mkinit 18328346SsteveINCLUDE <sys/types.h> 18417987SpeterINCLUDE <stdlib.h> 1851556Srgrimes 1861556SrgrimesSHELLPROC { 1871556Srgrimes backgndpid = -1; 188209600Sjilles bgjob = NULL; 1891556Srgrimes#if JOBS 1901556Srgrimes jobctl = 0; 1911556Srgrimes#endif 1921556Srgrimes} 1931556Srgrimes 1941556Srgrimes#endif 1951556Srgrimes 1961556Srgrimes 1971556Srgrimes 1981556Srgrimes#if JOBS 19917987Speterint 20090111Simpfgcmd(int argc __unused, char **argv) 20117987Speter{ 2021556Srgrimes struct job *jp; 203100308Stjr pid_t pgrp; 2041556Srgrimes int status; 2051556Srgrimes 2061556Srgrimes jp = getjob(argv[1]); 2071556Srgrimes if (jp->jobctl == 0) 2081556Srgrimes error("job not created under job control"); 209216217Sjilles printjobcmd(jp); 21096933Stjr flushout(&output); 2111556Srgrimes pgrp = jp->ps[0].pid; 21299762Stjr tcsetpgrp(ttyfd, pgrp); 2131556Srgrimes restartjob(jp); 214100305Stjr jp->foreground = 1; 2151556Srgrimes INTOFF; 21645916Scracauer status = waitforjob(jp, (int *)NULL); 2171556Srgrimes INTON; 2181556Srgrimes return status; 2191556Srgrimes} 2201556Srgrimes 2211556Srgrimes 22217987Speterint 22390111Simpbgcmd(int argc, char **argv) 22417987Speter{ 22596933Stjr char s[64]; 2261556Srgrimes struct job *jp; 2271556Srgrimes 2281556Srgrimes do { 2291556Srgrimes jp = getjob(*++argv); 2301556Srgrimes if (jp->jobctl == 0) 2311556Srgrimes error("job not created under job control"); 23296933Stjr if (jp->state == JOBDONE) 23396933Stjr continue; 2341556Srgrimes restartjob(jp); 235100305Stjr jp->foreground = 0; 236104275Smux fmtstr(s, 64, "[%td] ", jp - jobtab + 1); 23796933Stjr out1str(s); 238216217Sjilles printjobcmd(jp); 2391556Srgrimes } while (--argc > 1); 2401556Srgrimes return 0; 2411556Srgrimes} 2421556Srgrimes 2431556Srgrimes 244213811Sobrienstatic void 24590111Simprestartjob(struct job *jp) 24617987Speter{ 2471556Srgrimes struct procstat *ps; 2481556Srgrimes int i; 2491556Srgrimes 2501556Srgrimes if (jp->state == JOBDONE) 2511556Srgrimes return; 25297660Stjr setcurjob(jp); 2531556Srgrimes INTOFF; 2541556Srgrimes killpg(jp->ps[0].pid, SIGCONT); 2551556Srgrimes for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 25626104Ssteve if (WIFSTOPPED(ps->status)) { 2571556Srgrimes ps->status = -1; 2581556Srgrimes jp->state = 0; 2591556Srgrimes } 2601556Srgrimes } 2611556Srgrimes INTON; 2621556Srgrimes} 2631556Srgrimes#endif 2641556Srgrimes 2651556Srgrimes 2661556Srgrimesint 26797669Stjrjobscmd(int argc, char *argv[]) 26817987Speter{ 26997669Stjr char *id; 270163085Sstefanf int ch, mode; 27197669Stjr 27297669Stjr optind = optreset = 1; 273100663Stjr opterr = 0; 274163085Sstefanf mode = SHOWJOBS_DEFAULT; 275163085Sstefanf while ((ch = getopt(argc, argv, "lps")) != -1) { 27697669Stjr switch (ch) { 27797669Stjr case 'l': 278163085Sstefanf mode = SHOWJOBS_VERBOSE; 27997669Stjr break; 280163085Sstefanf case 'p': 281163085Sstefanf mode = SHOWJOBS_PGIDS; 282163085Sstefanf break; 28397669Stjr case 's': 284163085Sstefanf mode = SHOWJOBS_PIDS; 28597669Stjr break; 28697669Stjr case '?': 28797669Stjr default: 28897669Stjr error("unknown option: -%c", optopt); 28997669Stjr } 29097669Stjr } 29197669Stjr argc -= optind; 29297669Stjr argv += optind; 29397669Stjr 29497669Stjr if (argc == 0) 295163085Sstefanf showjobs(0, mode); 29697669Stjr else 29797669Stjr while ((id = *argv++) != NULL) 298216217Sjilles showjob(getjob(id), mode); 29997669Stjr 30097669Stjr return (0); 3011556Srgrimes} 3021556Srgrimes 303213811Sobrienstatic void 304216217Sjillesprintjobcmd(struct job *jp) 30597663Stjr{ 306216217Sjilles struct procstat *ps; 307216217Sjilles int i; 308216217Sjilles 309216217Sjilles for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 310216217Sjilles out1str(ps->cmd); 311216217Sjilles if (i > 0) 312216217Sjilles out1str(" | "); 313216217Sjilles } 314216217Sjilles out1c('\n'); 315216217Sjilles} 316216217Sjilles 317216217Sjillesstatic void 318216217Sjillesshowjob(struct job *jp, int mode) 319216217Sjilles{ 32097663Stjr char s[64]; 321216217Sjilles char statestr[64]; 32297663Stjr struct procstat *ps; 32397669Stjr struct job *j; 32497669Stjr int col, curr, i, jobno, prev, procno; 32597669Stjr char c; 3261556Srgrimes 327163085Sstefanf procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs; 32897663Stjr jobno = jp - jobtab + 1; 32997669Stjr curr = prev = 0; 33097669Stjr#if JOBS 33197669Stjr if ((j = getcurjob(NULL)) != NULL) { 33297669Stjr curr = j - jobtab + 1; 33397669Stjr if ((j = getcurjob(j)) != NULL) 33497669Stjr prev = j - jobtab + 1; 33597669Stjr } 33697669Stjr#endif 337216217Sjilles ps = jp->ps + jp->nprocs - 1; 338216217Sjilles if (jp->state == 0) { 339216217Sjilles strcpy(statestr, "Running"); 340216217Sjilles#if JOBS 341216217Sjilles } else if (jp->state == JOBSTOPPED) { 342216217Sjilles while (!WIFSTOPPED(ps->status) && ps > jp->ps) 343216217Sjilles ps--; 344216217Sjilles if (WIFSTOPPED(ps->status)) 345216217Sjilles i = WSTOPSIG(ps->status); 346216217Sjilles else 347216217Sjilles i = -1; 348216217Sjilles if (i > 0 && i < sys_nsig && sys_siglist[i]) 349216217Sjilles strcpy(statestr, sys_siglist[i]); 350216217Sjilles else 351216217Sjilles strcpy(statestr, "Suspended"); 352216217Sjilles#endif 353216217Sjilles } else if (WIFEXITED(ps->status)) { 354216217Sjilles if (WEXITSTATUS(ps->status) == 0) 355216217Sjilles strcpy(statestr, "Done"); 356216217Sjilles else 357216220Sjilles fmtstr(statestr, 64, "Done(%d)", 358216217Sjilles WEXITSTATUS(ps->status)); 359216217Sjilles } else { 360216217Sjilles i = WTERMSIG(ps->status); 361216217Sjilles if (i > 0 && i < sys_nsig && sys_siglist[i]) 362216217Sjilles strcpy(statestr, sys_siglist[i]); 363216217Sjilles else 364216217Sjilles fmtstr(statestr, 64, "Signal %d", i); 365216217Sjilles if (WCOREDUMP(ps->status)) 366216217Sjilles strcat(statestr, " (core dumped)"); 367216217Sjilles } 368216217Sjilles 36997663Stjr for (ps = jp->ps ; ; ps++) { /* for each process */ 370163085Sstefanf if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) { 371216199Sjilles out1fmt("%d\n", (int)ps->pid); 37297669Stjr goto skip; 37397669Stjr } 374216217Sjilles if (mode != SHOWJOBS_VERBOSE && ps != jp->ps) 37597669Stjr goto skip; 37697819Stjr if (jobno == curr && ps == jp->ps) 37797669Stjr c = '+'; 37897819Stjr else if (jobno == prev && ps == jp->ps) 37997669Stjr c = '-'; 38097669Stjr else 38197669Stjr c = ' '; 38297663Stjr if (ps == jp->ps) 38397669Stjr fmtstr(s, 64, "[%d] %c ", jobno, c); 38497663Stjr else 38597816Stjr fmtstr(s, 64, " %c ", c); 38697663Stjr out1str(s); 38797663Stjr col = strlen(s); 388163085Sstefanf if (mode == SHOWJOBS_VERBOSE) { 389100308Stjr fmtstr(s, 64, "%d ", (int)ps->pid); 39097669Stjr out1str(s); 39197669Stjr col += strlen(s); 39297669Stjr } 393216217Sjilles if (ps == jp->ps) { 394216217Sjilles out1str(statestr); 395216217Sjilles col += strlen(statestr); 39697663Stjr } 39797663Stjr do { 39897663Stjr out1c(' '); 39997663Stjr col++; 40097663Stjr } while (col < 30); 401216217Sjilles if (mode == SHOWJOBS_VERBOSE) { 402216217Sjilles out1str(ps->cmd); 403216217Sjilles out1c('\n'); 404216217Sjilles } else 405216217Sjilles printjobcmd(jp); 40697669Stjrskip: if (--procno <= 0) 40797663Stjr break; 40897663Stjr } 40997663Stjr} 41097663Stjr 4111556Srgrimes/* 4121556Srgrimes * Print a list of jobs. If "change" is nonzero, only print jobs whose 4131556Srgrimes * statuses have changed since the last call to showjobs. 4141556Srgrimes * 4151556Srgrimes * If the shell is interrupted in the process of creating a job, the 4161556Srgrimes * result may be a job structure containing zero processes. Such structures 4171556Srgrimes * will be freed here. 4181556Srgrimes */ 4191556Srgrimes 4201556Srgrimesvoid 421163085Sstefanfshowjobs(int change, int mode) 42217987Speter{ 4231556Srgrimes int jobno; 4241556Srgrimes struct job *jp; 4251556Srgrimes 4261556Srgrimes TRACE(("showjobs(%d) called\n", change)); 427208489Sjilles checkzombies(); 4281556Srgrimes for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) { 4291556Srgrimes if (! jp->used) 4301556Srgrimes continue; 4311556Srgrimes if (jp->nprocs == 0) { 4321556Srgrimes freejob(jp); 4331556Srgrimes continue; 4341556Srgrimes } 4351556Srgrimes if (change && ! jp->changed) 4361556Srgrimes continue; 437216217Sjilles showjob(jp, mode); 4381556Srgrimes jp->changed = 0; 439209600Sjilles /* Hack: discard jobs for which $! has not been referenced 440209600Sjilles * in interactive mode when they terminate. 441209600Sjilles */ 442209600Sjilles if (jp->state == JOBDONE && !jp->remembered && 443209600Sjilles (iflag || jp != bgjob)) { 4441556Srgrimes freejob(jp); 4451556Srgrimes } 4461556Srgrimes } 4471556Srgrimes} 4481556Srgrimes 4491556Srgrimes 4501556Srgrimes/* 4511556Srgrimes * Mark a job structure as unused. 4521556Srgrimes */ 4531556Srgrimes 454213811Sobrienstatic void 45590111Simpfreejob(struct job *jp) 45690111Simp{ 4571556Srgrimes struct procstat *ps; 4581556Srgrimes int i; 4591556Srgrimes 4601556Srgrimes INTOFF; 461209600Sjilles if (bgjob == jp) 462209600Sjilles bgjob = NULL; 4631556Srgrimes for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { 4641556Srgrimes if (ps->cmd != nullstr) 4651556Srgrimes ckfree(ps->cmd); 4661556Srgrimes } 4671556Srgrimes if (jp->ps != &jp->ps0) 4681556Srgrimes ckfree(jp->ps); 4691556Srgrimes jp->used = 0; 4701556Srgrimes#if JOBS 47197659Stjr deljob(jp); 4721556Srgrimes#endif 4731556Srgrimes INTON; 4741556Srgrimes} 4751556Srgrimes 4761556Srgrimes 4771556Srgrimes 4781556Srgrimesint 47990111Simpwaitcmd(int argc, char **argv) 48017987Speter{ 4811556Srgrimes struct job *job; 48226104Ssteve int status, retval; 4831556Srgrimes struct job *jp; 4841556Srgrimes 4851556Srgrimes if (argc > 1) { 4861556Srgrimes job = getjob(argv[1]); 4871556Srgrimes } else { 4881556Srgrimes job = NULL; 4891556Srgrimes } 49038536Scracauer 49138536Scracauer /* 49238536Scracauer * Loop until a process is terminated or stopped, or a SIGINT is 49338536Scracauer * received. 49438536Scracauer */ 49538536Scracauer 49638521Scracauer in_waitcmd++; 49738536Scracauer do { 4981556Srgrimes if (job != NULL) { 4991556Srgrimes if (job->state) { 5001556Srgrimes status = job->ps[job->nprocs - 1].status; 50126104Ssteve if (WIFEXITED(status)) 50226104Ssteve retval = WEXITSTATUS(status); 5031556Srgrimes#if JOBS 50426104Ssteve else if (WIFSTOPPED(status)) 50526104Ssteve retval = WSTOPSIG(status) + 128; 5061556Srgrimes#endif 5071556Srgrimes else 50826104Ssteve retval = WTERMSIG(status) + 128; 509209600Sjilles if (! iflag || ! job->changed) 5101556Srgrimes freejob(job); 511209600Sjilles else { 512209600Sjilles job->remembered = 0; 513209600Sjilles if (job == bgjob) 514209600Sjilles bgjob = NULL; 515209600Sjilles } 51638536Scracauer in_waitcmd--; 51726104Ssteve return retval; 5181556Srgrimes } 5191556Srgrimes } else { 520209600Sjilles for (jp = jobtab ; jp < jobtab + njobs; jp++) 521209600Sjilles if (jp->used && jp->state == JOBDONE) { 522209600Sjilles if (! iflag || ! jp->changed) 523209600Sjilles freejob(jp); 524209600Sjilles else { 525209600Sjilles jp->remembered = 0; 526209600Sjilles if (jp == bgjob) 527209600Sjilles bgjob = NULL; 528209600Sjilles } 529209600Sjilles } 5301556Srgrimes for (jp = jobtab ; ; jp++) { 5311556Srgrimes if (jp >= jobtab + njobs) { /* no running procs */ 53238536Scracauer in_waitcmd--; 5331556Srgrimes return 0; 5341556Srgrimes } 5351556Srgrimes if (jp->used && jp->state == 0) 5361556Srgrimes break; 5371556Srgrimes } 5381556Srgrimes } 53938521Scracauer } while (dowait(1, (struct job *)NULL) != -1); 54038521Scracauer in_waitcmd--; 54138521Scracauer 54238521Scracauer return 0; 5431556Srgrimes} 5441556Srgrimes 5451556Srgrimes 5461556Srgrimes 54717987Speterint 54890111Simpjobidcmd(int argc __unused, char **argv) 54917987Speter{ 5501556Srgrimes struct job *jp; 5511556Srgrimes int i; 5521556Srgrimes 5531556Srgrimes jp = getjob(argv[1]); 5541556Srgrimes for (i = 0 ; i < jp->nprocs ; ) { 555100308Stjr out1fmt("%d", (int)jp->ps[i].pid); 5561556Srgrimes out1c(++i < jp->nprocs? ' ' : '\n'); 5571556Srgrimes } 5581556Srgrimes return 0; 5591556Srgrimes} 5601556Srgrimes 5611556Srgrimes 5621556Srgrimes 5631556Srgrimes/* 5641556Srgrimes * Convert a job name to a job structure. 5651556Srgrimes */ 5661556Srgrimes 567213811Sobrienstatic struct job * 56890111Simpgetjob(char *name) 56990111Simp{ 5701556Srgrimes int jobno; 57197688Stjr struct job *found, *jp; 572100308Stjr pid_t pid; 5731556Srgrimes int i; 5741556Srgrimes 5751556Srgrimes if (name == NULL) { 5761556Srgrimes#if JOBS 57797659Stjrcurrentjob: if ((jp = getcurjob(NULL)) == NULL) 5781556Srgrimes error("No current job"); 57997659Stjr return (jp); 5801556Srgrimes#else 5811556Srgrimes error("No current job"); 5821556Srgrimes#endif 5831556Srgrimes } else if (name[0] == '%') { 5841556Srgrimes if (is_digit(name[1])) { 5851556Srgrimes jobno = number(name + 1); 5861556Srgrimes if (jobno > 0 && jobno <= njobs 5871556Srgrimes && jobtab[jobno - 1].used != 0) 5881556Srgrimes return &jobtab[jobno - 1]; 5891556Srgrimes#if JOBS 5901556Srgrimes } else if (name[1] == '%' && name[2] == '\0') { 5911556Srgrimes goto currentjob; 59297688Stjr } else if (name[1] == '+' && name[2] == '\0') { 59397688Stjr goto currentjob; 59497688Stjr } else if (name[1] == '-' && name[2] == '\0') { 59597688Stjr if ((jp = getcurjob(NULL)) == NULL || 59697688Stjr (jp = getcurjob(jp)) == NULL) 59797688Stjr error("No previous job"); 59897688Stjr return (jp); 5991556Srgrimes#endif 60097688Stjr } else if (name[1] == '?') { 60197688Stjr found = NULL; 60297688Stjr for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 60397688Stjr if (jp->used && jp->nprocs > 0 60497688Stjr && strstr(jp->ps[0].cmd, name + 2) != NULL) { 60597688Stjr if (found) 60697688Stjr error("%s: ambiguous", name); 60797688Stjr found = jp; 60897688Stjr } 60997688Stjr } 61097688Stjr if (found != NULL) 61197688Stjr return (found); 6121556Srgrimes } else { 61397688Stjr found = NULL; 6141556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 6151556Srgrimes if (jp->used && jp->nprocs > 0 6161556Srgrimes && prefix(name + 1, jp->ps[0].cmd)) { 6171556Srgrimes if (found) 6181556Srgrimes error("%s: ambiguous", name); 6191556Srgrimes found = jp; 6201556Srgrimes } 6211556Srgrimes } 6221556Srgrimes if (found) 6231556Srgrimes return found; 6241556Srgrimes } 6251556Srgrimes } else if (is_number(name)) { 626100308Stjr pid = (pid_t)number(name); 6271556Srgrimes for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 6281556Srgrimes if (jp->used && jp->nprocs > 0 6291556Srgrimes && jp->ps[jp->nprocs - 1].pid == pid) 6301556Srgrimes return jp; 6311556Srgrimes } 6321556Srgrimes } 6331556Srgrimes error("No such job: %s", name); 63417987Speter /*NOTREACHED*/ 63517987Speter return NULL; 6361556Srgrimes} 6371556Srgrimes 6381556Srgrimes 6391556Srgrimes 6401556Srgrimes/* 6411556Srgrimes * Return a new job structure, 6421556Srgrimes */ 6431556Srgrimes 6441556Srgrimesstruct job * 64590111Simpmakejob(union node *node __unused, int nprocs) 64617987Speter{ 6471556Srgrimes int i; 6481556Srgrimes struct job *jp; 6491556Srgrimes 6501556Srgrimes for (i = njobs, jp = jobtab ; ; jp++) { 6511556Srgrimes if (--i < 0) { 6521556Srgrimes INTOFF; 6531556Srgrimes if (njobs == 0) { 6541556Srgrimes jobtab = ckmalloc(4 * sizeof jobtab[0]); 65597664Stjr#if JOBS 65697659Stjr jobmru = NULL; 65797664Stjr#endif 6581556Srgrimes } else { 6591556Srgrimes jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 66017987Speter memcpy(jp, jobtab, njobs * sizeof jp[0]); 66197659Stjr#if JOBS 66297659Stjr /* Relocate `next' pointers and list head */ 66399760Stjr if (jobmru != NULL) 66499760Stjr jobmru = &jp[jobmru - jobtab]; 66597659Stjr for (i = 0; i < njobs; i++) 66697659Stjr if (jp[i].next != NULL) 66797659Stjr jp[i].next = &jp[jp[i].next - 66897659Stjr jobtab]; 66997659Stjr#endif 670209600Sjilles if (bgjob != NULL) 671209600Sjilles bgjob = &jp[bgjob - jobtab]; 67220425Ssteve /* Relocate `ps' pointers */ 67320425Ssteve for (i = 0; i < njobs; i++) 67420425Ssteve if (jp[i].ps == &jobtab[i].ps0) 67520425Ssteve jp[i].ps = &jp[i].ps0; 6761556Srgrimes ckfree(jobtab); 6771556Srgrimes jobtab = jp; 6781556Srgrimes } 6791556Srgrimes jp = jobtab + njobs; 6801556Srgrimes for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 6811556Srgrimes INTON; 6821556Srgrimes break; 6831556Srgrimes } 6841556Srgrimes if (jp->used == 0) 6851556Srgrimes break; 6861556Srgrimes } 6871556Srgrimes INTOFF; 6881556Srgrimes jp->state = 0; 6891556Srgrimes jp->used = 1; 6901556Srgrimes jp->changed = 0; 6911556Srgrimes jp->nprocs = 0; 692100305Stjr jp->foreground = 0; 693209600Sjilles jp->remembered = 0; 6941556Srgrimes#if JOBS 6951556Srgrimes jp->jobctl = jobctl; 69697659Stjr jp->next = NULL; 6971556Srgrimes#endif 6981556Srgrimes if (nprocs > 1) { 6991556Srgrimes jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 7001556Srgrimes } else { 7011556Srgrimes jp->ps = &jp->ps0; 7021556Srgrimes } 7031556Srgrimes INTON; 704213775Sjhb TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs, 70517987Speter jp - jobtab + 1)); 7061556Srgrimes return jp; 7078855Srgrimes} 7081556Srgrimes 70997659Stjr#if JOBS 710213811Sobrienstatic void 71197659Stjrsetcurjob(struct job *cj) 71297659Stjr{ 71397659Stjr struct job *jp, *prev; 7141556Srgrimes 71597659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 71697659Stjr if (jp == cj) { 71797659Stjr if (prev != NULL) 71897659Stjr prev->next = jp->next; 71997659Stjr else 72097659Stjr jobmru = jp->next; 72197659Stjr jp->next = jobmru; 72297659Stjr jobmru = cj; 72397659Stjr return; 72497659Stjr } 72597659Stjr } 72697659Stjr cj->next = jobmru; 72797659Stjr jobmru = cj; 72897659Stjr} 72997659Stjr 730213811Sobrienstatic void 73197659Stjrdeljob(struct job *j) 73297659Stjr{ 73397659Stjr struct job *jp, *prev; 73497659Stjr 73597659Stjr for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) { 73697659Stjr if (jp == j) { 73797659Stjr if (prev != NULL) 73897659Stjr prev->next = jp->next; 73997659Stjr else 74097659Stjr jobmru = jp->next; 74197659Stjr return; 74297659Stjr } 74397659Stjr } 74497659Stjr} 74597659Stjr 7461556Srgrimes/* 74797659Stjr * Return the most recently used job that isn't `nj', and preferably one 74897659Stjr * that is stopped. 74997659Stjr */ 750213811Sobrienstatic struct job * 75197659Stjrgetcurjob(struct job *nj) 75297659Stjr{ 75397659Stjr struct job *jp; 75497659Stjr 75597659Stjr /* Try to find a stopped one.. */ 75697659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 75797659Stjr if (jp->used && jp != nj && jp->state == JOBSTOPPED) 75897659Stjr return (jp); 75997659Stjr /* Otherwise the most recently used job that isn't `nj' */ 76097659Stjr for (jp = jobmru; jp != NULL; jp = jp->next) 76197659Stjr if (jp->used && jp != nj) 76297659Stjr return (jp); 76397659Stjr 76497659Stjr return (NULL); 76597659Stjr} 76697659Stjr 76797659Stjr#endif 76897659Stjr 76997659Stjr/* 7701556Srgrimes * Fork of a subshell. If we are doing job control, give the subshell its 7711556Srgrimes * own process group. Jp is a job structure that the job is to be added to. 7721556Srgrimes * N is the command that will be evaluated by the child. Both jp and n may 7731556Srgrimes * be NULL. The mode parameter can be one of the following: 7741556Srgrimes * FORK_FG - Fork off a foreground process. 7751556Srgrimes * FORK_BG - Fork off a background process. 7761556Srgrimes * FORK_NOJOB - Like FORK_FG, but don't give the process its own 7771556Srgrimes * process group even if job control is on. 7781556Srgrimes * 7791556Srgrimes * When job control is turned off, background processes have their standard 7801556Srgrimes * input redirected to /dev/null (except for the second and later processes 7811556Srgrimes * in a pipeline). 7821556Srgrimes */ 7831556Srgrimes 784100308Stjrpid_t 78590111Simpforkshell(struct job *jp, union node *n, int mode) 78617987Speter{ 787100308Stjr pid_t pid; 788100308Stjr pid_t pgrp; 7891556Srgrimes 790213775Sjhb TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n, 79117987Speter mode)); 7921556Srgrimes INTOFF; 793216208Sjilles if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0)) 794208489Sjilles checkzombies(); 795112341Stjr flushall(); 7961556Srgrimes pid = fork(); 7971556Srgrimes if (pid == -1) { 7981556Srgrimes TRACE(("Fork failed, errno=%d\n", errno)); 7991556Srgrimes INTON; 80053891Scracauer error("Cannot fork: %s", strerror(errno)); 8011556Srgrimes } 8021556Srgrimes if (pid == 0) { 8031556Srgrimes struct job *p; 8041556Srgrimes int wasroot; 8051556Srgrimes int i; 8061556Srgrimes 807100308Stjr TRACE(("Child shell %d\n", (int)getpid())); 8081556Srgrimes wasroot = rootshell; 8091556Srgrimes rootshell = 0; 810200998Sjilles handler = &main_handler; 8111556Srgrimes closescript(); 8121556Srgrimes INTON; 8131556Srgrimes clear_traps(); 8141556Srgrimes#if JOBS 8151556Srgrimes jobctl = 0; /* do job control only in root shell */ 8161556Srgrimes if (wasroot && mode != FORK_NOJOB && mflag) { 8171556Srgrimes if (jp == NULL || jp->nprocs == 0) 8181556Srgrimes pgrp = getpid(); 8191556Srgrimes else 8201556Srgrimes pgrp = jp->ps[0].pid; 82121352Ssteve if (setpgid(0, pgrp) == 0 && mode == FORK_FG) { 8221556Srgrimes /*** this causes superfluous TIOCSPGRPS ***/ 82399762Stjr if (tcsetpgrp(ttyfd, pgrp) < 0) 82420425Ssteve error("tcsetpgrp failed, errno=%d", errno); 8251556Srgrimes } 8261556Srgrimes setsignal(SIGTSTP); 8271556Srgrimes setsignal(SIGTTOU); 8281556Srgrimes } else if (mode == FORK_BG) { 8291556Srgrimes ignoresig(SIGINT); 8301556Srgrimes ignoresig(SIGQUIT); 8311556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 8321556Srgrimes ! fd0_redirected_p ()) { 8331556Srgrimes close(0); 83469793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 83569793Sobrien error("Can't open %s: %s", 83669793Sobrien _PATH_DEVNULL, strerror(errno)); 8371556Srgrimes } 8381556Srgrimes } 8391556Srgrimes#else 8401556Srgrimes if (mode == FORK_BG) { 8411556Srgrimes ignoresig(SIGINT); 8421556Srgrimes ignoresig(SIGQUIT); 8431556Srgrimes if ((jp == NULL || jp->nprocs == 0) && 8441556Srgrimes ! fd0_redirected_p ()) { 8451556Srgrimes close(0); 84669793Sobrien if (open(_PATH_DEVNULL, O_RDONLY) != 0) 847155301Sschweikh error("Can't open %s: %s", 84869793Sobrien _PATH_DEVNULL, strerror(errno)); 8491556Srgrimes } 8501556Srgrimes } 8511556Srgrimes#endif 852102051Stjr INTOFF; 853102051Stjr for (i = njobs, p = jobtab ; --i >= 0 ; p++) 854102051Stjr if (p->used) 855102051Stjr freejob(p); 856102051Stjr INTON; 8571556Srgrimes if (wasroot && iflag) { 8581556Srgrimes setsignal(SIGINT); 8591556Srgrimes setsignal(SIGQUIT); 8601556Srgrimes setsignal(SIGTERM); 8611556Srgrimes } 8621556Srgrimes return pid; 8631556Srgrimes } 8641556Srgrimes if (rootshell && mode != FORK_NOJOB && mflag) { 8651556Srgrimes if (jp == NULL || jp->nprocs == 0) 8661556Srgrimes pgrp = pid; 8671556Srgrimes else 8681556Srgrimes pgrp = jp->ps[0].pid; 86917987Speter setpgid(pid, pgrp); 8701556Srgrimes } 871209600Sjilles if (mode == FORK_BG) { 872209600Sjilles if (bgjob != NULL && bgjob->state == JOBDONE && 873209600Sjilles !bgjob->remembered && !iflag) 874209600Sjilles freejob(bgjob); 8751556Srgrimes backgndpid = pid; /* set $! */ 876209600Sjilles bgjob = jp; 877209600Sjilles } 8781556Srgrimes if (jp) { 8791556Srgrimes struct procstat *ps = &jp->ps[jp->nprocs++]; 8801556Srgrimes ps->pid = pid; 8811556Srgrimes ps->status = -1; 8821556Srgrimes ps->cmd = nullstr; 8831556Srgrimes if (iflag && rootshell && n) 8841556Srgrimes ps->cmd = commandtext(n); 885100305Stjr jp->foreground = mode == FORK_FG; 88697659Stjr#if JOBS 88797659Stjr setcurjob(jp); 88897659Stjr#endif 8891556Srgrimes } 8901556Srgrimes INTON; 891100308Stjr TRACE(("In parent shell: child = %d\n", (int)pid)); 8921556Srgrimes return pid; 8931556Srgrimes} 8941556Srgrimes 8951556Srgrimes 8961556Srgrimes 8971556Srgrimes/* 8981556Srgrimes * Wait for job to finish. 8991556Srgrimes * 9001556Srgrimes * Under job control we have the problem that while a child process is 9011556Srgrimes * running interrupts generated by the user are sent to the child but not 9021556Srgrimes * to the shell. This means that an infinite loop started by an inter- 9031556Srgrimes * active user may be hard to kill. With job control turned off, an 9041556Srgrimes * interactive user may place an interactive program inside a loop. If 9051556Srgrimes * the interactive program catches interrupts, the user doesn't want 9061556Srgrimes * these interrupts to also abort the loop. The approach we take here 9071556Srgrimes * is to have the shell ignore interrupt signals while waiting for a 90846684Skris * foreground process to terminate, and then send itself an interrupt 9091556Srgrimes * signal if the child process was terminated by an interrupt signal. 9101556Srgrimes * Unfortunately, some programs want to do a bit of cleanup and then 9111556Srgrimes * exit on interrupt; unless these processes terminate themselves by 9121556Srgrimes * sending a signal to themselves (instead of calling exit) they will 9131556Srgrimes * confuse this approach. 9141556Srgrimes */ 9151556Srgrimes 9161556Srgrimesint 91790111Simpwaitforjob(struct job *jp, int *origstatus) 91845916Scracauer{ 9191556Srgrimes#if JOBS 920100308Stjr pid_t mypgrp = getpgrp(); 921208881Sjilles int propagate_int = jp->jobctl && jp->foreground; 9221556Srgrimes#endif 9231556Srgrimes int status; 9241556Srgrimes int st; 9251556Srgrimes 9261556Srgrimes INTOFF; 927213775Sjhb TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1)); 92838950Scracauer while (jp->state == 0) 92938950Scracauer if (dowait(1, jp) == -1) 93038950Scracauer dotrap(); 9311556Srgrimes#if JOBS 9321556Srgrimes if (jp->jobctl) { 93399762Stjr if (tcsetpgrp(ttyfd, mypgrp) < 0) 93420425Ssteve error("tcsetpgrp failed, errno=%d\n", errno); 9351556Srgrimes } 9361556Srgrimes if (jp->state == JOBSTOPPED) 93797659Stjr setcurjob(jp); 9381556Srgrimes#endif 9391556Srgrimes status = jp->ps[jp->nprocs - 1].status; 94045916Scracauer if (origstatus != NULL) 94145916Scracauer *origstatus = status; 9421556Srgrimes /* convert to 8 bits */ 94326104Ssteve if (WIFEXITED(status)) 94426104Ssteve st = WEXITSTATUS(status); 9451556Srgrimes#if JOBS 94626104Ssteve else if (WIFSTOPPED(status)) 94726104Ssteve st = WSTOPSIG(status) + 128; 9481556Srgrimes#endif 9491556Srgrimes else 95026104Ssteve st = WTERMSIG(status) + 128; 9511556Srgrimes if (! JOBS || jp->state == JOBDONE) 9521556Srgrimes freejob(jp); 95338521Scracauer if (int_pending()) { 95438521Scracauer if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 95538521Scracauer kill(getpid(), SIGINT); 95638521Scracauer else 95738521Scracauer CLEAR_PENDING_INT; 95838521Scracauer } 959208881Sjilles#if JOBS 960208881Sjilles else if (rootshell && iflag && propagate_int && 961208881Sjilles WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) 962208881Sjilles kill(getpid(), SIGINT); 963208881Sjilles#endif 9641556Srgrimes INTON; 9651556Srgrimes return st; 9661556Srgrimes} 9671556Srgrimes 9681556Srgrimes 9691556Srgrimes 9701556Srgrimes/* 9711556Srgrimes * Wait for a process to terminate. 9721556Srgrimes */ 9731556Srgrimes 974213811Sobrienstatic pid_t 97590111Simpdowait(int block, struct job *job) 97617987Speter{ 977100308Stjr pid_t pid; 9781556Srgrimes int status; 9791556Srgrimes struct procstat *sp; 9801556Srgrimes struct job *jp; 9811556Srgrimes struct job *thisjob; 9821556Srgrimes int done; 9831556Srgrimes int stopped; 98426104Ssteve int sig; 985216217Sjilles int coredump; 9861556Srgrimes 98738950Scracauer in_dowait++; 9881556Srgrimes TRACE(("dowait(%d) called\n", block)); 9891556Srgrimes do { 9901556Srgrimes pid = waitproc(block, &status); 991100308Stjr TRACE(("wait returns %d, status=%d\n", (int)pid, status)); 99272086Scracauer } while ((pid == -1 && errno == EINTR && breakwaitcmd == 0) || 993125501Scracauer (pid > 0 && WIFSTOPPED(status) && !iflag)); 99438950Scracauer in_dowait--; 995153417Smaxim if (pid == -1 && errno == ECHILD && job != NULL) 996153417Smaxim job->state = JOBDONE; 99738521Scracauer if (breakwaitcmd != 0) { 99838521Scracauer breakwaitcmd = 0; 999138312Smaxim if (pid <= 0) 1000138312Smaxim return -1; 100138521Scracauer } 10021556Srgrimes if (pid <= 0) 10031556Srgrimes return pid; 10041556Srgrimes INTOFF; 10051556Srgrimes thisjob = NULL; 10061556Srgrimes for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 1007216208Sjilles if (jp->used && jp->nprocs > 0) { 10081556Srgrimes done = 1; 10091556Srgrimes stopped = 1; 10101556Srgrimes for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 10111556Srgrimes if (sp->pid == -1) 10121556Srgrimes continue; 10131556Srgrimes if (sp->pid == pid) { 101426104Ssteve TRACE(("Changing status of proc %d from 0x%x to 0x%x\n", 1015100308Stjr (int)pid, sp->status, 1016100308Stjr status)); 10171556Srgrimes sp->status = status; 10181556Srgrimes thisjob = jp; 10191556Srgrimes } 10201556Srgrimes if (sp->status == -1) 10211556Srgrimes stopped = 0; 102226104Ssteve else if (WIFSTOPPED(sp->status)) 10231556Srgrimes done = 0; 10241556Srgrimes } 10251556Srgrimes if (stopped) { /* stopped or done */ 10261556Srgrimes int state = done? JOBDONE : JOBSTOPPED; 10271556Srgrimes if (jp->state != state) { 1028213775Sjhb TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 10291556Srgrimes jp->state = state; 1030209600Sjilles if (jp != job) { 1031209600Sjilles if (done && !jp->remembered && 1032209600Sjilles !iflag && jp != bgjob) 1033209600Sjilles freejob(jp); 10341556Srgrimes#if JOBS 1035209600Sjilles else if (done) 1036209600Sjilles deljob(jp); 10371556Srgrimes#endif 1038209600Sjilles } 10391556Srgrimes } 10401556Srgrimes } 10411556Srgrimes } 10421556Srgrimes } 10431556Srgrimes INTON; 1044216217Sjilles if (!thisjob || thisjob->state == 0) 1045216217Sjilles ; 1046216217Sjilles else if ((!rootshell || !iflag || thisjob == job) && 1047216217Sjilles thisjob->foreground && thisjob->state != JOBSTOPPED) { 1048216217Sjilles sig = 0; 1049216217Sjilles coredump = 0; 1050216217Sjilles for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++) 1051216217Sjilles if (WIFSIGNALED(sp->status)) { 1052216217Sjilles sig = WTERMSIG(sp->status); 1053216217Sjilles coredump = WCOREDUMP(sp->status); 1054216217Sjilles } 1055216217Sjilles if (sig > 0 && sig != SIGINT && sig != SIGPIPE) { 1056216217Sjilles if (sig < sys_nsig && sys_siglist[sig]) 1057216217Sjilles out1str(sys_siglist[sig]); 105826104Ssteve else 1059216217Sjilles out1fmt("Signal %d", sig); 1060216217Sjilles if (coredump) 1061216217Sjilles out1str(" (core dumped)"); 1062216217Sjilles out1c('\n'); 10631556Srgrimes } 10641556Srgrimes } else { 1065109627Stjr TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job)); 1066216217Sjilles thisjob->changed = 1; 10671556Srgrimes } 10681556Srgrimes return pid; 10691556Srgrimes} 10701556Srgrimes 10711556Srgrimes 10721556Srgrimes 10731556Srgrimes/* 10741556Srgrimes * Do a wait system call. If job control is compiled in, we accept 10751556Srgrimes * stopped processes. If block is zero, we return a value of zero 10761556Srgrimes * rather than blocking. 10771556Srgrimes */ 1078213811Sobrienstatic pid_t 107990111Simpwaitproc(int block, int *status) 108017987Speter{ 10811556Srgrimes int flags; 10821556Srgrimes 10831556Srgrimes#if JOBS 10841556Srgrimes flags = WUNTRACED; 10851556Srgrimes#else 10861556Srgrimes flags = 0; 10871556Srgrimes#endif 10881556Srgrimes if (block == 0) 10891556Srgrimes flags |= WNOHANG; 10901556Srgrimes return wait3(status, flags, (struct rusage *)NULL); 10911556Srgrimes} 10921556Srgrimes 10931556Srgrimes/* 10941556Srgrimes * return 1 if there are stopped jobs, otherwise 0 10951556Srgrimes */ 10961556Srgrimesint job_warning = 0; 10971556Srgrimesint 109890111Simpstoppedjobs(void) 10991556Srgrimes{ 110025222Ssteve int jobno; 110125222Ssteve struct job *jp; 11021556Srgrimes 11031556Srgrimes if (job_warning) 11041556Srgrimes return (0); 11051556Srgrimes for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 11061556Srgrimes if (jp->used == 0) 11071556Srgrimes continue; 11081556Srgrimes if (jp->state == JOBSTOPPED) { 1109199629Sjilles out2fmt_flush("You have stopped jobs.\n"); 11101556Srgrimes job_warning = 2; 11111556Srgrimes return (1); 11121556Srgrimes } 11131556Srgrimes } 11141556Srgrimes 11151556Srgrimes return (0); 11161556Srgrimes} 11171556Srgrimes 1118208489Sjilles 1119213811Sobrienstatic void 1120208489Sjillescheckzombies(void) 1121208489Sjilles{ 1122208489Sjilles while (njobs > 0 && dowait(0, NULL) > 0) 1123208489Sjilles ; 1124208489Sjilles} 1125208489Sjilles 1126208489Sjilles 1127209600Sjillesint 1128209600Sjillesbackgndpidset(void) 1129209600Sjilles{ 1130209600Sjilles return backgndpid != -1; 1131209600Sjilles} 1132209600Sjilles 1133209600Sjilles 1134209600Sjillespid_t 1135209600Sjillesbackgndpidval(void) 1136209600Sjilles{ 1137209600Sjilles if (bgjob != NULL) 1138209600Sjilles bgjob->remembered = 1; 1139209600Sjilles return backgndpid; 1140209600Sjilles} 1141209600Sjilles 11421556Srgrimes/* 11431556Srgrimes * Return a string identifying a command (to be printed by the 11441556Srgrimes * jobs command. 11451556Srgrimes */ 11461556Srgrimes 1147213760Sobrienstatic char *cmdnextc; 1148213760Sobrienstatic int cmdnleft; 11491556Srgrimes#define MAXCMDTEXT 200 11501556Srgrimes 11511556Srgrimeschar * 115290111Simpcommandtext(union node *n) 115390111Simp{ 11541556Srgrimes char *name; 11551556Srgrimes 11561556Srgrimes cmdnextc = name = ckmalloc(MAXCMDTEXT); 11571556Srgrimes cmdnleft = MAXCMDTEXT - 4; 11581556Srgrimes cmdtxt(n); 11591556Srgrimes *cmdnextc = '\0'; 11601556Srgrimes return name; 11611556Srgrimes} 11621556Srgrimes 11631556Srgrimes 1164213811Sobrienstatic void 116590111Simpcmdtxt(union node *n) 116690111Simp{ 11671556Srgrimes union node *np; 11681556Srgrimes struct nodelist *lp; 1169201053Sjilles const char *p; 11701556Srgrimes int i; 11711556Srgrimes char s[2]; 11721556Srgrimes 11731556Srgrimes if (n == NULL) 11741556Srgrimes return; 11751556Srgrimes switch (n->type) { 11761556Srgrimes case NSEMI: 11771556Srgrimes cmdtxt(n->nbinary.ch1); 11781556Srgrimes cmdputs("; "); 11791556Srgrimes cmdtxt(n->nbinary.ch2); 11801556Srgrimes break; 11811556Srgrimes case NAND: 11821556Srgrimes cmdtxt(n->nbinary.ch1); 11831556Srgrimes cmdputs(" && "); 11841556Srgrimes cmdtxt(n->nbinary.ch2); 11851556Srgrimes break; 11861556Srgrimes case NOR: 11871556Srgrimes cmdtxt(n->nbinary.ch1); 11881556Srgrimes cmdputs(" || "); 11891556Srgrimes cmdtxt(n->nbinary.ch2); 11901556Srgrimes break; 11911556Srgrimes case NPIPE: 11921556Srgrimes for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 11931556Srgrimes cmdtxt(lp->n); 11941556Srgrimes if (lp->next) 11951556Srgrimes cmdputs(" | "); 11961556Srgrimes } 11971556Srgrimes break; 11981556Srgrimes case NSUBSHELL: 11991556Srgrimes cmdputs("("); 12001556Srgrimes cmdtxt(n->nredir.n); 12011556Srgrimes cmdputs(")"); 12021556Srgrimes break; 12031556Srgrimes case NREDIR: 12041556Srgrimes case NBACKGND: 12051556Srgrimes cmdtxt(n->nredir.n); 12061556Srgrimes break; 12071556Srgrimes case NIF: 12081556Srgrimes cmdputs("if "); 12091556Srgrimes cmdtxt(n->nif.test); 12101556Srgrimes cmdputs("; then "); 12111556Srgrimes cmdtxt(n->nif.ifpart); 12121556Srgrimes cmdputs("..."); 12131556Srgrimes break; 12141556Srgrimes case NWHILE: 12151556Srgrimes cmdputs("while "); 12161556Srgrimes goto until; 12171556Srgrimes case NUNTIL: 12181556Srgrimes cmdputs("until "); 12191556Srgrimesuntil: 12201556Srgrimes cmdtxt(n->nbinary.ch1); 12211556Srgrimes cmdputs("; do "); 12221556Srgrimes cmdtxt(n->nbinary.ch2); 12231556Srgrimes cmdputs("; done"); 12241556Srgrimes break; 12251556Srgrimes case NFOR: 12261556Srgrimes cmdputs("for "); 12271556Srgrimes cmdputs(n->nfor.var); 12281556Srgrimes cmdputs(" in ..."); 12291556Srgrimes break; 12301556Srgrimes case NCASE: 12311556Srgrimes cmdputs("case "); 12321556Srgrimes cmdputs(n->ncase.expr->narg.text); 12331556Srgrimes cmdputs(" in ..."); 12341556Srgrimes break; 12351556Srgrimes case NDEFUN: 12361556Srgrimes cmdputs(n->narg.text); 12371556Srgrimes cmdputs("() ..."); 12381556Srgrimes break; 12391556Srgrimes case NCMD: 12401556Srgrimes for (np = n->ncmd.args ; np ; np = np->narg.next) { 12411556Srgrimes cmdtxt(np); 12421556Srgrimes if (np->narg.next) 12431556Srgrimes cmdputs(" "); 12441556Srgrimes } 12451556Srgrimes for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 12461556Srgrimes cmdputs(" "); 12471556Srgrimes cmdtxt(np); 12481556Srgrimes } 12491556Srgrimes break; 12501556Srgrimes case NARG: 12511556Srgrimes cmdputs(n->narg.text); 12521556Srgrimes break; 12531556Srgrimes case NTO: 12541556Srgrimes p = ">"; i = 1; goto redir; 12551556Srgrimes case NAPPEND: 12561556Srgrimes p = ">>"; i = 1; goto redir; 12571556Srgrimes case NTOFD: 12581556Srgrimes p = ">&"; i = 1; goto redir; 125996922Stjr case NCLOBBER: 126096922Stjr p = ">|"; i = 1; goto redir; 12611556Srgrimes case NFROM: 12621556Srgrimes p = "<"; i = 0; goto redir; 126366612Sbrian case NFROMTO: 126466612Sbrian p = "<>"; i = 0; goto redir; 12651556Srgrimes case NFROMFD: 12661556Srgrimes p = "<&"; i = 0; goto redir; 12671556Srgrimesredir: 12681556Srgrimes if (n->nfile.fd != i) { 12691556Srgrimes s[0] = n->nfile.fd + '0'; 12701556Srgrimes s[1] = '\0'; 12711556Srgrimes cmdputs(s); 12721556Srgrimes } 12731556Srgrimes cmdputs(p); 12741556Srgrimes if (n->type == NTOFD || n->type == NFROMFD) { 127599634Stjr if (n->ndup.dupfd >= 0) 127699634Stjr s[0] = n->ndup.dupfd + '0'; 127799634Stjr else 127899634Stjr s[0] = '-'; 12791556Srgrimes s[1] = '\0'; 12801556Srgrimes cmdputs(s); 12811556Srgrimes } else { 12821556Srgrimes cmdtxt(n->nfile.fname); 12831556Srgrimes } 12841556Srgrimes break; 12851556Srgrimes case NHERE: 12861556Srgrimes case NXHERE: 12871556Srgrimes cmdputs("<<..."); 12881556Srgrimes break; 12891556Srgrimes default: 12901556Srgrimes cmdputs("???"); 12911556Srgrimes break; 12921556Srgrimes } 12931556Srgrimes} 12941556Srgrimes 12951556Srgrimes 12961556Srgrimes 1297213811Sobrienstatic void 1298201053Sjillescmdputs(const char *s) 129990111Simp{ 1300201053Sjilles const char *p; 1301201053Sjilles char *q; 130225222Ssteve char c; 13031556Srgrimes int subtype = 0; 13041556Srgrimes 13051556Srgrimes if (cmdnleft <= 0) 13061556Srgrimes return; 13071556Srgrimes p = s; 13081556Srgrimes q = cmdnextc; 13091556Srgrimes while ((c = *p++) != '\0') { 13101556Srgrimes if (c == CTLESC) 13111556Srgrimes *q++ = *p++; 13121556Srgrimes else if (c == CTLVAR) { 13131556Srgrimes *q++ = '$'; 13141556Srgrimes if (--cmdnleft > 0) 13151556Srgrimes *q++ = '{'; 13161556Srgrimes subtype = *p++; 1317216246Sjilles if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0) 1318216246Sjilles *q++ = '#'; 13191556Srgrimes } else if (c == '=' && subtype != 0) { 1320216246Sjilles *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL]; 1321216246Sjilles if (*q) 1322216246Sjilles q++; 1323216246Sjilles else 1324216246Sjilles cmdnleft++; 1325216246Sjilles if (((subtype & VSTYPE) == VSTRIMLEFTMAX || 1326216246Sjilles (subtype & VSTYPE) == VSTRIMRIGHTMAX) && 1327216246Sjilles --cmdnleft > 0) 1328216246Sjilles *q = q[-1], q++; 13291556Srgrimes subtype = 0; 13301556Srgrimes } else if (c == CTLENDVAR) { 13311556Srgrimes *q++ = '}'; 1332216246Sjilles } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) { 1333216246Sjilles cmdnleft -= 5; 1334216246Sjilles if (cmdnleft > 0) { 1335216246Sjilles *q++ = '$'; 1336216246Sjilles *q++ = '('; 1337216246Sjilles *q++ = '.'; 1338216246Sjilles *q++ = '.'; 1339216246Sjilles *q++ = '.'; 1340216246Sjilles *q++ = ')'; 1341216246Sjilles } 1342216246Sjilles } else if (c == CTLARI) { 1343216246Sjilles cmdnleft -= 2; 1344216246Sjilles if (cmdnleft > 0) { 1345216246Sjilles *q++ = '$'; 1346216246Sjilles *q++ = '('; 1347216246Sjilles *q++ = '('; 1348216246Sjilles } 1349216246Sjilles p++; 1350216246Sjilles } else if (c == CTLENDARI) { 1351216246Sjilles if (--cmdnleft > 0) { 1352216246Sjilles *q++ = ')'; 1353216246Sjilles *q++ = ')'; 1354216246Sjilles } 1355216246Sjilles } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND) 1356216246Sjilles cmdnleft++; /* ignore */ 13571556Srgrimes else 13581556Srgrimes *q++ = c; 13591556Srgrimes if (--cmdnleft <= 0) { 13601556Srgrimes *q++ = '.'; 13611556Srgrimes *q++ = '.'; 13621556Srgrimes *q++ = '.'; 13631556Srgrimes break; 13641556Srgrimes } 13651556Srgrimes } 13661556Srgrimes cmdnextc = q; 13671556Srgrimes} 1368