perform.c revision 81699
1#ifndef lint
2static const char rcsid[] =
3  "$FreeBSD: head/usr.sbin/pkg_install/add/perform.c 81699 2001-08-15 14:22:01Z sobomax $";
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", 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	if (getuid() != 0)
399	    warnx("not running as root - trying to record install anyway");
400	if (!PkgName) {
401	    warnx("no package name! can't record package, sorry");
402	    code = 1;
403	    goto success;	/* well, partial anyway */
404	}
405	sprintf(LogDir, "%s/%s", LOG_DIR, PkgName);
406	zapLogDir = 1;
407	if (Verbose)
408	    printf("Attempting to record package into %s..\n", LogDir);
409	if (make_hierarchy(LogDir)) {
410	    warnx("can't record package into '%s', you're on your own!",
411		   LogDir);
412	    bzero(LogDir, FILENAME_MAX);
413	    code = 1;
414	    goto success;	/* close enough for government work */
415	}
416	/* Make sure pkg_info can read the entry */
417	vsystem("chmod a+rx %s", LogDir);
418	if (fexists(DEINSTALL_FNAME))
419	    move_file(".", DEINSTALL_FNAME, LogDir);
420	if (fexists(POST_DEINSTALL_FNAME))
421	    move_file(".", POST_DEINSTALL_FNAME, LogDir);
422	if (fexists(REQUIRE_FNAME))
423	    move_file(".", REQUIRE_FNAME, LogDir);
424	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
425	cfile = fopen(contents, "w");
426	if (!cfile) {
427	    warnx("can't open new contents file '%s'! can't register pkg",
428		contents);
429	    goto success; /* can't log, but still keep pkg */
430	}
431	write_plist(&Plist, cfile);
432	fclose(cfile);
433	move_file(".", DESC_FNAME, LogDir);
434	move_file(".", COMMENT_FNAME, LogDir);
435	if (fexists(DISPLAY_FNAME))
436	    move_file(".", DISPLAY_FNAME, LogDir);
437	for (p = Plist.head; p ; p = p->next) {
438	    if (p->type != PLIST_PKGDEP)
439		continue;
440	    if (Verbose)
441		printf("Attempting to record dependency on package '%s'\n", p->name);
442	    sprintf(contents, "%s/%s/%s", LOG_DIR, basename(p->name),
443	    	    REQUIRED_BY_FNAME);
444	    cfile = fopen(contents, "a");
445	    if (!cfile)
446		warnx("can't open dependency file '%s'!\n"
447		       "dependency registration is incomplete", contents);
448	    else {
449		fprintf(cfile, "%s\n", PkgName);
450		if (fclose(cfile) == EOF)
451		    warnx("cannot properly close file %s", contents);
452	    }
453	}
454	if (Verbose)
455	    printf("Package %s registered in %s\n", PkgName, LogDir);
456    }
457
458    if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) {
459	FILE *fp;
460	char buf[BUFSIZ];
461
462	snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name);
463	fp = fopen(buf, "r");
464	if (fp) {
465	    putc('\n', stdout);
466	    while (fgets(buf, sizeof(buf), fp))
467		fputs(buf, stdout);
468	    putc('\n', stdout);
469	    (void) fclose(fp);
470	} else
471	    warnx("cannot open %s as display file", buf);
472    }
473
474    goto success;
475
476 bomb:
477    code = 1;
478    goto success;
479
480 fail:
481    /* Nuke the whole (installed) show, XXX but don't clean directories */
482    if (!Fake)
483	delete_package(FALSE, FALSE, &Plist);
484
485 success:
486    /* delete the packing list contents */
487    free_plist(&Plist);
488    leave_playpen();
489    return code;
490}
491
492static int
493sanity_check(char *pkg)
494{
495    int code = 0;
496
497    if (!fexists(CONTENTS_FNAME)) {
498	warnx("package %s has no CONTENTS file!", pkg);
499	code = 1;
500    }
501    else if (!fexists(COMMENT_FNAME)) {
502	warnx("package %s has no COMMENT file!", pkg);
503	code = 1;
504    }
505    else if (!fexists(DESC_FNAME)) {
506	warnx("package %s has no DESC file!", pkg);
507	code = 1;
508    }
509    return code;
510}
511
512void
513cleanup(int sig)
514{
515    static int in_cleanup = 0;
516
517    if (!in_cleanup) {
518	in_cleanup = 1;
519    	if (sig)
520	    printf("Signal %d received, cleaning up..\n", sig);
521    	if (!Fake && zapLogDir && LogDir[0])
522	    vsystem("%s -rf %s", REMOVE_CMD, LogDir);
523    	leave_playpen();
524    }
525    if (sig)
526	exit(1);
527}
528