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 (chdir(rootdir ? rootdir : "/") == -1) {
34 perror("can not chdir to rootdir");
38 if (rootdir && strcmp(rootdir, "/")) {
40 /* chroot is deprecated, and not in posix. need to use
41 * OS/kernel specific code.
43 fprintf(stderr, "support for chroot equivalent not supported on this OS\n");
45 fprintf(stderr, "unable to chroot as non root user\n");
53 int run(char *program, char **args, char *output, int *status) {
54 /* if stdout is a terminal, leave stdout where it goes,
55 * if it's not a terminal, redirect stdout to output.
56 * in either case, send stderr to output, unless null
57 * if output is null or "-", just run the program
65 interactive = isatty(1);
76 if (output && strcmp(output, "-") != 0) {
78 rv = open(output, O_NOFOLLOW|O_TRUNC|O_CREAT|O_WRONLY,
81 perror("cannot open output file");
86 perror("unable to redirect stdout");
92 rv = execvp(program, args);
94 perror("cannot exec");
101 perror("error waiting for child");
105 if (WIFEXITED(*status)) {
106 return WEXITSTATUS(*status);
119 static int list_scripts(void *ud, const char *pkg, const char *stage,
121 printf("%s %s %.8s\n", pkg, stage, hash);
125 int main(int ac, char **av){
134 char hash[ZPM_HASH_STRLEN+1];
139 char *db = "/var/lib/zpm/local.db";
140 char *script = "/var/tmp/zpm-script";
141 char *output = "/var/tmp/zpm-script.out";
145 int scriptishash = 0;
148 if (getenv("ZPMDB")) {
149 db = getenv("ZPMDB");
151 /* ZPM_PACKAGE_FILE ? */
153 rootdir = getenv("ZPM_ROOT_DIR");
155 envvar = getenv("ZPM_SCRIPT_FILE");
160 envvar = getenv("ZPM_SCRIPT_OUTPUT");
165 /* run, set, show, hash */
166 /* set -S, if -H set the hash, output hash, unless quiet
167 * show: -o, or stdout,
168 * All modes: [-f pkgfile] default to zpmdb
169 * All modes: [-p phase], default to configure
170 * All modes: [-R rootdir], chdir, attempt to chroot
171 * All modes: [-N], required, error if no such script
173 * run: zpm-script -r -p foo <pkgid args...>
174 * set: zpm-script -s <pkgid [script]>
175 * set: zpm-script -s -h <pkgid [hash]>
176 * show: zpm-script -l [-a] [-o outfile] <pkgid>
177 * show hash: zpm-script -lh <pkgid>
179 while ((opt = getopt(ac, av, "f:p:rslRFho:S:q")) != -1) {
181 case 'f': db = optarg; break;
182 case 'p': phase = optarg; break;
183 case 'r': mode = RUN; break;
184 case 's': mode = SET; break;
185 case 'l': mode = LIST; break;
186 case 'R': rootdir = optarg; break;
187 case 'F': required = 1; break;
189 case 'h': scriptishash = 1; break;
190 case 'o': output = optarg; break;
191 case 'S': script = optarg; break;
192 case 'q': quiet = 1; break;
207 if (!zpm_open(&zpm, db)) {
208 fprintf(stderr, "unable to open zpm database: %s\n", db);
210 fprintf(stderr, "error detail: %s\n", zpm.errmsg);
215 /* first non option arg is always a package id */
217 pkgid = zpm_findpkg(&zpm, pkgstr, NULL);
220 fprintf(stderr, "no package for %s\n", pkgstr);
240 setscript = av[argn];
245 rv = zpm_import(&zpm,setscript,0,hash);
247 fprintf(stderr, "unable to import %s\n",
255 if (!fail && !zpm_script_set(&zpm,pkgid,phase,sethash)) {
256 fprintf(stderr, "unable to set %s script hash %s\n",
258 sethash ? sethash : "null");
259 fprintf(stderr, "zpm error: %s\n", zpm.errmsg);
262 } else if (mode == LIST) {
264 zpm_foreach_script(&zpm, pkgid, phase, 0, list_scripts);
265 } else if (!zpm_script_hash(&zpm, pkgid, phase, hash)) {
267 } else if (scriptishash) {
269 printf("%s\n", hash);
275 if (!zpm_extract(&zpm, hash, output, 0700)) {
286 output = "/var/tmp/zpm-script.out";
290 script = "/var/tmp/zpm-script";
293 /* since the script file name doesn't really
294 * mean anything, pass in the phase as arg 0
296 /* TODO sanitize environment ? */
306 if (!zpm_script_hash(&zpm, pkgid, phase, hash)) {
311 if (!setdir(rootdir)) {
316 if (!zpm_extract(&zpm, hash, script, 0700)) {
317 fprintf(stderr, "unable to extract script");
319 zpm_note_add(&zpm, pkgid, NULL, hash, "unable to extract %s script", phase);
323 rv = run(script, args, output, &status);
325 fprintf(stderr, "package %s script failed with code %d\n",
328 zpm_import(&zpm, output, 0, hash);
329 zpm_note_add(&zpm, pkgid, NULL, hash, "package %s script failed with code %d", phase, rv);
334 /* TODO log output */
347 return (fail == HARD || (required && fail)) ? EXIT_FAILURE : 0;