2 * The new sysinstall program.
4 * This is probably the last program in the `sysinstall' line - the next
5 * generation being essentially a complete rewrite.
8 * Jordan Hubbard. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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.
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
36 #include "sysinstall.h"
37 #include <sys/errno.h>
39 #include <sys/param.h>
40 #include <sys/mount.h>
43 static Boolean sigpipe_caught;
48 sigpipe_caught = TRUE;
53 /* Like package_extract, but assumes current media device and chases deps */
55 package_add(char *name)
58 int i, current, low, high;
63 if (!DEVICE_INIT(mediaDevice))
66 i = index_initialize("packages/INDEX");
67 if (DITEM_STATUS(i) != DITEM_SUCCESS)
70 tmp = index_search(&Top, name, &tmp);
77 for (current = low; current <= high; current++)
78 i = index_extract(mediaDevice, &Top, tmp, FALSE, current);
81 msgConfirm("Sorry, package %s was not found in the INDEX.", name);
86 /* For use by dispatch */
88 packageAdd(dialogMenuItem *self)
92 cp = variable_get(VAR_PACKAGE);
94 msgDebug("packageAdd: No package name passed in package variable\n");
98 return package_add(cp);
102 package_installed(char *name)
104 char fname[FILENAME_MAX];
105 int status /* = vsystem("pkg_info -e %s", name) */;
107 /* XXX KLUDGE ALERT! This makes evil assumptions about how XXX
108 * packages register themselves and should *really be done with
109 * `pkg_info -e <name>' except that this is too slow for an
110 * item check routine.. :-(
112 snprintf(fname, FILENAME_MAX, "/var/db/pkg/%s", name);
113 status = access(fname, R_OK);
115 msgDebug("package check for %s returns %s.\n", name, status ? "failure" : "success");
119 /* Extract a package based on a namespec and a media device */
121 package_extract(Device *dev, char *name, Boolean depended)
123 char path[MAXPATHLEN];
124 const char *PkgExts[] = { "", ".tbz", ".tbz2", ".tgz" };
125 int last_msg, pathend, ret;
131 /* Check to make sure it's not already there */
132 if (package_installed(name))
133 return DITEM_SUCCESS;
135 if (!DEVICE_INIT(dev)) {
136 msgConfirm("Unable to initialize media type for package extract.");
137 return DITEM_FAILURE;
140 /* If necessary, initialize the ldconfig hints */
141 if (!file_readable("/var/run/ld-elf.so.hints"))
142 vsystem("ldconfig /usr/lib /usr/lib/compat /usr/local/lib");
144 /* Be initially optimistic */
146 /* Make a couple of paranoid locations for temp files to live if user specified none */
147 if (!variable_get(VAR_PKG_TMPDIR)) {
148 /* Set it to a location with as much space as possible */
149 variable_set2(VAR_PKG_TMPDIR, "/var/tmp", 0);
151 Mkdir(variable_get(VAR_PKG_TMPDIR));
152 vsystem("chmod 1777 %s", variable_get(VAR_PKG_TMPDIR));
154 if (!index(name, '/')) {
155 if (!strpbrk(name, "-_"))
156 pathend = snprintf(path, sizeof path, "packages/Latest/%s", name);
158 pathend = snprintf(path, sizeof path, "packages/All/%s", name);
161 pathend = snprintf(path, sizeof path, "%s", name);
163 /* We have a path, call the device strategy routine to get the file */
164 for (ext = 0 ; ext < sizeof PkgExts / sizeof PkgExts[0]; ++ext) {
165 strlcpy(path + pathend, PkgExts[ext], sizeof path - pathend);
166 if ((fp = DEVICE_GET(dev, path, TRUE)))
171 int i = 0, tot, pfd[2];
173 WINDOW *w = savescr();
175 sigpipe_caught = FALSE;
176 signal(SIGPIPE, catch_pipe);
178 dialog_clear_norefresh();
179 msgNotify("Adding %s%s\nfrom %s", path, depended ? " (as a dependency)" : "", dev->name);
183 dup2(pfd[0], 0); close(pfd[0]);
184 dup2(DebugFD, 1); dup2(1, 2);
187 /* Prevent pkg_add from wanting to interact in bad ways */
188 setenv("BATCH", "t", 1);
191 i = execl("/usr/sbin/pkg_add", "/usr/sbin/pkg_add", "-v", "-",
194 i = execl("/usr/sbin/pkg_add", "/usr/sbin/pkg_add", "-",
199 struct timeval start, stop;
203 (void)gettimeofday(&start, (struct timezone *)0);
205 while (!sigpipe_caught && (i = fread(buf, 1, BUFSIZ, fp)) > 0) {
209 /* Print statistics about how we're doing */
210 (void) gettimeofday(&stop, (struct timezone *)0);
211 stop.tv_sec = stop.tv_sec - start.tv_sec;
212 stop.tv_usec = stop.tv_usec - start.tv_usec;
213 if (stop.tv_usec < 0)
214 stop.tv_sec--, stop.tv_usec += 1000000;
215 seconds = stop.tv_sec + (stop.tv_usec / 1000000.0);
218 if (seconds != last_msg) {
220 msgInfo("%10d bytes read from package %s @ %4.1f KBytes/second", tot, name, (tot / seconds) / 1000.0);
223 if (sigpipe_caught || write(pfd[1], buf, i) != i) {
224 msgInfo("Write failure to pkg_add! Package may be corrupt.");
231 msgInfo("pkg_add(1) apparently did not like the %s package.", name);
233 msgInfo("I/O error while reading in the %s package.", name);
235 msgInfo("Package %s read successfully - waiting for pkg_add(1)", name);
237 i = waitpid(pid, &tot, 0);
238 dialog_clear_norefresh();
239 if (sigpipe_caught || i < 0 || WEXITSTATUS(tot)) {
241 if (variable_get(VAR_NO_CONFIRM))
242 msgNotify("Add of package %s aborted, error code %d -\n"
243 "Please check the debug screen for more info.", name, WEXITSTATUS(tot));
245 msgConfirm("Add of package %s aborted, error code %d -\n"
246 "Please check the debug screen for more info.", name, WEXITSTATUS(tot));
249 msgNotify("Package %s was added successfully", name);
251 /* Now catch any stragglers */
252 while (wait3(&tot, WNOHANG, NULL) > 0);
259 dialog_clear_norefresh();
260 if (variable_get(VAR_NO_CONFIRM))
261 msgNotify("Unable to fetch package %s from selected media.\n"
262 "No package add will be done.", name);
264 msgConfirm("Unable to fetch package %s from selected media.\n"
265 "No package add will be done.", name);
268 signal(SIGPIPE, SIG_IGN);