perform.c revision 8115
1#ifndef lint
2static const char *rcsid = "$Id: perform.c,v 1.23 1995/04/27 11:33:08 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	Home = make_playpen(PlayPen, 0);
121	sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
122	if (unpack(pkg_fullname, extract_contents)) {
123	    whinge("Unable to extract table of contents file from `%s' - not a package?.", pkg_fullname);
124	    goto bomb;
125	}
126	cfile = fopen(CONTENTS_FNAME, "r");
127	if (!cfile) {
128	    whinge("Unable to open table of contents file `%s' - not a package?", CONTENTS_FNAME);
129	    goto bomb;
130	}
131	read_plist(&Plist, cfile);
132	fclose(cfile);
133
134	/*
135	 * If we have a prefix, delete the first one we see and add this
136	 * one in place of it.
137	 */
138	if (Prefix) {
139	    delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
140	    add_plist_top(&Plist, PLIST_CWD, Prefix);
141	}
142
143	/* Extract directly rather than moving?  Oh goodie! */
144	if (find_plist_option(&Plist, "extract-in-place")) {
145	    if (Verbose)
146		printf("Doing in-place extraction for %s\n", pkg_fullname);
147	    p = find_plist(&Plist, PLIST_CWD);
148	    if (p) {
149		if (!isdir(p->name) && !Fake) {
150		    if (Verbose)
151			printf("Desired prefix of %s does not exist, creating..\n", p->name);
152		    vsystem("mkdir -p %s", p->name);
153		    if (chdir(p->name)) {
154			whinge("Unable to change directory to `%s' - no permission?", p->name);
155			perror("chdir");
156			goto bomb;
157		    }
158		}
159		where_to = p->name;
160	    }
161	    else {
162		whinge("No prefix specified in `%s' - this is a bad package!",
163		       pkg_fullname);
164		goto bomb;
165	    }
166	}
167	else
168	    where_to = PlayPen;
169	/*
170	 * Apply a crude heuristic to see how much space the package will
171	 * take up once it's unpacked.  I've noticed that most packages
172	 * compress an average of 75%, so multiply by 4 for good measure.
173	 */
174	if (stat(pkg_fullname, &sb) == FAIL) {
175	    whinge("Can't stat package file '%s'.", pkg_fullname);
176	    goto bomb;
177	}
178
179	if (min_free(where_to) < sb.st_size * 4) {
180	    whinge("Projected size of %d exceeds free space in %s.",
181		   sb.st_size * 4, where_to);
182	    whinge("Not extracting %s, sorry!", pkg_fullname);
183	    goto bomb;
184	}
185
186	setenv(PKG_PREFIX_VNAME,
187	       (p = find_plist(&Plist, PLIST_CWD)) ? p->name : NULL, 1);
188	/* Protect against old packages with bogus @name fields */
189	PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous";
190
191	/* See if we're already registered */
192	sprintf(LogDir, "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR,
193		basename_of(PkgName));
194	if (isdir(LogDir)) {
195	    char tmp[FILENAME_MAX];
196
197	    whinge("Package `%s' already recorded as installed.\n", PkgName);
198	    code = 1;
199	    goto success;	/* close enough for government work */
200        }
201
202	/* Now check the packing list for dependencies */
203	for (p = Plist.head; p ; p = p->next) {
204	    char *isTMP = NULL;	/* local copy for depends only */
205
206	    if (p->type != PLIST_PKGDEP)
207		continue;
208	    if (Verbose)
209		printf("Package `%s' depends on `%s'", pkg, p->name);
210	    if (!Fake && vsystem("pkg_info -e %s", p->name)) {
211		char path[FILENAME_MAX], *cp = NULL;
212
213		if (Verbose)
214		    printf(" which is not currently loaded");
215		if (!isURL(p->name)) {
216		    snprintf(path, FILENAME_MAX, "%s/%s", Home, p->name);
217		    if (fexists(path))
218			cp = path;
219		    else
220			cp = fileFindByPath(p->name);
221		}
222		else {
223		    cp = fileGetURL(p->name);
224		    isTMP = cp;
225		}
226		if (cp) {
227		    if (Verbose)
228			printf(" but was found - loading:\n");
229		    if (!Fake && vsystem("pkg_add %s", cp)) {
230			whinge("Autoload of dependency `%s' failed%s",
231			       p->name, Force ? " (proceeding anyway)" : "!");
232			if (!Force)
233			    ++code;
234		    }
235		    else if (Verbose)
236			printf("\t`%s' loaded successfully.\n", p->name);
237		    /* Nuke the temporary URL copy */
238		    if (isTMP) {
239			unlink(isTMP);
240			isTMP = NULL;
241		    }
242		}
243		else {
244		    if (Verbose)
245			printf("and was not found%s.\n",
246			       Force ? " (proceeding anyway)" : "");
247		    else
248			printf("Package dependency %s for %s not found%s\n",
249			       p->name, pkg,
250			       Force ? " (proceeding anyway)" : "!");
251		    if (!Force)
252			++code;
253		}
254	    }
255	    else if (Verbose)
256		printf(" - already installed.\n");
257	}
258
259	/* If this is a direct extract and we didn't want it, stop now */
260	if (where_to != PlayPen && Fake)
261	    goto success;
262
263	/* Finally unpack the whole mess */
264	if (unpack(pkg_fullname, NULL)) {
265	    whinge("Unable to extract `%s'!", pkg_fullname);
266	    goto bomb;
267	}
268
269	/* Check for sanity and dependencies */
270	if (sanity_check(pkg_fullname))
271	    goto bomb;
272
273	/* If we're running in MASTER mode, just output the plist and return */
274	if (AddMode == MASTER) {
275	    printf("%s\n", where_playpen());
276	    write_plist(&Plist, stdout);
277	    return 0;
278	}
279    }
280
281    /* Look for the requirements file */
282    if (fexists(REQUIRE_FNAME)) {
283	vsystem("chmod +x %s", REQUIRE_FNAME);	/* be sure */
284	if (Verbose)
285	    printf("Running requirements file first for %s..\n", PkgName);
286	if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) {
287	    whinge("Package %s fails requirements %s",
288		   pkg_fullname,
289		   Force ? "installing anyway" : "- not installed.");
290	    if (!Force) {
291		code = 1;
292		goto success;	/* close enough for government work */
293	    }
294	}
295    }
296
297    /* If we're really installing, and have an installation file, run it */
298    if (!NoInstall && fexists(INSTALL_FNAME)) {
299	vsystem("chmod +x %s", INSTALL_FNAME);	/* make sure */
300	if (Verbose)
301	    printf("Running install with PRE-INSTALL for %s..\n", PkgName);
302	if (!Fake && vsystem("./%s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) {
303	    whinge("Install script returned error status.");
304	    unlink(INSTALL_FNAME);
305	    code = 1;
306	    goto success;		/* nothing to uninstall yet */
307	}
308    }
309
310    /* Now finally extract the entire show if we're not going direct */
311    if (where_to == PlayPen && !Fake)
312	extract_plist(home, &Plist);
313
314    if (!Fake && fexists(MTREE_FNAME)) {
315	if (Verbose)
316	    printf("Running mtree for %s..\n", PkgName);
317	p = find_plist(&Plist, PLIST_CWD);
318	if (Verbose)
319	    printf("mtree -U -f %s -d -e -p %s\n", MTREE_FNAME,
320		   p ? p->name : "/");
321	if (!Fake) {
322	    if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s",
323		        MTREE_FNAME, p ? p->name : "/"))
324		whinge("mtree returned a non-zero status - continuing.");
325	}
326	unlink(MTREE_FNAME);
327    }
328
329    /* Run the installation script one last time? */
330    if (!NoInstall && fexists(INSTALL_FNAME)) {
331	if (Verbose)
332	    printf("Running install with POST-INSTALL for %s..\n", PkgName);
333	if (!Fake && vsystem("./%s %s POST-INSTALL", INSTALL_FNAME, PkgName)) {
334	    whinge("Install script returned error status.");
335	    unlink(INSTALL_FNAME);
336	    code = 1;
337	    goto fail;
338	}
339	unlink(INSTALL_FNAME);
340    }
341
342    /* Time to record the deed? */
343    if (!NoRecord && !Fake) {
344	char contents[FILENAME_MAX];
345	FILE *cfile;
346
347	umask(022);
348	if (getuid() != 0)
349	    whinge("Not running as root - trying to record install anyway.");
350	if (!PkgName) {
351	    whinge("No package name!  Can't record package, sorry.");
352	    code = 1;
353	    goto success;	/* well, partial anyway */
354	}
355	sprintf(LogDir, "%s/%s",
356		(tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR,
357    	    	basename_of(PkgName));
358	if (Verbose)
359	    printf("Attempting to record package into %s..\n", LogDir);
360	if (make_hierarchy(LogDir)) {
361	    whinge("Can't record package into '%s', you're on your own!",
362		   LogDir);
363	    bzero(LogDir, FILENAME_MAX);
364	    code = 1;
365	    goto success;	/* close enough for government work */
366	}
367	/* Make sure pkg_info can read the entry */
368	vsystem("chmod a+rx %s", LogDir);
369	if (fexists(DEINSTALL_FNAME))
370	    move_file(".", DEINSTALL_FNAME, LogDir);
371	if (fexists(REQUIRE_FNAME))
372	    move_file(".", REQUIRE_FNAME, LogDir);
373	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
374	cfile = fopen(contents, "w");
375	if (!cfile) {
376	    whinge("Can't open new contents file '%s'!  Can't register pkg.",
377		   contents);
378	    goto success; /* can't log, but still keep pkg */
379	}
380	write_plist(&Plist, cfile);
381	fclose(cfile);
382	move_file(".", DESC_FNAME, LogDir);
383	move_file(".", COMMENT_FNAME, LogDir);
384	if (fexists(DISPLAY_FNAME))
385	    move_file(".", DISPLAY_FNAME, LogDir);
386	for (p = Plist.head; p ; p = p->next) {
387	    if (p->type != PLIST_PKGDEP)
388		continue;
389	    if (Verbose)
390		printf("Attempting to record dependency on package `%s'\n",
391		       p->name);
392	    sprintf(contents, "%s/%s/%s",
393	    	    (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR,
394	    	    basename_of(p->name), REQUIRED_BY_FNAME);
395	    cfile = fopen(contents, "a");
396	    if (!cfile) {
397		whinge("Can't open dependency file '%s'!\n\tDependency registration incomplete.",
398		   contents);
399		continue;
400	    }
401	    fprintf(cfile, "%s\n", basename_of(PkgName));
402	    if (fclose(cfile) == EOF)
403		warn("Cannot properly close file %s", contents);
404	}
405	if (Verbose)
406	    printf("Package %s registered in %s\n", PkgName, LogDir);
407    }
408
409    if (p = find_plist(&Plist, PLIST_DISPLAY)) {
410	FILE *fp;
411	char buf[BUFSIZ];
412	fp = fopen(p->name, "r");
413	if (fp) {
414	    putc('\n', stdout);
415	    while (fgets(buf, sizeof(buf), fp))
416		fputs(buf, stdout);
417	    putc('\n', stdout);
418	    (void) fclose(fp);
419	} else
420	    warn("Cannot open display file `%s'.", p->name);
421    }
422
423    goto success;
424
425 bomb:
426    code = 1;
427    goto success;
428
429 fail:
430    /* Nuke the whole (installed) show, XXX but don't clean directories */
431    if (!Fake)
432	delete_package(FALSE, FALSE, &Plist);
433
434 success:
435    /* delete the packing list contents */
436    leave_playpen();
437    if (isTMP)
438	unlink(isTMP);
439    return code;
440}
441
442static int
443sanity_check(char *pkg)
444{
445    PackingList p;
446    int code = 0;
447
448    if (!fexists(CONTENTS_FNAME)) {
449	whinge("Package %s has no CONTENTS file!", pkg);
450	code = 1;
451    }
452    else if (!fexists(COMMENT_FNAME)) {
453	whinge("Package %s has no COMMENT file!", pkg);
454	code = 1;
455    }
456    else if (!fexists(DESC_FNAME)) {
457	whinge("Package %s has no DESC file!", pkg);
458	code = 1;
459    }
460    return code;
461}
462
463void
464cleanup(int signo)
465{
466    if (signo)
467	printf("Signal %d received, cleaning up..\n", signo);
468    if (Plist.head) {
469	if (!Fake)
470	    delete_package(FALSE, FALSE, &Plist);
471	free_plist(&Plist);
472    }
473    if (!Fake && LogDir[0])
474	vsystem("%s -rf %s", REMOVE_CMD, LogDir);
475    leave_playpen();
476}
477