package.c revision 29539
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.63 1997/09/16 10:14:22 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
52/* Like package_extract, but assumes current media device */
53int
54package_add(char *name)
55{
56    if (!mediaVerify())
57	return DITEM_FAILURE;
58    return package_extract(mediaDevice, name, FALSE);
59}
60
61/* For use by dispatch */
62int
63packageAdd(dialogMenuItem *self)
64{
65    char *cp;
66
67    cp = variable_get(VAR_PACKAGE);
68    if (!cp) {
69	msgDebug("packageAdd:  No package name passed in package variable\n");
70	return DITEM_FAILURE;
71    }
72    else
73	return package_add(cp);
74}
75
76Boolean
77package_exists(char *name)
78{
79    char fname[FILENAME_MAX];
80    int status /* = vsystem("pkg_info -e %s", name) */;
81
82    /* XXX KLUDGE ALERT!  This makes evil assumptions about how XXX
83     * packages register themselves and should *really be done with
84     * `pkg_info -e <name>' except that this it's too slow for an
85     * item check routine.. :-(
86     */
87    snprintf(fname, FILENAME_MAX, "/var/db/pkg/%s", name);
88    status = access(fname, R_OK);
89    if (isDebug())
90	msgDebug("package check for %s returns %s.\n", name, status ? "failure" : "success");
91    return !status;
92}
93
94/* Extract a package based on a namespec and a media device */
95int
96package_extract(Device *dev, char *name, Boolean depended)
97{
98    char path[511];
99    int ret, last_msg = 0;
100    FILE *fp;
101
102    /* Check to make sure it's not already there */
103    if (package_exists(name))
104	return DITEM_SUCCESS;
105
106    /* If necessary, initialize the ldconfig hints */
107    if (!file_readable("/var/run/ld.so.hints"))
108	vsystem("ldconfig /usr/lib /usr/local/lib /usr/X11R6/lib");
109
110    if (!dev->init(dev)) {
111	msgConfirm("Unable to initialize media type for package extract.");
112	return DITEM_FAILURE;
113    }
114
115    /* Be initially optimistic */
116    ret = DITEM_SUCCESS | DITEM_RESTORE;
117    /* Make a couple of paranoid locations for temp files to live if user specified none */
118    if (!variable_get("PKG_TMPDIR")) {
119	/* Set it to a location with as much space as possible */
120	variable_set2("PKG_TMPDIR", "/usr/tmp");
121    }
122    Mkdir(variable_get("PKG_TMPDIR"));
123
124    if (!index(name, '/'))
125	sprintf(path, "packages/All/%s%s", name, strstr(name, ".tgz") ? "" : ".tgz");
126    else
127	sprintf(path, "%s%s", name, strstr(name, ".tgz") ? "" : ".tgz");
128    fp = dev->get(dev, path, TRUE);
129    if (fp) {
130	int i = 0, tot, pfd[2];
131	pid_t pid;
132
133	signal(SIGPIPE, catch_pipe);
134	msgNotify("Adding %s%s\nfrom %s", path, depended ? " (as a dependency)" : "", dev->name);
135	pipe(pfd);
136	pid = fork();
137	if (!pid) {
138	    dup2(pfd[0], 0); close(pfd[0]);
139	    dup2(DebugFD, 1);
140	    close(2);
141	    close(pfd[1]);
142	    i = execl("/usr/sbin/pkg_add", "/usr/sbin/pkg_add", "-", 0);
143	    if (isDebug())
144		msgDebug("pkg_add returns %d status\n", i);
145	}
146	else {
147	    char buf[BUFSIZ];
148	    WINDOW *w = savescr();
149	    struct timeval start, stop;
150
151	    close(pfd[0]);
152	    tot = 0;
153	    (void)gettimeofday(&start, (struct timezone *)0);
154
155	    while (!sigpipe_caught && (i = fread(buf, 1, BUFSIZ, fp)) > 0) {
156		int seconds;
157
158		tot += i;
159		/* Print statistics about how we're doing */
160		(void) gettimeofday(&stop, (struct timezone *)0);
161		stop.tv_sec = stop.tv_sec - start.tv_sec;
162		stop.tv_usec = stop.tv_usec - start.tv_usec;
163		if (stop.tv_usec < 0)
164		    stop.tv_sec--, stop.tv_usec += 1000000;
165		seconds = stop.tv_sec + (stop.tv_usec / 1000000.0);
166		if (!seconds)
167		    seconds = 1;
168		if (seconds != last_msg) {
169		    last_msg = seconds;
170		    msgInfo("%10d bytes read from package %s @ %4.1f KBytes/second", tot, name, (tot / seconds) / 1024.0);
171		}
172		/* Write it out */
173		if (sigpipe_caught || write(pfd[1], buf, i) != i) {
174		    msgInfo("Write failure to pkg_add!  Package may be corrupt.");
175		    break;
176		}
177	    }
178	    close(pfd[1]);
179	    fclose(fp);
180	    if (sigpipe_caught)
181		msgInfo("pkg_add(1) apparently did not like the %s package.", name);
182	    else if (i == -1)
183		msgInfo("I/O error while reading in the %s package.", name);
184	    else
185		msgInfo("Package %s read successfully - waiting for pkg_add(1)", name);
186	    refresh();
187	    i = waitpid(pid, &tot, 0);
188	    if (sigpipe_caught || i < 0 || WEXITSTATUS(tot)) {
189		if (variable_get(VAR_NO_CONFIRM))
190		    msgNotify("Add of package %s aborted, error code %d -\n"
191			      "Please check the debug screen for more info.", name, WEXITSTATUS(tot));
192		else
193		    msgConfirm("Add of package %s aborted, error code %d -\n"
194			       "Please check the debug screen for more info.", name, WEXITSTATUS(tot));
195	    }
196	    else
197		msgNotify("Package %s was added successfully", name);
198
199	    /* Now catch any stragglers */
200	    while (wait3(&tot, WNOHANG, NULL) > 0);
201
202	    sleep(1);
203	    restorescr(w);
204	    sigpipe_caught = FALSE;
205	}
206    }
207    else {
208	dialog_clear_norefresh();
209	if (variable_get(VAR_NO_CONFIRM))
210	    msgNotify("Unable to fetch package %s from selected media.\n"
211		      "No package add will be done.", name);
212	else
213	    msgConfirm("Unable to fetch package %s from selected media.\n"
214		       "No package add will be done.", name);
215	ret = DITEM_FAILURE | DITEM_RESTORE;
216    }
217    return ret;
218}
219