2 * Process and job control
6 * Reworked/Rewritten version of Eric Gisin's/Ron Natalie's code by
7 * Larry Bouzane (larry@cs.mun.ca) and hacked again by
8 * Michael Rendell (michael@cs.mun.ca)
10 * The interface to the rest of the shell should probably be changed
11 * to allow use of vfork() when available but that would be way too much
19 #include "ksh_times.h"
24 /* Start of system configuration stuff */
26 /* We keep CHILD_MAX zombie processes around (exact value isn't critical) */
28 # ifdef _POSIX_CHILD_MAX
29 # define CHILD_MAX ((_POSIX_CHILD_MAX) * 2)
30 # else /* _POSIX_CHILD_MAX */
32 # endif /* _POSIX_CHILD_MAX */
33 #endif /* !CHILD_MAX */
35 /* Order important! */
41 typedef struct proc Proc
;
43 Proc
*next
; /* next process in pipeline (if any) */
45 int status
; /* wait status */
46 pid_t pid
; /* process id */
47 char command
[48]; /* process command string */
50 /* Notify/print flag - j_print() argument */
51 #define JP_NONE 0 /* don't print anything */
52 #define JP_SHORT 1 /* print signals processes were killed by */
53 #define JP_MEDIUM 2 /* print [job-num] -/+ command */
54 #define JP_LONG 3 /* print [job-num] -/+ pid command */
55 #define JP_PGRP 4 /* print pgrp */
58 #define PJ_ON_FRONT 0 /* at very front */
59 #define PJ_PAST_STOPPED 1 /* just past any stopped jobs */
61 /* Job.flags values */
62 #define JF_STARTED 0x001 /* set when all processes in job are started */
63 #define JF_WAITING 0x002 /* set if j_waitj() is waiting on job */
64 #define JF_W_ASYNCNOTIFY 0x004 /* set if waiting and async notification ok */
65 #define JF_XXCOM 0x008 /* set for `command` jobs */
66 #define JF_FG 0x010 /* running in foreground (also has tty pgrp) */
67 #define JF_SAVEDTTY 0x020 /* j->ttystate is valid */
68 #define JF_CHANGED 0x040 /* process has changed state */
69 #define JF_KNOWN 0x080 /* $! referenced */
70 #define JF_ZOMBIE 0x100 /* known, unwaited process */
71 #define JF_REMOVE 0x200 /* flagged for removal (j_jobs()/j_noityf()) */
72 #define JF_USETTYMODE 0x400 /* tty mode saved if process exits normally */
73 #define JF_SAVEDTTYPGRP 0x800 /* j->saved_ttypgrp is valid */
75 typedef struct job Job
;
77 Job
*next
; /* next job in list */
78 int job
; /* job number: %n */
79 int flags
; /* see JF_* */
80 int state
; /* job state */
81 int status
; /* exit status of last process */
82 pid_t pgrp
; /* process group of job */
83 pid_t ppid
; /* pid of process that forked job */
84 INT32 age
; /* number of jobs started */
85 INT32 systime
; /* system time used by job */
86 INT32 usrtime
; /* user time used by job */
87 Proc
*proc_list
; /* process list */
88 Proc
*last_proc
; /* last process in list */
89 Coproc_id coproc_id
; /* 0 or id of coprocess output pipe */
92 /* Flags for j_waitj() */
94 #define JW_INTERRUPT 0x01 /* ^C will stop the wait */
95 #define JW_ASYNCNOTIFY 0x02 /* asynchronous notification during wait ok */
96 #define JW_STOPPEDWAIT 0x04 /* wait even if job stopped */
98 /* Error codes for j_lookup() */
100 #define JL_NOSUCH 1 /* no such job */
101 #define JL_AMBIG 2 /* %foo or %?foo is ambiguous */
102 #define JL_INVALID 3 /* non-pid, non-% job id */
104 static const char *const lookup_msgs
[] = {
108 "argument must be %job or process id",
112 INT32 j_systime
, j_usrtime
; /* user and system time of last j_waitjed job */
114 static Job
*job_list
; /* job list */
115 static Job
*last_job
;
116 static Job
*async_job
;
117 static pid_t async_pid
;
119 static int nzombie
; /* # of zombies owned by this process */
120 INT32 njobs
; /* # of jobs started */
121 static int child_max
; /* CHILD_MAX */
123 /* held_sigchld is set if sigchld occurs before a job is completely started */
124 static volatile sig_atomic_t held_sigchld
;
127 static void j_set_async(Job
*);
129 static void j_startjob(Job
*);
130 static int j_waitj(Job
*, int, const char *);
131 static void j_sigchld(int);
132 static void j_print(Job
*, int, struct shf
*);
133 static Job
*j_lookup(const char *, int *);
135 static Job
*new_job(void);
136 static Proc
*new_proc(void);
138 static void check_job(Job
*);
140 static void put_job(Job
*, int);
142 static void remove_job(Job
*, const char *);
143 static int kill_job(Job
*, int);
145 /* initialize job control */
149 child_max
= CHILD_MAX
; /* so syscon() isn't always being called */
151 sigemptyset(&sm_default
);
152 sigprocmask(SIG_SETMASK
, &sm_default
, (sigset_t
*) 0);
154 sigemptyset(&sm_sigchld
);
155 sigaddset(&sm_sigchld
, SIGCHLD
);
157 setsig(&sigtraps
[SIGCHLD
], j_sigchld
,
158 SS_RESTORE_ORIG
|SS_FORCE
|SS_SHTRAP
);
160 // if (Flag(FTALKING))
164 /* job cleanup before shell exit */
168 /* kill stopped, and possibly running, jobs */
172 for (j
= job_list
; j
!= (Job
*) 0; j
= j
->next
) {
173 if (j
->ppid
== procpid
&& (j
->state
== PSTOPPED
||
174 (j
->state
== PRUNNING
&& (j
->flags
& JF_FG
)))) {
179 killpg(j
->pgrp
, SIGHUP
);
187 /* execute tree in child subprocess */
191 exchild(struct op
*t
, int flags
,
192 int close_fd
) /* used if XPCLOSE or XCCLOSE */
194 static Proc
*last_proc
; /* for pipelines */
205 /* Clear XFORK|XPCLOSE|XCCLOSE|XCOPROC|XPIPEO|XPIPEI|XXCOM|XBGND
206 * (also done in another execute() below)
208 return execute(t
, flags
& (XEXEC
| XERROK
));
211 p
->next
= (Proc
*) 0;
216 /* link process into jobs list */
217 if (flags
&XPIPEI
) { /* continuing with a pipe */
220 "exchild: XPIPEI and no last_job - pid %d",
226 j
= new_job(); /* fills in j->job */
227 /* we don't consider XXCOM's foreground since they don't get
228 * tty process group and we don't save or restore tty modes.
230 j
->flags
= (flags
& XXCOM
) ? JF_XXCOM
:
231 ((flags
& XBGND
) ? 0 : (JF_FG
|JF_USETTYMODE
));
232 j
->usrtime
= j
->systime
= 0;
241 put_job(j
, PJ_PAST_STOPPED
);
244 snptreef(p
->command
, sizeof(p
->command
), "%T", t
);
246 /* create child process */
248 while ((i
= fork()) < 0 && errno
== EAGAIN
&& forksleep
< 32) {
249 if (intrsig
) /* allow user to ^C out... */
255 kill_job(j
, SIGKILL
);
256 remove_job(j
, "fork failed");
257 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
258 errorf("cannot fork - try again");
262 p
->pid
= procpid
= getpid();
266 /* used to close pipe input fd */
267 if (close_fd
>= 0 && (((flags
& XPCLOSE
) && !ischild
) ||
268 ((flags
& XCCLOSE
) && ischild
)))
270 if (ischild
) { /* child */
271 /* Do this before restoring signal */
273 coproc_cleanup(false);
274 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
275 cleanup_parents_env();
276 if ((flags
& XBGND
)) {
277 setsig(&sigtraps
[SIGINT
], SIG_IGN
,
278 SS_RESTORE_IGN
|SS_FORCE
);
279 setsig(&sigtraps
[SIGQUIT
], SIG_IGN
,
280 SS_RESTORE_IGN
|SS_FORCE
);
281 if (!(flags
& (XPIPEI
| XCOPROC
))) {
282 int fd
= open("/dev/null", 0);
284 (void) ksh_dup2(fd
, 0, true);
290 remove_job(j
, "child"); /* in case of `jobs` command */
295 execute(t
, (flags
& XERROK
) | XEXEC
); /* no return */
296 internal_errorf(0, "exchild: execute() returned");
301 /* shell (parent) stuff */
302 /* Ensure next child gets a (slightly) different $RANDOM sequence */
304 if (!(flags
& XPIPEO
)) { /* last process in a job */
306 if (flags
& XCOPROC
) {
307 j
->coproc_id
= coproc
.id
;
308 coproc
.njobs
++; /* n jobs using co-process output */
309 coproc
.job
= (void *) j
; /* j using co-process input */
313 if (Flag(FTALKING
)) {
314 shf_fprintf(shl_out
, "[%d]", j
->job
);
315 for (p
= j
->proc_list
; p
; p
= p
->next
)
316 shf_fprintf(shl_out
, " %d", p
->pid
);
317 shf_putchar('\n', shl_out
);
321 rv
= j_waitj(j
, JW_NONE
, "jw:last proc");
324 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
330 /* start the last job: only used for `command` jobs */
336 sigprocmask(SIG_BLOCK
, &sm_sigchld
, &omask
);
338 if (last_job
) { /* no need to report error - waitlast() will do it */
339 /* ensure it isn't removed by check_job() */
340 last_job
->flags
|= JF_WAITING
;
341 j_startjob(last_job
);
343 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
346 /* wait for last job: only used for `command` jobs */
348 int lastresult
= 125;
357 sigprocmask(SIG_BLOCK
, &sm_sigchld
, &omask
);
360 if (!j
|| !(j
->flags
& JF_STARTED
)) {
363 // warningf(true, "waitlast: no last job");
366 internal_errorf(0, "waitlast: not started");
367 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
369 // work arround for no job control
370 // place the result of the last command
371 // subshell or whatever in lastresult
372 // and return it here.
374 //return 125; /* not so arbitrary, non-zero value */
377 rv
= j_waitj(j
, JW_NONE
, "jw:waitlast");
379 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
384 /* wait for child, interruptable. */
386 waitfor(const char *cp
, int *sigp
)
391 int flags
= JW_INTERRUPT
|JW_ASYNCNOTIFY
;
394 sigprocmask(SIG_BLOCK
, &sm_sigchld
, &omask
);
398 if (cp
== (char *) 0) {
399 /* wait for an unspecified job - always returns 0, so
400 * don't have to worry about exited/signaled jobs
402 for (j
= job_list
; j
; j
= j
->next
)
403 /* at&t ksh will wait for stopped jobs - we don't */
404 if (j
->ppid
== procpid
&& j
->state
== PRUNNING
)
409 } else if ((j
= j_lookup(cp
, &ecode
))) {
410 /* don't report normal job completion */
411 flags
&= ~JW_ASYNCNOTIFY
;
412 if (j
->ppid
!= procpid
) {
413 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
417 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
418 if (ecode
!= JL_NOSUCH
)
419 bi_errorf("%s: %s", cp
, lookup_msgs
[ecode
]);
423 /* at&t ksh will wait for stopped jobs - we don't */
424 rv
= j_waitj(j
, flags
, "jw:waitfor");
426 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
428 if (rv
< 0) /* we were interrupted */
434 /* kill (built-in) a job */
436 j_kill(const char *cp
, int sig
)
443 sigprocmask(SIG_BLOCK
, &sm_sigchld
, &omask
);
445 if ((j
= j_lookup(cp
, &ecode
)) == (Job
*) 0) {
446 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
447 bi_errorf("%s: %s", cp
, lookup_msgs
[ecode
]);
452 if (kill_job(j
, sig
) < 0) {
453 bi_errorf("%s: %s", cp
, strerror(errno
));
457 if (killpg(j
->pgrp
, sig
) < 0) {
458 bi_errorf("%s: %s", cp
, strerror(errno
));
463 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
468 /* are there any running or stopped jobs ? */
470 j_stopped_running(void)
475 for (j
= job_list
; j
!= (Job
*) 0; j
= j
->next
) {
478 shellf("You have %s%s%s jobs\n",
479 which
& 1 ? "stopped" : "",
480 which
== 3 ? " and " : "",
481 which
& 2 ? "running" : "");
495 sigprocmask(SIG_BLOCK
, &sm_sigchld
, &omask
);
496 for (j
= job_list
; j
; j
= j
->next
)
499 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
503 /* list jobs for jobs built-in */
505 j_jobs(const char *cp
, int slp
,
506 int nflag
) /* 0: short, 1: long, 2: pgrp */
513 sigprocmask(SIG_BLOCK
, &sm_sigchld
, &omask
);
515 if (nflag
< 0) { /* kludge: print zombies */
522 if ((j
= j_lookup(cp
, &ecode
)) == (Job
*) 0) {
523 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
524 bi_errorf("%s: %s", cp
, lookup_msgs
[ecode
]);
529 how
= slp
== 0 ? JP_MEDIUM
: (slp
== 1 ? JP_LONG
: JP_PGRP
);
530 for (; j
; j
= j
->next
) {
531 if ((!(j
->flags
& JF_ZOMBIE
) || zflag
) &&
532 (!nflag
|| (j
->flags
& JF_CHANGED
)))
534 j_print(j
, how
, shl_stdout
);
535 if (j
->state
== PEXITED
|| j
->state
== PSIGNALLED
)
536 j
->flags
|= JF_REMOVE
;
541 /* Remove jobs after printing so there won't be multiple + or - jobs */
542 for (j
= job_list
; j
; j
= tmp
) {
544 if (j
->flags
& JF_REMOVE
)
545 remove_job(j
, "jobs");
547 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
551 /* list jobs for top-level notification */
558 sigprocmask(SIG_BLOCK
, &sm_sigchld
, &omask
);
559 for (j
= job_list
; j
; j
= j
->next
) {
560 /* Remove job after doing reports so there aren't
563 if (j
->state
== PEXITED
|| j
->state
== PSIGNALLED
)
564 j
->flags
|= JF_REMOVE
;
566 for (j
= job_list
; j
; j
= tmp
) {
568 if (j
->flags
& JF_REMOVE
)
569 remove_job(j
, "notify");
572 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
575 /* Return pid of last process in last asynchronous job */
581 sigprocmask(SIG_BLOCK
, &sm_sigchld
, &omask
);
584 async_job
->flags
|= JF_KNOWN
;
586 sigprocmask(SIG_SETMASK
, &omask
, (sigset_t
*) 0);
592 /* Make j the last async process
594 * If jobs are compiled in then this routine expects sigchld to be blocked.
601 if (async_job
&& (async_job
->flags
& (JF_KNOWN
|JF_ZOMBIE
)) == JF_ZOMBIE
)
602 remove_job(async_job
, "async");
603 if (!(j
->flags
& JF_STARTED
)) {
604 internal_errorf(0, "j_async: job not started");
608 async_pid
= j
->last_proc
->pid
;
609 while (nzombie
> child_max
) {
611 for (jl
= job_list
; jl
; jl
= jl
->next
)
612 if (jl
!= async_job
&& (jl
->flags
& JF_ZOMBIE
) &&
613 (!oldest
|| jl
->age
< oldest
->age
))
617 if (!(async_job
->flags
& JF_ZOMBIE
) || nzombie
!= 1) {
619 "j_async: bad nzombie (%d)", nzombie
);
624 remove_job(oldest
, "zombie");
629 /* Start a job: set STARTED, check for held signals and set j->last_proc
631 * If jobs are compiled in then this routine expects sigchld to be blocked.
638 j
->flags
|= JF_STARTED
;
639 for (p
= j
->proc_list
; p
->next
; p
= p
->next
)
645 /* Don't call j_sigchld() as it may remove job... */
646 kill(procpid
, SIGCHLD
);
651 * wait for job to complete or change state
653 * If jobs are compiled in then this routine expects sigchld to be blocked.
657 int flags
, /* see JW_* */
663 * No auto-notify on the job we are waiting on.
665 j
->flags
|= JF_WAITING
;
666 if (flags
& JW_ASYNCNOTIFY
)
667 j
->flags
|= JF_W_ASYNCNOTIFY
;
669 flags
|= JW_STOPPEDWAIT
;
671 while ((volatile int) j
->state
== PRUNNING
||
672 ((flags
& JW_STOPPEDWAIT
) && (volatile int) j
->state
== PSTOPPED
))
675 int oldf
= j
->flags
& (JF_WAITING
|JF_W_ASYNCNOTIFY
);
676 j
->flags
&= ~(JF_WAITING
|JF_W_ASYNCNOTIFY
);
678 j
->flags
|= oldf
; /* not reached... */
680 if ((flags
& JW_INTERRUPT
) && (rv
= trap_pending())) {
681 j
->flags
&= ~(JF_WAITING
|JF_W_ASYNCNOTIFY
);
685 j
->flags
&= ~(JF_WAITING
|JF_W_ASYNCNOTIFY
);
687 if (j
->flags
& JF_FG
) {
690 /* Only restore tty settings if job was originally
691 * started in the foreground. Problems can be
692 * caused by things like `more foobar &' which will
693 * typically get and save the shell's vi/emacs tty
694 * settings before setting up the tty for itself;
695 * when more exits, it restores the `original'
696 * settings, and things go down hill from there...
698 if (j
->state
== PEXITED
&& j
->status
== 0 &&
699 (j
->flags
& JF_USETTYMODE
))
701 //tcgetattr(tty_fd, &tty_state);
703 //tcsetattr(tty_fd, TCSADRAIN, &tty_state);
704 /* Don't use tty mode if job is stopped and
705 * later restarted and exits. Consider
712 * mode should be that of the stty, not what
713 * was before the vi started.
715 if (j
->state
== PSTOPPED
)
716 j
->flags
&= ~JF_USETTYMODE
;
721 j_usrtime
= j
->usrtime
;
722 j_systime
= j
->systime
;
725 if (!(flags
& JW_ASYNCNOTIFY
) &&
726 (j
->state
!= PSTOPPED
))
728 j_print(j
, JP_SHORT
, shl_out
);
731 if (j
->state
!= PSTOPPED
&&
732 (!(flags
& JW_ASYNCNOTIFY
)))
733 remove_job(j
, where
);
738 /* SIGCHLD handler to reap children and update job states
740 * If jobs are compiled in then this routine expects sigchld to be blocked.
752 /* Don't wait for any processes if a job is partially started.
753 * This is so we don't do away with the process group leader
754 * before all the processes in a pipe line are started (so the
755 * setpgid() won't fail)
757 for (j
= job_list
; j
; j
= j
->next
)
758 if (j
->ppid
== procpid
&& !(j
->flags
& JF_STARTED
)) {
768 if (pid
<= 0) /* return if would block (0) ... */
769 break; /* ... or no children or interrupted (-1) */
773 /* find job and process structures for this pid */
774 for (j
= job_list
; j
!= (Job
*) 0; j
= j
->next
)
775 for (p
= j
->proc_list
; p
!= (Proc
*) 0; p
= p
->next
)
779 if (j
== (Job
*) 0) {
780 /* Can occur if process has kids, then execs shell
781 warningf(true, "bad process waited for (pid = %d)",
788 j
->usrtime
+= t1
.tms_cutime
- t0
.tms_cutime
;
789 j
->systime
+= t1
.tms_cstime
- t0
.tms_cstime
;
792 if (WIFSIGNALED(status
))
793 p
->state
= PSIGNALLED
;
797 check_job(j
); /* check to see if entire job is done */
805 * Called only when a process in j has exited/stopped (ie, called only
806 * from j_sigchld()). If no processes are running, the job status
807 * and state are updated, asynchronous job notification is done and,
808 * if unneeded, the job is removed.
810 * If jobs are compiled in then this routine expects sigchld to be blocked.
818 /* XXX debugging (nasty - interrupt routine using shl_out) */
819 if (!(j
->flags
& JF_STARTED
)) {
820 internal_errorf(0, "check_job: job started (flags 0x%x)",
826 for (p
=j
->proc_list
; p
!= (Proc
*) 0; p
= p
->next
) {
827 if (p
->state
== PRUNNING
)
828 return; /* some processes still running */
829 if (p
->state
> jstate
)
834 switch (j
->last_proc
->state
) {
836 j
->status
= WEXITSTATUS(j
->last_proc
->status
);
839 j
->status
= 128 + WTERMSIG(j
->last_proc
->status
);
846 /* Note when co-process dies: can't be done in j_wait() nor
847 * remove_job() since neither may be called for non-interactive
850 if (j
->state
== PEXITED
|| j
->state
== PSIGNALLED
) {
851 /* No need to keep co-process input any more
852 * (at leasst, this is what ksh93d thinks)
854 if (coproc
.job
== j
) {
855 coproc
.job
= (void *) 0;
856 /* XXX would be nice to get the closes out of here
857 * so they aren't done in the signal handler.
858 * Would mean a check in coproc_getfd() to
859 * do "if job == 0 && write >= 0, close write".
861 coproc_write_close(coproc
.write
);
863 /* Do we need to keep the output? */
864 if (j
->coproc_id
&& j
->coproc_id
== coproc
.id
&&
866 coproc_readw_close(coproc
.read
);
869 j
->flags
|= JF_CHANGED
;
870 if (!(j
->flags
& (JF_WAITING
|JF_FG
))
871 && j
->state
!= PSTOPPED
)
873 if (j
== async_job
|| (j
->flags
& JF_KNOWN
)) {
874 j
->flags
|= JF_ZOMBIE
;
878 remove_job(j
, "checkjob");
883 * Print job status in either short, medium or long format.
885 * If jobs are compiled in then this routine expects sigchld to be blocked.
888 j_print(Job
*j
, int how
, struct shf
*shf
)
899 if (how
== JP_PGRP
) {
900 /* POSIX doesn't say what to do it there is no process
901 * group leader. We arbitrarily return
902 * last pid (which is what $! returns).
904 shf_fprintf(shf
, "%d\n", j
->pgrp
? j
->pgrp
:
905 (j
->last_proc
? j
->last_proc
->pid
: 0));
908 j
->flags
&= ~JF_CHANGED
;
909 filler
= j
->job
> 10 ? "\n " : "\n ";
912 else if (j
== job_list
->next
)
915 for (p
= j
->proc_list
; p
!= (Proc
*) 0;) {
919 strlcpy(buf
, "Running", sizeof buf
);
922 strlcpy(buf
, sigtraps
[WSTOPSIG(p
->status
)].mess
,
928 else if (WEXITSTATUS(p
->status
) == 0)
929 strlcpy(buf
, "Done", sizeof buf
);
931 shf_snprintf(buf
, sizeof(buf
), "Done (%d)",
932 WEXITSTATUS(p
->status
));
935 if (WIFCORED(p
->status
))
937 /* kludge for not reporting `normal termination signals'
938 * (ie, SIGINT, SIGPIPE)
940 if (how
== JP_SHORT
&& !coredumped
&&
941 (WTERMSIG(p
->status
) == SIGINT
||
942 WTERMSIG(p
->status
) == SIGPIPE
)) {
945 strlcpy(buf
, sigtraps
[WTERMSIG(p
->status
)].mess
,
950 if (how
!= JP_SHORT
) {
951 if (p
== j
->proc_list
)
952 shf_fprintf(shf
, "[%d] %c ", j
->job
, jobchar
);
954 shf_fprintf(shf
, "%s", filler
);
958 shf_fprintf(shf
, "%5d ", p
->pid
);
960 if (how
== JP_SHORT
) {
963 shf_fprintf(shf
, "%s%s ",
964 buf
, coredumped
? " (core dumped)" : null
);
968 shf_fprintf(shf
, "%-20s %s%s%s", buf
, p
->command
,
969 p
->next
? "|" : null
,
970 coredumped
? " (core dumped)" : null
);
976 while (p
&& p
->state
== state
&& p
->status
== status
)
979 shf_fprintf(shf
, "%s%5d %-20s %s%s", filler
, p
->pid
,
980 space
, p
->command
, p
->next
? "|" : null
);
981 else if (how
== JP_MEDIUM
)
982 shf_fprintf(shf
, " %s%s", p
->command
,
983 p
->next
? "|" : null
);
988 shf_fprintf(shf
, newline
);
991 /* Convert % sequence to job
993 * If jobs are compiled in then this routine expects sigchld to be blocked.
996 j_lookup(const char *cp
, int *ecodep
)
1004 /* Look for last_proc->pid (what $! returns) first... */
1005 for (j
= job_list
; j
!= (Job
*) 0; j
= j
->next
)
1006 if (j
->last_proc
&& j
->last_proc
->pid
== job
)
1008 /* ...then look for process group (this is non-POSIX),
1009 * but should not break anything (so FPOSIX isn't used).
1011 for (j
= job_list
; j
!= (Job
*) 0; j
= j
->next
)
1012 if (j
->pgrp
&& j
->pgrp
== job
)
1015 *ecodep
= JL_NOSUCH
;
1020 *ecodep
= JL_INVALID
;
1024 case '\0': /* non-standard */
1027 if (job_list
!= (Job
*) 0)
1032 if (job_list
!= (Job
*) 0 && job_list
->next
)
1033 return job_list
->next
;
1036 case '0': case '1': case '2': case '3': case '4':
1037 case '5': case '6': case '7': case '8': case '9':
1039 for (j
= job_list
; j
!= (Job
*) 0; j
= j
->next
)
1044 case '?': /* %?string */
1045 last_match
= (Job
*) 0;
1046 for (j
= job_list
; j
!= (Job
*) 0; j
= j
->next
)
1047 for (p
= j
->proc_list
; p
!= (Proc
*) 0; p
= p
->next
)
1048 if (strstr(p
->command
, cp
+1) != (char *) 0) {
1060 default: /* %string */
1062 last_match
= (Job
*) 0;
1063 for (j
= job_list
; j
!= (Job
*) 0; j
= j
->next
)
1064 if (strncmp(cp
, j
->proc_list
->command
, len
) == 0) {
1077 *ecodep
= JL_NOSUCH
;
1081 static Job
*free_jobs
;
1082 static Proc
*free_procs
;
1085 /* allocate a new job and fill in the job number.
1087 * If jobs are compiled in then this routine expects sigchld to be blocked.
1095 if (free_jobs
!= (Job
*) 0) {
1097 free_jobs
= free_jobs
->next
;
1099 newj
= (Job
*) alloc(sizeof(Job
), APERM
);
1101 /* brute force method */
1102 for (i
= 1; ; i
++) {
1103 for (j
= job_list
; j
&& j
->job
!= i
; j
= j
->next
)
1113 /* Allocate new process strut
1115 * If jobs are compiled in then this routine expects sigchld to be blocked.
1122 if (free_procs
!= (Proc
*) 0) {
1124 free_procs
= free_procs
->next
;
1126 p
= (Proc
*) alloc(sizeof(Proc
), APERM
);
1132 /* Take job out of job_list and put old structures into free list.
1133 * Keeps nzombies, last_job and async_job up to date.
1135 * If jobs are compiled in then this routine expects sigchld to be blocked.
1138 remove_job(Job
*j
, const char *where
)
1145 for (; curr
!= (Job
*) 0 && curr
!= j
; prev
= &curr
->next
, curr
= *prev
)
1148 internal_errorf(0, "remove_job: job not found (%s)", where
);
1153 /* free up proc structures */
1154 for (p
= j
->proc_list
; p
!= (Proc
*) 0; ) {
1157 tmp
->next
= free_procs
;
1161 if ((j
->flags
& JF_ZOMBIE
) && j
->ppid
== procpid
)
1163 j
->next
= free_jobs
;
1167 last_job
= (Job
*) 0;
1169 async_job
= (Job
*) 0;
1173 /* put j in a particular location (taking it out job_list if it is there
1176 * If jobs are compiled in then this routine expects sigchld to be blocked.
1179 put_job(Job
*j
, int where
)
1183 /* Remove job from list (if there) */
1186 for (; curr
&& curr
!= j
; prev
= &curr
->next
,
1197 case PJ_PAST_STOPPED
:
1200 for (; curr
&& curr
->state
== PSTOPPED
; prev
= &curr
->next
,
1210 /* nuke a job (called when unable to start full job).
1212 * If jobs are compiled in then this routine expects sigchld to be blocked.
1215 kill_job(Job
*j
, int sig
)
1220 for (p
= j
->proc_list
; p
!= (Proc
*) 0; p
= p
->next
)
1222 if (kill(p
->pid
, sig
) < 0)