perform.c revision 154145
178064Sume/*
278064Sume * FreeBSD install - a package for the installation and maintainance
378064Sume * of non-core utilities.
478064Sume *
578064Sume * Redistribution and use in source and binary forms, with or without
678064Sume * modification, are permitted provided that the following conditions
778064Sume * are met:
878064Sume * 1. Redistributions of source code must retain the above copyright
978064Sume *    notice, this list of conditions and the following disclaimer.
1078064Sume * 2. Redistributions in binary form must reproduce the above copyright
1178064Sume *    notice, this list of conditions and the following disclaimer in the
1278064Sume *    documentation and/or other materials provided with the distribution.
1378064Sume *
1478064Sume * Jordan K. Hubbard
1578064Sume * 18 July 1993
1678064Sume *
1778064Sume * This is the main body of the add module.
1878064Sume *
1978064Sume */
2078064Sume
2178064Sume#include <sys/cdefs.h>
2278064Sume__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/add/perform.c 154145 2006-01-09 18:27:21Z flz $");
2378064Sume
2478064Sume#include <err.h>
2578064Sume#include <paths.h>
2678064Sume#include "lib.h"
2778064Sume#include "add.h"
2878064Sume
2978064Sume#include <libgen.h>
3078064Sume#include <signal.h>
3178064Sume#include <sys/wait.h>
3278064Sume
3378064Sumestatic int pkg_do(char *);
3478064Sumestatic int sanity_check(char *);
3578064Sumestatic char LogDir[FILENAME_MAX];
3678064Sumestatic int zapLogDir;		/* Should we delete LogDir? */
3778064Sume
3878064Sumeint
3978064Sumepkg_perform(char **pkgs)
4078064Sume{
4178064Sume    int i, err_cnt = 0;
4278064Sume
4378064Sume    signal(SIGINT, cleanup);
4478064Sume    signal(SIGHUP, cleanup);
4578064Sume
4678064Sume    if (AddMode == SLAVE)
4778064Sume	err_cnt = pkg_do(NULL);
4878064Sume    else {
4978064Sume	for (i = 0; pkgs[i]; i++)
5078064Sume	    err_cnt += pkg_do(pkgs[i]);
5178064Sume    }
5278064Sume    return err_cnt;
5378064Sume}
5478064Sume
5578064Sumestatic Package Plist;
5678064Sumestatic char *Home;
5778064Sume
5878064Sume/*
5978064Sume * This is seriously ugly code following.  Written very fast!
6078064Sume * [And subsequently made even worse..  Sigh!  This code was just born
6178064Sume * to be hacked, I guess.. :) -jkh]
6278064Sume */
6378064Sumestatic int
6478064Sumepkg_do(char *pkg)
6578064Sume{
6678064Sume    char pkg_fullname[FILENAME_MAX];
6778064Sume    char playpen[FILENAME_MAX];
6878064Sume    char extract_contents[FILENAME_MAX];
6978064Sume    char *where_to, *extract;
7078064Sume    FILE *cfile;
7178064Sume    int code;
7278064Sume    PackingList p;
7378064Sume    struct stat sb;
7478064Sume    int inPlace, conflictsfound, errcode;
7578064Sume    /* support for separate pre/post install scripts */
7678064Sume    int new_m = 0;
7778064Sume    char pre_script[FILENAME_MAX] = INSTALL_FNAME;
7878064Sume    char post_script[FILENAME_MAX];
79127141Sru    char pre_arg[FILENAME_MAX], post_arg[FILENAME_MAX];
8078064Sume    char *conflict[2];
8178064Sume    char **matched;
8278064Sume
83124524Sume    conflictsfound = 0;
8478064Sume    code = 0;
8578064Sume    zapLogDir = 0;
8678064Sume    LogDir[0] = '\0';
8778064Sume    strcpy(playpen, FirstPen);
8878064Sume    inPlace = 0;
8978064Sume
90124524Sume    /* Are we coming in for a second pass, everything already extracted? */
9178064Sume    if (!pkg) {
9278064Sume	fgets(playpen, FILENAME_MAX, stdin);
9378064Sume	playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */
9478064Sume	if (chdir(playpen) == FAIL) {
9578064Sume	    warnx("pkg_add in SLAVE mode can't chdir to %s", playpen);
9678064Sume	    return 1;
9778064Sume	}
9878064Sume	read_plist(&Plist, stdin);
9978064Sume	where_to = playpen;
10078064Sume    }
10178064Sume    /* Nope - do it now */
10278064Sume    else {
10378064Sume	/* Is it an ftp://foo.bar.baz/file.t[bg]z specification? */
10478064Sume	if (isURL(pkg)) {
10578064Sume	    if (!(Home = fileGetURL(NULL, pkg, KeepPackage))) {
10678064Sume		warnx("unable to fetch '%s' by URL", pkg);
10778064Sume		return 1;
10878064Sume	    }
10978064Sume	    where_to = Home;
11078064Sume	    strcpy(pkg_fullname, pkg);
11178064Sume	    cfile = fopen(CONTENTS_FNAME, "r");
11278064Sume	    if (!cfile) {
11378064Sume		warnx(
114118660Sume		"unable to open table of contents file '%s' - not a package?",
11578064Sume		CONTENTS_FNAME);
11678064Sume		goto bomb;
11778064Sume	    }
11878064Sume	    read_plist(&Plist, cfile);
11978064Sume	    fclose(cfile);
12078064Sume	}
12178064Sume	else {
12278064Sume	    strcpy(pkg_fullname, pkg);		/*
123118660Sume						 * Copy for sanity's sake,
12478064Sume						 * could remove pkg_fullname
12578064Sume						 */
12678064Sume	    if (strcmp(pkg, "-")) {
12778064Sume		if (stat(pkg_fullname, &sb) == FAIL) {
12878064Sume		    warnx("can't stat package file '%s'", pkg_fullname);
12978064Sume		    goto bomb;
13078064Sume		}
13178064Sume		sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
13278064Sume		extract = extract_contents;
13378064Sume	    }
13478064Sume	    else {
13578064Sume		extract = NULL;
13678064Sume		sb.st_size = 100000;	/* Make up a plausible average size */
13778064Sume	    }
138124524Sume	    Home = make_playpen(playpen, sb.st_size * 4);
13978064Sume	    if (!Home)
14078064Sume		errx(1, "unable to make playpen for %lld bytes", (long long)sb.st_size * 4);
14178064Sume	    where_to = Home;
14278064Sume	    /* Since we can call ourselves recursively, keep notes on where we came from */
14378064Sume	    if (!getenv("_TOP"))
14478064Sume		setenv("_TOP", Home, 1);
14578064Sume	    if (unpack(pkg_fullname, extract)) {
14678064Sume		warnx(
14778064Sume	"unable to extract table of contents file from '%s' - not a package?",
14878064Sume		pkg_fullname);
14978064Sume		goto bomb;
15078064Sume	    }
15178064Sume	    cfile = fopen(CONTENTS_FNAME, "r");
15278064Sume	    if (!cfile) {
15378064Sume		warnx(
15478064Sume	"unable to open table of contents file '%s' - not a package?",
155118660Sume		CONTENTS_FNAME);
15678064Sume		goto bomb;
15778064Sume	    }
15878064Sume	    read_plist(&Plist, cfile);
159118660Sume	    fclose(cfile);
16078064Sume
16178064Sume	    /* Extract directly rather than moving?  Oh goodie! */
16278064Sume	    if (find_plist_option(&Plist, "extract-in-place")) {
16378064Sume		if (Verbose)
164118660Sume		    printf("Doing in-place extraction for %s\n", pkg_fullname);
16578064Sume		p = find_plist(&Plist, PLIST_CWD);
16678064Sume		if (p) {
16778064Sume		    if (!isdir(p->name) && !Fake) {
16878064Sume			if (Verbose)
16978064Sume			    printf("Desired prefix of %s does not exist, creating..\n", p->name);
17078064Sume			vsystem("/bin/mkdir -p %s", p->name);
17178064Sume			if (chdir(p->name) == -1) {
17278064Sume			    warn("unable to change directory to '%s'", p->name);
17378064Sume			    goto bomb;
17478064Sume			}
17578064Sume		    }
176		    where_to = p->name;
177		    inPlace = 1;
178		}
179		else {
180		    warnx(
181		"no prefix specified in '%s' - this is a bad package!",
182			pkg_fullname);
183		    goto bomb;
184		}
185	    }
186
187	    /*
188	     * Apply a crude heuristic to see how much space the package will
189	     * take up once it's unpacked.  I've noticed that most packages
190	     * compress an average of 75%, so multiply by 4 for good measure.
191	     */
192
193	    if (!extract && !inPlace && min_free(playpen) < sb.st_size * 4) {
194		warnx("projected size of %lld exceeds available free space.\n"
195"Please set your PKG_TMPDIR variable to point to a location with more\n"
196		       "free space and try again", (long long)sb.st_size * 4);
197		warnx("not extracting %s\ninto %s, sorry!",
198			pkg_fullname, where_to);
199		goto bomb;
200	    }
201
202	    /* If this is a direct extract and we didn't want it, stop now */
203	    if (inPlace && Fake)
204		goto success;
205
206	    /* Finally unpack the whole mess.  If extract is null we
207	       already + did so so don't bother doing it again. */
208	    if (extract && unpack(pkg_fullname, NULL)) {
209		warnx("unable to extract '%s'!", pkg_fullname);
210		goto bomb;
211	    }
212	}
213
214	/* Check for sanity and dependencies */
215	if (sanity_check(pkg))
216	    goto bomb;
217
218	/* If we're running in MASTER mode, just output the plist and return */
219	if (AddMode == MASTER) {
220	    printf("%s\n", where_playpen());
221	    write_plist(&Plist, stdout);
222	    return 0;
223	}
224    }
225
226    /*
227     * If we have a prefix, delete the first one we see and add this
228     * one in place of it.
229     */
230    if (Prefix) {
231	delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
232	add_plist_top(&Plist, PLIST_CWD, Prefix);
233    }
234
235    setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1);
236    /* Protect against old packages with bogus @name and origin fields */
237    if (Plist.name == NULL)
238	Plist.name = "anonymous";
239    if (Plist.origin == NULL)
240	Plist.origin = "anonymous/anonymous";
241
242    /*
243     * See if we're already registered either with the same name (the same
244     * version) or some other version with the same origin.
245     */
246    if ((isinstalledpkg(Plist.name) > 0 ||
247         matchbyorigin(Plist.origin, NULL) != NULL) && !Force) {
248	warnx("package '%s' or its older version already installed",
249	      Plist.name);
250	code = 1;
251	goto success;	/* close enough for government work */
252    }
253
254    /* Now check the packing list for conflicts */
255    for (p = Plist.head; p != NULL; p = p->next) {
256	if (p->type == PLIST_CONFLICTS) {
257	    int i;
258	    conflict[0] = strdup(p->name);
259	    conflict[1] = NULL;
260	    matched = matchinstalled(MATCH_GLOB, conflict, &errcode);
261	    free(conflict[0]);
262	    if (errcode == 0 && matched != NULL)
263		for (i = 0; matched[i] != NULL; i++)
264		    if (isinstalledpkg(matched[i]) > 0) {
265			warnx("package '%s' conflicts with %s", Plist.name,
266				matched[i]);
267			conflictsfound = 1;
268		    }
269
270	    continue;
271	}
272    }
273    if(conflictsfound) {
274	if(!Force) {
275	    warnx("please use pkg_delete first to remove conflicting package(s) or -f to force installation");
276	    code = 1;
277	    goto bomb;
278	} else
279	    warnx("-f specified; proceeding anyway");
280    }
281
282    /* Now check the packing list for dependencies */
283    for (p = Plist.head; p ; p = p->next) {
284	char *deporigin;
285
286	if (p->type != PLIST_PKGDEP)
287	    continue;
288	deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : NULL;
289	if (Verbose) {
290	    printf("Package '%s' depends on '%s'", Plist.name, p->name);
291	    if (deporigin != NULL)
292		printf(" with '%s' origin", deporigin);
293	    printf(".\n");
294	}
295	if (isinstalledpkg(p->name) <= 0 &&
296	    !(deporigin != NULL && matchbyorigin(deporigin, NULL) != NULL)) {
297	    char path[FILENAME_MAX], *cp = NULL;
298
299	    if (!Fake) {
300		char prefixArg[2 + MAXPATHLEN]; /* "-P" + Prefix */
301		if (PrefixRecursive) {
302		    strlcpy(prefixArg, "-P", sizeof(prefixArg));
303		    strlcat(prefixArg, Prefix, sizeof(prefixArg));
304		}
305		if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) {
306		    const char *ext;
307
308		    ext = strrchr(pkg_fullname, '.');
309		    if (ext == NULL)
310#if defined(__FreeBSD_version) && __FreeBSD_version >= 500039
311			ext = ".tbz";
312#else
313			ext = ".tgz";
314#endif
315		    snprintf(path, FILENAME_MAX, "%s/%s%s", getenv("_TOP"), p->name, ext);
316		    if (fexists(path))
317			cp = path;
318		    else
319			cp = fileFindByPath(pkg, p->name);
320		    if (cp) {
321			if (Verbose)
322			    printf("Loading it from %s.\n", cp);
323			if (vsystem("%s %s %s '%s'", PkgAddCmd, Verbose ? "-v " : "", PrefixRecursive ? prefixArg : "", cp)) {
324			    warnx("autoload of dependency '%s' failed%s",
325				cp, Force ? " (proceeding anyway)" : "!");
326			    if (!Force)
327				++code;
328			}
329		    }
330		    else {
331			warnx("could not find package %s %s",
332			      p->name, Force ? " (proceeding anyway)" : "!");
333			if (!Force)
334			    ++code;
335		    }
336		}
337		else if ((cp = fileGetURL(pkg, p->name, KeepPackage)) != NULL) {
338		    if (Verbose)
339			printf("Finished loading %s over FTP.\n", p->name);
340		    if (!fexists("+CONTENTS")) {
341			warnx("autoloaded package %s has no +CONTENTS file?",
342				p->name);
343			if (!Force)
344			    ++code;
345		    }
346		    else if (vsystem("(pwd; /bin/cat +CONTENTS) | %s %s %s -S", PkgAddCmd, Verbose ? "-v" : "", PrefixRecursive ? prefixArg : "")) {
347			warnx("pkg_add of dependency '%s' failed%s",
348				p->name, Force ? " (proceeding anyway)" : "!");
349			if (!Force)
350			    ++code;
351		    }
352		    else if (Verbose)
353			printf("\t'%s' loaded successfully.\n", p->name);
354		    /* Nuke the temporary playpen */
355		    leave_playpen();
356		}
357	    }
358	    else {
359		if (Verbose)
360		    printf("and was not found%s.\n", Force ? " (proceeding anyway)" : "");
361		else
362		    printf("Package dependency %s for %s not found%s\n", p->name, pkg,
363			   Force ? " (proceeding anyway)" : "!");
364		if (!Force)
365		    ++code;
366	    }
367	}
368	else if (Verbose)
369	    printf(" - already installed.\n");
370    }
371
372    if (code != 0)
373	goto bomb;
374
375    /* Look for the requirements file */
376    if (fexists(REQUIRE_FNAME)) {
377	vsystem("/bin/chmod +x %s", REQUIRE_FNAME);	/* be sure */
378	if (Verbose)
379	    printf("Running requirements file first for %s..\n", Plist.name);
380	if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, Plist.name)) {
381	    warnx("package %s fails requirements %s", pkg_fullname,
382		   Force ? "installing anyway" : "- not installed");
383	    if (!Force) {
384		code = 1;
385		goto success;	/* close enough for government work */
386	    }
387	}
388    }
389
390    /*
391     * Test whether to use the old method of passing tokens to installation
392     * scripts, and set appropriate variables..
393     */
394
395    if (fexists(POST_INSTALL_FNAME)) {
396	new_m = 1;
397	sprintf(post_script, "%s", POST_INSTALL_FNAME);
398	pre_arg[0] = '\0';
399	post_arg[0] = '\0';
400    } else {
401	if (fexists(INSTALL_FNAME)) {
402	    sprintf(post_script, "%s", INSTALL_FNAME);
403	    sprintf(pre_arg, "PRE-INSTALL");
404	    sprintf(post_arg, "POST-INSTALL");
405	}
406    }
407
408    /* If we're really installing, and have an installation file, run it */
409    if (!NoInstall && fexists(pre_script)) {
410	vsystem("/bin/chmod +x %s", pre_script);	/* make sure */
411	if (Verbose)
412	    printf("Running pre-install for %s..\n", Plist.name);
413	if (!Fake && vsystem("./%s %s %s", pre_script, Plist.name, pre_arg)) {
414	    warnx("install script returned error status");
415	    unlink(pre_script);
416	    code = 1;
417	    goto success;		/* nothing to uninstall yet */
418	}
419    }
420
421    /* Now finally extract the entire show if we're not going direct */
422    if (!inPlace && !Fake)
423	extract_plist(".", &Plist);
424
425    if (!Fake && fexists(MTREE_FNAME)) {
426	if (Verbose)
427	    printf("Running mtree for %s..\n", Plist.name);
428	p = find_plist(&Plist, PLIST_CWD);
429	if (Verbose)
430	    printf("mtree -U -f %s -d -e -p %s >%s\n", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL);
431	if (!Fake) {
432	    if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s >%s", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL))
433		warnx("mtree returned a non-zero status - continuing");
434	}
435    }
436
437    /* Run the installation script one last time? */
438    if (!NoInstall && fexists(post_script)) {
439	vsystem("/bin/chmod +x %s", post_script);	/* make sure */
440	if (Verbose)
441	    printf("Running post-install for %s..\n", Plist.name);
442	if (!Fake && vsystem("./%s %s %s", post_script, Plist.name, post_arg)) {
443	    warnx("install script returned error status");
444	    unlink(post_script);
445	    code = 1;
446	    goto fail;
447	}
448    }
449
450    /* Time to record the deed? */
451    if (!NoRecord && !Fake) {
452	char contents[FILENAME_MAX];
453	FILE *contfile;
454
455	if (getuid() != 0)
456	    warnx("not running as root - trying to record install anyway");
457	sprintf(LogDir, "%s/%s", LOG_DIR, Plist.name);
458	zapLogDir = 1;
459	if (Verbose)
460	    printf("Attempting to record package into %s..\n", LogDir);
461	if (make_hierarchy(LogDir)) {
462	    warnx("can't record package into '%s', you're on your own!",
463		   LogDir);
464	    bzero(LogDir, FILENAME_MAX);
465	    code = 1;
466	    goto success;	/* close enough for government work */
467	}
468	/* Make sure pkg_info can read the entry */
469	vsystem("/bin/chmod a+rx %s", LogDir);
470	move_file(".", DESC_FNAME, LogDir);
471	move_file(".", COMMENT_FNAME, LogDir);
472	if (fexists(INSTALL_FNAME))
473	    move_file(".", INSTALL_FNAME, LogDir);
474	if (fexists(POST_INSTALL_FNAME))
475	    move_file(".", POST_INSTALL_FNAME, LogDir);
476	if (fexists(DEINSTALL_FNAME))
477	    move_file(".", DEINSTALL_FNAME, LogDir);
478	if (fexists(POST_DEINSTALL_FNAME))
479	    move_file(".", POST_DEINSTALL_FNAME, LogDir);
480	if (fexists(REQUIRE_FNAME))
481	    move_file(".", REQUIRE_FNAME, LogDir);
482	if (fexists(DISPLAY_FNAME))
483	    move_file(".", DISPLAY_FNAME, LogDir);
484	if (fexists(MTREE_FNAME))
485	    move_file(".", MTREE_FNAME, LogDir);
486	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
487	contfile = fopen(contents, "w");
488	if (!contfile) {
489	    warnx("can't open new contents file '%s'! can't register pkg",
490		contents);
491	    goto success; /* can't log, but still keep pkg */
492	}
493	write_plist(&Plist, contfile);
494	fclose(contfile);
495	for (p = Plist.head; p ; p = p->next) {
496	    char *deporigin, **depnames;
497	    int i;
498
499	    if (p->type != PLIST_PKGDEP)
500		continue;
501	    deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name :
502							     NULL;
503	    if (Verbose) {
504		printf("Trying to record dependency on package '%s'", p->name);
505		if (deporigin != NULL)
506		    printf(" with '%s' origin", deporigin);
507		printf(".\n");
508	    }
509
510	    depnames = (deporigin != NULL) ? matchbyorigin(deporigin, NULL) :
511					     NULL;
512	    if (depnames == NULL) {
513		depnames = alloca(sizeof(*depnames) * 2);
514		depnames[0] = p->name;
515		depnames[1] = NULL;
516	    }
517	    for (i = 0; depnames[i] != NULL; i++) {
518		sprintf(contents, "%s/%s/%s", LOG_DIR, depnames[i],
519			REQUIRED_BY_FNAME);
520		if (strcmp(p->name, depnames[i]) != 0)
521		    warnx("warning: package '%s' requires '%s', but '%s' "
522			  "is installed", Plist.name, p->name, depnames[i]);
523		contfile = fopen(contents, "a");
524		if (!contfile)
525		    warnx("can't open dependency file '%s'!\n"
526			  "dependency registration is incomplete", contents);
527		else {
528		    fprintf(contfile, "%s\n", Plist.name);
529		    if (fclose(contfile) == EOF)
530			warnx("cannot properly close file %s", contents);
531		}
532	    }
533	}
534	if (Verbose)
535	    printf("Package %s registered in %s\n", Plist.name, LogDir);
536    }
537
538    if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) {
539	FILE *fp;
540	char buf[BUFSIZ];
541
542	snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name);
543	fp = fopen(buf, "r");
544	if (fp) {
545	    putc('\n', stdout);
546	    while (fgets(buf, sizeof(buf), fp))
547		fputs(buf, stdout);
548	    putc('\n', stdout);
549	    (void) fclose(fp);
550	} else {
551    	    if (!Fake) {
552		warnx("cannot open %s as display file", buf);
553	    }
554	}
555    }
556
557    goto success;
558
559 bomb:
560    code = 1;
561    goto success;
562
563 fail:
564    /* Nuke the whole (installed) show, XXX but don't clean directories */
565    if (!Fake)
566	delete_package(FALSE, FALSE, &Plist);
567
568 success:
569    /* delete the packing list contents */
570    free_plist(&Plist);
571    leave_playpen();
572    return code;
573}
574
575static int
576sanity_check(char *pkg)
577{
578    int code = 0;
579
580    if (!fexists(CONTENTS_FNAME)) {
581	warnx("package %s has no CONTENTS file!", pkg);
582	code = 1;
583    }
584    else if (!fexists(COMMENT_FNAME)) {
585	warnx("package %s has no COMMENT file!", pkg);
586	code = 1;
587    }
588    else if (!fexists(DESC_FNAME)) {
589	warnx("package %s has no DESC file!", pkg);
590	code = 1;
591    }
592    return code;
593}
594
595void
596cleanup(int sig)
597{
598    static int in_cleanup = 0;
599
600    if (!in_cleanup) {
601	in_cleanup = 1;
602    	if (sig)
603	    printf("Signal %d received, cleaning up..\n", sig);
604    	if (!Fake && zapLogDir && LogDir[0])
605	    vsystem("%s -rf %s", REMOVE_CMD, LogDir);
606    	leave_playpen();
607    }
608    if (sig)
609	exit(1);
610}
611