From 9271207c4eafe6e63fa14fdb02ee36f32d4b2a21 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 29 Nov 2018 08:49:07 +0000 Subject: ash: fixes to spawn_forkshell If spawn fails: - raise an error in spawn_forkshell, not at each call site; - free the job by calling freejob, not free. --- shell/ash.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/shell/ash.c b/shell/ash.c index 1b40e3407..29d93cdde 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -380,7 +380,7 @@ static void forkshell_init(const char *idstr); static void forkshell_child(struct forkshell *fs); static void sticky_free(void *p); #define free(p) sticky_free(p) -static int spawn_forkshell(struct job *jp, struct forkshell *fs, int mode); +static void spawn_forkshell(struct job *jp, struct forkshell *fs, int mode); #endif /* ============ Hash table sizes. Configurable. */ @@ -5700,8 +5700,7 @@ openhere(union node *redir) fs.n = redir; fs.fd[0] = pip[0]; fs.fd[1] = pip[1]; - if (spawn_forkshell(NULL, &fs, FORK_NOJOB) < 0) - ash_msg_and_raise_error("unable to spawn shell"); + spawn_forkshell(NULL, &fs, FORK_NOJOB); #else if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { /* child */ @@ -6796,8 +6795,7 @@ evalbackcmd(union node *n, struct backcmd *result) fs.n = n; fs.fd[0] = pip[0]; fs.fd[1] = pip[1]; - if (spawn_forkshell(jp, &fs, FORK_NOJOB) < 0) - ash_msg_and_raise_error("unable to spawn shell"); + spawn_forkshell(jp, &fs, FORK_NOJOB); #else if (forkshell(jp, n, FORK_NOJOB) == 0) { /* child */ @@ -9769,8 +9767,7 @@ evalsubshell(union node *n, int flags) fs.fpid = FS_EVALSUBSHELL; fs.n = n; fs.flags = flags; - if (spawn_forkshell(jp, &fs, backgnd) < 0) - ash_msg_and_raise_error("unable to spawn shell"); + spawn_forkshell(jp, &fs, backgnd); if ( 0 ) { #else if (forkshell(jp, n, backgnd) == 0) { @@ -9901,8 +9898,7 @@ evalpipe(union node *n, int flags) fs.fd[0] = pip[0]; fs.fd[1] = pip[1]; fs.fd[2] = prevfd; - if (spawn_forkshell(jp, &fs, n->npipe.pipe_backgnd) < 0) - ash_msg_and_raise_error("unable to spawn shell"); + spawn_forkshell(jp, &fs, n->npipe.pipe_backgnd); #else if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) { /* child */ @@ -10714,8 +10710,7 @@ evalcommand(union node *cmd, int flags) fs.fd[0] = cmdentry.u.index; fs.strlist = varlist.list; jp = makejob(/*cmd,*/ 1); - if (spawn_forkshell(jp, &fs, FORK_FG) < 0) - ash_msg_and_raise_error("unable to spawn shell"); + spawn_forkshell(jp, &fs, FORK_FG); status = waitforjob(jp); INT_ON; TRACE(("forked child exited with %d\n", exitstatus)); @@ -15105,8 +15100,7 @@ reinitvar(void) vlineno.var_text = linenovar; } -/* FIXME: should consider running forkparent() and forkchild() */ -static int +static void spawn_forkshell(struct job *jp, struct forkshell *fs, int mode) { struct forkshell *new; @@ -15121,11 +15115,11 @@ spawn_forkshell(struct job *jp, struct forkshell *fs, int mode) CloseHandle(new->hMapFile); UnmapViewOfFile(new); if (ret == -1) { - free(jp); - return -1; + if (jp) + freejob(jp); + ash_msg_and_raise_error("unable to spawn shell"); } forkparent(jp, fs->node, mode, (HANDLE)ret); - return ret == -1 ? -1 : 0; } /* -- cgit v1.2.3-55-g6feb