17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * bootadm(1M) is a new utility for managing bootability of 317c478bd9Sstevel@tonic-gate * Solaris *Newboot* environments. It has two primary tasks: 327c478bd9Sstevel@tonic-gate * - Allow end users to manage bootability of Newboot Solaris instances 337c478bd9Sstevel@tonic-gate * - Provide services to other subsystems in Solaris (primarily Install) 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate /* Headers */ 377c478bd9Sstevel@tonic-gate #include <stdio.h> 387c478bd9Sstevel@tonic-gate #include <errno.h> 397c478bd9Sstevel@tonic-gate #include <stdlib.h> 407c478bd9Sstevel@tonic-gate #include <string.h> 417c478bd9Sstevel@tonic-gate #include <unistd.h> 427c478bd9Sstevel@tonic-gate #include <sys/types.h> 437c478bd9Sstevel@tonic-gate #include <sys/stat.h> 447c478bd9Sstevel@tonic-gate #include <stdarg.h> 457c478bd9Sstevel@tonic-gate #include <limits.h> 467c478bd9Sstevel@tonic-gate #include <signal.h> 477c478bd9Sstevel@tonic-gate #include <sys/wait.h> 487c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 497c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 507c478bd9Sstevel@tonic-gate #include <libnvpair.h> 517c478bd9Sstevel@tonic-gate #include <ftw.h> 527c478bd9Sstevel@tonic-gate #include <fcntl.h> 537c478bd9Sstevel@tonic-gate #include <strings.h> 547c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 557c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #include <pwd.h> 587c478bd9Sstevel@tonic-gate #include <grp.h> 597c478bd9Sstevel@tonic-gate #include <device_info.h> 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #include <libintl.h> 627c478bd9Sstevel@tonic-gate #include <locale.h> 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate #include <assert.h> 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate #include "message.h" 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN 697c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD" 707c478bd9Sstevel@tonic-gate #endif /* TEXT_DOMAIN */ 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate /* Type definitions */ 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate /* Primary subcmds */ 757c478bd9Sstevel@tonic-gate typedef enum { 767c478bd9Sstevel@tonic-gate BAM_MENU = 3, 777c478bd9Sstevel@tonic-gate BAM_ARCHIVE 787c478bd9Sstevel@tonic-gate } subcmd_t; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /* GRUB menu per-line classification */ 817c478bd9Sstevel@tonic-gate typedef enum { 827c478bd9Sstevel@tonic-gate BAM_INVALID = 0, 837c478bd9Sstevel@tonic-gate BAM_EMPTY, 847c478bd9Sstevel@tonic-gate BAM_COMMENT, 857c478bd9Sstevel@tonic-gate BAM_GLOBAL, 867c478bd9Sstevel@tonic-gate BAM_ENTRY, 877c478bd9Sstevel@tonic-gate BAM_TITLE 887c478bd9Sstevel@tonic-gate } menu_flag_t; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* struct for menu.lst contents */ 917c478bd9Sstevel@tonic-gate typedef struct line { 927c478bd9Sstevel@tonic-gate int lineNum; /* Line number in menu.lst */ 937c478bd9Sstevel@tonic-gate int entryNum; /* menu boot entry #. ENTRY_INIT if not applicable */ 947c478bd9Sstevel@tonic-gate char *cmd; 957c478bd9Sstevel@tonic-gate char *sep; 967c478bd9Sstevel@tonic-gate char *arg; 977c478bd9Sstevel@tonic-gate char *line; 987c478bd9Sstevel@tonic-gate menu_flag_t flags; 997c478bd9Sstevel@tonic-gate struct line *next; 1007c478bd9Sstevel@tonic-gate } line_t; 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate typedef struct { 1037c478bd9Sstevel@tonic-gate line_t *start; 1047c478bd9Sstevel@tonic-gate line_t *end; 1057c478bd9Sstevel@tonic-gate } menu_t; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate typedef enum { 1087c478bd9Sstevel@tonic-gate OPT_ABSENT = 0, /* No option */ 1097c478bd9Sstevel@tonic-gate OPT_REQ, /* option required */ 1107c478bd9Sstevel@tonic-gate OPT_OPTIONAL /* option may or may not be present */ 1117c478bd9Sstevel@tonic-gate } option_t; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate typedef enum { 114b610f78eSvikram BAM_ERROR = -1, /* Must be negative. add_boot_entry() depends on it */ 1157c478bd9Sstevel@tonic-gate BAM_SUCCESS = 0, 1167c478bd9Sstevel@tonic-gate BAM_WRITE = 2 1177c478bd9Sstevel@tonic-gate } error_t; 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate typedef struct { 1207c478bd9Sstevel@tonic-gate char *subcmd; 1217c478bd9Sstevel@tonic-gate option_t option; 1227c478bd9Sstevel@tonic-gate error_t (*handler)(); 1237c478bd9Sstevel@tonic-gate } subcmd_defn_t; 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate #define BAM_MAXLINE 8192 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate #define LINE_INIT 0 /* lineNum initial value */ 1297c478bd9Sstevel@tonic-gate #define ENTRY_INIT -1 /* entryNum initial value */ 1307c478bd9Sstevel@tonic-gate #define ALL_ENTRIES -2 /* selects all boot entries */ 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate #define GRUB_DIR "/boot/grub" 1337c478bd9Sstevel@tonic-gate #define MULTI_BOOT "/platform/i86pc/multiboot" 1347c478bd9Sstevel@tonic-gate #define BOOT_ARCHIVE "/platform/i86pc/boot_archive" 1357c478bd9Sstevel@tonic-gate #define GRUB_MENU "/boot/grub/menu.lst" 1367c478bd9Sstevel@tonic-gate #define MENU_TMP "/boot/grub/menu.lst.tmp" 1377c478bd9Sstevel@tonic-gate #define RAMDISK_SPECIAL "/ramdisk" 138*40541d5dSvikram #define STUBBOOT "/stubboot" 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate /* lock related */ 1417c478bd9Sstevel@tonic-gate #define BAM_LOCK_FILE "/var/run/bootadm.lock" 1427c478bd9Sstevel@tonic-gate #define LOCK_FILE_PERMS (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate #define CREATE_RAMDISK "/boot/solaris/bin/create_ramdisk" 1457c478bd9Sstevel@tonic-gate #define CREATE_DISKMAP "/boot/solaris/bin/create_diskmap" 1467c478bd9Sstevel@tonic-gate #define GRUBDISK_MAP "/var/run/solaris_grubdisk.map" 1477c478bd9Sstevel@tonic-gate 148b610f78eSvikram #define GRUB_slice "/etc/lu/GRUB_slice" 149b610f78eSvikram #define GRUB_root "/etc/lu/GRUB_root" 150b610f78eSvikram #define GRUB_backup_menu "/etc/lu/GRUB_backup_menu" 151b610f78eSvikram #define GRUB_slice_mntpt "/tmp/GRUB_slice_mntpt" 152b610f78eSvikram #define LU_ACTIVATE_FILE "/etc/lu/DelayUpdate/activate.sh" 153b610f78eSvikram 154b610f78eSvikram #define INSTALLGRUB "/sbin/installgrub" 155b610f78eSvikram #define STAGE1 "/boot/grub/stage1" 156b610f78eSvikram #define STAGE2 "/boot/grub/stage2" 157b610f78eSvikram 1587c478bd9Sstevel@tonic-gate /* 1597c478bd9Sstevel@tonic-gate * Default file attributes 1607c478bd9Sstevel@tonic-gate */ 1617c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_MODE 0644 /* default permissions */ 1627c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_UID 0 /* user root */ 1637c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_GID 3 /* group sys */ 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate /* 1667c478bd9Sstevel@tonic-gate * Menu related 1677c478bd9Sstevel@tonic-gate * menu_cmd_t and menu_cmds must be kept in sync 1687c478bd9Sstevel@tonic-gate */ 1697c478bd9Sstevel@tonic-gate typedef enum { 1707c478bd9Sstevel@tonic-gate DEFAULT_CMD = 0, 1717c478bd9Sstevel@tonic-gate TIMEOUT_CMD, 1727c478bd9Sstevel@tonic-gate TITLE_CMD, 1737c478bd9Sstevel@tonic-gate ROOT_CMD, 1747c478bd9Sstevel@tonic-gate KERNEL_CMD, 1757c478bd9Sstevel@tonic-gate MODULE_CMD, 1767c478bd9Sstevel@tonic-gate SEP_CMD, 1777c478bd9Sstevel@tonic-gate COMMENT_CMD 1787c478bd9Sstevel@tonic-gate } menu_cmd_t; 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate static char *menu_cmds[] = { 1817c478bd9Sstevel@tonic-gate "default", /* DEFAULT_CMD */ 1827c478bd9Sstevel@tonic-gate "timeout", /* TIMEOUT_CMD */ 1837c478bd9Sstevel@tonic-gate "title", /* TITLE_CMD */ 1847c478bd9Sstevel@tonic-gate "root", /* ROOT_CMD */ 1857c478bd9Sstevel@tonic-gate "kernel", /* KERNEL_CMD */ 1867c478bd9Sstevel@tonic-gate "module", /* MODULE_CMD */ 1877c478bd9Sstevel@tonic-gate " ", /* SEP_CMD */ 1887c478bd9Sstevel@tonic-gate "#", /* COMMENT_CMD */ 1897c478bd9Sstevel@tonic-gate NULL 1907c478bd9Sstevel@tonic-gate }; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate #define OPT_ENTRY_NUM "entry" 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate /* 1957c478bd9Sstevel@tonic-gate * archive related 1967c478bd9Sstevel@tonic-gate */ 1977c478bd9Sstevel@tonic-gate typedef struct { 1987c478bd9Sstevel@tonic-gate line_t *head; 1997c478bd9Sstevel@tonic-gate line_t *tail; 2007c478bd9Sstevel@tonic-gate } filelist_t; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate #define BOOT_FILE_LIST "boot/solaris/filelist.ramdisk" 2037c478bd9Sstevel@tonic-gate #define ETC_FILE_LIST "etc/boot/solaris/filelist.ramdisk" 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate #define FILE_STAT "boot/solaris/filestat.ramdisk" 2067c478bd9Sstevel@tonic-gate #define FILE_STAT_TMP "boot/solaris/filestat.ramdisk.tmp" 2077c478bd9Sstevel@tonic-gate #define DIR_PERMS (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) 2087c478bd9Sstevel@tonic-gate #define FILE_STAT_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate #define BAM_HDR "---------- ADDED BY BOOTADM - DO NOT EDIT ----------" 2117c478bd9Sstevel@tonic-gate #define BAM_FTR "---------------------END BOOTADM--------------------" 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate /* Globals */ 2157c478bd9Sstevel@tonic-gate static char *prog; 2167c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd; 2177c478bd9Sstevel@tonic-gate static char *bam_root; 2187c478bd9Sstevel@tonic-gate static int bam_rootlen; 2197c478bd9Sstevel@tonic-gate static int bam_root_readonly; 220*40541d5dSvikram static int bam_alt_root; 2217c478bd9Sstevel@tonic-gate static char *bam_subcmd; 2227c478bd9Sstevel@tonic-gate static char *bam_opt; 2237c478bd9Sstevel@tonic-gate static int bam_debug; 2247c478bd9Sstevel@tonic-gate static char **bam_argv; 2257c478bd9Sstevel@tonic-gate static int bam_argc; 2267c478bd9Sstevel@tonic-gate static int bam_force; 2277c478bd9Sstevel@tonic-gate static int bam_verbose; 2287c478bd9Sstevel@tonic-gate static int bam_check; 2297c478bd9Sstevel@tonic-gate static int bam_smf_check; 2307c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1; 2317c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/"; 232b610f78eSvikram static int bam_update_all; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate /* function prototypes */ 2357c478bd9Sstevel@tonic-gate static void parse_args_internal(int argc, char *argv[]); 2367c478bd9Sstevel@tonic-gate static void parse_args(int argc, char *argv[]); 2377c478bd9Sstevel@tonic-gate static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]); 2387c478bd9Sstevel@tonic-gate static error_t bam_archive(char *subcmd, char *opt); 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate static void bam_error(char *format, ...); 2417c478bd9Sstevel@tonic-gate static void bam_print(char *format, ...); 2427c478bd9Sstevel@tonic-gate static void bam_exit(int excode); 2437c478bd9Sstevel@tonic-gate static void bam_lock(void); 2447c478bd9Sstevel@tonic-gate static void bam_unlock(void); 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate static int exec_cmd(char *cmdline, char *output, int64_t osize); 2477c478bd9Sstevel@tonic-gate static error_t read_globals(menu_t *mp, char *menu_path, 2487c478bd9Sstevel@tonic-gate char *globalcmd, int quiet); 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate static menu_t *menu_read(char *menu_path); 2517c478bd9Sstevel@tonic-gate static error_t menu_write(char *root, menu_t *mp); 2527c478bd9Sstevel@tonic-gate static void linelist_free(line_t *start); 2537c478bd9Sstevel@tonic-gate static void menu_free(menu_t *mp); 2547c478bd9Sstevel@tonic-gate static void line_free(line_t *lp); 2557c478bd9Sstevel@tonic-gate static void filelist_free(filelist_t *flistp); 2567c478bd9Sstevel@tonic-gate static error_t list2file(char *root, char *tmp, 2577c478bd9Sstevel@tonic-gate char *final, line_t *start); 2587c478bd9Sstevel@tonic-gate static error_t list_entry(menu_t *mp, char *menu_path, char *opt); 2597c478bd9Sstevel@tonic-gate static error_t delete_entry(menu_t *mp, char *menu_path, char *opt); 2607c478bd9Sstevel@tonic-gate static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt); 2617c478bd9Sstevel@tonic-gate static error_t update_entry(menu_t *mp, char *root, char *opt); 2627c478bd9Sstevel@tonic-gate static error_t update_temp(menu_t *mp, char *root, char *opt); 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate static error_t update_archive(char *root, char *opt); 2657c478bd9Sstevel@tonic-gate static error_t list_archive(char *root, char *opt); 2667c478bd9Sstevel@tonic-gate static error_t update_all(char *root, char *opt); 2677c478bd9Sstevel@tonic-gate static error_t read_list(char *root, filelist_t *flistp); 2687c478bd9Sstevel@tonic-gate static error_t set_global(menu_t *mp, char *globalcmd, int val); 2697c478bd9Sstevel@tonic-gate static error_t set_option(menu_t *mp, char *globalcmd, char *opt); 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate static long s_strtol(char *str); 2727c478bd9Sstevel@tonic-gate static char *s_fgets(char *buf, int n, FILE *fp); 2737c478bd9Sstevel@tonic-gate static int s_fputs(char *str, FILE *fp); 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate static void *s_calloc(size_t nelem, size_t sz); 2767c478bd9Sstevel@tonic-gate static char *s_strdup(char *str); 2777c478bd9Sstevel@tonic-gate static int is_readonly(char *); 2787c478bd9Sstevel@tonic-gate static int is_amd64(void); 2797c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *); 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate #if defined(__sparc) 2827c478bd9Sstevel@tonic-gate static void sparc_abort(void); 2837c478bd9Sstevel@tonic-gate #endif 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate /* Menu related sub commands */ 2867c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = { 2877c478bd9Sstevel@tonic-gate "set_option", OPT_OPTIONAL, set_option, /* PUB */ 2887c478bd9Sstevel@tonic-gate "list_entry", OPT_OPTIONAL, list_entry, /* PUB */ 2897c478bd9Sstevel@tonic-gate "delete_all_entries", OPT_ABSENT, delete_all_entries, /* PVT */ 2907c478bd9Sstevel@tonic-gate "update_entry", OPT_REQ, update_entry, /* menu */ 2917c478bd9Sstevel@tonic-gate "update_temp", OPT_OPTIONAL, update_temp, /* reboot */ 2927c478bd9Sstevel@tonic-gate NULL, 0, NULL /* must be last */ 2937c478bd9Sstevel@tonic-gate }; 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate /* Archive related sub commands */ 2967c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = { 2977c478bd9Sstevel@tonic-gate "update", OPT_ABSENT, update_archive, /* PUB */ 2987c478bd9Sstevel@tonic-gate "update_all", OPT_ABSENT, update_all, /* PVT */ 2997c478bd9Sstevel@tonic-gate "list", OPT_OPTIONAL, list_archive, /* PUB */ 3007c478bd9Sstevel@tonic-gate NULL, 0, NULL /* must be last */ 3017c478bd9Sstevel@tonic-gate }; 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate static struct { 3047c478bd9Sstevel@tonic-gate nvlist_t *new_nvlp; 3057c478bd9Sstevel@tonic-gate nvlist_t *old_nvlp; 3067c478bd9Sstevel@tonic-gate int need_update; 3077c478bd9Sstevel@tonic-gate } walk_arg; 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate static void 3107c478bd9Sstevel@tonic-gate usage(void) 3117c478bd9Sstevel@tonic-gate { 3127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "USAGE:\n"); 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate /* archive usage */ 3167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n", 3177c478bd9Sstevel@tonic-gate prog); 3187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog); 3197c478bd9Sstevel@tonic-gate #ifndef __sparc 3207c478bd9Sstevel@tonic-gate /* x86 only */ 3217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog); 3227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog); 3237c478bd9Sstevel@tonic-gate #endif 3247c478bd9Sstevel@tonic-gate } 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate int 3277c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 3287c478bd9Sstevel@tonic-gate { 3297c478bd9Sstevel@tonic-gate error_t ret; 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 3327c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate if ((prog = strrchr(argv[0], '/')) == NULL) { 3357c478bd9Sstevel@tonic-gate prog = argv[0]; 3367c478bd9Sstevel@tonic-gate } else { 3377c478bd9Sstevel@tonic-gate prog++; 3387c478bd9Sstevel@tonic-gate } 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate if (geteuid() != 0) { 3417c478bd9Sstevel@tonic-gate bam_error(MUST_BE_ROOT); 3427c478bd9Sstevel@tonic-gate bam_exit(1); 3437c478bd9Sstevel@tonic-gate } 3447c478bd9Sstevel@tonic-gate 3457c478bd9Sstevel@tonic-gate bam_lock(); 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate /* 3487c478bd9Sstevel@tonic-gate * Don't depend on caller's umask 3497c478bd9Sstevel@tonic-gate */ 3507c478bd9Sstevel@tonic-gate (void) umask(0022); 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate parse_args(argc, argv); 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate #if defined(__sparc) 3557c478bd9Sstevel@tonic-gate /* 3567c478bd9Sstevel@tonic-gate * There are only two valid invocations of bootadm 3577c478bd9Sstevel@tonic-gate * on SPARC: 3587c478bd9Sstevel@tonic-gate * 3597c478bd9Sstevel@tonic-gate * - SPARC diskless server creating boot_archive for i386 clients 3607c478bd9Sstevel@tonic-gate * - archive creation call during reboot of a SPARC system 3617c478bd9Sstevel@tonic-gate * 3627c478bd9Sstevel@tonic-gate * The latter should be a NOP 3637c478bd9Sstevel@tonic-gate */ 3647c478bd9Sstevel@tonic-gate if (bam_cmd != BAM_ARCHIVE) { 3657c478bd9Sstevel@tonic-gate sparc_abort(); 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate #endif 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate switch (bam_cmd) { 3707c478bd9Sstevel@tonic-gate case BAM_MENU: 3717c478bd9Sstevel@tonic-gate ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv); 3727c478bd9Sstevel@tonic-gate break; 3737c478bd9Sstevel@tonic-gate case BAM_ARCHIVE: 3747c478bd9Sstevel@tonic-gate ret = bam_archive(bam_subcmd, bam_opt); 3757c478bd9Sstevel@tonic-gate break; 3767c478bd9Sstevel@tonic-gate default: 3777c478bd9Sstevel@tonic-gate usage(); 3787c478bd9Sstevel@tonic-gate bam_exit(1); 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate if (ret != BAM_SUCCESS) 3827c478bd9Sstevel@tonic-gate bam_exit(1); 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate bam_unlock(); 3857c478bd9Sstevel@tonic-gate return (0); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate #if defined(__sparc) 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate static void 3917c478bd9Sstevel@tonic-gate sparc_abort(void) 3927c478bd9Sstevel@tonic-gate { 3937c478bd9Sstevel@tonic-gate bam_error(NOT_ON_SPARC); 3947c478bd9Sstevel@tonic-gate bam_exit(1); 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate #endif 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate /* 4007c478bd9Sstevel@tonic-gate * Equivalence of public and internal commands: 4017c478bd9Sstevel@tonic-gate * update-archive -- -a update 4027c478bd9Sstevel@tonic-gate * list-archive -- -a list 4037c478bd9Sstevel@tonic-gate * set-menu -- -m set_option 4047c478bd9Sstevel@tonic-gate * list-menu -- -m list_entry 4057c478bd9Sstevel@tonic-gate * update-menu -- -m update_entry 4067c478bd9Sstevel@tonic-gate */ 4077c478bd9Sstevel@tonic-gate static struct cmd_map { 4087c478bd9Sstevel@tonic-gate char *bam_cmdname; 4097c478bd9Sstevel@tonic-gate int bam_cmd; 4107c478bd9Sstevel@tonic-gate char *bam_subcmd; 4117c478bd9Sstevel@tonic-gate } cmd_map[] = { 4127c478bd9Sstevel@tonic-gate { "update-archive", BAM_ARCHIVE, "update"}, 4137c478bd9Sstevel@tonic-gate { "list-archive", BAM_ARCHIVE, "list"}, 4147c478bd9Sstevel@tonic-gate { "set-menu", BAM_MENU, "set_option"}, 4157c478bd9Sstevel@tonic-gate { "list-menu", BAM_MENU, "list_entry"}, 4167c478bd9Sstevel@tonic-gate { "update-menu", BAM_MENU, "update_entry"}, 4177c478bd9Sstevel@tonic-gate { NULL, 0, NULL} 4187c478bd9Sstevel@tonic-gate }; 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate /* 4217c478bd9Sstevel@tonic-gate * Commands syntax published in bootadm(1M) are parsed here 4227c478bd9Sstevel@tonic-gate */ 4237c478bd9Sstevel@tonic-gate static void 4247c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[]) 4257c478bd9Sstevel@tonic-gate { 4267c478bd9Sstevel@tonic-gate struct cmd_map *cmp = cmd_map; 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate /* command conforming to the final spec */ 4297c478bd9Sstevel@tonic-gate if (argc > 1 && argv[1][0] != '-') { 4307c478bd9Sstevel@tonic-gate /* 4317c478bd9Sstevel@tonic-gate * Map commands to internal table. 4327c478bd9Sstevel@tonic-gate */ 4337c478bd9Sstevel@tonic-gate while (cmp->bam_cmdname) { 4347c478bd9Sstevel@tonic-gate if (strcmp(argv[1], cmp->bam_cmdname) == 0) { 4357c478bd9Sstevel@tonic-gate bam_cmd = cmp->bam_cmd; 4367c478bd9Sstevel@tonic-gate bam_subcmd = cmp->bam_subcmd; 4377c478bd9Sstevel@tonic-gate break; 4387c478bd9Sstevel@tonic-gate } 4397c478bd9Sstevel@tonic-gate cmp++; 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate if (cmp->bam_cmdname == NULL) { 4427c478bd9Sstevel@tonic-gate usage(); 4437c478bd9Sstevel@tonic-gate bam_exit(1); 4447c478bd9Sstevel@tonic-gate } 4457c478bd9Sstevel@tonic-gate argc--; 4467c478bd9Sstevel@tonic-gate argv++; 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate parse_args_internal(argc, argv); 4507c478bd9Sstevel@tonic-gate } 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate /* 4537c478bd9Sstevel@tonic-gate * A combination of public and private commands are parsed here. 4547c478bd9Sstevel@tonic-gate * The internal syntax and the corresponding functionality are: 4557c478bd9Sstevel@tonic-gate * -a update -- update-archive 4567c478bd9Sstevel@tonic-gate * -a list -- list-archive 4577c478bd9Sstevel@tonic-gate * -a update-all -- (reboot to sync all mounted OS archive) 4587c478bd9Sstevel@tonic-gate * -m update_entry -- update-menu 4597c478bd9Sstevel@tonic-gate * -m list_entry -- list-menu 4607c478bd9Sstevel@tonic-gate * -m update_temp -- (reboot -- [boot-args]) 4617c478bd9Sstevel@tonic-gate * -m delete_all_entries -- (called from install) 4627c478bd9Sstevel@tonic-gate */ 4637c478bd9Sstevel@tonic-gate static void 4647c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[]) 4657c478bd9Sstevel@tonic-gate { 4667c478bd9Sstevel@tonic-gate int c, error; 4677c478bd9Sstevel@tonic-gate extern char *optarg; 4687c478bd9Sstevel@tonic-gate extern int optind, opterr; 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate /* Suppress error message from getopt */ 4717c478bd9Sstevel@tonic-gate opterr = 0; 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate error = 0; 4747c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "a:d:fm:no:vR:")) != -1) { 4757c478bd9Sstevel@tonic-gate switch (c) { 4767c478bd9Sstevel@tonic-gate case 'a': 4777c478bd9Sstevel@tonic-gate if (bam_cmd) { 4787c478bd9Sstevel@tonic-gate error = 1; 4797c478bd9Sstevel@tonic-gate bam_error(MULT_CMDS, c); 4807c478bd9Sstevel@tonic-gate } 4817c478bd9Sstevel@tonic-gate bam_cmd = BAM_ARCHIVE; 4827c478bd9Sstevel@tonic-gate bam_subcmd = optarg; 4837c478bd9Sstevel@tonic-gate break; 4847c478bd9Sstevel@tonic-gate case 'd': 4857c478bd9Sstevel@tonic-gate if (bam_debug) { 4867c478bd9Sstevel@tonic-gate error = 1; 4877c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 4887c478bd9Sstevel@tonic-gate } 4897c478bd9Sstevel@tonic-gate bam_debug = s_strtol(optarg); 4907c478bd9Sstevel@tonic-gate break; 4917c478bd9Sstevel@tonic-gate case 'f': 4927c478bd9Sstevel@tonic-gate if (bam_force) { 4937c478bd9Sstevel@tonic-gate error = 1; 4947c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 4957c478bd9Sstevel@tonic-gate } 4967c478bd9Sstevel@tonic-gate bam_force = 1; 4977c478bd9Sstevel@tonic-gate break; 4987c478bd9Sstevel@tonic-gate case 'm': 4997c478bd9Sstevel@tonic-gate if (bam_cmd) { 5007c478bd9Sstevel@tonic-gate error = 1; 5017c478bd9Sstevel@tonic-gate bam_error(MULT_CMDS, c); 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate bam_cmd = BAM_MENU; 5047c478bd9Sstevel@tonic-gate bam_subcmd = optarg; 5057c478bd9Sstevel@tonic-gate break; 5067c478bd9Sstevel@tonic-gate case 'n': 5077c478bd9Sstevel@tonic-gate if (bam_check) { 5087c478bd9Sstevel@tonic-gate error = 1; 5097c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5107c478bd9Sstevel@tonic-gate } 5117c478bd9Sstevel@tonic-gate bam_check = 1; 5127c478bd9Sstevel@tonic-gate bam_smf_check = bam_root_readonly; 5137c478bd9Sstevel@tonic-gate break; 5147c478bd9Sstevel@tonic-gate case 'o': 5157c478bd9Sstevel@tonic-gate if (bam_opt) { 5167c478bd9Sstevel@tonic-gate error = 1; 5177c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate bam_opt = optarg; 5207c478bd9Sstevel@tonic-gate break; 5217c478bd9Sstevel@tonic-gate case 'v': 5227c478bd9Sstevel@tonic-gate if (bam_verbose) { 5237c478bd9Sstevel@tonic-gate error = 1; 5247c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5257c478bd9Sstevel@tonic-gate } 5267c478bd9Sstevel@tonic-gate bam_verbose = 1; 5277c478bd9Sstevel@tonic-gate break; 5287c478bd9Sstevel@tonic-gate case 'R': 5297c478bd9Sstevel@tonic-gate if (bam_root) { 5307c478bd9Sstevel@tonic-gate error = 1; 5317c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5327c478bd9Sstevel@tonic-gate break; 5337c478bd9Sstevel@tonic-gate } else if (realpath(optarg, rootbuf) == NULL) { 5347c478bd9Sstevel@tonic-gate error = 1; 5357c478bd9Sstevel@tonic-gate bam_error(CANT_RESOLVE, optarg, 5367c478bd9Sstevel@tonic-gate strerror(errno)); 5377c478bd9Sstevel@tonic-gate break; 5387c478bd9Sstevel@tonic-gate } 539*40541d5dSvikram bam_alt_root = 1; 5407c478bd9Sstevel@tonic-gate bam_root = rootbuf; 5417c478bd9Sstevel@tonic-gate if (rootbuf[strlen(rootbuf) - 1] != '/') 5427c478bd9Sstevel@tonic-gate (void) strcat(rootbuf, "/"); 5437c478bd9Sstevel@tonic-gate bam_rootlen = strlen(rootbuf); 5447c478bd9Sstevel@tonic-gate break; 5457c478bd9Sstevel@tonic-gate case '?': 5467c478bd9Sstevel@tonic-gate error = 1; 5477c478bd9Sstevel@tonic-gate bam_error(BAD_OPT, optopt); 5487c478bd9Sstevel@tonic-gate break; 5497c478bd9Sstevel@tonic-gate default : 5507c478bd9Sstevel@tonic-gate error = 1; 5517c478bd9Sstevel@tonic-gate bam_error(BAD_OPT, c); 5527c478bd9Sstevel@tonic-gate break; 5537c478bd9Sstevel@tonic-gate } 5547c478bd9Sstevel@tonic-gate } 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate /* 5577c478bd9Sstevel@tonic-gate * A command option must be specfied 5587c478bd9Sstevel@tonic-gate */ 5597c478bd9Sstevel@tonic-gate if (!bam_cmd) { 5607c478bd9Sstevel@tonic-gate if (bam_opt && strcmp(bam_opt, "all") == 0) { 5617c478bd9Sstevel@tonic-gate usage(); 5627c478bd9Sstevel@tonic-gate bam_exit(0); 5637c478bd9Sstevel@tonic-gate } 5647c478bd9Sstevel@tonic-gate bam_error(NEED_CMD); 5657c478bd9Sstevel@tonic-gate error = 1; 5667c478bd9Sstevel@tonic-gate } 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate if (error) { 5697c478bd9Sstevel@tonic-gate usage(); 5707c478bd9Sstevel@tonic-gate bam_exit(1); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate if (optind > argc) { 5747c478bd9Sstevel@tonic-gate bam_error(INT_ERROR, "parse_args"); 5757c478bd9Sstevel@tonic-gate bam_exit(1); 5767c478bd9Sstevel@tonic-gate } else if (optind < argc) { 5777c478bd9Sstevel@tonic-gate bam_argv = &argv[optind]; 5787c478bd9Sstevel@tonic-gate bam_argc = argc - optind; 5797c478bd9Sstevel@tonic-gate } 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate /* 5827c478bd9Sstevel@tonic-gate * -n implies verbose mode 5837c478bd9Sstevel@tonic-gate */ 5847c478bd9Sstevel@tonic-gate if (bam_check) 5857c478bd9Sstevel@tonic-gate bam_verbose = 1; 5867c478bd9Sstevel@tonic-gate } 5877c478bd9Sstevel@tonic-gate 5887c478bd9Sstevel@tonic-gate static error_t 5897c478bd9Sstevel@tonic-gate check_subcmd_and_options( 5907c478bd9Sstevel@tonic-gate char *subcmd, 5917c478bd9Sstevel@tonic-gate char *opt, 5927c478bd9Sstevel@tonic-gate subcmd_defn_t *table, 5937c478bd9Sstevel@tonic-gate error_t (**fp)()) 5947c478bd9Sstevel@tonic-gate { 5957c478bd9Sstevel@tonic-gate int i; 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate if (subcmd == NULL) { 5987c478bd9Sstevel@tonic-gate bam_error(NEED_SUBCMD); 5997c478bd9Sstevel@tonic-gate return (BAM_ERROR); 6007c478bd9Sstevel@tonic-gate } 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate if (bam_root == NULL) { 6037c478bd9Sstevel@tonic-gate bam_root = rootbuf; 6047c478bd9Sstevel@tonic-gate bam_rootlen = 1; 6057c478bd9Sstevel@tonic-gate } 6067c478bd9Sstevel@tonic-gate 6077c478bd9Sstevel@tonic-gate /* verify that subcmd is valid */ 6087c478bd9Sstevel@tonic-gate for (i = 0; table[i].subcmd != NULL; i++) { 6097c478bd9Sstevel@tonic-gate if (strcmp(table[i].subcmd, subcmd) == 0) 6107c478bd9Sstevel@tonic-gate break; 6117c478bd9Sstevel@tonic-gate } 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate if (table[i].subcmd == NULL) { 6147c478bd9Sstevel@tonic-gate bam_error(INVALID_SUBCMD, subcmd); 6157c478bd9Sstevel@tonic-gate return (BAM_ERROR); 6167c478bd9Sstevel@tonic-gate } 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate /* subcmd verifies that opt is appropriate */ 6197c478bd9Sstevel@tonic-gate if (table[i].option != OPT_OPTIONAL) { 6207c478bd9Sstevel@tonic-gate if ((table[i].option == OPT_REQ) ^ (opt != NULL)) { 6217c478bd9Sstevel@tonic-gate if (opt) 6227c478bd9Sstevel@tonic-gate bam_error(NO_OPT_REQ, subcmd); 6237c478bd9Sstevel@tonic-gate else 6247c478bd9Sstevel@tonic-gate bam_error(MISS_OPT, subcmd); 6257c478bd9Sstevel@tonic-gate return (BAM_ERROR); 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate } 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate *fp = table[i].handler; 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 6327c478bd9Sstevel@tonic-gate } 6337c478bd9Sstevel@tonic-gate 634b610f78eSvikram 635b610f78eSvikram static char * 636*40541d5dSvikram mount_grub_slice(int *mnted, char **physlice, char **logslice, char **fs_type) 637b610f78eSvikram { 638b610f78eSvikram struct extmnttab mnt; 639b610f78eSvikram struct stat sb; 640b610f78eSvikram char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32]; 641f0f2c544Svikram char cmd[PATH_MAX]; 642b610f78eSvikram char *mntpt; 643b610f78eSvikram int p, l, f; 644b610f78eSvikram FILE *fp; 645b610f78eSvikram 646b610f78eSvikram assert(mnted); 647b610f78eSvikram *mnted = 0; 648b610f78eSvikram 649b610f78eSvikram /* 650*40541d5dSvikram * physlice, logslice, fs_type args may be NULL 651b610f78eSvikram */ 652b610f78eSvikram if (physlice) 653b610f78eSvikram *physlice = NULL; 654*40541d5dSvikram if (logslice) 655*40541d5dSvikram *logslice = NULL; 656*40541d5dSvikram if (fs_type) 657*40541d5dSvikram *fs_type = NULL; 658b610f78eSvikram 659b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 660b610f78eSvikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 661b610f78eSvikram return (NULL); 662b610f78eSvikram } 663b610f78eSvikram 664b610f78eSvikram fp = fopen(GRUB_slice, "r"); 665b610f78eSvikram if (fp == NULL) { 666b610f78eSvikram bam_error(OPEN_FAIL, GRUB_slice, strerror(errno)); 667b610f78eSvikram return (NULL); 668b610f78eSvikram } 669b610f78eSvikram 670b610f78eSvikram dev[0] = fstype[0] = phys[0] = '\0'; 671b610f78eSvikram p = sizeof ("PHYS_SLICE=") - 1; 672b610f78eSvikram l = sizeof ("LOG_SLICE=") - 1; 673b610f78eSvikram f = sizeof ("LOG_FSTYP=") - 1; 674b610f78eSvikram while (s_fgets(buf, sizeof (buf), fp) != NULL) { 675b610f78eSvikram if (strncmp(buf, "PHYS_SLICE=", p) == 0) { 676b610f78eSvikram (void) strlcpy(phys, buf + p, sizeof (phys)); 677b610f78eSvikram continue; 678b610f78eSvikram } 679b610f78eSvikram if (strncmp(buf, "LOG_SLICE=", l) == 0) { 680b610f78eSvikram (void) strlcpy(dev, buf + l, sizeof (dev)); 681b610f78eSvikram continue; 682b610f78eSvikram } 683b610f78eSvikram if (strncmp(buf, "LOG_FSTYP=", f) == 0) { 684b610f78eSvikram (void) strlcpy(fstype, buf + f, sizeof (fstype)); 685b610f78eSvikram continue; 686b610f78eSvikram } 687b610f78eSvikram } 688b610f78eSvikram (void) fclose(fp); 689b610f78eSvikram 690b610f78eSvikram if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') { 691b610f78eSvikram bam_error(BAD_SLICE_FILE, GRUB_slice); 692b610f78eSvikram return (NULL); 693b610f78eSvikram } 694b610f78eSvikram 695b610f78eSvikram if (physlice) { 696b610f78eSvikram *physlice = s_strdup(phys); 697b610f78eSvikram } 698*40541d5dSvikram if (logslice) { 699*40541d5dSvikram *logslice = s_strdup(dev); 700*40541d5dSvikram } 701*40541d5dSvikram if (fs_type) { 702*40541d5dSvikram *fs_type = s_strdup(fstype); 703*40541d5dSvikram } 704b610f78eSvikram 705b610f78eSvikram /* 706b610f78eSvikram * Check if the slice is already mounted 707b610f78eSvikram */ 708b610f78eSvikram fp = fopen(MNTTAB, "r"); 709b610f78eSvikram if (fp == NULL) { 710b610f78eSvikram bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 711*40541d5dSvikram goto error; 712b610f78eSvikram } 713b610f78eSvikram 714b610f78eSvikram resetmnttab(fp); 715b610f78eSvikram 716b610f78eSvikram mntpt = NULL; 717b610f78eSvikram while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 718b610f78eSvikram if (strcmp(mnt.mnt_special, dev) == 0) { 719b610f78eSvikram mntpt = s_strdup(mnt.mnt_mountp); 720b610f78eSvikram break; 721b610f78eSvikram } 722b610f78eSvikram } 723b610f78eSvikram 724b610f78eSvikram (void) fclose(fp); 725b610f78eSvikram 726b610f78eSvikram if (mntpt) { 727b610f78eSvikram return (mntpt); 728b610f78eSvikram } 729b610f78eSvikram 730b610f78eSvikram 731b610f78eSvikram /* 732b610f78eSvikram * GRUB slice is not mounted, we need to mount it now. 733b610f78eSvikram * First create the mountpoint 734b610f78eSvikram */ 735b610f78eSvikram mntpt = s_calloc(1, PATH_MAX); 736b610f78eSvikram (void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid()); 737b610f78eSvikram if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) { 738b610f78eSvikram bam_error(MKDIR_FAILED, mntpt, strerror(errno)); 739b610f78eSvikram free(mntpt); 740*40541d5dSvikram goto error; 741b610f78eSvikram } 742b610f78eSvikram 743f0f2c544Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s", 744f0f2c544Svikram fstype, dev, mntpt); 745f0f2c544Svikram 746f0f2c544Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 747*40541d5dSvikram bam_error(MOUNT_FAILED, dev, fstype); 748b610f78eSvikram if (rmdir(mntpt) != 0) { 749b610f78eSvikram bam_error(RMDIR_FAILED, mntpt, strerror(errno)); 750b610f78eSvikram } 751b610f78eSvikram free(mntpt); 752*40541d5dSvikram goto error; 753b610f78eSvikram } 754b610f78eSvikram 755b610f78eSvikram *mnted = 1; 756b610f78eSvikram return (mntpt); 757*40541d5dSvikram 758*40541d5dSvikram error: 759*40541d5dSvikram if (physlice) { 760*40541d5dSvikram free(*physlice); 761*40541d5dSvikram *physlice = NULL; 762*40541d5dSvikram } 763*40541d5dSvikram if (logslice) { 764*40541d5dSvikram free(*logslice); 765*40541d5dSvikram *logslice = NULL; 766*40541d5dSvikram } 767*40541d5dSvikram if (fs_type) { 768*40541d5dSvikram free(*fs_type); 769*40541d5dSvikram *fs_type = NULL; 770*40541d5dSvikram } 771*40541d5dSvikram return (NULL); 772b610f78eSvikram } 773b610f78eSvikram 774b610f78eSvikram static void 775*40541d5dSvikram umount_grub_slice( 776*40541d5dSvikram int mnted, 777*40541d5dSvikram char *mntpt, 778*40541d5dSvikram char *physlice, 779*40541d5dSvikram char *logslice, 780*40541d5dSvikram char *fs_type) 781b610f78eSvikram { 782f0f2c544Svikram char cmd[PATH_MAX]; 783f0f2c544Svikram 784b610f78eSvikram /* 785b610f78eSvikram * If we have not dealt with GRUB slice 786b610f78eSvikram * we have nothing to do - just return. 787b610f78eSvikram */ 788b610f78eSvikram if (mntpt == NULL) 789b610f78eSvikram return; 790b610f78eSvikram 791b610f78eSvikram 792b610f78eSvikram /* 793b610f78eSvikram * If we mounted the filesystem earlier in mount_grub_slice() 794b610f78eSvikram * unmount it now. 795b610f78eSvikram */ 796b610f78eSvikram if (mnted) { 797f0f2c544Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/umount %s", 798f0f2c544Svikram mntpt); 799f0f2c544Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 800f0f2c544Svikram bam_error(UMOUNT_FAILED, mntpt); 801b610f78eSvikram } 802b610f78eSvikram if (rmdir(mntpt) != 0) { 803b610f78eSvikram bam_error(RMDIR_FAILED, mntpt, strerror(errno)); 804b610f78eSvikram } 805b610f78eSvikram } 806*40541d5dSvikram 807b610f78eSvikram if (physlice) 808b610f78eSvikram free(physlice); 809*40541d5dSvikram if (logslice) 810*40541d5dSvikram free(logslice); 811*40541d5dSvikram if (fs_type) 812*40541d5dSvikram free(fs_type); 813*40541d5dSvikram 814b610f78eSvikram free(mntpt); 815b610f78eSvikram } 816b610f78eSvikram 817*40541d5dSvikram static char * 818*40541d5dSvikram use_stubboot(void) 819*40541d5dSvikram { 820*40541d5dSvikram int mnted; 821*40541d5dSvikram struct stat sb; 822*40541d5dSvikram struct extmnttab mnt; 823*40541d5dSvikram FILE *fp; 824*40541d5dSvikram char cmd[PATH_MAX]; 825*40541d5dSvikram 826*40541d5dSvikram if (stat(STUBBOOT, &sb) != 0) { 827*40541d5dSvikram bam_error(STUBBOOT_DIR_NOT_FOUND); 828*40541d5dSvikram return (NULL); 829*40541d5dSvikram } 830*40541d5dSvikram 831*40541d5dSvikram /* 832*40541d5dSvikram * Check if stubboot is mounted. If not, mount it 833*40541d5dSvikram */ 834*40541d5dSvikram fp = fopen(MNTTAB, "r"); 835*40541d5dSvikram if (fp == NULL) { 836*40541d5dSvikram bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 837*40541d5dSvikram return (NULL); 838*40541d5dSvikram } 839*40541d5dSvikram 840*40541d5dSvikram resetmnttab(fp); 841*40541d5dSvikram 842*40541d5dSvikram mnted = 0; 843*40541d5dSvikram while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 844*40541d5dSvikram if (strcmp(mnt.mnt_mountp, STUBBOOT) == 0) { 845*40541d5dSvikram mnted = 1; 846*40541d5dSvikram break; 847*40541d5dSvikram } 848*40541d5dSvikram } 849*40541d5dSvikram 850*40541d5dSvikram (void) fclose(fp); 851*40541d5dSvikram 852*40541d5dSvikram if (mnted) 853*40541d5dSvikram return (STUBBOOT); 854*40541d5dSvikram 855*40541d5dSvikram /* 856*40541d5dSvikram * Stubboot is not mounted, mount it now. 857*40541d5dSvikram * It should exist in /etc/vfstab 858*40541d5dSvikram */ 859*40541d5dSvikram (void) snprintf(cmd, sizeof (cmd), "/sbin/mount %s", 860*40541d5dSvikram STUBBOOT); 861*40541d5dSvikram if (exec_cmd(cmd, NULL, 0) != 0) { 862*40541d5dSvikram bam_error(MOUNT_MNTPT_FAILED, STUBBOOT); 863*40541d5dSvikram return (NULL); 864*40541d5dSvikram } 865*40541d5dSvikram 866*40541d5dSvikram return (STUBBOOT); 867*40541d5dSvikram } 868*40541d5dSvikram 869*40541d5dSvikram static void 870*40541d5dSvikram disp_active_menu_locn(char *menu_path, char *logslice, char *fstype, int mnted) 871*40541d5dSvikram { 872*40541d5dSvikram /* 873*40541d5dSvikram * Check if we did a temp mount of an unmounted device. 874*40541d5dSvikram * If yes, print the block device and fstype for that device 875*40541d5dSvikram * else it is already mounted, so we print the path to the GRUB menu. 876*40541d5dSvikram */ 877*40541d5dSvikram if (mnted) { 878*40541d5dSvikram bam_print(GRUB_MENU_DEVICE, logslice); 879*40541d5dSvikram bam_print(GRUB_MENU_FSTYPE, fstype); 880*40541d5dSvikram } else { 881*40541d5dSvikram bam_print(GRUB_MENU_PATH, menu_path); 882*40541d5dSvikram } 883*40541d5dSvikram } 884*40541d5dSvikram 885*40541d5dSvikram /* 886*40541d5dSvikram * NOTE: A single "/" is also considered a trailing slash and will 887*40541d5dSvikram * be deleted. 888*40541d5dSvikram */ 889*40541d5dSvikram static void 890*40541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize) 891*40541d5dSvikram { 892*40541d5dSvikram size_t dstlen; 893*40541d5dSvikram 894*40541d5dSvikram assert(src); 895*40541d5dSvikram assert(dst); 896*40541d5dSvikram 897*40541d5dSvikram (void) strlcpy(dst, src, dstsize); 898*40541d5dSvikram 899*40541d5dSvikram dstlen = strlen(dst); 900*40541d5dSvikram if (dst[dstlen - 1] == '/') { 901*40541d5dSvikram dst[dstlen - 1] = '\0'; 902*40541d5dSvikram } 903*40541d5dSvikram } 904*40541d5dSvikram 9057c478bd9Sstevel@tonic-gate static error_t 9067c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[]) 9077c478bd9Sstevel@tonic-gate { 9087c478bd9Sstevel@tonic-gate error_t ret; 9097c478bd9Sstevel@tonic-gate char menu_path[PATH_MAX]; 9107c478bd9Sstevel@tonic-gate menu_t *menu; 911*40541d5dSvikram char *mntpt, *menu_root, *logslice, *fstype; 912b610f78eSvikram struct stat sb; 913b610f78eSvikram int mnted; /* set if we did a mount */ 9147c478bd9Sstevel@tonic-gate error_t (*f)(menu_t *mp, char *menu_path, char *opt); 9157c478bd9Sstevel@tonic-gate 9167c478bd9Sstevel@tonic-gate /* 9177c478bd9Sstevel@tonic-gate * Check arguments 9187c478bd9Sstevel@tonic-gate */ 9197c478bd9Sstevel@tonic-gate ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f); 9207c478bd9Sstevel@tonic-gate if (ret == BAM_ERROR) { 9217c478bd9Sstevel@tonic-gate return (BAM_ERROR); 9227c478bd9Sstevel@tonic-gate } 9237c478bd9Sstevel@tonic-gate 924b610f78eSvikram mntpt = NULL; 925b610f78eSvikram mnted = 0; 926*40541d5dSvikram logslice = fstype = NULL; 927*40541d5dSvikram 928*40541d5dSvikram /* 929*40541d5dSvikram * If the user provides an alternate root, we 930*40541d5dSvikram * assume they know what they are doing and we 931*40541d5dSvikram * use it. Else we check if there is an 932*40541d5dSvikram * alternate location (other than /boot/grub) 933*40541d5dSvikram * for the GRUB menu 934*40541d5dSvikram */ 935*40541d5dSvikram if (bam_alt_root) { 936*40541d5dSvikram menu_root = bam_root; 937*40541d5dSvikram } else if (stat(GRUB_slice, &sb) == 0) { 938*40541d5dSvikram mntpt = mount_grub_slice(&mnted, NULL, &logslice, &fstype); 939b610f78eSvikram menu_root = mntpt; 940*40541d5dSvikram } else if (stat(STUBBOOT, &sb) == 0) { 941*40541d5dSvikram menu_root = use_stubboot(); 942b610f78eSvikram } else { 943b610f78eSvikram menu_root = bam_root; 944b610f78eSvikram } 945b610f78eSvikram 946*40541d5dSvikram if (menu_root == NULL) { 947*40541d5dSvikram bam_error(CANNOT_LOCATE_GRUB_MENU); 948*40541d5dSvikram return (BAM_ERROR); 949*40541d5dSvikram } 950*40541d5dSvikram 951*40541d5dSvikram elide_trailing_slash(menu_root, menu_path, sizeof (menu_path)); 952*40541d5dSvikram (void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path)); 953*40541d5dSvikram 954*40541d5dSvikram /* 955*40541d5dSvikram * If listing the menu, display the active menu 956*40541d5dSvikram * location 957*40541d5dSvikram */ 958*40541d5dSvikram if (strcmp(subcmd, "list_entry") == 0) { 959*40541d5dSvikram disp_active_menu_locn(menu_path, logslice, fstype, mnted); 960*40541d5dSvikram } 9617c478bd9Sstevel@tonic-gate 9627c478bd9Sstevel@tonic-gate menu = menu_read(menu_path); 9637c478bd9Sstevel@tonic-gate assert(menu); 9647c478bd9Sstevel@tonic-gate 9657c478bd9Sstevel@tonic-gate /* 9667c478bd9Sstevel@tonic-gate * Special handling for setting timeout and default 9677c478bd9Sstevel@tonic-gate */ 9687c478bd9Sstevel@tonic-gate if (strcmp(subcmd, "set_option") == 0) { 9697c478bd9Sstevel@tonic-gate if (largc != 1 || largv[0] == NULL) { 9707c478bd9Sstevel@tonic-gate usage(); 971*40541d5dSvikram menu_free(menu); 972*40541d5dSvikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 9737c478bd9Sstevel@tonic-gate return (BAM_ERROR); 9747c478bd9Sstevel@tonic-gate } 9757c478bd9Sstevel@tonic-gate opt = largv[0]; 9767c478bd9Sstevel@tonic-gate } else if (largc != 0) { 9777c478bd9Sstevel@tonic-gate usage(); 978*40541d5dSvikram menu_free(menu); 979*40541d5dSvikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 9807c478bd9Sstevel@tonic-gate return (BAM_ERROR); 9817c478bd9Sstevel@tonic-gate } 9827c478bd9Sstevel@tonic-gate 9837c478bd9Sstevel@tonic-gate /* 9847c478bd9Sstevel@tonic-gate * Once the sub-cmd handler has run 9857c478bd9Sstevel@tonic-gate * only the line field is guaranteed to have valid values 9867c478bd9Sstevel@tonic-gate */ 9877c478bd9Sstevel@tonic-gate if (strcmp(subcmd, "update_entry") == 0) 9887c478bd9Sstevel@tonic-gate ret = f(menu, bam_root, opt); 9897c478bd9Sstevel@tonic-gate else 9907c478bd9Sstevel@tonic-gate ret = f(menu, menu_path, opt); 9917c478bd9Sstevel@tonic-gate if (ret == BAM_WRITE) { 992b610f78eSvikram ret = menu_write(menu_root, menu); 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate 9957c478bd9Sstevel@tonic-gate menu_free(menu); 9967c478bd9Sstevel@tonic-gate 997*40541d5dSvikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 998b610f78eSvikram 9997c478bd9Sstevel@tonic-gate return (ret); 10007c478bd9Sstevel@tonic-gate } 10017c478bd9Sstevel@tonic-gate 10027c478bd9Sstevel@tonic-gate 10037c478bd9Sstevel@tonic-gate static error_t 10047c478bd9Sstevel@tonic-gate bam_archive( 10057c478bd9Sstevel@tonic-gate char *subcmd, 10067c478bd9Sstevel@tonic-gate char *opt) 10077c478bd9Sstevel@tonic-gate { 10087c478bd9Sstevel@tonic-gate error_t ret; 10097c478bd9Sstevel@tonic-gate error_t (*f)(char *root, char *opt); 10107c478bd9Sstevel@tonic-gate 10117c478bd9Sstevel@tonic-gate /* 10127c478bd9Sstevel@tonic-gate * Check arguments 10137c478bd9Sstevel@tonic-gate */ 10147c478bd9Sstevel@tonic-gate ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f); 10157c478bd9Sstevel@tonic-gate if (ret != BAM_SUCCESS) { 10167c478bd9Sstevel@tonic-gate return (BAM_ERROR); 10177c478bd9Sstevel@tonic-gate } 10187c478bd9Sstevel@tonic-gate 10197c478bd9Sstevel@tonic-gate #if defined(__sparc) 10207c478bd9Sstevel@tonic-gate /* 10217c478bd9Sstevel@tonic-gate * A NOP if called on SPARC during reboot 10227c478bd9Sstevel@tonic-gate */ 10237c478bd9Sstevel@tonic-gate if (strcmp(subcmd, "update_all") == 0) 10247c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 10257c478bd9Sstevel@tonic-gate else if (strcmp(subcmd, "update") != 0) 10267c478bd9Sstevel@tonic-gate sparc_abort(); 10277c478bd9Sstevel@tonic-gate #endif 10287c478bd9Sstevel@tonic-gate 10297c478bd9Sstevel@tonic-gate /* 10307c478bd9Sstevel@tonic-gate * Check archive not supported with update_all 10317c478bd9Sstevel@tonic-gate * since it is awkward to display out-of-sync 10327c478bd9Sstevel@tonic-gate * information for each BE. 10337c478bd9Sstevel@tonic-gate */ 10347c478bd9Sstevel@tonic-gate if (bam_check && strcmp(subcmd, "update_all") == 0) { 10357c478bd9Sstevel@tonic-gate bam_error(CHECK_NOT_SUPPORTED, subcmd); 10367c478bd9Sstevel@tonic-gate return (BAM_ERROR); 10377c478bd9Sstevel@tonic-gate } 10387c478bd9Sstevel@tonic-gate 1039b610f78eSvikram if (strcmp(subcmd, "update_all") == 0) 1040b610f78eSvikram bam_update_all = 1; 1041b610f78eSvikram 1042b610f78eSvikram ret = f(bam_root, opt); 1043b610f78eSvikram 1044b610f78eSvikram bam_update_all = 0; 1045b610f78eSvikram 1046b610f78eSvikram return (ret); 10477c478bd9Sstevel@tonic-gate } 10487c478bd9Sstevel@tonic-gate 10497c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 10507c478bd9Sstevel@tonic-gate static void 10517c478bd9Sstevel@tonic-gate bam_error(char *format, ...) 10527c478bd9Sstevel@tonic-gate { 10537c478bd9Sstevel@tonic-gate va_list ap; 10547c478bd9Sstevel@tonic-gate 10557c478bd9Sstevel@tonic-gate va_start(ap, format); 10567c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", prog); 10577c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, ap); 10587c478bd9Sstevel@tonic-gate va_end(ap); 10597c478bd9Sstevel@tonic-gate } 10607c478bd9Sstevel@tonic-gate 10617c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 10627c478bd9Sstevel@tonic-gate static void 10637c478bd9Sstevel@tonic-gate bam_print(char *format, ...) 10647c478bd9Sstevel@tonic-gate { 10657c478bd9Sstevel@tonic-gate va_list ap; 10667c478bd9Sstevel@tonic-gate 10677c478bd9Sstevel@tonic-gate va_start(ap, format); 10687c478bd9Sstevel@tonic-gate (void) vfprintf(stdout, format, ap); 10697c478bd9Sstevel@tonic-gate va_end(ap); 10707c478bd9Sstevel@tonic-gate } 10717c478bd9Sstevel@tonic-gate 10727c478bd9Sstevel@tonic-gate static void 10737c478bd9Sstevel@tonic-gate bam_exit(int excode) 10747c478bd9Sstevel@tonic-gate { 10757c478bd9Sstevel@tonic-gate bam_unlock(); 10767c478bd9Sstevel@tonic-gate exit(excode); 10777c478bd9Sstevel@tonic-gate } 10787c478bd9Sstevel@tonic-gate 10797c478bd9Sstevel@tonic-gate static void 10807c478bd9Sstevel@tonic-gate bam_lock(void) 10817c478bd9Sstevel@tonic-gate { 10827c478bd9Sstevel@tonic-gate struct flock lock; 10837c478bd9Sstevel@tonic-gate pid_t pid; 10847c478bd9Sstevel@tonic-gate 10857c478bd9Sstevel@tonic-gate bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS); 10867c478bd9Sstevel@tonic-gate if (bam_lock_fd < 0) { 10877c478bd9Sstevel@tonic-gate /* 10887c478bd9Sstevel@tonic-gate * We may be invoked early in boot for archive verification. 10897c478bd9Sstevel@tonic-gate * In this case, root is readonly and /var/run may not exist. 10907c478bd9Sstevel@tonic-gate * Proceed without the lock 10917c478bd9Sstevel@tonic-gate */ 10927c478bd9Sstevel@tonic-gate if (errno == EROFS || errno == ENOENT) { 10937c478bd9Sstevel@tonic-gate bam_root_readonly = 1; 10947c478bd9Sstevel@tonic-gate return; 10957c478bd9Sstevel@tonic-gate } 10967c478bd9Sstevel@tonic-gate 10977c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno)); 10987c478bd9Sstevel@tonic-gate bam_exit(1); 10997c478bd9Sstevel@tonic-gate } 11007c478bd9Sstevel@tonic-gate 11017c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 11027c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 11037c478bd9Sstevel@tonic-gate lock.l_start = 0; 11047c478bd9Sstevel@tonic-gate lock.l_len = 0; 11057c478bd9Sstevel@tonic-gate 11067c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) { 11077c478bd9Sstevel@tonic-gate if (errno != EACCES && errno != EAGAIN) { 11087c478bd9Sstevel@tonic-gate bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11097c478bd9Sstevel@tonic-gate (void) close(bam_lock_fd); 11107c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 11117c478bd9Sstevel@tonic-gate bam_exit(1); 11127c478bd9Sstevel@tonic-gate } 11137c478bd9Sstevel@tonic-gate pid = 0; 11147c478bd9Sstevel@tonic-gate (void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0); 11157c478bd9Sstevel@tonic-gate bam_print(FILE_LOCKED, pid); 11167c478bd9Sstevel@tonic-gate 11177c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 11187c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 11197c478bd9Sstevel@tonic-gate lock.l_start = 0; 11207c478bd9Sstevel@tonic-gate lock.l_len = 0; 11217c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) { 11227c478bd9Sstevel@tonic-gate bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11237c478bd9Sstevel@tonic-gate (void) close(bam_lock_fd); 11247c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 11257c478bd9Sstevel@tonic-gate bam_exit(1); 11267c478bd9Sstevel@tonic-gate } 11277c478bd9Sstevel@tonic-gate } 11287c478bd9Sstevel@tonic-gate 11297c478bd9Sstevel@tonic-gate /* We own the lock now */ 11307c478bd9Sstevel@tonic-gate pid = getpid(); 11317c478bd9Sstevel@tonic-gate (void) write(bam_lock_fd, &pid, sizeof (pid)); 11327c478bd9Sstevel@tonic-gate } 11337c478bd9Sstevel@tonic-gate 11347c478bd9Sstevel@tonic-gate static void 11357c478bd9Sstevel@tonic-gate bam_unlock(void) 11367c478bd9Sstevel@tonic-gate { 11377c478bd9Sstevel@tonic-gate struct flock unlock; 11387c478bd9Sstevel@tonic-gate 11397c478bd9Sstevel@tonic-gate /* 11407c478bd9Sstevel@tonic-gate * NOP if we don't hold the lock 11417c478bd9Sstevel@tonic-gate */ 11427c478bd9Sstevel@tonic-gate if (bam_lock_fd < 0) { 11437c478bd9Sstevel@tonic-gate return; 11447c478bd9Sstevel@tonic-gate } 11457c478bd9Sstevel@tonic-gate 11467c478bd9Sstevel@tonic-gate unlock.l_type = F_UNLCK; 11477c478bd9Sstevel@tonic-gate unlock.l_whence = SEEK_SET; 11487c478bd9Sstevel@tonic-gate unlock.l_start = 0; 11497c478bd9Sstevel@tonic-gate unlock.l_len = 0; 11507c478bd9Sstevel@tonic-gate 11517c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) { 11527c478bd9Sstevel@tonic-gate bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11537c478bd9Sstevel@tonic-gate } 11547c478bd9Sstevel@tonic-gate 11557c478bd9Sstevel@tonic-gate if (close(bam_lock_fd) == -1) { 11567c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno)); 11577c478bd9Sstevel@tonic-gate } 11587c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 11597c478bd9Sstevel@tonic-gate } 11607c478bd9Sstevel@tonic-gate 11617c478bd9Sstevel@tonic-gate static error_t 11627c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt) 11637c478bd9Sstevel@tonic-gate { 11647c478bd9Sstevel@tonic-gate filelist_t flist; 11657c478bd9Sstevel@tonic-gate filelist_t *flistp = &flist; 11667c478bd9Sstevel@tonic-gate line_t *lp; 11677c478bd9Sstevel@tonic-gate 11687c478bd9Sstevel@tonic-gate assert(root); 11697c478bd9Sstevel@tonic-gate assert(opt == NULL); 11707c478bd9Sstevel@tonic-gate 11717c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 11727c478bd9Sstevel@tonic-gate if (read_list(root, flistp) != BAM_SUCCESS) { 11737c478bd9Sstevel@tonic-gate return (BAM_ERROR); 11747c478bd9Sstevel@tonic-gate } 11757c478bd9Sstevel@tonic-gate assert(flistp->head && flistp->tail); 11767c478bd9Sstevel@tonic-gate 11777c478bd9Sstevel@tonic-gate for (lp = flistp->head; lp; lp = lp->next) { 11787c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 11797c478bd9Sstevel@tonic-gate } 11807c478bd9Sstevel@tonic-gate 11817c478bd9Sstevel@tonic-gate filelist_free(flistp); 11827c478bd9Sstevel@tonic-gate 11837c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 11847c478bd9Sstevel@tonic-gate } 11857c478bd9Sstevel@tonic-gate 11867c478bd9Sstevel@tonic-gate /* 11877c478bd9Sstevel@tonic-gate * This routine writes a list of lines to a file. 11887c478bd9Sstevel@tonic-gate * The list is *not* freed 11897c478bd9Sstevel@tonic-gate */ 11907c478bd9Sstevel@tonic-gate static error_t 11917c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start) 11927c478bd9Sstevel@tonic-gate { 11937c478bd9Sstevel@tonic-gate char tmpfile[PATH_MAX]; 11947c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 11957c478bd9Sstevel@tonic-gate FILE *fp; 11967c478bd9Sstevel@tonic-gate int ret; 11977c478bd9Sstevel@tonic-gate struct stat sb; 11987c478bd9Sstevel@tonic-gate mode_t mode; 11997c478bd9Sstevel@tonic-gate uid_t root_uid; 12007c478bd9Sstevel@tonic-gate gid_t sys_gid; 12017c478bd9Sstevel@tonic-gate struct passwd *pw; 12027c478bd9Sstevel@tonic-gate struct group *gp; 12037c478bd9Sstevel@tonic-gate 12047c478bd9Sstevel@tonic-gate 12057c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, final); 12067c478bd9Sstevel@tonic-gate 12077c478bd9Sstevel@tonic-gate if (start == NULL) { 12087c478bd9Sstevel@tonic-gate if (stat(path, &sb) != -1) { 12097c478bd9Sstevel@tonic-gate bam_print(UNLINK_EMPTY, path); 12107c478bd9Sstevel@tonic-gate if (unlink(path) != 0) { 12117c478bd9Sstevel@tonic-gate bam_error(UNLINK_FAIL, path, strerror(errno)); 12127c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12137c478bd9Sstevel@tonic-gate } else { 12147c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 12157c478bd9Sstevel@tonic-gate } 12167c478bd9Sstevel@tonic-gate } 12177c478bd9Sstevel@tonic-gate } 12187c478bd9Sstevel@tonic-gate 12197c478bd9Sstevel@tonic-gate /* 12207c478bd9Sstevel@tonic-gate * Preserve attributes of existing file if possible, 12217c478bd9Sstevel@tonic-gate * otherwise ask the system for uid/gid of root/sys. 12227c478bd9Sstevel@tonic-gate * If all fails, fall back on hard-coded defaults. 12237c478bd9Sstevel@tonic-gate */ 12247c478bd9Sstevel@tonic-gate if (stat(path, &sb) != -1) { 12257c478bd9Sstevel@tonic-gate mode = sb.st_mode; 12267c478bd9Sstevel@tonic-gate root_uid = sb.st_uid; 12277c478bd9Sstevel@tonic-gate sys_gid = sb.st_gid; 12287c478bd9Sstevel@tonic-gate } else { 12297c478bd9Sstevel@tonic-gate mode = DEFAULT_DEV_MODE; 12307c478bd9Sstevel@tonic-gate if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) { 12317c478bd9Sstevel@tonic-gate root_uid = pw->pw_uid; 12327c478bd9Sstevel@tonic-gate } else { 12337c478bd9Sstevel@tonic-gate if (bam_verbose) 12347c478bd9Sstevel@tonic-gate bam_error(CANT_FIND_USER, 12357c478bd9Sstevel@tonic-gate DEFAULT_DEV_USER, DEFAULT_DEV_UID); 12367c478bd9Sstevel@tonic-gate root_uid = (uid_t)DEFAULT_DEV_UID; 12377c478bd9Sstevel@tonic-gate } 12387c478bd9Sstevel@tonic-gate if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) { 12397c478bd9Sstevel@tonic-gate sys_gid = gp->gr_gid; 12407c478bd9Sstevel@tonic-gate } else { 12417c478bd9Sstevel@tonic-gate if (bam_verbose) 12427c478bd9Sstevel@tonic-gate bam_error(CANT_FIND_GROUP, 12437c478bd9Sstevel@tonic-gate DEFAULT_DEV_GROUP, DEFAULT_DEV_GID); 12447c478bd9Sstevel@tonic-gate sys_gid = (gid_t)DEFAULT_DEV_GID; 12457c478bd9Sstevel@tonic-gate } 12467c478bd9Sstevel@tonic-gate } 12477c478bd9Sstevel@tonic-gate 12487c478bd9Sstevel@tonic-gate (void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp); 12497c478bd9Sstevel@tonic-gate 12507c478bd9Sstevel@tonic-gate /* Truncate tmpfile first */ 12517c478bd9Sstevel@tonic-gate fp = fopen(tmpfile, "w"); 12527c478bd9Sstevel@tonic-gate if (fp == NULL) { 12537c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 12547c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12557c478bd9Sstevel@tonic-gate } 12567c478bd9Sstevel@tonic-gate ret = fclose(fp); 12577c478bd9Sstevel@tonic-gate if (ret == EOF) { 12587c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 12597c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12607c478bd9Sstevel@tonic-gate } 12617c478bd9Sstevel@tonic-gate 12627c478bd9Sstevel@tonic-gate /* Now open it in append mode */ 12637c478bd9Sstevel@tonic-gate fp = fopen(tmpfile, "a"); 12647c478bd9Sstevel@tonic-gate if (fp == NULL) { 12657c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 12667c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12677c478bd9Sstevel@tonic-gate } 12687c478bd9Sstevel@tonic-gate 12697c478bd9Sstevel@tonic-gate for (; start; start = start->next) { 12707c478bd9Sstevel@tonic-gate ret = s_fputs(start->line, fp); 12717c478bd9Sstevel@tonic-gate if (ret == EOF) { 12727c478bd9Sstevel@tonic-gate bam_error(WRITE_FAIL, tmpfile, strerror(errno)); 12737c478bd9Sstevel@tonic-gate (void) fclose(fp); 12747c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12757c478bd9Sstevel@tonic-gate } 12767c478bd9Sstevel@tonic-gate } 12777c478bd9Sstevel@tonic-gate 12787c478bd9Sstevel@tonic-gate ret = fclose(fp); 12797c478bd9Sstevel@tonic-gate if (ret == EOF) { 12807c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 12817c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12827c478bd9Sstevel@tonic-gate } 12837c478bd9Sstevel@tonic-gate 12847c478bd9Sstevel@tonic-gate /* 1285de531be4Sjg * Set up desired attributes. Ignore failures on filesystems 1286de531be4Sjg * not supporting these operations - pcfs reports unsupported 1287de531be4Sjg * operations as EINVAL. 12887c478bd9Sstevel@tonic-gate */ 12897c478bd9Sstevel@tonic-gate ret = chmod(tmpfile, mode); 1290de531be4Sjg if (ret == -1 && 1291de531be4Sjg errno != EINVAL && errno != ENOTSUP) { 12927c478bd9Sstevel@tonic-gate bam_error(CHMOD_FAIL, tmpfile, strerror(errno)); 12937c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12947c478bd9Sstevel@tonic-gate } 12957c478bd9Sstevel@tonic-gate 12967c478bd9Sstevel@tonic-gate ret = chown(tmpfile, root_uid, sys_gid); 1297de531be4Sjg if (ret == -1 && 1298de531be4Sjg errno != EINVAL && errno != ENOTSUP) { 12997c478bd9Sstevel@tonic-gate bam_error(CHOWN_FAIL, tmpfile, strerror(errno)); 13007c478bd9Sstevel@tonic-gate return (BAM_ERROR); 13017c478bd9Sstevel@tonic-gate } 13027c478bd9Sstevel@tonic-gate 13037c478bd9Sstevel@tonic-gate 13047c478bd9Sstevel@tonic-gate /* 13057c478bd9Sstevel@tonic-gate * Do an atomic rename 13067c478bd9Sstevel@tonic-gate */ 13077c478bd9Sstevel@tonic-gate ret = rename(tmpfile, path); 13087c478bd9Sstevel@tonic-gate if (ret != 0) { 13097c478bd9Sstevel@tonic-gate bam_error(RENAME_FAIL, path, strerror(errno)); 13107c478bd9Sstevel@tonic-gate return (BAM_ERROR); 13117c478bd9Sstevel@tonic-gate } 13127c478bd9Sstevel@tonic-gate 13137c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 13147c478bd9Sstevel@tonic-gate } 13157c478bd9Sstevel@tonic-gate 13167c478bd9Sstevel@tonic-gate /* 13177c478bd9Sstevel@tonic-gate * This function should always return 0 - since we want 13187c478bd9Sstevel@tonic-gate * to create stat data for *all* files in the list. 13197c478bd9Sstevel@tonic-gate */ 13207c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 13217c478bd9Sstevel@tonic-gate static int 13227c478bd9Sstevel@tonic-gate cmpstat( 13237c478bd9Sstevel@tonic-gate const char *file, 13247c478bd9Sstevel@tonic-gate const struct stat *stat, 13257c478bd9Sstevel@tonic-gate int flags, 13267c478bd9Sstevel@tonic-gate struct FTW *ftw) 13277c478bd9Sstevel@tonic-gate { 13287c478bd9Sstevel@tonic-gate uint_t sz; 13297c478bd9Sstevel@tonic-gate uint64_t *value; 13307c478bd9Sstevel@tonic-gate uint64_t filestat[2]; 13317c478bd9Sstevel@tonic-gate int error; 13327c478bd9Sstevel@tonic-gate 13337c478bd9Sstevel@tonic-gate /* 13347c478bd9Sstevel@tonic-gate * We only want regular files 13357c478bd9Sstevel@tonic-gate */ 13367c478bd9Sstevel@tonic-gate if (!S_ISREG(stat->st_mode)) 13377c478bd9Sstevel@tonic-gate return (0); 13387c478bd9Sstevel@tonic-gate 13397c478bd9Sstevel@tonic-gate /* 13407c478bd9Sstevel@tonic-gate * new_nvlp may be NULL if there were errors earlier 13417c478bd9Sstevel@tonic-gate * but this is not fatal to update determination. 13427c478bd9Sstevel@tonic-gate */ 13437c478bd9Sstevel@tonic-gate if (walk_arg.new_nvlp) { 13447c478bd9Sstevel@tonic-gate filestat[0] = stat->st_size; 13457c478bd9Sstevel@tonic-gate filestat[1] = stat->st_mtime; 13467c478bd9Sstevel@tonic-gate error = nvlist_add_uint64_array(walk_arg.new_nvlp, 13477c478bd9Sstevel@tonic-gate file + bam_rootlen, filestat, 2); 13487c478bd9Sstevel@tonic-gate if (error) 13497c478bd9Sstevel@tonic-gate bam_error(NVADD_FAIL, file, strerror(error)); 13507c478bd9Sstevel@tonic-gate } 13517c478bd9Sstevel@tonic-gate 13527c478bd9Sstevel@tonic-gate /* 13537c478bd9Sstevel@tonic-gate * The remaining steps are only required if we haven't made a 13547c478bd9Sstevel@tonic-gate * decision about update or if we are checking (-n) 13557c478bd9Sstevel@tonic-gate */ 13567c478bd9Sstevel@tonic-gate if (walk_arg.need_update && !bam_check) 13577c478bd9Sstevel@tonic-gate return (0); 13587c478bd9Sstevel@tonic-gate 13597c478bd9Sstevel@tonic-gate /* 13607c478bd9Sstevel@tonic-gate * If we are invoked as part of system/filesyste/boot-archive 13617c478bd9Sstevel@tonic-gate * SMF service, ignore amd64 modules unless we are booted amd64. 13627c478bd9Sstevel@tonic-gate */ 13637c478bd9Sstevel@tonic-gate if (bam_smf_check && !is_amd64() && strstr(file, "/amd64/") == 0) 13647c478bd9Sstevel@tonic-gate return (0); 13657c478bd9Sstevel@tonic-gate 13667c478bd9Sstevel@tonic-gate /* 13677c478bd9Sstevel@tonic-gate * We need an update if file doesn't exist in old archive 13687c478bd9Sstevel@tonic-gate */ 13697c478bd9Sstevel@tonic-gate if (walk_arg.old_nvlp == NULL || 13707c478bd9Sstevel@tonic-gate nvlist_lookup_uint64_array(walk_arg.old_nvlp, 13717c478bd9Sstevel@tonic-gate file + bam_rootlen, &value, &sz) != 0) { 13727c478bd9Sstevel@tonic-gate if (bam_smf_check) /* ignore new during smf check */ 13737c478bd9Sstevel@tonic-gate return (0); 13747c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 13757c478bd9Sstevel@tonic-gate if (bam_verbose) 13767c478bd9Sstevel@tonic-gate bam_print(PARSEABLE_NEW_FILE, file); 13777c478bd9Sstevel@tonic-gate return (0); 13787c478bd9Sstevel@tonic-gate } 13797c478bd9Sstevel@tonic-gate 13807c478bd9Sstevel@tonic-gate /* 13817c478bd9Sstevel@tonic-gate * File exists in old archive. Check if file has changed 13827c478bd9Sstevel@tonic-gate */ 13837c478bd9Sstevel@tonic-gate assert(sz == 2); 13847c478bd9Sstevel@tonic-gate bcopy(value, filestat, sizeof (filestat)); 13857c478bd9Sstevel@tonic-gate 13867c478bd9Sstevel@tonic-gate if (filestat[0] != stat->st_size || 13877c478bd9Sstevel@tonic-gate filestat[1] != stat->st_mtime) { 13887c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 13897c478bd9Sstevel@tonic-gate if (bam_verbose) 13907c478bd9Sstevel@tonic-gate if (bam_smf_check) 13917c478bd9Sstevel@tonic-gate bam_print(" %s\n", file); 13927c478bd9Sstevel@tonic-gate else 13937c478bd9Sstevel@tonic-gate bam_print(PARSEABLE_OUT_DATE, file); 13947c478bd9Sstevel@tonic-gate } 13957c478bd9Sstevel@tonic-gate 13967c478bd9Sstevel@tonic-gate return (0); 13977c478bd9Sstevel@tonic-gate } 13987c478bd9Sstevel@tonic-gate 13997c478bd9Sstevel@tonic-gate /* 14007c478bd9Sstevel@tonic-gate * Check flags and presence of required files. 14017c478bd9Sstevel@tonic-gate * The force flag and/or absence of files should 14027c478bd9Sstevel@tonic-gate * trigger an update. 14037c478bd9Sstevel@tonic-gate * Suppress stdout output if check (-n) option is set 14047c478bd9Sstevel@tonic-gate * (as -n should only produce parseable output.) 14057c478bd9Sstevel@tonic-gate */ 14067c478bd9Sstevel@tonic-gate static void 14077c478bd9Sstevel@tonic-gate check_flags_and_files(char *root) 14087c478bd9Sstevel@tonic-gate { 14097c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 14107c478bd9Sstevel@tonic-gate struct stat sb; 14117c478bd9Sstevel@tonic-gate 14127c478bd9Sstevel@tonic-gate /* 14137c478bd9Sstevel@tonic-gate * if force, create archive unconditionally 14147c478bd9Sstevel@tonic-gate */ 14157c478bd9Sstevel@tonic-gate if (bam_force) { 14167c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 14177c478bd9Sstevel@tonic-gate if (bam_verbose && !bam_check) 14187c478bd9Sstevel@tonic-gate bam_print(UPDATE_FORCE); 14197c478bd9Sstevel@tonic-gate return; 14207c478bd9Sstevel@tonic-gate } 14217c478bd9Sstevel@tonic-gate 14227c478bd9Sstevel@tonic-gate /* 14237c478bd9Sstevel@tonic-gate * If archive is missing, create archive 14247c478bd9Sstevel@tonic-gate */ 14257c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE); 14267c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) { 14277c478bd9Sstevel@tonic-gate if (bam_verbose && !bam_check) 14287c478bd9Sstevel@tonic-gate bam_print(UPDATE_ARCH_MISS, path); 14297c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 14307c478bd9Sstevel@tonic-gate return; 14317c478bd9Sstevel@tonic-gate } 14327c478bd9Sstevel@tonic-gate } 14337c478bd9Sstevel@tonic-gate 14347c478bd9Sstevel@tonic-gate static error_t 14357c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t *flistp, char *filelist) 14367c478bd9Sstevel@tonic-gate { 14377c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 14387c478bd9Sstevel@tonic-gate FILE *fp; 14397c478bd9Sstevel@tonic-gate char buf[BAM_MAXLINE]; 14407c478bd9Sstevel@tonic-gate 14417c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, filelist); 14427c478bd9Sstevel@tonic-gate 14437c478bd9Sstevel@tonic-gate fp = fopen(path, "r"); 14447c478bd9Sstevel@tonic-gate if (fp == NULL) { 14457c478bd9Sstevel@tonic-gate if (bam_debug) 14467c478bd9Sstevel@tonic-gate bam_error(FLIST_FAIL, path, strerror(errno)); 14477c478bd9Sstevel@tonic-gate return (BAM_ERROR); 14487c478bd9Sstevel@tonic-gate } 14497c478bd9Sstevel@tonic-gate while (s_fgets(buf, sizeof (buf), fp) != NULL) { 1450b610f78eSvikram /* skip blank lines */ 1451b610f78eSvikram if (strspn(buf, " \t") == strlen(buf)) 1452b610f78eSvikram continue; 14537c478bd9Sstevel@tonic-gate append_to_flist(flistp, buf); 14547c478bd9Sstevel@tonic-gate } 14557c478bd9Sstevel@tonic-gate if (fclose(fp) != 0) { 14567c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, path, strerror(errno)); 14577c478bd9Sstevel@tonic-gate return (BAM_ERROR); 14587c478bd9Sstevel@tonic-gate } 14597c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 14607c478bd9Sstevel@tonic-gate } 14617c478bd9Sstevel@tonic-gate 14627c478bd9Sstevel@tonic-gate static error_t 14637c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t *flistp) 14647c478bd9Sstevel@tonic-gate { 14657c478bd9Sstevel@tonic-gate int rval; 14667c478bd9Sstevel@tonic-gate 14677c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 14687c478bd9Sstevel@tonic-gate 14697c478bd9Sstevel@tonic-gate /* 14707c478bd9Sstevel@tonic-gate * Read current lists of files - only the first is mandatory 14717c478bd9Sstevel@tonic-gate */ 14727c478bd9Sstevel@tonic-gate rval = read_one_list(root, flistp, BOOT_FILE_LIST); 14737c478bd9Sstevel@tonic-gate if (rval != BAM_SUCCESS) 14747c478bd9Sstevel@tonic-gate return (rval); 14757c478bd9Sstevel@tonic-gate (void) read_one_list(root, flistp, ETC_FILE_LIST); 14767c478bd9Sstevel@tonic-gate 14777c478bd9Sstevel@tonic-gate if (flistp->head == NULL) { 14787c478bd9Sstevel@tonic-gate bam_error(NO_FLIST); 14797c478bd9Sstevel@tonic-gate return (BAM_ERROR); 14807c478bd9Sstevel@tonic-gate } 14817c478bd9Sstevel@tonic-gate 14827c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 14837c478bd9Sstevel@tonic-gate } 14847c478bd9Sstevel@tonic-gate 14857c478bd9Sstevel@tonic-gate static void 14867c478bd9Sstevel@tonic-gate getoldstat(char *root) 14877c478bd9Sstevel@tonic-gate { 14887c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 14897c478bd9Sstevel@tonic-gate int fd, error; 14907c478bd9Sstevel@tonic-gate struct stat sb; 14917c478bd9Sstevel@tonic-gate char *ostat; 14927c478bd9Sstevel@tonic-gate 14937c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 14947c478bd9Sstevel@tonic-gate fd = open(path, O_RDONLY); 14957c478bd9Sstevel@tonic-gate if (fd == -1) { 14967c478bd9Sstevel@tonic-gate if (bam_verbose) 14977c478bd9Sstevel@tonic-gate bam_print(OPEN_FAIL, path, strerror(errno)); 14987c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 14997c478bd9Sstevel@tonic-gate return; 15007c478bd9Sstevel@tonic-gate } 15017c478bd9Sstevel@tonic-gate 15027c478bd9Sstevel@tonic-gate if (fstat(fd, &sb) != 0) { 15037c478bd9Sstevel@tonic-gate bam_error(STAT_FAIL, path, strerror(errno)); 15047c478bd9Sstevel@tonic-gate (void) close(fd); 15057c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 15067c478bd9Sstevel@tonic-gate return; 15077c478bd9Sstevel@tonic-gate } 15087c478bd9Sstevel@tonic-gate 15097c478bd9Sstevel@tonic-gate ostat = s_calloc(1, sb.st_size); 15107c478bd9Sstevel@tonic-gate 15117c478bd9Sstevel@tonic-gate if (read(fd, ostat, sb.st_size) != sb.st_size) { 15127c478bd9Sstevel@tonic-gate bam_error(READ_FAIL, path, strerror(errno)); 15137c478bd9Sstevel@tonic-gate (void) close(fd); 15147c478bd9Sstevel@tonic-gate free(ostat); 15157c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 15167c478bd9Sstevel@tonic-gate return; 15177c478bd9Sstevel@tonic-gate } 15187c478bd9Sstevel@tonic-gate 15197c478bd9Sstevel@tonic-gate (void) close(fd); 15207c478bd9Sstevel@tonic-gate 15217c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 15227c478bd9Sstevel@tonic-gate error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0); 15237c478bd9Sstevel@tonic-gate 15247c478bd9Sstevel@tonic-gate free(ostat); 15257c478bd9Sstevel@tonic-gate 15267c478bd9Sstevel@tonic-gate if (error) { 15277c478bd9Sstevel@tonic-gate bam_error(UNPACK_FAIL, path, strerror(error)); 15287c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 15297c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 15307c478bd9Sstevel@tonic-gate return; 15317c478bd9Sstevel@tonic-gate } 15327c478bd9Sstevel@tonic-gate } 15337c478bd9Sstevel@tonic-gate 15347c478bd9Sstevel@tonic-gate static void 15357c478bd9Sstevel@tonic-gate create_newstat(void) 15367c478bd9Sstevel@tonic-gate { 15377c478bd9Sstevel@tonic-gate int error; 15387c478bd9Sstevel@tonic-gate 15397c478bd9Sstevel@tonic-gate error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0); 15407c478bd9Sstevel@tonic-gate if (error) { 15417c478bd9Sstevel@tonic-gate /* 15427c478bd9Sstevel@tonic-gate * Not fatal - we can still create archive 15437c478bd9Sstevel@tonic-gate */ 15447c478bd9Sstevel@tonic-gate walk_arg.new_nvlp = NULL; 15457c478bd9Sstevel@tonic-gate bam_error(NVALLOC_FAIL, strerror(error)); 15467c478bd9Sstevel@tonic-gate } 15477c478bd9Sstevel@tonic-gate } 15487c478bd9Sstevel@tonic-gate 15497c478bd9Sstevel@tonic-gate static void 15507c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp) 15517c478bd9Sstevel@tonic-gate { 15527c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 15537c478bd9Sstevel@tonic-gate line_t *lp; 15547c478bd9Sstevel@tonic-gate 15557c478bd9Sstevel@tonic-gate for (lp = flistp->head; lp; lp = lp->next) { 15567c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, lp->line); 15577c478bd9Sstevel@tonic-gate /* XXX shouldn't we use FTW_MOUNT ? */ 15587c478bd9Sstevel@tonic-gate if (nftw(path, cmpstat, 20, 0) == -1) { 15597c478bd9Sstevel@tonic-gate /* 15607c478bd9Sstevel@tonic-gate * Some files may not exist. 15617c478bd9Sstevel@tonic-gate * For example: etc/rtc_config on a x86 diskless system 15627c478bd9Sstevel@tonic-gate * Emit verbose message only 15637c478bd9Sstevel@tonic-gate */ 15647c478bd9Sstevel@tonic-gate if (bam_verbose) 15657c478bd9Sstevel@tonic-gate bam_print(NFTW_FAIL, path, strerror(errno)); 15667c478bd9Sstevel@tonic-gate } 15677c478bd9Sstevel@tonic-gate } 15687c478bd9Sstevel@tonic-gate } 15697c478bd9Sstevel@tonic-gate 15707c478bd9Sstevel@tonic-gate static void 15717c478bd9Sstevel@tonic-gate savenew(char *root) 15727c478bd9Sstevel@tonic-gate { 15737c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 15747c478bd9Sstevel@tonic-gate char path2[PATH_MAX]; 15757c478bd9Sstevel@tonic-gate size_t sz; 15767c478bd9Sstevel@tonic-gate char *nstat; 15777c478bd9Sstevel@tonic-gate int fd, wrote, error; 15787c478bd9Sstevel@tonic-gate 15797c478bd9Sstevel@tonic-gate nstat = NULL; 15807c478bd9Sstevel@tonic-gate sz = 0; 15817c478bd9Sstevel@tonic-gate error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz, 15827c478bd9Sstevel@tonic-gate NV_ENCODE_XDR, 0); 15837c478bd9Sstevel@tonic-gate if (error) { 15847c478bd9Sstevel@tonic-gate bam_error(PACK_FAIL, strerror(error)); 15857c478bd9Sstevel@tonic-gate return; 15867c478bd9Sstevel@tonic-gate } 15877c478bd9Sstevel@tonic-gate 15887c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP); 15897c478bd9Sstevel@tonic-gate fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE); 15907c478bd9Sstevel@tonic-gate if (fd == -1) { 15917c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, path, strerror(errno)); 15927c478bd9Sstevel@tonic-gate free(nstat); 15937c478bd9Sstevel@tonic-gate return; 15947c478bd9Sstevel@tonic-gate } 15957c478bd9Sstevel@tonic-gate wrote = write(fd, nstat, sz); 15967c478bd9Sstevel@tonic-gate if (wrote != sz) { 15977c478bd9Sstevel@tonic-gate bam_error(WRITE_FAIL, path, strerror(errno)); 15987c478bd9Sstevel@tonic-gate (void) close(fd); 15997c478bd9Sstevel@tonic-gate free(nstat); 16007c478bd9Sstevel@tonic-gate return; 16017c478bd9Sstevel@tonic-gate } 16027c478bd9Sstevel@tonic-gate (void) close(fd); 16037c478bd9Sstevel@tonic-gate free(nstat); 16047c478bd9Sstevel@tonic-gate 16057c478bd9Sstevel@tonic-gate (void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT); 16067c478bd9Sstevel@tonic-gate if (rename(path, path2) != 0) { 16077c478bd9Sstevel@tonic-gate bam_error(RENAME_FAIL, path2, strerror(errno)); 16087c478bd9Sstevel@tonic-gate } 16097c478bd9Sstevel@tonic-gate } 16107c478bd9Sstevel@tonic-gate 16117c478bd9Sstevel@tonic-gate static void 16127c478bd9Sstevel@tonic-gate clear_walk_args(void) 16137c478bd9Sstevel@tonic-gate { 16147c478bd9Sstevel@tonic-gate if (walk_arg.old_nvlp) 16157c478bd9Sstevel@tonic-gate nvlist_free(walk_arg.old_nvlp); 16167c478bd9Sstevel@tonic-gate if (walk_arg.new_nvlp) 16177c478bd9Sstevel@tonic-gate nvlist_free(walk_arg.new_nvlp); 16187c478bd9Sstevel@tonic-gate walk_arg.need_update = 0; 16197c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 16207c478bd9Sstevel@tonic-gate walk_arg.new_nvlp = NULL; 16217c478bd9Sstevel@tonic-gate } 16227c478bd9Sstevel@tonic-gate 16237c478bd9Sstevel@tonic-gate /* 16247c478bd9Sstevel@tonic-gate * Returns: 16257c478bd9Sstevel@tonic-gate * 0 - no update necessary 16267c478bd9Sstevel@tonic-gate * 1 - update required. 16277c478bd9Sstevel@tonic-gate * BAM_ERROR (-1) - An error occurred 16287c478bd9Sstevel@tonic-gate * 16297c478bd9Sstevel@tonic-gate * Special handling for check (-n): 16307c478bd9Sstevel@tonic-gate * ================================ 16317c478bd9Sstevel@tonic-gate * The check (-n) option produces parseable output. 16327c478bd9Sstevel@tonic-gate * To do this, we suppress all stdout messages unrelated 16337c478bd9Sstevel@tonic-gate * to out of sync files. 16347c478bd9Sstevel@tonic-gate * All stderr messages are still printed though. 16357c478bd9Sstevel@tonic-gate * 16367c478bd9Sstevel@tonic-gate */ 16377c478bd9Sstevel@tonic-gate static int 16387c478bd9Sstevel@tonic-gate update_required(char *root) 16397c478bd9Sstevel@tonic-gate { 16407c478bd9Sstevel@tonic-gate struct stat sb; 16417c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 16427c478bd9Sstevel@tonic-gate filelist_t flist; 16437c478bd9Sstevel@tonic-gate filelist_t *flistp = &flist; 16447c478bd9Sstevel@tonic-gate int need_update; 16457c478bd9Sstevel@tonic-gate 16467c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 16477c478bd9Sstevel@tonic-gate 16487c478bd9Sstevel@tonic-gate walk_arg.need_update = 0; 16497c478bd9Sstevel@tonic-gate 16507c478bd9Sstevel@tonic-gate /* 16517c478bd9Sstevel@tonic-gate * Without consulting stat data, check if we need update 16527c478bd9Sstevel@tonic-gate */ 16537c478bd9Sstevel@tonic-gate check_flags_and_files(root); 16547c478bd9Sstevel@tonic-gate 16557c478bd9Sstevel@tonic-gate /* 16567c478bd9Sstevel@tonic-gate * In certain deployment scenarios, filestat may not 16577c478bd9Sstevel@tonic-gate * exist. Ignore it during boot-archive SMF check. 16587c478bd9Sstevel@tonic-gate */ 16597c478bd9Sstevel@tonic-gate if (bam_smf_check) { 16607c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 16617c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) 16627c478bd9Sstevel@tonic-gate return (0); 16637c478bd9Sstevel@tonic-gate } 16647c478bd9Sstevel@tonic-gate 16657c478bd9Sstevel@tonic-gate /* 16667c478bd9Sstevel@tonic-gate * consult stat data only if we haven't made a decision 16677c478bd9Sstevel@tonic-gate * about update. If checking (-n) however, we always 16687c478bd9Sstevel@tonic-gate * need stat data (since we want to compare old and new) 16697c478bd9Sstevel@tonic-gate */ 16707c478bd9Sstevel@tonic-gate if (!walk_arg.need_update || bam_check) 16717c478bd9Sstevel@tonic-gate getoldstat(root); 16727c478bd9Sstevel@tonic-gate 16737c478bd9Sstevel@tonic-gate /* 16747c478bd9Sstevel@tonic-gate * read list of files 16757c478bd9Sstevel@tonic-gate */ 16767c478bd9Sstevel@tonic-gate if (read_list(root, flistp) != BAM_SUCCESS) { 16777c478bd9Sstevel@tonic-gate clear_walk_args(); 16787c478bd9Sstevel@tonic-gate return (BAM_ERROR); 16797c478bd9Sstevel@tonic-gate } 16807c478bd9Sstevel@tonic-gate 16817c478bd9Sstevel@tonic-gate assert(flistp->head && flistp->tail); 16827c478bd9Sstevel@tonic-gate 16837c478bd9Sstevel@tonic-gate /* 16847c478bd9Sstevel@tonic-gate * At this point either the update is required 16857c478bd9Sstevel@tonic-gate * or the decision is pending. In either case 16867c478bd9Sstevel@tonic-gate * we need to create new stat nvlist 16877c478bd9Sstevel@tonic-gate */ 16887c478bd9Sstevel@tonic-gate create_newstat(); 16897c478bd9Sstevel@tonic-gate 16907c478bd9Sstevel@tonic-gate /* 16917c478bd9Sstevel@tonic-gate * This walk does 2 things: 16927c478bd9Sstevel@tonic-gate * - gets new stat data for every file 16937c478bd9Sstevel@tonic-gate * - (optional) compare old and new stat data 16947c478bd9Sstevel@tonic-gate */ 16957c478bd9Sstevel@tonic-gate walk_list(root, &flist); 16967c478bd9Sstevel@tonic-gate 16977c478bd9Sstevel@tonic-gate /* done with the file list */ 16987c478bd9Sstevel@tonic-gate filelist_free(flistp); 16997c478bd9Sstevel@tonic-gate 17007c478bd9Sstevel@tonic-gate /* 17017c478bd9Sstevel@tonic-gate * if we didn't succeed in creating new stat data above 17027c478bd9Sstevel@tonic-gate * just return result of update check so that archive is built. 17037c478bd9Sstevel@tonic-gate */ 17047c478bd9Sstevel@tonic-gate if (walk_arg.new_nvlp == NULL) { 17057c478bd9Sstevel@tonic-gate bam_error(NO_NEW_STAT); 17067c478bd9Sstevel@tonic-gate need_update = walk_arg.need_update; 17077c478bd9Sstevel@tonic-gate clear_walk_args(); 17087c478bd9Sstevel@tonic-gate return (need_update ? 1 : 0); 17097c478bd9Sstevel@tonic-gate } 17107c478bd9Sstevel@tonic-gate 17117c478bd9Sstevel@tonic-gate 17127c478bd9Sstevel@tonic-gate /* 17137c478bd9Sstevel@tonic-gate * If no update required, discard newstat 17147c478bd9Sstevel@tonic-gate */ 17157c478bd9Sstevel@tonic-gate if (!walk_arg.need_update) { 17167c478bd9Sstevel@tonic-gate clear_walk_args(); 17177c478bd9Sstevel@tonic-gate return (0); 17187c478bd9Sstevel@tonic-gate } 17197c478bd9Sstevel@tonic-gate 17207c478bd9Sstevel@tonic-gate /* 17217c478bd9Sstevel@tonic-gate * At this point we need an update - so save new stat data 17227c478bd9Sstevel@tonic-gate * However, if only checking (-n), don't save new stat data. 17237c478bd9Sstevel@tonic-gate */ 17247c478bd9Sstevel@tonic-gate if (!bam_check) 17257c478bd9Sstevel@tonic-gate savenew(root); 17267c478bd9Sstevel@tonic-gate 17277c478bd9Sstevel@tonic-gate clear_walk_args(); 17287c478bd9Sstevel@tonic-gate 17297c478bd9Sstevel@tonic-gate return (1); 17307c478bd9Sstevel@tonic-gate } 17317c478bd9Sstevel@tonic-gate 17327c478bd9Sstevel@tonic-gate static error_t 17337c478bd9Sstevel@tonic-gate create_ramdisk(char *root) 17347c478bd9Sstevel@tonic-gate { 17357c478bd9Sstevel@tonic-gate char *cmdline, path[PATH_MAX]; 17367c478bd9Sstevel@tonic-gate size_t len; 17377c478bd9Sstevel@tonic-gate struct stat sb; 17387c478bd9Sstevel@tonic-gate 17397c478bd9Sstevel@tonic-gate /* 17407c478bd9Sstevel@tonic-gate * Setup command args for create_ramdisk.ksh 17417c478bd9Sstevel@tonic-gate */ 17427c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK); 17437c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) { 17447c478bd9Sstevel@tonic-gate bam_error(ARCH_EXEC_MISS, path, strerror(errno)); 17457c478bd9Sstevel@tonic-gate return (BAM_ERROR); 17467c478bd9Sstevel@tonic-gate } 17477c478bd9Sstevel@tonic-gate 17487c478bd9Sstevel@tonic-gate len = strlen(path) + strlen(root) + 10; /* room for space + -R */ 17497c478bd9Sstevel@tonic-gate cmdline = s_calloc(1, len); 17507c478bd9Sstevel@tonic-gate 17517c478bd9Sstevel@tonic-gate if (strlen(root) > 1) { 17527c478bd9Sstevel@tonic-gate (void) snprintf(cmdline, len, "%s -R %s", path, root); 17537c478bd9Sstevel@tonic-gate /* chop off / at the end */ 17547c478bd9Sstevel@tonic-gate cmdline[strlen(cmdline) - 1] = '\0'; 17557c478bd9Sstevel@tonic-gate } else 17567c478bd9Sstevel@tonic-gate (void) snprintf(cmdline, len, "%s", path); 17577c478bd9Sstevel@tonic-gate 17587c478bd9Sstevel@tonic-gate if (exec_cmd(cmdline, NULL, 0) != 0) { 17597c478bd9Sstevel@tonic-gate bam_error(ARCHIVE_FAIL, cmdline); 17607c478bd9Sstevel@tonic-gate free(cmdline); 17617c478bd9Sstevel@tonic-gate return (BAM_ERROR); 17627c478bd9Sstevel@tonic-gate } 17637c478bd9Sstevel@tonic-gate free(cmdline); 17647c478bd9Sstevel@tonic-gate 17657c478bd9Sstevel@tonic-gate /* 17667c478bd9Sstevel@tonic-gate * Verify that the archive has been created 17677c478bd9Sstevel@tonic-gate */ 17687c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE); 17697c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) { 17707c478bd9Sstevel@tonic-gate bam_error(ARCHIVE_NOT_CREATED, path); 17717c478bd9Sstevel@tonic-gate return (BAM_ERROR); 17727c478bd9Sstevel@tonic-gate } 17737c478bd9Sstevel@tonic-gate 17747c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 17757c478bd9Sstevel@tonic-gate } 17767c478bd9Sstevel@tonic-gate 17777c478bd9Sstevel@tonic-gate /* 17787c478bd9Sstevel@tonic-gate * Checks if target filesystem is on a ramdisk 17797c478bd9Sstevel@tonic-gate * 1 - is miniroot 17807c478bd9Sstevel@tonic-gate * 0 - is not 17817c478bd9Sstevel@tonic-gate * When in doubt assume it is not a ramdisk. 17827c478bd9Sstevel@tonic-gate */ 17837c478bd9Sstevel@tonic-gate static int 17847c478bd9Sstevel@tonic-gate is_ramdisk(char *root) 17857c478bd9Sstevel@tonic-gate { 17867c478bd9Sstevel@tonic-gate struct extmnttab mnt; 17877c478bd9Sstevel@tonic-gate FILE *fp; 17887c478bd9Sstevel@tonic-gate int found; 1789b610f78eSvikram char mntpt[PATH_MAX]; 1790b610f78eSvikram char *cp; 17917c478bd9Sstevel@tonic-gate 17927c478bd9Sstevel@tonic-gate /* 17937c478bd9Sstevel@tonic-gate * There are 3 situations where creating archive is 17947c478bd9Sstevel@tonic-gate * of dubious value: 1795b610f78eSvikram * - create boot_archive on a lofi-mounted boot_archive 17967c478bd9Sstevel@tonic-gate * - create it on a ramdisk which is the root filesystem 17977c478bd9Sstevel@tonic-gate * - create it on a ramdisk mounted somewhere else 17987c478bd9Sstevel@tonic-gate * The first is not easy to detect and checking for it is not 17997c478bd9Sstevel@tonic-gate * worth it. 18007c478bd9Sstevel@tonic-gate * The other two conditions are handled here 18017c478bd9Sstevel@tonic-gate */ 18027c478bd9Sstevel@tonic-gate 18037c478bd9Sstevel@tonic-gate fp = fopen(MNTTAB, "r"); 18047c478bd9Sstevel@tonic-gate if (fp == NULL) { 18057c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 18067c478bd9Sstevel@tonic-gate return (0); 18077c478bd9Sstevel@tonic-gate } 18087c478bd9Sstevel@tonic-gate 18097c478bd9Sstevel@tonic-gate resetmnttab(fp); 18107c478bd9Sstevel@tonic-gate 1811b610f78eSvikram /* 1812b610f78eSvikram * Remove any trailing / from the mount point 1813b610f78eSvikram */ 1814b610f78eSvikram (void) strlcpy(mntpt, root, sizeof (mntpt)); 1815b610f78eSvikram if (strcmp(root, "/") != 0) { 1816b610f78eSvikram cp = mntpt + strlen(mntpt) - 1; 1817b610f78eSvikram if (*cp == '/') 1818b610f78eSvikram *cp = '\0'; 1819b610f78eSvikram } 18207c478bd9Sstevel@tonic-gate found = 0; 18217c478bd9Sstevel@tonic-gate while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 1822b610f78eSvikram if (strcmp(mnt.mnt_mountp, mntpt) == 0) { 18237c478bd9Sstevel@tonic-gate found = 1; 18247c478bd9Sstevel@tonic-gate break; 18257c478bd9Sstevel@tonic-gate } 18267c478bd9Sstevel@tonic-gate } 18277c478bd9Sstevel@tonic-gate 18287c478bd9Sstevel@tonic-gate if (!found) { 18297c478bd9Sstevel@tonic-gate if (bam_verbose) 1830b610f78eSvikram bam_error(NOT_IN_MNTTAB, mntpt); 18317c478bd9Sstevel@tonic-gate (void) fclose(fp); 18327c478bd9Sstevel@tonic-gate return (0); 18337c478bd9Sstevel@tonic-gate } 18347c478bd9Sstevel@tonic-gate 18357c478bd9Sstevel@tonic-gate if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) { 18367c478bd9Sstevel@tonic-gate if (bam_verbose) 18377c478bd9Sstevel@tonic-gate bam_error(IS_RAMDISK, bam_root); 18387c478bd9Sstevel@tonic-gate (void) fclose(fp); 18397c478bd9Sstevel@tonic-gate return (1); 18407c478bd9Sstevel@tonic-gate } 18417c478bd9Sstevel@tonic-gate 18427c478bd9Sstevel@tonic-gate (void) fclose(fp); 18437c478bd9Sstevel@tonic-gate 18447c478bd9Sstevel@tonic-gate return (0); 18457c478bd9Sstevel@tonic-gate } 18467c478bd9Sstevel@tonic-gate 18477c478bd9Sstevel@tonic-gate static int 18487c478bd9Sstevel@tonic-gate is_newboot(char *root) 18497c478bd9Sstevel@tonic-gate { 18507c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 18517c478bd9Sstevel@tonic-gate struct stat sb; 18527c478bd9Sstevel@tonic-gate 18537c478bd9Sstevel@tonic-gate /* 18547c478bd9Sstevel@tonic-gate * We can't boot without MULTI_BOOT 18557c478bd9Sstevel@tonic-gate */ 18567c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT); 18577c478bd9Sstevel@tonic-gate if (stat(path, &sb) == -1) { 18587c478bd9Sstevel@tonic-gate if (bam_verbose) 18597c478bd9Sstevel@tonic-gate bam_print(FILE_MISS, path); 18607c478bd9Sstevel@tonic-gate return (0); 18617c478bd9Sstevel@tonic-gate } 18627c478bd9Sstevel@tonic-gate 18637c478bd9Sstevel@tonic-gate /* 18647c478bd9Sstevel@tonic-gate * We can't generate archive without GRUB_DIR 18657c478bd9Sstevel@tonic-gate */ 18667c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR); 18677c478bd9Sstevel@tonic-gate if (stat(path, &sb) == -1) { 18687c478bd9Sstevel@tonic-gate if (bam_verbose) 18697c478bd9Sstevel@tonic-gate bam_print(DIR_MISS, path); 18707c478bd9Sstevel@tonic-gate return (0); 18717c478bd9Sstevel@tonic-gate } 18727c478bd9Sstevel@tonic-gate 18737c478bd9Sstevel@tonic-gate return (1); 18747c478bd9Sstevel@tonic-gate } 18757c478bd9Sstevel@tonic-gate 18767c478bd9Sstevel@tonic-gate static int 18777c478bd9Sstevel@tonic-gate is_readonly(char *root) 18787c478bd9Sstevel@tonic-gate { 18797c478bd9Sstevel@tonic-gate struct statvfs vfs; 18807c478bd9Sstevel@tonic-gate 18817c478bd9Sstevel@tonic-gate /* 18827c478bd9Sstevel@tonic-gate * Check for RDONLY filesystem 18837c478bd9Sstevel@tonic-gate * When in doubt assume it is not readonly 18847c478bd9Sstevel@tonic-gate */ 18857c478bd9Sstevel@tonic-gate if (statvfs(root, &vfs) != 0) { 18867c478bd9Sstevel@tonic-gate if (bam_verbose) 18877c478bd9Sstevel@tonic-gate bam_error(STATVFS_FAIL, root, strerror(errno)); 18887c478bd9Sstevel@tonic-gate return (0); 18897c478bd9Sstevel@tonic-gate } 18907c478bd9Sstevel@tonic-gate 18917c478bd9Sstevel@tonic-gate if (vfs.f_flag & ST_RDONLY) { 18927c478bd9Sstevel@tonic-gate return (1); 18937c478bd9Sstevel@tonic-gate } 18947c478bd9Sstevel@tonic-gate 18957c478bd9Sstevel@tonic-gate return (0); 18967c478bd9Sstevel@tonic-gate } 18977c478bd9Sstevel@tonic-gate 18987c478bd9Sstevel@tonic-gate static error_t 18997c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt) 19007c478bd9Sstevel@tonic-gate { 19017c478bd9Sstevel@tonic-gate error_t ret; 19027c478bd9Sstevel@tonic-gate 19037c478bd9Sstevel@tonic-gate assert(root); 19047c478bd9Sstevel@tonic-gate assert(opt == NULL); 19057c478bd9Sstevel@tonic-gate 19067c478bd9Sstevel@tonic-gate /* 1907b610f78eSvikram * root must belong to a GRUB boot OS, 19087c478bd9Sstevel@tonic-gate * don't care on sparc except for diskless clients 19097c478bd9Sstevel@tonic-gate */ 19107c478bd9Sstevel@tonic-gate if (!is_newboot(root)) { 1911b610f78eSvikram /* 1912b610f78eSvikram * Emit message only if not in context of update_all. 1913b610f78eSvikram * If in update_all, emit only if verbose flag is set. 1914b610f78eSvikram */ 1915b610f78eSvikram if (!bam_update_all || bam_verbose) 1916b610f78eSvikram bam_print(NOT_GRUB_BOOT, root); 19177c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 19187c478bd9Sstevel@tonic-gate } 19197c478bd9Sstevel@tonic-gate 19207c478bd9Sstevel@tonic-gate /* 19217c478bd9Sstevel@tonic-gate * root must be writable 19227c478bd9Sstevel@tonic-gate * Note: statvfs() does not always report the truth 19237c478bd9Sstevel@tonic-gate */ 19247c478bd9Sstevel@tonic-gate if (is_readonly(root)) { 19257c478bd9Sstevel@tonic-gate if (!bam_smf_check && bam_verbose) 19267c478bd9Sstevel@tonic-gate bam_print(RDONLY_FS, root); 19277c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 19287c478bd9Sstevel@tonic-gate } 19297c478bd9Sstevel@tonic-gate 19307c478bd9Sstevel@tonic-gate /* 19317c478bd9Sstevel@tonic-gate * Don't generate archive on ramdisk 19327c478bd9Sstevel@tonic-gate */ 19337c478bd9Sstevel@tonic-gate if (is_ramdisk(root)) { 19347c478bd9Sstevel@tonic-gate if (bam_verbose) 19357c478bd9Sstevel@tonic-gate bam_print(SKIP_RAMDISK); 19367c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 19377c478bd9Sstevel@tonic-gate } 19387c478bd9Sstevel@tonic-gate 19397c478bd9Sstevel@tonic-gate /* 19407c478bd9Sstevel@tonic-gate * Now check if updated is really needed 19417c478bd9Sstevel@tonic-gate */ 19427c478bd9Sstevel@tonic-gate ret = update_required(root); 19437c478bd9Sstevel@tonic-gate 19447c478bd9Sstevel@tonic-gate /* 19457c478bd9Sstevel@tonic-gate * The check command (-n) is *not* a dry run 19467c478bd9Sstevel@tonic-gate * It only checks if the archive is in sync. 19477c478bd9Sstevel@tonic-gate */ 19487c478bd9Sstevel@tonic-gate if (bam_check) { 19497c478bd9Sstevel@tonic-gate bam_exit((ret != 0) ? 1 : 0); 19507c478bd9Sstevel@tonic-gate } 19517c478bd9Sstevel@tonic-gate 19527c478bd9Sstevel@tonic-gate if (ret == 1) { 19537c478bd9Sstevel@tonic-gate /* create the ramdisk */ 19547c478bd9Sstevel@tonic-gate ret = create_ramdisk(root); 19557c478bd9Sstevel@tonic-gate } 19567c478bd9Sstevel@tonic-gate return (ret); 19577c478bd9Sstevel@tonic-gate } 19587c478bd9Sstevel@tonic-gate 1959b610f78eSvikram static void 1960b610f78eSvikram restore_grub_slice(void) 1961b610f78eSvikram { 1962b610f78eSvikram struct stat sb; 1963b610f78eSvikram char *mntpt, *physlice; 1964b610f78eSvikram int mnted; /* set if we did a mount */ 1965b610f78eSvikram char menupath[PATH_MAX], cmd[PATH_MAX]; 1966b610f78eSvikram 1967b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 1968b610f78eSvikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 1969b610f78eSvikram return; 1970b610f78eSvikram } 1971b610f78eSvikram 1972b610f78eSvikram /* 1973b610f78eSvikram * If we are doing an luactivate, don't attempt to restore GRUB or else 1974b610f78eSvikram * we may not be able to get to DCA boot environments. Let luactivate 1975b610f78eSvikram * handle GRUB/DCA installation 1976b610f78eSvikram */ 1977b610f78eSvikram if (stat(LU_ACTIVATE_FILE, &sb) == 0) { 1978b610f78eSvikram return; 1979b610f78eSvikram } 1980b610f78eSvikram 1981b610f78eSvikram mnted = 0; 1982b610f78eSvikram physlice = NULL; 1983*40541d5dSvikram mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL); 1984b610f78eSvikram if (mntpt == NULL) { 1985b610f78eSvikram bam_error(CANNOT_RESTORE_GRUB_SLICE); 1986b610f78eSvikram return; 1987b610f78eSvikram } 1988b610f78eSvikram 1989b610f78eSvikram (void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU); 1990b610f78eSvikram if (stat(menupath, &sb) == 0) { 1991*40541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 1992b610f78eSvikram return; 1993b610f78eSvikram } 1994b610f78eSvikram 1995b610f78eSvikram /* 1996b610f78eSvikram * The menu is missing - we need to do a restore 1997b610f78eSvikram */ 1998b610f78eSvikram bam_print(RESTORING_GRUB); 1999b610f78eSvikram 2000b610f78eSvikram (void) snprintf(cmd, sizeof (cmd), "%s %s %s %s", 2001b610f78eSvikram INSTALLGRUB, STAGE1, STAGE2, physlice); 2002b610f78eSvikram 2003b610f78eSvikram if (exec_cmd(cmd, NULL, 0) != 0) { 2004b610f78eSvikram bam_error(RESTORE_GRUB_FAILED); 2005*40541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2006b610f78eSvikram return; 2007b610f78eSvikram } 2008b610f78eSvikram 2009b610f78eSvikram if (stat(GRUB_backup_menu, &sb) != 0) { 2010b610f78eSvikram bam_error(MISSING_BACKUP_MENU, 2011b610f78eSvikram GRUB_backup_menu, strerror(errno)); 2012*40541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2013b610f78eSvikram return; 2014b610f78eSvikram } 2015b610f78eSvikram 2016b610f78eSvikram (void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s", 2017b610f78eSvikram GRUB_backup_menu, menupath); 2018b610f78eSvikram 2019b610f78eSvikram if (exec_cmd(cmd, NULL, 0) != 0) { 2020b610f78eSvikram bam_error(RESTORE_MENU_FAILED, menupath); 2021*40541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2022b610f78eSvikram return; 2023b610f78eSvikram } 2024b610f78eSvikram 2025b610f78eSvikram /* Success */ 2026*40541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2027b610f78eSvikram } 2028b610f78eSvikram 20297c478bd9Sstevel@tonic-gate static error_t 20307c478bd9Sstevel@tonic-gate update_all(char *root, char *opt) 20317c478bd9Sstevel@tonic-gate { 20327c478bd9Sstevel@tonic-gate struct extmnttab mnt; 20337c478bd9Sstevel@tonic-gate struct stat sb; 20347c478bd9Sstevel@tonic-gate FILE *fp; 20357c478bd9Sstevel@tonic-gate char multibt[PATH_MAX]; 20367c478bd9Sstevel@tonic-gate error_t ret = BAM_SUCCESS; 20377c478bd9Sstevel@tonic-gate 2038*40541d5dSvikram assert(root); 20397c478bd9Sstevel@tonic-gate assert(opt == NULL); 20407c478bd9Sstevel@tonic-gate 2041*40541d5dSvikram if (bam_rootlen != 1 || *root != '/') { 2042*40541d5dSvikram elide_trailing_slash(root, multibt, sizeof (multibt)); 2043*40541d5dSvikram bam_error(ALT_ROOT_INVALID, multibt); 2044*40541d5dSvikram return (BAM_ERROR); 2045*40541d5dSvikram } 2046*40541d5dSvikram 20477c478bd9Sstevel@tonic-gate /* 20487c478bd9Sstevel@tonic-gate * First update archive for current root 20497c478bd9Sstevel@tonic-gate */ 20507c478bd9Sstevel@tonic-gate if (update_archive(root, opt) != BAM_SUCCESS) 20517c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 20527c478bd9Sstevel@tonic-gate 20537c478bd9Sstevel@tonic-gate /* 20547c478bd9Sstevel@tonic-gate * Now walk the mount table, performing archive update 20557c478bd9Sstevel@tonic-gate * for all mounted Newboot root filesystems 20567c478bd9Sstevel@tonic-gate */ 20577c478bd9Sstevel@tonic-gate fp = fopen(MNTTAB, "r"); 20587c478bd9Sstevel@tonic-gate if (fp == NULL) { 20597c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 2060b610f78eSvikram ret = BAM_ERROR; 2061b610f78eSvikram goto out; 20627c478bd9Sstevel@tonic-gate } 20637c478bd9Sstevel@tonic-gate 20647c478bd9Sstevel@tonic-gate resetmnttab(fp); 20657c478bd9Sstevel@tonic-gate 20667c478bd9Sstevel@tonic-gate while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 20677c478bd9Sstevel@tonic-gate if (mnt.mnt_special == NULL) 20687c478bd9Sstevel@tonic-gate continue; 20697c478bd9Sstevel@tonic-gate if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0) 20707c478bd9Sstevel@tonic-gate continue; 20717c478bd9Sstevel@tonic-gate if (strcmp(mnt.mnt_mountp, "/") == 0) 20727c478bd9Sstevel@tonic-gate continue; 20737c478bd9Sstevel@tonic-gate 20747c478bd9Sstevel@tonic-gate (void) snprintf(multibt, sizeof (multibt), "%s%s", 20757c478bd9Sstevel@tonic-gate mnt.mnt_mountp, MULTI_BOOT); 20767c478bd9Sstevel@tonic-gate 20777c478bd9Sstevel@tonic-gate if (stat(multibt, &sb) == -1) 20787c478bd9Sstevel@tonic-gate continue; 20797c478bd9Sstevel@tonic-gate 20807c478bd9Sstevel@tonic-gate /* 20817c478bd9Sstevel@tonic-gate * We put a trailing slash to be consistent with root = "/" 20827c478bd9Sstevel@tonic-gate * case, such that we don't have to print // in some cases. 20837c478bd9Sstevel@tonic-gate */ 20847c478bd9Sstevel@tonic-gate (void) snprintf(rootbuf, sizeof (rootbuf), "%s/", 20857c478bd9Sstevel@tonic-gate mnt.mnt_mountp); 20867c478bd9Sstevel@tonic-gate bam_rootlen = strlen(rootbuf); 20877c478bd9Sstevel@tonic-gate if (update_archive(rootbuf, opt) != BAM_SUCCESS) 20887c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 20897c478bd9Sstevel@tonic-gate } 20907c478bd9Sstevel@tonic-gate 20917c478bd9Sstevel@tonic-gate (void) fclose(fp); 20927c478bd9Sstevel@tonic-gate 2093b610f78eSvikram out: 2094b610f78eSvikram if (stat(GRUB_slice, &sb) == 0) { 2095b610f78eSvikram restore_grub_slice(); 2096b610f78eSvikram } 2097b610f78eSvikram 20987c478bd9Sstevel@tonic-gate return (ret); 20997c478bd9Sstevel@tonic-gate } 21007c478bd9Sstevel@tonic-gate 21017c478bd9Sstevel@tonic-gate static void 21027c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp) 21037c478bd9Sstevel@tonic-gate { 21047c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 21057c478bd9Sstevel@tonic-gate mp->start = lp; 21067c478bd9Sstevel@tonic-gate } else { 21077c478bd9Sstevel@tonic-gate mp->end->next = lp; 21087c478bd9Sstevel@tonic-gate } 21097c478bd9Sstevel@tonic-gate mp->end = lp; 21107c478bd9Sstevel@tonic-gate } 21117c478bd9Sstevel@tonic-gate 21127c478bd9Sstevel@tonic-gate /* 21137c478bd9Sstevel@tonic-gate * A line in menu.lst looks like 21147c478bd9Sstevel@tonic-gate * [ ]*<cmd>[ \t=]*<arg>* 21157c478bd9Sstevel@tonic-gate */ 21167c478bd9Sstevel@tonic-gate static void 21177c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum) 21187c478bd9Sstevel@tonic-gate { 21197c478bd9Sstevel@tonic-gate /* 21207c478bd9Sstevel@tonic-gate * save state across calls. This is so that 21217c478bd9Sstevel@tonic-gate * header gets the right entry# after title has 21227c478bd9Sstevel@tonic-gate * been processed 21237c478bd9Sstevel@tonic-gate */ 21247c478bd9Sstevel@tonic-gate static line_t *prev; 21257c478bd9Sstevel@tonic-gate 21267c478bd9Sstevel@tonic-gate line_t *lp; 21277c478bd9Sstevel@tonic-gate char *cmd, *sep, *arg; 21287c478bd9Sstevel@tonic-gate char save, *cp, *line; 21297c478bd9Sstevel@tonic-gate menu_flag_t flag = BAM_INVALID; 21307c478bd9Sstevel@tonic-gate 21317c478bd9Sstevel@tonic-gate if (str == NULL) { 21327c478bd9Sstevel@tonic-gate return; 21337c478bd9Sstevel@tonic-gate } 21347c478bd9Sstevel@tonic-gate 21357c478bd9Sstevel@tonic-gate /* 21367c478bd9Sstevel@tonic-gate * First save a copy of the entire line. 21377c478bd9Sstevel@tonic-gate * We use this later to set the line field. 21387c478bd9Sstevel@tonic-gate */ 21397c478bd9Sstevel@tonic-gate line = s_strdup(str); 21407c478bd9Sstevel@tonic-gate 21417c478bd9Sstevel@tonic-gate /* Eat up leading whitespace */ 21427c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 21437c478bd9Sstevel@tonic-gate str++; 21447c478bd9Sstevel@tonic-gate 21457c478bd9Sstevel@tonic-gate if (*str == '#') { /* comment */ 21467c478bd9Sstevel@tonic-gate cmd = s_strdup("#"); 21477c478bd9Sstevel@tonic-gate sep = NULL; 21487c478bd9Sstevel@tonic-gate arg = s_strdup(str + 1); 21497c478bd9Sstevel@tonic-gate flag = BAM_COMMENT; 21507c478bd9Sstevel@tonic-gate } else if (*str == '\0') { /* blank line */ 21517c478bd9Sstevel@tonic-gate cmd = sep = arg = NULL; 21527c478bd9Sstevel@tonic-gate flag = BAM_EMPTY; 21537c478bd9Sstevel@tonic-gate } else { 21547c478bd9Sstevel@tonic-gate /* 21557c478bd9Sstevel@tonic-gate * '=' is not a documented separator in grub syntax. 21567c478bd9Sstevel@tonic-gate * However various development bits use '=' as a 21577c478bd9Sstevel@tonic-gate * separator. In addition, external users also 21587c478bd9Sstevel@tonic-gate * use = as a separator. So we will allow that usage. 21597c478bd9Sstevel@tonic-gate */ 21607c478bd9Sstevel@tonic-gate cp = str; 21617c478bd9Sstevel@tonic-gate while (*str != ' ' && *str != '\t' && *str != '=') { 21627c478bd9Sstevel@tonic-gate if (*str == '\0') { 21637c478bd9Sstevel@tonic-gate cmd = s_strdup(cp); 21647c478bd9Sstevel@tonic-gate sep = arg = NULL; 21657c478bd9Sstevel@tonic-gate break; 21667c478bd9Sstevel@tonic-gate } 21677c478bd9Sstevel@tonic-gate str++; 21687c478bd9Sstevel@tonic-gate } 21697c478bd9Sstevel@tonic-gate 21707c478bd9Sstevel@tonic-gate if (*str != '\0') { 21717c478bd9Sstevel@tonic-gate save = *str; 21727c478bd9Sstevel@tonic-gate *str = '\0'; 21737c478bd9Sstevel@tonic-gate cmd = s_strdup(cp); 21747c478bd9Sstevel@tonic-gate *str = save; 21757c478bd9Sstevel@tonic-gate 21767c478bd9Sstevel@tonic-gate str++; 21777c478bd9Sstevel@tonic-gate save = *str; 21787c478bd9Sstevel@tonic-gate *str = '\0'; 21797c478bd9Sstevel@tonic-gate sep = s_strdup(str - 1); 21807c478bd9Sstevel@tonic-gate *str = save; 21817c478bd9Sstevel@tonic-gate 21827c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 21837c478bd9Sstevel@tonic-gate str++; 21847c478bd9Sstevel@tonic-gate if (*str == '\0') 21857c478bd9Sstevel@tonic-gate arg = NULL; 21867c478bd9Sstevel@tonic-gate else 21877c478bd9Sstevel@tonic-gate arg = s_strdup(str); 21887c478bd9Sstevel@tonic-gate } 21897c478bd9Sstevel@tonic-gate } 21907c478bd9Sstevel@tonic-gate 21917c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 21927c478bd9Sstevel@tonic-gate 21937c478bd9Sstevel@tonic-gate lp->cmd = cmd; 21947c478bd9Sstevel@tonic-gate lp->sep = sep; 21957c478bd9Sstevel@tonic-gate lp->arg = arg; 21967c478bd9Sstevel@tonic-gate lp->line = line; 21977c478bd9Sstevel@tonic-gate lp->lineNum = ++(*lineNum); 21987c478bd9Sstevel@tonic-gate if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) { 21997c478bd9Sstevel@tonic-gate lp->entryNum = ++(*entryNum); 22007c478bd9Sstevel@tonic-gate lp->flags = BAM_TITLE; 22017c478bd9Sstevel@tonic-gate if (prev && prev->flags == BAM_COMMENT && 22027c478bd9Sstevel@tonic-gate prev->arg && strcmp(prev->arg, BAM_HDR) == 0) 22037c478bd9Sstevel@tonic-gate prev->entryNum = lp->entryNum; 22047c478bd9Sstevel@tonic-gate } else if (flag != BAM_INVALID) { 22057c478bd9Sstevel@tonic-gate /* 22067c478bd9Sstevel@tonic-gate * For header comments, the entry# is "fixed up" 22077c478bd9Sstevel@tonic-gate * by the subsequent title 22087c478bd9Sstevel@tonic-gate */ 22097c478bd9Sstevel@tonic-gate lp->entryNum = *entryNum; 22107c478bd9Sstevel@tonic-gate lp->flags = flag; 22117c478bd9Sstevel@tonic-gate } else { 22127c478bd9Sstevel@tonic-gate lp->entryNum = *entryNum; 22137c478bd9Sstevel@tonic-gate lp->flags = (*entryNum == ENTRY_INIT) ? BAM_GLOBAL : BAM_ENTRY; 22147c478bd9Sstevel@tonic-gate } 22157c478bd9Sstevel@tonic-gate 22167c478bd9Sstevel@tonic-gate append_line(mp, lp); 22177c478bd9Sstevel@tonic-gate 22187c478bd9Sstevel@tonic-gate prev = lp; 22197c478bd9Sstevel@tonic-gate } 22207c478bd9Sstevel@tonic-gate 2221*40541d5dSvikram static void 2222*40541d5dSvikram update_numbering(menu_t *mp) 2223*40541d5dSvikram { 2224*40541d5dSvikram int lineNum; 2225*40541d5dSvikram int entryNum; 2226*40541d5dSvikram int old_default_value; 2227*40541d5dSvikram line_t *lp, *prev, *default_lp, *default_entry; 2228*40541d5dSvikram char buf[PATH_MAX]; 2229*40541d5dSvikram 2230*40541d5dSvikram if (mp->start == NULL) { 2231*40541d5dSvikram return; 2232*40541d5dSvikram } 2233*40541d5dSvikram 2234*40541d5dSvikram lineNum = LINE_INIT; 2235*40541d5dSvikram entryNum = ENTRY_INIT; 2236*40541d5dSvikram old_default_value = ENTRY_INIT; 2237*40541d5dSvikram lp = default_lp = default_entry = NULL; 2238*40541d5dSvikram 2239*40541d5dSvikram prev = NULL; 2240*40541d5dSvikram for (lp = mp->start; lp; prev = lp, lp = lp->next) { 2241*40541d5dSvikram lp->lineNum = ++lineNum; 2242*40541d5dSvikram 2243*40541d5dSvikram /* 2244*40541d5dSvikram * Get the value of the default command 2245*40541d5dSvikram */ 2246*40541d5dSvikram if (lp->entryNum == ENTRY_INIT && lp->cmd && 2247*40541d5dSvikram strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 && 2248*40541d5dSvikram lp->arg) { 2249*40541d5dSvikram old_default_value = atoi(lp->arg); 2250*40541d5dSvikram default_lp = lp; 2251*40541d5dSvikram } 2252*40541d5dSvikram 2253*40541d5dSvikram /* 2254*40541d5dSvikram * If not boot entry, nothing else to fix for this 2255*40541d5dSvikram * entry 2256*40541d5dSvikram */ 2257*40541d5dSvikram if (lp->entryNum == ENTRY_INIT) 2258*40541d5dSvikram continue; 2259*40541d5dSvikram 2260*40541d5dSvikram /* 2261*40541d5dSvikram * Record the position of the default entry. 2262*40541d5dSvikram * The following works because global 2263*40541d5dSvikram * commands like default and timeout should precede 2264*40541d5dSvikram * actual boot entries, so old_default_value 2265*40541d5dSvikram * is already known (or default cmd is missing). 2266*40541d5dSvikram */ 2267*40541d5dSvikram if (default_entry == NULL && 2268*40541d5dSvikram old_default_value != ENTRY_INIT && 2269*40541d5dSvikram lp->entryNum == old_default_value) { 2270*40541d5dSvikram default_entry = lp; 2271*40541d5dSvikram } 2272*40541d5dSvikram 2273*40541d5dSvikram /* 2274*40541d5dSvikram * Now fixup the entry number 2275*40541d5dSvikram */ 2276*40541d5dSvikram if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) { 2277*40541d5dSvikram lp->entryNum = ++entryNum; 2278*40541d5dSvikram /* fixup the bootadm header */ 2279*40541d5dSvikram if (prev && prev->flags == BAM_COMMENT && 2280*40541d5dSvikram prev->arg && strcmp(prev->arg, BAM_HDR) == 0) { 2281*40541d5dSvikram prev->entryNum = lp->entryNum; 2282*40541d5dSvikram } 2283*40541d5dSvikram } else { 2284*40541d5dSvikram lp->entryNum = entryNum; 2285*40541d5dSvikram } 2286*40541d5dSvikram } 2287*40541d5dSvikram 2288*40541d5dSvikram /* 2289*40541d5dSvikram * No default command in menu, simply return 2290*40541d5dSvikram */ 2291*40541d5dSvikram if (default_lp == NULL) { 2292*40541d5dSvikram return; 2293*40541d5dSvikram } 2294*40541d5dSvikram 2295*40541d5dSvikram free(default_lp->arg); 2296*40541d5dSvikram free(default_lp->line); 2297*40541d5dSvikram 2298*40541d5dSvikram if (default_entry == NULL) { 2299*40541d5dSvikram default_lp->arg = s_strdup("0"); 2300*40541d5dSvikram } else { 2301*40541d5dSvikram (void) snprintf(buf, sizeof (buf), "%d", 2302*40541d5dSvikram default_entry->entryNum); 2303*40541d5dSvikram default_lp->arg = s_strdup(buf); 2304*40541d5dSvikram } 2305*40541d5dSvikram 2306*40541d5dSvikram /* 2307*40541d5dSvikram * The following is required since only the line field gets 2308*40541d5dSvikram * written back to menu.lst 2309*40541d5dSvikram */ 2310*40541d5dSvikram (void) snprintf(buf, sizeof (buf), "%s%s%s", 2311*40541d5dSvikram menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg); 2312*40541d5dSvikram default_lp->line = s_strdup(buf); 2313*40541d5dSvikram } 2314*40541d5dSvikram 2315*40541d5dSvikram 23167c478bd9Sstevel@tonic-gate static menu_t * 23177c478bd9Sstevel@tonic-gate menu_read(char *menu_path) 23187c478bd9Sstevel@tonic-gate { 23197c478bd9Sstevel@tonic-gate FILE *fp; 23207c478bd9Sstevel@tonic-gate char buf[BAM_MAXLINE], *cp; 23217c478bd9Sstevel@tonic-gate menu_t *mp; 23227c478bd9Sstevel@tonic-gate int line, entry, len, n; 23237c478bd9Sstevel@tonic-gate 23247c478bd9Sstevel@tonic-gate mp = s_calloc(1, sizeof (menu_t)); 23257c478bd9Sstevel@tonic-gate 23267c478bd9Sstevel@tonic-gate fp = fopen(menu_path, "r"); 23277c478bd9Sstevel@tonic-gate if (fp == NULL) { /* Let the caller handle this error */ 23287c478bd9Sstevel@tonic-gate return (mp); 23297c478bd9Sstevel@tonic-gate } 23307c478bd9Sstevel@tonic-gate 23317c478bd9Sstevel@tonic-gate 23327c478bd9Sstevel@tonic-gate /* Note: GRUB boot entry number starts with 0 */ 23337c478bd9Sstevel@tonic-gate line = LINE_INIT; 23347c478bd9Sstevel@tonic-gate entry = ENTRY_INIT; 23357c478bd9Sstevel@tonic-gate cp = buf; 23367c478bd9Sstevel@tonic-gate len = sizeof (buf); 23377c478bd9Sstevel@tonic-gate while (s_fgets(cp, len, fp) != NULL) { 23387c478bd9Sstevel@tonic-gate n = strlen(cp); 23397c478bd9Sstevel@tonic-gate if (cp[n - 1] == '\\') { 23407c478bd9Sstevel@tonic-gate len -= n - 1; 23417c478bd9Sstevel@tonic-gate assert(len >= 2); 23427c478bd9Sstevel@tonic-gate cp += n - 1; 23437c478bd9Sstevel@tonic-gate continue; 23447c478bd9Sstevel@tonic-gate } 23457c478bd9Sstevel@tonic-gate line_parser(mp, buf, &line, &entry); 23467c478bd9Sstevel@tonic-gate cp = buf; 23477c478bd9Sstevel@tonic-gate len = sizeof (buf); 23487c478bd9Sstevel@tonic-gate } 23497c478bd9Sstevel@tonic-gate 23507c478bd9Sstevel@tonic-gate if (fclose(fp) == EOF) { 23517c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, menu_path, strerror(errno)); 23527c478bd9Sstevel@tonic-gate } 23537c478bd9Sstevel@tonic-gate 23547c478bd9Sstevel@tonic-gate return (mp); 23557c478bd9Sstevel@tonic-gate } 23567c478bd9Sstevel@tonic-gate 23577c478bd9Sstevel@tonic-gate static error_t 23587c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title) 23597c478bd9Sstevel@tonic-gate { 23607c478bd9Sstevel@tonic-gate char *eq; 23617c478bd9Sstevel@tonic-gate char *opt_dup; 23627c478bd9Sstevel@tonic-gate int entryNum; 23637c478bd9Sstevel@tonic-gate 23647c478bd9Sstevel@tonic-gate assert(mp); 23657c478bd9Sstevel@tonic-gate assert(mp->start); 23667c478bd9Sstevel@tonic-gate assert(opt); 23677c478bd9Sstevel@tonic-gate 23687c478bd9Sstevel@tonic-gate opt_dup = s_strdup(opt); 23697c478bd9Sstevel@tonic-gate 23707c478bd9Sstevel@tonic-gate if (entry) 23717c478bd9Sstevel@tonic-gate *entry = ENTRY_INIT; 23727c478bd9Sstevel@tonic-gate if (title) 23737c478bd9Sstevel@tonic-gate *title = NULL; 23747c478bd9Sstevel@tonic-gate 23757c478bd9Sstevel@tonic-gate eq = strchr(opt_dup, '='); 23767c478bd9Sstevel@tonic-gate if (eq == NULL) { 23777c478bd9Sstevel@tonic-gate bam_error(INVALID_OPT, opt); 23787c478bd9Sstevel@tonic-gate free(opt_dup); 23797c478bd9Sstevel@tonic-gate return (BAM_ERROR); 23807c478bd9Sstevel@tonic-gate } 23817c478bd9Sstevel@tonic-gate 23827c478bd9Sstevel@tonic-gate *eq = '\0'; 23837c478bd9Sstevel@tonic-gate if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) { 23847c478bd9Sstevel@tonic-gate assert(mp->end); 23857c478bd9Sstevel@tonic-gate entryNum = s_strtol(eq + 1); 23867c478bd9Sstevel@tonic-gate if (entryNum < 0 || entryNum > mp->end->entryNum) { 23877c478bd9Sstevel@tonic-gate bam_error(INVALID_ENTRY, eq + 1); 23887c478bd9Sstevel@tonic-gate free(opt_dup); 23897c478bd9Sstevel@tonic-gate return (BAM_ERROR); 23907c478bd9Sstevel@tonic-gate } 23917c478bd9Sstevel@tonic-gate *entry = entryNum; 23927c478bd9Sstevel@tonic-gate } else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) { 23937c478bd9Sstevel@tonic-gate *title = opt + (eq - opt_dup) + 1; 23947c478bd9Sstevel@tonic-gate } else { 23957c478bd9Sstevel@tonic-gate bam_error(INVALID_OPT, opt); 23967c478bd9Sstevel@tonic-gate free(opt_dup); 23977c478bd9Sstevel@tonic-gate return (BAM_ERROR); 23987c478bd9Sstevel@tonic-gate } 23997c478bd9Sstevel@tonic-gate 24007c478bd9Sstevel@tonic-gate free(opt_dup); 24017c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 24027c478bd9Sstevel@tonic-gate } 24037c478bd9Sstevel@tonic-gate 24047c478bd9Sstevel@tonic-gate /* 24057c478bd9Sstevel@tonic-gate * If invoked with no titles/entries (opt == NULL) 24067c478bd9Sstevel@tonic-gate * only title lines in file are printed. 24077c478bd9Sstevel@tonic-gate * 24087c478bd9Sstevel@tonic-gate * If invoked with a title or entry #, all 24097c478bd9Sstevel@tonic-gate * lines in *every* matching entry are listed 24107c478bd9Sstevel@tonic-gate */ 24117c478bd9Sstevel@tonic-gate static error_t 24127c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt) 24137c478bd9Sstevel@tonic-gate { 24147c478bd9Sstevel@tonic-gate line_t *lp; 24157c478bd9Sstevel@tonic-gate int entry = ENTRY_INIT; 24167c478bd9Sstevel@tonic-gate int found; 24177c478bd9Sstevel@tonic-gate char *title = NULL; 24187c478bd9Sstevel@tonic-gate 24197c478bd9Sstevel@tonic-gate assert(mp); 24207c478bd9Sstevel@tonic-gate assert(menu_path); 24217c478bd9Sstevel@tonic-gate 24227c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 24237c478bd9Sstevel@tonic-gate bam_error(NO_MENU, menu_path); 24247c478bd9Sstevel@tonic-gate return (BAM_ERROR); 24257c478bd9Sstevel@tonic-gate } 24267c478bd9Sstevel@tonic-gate 24277c478bd9Sstevel@tonic-gate if (opt != NULL) { 24287c478bd9Sstevel@tonic-gate if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) { 24297c478bd9Sstevel@tonic-gate return (BAM_ERROR); 24307c478bd9Sstevel@tonic-gate } 24317c478bd9Sstevel@tonic-gate assert((entry != ENTRY_INIT) ^ (title != NULL)); 24327c478bd9Sstevel@tonic-gate } else { 24337c478bd9Sstevel@tonic-gate (void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0); 24347c478bd9Sstevel@tonic-gate (void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0); 24357c478bd9Sstevel@tonic-gate } 24367c478bd9Sstevel@tonic-gate 24377c478bd9Sstevel@tonic-gate found = 0; 24387c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 24397c478bd9Sstevel@tonic-gate if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY) 24407c478bd9Sstevel@tonic-gate continue; 24417c478bd9Sstevel@tonic-gate if (opt == NULL && lp->flags == BAM_TITLE) { 24427c478bd9Sstevel@tonic-gate bam_print(PRINT_TITLE, lp->entryNum, 24437c478bd9Sstevel@tonic-gate lp->arg); 24447c478bd9Sstevel@tonic-gate found = 1; 24457c478bd9Sstevel@tonic-gate continue; 24467c478bd9Sstevel@tonic-gate } 24477c478bd9Sstevel@tonic-gate if (entry != ENTRY_INIT && lp->entryNum == entry) { 24487c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 24497c478bd9Sstevel@tonic-gate found = 1; 24507c478bd9Sstevel@tonic-gate continue; 24517c478bd9Sstevel@tonic-gate } 24527c478bd9Sstevel@tonic-gate 24537c478bd9Sstevel@tonic-gate /* 24547c478bd9Sstevel@tonic-gate * We set the entry value here so that all lines 24557c478bd9Sstevel@tonic-gate * in entry get printed. If we subsequently match 24567c478bd9Sstevel@tonic-gate * title in other entries, all lines in those 24577c478bd9Sstevel@tonic-gate * entries get printed as well. 24587c478bd9Sstevel@tonic-gate */ 24597c478bd9Sstevel@tonic-gate if (title && lp->flags == BAM_TITLE && lp->arg && 24607c478bd9Sstevel@tonic-gate strncmp(title, lp->arg, strlen(title)) == 0) { 24617c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 24627c478bd9Sstevel@tonic-gate entry = lp->entryNum; 24637c478bd9Sstevel@tonic-gate found = 1; 24647c478bd9Sstevel@tonic-gate continue; 24657c478bd9Sstevel@tonic-gate } 24667c478bd9Sstevel@tonic-gate } 24677c478bd9Sstevel@tonic-gate 24687c478bd9Sstevel@tonic-gate if (!found) { 24697c478bd9Sstevel@tonic-gate bam_error(NO_MATCH_ENTRY); 24707c478bd9Sstevel@tonic-gate return (BAM_ERROR); 24717c478bd9Sstevel@tonic-gate } 24727c478bd9Sstevel@tonic-gate 24737c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 24747c478bd9Sstevel@tonic-gate } 24757c478bd9Sstevel@tonic-gate 24767c478bd9Sstevel@tonic-gate static int 24777c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp, 24787c478bd9Sstevel@tonic-gate char *title, 24797c478bd9Sstevel@tonic-gate char *root, 24807c478bd9Sstevel@tonic-gate char *kernel, 24817c478bd9Sstevel@tonic-gate char *module) 24827c478bd9Sstevel@tonic-gate { 24837c478bd9Sstevel@tonic-gate menu_t dummy; 24847c478bd9Sstevel@tonic-gate int lineNum, entryNum; 24857c478bd9Sstevel@tonic-gate char linebuf[BAM_MAXLINE]; 24867c478bd9Sstevel@tonic-gate 24877c478bd9Sstevel@tonic-gate assert(mp); 24887c478bd9Sstevel@tonic-gate 24897c478bd9Sstevel@tonic-gate if (title == NULL) { 24907c478bd9Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[TITLE_CMD]); 24917c478bd9Sstevel@tonic-gate return (BAM_ERROR); 24927c478bd9Sstevel@tonic-gate } 24937c478bd9Sstevel@tonic-gate if (root == NULL) { 24947c478bd9Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[ROOT_CMD]); 24957c478bd9Sstevel@tonic-gate return (BAM_ERROR); 24967c478bd9Sstevel@tonic-gate } 24977c478bd9Sstevel@tonic-gate if (kernel == NULL) { 24987c478bd9Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]); 24997c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25007c478bd9Sstevel@tonic-gate } 25017c478bd9Sstevel@tonic-gate if (module == NULL) { 25027c478bd9Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]); 25037c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25047c478bd9Sstevel@tonic-gate } 25057c478bd9Sstevel@tonic-gate 25067c478bd9Sstevel@tonic-gate if (mp->start) { 25077c478bd9Sstevel@tonic-gate lineNum = mp->end->lineNum; 25087c478bd9Sstevel@tonic-gate entryNum = mp->end->entryNum; 25097c478bd9Sstevel@tonic-gate } else { 25107c478bd9Sstevel@tonic-gate lineNum = LINE_INIT; 25117c478bd9Sstevel@tonic-gate entryNum = ENTRY_INIT; 25127c478bd9Sstevel@tonic-gate } 25137c478bd9Sstevel@tonic-gate 25147c478bd9Sstevel@tonic-gate /* 25157c478bd9Sstevel@tonic-gate * No separator for comment (HDR/FTR) commands 25167c478bd9Sstevel@tonic-gate * The syntax for comments is #<comment> 25177c478bd9Sstevel@tonic-gate */ 25187c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 25197c478bd9Sstevel@tonic-gate menu_cmds[COMMENT_CMD], BAM_HDR); 25207c478bd9Sstevel@tonic-gate dummy.start = dummy.end = NULL; 25217c478bd9Sstevel@tonic-gate line_parser(&dummy, linebuf, &lineNum, &entryNum); 25227c478bd9Sstevel@tonic-gate if (dummy.start == NULL || dummy.start->flags != BAM_COMMENT) { 25237c478bd9Sstevel@tonic-gate line_free(dummy.start); 25247c478bd9Sstevel@tonic-gate bam_error(INVALID_HDR, BAM_HDR); 25257c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25267c478bd9Sstevel@tonic-gate } 25277c478bd9Sstevel@tonic-gate assert(dummy.start == dummy.end); 25287c478bd9Sstevel@tonic-gate append_line(mp, dummy.start); 25297c478bd9Sstevel@tonic-gate 25307c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 25317c478bd9Sstevel@tonic-gate menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title); 25327c478bd9Sstevel@tonic-gate dummy.start = dummy.end = NULL; 25337c478bd9Sstevel@tonic-gate line_parser(&dummy, linebuf, &lineNum, &entryNum); 25347c478bd9Sstevel@tonic-gate if (dummy.start == NULL || dummy.start->flags != BAM_TITLE) { 25357c478bd9Sstevel@tonic-gate line_free(dummy.start); 25367c478bd9Sstevel@tonic-gate bam_error(INVALID_TITLE, title); 25377c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25387c478bd9Sstevel@tonic-gate } 25397c478bd9Sstevel@tonic-gate assert(dummy.start == dummy.end); 25407c478bd9Sstevel@tonic-gate append_line(mp, dummy.start); 25417c478bd9Sstevel@tonic-gate 25427c478bd9Sstevel@tonic-gate 25437c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 25447c478bd9Sstevel@tonic-gate menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root); 25457c478bd9Sstevel@tonic-gate dummy.start = dummy.end = NULL; 25467c478bd9Sstevel@tonic-gate line_parser(&dummy, linebuf, &lineNum, &entryNum); 25477c478bd9Sstevel@tonic-gate if (dummy.start == NULL || dummy.start->flags != BAM_ENTRY) { 25487c478bd9Sstevel@tonic-gate line_free(dummy.start); 25497c478bd9Sstevel@tonic-gate bam_error(INVALID_ROOT, root); 25507c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25517c478bd9Sstevel@tonic-gate } 25527c478bd9Sstevel@tonic-gate assert(dummy.start == dummy.end); 25537c478bd9Sstevel@tonic-gate append_line(mp, dummy.start); 25547c478bd9Sstevel@tonic-gate 25557c478bd9Sstevel@tonic-gate 25567c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 25577c478bd9Sstevel@tonic-gate menu_cmds[KERNEL_CMD], menu_cmds[SEP_CMD], kernel); 25587c478bd9Sstevel@tonic-gate dummy.start = dummy.end = NULL; 25597c478bd9Sstevel@tonic-gate line_parser(&dummy, linebuf, &lineNum, &entryNum); 25607c478bd9Sstevel@tonic-gate if (dummy.start == NULL || dummy.start->flags != BAM_ENTRY) { 25617c478bd9Sstevel@tonic-gate line_free(dummy.start); 25627c478bd9Sstevel@tonic-gate bam_error(INVALID_KERNEL, kernel); 25637c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25647c478bd9Sstevel@tonic-gate } 25657c478bd9Sstevel@tonic-gate assert(dummy.start == dummy.end); 25667c478bd9Sstevel@tonic-gate append_line(mp, dummy.start); 25677c478bd9Sstevel@tonic-gate 25687c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 25697c478bd9Sstevel@tonic-gate menu_cmds[MODULE_CMD], menu_cmds[SEP_CMD], module); 25707c478bd9Sstevel@tonic-gate dummy.start = dummy.end = NULL; 25717c478bd9Sstevel@tonic-gate line_parser(&dummy, linebuf, &lineNum, &entryNum); 25727c478bd9Sstevel@tonic-gate if (dummy.start == NULL || dummy.start->flags != BAM_ENTRY) { 25737c478bd9Sstevel@tonic-gate line_free(dummy.start); 25747c478bd9Sstevel@tonic-gate bam_error(INVALID_MODULE, module); 25757c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25767c478bd9Sstevel@tonic-gate } 25777c478bd9Sstevel@tonic-gate assert(dummy.start == dummy.end); 25787c478bd9Sstevel@tonic-gate append_line(mp, dummy.start); 25797c478bd9Sstevel@tonic-gate 25807c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 25817c478bd9Sstevel@tonic-gate menu_cmds[COMMENT_CMD], BAM_FTR); 25827c478bd9Sstevel@tonic-gate dummy.start = dummy.end = NULL; 25837c478bd9Sstevel@tonic-gate line_parser(&dummy, linebuf, &lineNum, &entryNum); 25847c478bd9Sstevel@tonic-gate if (dummy.start == NULL || dummy.start->flags != BAM_COMMENT) { 25857c478bd9Sstevel@tonic-gate line_free(dummy.start); 25867c478bd9Sstevel@tonic-gate bam_error(INVALID_FOOTER, BAM_FTR); 25877c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25887c478bd9Sstevel@tonic-gate } 25897c478bd9Sstevel@tonic-gate assert(dummy.start == dummy.end); 25907c478bd9Sstevel@tonic-gate append_line(mp, dummy.start); 25917c478bd9Sstevel@tonic-gate 25927c478bd9Sstevel@tonic-gate return (entryNum); 25937c478bd9Sstevel@tonic-gate } 25947c478bd9Sstevel@tonic-gate 25957c478bd9Sstevel@tonic-gate static error_t 25967c478bd9Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum) 25977c478bd9Sstevel@tonic-gate { 25987c478bd9Sstevel@tonic-gate int bootadm_entry = 0; 25997c478bd9Sstevel@tonic-gate line_t *lp, *prev, *save; 26007c478bd9Sstevel@tonic-gate int deleted; 26017c478bd9Sstevel@tonic-gate 26027c478bd9Sstevel@tonic-gate assert(entryNum != ENTRY_INIT); 26037c478bd9Sstevel@tonic-gate 26047c478bd9Sstevel@tonic-gate deleted = 0; 26057c478bd9Sstevel@tonic-gate prev = NULL; 26067c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; ) { 26077c478bd9Sstevel@tonic-gate 26087c478bd9Sstevel@tonic-gate if (lp->entryNum == ENTRY_INIT) { 26097c478bd9Sstevel@tonic-gate prev = lp; 26107c478bd9Sstevel@tonic-gate lp = lp->next; 26117c478bd9Sstevel@tonic-gate continue; 26127c478bd9Sstevel@tonic-gate } 26137c478bd9Sstevel@tonic-gate 26147c478bd9Sstevel@tonic-gate if (entryNum != ALL_ENTRIES && lp->entryNum != entryNum) { 26157c478bd9Sstevel@tonic-gate prev = lp; 26167c478bd9Sstevel@tonic-gate lp = lp->next; 26177c478bd9Sstevel@tonic-gate continue; 26187c478bd9Sstevel@tonic-gate } 26197c478bd9Sstevel@tonic-gate 26207c478bd9Sstevel@tonic-gate /* 26217c478bd9Sstevel@tonic-gate * can only delete bootadm entries 26227c478bd9Sstevel@tonic-gate */ 26237c478bd9Sstevel@tonic-gate if (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_HDR) == 0) { 26247c478bd9Sstevel@tonic-gate bootadm_entry = 1; 26257c478bd9Sstevel@tonic-gate } 26267c478bd9Sstevel@tonic-gate 26277c478bd9Sstevel@tonic-gate if (!bootadm_entry) { 26287c478bd9Sstevel@tonic-gate prev = lp; 26297c478bd9Sstevel@tonic-gate lp = lp->next; 26307c478bd9Sstevel@tonic-gate continue; 26317c478bd9Sstevel@tonic-gate } 26327c478bd9Sstevel@tonic-gate 26337c478bd9Sstevel@tonic-gate if (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_FTR) == 0) 26347c478bd9Sstevel@tonic-gate bootadm_entry = 0; 26357c478bd9Sstevel@tonic-gate 26367c478bd9Sstevel@tonic-gate if (prev == NULL) 26377c478bd9Sstevel@tonic-gate mp->start = lp->next; 26387c478bd9Sstevel@tonic-gate else 26397c478bd9Sstevel@tonic-gate prev->next = lp->next; 26407c478bd9Sstevel@tonic-gate if (mp->end == lp) 26417c478bd9Sstevel@tonic-gate mp->end = prev; 26427c478bd9Sstevel@tonic-gate save = lp->next; 26437c478bd9Sstevel@tonic-gate line_free(lp); 26447c478bd9Sstevel@tonic-gate lp = save; /* prev stays the same */ 26457c478bd9Sstevel@tonic-gate 26467c478bd9Sstevel@tonic-gate deleted = 1; 26477c478bd9Sstevel@tonic-gate } 26487c478bd9Sstevel@tonic-gate 26497c478bd9Sstevel@tonic-gate if (!deleted && entryNum != ALL_ENTRIES) { 26507c478bd9Sstevel@tonic-gate bam_error(NO_BOOTADM_MATCH); 26517c478bd9Sstevel@tonic-gate return (BAM_ERROR); 26527c478bd9Sstevel@tonic-gate } 26537c478bd9Sstevel@tonic-gate 2654*40541d5dSvikram /* 2655*40541d5dSvikram * Now that we have deleted an entry, update 2656*40541d5dSvikram * the entry numbering and the default cmd. 2657*40541d5dSvikram */ 2658*40541d5dSvikram update_numbering(mp); 2659*40541d5dSvikram 26607c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 26617c478bd9Sstevel@tonic-gate } 26627c478bd9Sstevel@tonic-gate 26637c478bd9Sstevel@tonic-gate static error_t 26647c478bd9Sstevel@tonic-gate delete_entry(menu_t *mp, char *menu_path, char *opt) 26657c478bd9Sstevel@tonic-gate { 26667c478bd9Sstevel@tonic-gate int entry = ENTRY_INIT; 26677c478bd9Sstevel@tonic-gate char *title = NULL; 26687c478bd9Sstevel@tonic-gate line_t *lp; 26697c478bd9Sstevel@tonic-gate 26707c478bd9Sstevel@tonic-gate assert(mp); 26717c478bd9Sstevel@tonic-gate assert(opt); 26727c478bd9Sstevel@tonic-gate 26737c478bd9Sstevel@tonic-gate /* 26747c478bd9Sstevel@tonic-gate * Do a quick check. If the file is empty 26757c478bd9Sstevel@tonic-gate * we have nothing to delete 26767c478bd9Sstevel@tonic-gate */ 26777c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 26787c478bd9Sstevel@tonic-gate bam_print(EMPTY_FILE, menu_path); 26797c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 26807c478bd9Sstevel@tonic-gate } 26817c478bd9Sstevel@tonic-gate 26827c478bd9Sstevel@tonic-gate if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) { 26837c478bd9Sstevel@tonic-gate return (BAM_ERROR); 26847c478bd9Sstevel@tonic-gate } 26857c478bd9Sstevel@tonic-gate assert((entry != ENTRY_INIT) ^ (title != NULL)); 26867c478bd9Sstevel@tonic-gate 26877c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 26887c478bd9Sstevel@tonic-gate if (entry != ENTRY_INIT) 26897c478bd9Sstevel@tonic-gate break; 26907c478bd9Sstevel@tonic-gate assert(title); 26917c478bd9Sstevel@tonic-gate if (lp->flags == BAM_TITLE && 26927c478bd9Sstevel@tonic-gate lp->arg && strcmp(lp->arg, title) == 0) { 26937c478bd9Sstevel@tonic-gate entry = lp->entryNum; 26947c478bd9Sstevel@tonic-gate break; 26957c478bd9Sstevel@tonic-gate } 26967c478bd9Sstevel@tonic-gate } 26977c478bd9Sstevel@tonic-gate 26987c478bd9Sstevel@tonic-gate if (entry == ENTRY_INIT) { 26997c478bd9Sstevel@tonic-gate bam_error(NO_MATCH, title); 27007c478bd9Sstevel@tonic-gate return (BAM_ERROR); 27017c478bd9Sstevel@tonic-gate } 27027c478bd9Sstevel@tonic-gate 27037c478bd9Sstevel@tonic-gate if (do_delete(mp, entry) != BAM_SUCCESS) { 27047c478bd9Sstevel@tonic-gate return (BAM_ERROR); 27057c478bd9Sstevel@tonic-gate } 27067c478bd9Sstevel@tonic-gate 27077c478bd9Sstevel@tonic-gate return (BAM_WRITE); 27087c478bd9Sstevel@tonic-gate } 27097c478bd9Sstevel@tonic-gate 27107c478bd9Sstevel@tonic-gate static error_t 27117c478bd9Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt) 27127c478bd9Sstevel@tonic-gate { 27137c478bd9Sstevel@tonic-gate assert(mp); 27147c478bd9Sstevel@tonic-gate assert(opt == NULL); 27157c478bd9Sstevel@tonic-gate 27167c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 27177c478bd9Sstevel@tonic-gate bam_print(EMPTY_FILE, menu_path); 27187c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 27197c478bd9Sstevel@tonic-gate } 27207c478bd9Sstevel@tonic-gate 27217c478bd9Sstevel@tonic-gate if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) { 27227c478bd9Sstevel@tonic-gate return (BAM_ERROR); 27237c478bd9Sstevel@tonic-gate } 27247c478bd9Sstevel@tonic-gate 27257c478bd9Sstevel@tonic-gate return (BAM_WRITE); 27267c478bd9Sstevel@tonic-gate } 27277c478bd9Sstevel@tonic-gate 27287c478bd9Sstevel@tonic-gate static FILE * 27297c478bd9Sstevel@tonic-gate open_diskmap(void) 27307c478bd9Sstevel@tonic-gate { 27317c478bd9Sstevel@tonic-gate FILE *fp; 27327c478bd9Sstevel@tonic-gate char cmd[PATH_MAX]; 27337c478bd9Sstevel@tonic-gate 27347c478bd9Sstevel@tonic-gate /* make sure we have a map file */ 27357c478bd9Sstevel@tonic-gate fp = fopen(GRUBDISK_MAP, "r"); 27367c478bd9Sstevel@tonic-gate if (fp == NULL) { 27377c478bd9Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), 27387c478bd9Sstevel@tonic-gate "%s > /dev/null", CREATE_DISKMAP); 27397c478bd9Sstevel@tonic-gate (void) system(cmd); 27407c478bd9Sstevel@tonic-gate fp = fopen(GRUBDISK_MAP, "r"); 27417c478bd9Sstevel@tonic-gate } 27427c478bd9Sstevel@tonic-gate return (fp); 27437c478bd9Sstevel@tonic-gate } 27447c478bd9Sstevel@tonic-gate 27457c478bd9Sstevel@tonic-gate #define SECTOR_SIZE 512 27467c478bd9Sstevel@tonic-gate 27477c478bd9Sstevel@tonic-gate static int 27487c478bd9Sstevel@tonic-gate get_partition(char *device) 27497c478bd9Sstevel@tonic-gate { 27507c478bd9Sstevel@tonic-gate int i, fd, is_pcfs, partno = -1; 27517c478bd9Sstevel@tonic-gate struct mboot *mboot; 27527c478bd9Sstevel@tonic-gate char boot_sect[SECTOR_SIZE]; 27537c478bd9Sstevel@tonic-gate char *wholedisk, *slice; 27547c478bd9Sstevel@tonic-gate 27557c478bd9Sstevel@tonic-gate /* form whole disk (p0) */ 27567c478bd9Sstevel@tonic-gate slice = device + strlen(device) - 2; 27577c478bd9Sstevel@tonic-gate is_pcfs = (*slice != 's'); 27587c478bd9Sstevel@tonic-gate if (!is_pcfs) 27597c478bd9Sstevel@tonic-gate *slice = '\0'; 27607c478bd9Sstevel@tonic-gate wholedisk = s_calloc(1, strlen(device) + 3); 27617c478bd9Sstevel@tonic-gate (void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device); 27627c478bd9Sstevel@tonic-gate if (!is_pcfs) 27637c478bd9Sstevel@tonic-gate *slice = 's'; 27647c478bd9Sstevel@tonic-gate 27657c478bd9Sstevel@tonic-gate /* read boot sector */ 27667c478bd9Sstevel@tonic-gate fd = open(wholedisk, O_RDONLY); 27677c478bd9Sstevel@tonic-gate free(wholedisk); 27687c478bd9Sstevel@tonic-gate if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 27697c478bd9Sstevel@tonic-gate return (partno); 27707c478bd9Sstevel@tonic-gate } 27717c478bd9Sstevel@tonic-gate (void) close(fd); 27727c478bd9Sstevel@tonic-gate 27737c478bd9Sstevel@tonic-gate /* parse fdisk table */ 27747c478bd9Sstevel@tonic-gate mboot = (struct mboot *)((void *)boot_sect); 27757c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 27767c478bd9Sstevel@tonic-gate struct ipart *part = 27777c478bd9Sstevel@tonic-gate (struct ipart *)(uintptr_t)mboot->parts + i; 27787c478bd9Sstevel@tonic-gate if (is_pcfs) { /* looking for solaris boot part */ 27797c478bd9Sstevel@tonic-gate if (part->systid == 0xbe) { 27807c478bd9Sstevel@tonic-gate partno = i; 27817c478bd9Sstevel@tonic-gate break; 27827c478bd9Sstevel@tonic-gate } 27837c478bd9Sstevel@tonic-gate } else { /* look for solaris partition, old and new */ 27847c478bd9Sstevel@tonic-gate if (part->systid == SUNIXOS || 27857c478bd9Sstevel@tonic-gate part->systid == SUNIXOS2) { 27867c478bd9Sstevel@tonic-gate partno = i; 27877c478bd9Sstevel@tonic-gate break; 27887c478bd9Sstevel@tonic-gate } 27897c478bd9Sstevel@tonic-gate } 27907c478bd9Sstevel@tonic-gate } 27917c478bd9Sstevel@tonic-gate return (partno); 27927c478bd9Sstevel@tonic-gate } 27937c478bd9Sstevel@tonic-gate 27947c478bd9Sstevel@tonic-gate static char * 27957c478bd9Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev) 27967c478bd9Sstevel@tonic-gate { 27977c478bd9Sstevel@tonic-gate char *grubdisk; /* (hd#,#,#) */ 27987c478bd9Sstevel@tonic-gate char *slice; 27997c478bd9Sstevel@tonic-gate char *grubhd; 28007c478bd9Sstevel@tonic-gate int fdiskpart; 28017c478bd9Sstevel@tonic-gate int found = 0; 28027c478bd9Sstevel@tonic-gate char *devname, *ctdname = strstr(rootdev, "dsk/"); 28037c478bd9Sstevel@tonic-gate char linebuf[PATH_MAX]; 28047c478bd9Sstevel@tonic-gate 28057c478bd9Sstevel@tonic-gate if (ctdname == NULL) 28067c478bd9Sstevel@tonic-gate return (NULL); 28077c478bd9Sstevel@tonic-gate 28087c478bd9Sstevel@tonic-gate ctdname += strlen("dsk/"); 28097c478bd9Sstevel@tonic-gate slice = strrchr(ctdname, 's'); 28107c478bd9Sstevel@tonic-gate if (slice) 28117c478bd9Sstevel@tonic-gate *slice = '\0'; 28127c478bd9Sstevel@tonic-gate 28137c478bd9Sstevel@tonic-gate rewind(fp); 28147c478bd9Sstevel@tonic-gate while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) { 28157c478bd9Sstevel@tonic-gate grubhd = strtok(linebuf, " \t\n"); 28167c478bd9Sstevel@tonic-gate if (grubhd) 28177c478bd9Sstevel@tonic-gate devname = strtok(NULL, " \t\n"); 28187c478bd9Sstevel@tonic-gate else 28197c478bd9Sstevel@tonic-gate devname = NULL; 28207c478bd9Sstevel@tonic-gate if (devname && strcmp(devname, ctdname) == 0) { 28217c478bd9Sstevel@tonic-gate found = 1; 28227c478bd9Sstevel@tonic-gate break; 28237c478bd9Sstevel@tonic-gate } 28247c478bd9Sstevel@tonic-gate } 28257c478bd9Sstevel@tonic-gate 28267c478bd9Sstevel@tonic-gate if (slice) 28277c478bd9Sstevel@tonic-gate *slice = 's'; 28287c478bd9Sstevel@tonic-gate 28297c478bd9Sstevel@tonic-gate if (found == 0) { 28307c478bd9Sstevel@tonic-gate if (bam_verbose) 28317c478bd9Sstevel@tonic-gate bam_print(DISKMAP_FAIL_NONFATAL, rootdev); 28327c478bd9Sstevel@tonic-gate grubhd = "0"; /* assume disk 0 if can't match */ 28337c478bd9Sstevel@tonic-gate } 28347c478bd9Sstevel@tonic-gate 28357c478bd9Sstevel@tonic-gate fdiskpart = get_partition(rootdev); 28367c478bd9Sstevel@tonic-gate if (fdiskpart == -1) 28377c478bd9Sstevel@tonic-gate return (NULL); 28387c478bd9Sstevel@tonic-gate 28397c478bd9Sstevel@tonic-gate grubdisk = s_calloc(1, 10); 28407c478bd9Sstevel@tonic-gate if (slice) { 28417c478bd9Sstevel@tonic-gate (void) snprintf(grubdisk, 10, "(hd%s,%d,%c)", 28427c478bd9Sstevel@tonic-gate grubhd, fdiskpart, slice[1] + 'a' - '0'); 28437c478bd9Sstevel@tonic-gate } else 28447c478bd9Sstevel@tonic-gate (void) snprintf(grubdisk, 10, "(hd%s,%d)", 28457c478bd9Sstevel@tonic-gate grubhd, fdiskpart); 28467c478bd9Sstevel@tonic-gate 28477c478bd9Sstevel@tonic-gate /* if root not on bootdev, change GRUB disk to 0 */ 28487c478bd9Sstevel@tonic-gate if (!on_bootdev) 28497c478bd9Sstevel@tonic-gate grubdisk[3] = '0'; 28507c478bd9Sstevel@tonic-gate return (grubdisk); 28517c478bd9Sstevel@tonic-gate } 28527c478bd9Sstevel@tonic-gate 28537c478bd9Sstevel@tonic-gate static char *get_title(char *rootdir) 28547c478bd9Sstevel@tonic-gate { 28557c478bd9Sstevel@tonic-gate static char title[80]; /* from /etc/release */ 28567c478bd9Sstevel@tonic-gate char *cp, release[PATH_MAX]; 28577c478bd9Sstevel@tonic-gate FILE *fp; 28587c478bd9Sstevel@tonic-gate 28597c478bd9Sstevel@tonic-gate /* open the /etc/release file */ 28607c478bd9Sstevel@tonic-gate (void) snprintf(release, sizeof (release), "%s/etc/release", rootdir); 28617c478bd9Sstevel@tonic-gate 28627c478bd9Sstevel@tonic-gate fp = fopen(release, "r"); 28637c478bd9Sstevel@tonic-gate if (fp == NULL) 28647c478bd9Sstevel@tonic-gate return ("Solaris"); /* default to Solaris */ 28657c478bd9Sstevel@tonic-gate 28667c478bd9Sstevel@tonic-gate while (s_fgets(title, sizeof (title), fp) != NULL) { 28677c478bd9Sstevel@tonic-gate cp = strstr(title, "Solaris"); 28687c478bd9Sstevel@tonic-gate if (cp) 28697c478bd9Sstevel@tonic-gate break; 28707c478bd9Sstevel@tonic-gate } 28717c478bd9Sstevel@tonic-gate (void) fclose(fp); 28727c478bd9Sstevel@tonic-gate return (cp); 28737c478bd9Sstevel@tonic-gate } 28747c478bd9Sstevel@tonic-gate 28757c478bd9Sstevel@tonic-gate static char * 28767c478bd9Sstevel@tonic-gate get_special(char *mountp) 28777c478bd9Sstevel@tonic-gate { 28787c478bd9Sstevel@tonic-gate FILE *mntfp; 28797c478bd9Sstevel@tonic-gate struct mnttab mp = {0}, mpref = {0}; 28807c478bd9Sstevel@tonic-gate 28817c478bd9Sstevel@tonic-gate mntfp = fopen(MNTTAB, "r"); 28827c478bd9Sstevel@tonic-gate if (mntfp == NULL) { 28837c478bd9Sstevel@tonic-gate return (0); 28847c478bd9Sstevel@tonic-gate } 28857c478bd9Sstevel@tonic-gate 28867c478bd9Sstevel@tonic-gate if (*mountp == '\0') 28877c478bd9Sstevel@tonic-gate mpref.mnt_mountp = "/"; 28887c478bd9Sstevel@tonic-gate else 28897c478bd9Sstevel@tonic-gate mpref.mnt_mountp = mountp; 28907c478bd9Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 28917c478bd9Sstevel@tonic-gate (void) fclose(mntfp); 28927c478bd9Sstevel@tonic-gate return (NULL); 28937c478bd9Sstevel@tonic-gate } 28947c478bd9Sstevel@tonic-gate (void) fclose(mntfp); 28957c478bd9Sstevel@tonic-gate 28967c478bd9Sstevel@tonic-gate return (s_strdup(mp.mnt_special)); 28977c478bd9Sstevel@tonic-gate } 28987c478bd9Sstevel@tonic-gate 28997c478bd9Sstevel@tonic-gate static char * 29007c478bd9Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev) 29017c478bd9Sstevel@tonic-gate { 29027c478bd9Sstevel@tonic-gate FILE *fp; 29037c478bd9Sstevel@tonic-gate char *grubdisk; 29047c478bd9Sstevel@tonic-gate 29057c478bd9Sstevel@tonic-gate /* translate /dev/dsk name to grub disk name */ 29067c478bd9Sstevel@tonic-gate fp = open_diskmap(); 29077c478bd9Sstevel@tonic-gate if (fp == NULL) { 29087c478bd9Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdisk); 29097c478bd9Sstevel@tonic-gate return (NULL); 29107c478bd9Sstevel@tonic-gate } 29117c478bd9Sstevel@tonic-gate grubdisk = get_grubdisk(osdisk, fp, on_bootdev); 29127c478bd9Sstevel@tonic-gate (void) fclose(fp); 29137c478bd9Sstevel@tonic-gate return (grubdisk); 29147c478bd9Sstevel@tonic-gate } 29157c478bd9Sstevel@tonic-gate 29167c478bd9Sstevel@tonic-gate /* 29177c478bd9Sstevel@tonic-gate * Check if root is on the boot device 29187c478bd9Sstevel@tonic-gate * Return 0 (false) on error 29197c478bd9Sstevel@tonic-gate */ 29207c478bd9Sstevel@tonic-gate static int 29217c478bd9Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp) 29227c478bd9Sstevel@tonic-gate { 29237c478bd9Sstevel@tonic-gate int ret; 29247c478bd9Sstevel@tonic-gate char *grubhd, *bootp, *special; 29257c478bd9Sstevel@tonic-gate 29267c478bd9Sstevel@tonic-gate special = get_special(menu_root); 29277c478bd9Sstevel@tonic-gate if (special == NULL) 29287c478bd9Sstevel@tonic-gate return (0); 29297c478bd9Sstevel@tonic-gate bootp = strstr(special, "p0:boot"); 29307c478bd9Sstevel@tonic-gate if (bootp) 29317c478bd9Sstevel@tonic-gate *bootp = '\0'; 29327c478bd9Sstevel@tonic-gate grubhd = get_grubdisk(special, fp, 1); 29337c478bd9Sstevel@tonic-gate free(special); 29347c478bd9Sstevel@tonic-gate 29357c478bd9Sstevel@tonic-gate if (grubhd == NULL) 29367c478bd9Sstevel@tonic-gate return (0); 29377c478bd9Sstevel@tonic-gate ret = grubhd[3] == '0'; 29387c478bd9Sstevel@tonic-gate free(grubhd); 29397c478bd9Sstevel@tonic-gate return (ret); 29407c478bd9Sstevel@tonic-gate } 29417c478bd9Sstevel@tonic-gate 29427c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 29437c478bd9Sstevel@tonic-gate static error_t 29447c478bd9Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt) 29457c478bd9Sstevel@tonic-gate { 29467c478bd9Sstevel@tonic-gate FILE *fp; 29477c478bd9Sstevel@tonic-gate int entry; 29487c478bd9Sstevel@tonic-gate line_t *lp; 29497c478bd9Sstevel@tonic-gate char *grubdisk, *title, *osdev, *osroot; 29507c478bd9Sstevel@tonic-gate int bootadm_entry, entry_to_delete; 29517c478bd9Sstevel@tonic-gate 29527c478bd9Sstevel@tonic-gate assert(mp); 29537c478bd9Sstevel@tonic-gate assert(opt); 29547c478bd9Sstevel@tonic-gate 29557c478bd9Sstevel@tonic-gate osdev = strtok(opt, ","); 29567c478bd9Sstevel@tonic-gate osroot = strtok(NULL, ","); 29577c478bd9Sstevel@tonic-gate if (osroot == NULL) 29587c478bd9Sstevel@tonic-gate osroot = menu_root; 29597c478bd9Sstevel@tonic-gate title = get_title(osroot); 29607c478bd9Sstevel@tonic-gate 29617c478bd9Sstevel@tonic-gate /* translate /dev/dsk name to grub disk name */ 29627c478bd9Sstevel@tonic-gate fp = open_diskmap(); 29637c478bd9Sstevel@tonic-gate if (fp == NULL) { 29647c478bd9Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdev); 29657c478bd9Sstevel@tonic-gate return (BAM_ERROR); 29667c478bd9Sstevel@tonic-gate } 29677c478bd9Sstevel@tonic-gate grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp)); 29687c478bd9Sstevel@tonic-gate (void) fclose(fp); 29697c478bd9Sstevel@tonic-gate if (grubdisk == NULL) { 29707c478bd9Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdev); 29717c478bd9Sstevel@tonic-gate return (BAM_ERROR); 29727c478bd9Sstevel@tonic-gate } 29737c478bd9Sstevel@tonic-gate 29747c478bd9Sstevel@tonic-gate /* delete existing entries with matching grub hd name */ 29757c478bd9Sstevel@tonic-gate for (;;) { 29767c478bd9Sstevel@tonic-gate entry_to_delete = -1; 29777c478bd9Sstevel@tonic-gate bootadm_entry = 0; 29787c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 29797c478bd9Sstevel@tonic-gate /* 29807c478bd9Sstevel@tonic-gate * can only delete bootadm entries 29817c478bd9Sstevel@tonic-gate */ 29827c478bd9Sstevel@tonic-gate if (lp->flags == BAM_COMMENT) { 29837c478bd9Sstevel@tonic-gate if (strcmp(lp->arg, BAM_HDR) == 0) 29847c478bd9Sstevel@tonic-gate bootadm_entry = 1; 29857c478bd9Sstevel@tonic-gate else if (strcmp(lp->arg, BAM_FTR) == 0) 29867c478bd9Sstevel@tonic-gate bootadm_entry = 0; 29877c478bd9Sstevel@tonic-gate } 29887c478bd9Sstevel@tonic-gate 29897c478bd9Sstevel@tonic-gate if (bootadm_entry && lp->flags == BAM_ENTRY && 29907c478bd9Sstevel@tonic-gate strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0 && 29917c478bd9Sstevel@tonic-gate strcmp(lp->arg, grubdisk) == 0) { 29927c478bd9Sstevel@tonic-gate entry_to_delete = lp->entryNum; 29937c478bd9Sstevel@tonic-gate } 29947c478bd9Sstevel@tonic-gate } 29957c478bd9Sstevel@tonic-gate if (entry_to_delete == -1) 29967c478bd9Sstevel@tonic-gate break; 29977c478bd9Sstevel@tonic-gate (void) do_delete(mp, entry_to_delete); 29987c478bd9Sstevel@tonic-gate } 29997c478bd9Sstevel@tonic-gate 30007c478bd9Sstevel@tonic-gate /* add the entry for normal Solaris */ 30017c478bd9Sstevel@tonic-gate entry = add_boot_entry(mp, title, grubdisk, 30027c478bd9Sstevel@tonic-gate "/platform/i86pc/multiboot", 30037c478bd9Sstevel@tonic-gate "/platform/i86pc/boot_archive"); 30047c478bd9Sstevel@tonic-gate 30057c478bd9Sstevel@tonic-gate /* add the entry for failsafe archive */ 30067c478bd9Sstevel@tonic-gate (void) add_boot_entry(mp, "Solaris failsafe", grubdisk, 30077c478bd9Sstevel@tonic-gate "/boot/multiboot kernel/unix -s", 30087c478bd9Sstevel@tonic-gate "/boot/x86.miniroot-safe"); 30097c478bd9Sstevel@tonic-gate free(grubdisk); 30107c478bd9Sstevel@tonic-gate 30117c478bd9Sstevel@tonic-gate if (entry == BAM_ERROR) { 30127c478bd9Sstevel@tonic-gate return (BAM_ERROR); 30137c478bd9Sstevel@tonic-gate } 30147c478bd9Sstevel@tonic-gate (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 30157c478bd9Sstevel@tonic-gate return (BAM_WRITE); 30167c478bd9Sstevel@tonic-gate } 30177c478bd9Sstevel@tonic-gate 3018b610f78eSvikram static char * 3019b610f78eSvikram read_grub_root(void) 3020b610f78eSvikram { 3021b610f78eSvikram FILE *fp; 3022b610f78eSvikram struct stat sb; 3023b610f78eSvikram char buf[BAM_MAXLINE]; 3024b610f78eSvikram char *rootstr; 3025b610f78eSvikram 3026b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 3027b610f78eSvikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 3028b610f78eSvikram return (NULL); 3029b610f78eSvikram } 3030b610f78eSvikram 3031b610f78eSvikram if (stat(GRUB_root, &sb) != 0) { 3032b610f78eSvikram bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno)); 3033b610f78eSvikram return (NULL); 3034b610f78eSvikram } 3035b610f78eSvikram 3036b610f78eSvikram fp = fopen(GRUB_root, "r"); 3037b610f78eSvikram if (fp == NULL) { 3038b610f78eSvikram bam_error(OPEN_FAIL, GRUB_root, strerror(errno)); 3039b610f78eSvikram return (NULL); 3040b610f78eSvikram } 3041b610f78eSvikram 3042b610f78eSvikram if (s_fgets(buf, sizeof (buf), fp) == NULL) { 3043b610f78eSvikram bam_error(EMPTY_FILE, GRUB_root, strerror(errno)); 3044b610f78eSvikram (void) fclose(fp); 3045b610f78eSvikram return (NULL); 3046b610f78eSvikram } 3047b610f78eSvikram 3048b610f78eSvikram /* 3049b610f78eSvikram * Copy buf here as check below may trash the buffer 3050b610f78eSvikram */ 3051b610f78eSvikram rootstr = s_strdup(buf); 3052b610f78eSvikram 3053b610f78eSvikram if (s_fgets(buf, sizeof (buf), fp) != NULL) { 3054b610f78eSvikram bam_error(BAD_ROOT_FILE, GRUB_root); 3055b610f78eSvikram free(rootstr); 3056b610f78eSvikram rootstr = NULL; 3057b610f78eSvikram } 3058b610f78eSvikram 3059b610f78eSvikram (void) fclose(fp); 3060b610f78eSvikram 3061b610f78eSvikram return (rootstr); 3062b610f78eSvikram } 3063b610f78eSvikram 30647c478bd9Sstevel@tonic-gate /* 30657c478bd9Sstevel@tonic-gate * This function is for supporting reboot with args. 30667c478bd9Sstevel@tonic-gate * The opt value can be: 30677c478bd9Sstevel@tonic-gate * NULL delete temp entry, if present 30687c478bd9Sstevel@tonic-gate * entry=# switches default entry to 1 30697c478bd9Sstevel@tonic-gate * else treated as boot-args and setup a temperary menu entry 30707c478bd9Sstevel@tonic-gate * and make it the default 30717c478bd9Sstevel@tonic-gate */ 30727c478bd9Sstevel@tonic-gate #define REBOOT_TITLE "Solaris_reboot_transient" 30737c478bd9Sstevel@tonic-gate 30747c478bd9Sstevel@tonic-gate static error_t 30757c478bd9Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt) 30767c478bd9Sstevel@tonic-gate { 30777c478bd9Sstevel@tonic-gate int entry; 30787c478bd9Sstevel@tonic-gate char *grubdisk, *rootdev; 30797c478bd9Sstevel@tonic-gate char kernbuf[1024]; 3080b610f78eSvikram struct stat sb; 30817c478bd9Sstevel@tonic-gate 30827c478bd9Sstevel@tonic-gate assert(mp); 30837c478bd9Sstevel@tonic-gate 30847c478bd9Sstevel@tonic-gate if (opt != NULL && 30857c478bd9Sstevel@tonic-gate strncmp(opt, "entry=", strlen("entry=")) == 0 && 30867c478bd9Sstevel@tonic-gate selector(mp, opt, &entry, NULL) == BAM_SUCCESS) { 30877c478bd9Sstevel@tonic-gate /* this is entry=# option */ 30887c478bd9Sstevel@tonic-gate return (set_global(mp, menu_cmds[DEFAULT_CMD], entry)); 30897c478bd9Sstevel@tonic-gate } 30907c478bd9Sstevel@tonic-gate 30917c478bd9Sstevel@tonic-gate /* If no option, delete exiting reboot menu entry */ 30927c478bd9Sstevel@tonic-gate if (opt == NULL) 30937c478bd9Sstevel@tonic-gate return (delete_entry(mp, menupath, "title="REBOOT_TITLE)); 30947c478bd9Sstevel@tonic-gate 30957c478bd9Sstevel@tonic-gate /* 30967c478bd9Sstevel@tonic-gate * add a new menu entry base on opt and make it the default 30977c478bd9Sstevel@tonic-gate */ 3098b610f78eSvikram grubdisk = NULL; 3099b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 3100b610f78eSvikram /* 3101b610f78eSvikram * 1. First get root disk name from mnttab 3102b610f78eSvikram * 2. Translate disk name to grub name 3103b610f78eSvikram * 3. Add the new menu entry 3104b610f78eSvikram */ 3105b610f78eSvikram rootdev = get_special("/"); 3106b610f78eSvikram if (rootdev) { 3107b610f78eSvikram grubdisk = os_to_grubdisk(rootdev, 1); 3108b610f78eSvikram free(rootdev); 3109b610f78eSvikram } 3110b610f78eSvikram } else { 3111b610f78eSvikram /* 3112b610f78eSvikram * This is an LU BE. The GRUB_root file 3113b610f78eSvikram * contains entry for GRUB's "root" cmd. 3114b610f78eSvikram */ 3115b610f78eSvikram grubdisk = read_grub_root(); 31167c478bd9Sstevel@tonic-gate } 31177c478bd9Sstevel@tonic-gate if (grubdisk == NULL) { 3118b610f78eSvikram bam_error(REBOOT_WITH_ARGS_FAILED); 31197c478bd9Sstevel@tonic-gate return (BAM_ERROR); 31207c478bd9Sstevel@tonic-gate } 31217c478bd9Sstevel@tonic-gate 31227c478bd9Sstevel@tonic-gate /* add an entry for Solaris reboot */ 31237c478bd9Sstevel@tonic-gate (void) snprintf(kernbuf, sizeof (kernbuf), 31247c478bd9Sstevel@tonic-gate "/platform/i86pc/multiboot %s", opt); 31257c478bd9Sstevel@tonic-gate entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf, 31267c478bd9Sstevel@tonic-gate "/platform/i86pc/boot_archive"); 31277c478bd9Sstevel@tonic-gate free(grubdisk); 31287c478bd9Sstevel@tonic-gate 31297c478bd9Sstevel@tonic-gate if (entry == BAM_ERROR) { 3130b610f78eSvikram bam_error(REBOOT_WITH_ARGS_FAILED); 31317c478bd9Sstevel@tonic-gate return (BAM_ERROR); 31327c478bd9Sstevel@tonic-gate } 31337c478bd9Sstevel@tonic-gate (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 31347c478bd9Sstevel@tonic-gate return (BAM_WRITE); 31357c478bd9Sstevel@tonic-gate } 31367c478bd9Sstevel@tonic-gate 31377c478bd9Sstevel@tonic-gate static error_t 31387c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val) 31397c478bd9Sstevel@tonic-gate { 31407c478bd9Sstevel@tonic-gate line_t *lp, *found, *last; 31417c478bd9Sstevel@tonic-gate char *cp, *str; 31427c478bd9Sstevel@tonic-gate char prefix[BAM_MAXLINE]; 31437c478bd9Sstevel@tonic-gate size_t len; 31447c478bd9Sstevel@tonic-gate 31457c478bd9Sstevel@tonic-gate assert(mp); 31467c478bd9Sstevel@tonic-gate assert(globalcmd); 31477c478bd9Sstevel@tonic-gate 3148b610f78eSvikram if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) { 3149b610f78eSvikram if (val < 0 || mp->end == NULL || val > mp->end->entryNum) { 3150b610f78eSvikram (void) snprintf(prefix, sizeof (prefix), "%d", val); 3151b610f78eSvikram bam_error(INVALID_ENTRY, prefix); 3152b610f78eSvikram return (BAM_ERROR); 3153b610f78eSvikram } 3154b610f78eSvikram } 3155b610f78eSvikram 31567c478bd9Sstevel@tonic-gate found = last = NULL; 31577c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 31587c478bd9Sstevel@tonic-gate if (lp->flags != BAM_GLOBAL) 31597c478bd9Sstevel@tonic-gate continue; 31607c478bd9Sstevel@tonic-gate 31617c478bd9Sstevel@tonic-gate last = lp; /* track the last global found */ 31627c478bd9Sstevel@tonic-gate 31637c478bd9Sstevel@tonic-gate if (lp->cmd == NULL) { 31647c478bd9Sstevel@tonic-gate bam_error(NO_CMD, lp->lineNum); 31657c478bd9Sstevel@tonic-gate continue; 31667c478bd9Sstevel@tonic-gate } 31677c478bd9Sstevel@tonic-gate if (strcmp(globalcmd, lp->cmd) != 0) 31687c478bd9Sstevel@tonic-gate continue; 31697c478bd9Sstevel@tonic-gate 31707c478bd9Sstevel@tonic-gate if (found) { 31717c478bd9Sstevel@tonic-gate bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 31727c478bd9Sstevel@tonic-gate } 31737c478bd9Sstevel@tonic-gate found = lp; 31747c478bd9Sstevel@tonic-gate } 31757c478bd9Sstevel@tonic-gate 31767c478bd9Sstevel@tonic-gate if (found == NULL) { 31777c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 31787c478bd9Sstevel@tonic-gate if (last == NULL) { 31797c478bd9Sstevel@tonic-gate lp->next = mp->start; 31807c478bd9Sstevel@tonic-gate mp->start = lp; 31817c478bd9Sstevel@tonic-gate mp->end = (mp->end) ? mp->end : lp; 31827c478bd9Sstevel@tonic-gate } else { 31837c478bd9Sstevel@tonic-gate lp->next = last->next; 31847c478bd9Sstevel@tonic-gate last->next = lp; 31857c478bd9Sstevel@tonic-gate if (lp->next == NULL) 31867c478bd9Sstevel@tonic-gate mp->end = lp; 31877c478bd9Sstevel@tonic-gate } 31887c478bd9Sstevel@tonic-gate lp->flags = BAM_GLOBAL; /* other fields not needed for writes */ 31897c478bd9Sstevel@tonic-gate len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]); 31907c478bd9Sstevel@tonic-gate len += 10; /* val < 10 digits */ 31917c478bd9Sstevel@tonic-gate lp->line = s_calloc(1, len); 31927c478bd9Sstevel@tonic-gate (void) snprintf(lp->line, len, "%s%s%d", 31937c478bd9Sstevel@tonic-gate globalcmd, menu_cmds[SEP_CMD], val); 31947c478bd9Sstevel@tonic-gate return (BAM_WRITE); 31957c478bd9Sstevel@tonic-gate } 31967c478bd9Sstevel@tonic-gate 31977c478bd9Sstevel@tonic-gate /* 31987c478bd9Sstevel@tonic-gate * We are changing an existing entry. Retain any prefix whitespace, 31997c478bd9Sstevel@tonic-gate * but overwrite everything else. This preserves tabs added for 32007c478bd9Sstevel@tonic-gate * readability. 32017c478bd9Sstevel@tonic-gate */ 32027c478bd9Sstevel@tonic-gate str = found->line; 32037c478bd9Sstevel@tonic-gate cp = prefix; 32047c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 32057c478bd9Sstevel@tonic-gate *(cp++) = *(str++); 32067c478bd9Sstevel@tonic-gate *cp = '\0'; /* Terminate prefix */ 32077c478bd9Sstevel@tonic-gate len = strlen(prefix) + strlen(globalcmd); 32087c478bd9Sstevel@tonic-gate len += strlen(menu_cmds[SEP_CMD]) + 10; 32097c478bd9Sstevel@tonic-gate 32107c478bd9Sstevel@tonic-gate free(found->line); 32117c478bd9Sstevel@tonic-gate found->line = s_calloc(1, len); 32127c478bd9Sstevel@tonic-gate (void) snprintf(found->line, len, 32137c478bd9Sstevel@tonic-gate "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val); 32147c478bd9Sstevel@tonic-gate 32157c478bd9Sstevel@tonic-gate return (BAM_WRITE); /* need a write to menu */ 32167c478bd9Sstevel@tonic-gate } 32177c478bd9Sstevel@tonic-gate 32187c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 32197c478bd9Sstevel@tonic-gate static error_t 32207c478bd9Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt) 32217c478bd9Sstevel@tonic-gate { 32227c478bd9Sstevel@tonic-gate int optnum, optval; 32237c478bd9Sstevel@tonic-gate char *val; 32247c478bd9Sstevel@tonic-gate 32257c478bd9Sstevel@tonic-gate assert(mp); 32267c478bd9Sstevel@tonic-gate assert(opt); 32277c478bd9Sstevel@tonic-gate 32287c478bd9Sstevel@tonic-gate val = strchr(opt, '='); 32297c478bd9Sstevel@tonic-gate if (val == NULL) { 32307c478bd9Sstevel@tonic-gate bam_error(INVALID_ENTRY, opt); 32317c478bd9Sstevel@tonic-gate return (BAM_ERROR); 32327c478bd9Sstevel@tonic-gate } 32337c478bd9Sstevel@tonic-gate 32347c478bd9Sstevel@tonic-gate *val = '\0'; 32357c478bd9Sstevel@tonic-gate if (strcmp(opt, "default") == 0) { 32367c478bd9Sstevel@tonic-gate optnum = DEFAULT_CMD; 32377c478bd9Sstevel@tonic-gate } else if (strcmp(opt, "timeout") == 0) { 32387c478bd9Sstevel@tonic-gate optnum = TIMEOUT_CMD; 32397c478bd9Sstevel@tonic-gate } else { 32407c478bd9Sstevel@tonic-gate bam_error(INVALID_ENTRY, opt); 32417c478bd9Sstevel@tonic-gate return (BAM_ERROR); 32427c478bd9Sstevel@tonic-gate } 32437c478bd9Sstevel@tonic-gate 32447c478bd9Sstevel@tonic-gate optval = s_strtol(val + 1); 32457c478bd9Sstevel@tonic-gate *val = '='; 32467c478bd9Sstevel@tonic-gate return (set_global(mp, menu_cmds[optnum], optval)); 32477c478bd9Sstevel@tonic-gate } 32487c478bd9Sstevel@tonic-gate 32497c478bd9Sstevel@tonic-gate /* 32507c478bd9Sstevel@tonic-gate * The quiet argument suppresses messages. This is used 32517c478bd9Sstevel@tonic-gate * when invoked in the context of other commands (e.g. list_entry) 32527c478bd9Sstevel@tonic-gate */ 32537c478bd9Sstevel@tonic-gate static error_t 32547c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet) 32557c478bd9Sstevel@tonic-gate { 32567c478bd9Sstevel@tonic-gate line_t *lp; 32577c478bd9Sstevel@tonic-gate char *arg; 32587c478bd9Sstevel@tonic-gate int done, ret = BAM_SUCCESS; 32597c478bd9Sstevel@tonic-gate 32607c478bd9Sstevel@tonic-gate assert(mp); 32617c478bd9Sstevel@tonic-gate assert(menu_path); 32627c478bd9Sstevel@tonic-gate assert(globalcmd); 32637c478bd9Sstevel@tonic-gate 32647c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 32657c478bd9Sstevel@tonic-gate if (!quiet) 32667c478bd9Sstevel@tonic-gate bam_error(NO_MENU, menu_path); 32677c478bd9Sstevel@tonic-gate return (BAM_ERROR); 32687c478bd9Sstevel@tonic-gate } 32697c478bd9Sstevel@tonic-gate 32707c478bd9Sstevel@tonic-gate done = 0; 32717c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 32727c478bd9Sstevel@tonic-gate if (lp->flags != BAM_GLOBAL) 32737c478bd9Sstevel@tonic-gate continue; 32747c478bd9Sstevel@tonic-gate 32757c478bd9Sstevel@tonic-gate if (lp->cmd == NULL) { 32767c478bd9Sstevel@tonic-gate if (!quiet) 32777c478bd9Sstevel@tonic-gate bam_error(NO_CMD, lp->lineNum); 32787c478bd9Sstevel@tonic-gate continue; 32797c478bd9Sstevel@tonic-gate } 32807c478bd9Sstevel@tonic-gate 32817c478bd9Sstevel@tonic-gate if (strcmp(globalcmd, lp->cmd) != 0) 32827c478bd9Sstevel@tonic-gate continue; 32837c478bd9Sstevel@tonic-gate 32847c478bd9Sstevel@tonic-gate /* Found global. Check for duplicates */ 32857c478bd9Sstevel@tonic-gate if (done && !quiet) { 32867c478bd9Sstevel@tonic-gate bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 32877c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 32887c478bd9Sstevel@tonic-gate } 32897c478bd9Sstevel@tonic-gate 32907c478bd9Sstevel@tonic-gate arg = lp->arg ? lp->arg : ""; 32917c478bd9Sstevel@tonic-gate bam_print(GLOBAL_CMD, globalcmd, arg); 32927c478bd9Sstevel@tonic-gate done = 1; 32937c478bd9Sstevel@tonic-gate } 32947c478bd9Sstevel@tonic-gate 32957c478bd9Sstevel@tonic-gate if (!done && bam_verbose) 32967c478bd9Sstevel@tonic-gate bam_print(NO_ENTRY, globalcmd); 32977c478bd9Sstevel@tonic-gate 32987c478bd9Sstevel@tonic-gate return (ret); 32997c478bd9Sstevel@tonic-gate } 33007c478bd9Sstevel@tonic-gate 33017c478bd9Sstevel@tonic-gate static error_t 33027c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp) 33037c478bd9Sstevel@tonic-gate { 33047c478bd9Sstevel@tonic-gate return (list2file(root, MENU_TMP, GRUB_MENU, mp->start)); 33057c478bd9Sstevel@tonic-gate } 33067c478bd9Sstevel@tonic-gate 33077c478bd9Sstevel@tonic-gate static void 33087c478bd9Sstevel@tonic-gate line_free(line_t *lp) 33097c478bd9Sstevel@tonic-gate { 33107c478bd9Sstevel@tonic-gate if (lp == NULL) 33117c478bd9Sstevel@tonic-gate return; 33127c478bd9Sstevel@tonic-gate 33137c478bd9Sstevel@tonic-gate if (lp->cmd) 33147c478bd9Sstevel@tonic-gate free(lp->cmd); 33157c478bd9Sstevel@tonic-gate if (lp->sep) 33167c478bd9Sstevel@tonic-gate free(lp->sep); 33177c478bd9Sstevel@tonic-gate if (lp->arg) 33187c478bd9Sstevel@tonic-gate free(lp->arg); 33197c478bd9Sstevel@tonic-gate if (lp->line) 33207c478bd9Sstevel@tonic-gate free(lp->line); 33217c478bd9Sstevel@tonic-gate free(lp); 33227c478bd9Sstevel@tonic-gate } 33237c478bd9Sstevel@tonic-gate 33247c478bd9Sstevel@tonic-gate static void 33257c478bd9Sstevel@tonic-gate linelist_free(line_t *start) 33267c478bd9Sstevel@tonic-gate { 33277c478bd9Sstevel@tonic-gate line_t *lp; 33287c478bd9Sstevel@tonic-gate 33297c478bd9Sstevel@tonic-gate while (start) { 33307c478bd9Sstevel@tonic-gate lp = start; 33317c478bd9Sstevel@tonic-gate start = start->next; 33327c478bd9Sstevel@tonic-gate line_free(lp); 33337c478bd9Sstevel@tonic-gate } 33347c478bd9Sstevel@tonic-gate } 33357c478bd9Sstevel@tonic-gate 33367c478bd9Sstevel@tonic-gate static void 33377c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp) 33387c478bd9Sstevel@tonic-gate { 33397c478bd9Sstevel@tonic-gate linelist_free(flistp->head); 33407c478bd9Sstevel@tonic-gate flistp->head = NULL; 33417c478bd9Sstevel@tonic-gate flistp->tail = NULL; 33427c478bd9Sstevel@tonic-gate } 33437c478bd9Sstevel@tonic-gate 33447c478bd9Sstevel@tonic-gate static void 33457c478bd9Sstevel@tonic-gate menu_free(menu_t *mp) 33467c478bd9Sstevel@tonic-gate { 33477c478bd9Sstevel@tonic-gate assert(mp); 33487c478bd9Sstevel@tonic-gate 33497c478bd9Sstevel@tonic-gate if (mp->start) 33507c478bd9Sstevel@tonic-gate linelist_free(mp->start); 33517c478bd9Sstevel@tonic-gate free(mp); 33527c478bd9Sstevel@tonic-gate 33537c478bd9Sstevel@tonic-gate } 33547c478bd9Sstevel@tonic-gate 33557c478bd9Sstevel@tonic-gate /* 33567c478bd9Sstevel@tonic-gate * Utility routines 33577c478bd9Sstevel@tonic-gate */ 33587c478bd9Sstevel@tonic-gate 33597c478bd9Sstevel@tonic-gate 33607c478bd9Sstevel@tonic-gate /* 33617c478bd9Sstevel@tonic-gate * Returns 0 on success 33627c478bd9Sstevel@tonic-gate * Any other value indicates an error 33637c478bd9Sstevel@tonic-gate */ 33647c478bd9Sstevel@tonic-gate static int 33657c478bd9Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize) 33667c478bd9Sstevel@tonic-gate { 33677c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 33687c478bd9Sstevel@tonic-gate int ret; 33697c478bd9Sstevel@tonic-gate FILE *ptr; 33707c478bd9Sstevel@tonic-gate size_t len; 33717c478bd9Sstevel@tonic-gate sigset_t set; 33727c478bd9Sstevel@tonic-gate void (*disp)(int); 33737c478bd9Sstevel@tonic-gate 33747c478bd9Sstevel@tonic-gate /* 33757c478bd9Sstevel@tonic-gate * For security 33767c478bd9Sstevel@tonic-gate * - only absolute paths are allowed 33777c478bd9Sstevel@tonic-gate * - set IFS to space and tab 33787c478bd9Sstevel@tonic-gate */ 33797c478bd9Sstevel@tonic-gate if (*cmdline != '/') { 33807c478bd9Sstevel@tonic-gate bam_error(ABS_PATH_REQ, cmdline); 33817c478bd9Sstevel@tonic-gate return (-1); 33827c478bd9Sstevel@tonic-gate } 33837c478bd9Sstevel@tonic-gate (void) putenv("IFS= \t"); 33847c478bd9Sstevel@tonic-gate 33857c478bd9Sstevel@tonic-gate /* 33867c478bd9Sstevel@tonic-gate * We may have been exec'ed with SIGCHLD blocked 33877c478bd9Sstevel@tonic-gate * unblock it here 33887c478bd9Sstevel@tonic-gate */ 33897c478bd9Sstevel@tonic-gate (void) sigemptyset(&set); 33907c478bd9Sstevel@tonic-gate (void) sigaddset(&set, SIGCHLD); 33917c478bd9Sstevel@tonic-gate if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { 33927c478bd9Sstevel@tonic-gate bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno)); 33937c478bd9Sstevel@tonic-gate return (-1); 33947c478bd9Sstevel@tonic-gate } 33957c478bd9Sstevel@tonic-gate 33967c478bd9Sstevel@tonic-gate /* 33977c478bd9Sstevel@tonic-gate * Set SIGCHLD disposition to SIG_DFL for popen/pclose 33987c478bd9Sstevel@tonic-gate */ 33997c478bd9Sstevel@tonic-gate disp = sigset(SIGCHLD, SIG_DFL); 34007c478bd9Sstevel@tonic-gate if (disp == SIG_ERR) { 34017c478bd9Sstevel@tonic-gate bam_error(FAILED_SIG, strerror(errno)); 34027c478bd9Sstevel@tonic-gate return (-1); 34037c478bd9Sstevel@tonic-gate } 34047c478bd9Sstevel@tonic-gate if (disp == SIG_HOLD) { 34057c478bd9Sstevel@tonic-gate bam_error(BLOCKED_SIG, cmdline); 34067c478bd9Sstevel@tonic-gate return (-1); 34077c478bd9Sstevel@tonic-gate } 34087c478bd9Sstevel@tonic-gate 34097c478bd9Sstevel@tonic-gate ptr = popen(cmdline, "r"); 34107c478bd9Sstevel@tonic-gate if (ptr == NULL) { 34117c478bd9Sstevel@tonic-gate bam_error(POPEN_FAIL, cmdline, strerror(errno)); 34127c478bd9Sstevel@tonic-gate return (-1); 34137c478bd9Sstevel@tonic-gate } 34147c478bd9Sstevel@tonic-gate 34157c478bd9Sstevel@tonic-gate /* 34167c478bd9Sstevel@tonic-gate * If we simply do a pclose() following a popen(), pclose() 34177c478bd9Sstevel@tonic-gate * will close the reader end of the pipe immediately even 34187c478bd9Sstevel@tonic-gate * if the child process has not started/exited. pclose() 34197c478bd9Sstevel@tonic-gate * does wait for cmd to terminate before returning though. 34207c478bd9Sstevel@tonic-gate * When the executed command writes its output to the pipe 34217c478bd9Sstevel@tonic-gate * there is no reader process and the command dies with 34227c478bd9Sstevel@tonic-gate * SIGPIPE. To avoid this we read repeatedly until read 34237c478bd9Sstevel@tonic-gate * terminates with EOF. This indicates that the command 34247c478bd9Sstevel@tonic-gate * (writer) has closed the pipe and we can safely do a 34257c478bd9Sstevel@tonic-gate * pclose(). 34267c478bd9Sstevel@tonic-gate * 34277c478bd9Sstevel@tonic-gate * Since pclose() does wait for the command to exit, 34287c478bd9Sstevel@tonic-gate * we can safely reap the exit status of the command 34297c478bd9Sstevel@tonic-gate * from the value returned by pclose() 34307c478bd9Sstevel@tonic-gate */ 34317c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), ptr) != NULL) { 34327c478bd9Sstevel@tonic-gate /* if (bam_verbose) XXX */ 34337c478bd9Sstevel@tonic-gate bam_print(PRINT_NO_NEWLINE, buf); 34347c478bd9Sstevel@tonic-gate if (output && osize > 0) { 34357c478bd9Sstevel@tonic-gate (void) snprintf(output, osize, "%s", buf); 34367c478bd9Sstevel@tonic-gate len = strlen(buf); 34377c478bd9Sstevel@tonic-gate output += len; 34387c478bd9Sstevel@tonic-gate osize -= len; 34397c478bd9Sstevel@tonic-gate } 34407c478bd9Sstevel@tonic-gate } 34417c478bd9Sstevel@tonic-gate 34427c478bd9Sstevel@tonic-gate ret = pclose(ptr); 34437c478bd9Sstevel@tonic-gate if (ret == -1) { 34447c478bd9Sstevel@tonic-gate bam_error(PCLOSE_FAIL, cmdline, strerror(errno)); 34457c478bd9Sstevel@tonic-gate return (-1); 34467c478bd9Sstevel@tonic-gate } 34477c478bd9Sstevel@tonic-gate 34487c478bd9Sstevel@tonic-gate if (WIFEXITED(ret)) { 34497c478bd9Sstevel@tonic-gate return (WEXITSTATUS(ret)); 34507c478bd9Sstevel@tonic-gate } else { 34517c478bd9Sstevel@tonic-gate bam_error(EXEC_FAIL, cmdline, ret); 34527c478bd9Sstevel@tonic-gate return (-1); 34537c478bd9Sstevel@tonic-gate } 34547c478bd9Sstevel@tonic-gate } 34557c478bd9Sstevel@tonic-gate 34567c478bd9Sstevel@tonic-gate /* 34577c478bd9Sstevel@tonic-gate * Since this function returns -1 on error 34587c478bd9Sstevel@tonic-gate * it cannot be used to convert -1. However, 34597c478bd9Sstevel@tonic-gate * that is sufficient for what we need. 34607c478bd9Sstevel@tonic-gate */ 34617c478bd9Sstevel@tonic-gate static long 34627c478bd9Sstevel@tonic-gate s_strtol(char *str) 34637c478bd9Sstevel@tonic-gate { 34647c478bd9Sstevel@tonic-gate long l; 34657c478bd9Sstevel@tonic-gate char *res = NULL; 34667c478bd9Sstevel@tonic-gate 34677c478bd9Sstevel@tonic-gate if (str == NULL) { 34687c478bd9Sstevel@tonic-gate return (-1); 34697c478bd9Sstevel@tonic-gate } 34707c478bd9Sstevel@tonic-gate 34717c478bd9Sstevel@tonic-gate errno = 0; 34727c478bd9Sstevel@tonic-gate l = strtol(str, &res, 10); 34737c478bd9Sstevel@tonic-gate if (errno || *res != '\0') { 34747c478bd9Sstevel@tonic-gate return (-1); 34757c478bd9Sstevel@tonic-gate } 34767c478bd9Sstevel@tonic-gate 34777c478bd9Sstevel@tonic-gate return (l); 34787c478bd9Sstevel@tonic-gate } 34797c478bd9Sstevel@tonic-gate 34807c478bd9Sstevel@tonic-gate /* 34817c478bd9Sstevel@tonic-gate * Wrapper around fputs, that adds a newline (since fputs doesn't) 34827c478bd9Sstevel@tonic-gate */ 34837c478bd9Sstevel@tonic-gate static int 34847c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp) 34857c478bd9Sstevel@tonic-gate { 34867c478bd9Sstevel@tonic-gate char linebuf[BAM_MAXLINE]; 34877c478bd9Sstevel@tonic-gate 34887c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s\n", str); 34897c478bd9Sstevel@tonic-gate return (fputs(linebuf, fp)); 34907c478bd9Sstevel@tonic-gate } 34917c478bd9Sstevel@tonic-gate 34927c478bd9Sstevel@tonic-gate /* 34937c478bd9Sstevel@tonic-gate * Wrapper around fgets, that strips newlines returned by fgets 34947c478bd9Sstevel@tonic-gate */ 34957c478bd9Sstevel@tonic-gate static char * 34967c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp) 34977c478bd9Sstevel@tonic-gate { 34987c478bd9Sstevel@tonic-gate int n; 34997c478bd9Sstevel@tonic-gate 35007c478bd9Sstevel@tonic-gate buf = fgets(buf, buflen, fp); 35017c478bd9Sstevel@tonic-gate if (buf) { 35027c478bd9Sstevel@tonic-gate n = strlen(buf); 35037c478bd9Sstevel@tonic-gate if (n == buflen - 1 && buf[n-1] != '\n') 35047c478bd9Sstevel@tonic-gate bam_error(TOO_LONG, buflen - 1, buf); 35057c478bd9Sstevel@tonic-gate buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1]; 35067c478bd9Sstevel@tonic-gate } 35077c478bd9Sstevel@tonic-gate 35087c478bd9Sstevel@tonic-gate return (buf); 35097c478bd9Sstevel@tonic-gate } 35107c478bd9Sstevel@tonic-gate 35117c478bd9Sstevel@tonic-gate static void * 35127c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz) 35137c478bd9Sstevel@tonic-gate { 35147c478bd9Sstevel@tonic-gate void *ptr; 35157c478bd9Sstevel@tonic-gate 35167c478bd9Sstevel@tonic-gate ptr = calloc(nelem, sz); 35177c478bd9Sstevel@tonic-gate if (ptr == NULL) { 35187c478bd9Sstevel@tonic-gate bam_error(NO_MEM, nelem*sz); 35197c478bd9Sstevel@tonic-gate bam_exit(1); 35207c478bd9Sstevel@tonic-gate } 35217c478bd9Sstevel@tonic-gate return (ptr); 35227c478bd9Sstevel@tonic-gate } 35237c478bd9Sstevel@tonic-gate 35247c478bd9Sstevel@tonic-gate static char * 35257c478bd9Sstevel@tonic-gate s_strdup(char *str) 35267c478bd9Sstevel@tonic-gate { 35277c478bd9Sstevel@tonic-gate char *ptr; 35287c478bd9Sstevel@tonic-gate 35297c478bd9Sstevel@tonic-gate if (str == NULL) 35307c478bd9Sstevel@tonic-gate return (NULL); 35317c478bd9Sstevel@tonic-gate 35327c478bd9Sstevel@tonic-gate ptr = strdup(str); 35337c478bd9Sstevel@tonic-gate if (ptr == NULL) { 35347c478bd9Sstevel@tonic-gate bam_error(NO_MEM, strlen(str) + 1); 35357c478bd9Sstevel@tonic-gate bam_exit(1); 35367c478bd9Sstevel@tonic-gate } 35377c478bd9Sstevel@tonic-gate return (ptr); 35387c478bd9Sstevel@tonic-gate } 35397c478bd9Sstevel@tonic-gate 35407c478bd9Sstevel@tonic-gate /* 35417c478bd9Sstevel@tonic-gate * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients) 35427c478bd9Sstevel@tonic-gate * Returns 0 otherwise 35437c478bd9Sstevel@tonic-gate */ 35447c478bd9Sstevel@tonic-gate static int 35457c478bd9Sstevel@tonic-gate is_amd64(void) 35467c478bd9Sstevel@tonic-gate { 35477c478bd9Sstevel@tonic-gate static int amd64 = -1; 35487c478bd9Sstevel@tonic-gate char isabuf[257]; /* from sysinfo(2) manpage */ 35497c478bd9Sstevel@tonic-gate 35507c478bd9Sstevel@tonic-gate if (amd64 != -1) 35517c478bd9Sstevel@tonic-gate return (amd64); 35527c478bd9Sstevel@tonic-gate 35537c478bd9Sstevel@tonic-gate if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 && 35547c478bd9Sstevel@tonic-gate strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) 35557c478bd9Sstevel@tonic-gate amd64 = 1; 35567c478bd9Sstevel@tonic-gate else if (strstr(isabuf, "i386") == NULL) 35577c478bd9Sstevel@tonic-gate amd64 = 1; /* diskless server */ 35587c478bd9Sstevel@tonic-gate else 35597c478bd9Sstevel@tonic-gate amd64 = 0; 35607c478bd9Sstevel@tonic-gate 35617c478bd9Sstevel@tonic-gate return (amd64); 35627c478bd9Sstevel@tonic-gate } 35637c478bd9Sstevel@tonic-gate 35647c478bd9Sstevel@tonic-gate static void 35657c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s) 35667c478bd9Sstevel@tonic-gate { 35677c478bd9Sstevel@tonic-gate line_t *lp; 35687c478bd9Sstevel@tonic-gate 35697c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 35707c478bd9Sstevel@tonic-gate lp->line = s_strdup(s); 35717c478bd9Sstevel@tonic-gate if (flistp->head == NULL) 35727c478bd9Sstevel@tonic-gate flistp->head = lp; 35737c478bd9Sstevel@tonic-gate else 35747c478bd9Sstevel@tonic-gate flistp->tail->next = lp; 35757c478bd9Sstevel@tonic-gate flistp->tail = lp; 35767c478bd9Sstevel@tonic-gate } 3577