Skip to content

Commit

Permalink
Make compilation of spawnvex functions conditional
Browse files Browse the repository at this point in the history
spawnvex family of function were introduced after the last stable
release as an improvement over spawnveg. They were being compiled even
when _AST_no_spawnveg macro was defined. This was causing test cases to
fail if ksh is compiled with '-D_AST_no_spawnveg=1' flag. This commit
makes compilation of these functions conditional.

Related: #468
  • Loading branch information
siteshwar committed Apr 13, 2018
1 parent 566ec82 commit 71e6fda
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 12 deletions.
11 changes: 10 additions & 1 deletion src/cmd/ksh93/include/fault.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,19 @@ struct checkpt {
#endif
};

# if !_AST_no_spawnveg
#define sh_pushcontext(shp, bp, n) \
((bp)->mode = (n), (bp)->olist = 0, (bp)->topfd = shp->topfd, (bp)->prev = shp->jmplist, \
(bp)->vexi = ((Spawnvex_t *)shp->vexp)->cur, (bp)->err = *ERROR_CONTEXT_BASE, \
(bp)->vexi = ((Spawnvex_t *)shp->vexp)->cur, (bp)->err = *ERROR_CONTEXT_BASE, \
shp->jmplist = (sigjmp_buf *)(&(bp)->buff))

# else
#define sh_pushcontext(shp, bp, n) \
((bp)->mode = (n), (bp)->olist = 0, (bp)->topfd = shp->topfd, (bp)->prev = shp->jmplist, \
(bp)->err = *ERROR_CONTEXT_BASE, \
shp->jmplist = (sigjmp_buf *)(&(bp)->buff))
#endif

#define sh_popcontext(shp, bp) (shp->jmplist = (bp)->prev, errorpop(&((bp)->err)))

