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