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