perform.c revision 10085
1#ifndef lint
2static const char *rcsid = "$Id: perform.c,v 1.28 1995/08/06 03:20:01 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
35
36int
37pkg_perform(char **pkgs)
38{
39    int i, err_cnt = 0;
40
41    signal(SIGINT, cleanup);
42    signal(SIGHUP, cleanup);
43
44    if (AddMode == SLAVE)
45	err_cnt = pkg_do(NULL);
46    else {
47	for (i = 0; pkgs[i]; i++)
48	    err_cnt += pkg_do(pkgs[i]);
49    }
50    return err_cnt;
51}
52
53static Package Plist;
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 home[FILENAME_MAX];
65    char extract_contents[FILENAME_MAX];
66    char *where_to, *tmp;
67    FILE *cfile;
68    int code = 0;
69    PackingList p;
70    struct stat sb;
71    char *isTMP = NULL;
72
73    /* Reset some state */
74    if (Plist.head)
75	free_plist(&Plist);
76    LogDir[0] = '\0';
77
78    /* Are we coming in for a second pass, everything already extracted? */
79    if (AddMode == SLAVE) {
80	char tmp_dir[FILENAME_MAX];
81
82	fgets(tmp_dir, FILENAME_MAX, stdin);
83	tmp_dir[strlen(tmp_dir) - 1] = '\0'; /* pesky newline! */
84	if (chdir(tmp_dir) == FAIL) {
85	    whinge("pkg_add in SLAVE mode can't chdir to %s.", tmp_dir);
86	    return 1;
87	}
88	read_plist(&Plist, stdin);
89    }
90    /* Nope - do it now */
91    else {
92	if (!getcwd(home, FILENAME_MAX))
93	    upchuck("getcwd");
94
95	if (isURL(pkg)) {
96	    char *newname = fileGetURL(pkg);
97
98	    if (!newname) {
99		whinge("Unable to fetch `%s' by URL.", pkg);
100		return 1;
101	    }
102	    strcpy(pkg_fullname, newname);
103	    isTMP = pkg_fullname;
104	}
105	else {
106	    if (pkg[0] == '/')	/* full pathname? */
107		strcpy(pkg_fullname, pkg);
108	    else
109		sprintf(pkg_fullname, "%s/%s", home, pkg);
110	    if (!fexists(pkg_fullname)) {
111		char *tmp = fileFindByPath(pkg);
112
113		if (!tmp) {
114		    whinge("Can't find package `%s'.", pkg);
115		    return 1;
116		}
117		strcpy(pkg_fullname, tmp);
118	    }
119	}
120	if (stat(pkg_fullname, &sb) == FAIL) {
121	    whinge("Can't stat package file '%s'.", pkg_fullname);
122	    goto bomb;
123	}
124	Home = make_playpen(PlayPen, sb.st_size * 4);
125	where_to = PlayPen;
126	sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
127	if (unpack(pkg_fullname, extract_contents)) {
128	    whinge("Unable to extract table of contents file from `%s' - not a package?.", pkg_fullname);
129	    goto bomb;
130	}
131	cfile = fopen(CONTENTS_FNAME, "r");
132	if (!cfile) {
133	    whinge("Unable to open table of contents file `%s' - not a package?", CONTENTS_FNAME);
134	    goto bomb;
135	}
136	read_plist(&Plist, cfile);
137	fclose(cfile);
138
139	/*
140	 * If we have a prefix, delete the first one we see and add this
141	 * one in place of it.
142	 */
143	if (Prefix) {
144	    delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
145	    add_plist_top(&Plist, PLIST_CWD, Prefix);
146	}
147
148	/* Extract directly rather than moving?  Oh goodie! */
149	if (find_plist_option(&Plist, "extract-in-place")) {
150	    if (Verbose)
151		printf("Doing in-place extraction for %s\n", pkg_fullname);
152	    p = find_plist(&Plist, PLIST_CWD);
153	    if (p) {
154		if (!isdir(p->name) && !Fake) {
155		    if (Verbose)
156			printf("Desired prefix of %s does not exist, creating..\n", p->name);
157		    vsystem("mkdir -p %s", p->name);
158		    if (chdir(p->name)) {
159			whinge("Unable to change directory to `%s' - no permission?", p->name);
160			perror("chdir");
161			goto bomb;
162		    }
163		}
164		where_to = p->name;
165	    }
166	    else {
167		whinge("No prefix specified in `%s' - this is a bad package!",
168		       pkg_fullname);
169		goto bomb;
170	    }
171	}
172
173	/*
174	 * Apply a crude heuristic to see how much space the package will
175	 * take up once it's unpacked.  I've noticed that most packages
176	 * compress an average of 75%, so multiply by 4 for good measure.
177	 */
178
179	if (min_free(where_to) < sb.st_size * 4) {
180	    whinge("Projected size of %d exceeds available free space.\nPlease set your PKG_TMPDIR variable to point to a location with more\nfree 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	setenv(PKG_PREFIX_VNAME,
186	       (p = find_plist(&Plist, PLIST_CWD)) ? p->name : NULL, 1);
187	/* Protect against old packages with bogus @name fields */
188	PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous";
189
190	/* See if we're already registered */
191	sprintf(LogDir, "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR,
192		basename_of(PkgName));
193	if (isdir(LogDir)) {
194	    char tmp[FILENAME_MAX];
195
196	    whinge("Package `%s' already recorded as installed.\n", PkgName);
197	    code = 1;
198	    goto success;	/* close enough for government work */
199        }
200
201	/* Now check the packing list for dependencies */
202	for (p = Plist.head; p ; p = p->next) {
203	    char *isTMP = NULL;	/* local copy for depends only */
204
205	    if (p->type != PLIST_PKGDEP)
206		continue;
207	    if (Verbose)
208		printf("Package `%s' depends on `%s'", pkg, p->name);
209	    if (!Fake && vsystem("pkg_info -e %s", p->name)) {
210		char path[FILENAME_MAX], *cp = NULL;
211
212		if (Verbose)
213		    printf(" which is not currently loaded");
214		if (!isURL(p->name)) {
215		    snprintf(path, FILENAME_MAX, "%s/%s.tgz", Home, p->name);
216		    if (fexists(path))
217			cp = path;
218		    else
219			cp = fileFindByPath(p->name);
220		}
221		else {
222		    cp = fileGetURL(p->name);
223		    isTMP = cp;
224		}
225		if (cp) {
226		    if (Verbose)
227			printf(" but was found - loading:\n");
228		    if (!Fake && vsystem("pkg_add %s", cp)) {
229			whinge("Autoload of dependency `%s' failed%s",
230			       p->name, Force ? " (proceeding anyway)" : "!");
231			if (!Force)
232			    ++code;
233		    }
234		    else if (Verbose)
235			printf("\t`%s' loaded successfully.\n", p->name);
236		    /* Nuke the temporary URL copy */
237		    if (isTMP) {
238			unlink(isTMP);
239			isTMP = NULL;
240		    }
241		}
242		else {
243		    if (Verbose)
244			printf("and was not found%s.\n",
245			       Force ? " (proceeding anyway)" : "");
246		    else
247			printf("Package dependency %s for %s not found%s\n",
248			       p->name, pkg,
249			       Force ? " (proceeding anyway)" : "!");
250		    if (!Force)
251			++code;
252		}
253	    }
254	    else if (Verbose)
255		printf(" - already installed.\n");
256	}
257
258	/* If this is a direct extract and we didn't want it, stop now */
259	if (where_to != PlayPen && Fake)
260	    goto success;
261
262	/* Finally unpack the whole mess */
263	if (unpack(pkg_fullname, NULL)) {
264	    whinge("Unable to extract `%s'!", pkg_fullname);
265	    goto bomb;
266	}
267
268	/* Check for sanity and dependencies */
269	if (sanity_check(pkg_fullname))
270	    goto bomb;
271
272	/* If we're running in MASTER mode, just output the plist and return */
273	if (AddMode == MASTER) {
274	    printf("%s\n", where_playpen());
275	    write_plist(&Plist, stdout);
276	    return 0;
277	}
278    }
279
280    /* Look for the requirements file */
281    if (fexists(REQUIRE_FNAME)) {
282	vsystem("chmod +x %s", REQUIRE_FNAME);	/* be sure */
283	if (Verbose)
284	    printf("Running requirements file first for %s..\n", PkgName);
285	if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) {
286	    whinge("Package %s fails requirements %s",
287		   pkg_fullname,
288		   Force ? "installing anyway" : "- not installed.");
289	    if (!Force) {
290		code = 1;
291		goto success;	/* close enough for government work */
292	    }
293	}
294    }
295
296    /* If we're really installing, and have an installation file, run it */
297    if (!NoInstall && fexists(INSTALL_FNAME)) {
298	vsystem("chmod +x %s", INSTALL_FNAME);	/* make sure */
299	if (Verbose)
300	    printf("Running install with PRE-INSTALL for %s..\n", PkgName);
301	if (!Fake && vsystem("./%s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) {
302	    whinge("Install script returned error status.");
303	    unlink(INSTALL_FNAME);
304	    code = 1;
305	    goto success;		/* nothing to uninstall yet */
306	}
307    }
308
309    /* Now finally extract the entire show if we're not going direct */
310    if (where_to == PlayPen && !Fake)
311	extract_plist(home, &Plist);
312
313    if (!Fake && fexists(MTREE_FNAME)) {
314	if (Verbose)
315	    printf("Running mtree for %s..\n", PkgName);
316	p = find_plist(&Plist, PLIST_CWD);
317	if (Verbose)
318	    printf("mtree -U -f %s -d -e -p %s\n", MTREE_FNAME,
319		   p ? p->name : "/");
320	if (!Fake) {
321	    if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s",
322		        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",
355		(tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR,
356    	    	basename_of(PkgName));
357	if (Verbose)
358	    printf("Attempting to record package into %s..\n", LogDir);
359	if (make_hierarchy(LogDir)) {
360	    whinge("Can't record package into '%s', you're on your own!",
361		   LogDir);
362	    bzero(LogDir, FILENAME_MAX);
363	    code = 1;
364	    goto success;	/* close enough for government work */
365	}
366	/* Make sure pkg_info can read the entry */
367	vsystem("chmod a+rx %s", LogDir);
368	if (fexists(DEINSTALL_FNAME))
369	    move_file(".", DEINSTALL_FNAME, LogDir);
370	if (fexists(REQUIRE_FNAME))
371	    move_file(".", REQUIRE_FNAME, LogDir);
372	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
373	cfile = fopen(contents, "w");
374	if (!cfile) {
375	    whinge("Can't open new contents file '%s'!  Can't register pkg.",
376		   contents);
377	    goto success; /* can't log, but still keep pkg */
378	}
379	write_plist(&Plist, cfile);
380	fclose(cfile);
381	move_file(".", DESC_FNAME, LogDir);
382	move_file(".", COMMENT_FNAME, LogDir);
383	if (fexists(DISPLAY_FNAME))
384	    move_file(".", DISPLAY_FNAME, LogDir);
385	for (p = Plist.head; p ; p = p->next) {
386	    if (p->type != PLIST_PKGDEP)
387		continue;
388	    if (Verbose)
389		printf("Attempting to record dependency on package `%s'\n",
390		       p->name);
391	    sprintf(contents, "%s/%s/%s",
392	    	    (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR,
393	    	    basename_of(p->name), REQUIRED_BY_FNAME);
394	    cfile = fopen(contents, "a");
395	    if (!cfile)
396		whinge("Warning: Can't open dependency file '%s'!\n\tDependency registration is incomplete.", contents);
397	    else {
398		fprintf(cfile, "%s\n", basename_of(PkgName));
399		if (fclose(cfile) == EOF)
400		    warn("Cannot properly close file %s", contents);
401	    }
402	}
403	if (Verbose)
404	    printf("Package %s registered in %s\n", PkgName, LogDir);
405    }
406
407    if (p = find_plist(&Plist, PLIST_DISPLAY)) {
408	FILE *fp;
409	char buf[BUFSIZ];
410	fp = fopen(p->name, "r");
411	if (fp) {
412	    putc('\n', stdout);
413	    while (fgets(buf, sizeof(buf), fp))
414		fputs(buf, stdout);
415	    putc('\n', stdout);
416	    (void) fclose(fp);
417	} else
418	    warn("Cannot open display file `%s'.", p->name);
419    }
420
421    goto success;
422
423 bomb:
424    code = 1;
425    goto success;
426
427 fail:
428    /* Nuke the whole (installed) show, XXX but don't clean directories */
429    if (!Fake)
430	delete_package(FALSE, FALSE, &Plist);
431
432 success:
433    /* delete the packing list contents */
434    leave_playpen();
435    if (isTMP)
436	unlink(isTMP);
437    return code;
438}
439
440static int
441sanity_check(char *pkg)
442{
443    PackingList p;
444    int code = 0;
445
446    if (!fexists(CONTENTS_FNAME)) {
447	whinge("Package %s has no CONTENTS file!", pkg);
448	code = 1;
449    }
450    else if (!fexists(COMMENT_FNAME)) {
451	whinge("Package %s has no COMMENT file!", pkg);
452	code = 1;
453    }
454    else if (!fexists(DESC_FNAME)) {
455	whinge("Package %s has no DESC file!", pkg);
456	code = 1;
457    }
458    return code;
459}
460
461void
462cleanup(int signo)
463{
464    if (signo)
465	printf("Signal %d received, cleaning up..\n", signo);
466    if (Plist.head) {
467	if (!Fake)
468	    delete_package(FALSE, FALSE, &Plist);
469	free_plist(&Plist);
470    }
471    if (!Fake && LogDir[0])
472	vsystem("%s -rf %s", REMOVE_CMD, LogDir);
473    leave_playpen();
474}
475