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