10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51746Svikram * Common Development and Distribution License (the "License"). 61746Svikram * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 221746Svikram * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * bootadm(1M) is a new utility for managing bootability of 300Sstevel@tonic-gate * Solaris *Newboot* environments. It has two primary tasks: 310Sstevel@tonic-gate * - Allow end users to manage bootability of Newboot Solaris instances 320Sstevel@tonic-gate * - Provide services to other subsystems in Solaris (primarily Install) 330Sstevel@tonic-gate */ 340Sstevel@tonic-gate 350Sstevel@tonic-gate /* Headers */ 360Sstevel@tonic-gate #include <stdio.h> 370Sstevel@tonic-gate #include <errno.h> 380Sstevel@tonic-gate #include <stdlib.h> 390Sstevel@tonic-gate #include <string.h> 400Sstevel@tonic-gate #include <unistd.h> 410Sstevel@tonic-gate #include <sys/types.h> 420Sstevel@tonic-gate #include <sys/stat.h> 430Sstevel@tonic-gate #include <stdarg.h> 440Sstevel@tonic-gate #include <limits.h> 450Sstevel@tonic-gate #include <signal.h> 460Sstevel@tonic-gate #include <sys/wait.h> 470Sstevel@tonic-gate #include <sys/mnttab.h> 480Sstevel@tonic-gate #include <sys/statvfs.h> 490Sstevel@tonic-gate #include <libnvpair.h> 500Sstevel@tonic-gate #include <ftw.h> 510Sstevel@tonic-gate #include <fcntl.h> 520Sstevel@tonic-gate #include <strings.h> 530Sstevel@tonic-gate #include <sys/systeminfo.h> 540Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 552334Ssetje #include <sys/param.h> 560Sstevel@tonic-gate 570Sstevel@tonic-gate #include <pwd.h> 580Sstevel@tonic-gate #include <grp.h> 590Sstevel@tonic-gate #include <device_info.h> 600Sstevel@tonic-gate 610Sstevel@tonic-gate #include <libintl.h> 620Sstevel@tonic-gate #include <locale.h> 630Sstevel@tonic-gate 640Sstevel@tonic-gate #include <assert.h> 650Sstevel@tonic-gate 660Sstevel@tonic-gate #include "message.h" 670Sstevel@tonic-gate 680Sstevel@tonic-gate #ifndef TEXT_DOMAIN 690Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD" 700Sstevel@tonic-gate #endif /* TEXT_DOMAIN */ 710Sstevel@tonic-gate 720Sstevel@tonic-gate /* Type definitions */ 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* Primary subcmds */ 750Sstevel@tonic-gate typedef enum { 760Sstevel@tonic-gate BAM_MENU = 3, 770Sstevel@tonic-gate BAM_ARCHIVE 780Sstevel@tonic-gate } subcmd_t; 790Sstevel@tonic-gate 800Sstevel@tonic-gate /* GRUB menu per-line classification */ 810Sstevel@tonic-gate typedef enum { 820Sstevel@tonic-gate BAM_INVALID = 0, 830Sstevel@tonic-gate BAM_EMPTY, 840Sstevel@tonic-gate BAM_COMMENT, 850Sstevel@tonic-gate BAM_GLOBAL, 860Sstevel@tonic-gate BAM_ENTRY, 870Sstevel@tonic-gate BAM_TITLE 880Sstevel@tonic-gate } menu_flag_t; 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* struct for menu.lst contents */ 910Sstevel@tonic-gate typedef struct line { 920Sstevel@tonic-gate int lineNum; /* Line number in menu.lst */ 930Sstevel@tonic-gate int entryNum; /* menu boot entry #. ENTRY_INIT if not applicable */ 940Sstevel@tonic-gate char *cmd; 950Sstevel@tonic-gate char *sep; 960Sstevel@tonic-gate char *arg; 970Sstevel@tonic-gate char *line; 980Sstevel@tonic-gate menu_flag_t flags; 990Sstevel@tonic-gate struct line *next; 100662Sszhou struct line *prev; 1010Sstevel@tonic-gate } line_t; 1020Sstevel@tonic-gate 103662Sszhou typedef struct entry { 104662Sszhou struct entry *next; 105662Sszhou struct entry *prev; 106662Sszhou line_t *start; 107662Sszhou line_t *end; 108662Sszhou } entry_t; 109662Sszhou 1100Sstevel@tonic-gate typedef struct { 1110Sstevel@tonic-gate line_t *start; 1120Sstevel@tonic-gate line_t *end; 113662Sszhou line_t *curdefault; /* line containing default */ 114662Sszhou line_t *olddefault; /* old default line (commented) */ 115662Sszhou entry_t *entries; /* os entries */ 1160Sstevel@tonic-gate } menu_t; 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate typedef enum { 1190Sstevel@tonic-gate OPT_ABSENT = 0, /* No option */ 1200Sstevel@tonic-gate OPT_REQ, /* option required */ 1210Sstevel@tonic-gate OPT_OPTIONAL /* option may or may not be present */ 1220Sstevel@tonic-gate } option_t; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate typedef enum { 125316Svikram BAM_ERROR = -1, /* Must be negative. add_boot_entry() depends on it */ 1260Sstevel@tonic-gate BAM_SUCCESS = 0, 1270Sstevel@tonic-gate BAM_WRITE = 2 1280Sstevel@tonic-gate } error_t; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate typedef struct { 1310Sstevel@tonic-gate char *subcmd; 1320Sstevel@tonic-gate option_t option; 1330Sstevel@tonic-gate error_t (*handler)(); 1342115Svikram int unpriv; /* is this an unprivileged command */ 1350Sstevel@tonic-gate } subcmd_defn_t; 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate #define BAM_MAXLINE 8192 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate #define LINE_INIT 0 /* lineNum initial value */ 1410Sstevel@tonic-gate #define ENTRY_INIT -1 /* entryNum initial value */ 1420Sstevel@tonic-gate #define ALL_ENTRIES -2 /* selects all boot entries */ 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate #define GRUB_DIR "/boot/grub" 1450Sstevel@tonic-gate #define MULTI_BOOT "/platform/i86pc/multiboot" 1460Sstevel@tonic-gate #define BOOT_ARCHIVE "/platform/i86pc/boot_archive" 1470Sstevel@tonic-gate #define GRUB_MENU "/boot/grub/menu.lst" 1480Sstevel@tonic-gate #define MENU_TMP "/boot/grub/menu.lst.tmp" 1490Sstevel@tonic-gate #define RAMDISK_SPECIAL "/ramdisk" 150621Svikram #define STUBBOOT "/stubboot" 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate /* lock related */ 1530Sstevel@tonic-gate #define BAM_LOCK_FILE "/var/run/bootadm.lock" 1540Sstevel@tonic-gate #define LOCK_FILE_PERMS (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate #define CREATE_RAMDISK "/boot/solaris/bin/create_ramdisk" 1570Sstevel@tonic-gate #define CREATE_DISKMAP "/boot/solaris/bin/create_diskmap" 1580Sstevel@tonic-gate #define GRUBDISK_MAP "/var/run/solaris_grubdisk.map" 1590Sstevel@tonic-gate 160316Svikram #define GRUB_slice "/etc/lu/GRUB_slice" 161316Svikram #define GRUB_root "/etc/lu/GRUB_root" 162316Svikram #define GRUB_backup_menu "/etc/lu/GRUB_backup_menu" 163316Svikram #define GRUB_slice_mntpt "/tmp/GRUB_slice_mntpt" 164316Svikram #define LU_ACTIVATE_FILE "/etc/lu/DelayUpdate/activate.sh" 1651746Svikram #define GRUB_fdisk "/etc/lu/GRUB_fdisk" 1661746Svikram #define GRUB_fdisk_target "/etc/lu/GRUB_fdisk_target" 167316Svikram 168316Svikram #define INSTALLGRUB "/sbin/installgrub" 169316Svikram #define STAGE1 "/boot/grub/stage1" 170316Svikram #define STAGE2 "/boot/grub/stage2" 171316Svikram 1720Sstevel@tonic-gate /* 1730Sstevel@tonic-gate * Default file attributes 1740Sstevel@tonic-gate */ 1750Sstevel@tonic-gate #define DEFAULT_DEV_MODE 0644 /* default permissions */ 1760Sstevel@tonic-gate #define DEFAULT_DEV_UID 0 /* user root */ 1770Sstevel@tonic-gate #define DEFAULT_DEV_GID 3 /* group sys */ 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate /* 1800Sstevel@tonic-gate * Menu related 1810Sstevel@tonic-gate * menu_cmd_t and menu_cmds must be kept in sync 1820Sstevel@tonic-gate */ 1830Sstevel@tonic-gate typedef enum { 1840Sstevel@tonic-gate DEFAULT_CMD = 0, 1850Sstevel@tonic-gate TIMEOUT_CMD, 1860Sstevel@tonic-gate TITLE_CMD, 1870Sstevel@tonic-gate ROOT_CMD, 1880Sstevel@tonic-gate KERNEL_CMD, 1890Sstevel@tonic-gate MODULE_CMD, 1900Sstevel@tonic-gate SEP_CMD, 1910Sstevel@tonic-gate COMMENT_CMD 1920Sstevel@tonic-gate } menu_cmd_t; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate static char *menu_cmds[] = { 1950Sstevel@tonic-gate "default", /* DEFAULT_CMD */ 1960Sstevel@tonic-gate "timeout", /* TIMEOUT_CMD */ 1970Sstevel@tonic-gate "title", /* TITLE_CMD */ 1980Sstevel@tonic-gate "root", /* ROOT_CMD */ 1990Sstevel@tonic-gate "kernel", /* KERNEL_CMD */ 2000Sstevel@tonic-gate "module", /* MODULE_CMD */ 2010Sstevel@tonic-gate " ", /* SEP_CMD */ 2020Sstevel@tonic-gate "#", /* COMMENT_CMD */ 2030Sstevel@tonic-gate NULL 2040Sstevel@tonic-gate }; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate #define OPT_ENTRY_NUM "entry" 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate /* 2090Sstevel@tonic-gate * archive related 2100Sstevel@tonic-gate */ 2110Sstevel@tonic-gate typedef struct { 2120Sstevel@tonic-gate line_t *head; 2130Sstevel@tonic-gate line_t *tail; 2140Sstevel@tonic-gate } filelist_t; 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate #define BOOT_FILE_LIST "boot/solaris/filelist.ramdisk" 2170Sstevel@tonic-gate #define ETC_FILE_LIST "etc/boot/solaris/filelist.ramdisk" 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate #define FILE_STAT "boot/solaris/filestat.ramdisk" 2200Sstevel@tonic-gate #define FILE_STAT_TMP "boot/solaris/filestat.ramdisk.tmp" 2210Sstevel@tonic-gate #define DIR_PERMS (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) 2220Sstevel@tonic-gate #define FILE_STAT_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate #define BAM_HDR "---------- ADDED BY BOOTADM - DO NOT EDIT ----------" 2250Sstevel@tonic-gate #define BAM_FTR "---------------------END BOOTADM--------------------" 226662Sszhou #define BAM_OLDDEF "BOOTADM SAVED DEFAULT: " 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* Globals */ 2290Sstevel@tonic-gate static char *prog; 2300Sstevel@tonic-gate static subcmd_t bam_cmd; 2310Sstevel@tonic-gate static char *bam_root; 2320Sstevel@tonic-gate static int bam_rootlen; 2330Sstevel@tonic-gate static int bam_root_readonly; 234621Svikram static int bam_alt_root; 2350Sstevel@tonic-gate static char *bam_subcmd; 2360Sstevel@tonic-gate static char *bam_opt; 2370Sstevel@tonic-gate static int bam_debug; 2380Sstevel@tonic-gate static char **bam_argv; 2390Sstevel@tonic-gate static int bam_argc; 2400Sstevel@tonic-gate static int bam_force; 2410Sstevel@tonic-gate static int bam_verbose; 2420Sstevel@tonic-gate static int bam_check; 2430Sstevel@tonic-gate static int bam_smf_check; 2440Sstevel@tonic-gate static int bam_lock_fd = -1; 2450Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/"; 246316Svikram static int bam_update_all; 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate /* function prototypes */ 2490Sstevel@tonic-gate static void parse_args_internal(int argc, char *argv[]); 2500Sstevel@tonic-gate static void parse_args(int argc, char *argv[]); 2510Sstevel@tonic-gate static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]); 2520Sstevel@tonic-gate static error_t bam_archive(char *subcmd, char *opt); 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate static void bam_error(char *format, ...); 2550Sstevel@tonic-gate static void bam_print(char *format, ...); 2560Sstevel@tonic-gate static void bam_exit(int excode); 2570Sstevel@tonic-gate static void bam_lock(void); 2580Sstevel@tonic-gate static void bam_unlock(void); 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate static int exec_cmd(char *cmdline, char *output, int64_t osize); 2610Sstevel@tonic-gate static error_t read_globals(menu_t *mp, char *menu_path, 2620Sstevel@tonic-gate char *globalcmd, int quiet); 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate static menu_t *menu_read(char *menu_path); 2650Sstevel@tonic-gate static error_t menu_write(char *root, menu_t *mp); 2660Sstevel@tonic-gate static void linelist_free(line_t *start); 2670Sstevel@tonic-gate static void menu_free(menu_t *mp); 2680Sstevel@tonic-gate static void line_free(line_t *lp); 2690Sstevel@tonic-gate static void filelist_free(filelist_t *flistp); 2700Sstevel@tonic-gate static error_t list2file(char *root, char *tmp, 2710Sstevel@tonic-gate char *final, line_t *start); 2720Sstevel@tonic-gate static error_t list_entry(menu_t *mp, char *menu_path, char *opt); 2730Sstevel@tonic-gate static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt); 2740Sstevel@tonic-gate static error_t update_entry(menu_t *mp, char *root, char *opt); 2750Sstevel@tonic-gate static error_t update_temp(menu_t *mp, char *root, char *opt); 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate static error_t update_archive(char *root, char *opt); 2780Sstevel@tonic-gate static error_t list_archive(char *root, char *opt); 2790Sstevel@tonic-gate static error_t update_all(char *root, char *opt); 2800Sstevel@tonic-gate static error_t read_list(char *root, filelist_t *flistp); 2810Sstevel@tonic-gate static error_t set_global(menu_t *mp, char *globalcmd, int val); 2820Sstevel@tonic-gate static error_t set_option(menu_t *mp, char *globalcmd, char *opt); 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate static long s_strtol(char *str); 2850Sstevel@tonic-gate static char *s_fgets(char *buf, int n, FILE *fp); 2860Sstevel@tonic-gate static int s_fputs(char *str, FILE *fp); 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate static void *s_calloc(size_t nelem, size_t sz); 2890Sstevel@tonic-gate static char *s_strdup(char *str); 2900Sstevel@tonic-gate static int is_readonly(char *); 2910Sstevel@tonic-gate static int is_amd64(void); 2920Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *); 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate #if defined(__sparc) 2950Sstevel@tonic-gate static void sparc_abort(void); 2960Sstevel@tonic-gate #endif 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate /* Menu related sub commands */ 2990Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = { 3002115Svikram "set_option", OPT_OPTIONAL, set_option, 0, /* PUB */ 3012115Svikram "list_entry", OPT_OPTIONAL, list_entry, 1, /* PUB */ 3022115Svikram "delete_all_entries", OPT_ABSENT, delete_all_entries, 0, /* PVT */ 3032115Svikram "update_entry", OPT_REQ, update_entry, 0, /* menu */ 3042115Svikram "update_temp", OPT_OPTIONAL, update_temp, 0, /* reboot */ 3052115Svikram NULL, 0, NULL, 0 /* must be last */ 3060Sstevel@tonic-gate }; 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate /* Archive related sub commands */ 3090Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = { 3102115Svikram "update", OPT_ABSENT, update_archive, 0, /* PUB */ 3112115Svikram "update_all", OPT_ABSENT, update_all, 0, /* PVT */ 3122115Svikram "list", OPT_OPTIONAL, list_archive, 1, /* PUB */ 3132115Svikram NULL, 0, NULL, 0 /* must be last */ 3140Sstevel@tonic-gate }; 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate static struct { 3170Sstevel@tonic-gate nvlist_t *new_nvlp; 3180Sstevel@tonic-gate nvlist_t *old_nvlp; 3190Sstevel@tonic-gate int need_update; 3200Sstevel@tonic-gate } walk_arg; 3210Sstevel@tonic-gate 3222334Ssetje 3232345Ssetje struct safefile { 3242334Ssetje char *name; 3252334Ssetje struct safefile *next; 3262334Ssetje }; 3272334Ssetje 3282334Ssetje struct safefile *safefiles = NULL; 3292334Ssetje #define NEED_UPDATE_FILE "/etc/svc/volatile/boot_archive_needs_update" 3302334Ssetje 3310Sstevel@tonic-gate static void 3320Sstevel@tonic-gate usage(void) 3330Sstevel@tonic-gate { 3340Sstevel@tonic-gate (void) fprintf(stderr, "USAGE:\n"); 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate /* archive usage */ 3380Sstevel@tonic-gate (void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n", 3390Sstevel@tonic-gate prog); 3400Sstevel@tonic-gate (void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog); 3410Sstevel@tonic-gate #ifndef __sparc 3420Sstevel@tonic-gate /* x86 only */ 3430Sstevel@tonic-gate (void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog); 3440Sstevel@tonic-gate (void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog); 3450Sstevel@tonic-gate #endif 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate int 3490Sstevel@tonic-gate main(int argc, char *argv[]) 3500Sstevel@tonic-gate { 3510Sstevel@tonic-gate error_t ret; 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 3540Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate if ((prog = strrchr(argv[0], '/')) == NULL) { 3570Sstevel@tonic-gate prog = argv[0]; 3580Sstevel@tonic-gate } else { 3590Sstevel@tonic-gate prog++; 3600Sstevel@tonic-gate } 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate /* 3640Sstevel@tonic-gate * Don't depend on caller's umask 3650Sstevel@tonic-gate */ 3660Sstevel@tonic-gate (void) umask(0022); 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate parse_args(argc, argv); 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate #if defined(__sparc) 3710Sstevel@tonic-gate /* 3720Sstevel@tonic-gate * There are only two valid invocations of bootadm 3730Sstevel@tonic-gate * on SPARC: 3740Sstevel@tonic-gate * 3750Sstevel@tonic-gate * - SPARC diskless server creating boot_archive for i386 clients 3760Sstevel@tonic-gate * - archive creation call during reboot of a SPARC system 3770Sstevel@tonic-gate * 3780Sstevel@tonic-gate * The latter should be a NOP 3790Sstevel@tonic-gate */ 3800Sstevel@tonic-gate if (bam_cmd != BAM_ARCHIVE) { 3810Sstevel@tonic-gate sparc_abort(); 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate #endif 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate switch (bam_cmd) { 3860Sstevel@tonic-gate case BAM_MENU: 3870Sstevel@tonic-gate ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv); 3880Sstevel@tonic-gate break; 3890Sstevel@tonic-gate case BAM_ARCHIVE: 3900Sstevel@tonic-gate ret = bam_archive(bam_subcmd, bam_opt); 3910Sstevel@tonic-gate break; 3920Sstevel@tonic-gate default: 3930Sstevel@tonic-gate usage(); 3940Sstevel@tonic-gate bam_exit(1); 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate if (ret != BAM_SUCCESS) 3980Sstevel@tonic-gate bam_exit(1); 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate bam_unlock(); 4010Sstevel@tonic-gate return (0); 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate #if defined(__sparc) 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate static void 4070Sstevel@tonic-gate sparc_abort(void) 4080Sstevel@tonic-gate { 4090Sstevel@tonic-gate bam_error(NOT_ON_SPARC); 4100Sstevel@tonic-gate bam_exit(1); 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate #endif 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate /* 4160Sstevel@tonic-gate * Equivalence of public and internal commands: 4170Sstevel@tonic-gate * update-archive -- -a update 4180Sstevel@tonic-gate * list-archive -- -a list 4190Sstevel@tonic-gate * set-menu -- -m set_option 4200Sstevel@tonic-gate * list-menu -- -m list_entry 4210Sstevel@tonic-gate * update-menu -- -m update_entry 4220Sstevel@tonic-gate */ 4230Sstevel@tonic-gate static struct cmd_map { 4240Sstevel@tonic-gate char *bam_cmdname; 4250Sstevel@tonic-gate int bam_cmd; 4260Sstevel@tonic-gate char *bam_subcmd; 4270Sstevel@tonic-gate } cmd_map[] = { 4280Sstevel@tonic-gate { "update-archive", BAM_ARCHIVE, "update"}, 4290Sstevel@tonic-gate { "list-archive", BAM_ARCHIVE, "list"}, 4300Sstevel@tonic-gate { "set-menu", BAM_MENU, "set_option"}, 4310Sstevel@tonic-gate { "list-menu", BAM_MENU, "list_entry"}, 4320Sstevel@tonic-gate { "update-menu", BAM_MENU, "update_entry"}, 4330Sstevel@tonic-gate { NULL, 0, NULL} 4340Sstevel@tonic-gate }; 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate /* 4370Sstevel@tonic-gate * Commands syntax published in bootadm(1M) are parsed here 4380Sstevel@tonic-gate */ 4390Sstevel@tonic-gate static void 4400Sstevel@tonic-gate parse_args(int argc, char *argv[]) 4410Sstevel@tonic-gate { 4420Sstevel@tonic-gate struct cmd_map *cmp = cmd_map; 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate /* command conforming to the final spec */ 4450Sstevel@tonic-gate if (argc > 1 && argv[1][0] != '-') { 4460Sstevel@tonic-gate /* 4470Sstevel@tonic-gate * Map commands to internal table. 4480Sstevel@tonic-gate */ 4490Sstevel@tonic-gate while (cmp->bam_cmdname) { 4500Sstevel@tonic-gate if (strcmp(argv[1], cmp->bam_cmdname) == 0) { 4510Sstevel@tonic-gate bam_cmd = cmp->bam_cmd; 4520Sstevel@tonic-gate bam_subcmd = cmp->bam_subcmd; 4530Sstevel@tonic-gate break; 4540Sstevel@tonic-gate } 4550Sstevel@tonic-gate cmp++; 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate if (cmp->bam_cmdname == NULL) { 4580Sstevel@tonic-gate usage(); 4590Sstevel@tonic-gate bam_exit(1); 4600Sstevel@tonic-gate } 4610Sstevel@tonic-gate argc--; 4620Sstevel@tonic-gate argv++; 4630Sstevel@tonic-gate } 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate parse_args_internal(argc, argv); 4660Sstevel@tonic-gate } 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate /* 4690Sstevel@tonic-gate * A combination of public and private commands are parsed here. 4700Sstevel@tonic-gate * The internal syntax and the corresponding functionality are: 4710Sstevel@tonic-gate * -a update -- update-archive 4720Sstevel@tonic-gate * -a list -- list-archive 4730Sstevel@tonic-gate * -a update-all -- (reboot to sync all mounted OS archive) 4740Sstevel@tonic-gate * -m update_entry -- update-menu 4750Sstevel@tonic-gate * -m list_entry -- list-menu 4760Sstevel@tonic-gate * -m update_temp -- (reboot -- [boot-args]) 4770Sstevel@tonic-gate * -m delete_all_entries -- (called from install) 4780Sstevel@tonic-gate */ 4790Sstevel@tonic-gate static void 4800Sstevel@tonic-gate parse_args_internal(int argc, char *argv[]) 4810Sstevel@tonic-gate { 4820Sstevel@tonic-gate int c, error; 4830Sstevel@tonic-gate extern char *optarg; 4840Sstevel@tonic-gate extern int optind, opterr; 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate /* Suppress error message from getopt */ 4870Sstevel@tonic-gate opterr = 0; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate error = 0; 490662Sszhou while ((c = getopt(argc, argv, "a:d:fm:no:vCR:")) != -1) { 4910Sstevel@tonic-gate switch (c) { 4920Sstevel@tonic-gate case 'a': 4930Sstevel@tonic-gate if (bam_cmd) { 4940Sstevel@tonic-gate error = 1; 4950Sstevel@tonic-gate bam_error(MULT_CMDS, c); 4960Sstevel@tonic-gate } 4970Sstevel@tonic-gate bam_cmd = BAM_ARCHIVE; 4980Sstevel@tonic-gate bam_subcmd = optarg; 4990Sstevel@tonic-gate break; 5000Sstevel@tonic-gate case 'd': 5010Sstevel@tonic-gate if (bam_debug) { 5020Sstevel@tonic-gate error = 1; 5030Sstevel@tonic-gate bam_error(DUP_OPT, c); 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate bam_debug = s_strtol(optarg); 5060Sstevel@tonic-gate break; 5070Sstevel@tonic-gate case 'f': 5080Sstevel@tonic-gate if (bam_force) { 5090Sstevel@tonic-gate error = 1; 5100Sstevel@tonic-gate bam_error(DUP_OPT, c); 5110Sstevel@tonic-gate } 5120Sstevel@tonic-gate bam_force = 1; 5130Sstevel@tonic-gate break; 5140Sstevel@tonic-gate case 'm': 5150Sstevel@tonic-gate if (bam_cmd) { 5160Sstevel@tonic-gate error = 1; 5170Sstevel@tonic-gate bam_error(MULT_CMDS, c); 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate bam_cmd = BAM_MENU; 5200Sstevel@tonic-gate bam_subcmd = optarg; 5210Sstevel@tonic-gate break; 5220Sstevel@tonic-gate case 'n': 5230Sstevel@tonic-gate if (bam_check) { 5240Sstevel@tonic-gate error = 1; 5250Sstevel@tonic-gate bam_error(DUP_OPT, c); 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate bam_check = 1; 5280Sstevel@tonic-gate break; 5290Sstevel@tonic-gate case 'o': 5300Sstevel@tonic-gate if (bam_opt) { 5310Sstevel@tonic-gate error = 1; 5320Sstevel@tonic-gate bam_error(DUP_OPT, c); 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate bam_opt = optarg; 5350Sstevel@tonic-gate break; 5360Sstevel@tonic-gate case 'v': 5370Sstevel@tonic-gate if (bam_verbose) { 5380Sstevel@tonic-gate error = 1; 5390Sstevel@tonic-gate bam_error(DUP_OPT, c); 5400Sstevel@tonic-gate } 5410Sstevel@tonic-gate bam_verbose = 1; 5420Sstevel@tonic-gate break; 543662Sszhou case 'C': 544662Sszhou bam_smf_check = 1; 545662Sszhou break; 5460Sstevel@tonic-gate case 'R': 5470Sstevel@tonic-gate if (bam_root) { 5480Sstevel@tonic-gate error = 1; 5490Sstevel@tonic-gate bam_error(DUP_OPT, c); 5500Sstevel@tonic-gate break; 5510Sstevel@tonic-gate } else if (realpath(optarg, rootbuf) == NULL) { 5520Sstevel@tonic-gate error = 1; 5530Sstevel@tonic-gate bam_error(CANT_RESOLVE, optarg, 5540Sstevel@tonic-gate strerror(errno)); 5550Sstevel@tonic-gate break; 5560Sstevel@tonic-gate } 557621Svikram bam_alt_root = 1; 5580Sstevel@tonic-gate bam_root = rootbuf; 5590Sstevel@tonic-gate bam_rootlen = strlen(rootbuf); 5600Sstevel@tonic-gate break; 5610Sstevel@tonic-gate case '?': 5620Sstevel@tonic-gate error = 1; 5630Sstevel@tonic-gate bam_error(BAD_OPT, optopt); 5640Sstevel@tonic-gate break; 5650Sstevel@tonic-gate default : 5660Sstevel@tonic-gate error = 1; 5670Sstevel@tonic-gate bam_error(BAD_OPT, c); 5680Sstevel@tonic-gate break; 5690Sstevel@tonic-gate } 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate 5720Sstevel@tonic-gate /* 5730Sstevel@tonic-gate * A command option must be specfied 5740Sstevel@tonic-gate */ 5750Sstevel@tonic-gate if (!bam_cmd) { 5760Sstevel@tonic-gate if (bam_opt && strcmp(bam_opt, "all") == 0) { 5770Sstevel@tonic-gate usage(); 5780Sstevel@tonic-gate bam_exit(0); 5790Sstevel@tonic-gate } 5800Sstevel@tonic-gate bam_error(NEED_CMD); 5810Sstevel@tonic-gate error = 1; 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate if (error) { 5850Sstevel@tonic-gate usage(); 5860Sstevel@tonic-gate bam_exit(1); 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate if (optind > argc) { 5900Sstevel@tonic-gate bam_error(INT_ERROR, "parse_args"); 5910Sstevel@tonic-gate bam_exit(1); 5920Sstevel@tonic-gate } else if (optind < argc) { 5930Sstevel@tonic-gate bam_argv = &argv[optind]; 5940Sstevel@tonic-gate bam_argc = argc - optind; 5950Sstevel@tonic-gate } 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate /* 5980Sstevel@tonic-gate * -n implies verbose mode 5990Sstevel@tonic-gate */ 6000Sstevel@tonic-gate if (bam_check) 6010Sstevel@tonic-gate bam_verbose = 1; 6020Sstevel@tonic-gate } 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate static error_t 6050Sstevel@tonic-gate check_subcmd_and_options( 6060Sstevel@tonic-gate char *subcmd, 6070Sstevel@tonic-gate char *opt, 6080Sstevel@tonic-gate subcmd_defn_t *table, 6090Sstevel@tonic-gate error_t (**fp)()) 6100Sstevel@tonic-gate { 6110Sstevel@tonic-gate int i; 6120Sstevel@tonic-gate 6130Sstevel@tonic-gate if (subcmd == NULL) { 6140Sstevel@tonic-gate bam_error(NEED_SUBCMD); 6150Sstevel@tonic-gate return (BAM_ERROR); 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate 6182115Svikram if (bam_argc != 0 || bam_argv) { 6192115Svikram if (strcmp(subcmd, "set_option") != 0 || bam_argc != 1) { 6202115Svikram bam_error(TRAILING_ARGS); 6212115Svikram usage(); 6222115Svikram return (BAM_ERROR); 6232115Svikram } 6242115Svikram } 6252115Svikram 6260Sstevel@tonic-gate if (bam_root == NULL) { 6270Sstevel@tonic-gate bam_root = rootbuf; 6280Sstevel@tonic-gate bam_rootlen = 1; 6290Sstevel@tonic-gate } 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate /* verify that subcmd is valid */ 6320Sstevel@tonic-gate for (i = 0; table[i].subcmd != NULL; i++) { 6330Sstevel@tonic-gate if (strcmp(table[i].subcmd, subcmd) == 0) 6340Sstevel@tonic-gate break; 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate if (table[i].subcmd == NULL) { 6380Sstevel@tonic-gate bam_error(INVALID_SUBCMD, subcmd); 6390Sstevel@tonic-gate return (BAM_ERROR); 6400Sstevel@tonic-gate } 6410Sstevel@tonic-gate 6422115Svikram if (table[i].unpriv == 0 && geteuid() != 0) { 6432115Svikram bam_error(MUST_BE_ROOT); 6442115Svikram return (BAM_ERROR); 6452115Svikram } 6462115Svikram 6472115Svikram /* 6482115Svikram * Currently only privileged commands need a lock 6492115Svikram */ 6502115Svikram if (table[i].unpriv == 0) 6512115Svikram bam_lock(); 6522115Svikram 6530Sstevel@tonic-gate /* subcmd verifies that opt is appropriate */ 6540Sstevel@tonic-gate if (table[i].option != OPT_OPTIONAL) { 6550Sstevel@tonic-gate if ((table[i].option == OPT_REQ) ^ (opt != NULL)) { 6560Sstevel@tonic-gate if (opt) 6570Sstevel@tonic-gate bam_error(NO_OPT_REQ, subcmd); 6580Sstevel@tonic-gate else 6590Sstevel@tonic-gate bam_error(MISS_OPT, subcmd); 6600Sstevel@tonic-gate return (BAM_ERROR); 6610Sstevel@tonic-gate } 6620Sstevel@tonic-gate } 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate *fp = table[i].handler; 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate return (BAM_SUCCESS); 6670Sstevel@tonic-gate } 6680Sstevel@tonic-gate 669316Svikram 670316Svikram static char * 671621Svikram mount_grub_slice(int *mnted, char **physlice, char **logslice, char **fs_type) 672316Svikram { 673316Svikram struct extmnttab mnt; 674316Svikram struct stat sb; 675316Svikram char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32]; 676348Svikram char cmd[PATH_MAX]; 677316Svikram char *mntpt; 678316Svikram int p, l, f; 679316Svikram FILE *fp; 680316Svikram 681316Svikram assert(mnted); 682316Svikram *mnted = 0; 683316Svikram 684316Svikram /* 685621Svikram * physlice, logslice, fs_type args may be NULL 686316Svikram */ 687316Svikram if (physlice) 688316Svikram *physlice = NULL; 689621Svikram if (logslice) 690621Svikram *logslice = NULL; 691621Svikram if (fs_type) 692621Svikram *fs_type = NULL; 693316Svikram 694316Svikram if (stat(GRUB_slice, &sb) != 0) { 695316Svikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 696316Svikram return (NULL); 697316Svikram } 698316Svikram 699316Svikram fp = fopen(GRUB_slice, "r"); 700316Svikram if (fp == NULL) { 701316Svikram bam_error(OPEN_FAIL, GRUB_slice, strerror(errno)); 702316Svikram return (NULL); 703316Svikram } 704316Svikram 705316Svikram dev[0] = fstype[0] = phys[0] = '\0'; 706316Svikram p = sizeof ("PHYS_SLICE=") - 1; 707316Svikram l = sizeof ("LOG_SLICE=") - 1; 708316Svikram f = sizeof ("LOG_FSTYP=") - 1; 709316Svikram while (s_fgets(buf, sizeof (buf), fp) != NULL) { 710316Svikram if (strncmp(buf, "PHYS_SLICE=", p) == 0) { 711316Svikram (void) strlcpy(phys, buf + p, sizeof (phys)); 712316Svikram continue; 713316Svikram } 714316Svikram if (strncmp(buf, "LOG_SLICE=", l) == 0) { 715316Svikram (void) strlcpy(dev, buf + l, sizeof (dev)); 716316Svikram continue; 717316Svikram } 718316Svikram if (strncmp(buf, "LOG_FSTYP=", f) == 0) { 719316Svikram (void) strlcpy(fstype, buf + f, sizeof (fstype)); 720316Svikram continue; 721316Svikram } 722316Svikram } 723316Svikram (void) fclose(fp); 724316Svikram 725316Svikram if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') { 726316Svikram bam_error(BAD_SLICE_FILE, GRUB_slice); 727316Svikram return (NULL); 728316Svikram } 729316Svikram 730316Svikram if (physlice) { 731316Svikram *physlice = s_strdup(phys); 732316Svikram } 733621Svikram if (logslice) { 734621Svikram *logslice = s_strdup(dev); 735621Svikram } 736621Svikram if (fs_type) { 737621Svikram *fs_type = s_strdup(fstype); 738621Svikram } 739316Svikram 740316Svikram /* 741316Svikram * Check if the slice is already mounted 742316Svikram */ 743316Svikram fp = fopen(MNTTAB, "r"); 744316Svikram if (fp == NULL) { 745316Svikram bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 746621Svikram goto error; 747316Svikram } 748316Svikram 749316Svikram resetmnttab(fp); 750316Svikram 751316Svikram mntpt = NULL; 752316Svikram while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 753316Svikram if (strcmp(mnt.mnt_special, dev) == 0) { 754316Svikram mntpt = s_strdup(mnt.mnt_mountp); 755316Svikram break; 756316Svikram } 757316Svikram } 758316Svikram 759316Svikram (void) fclose(fp); 760316Svikram 761316Svikram if (mntpt) { 762316Svikram return (mntpt); 763316Svikram } 764316Svikram 765316Svikram 766316Svikram /* 767316Svikram * GRUB slice is not mounted, we need to mount it now. 768316Svikram * First create the mountpoint 769316Svikram */ 770316Svikram mntpt = s_calloc(1, PATH_MAX); 771316Svikram (void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid()); 772316Svikram if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) { 773316Svikram bam_error(MKDIR_FAILED, mntpt, strerror(errno)); 774316Svikram free(mntpt); 775621Svikram goto error; 776316Svikram } 777316Svikram 778348Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s", 779348Svikram fstype, dev, mntpt); 780348Svikram 781348Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 782621Svikram bam_error(MOUNT_FAILED, dev, fstype); 783316Svikram if (rmdir(mntpt) != 0) { 784316Svikram bam_error(RMDIR_FAILED, mntpt, strerror(errno)); 785316Svikram } 786316Svikram free(mntpt); 787621Svikram goto error; 788316Svikram } 789316Svikram 790316Svikram *mnted = 1; 791316Svikram return (mntpt); 792621Svikram 793621Svikram error: 794621Svikram if (physlice) { 795621Svikram free(*physlice); 796621Svikram *physlice = NULL; 797621Svikram } 798621Svikram if (logslice) { 799621Svikram free(*logslice); 800621Svikram *logslice = NULL; 801621Svikram } 802621Svikram if (fs_type) { 803621Svikram free(*fs_type); 804621Svikram *fs_type = NULL; 805621Svikram } 806621Svikram return (NULL); 807316Svikram } 808316Svikram 809316Svikram static void 810621Svikram umount_grub_slice( 811621Svikram int mnted, 812621Svikram char *mntpt, 813621Svikram char *physlice, 814621Svikram char *logslice, 815621Svikram char *fs_type) 816316Svikram { 817348Svikram char cmd[PATH_MAX]; 818348Svikram 819316Svikram /* 820316Svikram * If we have not dealt with GRUB slice 821316Svikram * we have nothing to do - just return. 822316Svikram */ 823316Svikram if (mntpt == NULL) 824316Svikram return; 825316Svikram 826316Svikram 827316Svikram /* 828316Svikram * If we mounted the filesystem earlier in mount_grub_slice() 829316Svikram * unmount it now. 830316Svikram */ 831316Svikram if (mnted) { 832348Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/umount %s", 833348Svikram mntpt); 834348Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 835348Svikram bam_error(UMOUNT_FAILED, mntpt); 836316Svikram } 837316Svikram if (rmdir(mntpt) != 0) { 838316Svikram bam_error(RMDIR_FAILED, mntpt, strerror(errno)); 839316Svikram } 840316Svikram } 841621Svikram 842316Svikram if (physlice) 843316Svikram free(physlice); 844621Svikram if (logslice) 845621Svikram free(logslice); 846621Svikram if (fs_type) 847621Svikram free(fs_type); 848621Svikram 849316Svikram free(mntpt); 850316Svikram } 851316Svikram 852621Svikram static char * 853621Svikram use_stubboot(void) 854621Svikram { 855621Svikram int mnted; 856621Svikram struct stat sb; 857621Svikram struct extmnttab mnt; 858621Svikram FILE *fp; 859621Svikram char cmd[PATH_MAX]; 860621Svikram 861621Svikram if (stat(STUBBOOT, &sb) != 0) { 862621Svikram bam_error(STUBBOOT_DIR_NOT_FOUND); 863621Svikram return (NULL); 864621Svikram } 865621Svikram 866621Svikram /* 867621Svikram * Check if stubboot is mounted. If not, mount it 868621Svikram */ 869621Svikram fp = fopen(MNTTAB, "r"); 870621Svikram if (fp == NULL) { 871621Svikram bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 872621Svikram return (NULL); 873621Svikram } 874621Svikram 875621Svikram resetmnttab(fp); 876621Svikram 877621Svikram mnted = 0; 878621Svikram while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 879621Svikram if (strcmp(mnt.mnt_mountp, STUBBOOT) == 0) { 880621Svikram mnted = 1; 881621Svikram break; 882621Svikram } 883621Svikram } 884621Svikram 885621Svikram (void) fclose(fp); 886621Svikram 887621Svikram if (mnted) 888621Svikram return (STUBBOOT); 889621Svikram 890621Svikram /* 891621Svikram * Stubboot is not mounted, mount it now. 892621Svikram * It should exist in /etc/vfstab 893621Svikram */ 894621Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/mount %s", 895621Svikram STUBBOOT); 896621Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 897621Svikram bam_error(MOUNT_MNTPT_FAILED, STUBBOOT); 898621Svikram return (NULL); 899621Svikram } 900621Svikram 901621Svikram return (STUBBOOT); 902621Svikram } 903621Svikram 904621Svikram static void 905621Svikram disp_active_menu_locn(char *menu_path, char *logslice, char *fstype, int mnted) 906621Svikram { 907621Svikram /* 908621Svikram * Check if we did a temp mount of an unmounted device. 909621Svikram * If yes, print the block device and fstype for that device 910621Svikram * else it is already mounted, so we print the path to the GRUB menu. 911621Svikram */ 912621Svikram if (mnted) { 913621Svikram bam_print(GRUB_MENU_DEVICE, logslice); 914621Svikram bam_print(GRUB_MENU_FSTYPE, fstype); 915621Svikram } else { 916621Svikram bam_print(GRUB_MENU_PATH, menu_path); 917621Svikram } 918621Svikram } 919621Svikram 920621Svikram /* 921621Svikram * NOTE: A single "/" is also considered a trailing slash and will 922621Svikram * be deleted. 923621Svikram */ 924621Svikram static void 925621Svikram elide_trailing_slash(const char *src, char *dst, size_t dstsize) 926621Svikram { 927621Svikram size_t dstlen; 928621Svikram 929621Svikram assert(src); 930621Svikram assert(dst); 931621Svikram 932621Svikram (void) strlcpy(dst, src, dstsize); 933621Svikram 934621Svikram dstlen = strlen(dst); 935621Svikram if (dst[dstlen - 1] == '/') { 936621Svikram dst[dstlen - 1] = '\0'; 937621Svikram } 938621Svikram } 939621Svikram 9400Sstevel@tonic-gate static error_t 9410Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[]) 9420Sstevel@tonic-gate { 9430Sstevel@tonic-gate error_t ret; 9440Sstevel@tonic-gate char menu_path[PATH_MAX]; 9450Sstevel@tonic-gate menu_t *menu; 946621Svikram char *mntpt, *menu_root, *logslice, *fstype; 947316Svikram struct stat sb; 948316Svikram int mnted; /* set if we did a mount */ 9490Sstevel@tonic-gate error_t (*f)(menu_t *mp, char *menu_path, char *opt); 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate /* 9520Sstevel@tonic-gate * Check arguments 9530Sstevel@tonic-gate */ 9540Sstevel@tonic-gate ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f); 9550Sstevel@tonic-gate if (ret == BAM_ERROR) { 9560Sstevel@tonic-gate return (BAM_ERROR); 9570Sstevel@tonic-gate } 9580Sstevel@tonic-gate 959316Svikram mntpt = NULL; 960316Svikram mnted = 0; 961621Svikram logslice = fstype = NULL; 962621Svikram 963621Svikram /* 964621Svikram * If the user provides an alternate root, we 965621Svikram * assume they know what they are doing and we 966621Svikram * use it. Else we check if there is an 967621Svikram * alternate location (other than /boot/grub) 968621Svikram * for the GRUB menu 969621Svikram */ 970621Svikram if (bam_alt_root) { 971621Svikram menu_root = bam_root; 972621Svikram } else if (stat(GRUB_slice, &sb) == 0) { 973621Svikram mntpt = mount_grub_slice(&mnted, NULL, &logslice, &fstype); 974316Svikram menu_root = mntpt; 975621Svikram } else if (stat(STUBBOOT, &sb) == 0) { 976621Svikram menu_root = use_stubboot(); 977316Svikram } else { 978316Svikram menu_root = bam_root; 979316Svikram } 980316Svikram 981621Svikram if (menu_root == NULL) { 982621Svikram bam_error(CANNOT_LOCATE_GRUB_MENU); 983621Svikram return (BAM_ERROR); 984621Svikram } 985621Svikram 986621Svikram elide_trailing_slash(menu_root, menu_path, sizeof (menu_path)); 987621Svikram (void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path)); 988621Svikram 989621Svikram /* 990621Svikram * If listing the menu, display the active menu 991621Svikram * location 992621Svikram */ 993621Svikram if (strcmp(subcmd, "list_entry") == 0) { 994621Svikram disp_active_menu_locn(menu_path, logslice, fstype, mnted); 995621Svikram } 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate menu = menu_read(menu_path); 9980Sstevel@tonic-gate assert(menu); 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate /* 10010Sstevel@tonic-gate * Special handling for setting timeout and default 10020Sstevel@tonic-gate */ 10030Sstevel@tonic-gate if (strcmp(subcmd, "set_option") == 0) { 10040Sstevel@tonic-gate if (largc != 1 || largv[0] == NULL) { 10050Sstevel@tonic-gate usage(); 1006621Svikram menu_free(menu); 1007621Svikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 10080Sstevel@tonic-gate return (BAM_ERROR); 10090Sstevel@tonic-gate } 10100Sstevel@tonic-gate opt = largv[0]; 10110Sstevel@tonic-gate } else if (largc != 0) { 10120Sstevel@tonic-gate usage(); 1013621Svikram menu_free(menu); 1014621Svikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 10150Sstevel@tonic-gate return (BAM_ERROR); 10160Sstevel@tonic-gate } 10170Sstevel@tonic-gate 10180Sstevel@tonic-gate /* 10190Sstevel@tonic-gate * Once the sub-cmd handler has run 10200Sstevel@tonic-gate * only the line field is guaranteed to have valid values 10210Sstevel@tonic-gate */ 10220Sstevel@tonic-gate if (strcmp(subcmd, "update_entry") == 0) 10230Sstevel@tonic-gate ret = f(menu, bam_root, opt); 10240Sstevel@tonic-gate else 10250Sstevel@tonic-gate ret = f(menu, menu_path, opt); 10260Sstevel@tonic-gate if (ret == BAM_WRITE) { 1027316Svikram ret = menu_write(menu_root, menu); 10280Sstevel@tonic-gate } 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate menu_free(menu); 10310Sstevel@tonic-gate 1032621Svikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 1033316Svikram 10340Sstevel@tonic-gate return (ret); 10350Sstevel@tonic-gate } 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate 10380Sstevel@tonic-gate static error_t 10390Sstevel@tonic-gate bam_archive( 10400Sstevel@tonic-gate char *subcmd, 10410Sstevel@tonic-gate char *opt) 10420Sstevel@tonic-gate { 10430Sstevel@tonic-gate error_t ret; 10440Sstevel@tonic-gate error_t (*f)(char *root, char *opt); 10450Sstevel@tonic-gate 10460Sstevel@tonic-gate /* 1047662Sszhou * Add trailing / for archive subcommands 1048662Sszhou */ 1049662Sszhou if (rootbuf[strlen(rootbuf) - 1] != '/') 1050662Sszhou (void) strcat(rootbuf, "/"); 1051662Sszhou bam_rootlen = strlen(rootbuf); 1052662Sszhou 1053662Sszhou /* 10540Sstevel@tonic-gate * Check arguments 10550Sstevel@tonic-gate */ 10560Sstevel@tonic-gate ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f); 10570Sstevel@tonic-gate if (ret != BAM_SUCCESS) { 10580Sstevel@tonic-gate return (BAM_ERROR); 10590Sstevel@tonic-gate } 10600Sstevel@tonic-gate 10610Sstevel@tonic-gate #if defined(__sparc) 10620Sstevel@tonic-gate /* 10630Sstevel@tonic-gate * A NOP if called on SPARC during reboot 10640Sstevel@tonic-gate */ 10650Sstevel@tonic-gate if (strcmp(subcmd, "update_all") == 0) 10660Sstevel@tonic-gate return (BAM_SUCCESS); 10670Sstevel@tonic-gate else if (strcmp(subcmd, "update") != 0) 10680Sstevel@tonic-gate sparc_abort(); 10690Sstevel@tonic-gate #endif 10700Sstevel@tonic-gate 10710Sstevel@tonic-gate /* 10720Sstevel@tonic-gate * Check archive not supported with update_all 10730Sstevel@tonic-gate * since it is awkward to display out-of-sync 10740Sstevel@tonic-gate * information for each BE. 10750Sstevel@tonic-gate */ 10760Sstevel@tonic-gate if (bam_check && strcmp(subcmd, "update_all") == 0) { 10770Sstevel@tonic-gate bam_error(CHECK_NOT_SUPPORTED, subcmd); 10780Sstevel@tonic-gate return (BAM_ERROR); 10790Sstevel@tonic-gate } 10800Sstevel@tonic-gate 1081316Svikram if (strcmp(subcmd, "update_all") == 0) 1082316Svikram bam_update_all = 1; 1083316Svikram 1084316Svikram ret = f(bam_root, opt); 1085316Svikram 1086316Svikram bam_update_all = 0; 1087316Svikram 1088316Svikram return (ret); 10890Sstevel@tonic-gate } 10900Sstevel@tonic-gate 10910Sstevel@tonic-gate /*PRINTFLIKE1*/ 10920Sstevel@tonic-gate static void 10930Sstevel@tonic-gate bam_error(char *format, ...) 10940Sstevel@tonic-gate { 10950Sstevel@tonic-gate va_list ap; 10960Sstevel@tonic-gate 10970Sstevel@tonic-gate va_start(ap, format); 10980Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", prog); 10990Sstevel@tonic-gate (void) vfprintf(stderr, format, ap); 11000Sstevel@tonic-gate va_end(ap); 11010Sstevel@tonic-gate } 11020Sstevel@tonic-gate 11030Sstevel@tonic-gate /*PRINTFLIKE1*/ 11040Sstevel@tonic-gate static void 11050Sstevel@tonic-gate bam_print(char *format, ...) 11060Sstevel@tonic-gate { 11070Sstevel@tonic-gate va_list ap; 11080Sstevel@tonic-gate 11090Sstevel@tonic-gate va_start(ap, format); 11100Sstevel@tonic-gate (void) vfprintf(stdout, format, ap); 11110Sstevel@tonic-gate va_end(ap); 11120Sstevel@tonic-gate } 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate static void 11150Sstevel@tonic-gate bam_exit(int excode) 11160Sstevel@tonic-gate { 11170Sstevel@tonic-gate bam_unlock(); 11180Sstevel@tonic-gate exit(excode); 11190Sstevel@tonic-gate } 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate static void 11220Sstevel@tonic-gate bam_lock(void) 11230Sstevel@tonic-gate { 11240Sstevel@tonic-gate struct flock lock; 11250Sstevel@tonic-gate pid_t pid; 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS); 11280Sstevel@tonic-gate if (bam_lock_fd < 0) { 11290Sstevel@tonic-gate /* 11300Sstevel@tonic-gate * We may be invoked early in boot for archive verification. 11310Sstevel@tonic-gate * In this case, root is readonly and /var/run may not exist. 11320Sstevel@tonic-gate * Proceed without the lock 11330Sstevel@tonic-gate */ 11340Sstevel@tonic-gate if (errno == EROFS || errno == ENOENT) { 11350Sstevel@tonic-gate bam_root_readonly = 1; 11360Sstevel@tonic-gate return; 11370Sstevel@tonic-gate } 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno)); 11400Sstevel@tonic-gate bam_exit(1); 11410Sstevel@tonic-gate } 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate lock.l_type = F_WRLCK; 11440Sstevel@tonic-gate lock.l_whence = SEEK_SET; 11450Sstevel@tonic-gate lock.l_start = 0; 11460Sstevel@tonic-gate lock.l_len = 0; 11470Sstevel@tonic-gate 11480Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) { 11490Sstevel@tonic-gate if (errno != EACCES && errno != EAGAIN) { 11500Sstevel@tonic-gate bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11510Sstevel@tonic-gate (void) close(bam_lock_fd); 11520Sstevel@tonic-gate bam_lock_fd = -1; 11530Sstevel@tonic-gate bam_exit(1); 11540Sstevel@tonic-gate } 11550Sstevel@tonic-gate pid = 0; 11560Sstevel@tonic-gate (void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0); 11570Sstevel@tonic-gate bam_print(FILE_LOCKED, pid); 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate lock.l_type = F_WRLCK; 11600Sstevel@tonic-gate lock.l_whence = SEEK_SET; 11610Sstevel@tonic-gate lock.l_start = 0; 11620Sstevel@tonic-gate lock.l_len = 0; 11630Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) { 11640Sstevel@tonic-gate bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11650Sstevel@tonic-gate (void) close(bam_lock_fd); 11660Sstevel@tonic-gate bam_lock_fd = -1; 11670Sstevel@tonic-gate bam_exit(1); 11680Sstevel@tonic-gate } 11690Sstevel@tonic-gate } 11700Sstevel@tonic-gate 11710Sstevel@tonic-gate /* We own the lock now */ 11720Sstevel@tonic-gate pid = getpid(); 11730Sstevel@tonic-gate (void) write(bam_lock_fd, &pid, sizeof (pid)); 11740Sstevel@tonic-gate } 11750Sstevel@tonic-gate 11760Sstevel@tonic-gate static void 11770Sstevel@tonic-gate bam_unlock(void) 11780Sstevel@tonic-gate { 11790Sstevel@tonic-gate struct flock unlock; 11800Sstevel@tonic-gate 11810Sstevel@tonic-gate /* 11820Sstevel@tonic-gate * NOP if we don't hold the lock 11830Sstevel@tonic-gate */ 11840Sstevel@tonic-gate if (bam_lock_fd < 0) { 11850Sstevel@tonic-gate return; 11860Sstevel@tonic-gate } 11870Sstevel@tonic-gate 11880Sstevel@tonic-gate unlock.l_type = F_UNLCK; 11890Sstevel@tonic-gate unlock.l_whence = SEEK_SET; 11900Sstevel@tonic-gate unlock.l_start = 0; 11910Sstevel@tonic-gate unlock.l_len = 0; 11920Sstevel@tonic-gate 11930Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) { 11940Sstevel@tonic-gate bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11950Sstevel@tonic-gate } 11960Sstevel@tonic-gate 11970Sstevel@tonic-gate if (close(bam_lock_fd) == -1) { 11980Sstevel@tonic-gate bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno)); 11990Sstevel@tonic-gate } 12000Sstevel@tonic-gate bam_lock_fd = -1; 12010Sstevel@tonic-gate } 12020Sstevel@tonic-gate 12030Sstevel@tonic-gate static error_t 12040Sstevel@tonic-gate list_archive(char *root, char *opt) 12050Sstevel@tonic-gate { 12060Sstevel@tonic-gate filelist_t flist; 12070Sstevel@tonic-gate filelist_t *flistp = &flist; 12080Sstevel@tonic-gate line_t *lp; 12090Sstevel@tonic-gate 12100Sstevel@tonic-gate assert(root); 12110Sstevel@tonic-gate assert(opt == NULL); 12120Sstevel@tonic-gate 12130Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 12140Sstevel@tonic-gate if (read_list(root, flistp) != BAM_SUCCESS) { 12150Sstevel@tonic-gate return (BAM_ERROR); 12160Sstevel@tonic-gate } 12170Sstevel@tonic-gate assert(flistp->head && flistp->tail); 12180Sstevel@tonic-gate 12190Sstevel@tonic-gate for (lp = flistp->head; lp; lp = lp->next) { 12200Sstevel@tonic-gate bam_print(PRINT, lp->line); 12210Sstevel@tonic-gate } 12220Sstevel@tonic-gate 12230Sstevel@tonic-gate filelist_free(flistp); 12240Sstevel@tonic-gate 12250Sstevel@tonic-gate return (BAM_SUCCESS); 12260Sstevel@tonic-gate } 12270Sstevel@tonic-gate 12280Sstevel@tonic-gate /* 12290Sstevel@tonic-gate * This routine writes a list of lines to a file. 12300Sstevel@tonic-gate * The list is *not* freed 12310Sstevel@tonic-gate */ 12320Sstevel@tonic-gate static error_t 12330Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start) 12340Sstevel@tonic-gate { 12350Sstevel@tonic-gate char tmpfile[PATH_MAX]; 12360Sstevel@tonic-gate char path[PATH_MAX]; 12370Sstevel@tonic-gate FILE *fp; 12380Sstevel@tonic-gate int ret; 12390Sstevel@tonic-gate struct stat sb; 12400Sstevel@tonic-gate mode_t mode; 12410Sstevel@tonic-gate uid_t root_uid; 12420Sstevel@tonic-gate gid_t sys_gid; 12430Sstevel@tonic-gate struct passwd *pw; 12440Sstevel@tonic-gate struct group *gp; 12450Sstevel@tonic-gate 12460Sstevel@tonic-gate 12470Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, final); 12480Sstevel@tonic-gate 12490Sstevel@tonic-gate if (start == NULL) { 12500Sstevel@tonic-gate if (stat(path, &sb) != -1) { 12510Sstevel@tonic-gate bam_print(UNLINK_EMPTY, path); 12520Sstevel@tonic-gate if (unlink(path) != 0) { 12530Sstevel@tonic-gate bam_error(UNLINK_FAIL, path, strerror(errno)); 12540Sstevel@tonic-gate return (BAM_ERROR); 12550Sstevel@tonic-gate } else { 12560Sstevel@tonic-gate return (BAM_SUCCESS); 12570Sstevel@tonic-gate } 12580Sstevel@tonic-gate } 12590Sstevel@tonic-gate } 12600Sstevel@tonic-gate 12610Sstevel@tonic-gate /* 12620Sstevel@tonic-gate * Preserve attributes of existing file if possible, 12630Sstevel@tonic-gate * otherwise ask the system for uid/gid of root/sys. 12640Sstevel@tonic-gate * If all fails, fall back on hard-coded defaults. 12650Sstevel@tonic-gate */ 12660Sstevel@tonic-gate if (stat(path, &sb) != -1) { 12670Sstevel@tonic-gate mode = sb.st_mode; 12680Sstevel@tonic-gate root_uid = sb.st_uid; 12690Sstevel@tonic-gate sys_gid = sb.st_gid; 12700Sstevel@tonic-gate } else { 12710Sstevel@tonic-gate mode = DEFAULT_DEV_MODE; 12720Sstevel@tonic-gate if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) { 12730Sstevel@tonic-gate root_uid = pw->pw_uid; 12740Sstevel@tonic-gate } else { 12750Sstevel@tonic-gate if (bam_verbose) 12760Sstevel@tonic-gate bam_error(CANT_FIND_USER, 12770Sstevel@tonic-gate DEFAULT_DEV_USER, DEFAULT_DEV_UID); 12780Sstevel@tonic-gate root_uid = (uid_t)DEFAULT_DEV_UID; 12790Sstevel@tonic-gate } 12800Sstevel@tonic-gate if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) { 12810Sstevel@tonic-gate sys_gid = gp->gr_gid; 12820Sstevel@tonic-gate } else { 12830Sstevel@tonic-gate if (bam_verbose) 12840Sstevel@tonic-gate bam_error(CANT_FIND_GROUP, 12850Sstevel@tonic-gate DEFAULT_DEV_GROUP, DEFAULT_DEV_GID); 12860Sstevel@tonic-gate sys_gid = (gid_t)DEFAULT_DEV_GID; 12870Sstevel@tonic-gate } 12880Sstevel@tonic-gate } 12890Sstevel@tonic-gate 12900Sstevel@tonic-gate (void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp); 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate /* Truncate tmpfile first */ 12930Sstevel@tonic-gate fp = fopen(tmpfile, "w"); 12940Sstevel@tonic-gate if (fp == NULL) { 12950Sstevel@tonic-gate bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 12960Sstevel@tonic-gate return (BAM_ERROR); 12970Sstevel@tonic-gate } 12980Sstevel@tonic-gate ret = fclose(fp); 12990Sstevel@tonic-gate if (ret == EOF) { 13000Sstevel@tonic-gate bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 13010Sstevel@tonic-gate return (BAM_ERROR); 13020Sstevel@tonic-gate } 13030Sstevel@tonic-gate 13040Sstevel@tonic-gate /* Now open it in append mode */ 13050Sstevel@tonic-gate fp = fopen(tmpfile, "a"); 13060Sstevel@tonic-gate if (fp == NULL) { 13070Sstevel@tonic-gate bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 13080Sstevel@tonic-gate return (BAM_ERROR); 13090Sstevel@tonic-gate } 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate for (; start; start = start->next) { 13120Sstevel@tonic-gate ret = s_fputs(start->line, fp); 13130Sstevel@tonic-gate if (ret == EOF) { 13140Sstevel@tonic-gate bam_error(WRITE_FAIL, tmpfile, strerror(errno)); 13150Sstevel@tonic-gate (void) fclose(fp); 13160Sstevel@tonic-gate return (BAM_ERROR); 13170Sstevel@tonic-gate } 13180Sstevel@tonic-gate } 13190Sstevel@tonic-gate 13200Sstevel@tonic-gate ret = fclose(fp); 13210Sstevel@tonic-gate if (ret == EOF) { 13220Sstevel@tonic-gate bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 13230Sstevel@tonic-gate return (BAM_ERROR); 13240Sstevel@tonic-gate } 13250Sstevel@tonic-gate 13260Sstevel@tonic-gate /* 1327271Sjg * Set up desired attributes. Ignore failures on filesystems 1328271Sjg * not supporting these operations - pcfs reports unsupported 1329271Sjg * operations as EINVAL. 13300Sstevel@tonic-gate */ 13310Sstevel@tonic-gate ret = chmod(tmpfile, mode); 1332271Sjg if (ret == -1 && 1333271Sjg errno != EINVAL && errno != ENOTSUP) { 13340Sstevel@tonic-gate bam_error(CHMOD_FAIL, tmpfile, strerror(errno)); 13350Sstevel@tonic-gate return (BAM_ERROR); 13360Sstevel@tonic-gate } 13370Sstevel@tonic-gate 13380Sstevel@tonic-gate ret = chown(tmpfile, root_uid, sys_gid); 1339271Sjg if (ret == -1 && 1340271Sjg errno != EINVAL && errno != ENOTSUP) { 13410Sstevel@tonic-gate bam_error(CHOWN_FAIL, tmpfile, strerror(errno)); 13420Sstevel@tonic-gate return (BAM_ERROR); 13430Sstevel@tonic-gate } 13440Sstevel@tonic-gate 13450Sstevel@tonic-gate 13460Sstevel@tonic-gate /* 13470Sstevel@tonic-gate * Do an atomic rename 13480Sstevel@tonic-gate */ 13490Sstevel@tonic-gate ret = rename(tmpfile, path); 13500Sstevel@tonic-gate if (ret != 0) { 13510Sstevel@tonic-gate bam_error(RENAME_FAIL, path, strerror(errno)); 13520Sstevel@tonic-gate return (BAM_ERROR); 13530Sstevel@tonic-gate } 13540Sstevel@tonic-gate 13550Sstevel@tonic-gate return (BAM_SUCCESS); 13560Sstevel@tonic-gate } 13570Sstevel@tonic-gate 13580Sstevel@tonic-gate /* 13590Sstevel@tonic-gate * This function should always return 0 - since we want 13600Sstevel@tonic-gate * to create stat data for *all* files in the list. 13610Sstevel@tonic-gate */ 13620Sstevel@tonic-gate /*ARGSUSED*/ 13630Sstevel@tonic-gate static int 13640Sstevel@tonic-gate cmpstat( 13650Sstevel@tonic-gate const char *file, 13660Sstevel@tonic-gate const struct stat *stat, 13670Sstevel@tonic-gate int flags, 13680Sstevel@tonic-gate struct FTW *ftw) 13690Sstevel@tonic-gate { 13700Sstevel@tonic-gate uint_t sz; 13710Sstevel@tonic-gate uint64_t *value; 13720Sstevel@tonic-gate uint64_t filestat[2]; 13730Sstevel@tonic-gate int error; 13740Sstevel@tonic-gate 13752334Ssetje struct safefile *safefilep; 13762334Ssetje FILE *fp; 13772334Ssetje 13780Sstevel@tonic-gate /* 13790Sstevel@tonic-gate * We only want regular files 13800Sstevel@tonic-gate */ 13810Sstevel@tonic-gate if (!S_ISREG(stat->st_mode)) 13820Sstevel@tonic-gate return (0); 13830Sstevel@tonic-gate 13840Sstevel@tonic-gate /* 13850Sstevel@tonic-gate * new_nvlp may be NULL if there were errors earlier 13860Sstevel@tonic-gate * but this is not fatal to update determination. 13870Sstevel@tonic-gate */ 13880Sstevel@tonic-gate if (walk_arg.new_nvlp) { 13890Sstevel@tonic-gate filestat[0] = stat->st_size; 13900Sstevel@tonic-gate filestat[1] = stat->st_mtime; 13910Sstevel@tonic-gate error = nvlist_add_uint64_array(walk_arg.new_nvlp, 13920Sstevel@tonic-gate file + bam_rootlen, filestat, 2); 13930Sstevel@tonic-gate if (error) 13940Sstevel@tonic-gate bam_error(NVADD_FAIL, file, strerror(error)); 13950Sstevel@tonic-gate } 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate /* 13980Sstevel@tonic-gate * The remaining steps are only required if we haven't made a 13990Sstevel@tonic-gate * decision about update or if we are checking (-n) 14000Sstevel@tonic-gate */ 14010Sstevel@tonic-gate if (walk_arg.need_update && !bam_check) 14020Sstevel@tonic-gate return (0); 14030Sstevel@tonic-gate 14040Sstevel@tonic-gate /* 14052334Ssetje * If we are invoked as part of system/filesyste/boot-archive, then 14062334Ssetje * there are a number of things we should not worry about 14070Sstevel@tonic-gate */ 14082334Ssetje if (bam_smf_check) { 14092334Ssetje /* ignore amd64 modules unless we are booted amd64. */ 14102334Ssetje if (!is_amd64() && strstr(file, "/amd64/") != 0) 14112334Ssetje return (0); 14122334Ssetje 14132334Ssetje /* read in list of safe files */ 14142334Ssetje if (safefiles == NULL) 14152334Ssetje if (fp = fopen("/boot/solaris/filelist.safe", "r")) { 14162334Ssetje safefiles = s_calloc(1, 14172334Ssetje sizeof (struct safefile)); 14182334Ssetje safefilep = safefiles; 14192334Ssetje safefilep->name = s_calloc(1, MAXPATHLEN + 14202334Ssetje MAXNAMELEN); 14212334Ssetje safefilep->next = NULL; 14222334Ssetje while (s_fgets(safefilep->name, MAXPATHLEN + 14232334Ssetje MAXNAMELEN, fp) != NULL) { 14242334Ssetje safefilep->next = s_calloc(1, 14252334Ssetje sizeof (struct safefile)); 14262334Ssetje safefilep = safefilep->next; 14272334Ssetje safefilep->name = s_calloc(1, 14282334Ssetje MAXPATHLEN + MAXNAMELEN); 14292334Ssetje safefilep->next = NULL; 14302334Ssetje } 14312334Ssetje (void) fclose(fp); 14322334Ssetje } 14332334Ssetje 14342334Ssetje safefilep = safefiles; 14352334Ssetje while (safefilep->next != NULL) 14362334Ssetje if (strcmp(file, safefilep->name) != 0) { 14372334Ssetje fp = fopen(NEED_UPDATE_FILE, "w"); 14382334Ssetje if (fclose(fp) != 0) 14392334Ssetje bam_error(CLOSE_FAIL, NEED_UPDATE_FILE, 14402334Ssetje strerror(errno)); 14412334Ssetje return (0); 14422334Ssetje } 14432334Ssetje } 14440Sstevel@tonic-gate 14450Sstevel@tonic-gate /* 14460Sstevel@tonic-gate * We need an update if file doesn't exist in old archive 14470Sstevel@tonic-gate */ 14480Sstevel@tonic-gate if (walk_arg.old_nvlp == NULL || 14490Sstevel@tonic-gate nvlist_lookup_uint64_array(walk_arg.old_nvlp, 14500Sstevel@tonic-gate file + bam_rootlen, &value, &sz) != 0) { 14510Sstevel@tonic-gate if (bam_smf_check) /* ignore new during smf check */ 14520Sstevel@tonic-gate return (0); 14530Sstevel@tonic-gate walk_arg.need_update = 1; 14540Sstevel@tonic-gate if (bam_verbose) 14550Sstevel@tonic-gate bam_print(PARSEABLE_NEW_FILE, file); 14560Sstevel@tonic-gate return (0); 14570Sstevel@tonic-gate } 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate /* 14600Sstevel@tonic-gate * File exists in old archive. Check if file has changed 14610Sstevel@tonic-gate */ 14620Sstevel@tonic-gate assert(sz == 2); 14630Sstevel@tonic-gate bcopy(value, filestat, sizeof (filestat)); 14640Sstevel@tonic-gate 14650Sstevel@tonic-gate if (filestat[0] != stat->st_size || 14660Sstevel@tonic-gate filestat[1] != stat->st_mtime) { 14670Sstevel@tonic-gate walk_arg.need_update = 1; 14680Sstevel@tonic-gate if (bam_verbose) 14690Sstevel@tonic-gate if (bam_smf_check) 14700Sstevel@tonic-gate bam_print(" %s\n", file); 14710Sstevel@tonic-gate else 14720Sstevel@tonic-gate bam_print(PARSEABLE_OUT_DATE, file); 14730Sstevel@tonic-gate } 14740Sstevel@tonic-gate 14750Sstevel@tonic-gate return (0); 14760Sstevel@tonic-gate } 14770Sstevel@tonic-gate 14780Sstevel@tonic-gate /* 14790Sstevel@tonic-gate * Check flags and presence of required files. 14800Sstevel@tonic-gate * The force flag and/or absence of files should 14810Sstevel@tonic-gate * trigger an update. 14820Sstevel@tonic-gate * Suppress stdout output if check (-n) option is set 14830Sstevel@tonic-gate * (as -n should only produce parseable output.) 14840Sstevel@tonic-gate */ 14850Sstevel@tonic-gate static void 14860Sstevel@tonic-gate check_flags_and_files(char *root) 14870Sstevel@tonic-gate { 14880Sstevel@tonic-gate char path[PATH_MAX]; 14890Sstevel@tonic-gate struct stat sb; 14900Sstevel@tonic-gate 14910Sstevel@tonic-gate /* 14920Sstevel@tonic-gate * if force, create archive unconditionally 14930Sstevel@tonic-gate */ 14940Sstevel@tonic-gate if (bam_force) { 14950Sstevel@tonic-gate walk_arg.need_update = 1; 14960Sstevel@tonic-gate if (bam_verbose && !bam_check) 14970Sstevel@tonic-gate bam_print(UPDATE_FORCE); 14980Sstevel@tonic-gate return; 14990Sstevel@tonic-gate } 15000Sstevel@tonic-gate 15010Sstevel@tonic-gate /* 15020Sstevel@tonic-gate * If archive is missing, create archive 15030Sstevel@tonic-gate */ 15040Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE); 15050Sstevel@tonic-gate if (stat(path, &sb) != 0) { 15060Sstevel@tonic-gate if (bam_verbose && !bam_check) 15070Sstevel@tonic-gate bam_print(UPDATE_ARCH_MISS, path); 15080Sstevel@tonic-gate walk_arg.need_update = 1; 15090Sstevel@tonic-gate return; 15100Sstevel@tonic-gate } 15110Sstevel@tonic-gate } 15120Sstevel@tonic-gate 15130Sstevel@tonic-gate static error_t 15140Sstevel@tonic-gate read_one_list(char *root, filelist_t *flistp, char *filelist) 15150Sstevel@tonic-gate { 15160Sstevel@tonic-gate char path[PATH_MAX]; 15170Sstevel@tonic-gate FILE *fp; 15180Sstevel@tonic-gate char buf[BAM_MAXLINE]; 15190Sstevel@tonic-gate 15200Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, filelist); 15210Sstevel@tonic-gate 15220Sstevel@tonic-gate fp = fopen(path, "r"); 15230Sstevel@tonic-gate if (fp == NULL) { 15240Sstevel@tonic-gate if (bam_debug) 15250Sstevel@tonic-gate bam_error(FLIST_FAIL, path, strerror(errno)); 15260Sstevel@tonic-gate return (BAM_ERROR); 15270Sstevel@tonic-gate } 15280Sstevel@tonic-gate while (s_fgets(buf, sizeof (buf), fp) != NULL) { 1529316Svikram /* skip blank lines */ 1530316Svikram if (strspn(buf, " \t") == strlen(buf)) 1531316Svikram continue; 15320Sstevel@tonic-gate append_to_flist(flistp, buf); 15330Sstevel@tonic-gate } 15340Sstevel@tonic-gate if (fclose(fp) != 0) { 15350Sstevel@tonic-gate bam_error(CLOSE_FAIL, path, strerror(errno)); 15360Sstevel@tonic-gate return (BAM_ERROR); 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate return (BAM_SUCCESS); 15390Sstevel@tonic-gate } 15400Sstevel@tonic-gate 15410Sstevel@tonic-gate static error_t 15420Sstevel@tonic-gate read_list(char *root, filelist_t *flistp) 15430Sstevel@tonic-gate { 15440Sstevel@tonic-gate int rval; 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 15470Sstevel@tonic-gate 15480Sstevel@tonic-gate /* 15490Sstevel@tonic-gate * Read current lists of files - only the first is mandatory 15500Sstevel@tonic-gate */ 15510Sstevel@tonic-gate rval = read_one_list(root, flistp, BOOT_FILE_LIST); 15520Sstevel@tonic-gate if (rval != BAM_SUCCESS) 15530Sstevel@tonic-gate return (rval); 15540Sstevel@tonic-gate (void) read_one_list(root, flistp, ETC_FILE_LIST); 15550Sstevel@tonic-gate 15560Sstevel@tonic-gate if (flistp->head == NULL) { 15570Sstevel@tonic-gate bam_error(NO_FLIST); 15580Sstevel@tonic-gate return (BAM_ERROR); 15590Sstevel@tonic-gate } 15600Sstevel@tonic-gate 15610Sstevel@tonic-gate return (BAM_SUCCESS); 15620Sstevel@tonic-gate } 15630Sstevel@tonic-gate 15640Sstevel@tonic-gate static void 15650Sstevel@tonic-gate getoldstat(char *root) 15660Sstevel@tonic-gate { 15670Sstevel@tonic-gate char path[PATH_MAX]; 15680Sstevel@tonic-gate int fd, error; 15690Sstevel@tonic-gate struct stat sb; 15700Sstevel@tonic-gate char *ostat; 15710Sstevel@tonic-gate 15720Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 15730Sstevel@tonic-gate fd = open(path, O_RDONLY); 15740Sstevel@tonic-gate if (fd == -1) { 15750Sstevel@tonic-gate if (bam_verbose) 15760Sstevel@tonic-gate bam_print(OPEN_FAIL, path, strerror(errno)); 15770Sstevel@tonic-gate walk_arg.need_update = 1; 15780Sstevel@tonic-gate return; 15790Sstevel@tonic-gate } 15800Sstevel@tonic-gate 15810Sstevel@tonic-gate if (fstat(fd, &sb) != 0) { 15820Sstevel@tonic-gate bam_error(STAT_FAIL, path, strerror(errno)); 15830Sstevel@tonic-gate (void) close(fd); 15840Sstevel@tonic-gate walk_arg.need_update = 1; 15850Sstevel@tonic-gate return; 15860Sstevel@tonic-gate } 15870Sstevel@tonic-gate 15880Sstevel@tonic-gate ostat = s_calloc(1, sb.st_size); 15890Sstevel@tonic-gate 15900Sstevel@tonic-gate if (read(fd, ostat, sb.st_size) != sb.st_size) { 15910Sstevel@tonic-gate bam_error(READ_FAIL, path, strerror(errno)); 15920Sstevel@tonic-gate (void) close(fd); 15930Sstevel@tonic-gate free(ostat); 15940Sstevel@tonic-gate walk_arg.need_update = 1; 15950Sstevel@tonic-gate return; 15960Sstevel@tonic-gate } 15970Sstevel@tonic-gate 15980Sstevel@tonic-gate (void) close(fd); 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 16010Sstevel@tonic-gate error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0); 16020Sstevel@tonic-gate 16030Sstevel@tonic-gate free(ostat); 16040Sstevel@tonic-gate 16050Sstevel@tonic-gate if (error) { 16060Sstevel@tonic-gate bam_error(UNPACK_FAIL, path, strerror(error)); 16070Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 16080Sstevel@tonic-gate walk_arg.need_update = 1; 16090Sstevel@tonic-gate return; 16100Sstevel@tonic-gate } 16110Sstevel@tonic-gate } 16120Sstevel@tonic-gate 1613*2583Svikram /* 1614*2583Svikram * Checks if a file in the current (old) archive has 1615*2583Svikram * been deleted from the root filesystem. This is needed for 1616*2583Svikram * software like Trusted Extensions (TX) that switch early 1617*2583Svikram * in boot based on presence/absence of a kernel module. 1618*2583Svikram */ 1619*2583Svikram static void 1620*2583Svikram check4stale(char *root) 1621*2583Svikram { 1622*2583Svikram nvpair_t *nvp; 1623*2583Svikram nvlist_t *nvlp; 1624*2583Svikram char *file; 1625*2583Svikram char path[PATH_MAX]; 1626*2583Svikram struct stat sb; 1627*2583Svikram 1628*2583Svikram /* 1629*2583Svikram * Skip stale file check during smf check 1630*2583Svikram */ 1631*2583Svikram if (bam_smf_check) 1632*2583Svikram return; 1633*2583Svikram 1634*2583Svikram /* Nothing to do if no old stats */ 1635*2583Svikram if ((nvlp = walk_arg.old_nvlp) == NULL) 1636*2583Svikram return; 1637*2583Svikram 1638*2583Svikram for (nvp = nvlist_next_nvpair(nvlp, NULL); nvp; 1639*2583Svikram nvp = nvlist_next_nvpair(nvlp, nvp)) { 1640*2583Svikram file = nvpair_name(nvp); 1641*2583Svikram if (file == NULL) 1642*2583Svikram continue; 1643*2583Svikram (void) snprintf(path, sizeof (path), "%s/%s", 1644*2583Svikram root, file); 1645*2583Svikram if (stat(path, &sb) == -1) { 1646*2583Svikram walk_arg.need_update = 1; 1647*2583Svikram if (bam_verbose) 1648*2583Svikram bam_print(PARSEABLE_STALE_FILE, path); 1649*2583Svikram } 1650*2583Svikram } 1651*2583Svikram } 1652*2583Svikram 16530Sstevel@tonic-gate static void 16540Sstevel@tonic-gate create_newstat(void) 16550Sstevel@tonic-gate { 16560Sstevel@tonic-gate int error; 16570Sstevel@tonic-gate 16580Sstevel@tonic-gate error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0); 16590Sstevel@tonic-gate if (error) { 16600Sstevel@tonic-gate /* 16610Sstevel@tonic-gate * Not fatal - we can still create archive 16620Sstevel@tonic-gate */ 16630Sstevel@tonic-gate walk_arg.new_nvlp = NULL; 16640Sstevel@tonic-gate bam_error(NVALLOC_FAIL, strerror(error)); 16650Sstevel@tonic-gate } 16660Sstevel@tonic-gate } 16670Sstevel@tonic-gate 16680Sstevel@tonic-gate static void 16690Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp) 16700Sstevel@tonic-gate { 16710Sstevel@tonic-gate char path[PATH_MAX]; 16720Sstevel@tonic-gate line_t *lp; 16730Sstevel@tonic-gate 16740Sstevel@tonic-gate for (lp = flistp->head; lp; lp = lp->next) { 16750Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, lp->line); 16760Sstevel@tonic-gate /* XXX shouldn't we use FTW_MOUNT ? */ 16770Sstevel@tonic-gate if (nftw(path, cmpstat, 20, 0) == -1) { 16780Sstevel@tonic-gate /* 16790Sstevel@tonic-gate * Some files may not exist. 16800Sstevel@tonic-gate * For example: etc/rtc_config on a x86 diskless system 16810Sstevel@tonic-gate * Emit verbose message only 16820Sstevel@tonic-gate */ 16830Sstevel@tonic-gate if (bam_verbose) 16840Sstevel@tonic-gate bam_print(NFTW_FAIL, path, strerror(errno)); 16850Sstevel@tonic-gate } 16860Sstevel@tonic-gate } 16870Sstevel@tonic-gate } 16880Sstevel@tonic-gate 16890Sstevel@tonic-gate static void 16900Sstevel@tonic-gate savenew(char *root) 16910Sstevel@tonic-gate { 16920Sstevel@tonic-gate char path[PATH_MAX]; 16930Sstevel@tonic-gate char path2[PATH_MAX]; 16940Sstevel@tonic-gate size_t sz; 16950Sstevel@tonic-gate char *nstat; 16960Sstevel@tonic-gate int fd, wrote, error; 16970Sstevel@tonic-gate 16980Sstevel@tonic-gate nstat = NULL; 16990Sstevel@tonic-gate sz = 0; 17000Sstevel@tonic-gate error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz, 17010Sstevel@tonic-gate NV_ENCODE_XDR, 0); 17020Sstevel@tonic-gate if (error) { 17030Sstevel@tonic-gate bam_error(PACK_FAIL, strerror(error)); 17040Sstevel@tonic-gate return; 17050Sstevel@tonic-gate } 17060Sstevel@tonic-gate 17070Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP); 17080Sstevel@tonic-gate fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE); 17090Sstevel@tonic-gate if (fd == -1) { 17100Sstevel@tonic-gate bam_error(OPEN_FAIL, path, strerror(errno)); 17110Sstevel@tonic-gate free(nstat); 17120Sstevel@tonic-gate return; 17130Sstevel@tonic-gate } 17140Sstevel@tonic-gate wrote = write(fd, nstat, sz); 17150Sstevel@tonic-gate if (wrote != sz) { 17160Sstevel@tonic-gate bam_error(WRITE_FAIL, path, strerror(errno)); 17170Sstevel@tonic-gate (void) close(fd); 17180Sstevel@tonic-gate free(nstat); 17190Sstevel@tonic-gate return; 17200Sstevel@tonic-gate } 17210Sstevel@tonic-gate (void) close(fd); 17220Sstevel@tonic-gate free(nstat); 17230Sstevel@tonic-gate 17240Sstevel@tonic-gate (void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT); 17250Sstevel@tonic-gate if (rename(path, path2) != 0) { 17260Sstevel@tonic-gate bam_error(RENAME_FAIL, path2, strerror(errno)); 17270Sstevel@tonic-gate } 17280Sstevel@tonic-gate } 17290Sstevel@tonic-gate 17300Sstevel@tonic-gate static void 17310Sstevel@tonic-gate clear_walk_args(void) 17320Sstevel@tonic-gate { 17330Sstevel@tonic-gate if (walk_arg.old_nvlp) 17340Sstevel@tonic-gate nvlist_free(walk_arg.old_nvlp); 17350Sstevel@tonic-gate if (walk_arg.new_nvlp) 17360Sstevel@tonic-gate nvlist_free(walk_arg.new_nvlp); 17370Sstevel@tonic-gate walk_arg.need_update = 0; 17380Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 17390Sstevel@tonic-gate walk_arg.new_nvlp = NULL; 17400Sstevel@tonic-gate } 17410Sstevel@tonic-gate 17420Sstevel@tonic-gate /* 17430Sstevel@tonic-gate * Returns: 17440Sstevel@tonic-gate * 0 - no update necessary 17450Sstevel@tonic-gate * 1 - update required. 17460Sstevel@tonic-gate * BAM_ERROR (-1) - An error occurred 17470Sstevel@tonic-gate * 17480Sstevel@tonic-gate * Special handling for check (-n): 17490Sstevel@tonic-gate * ================================ 17500Sstevel@tonic-gate * The check (-n) option produces parseable output. 17510Sstevel@tonic-gate * To do this, we suppress all stdout messages unrelated 17520Sstevel@tonic-gate * to out of sync files. 17530Sstevel@tonic-gate * All stderr messages are still printed though. 17540Sstevel@tonic-gate * 17550Sstevel@tonic-gate */ 17560Sstevel@tonic-gate static int 17570Sstevel@tonic-gate update_required(char *root) 17580Sstevel@tonic-gate { 17590Sstevel@tonic-gate struct stat sb; 17600Sstevel@tonic-gate char path[PATH_MAX]; 17610Sstevel@tonic-gate filelist_t flist; 17620Sstevel@tonic-gate filelist_t *flistp = &flist; 17630Sstevel@tonic-gate int need_update; 17640Sstevel@tonic-gate 17650Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 17660Sstevel@tonic-gate 17670Sstevel@tonic-gate walk_arg.need_update = 0; 17680Sstevel@tonic-gate 17690Sstevel@tonic-gate /* 17700Sstevel@tonic-gate * Without consulting stat data, check if we need update 17710Sstevel@tonic-gate */ 17720Sstevel@tonic-gate check_flags_and_files(root); 17730Sstevel@tonic-gate 17740Sstevel@tonic-gate /* 17750Sstevel@tonic-gate * In certain deployment scenarios, filestat may not 17760Sstevel@tonic-gate * exist. Ignore it during boot-archive SMF check. 17770Sstevel@tonic-gate */ 17780Sstevel@tonic-gate if (bam_smf_check) { 17790Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 17800Sstevel@tonic-gate if (stat(path, &sb) != 0) 17810Sstevel@tonic-gate return (0); 17820Sstevel@tonic-gate } 17830Sstevel@tonic-gate 17840Sstevel@tonic-gate /* 17850Sstevel@tonic-gate * consult stat data only if we haven't made a decision 17860Sstevel@tonic-gate * about update. If checking (-n) however, we always 17870Sstevel@tonic-gate * need stat data (since we want to compare old and new) 17880Sstevel@tonic-gate */ 17890Sstevel@tonic-gate if (!walk_arg.need_update || bam_check) 17900Sstevel@tonic-gate getoldstat(root); 17910Sstevel@tonic-gate 17920Sstevel@tonic-gate /* 1793*2583Svikram * Check if the archive contains files that are no longer 1794*2583Svikram * present on the root filesystem. 1795*2583Svikram */ 1796*2583Svikram if (!walk_arg.need_update || bam_check) 1797*2583Svikram check4stale(root); 1798*2583Svikram 1799*2583Svikram /* 18000Sstevel@tonic-gate * read list of files 18010Sstevel@tonic-gate */ 18020Sstevel@tonic-gate if (read_list(root, flistp) != BAM_SUCCESS) { 18030Sstevel@tonic-gate clear_walk_args(); 18040Sstevel@tonic-gate return (BAM_ERROR); 18050Sstevel@tonic-gate } 18060Sstevel@tonic-gate 18070Sstevel@tonic-gate assert(flistp->head && flistp->tail); 18080Sstevel@tonic-gate 18090Sstevel@tonic-gate /* 18100Sstevel@tonic-gate * At this point either the update is required 18110Sstevel@tonic-gate * or the decision is pending. In either case 18120Sstevel@tonic-gate * we need to create new stat nvlist 18130Sstevel@tonic-gate */ 18140Sstevel@tonic-gate create_newstat(); 18150Sstevel@tonic-gate 18160Sstevel@tonic-gate /* 18170Sstevel@tonic-gate * This walk does 2 things: 18180Sstevel@tonic-gate * - gets new stat data for every file 18190Sstevel@tonic-gate * - (optional) compare old and new stat data 18200Sstevel@tonic-gate */ 18210Sstevel@tonic-gate walk_list(root, &flist); 18220Sstevel@tonic-gate 18230Sstevel@tonic-gate /* done with the file list */ 18240Sstevel@tonic-gate filelist_free(flistp); 18250Sstevel@tonic-gate 18260Sstevel@tonic-gate /* 18270Sstevel@tonic-gate * if we didn't succeed in creating new stat data above 18280Sstevel@tonic-gate * just return result of update check so that archive is built. 18290Sstevel@tonic-gate */ 18300Sstevel@tonic-gate if (walk_arg.new_nvlp == NULL) { 18310Sstevel@tonic-gate bam_error(NO_NEW_STAT); 18320Sstevel@tonic-gate need_update = walk_arg.need_update; 18330Sstevel@tonic-gate clear_walk_args(); 18340Sstevel@tonic-gate return (need_update ? 1 : 0); 18350Sstevel@tonic-gate } 18360Sstevel@tonic-gate 18370Sstevel@tonic-gate 18380Sstevel@tonic-gate /* 18390Sstevel@tonic-gate * If no update required, discard newstat 18400Sstevel@tonic-gate */ 18410Sstevel@tonic-gate if (!walk_arg.need_update) { 18420Sstevel@tonic-gate clear_walk_args(); 18430Sstevel@tonic-gate return (0); 18440Sstevel@tonic-gate } 18450Sstevel@tonic-gate 18460Sstevel@tonic-gate /* 18470Sstevel@tonic-gate * At this point we need an update - so save new stat data 18480Sstevel@tonic-gate * However, if only checking (-n), don't save new stat data. 18490Sstevel@tonic-gate */ 18500Sstevel@tonic-gate if (!bam_check) 18510Sstevel@tonic-gate savenew(root); 18520Sstevel@tonic-gate 18530Sstevel@tonic-gate clear_walk_args(); 18540Sstevel@tonic-gate 18550Sstevel@tonic-gate return (1); 18560Sstevel@tonic-gate } 18570Sstevel@tonic-gate 18580Sstevel@tonic-gate static error_t 18590Sstevel@tonic-gate create_ramdisk(char *root) 18600Sstevel@tonic-gate { 18610Sstevel@tonic-gate char *cmdline, path[PATH_MAX]; 18620Sstevel@tonic-gate size_t len; 18630Sstevel@tonic-gate struct stat sb; 18640Sstevel@tonic-gate 18650Sstevel@tonic-gate /* 18660Sstevel@tonic-gate * Setup command args for create_ramdisk.ksh 18670Sstevel@tonic-gate */ 18680Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK); 18690Sstevel@tonic-gate if (stat(path, &sb) != 0) { 18700Sstevel@tonic-gate bam_error(ARCH_EXEC_MISS, path, strerror(errno)); 18710Sstevel@tonic-gate return (BAM_ERROR); 18720Sstevel@tonic-gate } 18730Sstevel@tonic-gate 18740Sstevel@tonic-gate len = strlen(path) + strlen(root) + 10; /* room for space + -R */ 18750Sstevel@tonic-gate cmdline = s_calloc(1, len); 18760Sstevel@tonic-gate 18770Sstevel@tonic-gate if (strlen(root) > 1) { 18780Sstevel@tonic-gate (void) snprintf(cmdline, len, "%s -R %s", path, root); 18790Sstevel@tonic-gate /* chop off / at the end */ 18800Sstevel@tonic-gate cmdline[strlen(cmdline) - 1] = '\0'; 18810Sstevel@tonic-gate } else 18820Sstevel@tonic-gate (void) snprintf(cmdline, len, "%s", path); 18830Sstevel@tonic-gate 18840Sstevel@tonic-gate if (exec_cmd(cmdline, NULL, 0) != 0) { 18850Sstevel@tonic-gate bam_error(ARCHIVE_FAIL, cmdline); 18860Sstevel@tonic-gate free(cmdline); 18870Sstevel@tonic-gate return (BAM_ERROR); 18880Sstevel@tonic-gate } 18890Sstevel@tonic-gate free(cmdline); 18900Sstevel@tonic-gate 18910Sstevel@tonic-gate /* 18920Sstevel@tonic-gate * Verify that the archive has been created 18930Sstevel@tonic-gate */ 18940Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE); 18950Sstevel@tonic-gate if (stat(path, &sb) != 0) { 18960Sstevel@tonic-gate bam_error(ARCHIVE_NOT_CREATED, path); 18970Sstevel@tonic-gate return (BAM_ERROR); 18980Sstevel@tonic-gate } 18990Sstevel@tonic-gate 19000Sstevel@tonic-gate return (BAM_SUCCESS); 19010Sstevel@tonic-gate } 19020Sstevel@tonic-gate 19030Sstevel@tonic-gate /* 19040Sstevel@tonic-gate * Checks if target filesystem is on a ramdisk 19050Sstevel@tonic-gate * 1 - is miniroot 19060Sstevel@tonic-gate * 0 - is not 19070Sstevel@tonic-gate * When in doubt assume it is not a ramdisk. 19080Sstevel@tonic-gate */ 19090Sstevel@tonic-gate static int 19100Sstevel@tonic-gate is_ramdisk(char *root) 19110Sstevel@tonic-gate { 19120Sstevel@tonic-gate struct extmnttab mnt; 19130Sstevel@tonic-gate FILE *fp; 19140Sstevel@tonic-gate int found; 1915316Svikram char mntpt[PATH_MAX]; 1916316Svikram char *cp; 19170Sstevel@tonic-gate 19180Sstevel@tonic-gate /* 19190Sstevel@tonic-gate * There are 3 situations where creating archive is 19200Sstevel@tonic-gate * of dubious value: 1921316Svikram * - create boot_archive on a lofi-mounted boot_archive 19220Sstevel@tonic-gate * - create it on a ramdisk which is the root filesystem 19230Sstevel@tonic-gate * - create it on a ramdisk mounted somewhere else 19240Sstevel@tonic-gate * The first is not easy to detect and checking for it is not 19250Sstevel@tonic-gate * worth it. 19260Sstevel@tonic-gate * The other two conditions are handled here 19270Sstevel@tonic-gate */ 19280Sstevel@tonic-gate 19290Sstevel@tonic-gate fp = fopen(MNTTAB, "r"); 19300Sstevel@tonic-gate if (fp == NULL) { 19310Sstevel@tonic-gate bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 19320Sstevel@tonic-gate return (0); 19330Sstevel@tonic-gate } 19340Sstevel@tonic-gate 19350Sstevel@tonic-gate resetmnttab(fp); 19360Sstevel@tonic-gate 1937316Svikram /* 1938316Svikram * Remove any trailing / from the mount point 1939316Svikram */ 1940316Svikram (void) strlcpy(mntpt, root, sizeof (mntpt)); 1941316Svikram if (strcmp(root, "/") != 0) { 1942316Svikram cp = mntpt + strlen(mntpt) - 1; 1943316Svikram if (*cp == '/') 1944316Svikram *cp = '\0'; 1945316Svikram } 19460Sstevel@tonic-gate found = 0; 19470Sstevel@tonic-gate while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 1948316Svikram if (strcmp(mnt.mnt_mountp, mntpt) == 0) { 19490Sstevel@tonic-gate found = 1; 19500Sstevel@tonic-gate break; 19510Sstevel@tonic-gate } 19520Sstevel@tonic-gate } 19530Sstevel@tonic-gate 19540Sstevel@tonic-gate if (!found) { 19550Sstevel@tonic-gate if (bam_verbose) 1956316Svikram bam_error(NOT_IN_MNTTAB, mntpt); 19570Sstevel@tonic-gate (void) fclose(fp); 19580Sstevel@tonic-gate return (0); 19590Sstevel@tonic-gate } 19600Sstevel@tonic-gate 19610Sstevel@tonic-gate if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) { 19620Sstevel@tonic-gate if (bam_verbose) 19630Sstevel@tonic-gate bam_error(IS_RAMDISK, bam_root); 19640Sstevel@tonic-gate (void) fclose(fp); 19650Sstevel@tonic-gate return (1); 19660Sstevel@tonic-gate } 19670Sstevel@tonic-gate 19680Sstevel@tonic-gate (void) fclose(fp); 19690Sstevel@tonic-gate 19700Sstevel@tonic-gate return (0); 19710Sstevel@tonic-gate } 19720Sstevel@tonic-gate 19730Sstevel@tonic-gate static int 19740Sstevel@tonic-gate is_newboot(char *root) 19750Sstevel@tonic-gate { 19760Sstevel@tonic-gate char path[PATH_MAX]; 19770Sstevel@tonic-gate struct stat sb; 19780Sstevel@tonic-gate 19790Sstevel@tonic-gate /* 19800Sstevel@tonic-gate * We can't boot without MULTI_BOOT 19810Sstevel@tonic-gate */ 19820Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT); 19830Sstevel@tonic-gate if (stat(path, &sb) == -1) { 19840Sstevel@tonic-gate if (bam_verbose) 19850Sstevel@tonic-gate bam_print(FILE_MISS, path); 19860Sstevel@tonic-gate return (0); 19870Sstevel@tonic-gate } 19880Sstevel@tonic-gate 19890Sstevel@tonic-gate /* 19900Sstevel@tonic-gate * We can't generate archive without GRUB_DIR 19910Sstevel@tonic-gate */ 19920Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR); 19930Sstevel@tonic-gate if (stat(path, &sb) == -1) { 19940Sstevel@tonic-gate if (bam_verbose) 19950Sstevel@tonic-gate bam_print(DIR_MISS, path); 19960Sstevel@tonic-gate return (0); 19970Sstevel@tonic-gate } 19980Sstevel@tonic-gate 19990Sstevel@tonic-gate return (1); 20000Sstevel@tonic-gate } 20010Sstevel@tonic-gate 20020Sstevel@tonic-gate static int 20030Sstevel@tonic-gate is_readonly(char *root) 20040Sstevel@tonic-gate { 20050Sstevel@tonic-gate struct statvfs vfs; 20060Sstevel@tonic-gate 20070Sstevel@tonic-gate /* 20080Sstevel@tonic-gate * Check for RDONLY filesystem 20090Sstevel@tonic-gate * When in doubt assume it is not readonly 20100Sstevel@tonic-gate */ 20110Sstevel@tonic-gate if (statvfs(root, &vfs) != 0) { 20120Sstevel@tonic-gate if (bam_verbose) 20130Sstevel@tonic-gate bam_error(STATVFS_FAIL, root, strerror(errno)); 20140Sstevel@tonic-gate return (0); 20150Sstevel@tonic-gate } 20160Sstevel@tonic-gate 20170Sstevel@tonic-gate if (vfs.f_flag & ST_RDONLY) { 20180Sstevel@tonic-gate return (1); 20190Sstevel@tonic-gate } 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate return (0); 20220Sstevel@tonic-gate } 20230Sstevel@tonic-gate 20240Sstevel@tonic-gate static error_t 20250Sstevel@tonic-gate update_archive(char *root, char *opt) 20260Sstevel@tonic-gate { 20270Sstevel@tonic-gate error_t ret; 20280Sstevel@tonic-gate 20290Sstevel@tonic-gate assert(root); 20300Sstevel@tonic-gate assert(opt == NULL); 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate /* 2033316Svikram * root must belong to a GRUB boot OS, 20340Sstevel@tonic-gate * don't care on sparc except for diskless clients 20350Sstevel@tonic-gate */ 20360Sstevel@tonic-gate if (!is_newboot(root)) { 2037316Svikram /* 2038316Svikram * Emit message only if not in context of update_all. 2039316Svikram * If in update_all, emit only if verbose flag is set. 2040316Svikram */ 2041316Svikram if (!bam_update_all || bam_verbose) 2042316Svikram bam_print(NOT_GRUB_BOOT, root); 20430Sstevel@tonic-gate return (BAM_SUCCESS); 20440Sstevel@tonic-gate } 20450Sstevel@tonic-gate 20460Sstevel@tonic-gate /* 2047662Sszhou * If smf check is requested when / is writable (can happen 2048662Sszhou * on first reboot following an upgrade because service 2049662Sszhou * dependency is messed up), skip the check. 2050662Sszhou */ 2051662Sszhou if (bam_smf_check && !bam_root_readonly) 2052662Sszhou return (BAM_SUCCESS); 2053662Sszhou 2054662Sszhou /* 2055662Sszhou * root must be writable. This check applies to alternate 2056662Sszhou * root (-R option); bam_root_readonly applies to '/' only. 20570Sstevel@tonic-gate * Note: statvfs() does not always report the truth 20580Sstevel@tonic-gate */ 2059756Ssetje if (!bam_smf_check && !bam_check && is_readonly(root)) { 2060662Sszhou if (bam_verbose) 20610Sstevel@tonic-gate bam_print(RDONLY_FS, root); 20620Sstevel@tonic-gate return (BAM_SUCCESS); 20630Sstevel@tonic-gate } 20640Sstevel@tonic-gate 20650Sstevel@tonic-gate /* 20660Sstevel@tonic-gate * Don't generate archive on ramdisk 20670Sstevel@tonic-gate */ 20680Sstevel@tonic-gate if (is_ramdisk(root)) { 20690Sstevel@tonic-gate if (bam_verbose) 20700Sstevel@tonic-gate bam_print(SKIP_RAMDISK); 20710Sstevel@tonic-gate return (BAM_SUCCESS); 20720Sstevel@tonic-gate } 20730Sstevel@tonic-gate 20740Sstevel@tonic-gate /* 20750Sstevel@tonic-gate * Now check if updated is really needed 20760Sstevel@tonic-gate */ 20770Sstevel@tonic-gate ret = update_required(root); 20780Sstevel@tonic-gate 20790Sstevel@tonic-gate /* 20800Sstevel@tonic-gate * The check command (-n) is *not* a dry run 20810Sstevel@tonic-gate * It only checks if the archive is in sync. 20820Sstevel@tonic-gate */ 20830Sstevel@tonic-gate if (bam_check) { 20840Sstevel@tonic-gate bam_exit((ret != 0) ? 1 : 0); 20850Sstevel@tonic-gate } 20860Sstevel@tonic-gate 20870Sstevel@tonic-gate if (ret == 1) { 20880Sstevel@tonic-gate /* create the ramdisk */ 20890Sstevel@tonic-gate ret = create_ramdisk(root); 20900Sstevel@tonic-gate } 20910Sstevel@tonic-gate return (ret); 20920Sstevel@tonic-gate } 20930Sstevel@tonic-gate 2094316Svikram static void 20951746Svikram update_fdisk(void) 20961746Svikram { 20971746Svikram struct stat sb; 20981746Svikram char cmd[PATH_MAX]; 20991746Svikram int ret1, ret2; 21001746Svikram 21011746Svikram assert(stat(GRUB_fdisk, &sb) == 0); 21021746Svikram assert(stat(GRUB_fdisk_target, &sb) == 0); 21031746Svikram 21041746Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/fdisk -F %s `/bin/cat %s`", 21051746Svikram GRUB_fdisk, GRUB_fdisk_target); 21061746Svikram 21071746Svikram bam_print(UPDATING_FDISK); 21081746Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 21091746Svikram bam_error(FDISK_UPDATE_FAILED); 21101746Svikram } 21111746Svikram 21121746Svikram /* 21131746Svikram * We are done, remove the files. 21141746Svikram */ 21151746Svikram ret1 = unlink(GRUB_fdisk); 21161746Svikram ret2 = unlink(GRUB_fdisk_target); 21171746Svikram if (ret1 != 0 || ret2 != 0) { 21181746Svikram bam_error(FILE_REMOVE_FAILED, GRUB_fdisk, GRUB_fdisk_target); 21191746Svikram } 21201746Svikram } 21211746Svikram 21221746Svikram static void 2123316Svikram restore_grub_slice(void) 2124316Svikram { 2125316Svikram struct stat sb; 2126316Svikram char *mntpt, *physlice; 2127316Svikram int mnted; /* set if we did a mount */ 2128316Svikram char menupath[PATH_MAX], cmd[PATH_MAX]; 2129316Svikram 2130316Svikram if (stat(GRUB_slice, &sb) != 0) { 2131316Svikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 2132316Svikram return; 2133316Svikram } 2134316Svikram 2135316Svikram /* 2136316Svikram * If we are doing an luactivate, don't attempt to restore GRUB or else 2137316Svikram * we may not be able to get to DCA boot environments. Let luactivate 2138316Svikram * handle GRUB/DCA installation 2139316Svikram */ 2140316Svikram if (stat(LU_ACTIVATE_FILE, &sb) == 0) { 2141316Svikram return; 2142316Svikram } 2143316Svikram 2144316Svikram mnted = 0; 2145316Svikram physlice = NULL; 2146621Svikram mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL); 2147316Svikram if (mntpt == NULL) { 2148316Svikram bam_error(CANNOT_RESTORE_GRUB_SLICE); 2149316Svikram return; 2150316Svikram } 2151316Svikram 2152316Svikram (void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU); 2153316Svikram if (stat(menupath, &sb) == 0) { 2154621Svikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2155316Svikram return; 2156316Svikram } 2157316Svikram 2158316Svikram /* 2159316Svikram * The menu is missing - we need to do a restore 2160316Svikram */ 2161316Svikram bam_print(RESTORING_GRUB); 2162316Svikram 2163316Svikram (void) snprintf(cmd, sizeof (cmd), "%s %s %s %s", 2164316Svikram INSTALLGRUB, STAGE1, STAGE2, physlice); 2165316Svikram 2166316Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 2167316Svikram bam_error(RESTORE_GRUB_FAILED); 2168621Svikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2169316Svikram return; 2170316Svikram } 2171316Svikram 2172316Svikram if (stat(GRUB_backup_menu, &sb) != 0) { 2173316Svikram bam_error(MISSING_BACKUP_MENU, 2174316Svikram GRUB_backup_menu, strerror(errno)); 2175621Svikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2176316Svikram return; 2177316Svikram } 2178316Svikram 2179316Svikram (void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s", 2180316Svikram GRUB_backup_menu, menupath); 2181316Svikram 2182316Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 2183316Svikram bam_error(RESTORE_MENU_FAILED, menupath); 2184621Svikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2185316Svikram return; 2186316Svikram } 2187316Svikram 2188316Svikram /* Success */ 2189621Svikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2190316Svikram } 2191316Svikram 21920Sstevel@tonic-gate static error_t 21930Sstevel@tonic-gate update_all(char *root, char *opt) 21940Sstevel@tonic-gate { 21950Sstevel@tonic-gate struct extmnttab mnt; 21960Sstevel@tonic-gate struct stat sb; 21970Sstevel@tonic-gate FILE *fp; 21980Sstevel@tonic-gate char multibt[PATH_MAX]; 21990Sstevel@tonic-gate error_t ret = BAM_SUCCESS; 22001746Svikram int ret1, ret2; 22010Sstevel@tonic-gate 2202621Svikram assert(root); 22030Sstevel@tonic-gate assert(opt == NULL); 22040Sstevel@tonic-gate 2205621Svikram if (bam_rootlen != 1 || *root != '/') { 2206621Svikram elide_trailing_slash(root, multibt, sizeof (multibt)); 2207621Svikram bam_error(ALT_ROOT_INVALID, multibt); 2208621Svikram return (BAM_ERROR); 2209621Svikram } 2210621Svikram 22110Sstevel@tonic-gate /* 22120Sstevel@tonic-gate * First update archive for current root 22130Sstevel@tonic-gate */ 22140Sstevel@tonic-gate if (update_archive(root, opt) != BAM_SUCCESS) 22150Sstevel@tonic-gate ret = BAM_ERROR; 22160Sstevel@tonic-gate 22170Sstevel@tonic-gate /* 22180Sstevel@tonic-gate * Now walk the mount table, performing archive update 22190Sstevel@tonic-gate * for all mounted Newboot root filesystems 22200Sstevel@tonic-gate */ 22210Sstevel@tonic-gate fp = fopen(MNTTAB, "r"); 22220Sstevel@tonic-gate if (fp == NULL) { 22230Sstevel@tonic-gate bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 2224316Svikram ret = BAM_ERROR; 2225316Svikram goto out; 22260Sstevel@tonic-gate } 22270Sstevel@tonic-gate 22280Sstevel@tonic-gate resetmnttab(fp); 22290Sstevel@tonic-gate 22300Sstevel@tonic-gate while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 22310Sstevel@tonic-gate if (mnt.mnt_special == NULL) 22320Sstevel@tonic-gate continue; 22330Sstevel@tonic-gate if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0) 22340Sstevel@tonic-gate continue; 22350Sstevel@tonic-gate if (strcmp(mnt.mnt_mountp, "/") == 0) 22360Sstevel@tonic-gate continue; 22370Sstevel@tonic-gate 22380Sstevel@tonic-gate (void) snprintf(multibt, sizeof (multibt), "%s%s", 22390Sstevel@tonic-gate mnt.mnt_mountp, MULTI_BOOT); 22400Sstevel@tonic-gate 22410Sstevel@tonic-gate if (stat(multibt, &sb) == -1) 22420Sstevel@tonic-gate continue; 22430Sstevel@tonic-gate 22440Sstevel@tonic-gate /* 22450Sstevel@tonic-gate * We put a trailing slash to be consistent with root = "/" 22460Sstevel@tonic-gate * case, such that we don't have to print // in some cases. 22470Sstevel@tonic-gate */ 22480Sstevel@tonic-gate (void) snprintf(rootbuf, sizeof (rootbuf), "%s/", 22490Sstevel@tonic-gate mnt.mnt_mountp); 22500Sstevel@tonic-gate bam_rootlen = strlen(rootbuf); 22510Sstevel@tonic-gate if (update_archive(rootbuf, opt) != BAM_SUCCESS) 22520Sstevel@tonic-gate ret = BAM_ERROR; 22530Sstevel@tonic-gate } 22540Sstevel@tonic-gate 22550Sstevel@tonic-gate (void) fclose(fp); 22560Sstevel@tonic-gate 2257316Svikram out: 2258316Svikram if (stat(GRUB_slice, &sb) == 0) { 2259316Svikram restore_grub_slice(); 2260316Svikram } 2261316Svikram 22621746Svikram /* 22631746Svikram * Update fdisk table as we go down. Updating it when 22641746Svikram * the system is running will confuse biosdev. 22651746Svikram */ 22661746Svikram ret1 = stat(GRUB_fdisk, &sb); 22671746Svikram ret2 = stat(GRUB_fdisk_target, &sb); 22681746Svikram if ((ret1 == 0) && (ret2 == 0)) { 22691746Svikram update_fdisk(); 22701746Svikram } else if ((ret1 == 0) ^ (ret2 == 0)) { 22711746Svikram /* 22721746Svikram * It is an error for one file to be 22731746Svikram * present and the other absent. 22741746Svikram * It is normal for both files to be 22751746Svikram * absent - it indicates that no fdisk 22761746Svikram * update is required. 22771746Svikram */ 22781746Svikram bam_error(MISSING_FDISK_FILE, 22791746Svikram ret1 ? GRUB_fdisk : GRUB_fdisk_target); 22801746Svikram ret = BAM_ERROR; 22811746Svikram } 22821746Svikram 22830Sstevel@tonic-gate return (ret); 22840Sstevel@tonic-gate } 22850Sstevel@tonic-gate 22860Sstevel@tonic-gate static void 22870Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp) 22880Sstevel@tonic-gate { 22890Sstevel@tonic-gate if (mp->start == NULL) { 22900Sstevel@tonic-gate mp->start = lp; 22910Sstevel@tonic-gate } else { 22920Sstevel@tonic-gate mp->end->next = lp; 2293662Sszhou lp->prev = mp->end; 22940Sstevel@tonic-gate } 22950Sstevel@tonic-gate mp->end = lp; 22960Sstevel@tonic-gate } 22970Sstevel@tonic-gate 2298662Sszhou static void 2299662Sszhou unlink_line(menu_t *mp, line_t *lp) 2300662Sszhou { 2301662Sszhou /* unlink from list */ 2302662Sszhou if (lp->prev) 2303662Sszhou lp->prev->next = lp->next; 2304662Sszhou else 2305662Sszhou mp->start = lp->next; 2306662Sszhou if (lp->next) 2307662Sszhou lp->next->prev = lp->prev; 2308662Sszhou else 2309662Sszhou mp->end = lp->prev; 2310662Sszhou } 2311662Sszhou 2312662Sszhou static entry_t * 2313662Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end) 2314662Sszhou { 2315662Sszhou entry_t *ent, *prev; 2316662Sszhou 2317662Sszhou ent = s_calloc(1, sizeof (entry_t)); 2318662Sszhou ent->start = start; 2319662Sszhou ent->end = end; 2320662Sszhou 2321662Sszhou if (mp->entries == NULL) { 2322662Sszhou mp->entries = ent; 2323662Sszhou return (ent); 2324662Sszhou } 2325662Sszhou 2326662Sszhou prev = mp->entries; 2327662Sszhou while (prev->next) 2328662Sszhou prev = prev-> next; 2329662Sszhou prev->next = ent; 2330662Sszhou ent->prev = prev; 2331662Sszhou return (ent); 2332662Sszhou } 2333662Sszhou 2334662Sszhou static void 2335662Sszhou boot_entry_addline(entry_t *ent, line_t *lp) 2336662Sszhou { 2337662Sszhou if (ent) 2338662Sszhou ent->end = lp; 2339662Sszhou } 2340662Sszhou 23410Sstevel@tonic-gate /* 23420Sstevel@tonic-gate * A line in menu.lst looks like 23430Sstevel@tonic-gate * [ ]*<cmd>[ \t=]*<arg>* 23440Sstevel@tonic-gate */ 23450Sstevel@tonic-gate static void 23460Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum) 23470Sstevel@tonic-gate { 23480Sstevel@tonic-gate /* 23490Sstevel@tonic-gate * save state across calls. This is so that 23500Sstevel@tonic-gate * header gets the right entry# after title has 23510Sstevel@tonic-gate * been processed 23520Sstevel@tonic-gate */ 2353662Sszhou static line_t *prev = NULL; 2354662Sszhou static entry_t *curr_ent = NULL; 23550Sstevel@tonic-gate 23560Sstevel@tonic-gate line_t *lp; 23570Sstevel@tonic-gate char *cmd, *sep, *arg; 23580Sstevel@tonic-gate char save, *cp, *line; 23590Sstevel@tonic-gate menu_flag_t flag = BAM_INVALID; 23600Sstevel@tonic-gate 23610Sstevel@tonic-gate if (str == NULL) { 23620Sstevel@tonic-gate return; 23630Sstevel@tonic-gate } 23640Sstevel@tonic-gate 23650Sstevel@tonic-gate /* 23660Sstevel@tonic-gate * First save a copy of the entire line. 23670Sstevel@tonic-gate * We use this later to set the line field. 23680Sstevel@tonic-gate */ 23690Sstevel@tonic-gate line = s_strdup(str); 23700Sstevel@tonic-gate 23710Sstevel@tonic-gate /* Eat up leading whitespace */ 23720Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 23730Sstevel@tonic-gate str++; 23740Sstevel@tonic-gate 23750Sstevel@tonic-gate if (*str == '#') { /* comment */ 23760Sstevel@tonic-gate cmd = s_strdup("#"); 23770Sstevel@tonic-gate sep = NULL; 23780Sstevel@tonic-gate arg = s_strdup(str + 1); 23790Sstevel@tonic-gate flag = BAM_COMMENT; 23800Sstevel@tonic-gate } else if (*str == '\0') { /* blank line */ 23810Sstevel@tonic-gate cmd = sep = arg = NULL; 23820Sstevel@tonic-gate flag = BAM_EMPTY; 23830Sstevel@tonic-gate } else { 23840Sstevel@tonic-gate /* 23850Sstevel@tonic-gate * '=' is not a documented separator in grub syntax. 23860Sstevel@tonic-gate * However various development bits use '=' as a 23870Sstevel@tonic-gate * separator. In addition, external users also 23880Sstevel@tonic-gate * use = as a separator. So we will allow that usage. 23890Sstevel@tonic-gate */ 23900Sstevel@tonic-gate cp = str; 23910Sstevel@tonic-gate while (*str != ' ' && *str != '\t' && *str != '=') { 23920Sstevel@tonic-gate if (*str == '\0') { 23930Sstevel@tonic-gate cmd = s_strdup(cp); 23940Sstevel@tonic-gate sep = arg = NULL; 23950Sstevel@tonic-gate break; 23960Sstevel@tonic-gate } 23970Sstevel@tonic-gate str++; 23980Sstevel@tonic-gate } 23990Sstevel@tonic-gate 24000Sstevel@tonic-gate if (*str != '\0') { 24010Sstevel@tonic-gate save = *str; 24020Sstevel@tonic-gate *str = '\0'; 24030Sstevel@tonic-gate cmd = s_strdup(cp); 24040Sstevel@tonic-gate *str = save; 24050Sstevel@tonic-gate 24060Sstevel@tonic-gate str++; 24070Sstevel@tonic-gate save = *str; 24080Sstevel@tonic-gate *str = '\0'; 24090Sstevel@tonic-gate sep = s_strdup(str - 1); 24100Sstevel@tonic-gate *str = save; 24110Sstevel@tonic-gate 24120Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 24130Sstevel@tonic-gate str++; 24140Sstevel@tonic-gate if (*str == '\0') 24150Sstevel@tonic-gate arg = NULL; 24160Sstevel@tonic-gate else 24170Sstevel@tonic-gate arg = s_strdup(str); 24180Sstevel@tonic-gate } 24190Sstevel@tonic-gate } 24200Sstevel@tonic-gate 24210Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 24220Sstevel@tonic-gate 24230Sstevel@tonic-gate lp->cmd = cmd; 24240Sstevel@tonic-gate lp->sep = sep; 24250Sstevel@tonic-gate lp->arg = arg; 24260Sstevel@tonic-gate lp->line = line; 24270Sstevel@tonic-gate lp->lineNum = ++(*lineNum); 24280Sstevel@tonic-gate if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) { 24290Sstevel@tonic-gate lp->entryNum = ++(*entryNum); 24300Sstevel@tonic-gate lp->flags = BAM_TITLE; 24310Sstevel@tonic-gate if (prev && prev->flags == BAM_COMMENT && 2432662Sszhou prev->arg && strcmp(prev->arg, BAM_HDR) == 0) { 24330Sstevel@tonic-gate prev->entryNum = lp->entryNum; 2434662Sszhou curr_ent = boot_entry_new(mp, prev, lp); 2435662Sszhou } else { 2436662Sszhou curr_ent = boot_entry_new(mp, lp, lp); 2437662Sszhou } 24380Sstevel@tonic-gate } else if (flag != BAM_INVALID) { 24390Sstevel@tonic-gate /* 24400Sstevel@tonic-gate * For header comments, the entry# is "fixed up" 24410Sstevel@tonic-gate * by the subsequent title 24420Sstevel@tonic-gate */ 24430Sstevel@tonic-gate lp->entryNum = *entryNum; 24440Sstevel@tonic-gate lp->flags = flag; 24450Sstevel@tonic-gate } else { 24460Sstevel@tonic-gate lp->entryNum = *entryNum; 24470Sstevel@tonic-gate lp->flags = (*entryNum == ENTRY_INIT) ? BAM_GLOBAL : BAM_ENTRY; 24480Sstevel@tonic-gate } 24490Sstevel@tonic-gate 2450662Sszhou /* record default, old default, and entry line ranges */ 2451662Sszhou if (lp->flags == BAM_GLOBAL && 2452662Sszhou strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) { 2453662Sszhou mp->curdefault = lp; 2454662Sszhou } else if (lp->flags == BAM_COMMENT && 2455662Sszhou strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) { 2456662Sszhou mp->olddefault = lp; 2457662Sszhou } else if (lp->flags == BAM_ENTRY || 2458662Sszhou (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_FTR) == 0)) { 2459662Sszhou boot_entry_addline(curr_ent, lp); 2460662Sszhou } 24610Sstevel@tonic-gate append_line(mp, lp); 24620Sstevel@tonic-gate 24630Sstevel@tonic-gate prev = lp; 24640Sstevel@tonic-gate } 24650Sstevel@tonic-gate 2466621Svikram static void 2467621Svikram update_numbering(menu_t *mp) 2468621Svikram { 2469621Svikram int lineNum; 2470621Svikram int entryNum; 2471621Svikram int old_default_value; 2472621Svikram line_t *lp, *prev, *default_lp, *default_entry; 2473621Svikram char buf[PATH_MAX]; 2474621Svikram 2475621Svikram if (mp->start == NULL) { 2476621Svikram return; 2477621Svikram } 2478621Svikram 2479621Svikram lineNum = LINE_INIT; 2480621Svikram entryNum = ENTRY_INIT; 2481621Svikram old_default_value = ENTRY_INIT; 2482621Svikram lp = default_lp = default_entry = NULL; 2483621Svikram 2484621Svikram prev = NULL; 2485621Svikram for (lp = mp->start; lp; prev = lp, lp = lp->next) { 2486621Svikram lp->lineNum = ++lineNum; 2487621Svikram 2488621Svikram /* 2489621Svikram * Get the value of the default command 2490621Svikram */ 2491621Svikram if (lp->entryNum == ENTRY_INIT && lp->cmd && 2492621Svikram strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 && 2493621Svikram lp->arg) { 2494621Svikram old_default_value = atoi(lp->arg); 2495621Svikram default_lp = lp; 2496621Svikram } 2497621Svikram 2498621Svikram /* 2499621Svikram * If not boot entry, nothing else to fix for this 2500621Svikram * entry 2501621Svikram */ 2502621Svikram if (lp->entryNum == ENTRY_INIT) 2503621Svikram continue; 2504621Svikram 2505621Svikram /* 2506621Svikram * Record the position of the default entry. 2507621Svikram * The following works because global 2508621Svikram * commands like default and timeout should precede 2509621Svikram * actual boot entries, so old_default_value 2510621Svikram * is already known (or default cmd is missing). 2511621Svikram */ 2512621Svikram if (default_entry == NULL && 2513621Svikram old_default_value != ENTRY_INIT && 2514621Svikram lp->entryNum == old_default_value) { 2515621Svikram default_entry = lp; 2516621Svikram } 2517621Svikram 2518621Svikram /* 2519621Svikram * Now fixup the entry number 2520621Svikram */ 2521621Svikram if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) { 2522621Svikram lp->entryNum = ++entryNum; 2523621Svikram /* fixup the bootadm header */ 2524621Svikram if (prev && prev->flags == BAM_COMMENT && 2525621Svikram prev->arg && strcmp(prev->arg, BAM_HDR) == 0) { 2526621Svikram prev->entryNum = lp->entryNum; 2527621Svikram } 2528621Svikram } else { 2529621Svikram lp->entryNum = entryNum; 2530621Svikram } 2531621Svikram } 2532621Svikram 2533621Svikram /* 2534621Svikram * No default command in menu, simply return 2535621Svikram */ 2536621Svikram if (default_lp == NULL) { 2537621Svikram return; 2538621Svikram } 2539621Svikram 2540621Svikram free(default_lp->arg); 2541621Svikram free(default_lp->line); 2542621Svikram 2543621Svikram if (default_entry == NULL) { 2544621Svikram default_lp->arg = s_strdup("0"); 2545621Svikram } else { 2546621Svikram (void) snprintf(buf, sizeof (buf), "%d", 2547621Svikram default_entry->entryNum); 2548621Svikram default_lp->arg = s_strdup(buf); 2549621Svikram } 2550621Svikram 2551621Svikram /* 2552621Svikram * The following is required since only the line field gets 2553621Svikram * written back to menu.lst 2554621Svikram */ 2555621Svikram (void) snprintf(buf, sizeof (buf), "%s%s%s", 2556621Svikram menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg); 2557621Svikram default_lp->line = s_strdup(buf); 2558621Svikram } 2559621Svikram 2560621Svikram 25610Sstevel@tonic-gate static menu_t * 25620Sstevel@tonic-gate menu_read(char *menu_path) 25630Sstevel@tonic-gate { 25640Sstevel@tonic-gate FILE *fp; 25650Sstevel@tonic-gate char buf[BAM_MAXLINE], *cp; 25660Sstevel@tonic-gate menu_t *mp; 25670Sstevel@tonic-gate int line, entry, len, n; 25680Sstevel@tonic-gate 25690Sstevel@tonic-gate mp = s_calloc(1, sizeof (menu_t)); 25700Sstevel@tonic-gate 25710Sstevel@tonic-gate fp = fopen(menu_path, "r"); 25720Sstevel@tonic-gate if (fp == NULL) { /* Let the caller handle this error */ 25730Sstevel@tonic-gate return (mp); 25740Sstevel@tonic-gate } 25750Sstevel@tonic-gate 25760Sstevel@tonic-gate 25770Sstevel@tonic-gate /* Note: GRUB boot entry number starts with 0 */ 25780Sstevel@tonic-gate line = LINE_INIT; 25790Sstevel@tonic-gate entry = ENTRY_INIT; 25800Sstevel@tonic-gate cp = buf; 25810Sstevel@tonic-gate len = sizeof (buf); 25820Sstevel@tonic-gate while (s_fgets(cp, len, fp) != NULL) { 25830Sstevel@tonic-gate n = strlen(cp); 25840Sstevel@tonic-gate if (cp[n - 1] == '\\') { 25850Sstevel@tonic-gate len -= n - 1; 25860Sstevel@tonic-gate assert(len >= 2); 25870Sstevel@tonic-gate cp += n - 1; 25880Sstevel@tonic-gate continue; 25890Sstevel@tonic-gate } 25900Sstevel@tonic-gate line_parser(mp, buf, &line, &entry); 25910Sstevel@tonic-gate cp = buf; 25920Sstevel@tonic-gate len = sizeof (buf); 25930Sstevel@tonic-gate } 25940Sstevel@tonic-gate 25950Sstevel@tonic-gate if (fclose(fp) == EOF) { 25960Sstevel@tonic-gate bam_error(CLOSE_FAIL, menu_path, strerror(errno)); 25970Sstevel@tonic-gate } 25980Sstevel@tonic-gate 25990Sstevel@tonic-gate return (mp); 26000Sstevel@tonic-gate } 26010Sstevel@tonic-gate 26020Sstevel@tonic-gate static error_t 26030Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title) 26040Sstevel@tonic-gate { 26050Sstevel@tonic-gate char *eq; 26060Sstevel@tonic-gate char *opt_dup; 26070Sstevel@tonic-gate int entryNum; 26080Sstevel@tonic-gate 26090Sstevel@tonic-gate assert(mp); 26100Sstevel@tonic-gate assert(mp->start); 26110Sstevel@tonic-gate assert(opt); 26120Sstevel@tonic-gate 26130Sstevel@tonic-gate opt_dup = s_strdup(opt); 26140Sstevel@tonic-gate 26150Sstevel@tonic-gate if (entry) 26160Sstevel@tonic-gate *entry = ENTRY_INIT; 26170Sstevel@tonic-gate if (title) 26180Sstevel@tonic-gate *title = NULL; 26190Sstevel@tonic-gate 26200Sstevel@tonic-gate eq = strchr(opt_dup, '='); 26210Sstevel@tonic-gate if (eq == NULL) { 26220Sstevel@tonic-gate bam_error(INVALID_OPT, opt); 26230Sstevel@tonic-gate free(opt_dup); 26240Sstevel@tonic-gate return (BAM_ERROR); 26250Sstevel@tonic-gate } 26260Sstevel@tonic-gate 26270Sstevel@tonic-gate *eq = '\0'; 26280Sstevel@tonic-gate if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) { 26290Sstevel@tonic-gate assert(mp->end); 26300Sstevel@tonic-gate entryNum = s_strtol(eq + 1); 26310Sstevel@tonic-gate if (entryNum < 0 || entryNum > mp->end->entryNum) { 26320Sstevel@tonic-gate bam_error(INVALID_ENTRY, eq + 1); 26330Sstevel@tonic-gate free(opt_dup); 26340Sstevel@tonic-gate return (BAM_ERROR); 26350Sstevel@tonic-gate } 26360Sstevel@tonic-gate *entry = entryNum; 26370Sstevel@tonic-gate } else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) { 26380Sstevel@tonic-gate *title = opt + (eq - opt_dup) + 1; 26390Sstevel@tonic-gate } else { 26400Sstevel@tonic-gate bam_error(INVALID_OPT, opt); 26410Sstevel@tonic-gate free(opt_dup); 26420Sstevel@tonic-gate return (BAM_ERROR); 26430Sstevel@tonic-gate } 26440Sstevel@tonic-gate 26450Sstevel@tonic-gate free(opt_dup); 26460Sstevel@tonic-gate return (BAM_SUCCESS); 26470Sstevel@tonic-gate } 26480Sstevel@tonic-gate 26490Sstevel@tonic-gate /* 26500Sstevel@tonic-gate * If invoked with no titles/entries (opt == NULL) 26510Sstevel@tonic-gate * only title lines in file are printed. 26520Sstevel@tonic-gate * 26530Sstevel@tonic-gate * If invoked with a title or entry #, all 26540Sstevel@tonic-gate * lines in *every* matching entry are listed 26550Sstevel@tonic-gate */ 26560Sstevel@tonic-gate static error_t 26570Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt) 26580Sstevel@tonic-gate { 26590Sstevel@tonic-gate line_t *lp; 26600Sstevel@tonic-gate int entry = ENTRY_INIT; 26610Sstevel@tonic-gate int found; 26620Sstevel@tonic-gate char *title = NULL; 26630Sstevel@tonic-gate 26640Sstevel@tonic-gate assert(mp); 26650Sstevel@tonic-gate assert(menu_path); 26660Sstevel@tonic-gate 26670Sstevel@tonic-gate if (mp->start == NULL) { 26680Sstevel@tonic-gate bam_error(NO_MENU, menu_path); 26690Sstevel@tonic-gate return (BAM_ERROR); 26700Sstevel@tonic-gate } 26710Sstevel@tonic-gate 26720Sstevel@tonic-gate if (opt != NULL) { 26730Sstevel@tonic-gate if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) { 26740Sstevel@tonic-gate return (BAM_ERROR); 26750Sstevel@tonic-gate } 26760Sstevel@tonic-gate assert((entry != ENTRY_INIT) ^ (title != NULL)); 26770Sstevel@tonic-gate } else { 26780Sstevel@tonic-gate (void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0); 26790Sstevel@tonic-gate (void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0); 26800Sstevel@tonic-gate } 26810Sstevel@tonic-gate 26820Sstevel@tonic-gate found = 0; 26830Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 26840Sstevel@tonic-gate if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY) 26850Sstevel@tonic-gate continue; 26860Sstevel@tonic-gate if (opt == NULL && lp->flags == BAM_TITLE) { 26870Sstevel@tonic-gate bam_print(PRINT_TITLE, lp->entryNum, 26880Sstevel@tonic-gate lp->arg); 26890Sstevel@tonic-gate found = 1; 26900Sstevel@tonic-gate continue; 26910Sstevel@tonic-gate } 26920Sstevel@tonic-gate if (entry != ENTRY_INIT && lp->entryNum == entry) { 26930Sstevel@tonic-gate bam_print(PRINT, lp->line); 26940Sstevel@tonic-gate found = 1; 26950Sstevel@tonic-gate continue; 26960Sstevel@tonic-gate } 26970Sstevel@tonic-gate 26980Sstevel@tonic-gate /* 26990Sstevel@tonic-gate * We set the entry value here so that all lines 27000Sstevel@tonic-gate * in entry get printed. If we subsequently match 27010Sstevel@tonic-gate * title in other entries, all lines in those 27020Sstevel@tonic-gate * entries get printed as well. 27030Sstevel@tonic-gate */ 27040Sstevel@tonic-gate if (title && lp->flags == BAM_TITLE && lp->arg && 27050Sstevel@tonic-gate strncmp(title, lp->arg, strlen(title)) == 0) { 27060Sstevel@tonic-gate bam_print(PRINT, lp->line); 27070Sstevel@tonic-gate entry = lp->entryNum; 27080Sstevel@tonic-gate found = 1; 27090Sstevel@tonic-gate continue; 27100Sstevel@tonic-gate } 27110Sstevel@tonic-gate } 27120Sstevel@tonic-gate 27130Sstevel@tonic-gate if (!found) { 27140Sstevel@tonic-gate bam_error(NO_MATCH_ENTRY); 27150Sstevel@tonic-gate return (BAM_ERROR); 27160Sstevel@tonic-gate } 27170Sstevel@tonic-gate 27180Sstevel@tonic-gate return (BAM_SUCCESS); 27190Sstevel@tonic-gate } 27200Sstevel@tonic-gate 27210Sstevel@tonic-gate static int 27220Sstevel@tonic-gate add_boot_entry(menu_t *mp, 27230Sstevel@tonic-gate char *title, 27240Sstevel@tonic-gate char *root, 27250Sstevel@tonic-gate char *kernel, 27260Sstevel@tonic-gate char *module) 27270Sstevel@tonic-gate { 27280Sstevel@tonic-gate int lineNum, entryNum; 27290Sstevel@tonic-gate char linebuf[BAM_MAXLINE]; 27300Sstevel@tonic-gate 27310Sstevel@tonic-gate assert(mp); 27320Sstevel@tonic-gate 27330Sstevel@tonic-gate if (title == NULL) { 2734656Sszhou title = "Solaris"; /* default to Solaris */ 27350Sstevel@tonic-gate } 27360Sstevel@tonic-gate if (kernel == NULL) { 27370Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]); 27380Sstevel@tonic-gate return (BAM_ERROR); 27390Sstevel@tonic-gate } 27400Sstevel@tonic-gate if (module == NULL) { 27410Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]); 27420Sstevel@tonic-gate return (BAM_ERROR); 27430Sstevel@tonic-gate } 27440Sstevel@tonic-gate 27450Sstevel@tonic-gate if (mp->start) { 27460Sstevel@tonic-gate lineNum = mp->end->lineNum; 27470Sstevel@tonic-gate entryNum = mp->end->entryNum; 27480Sstevel@tonic-gate } else { 27490Sstevel@tonic-gate lineNum = LINE_INIT; 27500Sstevel@tonic-gate entryNum = ENTRY_INIT; 27510Sstevel@tonic-gate } 27520Sstevel@tonic-gate 27530Sstevel@tonic-gate /* 27540Sstevel@tonic-gate * No separator for comment (HDR/FTR) commands 27550Sstevel@tonic-gate * The syntax for comments is #<comment> 27560Sstevel@tonic-gate */ 27570Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 27580Sstevel@tonic-gate menu_cmds[COMMENT_CMD], BAM_HDR); 2759662Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 27600Sstevel@tonic-gate 27610Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 27620Sstevel@tonic-gate menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title); 2763662Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 2764662Sszhou 2765662Sszhou if (root) { 2766662Sszhou (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 2767662Sszhou menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root); 2768662Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 27690Sstevel@tonic-gate } 27700Sstevel@tonic-gate 27710Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 27720Sstevel@tonic-gate menu_cmds[KERNEL_CMD], menu_cmds[SEP_CMD], kernel); 2773662Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 27740Sstevel@tonic-gate 27750Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 27760Sstevel@tonic-gate menu_cmds[MODULE_CMD], menu_cmds[SEP_CMD], module); 2777662Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 27780Sstevel@tonic-gate 27790Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 27800Sstevel@tonic-gate menu_cmds[COMMENT_CMD], BAM_FTR); 2781662Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 27820Sstevel@tonic-gate 27830Sstevel@tonic-gate return (entryNum); 27840Sstevel@tonic-gate } 27850Sstevel@tonic-gate 27860Sstevel@tonic-gate static error_t 27870Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum) 27880Sstevel@tonic-gate { 2789662Sszhou line_t *lp, *freed; 2790662Sszhou entry_t *ent, *tmp; 27910Sstevel@tonic-gate int deleted; 27920Sstevel@tonic-gate 27930Sstevel@tonic-gate assert(entryNum != ENTRY_INIT); 27940Sstevel@tonic-gate 2795662Sszhou ent = mp->entries; 2796662Sszhou while (ent) { 2797662Sszhou lp = ent->start; 2798662Sszhou /* check entry number and make sure it's a bootadm entry */ 2799662Sszhou if (lp->flags != BAM_COMMENT || 2800662Sszhou strcmp(lp->arg, BAM_HDR) != 0 || 2801662Sszhou (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) { 2802662Sszhou ent = ent->next; 28030Sstevel@tonic-gate continue; 28040Sstevel@tonic-gate } 28050Sstevel@tonic-gate 2806662Sszhou /* free the entry content */ 2807662Sszhou do { 2808662Sszhou freed = lp; 2809662Sszhou lp = lp->next; /* prev stays the same */ 2810662Sszhou unlink_line(mp, freed); 2811662Sszhou line_free(freed); 2812662Sszhou } while (freed != ent->end); 2813662Sszhou 2814662Sszhou /* free the entry_t structure */ 2815662Sszhou tmp = ent; 2816662Sszhou ent = ent->next; 2817662Sszhou if (tmp->prev) 2818662Sszhou tmp->prev->next = ent; 28190Sstevel@tonic-gate else 2820662Sszhou mp->entries = ent; 2821662Sszhou if (ent) 2822662Sszhou ent->prev = tmp->prev; 28230Sstevel@tonic-gate deleted = 1; 28240Sstevel@tonic-gate } 28250Sstevel@tonic-gate 28260Sstevel@tonic-gate if (!deleted && entryNum != ALL_ENTRIES) { 28270Sstevel@tonic-gate bam_error(NO_BOOTADM_MATCH); 28280Sstevel@tonic-gate return (BAM_ERROR); 28290Sstevel@tonic-gate } 28300Sstevel@tonic-gate 2831621Svikram /* 2832621Svikram * Now that we have deleted an entry, update 2833621Svikram * the entry numbering and the default cmd. 2834621Svikram */ 2835621Svikram update_numbering(mp); 2836621Svikram 28370Sstevel@tonic-gate return (BAM_SUCCESS); 28380Sstevel@tonic-gate } 28390Sstevel@tonic-gate 28400Sstevel@tonic-gate static error_t 28410Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt) 28420Sstevel@tonic-gate { 28430Sstevel@tonic-gate assert(mp); 28440Sstevel@tonic-gate assert(opt == NULL); 28450Sstevel@tonic-gate 28460Sstevel@tonic-gate if (mp->start == NULL) { 28470Sstevel@tonic-gate bam_print(EMPTY_FILE, menu_path); 28480Sstevel@tonic-gate return (BAM_SUCCESS); 28490Sstevel@tonic-gate } 28500Sstevel@tonic-gate 28510Sstevel@tonic-gate if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) { 28520Sstevel@tonic-gate return (BAM_ERROR); 28530Sstevel@tonic-gate } 28540Sstevel@tonic-gate 28550Sstevel@tonic-gate return (BAM_WRITE); 28560Sstevel@tonic-gate } 28570Sstevel@tonic-gate 28580Sstevel@tonic-gate static FILE * 2859662Sszhou open_diskmap(char *root) 28600Sstevel@tonic-gate { 28610Sstevel@tonic-gate FILE *fp; 28620Sstevel@tonic-gate char cmd[PATH_MAX]; 28630Sstevel@tonic-gate 28640Sstevel@tonic-gate /* make sure we have a map file */ 28650Sstevel@tonic-gate fp = fopen(GRUBDISK_MAP, "r"); 28660Sstevel@tonic-gate if (fp == NULL) { 28670Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), 2868662Sszhou "%s%s > /dev/null", root, CREATE_DISKMAP); 28690Sstevel@tonic-gate (void) system(cmd); 28700Sstevel@tonic-gate fp = fopen(GRUBDISK_MAP, "r"); 28710Sstevel@tonic-gate } 28720Sstevel@tonic-gate return (fp); 28730Sstevel@tonic-gate } 28740Sstevel@tonic-gate 28750Sstevel@tonic-gate #define SECTOR_SIZE 512 28760Sstevel@tonic-gate 28770Sstevel@tonic-gate static int 28780Sstevel@tonic-gate get_partition(char *device) 28790Sstevel@tonic-gate { 28800Sstevel@tonic-gate int i, fd, is_pcfs, partno = -1; 28810Sstevel@tonic-gate struct mboot *mboot; 28820Sstevel@tonic-gate char boot_sect[SECTOR_SIZE]; 28830Sstevel@tonic-gate char *wholedisk, *slice; 28840Sstevel@tonic-gate 28850Sstevel@tonic-gate /* form whole disk (p0) */ 28860Sstevel@tonic-gate slice = device + strlen(device) - 2; 28870Sstevel@tonic-gate is_pcfs = (*slice != 's'); 28880Sstevel@tonic-gate if (!is_pcfs) 28890Sstevel@tonic-gate *slice = '\0'; 28900Sstevel@tonic-gate wholedisk = s_calloc(1, strlen(device) + 3); 28910Sstevel@tonic-gate (void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device); 28920Sstevel@tonic-gate if (!is_pcfs) 28930Sstevel@tonic-gate *slice = 's'; 28940Sstevel@tonic-gate 28950Sstevel@tonic-gate /* read boot sector */ 28960Sstevel@tonic-gate fd = open(wholedisk, O_RDONLY); 28970Sstevel@tonic-gate free(wholedisk); 28980Sstevel@tonic-gate if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 28990Sstevel@tonic-gate return (partno); 29000Sstevel@tonic-gate } 29010Sstevel@tonic-gate (void) close(fd); 29020Sstevel@tonic-gate 29030Sstevel@tonic-gate /* parse fdisk table */ 29040Sstevel@tonic-gate mboot = (struct mboot *)((void *)boot_sect); 29050Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 29060Sstevel@tonic-gate struct ipart *part = 29070Sstevel@tonic-gate (struct ipart *)(uintptr_t)mboot->parts + i; 29080Sstevel@tonic-gate if (is_pcfs) { /* looking for solaris boot part */ 29090Sstevel@tonic-gate if (part->systid == 0xbe) { 29100Sstevel@tonic-gate partno = i; 29110Sstevel@tonic-gate break; 29120Sstevel@tonic-gate } 29130Sstevel@tonic-gate } else { /* look for solaris partition, old and new */ 29140Sstevel@tonic-gate if (part->systid == SUNIXOS || 29150Sstevel@tonic-gate part->systid == SUNIXOS2) { 29160Sstevel@tonic-gate partno = i; 29170Sstevel@tonic-gate break; 29180Sstevel@tonic-gate } 29190Sstevel@tonic-gate } 29200Sstevel@tonic-gate } 29210Sstevel@tonic-gate return (partno); 29220Sstevel@tonic-gate } 29230Sstevel@tonic-gate 29240Sstevel@tonic-gate static char * 29250Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev) 29260Sstevel@tonic-gate { 29270Sstevel@tonic-gate char *grubdisk; /* (hd#,#,#) */ 29280Sstevel@tonic-gate char *slice; 29290Sstevel@tonic-gate char *grubhd; 29300Sstevel@tonic-gate int fdiskpart; 29310Sstevel@tonic-gate int found = 0; 29320Sstevel@tonic-gate char *devname, *ctdname = strstr(rootdev, "dsk/"); 29330Sstevel@tonic-gate char linebuf[PATH_MAX]; 29340Sstevel@tonic-gate 29350Sstevel@tonic-gate if (ctdname == NULL) 29360Sstevel@tonic-gate return (NULL); 29370Sstevel@tonic-gate 29380Sstevel@tonic-gate ctdname += strlen("dsk/"); 29390Sstevel@tonic-gate slice = strrchr(ctdname, 's'); 29400Sstevel@tonic-gate if (slice) 29410Sstevel@tonic-gate *slice = '\0'; 29420Sstevel@tonic-gate 29430Sstevel@tonic-gate rewind(fp); 29440Sstevel@tonic-gate while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) { 29450Sstevel@tonic-gate grubhd = strtok(linebuf, " \t\n"); 29460Sstevel@tonic-gate if (grubhd) 29470Sstevel@tonic-gate devname = strtok(NULL, " \t\n"); 29480Sstevel@tonic-gate else 29490Sstevel@tonic-gate devname = NULL; 29500Sstevel@tonic-gate if (devname && strcmp(devname, ctdname) == 0) { 29510Sstevel@tonic-gate found = 1; 29520Sstevel@tonic-gate break; 29530Sstevel@tonic-gate } 29540Sstevel@tonic-gate } 29550Sstevel@tonic-gate 29560Sstevel@tonic-gate if (slice) 29570Sstevel@tonic-gate *slice = 's'; 29580Sstevel@tonic-gate 29590Sstevel@tonic-gate if (found == 0) { 29600Sstevel@tonic-gate if (bam_verbose) 29610Sstevel@tonic-gate bam_print(DISKMAP_FAIL_NONFATAL, rootdev); 29620Sstevel@tonic-gate grubhd = "0"; /* assume disk 0 if can't match */ 29630Sstevel@tonic-gate } 29640Sstevel@tonic-gate 29650Sstevel@tonic-gate fdiskpart = get_partition(rootdev); 29660Sstevel@tonic-gate if (fdiskpart == -1) 29670Sstevel@tonic-gate return (NULL); 29680Sstevel@tonic-gate 29690Sstevel@tonic-gate grubdisk = s_calloc(1, 10); 29700Sstevel@tonic-gate if (slice) { 29710Sstevel@tonic-gate (void) snprintf(grubdisk, 10, "(hd%s,%d,%c)", 29720Sstevel@tonic-gate grubhd, fdiskpart, slice[1] + 'a' - '0'); 29730Sstevel@tonic-gate } else 29740Sstevel@tonic-gate (void) snprintf(grubdisk, 10, "(hd%s,%d)", 29750Sstevel@tonic-gate grubhd, fdiskpart); 29760Sstevel@tonic-gate 29770Sstevel@tonic-gate /* if root not on bootdev, change GRUB disk to 0 */ 29780Sstevel@tonic-gate if (!on_bootdev) 29790Sstevel@tonic-gate grubdisk[3] = '0'; 29800Sstevel@tonic-gate return (grubdisk); 29810Sstevel@tonic-gate } 29820Sstevel@tonic-gate 2983656Sszhou static char * 2984656Sszhou get_title(char *rootdir) 29850Sstevel@tonic-gate { 29860Sstevel@tonic-gate static char title[80]; /* from /etc/release */ 2987662Sszhou char *cp = NULL, release[PATH_MAX]; 29880Sstevel@tonic-gate FILE *fp; 29890Sstevel@tonic-gate 29900Sstevel@tonic-gate /* open the /etc/release file */ 29910Sstevel@tonic-gate (void) snprintf(release, sizeof (release), "%s/etc/release", rootdir); 29920Sstevel@tonic-gate 29930Sstevel@tonic-gate fp = fopen(release, "r"); 29940Sstevel@tonic-gate if (fp == NULL) 2995656Sszhou return (NULL); 29960Sstevel@tonic-gate 29970Sstevel@tonic-gate while (s_fgets(title, sizeof (title), fp) != NULL) { 29980Sstevel@tonic-gate cp = strstr(title, "Solaris"); 29990Sstevel@tonic-gate if (cp) 30000Sstevel@tonic-gate break; 30010Sstevel@tonic-gate } 30020Sstevel@tonic-gate (void) fclose(fp); 3003662Sszhou return (cp == NULL ? "Solaris" : cp); 30040Sstevel@tonic-gate } 30050Sstevel@tonic-gate 30060Sstevel@tonic-gate static char * 30070Sstevel@tonic-gate get_special(char *mountp) 30080Sstevel@tonic-gate { 30090Sstevel@tonic-gate FILE *mntfp; 30100Sstevel@tonic-gate struct mnttab mp = {0}, mpref = {0}; 30110Sstevel@tonic-gate 30120Sstevel@tonic-gate mntfp = fopen(MNTTAB, "r"); 30130Sstevel@tonic-gate if (mntfp == NULL) { 30140Sstevel@tonic-gate return (0); 30150Sstevel@tonic-gate } 30160Sstevel@tonic-gate 30170Sstevel@tonic-gate if (*mountp == '\0') 30180Sstevel@tonic-gate mpref.mnt_mountp = "/"; 30190Sstevel@tonic-gate else 30200Sstevel@tonic-gate mpref.mnt_mountp = mountp; 30210Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 30220Sstevel@tonic-gate (void) fclose(mntfp); 30230Sstevel@tonic-gate return (NULL); 30240Sstevel@tonic-gate } 30250Sstevel@tonic-gate (void) fclose(mntfp); 30260Sstevel@tonic-gate 30270Sstevel@tonic-gate return (s_strdup(mp.mnt_special)); 30280Sstevel@tonic-gate } 30290Sstevel@tonic-gate 30300Sstevel@tonic-gate static char * 30310Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev) 30320Sstevel@tonic-gate { 30330Sstevel@tonic-gate FILE *fp; 30340Sstevel@tonic-gate char *grubdisk; 30350Sstevel@tonic-gate 30360Sstevel@tonic-gate /* translate /dev/dsk name to grub disk name */ 3037662Sszhou fp = open_diskmap(""); 30380Sstevel@tonic-gate if (fp == NULL) { 30390Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdisk); 30400Sstevel@tonic-gate return (NULL); 30410Sstevel@tonic-gate } 30420Sstevel@tonic-gate grubdisk = get_grubdisk(osdisk, fp, on_bootdev); 30430Sstevel@tonic-gate (void) fclose(fp); 30440Sstevel@tonic-gate return (grubdisk); 30450Sstevel@tonic-gate } 30460Sstevel@tonic-gate 30470Sstevel@tonic-gate /* 30480Sstevel@tonic-gate * Check if root is on the boot device 30490Sstevel@tonic-gate * Return 0 (false) on error 30500Sstevel@tonic-gate */ 30510Sstevel@tonic-gate static int 30520Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp) 30530Sstevel@tonic-gate { 30540Sstevel@tonic-gate int ret; 30550Sstevel@tonic-gate char *grubhd, *bootp, *special; 30560Sstevel@tonic-gate 30570Sstevel@tonic-gate special = get_special(menu_root); 30580Sstevel@tonic-gate if (special == NULL) 30590Sstevel@tonic-gate return (0); 30600Sstevel@tonic-gate bootp = strstr(special, "p0:boot"); 30610Sstevel@tonic-gate if (bootp) 30620Sstevel@tonic-gate *bootp = '\0'; 30630Sstevel@tonic-gate grubhd = get_grubdisk(special, fp, 1); 30640Sstevel@tonic-gate free(special); 30650Sstevel@tonic-gate 30660Sstevel@tonic-gate if (grubhd == NULL) 30670Sstevel@tonic-gate return (0); 30680Sstevel@tonic-gate ret = grubhd[3] == '0'; 30690Sstevel@tonic-gate free(grubhd); 30700Sstevel@tonic-gate return (ret); 30710Sstevel@tonic-gate } 30720Sstevel@tonic-gate 3073662Sszhou /* 3074662Sszhou * look for matching bootadm entry with specified parameters 3075662Sszhou * Here are the rules (based on existing usage): 3076662Sszhou * - If title is specified, match on title only 3077662Sszhou * - Else, match on grubdisk and module (don't care about kernel line). 3078662Sszhou * note that, if root_opt is non-zero, the absence of root line is 3079662Sszhou * considered a match. 3080662Sszhou */ 3081662Sszhou static entry_t * 3082662Sszhou find_boot_entry(menu_t *mp, char *title, char *root, char *module, 3083662Sszhou int root_opt, int *entry_num) 3084662Sszhou { 3085662Sszhou int i; 3086662Sszhou line_t *lp; 3087662Sszhou entry_t *ent; 3088662Sszhou 3089662Sszhou /* find matching entry */ 3090662Sszhou for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) { 3091662Sszhou lp = ent->start; 3092662Sszhou 3093662Sszhou /* first line of entry must be bootadm comment */ 3094662Sszhou lp = ent->start; 3095662Sszhou if (lp->flags != BAM_COMMENT || strcmp(lp->arg, BAM_HDR) != 0) { 3096662Sszhou continue; 3097662Sszhou } 3098662Sszhou 3099662Sszhou /* advance to title line */ 3100662Sszhou lp = lp->next; 3101662Sszhou if (title) { 3102662Sszhou if (lp->flags == BAM_TITLE && lp->arg && 3103662Sszhou strcmp(lp->arg, title) == 0) 3104662Sszhou break; 3105662Sszhou continue; /* check title only */ 3106662Sszhou } 3107662Sszhou 3108662Sszhou lp = lp->next; /* advance to root line */ 3109662Sszhou if (lp == NULL || strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) { 3110662Sszhou /* root command found, match grub disk */ 3111662Sszhou if (strcmp(lp->arg, root) != 0) { 3112662Sszhou continue; 3113662Sszhou } 3114662Sszhou lp = lp->next; /* advance to kernel line */ 3115662Sszhou } else { 3116662Sszhou /* no root command, see if root is optional */ 3117662Sszhou if (root_opt == 0) { 3118662Sszhou continue; 3119662Sszhou } 3120662Sszhou } 3121662Sszhou 3122662Sszhou if (lp == NULL || lp->next == NULL) { 3123662Sszhou continue; 3124662Sszhou } 3125662Sszhou 3126662Sszhou /* check for matching module entry (failsafe or normal) */ 3127662Sszhou lp = lp->next; /* advance to module line */ 3128662Sszhou if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) != 0 || 3129662Sszhou strcmp(lp->arg, module) != 0) { 3130662Sszhou continue; 3131662Sszhou } 3132662Sszhou break; /* match found */ 3133662Sszhou } 3134662Sszhou 3135662Sszhou *entry_num = i; 3136662Sszhou return (ent); 3137662Sszhou } 3138662Sszhou 3139662Sszhou static int 3140662Sszhou update_boot_entry(menu_t *mp, char *title, char *root, char *kernel, 3141662Sszhou char *module, int root_opt) 3142662Sszhou { 3143662Sszhou int i; 3144662Sszhou entry_t *ent; 3145662Sszhou line_t *lp; 3146662Sszhou char linebuf[BAM_MAXLINE]; 3147662Sszhou 3148662Sszhou /* note: don't match on title, it's updated on upgrade */ 3149662Sszhou ent = find_boot_entry(mp, NULL, root, module, root_opt, &i); 3150662Sszhou if (ent == NULL) 3151662Sszhou return (add_boot_entry(mp, title, root_opt ? NULL : root, 3152662Sszhou kernel, module)); 3153662Sszhou 3154662Sszhou /* replace title of exiting entry and delete root line */ 3155662Sszhou lp = ent->start; 3156662Sszhou lp = lp->next; /* title line */ 3157662Sszhou (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 3158662Sszhou menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title); 3159662Sszhou free(lp->arg); 3160662Sszhou free(lp->line); 3161662Sszhou lp->arg = s_strdup(title); 3162662Sszhou lp->line = s_strdup(linebuf); 3163662Sszhou 3164662Sszhou lp = lp->next; /* root line */ 3165662Sszhou if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) { 3166662Sszhou if (root_opt) { /* root line not needed */ 3167662Sszhou line_t *tmp = lp; 3168662Sszhou lp = lp->next; 3169662Sszhou unlink_line(mp, tmp); 3170662Sszhou line_free(tmp); 3171662Sszhou } else 3172662Sszhou lp = lp->next; 3173662Sszhou } 3174662Sszhou return (i); 3175662Sszhou } 3176662Sszhou 31770Sstevel@tonic-gate /*ARGSUSED*/ 31780Sstevel@tonic-gate static error_t 31790Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt) 31800Sstevel@tonic-gate { 31810Sstevel@tonic-gate FILE *fp; 31820Sstevel@tonic-gate int entry; 31830Sstevel@tonic-gate char *grubdisk, *title, *osdev, *osroot; 3184662Sszhou struct stat sbuf; 3185662Sszhou char failsafe[256]; 31860Sstevel@tonic-gate 31870Sstevel@tonic-gate assert(mp); 31880Sstevel@tonic-gate assert(opt); 31890Sstevel@tonic-gate 31900Sstevel@tonic-gate osdev = strtok(opt, ","); 31910Sstevel@tonic-gate osroot = strtok(NULL, ","); 31920Sstevel@tonic-gate if (osroot == NULL) 31930Sstevel@tonic-gate osroot = menu_root; 31940Sstevel@tonic-gate title = get_title(osroot); 31950Sstevel@tonic-gate 31960Sstevel@tonic-gate /* translate /dev/dsk name to grub disk name */ 3197662Sszhou fp = open_diskmap(osroot); 31980Sstevel@tonic-gate if (fp == NULL) { 31990Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdev); 32000Sstevel@tonic-gate return (BAM_ERROR); 32010Sstevel@tonic-gate } 32020Sstevel@tonic-gate grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp)); 32030Sstevel@tonic-gate (void) fclose(fp); 32040Sstevel@tonic-gate if (grubdisk == NULL) { 32050Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdev); 32060Sstevel@tonic-gate return (BAM_ERROR); 32070Sstevel@tonic-gate } 32080Sstevel@tonic-gate 32090Sstevel@tonic-gate /* add the entry for normal Solaris */ 3210662Sszhou entry = update_boot_entry(mp, title, grubdisk, 32110Sstevel@tonic-gate "/platform/i86pc/multiboot", 3212662Sszhou "/platform/i86pc/boot_archive", 3213662Sszhou osroot == menu_root); 32140Sstevel@tonic-gate 32150Sstevel@tonic-gate /* add the entry for failsafe archive */ 3216662Sszhou (void) snprintf(failsafe, sizeof (failsafe), 3217662Sszhou "%s/boot/x86.miniroot-safe", osroot); 3218662Sszhou if (stat(failsafe, &sbuf) == 0) 3219662Sszhou (void) update_boot_entry(mp, "Solaris failsafe", grubdisk, 3220662Sszhou "/boot/multiboot kernel/unix -s", 3221662Sszhou "/boot/x86.miniroot-safe", 3222662Sszhou osroot == menu_root); 32230Sstevel@tonic-gate free(grubdisk); 32240Sstevel@tonic-gate 32250Sstevel@tonic-gate if (entry == BAM_ERROR) { 32260Sstevel@tonic-gate return (BAM_ERROR); 32270Sstevel@tonic-gate } 32280Sstevel@tonic-gate (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 32290Sstevel@tonic-gate return (BAM_WRITE); 32300Sstevel@tonic-gate } 32310Sstevel@tonic-gate 3232316Svikram static char * 3233316Svikram read_grub_root(void) 3234316Svikram { 3235316Svikram FILE *fp; 3236316Svikram struct stat sb; 3237316Svikram char buf[BAM_MAXLINE]; 3238316Svikram char *rootstr; 3239316Svikram 3240316Svikram if (stat(GRUB_slice, &sb) != 0) { 3241316Svikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 3242316Svikram return (NULL); 3243316Svikram } 3244316Svikram 3245316Svikram if (stat(GRUB_root, &sb) != 0) { 3246316Svikram bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno)); 3247316Svikram return (NULL); 3248316Svikram } 3249316Svikram 3250316Svikram fp = fopen(GRUB_root, "r"); 3251316Svikram if (fp == NULL) { 3252316Svikram bam_error(OPEN_FAIL, GRUB_root, strerror(errno)); 3253316Svikram return (NULL); 3254316Svikram } 3255316Svikram 3256316Svikram if (s_fgets(buf, sizeof (buf), fp) == NULL) { 3257316Svikram bam_error(EMPTY_FILE, GRUB_root, strerror(errno)); 3258316Svikram (void) fclose(fp); 3259316Svikram return (NULL); 3260316Svikram } 3261316Svikram 3262316Svikram /* 3263316Svikram * Copy buf here as check below may trash the buffer 3264316Svikram */ 3265316Svikram rootstr = s_strdup(buf); 3266316Svikram 3267316Svikram if (s_fgets(buf, sizeof (buf), fp) != NULL) { 3268316Svikram bam_error(BAD_ROOT_FILE, GRUB_root); 3269316Svikram free(rootstr); 3270316Svikram rootstr = NULL; 3271316Svikram } 3272316Svikram 3273316Svikram (void) fclose(fp); 3274316Svikram 3275316Svikram return (rootstr); 3276316Svikram } 3277316Svikram 3278662Sszhou static void 3279662Sszhou save_default_entry(menu_t *mp) 3280662Sszhou { 3281662Sszhou int lineNum, entryNum; 3282662Sszhou int entry = 0; /* default is 0 */ 3283662Sszhou char linebuf[BAM_MAXLINE]; 3284662Sszhou line_t *lp = mp->curdefault; 3285662Sszhou 3286662Sszhou if (lp) 3287662Sszhou entry = s_strtol(lp->arg); 3288662Sszhou 3289662Sszhou (void) snprintf(linebuf, sizeof (linebuf), "#%s%d", BAM_OLDDEF, entry); 3290662Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 3291662Sszhou } 3292662Sszhou 3293662Sszhou static void 3294662Sszhou restore_default_entry(menu_t *mp) 3295662Sszhou { 3296662Sszhou int entry; 3297662Sszhou char *str; 3298662Sszhou line_t *lp = mp->olddefault; 3299662Sszhou 3300662Sszhou if (lp == NULL) 3301662Sszhou return; /* nothing to restore */ 3302662Sszhou 3303662Sszhou str = lp->arg + strlen(BAM_OLDDEF); 3304662Sszhou entry = s_strtol(str); 3305662Sszhou (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 3306662Sszhou 3307662Sszhou /* delete saved old default line */ 3308662Sszhou mp->olddefault = NULL; 3309662Sszhou unlink_line(mp, lp); 3310662Sszhou line_free(lp); 3311662Sszhou } 3312662Sszhou 33130Sstevel@tonic-gate /* 33140Sstevel@tonic-gate * This function is for supporting reboot with args. 33150Sstevel@tonic-gate * The opt value can be: 33160Sstevel@tonic-gate * NULL delete temp entry, if present 33170Sstevel@tonic-gate * entry=# switches default entry to 1 33180Sstevel@tonic-gate * else treated as boot-args and setup a temperary menu entry 33190Sstevel@tonic-gate * and make it the default 33200Sstevel@tonic-gate */ 33210Sstevel@tonic-gate #define REBOOT_TITLE "Solaris_reboot_transient" 33220Sstevel@tonic-gate 3323662Sszhou /*ARGSUSED*/ 33240Sstevel@tonic-gate static error_t 33250Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt) 33260Sstevel@tonic-gate { 33270Sstevel@tonic-gate int entry; 33280Sstevel@tonic-gate char *grubdisk, *rootdev; 33290Sstevel@tonic-gate char kernbuf[1024]; 3330316Svikram struct stat sb; 33310Sstevel@tonic-gate 33320Sstevel@tonic-gate assert(mp); 33330Sstevel@tonic-gate 3334662Sszhou /* If no option, delete exiting reboot menu entry */ 3335662Sszhou if (opt == NULL) { 3336662Sszhou entry_t *ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL, 3337662Sszhou 0, &entry); 3338662Sszhou if (ent == NULL) /* not found is ok */ 3339662Sszhou return (BAM_SUCCESS); 3340662Sszhou (void) do_delete(mp, entry); 3341662Sszhou restore_default_entry(mp); 3342662Sszhou return (BAM_WRITE); 3343662Sszhou } 3344662Sszhou 3345662Sszhou /* if entry= is specified, set the default entry */ 3346662Sszhou if (strncmp(opt, "entry=", strlen("entry=")) == 0 && 33470Sstevel@tonic-gate selector(mp, opt, &entry, NULL) == BAM_SUCCESS) { 33480Sstevel@tonic-gate /* this is entry=# option */ 33490Sstevel@tonic-gate return (set_global(mp, menu_cmds[DEFAULT_CMD], entry)); 33500Sstevel@tonic-gate } 33510Sstevel@tonic-gate 33520Sstevel@tonic-gate /* 33530Sstevel@tonic-gate * add a new menu entry base on opt and make it the default 33540Sstevel@tonic-gate */ 3355316Svikram grubdisk = NULL; 3356316Svikram if (stat(GRUB_slice, &sb) != 0) { 3357316Svikram /* 3358316Svikram * 1. First get root disk name from mnttab 3359316Svikram * 2. Translate disk name to grub name 3360316Svikram * 3. Add the new menu entry 3361316Svikram */ 3362316Svikram rootdev = get_special("/"); 3363316Svikram if (rootdev) { 3364316Svikram grubdisk = os_to_grubdisk(rootdev, 1); 3365316Svikram free(rootdev); 3366316Svikram } 3367316Svikram } else { 3368316Svikram /* 3369316Svikram * This is an LU BE. The GRUB_root file 3370316Svikram * contains entry for GRUB's "root" cmd. 3371316Svikram */ 3372316Svikram grubdisk = read_grub_root(); 33730Sstevel@tonic-gate } 33740Sstevel@tonic-gate if (grubdisk == NULL) { 3375316Svikram bam_error(REBOOT_WITH_ARGS_FAILED); 33760Sstevel@tonic-gate return (BAM_ERROR); 33770Sstevel@tonic-gate } 33780Sstevel@tonic-gate 33790Sstevel@tonic-gate /* add an entry for Solaris reboot */ 33800Sstevel@tonic-gate (void) snprintf(kernbuf, sizeof (kernbuf), 33810Sstevel@tonic-gate "/platform/i86pc/multiboot %s", opt); 33820Sstevel@tonic-gate entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf, 33830Sstevel@tonic-gate "/platform/i86pc/boot_archive"); 33840Sstevel@tonic-gate free(grubdisk); 33850Sstevel@tonic-gate 33860Sstevel@tonic-gate if (entry == BAM_ERROR) { 3387316Svikram bam_error(REBOOT_WITH_ARGS_FAILED); 33880Sstevel@tonic-gate return (BAM_ERROR); 33890Sstevel@tonic-gate } 3390662Sszhou 3391662Sszhou save_default_entry(mp); 33920Sstevel@tonic-gate (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 33930Sstevel@tonic-gate return (BAM_WRITE); 33940Sstevel@tonic-gate } 33950Sstevel@tonic-gate 33960Sstevel@tonic-gate static error_t 33970Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val) 33980Sstevel@tonic-gate { 33990Sstevel@tonic-gate line_t *lp, *found, *last; 34000Sstevel@tonic-gate char *cp, *str; 34010Sstevel@tonic-gate char prefix[BAM_MAXLINE]; 34020Sstevel@tonic-gate size_t len; 34030Sstevel@tonic-gate 34040Sstevel@tonic-gate assert(mp); 34050Sstevel@tonic-gate assert(globalcmd); 34060Sstevel@tonic-gate 3407316Svikram if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) { 3408316Svikram if (val < 0 || mp->end == NULL || val > mp->end->entryNum) { 3409316Svikram (void) snprintf(prefix, sizeof (prefix), "%d", val); 3410316Svikram bam_error(INVALID_ENTRY, prefix); 3411316Svikram return (BAM_ERROR); 3412316Svikram } 3413316Svikram } 3414316Svikram 34150Sstevel@tonic-gate found = last = NULL; 34160Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 34170Sstevel@tonic-gate if (lp->flags != BAM_GLOBAL) 34180Sstevel@tonic-gate continue; 34190Sstevel@tonic-gate 34200Sstevel@tonic-gate last = lp; /* track the last global found */ 34210Sstevel@tonic-gate 34220Sstevel@tonic-gate if (lp->cmd == NULL) { 34230Sstevel@tonic-gate bam_error(NO_CMD, lp->lineNum); 34240Sstevel@tonic-gate continue; 34250Sstevel@tonic-gate } 34260Sstevel@tonic-gate if (strcmp(globalcmd, lp->cmd) != 0) 34270Sstevel@tonic-gate continue; 34280Sstevel@tonic-gate 34290Sstevel@tonic-gate if (found) { 34300Sstevel@tonic-gate bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 34310Sstevel@tonic-gate } 34320Sstevel@tonic-gate found = lp; 34330Sstevel@tonic-gate } 34340Sstevel@tonic-gate 34350Sstevel@tonic-gate if (found == NULL) { 34360Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 34370Sstevel@tonic-gate if (last == NULL) { 34380Sstevel@tonic-gate lp->next = mp->start; 34390Sstevel@tonic-gate mp->start = lp; 34400Sstevel@tonic-gate mp->end = (mp->end) ? mp->end : lp; 34410Sstevel@tonic-gate } else { 34420Sstevel@tonic-gate lp->next = last->next; 34430Sstevel@tonic-gate last->next = lp; 34440Sstevel@tonic-gate if (lp->next == NULL) 34450Sstevel@tonic-gate mp->end = lp; 34460Sstevel@tonic-gate } 34470Sstevel@tonic-gate lp->flags = BAM_GLOBAL; /* other fields not needed for writes */ 34480Sstevel@tonic-gate len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]); 34490Sstevel@tonic-gate len += 10; /* val < 10 digits */ 34500Sstevel@tonic-gate lp->line = s_calloc(1, len); 34510Sstevel@tonic-gate (void) snprintf(lp->line, len, "%s%s%d", 34520Sstevel@tonic-gate globalcmd, menu_cmds[SEP_CMD], val); 34530Sstevel@tonic-gate return (BAM_WRITE); 34540Sstevel@tonic-gate } 34550Sstevel@tonic-gate 34560Sstevel@tonic-gate /* 34570Sstevel@tonic-gate * We are changing an existing entry. Retain any prefix whitespace, 34580Sstevel@tonic-gate * but overwrite everything else. This preserves tabs added for 34590Sstevel@tonic-gate * readability. 34600Sstevel@tonic-gate */ 34610Sstevel@tonic-gate str = found->line; 34620Sstevel@tonic-gate cp = prefix; 34630Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 34640Sstevel@tonic-gate *(cp++) = *(str++); 34650Sstevel@tonic-gate *cp = '\0'; /* Terminate prefix */ 34660Sstevel@tonic-gate len = strlen(prefix) + strlen(globalcmd); 34670Sstevel@tonic-gate len += strlen(menu_cmds[SEP_CMD]) + 10; 34680Sstevel@tonic-gate 34690Sstevel@tonic-gate free(found->line); 34700Sstevel@tonic-gate found->line = s_calloc(1, len); 34710Sstevel@tonic-gate (void) snprintf(found->line, len, 34720Sstevel@tonic-gate "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val); 34730Sstevel@tonic-gate 34740Sstevel@tonic-gate return (BAM_WRITE); /* need a write to menu */ 34750Sstevel@tonic-gate } 34760Sstevel@tonic-gate 34770Sstevel@tonic-gate /*ARGSUSED*/ 34780Sstevel@tonic-gate static error_t 34790Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt) 34800Sstevel@tonic-gate { 34810Sstevel@tonic-gate int optnum, optval; 34820Sstevel@tonic-gate char *val; 34830Sstevel@tonic-gate 34840Sstevel@tonic-gate assert(mp); 34850Sstevel@tonic-gate assert(opt); 34860Sstevel@tonic-gate 34870Sstevel@tonic-gate val = strchr(opt, '='); 34880Sstevel@tonic-gate if (val == NULL) { 34890Sstevel@tonic-gate bam_error(INVALID_ENTRY, opt); 34900Sstevel@tonic-gate return (BAM_ERROR); 34910Sstevel@tonic-gate } 34920Sstevel@tonic-gate 34930Sstevel@tonic-gate *val = '\0'; 34940Sstevel@tonic-gate if (strcmp(opt, "default") == 0) { 34950Sstevel@tonic-gate optnum = DEFAULT_CMD; 34960Sstevel@tonic-gate } else if (strcmp(opt, "timeout") == 0) { 34970Sstevel@tonic-gate optnum = TIMEOUT_CMD; 34980Sstevel@tonic-gate } else { 34990Sstevel@tonic-gate bam_error(INVALID_ENTRY, opt); 35000Sstevel@tonic-gate return (BAM_ERROR); 35010Sstevel@tonic-gate } 35020Sstevel@tonic-gate 35030Sstevel@tonic-gate optval = s_strtol(val + 1); 35040Sstevel@tonic-gate *val = '='; 35050Sstevel@tonic-gate return (set_global(mp, menu_cmds[optnum], optval)); 35060Sstevel@tonic-gate } 35070Sstevel@tonic-gate 35080Sstevel@tonic-gate /* 35090Sstevel@tonic-gate * The quiet argument suppresses messages. This is used 35100Sstevel@tonic-gate * when invoked in the context of other commands (e.g. list_entry) 35110Sstevel@tonic-gate */ 35120Sstevel@tonic-gate static error_t 35130Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet) 35140Sstevel@tonic-gate { 35150Sstevel@tonic-gate line_t *lp; 35160Sstevel@tonic-gate char *arg; 35170Sstevel@tonic-gate int done, ret = BAM_SUCCESS; 35180Sstevel@tonic-gate 35190Sstevel@tonic-gate assert(mp); 35200Sstevel@tonic-gate assert(menu_path); 35210Sstevel@tonic-gate assert(globalcmd); 35220Sstevel@tonic-gate 35230Sstevel@tonic-gate if (mp->start == NULL) { 35240Sstevel@tonic-gate if (!quiet) 35250Sstevel@tonic-gate bam_error(NO_MENU, menu_path); 35260Sstevel@tonic-gate return (BAM_ERROR); 35270Sstevel@tonic-gate } 35280Sstevel@tonic-gate 35290Sstevel@tonic-gate done = 0; 35300Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 35310Sstevel@tonic-gate if (lp->flags != BAM_GLOBAL) 35320Sstevel@tonic-gate continue; 35330Sstevel@tonic-gate 35340Sstevel@tonic-gate if (lp->cmd == NULL) { 35350Sstevel@tonic-gate if (!quiet) 35360Sstevel@tonic-gate bam_error(NO_CMD, lp->lineNum); 35370Sstevel@tonic-gate continue; 35380Sstevel@tonic-gate } 35390Sstevel@tonic-gate 35400Sstevel@tonic-gate if (strcmp(globalcmd, lp->cmd) != 0) 35410Sstevel@tonic-gate continue; 35420Sstevel@tonic-gate 35430Sstevel@tonic-gate /* Found global. Check for duplicates */ 35440Sstevel@tonic-gate if (done && !quiet) { 35450Sstevel@tonic-gate bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 35460Sstevel@tonic-gate ret = BAM_ERROR; 35470Sstevel@tonic-gate } 35480Sstevel@tonic-gate 35490Sstevel@tonic-gate arg = lp->arg ? lp->arg : ""; 35500Sstevel@tonic-gate bam_print(GLOBAL_CMD, globalcmd, arg); 35510Sstevel@tonic-gate done = 1; 35520Sstevel@tonic-gate } 35530Sstevel@tonic-gate 35540Sstevel@tonic-gate if (!done && bam_verbose) 35550Sstevel@tonic-gate bam_print(NO_ENTRY, globalcmd); 35560Sstevel@tonic-gate 35570Sstevel@tonic-gate return (ret); 35580Sstevel@tonic-gate } 35590Sstevel@tonic-gate 35600Sstevel@tonic-gate static error_t 35610Sstevel@tonic-gate menu_write(char *root, menu_t *mp) 35620Sstevel@tonic-gate { 35630Sstevel@tonic-gate return (list2file(root, MENU_TMP, GRUB_MENU, mp->start)); 35640Sstevel@tonic-gate } 35650Sstevel@tonic-gate 35660Sstevel@tonic-gate static void 35670Sstevel@tonic-gate line_free(line_t *lp) 35680Sstevel@tonic-gate { 35690Sstevel@tonic-gate if (lp == NULL) 35700Sstevel@tonic-gate return; 35710Sstevel@tonic-gate 35720Sstevel@tonic-gate if (lp->cmd) 35730Sstevel@tonic-gate free(lp->cmd); 35740Sstevel@tonic-gate if (lp->sep) 35750Sstevel@tonic-gate free(lp->sep); 35760Sstevel@tonic-gate if (lp->arg) 35770Sstevel@tonic-gate free(lp->arg); 35780Sstevel@tonic-gate if (lp->line) 35790Sstevel@tonic-gate free(lp->line); 35800Sstevel@tonic-gate free(lp); 35810Sstevel@tonic-gate } 35820Sstevel@tonic-gate 35830Sstevel@tonic-gate static void 35840Sstevel@tonic-gate linelist_free(line_t *start) 35850Sstevel@tonic-gate { 35860Sstevel@tonic-gate line_t *lp; 35870Sstevel@tonic-gate 35880Sstevel@tonic-gate while (start) { 35890Sstevel@tonic-gate lp = start; 35900Sstevel@tonic-gate start = start->next; 35910Sstevel@tonic-gate line_free(lp); 35920Sstevel@tonic-gate } 35930Sstevel@tonic-gate } 35940Sstevel@tonic-gate 35950Sstevel@tonic-gate static void 35960Sstevel@tonic-gate filelist_free(filelist_t *flistp) 35970Sstevel@tonic-gate { 35980Sstevel@tonic-gate linelist_free(flistp->head); 35990Sstevel@tonic-gate flistp->head = NULL; 36000Sstevel@tonic-gate flistp->tail = NULL; 36010Sstevel@tonic-gate } 36020Sstevel@tonic-gate 36030Sstevel@tonic-gate static void 36040Sstevel@tonic-gate menu_free(menu_t *mp) 36050Sstevel@tonic-gate { 3606662Sszhou entry_t *ent, *tmp; 36070Sstevel@tonic-gate assert(mp); 36080Sstevel@tonic-gate 36090Sstevel@tonic-gate if (mp->start) 36100Sstevel@tonic-gate linelist_free(mp->start); 3611662Sszhou ent = mp->entries; 3612662Sszhou while (ent) { 3613662Sszhou tmp = ent; 3614662Sszhou ent = tmp->next; 3615662Sszhou free(tmp); 3616662Sszhou } 3617662Sszhou 36180Sstevel@tonic-gate free(mp); 36190Sstevel@tonic-gate } 36200Sstevel@tonic-gate 36210Sstevel@tonic-gate /* 36220Sstevel@tonic-gate * Utility routines 36230Sstevel@tonic-gate */ 36240Sstevel@tonic-gate 36250Sstevel@tonic-gate 36260Sstevel@tonic-gate /* 36270Sstevel@tonic-gate * Returns 0 on success 36280Sstevel@tonic-gate * Any other value indicates an error 36290Sstevel@tonic-gate */ 36300Sstevel@tonic-gate static int 36310Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize) 36320Sstevel@tonic-gate { 36330Sstevel@tonic-gate char buf[BUFSIZ]; 36340Sstevel@tonic-gate int ret; 36350Sstevel@tonic-gate FILE *ptr; 36360Sstevel@tonic-gate size_t len; 36370Sstevel@tonic-gate sigset_t set; 36380Sstevel@tonic-gate void (*disp)(int); 36390Sstevel@tonic-gate 36400Sstevel@tonic-gate /* 36410Sstevel@tonic-gate * For security 36420Sstevel@tonic-gate * - only absolute paths are allowed 36430Sstevel@tonic-gate * - set IFS to space and tab 36440Sstevel@tonic-gate */ 36450Sstevel@tonic-gate if (*cmdline != '/') { 36460Sstevel@tonic-gate bam_error(ABS_PATH_REQ, cmdline); 36470Sstevel@tonic-gate return (-1); 36480Sstevel@tonic-gate } 36490Sstevel@tonic-gate (void) putenv("IFS= \t"); 36500Sstevel@tonic-gate 36510Sstevel@tonic-gate /* 36520Sstevel@tonic-gate * We may have been exec'ed with SIGCHLD blocked 36530Sstevel@tonic-gate * unblock it here 36540Sstevel@tonic-gate */ 36550Sstevel@tonic-gate (void) sigemptyset(&set); 36560Sstevel@tonic-gate (void) sigaddset(&set, SIGCHLD); 36570Sstevel@tonic-gate if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { 36580Sstevel@tonic-gate bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno)); 36590Sstevel@tonic-gate return (-1); 36600Sstevel@tonic-gate } 36610Sstevel@tonic-gate 36620Sstevel@tonic-gate /* 36630Sstevel@tonic-gate * Set SIGCHLD disposition to SIG_DFL for popen/pclose 36640Sstevel@tonic-gate */ 36650Sstevel@tonic-gate disp = sigset(SIGCHLD, SIG_DFL); 36660Sstevel@tonic-gate if (disp == SIG_ERR) { 36670Sstevel@tonic-gate bam_error(FAILED_SIG, strerror(errno)); 36680Sstevel@tonic-gate return (-1); 36690Sstevel@tonic-gate } 36700Sstevel@tonic-gate if (disp == SIG_HOLD) { 36710Sstevel@tonic-gate bam_error(BLOCKED_SIG, cmdline); 36720Sstevel@tonic-gate return (-1); 36730Sstevel@tonic-gate } 36740Sstevel@tonic-gate 36750Sstevel@tonic-gate ptr = popen(cmdline, "r"); 36760Sstevel@tonic-gate if (ptr == NULL) { 36770Sstevel@tonic-gate bam_error(POPEN_FAIL, cmdline, strerror(errno)); 36780Sstevel@tonic-gate return (-1); 36790Sstevel@tonic-gate } 36800Sstevel@tonic-gate 36810Sstevel@tonic-gate /* 36820Sstevel@tonic-gate * If we simply do a pclose() following a popen(), pclose() 36830Sstevel@tonic-gate * will close the reader end of the pipe immediately even 36840Sstevel@tonic-gate * if the child process has not started/exited. pclose() 36850Sstevel@tonic-gate * does wait for cmd to terminate before returning though. 36860Sstevel@tonic-gate * When the executed command writes its output to the pipe 36870Sstevel@tonic-gate * there is no reader process and the command dies with 36880Sstevel@tonic-gate * SIGPIPE. To avoid this we read repeatedly until read 36890Sstevel@tonic-gate * terminates with EOF. This indicates that the command 36900Sstevel@tonic-gate * (writer) has closed the pipe and we can safely do a 36910Sstevel@tonic-gate * pclose(). 36920Sstevel@tonic-gate * 36930Sstevel@tonic-gate * Since pclose() does wait for the command to exit, 36940Sstevel@tonic-gate * we can safely reap the exit status of the command 36950Sstevel@tonic-gate * from the value returned by pclose() 36960Sstevel@tonic-gate */ 36970Sstevel@tonic-gate while (fgets(buf, sizeof (buf), ptr) != NULL) { 36980Sstevel@tonic-gate /* if (bam_verbose) XXX */ 36990Sstevel@tonic-gate bam_print(PRINT_NO_NEWLINE, buf); 37000Sstevel@tonic-gate if (output && osize > 0) { 37010Sstevel@tonic-gate (void) snprintf(output, osize, "%s", buf); 37020Sstevel@tonic-gate len = strlen(buf); 37030Sstevel@tonic-gate output += len; 37040Sstevel@tonic-gate osize -= len; 37050Sstevel@tonic-gate } 37060Sstevel@tonic-gate } 37070Sstevel@tonic-gate 37080Sstevel@tonic-gate ret = pclose(ptr); 37090Sstevel@tonic-gate if (ret == -1) { 37100Sstevel@tonic-gate bam_error(PCLOSE_FAIL, cmdline, strerror(errno)); 37110Sstevel@tonic-gate return (-1); 37120Sstevel@tonic-gate } 37130Sstevel@tonic-gate 37140Sstevel@tonic-gate if (WIFEXITED(ret)) { 37150Sstevel@tonic-gate return (WEXITSTATUS(ret)); 37160Sstevel@tonic-gate } else { 37170Sstevel@tonic-gate bam_error(EXEC_FAIL, cmdline, ret); 37180Sstevel@tonic-gate return (-1); 37190Sstevel@tonic-gate } 37200Sstevel@tonic-gate } 37210Sstevel@tonic-gate 37220Sstevel@tonic-gate /* 37230Sstevel@tonic-gate * Since this function returns -1 on error 37240Sstevel@tonic-gate * it cannot be used to convert -1. However, 37250Sstevel@tonic-gate * that is sufficient for what we need. 37260Sstevel@tonic-gate */ 37270Sstevel@tonic-gate static long 37280Sstevel@tonic-gate s_strtol(char *str) 37290Sstevel@tonic-gate { 37300Sstevel@tonic-gate long l; 37310Sstevel@tonic-gate char *res = NULL; 37320Sstevel@tonic-gate 37330Sstevel@tonic-gate if (str == NULL) { 37340Sstevel@tonic-gate return (-1); 37350Sstevel@tonic-gate } 37360Sstevel@tonic-gate 37370Sstevel@tonic-gate errno = 0; 37380Sstevel@tonic-gate l = strtol(str, &res, 10); 37390Sstevel@tonic-gate if (errno || *res != '\0') { 37400Sstevel@tonic-gate return (-1); 37410Sstevel@tonic-gate } 37420Sstevel@tonic-gate 37430Sstevel@tonic-gate return (l); 37440Sstevel@tonic-gate } 37450Sstevel@tonic-gate 37460Sstevel@tonic-gate /* 37470Sstevel@tonic-gate * Wrapper around fputs, that adds a newline (since fputs doesn't) 37480Sstevel@tonic-gate */ 37490Sstevel@tonic-gate static int 37500Sstevel@tonic-gate s_fputs(char *str, FILE *fp) 37510Sstevel@tonic-gate { 37520Sstevel@tonic-gate char linebuf[BAM_MAXLINE]; 37530Sstevel@tonic-gate 37540Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s\n", str); 37550Sstevel@tonic-gate return (fputs(linebuf, fp)); 37560Sstevel@tonic-gate } 37570Sstevel@tonic-gate 37580Sstevel@tonic-gate /* 37590Sstevel@tonic-gate * Wrapper around fgets, that strips newlines returned by fgets 37600Sstevel@tonic-gate */ 37610Sstevel@tonic-gate static char * 37620Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp) 37630Sstevel@tonic-gate { 37640Sstevel@tonic-gate int n; 37650Sstevel@tonic-gate 37660Sstevel@tonic-gate buf = fgets(buf, buflen, fp); 37670Sstevel@tonic-gate if (buf) { 37680Sstevel@tonic-gate n = strlen(buf); 37690Sstevel@tonic-gate if (n == buflen - 1 && buf[n-1] != '\n') 37700Sstevel@tonic-gate bam_error(TOO_LONG, buflen - 1, buf); 37710Sstevel@tonic-gate buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1]; 37720Sstevel@tonic-gate } 37730Sstevel@tonic-gate 37740Sstevel@tonic-gate return (buf); 37750Sstevel@tonic-gate } 37760Sstevel@tonic-gate 37770Sstevel@tonic-gate static void * 37780Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz) 37790Sstevel@tonic-gate { 37800Sstevel@tonic-gate void *ptr; 37810Sstevel@tonic-gate 37820Sstevel@tonic-gate ptr = calloc(nelem, sz); 37830Sstevel@tonic-gate if (ptr == NULL) { 37840Sstevel@tonic-gate bam_error(NO_MEM, nelem*sz); 37850Sstevel@tonic-gate bam_exit(1); 37860Sstevel@tonic-gate } 37870Sstevel@tonic-gate return (ptr); 37880Sstevel@tonic-gate } 37890Sstevel@tonic-gate 37900Sstevel@tonic-gate static char * 37910Sstevel@tonic-gate s_strdup(char *str) 37920Sstevel@tonic-gate { 37930Sstevel@tonic-gate char *ptr; 37940Sstevel@tonic-gate 37950Sstevel@tonic-gate if (str == NULL) 37960Sstevel@tonic-gate return (NULL); 37970Sstevel@tonic-gate 37980Sstevel@tonic-gate ptr = strdup(str); 37990Sstevel@tonic-gate if (ptr == NULL) { 38000Sstevel@tonic-gate bam_error(NO_MEM, strlen(str) + 1); 38010Sstevel@tonic-gate bam_exit(1); 38020Sstevel@tonic-gate } 38030Sstevel@tonic-gate return (ptr); 38040Sstevel@tonic-gate } 38050Sstevel@tonic-gate 38060Sstevel@tonic-gate /* 38070Sstevel@tonic-gate * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients) 38080Sstevel@tonic-gate * Returns 0 otherwise 38090Sstevel@tonic-gate */ 38100Sstevel@tonic-gate static int 38110Sstevel@tonic-gate is_amd64(void) 38120Sstevel@tonic-gate { 38130Sstevel@tonic-gate static int amd64 = -1; 38140Sstevel@tonic-gate char isabuf[257]; /* from sysinfo(2) manpage */ 38150Sstevel@tonic-gate 38160Sstevel@tonic-gate if (amd64 != -1) 38170Sstevel@tonic-gate return (amd64); 38180Sstevel@tonic-gate 38190Sstevel@tonic-gate if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 && 38200Sstevel@tonic-gate strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) 38210Sstevel@tonic-gate amd64 = 1; 38220Sstevel@tonic-gate else if (strstr(isabuf, "i386") == NULL) 38230Sstevel@tonic-gate amd64 = 1; /* diskless server */ 38240Sstevel@tonic-gate else 38250Sstevel@tonic-gate amd64 = 0; 38260Sstevel@tonic-gate 38270Sstevel@tonic-gate return (amd64); 38280Sstevel@tonic-gate } 38290Sstevel@tonic-gate 38300Sstevel@tonic-gate static void 38310Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s) 38320Sstevel@tonic-gate { 38330Sstevel@tonic-gate line_t *lp; 38340Sstevel@tonic-gate 38350Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 38360Sstevel@tonic-gate lp->line = s_strdup(s); 38370Sstevel@tonic-gate if (flistp->head == NULL) 38380Sstevel@tonic-gate flistp->head = lp; 38390Sstevel@tonic-gate else 38400Sstevel@tonic-gate flistp->tail->next = lp; 38410Sstevel@tonic-gate flistp->tail = lp; 38420Sstevel@tonic-gate } 3843