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