diff options
author | jilles <jilles@FreeBSD.org> | 2010-06-29 22:37:45 +0000 |
---|---|---|
committer | jilles <jilles@FreeBSD.org> | 2010-06-29 22:37:45 +0000 |
commit | 8fcbe1caf89944ec27f00e1265b29374fd3e05de (patch) | |
tree | fec6cbce2d09b1054b1fc671f8093cc73e9c8694 /bin | |
parent | 49f38732fcf51babc8f4ba652aa99ac2a78eeb4b (diff) | |
download | FreeBSD-src-8fcbe1caf89944ec27f00e1265b29374fd3e05de.zip FreeBSD-src-8fcbe1caf89944ec27f00e1265b29374fd3e05de.tar.gz |
sh: Forget about terminated background processes sooner.
Unless $! has been referenced for a particular job or $! still contains that
job's pid, forget about it after it has terminated. If $! has been
referenced, remember the job until the wait builtin has reported its
completion (either with the pid as parameter or without parameters).
In interactive mode, jobs are forgotten after termination has been reported,
which happens before primary prompts and through the jobs builtin. Even
then, though, remember a job if $! has been referenced.
This is similar to what is suggested by POSIX and should fix most memory
leaks (which also tend to cause sh to use more CPU time) with long running
scripts that start background jobs.
Caveats:
* Repeatedly referencing $! without ever doing 'wait', like
while :; do foo & echo started foo: $!; sleep 60; done
will still use a lot of memory and CPU time in the long run.
* The jobs and jobid builtins do not cause a job to be remembered for longer
like expanding $! does.
PR: bin/55346
Diffstat (limited to 'bin')
-rw-r--r-- | bin/sh/expand.c | 4 | ||||
-rw-r--r-- | bin/sh/jobs.c | 61 | ||||
-rw-r--r-- | bin/sh/jobs.h | 4 | ||||
-rw-r--r-- | bin/sh/sh.1 | 6 |
4 files changed, 66 insertions, 9 deletions
diff --git a/bin/sh/expand.c b/bin/sh/expand.c index 2b522ab..6c81224 100644 --- a/bin/sh/expand.c +++ b/bin/sh/expand.c @@ -818,7 +818,7 @@ varisset(char *name, int nulok) { if (*name == '!') - return backgndpid != -1; + return backgndpidset(); else if (*name == '@' || *name == '*') { if (*shellparam.p == NULL) return 0; @@ -891,7 +891,7 @@ varvalue(char *name, int quoted, int subtype, int flag) num = shellparam.nparam; goto numvar; case '!': - num = backgndpid; + num = backgndpidval(); numvar: expdest = cvtnum(num, expdest); break; diff --git a/bin/sh/jobs.c b/bin/sh/jobs.c index 29939d1..f55b637 100644 --- a/bin/sh/jobs.c +++ b/bin/sh/jobs.c @@ -75,6 +75,7 @@ __FBSDID("$FreeBSD$"); STATIC struct job *jobtab; /* array of jobs */ STATIC int njobs; /* size of array */ MKINIT pid_t backgndpid = -1; /* pid of last background process */ +MKINIT struct job *bgjob = NULL; /* last background process */ #if JOBS STATIC struct job *jobmru; /* most recently used job list */ STATIC pid_t initialpgrp; /* pgrp of shell on invocation */ @@ -183,6 +184,7 @@ INCLUDE <stdlib.h> SHELLPROC { backgndpid = -1; + bgjob = NULL; #if JOBS jobctl = 0; #endif @@ -413,7 +415,11 @@ showjobs(int change, int mode) continue; showjob(jp, 0, mode); jp->changed = 0; - if (jp->state == JOBDONE) { + /* 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); } } @@ -431,6 +437,8 @@ freejob(struct job *jp) int i; INTOFF; + if (bgjob == jp) + bgjob = NULL; for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { if (ps->cmd != nullstr) ckfree(ps->cmd); @@ -477,12 +485,27 @@ waitcmd(int argc, char **argv) #endif else retval = WTERMSIG(status) + 128; - if (! iflag) + if (! iflag || ! job->changed) freejob(job); + else { + job->remembered = 0; + if (job == bgjob) + bgjob = NULL; + } in_waitcmd--; return retval; } } else { + 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; + } + } for (jp = jobtab ; ; jp++) { if (jp >= jobtab + njobs) { /* no running procs */ in_waitcmd--; @@ -623,6 +646,8 @@ makejob(union node *node __unused, int nprocs) jp[i].next = &jp[jp[i].next - jobtab]; #endif + if (bgjob != NULL) + bgjob = &jp[bgjob - jobtab]; /* Relocate `ps' pointers */ for (i = 0; i < njobs; i++) if (jp[i].ps == &jobtab[i].ps0) @@ -644,6 +669,7 @@ makejob(union node *node __unused, int nprocs) jp->changed = 0; jp->nprocs = 0; jp->foreground = 0; + jp->remembered = 0; #if JOBS jp->jobctl = jobctl; jp->next = NULL; @@ -821,8 +847,13 @@ forkshell(struct job *jp, union node *n, int mode) pgrp = jp->ps[0].pid; setpgid(pid, pgrp); } - if (mode == FORK_BG) + if (mode == FORK_BG) { + if (bgjob != NULL && bgjob->state == JOBDONE && + !bgjob->remembered && !iflag) + freejob(bgjob); backgndpid = pid; /* set $! */ + bgjob = jp; + } if (jp) { struct procstat *ps = &jp->ps[jp->nprocs++]; ps->pid = pid; @@ -975,10 +1006,15 @@ dowait(int block, struct job *job) if (jp->state != state) { TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); jp->state = state; + if (jp != job) { + if (done && !jp->remembered && + !iflag && jp != bgjob) + freejob(jp); #if JOBS - if (done) - deljob(jp); + else if (done) + deljob(jp); #endif + } } } } @@ -1074,6 +1110,21 @@ checkzombies(void) } +int +backgndpidset(void) +{ + return backgndpid != -1; +} + + +pid_t +backgndpidval(void) +{ + if (bgjob != NULL) + bgjob->remembered = 1; + return backgndpid; +} + /* * Return a string identifying a command (to be printed by the * jobs command. diff --git a/bin/sh/jobs.h b/bin/sh/jobs.h index c75c353..8bc1082 100644 --- a/bin/sh/jobs.h +++ b/bin/sh/jobs.h @@ -68,6 +68,7 @@ struct job { char used; /* true if this entry is in used */ char changed; /* true if status has changed */ char foreground; /* true if running in the foreground */ + char remembered; /* true if $! referenced */ #if JOBS char jobctl; /* job running under job control */ struct job *next; /* job used after this one */ @@ -81,7 +82,6 @@ enum { SHOWJOBS_PGIDS /* PID of the group leader only */ }; -extern pid_t backgndpid; /* pid of last background process */ extern int job_warning; /* user was warned about stopped jobs */ extern int in_waitcmd; /* are we in waitcmd()? */ extern int in_dowait; /* are we in dowait()? */ @@ -98,6 +98,8 @@ struct job *makejob(union node *, int); pid_t forkshell(struct job *, union node *, int); int waitforjob(struct job *, int *); int stoppedjobs(void); +int backgndpidset(void); +pid_t backgndpidval(void); char *commandtext(union node *); #if ! JOBS diff --git a/bin/sh/sh.1 b/bin/sh/sh.1 index aed995f..1498564 100644 --- a/bin/sh/sh.1 +++ b/bin/sh/sh.1 @@ -32,7 +32,7 @@ .\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95 .\" $FreeBSD$ .\" -.Dd May 24, 2010 +.Dd June 29, 2010 .Dt SH 1 .Os .Sh NAME @@ -1106,6 +1106,10 @@ command executed from the current shell. For a pipeline, the process ID is that of the last command in the pipeline. +If this parameter is referenced, the shell will remember +the process ID and its exit status until the +.Ic wait +built-in command reports completion of the process. .It Li $0 (zero) Expands to the name of the shell or shell script. .El |