perform.c revision 382
1#ifndef lint
2static const char *rcsid = "$Id: perform.c,v 1.8 1993/09/04 05:06:29 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
30static int pkg_do(char *);
31static char *find_name(Package *);
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    FILE *cfile;
61    char *home;
62    int code = 0;
63
64    /* Reset some state */
65    if (Plist.head)
66	free_plist(&Plist);
67    LogDir[0] = '\0';
68    if (AddMode == SLAVE) {
69	char tmp_dir[FILENAME_MAX];
70
71	fgets(tmp_dir, FILENAME_MAX, stdin);
72	tmp_dir[strlen(tmp_dir) - 1] = '\0'; /* pesky newline! */
73	if (chdir(tmp_dir) == FAIL) {
74	    whinge("pkg_add in SLAVE mode can't chdir to %s.", tmp_dir);
75	    return 1;
76	}
77	read_plist(&Plist, stdin);
78    }
79    else {
80	home = make_playpen(PlayPen);
81	if (pkg[0] == '/')	/* full pathname? */
82	    strcpy(pkg_fullname, pkg);
83	else
84	    sprintf(pkg_fullname, "%s/%s", home, pkg);
85	if (!fexists(pkg_fullname)) {
86	    whinge("Can't open package '%s'.", pkg_fullname);
87	    return 1;
88	}
89
90	if (unpack(pkg_fullname, NULL))
91	    return 1;
92
93	if (sanity_check(pkg_fullname))
94	    return 1;
95
96	cfile = fopen(CONTENTS_FNAME, "r");
97	if (!cfile) {
98	    whinge("Unable to open %s file.", CONTENTS_FNAME);
99	    goto fail;
100	}
101	read_plist(&Plist, cfile);
102	fclose(cfile);
103	if (Prefix) {
104	    /*
105	     * If we have a prefix, delete the first one we see and add this
106	     * one in place of it.
107	     */
108	    delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
109	    add_plist_top(&Plist, PLIST_CWD, Prefix);
110	}
111	/* Just to be safe - overridden if package has made a choice */
112	else
113	    add_plist_top(&Plist, PLIST_CWD, home);
114	/* If we're running in MASTER mode, just output the plist and return */
115	if (AddMode == MASTER) {
116	    printf("%s\n", where_playpen());
117	    write_plist(&Plist, stdout);
118	    return;
119	}
120    }
121    PkgName = find_name(&Plist);
122    if (fexists(REQUIRE_FNAME)) {
123	vsystem("chmod +x %s", REQUIRE_FNAME);	/* be sure */
124	if (Verbose)
125	    printf("Running requirements file first for %s..\n", PkgName);
126	if (!Fake && vsystem("%s %s INSTALL", REQUIRE_FNAME, PkgName)) {
127	    whinge("Package %s fails requirements - not installed.",
128		   pkg_fullname);
129	    goto fail;
130	}
131    }
132    if (!NoInstall && fexists(INSTALL_FNAME)) {
133	vsystem("chmod +x %s", INSTALL_FNAME);	/* make sure */
134	if (Verbose)
135	    printf("Running install with PRE-INSTALL for %s..\n", PkgName);
136	if (!Fake && vsystem("%s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) {
137	    whinge("Install script returned error status.");
138	    goto fail;
139	}
140    }
141    extract_plist(home, &Plist);
142    if (!NoInstall && fexists(INSTALL_FNAME)) {
143	if (Verbose)
144	    printf("Running install with POST-INSTALL for %s..\n", PkgName);
145	if (!Fake && vsystem("%s %s POST-INSTALL", INSTALL_FNAME, PkgName)) {
146	    whinge("Install script returned error status.");
147	    goto fail;
148	}
149    }
150    if (!NoRecord && !Fake) {
151	char contents[FILENAME_MAX];
152	FILE *cfile;
153
154	if (getuid() != 0)
155	    whinge("Not running as root - trying to record install anyway.");
156	if (!PkgName) {
157	    whinge("No package name!  Can't record package, sorry.");
158	    code = 1;
159	    goto success;	/* well, partial anyway */
160	}
161	sprintf(LogDir, "%s/%s", LOG_DIR, PkgName);
162	if (Verbose)
163	    printf("Attempting to record package into %s..\n", LogDir);
164	if (make_hierarchy(LogDir)) {
165	    whinge("Can't record package into '%s', you're on your own!",
166		   LogDir);
167	    bzero(LogDir, FILENAME_MAX);
168	    code = 1;
169	    goto success;	/* close enough for government work */
170	}
171	if (fexists(DEINSTALL_FNAME))
172	    copy_file(".", DEINSTALL_FNAME, LogDir);
173	if (fexists(REQUIRE_FNAME))
174	    copy_file(".", REQUIRE_FNAME, LogDir);
175	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
176	cfile = fopen(contents, "w");
177	if (!cfile) {
178	    whinge("Can't open new contents file '%s'!  Can't register pkg.",
179		   contents);
180	    goto success; /* can't log, but still keep pkg */
181	}
182	write_plist(&Plist, cfile);
183	fclose(cfile);
184	copy_file(".", DESC_FNAME, LogDir);
185	copy_file(".", COMMENT_FNAME, LogDir);
186	if (Verbose)
187	    printf("Package %s registered in %s\n", PkgName, LogDir);
188    }
189    goto success;
190
191 fail:
192    /* Nuke the whole (installed) show */
193    if (!Fake)
194	delete_package(FALSE, &Plist);
195
196 success:
197    /* delete the packing list contents */
198    leave_playpen();
199
200    return code;
201}
202
203static int
204sanity_check(char *pkg)
205{
206    if (!fexists(CONTENTS_FNAME)) {
207	whinge("Package %s has no CONTENTS file!", pkg);
208	return 1;
209    }
210    if (!fexists(COMMENT_FNAME)) {
211	whinge("Package %s has no COMMENT file!", pkg);
212	return 1;
213    }
214    if (!fexists(DESC_FNAME)) {
215	whinge("Package %s has no DESC file!", pkg);
216	return 1;
217    }
218    return 0;
219}
220
221static char *
222find_name(Package *pkg)
223{
224    PackingList p = pkg->head;
225
226    while (p) {
227	if (p->type == PLIST_NAME)
228	    return p->name;
229	p = p->next;
230    }
231    return "anonymous";
232}
233
234void
235cleanup(int signo)
236{
237    if (signo)
238	printf("Signal %d received, cleaning up..\n", signo);
239    if (Plist.head) {
240	if (!Fake)
241	    delete_package(FALSE, &Plist);
242	free_plist(&Plist);
243    }
244    if (!Fake && LogDir[0])
245	vsystem("%s -rf %s", REMOVE_CMD, LogDir);
246    leave_playpen();
247}
248