2009-12-12 18:45:42 +01:00
|
|
|
/* $OpenBSD: jobs.c,v 1.38 2009/12/12 04:28:44 deraadt Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
2012-02-06 18:42:24 +01:00
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011, 2012
|
2009-05-16 18:59:42 +02:00
|
|
|
* Thorsten Glaser <tg@mirbsd.org>
|
|
|
|
*
|
|
|
|
* Provided that these terms and disclaimer and all copyright notices
|
|
|
|
* are retained or reproduced in an accompanying document, permission
|
|
|
|
* is granted to deal in this work without restriction, including un-
|
|
|
|
* limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
* merge, give away, or sublicence.
|
|
|
|
*
|
|
|
|
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|
|
|
* the utmost extent permitted by applicable law, neither express nor
|
|
|
|
* implied; without malicious intent or gross negligence. In no event
|
|
|
|
* may a licensor, author or contributor be held liable for indirect,
|
|
|
|
* direct, other damage, loss, or other issues arising in any way out
|
|
|
|
* of dealing in the work, even if advised of the possibility of such
|
|
|
|
* damage or existence of a defect, except proven that it results out
|
|
|
|
* of said person's immediate fault when using the work as intended.
|
|
|
|
*/
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
#include "sh.h"
|
|
|
|
|
2012-05-05 00:34:51 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.88 2012/05/04 22:34:50 tg Exp $");
|
2009-06-08 22:34:40 +02:00
|
|
|
|
|
|
|
#if HAVE_KILLPG
|
|
|
|
#define mksh_killpg killpg
|
|
|
|
#else
|
|
|
|
/* cross fingers and hope kill is killpg-endowed */
|
|
|
|
#define mksh_killpg(p,s) kill(-(p), (s))
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* Order important! */
|
|
|
|
#define PRUNNING 0
|
|
|
|
#define PEXITED 1
|
|
|
|
#define PSIGNALLED 2
|
|
|
|
#define PSTOPPED 3
|
|
|
|
|
2012-02-06 18:49:52 +01:00
|
|
|
typedef struct proc Proc;
|
2005-05-23 05:06:10 +02:00
|
|
|
struct proc {
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
Proc *next; /* next process in pipeline (if any) */
|
2009-04-07 20:41:37 +02:00
|
|
|
pid_t pid; /* process id */
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
int state;
|
|
|
|
int status; /* wait status */
|
2012-02-06 18:49:52 +01:00
|
|
|
/* process command string from vistree */
|
|
|
|
char command[64 - (ALLOC_SIZE + sizeof(Proc *) + sizeof(pid_t) +
|
|
|
|
2 * sizeof(int))];
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* 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) */
|
2010-08-28 17:48:20 +02:00
|
|
|
#define JF_SAVEDTTY 0x020 /* j->ttystat is valid */
|
2005-05-23 05:06:10 +02:00
|
|
|
#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 {
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
Job *next; /* next job in list */
|
2009-04-07 20:41:37 +02:00
|
|
|
Proc *proc_list; /* process list */
|
|
|
|
Proc *last_proc; /* last process in list */
|
|
|
|
struct timeval systime; /* system time used by job */
|
|
|
|
struct timeval usrtime; /* user time used by job */
|
|
|
|
pid_t pgrp; /* process group of job */
|
|
|
|
pid_t ppid; /* pid of process that forked job */
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
int job; /* job number: %n */
|
|
|
|
int flags; /* see JF_* */
|
2007-06-07 01:28:17 +02:00
|
|
|
volatile int state; /* job state */
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
int status; /* exit status of last process */
|
2005-05-23 05:06:10 +02:00
|
|
|
int32_t age; /* number of jobs started */
|
|
|
|
Coproc_id coproc_id; /* 0 or id of coprocess output pipe */
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2012-05-05 00:18:27 +02:00
|
|
|
mksh_ttyst ttystat; /* saved tty state for stopped jobs */
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
pid_t saved_ttypgrp; /* saved tty process group for stopped jobs */
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* 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 */
|
2011-02-18 23:26:13 +01:00
|
|
|
#define JW_PIPEST 0x08 /* want PIPESTATUS */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
static const char *const lookup_msgs[] = {
|
2005-05-23 05:06:10 +02:00
|
|
|
null,
|
|
|
|
"no such job",
|
|
|
|
"ambiguous",
|
|
|
|
"argument must be %job or process id",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
static Job *job_list; /* job list */
|
|
|
|
static Job *last_job;
|
|
|
|
static Job *async_job;
|
|
|
|
static pid_t async_pid;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
static int nzombie; /* # of zombies owned by this process */
|
|
|
|
static int32_t njobs; /* # of jobs started */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2006-11-09 23:08:08 +01:00
|
|
|
#ifndef CHILD_MAX
|
2009-11-22 15:14:42 +01:00
|
|
|
#define CHILD_MAX 25
|
2006-11-09 23:08:08 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
/* held_sigchld is set if sigchld occurs before a job is completely started */
|
|
|
|
static volatile sig_atomic_t held_sigchld;
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
static struct shf *shl_j;
|
2008-05-15 17:24:11 +02:00
|
|
|
static bool ttypgrp_ok; /* set if can use tty pgrps */
|
2005-05-23 05:06:10 +02:00
|
|
|
static pid_t restore_ttypgrp = -1;
|
|
|
|
static int const tt_sigs[] = { SIGTSTP, SIGTTIN, SIGTTOU };
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
static void j_set_async(Job *);
|
|
|
|
static void j_startjob(Job *);
|
|
|
|
static int j_waitj(Job *, int, const char *);
|
|
|
|
static void j_sigchld(int);
|
|
|
|
static void j_print(Job *, int, struct shf *);
|
|
|
|
static Job *j_lookup(const char *, int *);
|
|
|
|
static Job *new_job(void);
|
|
|
|
static Proc *new_proc(void);
|
|
|
|
static void check_job(Job *);
|
|
|
|
static void put_job(Job *, int);
|
|
|
|
static void remove_job(Job *, const char *);
|
|
|
|
static int kill_job(Job *, int);
|
|
|
|
|
2007-01-12 11:18:22 +01:00
|
|
|
/* initialise job control */
|
2005-05-23 05:06:10 +02:00
|
|
|
void
|
2009-04-05 14:35:32 +02:00
|
|
|
j_init(void)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
|
|
|
bool mflagset = Flag(FMONITOR) != 127;
|
|
|
|
|
|
|
|
Flag(FMONITOR) = 0;
|
2009-09-20 18:40:58 +02:00
|
|
|
#endif
|
2009-04-05 14:35:32 +02:00
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2008-11-09 21:32:18 +01:00
|
|
|
(void)sigemptyset(&sm_default);
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &sm_default, NULL);
|
|
|
|
|
2008-11-09 21:32:18 +01:00
|
|
|
(void)sigemptyset(&sm_sigchld);
|
2009-03-26 12:22:53 +01:00
|
|
|
(void)sigaddset(&sm_sigchld, SIGCHLD);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
setsig(&sigtraps[SIGCHLD], j_sigchld,
|
|
|
|
SS_RESTORE_ORIG|SS_FORCE|SS_SHTRAP);
|
2011-01-30 02:35:35 +01:00
|
|
|
#else
|
|
|
|
/* Make sure SIGCHLD isn't ignored - can do odd things under SYSV */
|
|
|
|
setsig(&sigtraps[SIGCHLD], SIG_DFL, SS_RESTORE_ORIG|SS_FORCE);
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-04-03 11:42:25 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!mflagset && Flag(FTALKING))
|
|
|
|
Flag(FMONITOR) = 1;
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* shl_j is used to do asynchronous notification (used in
|
2005-05-23 05:06:10 +02:00
|
|
|
* an interrupt handler, so need a distinct shf)
|
|
|
|
*/
|
|
|
|
shl_j = shf_fdopen(2, SHF_WR, NULL);
|
|
|
|
|
|
|
|
if (Flag(FMONITOR) || Flag(FTALKING)) {
|
|
|
|
int i;
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* the TF_SHELL_USES test is a kludge that lets us know if
|
2005-05-23 05:06:10 +02:00
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* j_change() calls tty_init() */
|
|
|
|
if (Flag(FMONITOR))
|
|
|
|
j_change();
|
2009-04-05 14:35:32 +02:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (Flag(FTALKING))
|
2008-12-29 21:47:16 +01:00
|
|
|
tty_init(true, true);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-02-18 23:26:13 +01:00
|
|
|
static int
|
|
|
|
proc_errorlevel(Proc *p)
|
|
|
|
{
|
|
|
|
switch (p->state) {
|
|
|
|
case PEXITED:
|
|
|
|
return (WEXITSTATUS(p->status));
|
|
|
|
case PSIGNALLED:
|
|
|
|
return (128 + WTERMSIG(p->status));
|
|
|
|
default:
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/* job cleanup before shell exit */
|
|
|
|
void
|
|
|
|
j_exit(void)
|
|
|
|
{
|
|
|
|
/* kill stopped, and possibly running, jobs */
|
2011-01-30 02:35:35 +01:00
|
|
|
Job *j;
|
|
|
|
bool killed = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
for (j = job_list; j != NULL; j = j->next) {
|
|
|
|
if (j->ppid == procpid &&
|
|
|
|
(j->state == PSTOPPED ||
|
|
|
|
(j->state == PRUNNING &&
|
|
|
|
((j->flags & JF_FG) ||
|
|
|
|
(Flag(FLOGIN) && !Flag(FNOHUP) && procpid == kshpid))))) {
|
2011-01-30 02:35:35 +01:00
|
|
|
killed = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (j->pgrp == 0)
|
|
|
|
kill_job(j, SIGHUP);
|
|
|
|
else
|
2009-06-08 22:34:40 +02:00
|
|
|
mksh_killpg(j->pgrp, SIGHUP);
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
if (j->state == PSTOPPED) {
|
|
|
|
if (j->pgrp == 0)
|
|
|
|
kill_job(j, SIGCONT);
|
|
|
|
else
|
2009-06-08 22:34:40 +02:00
|
|
|
mksh_killpg(j->pgrp, SIGCONT);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (killed)
|
|
|
|
sleep(1);
|
|
|
|
j_notify();
|
|
|
|
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
if (kshpid == procpid && restore_ttypgrp >= 0) {
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Need to restore the tty pgrp to what it was when the
|
2005-05-23 05:06:10 +02:00
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
if (Flag(FMONITOR)) {
|
|
|
|
Flag(FMONITOR) = 0;
|
|
|
|
j_change();
|
|
|
|
}
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
/* turn job control on or off according to Flag(FMONITOR) */
|
|
|
|
void
|
|
|
|
j_change(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (Flag(FMONITOR)) {
|
2007-09-09 20:06:42 +02:00
|
|
|
bool use_tty = Flag(FTALKING);
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Don't call tcgetattr() 'til we own the tty process group */
|
2007-09-09 20:06:42 +02:00
|
|
|
if (use_tty)
|
2008-12-29 21:47:16 +01:00
|
|
|
tty_init(false, true);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* no controlling tty, no SIGT* */
|
2008-05-15 17:24:11 +02:00
|
|
|
if ((ttypgrp_ok = use_tty && tty_fd >= 0 && tty_devtty)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
setsig(&sigtraps[SIGTTIN], SIG_DFL,
|
|
|
|
SS_RESTORE_ORIG|SS_FORCE);
|
|
|
|
/* wait to be given tty (POSIX.1, B.2, job control) */
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
pid_t ttypgrp;
|
|
|
|
|
|
|
|
if ((ttypgrp = tcgetpgrp(tty_fd)) < 0) {
|
2010-08-28 22:22:24 +02:00
|
|
|
warningf(false, "%s: %s %s: %s",
|
|
|
|
"j_init", "tcgetpgrp", "failed",
|
2005-05-23 05:06:10 +02:00
|
|
|
strerror(errno));
|
2008-05-15 17:24:11 +02:00
|
|
|
ttypgrp_ok = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-05-17 21:03:25 +02:00
|
|
|
if (ttypgrp == kshpgrp)
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
kill(0, SIGTTIN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = NELEM(tt_sigs); --i >= 0; )
|
|
|
|
setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
|
|
|
|
SS_RESTORE_DFL|SS_FORCE);
|
2008-05-17 21:03:25 +02:00
|
|
|
if (ttypgrp_ok && kshpgrp != kshpid) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (setpgid(0, kshpid) < 0) {
|
2010-08-28 22:22:24 +02:00
|
|
|
warningf(false, "%s: %s %s: %s", "j_init",
|
|
|
|
"setpgid", "failed", strerror(errno));
|
2008-05-15 17:24:11 +02:00
|
|
|
ttypgrp_ok = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
if (tcsetpgrp(tty_fd, kshpid) < 0) {
|
2010-08-28 22:22:24 +02:00
|
|
|
warningf(false, "%s: %s %s: %s",
|
|
|
|
"j_init", "tcsetpgrp", "failed",
|
2005-05-23 05:06:10 +02:00
|
|
|
strerror(errno));
|
2008-05-15 17:24:11 +02:00
|
|
|
ttypgrp_ok = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else
|
2008-05-17 21:03:25 +02:00
|
|
|
restore_ttypgrp = kshpgrp;
|
|
|
|
kshpgrp = kshpid;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-05 00:34:51 +02:00
|
|
|
#ifndef MKSH_DISABLE_TTY_WARNING
|
2007-09-09 20:06:42 +02:00
|
|
|
if (use_tty && !ttypgrp_ok)
|
2010-08-28 20:50:58 +02:00
|
|
|
warningf(false, "%s: %s", "warning",
|
|
|
|
"won't have full job control");
|
2012-05-05 00:34:51 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
if (tty_fd >= 0)
|
2012-05-05 00:18:27 +02:00
|
|
|
mksh_tcget(tty_fd, &tty_state);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
2008-05-15 17:24:11 +02:00
|
|
|
ttypgrp_ok = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (Flag(FTALKING))
|
|
|
|
for (i = NELEM(tt_sigs); --i >= 0; )
|
|
|
|
setsig(&sigtraps[tt_sigs[i]], SIG_IGN,
|
|
|
|
SS_RESTORE_IGN|SS_FORCE);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (!Flag(FTALKING))
|
|
|
|
tty_close();
|
|
|
|
}
|
|
|
|
}
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-07-17 01:37:58 +02:00
|
|
|
#if HAVE_NICE
|
2011-07-16 19:41:50 +02:00
|
|
|
/* run nice(3) and ignore the result */
|
|
|
|
static void
|
|
|
|
ksh_nice(int ness)
|
|
|
|
{
|
|
|
|
#if defined(__USE_FORTIFY_LEVEL) && (__USE_FORTIFY_LEVEL > 0)
|
|
|
|
int e;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
/* this is gonna annoy users; complain to your distro, people! */
|
|
|
|
if (nice(ness) == -1 && (e = errno) != 0)
|
|
|
|
warningf(false, "%s: %s", "bgnice", strerror(e));
|
|
|
|
#else
|
|
|
|
(void)nice(ness);
|
|
|
|
#endif
|
|
|
|
}
|
2011-07-17 01:37:58 +02:00
|
|
|
#endif
|
2011-07-16 19:41:50 +02:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/* execute tree in child subprocess */
|
|
|
|
int
|
2009-03-22 18:47:38 +01:00
|
|
|
exchild(struct op *t, int flags,
|
|
|
|
volatile int *xerrok,
|
2011-01-30 02:35:35 +01:00
|
|
|
/* used if XPCLOSE or XCCLOSE */
|
|
|
|
int close_fd)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-01-30 02:35:35 +01:00
|
|
|
/* for pipelines */
|
|
|
|
static Proc *last_proc;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-02-18 23:26:13 +01:00
|
|
|
int rv = 0, forksleep, jwflags = JW_NONE;
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2009-10-17 23:16:05 +02:00
|
|
|
sigset_t omask;
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2011-01-21 22:04:48 +01:00
|
|
|
Proc *p;
|
|
|
|
Job *j;
|
|
|
|
pid_t cldpid;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-02-18 23:26:13 +01:00
|
|
|
if (flags & XPIPEST) {
|
|
|
|
flags &= ~XPIPEST;
|
|
|
|
jwflags |= JW_PIPEST;
|
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if (flags & XEXEC)
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Clear XFORK|XPCLOSE|XCCLOSE|XCOPROC|XPIPEO|XPIPEI|XXCOM|XBGND
|
2005-05-23 05:06:10 +02:00
|
|
|
* (also done in another execute() below)
|
|
|
|
*/
|
2009-06-08 22:06:50 +02:00
|
|
|
return (execute(t, flags & (XEXEC | XERROK), xerrok));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
/* no SIGCHLDs while messing with job and process lists */
|
|
|
|
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-01-21 22:04:48 +01:00
|
|
|
p = new_proc();
|
|
|
|
p->next = NULL;
|
|
|
|
p->state = PRUNNING;
|
|
|
|
p->status = 0;
|
|
|
|
p->pid = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* link process into jobs list */
|
2010-07-04 19:33:58 +02:00
|
|
|
if (flags & XPIPEI) {
|
|
|
|
/* continuing with a pipe */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!last_job)
|
2010-08-28 22:22:24 +02:00
|
|
|
internal_errorf("%s %d",
|
|
|
|
"exchild: XPIPEI and no last_job - pid",
|
2007-05-13 19:51:24 +02:00
|
|
|
(int)procpid);
|
2011-01-21 22:04:48 +01:00
|
|
|
j = last_job;
|
2008-10-14 01:06:04 +02:00
|
|
|
if (last_proc)
|
2011-01-21 22:04:48 +01:00
|
|
|
last_proc->next = p;
|
|
|
|
last_proc = p;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
2011-01-21 22:04:48 +01:00
|
|
|
/* fills in j->job */
|
|
|
|
j = new_job();
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* we don't consider XXCOMs foreground since they don't get
|
2005-05-23 05:06:10 +02:00
|
|
|
* tty process group and we don't save or restore tty modes.
|
|
|
|
*/
|
2011-01-21 22:04:48 +01:00
|
|
|
j->flags = (flags & XXCOM) ? JF_XXCOM :
|
2005-05-23 05:06:10 +02:00
|
|
|
((flags & XBGND) ? 0 : (JF_FG|JF_USETTYMODE));
|
2011-01-21 22:04:48 +01:00
|
|
|
timerclear(&j->usrtime);
|
|
|
|
timerclear(&j->systime);
|
|
|
|
j->state = PRUNNING;
|
|
|
|
j->pgrp = 0;
|
|
|
|
j->ppid = procpid;
|
|
|
|
j->age = ++njobs;
|
|
|
|
j->proc_list = p;
|
|
|
|
j->coproc_id = 0;
|
|
|
|
last_job = j;
|
|
|
|
last_proc = p;
|
|
|
|
put_job(j, PJ_PAST_STOPPED);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-03-06 18:08:14 +01:00
|
|
|
vistree(p->command, sizeof(p->command), t);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* create child process */
|
|
|
|
forksleep = 1;
|
2011-01-21 22:04:48 +01:00
|
|
|
while ((cldpid = fork()) < 0 && errno == EAGAIN && forksleep < 32) {
|
2011-01-30 02:35:35 +01:00
|
|
|
if (intrsig)
|
|
|
|
/* allow user to ^C out... */
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
sleep(forksleep);
|
|
|
|
forksleep <<= 1;
|
|
|
|
}
|
2011-01-21 22:04:48 +01:00
|
|
|
/* ensure $RANDOM changes between parent and child */
|
|
|
|
rndset((long)cldpid);
|
|
|
|
/* fork failed? */
|
|
|
|
if (cldpid < 0) {
|
|
|
|
kill_job(j, SIGKILL);
|
|
|
|
remove_job(j, "fork failed");
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2010-08-28 22:22:24 +02:00
|
|
|
errorf("can't fork - try again");
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-01-21 22:04:48 +01:00
|
|
|
p->pid = cldpid ? cldpid : (procpid = getpid());
|
2007-08-12 15:42:23 +02:00
|
|
|
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
/* job control set up */
|
|
|
|
if (Flag(FMONITOR) && !(flags&XXCOM)) {
|
2011-01-30 02:35:35 +01:00
|
|
|
bool dotty = false;
|
|
|
|
if (j->pgrp == 0) {
|
|
|
|
/* First process */
|
2011-01-21 22:04:48 +01:00
|
|
|
j->pgrp = p->pid;
|
2011-01-30 02:35:35 +01:00
|
|
|
dotty = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
/*
|
|
|
|
* set pgrp in both parent and child to deal with race
|
2005-05-23 05:06:10 +02:00
|
|
|
* condition
|
|
|
|
*/
|
2011-01-21 22:04:48 +01:00
|
|
|
setpgid(p->pid, j->pgrp);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (ttypgrp_ok && dotty && !(flags & XBGND))
|
2011-01-21 22:04:48 +01:00
|
|
|
tcsetpgrp(tty_fd, j->pgrp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* used to close pipe input fd */
|
2011-01-21 22:04:48 +01:00
|
|
|
if (close_fd >= 0 && (((flags & XPCLOSE) && cldpid) ||
|
|
|
|
((flags & XCCLOSE) && !cldpid)))
|
2005-05-23 05:06:10 +02:00
|
|
|
close(close_fd);
|
2011-01-21 22:04:48 +01:00
|
|
|
if (!cldpid) {
|
2010-07-04 19:33:58 +02:00
|
|
|
/* child */
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Do this before restoring signal */
|
|
|
|
if (flags & XCOPROC)
|
|
|
|
coproc_cleanup(false);
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
cleanup_parents_env();
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2011-01-30 02:35:35 +01:00
|
|
|
/*
|
|
|
|
* If FMONITOR or FTALKING is set, these signals are ignored,
|
2005-05-23 05:06:10 +02:00
|
|
|
* if neither FMONITOR nor FTALKING are set, the signals have
|
|
|
|
* their inherited values.
|
|
|
|
*/
|
|
|
|
if (Flag(FMONITOR) && !(flags & XXCOM)) {
|
2010-07-04 19:33:58 +02:00
|
|
|
for (forksleep = NELEM(tt_sigs); --forksleep >= 0; )
|
|
|
|
setsig(&sigtraps[tt_sigs[forksleep]], SIG_DFL,
|
2005-05-23 05:06:10 +02:00
|
|
|
SS_RESTORE_DFL|SS_FORCE);
|
|
|
|
}
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2008-10-26 22:51:27 +01:00
|
|
|
#if HAVE_NICE
|
2005-05-23 05:06:10 +02:00
|
|
|
if (Flag(FBGNICE) && (flags & XBGND))
|
2011-07-16 19:41:50 +02:00
|
|
|
ksh_nice(4);
|
2008-10-26 22:51:27 +01:00
|
|
|
#endif
|
2009-09-20 18:40:58 +02:00
|
|
|
if ((flags & XBGND)
|
|
|
|
#ifndef MKSH_UNEMPLOYED
|
|
|
|
&& !Flag(FMONITOR)
|
|
|
|
#endif
|
|
|
|
) {
|
2005-05-23 05:06:10 +02:00
|
|
|
setsig(&sigtraps[SIGINT], SIG_IGN,
|
|
|
|
SS_RESTORE_IGN|SS_FORCE);
|
|
|
|
setsig(&sigtraps[SIGQUIT], SIG_IGN,
|
|
|
|
SS_RESTORE_IGN|SS_FORCE);
|
2008-10-14 01:06:04 +02:00
|
|
|
if ((!(flags & (XPIPEI | XCOPROC))) &&
|
2010-07-04 19:33:58 +02:00
|
|
|
((forksleep = open("/dev/null", 0)) > 0)) {
|
|
|
|
(void)ksh_dup2(forksleep, 0, true);
|
|
|
|
close(forksleep);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
2011-01-30 02:35:35 +01:00
|
|
|
/* in case of $(jobs) command */
|
|
|
|
remove_job(j, "child");
|
2005-05-23 05:06:10 +02:00
|
|
|
nzombie = 0;
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2008-05-15 17:24:11 +02:00
|
|
|
ttypgrp_ok = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
Flag(FMONITOR) = 0;
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
Flag(FTALKING) = 0;
|
|
|
|
tty_close();
|
|
|
|
cleartraps();
|
2009-03-22 18:47:38 +01:00
|
|
|
/* no return */
|
|
|
|
execute(t, (flags & XERROK) | XEXEC, NULL);
|
2007-04-19 14:07:46 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2007-10-25 15:51:19 +02:00
|
|
|
if (t->type == TPIPE)
|
|
|
|
unwind(LLEAVE);
|
2010-08-28 20:50:58 +02:00
|
|
|
internal_warningf("%s: %s", "exchild", "execute() returned");
|
2011-03-06 18:08:14 +01:00
|
|
|
fptreef(shl_out, 8, "%s: tried to execute {\n\t%T\n}\n",
|
2010-08-28 20:50:58 +02:00
|
|
|
"exchild", t);
|
2007-04-19 14:07:46 +02:00
|
|
|
shf_flush(shl_out);
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
unwind(LLEAVE);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* shell (parent) stuff */
|
2011-01-30 02:35:35 +01:00
|
|
|
if (!(flags & XPIPEO)) {
|
|
|
|
/* last process in a job */
|
2011-01-21 22:04:48 +01:00
|
|
|
j_startjob(j);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (flags & XCOPROC) {
|
2011-01-21 22:04:48 +01:00
|
|
|
j->coproc_id = coproc.id;
|
2010-07-04 19:33:58 +02:00
|
|
|
/* n jobs using co-process output */
|
|
|
|
coproc.njobs++;
|
|
|
|
/* j using co-process input */
|
2011-01-21 22:04:48 +01:00
|
|
|
coproc.job = (void *)j;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (flags & XBGND) {
|
2011-01-21 22:04:48 +01:00
|
|
|
j_set_async(j);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (Flag(FTALKING)) {
|
2011-01-21 22:04:48 +01:00
|
|
|
shf_fprintf(shl_out, "[%d]", j->job);
|
|
|
|
for (p = j->proc_list; p; p = p->next)
|
2006-11-12 14:20:15 +01:00
|
|
|
shf_fprintf(shl_out, " %d",
|
2011-01-21 22:04:48 +01:00
|
|
|
(int)p->pid);
|
2005-05-23 05:06:10 +02:00
|
|
|
shf_putchar('\n', shl_out);
|
|
|
|
shf_flush(shl_out);
|
|
|
|
}
|
|
|
|
} else
|
2011-02-18 23:26:13 +01:00
|
|
|
rv = j_waitj(j, jwflags, "jw:last proc");
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* start the last job: only used for $(command) jobs */
|
|
|
|
void
|
|
|
|
startlast(void)
|
|
|
|
{
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigset_t omask;
|
|
|
|
|
|
|
|
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
/* no need to report error - waitlast() will do it */
|
|
|
|
if (last_job) {
|
2005-05-23 05:06:10 +02:00
|
|
|
/* ensure it isn't removed by check_job() */
|
|
|
|
last_job->flags |= JF_WAITING;
|
|
|
|
j_startjob(last_job);
|
|
|
|
}
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for last job: only used for $(command) jobs */
|
|
|
|
int
|
|
|
|
waitlast(void)
|
|
|
|
{
|
2011-01-30 02:35:35 +01:00
|
|
|
int rv;
|
|
|
|
Job *j;
|
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigset_t omask;
|
|
|
|
|
|
|
|
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
j = last_job;
|
|
|
|
if (!j || !(j->flags & JF_STARTED)) {
|
|
|
|
if (!j)
|
2010-08-28 20:50:58 +02:00
|
|
|
warningf(true, "%s: %s", "waitlast", "no last job");
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
2010-08-28 20:50:58 +02:00
|
|
|
internal_warningf("%s: %s", "waitlast", "not started");
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
|
|
|
/* not so arbitrary, non-zero value */
|
|
|
|
return (125);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2010-08-28 22:22:24 +02:00
|
|
|
rv = j_waitj(j, JW_NONE, "waitlast");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for child, interruptable. */
|
|
|
|
int
|
|
|
|
waitfor(const char *cp, int *sigp)
|
|
|
|
{
|
2011-01-30 02:35:35 +01:00
|
|
|
int rv, ecode, flags = JW_INTERRUPT|JW_ASYNCNOTIFY;
|
|
|
|
Job *j;
|
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigset_t omask;
|
|
|
|
|
|
|
|
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
*sigp = 0;
|
|
|
|
|
|
|
|
if (cp == NULL) {
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* wait for an unspecified job - always returns 0, so
|
2005-05-23 05:06:10 +02:00
|
|
|
* don't have to worry about exited/signaled jobs
|
|
|
|
*/
|
|
|
|
for (j = job_list; j; j = j->next)
|
2009-06-11 14:42:21 +02:00
|
|
|
/* AT&T ksh will wait for stopped jobs - we don't */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (j->ppid == procpid && j->state == PRUNNING)
|
|
|
|
break;
|
|
|
|
if (!j) {
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
} else if ((j = j_lookup(cp, &ecode))) {
|
|
|
|
/* don't report normal job completion */
|
|
|
|
flags &= ~JW_ASYNCNOTIFY;
|
|
|
|
if (j->ppid != procpid) {
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
} else {
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
if (ecode != JL_NOSUCH)
|
|
|
|
bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2009-06-11 14:42:21 +02:00
|
|
|
/* AT&T ksh will wait for stopped jobs - we don't */
|
2005-05-23 05:06:10 +02:00
|
|
|
rv = j_waitj(j, flags, "jw:waitfor");
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
if (rv < 0)
|
|
|
|
/* we were interrupted */
|
2005-05-23 05:06:10 +02:00
|
|
|
*sigp = 128 + -rv;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* kill (built-in) a job */
|
|
|
|
int
|
|
|
|
j_kill(const char *cp, int sig)
|
|
|
|
{
|
2011-01-30 02:35:35 +01:00
|
|
|
Job *j;
|
|
|
|
int rv = 0, ecode;
|
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigset_t omask;
|
|
|
|
|
|
|
|
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if ((j = j_lookup(cp, &ecode)) == NULL) {
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
if (j->pgrp == 0) {
|
|
|
|
/* started when !Flag(FMONITOR) */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (kill_job(j, sig) < 0) {
|
|
|
|
bi_errorf("%s: %s", cp, strerror(errno));
|
|
|
|
rv = 1;
|
|
|
|
}
|
|
|
|
} else {
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
if (j->state == PSTOPPED && (sig == SIGTERM || sig == SIGHUP))
|
2009-06-08 22:34:40 +02:00
|
|
|
mksh_killpg(j->pgrp, SIGCONT);
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2009-06-08 22:34:40 +02:00
|
|
|
if (mksh_killpg(j->pgrp, sig) < 0) {
|
2005-05-23 05:06:10 +02:00
|
|
|
bi_errorf("%s: %s", cp, strerror(errno));
|
|
|
|
rv = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
/* fg and bg built-ins: called only if Flag(FMONITOR) set */
|
|
|
|
int
|
|
|
|
j_resume(const char *cp, int bg)
|
|
|
|
{
|
2011-01-30 02:35:35 +01:00
|
|
|
Job *j;
|
|
|
|
Proc *p;
|
|
|
|
int ecode, rv = 0;
|
|
|
|
bool running;
|
2005-05-23 05:06:10 +02:00
|
|
|
sigset_t omask;
|
|
|
|
|
|
|
|
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
|
|
|
|
|
|
|
|
if ((j = j_lookup(cp, &ecode)) == NULL) {
|
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
|
|
|
bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (j->pgrp == 0) {
|
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
|
|
|
bi_errorf("job not job-controlled");
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bg)
|
|
|
|
shprintf("[%d] ", j->job);
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
running = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
for (p = j->proc_list; p != NULL; p = p->next) {
|
|
|
|
if (p->state == PSTOPPED) {
|
|
|
|
p->state = PRUNNING;
|
|
|
|
p->status = 0;
|
2011-01-30 02:35:35 +01:00
|
|
|
running = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2008-04-01 23:50:58 +02:00
|
|
|
shf_puts(p->command, shl_stdout);
|
|
|
|
if (p->next)
|
|
|
|
shf_puts("| ", shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2007-07-22 15:34:52 +02:00
|
|
|
shf_putc('\n', shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
shf_flush(shl_stdout);
|
|
|
|
if (running)
|
|
|
|
j->state = PRUNNING;
|
|
|
|
|
|
|
|
put_job(j, PJ_PAST_STOPPED);
|
|
|
|
if (bg)
|
|
|
|
j_set_async(j);
|
|
|
|
else {
|
|
|
|
/* attach tty to job */
|
|
|
|
if (j->state == PRUNNING) {
|
|
|
|
if (ttypgrp_ok && (j->flags & JF_SAVEDTTY))
|
2012-05-05 00:18:27 +02:00
|
|
|
mksh_tcset(tty_fd, &j->ttystat);
|
2005-05-23 05:06:10 +02:00
|
|
|
/* See comment in j_waitj regarding saved_ttypgrp. */
|
|
|
|
if (ttypgrp_ok &&
|
|
|
|
tcsetpgrp(tty_fd, (j->flags & JF_SAVEDTTYPGRP) ?
|
|
|
|
j->saved_ttypgrp : j->pgrp) < 0) {
|
2007-06-07 01:41:24 +02:00
|
|
|
rv = errno;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (j->flags & JF_SAVEDTTY)
|
2012-05-05 00:18:27 +02:00
|
|
|
mksh_tcset(tty_fd, &tty_state);
|
2011-01-30 02:35:35 +01:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2010-08-28 22:22:24 +02:00
|
|
|
bi_errorf("%s %s(%d, %ld) %s: %s",
|
|
|
|
"1st", "tcsetpgrp", tty_fd,
|
|
|
|
(long)((j->flags & JF_SAVEDTTYPGRP) ?
|
|
|
|
j->saved_ttypgrp : j->pgrp), "failed",
|
2007-06-07 01:41:24 +02:00
|
|
|
strerror(rv));
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
j->flags |= JF_FG;
|
|
|
|
j->flags &= ~JF_KNOWN;
|
|
|
|
if (j == async_job)
|
|
|
|
async_job = NULL;
|
|
|
|
}
|
|
|
|
|
2009-06-08 22:34:40 +02:00
|
|
|
if (j->state == PRUNNING && mksh_killpg(j->pgrp, SIGCONT) < 0) {
|
2007-06-07 01:41:24 +02:00
|
|
|
int err = errno;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (!bg) {
|
|
|
|
j->flags &= ~JF_FG;
|
|
|
|
if (ttypgrp_ok && (j->flags & JF_SAVEDTTY))
|
2012-05-05 00:18:27 +02:00
|
|
|
mksh_tcset(tty_fd, &tty_state);
|
2008-05-17 21:03:25 +02:00
|
|
|
if (ttypgrp_ok && tcsetpgrp(tty_fd, kshpgrp) < 0)
|
2010-08-28 22:22:24 +02:00
|
|
|
warningf(true, "%s %s(%d, %ld) %s: %s",
|
|
|
|
"fg: 2nd", "tcsetpgrp", tty_fd,
|
|
|
|
(long)kshpgrp, "failed", strerror(errno));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2010-08-28 22:22:24 +02:00
|
|
|
bi_errorf("%s %s %s", "can't continue job",
|
2005-05-23 05:06:10 +02:00
|
|
|
cp, strerror(err));
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (!bg) {
|
|
|
|
if (ttypgrp_ok) {
|
|
|
|
j->flags &= ~(JF_SAVEDTTY | JF_SAVEDTTYPGRP);
|
|
|
|
}
|
|
|
|
rv = j_waitj(j, JW_NONE, "jw:resume");
|
|
|
|
}
|
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* are there any running or stopped jobs ? */
|
|
|
|
int
|
|
|
|
j_stopped_running(void)
|
|
|
|
{
|
2011-01-30 02:35:35 +01:00
|
|
|
Job *j;
|
|
|
|
int which = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
for (j = job_list; j != NULL; j = j->next) {
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
if (j->ppid == procpid && j->state == PSTOPPED)
|
|
|
|
which |= 1;
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
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" : "");
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* list jobs for jobs built-in */
|
|
|
|
int
|
|
|
|
j_jobs(const char *cp, int slp,
|
2011-01-30 02:35:35 +01:00
|
|
|
/* 0: short, 1: long, 2: pgrp */
|
|
|
|
int nflag)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-01-30 02:35:35 +01:00
|
|
|
Job *j, *tmp;
|
|
|
|
int how, zflag = 0;
|
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigset_t omask;
|
|
|
|
|
|
|
|
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
if (nflag < 0) {
|
|
|
|
/* kludge: print zombies */
|
2005-05-23 05:06:10 +02:00
|
|
|
nflag = 0;
|
|
|
|
zflag = 1;
|
|
|
|
}
|
|
|
|
if (cp) {
|
|
|
|
int ecode;
|
|
|
|
|
|
|
|
if ((j = j_lookup(cp, &ecode)) == NULL) {
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
bi_errorf("%s: %s", cp, lookup_msgs[ecode]);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
j = job_list;
|
|
|
|
how = slp == 0 ? JP_MEDIUM : (slp == 1 ? JP_LONG : JP_PGRP);
|
|
|
|
for (; j; j = j->next) {
|
|
|
|
if ((!(j->flags & JF_ZOMBIE) || zflag) &&
|
|
|
|
(!nflag || (j->flags & JF_CHANGED))) {
|
|
|
|
j_print(j, how, shl_stdout);
|
|
|
|
if (j->state == PEXITED || j->state == PSIGNALLED)
|
|
|
|
j->flags |= JF_REMOVE;
|
|
|
|
}
|
|
|
|
if (cp)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Remove jobs after printing so there won't be multiple + or - jobs */
|
|
|
|
for (j = job_list; j; j = tmp) {
|
|
|
|
tmp = j->next;
|
|
|
|
if (j->flags & JF_REMOVE)
|
|
|
|
remove_job(j, "jobs");
|
|
|
|
}
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* list jobs for top-level notification */
|
|
|
|
void
|
|
|
|
j_notify(void)
|
|
|
|
{
|
2011-01-30 02:35:35 +01:00
|
|
|
Job *j, *tmp;
|
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigset_t omask;
|
|
|
|
|
|
|
|
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
for (j = job_list; j; j = j->next) {
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
if (Flag(FMONITOR) && (j->flags & JF_CHANGED))
|
|
|
|
j_print(j, JP_MEDIUM, shl_out);
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2011-01-30 02:35:35 +01:00
|
|
|
/*
|
|
|
|
* Remove job after doing reports so there aren't
|
2005-05-23 05:06:10 +02:00
|
|
|
* multiple +/- jobs.
|
|
|
|
*/
|
|
|
|
if (j->state == PEXITED || j->state == PSIGNALLED)
|
|
|
|
j->flags |= JF_REMOVE;
|
|
|
|
}
|
|
|
|
for (j = job_list; j; j = tmp) {
|
|
|
|
tmp = j->next;
|
|
|
|
if (j->flags & JF_REMOVE)
|
|
|
|
remove_job(j, "notify");
|
|
|
|
}
|
|
|
|
shf_flush(shl_out);
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return pid of last process in last asynchronous job */
|
|
|
|
pid_t
|
|
|
|
j_async(void)
|
|
|
|
{
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigset_t omask;
|
|
|
|
|
|
|
|
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (async_job)
|
|
|
|
async_job->flags |= JF_KNOWN;
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (async_pid);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Make j the last async process
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* If jobs are compiled in then this routine expects sigchld to be blocked.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
j_set_async(Job *j)
|
|
|
|
{
|
|
|
|
Job *jl, *oldest;
|
|
|
|
|
|
|
|
if (async_job && (async_job->flags & (JF_KNOWN|JF_ZOMBIE)) == JF_ZOMBIE)
|
|
|
|
remove_job(async_job, "async");
|
|
|
|
if (!(j->flags & JF_STARTED)) {
|
2010-08-28 20:50:58 +02:00
|
|
|
internal_warningf("%s: %s", "j_async", "job not started");
|
2005-05-23 05:06:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
async_job = j;
|
|
|
|
async_pid = j->last_proc->pid;
|
2006-11-09 23:08:08 +01:00
|
|
|
while (nzombie > CHILD_MAX) {
|
2005-05-23 05:06:10 +02:00
|
|
|
oldest = NULL;
|
|
|
|
for (jl = job_list; jl; jl = jl->next)
|
|
|
|
if (jl != async_job && (jl->flags & JF_ZOMBIE) &&
|
|
|
|
(!oldest || jl->age < oldest->age))
|
|
|
|
oldest = jl;
|
|
|
|
if (!oldest) {
|
|
|
|
/* XXX debugging */
|
|
|
|
if (!(async_job->flags & JF_ZOMBIE) || nzombie != 1) {
|
2010-08-28 20:50:58 +02:00
|
|
|
internal_warningf("%s: bad nzombie (%d)",
|
|
|
|
"j_async", nzombie);
|
2005-05-23 05:06:10 +02:00
|
|
|
nzombie = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
remove_job(oldest, "zombie");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Start a job: set STARTED, check for held signals and set j->last_proc
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* If jobs are compiled in then this routine expects sigchld to be blocked.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
j_startjob(Job *j)
|
|
|
|
{
|
|
|
|
Proc *p;
|
|
|
|
|
|
|
|
j->flags |= JF_STARTED;
|
|
|
|
for (p = j->proc_list; p->next; p = p->next)
|
|
|
|
;
|
|
|
|
j->last_proc = p;
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
if (held_sigchld) {
|
|
|
|
held_sigchld = 0;
|
|
|
|
/* Don't call j_sigchld() as it may remove job... */
|
|
|
|
kill(procpid, SIGCHLD);
|
|
|
|
}
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* wait for job to complete or change state
|
|
|
|
*
|
|
|
|
* If jobs are compiled in then this routine expects sigchld to be blocked.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
j_waitj(Job *j,
|
2011-01-30 02:35:35 +01:00
|
|
|
/* see JW_* */
|
|
|
|
int flags,
|
2005-05-23 05:06:10 +02:00
|
|
|
const char *where)
|
|
|
|
{
|
2011-08-27 20:06:52 +02:00
|
|
|
int rv;
|
2012-04-27 18:16:23 +02:00
|
|
|
#ifdef MKSH_NO_SIGSUSPEND
|
|
|
|
sigset_t omask;
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* No auto-notify on the job we are waiting on.
|
|
|
|
*/
|
|
|
|
j->flags |= JF_WAITING;
|
|
|
|
if (flags & JW_ASYNCNOTIFY)
|
|
|
|
j->flags |= JF_W_ASYNCNOTIFY;
|
|
|
|
|
2009-09-20 18:40:58 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!Flag(FMONITOR))
|
2009-09-20 18:40:58 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
flags |= JW_STOPPEDWAIT;
|
|
|
|
|
2007-06-07 01:28:17 +02:00
|
|
|
while (j->state == PRUNNING ||
|
|
|
|
((flags & JW_STOPPEDWAIT) && j->state == PSTOPPED)) {
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2012-04-27 18:16:23 +02:00
|
|
|
#ifdef MKSH_NO_SIGSUSPEND
|
|
|
|
sigprocmask(SIG_SETMASK, &sm_default, &omask);
|
|
|
|
pause();
|
|
|
|
/* note that handlers may run here so they need to know */
|
2012-05-04 23:48:29 +02:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2012-04-27 18:16:23 +02:00
|
|
|
#else
|
2005-05-23 05:06:10 +02:00
|
|
|
sigsuspend(&sm_default);
|
2012-04-27 18:16:23 +02:00
|
|
|
#endif
|
2011-01-30 02:35:35 +01:00
|
|
|
#else
|
|
|
|
j_sigchld(SIGCHLD);
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
if (fatal_trap) {
|
|
|
|
int oldf = j->flags & (JF_WAITING|JF_W_ASYNCNOTIFY);
|
|
|
|
j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
|
|
|
|
runtraps(TF_FATAL);
|
2011-01-30 02:35:35 +01:00
|
|
|
/* not reached... */
|
|
|
|
j->flags |= oldf;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if ((flags & JW_INTERRUPT) && (rv = trap_pending())) {
|
|
|
|
j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
j->flags &= ~(JF_WAITING|JF_W_ASYNCNOTIFY);
|
|
|
|
|
|
|
|
if (j->flags & JF_FG) {
|
|
|
|
j->flags &= ~JF_FG;
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
if (Flag(FMONITOR) && ttypgrp_ok && j->pgrp) {
|
|
|
|
/*
|
|
|
|
* Save the tty's current pgrp so it can be restored
|
2009-06-10 20:12:51 +02:00
|
|
|
* when the job is foregrounded. This is to
|
2005-05-23 05:06:10 +02:00
|
|
|
* deal with things like the GNU su which does
|
|
|
|
* a fork/exec instead of an exec (the fork means
|
|
|
|
* the execed shell gets a different pid from its
|
|
|
|
* pgrp, so naturally it sets its pgrp and gets hosed
|
2006-01-30 13:37:24 +01:00
|
|
|
* when it gets foregrounded by the parent shell which
|
2005-05-23 05:06:10 +02:00
|
|
|
* has restored the tty's pgrp to that of the su
|
|
|
|
* process).
|
|
|
|
*/
|
|
|
|
if (j->state == PSTOPPED &&
|
|
|
|
(j->saved_ttypgrp = tcgetpgrp(tty_fd)) >= 0)
|
|
|
|
j->flags |= JF_SAVEDTTYPGRP;
|
2008-05-17 21:03:25 +02:00
|
|
|
if (tcsetpgrp(tty_fd, kshpgrp) < 0)
|
2010-08-28 22:22:24 +02:00
|
|
|
warningf(true, "%s %s(%d, %ld) %s: %s",
|
|
|
|
"j_waitj:", "tcsetpgrp", tty_fd,
|
|
|
|
(long)kshpgrp, "failed", strerror(errno));
|
2005-05-23 05:06:10 +02:00
|
|
|
if (j->state == PSTOPPED) {
|
|
|
|
j->flags |= JF_SAVEDTTY;
|
2012-05-05 00:18:27 +02:00
|
|
|
mksh_tcget(tty_fd, &j->ttystat);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
if (tty_fd >= 0) {
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Only restore tty settings if job was originally
|
2009-06-10 20:12:51 +02:00
|
|
|
* started in the foreground. Problems can be
|
2005-05-23 05:06:10 +02:00
|
|
|
* caused by things like 'more foobar &' which will
|
|
|
|
* typically get and save the shell's vi/emacs tty
|
|
|
|
* settings before setting up the tty for itself;
|
|
|
|
* when more exits, it restores the 'original'
|
|
|
|
* settings, and things go down hill from there...
|
|
|
|
*/
|
|
|
|
if (j->state == PEXITED && j->status == 0 &&
|
|
|
|
(j->flags & JF_USETTYMODE)) {
|
2012-05-05 00:18:27 +02:00
|
|
|
mksh_tcget(tty_fd, &tty_state);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
2012-05-05 00:18:27 +02:00
|
|
|
mksh_tcset(tty_fd, &tty_state);
|
2010-07-04 15:36:43 +02:00
|
|
|
/*-
|
|
|
|
* Don't use tty mode if job is stopped and
|
2009-06-10 20:12:51 +02:00
|
|
|
* later restarted and exits. Consider
|
2005-05-23 05:06:10 +02:00
|
|
|
* the sequence:
|
|
|
|
* vi foo (stopped)
|
|
|
|
* ...
|
|
|
|
* stty something
|
|
|
|
* ...
|
|
|
|
* fg (vi; ZZ)
|
|
|
|
* mode should be that of the stty, not what
|
|
|
|
* was before the vi started.
|
|
|
|
*/
|
|
|
|
if (j->state == PSTOPPED)
|
|
|
|
j->flags &= ~JF_USETTYMODE;
|
|
|
|
}
|
|
|
|
}
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* If it looks like user hit ^C to kill a job, pretend we got
|
2009-06-11 14:42:21 +02:00
|
|
|
* one too to break out of for loops, etc. (AT&T ksh does this
|
2005-05-23 05:06:10 +02:00
|
|
|
* even when not monitoring, but this doesn't make sense since
|
|
|
|
* a tty generated ^C goes to the whole process group)
|
|
|
|
*/
|
2009-04-05 14:35:32 +02:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
status = j->last_proc->status;
|
|
|
|
if (Flag(FMONITOR) && j->state == PSIGNALLED &&
|
2009-04-05 15:37:37 +02:00
|
|
|
WIFSIGNALED(status) &&
|
2009-04-05 14:35:32 +02:00
|
|
|
(sigtraps[WTERMSIG(status)].flags & TF_TTY_INTR))
|
|
|
|
trapsig(WTERMSIG(status));
|
|
|
|
}
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
j_usrtime = j->usrtime;
|
|
|
|
j_systime = j->systime;
|
|
|
|
rv = j->status;
|
|
|
|
|
2011-02-18 23:26:13 +01:00
|
|
|
if ((flags & JW_PIPEST) && (j->proc_list != NULL)) {
|
2011-08-27 20:06:52 +02:00
|
|
|
uint32_t num = 0;
|
2011-02-18 23:26:13 +01:00
|
|
|
Proc *p = j->proc_list;
|
|
|
|
struct tbl *vp;
|
|
|
|
|
|
|
|
unset(vp_pipest, 1);
|
|
|
|
vp = vp_pipest;
|
|
|
|
vp->flag = DEFINED | ISSET | INTEGER | RDONLY | ARRAY | INT_U;
|
|
|
|
goto got_array;
|
|
|
|
|
|
|
|
while (p != NULL) {
|
|
|
|
{
|
|
|
|
struct tbl *vq;
|
|
|
|
|
|
|
|
/* strlen(vp_pipest->name) == 10 */
|
|
|
|
vq = alloc(offsetof(struct tbl, name[0]) + 11,
|
|
|
|
vp_pipest->areap);
|
|
|
|
memset(vq, 0, offsetof(struct tbl, name[0]));
|
|
|
|
memcpy(vq->name, vp_pipest->name, 11);
|
|
|
|
vp->u.array = vq;
|
|
|
|
vp = vq;
|
|
|
|
}
|
|
|
|
vp->areap = vp_pipest->areap;
|
|
|
|
vp->ua.index = ++num;
|
|
|
|
vp->flag = DEFINED | ISSET | INTEGER | RDONLY |
|
|
|
|
ARRAY | INT_U | AINDEX;
|
|
|
|
got_array:
|
|
|
|
vp->val.i = proc_errorlevel(p);
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-20 18:40:58 +02:00
|
|
|
if (!(flags & JW_ASYNCNOTIFY)
|
|
|
|
#ifndef MKSH_UNEMPLOYED
|
|
|
|
&& (!Flag(FMONITOR) || j->state != PSTOPPED)
|
|
|
|
#endif
|
|
|
|
) {
|
2005-05-23 05:06:10 +02:00
|
|
|
j_print(j, JP_SHORT, shl_out);
|
|
|
|
shf_flush(shl_out);
|
|
|
|
}
|
2009-09-20 18:40:58 +02:00
|
|
|
if (j->state != PSTOPPED
|
|
|
|
#ifndef MKSH_UNEMPLOYED
|
|
|
|
&& (!Flag(FMONITOR) || !(flags & JW_ASYNCNOTIFY))
|
|
|
|
#endif
|
|
|
|
)
|
2005-05-23 05:06:10 +02:00
|
|
|
remove_job(j, where);
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* SIGCHLD handler to reap children and update job states
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* If jobs are compiled in then this routine expects sigchld to be blocked.
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
|
|
|
static void
|
2009-12-12 23:27:10 +01:00
|
|
|
j_sigchld(int sig MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2010-07-04 15:36:43 +02:00
|
|
|
/* this runs inside interrupt context, with errno saved */
|
|
|
|
|
|
|
|
Job *j;
|
|
|
|
Proc *p = NULL;
|
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
struct rusage ru0, ru1;
|
2012-04-27 18:16:23 +02:00
|
|
|
#ifdef MKSH_NO_SIGSUSPEND
|
|
|
|
sigset_t omask;
|
|
|
|
|
|
|
|
/* this handler can run while SIGCHLD is not blocked, so block it now */
|
|
|
|
sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
|
|
|
|
#endif
|
2010-07-04 15:36:43 +02:00
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Don't wait for any processes if a job is partially started.
|
2005-05-23 05:06:10 +02:00
|
|
|
* This is so we don't do away with the process group leader
|
|
|
|
* before all the processes in a pipe line are started (so the
|
|
|
|
* setpgid() won't fail)
|
|
|
|
*/
|
|
|
|
for (j = job_list; j; j = j->next)
|
|
|
|
if (j->ppid == procpid && !(j->flags & JF_STARTED)) {
|
|
|
|
held_sigchld = 1;
|
2012-04-27 18:16:23 +02:00
|
|
|
goto j_sigchld_out;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
getrusage(RUSAGE_CHILDREN, &ru0);
|
|
|
|
do {
|
2011-01-30 02:35:35 +01:00
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2005-05-23 05:06:10 +02:00
|
|
|
pid = waitpid(-1, &status, (WNOHANG|WUNTRACED));
|
2011-01-30 02:35:35 +01:00
|
|
|
#else
|
|
|
|
pid = wait(&status);
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* return if this would block (0) or no children
|
|
|
|
* or interrupted (-1)
|
|
|
|
*/
|
|
|
|
if (pid <= 0)
|
2012-04-27 18:16:23 +02:00
|
|
|
goto j_sigchld_out;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
getrusage(RUSAGE_CHILDREN, &ru1);
|
|
|
|
|
|
|
|
/* find job and process structures for this pid */
|
|
|
|
for (j = job_list; j != NULL; j = j->next)
|
|
|
|
for (p = j->proc_list; p != NULL; p = p->next)
|
|
|
|
if (p->pid == pid)
|
|
|
|
goto found;
|
2006-08-01 15:43:28 +02:00
|
|
|
found:
|
2005-05-23 05:06:10 +02:00
|
|
|
if (j == NULL) {
|
|
|
|
/* Can occur if process has kids, then execs shell
|
|
|
|
warningf(true, "bad process waited for (pid = %d)",
|
|
|
|
pid);
|
|
|
|
*/
|
|
|
|
ru0 = ru1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeradd(&j->usrtime, &ru1.ru_utime, &j->usrtime);
|
|
|
|
timersub(&j->usrtime, &ru0.ru_utime, &j->usrtime);
|
|
|
|
timeradd(&j->systime, &ru1.ru_stime, &j->systime);
|
|
|
|
timersub(&j->systime, &ru0.ru_stime, &j->systime);
|
|
|
|
ru0 = ru1;
|
|
|
|
p->status = status;
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2009-04-05 15:37:37 +02:00
|
|
|
if (WIFSTOPPED(status))
|
2005-05-23 05:06:10 +02:00
|
|
|
p->state = PSTOPPED;
|
2009-04-05 14:35:32 +02:00
|
|
|
else
|
|
|
|
#endif
|
2009-04-05 15:37:37 +02:00
|
|
|
if (WIFSIGNALED(status))
|
2005-05-23 05:06:10 +02:00
|
|
|
p->state = PSIGNALLED;
|
|
|
|
else
|
|
|
|
p->state = PEXITED;
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
/* check to see if entire job is done */
|
|
|
|
check_job(j);
|
|
|
|
}
|
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 1);
|
2011-01-30 02:35:35 +01:00
|
|
|
#else
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 0);
|
2011-01-30 02:35:35 +01:00
|
|
|
#endif
|
2012-04-27 18:16:23 +02:00
|
|
|
|
|
|
|
j_sigchld_out:
|
|
|
|
#ifdef MKSH_NO_SIGSUSPEND
|
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
|
|
|
#endif
|
|
|
|
/* nothing */;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called only when a process in j has exited/stopped (ie, called only
|
2009-06-10 20:12:51 +02:00
|
|
|
* from j_sigchld()). If no processes are running, the job status
|
2005-05-23 05:06:10 +02:00
|
|
|
* and state are updated, asynchronous job notification is done and,
|
|
|
|
* if unneeded, the job is removed.
|
|
|
|
*
|
|
|
|
* If jobs are compiled in then this routine expects sigchld to be blocked.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
check_job(Job *j)
|
|
|
|
{
|
|
|
|
int jstate;
|
|
|
|
Proc *p;
|
|
|
|
|
|
|
|
/* XXX debugging (nasty - interrupt routine using shl_out) */
|
|
|
|
if (!(j->flags & JF_STARTED)) {
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_warningf("check_job: job started (flags 0x%x)",
|
2005-05-23 05:06:10 +02:00
|
|
|
j->flags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
jstate = PRUNNING;
|
|
|
|
for (p=j->proc_list; p != NULL; p = p->next) {
|
|
|
|
if (p->state == PRUNNING)
|
2011-01-30 02:35:35 +01:00
|
|
|
/* some processes still running */
|
|
|
|
return;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (p->state > jstate)
|
|
|
|
jstate = p->state;
|
|
|
|
}
|
|
|
|
j->state = jstate;
|
2011-02-18 23:26:13 +01:00
|
|
|
j->status = proc_errorlevel(j->last_proc);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Note when co-process dies: can't be done in j_wait() nor
|
2005-05-23 05:06:10 +02:00
|
|
|
* remove_job() since neither may be called for non-interactive
|
|
|
|
* shells.
|
|
|
|
*/
|
|
|
|
if (j->state == PEXITED || j->state == PSIGNALLED) {
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* No need to keep co-process input any more
|
2005-05-23 05:06:10 +02:00
|
|
|
* (at least, this is what ksh93d thinks)
|
|
|
|
*/
|
|
|
|
if (coproc.job == j) {
|
|
|
|
coproc.job = NULL;
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* XXX would be nice to get the closes out of here
|
2005-05-23 05:06:10 +02:00
|
|
|
* so they aren't done in the signal handler.
|
|
|
|
* Would mean a check in coproc_getfd() to
|
|
|
|
* do "if job == 0 && write >= 0, close write".
|
|
|
|
*/
|
|
|
|
coproc_write_close(coproc.write);
|
|
|
|
}
|
|
|
|
/* Do we need to keep the output? */
|
|
|
|
if (j->coproc_id && j->coproc_id == coproc.id &&
|
|
|
|
--coproc.njobs == 0)
|
|
|
|
coproc_readw_close(coproc.read);
|
|
|
|
}
|
|
|
|
|
|
|
|
j->flags |= JF_CHANGED;
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
if (Flag(FMONITOR) && !(j->flags & JF_XXCOM)) {
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Only put stopped jobs at the front to avoid confusing
|
2005-05-23 05:06:10 +02:00
|
|
|
* the user (don't want finished jobs effecting %+ or %-)
|
|
|
|
*/
|
|
|
|
if (j->state == PSTOPPED)
|
|
|
|
put_job(j, PJ_ON_FRONT);
|
|
|
|
if (Flag(FNOTIFY) &&
|
|
|
|
(j->flags & (JF_WAITING|JF_W_ASYNCNOTIFY)) != JF_WAITING) {
|
|
|
|
/* Look for the real file descriptor 2 */
|
|
|
|
{
|
|
|
|
struct env *ep;
|
|
|
|
int fd = 2;
|
|
|
|
|
|
|
|
for (ep = e; ep; ep = ep->oenv)
|
|
|
|
if (ep->savefd && ep->savefd[2])
|
|
|
|
fd = ep->savefd[2];
|
|
|
|
shf_reopen(fd, SHF_WR, shl_j);
|
|
|
|
}
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Can't call j_notify() as it removes jobs. The job
|
2005-05-23 05:06:10 +02:00
|
|
|
* must stay in the job list as j_waitj() may be
|
|
|
|
* running with this job.
|
|
|
|
*/
|
|
|
|
j_print(j, JP_MEDIUM, shl_j);
|
|
|
|
shf_flush(shl_j);
|
|
|
|
if (!(j->flags & JF_WAITING) && j->state != PSTOPPED)
|
|
|
|
remove_job(j, "notify");
|
|
|
|
}
|
|
|
|
}
|
2009-04-05 14:35:32 +02:00
|
|
|
#endif
|
2009-09-20 18:40:58 +02:00
|
|
|
if (
|
|
|
|
#ifndef MKSH_UNEMPLOYED
|
|
|
|
!Flag(FMONITOR) &&
|
|
|
|
#endif
|
|
|
|
!(j->flags & (JF_WAITING|JF_FG)) &&
|
2005-05-23 05:06:10 +02:00
|
|
|
j->state != PSTOPPED) {
|
|
|
|
if (j == async_job || (j->flags & JF_KNOWN)) {
|
|
|
|
j->flags |= JF_ZOMBIE;
|
|
|
|
j->job = -1;
|
|
|
|
nzombie++;
|
|
|
|
} else
|
|
|
|
remove_job(j, "checkjob");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print job status in either short, medium or long format.
|
|
|
|
*
|
|
|
|
* If jobs are compiled in then this routine expects sigchld to be blocked.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
j_print(Job *j, int how, struct shf *shf)
|
|
|
|
{
|
|
|
|
Proc *p;
|
|
|
|
int state;
|
|
|
|
int status;
|
|
|
|
int coredumped;
|
|
|
|
char jobchar = ' ';
|
|
|
|
char buf[64];
|
|
|
|
const char *filler;
|
|
|
|
int output = 0;
|
|
|
|
|
|
|
|
if (how == JP_PGRP) {
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* POSIX doesn't say what to do it there is no process
|
2009-06-10 20:12:51 +02:00
|
|
|
* group leader (ie, !FMONITOR). We arbitrarily return
|
2005-05-23 05:06:10 +02:00
|
|
|
* last pid (which is what $! returns).
|
|
|
|
*/
|
2006-11-12 13:49:25 +01:00
|
|
|
shf_fprintf(shf, "%d\n", (int)(j->pgrp ? j->pgrp :
|
|
|
|
(j->last_proc ? j->last_proc->pid : 0)));
|
2005-05-23 05:06:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
j->flags &= ~JF_CHANGED;
|
2009-06-10 20:12:51 +02:00
|
|
|
filler = j->job > 10 ? "\n " : "\n ";
|
2005-05-23 05:06:10 +02:00
|
|
|
if (j == job_list)
|
|
|
|
jobchar = '+';
|
|
|
|
else if (j == job_list->next)
|
|
|
|
jobchar = '-';
|
|
|
|
|
|
|
|
for (p = j->proc_list; p != NULL;) {
|
|
|
|
coredumped = 0;
|
|
|
|
switch (p->state) {
|
|
|
|
case PRUNNING:
|
2009-08-01 22:32:45 +02:00
|
|
|
memcpy(buf, "Running", 8);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case PSTOPPED:
|
|
|
|
strlcpy(buf, sigtraps[WSTOPSIG(p->status)].mess,
|
2009-06-08 22:06:50 +02:00
|
|
|
sizeof(buf));
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case PEXITED:
|
|
|
|
if (how == JP_SHORT)
|
|
|
|
buf[0] = '\0';
|
|
|
|
else if (WEXITSTATUS(p->status) == 0)
|
2009-08-01 22:32:45 +02:00
|
|
|
memcpy(buf, "Done", 5);
|
2005-05-23 05:06:10 +02:00
|
|
|
else
|
|
|
|
shf_snprintf(buf, sizeof(buf), "Done (%d)",
|
|
|
|
WEXITSTATUS(p->status));
|
|
|
|
break;
|
|
|
|
case PSIGNALLED:
|
2006-08-24 22:32:53 +02:00
|
|
|
#ifdef WCOREDUMP
|
2005-05-23 05:06:10 +02:00
|
|
|
if (WCOREDUMP(p->status))
|
|
|
|
coredumped = 1;
|
2006-08-24 22:32:53 +02:00
|
|
|
#endif
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* kludge for not reporting 'normal termination
|
|
|
|
* signals' (i.e. SIGINT, SIGPIPE)
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
if (how == JP_SHORT && !coredumped &&
|
|
|
|
(WTERMSIG(p->status) == SIGINT ||
|
|
|
|
WTERMSIG(p->status) == SIGPIPE)) {
|
|
|
|
buf[0] = '\0';
|
|
|
|
} else
|
|
|
|
strlcpy(buf, sigtraps[WTERMSIG(p->status)].mess,
|
2009-06-08 22:06:50 +02:00
|
|
|
sizeof(buf));
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2011-12-31 01:52:22 +01:00
|
|
|
default:
|
|
|
|
buf[0] = '\0';
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (how != JP_SHORT) {
|
|
|
|
if (p == j->proc_list)
|
|
|
|
shf_fprintf(shf, "[%d] %c ", j->job, jobchar);
|
|
|
|
else
|
2010-08-28 22:22:24 +02:00
|
|
|
shf_puts(filler, shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (how == JP_LONG)
|
2006-11-12 13:49:25 +01:00
|
|
|
shf_fprintf(shf, "%5d ", (int)p->pid);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (how == JP_SHORT) {
|
|
|
|
if (buf[0]) {
|
|
|
|
output = 1;
|
|
|
|
shf_fprintf(shf, "%s%s ",
|
|
|
|
buf, coredumped ? " (core dumped)" : null);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output = 1;
|
|
|
|
shf_fprintf(shf, "%-20s %s%s%s", buf, p->command,
|
|
|
|
p->next ? "|" : null,
|
|
|
|
coredumped ? " (core dumped)" : null);
|
|
|
|
}
|
|
|
|
|
|
|
|
state = p->state;
|
|
|
|
status = p->status;
|
|
|
|
p = p->next;
|
|
|
|
while (p && p->state == state && p->status == status) {
|
|
|
|
if (how == JP_LONG)
|
2006-11-12 13:49:25 +01:00
|
|
|
shf_fprintf(shf, "%s%5d %-20s %s%s", filler,
|
2007-07-22 15:34:52 +02:00
|
|
|
(int)p->pid, " ", p->command,
|
2006-11-12 13:49:25 +01:00
|
|
|
p->next ? "|" : null);
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (how == JP_MEDIUM)
|
|
|
|
shf_fprintf(shf, " %s%s", p->command,
|
|
|
|
p->next ? "|" : null);
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (output)
|
2007-07-22 15:34:52 +02:00
|
|
|
shf_putc('\n', shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Convert % sequence to job
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* If jobs are compiled in then this routine expects sigchld to be blocked.
|
|
|
|
*/
|
|
|
|
static Job *
|
|
|
|
j_lookup(const char *cp, int *ecodep)
|
|
|
|
{
|
2011-08-27 20:06:52 +02:00
|
|
|
Job *j, *last_match;
|
|
|
|
Proc *p;
|
|
|
|
size_t len;
|
|
|
|
int job = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2006-11-10 08:52:04 +01:00
|
|
|
if (ksh_isdigit(*cp)) {
|
2006-11-10 05:03:59 +01:00
|
|
|
getn(cp, &job);
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Look for last_proc->pid (what $! returns) first... */
|
|
|
|
for (j = job_list; j != NULL; j = j->next)
|
|
|
|
if (j->last_proc && j->last_proc->pid == job)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (j);
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* ...then look for process group (this is non-POSIX,
|
|
|
|
* but should not break anything
|
|
|
|
*/
|
2005-05-23 05:06:10 +02:00
|
|
|
for (j = job_list; j != NULL; j = j->next)
|
|
|
|
if (j->pgrp && j->pgrp == job)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (j);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (ecodep)
|
|
|
|
*ecodep = JL_NOSUCH;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (*cp != '%') {
|
|
|
|
if (ecodep)
|
|
|
|
*ecodep = JL_INVALID;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
switch (*++cp) {
|
|
|
|
case '\0': /* non-standard */
|
|
|
|
case '+':
|
|
|
|
case '%':
|
|
|
|
if (job_list != NULL)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (job_list);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
if (job_list != NULL && job_list->next)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (job_list->next);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
2006-11-10 05:03:59 +01:00
|
|
|
getn(cp, &job);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (j = job_list; j != NULL; j = j->next)
|
|
|
|
if (j->job == job)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (j);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
/* %?string */
|
|
|
|
case '?':
|
2005-05-23 05:06:10 +02:00
|
|
|
last_match = NULL;
|
|
|
|
for (j = job_list; j != NULL; j = j->next)
|
|
|
|
for (p = j->proc_list; p != NULL; p = p->next)
|
|
|
|
if (strstr(p->command, cp+1) != NULL) {
|
|
|
|
if (last_match) {
|
|
|
|
if (ecodep)
|
|
|
|
*ecodep = JL_AMBIG;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
last_match = j;
|
|
|
|
}
|
|
|
|
if (last_match)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (last_match);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
2011-01-30 02:35:35 +01:00
|
|
|
/* %string */
|
|
|
|
default:
|
2005-05-23 05:06:10 +02:00
|
|
|
len = strlen(cp);
|
|
|
|
last_match = NULL;
|
|
|
|
for (j = job_list; j != NULL; j = j->next)
|
|
|
|
if (strncmp(cp, j->proc_list->command, len) == 0) {
|
|
|
|
if (last_match) {
|
|
|
|
if (ecodep)
|
|
|
|
*ecodep = JL_AMBIG;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
last_match = j;
|
|
|
|
}
|
|
|
|
if (last_match)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (last_match);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ecodep)
|
|
|
|
*ecodep = JL_NOSUCH;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Job *free_jobs;
|
|
|
|
static Proc *free_procs;
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* allocate a new job and fill in the job number.
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* If jobs are compiled in then this routine expects sigchld to be blocked.
|
|
|
|
*/
|
|
|
|
static Job *
|
|
|
|
new_job(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Job *newj, *j;
|
|
|
|
|
|
|
|
if (free_jobs != NULL) {
|
|
|
|
newj = free_jobs;
|
|
|
|
free_jobs = free_jobs->next;
|
|
|
|
} else
|
2009-06-08 22:06:50 +02:00
|
|
|
newj = alloc(sizeof(Job), APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* brute force method */
|
|
|
|
for (i = 1; ; i++) {
|
|
|
|
for (j = job_list; j && j->job != i; j = j->next)
|
|
|
|
;
|
|
|
|
if (j == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
newj->job = i;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (newj);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Allocate new process struct
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* If jobs are compiled in then this routine expects sigchld to be blocked.
|
|
|
|
*/
|
|
|
|
static Proc *
|
|
|
|
new_proc(void)
|
|
|
|
{
|
|
|
|
Proc *p;
|
|
|
|
|
|
|
|
if (free_procs != NULL) {
|
|
|
|
p = free_procs;
|
|
|
|
free_procs = free_procs->next;
|
|
|
|
} else
|
2009-06-08 22:06:50 +02:00
|
|
|
p = alloc(sizeof(Proc), APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (p);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* Take job out of job_list and put old structures into free list.
|
2005-05-23 05:06:10 +02:00
|
|
|
* Keeps nzombies, last_job and async_job up to date.
|
|
|
|
*
|
|
|
|
* If jobs are compiled in then this routine expects sigchld to be blocked.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
remove_job(Job *j, const char *where)
|
|
|
|
{
|
|
|
|
Proc *p, *tmp;
|
|
|
|
Job **prev, *curr;
|
|
|
|
|
|
|
|
prev = &job_list;
|
|
|
|
curr = *prev;
|
|
|
|
for (; curr != NULL && curr != j; prev = &curr->next, curr = *prev)
|
|
|
|
;
|
|
|
|
if (curr != j) {
|
2010-08-28 22:22:24 +02:00
|
|
|
internal_warningf("remove_job: job %s (%s)", "not found", where);
|
2005-05-23 05:06:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
*prev = curr->next;
|
|
|
|
|
|
|
|
/* free up proc structures */
|
|
|
|
for (p = j->proc_list; p != NULL; ) {
|
|
|
|
tmp = p;
|
|
|
|
p = p->next;
|
|
|
|
tmp->next = free_procs;
|
|
|
|
free_procs = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((j->flags & JF_ZOMBIE) && j->ppid == procpid)
|
|
|
|
--nzombie;
|
|
|
|
j->next = free_jobs;
|
|
|
|
free_jobs = j;
|
|
|
|
|
|
|
|
if (j == last_job)
|
|
|
|
last_job = NULL;
|
|
|
|
if (j == async_job)
|
|
|
|
async_job = NULL;
|
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* put j in a particular location (taking it out job_list if it is there
|
2005-05-23 05:06:10 +02:00
|
|
|
* already)
|
|
|
|
*
|
|
|
|
* If jobs are compiled in then this routine expects sigchld to be blocked.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
put_job(Job *j, int where)
|
|
|
|
{
|
|
|
|
Job **prev, *curr;
|
|
|
|
|
|
|
|
/* Remove job from list (if there) */
|
|
|
|
prev = &job_list;
|
|
|
|
curr = job_list;
|
|
|
|
for (; curr && curr != j; prev = &curr->next, curr = *prev)
|
|
|
|
;
|
|
|
|
if (curr == j)
|
|
|
|
*prev = curr->next;
|
|
|
|
|
|
|
|
switch (where) {
|
|
|
|
case PJ_ON_FRONT:
|
|
|
|
j->next = job_list;
|
|
|
|
job_list = j;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PJ_PAST_STOPPED:
|
|
|
|
prev = &job_list;
|
|
|
|
curr = job_list;
|
|
|
|
for (; curr && curr->state == PSTOPPED; prev = &curr->next,
|
|
|
|
curr = *prev)
|
|
|
|
;
|
|
|
|
j->next = curr;
|
|
|
|
*prev = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-04 15:36:43 +02:00
|
|
|
/*
|
|
|
|
* nuke a job (called when unable to start full job).
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* If jobs are compiled in then this routine expects sigchld to be blocked.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
kill_job(Job *j, int sig)
|
|
|
|
{
|
|
|
|
Proc *p;
|
|
|
|
int rval = 0;
|
|
|
|
|
|
|
|
for (p = j->proc_list; p != NULL; p = p->next)
|
|
|
|
if (p->pid != 0)
|
|
|
|
if (kill(p->pid, sig) < 0)
|
|
|
|
rval = -1;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (rval);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|