1 #define _POSIX_C_SOURCE 200809L
18 * -f package file, otherwise localdb
19 * -p phase, defaults to 'configure'
20 * -s script name, defaults to /var/tmp/zpm-script
21 * -r chroot to directory
22 * -o script output, /var/tmp/zpm-script.out, '-' for stdout
28 fprintf(stderr, "usage: db hash file\n");
31 int run(char *program, char **args, char *output, int *status) {
32 /* if stdout is a terminal, leave stdout where it goes,
33 * if it's not a terminal, redirect stdout to output.
34 * in either case, send stderr to output, unless null
35 * if output is null, just run the program
43 interactive = isatty(1);
56 rv = open(output, O_NOFOLLOW|O_TRUNC|O_CREAT|O_WRONLY,
59 perror("cannot open output file");
64 perror("unable to redirect stdout");
70 rv = execvp(program, args);
72 perror("cannot exec");
79 perror("error waiting for child");
83 if (WIFEXITED(*status)) {
84 return WEXITSTATUS(*status);
90 int main(int ac, char **av){
98 char hash[ZPM_HASH_STRLEN+1];
103 char *db = "/var/lib/zpm/zpm.db";
104 char *script = "/var/tmp/zpm-script";
105 char *output = "/var/tmp/zpm-script.out";
106 char *phase = "configure";
108 if (getenv("ZPMDB")) {
109 db = getenv("ZPMDB");
111 /* ZPM_PACKAGE_FILE ? */
113 while ((opt = getopt(ac, av, "f:p:s:r:")) != -1) {
115 case 'f': db = optarg;
117 case 'p': phase = optarg;
119 case 's': script = optarg;
121 case 'r': chroot = optarg;
138 if (!zpm_open(&zpm, db)) {
139 fprintf(stderr, "unable to open zpm database: %s\n", db);
141 fprintf(stderr, "error detail: %s\n", zpm.errmsg);
146 pkgid = zpm_findpkg(&zpm, pkgstr);
148 if (zpm_script_hash(&zpm, pkgid, phase, hash)) {
149 rv = zpm_extract(&zpm, hash, script, 0700);
151 /* perhaps also pass in the phase name? or ENV? */
152 /* TODO sanitize environment ? */
157 fprintf(stderr, "failing to chroot %s\n", chroot);
159 rv = run(script, args, output, &status);
162 fprintf(stderr, "package %s script failed with code %d\n",
165 /* TODO log output */
172 fprintf(stderr, "no script for %s %s\n", phase, pkgid);
177 fprintf(stderr, "unable to find package for %s in %s\n",
184 return failures ? EXIT_FAILURE : EXIT_SUCCESS;