package.c revision 47055
1/*
2 * The new sysinstall program.
3 *
4 * This is probably the last program in the `sysinstall' line - the next
5 * generation being essentially a complete rewrite.
6 *
7 * $Id: package.c,v 1.70 1999/05/12 07:12:01 jkh Exp $
8 *
9 * Copyright (c) 1995
10 *	Jordan Hubbard.  All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer,
17 *    verbatim and that no modifications are made prior to this
18 *    point in the file.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 */
36
37#include "sysinstall.h"
38#include <sys/errno.h>
39#include <sys/time.h>
40#include <sys/param.h>
41#include <sys/mount.h>
42#include <sys/stat.h>
43
44static Boolean sigpipe_caught = FALSE;
45
46static void
47catch_pipe(int sig)
48{
49    sigpipe_caught = TRUE;
50}
51
52extern PkgNode Top;
53
54/* Like package_extract, but assumes current media device and chases deps */
55int
56package_add(char *name)
57{
58    PkgNodePtr tmp;
59    int i;
60
61    if (!mediaVerify())
62	return DITEM_FAILURE;
63    i = index_initialize("packages/INDEX");
64    if (DITEM_STATUS(i) != DITEM_SUCCESS)
65	return i;
66    tmp = index_search(&Top, name, NULL);
67    if (tmp)
68	return index_extract_one(mediaDevice, &Top, tmp, FALSE);
69    else {
70	msgConfirm("Sorry, package %s was not found in the INDEX.", name);
71	return DITEM_FAILURE | DITEM_RESTORE;
72    }
73}
74
75/* For use by dispatch */
76int
77packageAdd(dialogMenuItem *self)
78{
79    char *cp;
80
81    cp = variable_get(VAR_PACKAGE);
82    if (!cp) {
83	msgDebug("packageAdd:  No package name passed in package variable\n");
84	return DITEM_FAILURE;
85    }
86    else
87	return package_add(cp);
88}
89
90Boolean
91package_exists(char *name)
92{
93    char fname[FILENAME_MAX];
94    int status /* = vsystem("pkg_info -e %s", name) */;
95
96    /* XXX KLUDGE ALERT!  This makes evil assumptions about how XXX
97     * packages register themselves and should *really be done with
98     * `pkg_info -e <name>' except that this it's too slow for an
99     * item check routine.. :-(
100     */
101    snprintf(fname, FILENAME_MAX, "/var/db/pkg/%s", name);
102    status = access(fname, R_OK);
103    if (isDebug())
104	msgDebug("package check for %s returns %s.\n", name, status ? "failure" : "success");
105    return !status;
106}
107
108/* Extract a package based on a namespec and a media device */
109int
110package_extract(Device *dev, char *name, Boolean depended)
111{
112    char path[511];
113    int ret, last_msg = 0;
114    FILE *fp;
115
116    /* Check to make sure it's not already there */
117    if (package_exists(name))
118	return DITEM_SUCCESS;
119
120    /* If necessary, initialize the ldconfig hints */
121    if (!file_readable("/var/run/ld.so.hints"))
122	vsystem("ldconfig /usr/lib /usr/local/lib /usr/X11R6/lib");
123
124    if (!dev->init(dev)) {
125	msgConfirm("Unable to initialize media type for package extract.");
126	return DITEM_FAILURE;
127    }
128
129    /* Be initially optimistic */
130    ret = DITEM_SUCCESS | DITEM_RESTORE;
131    /* Make a couple of paranoid locations for temp files to live if user specified none */
132    if (!variable_get(VAR_PKG_TMPDIR)) {
133	/* Set it to a location with as much space as possible */
134	variable_set2(VAR_PKG_TMPDIR, "/usr/tmp", 0);
135    }
136    Mkdir(variable_get(VAR_PKG_TMPDIR));
137    vsystem("chmod 1777 %s", variable_get(VAR_PKG_TMPDIR));
138
139    if (!strpbrk(name, "-_"))
140	sprintf(path, "packages/Latest/%s.tgz", name);
141    else if (!index(name, '/'))
142	sprintf(path, "packages/All/%s%s", name, strstr(name, ".tgz") ? "" : ".tgz");
143    else
144	sprintf(path, "%s%s", name, strstr(name, ".tgz") ? "" : ".tgz");
145
146    /* We have a path, call the device strategy routine to get the file */
147    fp = dev->get(dev, path, TRUE);
148    if (fp) {
149	int i = 0, tot, pfd[2];
150	pid_t pid;
151
152	signal(SIGPIPE, catch_pipe);
153	msgNotify("Adding %s%s\nfrom %s", path, depended ? " (as a dependency)" : "", dev->name);
154	pipe(pfd);
155	pid = fork();
156	if (!pid) {
157	    dup2(pfd[0], 0); close(pfd[0]);
158	    dup2(DebugFD, 1);
159	    close(2);
160	    close(pfd[1]);
161	    i = execl("/usr/sbin/pkg_add", "/usr/sbin/pkg_add", "-", 0);
162	    if (isDebug())
163		msgDebug("pkg_add returns %d status\n", i);
164	}
165	else {
166	    char buf[BUFSIZ];
167	    WINDOW *w = savescr();
168	    struct timeval start, stop;
169
170	    close(pfd[0]);
171	    tot = 0;
172	    (void)gettimeofday(&start, (struct timezone *)0);
173
174	    while (!sigpipe_caught && (i = fread(buf, 1, BUFSIZ, fp)) > 0) {
175		int seconds;
176
177		tot += i;
178		/* Print statistics about how we're doing */
179		(void) gettimeofday(&stop, (struct timezone *)0);
180		stop.tv_sec = stop.tv_sec - start.tv_sec;
181		stop.tv_usec = stop.tv_usec - start.tv_usec;
182		if (stop.tv_usec < 0)
183		    stop.tv_sec--, stop.tv_usec += 1000000;
184		seconds = stop.tv_sec + (stop.tv_usec / 1000000.0);
185		if (!seconds)
186		    seconds = 1;
187		if (seconds != last_msg) {
188		    last_msg = seconds;
189		    msgInfo("%10d bytes read from package %s @ %4.1f KBytes/second", tot, name, (tot / seconds) / 1024.0);
190		}
191		/* Write it out */
192		if (sigpipe_caught || write(pfd[1], buf, i) != i) {
193		    msgInfo("Write failure to pkg_add!  Package may be corrupt.");
194		    break;
195		}
196	    }
197	    close(pfd[1]);
198	    fclose(fp);
199	    if (sigpipe_caught)
200		msgInfo("pkg_add(1) apparently did not like the %s package.", name);
201	    else if (i == -1)
202		msgInfo("I/O error while reading in the %s package.", name);
203	    else
204		msgInfo("Package %s read successfully - waiting for pkg_add(1)", name);
205	    refresh();
206	    i = waitpid(pid, &tot, 0);
207	    if (sigpipe_caught || i < 0 || WEXITSTATUS(tot)) {
208		ret = DITEM_FAILURE | DITEM_RESTORE;
209		if (variable_get(VAR_NO_CONFIRM))
210		    msgNotify("Add of package %s aborted, error code %d -\n"
211			      "Please check the debug screen for more info.", name, WEXITSTATUS(tot));
212		else
213		    msgConfirm("Add of package %s aborted, error code %d -\n"
214			       "Please check the debug screen for more info.", name, WEXITSTATUS(tot));
215	    }
216	    else
217		msgNotify("Package %s was added successfully", name);
218
219	    /* Now catch any stragglers */
220	    while (wait3(&tot, WNOHANG, NULL) > 0);
221
222	    sleep(1);
223	    restorescr(w);
224	    sigpipe_caught = FALSE;
225	}
226    }
227    else {
228	dialog_clear_norefresh();
229	if (variable_get(VAR_NO_CONFIRM))
230	    msgNotify("Unable to fetch package %s from selected media.\n"
231		      "No package add will be done.", name);
232	else
233	    msgConfirm("Unable to fetch package %s from selected media.\n"
234		       "No package add will be done.", name);
235	ret = DITEM_FAILURE | DITEM_RESTORE;
236    }
237    return ret;
238}
239