perform.c revision 84745
1327Sjkh#ifndef lint
230221Scharnierstatic const char rcsid[] =
350479Speter  "$FreeBSD: head/usr.sbin/pkg_install/add/perform.c 84745 2001-10-10 06:58:42Z sobomax $";
4327Sjkh#endif
5327Sjkh
6327Sjkh/*
7327Sjkh * FreeBSD install - a package for the installation and maintainance
8327Sjkh * of non-core utilities.
9327Sjkh *
10327Sjkh * Redistribution and use in source and binary forms, with or without
11327Sjkh * modification, are permitted provided that the following conditions
12327Sjkh * are met:
13327Sjkh * 1. Redistributions of source code must retain the above copyright
14327Sjkh *    notice, this list of conditions and the following disclaimer.
15327Sjkh * 2. Redistributions in binary form must reproduce the above copyright
16327Sjkh *    notice, this list of conditions and the following disclaimer in the
17327Sjkh *    documentation and/or other materials provided with the distribution.
18327Sjkh *
19327Sjkh * Jordan K. Hubbard
20327Sjkh * 18 July 1993
21327Sjkh *
22327Sjkh * This is the main body of the add module.
23327Sjkh *
24327Sjkh */
25327Sjkh
2630221Scharnier#include <err.h>
2769793Sobrien#include <paths.h>
28327Sjkh#include "lib.h"
29327Sjkh#include "add.h"
30327Sjkh
3184670Ssobomax#include <libgen.h>
32327Sjkh#include <signal.h>
334996Sjkh#include <sys/wait.h>
34327Sjkh
35327Sjkhstatic int pkg_do(char *);
36327Sjkhstatic int sanity_check(char *);
37327Sjkhstatic char LogDir[FILENAME_MAX];
3837728Seivindstatic int zapLogDir;		/* Should we delete LogDir? */
39327Sjkh
40327Sjkhint
41327Sjkhpkg_perform(char **pkgs)
42327Sjkh{
43327Sjkh    int i, err_cnt = 0;
44327Sjkh
45327Sjkh    signal(SIGINT, cleanup);
46327Sjkh    signal(SIGHUP, cleanup);
47327Sjkh
48382Sjkh    if (AddMode == SLAVE)
49382Sjkh	err_cnt = pkg_do(NULL);
50382Sjkh    else {
51382Sjkh	for (i = 0; pkgs[i]; i++)
52382Sjkh	    err_cnt += pkg_do(pkgs[i]);
53382Sjkh    }
54327Sjkh    return err_cnt;
55327Sjkh}
56327Sjkh
57327Sjkhstatic Package Plist;
5811780Sjkhstatic char *Home;
59327Sjkh
608083Sjkh/*
618083Sjkh * This is seriously ugly code following.  Written very fast!
628083Sjkh * [And subsequently made even worse..  Sigh!  This code was just born
638083Sjkh * to be hacked, I guess.. :) -jkh]
648083Sjkh */
65327Sjkhstatic int
66327Sjkhpkg_do(char *pkg)
67327Sjkh{
68327Sjkh    char pkg_fullname[FILENAME_MAX];
6911780Sjkh    char playpen[FILENAME_MAX];
707998Sjkh    char extract_contents[FILENAME_MAX];
7184670Ssobomax    char *where_to, *extract;
72327Sjkh    FILE *cfile;
7311780Sjkh    int code;
741545Sjkh    PackingList p;
753364Sjkh    struct stat sb;
7611780Sjkh    int inPlace;
7741866Sjkh    /* support for separate pre/post install scripts */
7841866Sjkh    int new_m = 0;
7941866Sjkh    char pre_script[FILENAME_MAX] = INSTALL_FNAME;
8041866Sjkh    char post_script[FILENAME_MAX];
8141866Sjkh    char pre_arg[FILENAME_MAX], post_arg[FILENAME_MAX];
82327Sjkh
8311780Sjkh    code = 0;
8437728Seivind    zapLogDir = 0;
85327Sjkh    LogDir[0] = '\0';
8611780Sjkh    strcpy(playpen, FirstPen);
8711780Sjkh    inPlace = 0;
888075Sjkh
898083Sjkh    /* Are we coming in for a second pass, everything already extracted? */
9011780Sjkh    if (!pkg) {
9111780Sjkh	fgets(playpen, FILENAME_MAX, stdin);
9211780Sjkh	playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */
9311780Sjkh	if (chdir(playpen) == FAIL) {
9430221Scharnier	    warnx("pkg_add in SLAVE mode can't chdir to %s", playpen);
95382Sjkh	    return 1;
96382Sjkh	}
97382Sjkh	read_plist(&Plist, stdin);
9811780Sjkh	where_to = playpen;
99327Sjkh    }
1008083Sjkh    /* Nope - do it now */
101382Sjkh    else {
10211780Sjkh	/* Is it an ftp://foo.bar.baz/file.tgz specification? */
1038083Sjkh	if (isURL(pkg)) {
10411780Sjkh	    if (!(Home = fileGetURL(NULL, pkg))) {
10581046Ssobomax		warnx("unable to fetch '%s' by URL", pkg);
1068083Sjkh		return 1;
1078083Sjkh	    }
10811780Sjkh	    where_to = Home;
10911780Sjkh	    strcpy(pkg_fullname, pkg);
11011780Sjkh	    cfile = fopen(CONTENTS_FNAME, "r");
11111780Sjkh	    if (!cfile) {
11230221Scharnier		warnx(
11381046Ssobomax		"unable to open table of contents file '%s' - not a package?",
11430221Scharnier		CONTENTS_FNAME);
11511780Sjkh		goto bomb;
11611780Sjkh	    }
11711780Sjkh	    read_plist(&Plist, cfile);
11811780Sjkh	    fclose(cfile);
119382Sjkh	}
1208083Sjkh	else {
12176739Ssobomax	    strcpy(pkg_fullname, pkg);		/*
12276739Ssobomax						 * Copy for sanity's sake,
12376739Ssobomax						 * could remove pkg_fullname
12476739Ssobomax						 */
12514582Sjkh	    if (strcmp(pkg, "-")) {
12614582Sjkh		if (stat(pkg_fullname, &sb) == FAIL) {
12730221Scharnier		    warnx("can't stat package file '%s'", pkg_fullname);
12814582Sjkh		    goto bomb;
12914582Sjkh		}
13014582Sjkh		sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
13114582Sjkh		extract = extract_contents;
13211780Sjkh	    }
13322750Sjkh	    else {
13414582Sjkh		extract = NULL;
13522750Sjkh		sb.st_size = 100000;	/* Make up a plausible average size */
13622750Sjkh	    }
13711780Sjkh	    Home = make_playpen(playpen, sb.st_size * 4);
13811780Sjkh	    if (!Home)
13984745Ssobomax		errx(1, "unable to make playpen for %qd bytes", (long long)sb.st_size * 4);
14011780Sjkh	    where_to = Home;
14138583Sjkh	    /* Since we can call ourselves recursively, keep notes on where we came from */
14238583Sjkh	    if (!getenv("_TOP"))
14338583Sjkh		setenv("_TOP", Home, 1);
14414582Sjkh	    if (unpack(pkg_fullname, extract)) {
14530221Scharnier		warnx(
14681046Ssobomax	"unable to extract table of contents file from '%s' - not a package?",
14730221Scharnier		pkg_fullname);
14811780Sjkh		goto bomb;
14911780Sjkh	    }
15011780Sjkh	    cfile = fopen(CONTENTS_FNAME, "r");
15111780Sjkh	    if (!cfile) {
15230221Scharnier		warnx(
15381046Ssobomax	"unable to open table of contents file '%s' - not a package?",
15430221Scharnier		CONTENTS_FNAME);
15511780Sjkh		goto bomb;
15611780Sjkh	    }
15711780Sjkh	    read_plist(&Plist, cfile);
15811780Sjkh	    fclose(cfile);
1597998Sjkh
16011780Sjkh	    /* Extract directly rather than moving?  Oh goodie! */
16111780Sjkh	    if (find_plist_option(&Plist, "extract-in-place")) {
16211780Sjkh		if (Verbose)
16311780Sjkh		    printf("Doing in-place extraction for %s\n", pkg_fullname);
16411780Sjkh		p = find_plist(&Plist, PLIST_CWD);
16511780Sjkh		if (p) {
16611780Sjkh		    if (!isdir(p->name) && !Fake) {
16711780Sjkh			if (Verbose)
16811780Sjkh			    printf("Desired prefix of %s does not exist, creating..\n", p->name);
16911780Sjkh			vsystem("mkdir -p %s", p->name);
17011780Sjkh			if (chdir(p->name) == -1) {
17181046Ssobomax			    warn("unable to change directory to '%s'", p->name);
17211780Sjkh			    goto bomb;
17311780Sjkh			}
1747998Sjkh		    }
17511780Sjkh		    where_to = p->name;
17611780Sjkh		    inPlace = 1;
1777998Sjkh		}
17811780Sjkh		else {
17930221Scharnier		    warnx(
18081046Ssobomax		"no prefix specified in '%s' - this is a bad package!",
18130221Scharnier			pkg_fullname);
18211780Sjkh		    goto bomb;
18311780Sjkh		}
1847998Sjkh	    }
18511780Sjkh
18611780Sjkh	    /*
18711780Sjkh	     * Apply a crude heuristic to see how much space the package will
18811780Sjkh	     * take up once it's unpacked.  I've noticed that most packages
18911780Sjkh	     * compress an average of 75%, so multiply by 4 for good measure.
19011780Sjkh	     */
19111780Sjkh
19214582Sjkh	    if (!inPlace && min_free(playpen) < sb.st_size * 4) {
19349637Sbillf		warnx("projected size of %qd exceeds available free space.\n"
19430221Scharnier"Please set your PKG_TMPDIR variable to point to a location with more\n"
19584745Ssobomax		       "free space and try again", (long long)sb.st_size * 4);
19630221Scharnier		warnx("not extracting %s\ninto %s, sorry!",
19730221Scharnier			pkg_fullname, where_to);
1988083Sjkh		goto bomb;
1997998Sjkh	    }
2009786Sjkh
20111780Sjkh	    /* If this is a direct extract and we didn't want it, stop now */
20211780Sjkh	    if (inPlace && Fake)
20311780Sjkh		goto success;
2047998Sjkh
20511780Sjkh	    /* Finally unpack the whole mess */
20611780Sjkh	    if (unpack(pkg_fullname, NULL)) {
20781046Ssobomax		warnx("unable to extract '%s'!", pkg_fullname);
20811780Sjkh		goto bomb;
20911780Sjkh	    }
21011780Sjkh	}
21114582Sjkh
21211780Sjkh	/* Check for sanity and dependencies */
21311780Sjkh	if (sanity_check(pkg))
2147998Sjkh	    goto bomb;
21511780Sjkh
21611780Sjkh	/* If we're running in MASTER mode, just output the plist and return */
21711780Sjkh	if (AddMode == MASTER) {
21811780Sjkh	    printf("%s\n", where_playpen());
21911780Sjkh	    write_plist(&Plist, stdout);
22011780Sjkh	    return 0;
2217996Sjkh	}
22211780Sjkh    }
223327Sjkh
22411780Sjkh    /*
22511780Sjkh     * If we have a prefix, delete the first one we see and add this
22611780Sjkh     * one in place of it.
22711780Sjkh     */
22811780Sjkh    if (Prefix) {
22911780Sjkh	delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
23011780Sjkh	add_plist_top(&Plist, PLIST_CWD, Prefix);
23111780Sjkh    }
2327998Sjkh
23311780Sjkh    setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1);
23411780Sjkh    /* Protect against old packages with bogus @name fields */
23584745Ssobomax    (const char *)PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous";
2367998Sjkh
23711780Sjkh    /* See if we're already registered */
23881049Ssobomax    sprintf(LogDir, "%s/%s", LOG_DIR, PkgName);
23917373Sjkh    if (isdir(LogDir) && !Force) {
24081046Ssobomax	warnx("package '%s' already recorded as installed", PkgName);
24111780Sjkh	code = 1;
24211780Sjkh	goto success;	/* close enough for government work */
24311780Sjkh    }
2447998Sjkh
24511780Sjkh    /* Now check the packing list for dependencies */
24611780Sjkh    for (p = Plist.head; p ; p = p->next) {
24711780Sjkh	if (p->type != PLIST_PKGDEP)
24811780Sjkh	    continue;
24911780Sjkh	if (Verbose)
25081046Ssobomax	    printf("Package '%s' depends on '%s'.\n", PkgName, p->name);
25118929Sjkh	if (vsystem("pkg_info -e %s", p->name)) {
25211780Sjkh	    char path[FILENAME_MAX], *cp = NULL;
2538083Sjkh
25418929Sjkh	    if (!Fake) {
25518929Sjkh		if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) {
25638583Sjkh		    snprintf(path, FILENAME_MAX, "%s/%s.tgz", getenv("_TOP"), p->name);
25718929Sjkh		    if (fexists(path))
25818929Sjkh			cp = path;
25918929Sjkh		    else
26018929Sjkh			cp = fileFindByPath(pkg, p->name);
26118929Sjkh		    if (cp) {
26218929Sjkh			if (Verbose)
26318929Sjkh			    printf("Loading it from %s.\n", cp);
26478118Sjkh			if (vsystem("pkg_add %s'%s'", Verbose ? "-v " : "", cp)) {
26581046Ssobomax			    warnx("autoload of dependency '%s' failed%s",
26630221Scharnier				cp, Force ? " (proceeding anyway)" : "!");
26718929Sjkh			    if (!Force)
26818929Sjkh				++code;
26918929Sjkh			}
27018929Sjkh		    }
27138931Sjkh		    else {
27238931Sjkh			warnx("could not find package %s %s",
27338931Sjkh			      p->name, Force ? " (proceeding anyway)" : "!");
27438931Sjkh			if (!Force)
27538931Sjkh			    ++code;
27638931Sjkh		    }
27718929Sjkh		}
27818929Sjkh		else if ((cp = fileGetURL(pkg, p->name)) != NULL) {
2798075Sjkh		    if (Verbose)
28018929Sjkh			printf("Finished loading %s over FTP.\n", p->name);
28118929Sjkh		    if (!fexists("+CONTENTS")) {
28230221Scharnier			warnx("autoloaded package %s has no +CONTENTS file?",
28330221Scharnier				p->name);
2848075Sjkh			if (!Force)
2858075Sjkh			    ++code;
2868075Sjkh		    }
28718929Sjkh		    else if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S", Verbose ? "-v " : "")) {
28881046Ssobomax			warnx("pkg_add of dependency '%s' failed%s",
28930221Scharnier				p->name, Force ? " (proceeding anyway)" : "!");
29018929Sjkh			if (!Force)
29118929Sjkh			    ++code;
29218929Sjkh		    }
29318929Sjkh		    else if (Verbose)
29481046Ssobomax			printf("\t'%s' loaded successfully.\n", p->name);
29518929Sjkh		    /* Nuke the temporary playpen */
29633427Sjkh		    leave_playpen();
2978075Sjkh		}
29811780Sjkh	    }
29911780Sjkh	    else {
30011780Sjkh		if (Verbose)
30111780Sjkh		    printf("and was not found%s.\n", Force ? " (proceeding anyway)" : "");
30211780Sjkh		else
30311780Sjkh		    printf("Package dependency %s for %s not found%s\n", p->name, pkg,
30411780Sjkh			   Force ? " (proceeding anyway)" : "!");
30511780Sjkh		if (!Force)
30611780Sjkh		    ++code;
30711780Sjkh	    }
3088075Sjkh	}
30911780Sjkh	else if (Verbose)
31011780Sjkh	    printf(" - already installed.\n");
311327Sjkh    }
3127713Sjkh
31316087Sjkh    if (code != 0)
31416087Sjkh	goto bomb;
31516087Sjkh
3168075Sjkh    /* Look for the requirements file */
317327Sjkh    if (fexists(REQUIRE_FNAME)) {
318327Sjkh	vsystem("chmod +x %s", REQUIRE_FNAME);	/* be sure */
319327Sjkh	if (Verbose)
320327Sjkh	    printf("Running requirements file first for %s..\n", PkgName);
32132665Sjkh	if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) {
32230221Scharnier	    warnx("package %s fails requirements %s", pkg_fullname,
32330221Scharnier		   Force ? "installing anyway" : "- not installed");
3244996Sjkh	    if (!Force) {
3254996Sjkh		code = 1;
3264996Sjkh		goto success;	/* close enough for government work */
3274996Sjkh	    }
328327Sjkh	}
329327Sjkh    }
3308075Sjkh
33176739Ssobomax    /*
33276739Ssobomax     * Test whether to use the old method of passing tokens to installation
33341866Sjkh     * scripts, and set appropriate variables..
33441866Sjkh     */
33541866Sjkh
33641866Sjkh    if (fexists(POST_INSTALL_FNAME)) {
33741866Sjkh	new_m = 1;
33841866Sjkh	sprintf(post_script, "%s", POST_INSTALL_FNAME);
33949637Sbillf	pre_arg[0] = '\0';
34049637Sbillf	post_arg[0] = '\0';
34141866Sjkh    } else {
34241866Sjkh	if (fexists(INSTALL_FNAME)) {
34341866Sjkh	    sprintf(post_script, "%s", INSTALL_FNAME);
34441866Sjkh	    sprintf(pre_arg, "PRE-INSTALL");
34541866Sjkh	    sprintf(post_arg, "POST-INSTALL");
34641866Sjkh	}
34741866Sjkh    }
34841866Sjkh
3498075Sjkh    /* If we're really installing, and have an installation file, run it */
35041866Sjkh    if (!NoInstall && fexists(pre_script)) {
35141866Sjkh	vsystem("chmod +x %s", pre_script);	/* make sure */
352327Sjkh	if (Verbose)
35341866Sjkh	    printf("Running pre-install for %s..\n", PkgName);
35441866Sjkh	if (!Fake && vsystem("./%s %s %s", pre_script, PkgName, pre_arg)) {
35530221Scharnier	    warnx("install script returned error status");
35641866Sjkh	    unlink(pre_script);
3574996Sjkh	    code = 1;
3584996Sjkh	    goto success;		/* nothing to uninstall yet */
359327Sjkh	}
360327Sjkh    }
3618075Sjkh
3628075Sjkh    /* Now finally extract the entire show if we're not going direct */
36311780Sjkh    if (!inPlace && !Fake)
36411780Sjkh	extract_plist(".", &Plist);
3658075Sjkh
3668075Sjkh    if (!Fake && fexists(MTREE_FNAME)) {
3674996Sjkh	if (Verbose)
3684996Sjkh	    printf("Running mtree for %s..\n", PkgName);
3694996Sjkh	p = find_plist(&Plist, PLIST_CWD);
3704996Sjkh	if (Verbose)
37169793Sobrien	    printf("mtree -U -f %s -d -e -p %s >%s\n", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL);
3728115Sjkh	if (!Fake) {
37371055Ssobomax	    if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s >%s", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL))
37430221Scharnier		warnx("mtree returned a non-zero status - continuing");
3758115Sjkh	}
3764996Sjkh    }
3778075Sjkh
3788083Sjkh    /* Run the installation script one last time? */
37941866Sjkh    if (!NoInstall && fexists(post_script)) {
38041866Sjkh	vsystem("chmod +x %s", post_script);	/* make sure */
381327Sjkh	if (Verbose)
38241866Sjkh	    printf("Running post-install for %s..\n", PkgName);
38341866Sjkh	if (!Fake && vsystem("./%s %s %s", post_script, PkgName, post_arg)) {
38430221Scharnier	    warnx("install script returned error status");
38541866Sjkh	    unlink(post_script);
3864996Sjkh	    code = 1;
387327Sjkh	    goto fail;
388327Sjkh	}
389327Sjkh    }
3908075Sjkh
3918083Sjkh    /* Time to record the deed? */
392327Sjkh    if (!NoRecord && !Fake) {
393382Sjkh	char contents[FILENAME_MAX];
39484745Ssobomax	FILE *contfile;
395382Sjkh
396327Sjkh	if (getuid() != 0)
39730221Scharnier	    warnx("not running as root - trying to record install anyway");
398327Sjkh	if (!PkgName) {
39930221Scharnier	    warnx("no package name! can't record package, sorry");
400327Sjkh	    code = 1;
401327Sjkh	    goto success;	/* well, partial anyway */
402327Sjkh	}
40381049Ssobomax	sprintf(LogDir, "%s/%s", LOG_DIR, PkgName);
40437728Seivind	zapLogDir = 1;
405327Sjkh	if (Verbose)
406327Sjkh	    printf("Attempting to record package into %s..\n", LogDir);
407327Sjkh	if (make_hierarchy(LogDir)) {
40830221Scharnier	    warnx("can't record package into '%s', you're on your own!",
409327Sjkh		   LogDir);
410327Sjkh	    bzero(LogDir, FILENAME_MAX);
411327Sjkh	    code = 1;
412327Sjkh	    goto success;	/* close enough for government work */
413327Sjkh	}
414477Sjkh	/* Make sure pkg_info can read the entry */
415477Sjkh	vsystem("chmod a+rx %s", LogDir);
41684670Ssobomax	move_file(".", DESC_FNAME, LogDir);
41784670Ssobomax	move_file(".", COMMENT_FNAME, LogDir);
41884670Ssobomax	if (fexists(INSTALL_FNAME))
41984670Ssobomax	    move_file(".", INSTALL_FNAME, LogDir);
42084670Ssobomax	if (fexists(POST_INSTALL_FNAME))
42184670Ssobomax	    move_file(".", POST_INSTALL_FNAME, LogDir);
422327Sjkh	if (fexists(DEINSTALL_FNAME))
4237998Sjkh	    move_file(".", DEINSTALL_FNAME, LogDir);
42441866Sjkh	if (fexists(POST_DEINSTALL_FNAME))
42541866Sjkh	    move_file(".", POST_DEINSTALL_FNAME, LogDir);
426327Sjkh	if (fexists(REQUIRE_FNAME))
4277998Sjkh	    move_file(".", REQUIRE_FNAME, LogDir);
42884670Ssobomax	if (fexists(DISPLAY_FNAME))
42984670Ssobomax	    move_file(".", DISPLAY_FNAME, LogDir);
43084670Ssobomax	if (fexists(MTREE_FNAME))
43184670Ssobomax	    move_file(".", MTREE_FNAME, LogDir);
432382Sjkh	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
43384745Ssobomax	contfile = fopen(contents, "w");
43484745Ssobomax	if (!contfile) {
43530221Scharnier	    warnx("can't open new contents file '%s'! can't register pkg",
43630221Scharnier		contents);
437382Sjkh	    goto success; /* can't log, but still keep pkg */
438382Sjkh	}
43984745Ssobomax	write_plist(&Plist, contfile);
44084745Ssobomax	fclose(contfile);
4414996Sjkh	for (p = Plist.head; p ; p = p->next) {
4424996Sjkh	    if (p->type != PLIST_PKGDEP)
4434996Sjkh		continue;
4444996Sjkh	    if (Verbose)
44581046Ssobomax		printf("Attempting to record dependency on package '%s'\n", p->name);
44681571Sobrien	    sprintf(contents, "%s/%s/%s", LOG_DIR, basename(p->name),
44781049Ssobomax	    	    REQUIRED_BY_FNAME);
44884745Ssobomax	    contfile = fopen(contents, "a");
44984745Ssobomax	    if (!contfile)
45030221Scharnier		warnx("can't open dependency file '%s'!\n"
45130221Scharnier		       "dependency registration is incomplete", contents);
4529202Srgrimes	    else {
45384745Ssobomax		fprintf(contfile, "%s\n", PkgName);
45484745Ssobomax		if (fclose(contfile) == EOF)
45530221Scharnier		    warnx("cannot properly close file %s", contents);
4564996Sjkh	    }
4574996Sjkh	}
458327Sjkh	if (Verbose)
459327Sjkh	    printf("Package %s registered in %s\n", PkgName, LogDir);
460327Sjkh    }
4618857Srgrimes
46216549Sjkh    if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) {
4634996Sjkh	FILE *fp;
4644996Sjkh	char buf[BUFSIZ];
46516549Sjkh
46621650Sjkh	snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name);
46721650Sjkh	fp = fopen(buf, "r");
4684996Sjkh	if (fp) {
4694996Sjkh	    putc('\n', stdout);
4704996Sjkh	    while (fgets(buf, sizeof(buf), fp))
4714996Sjkh		fputs(buf, stdout);
4724996Sjkh	    putc('\n', stdout);
4734996Sjkh	    (void) fclose(fp);
4744996Sjkh	} else
47530221Scharnier	    warnx("cannot open %s as display file", buf);
4764996Sjkh    }
4774996Sjkh
478327Sjkh    goto success;
479327Sjkh
4807998Sjkh bomb:
4817998Sjkh    code = 1;
4827998Sjkh    goto success;
4837998Sjkh
484327Sjkh fail:
4854996Sjkh    /* Nuke the whole (installed) show, XXX but don't clean directories */
486327Sjkh    if (!Fake)
4874996Sjkh	delete_package(FALSE, FALSE, &Plist);
488327Sjkh
489327Sjkh success:
490327Sjkh    /* delete the packing list contents */
49111780Sjkh    free_plist(&Plist);
49233427Sjkh    leave_playpen();
493327Sjkh    return code;
494327Sjkh}
495327Sjkh
496327Sjkhstatic int
497327Sjkhsanity_check(char *pkg)
498327Sjkh{
4998075Sjkh    int code = 0;
5008075Sjkh
501327Sjkh    if (!fexists(CONTENTS_FNAME)) {
50230221Scharnier	warnx("package %s has no CONTENTS file!", pkg);
5038075Sjkh	code = 1;
504327Sjkh    }
5058075Sjkh    else if (!fexists(COMMENT_FNAME)) {
50630221Scharnier	warnx("package %s has no COMMENT file!", pkg);
5078075Sjkh	code = 1;
508327Sjkh    }
5098075Sjkh    else if (!fexists(DESC_FNAME)) {
51030221Scharnier	warnx("package %s has no DESC file!", pkg);
5118075Sjkh	code = 1;
512327Sjkh    }
5138075Sjkh    return code;
514327Sjkh}
515327Sjkh
516327Sjkhvoid
51739068Sjkhcleanup(int sig)
518327Sjkh{
51933427Sjkh    static int in_cleanup = 0;
52033427Sjkh
52133427Sjkh    if (!in_cleanup) {
52233427Sjkh	in_cleanup = 1;
52339068Sjkh    	if (sig)
52439068Sjkh	    printf("Signal %d received, cleaning up..\n", sig);
52537728Seivind    	if (!Fake && zapLogDir && LogDir[0])
52639068Sjkh	    vsystem("%s -rf %s", REMOVE_CMD, LogDir);
52733427Sjkh    	leave_playpen();
52833427Sjkh    }
52939068Sjkh    if (sig)
53039068Sjkh	exit(1);
531327Sjkh}
532