perform.c revision 7998
1#ifndef lint
2static const char *rcsid = "$Id: perform.c,v 1.18 1995/04/22 07:40:54 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/* This is seriously ugly code following.  Written very fast! */
56static int
57pkg_do(char *pkg)
58{
59    char pkg_fullname[FILENAME_MAX];
60    char home[FILENAME_MAX];
61    char extract_contents[FILENAME_MAX];
62    char *where_to, *tmp;
63    FILE *cfile;
64    int code = 0;
65    PackingList p;
66    struct stat sb;
67
68    /* Reset some state */
69    if (Plist.head)
70	free_plist(&Plist);
71    LogDir[0] = '\0';
72    if (AddMode == SLAVE) {
73	char tmp_dir[FILENAME_MAX];
74
75	fgets(tmp_dir, FILENAME_MAX, stdin);
76	tmp_dir[strlen(tmp_dir) - 1] = '\0'; /* pesky newline! */
77	if (chdir(tmp_dir) == FAIL) {
78	    whinge("pkg_add in SLAVE mode can't chdir to %s.", tmp_dir);
79	    return 1;
80	}
81	read_plist(&Plist, stdin);
82    }
83    else {
84	if (!getcwd(home, FILENAME_MAX))
85	    upchuck("getcwd");
86
87	if (pkg[0] == '/')	/* full pathname? */
88	    strcpy(pkg_fullname, pkg);
89	else
90	    sprintf(pkg_fullname, "%s/%s", home, pkg);
91	if (!fexists(pkg_fullname)) {
92	    whinge("Can't find package '%s'.", pkg_fullname);
93	    return 1;
94	}
95	Home = make_playpen(PlayPen, 0);
96	sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
97	if (unpack(pkg_fullname, extract_contents)) {
98	    whinge("Unable to extract table of contents file from `%s' - not a package?.", pkg_fullname);
99	    goto bomb;
100	}
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	/*
110	 * If we have a prefix, delete the first one we see and add this
111	 * one in place of it.
112	 */
113	if (Prefix) {
114	    delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
115	    add_plist_top(&Plist, PLIST_CWD, Prefix);
116	}
117
118	/* Extract directly rather than moving?  Oh goodie! */
119	if (find_plist_option(&Plist, "extract-in-place")) {
120	    if (Verbose)
121		printf("Doing in-place extraction for %s\n", pkg_fullname);
122	    p = find_plist(&Plist, PLIST_CWD);
123	    if (p) {
124		if (!isdir(p->name) && !NoInstall) {
125		    if (Verbose)
126			printf("Desired prefix of %s does not exist, creating..\n", p->name);
127		    vsystem("mkdir -p %s", p->name);
128		    if (chdir(p->name)) {
129			whinge("Unable to change directory to `%s' - no permission?", p->name);
130			perror("chdir");
131			leave_playpen();
132			return 1;
133		    }
134		}
135		where_to = p->name;
136	    }
137	    else {
138		whinge("No prefix specified in `%s' - this is a bad package!",
139		       pkg_fullname);
140		leave_playpen();
141		return 1;
142	    }
143	}
144	else
145	    where_to = PlayPen;
146	/*
147	 * Apply a crude heuristic to see how much space the package will
148	 * take up once it's unpacked.  I've noticed that most packages
149	 * compress an average of 75%, so multiply by 4 for good measure.
150	 */
151	if (stat(pkg_fullname, &sb) == FAIL) {
152	    whinge("Can't stat package file '%s'.", pkg_fullname);
153	    return 1;
154	}
155
156	if (min_free(where_to) < sb.st_size * 4) {
157	    whinge("Projected size of %d exceeds free space in %s.",
158		   sb.st_size * 4, where_to);
159	    whinge("Not extracting %s, sorry!", pkg_fullname);
160	    goto bomb;
161	}
162
163	/* If this is a direct extract and we didn't want it, stop now */
164	if (where_to != PlayPen && NoInstall)
165	    goto success;
166
167	setenv(PKG_PREFIX_VNAME,
168	       (p = find_plist(&Plist, PLIST_CWD)) ? p->name : NULL, 1);
169	/* Protect against old packages with bogus @name fields */
170	PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous";
171
172	/* See if we're already registered */
173	sprintf(LogDir, "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR,
174		basename_of(PkgName));
175	if (isdir(LogDir)) {
176	    char tmp[FILENAME_MAX];
177
178	    whinge("Package `%s' already recorded as installed.\n", PkgName);
179	    code = 1;
180	    goto success;	/* close enough for government work */
181        }
182
183	/* Finally unpack the whole mess */
184	if (unpack(pkg_fullname, NULL)) {
185	    whinge("Unable to extract `%s'!", pkg_fullname);
186	    goto bomb;
187	}
188
189	if (sanity_check(pkg_fullname))
190	    goto bomb;
191
192	/* If we're running in MASTER mode, just output the plist and return */
193	if (AddMode == MASTER) {
194	    printf("%s\n", where_playpen());
195	    write_plist(&Plist, stdout);
196	    return 0;
197	}
198    }
199    for (p = Plist.head; p ; p = p->next) {
200	if (p->type != PLIST_PKGDEP)
201	    continue;
202	if (Verbose)
203	    printf("Package `%s' depends on `%s'", PkgName, p->name);
204	if (!Fake && vsystem("pkg_info -e %s", p->name)) {
205	    char *cp, tmp[FILENAME_MAX], path[FILENAME_MAX*2];
206
207	    if (Verbose)
208		printf(" which is not currently loaded");
209	    cp = getenv("PKG_PATH");
210	    if (!cp)
211		cp = Home;
212	    strcpy(path, cp);
213	    cp = path;
214	    while (cp) {
215		char *cp2 = strsep(&cp, ":");
216
217		sprintf(tmp, "%s/%s.tgz", cp2 ? cp2 : cp, p->name);
218		if (fexists(tmp))
219		    break;
220	    }
221	    if (fexists(tmp)) {
222		if (Verbose)
223		    printf(" but was found - loading:\n");
224		if (vsystem("pkg_add %s", tmp)) {
225		    whinge("Autoload of dependency package `%s' failed!%s",
226			   p->name, Force ? " (proceeding anyway)" : "");
227		    if (!Force)
228			code++;
229		}
230		else if (Verbose)
231		    printf("\t`%s' loaded successfully.\n", p->name);
232	    }
233	    else {
234	    	printf("and was not found%s.\n",
235	    	       Force ? " (proceeding anyway)" : "");
236	    	if (!Force)
237		    code++;
238	    }
239	}
240        else if (Verbose)
241	    printf(" - already installed.\n");
242    }
243    if (code != 0)
244	goto success;	/* close enough for government work */
245
246    if (fexists(REQUIRE_FNAME)) {
247	vsystem("chmod +x %s", REQUIRE_FNAME);	/* be sure */
248	if (Verbose)
249	    printf("Running requirements file first for %s..\n", PkgName);
250	if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) {
251	    whinge("Package %s fails requirements %s",
252		   pkg_fullname,
253		   Force ? "installing anyway" : "- not installed.");
254	    if (!Force) {
255		code = 1;
256		goto success;	/* close enough for government work */
257	    }
258	}
259    }
260    if (!NoInstall && fexists(INSTALL_FNAME)) {
261	vsystem("chmod +x %s", INSTALL_FNAME);	/* make sure */
262	if (Verbose)
263	    printf("Running install with PRE-INSTALL for %s..\n", PkgName);
264	if (!Fake && vsystem("./%s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) {
265	    whinge("Install script returned error status.");
266	    unlink(INSTALL_FNAME);
267	    code = 1;
268	    goto success;		/* nothing to uninstall yet */
269	}
270    }
271    extract_plist(home, &Plist);
272    if (!NoInstall && fexists(MTREE_FNAME)) {
273	if (Verbose)
274	    printf("Running mtree for %s..\n", PkgName);
275	p = find_plist(&Plist, PLIST_CWD);
276	if (Verbose)
277	    printf("mtree -u -f %s -d -e -p %s\n", MTREE_FNAME,
278		   p ? p->name : "/");
279	if (!Fake) {
280	    if (vsystem("/usr/sbin/mtree -u -f %s -d -e -p %s",
281			MTREE_FNAME, p ? p->name : "/")) {
282		perror("error in the execution of mtree");
283		unlink(MTREE_FNAME);
284		goto fail;
285	    }
286	}
287	unlink(MTREE_FNAME);
288    }
289    if (!NoInstall && fexists(INSTALL_FNAME)) {
290	if (Verbose)
291	    printf("Running install with POST-INSTALL for %s..\n", PkgName);
292	if (!Fake && vsystem("./%s %s POST-INSTALL", INSTALL_FNAME, PkgName)) {
293	    whinge("Install script returned error status.");
294	    unlink(INSTALL_FNAME);
295	    code = 1;
296	    goto fail;
297	}
298	unlink(INSTALL_FNAME);
299    }
300    if (!NoRecord && !Fake) {
301	char contents[FILENAME_MAX];
302	FILE *cfile;
303
304	umask(022);
305	if (getuid() != 0)
306	    whinge("Not running as root - trying to record install anyway.");
307	if (!PkgName) {
308	    whinge("No package name!  Can't record package, sorry.");
309	    code = 1;
310	    goto success;	/* well, partial anyway */
311	}
312	sprintf(LogDir, "%s/%s",
313		(tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR,
314    	    	basename_of(PkgName));
315	if (Verbose)
316	    printf("Attempting to record package into %s..\n", LogDir);
317	if (make_hierarchy(LogDir)) {
318	    whinge("Can't record package into '%s', you're on your own!",
319		   LogDir);
320	    bzero(LogDir, FILENAME_MAX);
321	    code = 1;
322	    goto success;	/* close enough for government work */
323	}
324	/* Make sure pkg_info can read the entry */
325	vsystem("chmod a+rx %s", LogDir);
326	if (fexists(DEINSTALL_FNAME))
327	    move_file(".", DEINSTALL_FNAME, LogDir);
328	if (fexists(REQUIRE_FNAME))
329	    move_file(".", REQUIRE_FNAME, LogDir);
330	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
331	cfile = fopen(contents, "w");
332	if (!cfile) {
333	    whinge("Can't open new contents file '%s'!  Can't register pkg.",
334		   contents);
335	    goto success; /* can't log, but still keep pkg */
336	}
337	write_plist(&Plist, cfile);
338	fclose(cfile);
339	move_file(".", DESC_FNAME, LogDir);
340	move_file(".", COMMENT_FNAME, LogDir);
341	if (fexists(DISPLAY_FNAME))
342	    move_file(".", DISPLAY_FNAME, LogDir);
343	for (p = Plist.head; p ; p = p->next) {
344	    if (p->type != PLIST_PKGDEP)
345		continue;
346	    if (Verbose)
347		printf("Attempting to record dependency on package `%s'\n",
348		       p->name);
349	    sprintf(contents, "%s/%s/%s",
350	    	    (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR,
351	    	    basename_of(p->name), REQUIRED_BY_FNAME);
352	    cfile = fopen(contents, "a");
353	    if (!cfile) {
354		whinge("Can't open dependency file '%s'!\n\tDependency registration incomplete.",
355		   contents);
356		continue;
357	    }
358	    fprintf(cfile, "%s\n", basename_of(PkgName));
359	    if (fclose(cfile) == EOF)
360		warn("Cannot properly close file %s", contents);
361	}
362	if (Verbose)
363	    printf("Package %s registered in %s\n", PkgName, LogDir);
364    }
365
366    if (p = find_plist(&Plist, PLIST_DISPLAY)) {
367	FILE *fp;
368	char buf[BUFSIZ];
369	fp = fopen(p->name, "r");
370	if (fp) {
371	    putc('\n', stdout);
372	    while (fgets(buf, sizeof(buf), fp))
373		fputs(buf, stdout);
374	    putc('\n', stdout);
375	    (void) fclose(fp);
376	} else
377	    warn("Cannot open display file `%s'.", p->name);
378    }
379
380    goto success;
381
382 bomb:
383    code = 1;
384    goto success;
385
386 fail:
387    /* Nuke the whole (installed) show, XXX but don't clean directories */
388    if (!Fake)
389	delete_package(FALSE, FALSE, &Plist);
390
391 success:
392    /* delete the packing list contents */
393    leave_playpen();
394
395    return code;
396}
397
398static int
399sanity_check(char *pkg)
400{
401    if (!fexists(CONTENTS_FNAME)) {
402	whinge("Package %s has no CONTENTS file!", pkg);
403	return 1;
404    }
405    if (!fexists(COMMENT_FNAME)) {
406	whinge("Package %s has no COMMENT file!", pkg);
407	return 1;
408    }
409    if (!fexists(DESC_FNAME)) {
410	whinge("Package %s has no DESC file!", pkg);
411	return 1;
412    }
413    return 0;
414}
415
416void
417cleanup(int signo)
418{
419    if (signo)
420	printf("Signal %d received, cleaning up..\n", signo);
421    if (Plist.head) {
422	if (!Fake)
423	    delete_package(FALSE, FALSE, &Plist);
424	free_plist(&Plist);
425    }
426    if (!Fake && LogDir[0])
427	vsystem("%s -rf %s", REMOVE_CMD, LogDir);
428    leave_playpen();
429}
430