typedef void (*sh_sigfun_t)(int);
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/ksh93/include/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ extern int sh_iorenumber(Shell_t *, int, int);
extern void sh_pclose(int[]);
extern int sh_rpipe(int[]);
extern void sh_iorestore(Shell_t *, int, int);
#if !_AST_no_spawnveg
extern void sh_vexrestore(Shell_t *, int);
extern void sh_vexsave(Shell_t *, int, int, Spawnvex_f, void *);
#endif
#if defined(__EXPORT__) && defined(_BLD_DLL) && defined(_BLD_shell)
__EXPORT__
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/ksh93/include/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,10 @@ struct Shell_s {
char **argaddr;
void *optlist;
void **siginfo;
#if !_AST_no_spawnveg
Spawnvex_t *vex;
Spawnvex_t *vexp;
#endif
struct sh_scoped global;
struct checkpt checkbase;
Shinit_f userinit;
Expand Down
24 changes: 23 additions & 1 deletion src/cmd/ksh93/sh/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,11 @@ int sh_redirect(Shell_t *shp, struct ionod *iop, int flag) {
flag &= ~(IOHERESTRING | IOUSEVEX);
if (flag == 2) clexec = 1;
if (iop) traceon = sh_trace(shp, NULL, 0);

#ifdef SPAWN_cwd
if (vex) indx = vp->cur;
#endif

for (; iop; iop = iop->ionxt) {
iof = iop->iofile;
fn = (iof & IOUFD);
Expand Down Expand Up @@ -1105,8 +1109,10 @@ int sh_redirect(Shell_t *shp, struct ionod *iop, int flag) {
shp->sftable[fd] = sfnew(NULL, cp, r, -1, SF_READ | SF_STRING);
shp->fdstatus[fd] = shp->fdstatus[dupfd];
} else if (vex && toclose >= 0) {
#ifdef SPAWN_cwd
indx = spawnvex_add(vp, dupfd, -1, 0, 0);
spawnvex_add(vc, dupfd, -1, 0, 0);
#endif
fd = dupfd;
} else if ((fd = sh_fcntl(dupfd, F_DUPFD_CLOEXEC, 3)) < 0) {
goto fail;
Expand Down Expand Up @@ -1280,7 +1286,9 @@ int sh_redirect(Shell_t *shp, struct ionod *iop, int flag) {
if (fd < 0) {
if (vex) {
if (flag < 2) {
#ifdef SPAWN_cwd
sh_vexsave(shp, fn, (iof & IODOC) ? -1 : -2, 0, 0);
#endif
} else if (!(iof & IODOC)) {
sh_close(fn);
} else {
Expand Down Expand Up @@ -1338,7 +1346,9 @@ int sh_redirect(Shell_t *shp, struct ionod *iop, int flag) {
}
if (fx != fd) shp->fdstatus[fx] = status;
#endif
#ifdef SPAWN_cwd
if (fn <= 2) iovex_stdstream(shp, fn);
#endif
} else if (vex) {
#ifdef SPAWN_cwd
Spawnvex_f fun = 0;
Expand All @@ -1348,17 +1358,27 @@ int sh_redirect(Shell_t *shp, struct ionod *iop, int flag) {
fd = sh_fcntl(fn, F_DUPFD_CLOEXEC, fn);
close(fn);
}

#ifdef SPAWN_cwd
if (trunc)
fun = iovex_trunc;
else if (tname) {
else
#endif
if (tname) {
arg = malloc(sizeof(void *) + strlen(fname) + 1);
*(Shell_t **)arg = shp;
strcpy((char *)arg + sizeof(void *), fname);
#ifdef SPAWN_cwd
fun = iovex_rename;
#endif
} else if (shp->sftable[fn]) {
#ifdef SPAWN_cwd
fun = iovex_stream;
#endif
}
#ifdef SPAWN_cwd
sh_vexsave(shp, fn, fd, fun, arg);
#endif
} else {
fd = sh_iorenumber(shp, sh_iomovefd(shp, fd), fn);
if (fn > 2 && fn < 10) shp->inuse_bits |= (1 << fn);
Expand Down Expand Up @@ -1570,6 +1590,7 @@ void sh_iosave(Shell_t *shp, int origfd, int oldtop, char *name) {
}
}

#ifdef SPAWN_cwd
void sh_vexsave(Shell_t *shp, int fn, int fd, Spawnvex_f vexfun, void *arg) {
Spawnvex_t *vp = shp->vexp;
Spawnvex_t *vc = shp->vex;
Expand Down Expand Up @@ -1606,6 +1627,7 @@ void sh_vexsave(Shell_t *shp, int fn, int fd, Spawnvex_f vexfun, void *arg) {
iovex_stdstream(shp, fn);
}
}
#endif

// Return the lowest numbered fd that is equal to or greater than the requested
// `min_fd` and which is not currently in use.
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/ksh93/sh/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -2383,8 +2383,10 @@ static char *sh_tilde(Shell_t *shp, const char *string) {
char *s2;
size_t len;
int fd, offset = stktell(shp->stk);
#ifdef SPAWN_cwd
Spawnvex_t *vc = (Spawnvex_t *)shp->vex;
if (!vc && (vc = spawnvex_open(0))) shp->vex = (void *)vc;
#endif
if (!(s2 = strchr(string++, '}'))) return NULL;
len = s2 - string;
sfwrite(shp->stk, string, len + 1);
Expand All @@ -2410,7 +2412,9 @@ static char *sh_tilde(Shell_t *shp, const char *string) {
sfprintf(shp->stk, "/dev/fd/%d", fd);
#endif
#endif
#ifdef SPAWN_cwd
if (vc) spawnvex_add(vc, fd, fd, 0, 0);
#endif
return stkfreeze(shp->stk, 1);
}
#if _WINIX
Expand Down
10 changes: 8 additions & 2 deletions src/cmd/ksh93/sh/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "test.h"
#include "variables.h"

#include <assert.h>

#if SHOPT_DYNAMIC
#include <dlldefs.h>
#endif
Expand Down Expand Up @@ -70,14 +72,17 @@ static bool onstdpath(Shell_t *shp, const char *name) {

static pid_t path_pfexecve(Shell_t *shp, const char *path, char *argv[], char *const envp[],
int spawn) {

#ifdef SPAWN_cwd
if (shp->vex->cur) {
spawnvex_apply(shp->vex, 0, 0);
spawnvex_apply(shp->vexp, 0, SPAWN_RESET);
}

#endif
return execve(path, argv, envp);
}


static pid_t _spawnveg(Shell_t *shp, const char *path, char *const argv[], char *const envp[],
pid_t pgid) {
pid_t pid;
Expand All @@ -101,7 +106,8 @@ static pid_t _spawnveg(Shell_t *shp, const char *path, char *const argv[], char
}
}
#else
pid = spawnveg(path, argv, envp, pgid);
assert(0); // We should never reach here
// pid = spawnveg(path, argv, envp, pgid);
#endif // SPAWN_cwd
if (pid >= 0 || errno != EAGAIN) break;
}
Expand Down
16 changes: 16 additions & 0 deletions src/cmd/ksh93/sh/xec.c
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,9 @@ int sh_exec(Shell_t *shp, const Shnode_t *t, int flags) {
type &= (COMMSK | COMSCAN);
sh_stats(STAT_SCMDS);
error_info.line = t->com.comline - shp->st.firstline;
#ifdef SPAWN_cwd
spawnvex_add(shp->vex, SPAWN_frame, 0, 0, 0);
#endif
com = sh_argbuild(shp, &argn, &(t->com), OPTIMIZE);
procsub = shp->procsub;
shp->procsub = 0;
Expand Down Expand Up @@ -1252,6 +1254,7 @@ int sh_exec(Shell_t *shp, const Shnode_t *t, int flags) {
jmpval = 0;
}
}
#ifdef SPAWN_cwd
if (np != SYSEXEC && shp->vex->cur) {
#if 1
spawnvex_apply(shp->vex, 0, SPAWN_RESET | SPAWN_FRAME);
Expand All @@ -1262,6 +1265,7 @@ int sh_exec(Shell_t *shp, const Shnode_t *t, int flags) {
spawnvex_add(shp->vex, fd, 1, 0, 0);
#endif
}
#endif
bp->bnode = NULL;
if (bp->ptr != nv_context(np)) np->nvfun = (Namfun_t *)bp->ptr;
if (execflg && !was_nofork) sh_offstate(shp, SH_NOFORK);
Expand Down Expand Up @@ -1312,7 +1316,9 @@ int sh_exec(Shell_t *shp, const Shnode_t *t, int flags) {
if ((shp->topfd > topfd) && !(shp->subshell && np == SYSEXEC)) {
sh_iorestore(shp, topfd, jmpval);
}
#ifdef SPAWN_cwd
if (shp->vexp->cur > vexi) sh_vexrestore(shp, vexi);
#endif
shp->redir0 = 0;
if (jmpval) siglongjmp(*shp->jmplist, jmpval);
#if 0
Expand Down Expand Up @@ -1395,8 +1401,10 @@ int sh_exec(Shell_t *shp, const Shnode_t *t, int flags) {
sh_funct(shp, np, argn, com, t->com.comset, (flags & ~OPTIMIZE_FLAG));
}
enter_namespace(shp, namespace);
#ifdef SPAWN_cwd
spawnvex_apply(shp->vex, 0, SPAWN_RESET | SPAWN_FRAME);
if (shp->vexp->cur > vexi) sh_vexrestore(shp, vexi);
#endif
if (io) {
if (buffp->olist) free_list(buffp->olist);
sh_popcontext(shp, buffp);
Expand All @@ -1411,7 +1419,9 @@ int sh_exec(Shell_t *shp, const Shnode_t *t, int flags) {
goto setexit;
}
} else if (!io) {
#ifdef SPAWN_cwd
spawnvex_apply(shp->vex, 0, SPAWN_RESET | SPAWN_FRAME);
#endif
setexit:
exitset(shp);
break;
Expand Down Expand Up @@ -1647,7 +1657,9 @@ int sh_exec(Shell_t *shp, const Shnode_t *t, int flags) {
job_post(shp, parent, 0);
job_wait(parent);
sh_iorestore(shp, topfd, SH_JMPCMD);
#ifdef SPAWN_cwd
if (shp->vexp->cur > vexi) sh_vexrestore(shp, vexi);
#endif
sh_done(shp,
(shp->exitval & SH_EXITSIG) ? (shp->exitval & SH_EXITMASK) : 0);
}
Expand Down Expand Up @@ -1724,7 +1736,9 @@ int sh_exec(Shell_t *shp, const Shnode_t *t, int flags) {
}
sh_popcontext(shp, buffp);
sh_iorestore(shp, buffp->topfd, jmpval);
#ifdef SPAWN_cwd
if (shp->vexp->cur > vexi) sh_vexrestore(shp, buffp->vexi);
#endif
if (buffp->olist) free_list(buffp->olist);
if (type & FPIN) {
job.waitall = waitall;
Expand Down Expand Up @@ -3216,7 +3230,9 @@ static pid_t sh_ntfork(Shell_t *shp, const Shnode_t *t, char *argv[], int *jobid
}
if (t->fork.forkio || otype) {
sh_iorestore(shp, buffp->topfd, jmpval);
#ifdef SPAWN_cwd
if (shp->vexp->cur > buffp->vexi) sh_vexrestore(shp, buffp->vexi);
#endif
}
if (optimize == 0) {
if (spawnpid > 0) _sh_fork(shp, spawnpid, otype, jobid);
Expand Down
18 changes: 11 additions & 7 deletions src/lib/libast/include/ast_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <sys/types.h>
#include <unistd.h>

#if !_AST_no_spawnveg
typedef struct Spawnvex_s {
unsigned int cur;
int io;
Expand Down Expand Up @@ -95,6 +96,16 @@ typedef int (*Spawnvex_f)(void *, uintmax_t, uintmax_t);
#define SPAWN_truncate (-10)
#define SPAWN_umask (-11)

extern pid_t spawnveg(const char *, char *const[], char *const[], pid_t);
extern pid_t spawnvex(const char *, char *const[], char *const[], Spawnvex_t *);
extern Spawnvex_t *spawnvex_open(unsigned int);
extern int spawnvex_add(Spawnvex_t *, intmax_t, intmax_t, Spawnvex_f, void *);
extern int spawnvex_apply(Spawnvex_t *, int, int);
extern intmax_t spawnvex_get(Spawnvex_t *, int, int);
extern int spawnvex_close(Spawnvex_t *);

#endif

#if _BLD_ast && defined(__EXPORT__)
#define extern __EXPORT__
#endif
Expand All @@ -105,13 +116,6 @@ extern char *gettxt(const char *, const char *);
extern void *memalign(size_t, size_t);
extern void *pvalloc(size_t);
extern char *resolvepath(const char *, char *, size_t);
extern pid_t spawnveg(const char *, char *const[], char *const[], pid_t);
extern pid_t spawnvex(const char *, char *const[], char *const[], Spawnvex_t *);
extern Spawnvex_t *spawnvex_open(unsigned int);
extern int spawnvex_add(Spawnvex_t *, intmax_t, intmax_t, Spawnvex_f, void *);
extern int spawnvex_apply(Spawnvex_t *, int, int);
extern intmax_t spawnvex_get(Spawnvex_t *, int, int);
extern int spawnvex_close(Spawnvex_t *);
extern size_t strlcat(char *, const char *, size_t);
extern size_t strlcpy(char *, const char *, size_t);
extern void swab(const void *, void *, ssize_t);
Expand Down
6 changes: 5 additions & 1 deletion src/lib/libast/misc/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ libast_files += [
'misc/optget.c', 'misc/optjoin.c', 'misc/procclose.c', 'misc/procfree.c',
'misc/procopen.c', 'misc/procrun.c', 'misc/recfmt.c', 'misc/reclen.c',
'misc/recstr.c', 'misc/setenviron.c', 'misc/sigcrit.c', 'misc/sigdata.c',
'misc/signal.c', 'misc/spawnvex.c', 'misc/stack.c', 'misc/state.c',
'misc/signal.c', 'misc/stack.c', 'misc/state.c',
'misc/stk.c', 'misc/systrace.c', 'misc/translate.c', 'misc/univdata.c'
]

if not shared_c_args.contains('-D_AST_no_spawnveg=1')
libast_files += [ 'misc/spawnvex.c' ]
endif

0 comments on commit 71e6fda

Please # to comment.