perform.c revision 1550
1#ifndef lint
2static const char *rcsid = "$Id: perform.c,v 1.7 1994/05/25 06:24:18 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 int sanity_check(char *);
32static char LogDir[FILENAME_MAX];
33
34
35int
36pkg_perform(char **pkgs)
37{
38    int i, err_cnt = 0;
39
40    signal(SIGINT, cleanup);
41    signal(SIGHUP, cleanup);
42
43    if (AddMode == SLAVE)
44	err_cnt = pkg_do(NULL);
45    else {
46	for (i = 0; pkgs[i]; i++)
47	    err_cnt += pkg_do(pkgs[i]);
48    }
49    return err_cnt;
50}
51
52static Package Plist;
53
54/* This is seriously ugly code following.  Written very fast! */
55static int
56pkg_do(char *pkg)
57{
58    char pkg_fullname[FILENAME_MAX];
59    FILE *cfile;
60    char *home;
61    int code = 0;
62    PackingList p;
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	/* If we're running in MASTER mode, just output the plist and return */
112	if (AddMode == MASTER) {
113	    printf("%s\n", where_playpen());
114	    write_plist(&Plist, stdout);
115	    return 0;
116	}
117    }
118    setenv(PKG_PREFIX_VNAME,
119	   (p = find_plist(&Plist, PLIST_CWD)) ? p->name : NULL, 1);
120    PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous";
121    if (fexists(REQUIRE_FNAME)) {
122	vsystem("chmod +x %s", REQUIRE_FNAME);	/* be sure */
123	if (Verbose)
124	    printf("Running requirements file first for %s..\n", PkgName);
125	if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) {
126	    whinge("Package %s fails requirements - not installed.",
127		   pkg_fullname);
128	    return 1;
129	}
130    }
131    if (!NoInstall && fexists(INSTALL_FNAME)) {
132	vsystem("chmod +x %s", INSTALL_FNAME);	/* make sure */
133	if (Verbose)
134	    printf("Running install with PRE-INSTALL for %s..\n", PkgName);
135	if (!Fake && vsystem("./%s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) {
136	    whinge("Install script returned error status.");
137	    goto fail;
138	}
139    }
140    extract_plist(home, &Plist);
141    if (!NoInstall && fexists(INSTALL_FNAME)) {
142	if (Verbose)
143	    printf("Running install with POST-INSTALL for %s..\n", PkgName);
144	if (!Fake && vsystem("./%s %s POST-INSTALL", INSTALL_FNAME, PkgName)) {
145	    whinge("Install script returned error status.");
146	    goto fail;
147	}
148    }
149    if (!NoRecord && !Fake) {
150	char contents[FILENAME_MAX];
151	FILE *cfile;
152
153	if (getuid() != 0)
154	    whinge("Not running as root - trying to record install anyway.");
155	if (!PkgName) {
156	    whinge("No package name!  Can't record package, sorry.");
157	    code = 1;
158	    goto success;	/* well, partial anyway */
159	}
160	sprintf(LogDir, "%s/%s", LOG_DIR, PkgName);
161	if (Verbose)
162	    printf("Attempting to record package into %s..\n", LogDir);
163	if (make_hierarchy(LogDir)) {
164	    whinge("Can't record package into '%s', you're on your own!",
165		   LogDir);
166	    bzero(LogDir, FILENAME_MAX);
167	    code = 1;
168	    goto success;	/* close enough for government work */
169	}
170	/* Make sure pkg_info can read the entry */
171	vsystem("chmod a+rx %s", LogDir);
172	if (fexists(DEINSTALL_FNAME))
173	    copy_file(".", DEINSTALL_FNAME, LogDir);
174	if (fexists(REQUIRE_FNAME))
175	    copy_file(".", REQUIRE_FNAME, LogDir);
176	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
177	cfile = fopen(contents, "w");
178	if (!cfile) {
179	    whinge("Can't open new contents file '%s'!  Can't register pkg.",
180		   contents);
181	    goto success; /* can't log, but still keep pkg */
182	}
183	write_plist(&Plist, cfile);
184	fclose(cfile);
185	copy_file(".", DESC_FNAME, LogDir);
186	copy_file(".", COMMENT_FNAME, LogDir);
187	if (Verbose)
188	    printf("Package %s registered in %s\n", PkgName, LogDir);
189    }
190    goto success;
191
192 fail:
193    /* Nuke the whole (installed) show */
194    if (!Fake)
195	delete_package(FALSE, &Plist);
196
197 success:
198    /* delete the packing list contents */
199    leave_playpen();
200
201    return code;
202}
203
204static int
205sanity_check(char *pkg)
206{
207    if (!fexists(CONTENTS_FNAME)) {
208	whinge("Package %s has no CONTENTS file!", pkg);
209	return 1;
210    }
211    if (!fexists(COMMENT_FNAME)) {
212	whinge("Package %s has no COMMENT file!", pkg);
213	return 1;
214    }
215    if (!fexists(DESC_FNAME)) {
216	whinge("Package %s has no DESC file!", pkg);
217	return 1;
218    }
219    return 0;
220}
221
222void
223cleanup(int signo)
224{
225    if (signo)
226	printf("Signal %d received, cleaning up..\n", signo);
227    if (Plist.head) {
228	if (!Fake)
229	    delete_package(FALSE, &Plist);
230	free_plist(&Plist);
231    }
232    if (!Fake && LogDir[0])
233	vsystem("%s -rf %s", REMOVE_CMD, LogDir);
234    leave_playpen();
235}
236