1 /* $OpenBSD: jobs.c,v 1.38 2009/12/12 04:28:44 deraadt Exp $ */
4 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011
5 * Thorsten Glaser <tg@mirbsd.org>
7 * Provided that these terms and disclaimer and all copyright notices
8 * are retained or reproduced in an accompanying document, permission
9 * is granted to deal in this work without restriction, including un-
10 * limited rights to use, publicly perform, distribute, sell, modify,
11 * merge, give away, or sublicence.
13 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
14 * the utmost extent permitted by applicable law, neither express nor
15 * implied; without malicious intent or gross negligence. In no event
16 * may a licensor, author or contributor be held liable for indirect,
17 * direct, other damage, loss, or other issues arising in any way out
18 * of dealing in the work, even if advised of the possibility of such
19 * damage or existence of a defect, except proven that it results out
20 * of said person's immediate fault when using the work as intended.
25 __RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.81 2011/08/27 18:06:46 tg Exp $");
28 #define mksh_killpg killpg
30 /* cross fingers and hope kill is killpg-endowed */
31 #define mksh_killpg(p,s) kill(-(p), (s))
34 /* Order important! */
40 typedef struct proc Proc;
42 Proc *next; /* next process in pipeline (if any) */
43 pid_t pid; /* process id */
45 int status; /* wait status */
46 char command[48]; /* process command string */
49 /* Notify/print flag - j_print() argument */
50 #define JP_NONE 0 /* don't print anything */
51 #define JP_SHORT 1 /* print signals processes were killed by */
52 #define JP_MEDIUM 2 /* print [job-num] -/+ command */
53 #define JP_LONG 3 /* print [job-num] -/+ pid command */
54 #define JP_PGRP 4 /* print pgrp */
57 #define PJ_ON_FRONT 0 /* at very front */
58 #define PJ_PAST_STOPPED 1 /* just past any stopped jobs */
60 /* Job.flags values */
61 #define JF_STARTED 0x001 /* set when all processes in job are started */
62 #define JF_WAITING 0x002 /* set if j_waitj() is waiting on job */
63 #define JF_W_ASYNCNOTIFY 0x004 /* set if waiting and async notification ok */
64 #define JF_XXCOM 0x008 /* set for $(command) jobs */
65 #define JF_FG 0x010 /* running in foreground (also has tty pgrp) */
66 #define JF_SAVEDTTY 0x020 /* j->ttystat is valid */
67 #define JF_CHANGED 0x040 /* process has changed state */
68 #define JF_KNOWN 0x080 /* $! referenced */
69 #define JF_ZOMBIE 0x100 /* known, unwaited process */
70 #define JF_REMOVE 0x200 /* flagged for removal (j_jobs()/j_noityf()) */
71 #define JF_USETTYMODE 0x400 /* tty mode saved if process exits normally */
72 #define JF_SAVEDTTYPGRP 0x800 /* j->saved_ttypgrp is valid */
74 typedef struct job Job;
76 Job *next; /* next job in list */
77 Proc *proc_list; /* process list */
78 Proc *last_proc; /* last process in list */
79 struct timeval systime; /* system time used by job */
80 struct timeval usrtime; /* user time used by job */
81 pid_t pgrp; /* process group of job */
82 pid_t ppid; /* pid of process that forked job */
83 int job; /* job number: %n */
84 int flags; /* see JF_* */
85 volatile int state; /* job state */
86 int status; /* exit status of last process */
87 int32_t age; /* number of jobs started */
88 Coproc_id coproc_id; /* 0 or id of coprocess output pipe */
89 #ifndef MKSH_UNEMPLOYED
90 struct termios ttystat; /* saved tty state for stopped jobs */
91 pid_t saved_ttypgrp; /* saved tty process group for stopped jobs */
95 /* Flags for j_waitj() */
97 #define JW_INTERRUPT 0x01 /* ^C will stop the wait */
98 #define JW_ASYNCNOTIFY 0x02 /* asynchronous notification during wait ok */
99 #define JW_STOPPEDWAIT 0x04 /* wait even if job stopped */
100 #define JW_PIPEST 0x08 /* want PIPESTATUS */
102 /* Error codes for j_lookup() */
104 #define JL_NOSUCH 1 /* no such job */
105 #define JL_AMBIG 2 /* %foo or %?foo is ambiguous */
106 #define JL_INVALID 3 /* non-pid, non-% job id */
108 static const char *const lookup_msgs[] = {
112 "argument must be %job or process id",
116 static Job *job_list; /* job list */
117 static Job *last_job;
118 static Job *async_job;
119 static pid_t async_pid;
121 static int nzombie; /* # of zombies owned by this process */
122 static int32_t njobs; /* # of jobs started */
128 #ifndef MKSH_NOPROSPECTOFWORK
129 /* held_sigchld is set if sigchld occurs before a job is completely started */
130 static volatile sig_atomic_t held_sigchld;
133 #ifndef MKSH_UNEMPLOYED
134 static struct shf *shl_j;
135 static bool ttypgrp_ok; /* set if can use tty pgrps */
136 static pid_t restore_ttypgrp = -1;
137 static int const tt_sigs[] = { SIGTSTP, SIGTTIN, SIGTTOU };
140 static void j_set_async(Job *);
141 static void j_startjob(Job *);
142 static int j_waitj(Job *, int, const char *);
143 static void j_sigchld(int);
144 static void j_print(Job *, int, struct shf *);
145 static Job *j_lookup(const char *, int *);
146 static Job *new_job(void);
147 static Proc *new_proc(void);
148 static void check_job(Job *);
149 static void put_job(Job *, int);
150 static void remove_job(Job *, const char *);
151 static int kill_job(Job *, int);
153 /* initialise job control */
157 #ifndef MKSH_UNEMPLOYED
158 bool mflagset = Flag(FMONITOR) != 127;
163 #ifndef MKSH_NOPROSPECTOFWORK
164 (void)sigemptyset(&sm_default);
165 sigprocmask(SIG_SETMASK, &sm_default, NULL);
167 (void)sigemptyset(&sm_sigchld);
168 (void)sigaddset(&sm_sigchld, SIGCHLD);
170 setsig(&sigtraps[SIGCHLD], j_sigchld,
171 SS_RESTORE_ORIG|SS_FORCE|SS_SHTRAP);
173 /* Make sure SIGCHLD isn't ignored - can do odd things under SYSV */
174 setsig(&sigtraps[SIGCHLD], SIG_DFL, SS_RESTORE_ORIG|SS_FORCE);
177 #ifndef MKSH_UNEMPLOYED
178 if (!mflagset && Flag(FTALKING))
182 * shl_j is used to do asynchronous notification (used in
183 * an interrupt handler, so need a distinct shf)
185 shl_j = shf_fdopen(2, SHF_WR, NULL);
187 if (Flag(FMONITOR) || Flag(FTALKING)) {
191 * the TF_SHELL_USES test is a kludge that lets us know if
192 * if the signals have been changed by the shell.
194 for (i = NELEM(tt_sigs); --i >= 0; ) {
195 sigtraps[tt_sigs[i]].flags |= TF_SHELL_USES;
196 /* j_change() sets this to SS_RESTORE_DFL if FMONITOR */
197 setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
198 SS_RESTORE_IGN|SS_FORCE);
202 /* j_change() calls tty_init() */
208 tty_init(true, true);
212 proc_errorlevel(Proc *p)
216 return (WEXITSTATUS(p->status));
218 return (128 + WTERMSIG(p->status));
224 /* job cleanup before shell exit */
228 /* kill stopped, and possibly running, jobs */
232 for (j = job_list; j != NULL; j = j->next) {
233 if (j->ppid == procpid &&
234 (j->state == PSTOPPED ||
235 (j->state == PRUNNING &&
236 ((j->flags & JF_FG) ||
237 (Flag(FLOGIN) && !Flag(FNOHUP) && procpid == kshpid))))) {
242 mksh_killpg(j->pgrp, SIGHUP);
243 #ifndef MKSH_UNEMPLOYED
244 if (j->state == PSTOPPED) {
246 kill_job(j, SIGCONT);
248 mksh_killpg(j->pgrp, SIGCONT);
257 #ifndef MKSH_UNEMPLOYED
258 if (kshpid == procpid && restore_ttypgrp >= 0) {
260 * Need to restore the tty pgrp to what it was when the
261 * shell started up, so that the process that started us
262 * will be able to access the tty when we are done.
263 * Also need to restore our process group in case we are
264 * about to do an exec so that both our parent and the
265 * process we are to become will be able to access the tty.
267 tcsetpgrp(tty_fd, restore_ttypgrp);
268 setpgid(0, restore_ttypgrp);
270 if (Flag(FMONITOR)) {
277 #ifndef MKSH_UNEMPLOYED
278 /* turn job control on or off according to Flag(FMONITOR) */
284 if (Flag(FMONITOR)) {
285 bool use_tty = Flag(FTALKING);
287 /* Don't call tcgetattr() 'til we own the tty process group */
289 tty_init(false, true);
291 /* no controlling tty, no SIGT* */
292 if ((ttypgrp_ok = use_tty && tty_fd >= 0 && tty_devtty)) {
293 setsig(&sigtraps[SIGTTIN], SIG_DFL,
294 SS_RESTORE_ORIG|SS_FORCE);
295 /* wait to be given tty (POSIX.1, B.2, job control) */
296 while (/* CONSTCOND */ 1) {
299 if ((ttypgrp = tcgetpgrp(tty_fd)) < 0) {
300 warningf(false, "%s: %s %s: %s",
301 "j_init", "tcgetpgrp", "failed",
306 if (ttypgrp == kshpgrp)
311 for (i = NELEM(tt_sigs); --i >= 0; )
312 setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
313 SS_RESTORE_DFL|SS_FORCE);
314 if (ttypgrp_ok && kshpgrp != kshpid) {
315 if (setpgid(0, kshpid) < 0) {
316 warningf(false, "%s: %s %s: %s", "j_init",
317 "setpgid", "failed", strerror(errno));
320 if (tcsetpgrp(tty_fd, kshpid) < 0) {
321 warningf(false, "%s: %s %s: %s",
322 "j_init", "tcsetpgrp", "failed",
326 restore_ttypgrp = kshpgrp;
330 if (use_tty && !ttypgrp_ok)
331 warningf(false, "%s: %s", "warning",
332 "won't have full job control");
334 tcgetattr(tty_fd, &tty_state);
338 for (i = NELEM(tt_sigs); --i >= 0; )
339 setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
340 SS_RESTORE_IGN|SS_FORCE);
342 for (i = NELEM(tt_sigs); --i >= 0; ) {
343 if (sigtraps[tt_sigs[i]].flags &
344 (TF_ORIG_IGN | TF_ORIG_DFL))
345 setsig(&sigtraps[tt_sigs[i]],
346 (sigtraps[tt_sigs[i]].flags & TF_ORIG_IGN) ?
348 SS_RESTORE_ORIG|SS_FORCE);
357 /* run nice(3) and ignore the result */
361 #if defined(__USE_FORTIFY_LEVEL) && (__USE_FORTIFY_LEVEL > 0)
365 /* this is gonna annoy users; complain to your distro, people! */
366 if (nice(ness) == -1 && (e = errno) != 0)
367 warningf(false, "%s: %s", "bgnice", strerror(e));
374 /* execute tree in child subprocess */
376 exchild(struct op *t, int flags,
377 volatile int *xerrok,
378 /* used if XPCLOSE or XCCLOSE */
382 static Proc *last_proc;
384 int rv = 0, forksleep, jwflags = JW_NONE;
385 #ifndef MKSH_NOPROSPECTOFWORK
392 if (flags & XPIPEST) {
394 jwflags |= JW_PIPEST;
399 * Clear XFORK|XPCLOSE|XCCLOSE|XCOPROC|XPIPEO|XPIPEI|XXCOM|XBGND
400 * (also done in another execute() below)
402 return (execute(t, flags & (XEXEC | XERROK), xerrok));
404 #ifndef MKSH_NOPROSPECTOFWORK
405 /* no SIGCHLDs while messing with job and process lists */
406 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
415 /* link process into jobs list */
416 if (flags & XPIPEI) {
417 /* continuing with a pipe */
419 internal_errorf("%s %d",
420 "exchild: XPIPEI and no last_job - pid",
427 /* fills in j->job */
430 * we don't consider XXCOMs foreground since they don't get
431 * tty process group and we don't save or restore tty modes.
433 j->flags = (flags & XXCOM) ? JF_XXCOM :
434 ((flags & XBGND) ? 0 : (JF_FG|JF_USETTYMODE));
435 timerclear(&j->usrtime);
436 timerclear(&j->systime);
445 put_job(j, PJ_PAST_STOPPED);
448 vistree(p->command, sizeof(p->command), t);
450 /* create child process */
452 while ((cldpid = fork()) < 0 && errno == EAGAIN && forksleep < 32) {
454 /* allow user to ^C out... */
459 /* ensure $RANDOM changes between parent and child */
460 rndset((long)cldpid);
463 kill_job(j, SIGKILL);
464 remove_job(j, "fork failed");
465 #ifndef MKSH_NOPROSPECTOFWORK
466 sigprocmask(SIG_SETMASK, &omask, NULL);
468 errorf("can't fork - try again");
470 p->pid = cldpid ? cldpid : (procpid = getpid());
472 #ifndef MKSH_UNEMPLOYED
473 /* job control set up */
474 if (Flag(FMONITOR) && !(flags&XXCOM)) {
483 * set pgrp in both parent and child to deal with race
486 setpgid(p->pid, j->pgrp);
487 if (ttypgrp_ok && dotty && !(flags & XBGND))
488 tcsetpgrp(tty_fd, j->pgrp);
492 /* used to close pipe input fd */
493 if (close_fd >= 0 && (((flags & XPCLOSE) && cldpid) ||
494 ((flags & XCCLOSE) && !cldpid)))
499 /* Do this before restoring signal */
501 coproc_cleanup(false);
502 #ifndef MKSH_NOPROSPECTOFWORK
503 sigprocmask(SIG_SETMASK, &omask, NULL);
505 cleanup_parents_env();
506 #ifndef MKSH_UNEMPLOYED
508 * If FMONITOR or FTALKING is set, these signals are ignored,
509 * if neither FMONITOR nor FTALKING are set, the signals have
510 * their inherited values.
512 if (Flag(FMONITOR) && !(flags & XXCOM)) {
513 for (forksleep = NELEM(tt_sigs); --forksleep >= 0; )
514 setsig(&sigtraps[tt_sigs[forksleep]], SIG_DFL,
515 SS_RESTORE_DFL|SS_FORCE);
519 if (Flag(FBGNICE) && (flags & XBGND))
523 #ifndef MKSH_UNEMPLOYED
527 setsig(&sigtraps[SIGINT], SIG_IGN,
528 SS_RESTORE_IGN|SS_FORCE);
529 setsig(&sigtraps[SIGQUIT], SIG_IGN,
530 SS_RESTORE_IGN|SS_FORCE);
531 if ((!(flags & (XPIPEI | XCOPROC))) &&
532 ((forksleep = open("/dev/null", 0)) > 0)) {
533 (void)ksh_dup2(forksleep, 0, true);
537 /* in case of $(jobs) command */
538 remove_job(j, "child");
540 #ifndef MKSH_UNEMPLOYED
548 execute(t, (flags & XERROK) | XEXEC, NULL);
550 if (t->type == TPIPE)
552 internal_warningf("%s: %s", "exchild", "execute() returned");
553 fptreef(shl_out, 8, "%s: tried to execute {\n\t%T\n}\n",
561 /* shell (parent) stuff */
562 if (!(flags & XPIPEO)) {
563 /* last process in a job */
565 if (flags & XCOPROC) {
566 j->coproc_id = coproc.id;
567 /* n jobs using co-process output */
569 /* j using co-process input */
570 coproc.job = (void *)j;
574 if (Flag(FTALKING)) {
575 shf_fprintf(shl_out, "[%d]", j->job);
576 for (p = j->proc_list; p; p = p->next)
577 shf_fprintf(shl_out, " %d",
579 shf_putchar('\n', shl_out);
583 rv = j_waitj(j, jwflags, "jw:last proc");
586 #ifndef MKSH_NOPROSPECTOFWORK
587 sigprocmask(SIG_SETMASK, &omask, NULL);
593 /* start the last job: only used for $(command) jobs */
597 #ifndef MKSH_NOPROSPECTOFWORK
600 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
603 /* no need to report error - waitlast() will do it */
605 /* ensure it isn't removed by check_job() */
606 last_job->flags |= JF_WAITING;
607 j_startjob(last_job);
609 #ifndef MKSH_NOPROSPECTOFWORK
610 sigprocmask(SIG_SETMASK, &omask, NULL);
614 /* wait for last job: only used for $(command) jobs */
620 #ifndef MKSH_NOPROSPECTOFWORK
623 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
627 if (!j || !(j->flags & JF_STARTED)) {
629 warningf(true, "%s: %s", "waitlast", "no last job");
631 internal_warningf("%s: %s", "waitlast", "not started");
632 #ifndef MKSH_NOPROSPECTOFWORK
633 sigprocmask(SIG_SETMASK, &omask, NULL);
635 /* not so arbitrary, non-zero value */
639 rv = j_waitj(j, JW_NONE, "waitlast");
641 #ifndef MKSH_NOPROSPECTOFWORK
642 sigprocmask(SIG_SETMASK, &omask, NULL);
648 /* wait for child, interruptable. */
650 waitfor(const char *cp, int *sigp)
652 int rv, ecode, flags = JW_INTERRUPT|JW_ASYNCNOTIFY;
654 #ifndef MKSH_NOPROSPECTOFWORK
657 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
664 * wait for an unspecified job - always returns 0, so
665 * don't have to worry about exited/signaled jobs
667 for (j = job_list; j; j = j->next)
668 /* AT&T ksh will wait for stopped jobs - we don't */
669 if (j->ppid == procpid && j->state == PRUNNING)
672 #ifndef MKSH_NOPROSPECTOFWORK
673 sigprocmask(SIG_SETMASK, &omask, NULL);
677 } else if ((j = j_lookup(cp, &ecode))) {
678 /* don't report normal job completion */
679 flags &= ~JW_ASYNCNOTIFY;
680 if (j->ppid != procpid) {
681 #ifndef MKSH_NOPROSPECTOFWORK
682 sigprocmask(SIG_SETMASK, &omask, NULL);
687 #ifndef MKSH_NOPROSPECTOFWORK
688 sigprocmask(SIG_SETMASK, &omask, NULL);
690 if (ecode != JL_NOSUCH)
691 bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
695 /* AT&T ksh will wait for stopped jobs - we don't */
696 rv = j_waitj(j, flags, "jw:waitfor");
698 #ifndef MKSH_NOPROSPECTOFWORK
699 sigprocmask(SIG_SETMASK, &omask, NULL);
703 /* we were interrupted */
709 /* kill (built-in) a job */
711 j_kill(const char *cp, int sig)
715 #ifndef MKSH_NOPROSPECTOFWORK
718 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
721 if ((j = j_lookup(cp, &ecode)) == NULL) {
722 #ifndef MKSH_NOPROSPECTOFWORK
723 sigprocmask(SIG_SETMASK, &omask, NULL);
725 bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
730 /* started when !Flag(FMONITOR) */
731 if (kill_job(j, sig) < 0) {
732 bi_errorf("%s: %s", cp, strerror(errno));
736 #ifndef MKSH_UNEMPLOYED
737 if (j->state == PSTOPPED && (sig == SIGTERM || sig == SIGHUP))
738 mksh_killpg(j->pgrp, SIGCONT);
740 if (mksh_killpg(j->pgrp, sig) < 0) {
741 bi_errorf("%s: %s", cp, strerror(errno));
746 #ifndef MKSH_NOPROSPECTOFWORK
747 sigprocmask(SIG_SETMASK, &omask, NULL);
753 #ifndef MKSH_UNEMPLOYED
754 /* fg and bg built-ins: called only if Flag(FMONITOR) set */
756 j_resume(const char *cp, int bg)
764 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
766 if ((j = j_lookup(cp, &ecode)) == NULL) {
767 sigprocmask(SIG_SETMASK, &omask, NULL);
768 bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
773 sigprocmask(SIG_SETMASK, &omask, NULL);
774 bi_errorf("job not job-controlled");
779 shprintf("[%d] ", j->job);
782 for (p = j->proc_list; p != NULL; p = p->next) {
783 if (p->state == PSTOPPED) {
788 shf_puts(p->command, shl_stdout);
790 shf_puts("| ", shl_stdout);
792 shf_putc('\n', shl_stdout);
793 shf_flush(shl_stdout);
797 put_job(j, PJ_PAST_STOPPED);
801 /* attach tty to job */
802 if (j->state == PRUNNING) {
803 if (ttypgrp_ok && (j->flags & JF_SAVEDTTY))
804 tcsetattr(tty_fd, TCSADRAIN, &j->ttystat);
805 /* See comment in j_waitj regarding saved_ttypgrp. */
807 tcsetpgrp(tty_fd, (j->flags & JF_SAVEDTTYPGRP) ?
808 j->saved_ttypgrp : j->pgrp) < 0) {
810 if (j->flags & JF_SAVEDTTY)
811 tcsetattr(tty_fd, TCSADRAIN, &tty_state);
812 sigprocmask(SIG_SETMASK, &omask, NULL);
813 bi_errorf("%s %s(%d, %ld) %s: %s",
814 "1st", "tcsetpgrp", tty_fd,
815 (long)((j->flags & JF_SAVEDTTYPGRP) ?
816 j->saved_ttypgrp : j->pgrp), "failed",
822 j->flags &= ~JF_KNOWN;
827 if (j->state == PRUNNING && mksh_killpg(j->pgrp, SIGCONT) < 0) {
832 if (ttypgrp_ok && (j->flags & JF_SAVEDTTY))
833 tcsetattr(tty_fd, TCSADRAIN, &tty_state);
834 if (ttypgrp_ok && tcsetpgrp(tty_fd, kshpgrp) < 0)
835 warningf(true, "%s %s(%d, %ld) %s: %s",
836 "fg: 2nd", "tcsetpgrp", tty_fd,
837 (long)kshpgrp, "failed", strerror(errno));
839 sigprocmask(SIG_SETMASK, &omask, NULL);
840 bi_errorf("%s %s %s", "can't continue job",
846 j->flags &= ~(JF_SAVEDTTY | JF_SAVEDTTYPGRP);
848 rv = j_waitj(j, JW_NONE, "jw:resume");
850 sigprocmask(SIG_SETMASK, &omask, NULL);
855 /* are there any running or stopped jobs ? */
857 j_stopped_running(void)
862 for (j = job_list; j != NULL; j = j->next) {
863 #ifndef MKSH_UNEMPLOYED
864 if (j->ppid == procpid && j->state == PSTOPPED)
867 if (Flag(FLOGIN) && !Flag(FNOHUP) && procpid == kshpid &&
868 j->ppid == procpid && j->state == PRUNNING)
872 shellf("You have %s%s%s jobs\n",
873 which & 1 ? "stopped" : "",
874 which == 3 ? " and " : "",
875 which & 2 ? "running" : "");
883 /* list jobs for jobs built-in */
885 j_jobs(const char *cp, int slp,
886 /* 0: short, 1: long, 2: pgrp */
891 #ifndef MKSH_NOPROSPECTOFWORK
894 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
898 /* kludge: print zombies */
905 if ((j = j_lookup(cp, &ecode)) == NULL) {
906 #ifndef MKSH_NOPROSPECTOFWORK
907 sigprocmask(SIG_SETMASK, &omask, NULL);
909 bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
914 how = slp == 0 ? JP_MEDIUM : (slp == 1 ? JP_LONG : JP_PGRP);
915 for (; j; j = j->next) {
916 if ((!(j->flags & JF_ZOMBIE) || zflag) &&
917 (!nflag || (j->flags & JF_CHANGED))) {
918 j_print(j, how, shl_stdout);
919 if (j->state == PEXITED || j->state == PSIGNALLED)
920 j->flags |= JF_REMOVE;
925 /* Remove jobs after printing so there won't be multiple + or - jobs */
926 for (j = job_list; j; j = tmp) {
928 if (j->flags & JF_REMOVE)
929 remove_job(j, "jobs");
931 #ifndef MKSH_NOPROSPECTOFWORK
932 sigprocmask(SIG_SETMASK, &omask, NULL);
937 /* list jobs for top-level notification */
942 #ifndef MKSH_NOPROSPECTOFWORK
945 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
947 for (j = job_list; j; j = j->next) {
948 #ifndef MKSH_UNEMPLOYED
949 if (Flag(FMONITOR) && (j->flags & JF_CHANGED))
950 j_print(j, JP_MEDIUM, shl_out);
953 * Remove job after doing reports so there aren't
956 if (j->state == PEXITED || j->state == PSIGNALLED)
957 j->flags |= JF_REMOVE;
959 for (j = job_list; j; j = tmp) {
961 if (j->flags & JF_REMOVE)
962 remove_job(j, "notify");
965 #ifndef MKSH_NOPROSPECTOFWORK
966 sigprocmask(SIG_SETMASK, &omask, NULL);
970 /* Return pid of last process in last asynchronous job */
974 #ifndef MKSH_NOPROSPECTOFWORK
977 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
981 async_job->flags |= JF_KNOWN;
983 #ifndef MKSH_NOPROSPECTOFWORK
984 sigprocmask(SIG_SETMASK, &omask, NULL);
991 * Make j the last async process
993 * If jobs are compiled in then this routine expects sigchld to be blocked.
1000 if (async_job && (async_job->flags & (JF_KNOWN|JF_ZOMBIE)) == JF_ZOMBIE)
1001 remove_job(async_job, "async");
1002 if (!(j->flags & JF_STARTED)) {
1003 internal_warningf("%s: %s", "j_async", "job not started");
1007 async_pid = j->last_proc->pid;
1008 while (nzombie > CHILD_MAX) {
1010 for (jl = job_list; jl; jl = jl->next)
1011 if (jl != async_job && (jl->flags & JF_ZOMBIE) &&
1012 (!oldest || jl->age < oldest->age))
1016 if (!(async_job->flags & JF_ZOMBIE) || nzombie != 1) {
1017 internal_warningf("%s: bad nzombie (%d)",
1018 "j_async", nzombie);
1023 remove_job(oldest, "zombie");
1028 * Start a job: set STARTED, check for held signals and set j->last_proc
1030 * If jobs are compiled in then this routine expects sigchld to be blocked.
1037 j->flags |= JF_STARTED;
1038 for (p = j->proc_list; p->next; p = p->next)
1042 #ifndef MKSH_NOPROSPECTOFWORK
1045 /* Don't call j_sigchld() as it may remove job... */
1046 kill(procpid, SIGCHLD);
1052 * wait for job to complete or change state
1054 * If jobs are compiled in then this routine expects sigchld to be blocked.
1065 * No auto-notify on the job we are waiting on.
1067 j->flags |= JF_WAITING;
1068 if (flags & JW_ASYNCNOTIFY)
1069 j->flags |= JF_W_ASYNCNOTIFY;
1071 #ifndef MKSH_UNEMPLOYED
1072 if (!Flag(FMONITOR))
1074 flags |= JW_STOPPEDWAIT;
1076 while (j->state == PRUNNING ||
1077 ((flags & JW_STOPPEDWAIT) && j->state == PSTOPPED)) {
1078 #ifndef MKSH_NOPROSPECTOFWORK
1079 sigsuspend(&sm_default);
1084 int oldf = j->flags & (JF_WAITING|JF_W_ASYNCNOTIFY);
1085 j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
1087 /* not reached... */
1090 if ((flags & JW_INTERRUPT) && (rv = trap_pending())) {
1091 j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
1095 j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
1097 if (j->flags & JF_FG) {
1099 #ifndef MKSH_UNEMPLOYED
1100 if (Flag(FMONITOR) && ttypgrp_ok && j->pgrp) {
1102 * Save the tty's current pgrp so it can be restored
1103 * when the job is foregrounded. This is to
1104 * deal with things like the GNU su which does
1105 * a fork/exec instead of an exec (the fork means
1106 * the execed shell gets a different pid from its
1107 * pgrp, so naturally it sets its pgrp and gets hosed
1108 * when it gets foregrounded by the parent shell which
1109 * has restored the tty's pgrp to that of the su
1112 if (j->state == PSTOPPED &&
1113 (j->saved_ttypgrp = tcgetpgrp(tty_fd)) >= 0)
1114 j->flags |= JF_SAVEDTTYPGRP;
1115 if (tcsetpgrp(tty_fd, kshpgrp) < 0)
1116 warningf(true, "%s %s(%d, %ld) %s: %s",
1117 "j_waitj:", "tcsetpgrp", tty_fd,
1118 (long)kshpgrp, "failed", strerror(errno));
1119 if (j->state == PSTOPPED) {
1120 j->flags |= JF_SAVEDTTY;
1121 tcgetattr(tty_fd, &j->ttystat);
1127 * Only restore tty settings if job was originally
1128 * started in the foreground. Problems can be
1129 * caused by things like 'more foobar &' which will
1130 * typically get and save the shell's vi/emacs tty
1131 * settings before setting up the tty for itself;
1132 * when more exits, it restores the 'original'
1133 * settings, and things go down hill from there...
1135 if (j->state == PEXITED && j->status == 0 &&
1136 (j->flags & JF_USETTYMODE)) {
1137 tcgetattr(tty_fd, &tty_state);
1139 tcsetattr(tty_fd, TCSADRAIN, &tty_state);
1141 * Don't use tty mode if job is stopped and
1142 * later restarted and exits. Consider
1149 * mode should be that of the stty, not what
1150 * was before the vi started.
1152 if (j->state == PSTOPPED)
1153 j->flags &= ~JF_USETTYMODE;
1156 #ifndef MKSH_UNEMPLOYED
1158 * If it looks like user hit ^C to kill a job, pretend we got
1159 * one too to break out of for loops, etc. (AT&T ksh does this
1160 * even when not monitoring, but this doesn't make sense since
1161 * a tty generated ^C goes to the whole process group)
1166 status = j->last_proc->status;
1167 if (Flag(FMONITOR) && j->state == PSIGNALLED &&
1168 WIFSIGNALED(status) &&
1169 (sigtraps[WTERMSIG(status)].flags & TF_TTY_INTR))
1170 trapsig(WTERMSIG(status));
1175 j_usrtime = j->usrtime;
1176 j_systime = j->systime;
1179 if ((flags & JW_PIPEST) && (j->proc_list != NULL)) {
1181 Proc *p = j->proc_list;
1184 unset(vp_pipest, 1);
1186 vp->flag = DEFINED | ISSET | INTEGER | RDONLY | ARRAY | INT_U;
1193 /* strlen(vp_pipest->name) == 10 */
1194 vq = alloc(offsetof(struct tbl, name[0]) + 11,
1196 memset(vq, 0, offsetof(struct tbl, name[0]));
1197 memcpy(vq->name, vp_pipest->name, 11);
1201 vp->areap = vp_pipest->areap;
1202 vp->ua.index = ++num;
1203 vp->flag = DEFINED | ISSET | INTEGER | RDONLY |
1204 ARRAY | INT_U | AINDEX;
1206 vp->val.i = proc_errorlevel(p);
1211 if (!(flags & JW_ASYNCNOTIFY)
1212 #ifndef MKSH_UNEMPLOYED
1213 && (!Flag(FMONITOR) || j->state != PSTOPPED)
1216 j_print(j, JP_SHORT, shl_out);
1219 if (j->state != PSTOPPED
1220 #ifndef MKSH_UNEMPLOYED
1221 && (!Flag(FMONITOR) || !(flags & JW_ASYNCNOTIFY))
1224 remove_job(j, where);
1230 * SIGCHLD handler to reap children and update job states
1232 * If jobs are compiled in then this routine expects sigchld to be blocked.
1236 j_sigchld(int sig MKSH_A_UNUSED)
1238 /* this runs inside interrupt context, with errno saved */
1244 struct rusage ru0, ru1;
1246 #ifndef MKSH_NOPROSPECTOFWORK
1248 * Don't wait for any processes if a job is partially started.
1249 * This is so we don't do away with the process group leader
1250 * before all the processes in a pipe line are started (so the
1251 * setpgid() won't fail)
1253 for (j = job_list; j; j = j->next)
1254 if (j->ppid == procpid && !(j->flags & JF_STARTED)) {
1260 getrusage(RUSAGE_CHILDREN, &ru0);
1262 #ifndef MKSH_NOPROSPECTOFWORK
1263 pid = waitpid(-1, &status, (WNOHANG|WUNTRACED));
1265 pid = wait(&status);
1269 * return if this would block (0) or no children
1270 * or interrupted (-1)
1275 getrusage(RUSAGE_CHILDREN, &ru1);
1277 /* find job and process structures for this pid */
1278 for (j = job_list; j != NULL; j = j->next)
1279 for (p = j->proc_list; p != NULL; p = p->next)
1284 /* Can occur if process has kids, then execs shell
1285 warningf(true, "bad process waited for (pid = %d)",
1292 timeradd(&j->usrtime, &ru1.ru_utime, &j->usrtime);
1293 timersub(&j->usrtime, &ru0.ru_utime, &j->usrtime);
1294 timeradd(&j->systime, &ru1.ru_stime, &j->systime);
1295 timersub(&j->systime, &ru0.ru_stime, &j->systime);
1298 #ifndef MKSH_UNEMPLOYED
1299 if (WIFSTOPPED(status))
1300 p->state = PSTOPPED;
1303 if (WIFSIGNALED(status))
1304 p->state = PSIGNALLED;
1308 /* check to see if entire job is done */
1311 #ifndef MKSH_NOPROSPECTOFWORK
1312 while (/* CONSTCOND */ 1);
1314 while (/* CONSTCOND */ 0);
1319 * Called only when a process in j has exited/stopped (ie, called only
1320 * from j_sigchld()). If no processes are running, the job status
1321 * and state are updated, asynchronous job notification is done and,
1322 * if unneeded, the job is removed.
1324 * If jobs are compiled in then this routine expects sigchld to be blocked.
1332 /* XXX debugging (nasty - interrupt routine using shl_out) */
1333 if (!(j->flags & JF_STARTED)) {
1334 internal_warningf("check_job: job started (flags 0x%x)",
1340 for (p=j->proc_list; p != NULL; p = p->next) {
1341 if (p->state == PRUNNING)
1342 /* some processes still running */
1344 if (p->state > jstate)
1348 j->status = proc_errorlevel(j->last_proc);
1351 * Note when co-process dies: can't be done in j_wait() nor
1352 * remove_job() since neither may be called for non-interactive
1355 if (j->state == PEXITED || j->state == PSIGNALLED) {
1357 * No need to keep co-process input any more
1358 * (at least, this is what ksh93d thinks)
1360 if (coproc.job == j) {
1363 * XXX would be nice to get the closes out of here
1364 * so they aren't done in the signal handler.
1365 * Would mean a check in coproc_getfd() to
1366 * do "if job == 0 && write >= 0, close write".
1368 coproc_write_close(coproc.write);
1370 /* Do we need to keep the output? */
1371 if (j->coproc_id && j->coproc_id == coproc.id &&
1372 --coproc.njobs == 0)
1373 coproc_readw_close(coproc.read);
1376 j->flags |= JF_CHANGED;
1377 #ifndef MKSH_UNEMPLOYED
1378 if (Flag(FMONITOR) && !(j->flags & JF_XXCOM)) {
1380 * Only put stopped jobs at the front to avoid confusing
1381 * the user (don't want finished jobs effecting %+ or %-)
1383 if (j->state == PSTOPPED)
1384 put_job(j, PJ_ON_FRONT);
1385 if (Flag(FNOTIFY) &&
1386 (j->flags & (JF_WAITING|JF_W_ASYNCNOTIFY)) != JF_WAITING) {
1387 /* Look for the real file descriptor 2 */
1392 for (ep = e; ep; ep = ep->oenv)
1393 if (ep->savefd && ep->savefd[2])
1395 shf_reopen(fd, SHF_WR, shl_j);
1398 * Can't call j_notify() as it removes jobs. The job
1399 * must stay in the job list as j_waitj() may be
1400 * running with this job.
1402 j_print(j, JP_MEDIUM, shl_j);
1404 if (!(j->flags & JF_WAITING) && j->state != PSTOPPED)
1405 remove_job(j, "notify");
1410 #ifndef MKSH_UNEMPLOYED
1413 !(j->flags & (JF_WAITING|JF_FG)) &&
1414 j->state != PSTOPPED) {
1415 if (j == async_job || (j->flags & JF_KNOWN)) {
1416 j->flags |= JF_ZOMBIE;
1420 remove_job(j, "checkjob");
1425 * Print job status in either short, medium or long format.
1427 * If jobs are compiled in then this routine expects sigchld to be blocked.
1430 j_print(Job *j, int how, struct shf *shf)
1441 if (how == JP_PGRP) {
1443 * POSIX doesn't say what to do it there is no process
1444 * group leader (ie, !FMONITOR). We arbitrarily return
1445 * last pid (which is what $! returns).
1447 shf_fprintf(shf, "%d\n", (int)(j->pgrp ? j->pgrp :
1448 (j->last_proc ? j->last_proc->pid : 0)));
1451 j->flags &= ~JF_CHANGED;
1452 filler = j->job > 10 ? "\n " : "\n ";
1455 else if (j == job_list->next)
1458 for (p = j->proc_list; p != NULL;) {
1462 memcpy(buf, "Running", 8);
1465 strlcpy(buf, sigtraps[WSTOPSIG(p->status)].mess,
1469 if (how == JP_SHORT)
1471 else if (WEXITSTATUS(p->status) == 0)
1472 memcpy(buf, "Done", 5);
1474 shf_snprintf(buf, sizeof(buf), "Done (%d)",
1475 WEXITSTATUS(p->status));
1479 if (WCOREDUMP(p->status))
1483 * kludge for not reporting 'normal termination
1484 * signals' (i.e. SIGINT, SIGPIPE)
1486 if (how == JP_SHORT && !coredumped &&
1487 (WTERMSIG(p->status) == SIGINT ||
1488 WTERMSIG(p->status) == SIGPIPE)) {
1491 strlcpy(buf, sigtraps[WTERMSIG(p->status)].mess,
1496 if (how != JP_SHORT) {
1497 if (p == j->proc_list)
1498 shf_fprintf(shf, "[%d] %c ", j->job, jobchar);
1500 shf_puts(filler, shf);
1504 shf_fprintf(shf, "%5d ", (int)p->pid);
1506 if (how == JP_SHORT) {
1509 shf_fprintf(shf, "%s%s ",
1510 buf, coredumped ? " (core dumped)" : null);
1514 shf_fprintf(shf, "%-20s %s%s%s", buf, p->command,
1515 p->next ? "|" : null,
1516 coredumped ? " (core dumped)" : null);
1522 while (p && p->state == state && p->status == status) {
1524 shf_fprintf(shf, "%s%5d %-20s %s%s", filler,
1525 (int)p->pid, " ", p->command,
1526 p->next ? "|" : null);
1527 else if (how == JP_MEDIUM)
1528 shf_fprintf(shf, " %s%s", p->command,
1529 p->next ? "|" : null);
1534 shf_putc('\n', shf);
1538 * Convert % sequence to job
1540 * If jobs are compiled in then this routine expects sigchld to be blocked.
1543 j_lookup(const char *cp, int *ecodep)
1545 Job *j, *last_match;
1550 if (ksh_isdigit(*cp)) {
1552 /* Look for last_proc->pid (what $! returns) first... */
1553 for (j = job_list; j != NULL; j = j->next)
1554 if (j->last_proc && j->last_proc->pid == job)
1557 * ...then look for process group (this is non-POSIX,
1558 * but should not break anything
1560 for (j = job_list; j != NULL; j = j->next)
1561 if (j->pgrp && j->pgrp == job)
1564 *ecodep = JL_NOSUCH;
1569 *ecodep = JL_INVALID;
1573 case '\0': /* non-standard */
1576 if (job_list != NULL)
1581 if (job_list != NULL && job_list->next)
1582 return (job_list->next);
1585 case '0': case '1': case '2': case '3': case '4':
1586 case '5': case '6': case '7': case '8': case '9':
1588 for (j = job_list; j != NULL; j = j->next)
1596 for (j = job_list; j != NULL; j = j->next)
1597 for (p = j->proc_list; p != NULL; p = p->next)
1598 if (strstr(p->command, cp+1) != NULL) {
1607 return (last_match);
1614 for (j = job_list; j != NULL; j = j->next)
1615 if (strncmp(cp, j->proc_list->command, len) == 0) {
1624 return (last_match);
1628 *ecodep = JL_NOSUCH;
1632 static Job *free_jobs;
1633 static Proc *free_procs;
1636 * allocate a new job and fill in the job number.
1638 * If jobs are compiled in then this routine expects sigchld to be blocked.
1646 if (free_jobs != NULL) {
1648 free_jobs = free_jobs->next;
1650 newj = alloc(sizeof(Job), APERM);
1652 /* brute force method */
1653 for (i = 1; ; i++) {
1654 for (j = job_list; j && j->job != i; j = j->next)
1665 * Allocate new process struct
1667 * If jobs are compiled in then this routine expects sigchld to be blocked.
1674 if (free_procs != NULL) {
1676 free_procs = free_procs->next;
1678 p = alloc(sizeof(Proc), APERM);
1684 * Take job out of job_list and put old structures into free list.
1685 * Keeps nzombies, last_job and async_job up to date.
1687 * If jobs are compiled in then this routine expects sigchld to be blocked.
1690 remove_job(Job *j, const char *where)
1697 for (; curr != NULL && curr != j; prev = &curr->next, curr = *prev)
1700 internal_warningf("remove_job: job %s (%s)", "not found", where);
1705 /* free up proc structures */
1706 for (p = j->proc_list; p != NULL; ) {
1709 tmp->next = free_procs;
1713 if ((j->flags & JF_ZOMBIE) && j->ppid == procpid)
1715 j->next = free_jobs;
1725 * put j in a particular location (taking it out job_list if it is there
1728 * If jobs are compiled in then this routine expects sigchld to be blocked.
1731 put_job(Job *j, int where)
1735 /* Remove job from list (if there) */
1738 for (; curr && curr != j; prev = &curr->next, curr = *prev)
1749 case PJ_PAST_STOPPED:
1752 for (; curr && curr->state == PSTOPPED; prev = &curr->next,
1762 * nuke a job (called when unable to start full job).
1764 * If jobs are compiled in then this routine expects sigchld to be blocked.
1767 kill_job(Job *j, int sig)
1772 for (p = j->proc_list; p != NULL; p = p->next)
1774 if (kill(p->pid, sig) < 0)