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