From: Nathan Wagner Date: Fri, 21 Sep 2018 22:26:26 +0000 (+0000) Subject: add -o option to runscript X-Git-Tag: v0.1.6~9 X-Git-Url: https://pd.if.org/git/?p=zpackage;a=commitdiff_plain;h=b547a6673cbb8bf2685fa0c03e143f14d3c803ed add -o option to runscript honor ZPM_ROOT_DIR environment variable Change directory to either / or the rootdir. Actually doing a chroot(2) call is apparently deprecated and has been removed from posix. Doing this will require OS specific code. Unless and until that is done, scripts will need to be aware of that and make changes in the correct place. --- diff --git a/t/scripts.t b/t/scripts.t index 47ac6f3..8328426 100755 --- a/t/scripts.t +++ b/t/scripts.t @@ -4,11 +4,15 @@ . tap.sh -plan 8 +plan 16 PF=test.db pkgid=scriptrunner-1.0-1 +require rm -rf tmp +require mkdir tmp +cd tmp 2>/dev/null || bailout "can't cd to tmp" + require zpm init $PF require zpm newpackage -f $PF $pkgid @@ -22,6 +26,27 @@ zpm runscript -f $PF -p nosuchphase -R scriptrunner-1.0-1 2>/dev/null failsok required non-existing script fails tryrun zpm runscript -f $PF -p nosuchphase scriptrunner-1.0-1 2>/dev/null +cat >script <<-'EOC' +#!/bin/sh +echo $1 +EOC +tryrun zpm setscript -f $PF scriptrunner-1.0-1 configure script +first=$(zpm runscript -f $PF -p configure -o - scriptrunner-1.0-1) +okexit first arg script +okstreq "$first" "scriptrunner-1.0-1" "first arg value" + +cat >script <<-'EOC' +#!/bin/sh +echo $2 +EOC +tryrun zpm setscript -f $PF scriptrunner-1.0-1 configure script +second=$(zpm runscript -f $PF -p configure -o - scriptrunner-1.0-1 secondarg) +okexit second arg script +okstreq "$second" "secondarg" "second arg value" + +cd .. +rm -rf tmp + finish rm -f $PF diff --git a/zpm-runscript.c b/zpm-runscript.c index 84764b6..db337be 100644 --- a/zpm-runscript.c +++ b/zpm-runscript.c @@ -2,13 +2,14 @@ #include #include +#include #include #include #include #include #include #include - +#include #include @@ -32,7 +33,7 @@ int run(char *program, char **args, char *output, int *status) { /* if stdout is a terminal, leave stdout where it goes, * if it's not a terminal, redirect stdout to output. * in either case, send stderr to output, unless null - * if output is null, just run the program + * if output is null or "-", just run the program */ int interactive = 0; pid_t pid; @@ -51,7 +52,7 @@ int run(char *program, char **args, char *output, int *status) { if (pid == 0) { /* child */ - if (output) { + if (output && strcmp(output, "-") != 0) { close(2); rv = open(output, O_NOFOLLOW|O_TRUNC|O_CREAT|O_WRONLY, 0600); @@ -97,10 +98,10 @@ int main(int ac, char **av){ int required = 0; char hash[ZPM_HASH_STRLEN+1]; - char *args[3]; + char *args[4]; char *pkgid; - char *chroot = 0; + char *rootdir = 0; char *db = "/var/lib/zpm/zpm.db"; char *script = "/var/tmp/zpm-script"; char *output = "/var/tmp/zpm-script.out"; @@ -111,12 +112,15 @@ int main(int ac, char **av){ } /* ZPM_PACKAGE_FILE ? */ - while ((opt = getopt(ac, av, "f:p:s:r:R")) != -1) { + rootdir = getenv("ZPM_ROOT_DIR"); + + while ((opt = getopt(ac, av, "f:p:o:s:r:R")) != -1) { switch (opt) { case 'f': db = optarg; break; case 'p': phase = optarg; break; case 's': script = optarg; break; - case 'r': chroot = optarg; break; + case 'o': output = optarg; break; + case 'r': rootdir = optarg; break; case 'R': required = 1; break; default: usage(); @@ -151,8 +155,29 @@ int main(int ac, char **av){ args[0] = script; args[1] = pkgid; args[2] = 0; - if (chroot) { - fprintf(stderr, "failing to chroot %s\n", chroot); + args[3] = 0; + if (argn + 1 <= ac) { + args[2] = av[argn+1]; + } + if (rootdir) { + if (chdir(rootdir) == -1) { + perror("can not chdir to rootdir"); + exit(EXIT_FAILURE); + } + if (geteuid() == 0) { + /* chroot is deprecated, and not in + * posix. need to use OS/kernel + * specific code. + */ + fprintf(stderr, "support for chroot equivalent not supported on this OS\n"); + } else { + fprintf(stderr, "unable to chroot as non root use\n"); + } + } else { + if (chdir("/") == -1) { + perror("can not chdir to /"); + exit(EXIT_FAILURE); + } } rv = run(script, args, output, &status); if (rv) {