util.c revision 1.1 1 1.1 dholland /* $NetBSD: util.c,v 1.1 2014/07/26 19:30:44 dholland Exp $ */
2 1.1 dholland
3 1.1 dholland /*
4 1.1 dholland * Copyright 1997 Piermont Information Systems Inc.
5 1.1 dholland * All rights reserved.
6 1.1 dholland *
7 1.1 dholland * Written by Philip A. Nelson for Piermont Information Systems Inc.
8 1.1 dholland *
9 1.1 dholland * Redistribution and use in source and binary forms, with or without
10 1.1 dholland * modification, are permitted provided that the following conditions
11 1.1 dholland * are met:
12 1.1 dholland * 1. Redistributions of source code must retain the above copyright
13 1.1 dholland * notice, this list of conditions and the following disclaimer.
14 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 dholland * notice, this list of conditions and the following disclaimer in the
16 1.1 dholland * documentation and/or other materials provided with the distribution.
17 1.1 dholland * 3. The name of Piermont Information Systems Inc. may not be used to endorse
18 1.1 dholland * or promote products derived from this software without specific prior
19 1.1 dholland * written permission.
20 1.1 dholland *
21 1.1 dholland * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
22 1.1 dholland * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
25 1.1 dholland * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 1.1 dholland * THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 dholland *
33 1.1 dholland */
34 1.1 dholland
35 1.1 dholland /* util.c -- routines that don't really fit anywhere else... */
36 1.1 dholland
37 1.1 dholland #include <stdio.h>
38 1.1 dholland #include <stdarg.h>
39 1.1 dholland #include <string.h>
40 1.1 dholland #include <unistd.h>
41 1.1 dholland #include <sys/mount.h>
42 1.1 dholland #include <sys/disklabel.h>
43 1.1 dholland #include <sys/dkio.h>
44 1.1 dholland #include <sys/ioctl.h>
45 1.1 dholland #include <sys/types.h>
46 1.1 dholland #include <sys/param.h>
47 1.1 dholland #include <sys/sysctl.h>
48 1.1 dholland #include <sys/stat.h>
49 1.1 dholland #include <sys/statvfs.h>
50 1.1 dholland #include <isofs/cd9660/iso.h>
51 1.1 dholland #include <curses.h>
52 1.1 dholland #include <err.h>
53 1.1 dholland #include <errno.h>
54 1.1 dholland #include <dirent.h>
55 1.1 dholland #include <util.h>
56 1.1 dholland #include "defs.h"
57 1.1 dholland #include "md.h"
58 1.1 dholland #include "msg_defs.h"
59 1.1 dholland #include "menu_defs.h"
60 1.1 dholland
61 1.1 dholland #ifndef MD_SETS_SELECTED
62 1.1 dholland #define MD_SETS_SELECTED SET_KERNEL_1, SET_SYSTEM, SET_X11, SET_MD
63 1.1 dholland #endif
64 1.1 dholland #ifndef MD_SETS_SELECTED_MINIMAL
65 1.1 dholland #define MD_SETS_SELECTED_MINIMAL SET_KERNEL_1, SET_CORE
66 1.1 dholland #endif
67 1.1 dholland #ifndef MD_SETS_SELECTED_NOX
68 1.1 dholland #define MD_SETS_SELECTED_NOX SET_KERNEL_1, SET_SYSTEM, SET_MD
69 1.1 dholland #endif
70 1.1 dholland #ifndef MD_SETS_VALID
71 1.1 dholland #define MD_SETS_VALID SET_KERNEL, SET_SYSTEM, SET_X11, SET_MD, SET_SOURCE, SET_DEBUGGING
72 1.1 dholland #endif
73 1.1 dholland
74 1.1 dholland #define MAX_CD_DEVS 256 /* how many cd drives do we expect to attach */
75 1.1 dholland #define ISO_BLKSIZE ISO_DEFAULT_BLOCK_SIZE
76 1.1 dholland
77 1.1 dholland static const char *msg_yes, *msg_no, *msg_all, *msg_some, *msg_none;
78 1.1 dholland static const char *msg_cur_distsets_row;
79 1.1 dholland static int select_menu_width;
80 1.1 dholland
81 1.1 dholland static uint8_t set_status[SET_GROUP_END];
82 1.1 dholland #define SET_VALID 0x01
83 1.1 dholland #define SET_SELECTED 0x02
84 1.1 dholland #define SET_SKIPPED 0x04
85 1.1 dholland #define SET_INSTALLED 0x08
86 1.1 dholland
87 1.1 dholland struct tarstats {
88 1.1 dholland int nselected;
89 1.1 dholland int nfound;
90 1.1 dholland int nnotfound;
91 1.1 dholland int nerror;
92 1.1 dholland int nsuccess;
93 1.1 dholland int nskipped;
94 1.1 dholland } tarstats;
95 1.1 dholland
96 1.1 dholland distinfo dist_list[] = {
97 1.1 dholland #ifdef SET_KERNEL_1_NAME
98 1.1 dholland {SET_KERNEL_1_NAME, SET_KERNEL_1, MSG_set_kernel_1, NULL},
99 1.1 dholland #endif
100 1.1 dholland #ifdef SET_KERNEL_2_NAME
101 1.1 dholland {SET_KERNEL_2_NAME, SET_KERNEL_2, MSG_set_kernel_2, NULL},
102 1.1 dholland #endif
103 1.1 dholland #ifdef SET_KERNEL_3_NAME
104 1.1 dholland {SET_KERNEL_3_NAME, SET_KERNEL_3, MSG_set_kernel_3, NULL},
105 1.1 dholland #endif
106 1.1 dholland #ifdef SET_KERNEL_4_NAME
107 1.1 dholland {SET_KERNEL_4_NAME, SET_KERNEL_4, MSG_set_kernel_4, NULL},
108 1.1 dholland #endif
109 1.1 dholland #ifdef SET_KERNEL_5_NAME
110 1.1 dholland {SET_KERNEL_5_NAME, SET_KERNEL_5, MSG_set_kernel_5, NULL},
111 1.1 dholland #endif
112 1.1 dholland #ifdef SET_KERNEL_6_NAME
113 1.1 dholland {SET_KERNEL_6_NAME, SET_KERNEL_6, MSG_set_kernel_6, NULL},
114 1.1 dholland #endif
115 1.1 dholland #ifdef SET_KERNEL_7_NAME
116 1.1 dholland {SET_KERNEL_7_NAME, SET_KERNEL_7, MSG_set_kernel_7, NULL},
117 1.1 dholland #endif
118 1.1 dholland #ifdef SET_KERNEL_8_NAME
119 1.1 dholland {SET_KERNEL_8_NAME, SET_KERNEL_8, MSG_set_kernel_8, NULL},
120 1.1 dholland #endif
121 1.1 dholland #ifdef SET_KERNEL_9_NAME
122 1.1 dholland {SET_KERNEL_9_NAME, SET_KERNEL_9, MSG_set_kernel_9, NULL},
123 1.1 dholland #endif
124 1.1 dholland
125 1.1 dholland {"modules", SET_MODULES, MSG_set_modules, NULL},
126 1.1 dholland {"base", SET_BASE, MSG_set_base, NULL},
127 1.1 dholland {"etc", SET_ETC, MSG_set_system, NULL},
128 1.1 dholland {"comp", SET_COMPILER, MSG_set_compiler, NULL},
129 1.1 dholland {"games", SET_GAMES, MSG_set_games, NULL},
130 1.1 dholland {"man", SET_MAN_PAGES, MSG_set_man_pages, NULL},
131 1.1 dholland {"misc", SET_MISC, MSG_set_misc, NULL},
132 1.1 dholland {"tests", SET_TESTS, MSG_set_tests, NULL},
133 1.1 dholland {"text", SET_TEXT_TOOLS, MSG_set_text_tools, NULL},
134 1.1 dholland
135 1.1 dholland {NULL, SET_GROUP, MSG_set_X11, NULL},
136 1.1 dholland {"xbase", SET_X11_BASE, MSG_set_X11_base, NULL},
137 1.1 dholland {"xcomp", SET_X11_PROG, MSG_set_X11_prog, NULL},
138 1.1 dholland {"xetc", SET_X11_ETC, MSG_set_X11_etc, NULL},
139 1.1 dholland {"xfont", SET_X11_FONTS, MSG_set_X11_fonts, NULL},
140 1.1 dholland {"xserver", SET_X11_SERVERS, MSG_set_X11_servers, NULL},
141 1.1 dholland {NULL, SET_GROUP_END, NULL, NULL},
142 1.1 dholland
143 1.1 dholland #ifdef SET_MD_1_NAME
144 1.1 dholland {SET_MD_1_NAME, SET_MD_1, MSG_set_md_1, NULL},
145 1.1 dholland #endif
146 1.1 dholland #ifdef SET_MD_2_NAME
147 1.1 dholland {SET_MD_2_NAME, SET_MD_2, MSG_set_md_2, NULL},
148 1.1 dholland #endif
149 1.1 dholland #ifdef SET_MD_3_NAME
150 1.1 dholland {SET_MD_3_NAME, SET_MD_3, MSG_set_md_3, NULL},
151 1.1 dholland #endif
152 1.1 dholland #ifdef SET_MD_4_NAME
153 1.1 dholland {SET_MD_4_NAME, SET_MD_4, MSG_set_md_4, NULL},
154 1.1 dholland #endif
155 1.1 dholland
156 1.1 dholland {NULL, SET_GROUP, MSG_set_source, NULL},
157 1.1 dholland {"syssrc", SET_SYSSRC, MSG_set_syssrc, NULL},
158 1.1 dholland {"src", SET_SRC, MSG_set_src, NULL},
159 1.1 dholland {"sharesrc", SET_SHARESRC, MSG_set_sharesrc, NULL},
160 1.1 dholland {"gnusrc", SET_GNUSRC, MSG_set_gnusrc, NULL},
161 1.1 dholland {"xsrc", SET_XSRC, MSG_set_xsrc, NULL},
162 1.1 dholland {"debug", SET_DEBUG, MSG_set_debug, NULL},
163 1.1 dholland {"xdebug", SET_X11_DEBUG, MSG_set_xdebug, NULL},
164 1.1 dholland {NULL, SET_GROUP_END, NULL, NULL},
165 1.1 dholland
166 1.1 dholland {NULL, SET_LAST, NULL, NULL},
167 1.1 dholland };
168 1.1 dholland
169 1.1 dholland #define MAX_CD_INFOS 16 /* how many media can be found? */
170 1.1 dholland struct cd_info {
171 1.1 dholland char device_name[16];
172 1.1 dholland char menu[100];
173 1.1 dholland };
174 1.1 dholland static struct cd_info cds[MAX_CD_INFOS];
175 1.1 dholland
176 1.1 dholland /*
177 1.1 dholland * local prototypes
178 1.1 dholland */
179 1.1 dholland
180 1.1 dholland static int check_for(unsigned int mode, const char *pathname);
181 1.1 dholland static int get_iso9660_volname(int dev, int sess, char *volname);
182 1.1 dholland static int get_available_cds(void);
183 1.1 dholland
184 1.1 dholland void
185 1.1 dholland init_set_status(int flags)
186 1.1 dholland {
187 1.1 dholland static const uint8_t sets_valid[] = {MD_SETS_VALID};
188 1.1 dholland static const uint8_t sets_selected_full[] = {MD_SETS_SELECTED};
189 1.1 dholland static const uint8_t sets_selected_minimal[] = {MD_SETS_SELECTED_MINIMAL};
190 1.1 dholland static const uint8_t sets_selected_nox[] = {MD_SETS_SELECTED_NOX};
191 1.1 dholland static const uint8_t *sets_selected;
192 1.1 dholland unsigned int nelem_selected;
193 1.1 dholland unsigned int i, len;
194 1.1 dholland const char *longest;
195 1.1 dholland
196 1.1 dholland if (flags & SFLAG_MINIMAL) {
197 1.1 dholland sets_selected = sets_selected_minimal;
198 1.1 dholland nelem_selected = nelem(sets_selected_minimal);
199 1.1 dholland } else if (flags & SFLAG_NOX) {
200 1.1 dholland sets_selected = sets_selected_nox;
201 1.1 dholland nelem_selected = nelem(sets_selected_nox);
202 1.1 dholland } else {
203 1.1 dholland sets_selected = sets_selected_full;
204 1.1 dholland nelem_selected = nelem(sets_selected_full);
205 1.1 dholland }
206 1.1 dholland
207 1.1 dholland for (i = 0; i < nelem(sets_valid); i++)
208 1.1 dholland set_status[sets_valid[i]] = SET_VALID;
209 1.1 dholland for (i = 0; i < nelem_selected; i++)
210 1.1 dholland set_status[sets_selected[i]] |= SET_SELECTED;
211 1.1 dholland
212 1.1 dholland set_status[SET_GROUP] = SET_VALID;
213 1.1 dholland
214 1.1 dholland /* Lookup some strings we need lots of times */
215 1.1 dholland msg_yes = msg_string(MSG_Yes);
216 1.1 dholland msg_no = msg_string(MSG_No);
217 1.1 dholland msg_all = msg_string(MSG_All);
218 1.1 dholland msg_some = msg_string(MSG_Some);
219 1.1 dholland msg_none = msg_string(MSG_None);
220 1.1 dholland msg_cur_distsets_row = msg_string(MSG_cur_distsets_row);
221 1.1 dholland
222 1.1 dholland /* Find longest and use it to determine width of selection menu */
223 1.1 dholland len = strlen(msg_no); longest = msg_no;
224 1.1 dholland i = strlen(msg_yes); if (i > len) {len = i; longest = msg_yes; }
225 1.1 dholland i = strlen(msg_all); if (i > len) {len = i; longest = msg_all; }
226 1.1 dholland i = strlen(msg_some); if (i > len) {len = i; longest = msg_some; }
227 1.1 dholland i = strlen(msg_none); if (i > len) {len = i; longest = msg_none; }
228 1.1 dholland select_menu_width = snprintf(NULL, 0, msg_cur_distsets_row, "",longest);
229 1.1 dholland
230 1.1 dholland /* Give the md code a chance to choose the right kernel, etc. */
231 1.1 dholland md_init_set_status(flags);
232 1.1 dholland }
233 1.1 dholland
234 1.1 dholland int
235 1.1 dholland dir_exists_p(const char *path)
236 1.1 dholland {
237 1.1 dholland
238 1.1 dholland return file_mode_match(path, S_IFDIR);
239 1.1 dholland }
240 1.1 dholland
241 1.1 dholland int
242 1.1 dholland file_exists_p(const char *path)
243 1.1 dholland {
244 1.1 dholland
245 1.1 dholland return file_mode_match(path, S_IFREG);
246 1.1 dholland }
247 1.1 dholland
248 1.1 dholland int
249 1.1 dholland file_mode_match(const char *path, unsigned int mode)
250 1.1 dholland {
251 1.1 dholland struct stat st;
252 1.1 dholland
253 1.1 dholland return (stat(path, &st) == 0 && (st.st_mode & S_IFMT) == mode);
254 1.1 dholland }
255 1.1 dholland
256 1.1 dholland uint
257 1.1 dholland get_ramsize(void)
258 1.1 dholland {
259 1.1 dholland uint64_t ramsize;
260 1.1 dholland size_t len = sizeof ramsize;
261 1.1 dholland int mib[2] = {CTL_HW, HW_PHYSMEM64};
262 1.1 dholland
263 1.1 dholland sysctl(mib, 2, &ramsize, &len, NULL, 0);
264 1.1 dholland
265 1.1 dholland /* Find out how many Megs ... round up. */
266 1.1 dholland return (ramsize + MEG - 1) / MEG;
267 1.1 dholland }
268 1.1 dholland
269 1.1 dholland void
270 1.1 dholland run_makedev(void)
271 1.1 dholland {
272 1.1 dholland char *owd;
273 1.1 dholland
274 1.1 dholland msg_display_add("\n\n");
275 1.1 dholland msg_display_add(MSG_makedev);
276 1.1 dholland
277 1.1 dholland owd = getcwd(NULL, 0);
278 1.1 dholland
279 1.1 dholland /* make /dev, in case the user didn't extract it. */
280 1.1 dholland make_target_dir("/dev");
281 1.1 dholland target_chdir_or_die("/dev");
282 1.1 dholland run_program(0, "/bin/sh MAKEDEV all");
283 1.1 dholland
284 1.1 dholland chdir(owd);
285 1.1 dholland free(owd);
286 1.1 dholland }
287 1.1 dholland
288 1.1 dholland /*
289 1.1 dholland * Performs in-place replacement of a set of patterns in a file that lives
290 1.1 dholland * inside the installed system. The patterns must be separated by a semicolon.
291 1.1 dholland * For example:
292 1.1 dholland *
293 1.1 dholland * replace("/etc/some-file.conf", "s/prop1=NO/prop1=YES/;s/foo/bar/");
294 1.1 dholland */
295 1.1 dholland void
296 1.1 dholland replace(const char *path, const char *patterns, ...)
297 1.1 dholland {
298 1.1 dholland char *spatterns;
299 1.1 dholland va_list ap;
300 1.1 dholland
301 1.1 dholland va_start(ap, patterns);
302 1.1 dholland vasprintf(&spatterns, patterns, ap);
303 1.1 dholland va_end(ap);
304 1.1 dholland if (spatterns == NULL)
305 1.1 dholland err(1, "vasprintf(&spatterns, \"%s\", ...)", patterns);
306 1.1 dholland
307 1.1 dholland run_program(RUN_CHROOT, "sed -an -e '%s;H;$!d;g;w %s' %s", spatterns,
308 1.1 dholland path, path);
309 1.1 dholland
310 1.1 dholland free(spatterns);
311 1.1 dholland }
312 1.1 dholland
313 1.1 dholland static int
314 1.1 dholland floppy_fetch(const char *set_name)
315 1.1 dholland {
316 1.1 dholland char post[4];
317 1.1 dholland msg errmsg;
318 1.1 dholland int menu;
319 1.1 dholland int status;
320 1.1 dholland const char *write_mode = ">";
321 1.1 dholland
322 1.1 dholland strcpy(post, "aa");
323 1.1 dholland
324 1.1 dholland errmsg = "";
325 1.1 dholland menu = MENU_fdok;
326 1.1 dholland for (;;) {
327 1.1 dholland umount_mnt2();
328 1.1 dholland msg_display(errmsg);
329 1.1 dholland msg_display_add(MSG_fdmount, set_name, post);
330 1.1 dholland process_menu(menu, &status);
331 1.1 dholland if (status != SET_CONTINUE)
332 1.1 dholland return status;
333 1.1 dholland menu = MENU_fdremount;
334 1.1 dholland errmsg = MSG_fdremount;
335 1.1 dholland if (run_program(0, "/sbin/mount -r -t %s %s /mnt2",
336 1.1 dholland fd_type, fd_dev))
337 1.1 dholland continue;
338 1.1 dholland mnt2_mounted = 1;
339 1.1 dholland errmsg = MSG_fdnotfound;
340 1.1 dholland
341 1.1 dholland /* Display this because it might take a while.... */
342 1.1 dholland if (run_program(RUN_DISPLAY,
343 1.1 dholland "sh -c '/bin/cat /mnt2/%s.%s %s %s/%s/%s%s'",
344 1.1 dholland set_name, post, write_mode,
345 1.1 dholland target_prefix(), xfer_dir, set_name, dist_postfix))
346 1.1 dholland /* XXX: a read error will give a corrupt file! */
347 1.1 dholland continue;
348 1.1 dholland
349 1.1 dholland /* We got that file, advance to next fragment */
350 1.1 dholland if (post[1] < 'z')
351 1.1 dholland post[1]++;
352 1.1 dholland else
353 1.1 dholland post[1] = 'a', post[0]++;
354 1.1 dholland write_mode = ">>";
355 1.1 dholland errmsg = "";
356 1.1 dholland menu = MENU_fdok;
357 1.1 dholland }
358 1.1 dholland }
359 1.1 dholland
360 1.1 dholland /*
361 1.1 dholland * Load files from floppy. Requires a /mnt2 directory for mounting them.
362 1.1 dholland */
363 1.1 dholland int
364 1.1 dholland get_via_floppy(void)
365 1.1 dholland {
366 1.1 dholland
367 1.1 dholland process_menu(MENU_floppysource, NULL);
368 1.1 dholland
369 1.1 dholland fetch_fn = floppy_fetch;
370 1.1 dholland
371 1.1 dholland /* Set ext_dir for absolute path. */
372 1.1 dholland snprintf(ext_dir_bin, sizeof ext_dir_bin, "%s/%s", target_prefix(), xfer_dir);
373 1.1 dholland snprintf(ext_dir_src, sizeof ext_dir_src, "%s/%s", target_prefix(), xfer_dir);
374 1.1 dholland
375 1.1 dholland return SET_OK;
376 1.1 dholland }
377 1.1 dholland
378 1.1 dholland /*
379 1.1 dholland * Get the volume name of a ISO9660 file system
380 1.1 dholland */
381 1.1 dholland static int
382 1.1 dholland get_iso9660_volname(int dev, int sess, char *volname)
383 1.1 dholland {
384 1.1 dholland int blkno, error, last;
385 1.1 dholland char buf[ISO_BLKSIZE];
386 1.1 dholland struct iso_volume_descriptor *vd = NULL;
387 1.1 dholland struct iso_primary_descriptor *pd = NULL;
388 1.1 dholland
389 1.1 dholland for (blkno = sess+16; blkno < sess+16+100; blkno++) {
390 1.1 dholland error = pread(dev, buf, ISO_BLKSIZE, blkno*ISO_BLKSIZE);
391 1.1 dholland if (error == -1)
392 1.1 dholland return -1;
393 1.1 dholland vd = (struct iso_volume_descriptor *)&buf;
394 1.1 dholland if (memcmp(vd->id, ISO_STANDARD_ID, sizeof(vd->id)) != 0)
395 1.1 dholland return -1;
396 1.1 dholland if (isonum_711((const unsigned char *)&vd->type)
397 1.1 dholland == ISO_VD_PRIMARY) {
398 1.1 dholland pd = (struct iso_primary_descriptor*)buf;
399 1.1 dholland strncpy(volname, pd->volume_id, sizeof pd->volume_id);
400 1.1 dholland last = sizeof pd->volume_id-1;
401 1.1 dholland while (last >= 0
402 1.1 dholland && (volname[last] == ' ' || volname[last] == 0))
403 1.1 dholland last--;
404 1.1 dholland volname[last+1] = 0;
405 1.1 dholland return 0;
406 1.1 dholland }
407 1.1 dholland }
408 1.1 dholland return -1;
409 1.1 dholland }
410 1.1 dholland
411 1.1 dholland /*
412 1.1 dholland * Get a list of all available CD media (not drives!), return
413 1.1 dholland * the number of entries collected.
414 1.1 dholland */
415 1.1 dholland static int
416 1.1 dholland get_available_cds(void)
417 1.1 dholland {
418 1.1 dholland char dname[16], volname[80];
419 1.1 dholland struct cd_info *info = cds;
420 1.1 dholland struct disklabel label;
421 1.1 dholland int i, part, dev, error, sess, ready, count = 0;
422 1.1 dholland
423 1.1 dholland for (i = 0; i < MAX_CD_DEVS; i++) {
424 1.1 dholland sprintf(dname, "/dev/rcd%d%c", i, 'a'+RAW_PART);
425 1.1 dholland dev = open(dname, O_RDONLY, 0);
426 1.1 dholland if (dev == -1)
427 1.1 dholland break;
428 1.1 dholland ready = 0;
429 1.1 dholland error = ioctl(dev, DIOCTUR, &ready);
430 1.1 dholland if (error != 0 || ready == 0) {
431 1.1 dholland close(dev);
432 1.1 dholland continue;
433 1.1 dholland }
434 1.1 dholland error = ioctl(dev, DIOCGDINFO, &label);
435 1.1 dholland close(dev);
436 1.1 dholland if (error == 0) {
437 1.1 dholland for (part = 0; part < label.d_npartitions; part++) {
438 1.1 dholland if (label.d_partitions[part].p_fstype
439 1.1 dholland == FS_UNUSED
440 1.1 dholland || label.d_partitions[part].p_size == 0)
441 1.1 dholland continue;
442 1.1 dholland if (label.d_partitions[part].p_fstype
443 1.1 dholland == FS_ISO9660) {
444 1.1 dholland sess = label.d_partitions[part]
445 1.1 dholland .p_cdsession;
446 1.1 dholland sprintf(dname, "/dev/rcd%d%c", i,
447 1.1 dholland 'a'+part);
448 1.1 dholland dev = open(dname, O_RDONLY, 0);
449 1.1 dholland if (dev == -1)
450 1.1 dholland continue;
451 1.1 dholland error = get_iso9660_volname(dev, sess,
452 1.1 dholland volname);
453 1.1 dholland close(dev);
454 1.1 dholland if (error) continue;
455 1.1 dholland sprintf(info->device_name, "cd%d%c",
456 1.1 dholland i, 'a'+part);
457 1.1 dholland sprintf(info->menu, "%s (%s)",
458 1.1 dholland info->device_name,
459 1.1 dholland volname);
460 1.1 dholland } else {
461 1.1 dholland /*
462 1.1 dholland * All install CDs use partition
463 1.1 dholland * a for the sets.
464 1.1 dholland */
465 1.1 dholland if (part > 0)
466 1.1 dholland continue;
467 1.1 dholland sprintf(info->device_name, "cd%d%c",
468 1.1 dholland i, 'a'+part);
469 1.1 dholland strcpy(info->menu, info->device_name);
470 1.1 dholland }
471 1.1 dholland info++;
472 1.1 dholland if (++count >= MAX_CD_INFOS)
473 1.1 dholland break;
474 1.1 dholland }
475 1.1 dholland }
476 1.1 dholland }
477 1.1 dholland return count;
478 1.1 dholland }
479 1.1 dholland
480 1.1 dholland static int
481 1.1 dholland cd_has_sets(void)
482 1.1 dholland {
483 1.1 dholland /* Mount it */
484 1.1 dholland if (run_program(RUN_SILENT, "/sbin/mount -rt cd9660 /dev/%s /mnt2",
485 1.1 dholland cdrom_dev) != 0)
486 1.1 dholland return 0;
487 1.1 dholland
488 1.1 dholland mnt2_mounted = 1;
489 1.1 dholland
490 1.1 dholland snprintf(ext_dir_bin, sizeof ext_dir_bin, "%s/%s", "/mnt2", set_dir_bin);
491 1.1 dholland snprintf(ext_dir_src, sizeof ext_dir_src, "%s/%s", "/mnt2", set_dir_src);
492 1.1 dholland return dir_exists_p(ext_dir_bin);
493 1.1 dholland }
494 1.1 dholland
495 1.1 dholland
496 1.1 dholland static int
497 1.1 dholland set_cd_select(menudesc *m, void *arg)
498 1.1 dholland {
499 1.1 dholland *(int *)arg = m->cursel;
500 1.1 dholland return 1;
501 1.1 dholland }
502 1.1 dholland
503 1.1 dholland /*
504 1.1 dholland * Check whether we can remove the boot media.
505 1.1 dholland * If it is not a local filesystem, return -1.
506 1.1 dholland * If we can not decide for sure (can not tell MD content from plain ffs
507 1.1 dholland * on hard disk, for example), return 0.
508 1.1 dholland * If it is a CD/DVD, return 1.
509 1.1 dholland */
510 1.1 dholland int
511 1.1 dholland boot_media_still_needed(void)
512 1.1 dholland {
513 1.1 dholland struct statvfs sb;
514 1.1 dholland
515 1.1 dholland if (statvfs("/", &sb) == 0) {
516 1.1 dholland if (!(sb.f_flag & ST_LOCAL))
517 1.1 dholland return -1;
518 1.1 dholland if (strcmp(sb.f_fstypename, MOUNT_CD9660) == 0
519 1.1 dholland || strcmp(sb.f_fstypename, MOUNT_UDF) == 0)
520 1.1 dholland return 1;
521 1.1 dholland }
522 1.1 dholland
523 1.1 dholland return 0;
524 1.1 dholland }
525 1.1 dholland
526 1.1 dholland /*
527 1.1 dholland * Get from a CDROM distribution.
528 1.1 dholland * Also used on "installation using bootable install media"
529 1.1 dholland * as the default option in the "distmedium" menu.
530 1.1 dholland */
531 1.1 dholland int
532 1.1 dholland get_via_cdrom(void)
533 1.1 dholland {
534 1.1 dholland menu_ent cd_menu[MAX_CD_INFOS];
535 1.1 dholland struct stat sb;
536 1.1 dholland int num_cds, menu_cd, i, selected_cd = 0;
537 1.1 dholland bool silent = false;
538 1.1 dholland int mib[2];
539 1.1 dholland char rootdev[SSTRSIZE] = "";
540 1.1 dholland size_t varlen;
541 1.1 dholland
542 1.1 dholland /* If root is not md(4) and we have set dir, skip this step. */
543 1.1 dholland mib[0] = CTL_KERN;
544 1.1 dholland mib[1] = KERN_ROOT_DEVICE;
545 1.1 dholland varlen = sizeof(rootdev);
546 1.1 dholland (void)sysctl(mib, 2, rootdev, &varlen, NULL, 0);
547 1.1 dholland if (stat(set_dir_bin, &sb) == 0 && S_ISDIR(sb.st_mode) &&
548 1.1 dholland strncmp("md", rootdev, 2) != 0) {
549 1.1 dholland strlcpy(ext_dir_bin, set_dir_bin, sizeof ext_dir_bin);
550 1.1 dholland strlcpy(ext_dir_src, set_dir_src, sizeof ext_dir_src);
551 1.1 dholland return SET_OK;
552 1.1 dholland }
553 1.1 dholland
554 1.1 dholland num_cds = get_available_cds();
555 1.1 dholland if (num_cds <= 0) {
556 1.1 dholland silent = true;
557 1.1 dholland } else if (num_cds == 1) {
558 1.1 dholland /* single CD found, check for sets on it */
559 1.1 dholland strcpy(cdrom_dev, cds[0].device_name);
560 1.1 dholland if (cd_has_sets())
561 1.1 dholland return SET_OK;
562 1.1 dholland } else {
563 1.1 dholland for (i = 0; i< num_cds; i++) {
564 1.1 dholland cd_menu[i].opt_name = cds[i].menu;
565 1.1 dholland cd_menu[i].opt_menu = OPT_NOMENU;
566 1.1 dholland cd_menu[i].opt_flags = OPT_EXIT;
567 1.1 dholland cd_menu[i].opt_action = set_cd_select;
568 1.1 dholland }
569 1.1 dholland /* create a menu offering available choices */
570 1.1 dholland menu_cd = new_menu(MSG_Available_cds,
571 1.1 dholland cd_menu, num_cds, -1, 4, 0, 0,
572 1.1 dholland MC_SCROLL | MC_NOEXITOPT,
573 1.1 dholland NULL, NULL, NULL, NULL, NULL);
574 1.1 dholland if (menu_cd == -1)
575 1.1 dholland return SET_RETRY;
576 1.1 dholland msg_display(MSG_ask_cd);
577 1.1 dholland process_menu(menu_cd, &selected_cd);
578 1.1 dholland free_menu(menu_cd);
579 1.1 dholland strcpy(cdrom_dev, cds[selected_cd].device_name);
580 1.1 dholland if (cd_has_sets())
581 1.1 dholland return SET_OK;
582 1.1 dholland }
583 1.1 dholland
584 1.1 dholland if (silent)
585 1.1 dholland msg_display("");
586 1.1 dholland else {
587 1.1 dholland umount_mnt2();
588 1.1 dholland msg_display(MSG_cd_path_not_found);
589 1.1 dholland process_menu(MENU_ok, NULL);
590 1.1 dholland }
591 1.1 dholland
592 1.1 dholland /* ask for paths on the CD */
593 1.1 dholland process_menu(MENU_cdromsource, NULL);
594 1.1 dholland
595 1.1 dholland if (cd_has_sets())
596 1.1 dholland return SET_OK;
597 1.1 dholland
598 1.1 dholland return SET_RETRY;
599 1.1 dholland }
600 1.1 dholland
601 1.1 dholland
602 1.1 dholland /*
603 1.1 dholland * Get from a pathname inside an unmounted local filesystem
604 1.1 dholland * (e.g., where sets were preloaded onto a local DOS partition)
605 1.1 dholland */
606 1.1 dholland int
607 1.1 dholland get_via_localfs(void)
608 1.1 dholland {
609 1.1 dholland
610 1.1 dholland /* Get device, filesystem, and filepath */
611 1.1 dholland process_menu (MENU_localfssource, NULL);
612 1.1 dholland
613 1.1 dholland /* Mount it */
614 1.1 dholland if (run_program(0, "/sbin/mount -rt %s /dev/%s /mnt2",
615 1.1 dholland localfs_fs, localfs_dev))
616 1.1 dholland return SET_RETRY;
617 1.1 dholland
618 1.1 dholland mnt2_mounted = 1;
619 1.1 dholland
620 1.1 dholland snprintf(ext_dir_bin, sizeof ext_dir_bin, "%s/%s/%s",
621 1.1 dholland "/mnt2", localfs_dir, set_dir_bin);
622 1.1 dholland snprintf(ext_dir_src, sizeof ext_dir_src, "%s/%s/%s",
623 1.1 dholland "/mnt2", localfs_dir, set_dir_src);
624 1.1 dholland
625 1.1 dholland return SET_OK;
626 1.1 dholland }
627 1.1 dholland
628 1.1 dholland /*
629 1.1 dholland * Get from an already-mounted pathname.
630 1.1 dholland */
631 1.1 dholland
632 1.1 dholland int
633 1.1 dholland get_via_localdir(void)
634 1.1 dholland {
635 1.1 dholland
636 1.1 dholland /* Get filepath */
637 1.1 dholland process_menu(MENU_localdirsource, NULL);
638 1.1 dholland
639 1.1 dholland /*
640 1.1 dholland * We have to have an absolute path ('cos pax runs in a
641 1.1 dholland * different directory), make it so.
642 1.1 dholland */
643 1.1 dholland snprintf(ext_dir_bin, sizeof ext_dir_bin, "/%s/%s", localfs_dir, set_dir_bin);
644 1.1 dholland snprintf(ext_dir_src, sizeof ext_dir_src, "/%s/%s", localfs_dir, set_dir_src);
645 1.1 dholland
646 1.1 dholland return SET_OK;
647 1.1 dholland }
648 1.1 dholland
649 1.1 dholland
650 1.1 dholland /*
651 1.1 dholland * Support for custom distribution fetches / unpacks.
652 1.1 dholland */
653 1.1 dholland
654 1.1 dholland unsigned int
655 1.1 dholland set_X11_selected(void)
656 1.1 dholland {
657 1.1 dholland int i;
658 1.1 dholland
659 1.1 dholland for (i = SET_X11_FIRST; ++i < SET_X11_LAST;)
660 1.1 dholland if (set_status[i] & SET_SELECTED)
661 1.1 dholland return 1;
662 1.1 dholland return 0;
663 1.1 dholland }
664 1.1 dholland
665 1.1 dholland unsigned int
666 1.1 dholland get_kernel_set(void)
667 1.1 dholland {
668 1.1 dholland int i;
669 1.1 dholland
670 1.1 dholland for (i = SET_KERNEL_FIRST; ++i < SET_KERNEL_LAST;)
671 1.1 dholland if (set_status[i] & SET_SELECTED)
672 1.1 dholland return i;
673 1.1 dholland return SET_NONE;
674 1.1 dholland }
675 1.1 dholland
676 1.1 dholland void
677 1.1 dholland set_kernel_set(unsigned int kernel_set)
678 1.1 dholland {
679 1.1 dholland int i;
680 1.1 dholland
681 1.1 dholland /* only one kernel set is allowed */
682 1.1 dholland for (i = SET_KERNEL_FIRST; ++i < SET_KERNEL_LAST;)
683 1.1 dholland set_status[i] &= ~SET_SELECTED;
684 1.1 dholland set_status[kernel_set] |= SET_SELECTED;
685 1.1 dholland }
686 1.1 dholland
687 1.1 dholland static int
688 1.1 dholland set_toggle(menudesc *menu, void *arg)
689 1.1 dholland {
690 1.1 dholland distinfo **distp = arg;
691 1.1 dholland int set = distp[menu->cursel]->set;
692 1.1 dholland
693 1.1 dholland if (set > SET_KERNEL_FIRST && set < SET_KERNEL_LAST &&
694 1.1 dholland !(set_status[set] & SET_SELECTED))
695 1.1 dholland set_kernel_set(set);
696 1.1 dholland else
697 1.1 dholland set_status[set] ^= SET_SELECTED;
698 1.1 dholland return 0;
699 1.1 dholland }
700 1.1 dholland
701 1.1 dholland static int
702 1.1 dholland set_all_none(menudesc *menu, void *arg, int set, int clr)
703 1.1 dholland {
704 1.1 dholland distinfo **distp = arg;
705 1.1 dholland distinfo *dist = *distp;
706 1.1 dholland int nested;
707 1.1 dholland
708 1.1 dholland for (nested = 0; dist->set != SET_GROUP_END || nested--; dist++) {
709 1.1 dholland if (dist->set == SET_GROUP) {
710 1.1 dholland nested++;
711 1.1 dholland continue;
712 1.1 dholland }
713 1.1 dholland set_status[dist->set] = (set_status[dist->set] & ~clr) | set;
714 1.1 dholland }
715 1.1 dholland return 0;
716 1.1 dholland }
717 1.1 dholland
718 1.1 dholland static int
719 1.1 dholland set_all(menudesc *menu, void *arg)
720 1.1 dholland {
721 1.1 dholland return set_all_none(menu, arg, SET_SELECTED, 0);
722 1.1 dholland }
723 1.1 dholland
724 1.1 dholland static int
725 1.1 dholland set_none(menudesc *menu, void *arg)
726 1.1 dholland {
727 1.1 dholland return set_all_none(menu, arg, 0, SET_SELECTED);
728 1.1 dholland }
729 1.1 dholland
730 1.1 dholland static void
731 1.1 dholland set_label(menudesc *menu, int opt, void *arg)
732 1.1 dholland {
733 1.1 dholland distinfo **distp = arg;
734 1.1 dholland distinfo *dist = distp[opt];
735 1.1 dholland const char *selected;
736 1.1 dholland const char *desc;
737 1.1 dholland int nested;
738 1.1 dholland
739 1.1 dholland desc = dist->desc;
740 1.1 dholland
741 1.1 dholland if (dist->set != SET_GROUP)
742 1.1 dholland selected = set_status[dist->set] & SET_SELECTED ? msg_yes : msg_no;
743 1.1 dholland else {
744 1.1 dholland /* sub menu - display None/Some/All */
745 1.1 dholland nested = 0;
746 1.1 dholland selected = "unknown";
747 1.1 dholland while ((++dist)->set != SET_GROUP_END || nested--) {
748 1.1 dholland if (dist->set == SET_GROUP) {
749 1.1 dholland nested++;
750 1.1 dholland continue;
751 1.1 dholland }
752 1.1 dholland if (!(set_status[dist->set] & SET_VALID))
753 1.1 dholland continue;
754 1.1 dholland if (set_status[dist->set] & SET_SELECTED) {
755 1.1 dholland if (selected == msg_none) {
756 1.1 dholland selected = msg_some;
757 1.1 dholland break;
758 1.1 dholland }
759 1.1 dholland selected = msg_all;
760 1.1 dholland } else {
761 1.1 dholland if (selected == msg_all) {
762 1.1 dholland selected = msg_some;
763 1.1 dholland break;
764 1.1 dholland }
765 1.1 dholland selected = msg_none;
766 1.1 dholland }
767 1.1 dholland }
768 1.1 dholland }
769 1.1 dholland
770 1.1 dholland wprintw(menu->mw, msg_cur_distsets_row, msg_string(desc), selected);
771 1.1 dholland }
772 1.1 dholland
773 1.1 dholland static int set_sublist(menudesc *menu, void *arg);
774 1.1 dholland
775 1.1 dholland static int
776 1.1 dholland initialise_set_menu(distinfo *dist, menu_ent *me, distinfo **de, int all_none)
777 1.1 dholland {
778 1.1 dholland int set;
779 1.1 dholland int sets;
780 1.1 dholland int nested;
781 1.1 dholland
782 1.1 dholland for (sets = 0; ; dist++) {
783 1.1 dholland set = dist->set;
784 1.1 dholland if (set == SET_LAST || set == SET_GROUP_END)
785 1.1 dholland break;
786 1.1 dholland if (!(set_status[set] & SET_VALID))
787 1.1 dholland continue;
788 1.1 dholland *de = dist;
789 1.1 dholland me->opt_menu = OPT_NOMENU;
790 1.1 dholland me->opt_flags = 0;
791 1.1 dholland me->opt_name = NULL;
792 1.1 dholland if (set != SET_GROUP)
793 1.1 dholland me->opt_action = set_toggle;
794 1.1 dholland else {
795 1.1 dholland /* Collapse sublist */
796 1.1 dholland nested = 0;
797 1.1 dholland while ((++dist)->set != SET_GROUP_END || nested--) {
798 1.1 dholland if (dist->set == SET_GROUP)
799 1.1 dholland nested++;
800 1.1 dholland }
801 1.1 dholland me->opt_action = set_sublist;
802 1.1 dholland }
803 1.1 dholland sets++;
804 1.1 dholland de++;
805 1.1 dholland me++;
806 1.1 dholland }
807 1.1 dholland
808 1.1 dholland if (all_none) {
809 1.1 dholland me->opt_menu = OPT_NOMENU;
810 1.1 dholland me->opt_flags = 0;
811 1.1 dholland me->opt_name = MSG_select_all;
812 1.1 dholland me->opt_action = set_all;
813 1.1 dholland me++;
814 1.1 dholland me->opt_menu = OPT_NOMENU;
815 1.1 dholland me->opt_flags = 0;
816 1.1 dholland me->opt_name = MSG_select_none;
817 1.1 dholland me->opt_action = set_none;
818 1.1 dholland sets += 2;
819 1.1 dholland }
820 1.1 dholland
821 1.1 dholland return sets;
822 1.1 dholland }
823 1.1 dholland
824 1.1 dholland static int
825 1.1 dholland set_sublist(menudesc *menu, void *arg)
826 1.1 dholland {
827 1.1 dholland distinfo *de[SET_LAST];
828 1.1 dholland menu_ent me[SET_LAST];
829 1.1 dholland distinfo **dist = arg;
830 1.1 dholland int menu_no;
831 1.1 dholland int sets;
832 1.1 dholland
833 1.1 dholland sets = initialise_set_menu(dist[menu->cursel] + 1, me, de, 1);
834 1.1 dholland
835 1.1 dholland menu_no = new_menu(NULL, me, sets, 20, 10, 0, select_menu_width,
836 1.1 dholland MC_SUBMENU | MC_SCROLL | MC_DFLTEXIT,
837 1.1 dholland NULL, set_label, NULL, NULL,
838 1.1 dholland MSG_install_selected_sets);
839 1.1 dholland
840 1.1 dholland process_menu(menu_no, de);
841 1.1 dholland free_menu(menu_no);
842 1.1 dholland
843 1.1 dholland return 0;
844 1.1 dholland }
845 1.1 dholland
846 1.1 dholland void
847 1.1 dholland customise_sets(void)
848 1.1 dholland {
849 1.1 dholland distinfo *de[SET_LAST];
850 1.1 dholland menu_ent me[SET_LAST];
851 1.1 dholland int sets;
852 1.1 dholland int menu_no;
853 1.1 dholland
854 1.1 dholland msg_display(MSG_cur_distsets);
855 1.1 dholland msg_table_add(MSG_cur_distsets_header);
856 1.1 dholland
857 1.1 dholland sets = initialise_set_menu(dist_list, me, de, 0);
858 1.1 dholland
859 1.1 dholland menu_no = new_menu(NULL, me, sets, 0, 5, 0, select_menu_width,
860 1.1 dholland MC_SCROLL | MC_NOBOX | MC_DFLTEXIT | MC_NOCLEAR,
861 1.1 dholland NULL, set_label, NULL, NULL,
862 1.1 dholland MSG_install_selected_sets);
863 1.1 dholland
864 1.1 dholland process_menu(menu_no, de);
865 1.1 dholland free_menu(menu_no);
866 1.1 dholland }
867 1.1 dholland
868 1.1 dholland /*
869 1.1 dholland * Extract_file **REQUIRES** an absolute path in ext_dir. Any code
870 1.1 dholland * that sets up xfer_dir for use by extract_file needs to put in the
871 1.1 dholland * full path name to the directory.
872 1.1 dholland */
873 1.1 dholland
874 1.1 dholland int
875 1.1 dholland extract_file(distinfo *dist, int update)
876 1.1 dholland {
877 1.1 dholland char path[STRSIZE];
878 1.1 dholland char *owd;
879 1.1 dholland int rval;
880 1.1 dholland
881 1.1 dholland /* If we might need to tidy up, ensure directory exists */
882 1.1 dholland if (fetch_fn != NULL)
883 1.1 dholland make_target_dir(xfer_dir);
884 1.1 dholland
885 1.1 dholland (void)snprintf(path, sizeof path, "%s/%s%s",
886 1.1 dholland ext_dir_for_set(dist->name), dist->name, dist_postfix);
887 1.1 dholland
888 1.1 dholland owd = getcwd(NULL, 0);
889 1.1 dholland
890 1.1 dholland /* Do we need to fetch the file now? */
891 1.1 dholland if (fetch_fn != NULL) {
892 1.1 dholland rval = fetch_fn(dist->name);
893 1.1 dholland if (rval != SET_OK)
894 1.1 dholland return rval;
895 1.1 dholland }
896 1.1 dholland
897 1.1 dholland /* check tarfile exists */
898 1.1 dholland if (!file_exists_p(path)) {
899 1.1 dholland
900 1.1 dholland #ifdef SUPPORT_8_3_SOURCE_FILESYSTEM
901 1.1 dholland /*
902 1.1 dholland * Update path to use dist->name truncated to the first eight
903 1.1 dholland * characters and check again
904 1.1 dholland */
905 1.1 dholland (void)snprintf(path, sizeof path, "%s/%.8s%.4s", /* 4 as includes '.' */
906 1.1 dholland ext_dir_for_set(dist->name), dist->name, dist_postfix);
907 1.1 dholland if (!file_exists_p(path)) {
908 1.1 dholland #endif /* SUPPORT_8_3_SOURCE_FILESYSTEM */
909 1.1 dholland
910 1.1 dholland tarstats.nnotfound++;
911 1.1 dholland
912 1.1 dholland msg_display(MSG_notarfile, path);
913 1.1 dholland process_menu(MENU_ok, NULL);
914 1.1 dholland return SET_RETRY;
915 1.1 dholland }
916 1.1 dholland #ifdef SUPPORT_8_3_SOURCE_FILESYSTEM
917 1.1 dholland }
918 1.1 dholland #endif /* SUPPORT_8_3_SOURCE_FILESYSTEM */
919 1.1 dholland
920 1.1 dholland tarstats.nfound++;
921 1.1 dholland /* cd to the target root. */
922 1.1 dholland if (update && (dist->set == SET_ETC || dist->set == SET_X11_ETC)) {
923 1.1 dholland make_target_dir("/.sysinst");
924 1.1 dholland target_chdir_or_die("/.sysinst");
925 1.1 dholland } else if (dist->set == SET_PKGSRC)
926 1.1 dholland target_chdir_or_die("/usr");
927 1.1 dholland else
928 1.1 dholland target_chdir_or_die("/");
929 1.1 dholland
930 1.1 dholland /*
931 1.1 dholland * /usr/X11R7/lib/X11/xkb/symbols/pc was a directory in 5.0
932 1.1 dholland * but is a file in 5.1 and beyond, so on upgrades we need to
933 1.1 dholland * delete it before extracting the xbase set.
934 1.1 dholland */
935 1.1 dholland if (update && dist->set == SET_X11_BASE)
936 1.1 dholland run_program(0, "rm -rf usr/X11R7/lib/X11/xkb/symbols/pc");
937 1.1 dholland
938 1.1 dholland /* now extract set files into "./". */
939 1.1 dholland rval = run_program(RUN_DISPLAY | RUN_PROGRESS,
940 1.1 dholland "progress -zf %s tar --chroot -xhepf -", path);
941 1.1 dholland
942 1.1 dholland chdir(owd);
943 1.1 dholland free(owd);
944 1.1 dholland
945 1.1 dholland /* Check rval for errors and give warning. */
946 1.1 dholland if (rval != 0) {
947 1.1 dholland tarstats.nerror++;
948 1.1 dholland msg_display(MSG_tarerror, path);
949 1.1 dholland process_menu(MENU_ok, NULL);
950 1.1 dholland return SET_RETRY;
951 1.1 dholland }
952 1.1 dholland
953 1.1 dholland if (fetch_fn != NULL && clean_xfer_dir) {
954 1.1 dholland run_program(0, "rm %s", path);
955 1.1 dholland /* Plausibly we should unlink an empty xfer_dir as well */
956 1.1 dholland }
957 1.1 dholland
958 1.1 dholland set_status[dist->set] |= SET_INSTALLED;
959 1.1 dholland tarstats.nsuccess++;
960 1.1 dholland return SET_OK;
961 1.1 dholland }
962 1.1 dholland
963 1.1 dholland static void
964 1.1 dholland skip_set(distinfo *dist, int skip_type)
965 1.1 dholland {
966 1.1 dholland int nested;
967 1.1 dholland int set;
968 1.1 dholland
969 1.1 dholland nested = 0;
970 1.1 dholland while ((++dist)->set != SET_GROUP_END || nested--) {
971 1.1 dholland set = dist->set;
972 1.1 dholland if (set == SET_GROUP) {
973 1.1 dholland nested++;
974 1.1 dholland continue;
975 1.1 dholland }
976 1.1 dholland if (set == SET_LAST)
977 1.1 dholland break;
978 1.1 dholland if (set_status[set] == (SET_SELECTED | SET_VALID))
979 1.1 dholland set_status[set] |= SET_SKIPPED;
980 1.1 dholland tarstats.nskipped++;
981 1.1 dholland }
982 1.1 dholland }
983 1.1 dholland
984 1.1 dholland /*
985 1.1 dholland * Get and unpack the distribution.
986 1.1 dholland * Show success_msg if installation completes.
987 1.1 dholland * Otherwise show failure_msg and wait for the user to ack it before continuing.
988 1.1 dholland * success_msg and failure_msg must both be 0-adic messages.
989 1.1 dholland */
990 1.1 dholland int
991 1.1 dholland get_and_unpack_sets(int update, msg setupdone_msg, msg success_msg, msg failure_msg)
992 1.1 dholland {
993 1.1 dholland distinfo *dist;
994 1.1 dholland int status;
995 1.1 dholland int set;
996 1.1 dholland
997 1.1 dholland /* Ensure mountpoint for distribution files exists in current root. */
998 1.1 dholland (void)mkdir("/mnt2", S_IRWXU| S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH);
999 1.1 dholland if (script)
1000 1.1 dholland (void)fprintf(script, "mkdir -m 755 /mnt2\n");
1001 1.1 dholland
1002 1.1 dholland /* reset failure/success counters */
1003 1.1 dholland memset(&tarstats, 0, sizeof(tarstats));
1004 1.1 dholland
1005 1.1 dholland /* Find out which files to "get" if we get files. */
1006 1.1 dholland
1007 1.1 dholland /* Accurately count selected sets */
1008 1.1 dholland for (dist = dist_list; (set = dist->set) != SET_LAST; dist++) {
1009 1.1 dholland if ((set_status[set] & (SET_VALID | SET_SELECTED))
1010 1.1 dholland == (SET_VALID | SET_SELECTED))
1011 1.1 dholland tarstats.nselected++;
1012 1.1 dholland }
1013 1.1 dholland
1014 1.1 dholland status = SET_RETRY;
1015 1.1 dholland for (dist = dist_list; ; dist++) {
1016 1.1 dholland set = dist->set;
1017 1.1 dholland if (set == SET_LAST)
1018 1.1 dholland break;
1019 1.1 dholland if (dist->name == NULL)
1020 1.1 dholland continue;
1021 1.1 dholland if (set_status[set] != (SET_VALID | SET_SELECTED))
1022 1.1 dholland continue;
1023 1.1 dholland
1024 1.1 dholland if (status != SET_OK) {
1025 1.1 dholland /* This might force a redraw.... */
1026 1.1 dholland clearok(curscr, 1);
1027 1.1 dholland touchwin(stdscr);
1028 1.1 dholland wrefresh(stdscr);
1029 1.1 dholland /* Sort out the location of the set files */
1030 1.1 dholland do {
1031 1.1 dholland umount_mnt2();
1032 1.1 dholland msg_display(MSG_distmedium, tarstats.nselected,
1033 1.1 dholland tarstats.nsuccess + tarstats.nskipped,
1034 1.1 dholland dist->name);
1035 1.1 dholland fetch_fn = NULL;
1036 1.1 dholland process_menu(MENU_distmedium, &status);
1037 1.1 dholland } while (status == SET_RETRY);
1038 1.1 dholland
1039 1.1 dholland if (status == SET_SKIP) {
1040 1.1 dholland set_status[set] |= SET_SKIPPED;
1041 1.1 dholland tarstats.nskipped++;
1042 1.1 dholland continue;
1043 1.1 dholland }
1044 1.1 dholland if (status == SET_SKIP_GROUP) {
1045 1.1 dholland skip_set(dist, status);
1046 1.1 dholland continue;
1047 1.1 dholland }
1048 1.1 dholland if (status != SET_OK) {
1049 1.1 dholland msg_display(failure_msg);
1050 1.1 dholland process_menu(MENU_ok, NULL);
1051 1.1 dholland return 1;
1052 1.1 dholland }
1053 1.1 dholland }
1054 1.1 dholland
1055 1.1 dholland /* Try to extract this set */
1056 1.1 dholland status = extract_file(dist, update);
1057 1.1 dholland if (status == SET_RETRY)
1058 1.1 dholland dist--;
1059 1.1 dholland }
1060 1.1 dholland
1061 1.1 dholland if (tarstats.nerror == 0 && tarstats.nsuccess == tarstats.nselected) {
1062 1.1 dholland msg_display(MSG_endtarok);
1063 1.1 dholland /* Give user a chance to see the success message */
1064 1.1 dholland sleep(1);
1065 1.1 dholland } else {
1066 1.1 dholland /* We encountered errors. Let the user know. */
1067 1.1 dholland msg_display(MSG_endtar,
1068 1.1 dholland tarstats.nselected, tarstats.nnotfound, tarstats.nskipped,
1069 1.1 dholland tarstats.nfound, tarstats.nsuccess, tarstats.nerror);
1070 1.1 dholland process_menu(MENU_ok, NULL);
1071 1.1 dholland msg_clear();
1072 1.1 dholland }
1073 1.1 dholland
1074 1.1 dholland /*
1075 1.1 dholland * postinstall needs to be run after extracting all sets, because
1076 1.1 dholland * otherwise /var/db/obsolete will only have current information
1077 1.1 dholland * from the base, comp, and etc sets.
1078 1.1 dholland */
1079 1.1 dholland if (update && (set_status[SET_ETC] & SET_INSTALLED)) {
1080 1.1 dholland int oldsendmail;
1081 1.1 dholland oldsendmail = run_program(RUN_DISPLAY | RUN_CHROOT |
1082 1.1 dholland RUN_ERROR_OK | RUN_PROGRESS,
1083 1.1 dholland "/usr/sbin/postinstall -s /.sysinst -d / check mailerconf");
1084 1.1 dholland if (oldsendmail == 1) {
1085 1.1 dholland msg_display(MSG_oldsendmail);
1086 1.1 dholland process_menu(MENU_yesno, NULL);
1087 1.1 dholland if (yesno) {
1088 1.1 dholland run_program(RUN_DISPLAY | RUN_CHROOT,
1089 1.1 dholland "/usr/sbin/postinstall -s /.sysinst -d / fix mailerconf");
1090 1.1 dholland }
1091 1.1 dholland }
1092 1.1 dholland run_program(RUN_DISPLAY | RUN_CHROOT,
1093 1.1 dholland "/usr/sbin/postinstall -s /.sysinst -d / fix");
1094 1.1 dholland }
1095 1.1 dholland
1096 1.1 dholland /* Configure the system */
1097 1.1 dholland if (set_status[SET_BASE] & SET_INSTALLED)
1098 1.1 dholland run_makedev();
1099 1.1 dholland
1100 1.1 dholland if (!update) {
1101 1.1 dholland /* Save keybard type */
1102 1.1 dholland save_kb_encoding();
1103 1.1 dholland
1104 1.1 dholland /* Other configuration. */
1105 1.1 dholland mnt_net_config();
1106 1.1 dholland }
1107 1.1 dholland
1108 1.1 dholland /* Mounted dist dir? */
1109 1.1 dholland umount_mnt2();
1110 1.1 dholland
1111 1.1 dholland /* Install/Upgrade complete ... reboot or exit to script */
1112 1.1 dholland msg_display(success_msg);
1113 1.1 dholland process_menu(MENU_ok, NULL);
1114 1.1 dholland return 0;
1115 1.1 dholland }
1116 1.1 dholland
1117 1.1 dholland void
1118 1.1 dholland umount_mnt2(void)
1119 1.1 dholland {
1120 1.1 dholland if (!mnt2_mounted)
1121 1.1 dholland return;
1122 1.1 dholland run_program(RUN_SILENT, "/sbin/umount /mnt2");
1123 1.1 dholland mnt2_mounted = 0;
1124 1.1 dholland }
1125 1.1 dholland
1126 1.1 dholland
1127 1.1 dholland /*
1128 1.1 dholland * Do a quick sanity check that the target can reboot.
1129 1.1 dholland * return 1 if everything OK, 0 if there is a problem.
1130 1.1 dholland * Uses a table of files we expect to find after a base install/upgrade.
1131 1.1 dholland */
1132 1.1 dholland
1133 1.1 dholland /* test flag and pathname to check for after unpacking. */
1134 1.1 dholland struct check_table { unsigned int mode; const char *path;} checks[] = {
1135 1.1 dholland { S_IFREG, "/netbsd" },
1136 1.1 dholland { S_IFDIR, "/etc" },
1137 1.1 dholland { S_IFREG, "/etc/fstab" },
1138 1.1 dholland { S_IFREG, "/sbin/init" },
1139 1.1 dholland { S_IFREG, "/bin/sh" },
1140 1.1 dholland { S_IFREG, "/etc/rc" },
1141 1.1 dholland { S_IFREG, "/etc/rc.subr" },
1142 1.1 dholland { S_IFREG, "/etc/rc.conf" },
1143 1.1 dholland { S_IFDIR, "/dev" },
1144 1.1 dholland { S_IFCHR, "/dev/console" },
1145 1.1 dholland /* XXX check for rootdev in target /dev? */
1146 1.1 dholland { S_IFREG, "/sbin/fsck" },
1147 1.1 dholland { S_IFREG, "/sbin/fsck_ffs" },
1148 1.1 dholland { S_IFREG, "/sbin/mount" },
1149 1.1 dholland { S_IFREG, "/sbin/mount_ffs" },
1150 1.1 dholland { S_IFREG, "/sbin/mount_nfs" },
1151 1.1 dholland #if defined(DEBUG) || defined(DEBUG_CHECK)
1152 1.1 dholland { S_IFREG, "/foo/bar" }, /* bad entry to exercise warning */
1153 1.1 dholland #endif
1154 1.1 dholland { 0, 0 }
1155 1.1 dholland
1156 1.1 dholland };
1157 1.1 dholland
1158 1.1 dholland /*
1159 1.1 dholland * Check target for a single file.
1160 1.1 dholland */
1161 1.1 dholland static int
1162 1.1 dholland check_for(unsigned int mode, const char *pathname)
1163 1.1 dholland {
1164 1.1 dholland int found;
1165 1.1 dholland
1166 1.1 dholland found = (target_test(mode, pathname) == 0);
1167 1.1 dholland if (found == 0)
1168 1.1 dholland msg_display(MSG_rootmissing, pathname);
1169 1.1 dholland return found;
1170 1.1 dholland }
1171 1.1 dholland
1172 1.1 dholland /*
1173 1.1 dholland * Check that all the files in check_table are present in the
1174 1.1 dholland * target root. Warn if not found.
1175 1.1 dholland */
1176 1.1 dholland int
1177 1.1 dholland sanity_check(void)
1178 1.1 dholland {
1179 1.1 dholland int target_ok = 1;
1180 1.1 dholland struct check_table *p;
1181 1.1 dholland
1182 1.1 dholland for (p = checks; p->path; p++) {
1183 1.1 dholland target_ok = target_ok && check_for(p->mode, p->path);
1184 1.1 dholland }
1185 1.1 dholland if (target_ok)
1186 1.1 dholland return 0;
1187 1.1 dholland
1188 1.1 dholland /* Uh, oh. Something's missing. */
1189 1.1 dholland msg_display(MSG_badroot);
1190 1.1 dholland process_menu(MENU_ok, NULL);
1191 1.1 dholland return 1;
1192 1.1 dholland }
1193 1.1 dholland
1194 1.1 dholland /*
1195 1.1 dholland * Some globals to pass things back from callbacks
1196 1.1 dholland */
1197 1.1 dholland static char zoneinfo_dir[STRSIZE];
1198 1.1 dholland static int zonerootlen;
1199 1.1 dholland static char *tz_selected; /* timezonename (relative to share/zoneinfo */
1200 1.1 dholland const char *tz_default; /* UTC, or whatever /etc/localtime points to */
1201 1.1 dholland static char tz_env[STRSIZE];
1202 1.1 dholland static int save_cursel, save_topline;
1203 1.1 dholland
1204 1.1 dholland /*
1205 1.1 dholland * Callback from timezone menu
1206 1.1 dholland */
1207 1.1 dholland static int
1208 1.1 dholland set_tz_select(menudesc *m, void *arg)
1209 1.1 dholland {
1210 1.1 dholland time_t t;
1211 1.1 dholland char *new;
1212 1.1 dholland
1213 1.1 dholland if (m && strcmp(tz_selected, m->opts[m->cursel].opt_name) != 0) {
1214 1.1 dholland /* Change the displayed timezone */
1215 1.1 dholland new = strdup(m->opts[m->cursel].opt_name);
1216 1.1 dholland if (new == NULL)
1217 1.1 dholland return 0;
1218 1.1 dholland free(tz_selected);
1219 1.1 dholland tz_selected = new;
1220 1.1 dholland snprintf(tz_env, sizeof tz_env, "%.*s%s",
1221 1.1 dholland zonerootlen, zoneinfo_dir, tz_selected);
1222 1.1 dholland setenv("TZ", tz_env, 1);
1223 1.1 dholland }
1224 1.1 dholland if (m)
1225 1.1 dholland /* Warp curser to 'Exit' line on menu */
1226 1.1 dholland m->cursel = -1;
1227 1.1 dholland
1228 1.1 dholland /* Update displayed time */
1229 1.1 dholland t = time(NULL);
1230 1.1 dholland msg_display(MSG_choose_timezone,
1231 1.1 dholland tz_default, tz_selected, ctime(&t), localtime(&t)->tm_zone);
1232 1.1 dholland return 0;
1233 1.1 dholland }
1234 1.1 dholland
1235 1.1 dholland static int
1236 1.1 dholland set_tz_back(menudesc *m, void *arg)
1237 1.1 dholland {
1238 1.1 dholland
1239 1.1 dholland zoneinfo_dir[zonerootlen] = 0;
1240 1.1 dholland m->cursel = save_cursel;
1241 1.1 dholland m->topline = save_topline;
1242 1.1 dholland return 0;
1243 1.1 dholland }
1244 1.1 dholland
1245 1.1 dholland static int
1246 1.1 dholland set_tz_dir(menudesc *m, void *arg)
1247 1.1 dholland {
1248 1.1 dholland
1249 1.1 dholland strlcpy(zoneinfo_dir + zonerootlen, m->opts[m->cursel].opt_name,
1250 1.1 dholland sizeof zoneinfo_dir - zonerootlen);
1251 1.1 dholland save_cursel = m->cursel;
1252 1.1 dholland save_topline = m->topline;
1253 1.1 dholland m->cursel = 0;
1254 1.1 dholland m->topline = 0;
1255 1.1 dholland return 0;
1256 1.1 dholland }
1257 1.1 dholland
1258 1.1 dholland /*
1259 1.1 dholland * Alarm-handler to update example-display
1260 1.1 dholland */
1261 1.1 dholland static void
1262 1.1 dholland /*ARGSUSED*/
1263 1.1 dholland timezone_sig(int sig)
1264 1.1 dholland {
1265 1.1 dholland
1266 1.1 dholland set_tz_select(NULL, NULL);
1267 1.1 dholland alarm(60);
1268 1.1 dholland }
1269 1.1 dholland
1270 1.1 dholland static int
1271 1.1 dholland tz_sort(const void *a, const void *b)
1272 1.1 dholland {
1273 1.1 dholland return strcmp(((const menu_ent *)a)->opt_name, ((const menu_ent *)b)->opt_name);
1274 1.1 dholland }
1275 1.1 dholland
1276 1.1 dholland static void
1277 1.1 dholland tzm_set_names(menudesc *m, void *arg)
1278 1.1 dholland {
1279 1.1 dholland DIR *dir;
1280 1.1 dholland struct dirent *dp;
1281 1.1 dholland static int nfiles;
1282 1.1 dholland static int maxfiles = 32;
1283 1.1 dholland static menu_ent *tz_menu;
1284 1.1 dholland static char **tz_names;
1285 1.1 dholland void *p;
1286 1.1 dholland int maxfname;
1287 1.1 dholland char *fp;
1288 1.1 dholland struct stat sb;
1289 1.1 dholland
1290 1.1 dholland if (tz_menu == NULL)
1291 1.1 dholland tz_menu = malloc(maxfiles * sizeof *tz_menu);
1292 1.1 dholland if (tz_names == NULL)
1293 1.1 dholland tz_names = malloc(maxfiles * sizeof *tz_names);
1294 1.1 dholland if (tz_menu == NULL || tz_names == NULL)
1295 1.1 dholland return; /* error - skip timezone setting */
1296 1.1 dholland while (nfiles > 0)
1297 1.1 dholland free(tz_names[--nfiles]);
1298 1.1 dholland
1299 1.1 dholland dir = opendir(zoneinfo_dir);
1300 1.1 dholland fp = strchr(zoneinfo_dir, 0);
1301 1.1 dholland if (fp != zoneinfo_dir + zonerootlen) {
1302 1.1 dholland tz_names[0] = 0;
1303 1.1 dholland tz_menu[0].opt_name = msg_string(MSG_tz_back);
1304 1.1 dholland tz_menu[0].opt_menu = OPT_NOMENU;
1305 1.1 dholland tz_menu[0].opt_flags = 0;
1306 1.1 dholland tz_menu[0].opt_action = set_tz_back;
1307 1.1 dholland nfiles = 1;
1308 1.1 dholland }
1309 1.1 dholland maxfname = zoneinfo_dir + sizeof zoneinfo_dir - fp - 1;
1310 1.1 dholland if (dir != NULL) {
1311 1.1 dholland while ((dp = readdir(dir)) != NULL) {
1312 1.1 dholland if (dp->d_namlen > maxfname || dp->d_name[0] == '.')
1313 1.1 dholland continue;
1314 1.1 dholland strlcpy(fp, dp->d_name, maxfname);
1315 1.1 dholland if (stat(zoneinfo_dir, &sb) == -1)
1316 1.1 dholland continue;
1317 1.1 dholland if (nfiles >= maxfiles) {
1318 1.1 dholland p = realloc(tz_menu, 2 * maxfiles * sizeof *tz_menu);
1319 1.1 dholland if (p == NULL)
1320 1.1 dholland break;
1321 1.1 dholland tz_menu = p;
1322 1.1 dholland p = realloc(tz_names, 2 * maxfiles * sizeof *tz_names);
1323 1.1 dholland if (p == NULL)
1324 1.1 dholland break;
1325 1.1 dholland tz_names = p;
1326 1.1 dholland maxfiles *= 2;
1327 1.1 dholland }
1328 1.1 dholland if (S_ISREG(sb.st_mode))
1329 1.1 dholland tz_menu[nfiles].opt_action = set_tz_select;
1330 1.1 dholland else if (S_ISDIR(sb.st_mode)) {
1331 1.1 dholland tz_menu[nfiles].opt_action = set_tz_dir;
1332 1.1 dholland strlcat(fp, "/",
1333 1.1 dholland sizeof(zoneinfo_dir) - (fp - zoneinfo_dir));
1334 1.1 dholland } else
1335 1.1 dholland continue;
1336 1.1 dholland tz_names[nfiles] = strdup(zoneinfo_dir + zonerootlen);
1337 1.1 dholland tz_menu[nfiles].opt_name = tz_names[nfiles];
1338 1.1 dholland tz_menu[nfiles].opt_menu = OPT_NOMENU;
1339 1.1 dholland tz_menu[nfiles].opt_flags = 0;
1340 1.1 dholland nfiles++;
1341 1.1 dholland }
1342 1.1 dholland closedir(dir);
1343 1.1 dholland }
1344 1.1 dholland *fp = 0;
1345 1.1 dholland
1346 1.1 dholland m->opts = tz_menu;
1347 1.1 dholland m->numopts = nfiles;
1348 1.1 dholland qsort(tz_menu, nfiles, sizeof *tz_menu, tz_sort);
1349 1.1 dholland }
1350 1.1 dholland
1351 1.1 dholland void
1352 1.1 dholland get_tz_default(void)
1353 1.1 dholland {
1354 1.1 dholland char localtime_link[STRSIZE];
1355 1.1 dholland static char localtime_target[STRSIZE];
1356 1.1 dholland int rc;
1357 1.1 dholland
1358 1.1 dholland strlcpy(localtime_link, target_expand("/etc/localtime"),
1359 1.1 dholland sizeof localtime_link);
1360 1.1 dholland
1361 1.1 dholland /* Add sanity check that /mnt/usr/share/zoneinfo contains
1362 1.1 dholland * something useful
1363 1.1 dholland */
1364 1.1 dholland
1365 1.1 dholland rc = readlink(localtime_link, localtime_target,
1366 1.1 dholland sizeof(localtime_target) - 1);
1367 1.1 dholland if (rc < 0) {
1368 1.1 dholland /* error, default to UTC */
1369 1.1 dholland tz_default = "UTC";
1370 1.1 dholland } else {
1371 1.1 dholland localtime_target[rc] = '\0';
1372 1.1 dholland tz_default = strchr(strstr(localtime_target, "zoneinfo"), '/') + 1;
1373 1.1 dholland }
1374 1.1 dholland }
1375 1.1 dholland
1376 1.1 dholland /*
1377 1.1 dholland * Choose from the files in usr/share/zoneinfo and set etc/localtime
1378 1.1 dholland */
1379 1.1 dholland int
1380 1.1 dholland set_timezone(void)
1381 1.1 dholland {
1382 1.1 dholland char localtime_link[STRSIZE];
1383 1.1 dholland char localtime_target[STRSIZE];
1384 1.1 dholland time_t t;
1385 1.1 dholland int menu_no;
1386 1.1 dholland
1387 1.1 dholland strlcpy(zoneinfo_dir, target_expand("/usr/share/zoneinfo/"),
1388 1.1 dholland sizeof zoneinfo_dir - 1);
1389 1.1 dholland zonerootlen = strlen(zoneinfo_dir);
1390 1.1 dholland
1391 1.1 dholland get_tz_default();
1392 1.1 dholland
1393 1.1 dholland tz_selected = strdup(tz_default);
1394 1.1 dholland snprintf(tz_env, sizeof(tz_env), "%s%s", zoneinfo_dir, tz_selected);
1395 1.1 dholland setenv("TZ", tz_env, 1);
1396 1.1 dholland t = time(NULL);
1397 1.1 dholland msg_display(MSG_choose_timezone,
1398 1.1 dholland tz_default, tz_selected, ctime(&t), localtime(&t)->tm_zone);
1399 1.1 dholland
1400 1.1 dholland signal(SIGALRM, timezone_sig);
1401 1.1 dholland alarm(60);
1402 1.1 dholland
1403 1.1 dholland menu_no = new_menu(NULL, NULL, 14, 23, 9,
1404 1.1 dholland 12, 32, MC_ALWAYS_SCROLL | MC_NOSHORTCUT,
1405 1.1 dholland tzm_set_names, NULL, NULL,
1406 1.1 dholland "\nPlease consult the install documents.", NULL);
1407 1.1 dholland if (menu_no < 0)
1408 1.1 dholland goto done; /* error - skip timezone setting */
1409 1.1 dholland
1410 1.1 dholland process_menu(menu_no, NULL);
1411 1.1 dholland
1412 1.1 dholland free_menu(menu_no);
1413 1.1 dholland
1414 1.1 dholland signal(SIGALRM, SIG_IGN);
1415 1.1 dholland
1416 1.1 dholland snprintf(localtime_target, sizeof(localtime_target),
1417 1.1 dholland "/usr/share/zoneinfo/%s", tz_selected);
1418 1.1 dholland strlcpy(localtime_link, target_expand("/etc/localtime"),
1419 1.1 dholland sizeof localtime_link);
1420 1.1 dholland unlink(localtime_link);
1421 1.1 dholland symlink(localtime_target, localtime_link);
1422 1.1 dholland
1423 1.1 dholland done:
1424 1.1 dholland return 1;
1425 1.1 dholland }
1426 1.1 dholland
1427 1.1 dholland void
1428 1.1 dholland scripting_vfprintf(FILE *f, const char *fmt, va_list ap)
1429 1.1 dholland {
1430 1.1 dholland
1431 1.1 dholland if (f)
1432 1.1 dholland (void)vfprintf(f, fmt, ap);
1433 1.1 dholland if (script)
1434 1.1 dholland (void)vfprintf(script, fmt, ap);
1435 1.1 dholland }
1436 1.1 dholland
1437 1.1 dholland void
1438 1.1 dholland scripting_fprintf(FILE *f, const char *fmt, ...)
1439 1.1 dholland {
1440 1.1 dholland va_list ap;
1441 1.1 dholland
1442 1.1 dholland va_start(ap, fmt);
1443 1.1 dholland scripting_vfprintf(f, fmt, ap);
1444 1.1 dholland va_end(ap);
1445 1.1 dholland }
1446 1.1 dholland
1447 1.1 dholland void
1448 1.1 dholland add_rc_conf(const char *fmt, ...)
1449 1.1 dholland {
1450 1.1 dholland FILE *f;
1451 1.1 dholland va_list ap;
1452 1.1 dholland
1453 1.1 dholland va_start(ap, fmt);
1454 1.1 dholland f = target_fopen("/etc/rc.conf", "a");
1455 1.1 dholland if (f != 0) {
1456 1.1 dholland scripting_fprintf(NULL, "cat <<EOF >>%s/etc/rc.conf\n",
1457 1.1 dholland target_prefix());
1458 1.1 dholland scripting_vfprintf(f, fmt, ap);
1459 1.1 dholland fclose(f);
1460 1.1 dholland scripting_fprintf(NULL, "EOF\n");
1461 1.1 dholland }
1462 1.1 dholland va_end(ap);
1463 1.1 dholland }
1464 1.1 dholland
1465 1.1 dholland int
1466 1.1 dholland del_rc_conf(const char *value)
1467 1.1 dholland {
1468 1.1 dholland FILE *fp, *nfp;
1469 1.1 dholland char buf[4096]; /* Ridiculously high, but should be enough in any way */
1470 1.1 dholland char *rcconf, *tempname = NULL, *bakname = NULL;
1471 1.1 dholland char *cp;
1472 1.1 dholland int done = 0;
1473 1.1 dholland int fd;
1474 1.1 dholland int retval = 0;
1475 1.1 dholland
1476 1.1 dholland /* The paths might seem strange, but using /tmp would require copy instead
1477 1.1 dholland * of rename operations. */
1478 1.1 dholland if (asprintf(&rcconf, "%s", target_expand("/etc/rc.conf")) < 0
1479 1.1 dholland || asprintf(&tempname, "%s", target_expand("/etc/rc.conf.tmp.XXXXXX")) < 0
1480 1.1 dholland || asprintf(&bakname, "%s", target_expand("/etc/rc.conf.bak.XXXXXX")) < 0) {
1481 1.1 dholland if (rcconf)
1482 1.1 dholland free(rcconf);
1483 1.1 dholland if (tempname)
1484 1.1 dholland free(tempname);
1485 1.1 dholland msg_display(MSG_rcconf_delete_failed, value);
1486 1.1 dholland process_menu(MENU_ok, NULL);
1487 1.1 dholland return -1;
1488 1.1 dholland }
1489 1.1 dholland
1490 1.1 dholland if ((fd = mkstemp(bakname)) < 0) {
1491 1.1 dholland msg_display(MSG_rcconf_delete_failed, value);
1492 1.1 dholland process_menu(MENU_ok, NULL);
1493 1.1 dholland return -1;
1494 1.1 dholland }
1495 1.1 dholland close(fd);
1496 1.1 dholland
1497 1.1 dholland if (!(fp = fopen(rcconf, "r+")) || (fd = mkstemp(tempname)) < 0) {
1498 1.1 dholland if (fp)
1499 1.1 dholland fclose(fp);
1500 1.1 dholland msg_display(MSG_rcconf_delete_failed, value);
1501 1.1 dholland process_menu(MENU_ok, NULL);
1502 1.1 dholland return -1;
1503 1.1 dholland }
1504 1.1 dholland
1505 1.1 dholland nfp = fdopen(fd, "w");
1506 1.1 dholland if (!nfp) {
1507 1.1 dholland fclose(fp);
1508 1.1 dholland close(fd);
1509 1.1 dholland msg_display(MSG_rcconf_delete_failed, value);
1510 1.1 dholland process_menu(MENU_ok, NULL);
1511 1.1 dholland return -1;
1512 1.1 dholland }
1513 1.1 dholland
1514 1.1 dholland while (fgets(buf, sizeof buf, fp) != NULL) {
1515 1.1 dholland
1516 1.1 dholland cp = buf + strspn(buf, " \t"); /* Skip initial spaces */
1517 1.1 dholland if (strncmp(cp, value, strlen(value)) == 0) {
1518 1.1 dholland cp += strlen(value);
1519 1.1 dholland if (*cp != '=')
1520 1.1 dholland scripting_fprintf(nfp, "%s", buf);
1521 1.1 dholland else
1522 1.1 dholland done = 1;
1523 1.1 dholland } else {
1524 1.1 dholland scripting_fprintf(nfp, "%s", buf);
1525 1.1 dholland }
1526 1.1 dholland }
1527 1.1 dholland fclose(fp);
1528 1.1 dholland fclose(nfp);
1529 1.1 dholland
1530 1.1 dholland if (done) {
1531 1.1 dholland if (rename(rcconf, bakname)) {
1532 1.1 dholland msg_display(MSG_rcconf_backup_failed);
1533 1.1 dholland process_menu(MENU_noyes, NULL);
1534 1.1 dholland if (!yesno) {
1535 1.1 dholland retval = -1;
1536 1.1 dholland goto done;
1537 1.1 dholland }
1538 1.1 dholland }
1539 1.1 dholland
1540 1.1 dholland if (rename(tempname, rcconf)) {
1541 1.1 dholland if (rename(bakname, rcconf)) {
1542 1.1 dholland msg_display(MSG_rcconf_restore_failed);
1543 1.1 dholland process_menu(MENU_ok, NULL);
1544 1.1 dholland } else {
1545 1.1 dholland msg_display(MSG_rcconf_delete_failed, value);
1546 1.1 dholland process_menu(MENU_ok, NULL);
1547 1.1 dholland }
1548 1.1 dholland } else {
1549 1.1 dholland (void)unlink(bakname);
1550 1.1 dholland }
1551 1.1 dholland }
1552 1.1 dholland
1553 1.1 dholland done:
1554 1.1 dholland (void)unlink(tempname);
1555 1.1 dholland free(rcconf);
1556 1.1 dholland free(tempname);
1557 1.1 dholland free(bakname);
1558 1.1 dholland return retval;
1559 1.1 dholland }
1560 1.1 dholland
1561 1.1 dholland void
1562 1.1 dholland add_sysctl_conf(const char *fmt, ...)
1563 1.1 dholland {
1564 1.1 dholland FILE *f;
1565 1.1 dholland va_list ap;
1566 1.1 dholland
1567 1.1 dholland va_start(ap, fmt);
1568 1.1 dholland f = target_fopen("/etc/sysctl.conf", "a");
1569 1.1 dholland if (f != 0) {
1570 1.1 dholland scripting_fprintf(NULL, "cat <<EOF >>%s/etc/sysctl.conf\n",
1571 1.1 dholland target_prefix());
1572 1.1 dholland scripting_vfprintf(f, fmt, ap);
1573 1.1 dholland fclose(f);
1574 1.1 dholland scripting_fprintf(NULL, "EOF\n");
1575 1.1 dholland }
1576 1.1 dholland va_end(ap);
1577 1.1 dholland }
1578 1.1 dholland
1579 1.1 dholland void
1580 1.1 dholland enable_rc_conf(void)
1581 1.1 dholland {
1582 1.1 dholland
1583 1.1 dholland replace("/etc/rc.conf", "s/^rc_configured=NO/rc_configured=YES/");
1584 1.1 dholland }
1585 1.1 dholland
1586 1.1 dholland int
1587 1.1 dholland check_lfs_progs(void)
1588 1.1 dholland {
1589 1.1 dholland
1590 1.1 dholland #ifndef NO_LFS
1591 1.1 dholland return (access("/sbin/fsck_lfs", X_OK) == 0 &&
1592 1.1 dholland access("/sbin/mount_lfs", X_OK) == 0 &&
1593 1.1 dholland access("/sbin/newfs_lfs", X_OK) == 0);
1594 1.1 dholland #else
1595 1.1 dholland return 0;
1596 1.1 dholland #endif
1597 1.1 dholland }
1598 1.1 dholland
1599 1.1 dholland int
1600 1.1 dholland set_is_source(const char *set_name) {
1601 1.1 dholland int len = strlen(set_name);
1602 1.1 dholland return len >= 3 && memcmp(set_name + len - 3, "src", 3) == 0;
1603 1.1 dholland }
1604 1.1 dholland
1605 1.1 dholland const char *
1606 1.1 dholland set_dir_for_set(const char *set_name) {
1607 1.1 dholland if (strcmp(set_name, "pkgsrc") == 0)
1608 1.1 dholland return pkgsrc_dir;
1609 1.1 dholland return set_is_source(set_name) ? set_dir_src : set_dir_bin;
1610 1.1 dholland }
1611 1.1 dholland
1612 1.1 dholland const char *
1613 1.1 dholland ext_dir_for_set(const char *set_name) {
1614 1.1 dholland if (strcmp(set_name, "pkgsrc") == 0)
1615 1.1 dholland return ext_dir_pkgsrc;
1616 1.1 dholland return set_is_source(set_name) ? ext_dir_src : ext_dir_bin;
1617 1.1 dholland }
1618 1.1 dholland
1619