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: db hash file\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";
147 int scriptishash = 0;
151 if (getenv("ZPMDB")) {
152 db = getenv("ZPMDB");
154 /* ZPM_PACKAGE_FILE ? */
156 rootdir = getenv("ZPM_ROOT_DIR");
158 /* run, set, show, hash */
159 /* set -S, if -H set the hash, output hash, unless quiet
160 * show: -o, or stdout,
161 * All modes: [-f pkgfile] default to zpmdb
162 * All modes: [-p phase], default to configure
163 * All modes: [-R rootdir], chdir, attempt to chroot
164 * All modes: [-N], required, error if no such script
166 * run: zpm-script -r -p foo <pkgid args...>
167 * set: zpm-script -s <pkgid [script]>
168 * set: zpm-script -s -h <pkgid [hash]>
169 * show: zpm-script -l [-a] [-o outfile] <pkgid>
170 * show hash: zpm-script -lh <pkgid>
172 while ((opt = getopt(ac, av, "f:p:rslRFho:S:q")) != -1) {
174 case 'f': db = optarg; break;
175 case 'p': phase = optarg; break;
176 case 'r': mode = RUN; break;
177 case 's': mode = SET; break;
178 case 'l': mode = LIST; break;
179 case 'R': rootdir = optarg; break;
180 case 'F': required = 1; break;
181 case 'a': all = 1; break;
183 case 'h': scriptishash = 1; break;
184 case 'o': output = optarg; break;
185 case 'S': script = optarg; break;
201 if (!zpm_open(&zpm, db)) {
202 fprintf(stderr, "unable to open zpm database: %s\n", db);
204 fprintf(stderr, "error detail: %s\n", zpm.errmsg);
209 /* first non option arg is always a package id */
211 pkgid = zpm_findpkg(&zpm, pkgstr, NULL);
214 fprintf(stderr, "no package for %s\n", pkgstr);
234 setscript = av[argn];
239 rv = zpm_import(&zpm,setscript,0,hash);
241 fprintf(stderr, "unable to import %s\n",
249 if (!fail && !zpm_script_set(&zpm,pkgid,phase,sethash)) {
250 fprintf(stderr, "unable to set %s script hash %s\n",
252 sethash ? sethash : "null");
253 fprintf(stderr, "zpm error: %s\n", zpm.errmsg);
256 } else if (mode == LIST) {
258 zpm_foreach_script(&zpm, pkgid, phase, 0, list_scripts);
259 } else if (!zpm_script_hash(&zpm, pkgid, phase, hash)) {
261 } else if (scriptishash) {
262 printf("%s\n", hash);
267 if (!zpm_extract(&zpm, hash, output, 0700)) {
278 output = "/var/tmp/zpm-script.out";
282 script = "/var/tmp/zpm-script";
285 /* since the script file name doesn't really
286 * mean anything, pass in the phase as arg 0
288 /* TODO sanitize environment ? */
298 if (!zpm_script_hash(&zpm, pkgid, phase, hash)) {
303 if (!setdir(rootdir)) {
308 if (!zpm_extract(&zpm, hash, script, 0700)) {
309 fprintf(stderr, "unable to extract script");
314 rv = run(script, args, output, &status);
316 fprintf(stderr, "package %s script failed with code %d\n",
321 /* TODO log output */
334 return (fail == HARD || (required && fail)) ? EXIT_FAILURE : 0;