perform.c revision 173513
19313Ssos/*
29313Ssos * FreeBSD install - a package for the installation and maintainance
39313Ssos * of non-core utilities.
49313Ssos *
59313Ssos * Redistribution and use in source and binary forms, with or without
69313Ssos * modification, are permitted provided that the following conditions
79313Ssos * are met:
89313Ssos * 1. Redistributions of source code must retain the above copyright
99313Ssos *    notice, this list of conditions and the following disclaimer.
109313Ssos * 2. Redistributions in binary form must reproduce the above copyright
119313Ssos *    notice, this list of conditions and the following disclaimer in the
129313Ssos *    documentation and/or other materials provided with the distribution.
139313Ssos *
149313Ssos * Jordan K. Hubbard
159313Ssos * 18 July 1993
169313Ssos *
179313Ssos * This is the main body of the add module.
189313Ssos *
199313Ssos */
209313Ssos
219313Ssos#include <sys/cdefs.h>
229313Ssos__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/add/perform.c 173513 2007-11-10 09:40:39Z krion $");
239313Ssos
249313Ssos#include <err.h>
259313Ssos#include <paths.h>
269313Ssos#include "lib.h"
279313Ssos#include "add.h"
2814331Speter
299313Ssos#include <libgen.h>
309313Ssos#include <signal.h>
319313Ssos#include <sys/wait.h>
3212458Sbde
339313Ssosstatic int pkg_do(char *);
349313Ssosstatic int sanity_check(char *);
359313Ssosstatic char LogDir[FILENAME_MAX];
369313Ssosstatic int zapLogDir;		/* Should we delete LogDir? */
3714331Speter
3814331Speterint
3912458Sbdepkg_perform(char **pkgs)
409313Ssos{
4114331Speter    int i, err_cnt = 0;
429313Ssos
439313Ssos    signal(SIGINT, cleanup);
449313Ssos    signal(SIGHUP, cleanup);
459313Ssos
469313Ssos    if (AddMode == SLAVE)
479313Ssos	err_cnt = pkg_do(NULL);
4814331Speter    else {
499313Ssos	for (i = 0; pkgs[i]; i++)
509313Ssos	    err_cnt += pkg_do(pkgs[i]);
519313Ssos    }
529313Ssos    return err_cnt;
539313Ssos}
549313Ssos
5514331Speterstatic Package Plist;
569313Ssosstatic char *Home;
579313Ssos
589313Ssos/*
599313Ssos * This is seriously ugly code following.  Written very fast!
609313Ssos * [And subsequently made even worse..  Sigh!  This code was just born
619313Ssos * to be hacked, I guess.. :) -jkh]
6214331Speter */
639313Ssosstatic int
649313Ssospkg_do(char *pkg)
659313Ssos{
669313Ssos    char pkg_fullname[FILENAME_MAX];
679313Ssos    char playpen[FILENAME_MAX];
689313Ssos    char extract_contents[FILENAME_MAX];
6914331Speter    char *where_to, *extract;
709313Ssos    FILE *cfile;
719313Ssos    int code;
729313Ssos    PackingList p;
739313Ssos    struct stat sb;
749313Ssos    int inPlace, conflictsfound, errcode;
759313Ssos    /* support for separate pre/post install scripts */
7614331Speter    int new_m = 0;
779313Ssos    char pre_script[FILENAME_MAX] = INSTALL_FNAME;
789313Ssos    char post_script[FILENAME_MAX];
799313Ssos    char pre_arg[FILENAME_MAX], post_arg[FILENAME_MAX];
809313Ssos    char *conflict[2];
819313Ssos    char **matched;
829313Ssos
8314331Speter    conflictsfound = 0;
849313Ssos    code = 0;
859313Ssos    zapLogDir = 0;
869313Ssos    LogDir[0] = '\0';
879313Ssos    strcpy(playpen, FirstPen);
889313Ssos    inPlace = 0;
899313Ssos
9014331Speter    /* Are we coming in for a second pass, everything already extracted? */
919313Ssos    if (!pkg) {
929313Ssos	fgets(playpen, FILENAME_MAX, stdin);
939313Ssos	playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */
949313Ssos	if (chdir(playpen) == FAIL) {
959313Ssos	    warnx("pkg_add in SLAVE mode can't chdir to %s", playpen);
969313Ssos	    return 1;
9714331Speter	}
989313Ssos	read_plist(&Plist, stdin);
999313Ssos	where_to = playpen;
1009313Ssos    }
1019313Ssos    /* Nope - do it now */
1029313Ssos    else {
1039313Ssos	/* Is it an ftp://foo.bar.baz/file.t[bg]z specification? */
10414331Speter	if (isURL(pkg)) {
1059313Ssos	    if (!(Home = fileGetURL(NULL, pkg, KeepPackage))) {
1069313Ssos		warnx("unable to fetch '%s' by URL", pkg);
1079313Ssos		return 1;
1089313Ssos	    }
1099313Ssos	    where_to = Home;
1109313Ssos	    strcpy(pkg_fullname, pkg);
11114331Speter	    cfile = fopen(CONTENTS_FNAME, "r");
1129313Ssos	    if (!cfile) {
1139313Ssos		warnx(
1149313Ssos		"unable to open table of contents file '%s' - not a package?",
1159313Ssos		CONTENTS_FNAME);
1169313Ssos		goto bomb;
1179313Ssos	    }
11814331Speter	    read_plist(&Plist, cfile);
1199313Ssos	    fclose(cfile);
1209313Ssos	}
1219313Ssos	else {
1229313Ssos	    strcpy(pkg_fullname, pkg);		/*
1239313Ssos						 * Copy for sanity's sake,
1249313Ssos						 * could remove pkg_fullname
12514331Speter						 */
1269313Ssos	    if (strcmp(pkg, "-")) {
1279313Ssos		if (stat(pkg_fullname, &sb) == FAIL) {
1289313Ssos		    warnx("can't stat package file '%s'", pkg_fullname);
1299313Ssos		    goto bomb;
1309313Ssos		}
1319313Ssos		sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
13214331Speter		extract = extract_contents;
1339313Ssos	    }
1349313Ssos	    else {
1359313Ssos		extract = NULL;
1369313Ssos		sb.st_size = 100000;	/* Make up a plausible average size */
1379313Ssos	    }
1389313Ssos	    Home = make_playpen(playpen, sb.st_size * 4);
13914331Speter	    if (!Home)
1409313Ssos		errx(1, "unable to make playpen for %lld bytes", (long long)sb.st_size * 4);
1419313Ssos	    where_to = Home;
1429313Ssos	    /* Since we can call ourselves recursively, keep notes on where we came from */
1439313Ssos	    if (!getenv("_TOP"))
1449313Ssos		setenv("_TOP", Home, 1);
1459313Ssos	    if (unpack(pkg_fullname, extract)) {
14614331Speter		warnx(
1479313Ssos	"unable to extract table of contents file from '%s' - not a package?",
1489313Ssos		pkg_fullname);
1499313Ssos		goto bomb;
1509313Ssos	    }
1519313Ssos	    cfile = fopen(CONTENTS_FNAME, "r");
1529313Ssos	    if (!cfile) {
15314331Speter		warnx(
1549313Ssos	"unable to open table of contents file '%s' - not a package?",
1559313Ssos		CONTENTS_FNAME);
1569313Ssos		goto bomb;
1579313Ssos	    }
1589313Ssos	    read_plist(&Plist, cfile);
1599313Ssos	    fclose(cfile);
16014331Speter
1619313Ssos	    /* Extract directly rather than moving?  Oh goodie! */
1629313Ssos	    if (find_plist_option(&Plist, "extract-in-place")) {
1639313Ssos		if (Verbose)
1649313Ssos		    printf("Doing in-place extraction for %s\n", pkg_fullname);
1659313Ssos		p = find_plist(&Plist, PLIST_CWD);
1669313Ssos		if (p) {
16714331Speter		    if (!isdir(p->name) && !Fake) {
1689313Ssos			if (Verbose)
1699313Ssos			    printf("Desired prefix of %s does not exist, creating..\n", p->name);
1709313Ssos			vsystem("/bin/mkdir -p %s", p->name);
1719313Ssos			if (chdir(p->name) == -1) {
1729313Ssos			    warn("unable to change directory to '%s'", p->name);
1739313Ssos			    goto bomb;
17414331Speter			}
1759313Ssos		    }
1769313Ssos		    where_to = p->name;
1779313Ssos		    inPlace = 1;
1789313Ssos		}
1799313Ssos		else {
1809313Ssos		    warnx(
18114331Speter		"no prefix specified in '%s' - this is a bad package!",
1829313Ssos			pkg_fullname);
18314331Speter		    goto bomb;
18414331Speter		}
18514331Speter	    }
1869313Ssos
1879313Ssos	    /*
1889313Ssos	     * Apply a crude heuristic to see how much space the package will
18914331Speter	     * take up once it's unpacked.  I've noticed that most packages
1909313Ssos	     * compress an average of 75%, so multiply by 4 for good measure.
1919313Ssos	     */
1929313Ssos
1939313Ssos	    if (!extract && !inPlace && min_free(playpen) < sb.st_size * 4) {
1949313Ssos		warnx("projected size of %lld exceeds available free space.\n"
1959313Ssos"Please set your PKG_TMPDIR variable to point to a location with more\n"
19614331Speter		       "free space and try again", (long long)sb.st_size * 4);
1979313Ssos		warnx("not extracting %s\ninto %s, sorry!",
1989313Ssos			pkg_fullname, where_to);
1999313Ssos		goto bomb;
2009313Ssos	    }
2019313Ssos
2029313Ssos	    /* If this is a direct extract and we didn't want it, stop now */
20314331Speter	    if (inPlace && Fake)
2049313Ssos		goto success;
2059313Ssos
2069313Ssos	    /* Finally unpack the whole mess.  If extract is null we
2079313Ssos	       already + did so so don't bother doing it again. */
2089313Ssos	    if (extract && unpack(pkg_fullname, NULL)) {
2099313Ssos		warnx("unable to extract '%s'!", pkg_fullname);
21014331Speter		goto bomb;
2119313Ssos	    }
2129313Ssos	}
2139313Ssos
2149313Ssos	/* Check for sanity and dependencies */
2159313Ssos	if (sanity_check(pkg))
2169313Ssos	    goto bomb;
21714331Speter
2189313Ssos	/* If we're running in MASTER mode, just output the plist and return */
2199313Ssos	if (AddMode == MASTER) {
2209313Ssos	    printf("%s\n", where_playpen());
2219313Ssos	    write_plist(&Plist, stdout);
2229313Ssos	    return 0;
2239313Ssos	}
22414331Speter    }
2259313Ssos
2269313Ssos    /*
2279313Ssos     * If we have a prefix, delete the first one we see and add this
2289313Ssos     * one in place of it.
2299313Ssos     */
2309313Ssos    if (Prefix) {
23114331Speter	delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
2329313Ssos	add_plist_top(&Plist, PLIST_CWD, Prefix);
2339313Ssos    }
2349313Ssos
2359313Ssos    setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1);
2369313Ssos    /* Protect against old packages with bogus @name and origin fields */
2379313Ssos    if (Plist.name == NULL)
23814331Speter	Plist.name = "anonymous";
2399313Ssos    if (Plist.origin == NULL)
2409313Ssos	Plist.origin = "anonymous/anonymous";
2419313Ssos
2429313Ssos    /*
2439313Ssos     * See if we're already registered either with the same name (the same
2449313Ssos     * version) or some other version with the same origin.
24514331Speter     */
2469313Ssos    if ((isinstalledpkg(Plist.name) > 0 ||
2479313Ssos         matchbyorigin(Plist.origin, NULL) != NULL) && !Force) {
2489313Ssos	warnx("package '%s' or its older version already installed%s",
2499313Ssos	      Plist.name, FailOnAlreadyInstalled ? "" : " (ignored)");
2509313Ssos	code = FailOnAlreadyInstalled != FALSE;
2519313Ssos	goto success;	/* close enough for government work */
25214331Speter    }
2539313Ssos
2549313Ssos    /* Now check the packing list for conflicts */
2559313Ssos	if(!IgnoreDeps){
2569313Ssos    for (p = Plist.head; p != NULL; p = p->next) {
2579313Ssos	if (p->type == PLIST_CONFLICTS) {
2589313Ssos	    int i;
25914331Speter	    conflict[0] = strdup(p->name);
2609313Ssos	    conflict[1] = NULL;
2619313Ssos	    matched = matchinstalled(MATCH_GLOB, conflict, &errcode);
2629313Ssos	    free(conflict[0]);
2639313Ssos	    if (errcode == 0 && matched != NULL)
2649313Ssos		for (i = 0; matched[i] != NULL; i++)
2659313Ssos		    if (isinstalledpkg(matched[i]) > 0) {
26614331Speter			warnx("package '%s' conflicts with %s", Plist.name,
2679313Ssos				matched[i]);
2689313Ssos			conflictsfound = 1;
2699313Ssos		    }
2709313Ssos
2719313Ssos	    continue;
2729313Ssos	}
27314331Speter    }
2749313Ssos    if(conflictsfound) {
2759313Ssos	if(!Force) {
2769313Ssos	    warnx("please use pkg_delete first to remove conflicting package(s) or -f to force installation");
2779313Ssos	    code = 1;
2789313Ssos	    goto bomb;
2799313Ssos	} else
28014331Speter	    warnx("-f specified; proceeding anyway");
2819313Ssos    }
2829313Ssos
2839313Ssos    /* Now check the packing list for dependencies */
2849313Ssos    for (p = Plist.head; p ; p = p->next) {
2859313Ssos	char *deporigin;
2869313Ssos
28714331Speter	if (p->type != PLIST_PKGDEP)
2889313Ssos	    continue;
2899313Ssos	deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : NULL;
2909313Ssos	if (Verbose) {
2919313Ssos	    printf("Package '%s' depends on '%s'", Plist.name, p->name);
2929313Ssos	    if (deporigin != NULL)
2939313Ssos		printf(" with '%s' origin", deporigin);
29414331Speter	    printf(".\n");
2959313Ssos	}
2969313Ssos	if (isinstalledpkg(p->name) <= 0 &&
2979313Ssos	    !(deporigin != NULL && matchbyorigin(deporigin, NULL) != NULL)) {
2989313Ssos	    char path[FILENAME_MAX], *cp = NULL;
299
300	    if (!Fake&&!IgnoreDeps) {
301		char prefixArg[2 + MAXPATHLEN]; /* "-P" + Prefix */
302		if (PrefixRecursive) {
303		    strlcpy(prefixArg, "-P", sizeof(prefixArg));
304		    strlcat(prefixArg, Prefix, sizeof(prefixArg));
305		}
306		if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) {
307		    const char *ext;
308
309		    ext = strrchr(pkg_fullname, '.');
310		    if (ext == NULL)
311#if defined(__FreeBSD_version) && __FreeBSD_version >= 500039
312			ext = ".tbz";
313#else
314			ext = ".tgz";
315#endif
316		    snprintf(path, FILENAME_MAX, "%s/%s%s", getenv("_TOP"), p->name, ext);
317		    if (fexists(path))
318			cp = path;
319		    else
320			cp = fileFindByPath(pkg, p->name);
321		    if (cp) {
322			if (Verbose)
323			    printf("Loading it from %s.\n", cp);
324			if (vsystem("%s %s %s '%s'", PkgAddCmd, Verbose ? "-v " : "", PrefixRecursive ? prefixArg : "", cp)) {
325			    warnx("autoload of dependency '%s' failed%s",
326				cp, Force ? " (proceeding anyway)" : "!");
327			    if (!Force)
328				++code;
329			}
330		    }
331		    else {
332			warnx("could not find package %s %s",
333			      p->name, Force ? " (proceeding anyway)" : "!");
334			if (!Force)
335			    ++code;
336		    }
337		}
338		else if ((cp = fileGetURL(pkg, p->name, KeepPackage)) != NULL) {
339		    if (Verbose)
340			printf("Finished loading %s over FTP.\n", p->name);
341		    if (!fexists("+CONTENTS")) {
342			warnx("autoloaded package %s has no +CONTENTS file?",
343				p->name);
344			if (!Force)
345			    ++code;
346		    }
347		    else if (vsystem("(pwd; /bin/cat +CONTENTS) | %s %s %s -S", PkgAddCmd, Verbose ? "-v" : "", PrefixRecursive ? prefixArg : "")) {
348			warnx("pkg_add of dependency '%s' failed%s",
349				p->name, Force ? " (proceeding anyway)" : "!");
350			if (!Force)
351			    ++code;
352		    }
353		    else if (Verbose)
354			printf("\t'%s' loaded successfully.\n", p->name);
355		    /* Nuke the temporary playpen */
356		    leave_playpen();
357		}
358	    }
359	    else {
360		if (Verbose)
361		    printf("and was not found%s.\n", Force ? " (proceeding anyway)" : "");
362		else
363		    printf("Package dependency %s for %s not found%s\n", p->name, pkg,
364			   Force ? " (proceeding anyway)" : "!");
365		if (!Force)
366		    ++code;
367	    }
368	}
369	else if (Verbose)
370	    printf(" - already installed.\n");
371    }
372	}
373
374    if (code != 0)
375	goto bomb;
376
377    /* Look for the requirements file */
378    if (fexists(REQUIRE_FNAME)) {
379	vsystem("/bin/chmod +x %s", REQUIRE_FNAME);	/* be sure */
380	if (Verbose)
381	    printf("Running requirements file first for %s..\n", Plist.name);
382	if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, Plist.name)) {
383	    warnx("package %s fails requirements %s", pkg_fullname,
384		   Force ? "installing anyway" : "- not installed");
385	    if (!Force) {
386		code = 1;
387		goto success;	/* close enough for government work */
388	    }
389	}
390    }
391
392    /*
393     * Test whether to use the old method of passing tokens to installation
394     * scripts, and set appropriate variables..
395     */
396
397    if (fexists(POST_INSTALL_FNAME)) {
398	new_m = 1;
399	sprintf(post_script, "%s", POST_INSTALL_FNAME);
400	pre_arg[0] = '\0';
401	post_arg[0] = '\0';
402    } else {
403	if (fexists(INSTALL_FNAME)) {
404	    sprintf(post_script, "%s", INSTALL_FNAME);
405	    sprintf(pre_arg, "PRE-INSTALL");
406	    sprintf(post_arg, "POST-INSTALL");
407	}
408    }
409
410    /* If we're really installing, and have an installation file, run it */
411    if (!NoInstall && fexists(pre_script)) {
412	vsystem("/bin/chmod +x %s", pre_script);	/* make sure */
413	if (Verbose)
414	    printf("Running pre-install for %s..\n", Plist.name);
415	if (!Fake && vsystem("./%s %s %s", pre_script, Plist.name, pre_arg)) {
416	    warnx("install script returned error status");
417	    unlink(pre_script);
418	    code = 1;
419	    goto success;		/* nothing to uninstall yet */
420	}
421    }
422
423    /* Now finally extract the entire show if we're not going direct */
424    if (!inPlace && !Fake)
425	extract_plist(".", &Plist);
426
427    if (!Fake && fexists(MTREE_FNAME)) {
428	if (Verbose)
429	    printf("Running mtree for %s..\n", Plist.name);
430	p = find_plist(&Plist, PLIST_CWD);
431	if (Verbose)
432	    printf("mtree -U -f %s -d -e -p %s >%s\n", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL);
433	if (!Fake) {
434	    if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s >%s", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL))
435		warnx("mtree returned a non-zero status - continuing");
436	}
437    }
438
439    /* Run the installation script one last time? */
440    if (!NoInstall && fexists(post_script)) {
441	vsystem("/bin/chmod +x %s", post_script);	/* make sure */
442	if (Verbose)
443	    printf("Running post-install for %s..\n", Plist.name);
444	if (!Fake && vsystem("./%s %s %s", post_script, Plist.name, post_arg)) {
445	    warnx("install script returned error status");
446	    unlink(post_script);
447	    code = 1;
448	    goto fail;
449	}
450    }
451
452    /* Time to record the deed? */
453    if (!NoRecord && !Fake) {
454	char contents[FILENAME_MAX];
455	FILE *contfile;
456
457	if (getuid() != 0)
458	    warnx("not running as root - trying to record install anyway");
459	sprintf(LogDir, "%s/%s", LOG_DIR, Plist.name);
460	zapLogDir = 1;
461	if (Verbose)
462	    printf("Attempting to record package into %s..\n", LogDir);
463	if (make_hierarchy(LogDir)) {
464	    warnx("can't record package into '%s', you're on your own!",
465		   LogDir);
466	    bzero(LogDir, FILENAME_MAX);
467	    code = 1;
468	    goto success;	/* close enough for government work */
469	}
470	/* Make sure pkg_info can read the entry */
471	vsystem("/bin/chmod a+rx %s", LogDir);
472	move_file(".", DESC_FNAME, LogDir);
473	move_file(".", COMMENT_FNAME, LogDir);
474	if (fexists(INSTALL_FNAME))
475	    move_file(".", INSTALL_FNAME, LogDir);
476	if (fexists(POST_INSTALL_FNAME))
477	    move_file(".", POST_INSTALL_FNAME, LogDir);
478	if (fexists(DEINSTALL_FNAME))
479	    move_file(".", DEINSTALL_FNAME, LogDir);
480	if (fexists(POST_DEINSTALL_FNAME))
481	    move_file(".", POST_DEINSTALL_FNAME, LogDir);
482	if (fexists(REQUIRE_FNAME))
483	    move_file(".", REQUIRE_FNAME, LogDir);
484	if (fexists(DISPLAY_FNAME))
485	    move_file(".", DISPLAY_FNAME, LogDir);
486	if (fexists(MTREE_FNAME))
487	    move_file(".", MTREE_FNAME, LogDir);
488	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
489	contfile = fopen(contents, "w");
490	if (!contfile) {
491	    warnx("can't open new contents file '%s'! can't register pkg",
492		contents);
493	    goto success; /* can't log, but still keep pkg */
494	}
495	write_plist(&Plist, contfile);
496	fclose(contfile);
497	for (p = Plist.head; p ; p = p->next) {
498	    char *deporigin, **depnames;
499	    int i;
500
501	    if (p->type != PLIST_PKGDEP)
502		continue;
503	    deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name :
504							     NULL;
505	    if (Verbose) {
506		printf("Trying to record dependency on package '%s'", p->name);
507		if (deporigin != NULL)
508		    printf(" with '%s' origin", deporigin);
509		printf(".\n");
510	    }
511
512	    depnames = (deporigin != NULL) ? matchbyorigin(deporigin, NULL) :
513					     NULL;
514	    if (depnames == NULL) {
515		depnames = alloca(sizeof(*depnames) * 2);
516		depnames[0] = p->name;
517		depnames[1] = NULL;
518	    }
519		if(!IgnoreDeps){
520	    for (i = 0; depnames[i] != NULL; i++) {
521		sprintf(contents, "%s/%s/%s", LOG_DIR, depnames[i],
522			REQUIRED_BY_FNAME);
523		if (strcmp(p->name, depnames[i]) != 0)
524		    warnx("warning: package '%s' requires '%s', but '%s' "
525			  "is installed", Plist.name, p->name, depnames[i]);
526		contfile = fopen(contents, "a");
527		if (!contfile)
528		    warnx("can't open dependency file '%s'!\n"
529			  "dependency registration is incomplete", contents);
530		else {
531		    fprintf(contfile, "%s\n", Plist.name);
532		    if (fclose(contfile) == EOF)
533			warnx("cannot properly close file %s", contents);
534		}
535	    }
536	}
537	}
538	if (Verbose)
539	    printf("Package %s registered in %s\n", Plist.name, LogDir);
540    }
541
542    if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) {
543	FILE *fp;
544	char buf[BUFSIZ];
545
546	snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name);
547	fp = fopen(buf, "r");
548	if (fp) {
549	    putc('\n', stdout);
550	    while (fgets(buf, sizeof(buf), fp))
551		fputs(buf, stdout);
552	    putc('\n', stdout);
553	    (void) fclose(fp);
554	} else {
555    	    if (!Fake) {
556		warnx("cannot open %s as display file", buf);
557	    }
558	}
559    }
560
561    goto success;
562
563 bomb:
564    code = 1;
565    goto success;
566
567 fail:
568    /* Nuke the whole (installed) show, XXX but don't clean directories */
569    if (!Fake)
570	delete_package(FALSE, FALSE, &Plist);
571
572 success:
573    /* delete the packing list contents */
574    free_plist(&Plist);
575    leave_playpen();
576    return code;
577}
578
579static int
580sanity_check(char *pkg)
581{
582    int code = 0;
583
584    if (!fexists(CONTENTS_FNAME)) {
585	warnx("package %s has no CONTENTS file!", pkg);
586	code = 1;
587    }
588    else if (!fexists(COMMENT_FNAME)) {
589	warnx("package %s has no COMMENT file!", pkg);
590	code = 1;
591    }
592    else if (!fexists(DESC_FNAME)) {
593	warnx("package %s has no DESC file!", pkg);
594	code = 1;
595    }
596    return code;
597}
598
599void
600cleanup(int sig)
601{
602    static int in_cleanup = 0;
603
604    if (!in_cleanup) {
605	in_cleanup = 1;
606    	if (sig)
607	    printf("Signal %d received, cleaning up..\n", sig);
608    	if (!Fake && zapLogDir && LogDir[0])
609	    vsystem("%s -rf %s", REMOVE_CMD, LogDir);
610    	leave_playpen();
611    }
612    if (sig)
613	exit(1);
614}
615