1994-05-26 06:18:55 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Kenneth Almquist.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-05-18 06:44:24 +00:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95";
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:15:05 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2010-10-13 13:22:11 +00:00
|
|
|
#include <sys/ioctl.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/resource.h>
|
2010-10-13 13:22:11 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2000-12-09 09:35:55 +00:00
|
|
|
#include <paths.h>
|
2010-10-13 13:22:11 +00:00
|
|
|
#include <signal.h>
|
2010-10-16 12:40:00 +00:00
|
|
|
#include <stddef.h>
|
2010-10-13 13:22:11 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "shell.h"
|
|
|
|
#if JOBS
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <termios.h>
|
1994-05-26 06:18:55 +00:00
|
|
|
#undef CEOF /* syntax.h redefines this */
|
|
|
|
#endif
|
1996-09-01 10:22:36 +00:00
|
|
|
#include "redir.h"
|
2012-02-04 23:12:14 +00:00
|
|
|
#include "exec.h"
|
1996-09-01 10:22:36 +00:00
|
|
|
#include "show.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "main.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "nodes.h"
|
|
|
|
#include "jobs.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "trap.h"
|
|
|
|
#include "syntax.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "memalloc.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "mystring.h"
|
2011-06-12 23:06:04 +00:00
|
|
|
#include "var.h"
|
2011-06-13 21:03:27 +00:00
|
|
|
#include "builtins.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
2010-10-13 04:01:01 +00:00
|
|
|
static struct job *jobtab; /* array of jobs */
|
|
|
|
static int njobs; /* size of array */
|
2013-07-25 19:48:15 +00:00
|
|
|
static pid_t backgndpid = -1; /* pid of last background process */
|
|
|
|
static struct job *bgjob = NULL; /* last background process */
|
1994-05-26 06:18:55 +00:00
|
|
|
#if JOBS
|
2010-10-13 04:01:01 +00:00
|
|
|
static struct job *jobmru; /* most recently used job list */
|
|
|
|
static pid_t initialpgrp; /* pgrp of shell on invocation */
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif
|
2002-07-11 06:42:11 +00:00
|
|
|
static int ttyfd = -1;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2012-07-29 18:04:38 +00:00
|
|
|
/* mode flags for dowait */
|
|
|
|
#define DOWAIT_BLOCK 0x1 /* wait until a child exits */
|
2013-09-02 21:57:46 +00:00
|
|
|
#define DOWAIT_SIG 0x2 /* if DOWAIT_BLOCK, abort on SIGINT/SIGQUIT */
|
|
|
|
#define DOWAIT_SIG_ANY 0x4 /* if DOWAIT_SIG, abort on any signal */
|
2012-07-29 18:04:38 +00:00
|
|
|
|
1996-12-14 06:20:03 +00:00
|
|
|
#if JOBS
|
2010-10-13 22:18:03 +00:00
|
|
|
static void restartjob(struct job *);
|
1996-12-14 06:20:03 +00:00
|
|
|
#endif
|
2010-10-13 22:18:03 +00:00
|
|
|
static void freejob(struct job *);
|
2013-06-05 19:08:22 +00:00
|
|
|
static int waitcmdloop(struct job *);
|
2014-03-14 21:45:37 +00:00
|
|
|
static struct job *getjob_nonotfound(const char *);
|
|
|
|
static struct job *getjob(const char *);
|
2014-03-15 14:58:48 +00:00
|
|
|
pid_t killjob(const char *, int);
|
2010-10-13 22:18:03 +00:00
|
|
|
static pid_t dowait(int, struct job *);
|
|
|
|
static void checkzombies(void);
|
|
|
|
static void cmdtxt(union node *);
|
|
|
|
static void cmdputs(const char *);
|
2002-05-31 12:31:23 +00:00
|
|
|
#if JOBS
|
2010-10-13 22:18:03 +00:00
|
|
|
static void setcurjob(struct job *);
|
|
|
|
static void deljob(struct job *);
|
|
|
|
static struct job *getcurjob(struct job *);
|
2002-05-31 12:31:23 +00:00
|
|
|
#endif
|
2010-12-05 22:37:01 +00:00
|
|
|
static void printjobcmd(struct job *);
|
|
|
|
static void showjob(struct job *, int);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Turn job control on and off.
|
|
|
|
*/
|
|
|
|
|
2013-07-25 19:48:15 +00:00
|
|
|
static int jobctl;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1996-12-14 06:20:03 +00:00
|
|
|
#if JOBS
|
2014-09-04 21:48:33 +00:00
|
|
|
static void
|
|
|
|
jobctl_notty(void)
|
|
|
|
{
|
|
|
|
if (ttyfd >= 0) {
|
|
|
|
close(ttyfd);
|
|
|
|
ttyfd = -1;
|
|
|
|
}
|
|
|
|
if (!iflag) {
|
|
|
|
setsignal(SIGTSTP);
|
|
|
|
setsignal(SIGTTOU);
|
|
|
|
setsignal(SIGTTIN);
|
|
|
|
jobctl = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
out2fmt_flush("sh: can't access tty; job control turned off\n");
|
|
|
|
mflag = 0;
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
setjobctl(int on)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2002-07-11 06:42:11 +00:00
|
|
|
int i;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
if (on == jobctl || rootshell == 0)
|
|
|
|
return;
|
|
|
|
if (on) {
|
2002-07-11 06:42:11 +00:00
|
|
|
if (ttyfd != -1)
|
|
|
|
close(ttyfd);
|
2013-05-05 10:51:40 +00:00
|
|
|
if ((ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) < 0) {
|
2002-07-11 06:42:11 +00:00
|
|
|
i = 0;
|
|
|
|
while (i <= 2 && !isatty(i))
|
|
|
|
i++;
|
2013-05-05 10:51:40 +00:00
|
|
|
if (i > 2 ||
|
2014-09-04 21:48:33 +00:00
|
|
|
(ttyfd = fcntl(i, F_DUPFD_CLOEXEC, 10)) < 0) {
|
|
|
|
jobctl_notty();
|
|
|
|
return;
|
|
|
|
}
|
2002-07-11 06:42:11 +00:00
|
|
|
}
|
2003-01-27 07:41:12 +00:00
|
|
|
if (ttyfd < 10) {
|
|
|
|
/*
|
|
|
|
* Keep our TTY file descriptor out of the way of
|
|
|
|
* the user's redirections.
|
|
|
|
*/
|
2013-05-05 10:51:40 +00:00
|
|
|
if ((i = fcntl(ttyfd, F_DUPFD_CLOEXEC, 10)) < 0) {
|
2014-09-04 21:48:33 +00:00
|
|
|
jobctl_notty();
|
|
|
|
return;
|
2003-01-27 07:41:12 +00:00
|
|
|
}
|
|
|
|
close(ttyfd);
|
|
|
|
ttyfd = i;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
do { /* while we are in the background */
|
2002-07-11 06:42:11 +00:00
|
|
|
initialpgrp = tcgetpgrp(ttyfd);
|
1996-12-14 06:20:03 +00:00
|
|
|
if (initialpgrp < 0) {
|
2014-09-04 21:48:33 +00:00
|
|
|
jobctl_notty();
|
1994-05-26 06:18:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-12-12 22:59:34 +00:00
|
|
|
if (initialpgrp != getpgrp()) {
|
2014-09-04 21:48:33 +00:00
|
|
|
if (!iflag) {
|
|
|
|
initialpgrp = -1;
|
|
|
|
jobctl_notty();
|
|
|
|
return;
|
|
|
|
}
|
2010-12-12 22:59:34 +00:00
|
|
|
kill(0, SIGTTIN);
|
1994-05-26 06:18:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
setsignal(SIGTSTP);
|
|
|
|
setsignal(SIGTTOU);
|
|
|
|
setsignal(SIGTTIN);
|
1996-09-01 10:22:36 +00:00
|
|
|
setpgid(0, rootpid);
|
2002-07-11 06:42:11 +00:00
|
|
|
tcsetpgrp(ttyfd, rootpid);
|
1994-05-26 06:18:55 +00:00
|
|
|
} else { /* turning job control off */
|
1996-09-01 10:22:36 +00:00
|
|
|
setpgid(0, initialpgrp);
|
2014-09-04 21:48:33 +00:00
|
|
|
if (ttyfd >= 0) {
|
|
|
|
tcsetpgrp(ttyfd, initialpgrp);
|
|
|
|
close(ttyfd);
|
|
|
|
ttyfd = -1;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
setsignal(SIGTSTP);
|
|
|
|
setsignal(SIGTTOU);
|
|
|
|
setsignal(SIGTTIN);
|
|
|
|
}
|
|
|
|
jobctl = on;
|
|
|
|
}
|
1996-12-14 06:20:03 +00:00
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
#if JOBS
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2013-08-16 13:56:43 +00:00
|
|
|
fgcmd(int argc __unused, char **argv __unused)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct job *jp;
|
2002-07-18 10:22:42 +00:00
|
|
|
pid_t pgrp;
|
1994-05-26 06:18:55 +00:00
|
|
|
int status;
|
|
|
|
|
2013-08-16 13:56:43 +00:00
|
|
|
nextopt("");
|
|
|
|
jp = getjob(*argptr);
|
1994-05-26 06:18:55 +00:00
|
|
|
if (jp->jobctl == 0)
|
|
|
|
error("job not created under job control");
|
2010-12-05 22:37:01 +00:00
|
|
|
printjobcmd(jp);
|
2002-05-19 07:27:26 +00:00
|
|
|
flushout(&output);
|
1994-05-26 06:18:55 +00:00
|
|
|
pgrp = jp->ps[0].pid;
|
2014-09-04 21:48:33 +00:00
|
|
|
if (ttyfd >= 0)
|
|
|
|
tcsetpgrp(ttyfd, pgrp);
|
1994-05-26 06:18:55 +00:00
|
|
|
restartjob(jp);
|
2002-07-18 09:37:51 +00:00
|
|
|
jp->foreground = 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
INTOFF;
|
1999-04-21 11:52:39 +00:00
|
|
|
status = waitforjob(jp, (int *)NULL);
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
bgcmd(int argc, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct job *jp;
|
|
|
|
|
2013-08-16 13:56:43 +00:00
|
|
|
nextopt("");
|
1994-05-26 06:18:55 +00:00
|
|
|
do {
|
2013-08-16 13:56:43 +00:00
|
|
|
jp = getjob(*argptr);
|
1994-05-26 06:18:55 +00:00
|
|
|
if (jp->jobctl == 0)
|
|
|
|
error("job not created under job control");
|
2002-05-19 07:27:26 +00:00
|
|
|
if (jp->state == JOBDONE)
|
|
|
|
continue;
|
1994-05-26 06:18:55 +00:00
|
|
|
restartjob(jp);
|
2002-07-18 09:37:51 +00:00
|
|
|
jp->foreground = 0;
|
2010-12-12 22:59:34 +00:00
|
|
|
out1fmt("[%td] ", jp - jobtab + 1);
|
2010-12-05 22:37:01 +00:00
|
|
|
printjobcmd(jp);
|
2013-08-16 13:56:43 +00:00
|
|
|
} while (*argptr != NULL && *++argptr != NULL);
|
1994-05-26 06:18:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
restartjob(struct job *jp)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct procstat *ps;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (jp->state == JOBDONE)
|
|
|
|
return;
|
2002-05-31 12:35:34 +00:00
|
|
|
setcurjob(jp);
|
1994-05-26 06:18:55 +00:00
|
|
|
INTOFF;
|
2010-12-12 22:59:34 +00:00
|
|
|
kill(-jp->ps[0].pid, SIGCONT);
|
1994-05-26 06:18:55 +00:00
|
|
|
for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
|
1997-05-24 21:04:55 +00:00
|
|
|
if (WIFSTOPPED(ps->status)) {
|
1994-05-26 06:18:55 +00:00
|
|
|
ps->status = -1;
|
|
|
|
jp->state = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2012-09-15 21:56:30 +00:00
|
|
|
jobscmd(int argc __unused, char *argv[] __unused)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2002-05-31 14:04:23 +00:00
|
|
|
char *id;
|
2006-10-07 16:51:16 +00:00
|
|
|
int ch, mode;
|
2002-05-31 14:04:23 +00:00
|
|
|
|
2006-10-07 16:51:16 +00:00
|
|
|
mode = SHOWJOBS_DEFAULT;
|
2012-09-15 21:56:30 +00:00
|
|
|
while ((ch = nextopt("lps")) != '\0') {
|
2002-05-31 14:04:23 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'l':
|
2006-10-07 16:51:16 +00:00
|
|
|
mode = SHOWJOBS_VERBOSE;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
mode = SHOWJOBS_PGIDS;
|
2002-05-31 14:04:23 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2006-10-07 16:51:16 +00:00
|
|
|
mode = SHOWJOBS_PIDS;
|
2002-05-31 14:04:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-15 21:56:30 +00:00
|
|
|
if (*argptr == NULL)
|
2006-10-07 16:51:16 +00:00
|
|
|
showjobs(0, mode);
|
2002-05-31 14:04:23 +00:00
|
|
|
else
|
2012-09-15 21:56:30 +00:00
|
|
|
while ((id = *argptr++) != NULL)
|
2010-12-05 22:37:01 +00:00
|
|
|
showjob(getjob(id), mode);
|
2002-05-31 14:04:23 +00:00
|
|
|
|
|
|
|
return (0);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2010-12-05 22:37:01 +00:00
|
|
|
printjobcmd(struct job *jp)
|
|
|
|
{
|
|
|
|
struct procstat *ps;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
|
|
|
|
out1str(ps->cmd);
|
|
|
|
if (i > 0)
|
|
|
|
out1str(" | ");
|
|
|
|
}
|
|
|
|
out1c('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
showjob(struct job *jp, int mode)
|
2002-05-31 13:07:05 +00:00
|
|
|
{
|
|
|
|
char s[64];
|
2010-12-05 22:37:01 +00:00
|
|
|
char statestr[64];
|
2012-12-25 14:17:09 +00:00
|
|
|
const char *sigstr;
|
2002-05-31 13:07:05 +00:00
|
|
|
struct procstat *ps;
|
2002-05-31 14:04:23 +00:00
|
|
|
struct job *j;
|
|
|
|
int col, curr, i, jobno, prev, procno;
|
|
|
|
char c;
|
2002-05-31 13:07:05 +00:00
|
|
|
|
2006-10-07 16:51:16 +00:00
|
|
|
procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
|
2002-05-31 13:07:05 +00:00
|
|
|
jobno = jp - jobtab + 1;
|
2002-05-31 14:04:23 +00:00
|
|
|
curr = prev = 0;
|
|
|
|
#if JOBS
|
|
|
|
if ((j = getcurjob(NULL)) != NULL) {
|
|
|
|
curr = j - jobtab + 1;
|
|
|
|
if ((j = getcurjob(j)) != NULL)
|
|
|
|
prev = j - jobtab + 1;
|
|
|
|
}
|
|
|
|
#endif
|
2010-12-05 22:37:01 +00:00
|
|
|
ps = jp->ps + jp->nprocs - 1;
|
|
|
|
if (jp->state == 0) {
|
|
|
|
strcpy(statestr, "Running");
|
|
|
|
#if JOBS
|
|
|
|
} else if (jp->state == JOBSTOPPED) {
|
|
|
|
while (!WIFSTOPPED(ps->status) && ps > jp->ps)
|
|
|
|
ps--;
|
|
|
|
if (WIFSTOPPED(ps->status))
|
|
|
|
i = WSTOPSIG(ps->status);
|
|
|
|
else
|
|
|
|
i = -1;
|
2012-12-25 14:17:09 +00:00
|
|
|
sigstr = strsignal(i);
|
|
|
|
if (sigstr != NULL)
|
|
|
|
strcpy(statestr, sigstr);
|
2010-12-05 22:37:01 +00:00
|
|
|
else
|
|
|
|
strcpy(statestr, "Suspended");
|
|
|
|
#endif
|
|
|
|
} else if (WIFEXITED(ps->status)) {
|
|
|
|
if (WEXITSTATUS(ps->status) == 0)
|
|
|
|
strcpy(statestr, "Done");
|
|
|
|
else
|
2010-12-05 22:56:46 +00:00
|
|
|
fmtstr(statestr, 64, "Done(%d)",
|
2010-12-05 22:37:01 +00:00
|
|
|
WEXITSTATUS(ps->status));
|
|
|
|
} else {
|
|
|
|
i = WTERMSIG(ps->status);
|
2012-12-25 14:17:09 +00:00
|
|
|
sigstr = strsignal(i);
|
|
|
|
if (sigstr != NULL)
|
|
|
|
strcpy(statestr, sigstr);
|
2010-12-05 22:37:01 +00:00
|
|
|
else
|
2012-12-25 14:17:09 +00:00
|
|
|
strcpy(statestr, "Unknown signal");
|
2010-12-05 22:37:01 +00:00
|
|
|
if (WCOREDUMP(ps->status))
|
|
|
|
strcat(statestr, " (core dumped)");
|
|
|
|
}
|
|
|
|
|
2014-10-05 21:51:36 +00:00
|
|
|
for (ps = jp->ps ; procno > 0 ; ps++, procno--) { /* for each process */
|
2006-10-07 16:51:16 +00:00
|
|
|
if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
|
2010-12-05 16:09:03 +00:00
|
|
|
out1fmt("%d\n", (int)ps->pid);
|
2014-10-05 21:51:36 +00:00
|
|
|
continue;
|
2002-05-31 14:04:23 +00:00
|
|
|
}
|
2010-12-05 22:37:01 +00:00
|
|
|
if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
|
2014-10-05 21:51:36 +00:00
|
|
|
continue;
|
2002-06-04 14:37:13 +00:00
|
|
|
if (jobno == curr && ps == jp->ps)
|
2002-05-31 14:04:23 +00:00
|
|
|
c = '+';
|
2002-06-04 14:37:13 +00:00
|
|
|
else if (jobno == prev && ps == jp->ps)
|
2002-05-31 14:04:23 +00:00
|
|
|
c = '-';
|
|
|
|
else
|
|
|
|
c = ' ';
|
2002-05-31 13:07:05 +00:00
|
|
|
if (ps == jp->ps)
|
2002-05-31 14:04:23 +00:00
|
|
|
fmtstr(s, 64, "[%d] %c ", jobno, c);
|
2002-05-31 13:07:05 +00:00
|
|
|
else
|
2002-06-04 13:01:51 +00:00
|
|
|
fmtstr(s, 64, " %c ", c);
|
2002-05-31 13:07:05 +00:00
|
|
|
out1str(s);
|
|
|
|
col = strlen(s);
|
2006-10-07 16:51:16 +00:00
|
|
|
if (mode == SHOWJOBS_VERBOSE) {
|
2002-07-18 10:22:42 +00:00
|
|
|
fmtstr(s, 64, "%d ", (int)ps->pid);
|
2002-05-31 14:04:23 +00:00
|
|
|
out1str(s);
|
|
|
|
col += strlen(s);
|
|
|
|
}
|
2010-12-05 22:37:01 +00:00
|
|
|
if (ps == jp->ps) {
|
|
|
|
out1str(statestr);
|
|
|
|
col += strlen(statestr);
|
2002-05-31 13:07:05 +00:00
|
|
|
}
|
|
|
|
do {
|
|
|
|
out1c(' ');
|
|
|
|
col++;
|
|
|
|
} while (col < 30);
|
2010-12-05 22:37:01 +00:00
|
|
|
if (mode == SHOWJOBS_VERBOSE) {
|
|
|
|
out1str(ps->cmd);
|
|
|
|
out1c('\n');
|
|
|
|
} else
|
|
|
|
printjobcmd(jp);
|
2002-05-31 13:07:05 +00:00
|
|
|
}
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Print a list of jobs. If "change" is nonzero, only print jobs whose
|
|
|
|
* statuses have changed since the last call to showjobs.
|
|
|
|
*
|
|
|
|
* If the shell is interrupted in the process of creating a job, the
|
|
|
|
* result may be a job structure containing zero processes. Such structures
|
|
|
|
* will be freed here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2006-10-07 16:51:16 +00:00
|
|
|
showjobs(int change, int mode)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int jobno;
|
|
|
|
struct job *jp;
|
|
|
|
|
|
|
|
TRACE(("showjobs(%d) called\n", change));
|
2010-05-24 10:35:57 +00:00
|
|
|
checkzombies();
|
1994-05-26 06:18:55 +00:00
|
|
|
for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
|
|
|
|
if (! jp->used)
|
|
|
|
continue;
|
|
|
|
if (jp->nprocs == 0) {
|
|
|
|
freejob(jp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (change && ! jp->changed)
|
|
|
|
continue;
|
2010-12-05 22:37:01 +00:00
|
|
|
showjob(jp, mode);
|
2013-04-27 14:07:25 +00:00
|
|
|
if (mode == SHOWJOBS_DEFAULT || mode == SHOWJOBS_VERBOSE) {
|
|
|
|
jp->changed = 0;
|
|
|
|
/* Hack: discard jobs for which $! has not been
|
|
|
|
* referenced in interactive mode when they terminate.
|
|
|
|
*/
|
|
|
|
if (jp->state == JOBDONE && !jp->remembered &&
|
|
|
|
(iflag || jp != bgjob)) {
|
|
|
|
freejob(jp);
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark a job structure as unused.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
freejob(struct job *jp)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct procstat *ps;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
INTOFF;
|
2010-06-29 22:37:45 +00:00
|
|
|
if (bgjob == jp)
|
|
|
|
bgjob = NULL;
|
1994-05-26 06:18:55 +00:00
|
|
|
for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
|
|
|
|
if (ps->cmd != nullstr)
|
|
|
|
ckfree(ps->cmd);
|
|
|
|
}
|
|
|
|
if (jp->ps != &jp->ps0)
|
|
|
|
ckfree(jp->ps);
|
|
|
|
jp->used = 0;
|
|
|
|
#if JOBS
|
2002-05-31 12:31:23 +00:00
|
|
|
deljob(jp);
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2013-03-15 20:29:31 +00:00
|
|
|
waitcmd(int argc __unused, char **argv __unused)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct job *job;
|
2013-06-05 19:08:22 +00:00
|
|
|
int retval;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2013-03-15 20:29:31 +00:00
|
|
|
nextopt("");
|
2013-06-05 19:08:22 +00:00
|
|
|
if (*argptr == NULL)
|
|
|
|
return (waitcmdloop(NULL));
|
|
|
|
|
|
|
|
do {
|
2013-06-05 19:40:52 +00:00
|
|
|
job = getjob_nonotfound(*argptr);
|
|
|
|
if (job == NULL)
|
|
|
|
retval = 127;
|
|
|
|
else
|
|
|
|
retval = waitcmdloop(job);
|
2013-06-05 19:08:22 +00:00
|
|
|
argptr++;
|
|
|
|
} while (*argptr != NULL);
|
|
|
|
|
|
|
|
return (retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
waitcmdloop(struct job *job)
|
|
|
|
{
|
2013-09-02 21:57:46 +00:00
|
|
|
int status, retval, sig;
|
2013-06-05 19:08:22 +00:00
|
|
|
struct job *jp;
|
1998-08-25 09:33:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop until a process is terminated or stopped, or a SIGINT is
|
|
|
|
* received.
|
|
|
|
*/
|
|
|
|
|
|
|
|
do {
|
1994-05-26 06:18:55 +00:00
|
|
|
if (job != NULL) {
|
2013-08-24 09:57:32 +00:00
|
|
|
if (job->state == JOBDONE) {
|
1994-05-26 06:18:55 +00:00
|
|
|
status = job->ps[job->nprocs - 1].status;
|
1997-05-24 21:04:55 +00:00
|
|
|
if (WIFEXITED(status))
|
|
|
|
retval = WEXITSTATUS(status);
|
1994-05-26 06:18:55 +00:00
|
|
|
else
|
1997-05-24 21:04:55 +00:00
|
|
|
retval = WTERMSIG(status) + 128;
|
2010-06-29 22:37:45 +00:00
|
|
|
if (! iflag || ! job->changed)
|
1994-05-26 06:18:55 +00:00
|
|
|
freejob(job);
|
2010-06-29 22:37:45 +00:00
|
|
|
else {
|
|
|
|
job->remembered = 0;
|
|
|
|
if (job == bgjob)
|
|
|
|
bgjob = NULL;
|
|
|
|
}
|
1997-05-24 21:04:55 +00:00
|
|
|
return retval;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-06-29 22:37:45 +00:00
|
|
|
for (jp = jobtab ; jp < jobtab + njobs; jp++)
|
|
|
|
if (jp->used && jp->state == JOBDONE) {
|
|
|
|
if (! iflag || ! jp->changed)
|
|
|
|
freejob(jp);
|
|
|
|
else {
|
|
|
|
jp->remembered = 0;
|
|
|
|
if (jp == bgjob)
|
|
|
|
bgjob = NULL;
|
|
|
|
}
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
for (jp = jobtab ; ; jp++) {
|
|
|
|
if (jp >= jobtab + njobs) { /* no running procs */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (jp->used && jp->state == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-07-29 18:04:38 +00:00
|
|
|
} while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, (struct job *)NULL) != -1);
|
1998-08-24 10:20:37 +00:00
|
|
|
|
2013-09-02 21:57:46 +00:00
|
|
|
sig = pendingsig_waitcmd;
|
|
|
|
pendingsig_waitcmd = 0;
|
|
|
|
return sig + 128;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2013-08-16 13:56:43 +00:00
|
|
|
jobidcmd(int argc __unused, char **argv __unused)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct job *jp;
|
|
|
|
int i;
|
|
|
|
|
2013-08-16 13:56:43 +00:00
|
|
|
nextopt("");
|
|
|
|
jp = getjob(*argptr);
|
1994-05-26 06:18:55 +00:00
|
|
|
for (i = 0 ; i < jp->nprocs ; ) {
|
2002-07-18 10:22:42 +00:00
|
|
|
out1fmt("%d", (int)jp->ps[i].pid);
|
1994-05-26 06:18:55 +00:00
|
|
|
out1c(++i < jp->nprocs? ' ' : '\n');
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a job name to a job structure.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static struct job *
|
2014-03-14 21:45:37 +00:00
|
|
|
getjob_nonotfound(const char *name)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int jobno;
|
2002-06-01 01:51:42 +00:00
|
|
|
struct job *found, *jp;
|
2014-07-20 12:06:52 +00:00
|
|
|
size_t namelen;
|
2002-07-18 10:22:42 +00:00
|
|
|
pid_t pid;
|
1994-05-26 06:18:55 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (name == NULL) {
|
|
|
|
#if JOBS
|
2002-05-31 12:31:23 +00:00
|
|
|
currentjob: if ((jp = getcurjob(NULL)) == NULL)
|
1994-05-26 06:18:55 +00:00
|
|
|
error("No current job");
|
2002-05-31 12:31:23 +00:00
|
|
|
return (jp);
|
1994-05-26 06:18:55 +00:00
|
|
|
#else
|
|
|
|
error("No current job");
|
|
|
|
#endif
|
|
|
|
} else if (name[0] == '%') {
|
|
|
|
if (is_digit(name[1])) {
|
|
|
|
jobno = number(name + 1);
|
|
|
|
if (jobno > 0 && jobno <= njobs
|
|
|
|
&& jobtab[jobno - 1].used != 0)
|
|
|
|
return &jobtab[jobno - 1];
|
|
|
|
#if JOBS
|
|
|
|
} else if (name[1] == '%' && name[2] == '\0') {
|
|
|
|
goto currentjob;
|
2002-06-01 01:51:42 +00:00
|
|
|
} else if (name[1] == '+' && name[2] == '\0') {
|
|
|
|
goto currentjob;
|
|
|
|
} else if (name[1] == '-' && name[2] == '\0') {
|
|
|
|
if ((jp = getcurjob(NULL)) == NULL ||
|
|
|
|
(jp = getcurjob(jp)) == NULL)
|
|
|
|
error("No previous job");
|
|
|
|
return (jp);
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif
|
2002-06-01 01:51:42 +00:00
|
|
|
} else if (name[1] == '?') {
|
|
|
|
found = NULL;
|
|
|
|
for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
|
|
|
|
if (jp->used && jp->nprocs > 0
|
|
|
|
&& strstr(jp->ps[0].cmd, name + 2) != NULL) {
|
|
|
|
if (found)
|
|
|
|
error("%s: ambiguous", name);
|
|
|
|
found = jp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found != NULL)
|
|
|
|
return (found);
|
1994-05-26 06:18:55 +00:00
|
|
|
} else {
|
2014-07-20 12:06:52 +00:00
|
|
|
namelen = strlen(name);
|
2002-06-01 01:51:42 +00:00
|
|
|
found = NULL;
|
1994-05-26 06:18:55 +00:00
|
|
|
for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
|
|
|
|
if (jp->used && jp->nprocs > 0
|
2014-07-20 12:06:52 +00:00
|
|
|
&& strncmp(jp->ps[0].cmd, name + 1,
|
|
|
|
namelen - 1) == 0) {
|
1994-05-26 06:18:55 +00:00
|
|
|
if (found)
|
|
|
|
error("%s: ambiguous", name);
|
|
|
|
found = jp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
} else if (is_number(name)) {
|
2002-07-18 10:22:42 +00:00
|
|
|
pid = (pid_t)number(name);
|
1994-05-26 06:18:55 +00:00
|
|
|
for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
|
|
|
|
if (jp->used && jp->nprocs > 0
|
|
|
|
&& jp->ps[jp->nprocs - 1].pid == pid)
|
|
|
|
return jp;
|
|
|
|
}
|
|
|
|
}
|
1996-09-01 10:22:36 +00:00
|
|
|
return NULL;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-05 19:40:52 +00:00
|
|
|
static struct job *
|
2014-03-14 21:45:37 +00:00
|
|
|
getjob(const char *name)
|
2013-06-05 19:40:52 +00:00
|
|
|
{
|
|
|
|
struct job *jp;
|
|
|
|
|
|
|
|
jp = getjob_nonotfound(name);
|
|
|
|
if (jp == NULL)
|
|
|
|
error("No such job: %s", name);
|
|
|
|
return (jp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-15 14:58:48 +00:00
|
|
|
int
|
|
|
|
killjob(const char *name, int sig)
|
2010-12-21 22:47:34 +00:00
|
|
|
{
|
|
|
|
struct job *jp;
|
2014-03-15 14:58:48 +00:00
|
|
|
int i, ret;
|
2010-12-21 22:47:34 +00:00
|
|
|
|
|
|
|
jp = getjob(name);
|
2014-03-08 19:44:34 +00:00
|
|
|
if (jp->state == JOBDONE)
|
|
|
|
return 0;
|
2014-03-15 14:58:48 +00:00
|
|
|
if (jp->jobctl)
|
|
|
|
return kill(-jp->ps[0].pid, sig);
|
|
|
|
ret = -1;
|
|
|
|
errno = ESRCH;
|
|
|
|
for (i = 0; i < jp->nprocs; i++)
|
|
|
|
if (jp->ps[i].status == -1 || WIFSTOPPED(jp->ps[i].status)) {
|
|
|
|
if (kill(jp->ps[i].pid, sig) == 0)
|
|
|
|
ret = 0;
|
|
|
|
} else
|
|
|
|
ret = 0;
|
|
|
|
return ret;
|
2010-12-21 22:47:34 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a new job structure,
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct job *
|
2002-02-02 06:50:57 +00:00
|
|
|
makejob(union node *node __unused, int nprocs)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int i;
|
|
|
|
struct job *jp;
|
|
|
|
|
|
|
|
for (i = njobs, jp = jobtab ; ; jp++) {
|
|
|
|
if (--i < 0) {
|
|
|
|
INTOFF;
|
|
|
|
if (njobs == 0) {
|
|
|
|
jobtab = ckmalloc(4 * sizeof jobtab[0]);
|
2002-05-31 13:10:38 +00:00
|
|
|
#if JOBS
|
2002-05-31 12:31:23 +00:00
|
|
|
jobmru = NULL;
|
2002-05-31 13:10:38 +00:00
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
} else {
|
|
|
|
jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
|
1996-09-01 10:22:36 +00:00
|
|
|
memcpy(jp, jobtab, njobs * sizeof jp[0]);
|
2002-05-31 12:31:23 +00:00
|
|
|
#if JOBS
|
|
|
|
/* Relocate `next' pointers and list head */
|
2002-07-11 04:22:41 +00:00
|
|
|
if (jobmru != NULL)
|
|
|
|
jobmru = &jp[jobmru - jobtab];
|
2002-05-31 12:31:23 +00:00
|
|
|
for (i = 0; i < njobs; i++)
|
|
|
|
if (jp[i].next != NULL)
|
|
|
|
jp[i].next = &jp[jp[i].next -
|
|
|
|
jobtab];
|
|
|
|
#endif
|
2010-06-29 22:37:45 +00:00
|
|
|
if (bgjob != NULL)
|
|
|
|
bgjob = &jp[bgjob - jobtab];
|
1996-12-14 06:20:03 +00:00
|
|
|
/* Relocate `ps' pointers */
|
|
|
|
for (i = 0; i < njobs; i++)
|
|
|
|
if (jp[i].ps == &jobtab[i].ps0)
|
|
|
|
jp[i].ps = &jp[i].ps0;
|
1994-05-26 06:18:55 +00:00
|
|
|
ckfree(jobtab);
|
|
|
|
jobtab = jp;
|
|
|
|
}
|
|
|
|
jp = jobtab + njobs;
|
2013-04-01 17:18:22 +00:00
|
|
|
for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0)
|
|
|
|
;
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (jp->used == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
INTOFF;
|
|
|
|
jp->state = 0;
|
|
|
|
jp->used = 1;
|
|
|
|
jp->changed = 0;
|
|
|
|
jp->nprocs = 0;
|
2002-07-18 09:37:51 +00:00
|
|
|
jp->foreground = 0;
|
2010-06-29 22:37:45 +00:00
|
|
|
jp->remembered = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
#if JOBS
|
|
|
|
jp->jobctl = jobctl;
|
2002-05-31 12:31:23 +00:00
|
|
|
jp->next = NULL;
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif
|
|
|
|
if (nprocs > 1) {
|
|
|
|
jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
|
|
|
|
} else {
|
|
|
|
jp->ps = &jp->ps0;
|
|
|
|
}
|
|
|
|
INTON;
|
2010-10-13 13:22:11 +00:00
|
|
|
TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
|
1996-09-01 10:22:36 +00:00
|
|
|
jp - jobtab + 1));
|
1994-05-26 06:18:55 +00:00
|
|
|
return jp;
|
1995-05-30 00:07:29 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2002-05-31 12:31:23 +00:00
|
|
|
#if JOBS
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-05-31 12:31:23 +00:00
|
|
|
setcurjob(struct job *cj)
|
|
|
|
{
|
|
|
|
struct job *jp, *prev;
|
|
|
|
|
|
|
|
for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
|
|
|
|
if (jp == cj) {
|
|
|
|
if (prev != NULL)
|
|
|
|
prev->next = jp->next;
|
|
|
|
else
|
|
|
|
jobmru = jp->next;
|
|
|
|
jp->next = jobmru;
|
|
|
|
jobmru = cj;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cj->next = jobmru;
|
|
|
|
jobmru = cj;
|
|
|
|
}
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-05-31 12:31:23 +00:00
|
|
|
deljob(struct job *j)
|
|
|
|
{
|
|
|
|
struct job *jp, *prev;
|
|
|
|
|
|
|
|
for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
|
|
|
|
if (jp == j) {
|
|
|
|
if (prev != NULL)
|
|
|
|
prev->next = jp->next;
|
|
|
|
else
|
|
|
|
jobmru = jp->next;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the most recently used job that isn't `nj', and preferably one
|
|
|
|
* that is stopped.
|
|
|
|
*/
|
2010-10-13 22:18:03 +00:00
|
|
|
static struct job *
|
2002-05-31 12:31:23 +00:00
|
|
|
getcurjob(struct job *nj)
|
|
|
|
{
|
|
|
|
struct job *jp;
|
|
|
|
|
|
|
|
/* Try to find a stopped one.. */
|
|
|
|
for (jp = jobmru; jp != NULL; jp = jp->next)
|
|
|
|
if (jp->used && jp != nj && jp->state == JOBSTOPPED)
|
|
|
|
return (jp);
|
|
|
|
/* Otherwise the most recently used job that isn't `nj' */
|
|
|
|
for (jp = jobmru; jp != NULL; jp = jp->next)
|
|
|
|
if (jp->used && jp != nj)
|
|
|
|
return (jp);
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fork of a subshell. If we are doing job control, give the subshell its
|
|
|
|
* own process group. Jp is a job structure that the job is to be added to.
|
|
|
|
* N is the command that will be evaluated by the child. Both jp and n may
|
|
|
|
* be NULL. The mode parameter can be one of the following:
|
|
|
|
* FORK_FG - Fork off a foreground process.
|
|
|
|
* FORK_BG - Fork off a background process.
|
|
|
|
* FORK_NOJOB - Like FORK_FG, but don't give the process its own
|
|
|
|
* process group even if job control is on.
|
|
|
|
*
|
|
|
|
* When job control is turned off, background processes have their standard
|
|
|
|
* input redirected to /dev/null (except for the second and later processes
|
|
|
|
* in a pipeline).
|
|
|
|
*/
|
|
|
|
|
2002-07-18 10:22:42 +00:00
|
|
|
pid_t
|
2002-02-02 06:50:57 +00:00
|
|
|
forkshell(struct job *jp, union node *n, int mode)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2002-07-18 10:22:42 +00:00
|
|
|
pid_t pid;
|
|
|
|
pid_t pgrp;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2010-10-13 13:22:11 +00:00
|
|
|
TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
|
1996-09-01 10:22:36 +00:00
|
|
|
mode));
|
1994-05-26 06:18:55 +00:00
|
|
|
INTOFF;
|
2010-12-05 21:53:29 +00:00
|
|
|
if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
|
2010-05-24 10:35:57 +00:00
|
|
|
checkzombies();
|
2003-03-17 11:28:56 +00:00
|
|
|
flushall();
|
1994-05-26 06:18:55 +00:00
|
|
|
pid = fork();
|
|
|
|
if (pid == -1) {
|
|
|
|
TRACE(("Fork failed, errno=%d\n", errno));
|
|
|
|
INTON;
|
1999-11-29 19:11:01 +00:00
|
|
|
error("Cannot fork: %s", strerror(errno));
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
if (pid == 0) {
|
|
|
|
struct job *p;
|
|
|
|
int wasroot;
|
|
|
|
int i;
|
|
|
|
|
2002-07-18 10:22:42 +00:00
|
|
|
TRACE(("Child shell %d\n", (int)getpid()));
|
1994-05-26 06:18:55 +00:00
|
|
|
wasroot = rootshell;
|
|
|
|
rootshell = 0;
|
2009-12-25 20:21:35 +00:00
|
|
|
handler = &main_handler;
|
1994-05-26 06:18:55 +00:00
|
|
|
closescript();
|
|
|
|
INTON;
|
2011-06-12 23:06:04 +00:00
|
|
|
forcelocal = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
clear_traps();
|
|
|
|
#if JOBS
|
|
|
|
jobctl = 0; /* do job control only in root shell */
|
|
|
|
if (wasroot && mode != FORK_NOJOB && mflag) {
|
|
|
|
if (jp == NULL || jp->nprocs == 0)
|
|
|
|
pgrp = getpid();
|
|
|
|
else
|
|
|
|
pgrp = jp->ps[0].pid;
|
2014-09-04 21:48:33 +00:00
|
|
|
if (setpgid(0, pgrp) == 0 && mode == FORK_FG &&
|
|
|
|
ttyfd >= 0) {
|
1994-05-26 06:18:55 +00:00
|
|
|
/*** this causes superfluous TIOCSPGRPS ***/
|
2002-07-11 06:42:11 +00:00
|
|
|
if (tcsetpgrp(ttyfd, pgrp) < 0)
|
1996-12-14 06:20:03 +00:00
|
|
|
error("tcsetpgrp failed, errno=%d", errno);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
setsignal(SIGTSTP);
|
|
|
|
setsignal(SIGTTOU);
|
|
|
|
} else if (mode == FORK_BG) {
|
|
|
|
ignoresig(SIGINT);
|
|
|
|
ignoresig(SIGQUIT);
|
|
|
|
if ((jp == NULL || jp->nprocs == 0) &&
|
|
|
|
! fd0_redirected_p ()) {
|
|
|
|
close(0);
|
2000-12-09 09:35:55 +00:00
|
|
|
if (open(_PATH_DEVNULL, O_RDONLY) != 0)
|
2011-06-04 15:05:52 +00:00
|
|
|
error("cannot open %s: %s",
|
2000-12-09 09:35:55 +00:00
|
|
|
_PATH_DEVNULL, strerror(errno));
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (mode == FORK_BG) {
|
|
|
|
ignoresig(SIGINT);
|
|
|
|
ignoresig(SIGQUIT);
|
|
|
|
if ((jp == NULL || jp->nprocs == 0) &&
|
|
|
|
! fd0_redirected_p ()) {
|
|
|
|
close(0);
|
2000-12-09 09:35:55 +00:00
|
|
|
if (open(_PATH_DEVNULL, O_RDONLY) != 0)
|
2011-06-04 15:05:52 +00:00
|
|
|
error("cannot open %s: %s",
|
2000-12-09 09:35:55 +00:00
|
|
|
_PATH_DEVNULL, strerror(errno));
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2002-08-18 06:43:44 +00:00
|
|
|
INTOFF;
|
|
|
|
for (i = njobs, p = jobtab ; --i >= 0 ; p++)
|
|
|
|
if (p->used)
|
|
|
|
freejob(p);
|
|
|
|
INTON;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (wasroot && iflag) {
|
|
|
|
setsignal(SIGINT);
|
|
|
|
setsignal(SIGQUIT);
|
|
|
|
setsignal(SIGTERM);
|
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
if (rootshell && mode != FORK_NOJOB && mflag) {
|
|
|
|
if (jp == NULL || jp->nprocs == 0)
|
|
|
|
pgrp = pid;
|
|
|
|
else
|
|
|
|
pgrp = jp->ps[0].pid;
|
1996-09-01 10:22:36 +00:00
|
|
|
setpgid(pid, pgrp);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2010-06-29 22:37:45 +00:00
|
|
|
if (mode == FORK_BG) {
|
|
|
|
if (bgjob != NULL && bgjob->state == JOBDONE &&
|
|
|
|
!bgjob->remembered && !iflag)
|
|
|
|
freejob(bgjob);
|
1994-05-26 06:18:55 +00:00
|
|
|
backgndpid = pid; /* set $! */
|
2010-06-29 22:37:45 +00:00
|
|
|
bgjob = jp;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
if (jp) {
|
|
|
|
struct procstat *ps = &jp->ps[jp->nprocs++];
|
|
|
|
ps->pid = pid;
|
|
|
|
ps->status = -1;
|
|
|
|
ps->cmd = nullstr;
|
|
|
|
if (iflag && rootshell && n)
|
|
|
|
ps->cmd = commandtext(n);
|
2002-07-18 09:37:51 +00:00
|
|
|
jp->foreground = mode == FORK_FG;
|
2002-05-31 12:31:23 +00:00
|
|
|
#if JOBS
|
|
|
|
setcurjob(jp);
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
INTON;
|
2002-07-18 10:22:42 +00:00
|
|
|
TRACE(("In parent shell: child = %d\n", (int)pid));
|
1994-05-26 06:18:55 +00:00
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-04 23:12:14 +00:00
|
|
|
pid_t
|
|
|
|
vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
struct jmploc jmploc;
|
|
|
|
struct jmploc *savehandler;
|
|
|
|
|
2012-04-02 17:16:24 +00:00
|
|
|
TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
|
|
|
|
(void *)pip));
|
2012-02-04 23:12:14 +00:00
|
|
|
INTOFF;
|
|
|
|
flushall();
|
|
|
|
savehandler = handler;
|
|
|
|
pid = vfork();
|
|
|
|
if (pid == -1) {
|
|
|
|
TRACE(("Vfork failed, errno=%d\n", errno));
|
|
|
|
INTON;
|
|
|
|
error("Cannot fork: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
if (pid == 0) {
|
|
|
|
TRACE(("Child shell %d\n", (int)getpid()));
|
|
|
|
if (setjmp(jmploc.loc))
|
|
|
|
_exit(exception == EXEXEC ? exerrno : 2);
|
|
|
|
if (pip != NULL) {
|
|
|
|
close(pip[0]);
|
|
|
|
if (pip[1] != 1) {
|
|
|
|
dup2(pip[1], 1);
|
|
|
|
close(pip[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handler = &jmploc;
|
|
|
|
shellexec(argv, envp, path, idx);
|
|
|
|
}
|
|
|
|
handler = savehandler;
|
|
|
|
if (jp) {
|
|
|
|
struct procstat *ps = &jp->ps[jp->nprocs++];
|
|
|
|
ps->pid = pid;
|
|
|
|
ps->status = -1;
|
|
|
|
ps->cmd = nullstr;
|
|
|
|
jp->foreground = 1;
|
|
|
|
#if JOBS
|
|
|
|
setcurjob(jp);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
TRACE(("In parent shell: child = %d\n", (int)pid));
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for job to finish.
|
|
|
|
*
|
|
|
|
* Under job control we have the problem that while a child process is
|
|
|
|
* running interrupts generated by the user are sent to the child but not
|
|
|
|
* to the shell. This means that an infinite loop started by an inter-
|
|
|
|
* active user may be hard to kill. With job control turned off, an
|
|
|
|
* interactive user may place an interactive program inside a loop. If
|
|
|
|
* the interactive program catches interrupts, the user doesn't want
|
|
|
|
* these interrupts to also abort the loop. The approach we take here
|
|
|
|
* is to have the shell ignore interrupt signals while waiting for a
|
1999-05-08 10:22:15 +00:00
|
|
|
* foreground process to terminate, and then send itself an interrupt
|
1994-05-26 06:18:55 +00:00
|
|
|
* signal if the child process was terminated by an interrupt signal.
|
|
|
|
* Unfortunately, some programs want to do a bit of cleanup and then
|
|
|
|
* exit on interrupt; unless these processes terminate themselves by
|
|
|
|
* sending a signal to themselves (instead of calling exit) they will
|
|
|
|
* confuse this approach.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
waitforjob(struct job *jp, int *origstatus)
|
1999-04-21 11:52:39 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
#if JOBS
|
2010-06-06 22:27:32 +00:00
|
|
|
int propagate_int = jp->jobctl && jp->foreground;
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif
|
|
|
|
int status;
|
|
|
|
int st;
|
|
|
|
|
|
|
|
INTOFF;
|
2010-10-13 13:22:11 +00:00
|
|
|
TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
|
1998-09-08 13:16:52 +00:00
|
|
|
while (jp->state == 0)
|
2013-09-02 21:57:46 +00:00
|
|
|
if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG |
|
|
|
|
DOWAIT_SIG_ANY : 0), jp) == -1)
|
1998-09-08 13:16:52 +00:00
|
|
|
dotrap();
|
1994-05-26 06:18:55 +00:00
|
|
|
#if JOBS
|
|
|
|
if (jp->jobctl) {
|
2014-09-04 21:48:33 +00:00
|
|
|
if (ttyfd >= 0 && tcsetpgrp(ttyfd, rootpid) < 0)
|
1996-12-14 06:20:03 +00:00
|
|
|
error("tcsetpgrp failed, errno=%d\n", errno);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
if (jp->state == JOBSTOPPED)
|
2002-05-31 12:31:23 +00:00
|
|
|
setcurjob(jp);
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif
|
|
|
|
status = jp->ps[jp->nprocs - 1].status;
|
1999-04-21 11:52:39 +00:00
|
|
|
if (origstatus != NULL)
|
|
|
|
*origstatus = status;
|
1994-05-26 06:18:55 +00:00
|
|
|
/* convert to 8 bits */
|
1997-05-24 21:04:55 +00:00
|
|
|
if (WIFEXITED(status))
|
|
|
|
st = WEXITSTATUS(status);
|
1994-05-26 06:18:55 +00:00
|
|
|
#if JOBS
|
1997-05-24 21:04:55 +00:00
|
|
|
else if (WIFSTOPPED(status))
|
|
|
|
st = WSTOPSIG(status) + 128;
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif
|
|
|
|
else
|
1997-05-24 21:04:55 +00:00
|
|
|
st = WTERMSIG(status) + 128;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (! JOBS || jp->state == JOBDONE)
|
|
|
|
freejob(jp);
|
1998-08-24 10:20:37 +00:00
|
|
|
if (int_pending()) {
|
2010-12-12 22:59:34 +00:00
|
|
|
if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
|
1998-08-24 10:20:37 +00:00
|
|
|
CLEAR_PENDING_INT;
|
|
|
|
}
|
2010-06-06 22:27:32 +00:00
|
|
|
#if JOBS
|
|
|
|
else if (rootshell && iflag && propagate_int &&
|
|
|
|
WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
|
|
|
|
kill(getpid(), SIGINT);
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-29 18:04:38 +00:00
|
|
|
static void
|
2013-04-01 17:18:22 +00:00
|
|
|
dummy_handler(int sig __unused)
|
2012-07-29 18:04:38 +00:00
|
|
|
{
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for a process to terminate.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static pid_t
|
2012-07-29 18:04:38 +00:00
|
|
|
dowait(int mode, struct job *job)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2012-07-29 18:04:38 +00:00
|
|
|
struct sigaction sa, osa;
|
|
|
|
sigset_t mask, omask;
|
2002-07-18 10:22:42 +00:00
|
|
|
pid_t pid;
|
1994-05-26 06:18:55 +00:00
|
|
|
int status;
|
|
|
|
struct procstat *sp;
|
|
|
|
struct job *jp;
|
|
|
|
struct job *thisjob;
|
2012-12-25 14:17:09 +00:00
|
|
|
const char *sigstr;
|
1994-05-26 06:18:55 +00:00
|
|
|
int done;
|
|
|
|
int stopped;
|
1997-05-24 21:04:55 +00:00
|
|
|
int sig;
|
2010-12-05 22:37:01 +00:00
|
|
|
int coredump;
|
2012-07-28 14:56:50 +00:00
|
|
|
int wflags;
|
2012-07-29 18:04:38 +00:00
|
|
|
int restore_sigchld;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2013-02-07 19:00:54 +00:00
|
|
|
TRACE(("dowait(%d, %p) called\n", mode, job));
|
2012-07-29 18:04:38 +00:00
|
|
|
restore_sigchld = 0;
|
|
|
|
if ((mode & DOWAIT_SIG) != 0) {
|
|
|
|
sigfillset(&mask);
|
|
|
|
sigprocmask(SIG_BLOCK, &mask, &omask);
|
|
|
|
INTOFF;
|
|
|
|
if (!issigchldtrapped()) {
|
|
|
|
restore_sigchld = 1;
|
|
|
|
sa.sa_handler = dummy_handler;
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(SIGCHLD, &sa, &osa);
|
|
|
|
}
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
do {
|
2012-07-28 14:56:50 +00:00
|
|
|
#if JOBS
|
2012-07-28 15:13:48 +00:00
|
|
|
if (iflag)
|
|
|
|
wflags = WUNTRACED | WCONTINUED;
|
|
|
|
else
|
2012-07-28 14:56:50 +00:00
|
|
|
#endif
|
2012-07-28 15:13:48 +00:00
|
|
|
wflags = 0;
|
2012-07-29 18:04:38 +00:00
|
|
|
if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
|
2012-07-28 14:56:50 +00:00
|
|
|
wflags |= WNOHANG;
|
|
|
|
pid = wait3(&status, wflags, (struct rusage *)NULL);
|
2002-07-18 10:22:42 +00:00
|
|
|
TRACE(("wait returns %d, status=%d\n", (int)pid, status));
|
2012-07-29 18:04:38 +00:00
|
|
|
if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
|
|
|
|
pid = -1;
|
2013-09-02 21:57:46 +00:00
|
|
|
if (((mode & DOWAIT_SIG_ANY) != 0 ?
|
|
|
|
pendingsig : pendingsig_waitcmd) != 0) {
|
|
|
|
errno = EINTR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sigsuspend(&omask);
|
2012-07-29 18:04:38 +00:00
|
|
|
if (int_pending())
|
|
|
|
break;
|
|
|
|
}
|
2013-09-02 21:57:46 +00:00
|
|
|
} while (pid == -1 && errno == EINTR);
|
2005-12-14 17:26:29 +00:00
|
|
|
if (pid == -1 && errno == ECHILD && job != NULL)
|
|
|
|
job->state = JOBDONE;
|
2012-07-29 18:04:38 +00:00
|
|
|
if ((mode & DOWAIT_SIG) != 0) {
|
|
|
|
if (restore_sigchld)
|
|
|
|
sigaction(SIGCHLD, &osa, NULL);
|
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
|
|
|
INTON;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
if (pid <= 0)
|
|
|
|
return pid;
|
|
|
|
INTOFF;
|
|
|
|
thisjob = NULL;
|
|
|
|
for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
|
2010-12-05 21:53:29 +00:00
|
|
|
if (jp->used && jp->nprocs > 0) {
|
1994-05-26 06:18:55 +00:00
|
|
|
done = 1;
|
|
|
|
stopped = 1;
|
|
|
|
for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
|
|
|
|
if (sp->pid == -1)
|
|
|
|
continue;
|
2014-03-20 22:38:13 +00:00
|
|
|
if (sp->pid == pid && (sp->status == -1 ||
|
|
|
|
WIFSTOPPED(sp->status))) {
|
1997-05-24 21:04:55 +00:00
|
|
|
TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
|
2002-07-18 10:22:42 +00:00
|
|
|
(int)pid, sp->status,
|
|
|
|
status));
|
2012-07-28 14:32:55 +00:00
|
|
|
if (WIFCONTINUED(status)) {
|
|
|
|
sp->status = -1;
|
|
|
|
jp->state = 0;
|
|
|
|
} else
|
|
|
|
sp->status = status;
|
1994-05-26 06:18:55 +00:00
|
|
|
thisjob = jp;
|
|
|
|
}
|
|
|
|
if (sp->status == -1)
|
|
|
|
stopped = 0;
|
1997-05-24 21:04:55 +00:00
|
|
|
else if (WIFSTOPPED(sp->status))
|
1994-05-26 06:18:55 +00:00
|
|
|
done = 0;
|
|
|
|
}
|
|
|
|
if (stopped) { /* stopped or done */
|
|
|
|
int state = done? JOBDONE : JOBSTOPPED;
|
|
|
|
if (jp->state != state) {
|
2010-10-13 13:22:11 +00:00
|
|
|
TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
|
1994-05-26 06:18:55 +00:00
|
|
|
jp->state = state;
|
2010-06-29 22:37:45 +00:00
|
|
|
if (jp != job) {
|
|
|
|
if (done && !jp->remembered &&
|
|
|
|
!iflag && jp != bgjob)
|
|
|
|
freejob(jp);
|
1994-05-26 06:18:55 +00:00
|
|
|
#if JOBS
|
2010-06-29 22:37:45 +00:00
|
|
|
else if (done)
|
|
|
|
deljob(jp);
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif
|
2010-06-29 22:37:45 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
INTON;
|
2010-12-05 22:37:01 +00:00
|
|
|
if (!thisjob || thisjob->state == 0)
|
|
|
|
;
|
|
|
|
else if ((!rootshell || !iflag || thisjob == job) &&
|
|
|
|
thisjob->foreground && thisjob->state != JOBSTOPPED) {
|
|
|
|
sig = 0;
|
|
|
|
coredump = 0;
|
|
|
|
for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
|
|
|
|
if (WIFSIGNALED(sp->status)) {
|
|
|
|
sig = WTERMSIG(sp->status);
|
|
|
|
coredump = WCOREDUMP(sp->status);
|
|
|
|
}
|
|
|
|
if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
|
2012-12-25 14:17:09 +00:00
|
|
|
sigstr = strsignal(sig);
|
|
|
|
if (sigstr != NULL)
|
|
|
|
out2str(sigstr);
|
1997-05-24 21:04:55 +00:00
|
|
|
else
|
2012-12-25 14:17:09 +00:00
|
|
|
out2str("Unknown signal");
|
2010-12-05 22:37:01 +00:00
|
|
|
if (coredump)
|
2011-01-30 22:57:52 +00:00
|
|
|
out2str(" (core dumped)");
|
|
|
|
out2c('\n');
|
|
|
|
flushout(out2);
|
2002-07-18 09:37:51 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
} else {
|
2003-01-21 10:06:04 +00:00
|
|
|
TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
|
2010-12-05 22:37:01 +00:00
|
|
|
thisjob->changed = 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* return 1 if there are stopped jobs, otherwise 0
|
|
|
|
*/
|
|
|
|
int job_warning = 0;
|
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
stoppedjobs(void)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
1997-04-28 03:06:52 +00:00
|
|
|
int jobno;
|
|
|
|
struct job *jp;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
if (job_warning)
|
|
|
|
return (0);
|
|
|
|
for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
|
|
|
|
if (jp->used == 0)
|
|
|
|
continue;
|
|
|
|
if (jp->state == JOBSTOPPED) {
|
2009-11-21 14:28:32 +00:00
|
|
|
out2fmt_flush("You have stopped jobs.\n");
|
1994-05-26 06:18:55 +00:00
|
|
|
job_warning = 2;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-05-24 10:35:57 +00:00
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2010-05-24 10:35:57 +00:00
|
|
|
checkzombies(void)
|
|
|
|
{
|
|
|
|
while (njobs > 0 && dowait(0, NULL) > 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-29 22:37:45 +00:00
|
|
|
int
|
|
|
|
backgndpidset(void)
|
|
|
|
{
|
|
|
|
return backgndpid != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pid_t
|
|
|
|
backgndpidval(void)
|
|
|
|
{
|
2011-06-12 23:06:04 +00:00
|
|
|
if (bgjob != NULL && !forcelocal)
|
2010-06-29 22:37:45 +00:00
|
|
|
bgjob->remembered = 1;
|
|
|
|
return backgndpid;
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* Return a string identifying a command (to be printed by the
|
|
|
|
* jobs command.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 04:01:01 +00:00
|
|
|
static char *cmdnextc;
|
|
|
|
static int cmdnleft;
|
1994-05-26 06:18:55 +00:00
|
|
|
#define MAXCMDTEXT 200
|
|
|
|
|
|
|
|
char *
|
2002-02-02 06:50:57 +00:00
|
|
|
commandtext(union node *n)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
char *name;
|
|
|
|
|
|
|
|
cmdnextc = name = ckmalloc(MAXCMDTEXT);
|
|
|
|
cmdnleft = MAXCMDTEXT - 4;
|
|
|
|
cmdtxt(n);
|
|
|
|
*cmdnextc = '\0';
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
cmdtxt(union node *n)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
union node *np;
|
|
|
|
struct nodelist *lp;
|
2009-12-27 18:04:05 +00:00
|
|
|
const char *p;
|
1994-05-26 06:18:55 +00:00
|
|
|
int i;
|
|
|
|
char s[2];
|
|
|
|
|
|
|
|
if (n == NULL)
|
|
|
|
return;
|
|
|
|
switch (n->type) {
|
|
|
|
case NSEMI:
|
|
|
|
cmdtxt(n->nbinary.ch1);
|
|
|
|
cmdputs("; ");
|
|
|
|
cmdtxt(n->nbinary.ch2);
|
|
|
|
break;
|
|
|
|
case NAND:
|
|
|
|
cmdtxt(n->nbinary.ch1);
|
|
|
|
cmdputs(" && ");
|
|
|
|
cmdtxt(n->nbinary.ch2);
|
|
|
|
break;
|
|
|
|
case NOR:
|
|
|
|
cmdtxt(n->nbinary.ch1);
|
|
|
|
cmdputs(" || ");
|
|
|
|
cmdtxt(n->nbinary.ch2);
|
|
|
|
break;
|
|
|
|
case NPIPE:
|
|
|
|
for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
|
|
|
|
cmdtxt(lp->n);
|
|
|
|
if (lp->next)
|
|
|
|
cmdputs(" | ");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NSUBSHELL:
|
|
|
|
cmdputs("(");
|
|
|
|
cmdtxt(n->nredir.n);
|
|
|
|
cmdputs(")");
|
|
|
|
break;
|
|
|
|
case NREDIR:
|
|
|
|
case NBACKGND:
|
|
|
|
cmdtxt(n->nredir.n);
|
|
|
|
break;
|
|
|
|
case NIF:
|
|
|
|
cmdputs("if ");
|
|
|
|
cmdtxt(n->nif.test);
|
|
|
|
cmdputs("; then ");
|
|
|
|
cmdtxt(n->nif.ifpart);
|
|
|
|
cmdputs("...");
|
|
|
|
break;
|
|
|
|
case NWHILE:
|
|
|
|
cmdputs("while ");
|
|
|
|
goto until;
|
|
|
|
case NUNTIL:
|
|
|
|
cmdputs("until ");
|
|
|
|
until:
|
|
|
|
cmdtxt(n->nbinary.ch1);
|
|
|
|
cmdputs("; do ");
|
|
|
|
cmdtxt(n->nbinary.ch2);
|
|
|
|
cmdputs("; done");
|
|
|
|
break;
|
|
|
|
case NFOR:
|
|
|
|
cmdputs("for ");
|
|
|
|
cmdputs(n->nfor.var);
|
|
|
|
cmdputs(" in ...");
|
|
|
|
break;
|
|
|
|
case NCASE:
|
|
|
|
cmdputs("case ");
|
|
|
|
cmdputs(n->ncase.expr->narg.text);
|
|
|
|
cmdputs(" in ...");
|
|
|
|
break;
|
|
|
|
case NDEFUN:
|
|
|
|
cmdputs(n->narg.text);
|
|
|
|
cmdputs("() ...");
|
|
|
|
break;
|
2013-01-31 21:50:44 +00:00
|
|
|
case NNOT:
|
|
|
|
cmdputs("! ");
|
|
|
|
cmdtxt(n->nnot.com);
|
|
|
|
break;
|
1994-05-26 06:18:55 +00:00
|
|
|
case NCMD:
|
|
|
|
for (np = n->ncmd.args ; np ; np = np->narg.next) {
|
|
|
|
cmdtxt(np);
|
|
|
|
if (np->narg.next)
|
|
|
|
cmdputs(" ");
|
|
|
|
}
|
|
|
|
for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
|
|
|
|
cmdputs(" ");
|
|
|
|
cmdtxt(np);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NARG:
|
|
|
|
cmdputs(n->narg.text);
|
|
|
|
break;
|
|
|
|
case NTO:
|
|
|
|
p = ">"; i = 1; goto redir;
|
|
|
|
case NAPPEND:
|
|
|
|
p = ">>"; i = 1; goto redir;
|
|
|
|
case NTOFD:
|
|
|
|
p = ">&"; i = 1; goto redir;
|
2002-05-19 06:03:05 +00:00
|
|
|
case NCLOBBER:
|
|
|
|
p = ">|"; i = 1; goto redir;
|
1994-05-26 06:18:55 +00:00
|
|
|
case NFROM:
|
|
|
|
p = "<"; i = 0; goto redir;
|
2000-10-03 23:13:14 +00:00
|
|
|
case NFROMTO:
|
|
|
|
p = "<>"; i = 0; goto redir;
|
1994-05-26 06:18:55 +00:00
|
|
|
case NFROMFD:
|
|
|
|
p = "<&"; i = 0; goto redir;
|
|
|
|
redir:
|
|
|
|
if (n->nfile.fd != i) {
|
|
|
|
s[0] = n->nfile.fd + '0';
|
|
|
|
s[1] = '\0';
|
|
|
|
cmdputs(s);
|
|
|
|
}
|
|
|
|
cmdputs(p);
|
|
|
|
if (n->type == NTOFD || n->type == NFROMFD) {
|
2002-07-09 03:26:47 +00:00
|
|
|
if (n->ndup.dupfd >= 0)
|
|
|
|
s[0] = n->ndup.dupfd + '0';
|
|
|
|
else
|
|
|
|
s[0] = '-';
|
1994-05-26 06:18:55 +00:00
|
|
|
s[1] = '\0';
|
|
|
|
cmdputs(s);
|
|
|
|
} else {
|
|
|
|
cmdtxt(n->nfile.fname);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NHERE:
|
|
|
|
case NXHERE:
|
|
|
|
cmdputs("<<...");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cmdputs("???");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2009-12-27 18:04:05 +00:00
|
|
|
cmdputs(const char *s)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
2009-12-27 18:04:05 +00:00
|
|
|
const char *p;
|
|
|
|
char *q;
|
1997-04-28 03:06:52 +00:00
|
|
|
char c;
|
1994-05-26 06:18:55 +00:00
|
|
|
int subtype = 0;
|
|
|
|
|
|
|
|
if (cmdnleft <= 0)
|
|
|
|
return;
|
|
|
|
p = s;
|
|
|
|
q = cmdnextc;
|
|
|
|
while ((c = *p++) != '\0') {
|
|
|
|
if (c == CTLESC)
|
|
|
|
*q++ = *p++;
|
|
|
|
else if (c == CTLVAR) {
|
|
|
|
*q++ = '$';
|
|
|
|
if (--cmdnleft > 0)
|
|
|
|
*q++ = '{';
|
|
|
|
subtype = *p++;
|
2010-12-06 23:49:27 +00:00
|
|
|
if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
|
|
|
|
*q++ = '#';
|
1994-05-26 06:18:55 +00:00
|
|
|
} else if (c == '=' && subtype != 0) {
|
2010-12-06 23:49:27 +00:00
|
|
|
*q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
|
|
|
|
if (*q)
|
|
|
|
q++;
|
|
|
|
else
|
|
|
|
cmdnleft++;
|
|
|
|
if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
|
|
|
|
(subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
|
|
|
|
--cmdnleft > 0)
|
|
|
|
*q = q[-1], q++;
|
1994-05-26 06:18:55 +00:00
|
|
|
subtype = 0;
|
|
|
|
} else if (c == CTLENDVAR) {
|
|
|
|
*q++ = '}';
|
2010-12-06 23:49:27 +00:00
|
|
|
} else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
|
|
|
|
cmdnleft -= 5;
|
|
|
|
if (cmdnleft > 0) {
|
|
|
|
*q++ = '$';
|
|
|
|
*q++ = '(';
|
|
|
|
*q++ = '.';
|
|
|
|
*q++ = '.';
|
|
|
|
*q++ = '.';
|
|
|
|
*q++ = ')';
|
|
|
|
}
|
|
|
|
} else if (c == CTLARI) {
|
|
|
|
cmdnleft -= 2;
|
|
|
|
if (cmdnleft > 0) {
|
|
|
|
*q++ = '$';
|
|
|
|
*q++ = '(';
|
|
|
|
*q++ = '(';
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
} else if (c == CTLENDARI) {
|
|
|
|
if (--cmdnleft > 0) {
|
|
|
|
*q++ = ')';
|
|
|
|
*q++ = ')';
|
|
|
|
}
|
|
|
|
} else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
|
|
|
|
cmdnleft++; /* ignore */
|
1994-05-26 06:18:55 +00:00
|
|
|
else
|
|
|
|
*q++ = c;
|
|
|
|
if (--cmdnleft <= 0) {
|
|
|
|
*q++ = '.';
|
|
|
|
*q++ = '.';
|
|
|
|
*q++ = '.';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmdnextc = q;
|
|
|
|
}
|