perform.c revision 32334
1139804Simp#ifndef lint
21541Srgrimesstatic const char rcsid[] =
31541Srgrimes	"$Id: perform.c,v 1.45 1997/10/18 05:54:14 jkh Exp $";
41541Srgrimes#endif
51541Srgrimes
61541Srgrimes/*
71541Srgrimes * FreeBSD install - a package for the installation and maintainance
81541Srgrimes * of non-core utilities.
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes *
191541Srgrimes * Jordan K. Hubbard
201541Srgrimes * 18 July 1993
211541Srgrimes *
221541Srgrimes * This is the main body of the add module.
231541Srgrimes *
241541Srgrimes */
251541Srgrimes
261541Srgrimes#include <err.h>
271541Srgrimes#include "lib.h"
281541Srgrimes#include "add.h"
291541Srgrimes
301541Srgrimes#include <signal.h>
311541Srgrimes#include <sys/wait.h>
321541Srgrimes
331541Srgrimesstatic int pkg_do(char *);
341541Srgrimesstatic int sanity_check(char *);
351541Srgrimesstatic char LogDir[FILENAME_MAX];
361541Srgrimes
37116182Sobrienint
38116182Sobrienpkg_perform(char **pkgs)
39116182Sobrien{
401541Srgrimes    int i, err_cnt = 0;
4129680Sgibbs
4229680Sgibbs    signal(SIGINT, cleanup);
431541Srgrimes    signal(SIGHUP, cleanup);
441541Srgrimes
451541Srgrimes    if (AddMode == SLAVE)
461541Srgrimes	err_cnt = pkg_do(NULL);
471541Srgrimes    else {
481541Srgrimes	for (i = 0; pkgs[i]; i++)
4929680Sgibbs	    err_cnt += pkg_do(pkgs[i]);
5029680Sgibbs    }
5160938Sjake    return err_cnt;
5229680Sgibbs}
5329680Sgibbs
5429680Sgibbsstatic Package Plist;
5529680Sgibbsstatic char *Home;
5692723Salfred
5729680Sgibbs/*
5829680Sgibbs * This is seriously ugly code following.  Written very fast!
5929680Sgibbs * [And subsequently made even worse..  Sigh!  This code was just born
6029680Sgibbs * to be hacked, I guess.. :) -jkh]
6151684Sn_hibma */
6229680Sgibbsstatic int
6351684Sn_hibmapkg_do(char *pkg)
6451684Sn_hibma{
6551684Sn_hibma    char pkg_fullname[FILENAME_MAX];
6651684Sn_hibma    char playpen[FILENAME_MAX];
6751684Sn_hibma    char extract_contents[FILENAME_MAX];
6829680Sgibbs    char *where_to, *tmp, *extract;
6929680Sgibbs    FILE *cfile;
7051684Sn_hibma    int code;
7129680Sgibbs    PackingList p;
7229680Sgibbs    struct stat sb;
7329680Sgibbs    int inPlace;
7429680Sgibbs
7529680Sgibbs    code = 0;
7629680Sgibbs    LogDir[0] = '\0';
7729680Sgibbs    strcpy(playpen, FirstPen);
7829680Sgibbs    inPlace = 0;
7929680Sgibbs
8029680Sgibbs    /* Are we coming in for a second pass, everything already extracted? */
8129680Sgibbs    if (!pkg) {
8229680Sgibbs	fgets(playpen, FILENAME_MAX, stdin);
8329680Sgibbs	playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */
8429680Sgibbs	if (chdir(playpen) == FAIL) {
8529680Sgibbs	    warnx("pkg_add in SLAVE mode can't chdir to %s", playpen);
8629680Sgibbs	    return 1;
8729680Sgibbs	}
8851684Sn_hibma	read_plist(&Plist, stdin);
8951684Sn_hibma	where_to = playpen;
9051684Sn_hibma    }
9129680Sgibbs    /* Nope - do it now */
9229680Sgibbs    else {
9329680Sgibbs	/* Is it an ftp://foo.bar.baz/file.tgz specification? */
9429680Sgibbs	if (isURL(pkg)) {
9529680Sgibbs	    if (!(Home = fileGetURL(NULL, pkg))) {
9629680Sgibbs		warnx("unable to fetch `%s' by URL", pkg);
9729680Sgibbs		return 1;
9829680Sgibbs	    }
9929680Sgibbs	    where_to = Home;
10045739Speter	    strcpy(pkg_fullname, pkg);
10129680Sgibbs	    cfile = fopen(CONTENTS_FNAME, "r");
10229680Sgibbs	    if (!cfile) {
10329680Sgibbs		warnx(
10429680Sgibbs		"unable to open table of contents file `%s' - not a package?",
10529680Sgibbs		CONTENTS_FNAME);
10629680Sgibbs		goto bomb;
10729680Sgibbs	    }
10829680Sgibbs	    read_plist(&Plist, cfile);
10929680Sgibbs	    fclose(cfile);
11029680Sgibbs	}
11151684Sn_hibma	else {
11251684Sn_hibma	    strcpy(pkg_fullname, pkg);		/* copy for sanity's sake, could remove pkg_fullname */
11351684Sn_hibma	    if (strcmp(pkg, "-")) {
11429680Sgibbs		if (stat(pkg_fullname, &sb) == FAIL) {
11529680Sgibbs		    warnx("can't stat package file '%s'", pkg_fullname);
11629680Sgibbs		    goto bomb;
11729680Sgibbs		}
11829680Sgibbs		sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
11929680Sgibbs		extract = extract_contents;
12029680Sgibbs	    }
12129680Sgibbs	    else {
12229680Sgibbs		extract = NULL;
12329680Sgibbs		sb.st_size = 100000;	/* Make up a plausible average size */
124	    }
125	    Home = make_playpen(playpen, sb.st_size * 4);
126	    if (!Home)
127		warnx("unable to make playpen for %d bytes", sb.st_size * 4);
128	    where_to = Home;
129	    if (unpack(pkg_fullname, extract)) {
130		warnx(
131	"unable to extract table of contents file from `%s' - not a package?",
132		pkg_fullname);
133		goto bomb;
134	    }
135	    cfile = fopen(CONTENTS_FNAME, "r");
136	    if (!cfile) {
137		warnx(
138	"unable to open table of contents file `%s' - not a package?",
139		CONTENTS_FNAME);
140		goto bomb;
141	    }
142	    read_plist(&Plist, cfile);
143	    fclose(cfile);
144
145	    /* Extract directly rather than moving?  Oh goodie! */
146	    if (find_plist_option(&Plist, "extract-in-place")) {
147		if (Verbose)
148		    printf("Doing in-place extraction for %s\n", pkg_fullname);
149		p = find_plist(&Plist, PLIST_CWD);
150		if (p) {
151		    if (!isdir(p->name) && !Fake) {
152			if (Verbose)
153			    printf("Desired prefix of %s does not exist, creating..\n", p->name);
154			vsystem("mkdir -p %s", p->name);
155			if (chdir(p->name) == -1) {
156			    warn("unable to change directory to `%s'", p->name);
157			    goto bomb;
158			}
159		    }
160		    where_to = p->name;
161		    inPlace = 1;
162		}
163		else {
164		    warnx(
165		"no prefix specified in `%s' - this is a bad package!",
166			pkg_fullname);
167		    goto bomb;
168		}
169	    }
170
171	    /*
172	     * Apply a crude heuristic to see how much space the package will
173	     * take up once it's unpacked.  I've noticed that most packages
174	     * compress an average of 75%, so multiply by 4 for good measure.
175	     */
176
177	    if (!inPlace && min_free(playpen) < sb.st_size * 4) {
178		warnx("projected size of %d exceeds available free space.\n"
179"Please set your PKG_TMPDIR variable to point to a location with more\n"
180		       "free space and try again", sb.st_size * 4);
181		warnx("not extracting %s\ninto %s, sorry!",
182			pkg_fullname, where_to);
183		goto bomb;
184	    }
185
186	    /* If this is a direct extract and we didn't want it, stop now */
187	    if (inPlace && Fake)
188		goto success;
189
190	    /* Finally unpack the whole mess */
191	    if (unpack(pkg_fullname, NULL)) {
192		warnx("unable to extract `%s'!", pkg_fullname);
193		goto bomb;
194	    }
195	}
196
197	/* Check for sanity and dependencies */
198	if (sanity_check(pkg))
199	    goto bomb;
200
201	/* If we're running in MASTER mode, just output the plist and return */
202	if (AddMode == MASTER) {
203	    printf("%s\n", where_playpen());
204	    write_plist(&Plist, stdout);
205	    return 0;
206	}
207    }
208
209    /*
210     * If we have a prefix, delete the first one we see and add this
211     * one in place of it.
212     */
213    if (Prefix) {
214	delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
215	add_plist_top(&Plist, PLIST_CWD, Prefix);
216    }
217
218    setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1);
219    /* Protect against old packages with bogus @name fields */
220    PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous";
221
222    /* See if we're already registered */
223    sprintf(LogDir, "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, PkgName);
224    if (isdir(LogDir) && !Force) {
225	warnx("package `%s' already recorded as installed", PkgName);
226	code = 1;
227	goto success;	/* close enough for government work */
228    }
229
230    /* Now check the packing list for dependencies */
231    for (p = Plist.head; p ; p = p->next) {
232	if (p->type != PLIST_PKGDEP)
233	    continue;
234	if (Verbose)
235	    printf("Package `%s' depends on `%s'.\n", PkgName, p->name);
236	if (vsystem("pkg_info -e %s", p->name)) {
237	    char path[FILENAME_MAX], *cp = NULL;
238
239	    if (!Fake) {
240		if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) {
241		    snprintf(path, FILENAME_MAX, "%s/%s.tgz", Home, p->name);
242		    if (fexists(path))
243			cp = path;
244		    else
245			cp = fileFindByPath(pkg, p->name);
246		    if (cp) {
247			if (Verbose)
248			    printf("Loading it from %s.\n", cp);
249			if (vsystem("pkg_add %s%s", Verbose ? "-v " : "", cp)) {
250			    warnx("autoload of dependency `%s' failed%s",
251				cp, Force ? " (proceeding anyway)" : "!");
252			    if (!Force)
253				++code;
254			}
255		    }
256		}
257		else if ((cp = fileGetURL(pkg, p->name)) != NULL) {
258		    if (Verbose)
259			printf("Finished loading %s over FTP.\n", p->name);
260		    if (!fexists("+CONTENTS")) {
261			warnx("autoloaded package %s has no +CONTENTS file?",
262				p->name);
263			if (!Force)
264			    ++code;
265		    }
266		    else if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S", Verbose ? "-v " : "")) {
267			warnx("pkg_add of dependency `%s' failed%s",
268				p->name, Force ? " (proceeding anyway)" : "!");
269			if (!Force)
270			    ++code;
271		    }
272		    else if (Verbose)
273			printf("\t`%s' loaded successfully.\n", p->name);
274		    /* Nuke the temporary playpen */
275		    leave_playpen(cp);
276		}
277	    }
278	    else {
279		if (Verbose)
280		    printf("and was not found%s.\n", Force ? " (proceeding anyway)" : "");
281		else
282		    printf("Package dependency %s for %s not found%s\n", p->name, pkg,
283			   Force ? " (proceeding anyway)" : "!");
284		if (!Force)
285		    ++code;
286	    }
287	}
288	else if (Verbose)
289	    printf(" - already installed.\n");
290    }
291
292    if (code != 0)
293	goto bomb;
294
295    /* Look for the requirements file */
296    if (fexists(REQUIRE_FNAME)) {
297	vsystem("chmod +x %s", REQUIRE_FNAME);	/* be sure */
298	if (Verbose)
299	    printf("Running requirements file first for %s..\n", PkgName);
300	if (!Fake && vsystem("sh -c %s %s INSTALL", REQUIRE_FNAME, PkgName)) {
301	    warnx("package %s fails requirements %s", pkg_fullname,
302		   Force ? "installing anyway" : "- not installed");
303	    if (!Force) {
304		code = 1;
305		goto success;	/* close enough for government work */
306	    }
307	}
308    }
309
310    /* If we're really installing, and have an installation file, run it */
311    if (!NoInstall && fexists(INSTALL_FNAME)) {
312	vsystem("chmod +x %s", INSTALL_FNAME);	/* make sure */
313	if (Verbose)
314	    printf("Running install with PRE-INSTALL for %s..\n", PkgName);
315	if (!Fake && vsystem("sh -c %s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) {
316	    warnx("install script returned error status");
317	    unlink(INSTALL_FNAME);
318	    code = 1;
319	    goto success;		/* nothing to uninstall yet */
320	}
321    }
322
323    /* Now finally extract the entire show if we're not going direct */
324    if (!inPlace && !Fake)
325	extract_plist(".", &Plist);
326
327    if (!Fake && fexists(MTREE_FNAME)) {
328	if (Verbose)
329	    printf("Running mtree for %s..\n", PkgName);
330	p = find_plist(&Plist, PLIST_CWD);
331	if (Verbose)
332	    printf("mtree -U -f %s -d -e -p %s\n", MTREE_FNAME, p ? p->name : "/");
333	if (!Fake) {
334	    if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s", MTREE_FNAME, p ? p->name : "/"))
335		warnx("mtree returned a non-zero status - continuing");
336	}
337	unlink(MTREE_FNAME);
338    }
339
340    /* Run the installation script one last time? */
341    if (!NoInstall && fexists(INSTALL_FNAME)) {
342	if (Verbose)
343	    printf("Running install with POST-INSTALL for %s..\n", PkgName);
344	if (!Fake && vsystem("sh -c %s %s POST-INSTALL", INSTALL_FNAME, PkgName)) {
345	    warnx("install script returned error status");
346	    unlink(INSTALL_FNAME);
347	    code = 1;
348	    goto fail;
349	}
350	unlink(INSTALL_FNAME);
351    }
352
353    /* Time to record the deed? */
354    if (!NoRecord && !Fake) {
355	char contents[FILENAME_MAX];
356	FILE *cfile;
357
358	umask(022);
359	if (getuid() != 0)
360	    warnx("not running as root - trying to record install anyway");
361	if (!PkgName) {
362	    warnx("no package name! can't record package, sorry");
363	    code = 1;
364	    goto success;	/* well, partial anyway */
365	}
366	sprintf(LogDir, "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, PkgName);
367	if (Verbose)
368	    printf("Attempting to record package into %s..\n", LogDir);
369	if (make_hierarchy(LogDir)) {
370	    warnx("can't record package into '%s', you're on your own!",
371		   LogDir);
372	    bzero(LogDir, FILENAME_MAX);
373	    code = 1;
374	    goto success;	/* close enough for government work */
375	}
376	/* Make sure pkg_info can read the entry */
377	vsystem("chmod a+rx %s", LogDir);
378	if (fexists(DEINSTALL_FNAME))
379	    move_file(".", DEINSTALL_FNAME, LogDir);
380	if (fexists(REQUIRE_FNAME))
381	    move_file(".", REQUIRE_FNAME, LogDir);
382	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
383	cfile = fopen(contents, "w");
384	if (!cfile) {
385	    warnx("can't open new contents file '%s'! can't register pkg",
386		contents);
387	    goto success; /* can't log, but still keep pkg */
388	}
389	write_plist(&Plist, cfile);
390	fclose(cfile);
391	move_file(".", DESC_FNAME, LogDir);
392	move_file(".", COMMENT_FNAME, LogDir);
393	if (fexists(DISPLAY_FNAME))
394	    move_file(".", DISPLAY_FNAME, LogDir);
395	for (p = Plist.head; p ; p = p->next) {
396	    if (p->type != PLIST_PKGDEP)
397		continue;
398	    if (Verbose)
399		printf("Attempting to record dependency on package `%s'\n", p->name);
400	    sprintf(contents, "%s/%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR,
401	    	    basename_of(p->name), REQUIRED_BY_FNAME);
402	    cfile = fopen(contents, "a");
403	    if (!cfile)
404		warnx("can't open dependency file '%s'!\n"
405		       "dependency registration is incomplete", contents);
406	    else {
407		fprintf(cfile, "%s\n", PkgName);
408		if (fclose(cfile) == EOF)
409		    warnx("cannot properly close file %s", contents);
410	    }
411	}
412	if (Verbose)
413	    printf("Package %s registered in %s\n", PkgName, LogDir);
414    }
415
416    if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) {
417	FILE *fp;
418	char buf[BUFSIZ];
419
420	snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name);
421	fp = fopen(buf, "r");
422	if (fp) {
423	    putc('\n', stdout);
424	    while (fgets(buf, sizeof(buf), fp))
425		fputs(buf, stdout);
426	    putc('\n', stdout);
427	    (void) fclose(fp);
428	} else
429	    warnx("cannot open %s as display file", buf);
430    }
431
432    goto success;
433
434 bomb:
435    code = 1;
436    goto success;
437
438 fail:
439    /* Nuke the whole (installed) show, XXX but don't clean directories */
440    if (!Fake)
441	delete_package(FALSE, FALSE, &Plist);
442
443 success:
444    /* delete the packing list contents */
445    free_plist(&Plist);
446    leave_playpen(Home);
447    return code;
448}
449
450static int
451sanity_check(char *pkg)
452{
453    int code = 0;
454
455    if (!fexists(CONTENTS_FNAME)) {
456	warnx("package %s has no CONTENTS file!", pkg);
457	code = 1;
458    }
459    else if (!fexists(COMMENT_FNAME)) {
460	warnx("package %s has no COMMENT file!", pkg);
461	code = 1;
462    }
463    else if (!fexists(DESC_FNAME)) {
464	warnx("package %s has no DESC file!", pkg);
465	code = 1;
466    }
467    return code;
468}
469
470void
471cleanup(int signo)
472{
473    if (signo)
474	printf("Signal %d received, cleaning up..\n", signo);
475    if (!Fake && LogDir[0])
476	vsystem("%s -rf %s", REMOVE_CMD, LogDir);
477    leave_playpen(Home);
478    exit(1);
479}
480