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)
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);
72 return index_extract(mediaDevice, &Top, tmp, FALSE);
74 msgConfirm("Sorry, package %s was not found in the INDEX.", name);
79 /* For use by dispatch */
81 packageAdd(dialogMenuItem *self)
85 cp = variable_get(VAR_PACKAGE);
87 msgDebug("packageAdd: No package name passed in package variable\n");
91 return package_add(cp);
95 package_installed(char *name)
97 char fname[FILENAME_MAX];
98 int status /* = vsystem("pkg_info -e %s", name) */;
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.. :-(
105 snprintf(fname, FILENAME_MAX, "/var/db/pkg/%s", name);
106 status = access(fname, R_OK);
108 msgDebug("package check for %s returns %s.\n", name, status ? "failure" : "success");
112 /* Extract a package based on a namespec and a media device */
114 package_extract(Device *dev, char *name, Boolean depended)
116 char path[MAXPATHLEN];
117 const char *PkgExts[] = { "", ".tbz", ".tbz2", ".tgz" };
118 int last_msg, pathend, ret;
124 /* Check to make sure it's not already there */
125 if (package_installed(name))
126 return DITEM_SUCCESS;
128 if (!DEVICE_INIT(dev)) {
129 msgConfirm("Unable to initialize media type for package extract.");
130 return DITEM_FAILURE;
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");
137 /* Be initially optimistic */
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);
144 Mkdir(variable_get(VAR_PKG_TMPDIR));
145 vsystem("chmod 1777 %s", variable_get(VAR_PKG_TMPDIR));
147 if (!index(name, '/')) {
148 if (!strpbrk(name, "-_"))
149 pathend = snprintf(path, sizeof path, "packages/Latest/%s", name);
151 pathend = snprintf(path, sizeof path, "packages/All/%s", name);
154 pathend = snprintf(path, sizeof path, "%s", name);
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)))
164 int i = 0, tot, pfd[2];
166 WINDOW *w = savescr();
168 sigpipe_caught = FALSE;
169 signal(SIGPIPE, catch_pipe);
171 dialog_clear_norefresh();
172 msgNotify("Adding %s%s\nfrom %s", path, depended ? " (as a dependency)" : "", dev->name);
176 dup2(pfd[0], 0); close(pfd[0]);
177 dup2(DebugFD, 1); dup2(1, 2);
180 /* Prevent pkg_add from wanting to interact in bad ways */
181 setenv("PACKAGE_BUILDING", "t", 1);
182 setenv("BATCH", "t", 1);
185 i = execl("/usr/sbin/pkg_add", "/usr/sbin/pkg_add", "-v", "-",
188 i = execl("/usr/sbin/pkg_add", "/usr/sbin/pkg_add", "-",
193 struct timeval start, stop;
197 (void)gettimeofday(&start, (struct timezone *)0);
199 while (!sigpipe_caught && (i = fread(buf, 1, BUFSIZ, fp)) > 0) {
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);
212 if (seconds != last_msg) {
214 msgInfo("%10d bytes read from package %s @ %4.1f KBytes/second", tot, name, (tot / seconds) / 1000.0);
217 if (sigpipe_caught || write(pfd[1], buf, i) != i) {
218 msgInfo("Write failure to pkg_add! Package may be corrupt.");
225 msgInfo("pkg_add(1) apparently did not like the %s package.", name);
227 msgInfo("I/O error while reading in the %s package.", name);
229 msgInfo("Package %s read successfully - waiting for pkg_add(1)", name);
231 i = waitpid(pid, &tot, 0);
232 dialog_clear_norefresh();
233 if (sigpipe_caught || i < 0 || WEXITSTATUS(tot)) {
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));
239 msgConfirm("Add of package %s aborted, error code %d -\n"
240 "Please check the debug screen for more info.", name, WEXITSTATUS(tot));
243 msgNotify("Package %s was added successfully", name);
245 /* Now catch any stragglers */
246 while (wait3(&tot, WNOHANG, NULL) > 0);
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);
258 msgConfirm("Unable to fetch package %s from selected media.\n"
259 "No package add will be done.", name);
262 signal(SIGPIPE, SIG_IGN);