perform.c revision 93520
1/*
2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * Jordan K. Hubbard
15 * 18 July 1993
16 *
17 * This is the main body of the add module.
18 *
19 */
20
21#include <sys/cdefs.h>
22__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/add/perform.c 93520 2002-04-01 09:39:07Z obrien $");
23
24#include <err.h>
25#include <paths.h>
26#include "lib.h"
27#include "add.h"
28
29#include <libgen.h>
30#include <signal.h>
31#include <sys/wait.h>
32
33static int pkg_do(char *);
34static int sanity_check(char *);
35static char LogDir[FILENAME_MAX];
36static int zapLogDir;		/* Should we delete LogDir? */
37
38int
39pkg_perform(char **pkgs)
40{
41    int i, err_cnt = 0;
42
43    signal(SIGINT, cleanup);
44    signal(SIGHUP, cleanup);
45
46    if (AddMode == SLAVE)
47	err_cnt = pkg_do(NULL);
48    else {
49	for (i = 0; pkgs[i]; i++)
50	    err_cnt += pkg_do(pkgs[i]);
51    }
52    return err_cnt;
53}
54
55static Package Plist;
56static char *Home;
57
58/*
59 * This is seriously ugly code following.  Written very fast!
60 * [And subsequently made even worse..  Sigh!  This code was just born
61 * to be hacked, I guess.. :) -jkh]
62 */
63static int
64pkg_do(char *pkg)
65{
66    char pkg_fullname[FILENAME_MAX];
67    char playpen[FILENAME_MAX];
68    char extract_contents[FILENAME_MAX];
69    char *where_to, *extract;
70    FILE *cfile;
71    int code;
72    PackingList p;
73    struct stat sb;
74    int inPlace;
75    /* support for separate pre/post install scripts */
76    int new_m = 0;
77    char pre_script[FILENAME_MAX] = INSTALL_FNAME;
78    char post_script[FILENAME_MAX];
79    char pre_arg[FILENAME_MAX], post_arg[FILENAME_MAX];
80
81    code = 0;
82    zapLogDir = 0;
83    LogDir[0] = '\0';
84    strcpy(playpen, FirstPen);
85    inPlace = 0;
86
87    /* Are we coming in for a second pass, everything already extracted? */
88    if (!pkg) {
89	fgets(playpen, FILENAME_MAX, stdin);
90	playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */
91	if (chdir(playpen) == FAIL) {
92	    warnx("pkg_add in SLAVE mode can't chdir to %s", playpen);
93	    return 1;
94	}
95	read_plist(&Plist, stdin);
96	where_to = playpen;
97    }
98    /* Nope - do it now */
99    else {
100	/* Is it an ftp://foo.bar.baz/file.tgz specification? */
101	if (isURL(pkg)) {
102	    if (!(Home = fileGetURL(NULL, pkg))) {
103		warnx("unable to fetch '%s' by URL", pkg);
104		return 1;
105	    }
106	    where_to = Home;
107	    strcpy(pkg_fullname, pkg);
108	    cfile = fopen(CONTENTS_FNAME, "r");
109	    if (!cfile) {
110		warnx(
111		"unable to open table of contents file '%s' - not a package?",
112		CONTENTS_FNAME);
113		goto bomb;
114	    }
115	    read_plist(&Plist, cfile);
116	    fclose(cfile);
117	}
118	else {
119	    strcpy(pkg_fullname, pkg);		/*
120						 * Copy for sanity's sake,
121						 * could remove pkg_fullname
122						 */
123	    if (strcmp(pkg, "-")) {
124		if (stat(pkg_fullname, &sb) == FAIL) {
125		    warnx("can't stat package file '%s'", pkg_fullname);
126		    goto bomb;
127		}
128		sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
129		extract = extract_contents;
130	    }
131	    else {
132		extract = NULL;
133		sb.st_size = 100000;	/* Make up a plausible average size */
134	    }
135	    Home = make_playpen(playpen, sb.st_size * 4);
136	    if (!Home)
137		errx(1, "unable to make playpen for %qd bytes", (long long)sb.st_size * 4);
138	    where_to = Home;
139	    /* Since we can call ourselves recursively, keep notes on where we came from */
140	    if (!getenv("_TOP"))
141		setenv("_TOP", Home, 1);
142	    if (unpack(pkg_fullname, extract)) {
143		warnx(
144	"unable to extract table of contents file from '%s' - not a package?",
145		pkg_fullname);
146		goto bomb;
147	    }
148	    cfile = fopen(CONTENTS_FNAME, "r");
149	    if (!cfile) {
150		warnx(
151	"unable to open table of contents file '%s' - not a package?",
152		CONTENTS_FNAME);
153		goto bomb;
154	    }
155	    read_plist(&Plist, cfile);
156	    fclose(cfile);
157
158	    /* Extract directly rather than moving?  Oh goodie! */
159	    if (find_plist_option(&Plist, "extract-in-place")) {
160		if (Verbose)
161		    printf("Doing in-place extraction for %s\n", pkg_fullname);
162		p = find_plist(&Plist, PLIST_CWD);
163		if (p) {
164		    if (!isdir(p->name) && !Fake) {
165			if (Verbose)
166			    printf("Desired prefix of %s does not exist, creating..\n", p->name);
167			vsystem("mkdir -p %s", p->name);
168			if (chdir(p->name) == -1) {
169			    warn("unable to change directory to '%s'", p->name);
170			    goto bomb;
171			}
172		    }
173		    where_to = p->name;
174		    inPlace = 1;
175		}
176		else {
177		    warnx(
178		"no prefix specified in '%s' - this is a bad package!",
179			pkg_fullname);
180		    goto bomb;
181		}
182	    }
183
184	    /*
185	     * Apply a crude heuristic to see how much space the package will
186	     * take up once it's unpacked.  I've noticed that most packages
187	     * compress an average of 75%, so multiply by 4 for good measure.
188	     */
189
190	    if (!inPlace && min_free(playpen) < sb.st_size * 4) {
191		warnx("projected size of %qd exceeds available free space.\n"
192"Please set your PKG_TMPDIR variable to point to a location with more\n"
193		       "free space and try again", (long long)sb.st_size * 4);
194		warnx("not extracting %s\ninto %s, sorry!",
195			pkg_fullname, where_to);
196		goto bomb;
197	    }
198
199	    /* If this is a direct extract and we didn't want it, stop now */
200	    if (inPlace && Fake)
201		goto success;
202
203	    /* Finally unpack the whole mess */
204	    if (unpack(pkg_fullname, NULL)) {
205		warnx("unable to extract '%s'!", pkg_fullname);
206		goto bomb;
207	    }
208	}
209
210	/* Check for sanity and dependencies */
211	if (sanity_check(pkg))
212	    goto bomb;
213
214	/* If we're running in MASTER mode, just output the plist and return */
215	if (AddMode == MASTER) {
216	    printf("%s\n", where_playpen());
217	    write_plist(&Plist, stdout);
218	    return 0;
219	}
220    }
221
222    /*
223     * If we have a prefix, delete the first one we see and add this
224     * one in place of it.
225     */
226    if (Prefix) {
227	delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
228	add_plist_top(&Plist, PLIST_CWD, Prefix);
229    }
230
231    setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1);
232    /* Protect against old packages with bogus @name fields */
233    (const char *)PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous";
234
235    /* See if we're already registered */
236    sprintf(LogDir, "%s/%s", LOG_DIR, PkgName);
237    if (isdir(LogDir) && !Force) {
238	warnx("package '%s' already recorded as installed", PkgName);
239	code = 1;
240	goto success;	/* close enough for government work */
241    }
242
243    /* Now check the packing list for dependencies */
244    for (p = Plist.head; p ; p = p->next) {
245	if (p->type != PLIST_PKGDEP)
246	    continue;
247	if (Verbose)
248	    printf("Package '%s' depends on '%s'.\n", PkgName, p->name);
249	if (vsystem("pkg_info -e %s", p->name)) {
250	    char path[FILENAME_MAX], *cp = NULL;
251
252	    if (!Fake) {
253		if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) {
254		    snprintf(path, FILENAME_MAX, "%s/%s.tgz", getenv("_TOP"), p->name);
255		    if (fexists(path))
256			cp = path;
257		    else
258			cp = fileFindByPath(pkg, p->name);
259		    if (cp) {
260			if (Verbose)
261			    printf("Loading it from %s.\n", cp);
262			if (vsystem("pkg_add %s'%s'", Verbose ? "-v " : "", cp)) {
263			    warnx("autoload of dependency '%s' failed%s",
264				cp, Force ? " (proceeding anyway)" : "!");
265			    if (!Force)
266				++code;
267			}
268		    }
269		    else {
270			warnx("could not find package %s %s",
271			      p->name, Force ? " (proceeding anyway)" : "!");
272			if (!Force)
273			    ++code;
274		    }
275		}
276		else if ((cp = fileGetURL(pkg, p->name)) != NULL) {
277		    if (Verbose)
278			printf("Finished loading %s over FTP.\n", p->name);
279		    if (!fexists("+CONTENTS")) {
280			warnx("autoloaded package %s has no +CONTENTS file?",
281				p->name);
282			if (!Force)
283			    ++code;
284		    }
285		    else if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S", Verbose ? "-v " : "")) {
286			warnx("pkg_add of dependency '%s' failed%s",
287				p->name, Force ? " (proceeding anyway)" : "!");
288			if (!Force)
289			    ++code;
290		    }
291		    else if (Verbose)
292			printf("\t'%s' loaded successfully.\n", p->name);
293		    /* Nuke the temporary playpen */
294		    leave_playpen();
295		}
296	    }
297	    else {
298		if (Verbose)
299		    printf("and was not found%s.\n", Force ? " (proceeding anyway)" : "");
300		else
301		    printf("Package dependency %s for %s not found%s\n", p->name, pkg,
302			   Force ? " (proceeding anyway)" : "!");
303		if (!Force)
304		    ++code;
305	    }
306	}
307	else if (Verbose)
308	    printf(" - already installed.\n");
309    }
310
311    if (code != 0)
312	goto bomb;
313
314    /* Look for the requirements file */
315    if (fexists(REQUIRE_FNAME)) {
316	vsystem("chmod +x %s", REQUIRE_FNAME);	/* be sure */
317	if (Verbose)
318	    printf("Running requirements file first for %s..\n", PkgName);
319	if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) {
320	    warnx("package %s fails requirements %s", pkg_fullname,
321		   Force ? "installing anyway" : "- not installed");
322	    if (!Force) {
323		code = 1;
324		goto success;	/* close enough for government work */
325	    }
326	}
327    }
328
329    /*
330     * Test whether to use the old method of passing tokens to installation
331     * scripts, and set appropriate variables..
332     */
333
334    if (fexists(POST_INSTALL_FNAME)) {
335	new_m = 1;
336	sprintf(post_script, "%s", POST_INSTALL_FNAME);
337	pre_arg[0] = '\0';
338	post_arg[0] = '\0';
339    } else {
340	if (fexists(INSTALL_FNAME)) {
341	    sprintf(post_script, "%s", INSTALL_FNAME);
342	    sprintf(pre_arg, "PRE-INSTALL");
343	    sprintf(post_arg, "POST-INSTALL");
344	}
345    }
346
347    /* If we're really installing, and have an installation file, run it */
348    if (!NoInstall && fexists(pre_script)) {
349	vsystem("chmod +x %s", pre_script);	/* make sure */
350	if (Verbose)
351	    printf("Running pre-install for %s..\n", PkgName);
352	if (!Fake && vsystem("./%s %s %s", pre_script, PkgName, pre_arg)) {
353	    warnx("install script returned error status");
354	    unlink(pre_script);
355	    code = 1;
356	    goto success;		/* nothing to uninstall yet */
357	}
358    }
359
360    /* Now finally extract the entire show if we're not going direct */
361    if (!inPlace && !Fake)
362	extract_plist(".", &Plist);
363
364    if (!Fake && fexists(MTREE_FNAME)) {
365	if (Verbose)
366	    printf("Running mtree for %s..\n", PkgName);
367	p = find_plist(&Plist, PLIST_CWD);
368	if (Verbose)
369	    printf("mtree -U -f %s -d -e -p %s >%s\n", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL);
370	if (!Fake) {
371	    if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s >%s", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL))
372		warnx("mtree returned a non-zero status - continuing");
373	}
374    }
375
376    /* Run the installation script one last time? */
377    if (!NoInstall && fexists(post_script)) {
378	vsystem("chmod +x %s", post_script);	/* make sure */
379	if (Verbose)
380	    printf("Running post-install for %s..\n", PkgName);
381	if (!Fake && vsystem("./%s %s %s", post_script, PkgName, post_arg)) {
382	    warnx("install script returned error status");
383	    unlink(post_script);
384	    code = 1;
385	    goto fail;
386	}
387    }
388
389    /* Time to record the deed? */
390    if (!NoRecord && !Fake) {
391	char contents[FILENAME_MAX];
392	FILE *contfile;
393
394	if (getuid() != 0)
395	    warnx("not running as root - trying to record install anyway");
396	if (!PkgName) {
397	    warnx("no package name! can't record package, sorry");
398	    code = 1;
399	    goto success;	/* well, partial anyway */
400	}
401	sprintf(LogDir, "%s/%s", LOG_DIR, PkgName);
402	zapLogDir = 1;
403	if (Verbose)
404	    printf("Attempting to record package into %s..\n", LogDir);
405	if (make_hierarchy(LogDir)) {
406	    warnx("can't record package into '%s', you're on your own!",
407		   LogDir);
408	    bzero(LogDir, FILENAME_MAX);
409	    code = 1;
410	    goto success;	/* close enough for government work */
411	}
412	/* Make sure pkg_info can read the entry */
413	vsystem("chmod a+rx %s", LogDir);
414	move_file(".", DESC_FNAME, LogDir);
415	move_file(".", COMMENT_FNAME, LogDir);
416	if (fexists(INSTALL_FNAME))
417	    move_file(".", INSTALL_FNAME, LogDir);
418	if (fexists(POST_INSTALL_FNAME))
419	    move_file(".", POST_INSTALL_FNAME, LogDir);
420	if (fexists(DEINSTALL_FNAME))
421	    move_file(".", DEINSTALL_FNAME, LogDir);
422	if (fexists(POST_DEINSTALL_FNAME))
423	    move_file(".", POST_DEINSTALL_FNAME, LogDir);
424	if (fexists(REQUIRE_FNAME))
425	    move_file(".", REQUIRE_FNAME, LogDir);
426	if (fexists(DISPLAY_FNAME))
427	    move_file(".", DISPLAY_FNAME, LogDir);
428	if (fexists(MTREE_FNAME))
429	    move_file(".", MTREE_FNAME, LogDir);
430	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
431	contfile = fopen(contents, "w");
432	if (!contfile) {
433	    warnx("can't open new contents file '%s'! can't register pkg",
434		contents);
435	    goto success; /* can't log, but still keep pkg */
436	}
437	write_plist(&Plist, contfile);
438	fclose(contfile);
439	for (p = Plist.head; p ; p = p->next) {
440	    if (p->type != PLIST_PKGDEP)
441		continue;
442	    if (Verbose)
443		printf("Attempting to record dependency on package '%s'\n", p->name);
444	    sprintf(contents, "%s/%s/%s", LOG_DIR, basename(p->name),
445	    	    REQUIRED_BY_FNAME);
446	    contfile = fopen(contents, "a");
447	    if (!contfile)
448		warnx("can't open dependency file '%s'!\n"
449		       "dependency registration is incomplete", contents);
450	    else {
451		fprintf(contfile, "%s\n", PkgName);
452		if (fclose(contfile) == EOF)
453		    warnx("cannot properly close file %s", contents);
454	    }
455	}
456	if (Verbose)
457	    printf("Package %s registered in %s\n", PkgName, LogDir);
458    }
459
460    if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) {
461	FILE *fp;
462	char buf[BUFSIZ];
463
464	snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name);
465	fp = fopen(buf, "r");
466	if (fp) {
467	    putc('\n', stdout);
468	    while (fgets(buf, sizeof(buf), fp))
469		fputs(buf, stdout);
470	    putc('\n', stdout);
471	    (void) fclose(fp);
472	} else
473	    warnx("cannot open %s as display file", buf);
474    }
475
476    goto success;
477
478 bomb:
479    code = 1;
480    goto success;
481
482 fail:
483    /* Nuke the whole (installed) show, XXX but don't clean directories */
484    if (!Fake)
485	delete_package(FALSE, FALSE, &Plist);
486
487 success:
488    /* delete the packing list contents */
489    free_plist(&Plist);
490    leave_playpen();
491    return code;
492}
493
494static int
495sanity_check(char *pkg)
496{
497    int code = 0;
498
499    if (!fexists(CONTENTS_FNAME)) {
500	warnx("package %s has no CONTENTS file!", pkg);
501	code = 1;
502    }
503    else if (!fexists(COMMENT_FNAME)) {
504	warnx("package %s has no COMMENT file!", pkg);
505	code = 1;
506    }
507    else if (!fexists(DESC_FNAME)) {
508	warnx("package %s has no DESC file!", pkg);
509	code = 1;
510    }
511    return code;
512}
513
514void
515cleanup(int sig)
516{
517    static int in_cleanup = 0;
518
519    if (!in_cleanup) {
520	in_cleanup = 1;
521    	if (sig)
522	    printf("Signal %d received, cleaning up..\n", sig);
523    	if (!Fake && zapLogDir && LogDir[0])
524	    vsystem("%s -rf %s", REMOVE_CMD, LogDir);
525    	leave_playpen();
526    }
527    if (sig)
528	exit(1);
529}
530