package.c revision 16823
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.39 1996/06/25 04:28:23 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 fd, ret;
89
90    /* If necessary, initialize the ldconfig hints */
91    if (!file_readable("/var/run/ld.so.hints"))
92	vsystem("ldconfig /usr/lib /usr/local/lib /usr/X11R6/lib");
93
94    /* Check to make sure it's not already there */
95    if (package_exists(name))
96	return DITEM_SUCCESS;
97
98    if (!dev->init(dev)) {
99	msgConfirm("Unable to initialize media type for package extract.");
100	return DITEM_FAILURE;
101    }
102
103    /* Be initially optimistic */
104    ret = DITEM_SUCCESS | DITEM_RESTORE;
105    /* Make a couple of paranoid locations for temp files to live if user specified none */
106    if (!variable_get("PKG_TMPDIR")) {
107	Mkdir("/usr/tmp", NULL);
108	Mkdir("/var/tmp", NULL);
109	/* Set it to a location with as much space as possible */
110	variable_set2("PKG_TMPDIR", "/usr/tmp");
111    }
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    fd = dev->get(dev, path, TRUE);
118    if (fd >= 0) {
119	int i, tot, pfd[2];
120	pid_t pid;
121
122	msgNotify("Adding %s%s\nfrom %s", path, depended ? " (as a dependency)" : "", dev->name);
123        signal(SIGPIPE, catch_pipe);
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	    i = execl("/usr/sbin/pkg_add", "/usr/sbin/pkg_add", "-", 0);
132	    if (isDebug())
133		msgDebug("pkg_add returns %d status\n", i);
134	}
135	else {
136	    char buf[BUFSIZ];
137	    WINDOW *w = savescr();
138	    struct timeval start, stop;
139
140	    close(pfd[0]);
141	    tot = 0;
142	    (void)gettimeofday(&start, (struct timezone *)0);
143
144	    while (!sigpipe_caught && (i = read(fd, buf, BUFSIZ)) > 0) {
145		int seconds;
146
147		tot += i;
148		/* Print statistics about how we're doing */
149		(void) gettimeofday(&stop, (struct timezone *)0);
150		stop.tv_sec = stop.tv_sec - start.tv_sec;
151		stop.tv_usec = stop.tv_usec - start.tv_usec;
152		if (stop.tv_usec < 0)
153		    stop.tv_sec--, stop.tv_usec += 1000000;
154		seconds = stop.tv_sec + (stop.tv_usec / 1000000.0);
155		if (!seconds)
156		    seconds = 1;
157		msgInfo("%d bytes read from package %s, %d KBytes/second", tot, name, (tot / seconds) / 1024);
158		/* Write it out */
159		if (write(pfd[1], buf, i) != i) {
160		    msgInfo("Write failure to pkg_add!  Package may be corrupt.");
161		    break;
162		}
163	    }
164	    close(pfd[1]);
165	    dev->close(dev, fd);
166	    if (sigpipe_caught)
167		msgDebug("Caught SIGPIPE while trying to install the %s package.\n", name);
168	    else
169		msgInfo("Package %s read successfully - waiting for pkg_add", name);
170	    refresh();
171	    i = waitpid(pid, &tot, 0);
172	    if (sigpipe_caught || i < 0 || WEXITSTATUS(tot)) {
173		if (variable_get(VAR_NO_CONFIRM))
174		    msgNotify("Add of package %s aborted due to some error -\n"
175			      "Please check the debug screen for more info.", name);
176		else
177		    msgConfirm("Add of package %s aborted due to some error -\n"
178			      "Please check the debug screen for more info.", name);
179	    }
180	    else
181		msgNotify("Package %s was added successfully", name);
182	    sleep(1);
183	    restorescr(w);
184	    sigpipe_caught = FALSE;
185	}
186    }
187    else {
188	msgDebug("pkg_extract: get operation returned %d\n", fd);
189	dialog_clear();
190	if (variable_get(VAR_NO_CONFIRM))
191	    msgNotify("Unable to fetch package %s from selected media.\n"
192		      "No package add will be done.", name);
193	else {
194	    msgConfirm("Unable to fetch package %s from selected media.\n"
195		       "No package add will be done.", name);
196	}
197	ret = DITEM_FAILURE | DITEM_RESTORE;
198    }
199    return ret;
200}
201