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
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.69 2010/07/04 17:33:54 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->ttystate 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 ttystate;/* 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 */
101 /* Error codes for j_lookup() */
103 #define JL_NOSUCH 1 /* no such job */
104 #define JL_AMBIG 2 /* %foo or %?foo is ambiguous */
105 #define JL_INVALID 3 /* non-pid, non-% job id */
107 static const char *const lookup_msgs[] = {
111 "argument must be %job or process id",
115 static Job *job_list; /* job list */
116 static Job *last_job;
117 static Job *async_job;
118 static pid_t async_pid;
120 static int nzombie; /* # of zombies owned by this process */
121 static int32_t njobs; /* # of jobs started */
127 /* held_sigchld is set if sigchld occurs before a job is completely started */
128 static volatile sig_atomic_t held_sigchld;
130 #ifndef MKSH_UNEMPLOYED
131 static struct shf *shl_j;
132 static bool ttypgrp_ok; /* set if can use tty pgrps */
133 static pid_t restore_ttypgrp = -1;
134 static int const tt_sigs[] = { SIGTSTP, SIGTTIN, SIGTTOU };
137 static void j_set_async(Job *);
138 static void j_startjob(Job *);
139 static int j_waitj(Job *, int, const char *);
140 static void j_sigchld(int);
141 static void j_print(Job *, int, struct shf *);
142 static Job *j_lookup(const char *, int *);
143 static Job *new_job(void);
144 static Proc *new_proc(void);
145 static void check_job(Job *);
146 static void put_job(Job *, int);
147 static void remove_job(Job *, const char *);
148 static int kill_job(Job *, int);
150 /* initialise job control */
154 #ifndef MKSH_UNEMPLOYED
155 bool mflagset = Flag(FMONITOR) != 127;
160 (void)sigemptyset(&sm_default);
161 sigprocmask(SIG_SETMASK, &sm_default, NULL);
163 (void)sigemptyset(&sm_sigchld);
164 (void)sigaddset(&sm_sigchld, SIGCHLD);
166 setsig(&sigtraps[SIGCHLD], j_sigchld,
167 SS_RESTORE_ORIG|SS_FORCE|SS_SHTRAP);
169 #ifndef MKSH_UNEMPLOYED
170 if (!mflagset && Flag(FTALKING))
174 * shl_j is used to do asynchronous notification (used in
175 * an interrupt handler, so need a distinct shf)
177 shl_j = shf_fdopen(2, SHF_WR, NULL);
179 if (Flag(FMONITOR) || Flag(FTALKING)) {
183 * the TF_SHELL_USES test is a kludge that lets us know if
184 * if the signals have been changed by the shell.
186 for (i = NELEM(tt_sigs); --i >= 0; ) {
187 sigtraps[tt_sigs[i]].flags |= TF_SHELL_USES;
188 /* j_change() sets this to SS_RESTORE_DFL if FMONITOR */
189 setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
190 SS_RESTORE_IGN|SS_FORCE);
194 /* j_change() calls tty_init() */
200 tty_init(true, true);
203 /* job cleanup before shell exit */
207 /* kill stopped, and possibly running, jobs */
211 for (j = job_list; j != NULL; j = j->next) {
212 if (j->ppid == procpid &&
213 (j->state == PSTOPPED ||
214 (j->state == PRUNNING &&
215 ((j->flags & JF_FG) ||
216 (Flag(FLOGIN) && !Flag(FNOHUP) && procpid == kshpid))))) {
221 mksh_killpg(j->pgrp, SIGHUP);
222 #ifndef MKSH_UNEMPLOYED
223 if (j->state == PSTOPPED) {
225 kill_job(j, SIGCONT);
227 mksh_killpg(j->pgrp, SIGCONT);
236 #ifndef MKSH_UNEMPLOYED
237 if (kshpid == procpid && restore_ttypgrp >= 0) {
239 * Need to restore the tty pgrp to what it was when the
240 * shell started up, so that the process that started us
241 * will be able to access the tty when we are done.
242 * Also need to restore our process group in case we are
243 * about to do an exec so that both our parent and the
244 * process we are to become will be able to access the tty.
246 tcsetpgrp(tty_fd, restore_ttypgrp);
247 setpgid(0, restore_ttypgrp);
249 if (Flag(FMONITOR)) {
256 #ifndef MKSH_UNEMPLOYED
257 /* turn job control on or off according to Flag(FMONITOR) */
263 if (Flag(FMONITOR)) {
264 bool use_tty = Flag(FTALKING);
266 /* Don't call tcgetattr() 'til we own the tty process group */
268 tty_init(false, true);
270 /* no controlling tty, no SIGT* */
271 if ((ttypgrp_ok = use_tty && tty_fd >= 0 && tty_devtty)) {
272 setsig(&sigtraps[SIGTTIN], SIG_DFL,
273 SS_RESTORE_ORIG|SS_FORCE);
274 /* wait to be given tty (POSIX.1, B.2, job control) */
278 if ((ttypgrp = tcgetpgrp(tty_fd)) < 0) {
280 "j_init: tcgetpgrp() failed: %s",
285 if (ttypgrp == kshpgrp)
290 for (i = NELEM(tt_sigs); --i >= 0; )
291 setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
292 SS_RESTORE_DFL|SS_FORCE);
293 if (ttypgrp_ok && kshpgrp != kshpid) {
294 if (setpgid(0, kshpid) < 0) {
296 "j_init: setpgid() failed: %s",
300 if (tcsetpgrp(tty_fd, kshpid) < 0) {
302 "j_init: tcsetpgrp() failed: %s",
306 restore_ttypgrp = kshpgrp;
310 if (use_tty && !ttypgrp_ok)
311 warningf(false, "warning: won't have full job control");
313 tcgetattr(tty_fd, &tty_state);
317 for (i = NELEM(tt_sigs); --i >= 0; )
318 setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
319 SS_RESTORE_IGN|SS_FORCE);
321 for (i = NELEM(tt_sigs); --i >= 0; ) {
322 if (sigtraps[tt_sigs[i]].flags &
323 (TF_ORIG_IGN | TF_ORIG_DFL))
324 setsig(&sigtraps[tt_sigs[i]],
325 (sigtraps[tt_sigs[i]].flags & TF_ORIG_IGN) ?
327 SS_RESTORE_ORIG|SS_FORCE);
335 /* execute tree in child subprocess */
337 exchild(struct op *t, int flags,
338 volatile int *xerrok,
339 /* used if XPCLOSE or XCCLOSE */ int close_fd)
341 static Proc *last_proc; /* for pipelines */
343 int rv = 0, forksleep;
353 * Clear XFORK|XPCLOSE|XCCLOSE|XCOPROC|XPIPEO|XPIPEI|XXCOM|XBGND
354 * (also done in another execute() below)
356 return (execute(t, flags & (XEXEC | XERROK), xerrok));
358 /* no SIGCHLDs while messing with job and process lists */
359 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
363 pi.p->state = PRUNNING;
367 /* link process into jobs list */
368 if (flags & XPIPEI) {
369 /* continuing with a pipe */
372 "exchild: XPIPEI and no last_job - pid %d",
376 last_proc->next = pi.p;
379 pi.j = new_job(); /* fills in pi.j->job */
381 * we don't consider XXCOMs foreground since they don't get
382 * tty process group and we don't save or restore tty modes.
384 pi.j->flags = (flags & XXCOM) ? JF_XXCOM :
385 ((flags & XBGND) ? 0 : (JF_FG|JF_USETTYMODE));
386 timerclear(&pi.j->usrtime);
387 timerclear(&pi.j->systime);
388 pi.j->state = PRUNNING;
390 pi.j->ppid = procpid;
392 pi.j->proc_list = pi.p;
396 put_job(pi.j, PJ_PAST_STOPPED);
399 snptreef(pi.p->command, sizeof(pi.p->command), "%T", t);
401 /* create child process */
403 while ((pi.cldpid = fork()) < 0 && errno == EAGAIN && forksleep < 32) {
404 if (intrsig) /* allow user to ^C out... */
410 kill_job(pi.j, SIGKILL);
411 remove_job(pi.j, "fork failed");
412 sigprocmask(SIG_SETMASK, &omask, NULL);
413 errorf("cannot fork - try again");
415 pi.p->pid = pi.cldpid ? pi.cldpid : (procpid = getpid());
418 * ensure next child gets a (slightly) different $RANDOM sequence
419 * from its parent process and other child processes
421 change_random(&pi, sizeof(pi));
423 #ifndef MKSH_UNEMPLOYED
424 /* job control set up */
425 if (Flag(FMONITOR) && !(flags&XXCOM)) {
427 if (pi.j->pgrp == 0) { /* First process */
428 pi.j->pgrp = pi.p->pid;
432 /* set pgrp in both parent and child to deal with race
435 setpgid(pi.p->pid, pi.j->pgrp);
436 if (ttypgrp_ok && dotty && !(flags & XBGND))
437 tcsetpgrp(tty_fd, pi.j->pgrp);
441 /* used to close pipe input fd */
442 if (close_fd >= 0 && (((flags & XPCLOSE) && pi.cldpid) ||
443 ((flags & XCCLOSE) && !pi.cldpid)))
448 /* Do this before restoring signal */
450 coproc_cleanup(false);
451 sigprocmask(SIG_SETMASK, &omask, NULL);
452 cleanup_parents_env();
453 #ifndef MKSH_UNEMPLOYED
454 /* If FMONITOR or FTALKING is set, these signals are ignored,
455 * if neither FMONITOR nor FTALKING are set, the signals have
456 * their inherited values.
458 if (Flag(FMONITOR) && !(flags & XXCOM)) {
459 for (forksleep = NELEM(tt_sigs); --forksleep >= 0; )
460 setsig(&sigtraps[tt_sigs[forksleep]], SIG_DFL,
461 SS_RESTORE_DFL|SS_FORCE);
465 if (Flag(FBGNICE) && (flags & XBGND))
469 #ifndef MKSH_UNEMPLOYED
473 setsig(&sigtraps[SIGINT], SIG_IGN,
474 SS_RESTORE_IGN|SS_FORCE);
475 setsig(&sigtraps[SIGQUIT], SIG_IGN,
476 SS_RESTORE_IGN|SS_FORCE);
477 if ((!(flags & (XPIPEI | XCOPROC))) &&
478 ((forksleep = open("/dev/null", 0)) > 0)) {
479 (void)ksh_dup2(forksleep, 0, true);
483 remove_job(pi.j, "child"); /* in case of $(jobs) command */
485 #ifndef MKSH_UNEMPLOYED
493 execute(t, (flags & XERROK) | XEXEC, NULL);
495 if (t->type == TPIPE)
497 internal_warningf("exchild: execute() returned");
498 fptreef(shl_out, 2, "exchild: tried to execute {\n%T\n}\n", t);
505 /* shell (parent) stuff */
506 if (!(flags & XPIPEO)) { /* last process in a job */
508 if (flags & XCOPROC) {
509 pi.j->coproc_id = coproc.id;
510 /* n jobs using co-process output */
512 /* j using co-process input */
513 coproc.job = (void *)pi.j;
517 if (Flag(FTALKING)) {
518 shf_fprintf(shl_out, "[%d]", pi.j->job);
519 for (pi.p = pi.j->proc_list; pi.p;
521 shf_fprintf(shl_out, " %d",
523 shf_putchar('\n', shl_out);
527 rv = j_waitj(pi.j, JW_NONE, "jw:last proc");
530 sigprocmask(SIG_SETMASK, &omask, NULL);
535 /* start the last job: only used for $(command) jobs */
541 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
543 if (last_job) { /* no need to report error - waitlast() will do it */
544 /* ensure it isn't removed by check_job() */
545 last_job->flags |= JF_WAITING;
546 j_startjob(last_job);
548 sigprocmask(SIG_SETMASK, &omask, NULL);
551 /* wait for last job: only used for $(command) jobs */
559 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
562 if (!j || !(j->flags & JF_STARTED)) {
564 warningf(true, "waitlast: no last job");
566 internal_warningf("waitlast: not started");
567 sigprocmask(SIG_SETMASK, &omask, NULL);
568 return (125); /* not so arbitrary, non-zero value */
571 rv = j_waitj(j, JW_NONE, "jw:waitlast");
573 sigprocmask(SIG_SETMASK, &omask, NULL);
578 /* wait for child, interruptable. */
580 waitfor(const char *cp, int *sigp)
585 int flags = JW_INTERRUPT|JW_ASYNCNOTIFY;
588 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
594 * wait for an unspecified job - always returns 0, so
595 * don't have to worry about exited/signaled jobs
597 for (j = job_list; j; j = j->next)
598 /* AT&T ksh will wait for stopped jobs - we don't */
599 if (j->ppid == procpid && j->state == PRUNNING)
602 sigprocmask(SIG_SETMASK, &omask, NULL);
605 } else if ((j = j_lookup(cp, &ecode))) {
606 /* don't report normal job completion */
607 flags &= ~JW_ASYNCNOTIFY;
608 if (j->ppid != procpid) {
609 sigprocmask(SIG_SETMASK, &omask, NULL);
613 sigprocmask(SIG_SETMASK, &omask, NULL);
614 if (ecode != JL_NOSUCH)
615 bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
619 /* AT&T ksh will wait for stopped jobs - we don't */
620 rv = j_waitj(j, flags, "jw:waitfor");
622 sigprocmask(SIG_SETMASK, &omask, NULL);
624 if (rv < 0) /* we were interrupted */
630 /* kill (built-in) a job */
632 j_kill(const char *cp, int sig)
639 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
641 if ((j = j_lookup(cp, &ecode)) == NULL) {
642 sigprocmask(SIG_SETMASK, &omask, NULL);
643 bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
647 if (j->pgrp == 0) { /* started when !Flag(FMONITOR) */
648 if (kill_job(j, sig) < 0) {
649 bi_errorf("%s: %s", cp, strerror(errno));
653 #ifndef MKSH_UNEMPLOYED
654 if (j->state == PSTOPPED && (sig == SIGTERM || sig == SIGHUP))
655 mksh_killpg(j->pgrp, SIGCONT);
657 if (mksh_killpg(j->pgrp, sig) < 0) {
658 bi_errorf("%s: %s", cp, strerror(errno));
663 sigprocmask(SIG_SETMASK, &omask, NULL);
668 #ifndef MKSH_UNEMPLOYED
669 /* fg and bg built-ins: called only if Flag(FMONITOR) set */
671 j_resume(const char *cp, int bg)
680 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
682 if ((j = j_lookup(cp, &ecode)) == NULL) {
683 sigprocmask(SIG_SETMASK, &omask, NULL);
684 bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
689 sigprocmask(SIG_SETMASK, &omask, NULL);
690 bi_errorf("job not job-controlled");
695 shprintf("[%d] ", j->job);
698 for (p = j->proc_list; p != NULL; p = p->next) {
699 if (p->state == PSTOPPED) {
704 shf_puts(p->command, shl_stdout);
706 shf_puts("| ", shl_stdout);
708 shf_putc('\n', shl_stdout);
709 shf_flush(shl_stdout);
713 put_job(j, PJ_PAST_STOPPED);
717 /* attach tty to job */
718 if (j->state == PRUNNING) {
719 if (ttypgrp_ok && (j->flags & JF_SAVEDTTY))
720 tcsetattr(tty_fd, TCSADRAIN, &j->ttystate);
721 /* See comment in j_waitj regarding saved_ttypgrp. */
723 tcsetpgrp(tty_fd, (j->flags & JF_SAVEDTTYPGRP) ?
724 j->saved_ttypgrp : j->pgrp) < 0) {
726 if (j->flags & JF_SAVEDTTY)
727 tcsetattr(tty_fd, TCSADRAIN, &tty_state);
728 sigprocmask(SIG_SETMASK, &omask,
730 bi_errorf("1st tcsetpgrp(%d, %d) failed: %s",
732 (int)((j->flags & JF_SAVEDTTYPGRP) ?
733 j->saved_ttypgrp : j->pgrp),
739 j->flags &= ~JF_KNOWN;
744 if (j->state == PRUNNING && mksh_killpg(j->pgrp, SIGCONT) < 0) {
749 if (ttypgrp_ok && (j->flags & JF_SAVEDTTY))
750 tcsetattr(tty_fd, TCSADRAIN, &tty_state);
751 if (ttypgrp_ok && tcsetpgrp(tty_fd, kshpgrp) < 0)
753 "fg: 2nd tcsetpgrp(%d, %ld) failed: %s",
754 tty_fd, (long)kshpgrp, strerror(errno));
756 sigprocmask(SIG_SETMASK, &omask, NULL);
757 bi_errorf("cannot continue job %s: %s",
763 j->flags &= ~(JF_SAVEDTTY | JF_SAVEDTTYPGRP);
765 rv = j_waitj(j, JW_NONE, "jw:resume");
767 sigprocmask(SIG_SETMASK, &omask, NULL);
772 /* are there any running or stopped jobs ? */
774 j_stopped_running(void)
779 for (j = job_list; j != NULL; j = j->next) {
780 #ifndef MKSH_UNEMPLOYED
781 if (j->ppid == procpid && j->state == PSTOPPED)
784 if (Flag(FLOGIN) && !Flag(FNOHUP) && procpid == kshpid &&
785 j->ppid == procpid && j->state == PRUNNING)
789 shellf("You have %s%s%s jobs\n",
790 which & 1 ? "stopped" : "",
791 which == 3 ? " and " : "",
792 which & 2 ? "running" : "");
806 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
807 for (j = job_list; j; j = j->next)
810 sigprocmask(SIG_SETMASK, &omask, NULL);
815 /* list jobs for jobs built-in */
817 j_jobs(const char *cp, int slp,
818 int nflag) /* 0: short, 1: long, 2: pgrp */
825 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
827 if (nflag < 0) { /* kludge: print zombies */
834 if ((j = j_lookup(cp, &ecode)) == NULL) {
835 sigprocmask(SIG_SETMASK, &omask, NULL);
836 bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
841 how = slp == 0 ? JP_MEDIUM : (slp == 1 ? JP_LONG : JP_PGRP);
842 for (; j; j = j->next) {
843 if ((!(j->flags & JF_ZOMBIE) || zflag) &&
844 (!nflag || (j->flags & JF_CHANGED))) {
845 j_print(j, how, shl_stdout);
846 if (j->state == PEXITED || j->state == PSIGNALLED)
847 j->flags |= JF_REMOVE;
852 /* Remove jobs after printing so there won't be multiple + or - jobs */
853 for (j = job_list; j; j = tmp) {
855 if (j->flags & JF_REMOVE)
856 remove_job(j, "jobs");
858 sigprocmask(SIG_SETMASK, &omask, NULL);
862 /* list jobs for top-level notification */
869 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
870 for (j = job_list; j; j = j->next) {
871 #ifndef MKSH_UNEMPLOYED
872 if (Flag(FMONITOR) && (j->flags & JF_CHANGED))
873 j_print(j, JP_MEDIUM, shl_out);
875 /* Remove job after doing reports so there aren't
878 if (j->state == PEXITED || j->state == PSIGNALLED)
879 j->flags |= JF_REMOVE;
881 for (j = job_list; j; j = tmp) {
883 if (j->flags & JF_REMOVE)
884 remove_job(j, "notify");
887 sigprocmask(SIG_SETMASK, &omask, NULL);
890 /* Return pid of last process in last asynchronous job */
896 sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
899 async_job->flags |= JF_KNOWN;
901 sigprocmask(SIG_SETMASK, &omask, NULL);
907 * Make j the last async process
909 * If jobs are compiled in then this routine expects sigchld to be blocked.
916 if (async_job && (async_job->flags & (JF_KNOWN|JF_ZOMBIE)) == JF_ZOMBIE)
917 remove_job(async_job, "async");
918 if (!(j->flags & JF_STARTED)) {
919 internal_warningf("j_async: job not started");
923 async_pid = j->last_proc->pid;
924 while (nzombie > CHILD_MAX) {
926 for (jl = job_list; jl; jl = jl->next)
927 if (jl != async_job && (jl->flags & JF_ZOMBIE) &&
928 (!oldest || jl->age < oldest->age))
932 if (!(async_job->flags & JF_ZOMBIE) || nzombie != 1) {
933 internal_warningf("j_async: bad nzombie (%d)",
939 remove_job(oldest, "zombie");
944 * Start a job: set STARTED, check for held signals and set j->last_proc
946 * If jobs are compiled in then this routine expects sigchld to be blocked.
953 j->flags |= JF_STARTED;
954 for (p = j->proc_list; p->next; p = p->next)
960 /* Don't call j_sigchld() as it may remove job... */
961 kill(procpid, SIGCHLD);
966 * wait for job to complete or change state
968 * If jobs are compiled in then this routine expects sigchld to be blocked.
972 int flags, /* see JW_* */
978 * No auto-notify on the job we are waiting on.
980 j->flags |= JF_WAITING;
981 if (flags & JW_ASYNCNOTIFY)
982 j->flags |= JF_W_ASYNCNOTIFY;
984 #ifndef MKSH_UNEMPLOYED
987 flags |= JW_STOPPEDWAIT;
989 while (j->state == PRUNNING ||
990 ((flags & JW_STOPPEDWAIT) && j->state == PSTOPPED)) {
991 sigsuspend(&sm_default);
993 int oldf = j->flags & (JF_WAITING|JF_W_ASYNCNOTIFY);
994 j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
996 j->flags |= oldf; /* not reached... */
998 if ((flags & JW_INTERRUPT) && (rv = trap_pending())) {
999 j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
1003 j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
1005 if (j->flags & JF_FG) {
1007 #ifndef MKSH_UNEMPLOYED
1008 if (Flag(FMONITOR) && ttypgrp_ok && j->pgrp) {
1010 * Save the tty's current pgrp so it can be restored
1011 * when the job is foregrounded. This is to
1012 * deal with things like the GNU su which does
1013 * a fork/exec instead of an exec (the fork means
1014 * the execed shell gets a different pid from its
1015 * pgrp, so naturally it sets its pgrp and gets hosed
1016 * when it gets foregrounded by the parent shell which
1017 * has restored the tty's pgrp to that of the su
1020 if (j->state == PSTOPPED &&
1021 (j->saved_ttypgrp = tcgetpgrp(tty_fd)) >= 0)
1022 j->flags |= JF_SAVEDTTYPGRP;
1023 if (tcsetpgrp(tty_fd, kshpgrp) < 0)
1025 "j_waitj: tcsetpgrp(%d, %ld) failed: %s",
1026 tty_fd, (long)kshpgrp, strerror(errno));
1027 if (j->state == PSTOPPED) {
1028 j->flags |= JF_SAVEDTTY;
1029 tcgetattr(tty_fd, &j->ttystate);
1035 * Only restore tty settings if job was originally
1036 * started in the foreground. Problems can be
1037 * caused by things like 'more foobar &' which will
1038 * typically get and save the shell's vi/emacs tty
1039 * settings before setting up the tty for itself;
1040 * when more exits, it restores the 'original'
1041 * settings, and things go down hill from there...
1043 if (j->state == PEXITED && j->status == 0 &&
1044 (j->flags & JF_USETTYMODE)) {
1045 tcgetattr(tty_fd, &tty_state);
1047 tcsetattr(tty_fd, TCSADRAIN, &tty_state);
1049 * Don't use tty mode if job is stopped and
1050 * later restarted and exits. Consider
1057 * mode should be that of the stty, not what
1058 * was before the vi started.
1060 if (j->state == PSTOPPED)
1061 j->flags &= ~JF_USETTYMODE;
1064 #ifndef MKSH_UNEMPLOYED
1066 * If it looks like user hit ^C to kill a job, pretend we got
1067 * one too to break out of for loops, etc. (AT&T ksh does this
1068 * even when not monitoring, but this doesn't make sense since
1069 * a tty generated ^C goes to the whole process group)
1074 status = j->last_proc->status;
1075 if (Flag(FMONITOR) && j->state == PSIGNALLED &&
1076 WIFSIGNALED(status) &&
1077 (sigtraps[WTERMSIG(status)].flags & TF_TTY_INTR))
1078 trapsig(WTERMSIG(status));
1083 j_usrtime = j->usrtime;
1084 j_systime = j->systime;
1087 if (!(flags & JW_ASYNCNOTIFY)
1088 #ifndef MKSH_UNEMPLOYED
1089 && (!Flag(FMONITOR) || j->state != PSTOPPED)
1092 j_print(j, JP_SHORT, shl_out);
1095 if (j->state != PSTOPPED
1096 #ifndef MKSH_UNEMPLOYED
1097 && (!Flag(FMONITOR) || !(flags & JW_ASYNCNOTIFY))
1100 remove_job(j, where);
1106 * SIGCHLD handler to reap children and update job states
1108 * If jobs are compiled in then this routine expects sigchld to be blocked.
1112 j_sigchld(int sig MKSH_A_UNUSED)
1114 /* this runs inside interrupt context, with errno saved */
1120 struct rusage ru0, ru1;
1123 * Don't wait for any processes if a job is partially started.
1124 * This is so we don't do away with the process group leader
1125 * before all the processes in a pipe line are started (so the
1126 * setpgid() won't fail)
1128 for (j = job_list; j; j = j->next)
1129 if (j->ppid == procpid && !(j->flags & JF_STARTED)) {
1134 getrusage(RUSAGE_CHILDREN, &ru0);
1136 pid = waitpid(-1, &status, (WNOHANG|WUNTRACED));
1139 * return if this would block (0) or no children
1140 * or interrupted (-1)
1145 getrusage(RUSAGE_CHILDREN, &ru1);
1147 /* find job and process structures for this pid */
1148 for (j = job_list; j != NULL; j = j->next)
1149 for (p = j->proc_list; p != NULL; p = p->next)
1154 /* Can occur if process has kids, then execs shell
1155 warningf(true, "bad process waited for (pid = %d)",
1162 timeradd(&j->usrtime, &ru1.ru_utime, &j->usrtime);
1163 timersub(&j->usrtime, &ru0.ru_utime, &j->usrtime);
1164 timeradd(&j->systime, &ru1.ru_stime, &j->systime);
1165 timersub(&j->systime, &ru0.ru_stime, &j->systime);
1168 #ifndef MKSH_UNEMPLOYED
1169 if (WIFSTOPPED(status))
1170 p->state = PSTOPPED;
1173 if (WIFSIGNALED(status))
1174 p->state = PSIGNALLED;
1178 check_job(j); /* check to see if entire job is done */
1183 * Called only when a process in j has exited/stopped (ie, called only
1184 * from j_sigchld()). If no processes are running, the job status
1185 * and state are updated, asynchronous job notification is done and,
1186 * if unneeded, the job is removed.
1188 * If jobs are compiled in then this routine expects sigchld to be blocked.
1196 /* XXX debugging (nasty - interrupt routine using shl_out) */
1197 if (!(j->flags & JF_STARTED)) {
1198 internal_warningf("check_job: job started (flags 0x%x)",
1204 for (p=j->proc_list; p != NULL; p = p->next) {
1205 if (p->state == PRUNNING)
1206 return; /* some processes still running */
1207 if (p->state > jstate)
1212 switch (j->last_proc->state) {
1214 j->status = WEXITSTATUS(j->last_proc->status);
1217 j->status = 128 + WTERMSIG(j->last_proc->status);
1225 * Note when co-process dies: can't be done in j_wait() nor
1226 * remove_job() since neither may be called for non-interactive
1229 if (j->state == PEXITED || j->state == PSIGNALLED) {
1231 * No need to keep co-process input any more
1232 * (at least, this is what ksh93d thinks)
1234 if (coproc.job == j) {
1237 * XXX would be nice to get the closes out of here
1238 * so they aren't done in the signal handler.
1239 * Would mean a check in coproc_getfd() to
1240 * do "if job == 0 && write >= 0, close write".
1242 coproc_write_close(coproc.write);
1244 /* Do we need to keep the output? */
1245 if (j->coproc_id && j->coproc_id == coproc.id &&
1246 --coproc.njobs == 0)
1247 coproc_readw_close(coproc.read);
1250 j->flags |= JF_CHANGED;
1251 #ifndef MKSH_UNEMPLOYED
1252 if (Flag(FMONITOR) && !(j->flags & JF_XXCOM)) {
1254 * Only put stopped jobs at the front to avoid confusing
1255 * the user (don't want finished jobs effecting %+ or %-)
1257 if (j->state == PSTOPPED)
1258 put_job(j, PJ_ON_FRONT);
1259 if (Flag(FNOTIFY) &&
1260 (j->flags & (JF_WAITING|JF_W_ASYNCNOTIFY)) != JF_WAITING) {
1261 /* Look for the real file descriptor 2 */
1266 for (ep = e; ep; ep = ep->oenv)
1267 if (ep->savefd && ep->savefd[2])
1269 shf_reopen(fd, SHF_WR, shl_j);
1272 * Can't call j_notify() as it removes jobs. The job
1273 * must stay in the job list as j_waitj() may be
1274 * running with this job.
1276 j_print(j, JP_MEDIUM, shl_j);
1278 if (!(j->flags & JF_WAITING) && j->state != PSTOPPED)
1279 remove_job(j, "notify");
1284 #ifndef MKSH_UNEMPLOYED
1287 !(j->flags & (JF_WAITING|JF_FG)) &&
1288 j->state != PSTOPPED) {
1289 if (j == async_job || (j->flags & JF_KNOWN)) {
1290 j->flags |= JF_ZOMBIE;
1294 remove_job(j, "checkjob");
1299 * Print job status in either short, medium or long format.
1301 * If jobs are compiled in then this routine expects sigchld to be blocked.
1304 j_print(Job *j, int how, struct shf *shf)
1315 if (how == JP_PGRP) {
1317 * POSIX doesn't say what to do it there is no process
1318 * group leader (ie, !FMONITOR). We arbitrarily return
1319 * last pid (which is what $! returns).
1321 shf_fprintf(shf, "%d\n", (int)(j->pgrp ? j->pgrp :
1322 (j->last_proc ? j->last_proc->pid : 0)));
1325 j->flags &= ~JF_CHANGED;
1326 filler = j->job > 10 ? "\n " : "\n ";
1329 else if (j == job_list->next)
1332 for (p = j->proc_list; p != NULL;) {
1336 memcpy(buf, "Running", 8);
1339 strlcpy(buf, sigtraps[WSTOPSIG(p->status)].mess,
1343 if (how == JP_SHORT)
1345 else if (WEXITSTATUS(p->status) == 0)
1346 memcpy(buf, "Done", 5);
1348 shf_snprintf(buf, sizeof(buf), "Done (%d)",
1349 WEXITSTATUS(p->status));
1353 if (WCOREDUMP(p->status))
1357 * kludge for not reporting 'normal termination
1358 * signals' (i.e. SIGINT, SIGPIPE)
1360 if (how == JP_SHORT && !coredumped &&
1361 (WTERMSIG(p->status) == SIGINT ||
1362 WTERMSIG(p->status) == SIGPIPE)) {
1365 strlcpy(buf, sigtraps[WTERMSIG(p->status)].mess,
1370 if (how != JP_SHORT) {
1371 if (p == j->proc_list)
1372 shf_fprintf(shf, "[%d] %c ", j->job, jobchar);
1374 shf_fprintf(shf, "%s", filler);
1378 shf_fprintf(shf, "%5d ", (int)p->pid);
1380 if (how == JP_SHORT) {
1383 shf_fprintf(shf, "%s%s ",
1384 buf, coredumped ? " (core dumped)" : null);
1388 shf_fprintf(shf, "%-20s %s%s%s", buf, p->command,
1389 p->next ? "|" : null,
1390 coredumped ? " (core dumped)" : null);
1396 while (p && p->state == state && p->status == status) {
1398 shf_fprintf(shf, "%s%5d %-20s %s%s", filler,
1399 (int)p->pid, " ", p->command,
1400 p->next ? "|" : null);
1401 else if (how == JP_MEDIUM)
1402 shf_fprintf(shf, " %s%s", p->command,
1403 p->next ? "|" : null);
1408 shf_putc('\n', shf);
1412 * Convert % sequence to job
1414 * If jobs are compiled in then this routine expects sigchld to be blocked.
1417 j_lookup(const char *cp, int *ecodep)
1419 Job *j, *last_match;
1423 if (ksh_isdigit(*cp)) {
1425 /* Look for last_proc->pid (what $! returns) first... */
1426 for (j = job_list; j != NULL; j = j->next)
1427 if (j->last_proc && j->last_proc->pid == job)
1430 * ...then look for process group (this is non-POSIX,
1431 * but should not break anything
1433 for (j = job_list; j != NULL; j = j->next)
1434 if (j->pgrp && j->pgrp == job)
1437 *ecodep = JL_NOSUCH;
1442 *ecodep = JL_INVALID;
1446 case '\0': /* non-standard */
1449 if (job_list != NULL)
1454 if (job_list != NULL && job_list->next)
1455 return (job_list->next);
1458 case '0': case '1': case '2': case '3': case '4':
1459 case '5': case '6': case '7': case '8': case '9':
1461 for (j = job_list; j != NULL; j = j->next)
1466 case '?': /* %?string */
1468 for (j = job_list; j != NULL; j = j->next)
1469 for (p = j->proc_list; p != NULL; p = p->next)
1470 if (strstr(p->command, cp+1) != NULL) {
1479 return (last_match);
1482 default: /* %string */
1485 for (j = job_list; j != NULL; j = j->next)
1486 if (strncmp(cp, j->proc_list->command, len) == 0) {
1495 return (last_match);
1499 *ecodep = JL_NOSUCH;
1503 static Job *free_jobs;
1504 static Proc *free_procs;
1507 * allocate a new job and fill in the job number.
1509 * If jobs are compiled in then this routine expects sigchld to be blocked.
1517 if (free_jobs != NULL) {
1519 free_jobs = free_jobs->next;
1521 newj = alloc(sizeof(Job), APERM);
1523 /* brute force method */
1524 for (i = 1; ; i++) {
1525 for (j = job_list; j && j->job != i; j = j->next)
1536 * Allocate new process struct
1538 * If jobs are compiled in then this routine expects sigchld to be blocked.
1545 if (free_procs != NULL) {
1547 free_procs = free_procs->next;
1549 p = alloc(sizeof(Proc), APERM);
1555 * Take job out of job_list and put old structures into free list.
1556 * Keeps nzombies, last_job and async_job up to date.
1558 * If jobs are compiled in then this routine expects sigchld to be blocked.
1561 remove_job(Job *j, const char *where)
1568 for (; curr != NULL && curr != j; prev = &curr->next, curr = *prev)
1571 internal_warningf("remove_job: job not found (%s)", where);
1576 /* free up proc structures */
1577 for (p = j->proc_list; p != NULL; ) {
1580 tmp->next = free_procs;
1584 if ((j->flags & JF_ZOMBIE) && j->ppid == procpid)
1586 j->next = free_jobs;
1596 * put j in a particular location (taking it out job_list if it is there
1599 * If jobs are compiled in then this routine expects sigchld to be blocked.
1602 put_job(Job *j, int where)
1606 /* Remove job from list (if there) */
1609 for (; curr && curr != j; prev = &curr->next, curr = *prev)
1620 case PJ_PAST_STOPPED:
1623 for (; curr && curr->state == PSTOPPED; prev = &curr->next,
1633 * nuke a job (called when unable to start full job).
1635 * If jobs are compiled in then this routine expects sigchld to be blocked.
1638 kill_job(Job *j, int sig)
1643 for (p = j->proc_list; p != NULL; p = p->next)
1645 if (kill(p->pid, sig) < 0)