1 #define _POSIX_C_SOURCE 200809L
19 * -f package file, otherwise localdb
20 * -p phase, defaults to 'configure'
21 * -s script name, defaults to /var/tmp/zpm-script
22 * -r chroot to directory
23 * -o script output, /var/tmp/zpm-script.out, '-' for stdout
29 fprintf(stderr, "usage: zpm script ...\n");
32 int setdir(char *rootdir) {
33 if (rootdir && strcmp(rootdir, "/")) {
34 if (chdir(rootdir) == -1) {
35 perror("can not chdir to rootdir");
39 /* chroot is deprecated, and not in posix. need to use
40 * OS/kernel specific code.
42 fprintf(stderr, "support for chroot equivalent not supported on this OS\n");
44 fprintf(stderr, "unable to chroot as non root user\n");
47 if (chdir("/") == -1) {
48 perror("can not chdir to /");
56 int run(char *program, char **args, char *output, int *status) {
57 /* if stdout is a terminal, leave stdout where it goes,
58 * if it's not a terminal, redirect stdout to output.
59 * in either case, send stderr to output, unless null
60 * if output is null or "-", just run the program
68 interactive = isatty(1);
79 if (output && strcmp(output, "-") != 0) {
81 rv = open(output, O_NOFOLLOW|O_TRUNC|O_CREAT|O_WRONLY,
84 perror("cannot open output file");
89 perror("unable to redirect stdout");
95 rv = execvp(program, args);
97 perror("cannot exec");
104 perror("error waiting for child");
108 if (WIFEXITED(*status)) {
109 return WEXITSTATUS(*status);
122 static int list_scripts(void *ud, const char *pkg, const char *stage,
124 printf("%s %s %.8s\n", pkg, stage, hash);
128 int main(int ac, char **av){
137 char hash[ZPM_HASH_STRLEN+1];
142 char *db = "/var/lib/zpm/local.db";
143 char *script = "/var/tmp/zpm-script";
144 char *output = "/var/tmp/zpm-script.out";
148 int scriptishash = 0;
151 if (getenv("ZPMDB")) {
152 db = getenv("ZPMDB");
154 /* ZPM_PACKAGE_FILE ? */
156 rootdir = getenv("ZPM_ROOT_DIR");
158 envvar = getenv("ZPM_SCRIPT_FILE");
163 envvar = getenv("ZPM_SCRIPT_OUTPUT");
168 /* run, set, show, hash */
169 /* set -S, if -H set the hash, output hash, unless quiet
170 * show: -o, or stdout,
171 * All modes: [-f pkgfile] default to zpmdb
172 * All modes: [-p phase], default to configure
173 * All modes: [-R rootdir], chdir, attempt to chroot
174 * All modes: [-N], required, error if no such script
176 * run: zpm-script -r -p foo <pkgid args...>
177 * set: zpm-script -s <pkgid [script]>
178 * set: zpm-script -s -h <pkgid [hash]>
179 * show: zpm-script -l [-a] [-o outfile] <pkgid>
180 * show hash: zpm-script -lh <pkgid>
182 while ((opt = getopt(ac, av, "f:p:rslRFho:S:q")) != -1) {
184 case 'f': db = optarg; break;
185 case 'p': phase = optarg; break;
186 case 'r': mode = RUN; break;
187 case 's': mode = SET; break;
188 case 'l': mode = LIST; break;
189 case 'R': rootdir = optarg; break;
190 case 'F': required = 1; break;
192 case 'h': scriptishash = 1; break;
193 case 'o': output = optarg; break;
194 case 'S': script = optarg; break;
195 case 'q': quiet = 1; break;
210 if (!zpm_open(&zpm, db)) {
211 fprintf(stderr, "unable to open zpm database: %s\n", db);
213 fprintf(stderr, "error detail: %s\n", zpm.errmsg);
218 /* first non option arg is always a package id */
220 pkgid = zpm_findpkg(&zpm, pkgstr, NULL);
223 fprintf(stderr, "no package for %s\n", pkgstr);
243 setscript = av[argn];
248 rv = zpm_import(&zpm,setscript,0,hash);
250 fprintf(stderr, "unable to import %s\n",
258 if (!fail && !zpm_script_set(&zpm,pkgid,phase,sethash)) {
259 fprintf(stderr, "unable to set %s script hash %s\n",
261 sethash ? sethash : "null");
262 fprintf(stderr, "zpm error: %s\n", zpm.errmsg);
265 } else if (mode == LIST) {
267 zpm_foreach_script(&zpm, pkgid, phase, 0, list_scripts);
268 } else if (!zpm_script_hash(&zpm, pkgid, phase, hash)) {
270 } else if (scriptishash) {
272 printf("%s\n", hash);
278 if (!zpm_extract(&zpm, hash, output, 0700)) {
289 output = "/var/tmp/zpm-script.out";
293 script = "/var/tmp/zpm-script";
296 /* since the script file name doesn't really
297 * mean anything, pass in the phase as arg 0
299 /* TODO sanitize environment ? */
309 if (!zpm_script_hash(&zpm, pkgid, phase, hash)) {
314 if (!setdir(rootdir)) {
319 if (!zpm_extract(&zpm, hash, script, 0700)) {
320 fprintf(stderr, "unable to extract script");
325 rv = run(script, args, output, &status);
327 fprintf(stderr, "package %s script failed with code %d\n",
332 /* TODO log output */
345 return (fail == HARD || (required && fail)) ? EXIT_FAILURE : 0;