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