8000 minix/bin/ksh/jobs.c at master · devinblood/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1858 lines (1686 loc) · 44 KB

jobs.c

File metadata and controls

1858 lines (1686 loc) · 44 KB
/* $NetBSD: jobs.c,v 1.10 2011/10/16 17:12:11 joerg Exp $ */
/*
* Process and job control
*/
/*
* Reworked/Rewritten version of Eric Gisin's/Ron Natalie's code by
* Larry Bouzane (larry@cs.mun.ca) and hacked again by
* Michael Rendell (michael@cs.mun.ca)
*
* The interface to the rest of the shell should probably be changed
* to allow use of vfork() when available but that would be way too much
* work :)
*
* Notes regarding the copious ifdefs:
* - JOB_SIGS is independent of JOBS - it is defined if there are modern
* signal and wait routines available. This is preferred, even when
* JOBS is not defined, since the shell will not otherwise notice when
* backgrou 8000 nd jobs die until the shell waits for a foreground process
* to die.
* - TTY_PGRP defined iff JOBS is defined - defined if there are tty
* process groups
* - NEED_PGRP_SYNC defined iff JOBS is defined - see comment below
*/
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: jobs.c,v 1.10 2011/10/16 17:12:11 joerg Exp $");
#endif
#include "sh.h"
#include "ksh_stat.h"
#include "ksh_wait.h"
#include "ksh_times.h"
#include "tty.h"
/* Start of system configuration stuff */
/* We keep CHILD_MAX zombie processes around (exact value isn't critical) */
#ifndef CHILD_MAX
# if defined(HAVE_SYSCONF) && defined(_SC_CHILD_MAX)
# define CHILD_MAX sysconf(_SC_CHILD_MAX)
# else /* _SC_CHILD_MAX */
# ifdef _POSIX_CHILD_MAX
# define CHILD_MAX ((_POSIX_CHILD_MAX) * 2)
# else /* _POSIX_CHILD_MAX */
# define CHILD_MAX 20
# endif /* _POSIX_CHILD_MAX */
# endif /* _SC_CHILD_MAX */
#endif /* !CHILD_MAX */
#ifdef JOBS
# if defined(HAVE_TCSETPGRP) || defined(TIOCSPGRP)
# define TTY_PGRP
# endif
# ifdef BSD_PGRP
# define setpgid setpgrp
# define getpgID() getpgrp(0)
# else
# define getpgID() getpgrp()
# endif
# if defined(TTY_PGRP) && !defined(HAVE_TCSETPGRP)
int tcsetpgrp ARGS((int fd, pid_t grp));
int tcgetpgrp ARGS((int fd));
int
tcsetpgrp(fd, grp)
int fd;
pid_t grp;
{
return ioctl(fd, TIOCSPGRP, &grp);
}
int
tcgetpgrp(fd)
int fd;
{
int r, grp;
if ((r = ioctl(fd, TIOCGPGRP, &grp)) < 0)
return r;
return grp;
}
# endif /* !HAVE_TCSETPGRP && TIOCSPGRP */
#else /* JOBS */
/* These so we can use ifdef xxx instead of if defined(JOBS) && defined(xxx) */
# undef TTY_PGRP
# undef NEED_PGRP_SYNC
#endif /* JOBS */
/* End of system configuration stuff */
/* Order important! */
#define PRUNNING 0
#define PEXITED 1
#define PSIGNALLED 2
#define PSTOPPED 3
typedef struct proc Proc;
struct proc {
Proc *next; /* next process in pipeline (if any) */
int state;
WAIT_T status; /* wait status */
pid_t pid; /* process id */
char command[48]; /* process command string */
};
/* Notify/print flag - j_print() argument */
#define JP_NONE 0 /* don't print anything */
#define JP_SHORT 1 /* print signals processes were killed by */
#define JP_MEDIUM 2 /* print [job-num] -/+ command */
#define JP_LONG 3 /* print [job-num] -/+ pid command */
#define JP_PGRP 4 /* print pgrp */
/* put_job() flags */
#define PJ_ON_FRONT 0 /* at very front */
#define PJ_PAST_STOPPED 1 /* just past any stopped jobs */
/* Job.flags values */
#define JF_STARTED 0x001 /* set when all processes in job are started */
#define JF_WAITING 0x002 /* set if j_waitj() is waiting on job */
#define JF_W_ASYNCNOTIFY 0x004 /* set if waiting and async notification ok */
#define JF_XXCOM 0x008 /* set for `command` jobs */
#define JF_FG 0x010 /* running in foreground (also has tty pgrp) */
#define JF_SAVEDTTY 0x020 /* j->ttystate is valid */
#define JF_CHANGED 0x040 /* process has changed state */
#define JF_KNOWN 0x080 /* $! referenced */
#define JF_ZOMBIE 0x100 /* known, unwaited process */
#define JF_REMOVE 0x200 /* flagged for removal (j_jobs()/j_noityf()) */
#define JF_USETTYMODE 0x400 /* tty mode saved if process exits normally */
#define JF_SAVEDTTYPGRP 0x800 /* j->saved_ttypgrp is valid */
typedef struct job Job;
struct job {
Job *next; /* next job in list */
int job; /* job number: %n */
int flags; /* see JF_* */
int state; /* job state */
int status; /* exit status of last process */
pid_t pgrp; /* process group of job */
pid_t ppid; /* pid of process that forked job */
INT32 age; /* number of jobs started */
clock_t systime; /* system time used by job */
clock_t usrtime; /* user time used by job */
Proc *proc_list; /* process list */
Proc *last_proc; /* last process in list */
#ifdef KSH
Coproc_id coproc_id; /* 0 or id of coprocess output pipe */
#endif /* KSH */
#ifdef TTY_PGRP
TTY_state ttystate; /* saved tty state for stopped jobs */
pid_t saved_ttypgrp; /* saved tty process group for stopped jobs */
#endif /* TTY_PGRP */
};
/* Flags for j_waitj() */
#define JW_NONE 0x00
#define JW_INTERRUPT 0x01 /* ^C will stop the wait */
#define JW_ASYNCNOTIFY 0x02 /* asynchronous notification during wait ok */
#define JW_STOPPEDWAIT 0x04 /* wait even if job stopped */
/* Error codes for j_lookup() */
#define JL_OK 0
#define JL_NOSUCH 1 /* no such job */
#define JL_AMBIG 2 /* %foo or %?foo is ambiguous */
#define JL_INVALID 3 /* non-pid, non-% job id */
static const char *const lookup_msgs[] = {
null,
"no such job",
"ambiguous",
"argument must be %job or process id",
(char *) 0
};
clock_t j_systime, j_usrtime; /* user and system time of last j_waitjed job */
static Job *job_list; /* job list */
static Job *last_job;
static Job *async_job;
static pid_t async_pid;
static int nzombie; /* # of zombies owned by this process */
static INT32 njobs; /* # of jobs started */
static int child_max; /* CHILD_MAX */
#ifdef JOB_SIGS
/* held_sigchld is set if sigchld occurs before a job is completely started */
static int held_sigchld;
#endif /* JOB_SIGS */
#ifdef JOBS
static struct shf *shl_j;
#endif /* JOBS */
#ifdef NEED_PGRP_SYNC
/* On some systems, the kernel doesn't count zombie processes when checking
* if a process group is valid, which can cause problems in creating the
* pipeline "cmd1 | cmd2": if cmd1 can die (and go into the zombie state)
* before cmd2 is started, the kernel doesn't allow the setpgid() for cmd2
* to succeed. Solution is to create a pipe between the parent and the first
* process; the first process doesn't do anything until the pipe is closed
* and the parent doesn't close the pipe until all the processes are started.
*/
static int j_sync_pipe[2];
static int j_sync_open;
#endif /* NEED_PGRP_SYNC */
#ifdef TTY_PGRP
static int ttypgrp_ok; /* set if can use tty pgrps */
static pid_t restore_ttypgrp = -1;
static pid_t our_pgrp;
static int const tt_sigs[] = { SIGTSTP, SIGTTIN, SIGTTOU };
#endif /* TTY_PGRP */
static void j_set_async ARGS((Job *j));
static void j_startjob ARGS((Job *j));
static int j_waitj ARGS((Job *j, int flags, const char *where));
static RETSIGTYPE j_sigchld ARGS((int sig));
static void j_print ARGS((Job *j, int how, struct shf *shf));
static Job *j_lookup ARGS((const char *cp, int *ecodep));
static Job *new_job ARGS((void));
static Proc *new_proc ARGS((void));
static void check_job ARGS((Job *j));
static void put_job ARGS((Job *j, int where));
static void remove_job ARGS((Job *j, const char *where));
static int kill_job ARGS((Job *j, int sig));
/* initialize job control */
void
j_init(mflagset)
int mflagset;
{
child_max = CHILD_MAX; /* so syscon() isn't always being called */
#ifdef JOB_SIGS
sigemptyset(&sm_default);
sigprocmask(SIG_SETMASK, &sm_default, (sigset_t *) 0);
sigemptyset(&sm_sigchld);
sigaddset(&sm_sigchld, SIGCHLD);
setsig(&sigtraps[SIGCHLD], j_sigchld,
SS_RESTORE_ORIG|SS_FORCE|SS_SHTRAP);
#else /* JOB_SIGS */
/* Make sure SIGCHLD isn't ignored - can do odd things under SYSV */
setsig(&sigtraps[SIGCHLD], SIG_DFL, SS_RESTORE_ORIG|SS_FORCE);
#endif /* JOB_SIGS */
#ifdef JOBS
if (!mflagset && Flag(FTALKING))
Flag(FMONITOR) = 1;
/* shl_j is used to do asynchronous notification (used in
* an interrupt handler, so need a distinct shf)
*/
shl_j = shf_fdopen(2, SHF_WR, (struct shf *) 0);
# ifdef TTY_PGRP
if (Flag(FMONITOR) || Flag(FTALKING)) {
int i;
/* the TF_SHELL_USES test is a kludge that lets us know if
* if the signals have been changed by the shell.
*/
for (i = NELEM(tt_sigs); --i >= 0; ) {
sigtraps[tt_sigs[i]].flags |= TF_SHELL_USES;
/* j_change() sets this to SS_RESTORE_DFL if FMONITOR */
setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
SS_RESTORE_IGN|SS_FORCE);
}
}
# endif /* TTY_PGRP */
/* j_change() calls tty_init() */
if (Flag(FMONITOR))
j_change();
else
#endif /* JOBS */
if (Flag(FTALKING))
tty_init(TRUE);
}
/* job cleanup before shell exit */
void
j_exit()
{
/* kill stopped, and possibly running, jobs */
Job *j;
int killed = 0;
for (j = job_list; j != (Job *) 0; j = j->next) {
if (j->ppid == procpid
&& (j->state == PSTOPPED
|| (j->state == PRUNNING
&& ((j->flags & JF_FG)
|| (Flag(FLOGIN) && !Flag(FNOHUP)
&& procpid == kshpid)))))
{
killed = 1;
if (j->pgrp == 0)
kill_job(j, SIGHUP);
else
killpg(j->pgrp, SIGHUP);
#ifdef JOBS
if (j->state == PSTOPPED) {
if (j->pgrp == 0)
kill_job(j, SIGCONT);
else
killpg(j->pgrp, SIGCONT);
}
#endif /* JOBS */
}
}
if (killed)
sleep(1);
j_notify();
#ifdef JOBS
# ifdef TTY_PGRP
if (kshpid == procpid && restore_ttypgrp >= 0) {
/* Need to restore the tty pgrp to what it was when the
* shell started up, so that the process that started us
* will be able to access the tty when we are done.
* Also need to restore our process group in case we are
* about to do an exec so that both our parent and the
* process we are to become will be able to access the tty.
*/
tcsetpgrp(tty_fd, restore_ttypgrp);
setpgid(0, restore_ttypgrp);
}
# endif /* TTY_PGRP */
if (Flag(FMONITOR)) {
Flag(FMONITOR) = 0;
j_change();
}
#endif /* JOBS */
}
#ifdef JOBS
/* turn job control on or off according to Flag(FMONITOR) */
void
j_change()
{
int i;
if (Flag(FMONITOR)) {
/* Don't call get_tty() 'til we own the tty process group */
tty_init(FALSE);
# ifdef TTY_PGRP
/* no controlling tty, no SIGT* */
ttypgrp_ok = tty_fd >= 0 && tty_devtty;
if (ttypgrp_ok && (our_pgrp = getpgID()) < 0) {
warningf(FALSE, "j_init: getpgrp() failed: %s",
strerror(errno));
ttypgrp_ok = 0;
}
if (ttypgrp_ok) {
setsig(&sigtraps[SIGTTIN], SIG_DFL,
SS_RESTORE_ORIG|SS_FORCE);
/* wait to be given tty (POSIX.1, B.2, job control) */
while (1) {
pid_t ttypgrp;
if ((ttypgrp = tcgetpgrp(tty_fd)) < 0) {
warningf(FALSE,
"j_init: tcgetpgrp() failed: %s",
strerror(errno));
ttypgrp_ok = 0;
break;
}
if (ttypgrp == our_pgrp)
break;
kill(0, SIGTTIN);
}
}
for (i = NELEM(tt_sigs); --i >= 0; )
setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
SS_RESTORE_DFL|SS_FORCE);
if (ttypgrp_ok && our_pgrp != kshpid) {
if (setpgid(0, kshpid) < 0) {
warningf(FALSE,
"j_init: setpgid() failed: %s",
strerror(errno));
ttypgrp_ok = 0;
} else {
if (tcsetpgrp(tty_fd, kshpid) < 0) {
warningf(FALSE,
"j_init: tcsetpgrp() failed: %s",
strerror(errno));
ttypgrp_ok = 0;
} else
restore_ttypgrp = our_pgrp;
our_pgrp = kshpid;
}
}
# if defined(NTTYDISC) && defined(TIOCSETD) && !defined(HAVE_TERMIOS_H) && !defined(HAVE_TERMIO_H)
if (ttypgrp_ok) {
int ldisc = NTTYDISC;
if (ioctl(tty_fd, TIOCSETD, &ldisc) < 0)
warningf(FALSE,
"j_init: can't set new line discipline: %s",
strerror(errno));
}
# endif /* NTTYDISC && TIOCSETD */
if (!ttypgrp_ok)
warningf(FALSE, "warning: won't have full job control");
# endif /* TTY_PGRP */
if (tty_fd >= 0)
get_tty(tty_fd, &tty_state);
} else {
# ifdef TTY_PGRP
ttypgrp_ok = 0;
if (Flag(FTALKING))
for (i = NELEM(tt_sigs); --i >= 0; )
setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
SS_RESTORE_IGN|SS_FORCE);
8 else
for (i = NELEM(tt_sigs); --i >= 0; ) {
if (sigtraps[tt_sigs[i]].flags & (TF_ORIG_IGN
|TF_ORIG_DFL))
setsig(&sigtraps[tt_sigs[i]],
(sigtraps[tt_sigs[i]].flags & TF_ORIG_IGN) ? SIG_IGN : SIG_DFL,
SS_RESTORE_ORIG|SS_FORCE);
}
# endif /* TTY_PGRP */
if (!Flag(FTALKING))
tty_close();
}
}
#endif /* JOBS */
/* execute tree in child subprocess */
int
exchild(t, flags, close_fd)
struct op *t;
int flags;
int close_fd; /* used if XPCLOSE or XCCLOSE */
{
static Proc *last_proc; /* for pipelines */
int i;
#ifdef JOB_SIGS
sigset_t omask;
#endif /* JOB_SIGS */
Proc *p;
Job *j;
int rv = 0;
int forksleep;
int ischild;
if (flags & XEXEC)
/* Clear XFORK|XPCLOSE|XCCLOSE|XCOPROC|XPIPEO|XPIPEI|XXCOM|XBGND
* (also done in another execute() below)
*/
return execute(t, flags & (XEXEC | XERROK));
#ifdef JOB_SIGS
/* no SIGCHLD's while messing with job and process lists */
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
#endif /* JOB_SIGS */
p = new_proc();
p->next = (Proc *) 0;
p->state = PRUNNING;
WSTATUS(p->status) = 0;
p->pid = 0;
/* link process into jobs list */
if (flags&XPIPEI) { /* continuing with a pipe */
if (!last_job)
internal_errorf(1, "exchild: XPIPEI and no last_job - pid %d", (int) procpid);
j = last_job;
last_proc->next = p;
last_proc = p;
} else {
#ifdef NEED_PGRP_SYNC
if (j_sync_open) { /* should never happen */
j_sync_open = 0;
closepipe(j_sync_pipe);
}
/* don't do the sync pipe business if there is no pipeline */
if (flags & XPIPEO) {
openpipe(j_sync_pipe);
j_sync_open = 1;
}
#endif /* NEED_PGRP_SYNC */
j = new_job(); /* fills in j->job */
/* we don't consider XXCOM's foreground since they don't get
* tty process group and we don't save or restore tty modes.
*/
j->flags = (flags & XXCOM) ? JF_XXCOM
: ((flags & XBGND) ? 0 : (JF_FG|JF_USETTYMODE));
j->usrtime = j->systime = 0;
j->state = PRUNNING;
j->pgrp = 0;
j->ppid = procpid;
j->age = ++njobs;
j->proc_list = p;
#ifdef KSH
j->coproc_id = 0;
#endif /* KSH */
last_job = j;
last_proc = p;
put_job(j, PJ_PAST_STOPPED);
}
snptreef(p->command, sizeof(p->command), "%T", t);
/* create child process */
forksleep = 1;
while ((i = fork()) < 0 && errno == EAGAIN && forksleep < 32) {
if (intrsig) /* allow user to ^C out... */
break;
sleep(forksleep);
forksleep <<= 1;
}
if (i < 0) {
kill_job(j, SIGKILL);
remove_job(j, "fork failed");
#ifdef NEED_PGRP_SYNC
if (j_sync_open) {
closepipe(j_sync_pipe);
j_sync_open = 0;
}
#endif /* NEED_PGRP_SYNC */
#ifdef JOB_SIGS
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
#endif /* JOB_SIGS */
errorf("cannot fork - try again");
}
ischild = i == 0;
if (ischild)
p->pid = procpid = getpid();
else
p->pid = i;
#ifdef JOBS
/* job control set up */
if (Flag(FMONITOR) && !(flags&XXCOM)) {
int dotty = 0;
# ifdef NEED_PGRP_SYNC
int first_child_sync = 0;
# endif /* NEED_PGRP_SYNC */
# ifdef NEED_PGRP_SYNC
if (j_sync_open) {
/*
* The Parent closes 0, keeps 1 open 'til the whole
* pipeline is started. The First child closes 1,
* keeps 0 open (reads from it). The remaining
* children just have to close 1 (parent has already
* closeed 0).
*/
if (j->pgrp == 0) { /* First process */
close(j_sync_pipe[ischild]);
j_sync_pipe[ischild] = -1;
first_child_sync = ischild;
} else if (ischild) {
j_sync_open = 0;
closepipe(j_sync_pipe);
}
}
# endif /* NEED_PGRP_SYNC */
if (j->pgrp == 0) { /* First process */
j->pgrp = p->pid;
dotty = 1;
}
/* set pgrp in both parent and child to deal with race
* condition
*/
setpgid(p->pid, j->pgrp);
# ifdef TTY_PGRP
/* YYY: should this be
if (ttypgrp_ok && ischild && !(flags&XBGND))
tcsetpgrp(tty_fd, j->pgrp);
instead? (see also YYY below)
*/
if (ttypgrp_ok && dotty && !(flags & XBGND))
tcsetpgrp(tty_fd, j->pgrp);
# endif /* TTY_PGRP */
# ifdef NEED_PGRP_SYNC
if (first_child_sync) {
char c;
while (read(j_sync_pipe[0], &c, 1) == -1
&& errno == EINTR)
;
close(j_sync_pipe[0]);
j_sync_open = 0;
}
# endif /* NEED_PGRP_SYNC */
}
#endif /* JOBS */
/* used to close pipe input fd */
if (close_fd >= 0 && (((flags & XPCLOSE) && !ischild)
|| ((flags & XCCLOSE) && ischild)))
close(close_fd);
if (ischild) { /* child */
#ifdef KSH
/* Do this before restoring signal */
if (flags & XCOPROC)
coproc_cleanup(FALSE);
#endif /* KSH */
#ifdef JOB_SIGS
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
#endif /* JOB_SIGS */
cleanup_parents_env();
#ifdef TTY_PGRP
/* If FMONITOR or FTALKING is set, these signals are ignored,
* if neither FMONITOR nor FTALKING are set, the signals have
* their inherited values.
*/
if (Flag(FMONITOR) && !(flags & XXCOM)) {
for (i = NELEM(tt_sigs); --i >= 0; )
setsig(&sigtraps[tt_sigs[i]], SIG_DFL,
SS_RESTORE_DFL|SS_FORCE);
}
#endif /* TTY_PGRP */
#ifdef HAVE_NICE
if (Flag(FBGNICE) && (flags & XBGND))
nice(4);
#endif /* HAVE_NICE */
if ((flags & XBGND) && !Flag(FMONITOR)) {
setsig(&sigtraps[SIGINT], SIG_IGN,
SS_RESTORE_IGN|SS_FORCE);
setsig(&sigtraps[SIGQUIT], SIG_IGN,
SS_RESTORE_IGN|SS_FORCE);
if (!(flags & (XPIPEI | XCOPROC))) {
int fd = open("/dev/null", 0);
if (fd != 0) {
(void) ksh_dup2(fd, 0, TRUE);
close(fd);
}
}
}
remove_job(j, "child"); /* in case of `jobs` command */
nzombie = 0;
#ifdef JOBS
ttypgrp_ok = 0;
Flag(FMONITOR) = 0;
#endif /* JOBS */
Flag(FTALKING) = 0;
#ifdef OS2
if (tty_fd >= 0)
flags |= XINTACT;
#endif /* OS2 */
tty_close();
cleartraps();
execute(t, (flags & XERROK) | XEXEC); /* no return */
internal_errorf(0, "exchild: execute() returned");
unwind(LLEAVE);
/* NOTREACHED */
}
/* shell (parent) stuff */
/* Ensure next child gets a (slightly) different $RANDOM sequence */
change_random();
if (!(flags & XPIPEO)) { /* last process in a job */
#ifdef TTY_PGRP
/* YYY: Is this needed? (see also YYY above)
if (Flag(FMONITOR) && !(flags&(XXCOM|XBGND)))
tcsetpgrp(tty_fd, j->pgrp);
*/
#endif /* TTY_PGRP */
j_startjob(j);
#ifdef KSH
if (flags & XCOPROC) {
j->coproc_id = coproc.id;
coproc.njobs++; /* n jobs using co-process output */
coproc.job = (void *) j; /* j using co-process input */
}
#endif /* KSH */
if (flags & XBGND) {
j_set_async(j);
if (Flag(FTALKING)) {
shf_fprintf(shl_out, "[%d]", j->job);
for (p = j->proc_list; p; p = p->next)
shf_fprintf(shl_out, " %d", p->pid);
shf_putchar('\n', shl_out);
shf_flush(shl_out);
}
} else
rv = j_waitj(j, JW_NONE, "jw:last proc");
}
#ifdef JOB_SIGS
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
#endif /* JOB_SIGS */
return rv;
}
/* start the last job: only used for `command` jobs */
void
startlast()
{
#ifdef JOB_SIGS
sigset_t omask;
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
#endif /* JOB_SIGS */
if (last_job) { /* no need to report error - waitlast() will do it */
/* ensure it isn't removed by check_job() */
last_job->flags |= JF_WAITING;
j_startjob(last_job);
}
#ifdef JOB_SIGS
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
#endif /* JOB_SIGS */
}
/* wait for last job: only used for `command` jobs */
int
waitlast()
{
int rv;
Job *j;
#ifdef JOB_SIGS
sigset_t omask;
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
#endif /* JOB_SIGS */
j = last_job;
if (!j || !(j->flags & JF_STARTED)) {
if (!j)
warningf(TRUE, "waitlast: no last job");
else
internal_errorf(0, "waitlast: not started");
#ifdef JOB_SIGS
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
#endif /* JOB_SIGS */
return 125; /* not so arbitrary, non-zero value */
}
rv = j_waitj(j, JW_NONE, "jw:waitlast");
#ifdef JOB_SIGS
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
#endif /* JOB_SIGS */
return rv;
}
/* wait for child, interruptable. */
int
waitfor(cp, sigp)
const char *cp;
int *sigp;
{
int rv;
Job *j;
int ecode;
int flags = JW_INTERRUPT|JW_ASYNCNOTIFY;
#ifdef JOB_SIGS
sigset_t omask;
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
#endif /* JOB_SIGS */
*sigp = 0;
if (cp == (char *) 0) {
/* wait for an unspecified job - always returns 0, so
* don't have to worry about exited/signaled jobs
*/
for (j = job_list; j; j = j->next)
/* at&t ksh will wait for stopped jobs - we don't */
if (j->ppid == procpid && j->state == PRUNNING)
break;
if (!j) {
#ifdef JOB_SIGS
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
#endif /* JOB_SIGS */
return -1;
}
} else if ((j = j_lookup(cp, &ecode))) {
/* don't report normal job completion */
flags &= ~JW_ASYNCNOTIFY;
if (j->ppid != procpid) {
#ifdef JOB_SIGS
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
#endif /* JOB_SIGS */
return -1;
}
} else {
#ifdef JOB_SIGS
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
#endif /* JOB_SIGS */
if (ecode != JL_NOSUCH)
bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
return -1;
}
/* at&t ksh will wait for stopped jobs - we don't */
rv = j_waitj(j, flags, "jw:waitfor");
#ifdef JOB_SIGS
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
#endif /* JOB_SIGS */
if (rv < 0) /* we were interrupted */
*sigp = 128 + -rv;
return rv;
}
/* kill (built-in) a job */
int
j_kill(cp, sig)
const char *cp;
int sig;
{
Job *j;
int rv = 0;
int ecode;
#ifdef JOB_SIGS
sigset_t omask;
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
#endif /* JOB_SIGS */
if ((j = j_lookup(cp, &ecode)) == (Job *) 0) {
#ifdef JOB_SIGS
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
#endif /* JOB_SIGS */
bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
return 1;
}
if (j->pgrp == 0) { /* started when !Flag(FMONITOR) */
if (kill_job(j, sig) < 0) {
bi_errorf("%s: %s", cp, strerror(errno));
rv = 1;
}
} else {
#ifdef JOBS
if (j->state == PSTOPPED && (sig == SIGTERM || sig == SIGHUP))
(void) killpg(j->pgrp, SIGCONT);
#endif /* JOBS */
if (killpg(j->pgrp, sig) < 0) {
bi_errorf("%s: %s", cp, strerror(errno));
rv = 1;
}
}
#ifdef JOB_SIGS
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
#endif /* JOB_SIGS */
return rv;
}
#ifdef JOBS
/* fg and bg built-ins: called only if Flag(FMONITOR) set */
int
j_resume(cp, bg)
const char *cp;
int bg;
{
Job *j;
Proc *p;
int ecode;
int running;
int rv = 0;
sigset_t omask;
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
if ((j = j_lookup(cp, &ecode)) == (Job *) 0) {
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
return 1;
}
if (j->pgrp == 0) {
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
bi_errorf("job not job-controlled");
return 1;
}
if (bg)
shprintf("[%d] ", j->job);
running = 0;
for (p = j->proc_list; p != (Proc *) 0; p = p->next) {
if (p->state == PSTOPPED) {
p->state = PRUNNING;
WSTATUS(p->status) = 0;
running = 1;
}
shprintf("%s%s", p->command, p->next ? "| " : null);
}
shprintf("%s", newline);
shf_flush(shl_stdout);
if (running)
j->state = PRUNNING;
put_job(j, PJ_PAST_STOPPED);
if (bg)
j_set_async(j);
else {
# ifdef TTY_PGRP
/* attach tty to job */
if (j->state == PRUNNING) {
if (ttypgrp_ok && (j->flags & JF_SAVEDTTY)) {
set_tty(tty_fd, &j->ttystate, TF_NONE);
}
/* See comment in j_waitj regarding saved_ttypgrp. */
if (ttypgrp_ok && tcsetpgrp(tty_fd, (j->flags & JF_SAVEDTTYPGRP) ? j->saved_ttypgrp : j->pgrp) < 0) {
if (j->flags & JF_SAVEDTTY) {
set_tty(tty_fd, &tty_state, TF_NONE);
}
sigprocmask(SIG_SETMASK, &omask,
(sigset_t *) 0);
bi_errorf("1st tcsetpgrp(%d, %d) failed: %s",
tty_fd, (int) ((j->flags & JF_SAVEDTTYPGRP) ? j->saved_ttypgrp : j->pgrp), strerror(errno));
return 1;
}
}
# endif /* TTY_PGRP */
j->flags |= JF_FG;
j->flags &= ~JF_KNOWN;
if (j == async_job)
async_job = (Job *) 0;
}
if (j->state == PRUNNING && killpg(j->pgrp, SIGCONT) < 0) {
int err = errno;
if (!bg) {
j->flags &= ~JF_FG;
# ifdef TTY_PGRP
if (ttypgrp_ok && (j->flags & JF_SAVEDTTY)) {
set_tty(tty_fd, &tty_state, TF_NONE);
}
if (ttypgrp_ok && tcsetpgrp(tty_fd, our_pgrp) < 0) {
warningf(TRUE,
"fg: 2nd tcsetpgrp(%d, %d) failed: %s",
tty_fd, (int) our_pgrp,
strerror(errno));
}
# endif /* TTY_PGRP */
}
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
bi_errorf("cannot continue job %s: %s",
cp, strerror(err));
return 1;
}
if (!bg) {
# ifdef TTY_PGRP
if (ttypgrp_ok) {
j->flags &= ~(JF_SAVEDTTY | JF_SAVEDTTYPGRP);
}
# endif /* TTY_PGRP */
rv = j_waitj(j, JW_NONE, "jw:resume");
}
sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
return rv;
}
#endif /* JOBS */
/* are there any running or stopped jobs ? */
int
j_stopped_running()
{
Job *j;
int which = 0;
for (j = job_list; j != (Job *) 0; j = j->next) {
#ifdef JOBS
if (j->ppid == procpid && j->state == PSTOPPED)
which |= 1;
#endif /* JOBS */
if (Flag(FLOGIN) && !Flag(FNOHUP) && procpid == kshpid
&& j->ppid == procpid && j->state == PRUNNING)
which |= 2;
}
if (which) {
shellf("You have %s%s%s jobs\n",
which & 1 ? "stopped" : "",
which == 3 ? " and " : "",
which & 2 ? "running" : "");
return 1;
}
return 0;
}
/* list jobs for jobs built-in */
int
0