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; 1008c1b6884Sszhou struct line *prev; 1017c478bd9Sstevel@tonic-gate } line_t; 1027c478bd9Sstevel@tonic-gate 1038c1b6884Sszhou typedef struct entry { 1048c1b6884Sszhou struct entry *next; 1058c1b6884Sszhou struct entry *prev; 1068c1b6884Sszhou line_t *start; 1078c1b6884Sszhou line_t *end; 1088c1b6884Sszhou } entry_t; 1098c1b6884Sszhou 1107c478bd9Sstevel@tonic-gate typedef struct { 1117c478bd9Sstevel@tonic-gate line_t *start; 1127c478bd9Sstevel@tonic-gate line_t *end; 1138c1b6884Sszhou line_t *curdefault; /* line containing default */ 1148c1b6884Sszhou line_t *olddefault; /* old default line (commented) */ 1158c1b6884Sszhou entry_t *entries; /* os entries */ 1167c478bd9Sstevel@tonic-gate } menu_t; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate typedef enum { 1197c478bd9Sstevel@tonic-gate OPT_ABSENT = 0, /* No option */ 1207c478bd9Sstevel@tonic-gate OPT_REQ, /* option required */ 1217c478bd9Sstevel@tonic-gate OPT_OPTIONAL /* option may or may not be present */ 1227c478bd9Sstevel@tonic-gate } option_t; 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate typedef enum { 125b610f78eSvikram BAM_ERROR = -1, /* Must be negative. add_boot_entry() depends on it */ 1267c478bd9Sstevel@tonic-gate BAM_SUCCESS = 0, 1277c478bd9Sstevel@tonic-gate BAM_WRITE = 2 1287c478bd9Sstevel@tonic-gate } error_t; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate typedef struct { 1317c478bd9Sstevel@tonic-gate char *subcmd; 1327c478bd9Sstevel@tonic-gate option_t option; 1337c478bd9Sstevel@tonic-gate error_t (*handler)(); 1347c478bd9Sstevel@tonic-gate } subcmd_defn_t; 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate #define BAM_MAXLINE 8192 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate #define LINE_INIT 0 /* lineNum initial value */ 1407c478bd9Sstevel@tonic-gate #define ENTRY_INIT -1 /* entryNum initial value */ 1417c478bd9Sstevel@tonic-gate #define ALL_ENTRIES -2 /* selects all boot entries */ 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate #define GRUB_DIR "/boot/grub" 1447c478bd9Sstevel@tonic-gate #define MULTI_BOOT "/platform/i86pc/multiboot" 1457c478bd9Sstevel@tonic-gate #define BOOT_ARCHIVE "/platform/i86pc/boot_archive" 1467c478bd9Sstevel@tonic-gate #define GRUB_MENU "/boot/grub/menu.lst" 1477c478bd9Sstevel@tonic-gate #define MENU_TMP "/boot/grub/menu.lst.tmp" 1487c478bd9Sstevel@tonic-gate #define RAMDISK_SPECIAL "/ramdisk" 14940541d5dSvikram #define STUBBOOT "/stubboot" 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* lock related */ 1527c478bd9Sstevel@tonic-gate #define BAM_LOCK_FILE "/var/run/bootadm.lock" 1537c478bd9Sstevel@tonic-gate #define LOCK_FILE_PERMS (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate #define CREATE_RAMDISK "/boot/solaris/bin/create_ramdisk" 1567c478bd9Sstevel@tonic-gate #define CREATE_DISKMAP "/boot/solaris/bin/create_diskmap" 1577c478bd9Sstevel@tonic-gate #define GRUBDISK_MAP "/var/run/solaris_grubdisk.map" 1587c478bd9Sstevel@tonic-gate 159b610f78eSvikram #define GRUB_slice "/etc/lu/GRUB_slice" 160b610f78eSvikram #define GRUB_root "/etc/lu/GRUB_root" 161b610f78eSvikram #define GRUB_backup_menu "/etc/lu/GRUB_backup_menu" 162b610f78eSvikram #define GRUB_slice_mntpt "/tmp/GRUB_slice_mntpt" 163b610f78eSvikram #define LU_ACTIVATE_FILE "/etc/lu/DelayUpdate/activate.sh" 164b610f78eSvikram 165b610f78eSvikram #define INSTALLGRUB "/sbin/installgrub" 166b610f78eSvikram #define STAGE1 "/boot/grub/stage1" 167b610f78eSvikram #define STAGE2 "/boot/grub/stage2" 168b610f78eSvikram 1697c478bd9Sstevel@tonic-gate /* 1707c478bd9Sstevel@tonic-gate * Default file attributes 1717c478bd9Sstevel@tonic-gate */ 1727c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_MODE 0644 /* default permissions */ 1737c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_UID 0 /* user root */ 1747c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_GID 3 /* group sys */ 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate /* 1777c478bd9Sstevel@tonic-gate * Menu related 1787c478bd9Sstevel@tonic-gate * menu_cmd_t and menu_cmds must be kept in sync 1797c478bd9Sstevel@tonic-gate */ 1807c478bd9Sstevel@tonic-gate typedef enum { 1817c478bd9Sstevel@tonic-gate DEFAULT_CMD = 0, 1827c478bd9Sstevel@tonic-gate TIMEOUT_CMD, 1837c478bd9Sstevel@tonic-gate TITLE_CMD, 1847c478bd9Sstevel@tonic-gate ROOT_CMD, 1857c478bd9Sstevel@tonic-gate KERNEL_CMD, 1867c478bd9Sstevel@tonic-gate MODULE_CMD, 1877c478bd9Sstevel@tonic-gate SEP_CMD, 1887c478bd9Sstevel@tonic-gate COMMENT_CMD 1897c478bd9Sstevel@tonic-gate } menu_cmd_t; 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate static char *menu_cmds[] = { 1927c478bd9Sstevel@tonic-gate "default", /* DEFAULT_CMD */ 1937c478bd9Sstevel@tonic-gate "timeout", /* TIMEOUT_CMD */ 1947c478bd9Sstevel@tonic-gate "title", /* TITLE_CMD */ 1957c478bd9Sstevel@tonic-gate "root", /* ROOT_CMD */ 1967c478bd9Sstevel@tonic-gate "kernel", /* KERNEL_CMD */ 1977c478bd9Sstevel@tonic-gate "module", /* MODULE_CMD */ 1987c478bd9Sstevel@tonic-gate " ", /* SEP_CMD */ 1997c478bd9Sstevel@tonic-gate "#", /* COMMENT_CMD */ 2007c478bd9Sstevel@tonic-gate NULL 2017c478bd9Sstevel@tonic-gate }; 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate #define OPT_ENTRY_NUM "entry" 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate /* 2067c478bd9Sstevel@tonic-gate * archive related 2077c478bd9Sstevel@tonic-gate */ 2087c478bd9Sstevel@tonic-gate typedef struct { 2097c478bd9Sstevel@tonic-gate line_t *head; 2107c478bd9Sstevel@tonic-gate line_t *tail; 2117c478bd9Sstevel@tonic-gate } filelist_t; 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate #define BOOT_FILE_LIST "boot/solaris/filelist.ramdisk" 2147c478bd9Sstevel@tonic-gate #define ETC_FILE_LIST "etc/boot/solaris/filelist.ramdisk" 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate #define FILE_STAT "boot/solaris/filestat.ramdisk" 2177c478bd9Sstevel@tonic-gate #define FILE_STAT_TMP "boot/solaris/filestat.ramdisk.tmp" 2187c478bd9Sstevel@tonic-gate #define DIR_PERMS (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) 2197c478bd9Sstevel@tonic-gate #define FILE_STAT_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate #define BAM_HDR "---------- ADDED BY BOOTADM - DO NOT EDIT ----------" 2227c478bd9Sstevel@tonic-gate #define BAM_FTR "---------------------END BOOTADM--------------------" 2238c1b6884Sszhou #define BAM_OLDDEF "BOOTADM SAVED DEFAULT: " 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate /* Globals */ 2267c478bd9Sstevel@tonic-gate static char *prog; 2277c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd; 2287c478bd9Sstevel@tonic-gate static char *bam_root; 2297c478bd9Sstevel@tonic-gate static int bam_rootlen; 2307c478bd9Sstevel@tonic-gate static int bam_root_readonly; 23140541d5dSvikram static int bam_alt_root; 2327c478bd9Sstevel@tonic-gate static char *bam_subcmd; 2337c478bd9Sstevel@tonic-gate static char *bam_opt; 2347c478bd9Sstevel@tonic-gate static int bam_debug; 2357c478bd9Sstevel@tonic-gate static char **bam_argv; 2367c478bd9Sstevel@tonic-gate static int bam_argc; 2377c478bd9Sstevel@tonic-gate static int bam_force; 2387c478bd9Sstevel@tonic-gate static int bam_verbose; 2397c478bd9Sstevel@tonic-gate static int bam_check; 2407c478bd9Sstevel@tonic-gate static int bam_smf_check; 2417c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1; 2427c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/"; 243b610f78eSvikram static int bam_update_all; 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate /* function prototypes */ 2467c478bd9Sstevel@tonic-gate static void parse_args_internal(int argc, char *argv[]); 2477c478bd9Sstevel@tonic-gate static void parse_args(int argc, char *argv[]); 2487c478bd9Sstevel@tonic-gate static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]); 2497c478bd9Sstevel@tonic-gate static error_t bam_archive(char *subcmd, char *opt); 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate static void bam_error(char *format, ...); 2527c478bd9Sstevel@tonic-gate static void bam_print(char *format, ...); 2537c478bd9Sstevel@tonic-gate static void bam_exit(int excode); 2547c478bd9Sstevel@tonic-gate static void bam_lock(void); 2557c478bd9Sstevel@tonic-gate static void bam_unlock(void); 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate static int exec_cmd(char *cmdline, char *output, int64_t osize); 2587c478bd9Sstevel@tonic-gate static error_t read_globals(menu_t *mp, char *menu_path, 2597c478bd9Sstevel@tonic-gate char *globalcmd, int quiet); 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate static menu_t *menu_read(char *menu_path); 2627c478bd9Sstevel@tonic-gate static error_t menu_write(char *root, menu_t *mp); 2637c478bd9Sstevel@tonic-gate static void linelist_free(line_t *start); 2647c478bd9Sstevel@tonic-gate static void menu_free(menu_t *mp); 2657c478bd9Sstevel@tonic-gate static void line_free(line_t *lp); 2667c478bd9Sstevel@tonic-gate static void filelist_free(filelist_t *flistp); 2677c478bd9Sstevel@tonic-gate static error_t list2file(char *root, char *tmp, 2687c478bd9Sstevel@tonic-gate char *final, line_t *start); 2697c478bd9Sstevel@tonic-gate static error_t list_entry(menu_t *mp, char *menu_path, char *opt); 2707c478bd9Sstevel@tonic-gate static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt); 2717c478bd9Sstevel@tonic-gate static error_t update_entry(menu_t *mp, char *root, char *opt); 2727c478bd9Sstevel@tonic-gate static error_t update_temp(menu_t *mp, char *root, char *opt); 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate static error_t update_archive(char *root, char *opt); 2757c478bd9Sstevel@tonic-gate static error_t list_archive(char *root, char *opt); 2767c478bd9Sstevel@tonic-gate static error_t update_all(char *root, char *opt); 2777c478bd9Sstevel@tonic-gate static error_t read_list(char *root, filelist_t *flistp); 2787c478bd9Sstevel@tonic-gate static error_t set_global(menu_t *mp, char *globalcmd, int val); 2797c478bd9Sstevel@tonic-gate static error_t set_option(menu_t *mp, char *globalcmd, char *opt); 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate static long s_strtol(char *str); 2827c478bd9Sstevel@tonic-gate static char *s_fgets(char *buf, int n, FILE *fp); 2837c478bd9Sstevel@tonic-gate static int s_fputs(char *str, FILE *fp); 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate static void *s_calloc(size_t nelem, size_t sz); 2867c478bd9Sstevel@tonic-gate static char *s_strdup(char *str); 2877c478bd9Sstevel@tonic-gate static int is_readonly(char *); 2887c478bd9Sstevel@tonic-gate static int is_amd64(void); 2897c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *); 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate #if defined(__sparc) 2927c478bd9Sstevel@tonic-gate static void sparc_abort(void); 2937c478bd9Sstevel@tonic-gate #endif 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate /* Menu related sub commands */ 2967c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = { 2977c478bd9Sstevel@tonic-gate "set_option", OPT_OPTIONAL, set_option, /* PUB */ 2987c478bd9Sstevel@tonic-gate "list_entry", OPT_OPTIONAL, list_entry, /* PUB */ 2997c478bd9Sstevel@tonic-gate "delete_all_entries", OPT_ABSENT, delete_all_entries, /* PVT */ 3007c478bd9Sstevel@tonic-gate "update_entry", OPT_REQ, update_entry, /* menu */ 3017c478bd9Sstevel@tonic-gate "update_temp", OPT_OPTIONAL, update_temp, /* reboot */ 3027c478bd9Sstevel@tonic-gate NULL, 0, NULL /* must be last */ 3037c478bd9Sstevel@tonic-gate }; 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate /* Archive related sub commands */ 3067c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = { 3077c478bd9Sstevel@tonic-gate "update", OPT_ABSENT, update_archive, /* PUB */ 3087c478bd9Sstevel@tonic-gate "update_all", OPT_ABSENT, update_all, /* PVT */ 3097c478bd9Sstevel@tonic-gate "list", OPT_OPTIONAL, list_archive, /* PUB */ 3107c478bd9Sstevel@tonic-gate NULL, 0, NULL /* must be last */ 3117c478bd9Sstevel@tonic-gate }; 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate static struct { 3147c478bd9Sstevel@tonic-gate nvlist_t *new_nvlp; 3157c478bd9Sstevel@tonic-gate nvlist_t *old_nvlp; 3167c478bd9Sstevel@tonic-gate int need_update; 3177c478bd9Sstevel@tonic-gate } walk_arg; 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate static void 3207c478bd9Sstevel@tonic-gate usage(void) 3217c478bd9Sstevel@tonic-gate { 3227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "USAGE:\n"); 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate /* archive usage */ 3267c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n", 3277c478bd9Sstevel@tonic-gate prog); 3287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog); 3297c478bd9Sstevel@tonic-gate #ifndef __sparc 3307c478bd9Sstevel@tonic-gate /* x86 only */ 3317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog); 3327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog); 3337c478bd9Sstevel@tonic-gate #endif 3347c478bd9Sstevel@tonic-gate } 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate int 3377c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 3387c478bd9Sstevel@tonic-gate { 3397c478bd9Sstevel@tonic-gate error_t ret; 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 3427c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate if ((prog = strrchr(argv[0], '/')) == NULL) { 3457c478bd9Sstevel@tonic-gate prog = argv[0]; 3467c478bd9Sstevel@tonic-gate } else { 3477c478bd9Sstevel@tonic-gate prog++; 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate if (geteuid() != 0) { 3517c478bd9Sstevel@tonic-gate bam_error(MUST_BE_ROOT); 3527c478bd9Sstevel@tonic-gate bam_exit(1); 3537c478bd9Sstevel@tonic-gate } 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate bam_lock(); 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate /* 3587c478bd9Sstevel@tonic-gate * Don't depend on caller's umask 3597c478bd9Sstevel@tonic-gate */ 3607c478bd9Sstevel@tonic-gate (void) umask(0022); 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate parse_args(argc, argv); 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate #if defined(__sparc) 3657c478bd9Sstevel@tonic-gate /* 3667c478bd9Sstevel@tonic-gate * There are only two valid invocations of bootadm 3677c478bd9Sstevel@tonic-gate * on SPARC: 3687c478bd9Sstevel@tonic-gate * 3697c478bd9Sstevel@tonic-gate * - SPARC diskless server creating boot_archive for i386 clients 3707c478bd9Sstevel@tonic-gate * - archive creation call during reboot of a SPARC system 3717c478bd9Sstevel@tonic-gate * 3727c478bd9Sstevel@tonic-gate * The latter should be a NOP 3737c478bd9Sstevel@tonic-gate */ 3747c478bd9Sstevel@tonic-gate if (bam_cmd != BAM_ARCHIVE) { 3757c478bd9Sstevel@tonic-gate sparc_abort(); 3767c478bd9Sstevel@tonic-gate } 3777c478bd9Sstevel@tonic-gate #endif 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate switch (bam_cmd) { 3807c478bd9Sstevel@tonic-gate case BAM_MENU: 3817c478bd9Sstevel@tonic-gate ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv); 3827c478bd9Sstevel@tonic-gate break; 3837c478bd9Sstevel@tonic-gate case BAM_ARCHIVE: 3847c478bd9Sstevel@tonic-gate ret = bam_archive(bam_subcmd, bam_opt); 3857c478bd9Sstevel@tonic-gate break; 3867c478bd9Sstevel@tonic-gate default: 3877c478bd9Sstevel@tonic-gate usage(); 3887c478bd9Sstevel@tonic-gate bam_exit(1); 3897c478bd9Sstevel@tonic-gate } 3907c478bd9Sstevel@tonic-gate 3917c478bd9Sstevel@tonic-gate if (ret != BAM_SUCCESS) 3927c478bd9Sstevel@tonic-gate bam_exit(1); 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate bam_unlock(); 3957c478bd9Sstevel@tonic-gate return (0); 3967c478bd9Sstevel@tonic-gate } 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate #if defined(__sparc) 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate static void 4017c478bd9Sstevel@tonic-gate sparc_abort(void) 4027c478bd9Sstevel@tonic-gate { 4037c478bd9Sstevel@tonic-gate bam_error(NOT_ON_SPARC); 4047c478bd9Sstevel@tonic-gate bam_exit(1); 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate #endif 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate /* 4107c478bd9Sstevel@tonic-gate * Equivalence of public and internal commands: 4117c478bd9Sstevel@tonic-gate * update-archive -- -a update 4127c478bd9Sstevel@tonic-gate * list-archive -- -a list 4137c478bd9Sstevel@tonic-gate * set-menu -- -m set_option 4147c478bd9Sstevel@tonic-gate * list-menu -- -m list_entry 4157c478bd9Sstevel@tonic-gate * update-menu -- -m update_entry 4167c478bd9Sstevel@tonic-gate */ 4177c478bd9Sstevel@tonic-gate static struct cmd_map { 4187c478bd9Sstevel@tonic-gate char *bam_cmdname; 4197c478bd9Sstevel@tonic-gate int bam_cmd; 4207c478bd9Sstevel@tonic-gate char *bam_subcmd; 4217c478bd9Sstevel@tonic-gate } cmd_map[] = { 4227c478bd9Sstevel@tonic-gate { "update-archive", BAM_ARCHIVE, "update"}, 4237c478bd9Sstevel@tonic-gate { "list-archive", BAM_ARCHIVE, "list"}, 4247c478bd9Sstevel@tonic-gate { "set-menu", BAM_MENU, "set_option"}, 4257c478bd9Sstevel@tonic-gate { "list-menu", BAM_MENU, "list_entry"}, 4267c478bd9Sstevel@tonic-gate { "update-menu", BAM_MENU, "update_entry"}, 4277c478bd9Sstevel@tonic-gate { NULL, 0, NULL} 4287c478bd9Sstevel@tonic-gate }; 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate /* 4317c478bd9Sstevel@tonic-gate * Commands syntax published in bootadm(1M) are parsed here 4327c478bd9Sstevel@tonic-gate */ 4337c478bd9Sstevel@tonic-gate static void 4347c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[]) 4357c478bd9Sstevel@tonic-gate { 4367c478bd9Sstevel@tonic-gate struct cmd_map *cmp = cmd_map; 4377c478bd9Sstevel@tonic-gate 4387c478bd9Sstevel@tonic-gate /* command conforming to the final spec */ 4397c478bd9Sstevel@tonic-gate if (argc > 1 && argv[1][0] != '-') { 4407c478bd9Sstevel@tonic-gate /* 4417c478bd9Sstevel@tonic-gate * Map commands to internal table. 4427c478bd9Sstevel@tonic-gate */ 4437c478bd9Sstevel@tonic-gate while (cmp->bam_cmdname) { 4447c478bd9Sstevel@tonic-gate if (strcmp(argv[1], cmp->bam_cmdname) == 0) { 4457c478bd9Sstevel@tonic-gate bam_cmd = cmp->bam_cmd; 4467c478bd9Sstevel@tonic-gate bam_subcmd = cmp->bam_subcmd; 4477c478bd9Sstevel@tonic-gate break; 4487c478bd9Sstevel@tonic-gate } 4497c478bd9Sstevel@tonic-gate cmp++; 4507c478bd9Sstevel@tonic-gate } 4517c478bd9Sstevel@tonic-gate if (cmp->bam_cmdname == NULL) { 4527c478bd9Sstevel@tonic-gate usage(); 4537c478bd9Sstevel@tonic-gate bam_exit(1); 4547c478bd9Sstevel@tonic-gate } 4557c478bd9Sstevel@tonic-gate argc--; 4567c478bd9Sstevel@tonic-gate argv++; 4577c478bd9Sstevel@tonic-gate } 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate parse_args_internal(argc, argv); 4607c478bd9Sstevel@tonic-gate } 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate /* 4637c478bd9Sstevel@tonic-gate * A combination of public and private commands are parsed here. 4647c478bd9Sstevel@tonic-gate * The internal syntax and the corresponding functionality are: 4657c478bd9Sstevel@tonic-gate * -a update -- update-archive 4667c478bd9Sstevel@tonic-gate * -a list -- list-archive 4677c478bd9Sstevel@tonic-gate * -a update-all -- (reboot to sync all mounted OS archive) 4687c478bd9Sstevel@tonic-gate * -m update_entry -- update-menu 4697c478bd9Sstevel@tonic-gate * -m list_entry -- list-menu 4707c478bd9Sstevel@tonic-gate * -m update_temp -- (reboot -- [boot-args]) 4717c478bd9Sstevel@tonic-gate * -m delete_all_entries -- (called from install) 4727c478bd9Sstevel@tonic-gate */ 4737c478bd9Sstevel@tonic-gate static void 4747c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[]) 4757c478bd9Sstevel@tonic-gate { 4767c478bd9Sstevel@tonic-gate int c, error; 4777c478bd9Sstevel@tonic-gate extern char *optarg; 4787c478bd9Sstevel@tonic-gate extern int optind, opterr; 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate /* Suppress error message from getopt */ 4817c478bd9Sstevel@tonic-gate opterr = 0; 4827c478bd9Sstevel@tonic-gate 4837c478bd9Sstevel@tonic-gate error = 0; 4848c1b6884Sszhou while ((c = getopt(argc, argv, "a:d:fm:no:vCR:")) != -1) { 4857c478bd9Sstevel@tonic-gate switch (c) { 4867c478bd9Sstevel@tonic-gate case 'a': 4877c478bd9Sstevel@tonic-gate if (bam_cmd) { 4887c478bd9Sstevel@tonic-gate error = 1; 4897c478bd9Sstevel@tonic-gate bam_error(MULT_CMDS, c); 4907c478bd9Sstevel@tonic-gate } 4917c478bd9Sstevel@tonic-gate bam_cmd = BAM_ARCHIVE; 4927c478bd9Sstevel@tonic-gate bam_subcmd = optarg; 4937c478bd9Sstevel@tonic-gate break; 4947c478bd9Sstevel@tonic-gate case 'd': 4957c478bd9Sstevel@tonic-gate if (bam_debug) { 4967c478bd9Sstevel@tonic-gate error = 1; 4977c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 4987c478bd9Sstevel@tonic-gate } 4997c478bd9Sstevel@tonic-gate bam_debug = s_strtol(optarg); 5007c478bd9Sstevel@tonic-gate break; 5017c478bd9Sstevel@tonic-gate case 'f': 5027c478bd9Sstevel@tonic-gate if (bam_force) { 5037c478bd9Sstevel@tonic-gate error = 1; 5047c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5057c478bd9Sstevel@tonic-gate } 5067c478bd9Sstevel@tonic-gate bam_force = 1; 5077c478bd9Sstevel@tonic-gate break; 5087c478bd9Sstevel@tonic-gate case 'm': 5097c478bd9Sstevel@tonic-gate if (bam_cmd) { 5107c478bd9Sstevel@tonic-gate error = 1; 5117c478bd9Sstevel@tonic-gate bam_error(MULT_CMDS, c); 5127c478bd9Sstevel@tonic-gate } 5137c478bd9Sstevel@tonic-gate bam_cmd = BAM_MENU; 5147c478bd9Sstevel@tonic-gate bam_subcmd = optarg; 5157c478bd9Sstevel@tonic-gate break; 5167c478bd9Sstevel@tonic-gate case 'n': 5177c478bd9Sstevel@tonic-gate if (bam_check) { 5187c478bd9Sstevel@tonic-gate error = 1; 5197c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate bam_check = 1; 5227c478bd9Sstevel@tonic-gate break; 5237c478bd9Sstevel@tonic-gate case 'o': 5247c478bd9Sstevel@tonic-gate if (bam_opt) { 5257c478bd9Sstevel@tonic-gate error = 1; 5267c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5277c478bd9Sstevel@tonic-gate } 5287c478bd9Sstevel@tonic-gate bam_opt = optarg; 5297c478bd9Sstevel@tonic-gate break; 5307c478bd9Sstevel@tonic-gate case 'v': 5317c478bd9Sstevel@tonic-gate if (bam_verbose) { 5327c478bd9Sstevel@tonic-gate error = 1; 5337c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5347c478bd9Sstevel@tonic-gate } 5357c478bd9Sstevel@tonic-gate bam_verbose = 1; 5367c478bd9Sstevel@tonic-gate break; 5378c1b6884Sszhou case 'C': 5388c1b6884Sszhou bam_smf_check = 1; 5398c1b6884Sszhou break; 5407c478bd9Sstevel@tonic-gate case 'R': 5417c478bd9Sstevel@tonic-gate if (bam_root) { 5427c478bd9Sstevel@tonic-gate error = 1; 5437c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5447c478bd9Sstevel@tonic-gate break; 5457c478bd9Sstevel@tonic-gate } else if (realpath(optarg, rootbuf) == NULL) { 5467c478bd9Sstevel@tonic-gate error = 1; 5477c478bd9Sstevel@tonic-gate bam_error(CANT_RESOLVE, optarg, 5487c478bd9Sstevel@tonic-gate strerror(errno)); 5497c478bd9Sstevel@tonic-gate break; 5507c478bd9Sstevel@tonic-gate } 55140541d5dSvikram bam_alt_root = 1; 5527c478bd9Sstevel@tonic-gate bam_root = rootbuf; 5537c478bd9Sstevel@tonic-gate bam_rootlen = strlen(rootbuf); 5547c478bd9Sstevel@tonic-gate break; 5557c478bd9Sstevel@tonic-gate case '?': 5567c478bd9Sstevel@tonic-gate error = 1; 5577c478bd9Sstevel@tonic-gate bam_error(BAD_OPT, optopt); 5587c478bd9Sstevel@tonic-gate break; 5597c478bd9Sstevel@tonic-gate default : 5607c478bd9Sstevel@tonic-gate error = 1; 5617c478bd9Sstevel@tonic-gate bam_error(BAD_OPT, c); 5627c478bd9Sstevel@tonic-gate break; 5637c478bd9Sstevel@tonic-gate } 5647c478bd9Sstevel@tonic-gate } 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate /* 5677c478bd9Sstevel@tonic-gate * A command option must be specfied 5687c478bd9Sstevel@tonic-gate */ 5697c478bd9Sstevel@tonic-gate if (!bam_cmd) { 5707c478bd9Sstevel@tonic-gate if (bam_opt && strcmp(bam_opt, "all") == 0) { 5717c478bd9Sstevel@tonic-gate usage(); 5727c478bd9Sstevel@tonic-gate bam_exit(0); 5737c478bd9Sstevel@tonic-gate } 5747c478bd9Sstevel@tonic-gate bam_error(NEED_CMD); 5757c478bd9Sstevel@tonic-gate error = 1; 5767c478bd9Sstevel@tonic-gate } 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate if (error) { 5797c478bd9Sstevel@tonic-gate usage(); 5807c478bd9Sstevel@tonic-gate bam_exit(1); 5817c478bd9Sstevel@tonic-gate } 5827c478bd9Sstevel@tonic-gate 5837c478bd9Sstevel@tonic-gate if (optind > argc) { 5847c478bd9Sstevel@tonic-gate bam_error(INT_ERROR, "parse_args"); 5857c478bd9Sstevel@tonic-gate bam_exit(1); 5867c478bd9Sstevel@tonic-gate } else if (optind < argc) { 5877c478bd9Sstevel@tonic-gate bam_argv = &argv[optind]; 5887c478bd9Sstevel@tonic-gate bam_argc = argc - optind; 5897c478bd9Sstevel@tonic-gate } 5907c478bd9Sstevel@tonic-gate 5917c478bd9Sstevel@tonic-gate /* 5927c478bd9Sstevel@tonic-gate * -n implies verbose mode 5937c478bd9Sstevel@tonic-gate */ 5947c478bd9Sstevel@tonic-gate if (bam_check) 5957c478bd9Sstevel@tonic-gate bam_verbose = 1; 5967c478bd9Sstevel@tonic-gate } 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate static error_t 5997c478bd9Sstevel@tonic-gate check_subcmd_and_options( 6007c478bd9Sstevel@tonic-gate char *subcmd, 6017c478bd9Sstevel@tonic-gate char *opt, 6027c478bd9Sstevel@tonic-gate subcmd_defn_t *table, 6037c478bd9Sstevel@tonic-gate error_t (**fp)()) 6047c478bd9Sstevel@tonic-gate { 6057c478bd9Sstevel@tonic-gate int i; 6067c478bd9Sstevel@tonic-gate 6077c478bd9Sstevel@tonic-gate if (subcmd == NULL) { 6087c478bd9Sstevel@tonic-gate bam_error(NEED_SUBCMD); 6097c478bd9Sstevel@tonic-gate return (BAM_ERROR); 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate if (bam_root == NULL) { 6137c478bd9Sstevel@tonic-gate bam_root = rootbuf; 6147c478bd9Sstevel@tonic-gate bam_rootlen = 1; 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate /* verify that subcmd is valid */ 6187c478bd9Sstevel@tonic-gate for (i = 0; table[i].subcmd != NULL; i++) { 6197c478bd9Sstevel@tonic-gate if (strcmp(table[i].subcmd, subcmd) == 0) 6207c478bd9Sstevel@tonic-gate break; 6217c478bd9Sstevel@tonic-gate } 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate if (table[i].subcmd == NULL) { 6247c478bd9Sstevel@tonic-gate bam_error(INVALID_SUBCMD, subcmd); 6257c478bd9Sstevel@tonic-gate return (BAM_ERROR); 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate /* subcmd verifies that opt is appropriate */ 6297c478bd9Sstevel@tonic-gate if (table[i].option != OPT_OPTIONAL) { 6307c478bd9Sstevel@tonic-gate if ((table[i].option == OPT_REQ) ^ (opt != NULL)) { 6317c478bd9Sstevel@tonic-gate if (opt) 6327c478bd9Sstevel@tonic-gate bam_error(NO_OPT_REQ, subcmd); 6337c478bd9Sstevel@tonic-gate else 6347c478bd9Sstevel@tonic-gate bam_error(MISS_OPT, subcmd); 6357c478bd9Sstevel@tonic-gate return (BAM_ERROR); 6367c478bd9Sstevel@tonic-gate } 6377c478bd9Sstevel@tonic-gate } 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate *fp = table[i].handler; 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 6427c478bd9Sstevel@tonic-gate } 6437c478bd9Sstevel@tonic-gate 644b610f78eSvikram 645b610f78eSvikram static char * 64640541d5dSvikram mount_grub_slice(int *mnted, char **physlice, char **logslice, char **fs_type) 647b610f78eSvikram { 648b610f78eSvikram struct extmnttab mnt; 649b610f78eSvikram struct stat sb; 650b610f78eSvikram char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32]; 651f0f2c544Svikram char cmd[PATH_MAX]; 652b610f78eSvikram char *mntpt; 653b610f78eSvikram int p, l, f; 654b610f78eSvikram FILE *fp; 655b610f78eSvikram 656b610f78eSvikram assert(mnted); 657b610f78eSvikram *mnted = 0; 658b610f78eSvikram 659b610f78eSvikram /* 66040541d5dSvikram * physlice, logslice, fs_type args may be NULL 661b610f78eSvikram */ 662b610f78eSvikram if (physlice) 663b610f78eSvikram *physlice = NULL; 66440541d5dSvikram if (logslice) 66540541d5dSvikram *logslice = NULL; 66640541d5dSvikram if (fs_type) 66740541d5dSvikram *fs_type = NULL; 668b610f78eSvikram 669b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 670b610f78eSvikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 671b610f78eSvikram return (NULL); 672b610f78eSvikram } 673b610f78eSvikram 674b610f78eSvikram fp = fopen(GRUB_slice, "r"); 675b610f78eSvikram if (fp == NULL) { 676b610f78eSvikram bam_error(OPEN_FAIL, GRUB_slice, strerror(errno)); 677b610f78eSvikram return (NULL); 678b610f78eSvikram } 679b610f78eSvikram 680b610f78eSvikram dev[0] = fstype[0] = phys[0] = '\0'; 681b610f78eSvikram p = sizeof ("PHYS_SLICE=") - 1; 682b610f78eSvikram l = sizeof ("LOG_SLICE=") - 1; 683b610f78eSvikram f = sizeof ("LOG_FSTYP=") - 1; 684b610f78eSvikram while (s_fgets(buf, sizeof (buf), fp) != NULL) { 685b610f78eSvikram if (strncmp(buf, "PHYS_SLICE=", p) == 0) { 686b610f78eSvikram (void) strlcpy(phys, buf + p, sizeof (phys)); 687b610f78eSvikram continue; 688b610f78eSvikram } 689b610f78eSvikram if (strncmp(buf, "LOG_SLICE=", l) == 0) { 690b610f78eSvikram (void) strlcpy(dev, buf + l, sizeof (dev)); 691b610f78eSvikram continue; 692b610f78eSvikram } 693b610f78eSvikram if (strncmp(buf, "LOG_FSTYP=", f) == 0) { 694b610f78eSvikram (void) strlcpy(fstype, buf + f, sizeof (fstype)); 695b610f78eSvikram continue; 696b610f78eSvikram } 697b610f78eSvikram } 698b610f78eSvikram (void) fclose(fp); 699b610f78eSvikram 700b610f78eSvikram if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') { 701b610f78eSvikram bam_error(BAD_SLICE_FILE, GRUB_slice); 702b610f78eSvikram return (NULL); 703b610f78eSvikram } 704b610f78eSvikram 705b610f78eSvikram if (physlice) { 706b610f78eSvikram *physlice = s_strdup(phys); 707b610f78eSvikram } 70840541d5dSvikram if (logslice) { 70940541d5dSvikram *logslice = s_strdup(dev); 71040541d5dSvikram } 71140541d5dSvikram if (fs_type) { 71240541d5dSvikram *fs_type = s_strdup(fstype); 71340541d5dSvikram } 714b610f78eSvikram 715b610f78eSvikram /* 716b610f78eSvikram * Check if the slice is already mounted 717b610f78eSvikram */ 718b610f78eSvikram fp = fopen(MNTTAB, "r"); 719b610f78eSvikram if (fp == NULL) { 720b610f78eSvikram bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 72140541d5dSvikram goto error; 722b610f78eSvikram } 723b610f78eSvikram 724b610f78eSvikram resetmnttab(fp); 725b610f78eSvikram 726b610f78eSvikram mntpt = NULL; 727b610f78eSvikram while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 728b610f78eSvikram if (strcmp(mnt.mnt_special, dev) == 0) { 729b610f78eSvikram mntpt = s_strdup(mnt.mnt_mountp); 730b610f78eSvikram break; 731b610f78eSvikram } 732b610f78eSvikram } 733b610f78eSvikram 734b610f78eSvikram (void) fclose(fp); 735b610f78eSvikram 736b610f78eSvikram if (mntpt) { 737b610f78eSvikram return (mntpt); 738b610f78eSvikram } 739b610f78eSvikram 740b610f78eSvikram 741b610f78eSvikram /* 742b610f78eSvikram * GRUB slice is not mounted, we need to mount it now. 743b610f78eSvikram * First create the mountpoint 744b610f78eSvikram */ 745b610f78eSvikram mntpt = s_calloc(1, PATH_MAX); 746b610f78eSvikram (void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid()); 747b610f78eSvikram if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) { 748b610f78eSvikram bam_error(MKDIR_FAILED, mntpt, strerror(errno)); 749b610f78eSvikram free(mntpt); 75040541d5dSvikram goto error; 751b610f78eSvikram } 752b610f78eSvikram 753f0f2c544Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s", 754f0f2c544Svikram fstype, dev, mntpt); 755f0f2c544Svikram 756f0f2c544Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 75740541d5dSvikram bam_error(MOUNT_FAILED, dev, fstype); 758b610f78eSvikram if (rmdir(mntpt) != 0) { 759b610f78eSvikram bam_error(RMDIR_FAILED, mntpt, strerror(errno)); 760b610f78eSvikram } 761b610f78eSvikram free(mntpt); 76240541d5dSvikram goto error; 763b610f78eSvikram } 764b610f78eSvikram 765b610f78eSvikram *mnted = 1; 766b610f78eSvikram return (mntpt); 76740541d5dSvikram 76840541d5dSvikram error: 76940541d5dSvikram if (physlice) { 77040541d5dSvikram free(*physlice); 77140541d5dSvikram *physlice = NULL; 77240541d5dSvikram } 77340541d5dSvikram if (logslice) { 77440541d5dSvikram free(*logslice); 77540541d5dSvikram *logslice = NULL; 77640541d5dSvikram } 77740541d5dSvikram if (fs_type) { 77840541d5dSvikram free(*fs_type); 77940541d5dSvikram *fs_type = NULL; 78040541d5dSvikram } 78140541d5dSvikram return (NULL); 782b610f78eSvikram } 783b610f78eSvikram 784b610f78eSvikram static void 78540541d5dSvikram umount_grub_slice( 78640541d5dSvikram int mnted, 78740541d5dSvikram char *mntpt, 78840541d5dSvikram char *physlice, 78940541d5dSvikram char *logslice, 79040541d5dSvikram char *fs_type) 791b610f78eSvikram { 792f0f2c544Svikram char cmd[PATH_MAX]; 793f0f2c544Svikram 794b610f78eSvikram /* 795b610f78eSvikram * If we have not dealt with GRUB slice 796b610f78eSvikram * we have nothing to do - just return. 797b610f78eSvikram */ 798b610f78eSvikram if (mntpt == NULL) 799b610f78eSvikram return; 800b610f78eSvikram 801b610f78eSvikram 802b610f78eSvikram /* 803b610f78eSvikram * If we mounted the filesystem earlier in mount_grub_slice() 804b610f78eSvikram * unmount it now. 805b610f78eSvikram */ 806b610f78eSvikram if (mnted) { 807f0f2c544Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/umount %s", 808f0f2c544Svikram mntpt); 809f0f2c544Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 810f0f2c544Svikram bam_error(UMOUNT_FAILED, mntpt); 811b610f78eSvikram } 812b610f78eSvikram if (rmdir(mntpt) != 0) { 813b610f78eSvikram bam_error(RMDIR_FAILED, mntpt, strerror(errno)); 814b610f78eSvikram } 815b610f78eSvikram } 81640541d5dSvikram 817b610f78eSvikram if (physlice) 818b610f78eSvikram free(physlice); 81940541d5dSvikram if (logslice) 82040541d5dSvikram free(logslice); 82140541d5dSvikram if (fs_type) 82240541d5dSvikram free(fs_type); 82340541d5dSvikram 824b610f78eSvikram free(mntpt); 825b610f78eSvikram } 826b610f78eSvikram 82740541d5dSvikram static char * 82840541d5dSvikram use_stubboot(void) 82940541d5dSvikram { 83040541d5dSvikram int mnted; 83140541d5dSvikram struct stat sb; 83240541d5dSvikram struct extmnttab mnt; 83340541d5dSvikram FILE *fp; 83440541d5dSvikram char cmd[PATH_MAX]; 83540541d5dSvikram 83640541d5dSvikram if (stat(STUBBOOT, &sb) != 0) { 83740541d5dSvikram bam_error(STUBBOOT_DIR_NOT_FOUND); 83840541d5dSvikram return (NULL); 83940541d5dSvikram } 84040541d5dSvikram 84140541d5dSvikram /* 84240541d5dSvikram * Check if stubboot is mounted. If not, mount it 84340541d5dSvikram */ 84440541d5dSvikram fp = fopen(MNTTAB, "r"); 84540541d5dSvikram if (fp == NULL) { 84640541d5dSvikram bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 84740541d5dSvikram return (NULL); 84840541d5dSvikram } 84940541d5dSvikram 85040541d5dSvikram resetmnttab(fp); 85140541d5dSvikram 85240541d5dSvikram mnted = 0; 85340541d5dSvikram while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 85440541d5dSvikram if (strcmp(mnt.mnt_mountp, STUBBOOT) == 0) { 85540541d5dSvikram mnted = 1; 85640541d5dSvikram break; 85740541d5dSvikram } 85840541d5dSvikram } 85940541d5dSvikram 86040541d5dSvikram (void) fclose(fp); 86140541d5dSvikram 86240541d5dSvikram if (mnted) 86340541d5dSvikram return (STUBBOOT); 86440541d5dSvikram 86540541d5dSvikram /* 86640541d5dSvikram * Stubboot is not mounted, mount it now. 86740541d5dSvikram * It should exist in /etc/vfstab 86840541d5dSvikram */ 86940541d5dSvikram (void) snprintf(cmd, sizeof (cmd), "/sbin/mount %s", 87040541d5dSvikram STUBBOOT); 87140541d5dSvikram if (exec_cmd(cmd, NULL, 0) != 0) { 87240541d5dSvikram bam_error(MOUNT_MNTPT_FAILED, STUBBOOT); 87340541d5dSvikram return (NULL); 87440541d5dSvikram } 87540541d5dSvikram 87640541d5dSvikram return (STUBBOOT); 87740541d5dSvikram } 87840541d5dSvikram 87940541d5dSvikram static void 88040541d5dSvikram disp_active_menu_locn(char *menu_path, char *logslice, char *fstype, int mnted) 88140541d5dSvikram { 88240541d5dSvikram /* 88340541d5dSvikram * Check if we did a temp mount of an unmounted device. 88440541d5dSvikram * If yes, print the block device and fstype for that device 88540541d5dSvikram * else it is already mounted, so we print the path to the GRUB menu. 88640541d5dSvikram */ 88740541d5dSvikram if (mnted) { 88840541d5dSvikram bam_print(GRUB_MENU_DEVICE, logslice); 88940541d5dSvikram bam_print(GRUB_MENU_FSTYPE, fstype); 89040541d5dSvikram } else { 89140541d5dSvikram bam_print(GRUB_MENU_PATH, menu_path); 89240541d5dSvikram } 89340541d5dSvikram } 89440541d5dSvikram 89540541d5dSvikram /* 89640541d5dSvikram * NOTE: A single "/" is also considered a trailing slash and will 89740541d5dSvikram * be deleted. 89840541d5dSvikram */ 89940541d5dSvikram static void 90040541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize) 90140541d5dSvikram { 90240541d5dSvikram size_t dstlen; 90340541d5dSvikram 90440541d5dSvikram assert(src); 90540541d5dSvikram assert(dst); 90640541d5dSvikram 90740541d5dSvikram (void) strlcpy(dst, src, dstsize); 90840541d5dSvikram 90940541d5dSvikram dstlen = strlen(dst); 91040541d5dSvikram if (dst[dstlen - 1] == '/') { 91140541d5dSvikram dst[dstlen - 1] = '\0'; 91240541d5dSvikram } 91340541d5dSvikram } 91440541d5dSvikram 9157c478bd9Sstevel@tonic-gate static error_t 9167c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[]) 9177c478bd9Sstevel@tonic-gate { 9187c478bd9Sstevel@tonic-gate error_t ret; 9197c478bd9Sstevel@tonic-gate char menu_path[PATH_MAX]; 9207c478bd9Sstevel@tonic-gate menu_t *menu; 92140541d5dSvikram char *mntpt, *menu_root, *logslice, *fstype; 922b610f78eSvikram struct stat sb; 923b610f78eSvikram int mnted; /* set if we did a mount */ 9247c478bd9Sstevel@tonic-gate error_t (*f)(menu_t *mp, char *menu_path, char *opt); 9257c478bd9Sstevel@tonic-gate 9267c478bd9Sstevel@tonic-gate /* 9277c478bd9Sstevel@tonic-gate * Check arguments 9287c478bd9Sstevel@tonic-gate */ 9297c478bd9Sstevel@tonic-gate ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f); 9307c478bd9Sstevel@tonic-gate if (ret == BAM_ERROR) { 9317c478bd9Sstevel@tonic-gate return (BAM_ERROR); 9327c478bd9Sstevel@tonic-gate } 9337c478bd9Sstevel@tonic-gate 934b610f78eSvikram mntpt = NULL; 935b610f78eSvikram mnted = 0; 93640541d5dSvikram logslice = fstype = NULL; 93740541d5dSvikram 93840541d5dSvikram /* 93940541d5dSvikram * If the user provides an alternate root, we 94040541d5dSvikram * assume they know what they are doing and we 94140541d5dSvikram * use it. Else we check if there is an 94240541d5dSvikram * alternate location (other than /boot/grub) 94340541d5dSvikram * for the GRUB menu 94440541d5dSvikram */ 94540541d5dSvikram if (bam_alt_root) { 94640541d5dSvikram menu_root = bam_root; 94740541d5dSvikram } else if (stat(GRUB_slice, &sb) == 0) { 94840541d5dSvikram mntpt = mount_grub_slice(&mnted, NULL, &logslice, &fstype); 949b610f78eSvikram menu_root = mntpt; 95040541d5dSvikram } else if (stat(STUBBOOT, &sb) == 0) { 95140541d5dSvikram menu_root = use_stubboot(); 952b610f78eSvikram } else { 953b610f78eSvikram menu_root = bam_root; 954b610f78eSvikram } 955b610f78eSvikram 95640541d5dSvikram if (menu_root == NULL) { 95740541d5dSvikram bam_error(CANNOT_LOCATE_GRUB_MENU); 95840541d5dSvikram return (BAM_ERROR); 95940541d5dSvikram } 96040541d5dSvikram 96140541d5dSvikram elide_trailing_slash(menu_root, menu_path, sizeof (menu_path)); 96240541d5dSvikram (void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path)); 96340541d5dSvikram 96440541d5dSvikram /* 96540541d5dSvikram * If listing the menu, display the active menu 96640541d5dSvikram * location 96740541d5dSvikram */ 96840541d5dSvikram if (strcmp(subcmd, "list_entry") == 0) { 96940541d5dSvikram disp_active_menu_locn(menu_path, logslice, fstype, mnted); 97040541d5dSvikram } 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate menu = menu_read(menu_path); 9737c478bd9Sstevel@tonic-gate assert(menu); 9747c478bd9Sstevel@tonic-gate 9757c478bd9Sstevel@tonic-gate /* 9767c478bd9Sstevel@tonic-gate * Special handling for setting timeout and default 9777c478bd9Sstevel@tonic-gate */ 9787c478bd9Sstevel@tonic-gate if (strcmp(subcmd, "set_option") == 0) { 9797c478bd9Sstevel@tonic-gate if (largc != 1 || largv[0] == NULL) { 9807c478bd9Sstevel@tonic-gate usage(); 98140541d5dSvikram menu_free(menu); 98240541d5dSvikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 9837c478bd9Sstevel@tonic-gate return (BAM_ERROR); 9847c478bd9Sstevel@tonic-gate } 9857c478bd9Sstevel@tonic-gate opt = largv[0]; 9867c478bd9Sstevel@tonic-gate } else if (largc != 0) { 9877c478bd9Sstevel@tonic-gate usage(); 98840541d5dSvikram menu_free(menu); 98940541d5dSvikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 9907c478bd9Sstevel@tonic-gate return (BAM_ERROR); 9917c478bd9Sstevel@tonic-gate } 9927c478bd9Sstevel@tonic-gate 9937c478bd9Sstevel@tonic-gate /* 9947c478bd9Sstevel@tonic-gate * Once the sub-cmd handler has run 9957c478bd9Sstevel@tonic-gate * only the line field is guaranteed to have valid values 9967c478bd9Sstevel@tonic-gate */ 9977c478bd9Sstevel@tonic-gate if (strcmp(subcmd, "update_entry") == 0) 9987c478bd9Sstevel@tonic-gate ret = f(menu, bam_root, opt); 9997c478bd9Sstevel@tonic-gate else 10007c478bd9Sstevel@tonic-gate ret = f(menu, menu_path, opt); 10017c478bd9Sstevel@tonic-gate if (ret == BAM_WRITE) { 1002b610f78eSvikram ret = menu_write(menu_root, menu); 10037c478bd9Sstevel@tonic-gate } 10047c478bd9Sstevel@tonic-gate 10057c478bd9Sstevel@tonic-gate menu_free(menu); 10067c478bd9Sstevel@tonic-gate 100740541d5dSvikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 1008b610f78eSvikram 10097c478bd9Sstevel@tonic-gate return (ret); 10107c478bd9Sstevel@tonic-gate } 10117c478bd9Sstevel@tonic-gate 10127c478bd9Sstevel@tonic-gate 10137c478bd9Sstevel@tonic-gate static error_t 10147c478bd9Sstevel@tonic-gate bam_archive( 10157c478bd9Sstevel@tonic-gate char *subcmd, 10167c478bd9Sstevel@tonic-gate char *opt) 10177c478bd9Sstevel@tonic-gate { 10187c478bd9Sstevel@tonic-gate error_t ret; 10197c478bd9Sstevel@tonic-gate error_t (*f)(char *root, char *opt); 10207c478bd9Sstevel@tonic-gate 10217c478bd9Sstevel@tonic-gate /* 10228c1b6884Sszhou * Add trailing / for archive subcommands 10238c1b6884Sszhou */ 10248c1b6884Sszhou if (rootbuf[strlen(rootbuf) - 1] != '/') 10258c1b6884Sszhou (void) strcat(rootbuf, "/"); 10268c1b6884Sszhou bam_rootlen = strlen(rootbuf); 10278c1b6884Sszhou 10288c1b6884Sszhou /* 10297c478bd9Sstevel@tonic-gate * Check arguments 10307c478bd9Sstevel@tonic-gate */ 10317c478bd9Sstevel@tonic-gate ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f); 10327c478bd9Sstevel@tonic-gate if (ret != BAM_SUCCESS) { 10337c478bd9Sstevel@tonic-gate return (BAM_ERROR); 10347c478bd9Sstevel@tonic-gate } 10357c478bd9Sstevel@tonic-gate 10367c478bd9Sstevel@tonic-gate #if defined(__sparc) 10377c478bd9Sstevel@tonic-gate /* 10387c478bd9Sstevel@tonic-gate * A NOP if called on SPARC during reboot 10397c478bd9Sstevel@tonic-gate */ 10407c478bd9Sstevel@tonic-gate if (strcmp(subcmd, "update_all") == 0) 10417c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 10427c478bd9Sstevel@tonic-gate else if (strcmp(subcmd, "update") != 0) 10437c478bd9Sstevel@tonic-gate sparc_abort(); 10447c478bd9Sstevel@tonic-gate #endif 10457c478bd9Sstevel@tonic-gate 10467c478bd9Sstevel@tonic-gate /* 10477c478bd9Sstevel@tonic-gate * Check archive not supported with update_all 10487c478bd9Sstevel@tonic-gate * since it is awkward to display out-of-sync 10497c478bd9Sstevel@tonic-gate * information for each BE. 10507c478bd9Sstevel@tonic-gate */ 10517c478bd9Sstevel@tonic-gate if (bam_check && strcmp(subcmd, "update_all") == 0) { 10527c478bd9Sstevel@tonic-gate bam_error(CHECK_NOT_SUPPORTED, subcmd); 10537c478bd9Sstevel@tonic-gate return (BAM_ERROR); 10547c478bd9Sstevel@tonic-gate } 10557c478bd9Sstevel@tonic-gate 1056b610f78eSvikram if (strcmp(subcmd, "update_all") == 0) 1057b610f78eSvikram bam_update_all = 1; 1058b610f78eSvikram 1059b610f78eSvikram ret = f(bam_root, opt); 1060b610f78eSvikram 1061b610f78eSvikram bam_update_all = 0; 1062b610f78eSvikram 1063b610f78eSvikram return (ret); 10647c478bd9Sstevel@tonic-gate } 10657c478bd9Sstevel@tonic-gate 10667c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 10677c478bd9Sstevel@tonic-gate static void 10687c478bd9Sstevel@tonic-gate bam_error(char *format, ...) 10697c478bd9Sstevel@tonic-gate { 10707c478bd9Sstevel@tonic-gate va_list ap; 10717c478bd9Sstevel@tonic-gate 10727c478bd9Sstevel@tonic-gate va_start(ap, format); 10737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", prog); 10747c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, ap); 10757c478bd9Sstevel@tonic-gate va_end(ap); 10767c478bd9Sstevel@tonic-gate } 10777c478bd9Sstevel@tonic-gate 10787c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 10797c478bd9Sstevel@tonic-gate static void 10807c478bd9Sstevel@tonic-gate bam_print(char *format, ...) 10817c478bd9Sstevel@tonic-gate { 10827c478bd9Sstevel@tonic-gate va_list ap; 10837c478bd9Sstevel@tonic-gate 10847c478bd9Sstevel@tonic-gate va_start(ap, format); 10857c478bd9Sstevel@tonic-gate (void) vfprintf(stdout, format, ap); 10867c478bd9Sstevel@tonic-gate va_end(ap); 10877c478bd9Sstevel@tonic-gate } 10887c478bd9Sstevel@tonic-gate 10897c478bd9Sstevel@tonic-gate static void 10907c478bd9Sstevel@tonic-gate bam_exit(int excode) 10917c478bd9Sstevel@tonic-gate { 10927c478bd9Sstevel@tonic-gate bam_unlock(); 10937c478bd9Sstevel@tonic-gate exit(excode); 10947c478bd9Sstevel@tonic-gate } 10957c478bd9Sstevel@tonic-gate 10967c478bd9Sstevel@tonic-gate static void 10977c478bd9Sstevel@tonic-gate bam_lock(void) 10987c478bd9Sstevel@tonic-gate { 10997c478bd9Sstevel@tonic-gate struct flock lock; 11007c478bd9Sstevel@tonic-gate pid_t pid; 11017c478bd9Sstevel@tonic-gate 11027c478bd9Sstevel@tonic-gate bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS); 11037c478bd9Sstevel@tonic-gate if (bam_lock_fd < 0) { 11047c478bd9Sstevel@tonic-gate /* 11057c478bd9Sstevel@tonic-gate * We may be invoked early in boot for archive verification. 11067c478bd9Sstevel@tonic-gate * In this case, root is readonly and /var/run may not exist. 11077c478bd9Sstevel@tonic-gate * Proceed without the lock 11087c478bd9Sstevel@tonic-gate */ 11097c478bd9Sstevel@tonic-gate if (errno == EROFS || errno == ENOENT) { 11107c478bd9Sstevel@tonic-gate bam_root_readonly = 1; 11117c478bd9Sstevel@tonic-gate return; 11127c478bd9Sstevel@tonic-gate } 11137c478bd9Sstevel@tonic-gate 11147c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno)); 11157c478bd9Sstevel@tonic-gate bam_exit(1); 11167c478bd9Sstevel@tonic-gate } 11177c478bd9Sstevel@tonic-gate 11187c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 11197c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 11207c478bd9Sstevel@tonic-gate lock.l_start = 0; 11217c478bd9Sstevel@tonic-gate lock.l_len = 0; 11227c478bd9Sstevel@tonic-gate 11237c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) { 11247c478bd9Sstevel@tonic-gate if (errno != EACCES && errno != EAGAIN) { 11257c478bd9Sstevel@tonic-gate bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11267c478bd9Sstevel@tonic-gate (void) close(bam_lock_fd); 11277c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 11287c478bd9Sstevel@tonic-gate bam_exit(1); 11297c478bd9Sstevel@tonic-gate } 11307c478bd9Sstevel@tonic-gate pid = 0; 11317c478bd9Sstevel@tonic-gate (void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0); 11327c478bd9Sstevel@tonic-gate bam_print(FILE_LOCKED, pid); 11337c478bd9Sstevel@tonic-gate 11347c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 11357c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 11367c478bd9Sstevel@tonic-gate lock.l_start = 0; 11377c478bd9Sstevel@tonic-gate lock.l_len = 0; 11387c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) { 11397c478bd9Sstevel@tonic-gate bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11407c478bd9Sstevel@tonic-gate (void) close(bam_lock_fd); 11417c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 11427c478bd9Sstevel@tonic-gate bam_exit(1); 11437c478bd9Sstevel@tonic-gate } 11447c478bd9Sstevel@tonic-gate } 11457c478bd9Sstevel@tonic-gate 11467c478bd9Sstevel@tonic-gate /* We own the lock now */ 11477c478bd9Sstevel@tonic-gate pid = getpid(); 11487c478bd9Sstevel@tonic-gate (void) write(bam_lock_fd, &pid, sizeof (pid)); 11497c478bd9Sstevel@tonic-gate } 11507c478bd9Sstevel@tonic-gate 11517c478bd9Sstevel@tonic-gate static void 11527c478bd9Sstevel@tonic-gate bam_unlock(void) 11537c478bd9Sstevel@tonic-gate { 11547c478bd9Sstevel@tonic-gate struct flock unlock; 11557c478bd9Sstevel@tonic-gate 11567c478bd9Sstevel@tonic-gate /* 11577c478bd9Sstevel@tonic-gate * NOP if we don't hold the lock 11587c478bd9Sstevel@tonic-gate */ 11597c478bd9Sstevel@tonic-gate if (bam_lock_fd < 0) { 11607c478bd9Sstevel@tonic-gate return; 11617c478bd9Sstevel@tonic-gate } 11627c478bd9Sstevel@tonic-gate 11637c478bd9Sstevel@tonic-gate unlock.l_type = F_UNLCK; 11647c478bd9Sstevel@tonic-gate unlock.l_whence = SEEK_SET; 11657c478bd9Sstevel@tonic-gate unlock.l_start = 0; 11667c478bd9Sstevel@tonic-gate unlock.l_len = 0; 11677c478bd9Sstevel@tonic-gate 11687c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) { 11697c478bd9Sstevel@tonic-gate bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11707c478bd9Sstevel@tonic-gate } 11717c478bd9Sstevel@tonic-gate 11727c478bd9Sstevel@tonic-gate if (close(bam_lock_fd) == -1) { 11737c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno)); 11747c478bd9Sstevel@tonic-gate } 11757c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 11767c478bd9Sstevel@tonic-gate } 11777c478bd9Sstevel@tonic-gate 11787c478bd9Sstevel@tonic-gate static error_t 11797c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt) 11807c478bd9Sstevel@tonic-gate { 11817c478bd9Sstevel@tonic-gate filelist_t flist; 11827c478bd9Sstevel@tonic-gate filelist_t *flistp = &flist; 11837c478bd9Sstevel@tonic-gate line_t *lp; 11847c478bd9Sstevel@tonic-gate 11857c478bd9Sstevel@tonic-gate assert(root); 11867c478bd9Sstevel@tonic-gate assert(opt == NULL); 11877c478bd9Sstevel@tonic-gate 11887c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 11897c478bd9Sstevel@tonic-gate if (read_list(root, flistp) != BAM_SUCCESS) { 11907c478bd9Sstevel@tonic-gate return (BAM_ERROR); 11917c478bd9Sstevel@tonic-gate } 11927c478bd9Sstevel@tonic-gate assert(flistp->head && flistp->tail); 11937c478bd9Sstevel@tonic-gate 11947c478bd9Sstevel@tonic-gate for (lp = flistp->head; lp; lp = lp->next) { 11957c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 11967c478bd9Sstevel@tonic-gate } 11977c478bd9Sstevel@tonic-gate 11987c478bd9Sstevel@tonic-gate filelist_free(flistp); 11997c478bd9Sstevel@tonic-gate 12007c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 12017c478bd9Sstevel@tonic-gate } 12027c478bd9Sstevel@tonic-gate 12037c478bd9Sstevel@tonic-gate /* 12047c478bd9Sstevel@tonic-gate * This routine writes a list of lines to a file. 12057c478bd9Sstevel@tonic-gate * The list is *not* freed 12067c478bd9Sstevel@tonic-gate */ 12077c478bd9Sstevel@tonic-gate static error_t 12087c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start) 12097c478bd9Sstevel@tonic-gate { 12107c478bd9Sstevel@tonic-gate char tmpfile[PATH_MAX]; 12117c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 12127c478bd9Sstevel@tonic-gate FILE *fp; 12137c478bd9Sstevel@tonic-gate int ret; 12147c478bd9Sstevel@tonic-gate struct stat sb; 12157c478bd9Sstevel@tonic-gate mode_t mode; 12167c478bd9Sstevel@tonic-gate uid_t root_uid; 12177c478bd9Sstevel@tonic-gate gid_t sys_gid; 12187c478bd9Sstevel@tonic-gate struct passwd *pw; 12197c478bd9Sstevel@tonic-gate struct group *gp; 12207c478bd9Sstevel@tonic-gate 12217c478bd9Sstevel@tonic-gate 12227c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, final); 12237c478bd9Sstevel@tonic-gate 12247c478bd9Sstevel@tonic-gate if (start == NULL) { 12257c478bd9Sstevel@tonic-gate if (stat(path, &sb) != -1) { 12267c478bd9Sstevel@tonic-gate bam_print(UNLINK_EMPTY, path); 12277c478bd9Sstevel@tonic-gate if (unlink(path) != 0) { 12287c478bd9Sstevel@tonic-gate bam_error(UNLINK_FAIL, path, strerror(errno)); 12297c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12307c478bd9Sstevel@tonic-gate } else { 12317c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 12327c478bd9Sstevel@tonic-gate } 12337c478bd9Sstevel@tonic-gate } 12347c478bd9Sstevel@tonic-gate } 12357c478bd9Sstevel@tonic-gate 12367c478bd9Sstevel@tonic-gate /* 12377c478bd9Sstevel@tonic-gate * Preserve attributes of existing file if possible, 12387c478bd9Sstevel@tonic-gate * otherwise ask the system for uid/gid of root/sys. 12397c478bd9Sstevel@tonic-gate * If all fails, fall back on hard-coded defaults. 12407c478bd9Sstevel@tonic-gate */ 12417c478bd9Sstevel@tonic-gate if (stat(path, &sb) != -1) { 12427c478bd9Sstevel@tonic-gate mode = sb.st_mode; 12437c478bd9Sstevel@tonic-gate root_uid = sb.st_uid; 12447c478bd9Sstevel@tonic-gate sys_gid = sb.st_gid; 12457c478bd9Sstevel@tonic-gate } else { 12467c478bd9Sstevel@tonic-gate mode = DEFAULT_DEV_MODE; 12477c478bd9Sstevel@tonic-gate if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) { 12487c478bd9Sstevel@tonic-gate root_uid = pw->pw_uid; 12497c478bd9Sstevel@tonic-gate } else { 12507c478bd9Sstevel@tonic-gate if (bam_verbose) 12517c478bd9Sstevel@tonic-gate bam_error(CANT_FIND_USER, 12527c478bd9Sstevel@tonic-gate DEFAULT_DEV_USER, DEFAULT_DEV_UID); 12537c478bd9Sstevel@tonic-gate root_uid = (uid_t)DEFAULT_DEV_UID; 12547c478bd9Sstevel@tonic-gate } 12557c478bd9Sstevel@tonic-gate if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) { 12567c478bd9Sstevel@tonic-gate sys_gid = gp->gr_gid; 12577c478bd9Sstevel@tonic-gate } else { 12587c478bd9Sstevel@tonic-gate if (bam_verbose) 12597c478bd9Sstevel@tonic-gate bam_error(CANT_FIND_GROUP, 12607c478bd9Sstevel@tonic-gate DEFAULT_DEV_GROUP, DEFAULT_DEV_GID); 12617c478bd9Sstevel@tonic-gate sys_gid = (gid_t)DEFAULT_DEV_GID; 12627c478bd9Sstevel@tonic-gate } 12637c478bd9Sstevel@tonic-gate } 12647c478bd9Sstevel@tonic-gate 12657c478bd9Sstevel@tonic-gate (void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp); 12667c478bd9Sstevel@tonic-gate 12677c478bd9Sstevel@tonic-gate /* Truncate tmpfile first */ 12687c478bd9Sstevel@tonic-gate fp = fopen(tmpfile, "w"); 12697c478bd9Sstevel@tonic-gate if (fp == NULL) { 12707c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 12717c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12727c478bd9Sstevel@tonic-gate } 12737c478bd9Sstevel@tonic-gate ret = fclose(fp); 12747c478bd9Sstevel@tonic-gate if (ret == EOF) { 12757c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 12767c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12777c478bd9Sstevel@tonic-gate } 12787c478bd9Sstevel@tonic-gate 12797c478bd9Sstevel@tonic-gate /* Now open it in append mode */ 12807c478bd9Sstevel@tonic-gate fp = fopen(tmpfile, "a"); 12817c478bd9Sstevel@tonic-gate if (fp == NULL) { 12827c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 12837c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12847c478bd9Sstevel@tonic-gate } 12857c478bd9Sstevel@tonic-gate 12867c478bd9Sstevel@tonic-gate for (; start; start = start->next) { 12877c478bd9Sstevel@tonic-gate ret = s_fputs(start->line, fp); 12887c478bd9Sstevel@tonic-gate if (ret == EOF) { 12897c478bd9Sstevel@tonic-gate bam_error(WRITE_FAIL, tmpfile, strerror(errno)); 12907c478bd9Sstevel@tonic-gate (void) fclose(fp); 12917c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12927c478bd9Sstevel@tonic-gate } 12937c478bd9Sstevel@tonic-gate } 12947c478bd9Sstevel@tonic-gate 12957c478bd9Sstevel@tonic-gate ret = fclose(fp); 12967c478bd9Sstevel@tonic-gate if (ret == EOF) { 12977c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 12987c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12997c478bd9Sstevel@tonic-gate } 13007c478bd9Sstevel@tonic-gate 13017c478bd9Sstevel@tonic-gate /* 1302de531be4Sjg * Set up desired attributes. Ignore failures on filesystems 1303de531be4Sjg * not supporting these operations - pcfs reports unsupported 1304de531be4Sjg * operations as EINVAL. 13057c478bd9Sstevel@tonic-gate */ 13067c478bd9Sstevel@tonic-gate ret = chmod(tmpfile, mode); 1307de531be4Sjg if (ret == -1 && 1308de531be4Sjg errno != EINVAL && errno != ENOTSUP) { 13097c478bd9Sstevel@tonic-gate bam_error(CHMOD_FAIL, tmpfile, strerror(errno)); 13107c478bd9Sstevel@tonic-gate return (BAM_ERROR); 13117c478bd9Sstevel@tonic-gate } 13127c478bd9Sstevel@tonic-gate 13137c478bd9Sstevel@tonic-gate ret = chown(tmpfile, root_uid, sys_gid); 1314de531be4Sjg if (ret == -1 && 1315de531be4Sjg errno != EINVAL && errno != ENOTSUP) { 13167c478bd9Sstevel@tonic-gate bam_error(CHOWN_FAIL, tmpfile, strerror(errno)); 13177c478bd9Sstevel@tonic-gate return (BAM_ERROR); 13187c478bd9Sstevel@tonic-gate } 13197c478bd9Sstevel@tonic-gate 13207c478bd9Sstevel@tonic-gate 13217c478bd9Sstevel@tonic-gate /* 13227c478bd9Sstevel@tonic-gate * Do an atomic rename 13237c478bd9Sstevel@tonic-gate */ 13247c478bd9Sstevel@tonic-gate ret = rename(tmpfile, path); 13257c478bd9Sstevel@tonic-gate if (ret != 0) { 13267c478bd9Sstevel@tonic-gate bam_error(RENAME_FAIL, path, strerror(errno)); 13277c478bd9Sstevel@tonic-gate return (BAM_ERROR); 13287c478bd9Sstevel@tonic-gate } 13297c478bd9Sstevel@tonic-gate 13307c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 13317c478bd9Sstevel@tonic-gate } 13327c478bd9Sstevel@tonic-gate 13337c478bd9Sstevel@tonic-gate /* 13347c478bd9Sstevel@tonic-gate * This function should always return 0 - since we want 13357c478bd9Sstevel@tonic-gate * to create stat data for *all* files in the list. 13367c478bd9Sstevel@tonic-gate */ 13377c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 13387c478bd9Sstevel@tonic-gate static int 13397c478bd9Sstevel@tonic-gate cmpstat( 13407c478bd9Sstevel@tonic-gate const char *file, 13417c478bd9Sstevel@tonic-gate const struct stat *stat, 13427c478bd9Sstevel@tonic-gate int flags, 13437c478bd9Sstevel@tonic-gate struct FTW *ftw) 13447c478bd9Sstevel@tonic-gate { 13457c478bd9Sstevel@tonic-gate uint_t sz; 13467c478bd9Sstevel@tonic-gate uint64_t *value; 13477c478bd9Sstevel@tonic-gate uint64_t filestat[2]; 13487c478bd9Sstevel@tonic-gate int error; 13497c478bd9Sstevel@tonic-gate 13507c478bd9Sstevel@tonic-gate /* 13517c478bd9Sstevel@tonic-gate * We only want regular files 13527c478bd9Sstevel@tonic-gate */ 13537c478bd9Sstevel@tonic-gate if (!S_ISREG(stat->st_mode)) 13547c478bd9Sstevel@tonic-gate return (0); 13557c478bd9Sstevel@tonic-gate 13567c478bd9Sstevel@tonic-gate /* 13577c478bd9Sstevel@tonic-gate * new_nvlp may be NULL if there were errors earlier 13587c478bd9Sstevel@tonic-gate * but this is not fatal to update determination. 13597c478bd9Sstevel@tonic-gate */ 13607c478bd9Sstevel@tonic-gate if (walk_arg.new_nvlp) { 13617c478bd9Sstevel@tonic-gate filestat[0] = stat->st_size; 13627c478bd9Sstevel@tonic-gate filestat[1] = stat->st_mtime; 13637c478bd9Sstevel@tonic-gate error = nvlist_add_uint64_array(walk_arg.new_nvlp, 13647c478bd9Sstevel@tonic-gate file + bam_rootlen, filestat, 2); 13657c478bd9Sstevel@tonic-gate if (error) 13667c478bd9Sstevel@tonic-gate bam_error(NVADD_FAIL, file, strerror(error)); 13677c478bd9Sstevel@tonic-gate } 13687c478bd9Sstevel@tonic-gate 13697c478bd9Sstevel@tonic-gate /* 13707c478bd9Sstevel@tonic-gate * The remaining steps are only required if we haven't made a 13717c478bd9Sstevel@tonic-gate * decision about update or if we are checking (-n) 13727c478bd9Sstevel@tonic-gate */ 13737c478bd9Sstevel@tonic-gate if (walk_arg.need_update && !bam_check) 13747c478bd9Sstevel@tonic-gate return (0); 13757c478bd9Sstevel@tonic-gate 13767c478bd9Sstevel@tonic-gate /* 13777c478bd9Sstevel@tonic-gate * If we are invoked as part of system/filesyste/boot-archive 13787c478bd9Sstevel@tonic-gate * SMF service, ignore amd64 modules unless we are booted amd64. 13797c478bd9Sstevel@tonic-gate */ 13808c1b6884Sszhou if (bam_smf_check && !is_amd64() && strstr(file, "/amd64/") != 0) 13817c478bd9Sstevel@tonic-gate return (0); 13827c478bd9Sstevel@tonic-gate 13837c478bd9Sstevel@tonic-gate /* 13847c478bd9Sstevel@tonic-gate * We need an update if file doesn't exist in old archive 13857c478bd9Sstevel@tonic-gate */ 13867c478bd9Sstevel@tonic-gate if (walk_arg.old_nvlp == NULL || 13877c478bd9Sstevel@tonic-gate nvlist_lookup_uint64_array(walk_arg.old_nvlp, 13887c478bd9Sstevel@tonic-gate file + bam_rootlen, &value, &sz) != 0) { 13897c478bd9Sstevel@tonic-gate if (bam_smf_check) /* ignore new during smf check */ 13907c478bd9Sstevel@tonic-gate return (0); 13917c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 13927c478bd9Sstevel@tonic-gate if (bam_verbose) 13937c478bd9Sstevel@tonic-gate bam_print(PARSEABLE_NEW_FILE, file); 13947c478bd9Sstevel@tonic-gate return (0); 13957c478bd9Sstevel@tonic-gate } 13967c478bd9Sstevel@tonic-gate 13977c478bd9Sstevel@tonic-gate /* 13987c478bd9Sstevel@tonic-gate * File exists in old archive. Check if file has changed 13997c478bd9Sstevel@tonic-gate */ 14007c478bd9Sstevel@tonic-gate assert(sz == 2); 14017c478bd9Sstevel@tonic-gate bcopy(value, filestat, sizeof (filestat)); 14027c478bd9Sstevel@tonic-gate 14037c478bd9Sstevel@tonic-gate if (filestat[0] != stat->st_size || 14047c478bd9Sstevel@tonic-gate filestat[1] != stat->st_mtime) { 14057c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 14067c478bd9Sstevel@tonic-gate if (bam_verbose) 14077c478bd9Sstevel@tonic-gate if (bam_smf_check) 14087c478bd9Sstevel@tonic-gate bam_print(" %s\n", file); 14097c478bd9Sstevel@tonic-gate else 14107c478bd9Sstevel@tonic-gate bam_print(PARSEABLE_OUT_DATE, file); 14117c478bd9Sstevel@tonic-gate } 14127c478bd9Sstevel@tonic-gate 14137c478bd9Sstevel@tonic-gate return (0); 14147c478bd9Sstevel@tonic-gate } 14157c478bd9Sstevel@tonic-gate 14167c478bd9Sstevel@tonic-gate /* 14177c478bd9Sstevel@tonic-gate * Check flags and presence of required files. 14187c478bd9Sstevel@tonic-gate * The force flag and/or absence of files should 14197c478bd9Sstevel@tonic-gate * trigger an update. 14207c478bd9Sstevel@tonic-gate * Suppress stdout output if check (-n) option is set 14217c478bd9Sstevel@tonic-gate * (as -n should only produce parseable output.) 14227c478bd9Sstevel@tonic-gate */ 14237c478bd9Sstevel@tonic-gate static void 14247c478bd9Sstevel@tonic-gate check_flags_and_files(char *root) 14257c478bd9Sstevel@tonic-gate { 14267c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 14277c478bd9Sstevel@tonic-gate struct stat sb; 14287c478bd9Sstevel@tonic-gate 14297c478bd9Sstevel@tonic-gate /* 14307c478bd9Sstevel@tonic-gate * if force, create archive unconditionally 14317c478bd9Sstevel@tonic-gate */ 14327c478bd9Sstevel@tonic-gate if (bam_force) { 14337c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 14347c478bd9Sstevel@tonic-gate if (bam_verbose && !bam_check) 14357c478bd9Sstevel@tonic-gate bam_print(UPDATE_FORCE); 14367c478bd9Sstevel@tonic-gate return; 14377c478bd9Sstevel@tonic-gate } 14387c478bd9Sstevel@tonic-gate 14397c478bd9Sstevel@tonic-gate /* 14407c478bd9Sstevel@tonic-gate * If archive is missing, create archive 14417c478bd9Sstevel@tonic-gate */ 14427c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE); 14437c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) { 14447c478bd9Sstevel@tonic-gate if (bam_verbose && !bam_check) 14457c478bd9Sstevel@tonic-gate bam_print(UPDATE_ARCH_MISS, path); 14467c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 14477c478bd9Sstevel@tonic-gate return; 14487c478bd9Sstevel@tonic-gate } 14497c478bd9Sstevel@tonic-gate } 14507c478bd9Sstevel@tonic-gate 14517c478bd9Sstevel@tonic-gate static error_t 14527c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t *flistp, char *filelist) 14537c478bd9Sstevel@tonic-gate { 14547c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 14557c478bd9Sstevel@tonic-gate FILE *fp; 14567c478bd9Sstevel@tonic-gate char buf[BAM_MAXLINE]; 14577c478bd9Sstevel@tonic-gate 14587c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, filelist); 14597c478bd9Sstevel@tonic-gate 14607c478bd9Sstevel@tonic-gate fp = fopen(path, "r"); 14617c478bd9Sstevel@tonic-gate if (fp == NULL) { 14627c478bd9Sstevel@tonic-gate if (bam_debug) 14637c478bd9Sstevel@tonic-gate bam_error(FLIST_FAIL, path, strerror(errno)); 14647c478bd9Sstevel@tonic-gate return (BAM_ERROR); 14657c478bd9Sstevel@tonic-gate } 14667c478bd9Sstevel@tonic-gate while (s_fgets(buf, sizeof (buf), fp) != NULL) { 1467b610f78eSvikram /* skip blank lines */ 1468b610f78eSvikram if (strspn(buf, " \t") == strlen(buf)) 1469b610f78eSvikram continue; 14707c478bd9Sstevel@tonic-gate append_to_flist(flistp, buf); 14717c478bd9Sstevel@tonic-gate } 14727c478bd9Sstevel@tonic-gate if (fclose(fp) != 0) { 14737c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, path, strerror(errno)); 14747c478bd9Sstevel@tonic-gate return (BAM_ERROR); 14757c478bd9Sstevel@tonic-gate } 14767c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 14777c478bd9Sstevel@tonic-gate } 14787c478bd9Sstevel@tonic-gate 14797c478bd9Sstevel@tonic-gate static error_t 14807c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t *flistp) 14817c478bd9Sstevel@tonic-gate { 14827c478bd9Sstevel@tonic-gate int rval; 14837c478bd9Sstevel@tonic-gate 14847c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 14857c478bd9Sstevel@tonic-gate 14867c478bd9Sstevel@tonic-gate /* 14877c478bd9Sstevel@tonic-gate * Read current lists of files - only the first is mandatory 14887c478bd9Sstevel@tonic-gate */ 14897c478bd9Sstevel@tonic-gate rval = read_one_list(root, flistp, BOOT_FILE_LIST); 14907c478bd9Sstevel@tonic-gate if (rval != BAM_SUCCESS) 14917c478bd9Sstevel@tonic-gate return (rval); 14927c478bd9Sstevel@tonic-gate (void) read_one_list(root, flistp, ETC_FILE_LIST); 14937c478bd9Sstevel@tonic-gate 14947c478bd9Sstevel@tonic-gate if (flistp->head == NULL) { 14957c478bd9Sstevel@tonic-gate bam_error(NO_FLIST); 14967c478bd9Sstevel@tonic-gate return (BAM_ERROR); 14977c478bd9Sstevel@tonic-gate } 14987c478bd9Sstevel@tonic-gate 14997c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 15007c478bd9Sstevel@tonic-gate } 15017c478bd9Sstevel@tonic-gate 15027c478bd9Sstevel@tonic-gate static void 15037c478bd9Sstevel@tonic-gate getoldstat(char *root) 15047c478bd9Sstevel@tonic-gate { 15057c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 15067c478bd9Sstevel@tonic-gate int fd, error; 15077c478bd9Sstevel@tonic-gate struct stat sb; 15087c478bd9Sstevel@tonic-gate char *ostat; 15097c478bd9Sstevel@tonic-gate 15107c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 15117c478bd9Sstevel@tonic-gate fd = open(path, O_RDONLY); 15127c478bd9Sstevel@tonic-gate if (fd == -1) { 15137c478bd9Sstevel@tonic-gate if (bam_verbose) 15147c478bd9Sstevel@tonic-gate bam_print(OPEN_FAIL, path, strerror(errno)); 15157c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 15167c478bd9Sstevel@tonic-gate return; 15177c478bd9Sstevel@tonic-gate } 15187c478bd9Sstevel@tonic-gate 15197c478bd9Sstevel@tonic-gate if (fstat(fd, &sb) != 0) { 15207c478bd9Sstevel@tonic-gate bam_error(STAT_FAIL, path, strerror(errno)); 15217c478bd9Sstevel@tonic-gate (void) close(fd); 15227c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 15237c478bd9Sstevel@tonic-gate return; 15247c478bd9Sstevel@tonic-gate } 15257c478bd9Sstevel@tonic-gate 15267c478bd9Sstevel@tonic-gate ostat = s_calloc(1, sb.st_size); 15277c478bd9Sstevel@tonic-gate 15287c478bd9Sstevel@tonic-gate if (read(fd, ostat, sb.st_size) != sb.st_size) { 15297c478bd9Sstevel@tonic-gate bam_error(READ_FAIL, path, strerror(errno)); 15307c478bd9Sstevel@tonic-gate (void) close(fd); 15317c478bd9Sstevel@tonic-gate free(ostat); 15327c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 15337c478bd9Sstevel@tonic-gate return; 15347c478bd9Sstevel@tonic-gate } 15357c478bd9Sstevel@tonic-gate 15367c478bd9Sstevel@tonic-gate (void) close(fd); 15377c478bd9Sstevel@tonic-gate 15387c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 15397c478bd9Sstevel@tonic-gate error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0); 15407c478bd9Sstevel@tonic-gate 15417c478bd9Sstevel@tonic-gate free(ostat); 15427c478bd9Sstevel@tonic-gate 15437c478bd9Sstevel@tonic-gate if (error) { 15447c478bd9Sstevel@tonic-gate bam_error(UNPACK_FAIL, path, strerror(error)); 15457c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 15467c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 15477c478bd9Sstevel@tonic-gate return; 15487c478bd9Sstevel@tonic-gate } 15497c478bd9Sstevel@tonic-gate } 15507c478bd9Sstevel@tonic-gate 15517c478bd9Sstevel@tonic-gate static void 15527c478bd9Sstevel@tonic-gate create_newstat(void) 15537c478bd9Sstevel@tonic-gate { 15547c478bd9Sstevel@tonic-gate int error; 15557c478bd9Sstevel@tonic-gate 15567c478bd9Sstevel@tonic-gate error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0); 15577c478bd9Sstevel@tonic-gate if (error) { 15587c478bd9Sstevel@tonic-gate /* 15597c478bd9Sstevel@tonic-gate * Not fatal - we can still create archive 15607c478bd9Sstevel@tonic-gate */ 15617c478bd9Sstevel@tonic-gate walk_arg.new_nvlp = NULL; 15627c478bd9Sstevel@tonic-gate bam_error(NVALLOC_FAIL, strerror(error)); 15637c478bd9Sstevel@tonic-gate } 15647c478bd9Sstevel@tonic-gate } 15657c478bd9Sstevel@tonic-gate 15667c478bd9Sstevel@tonic-gate static void 15677c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp) 15687c478bd9Sstevel@tonic-gate { 15697c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 15707c478bd9Sstevel@tonic-gate line_t *lp; 15717c478bd9Sstevel@tonic-gate 15727c478bd9Sstevel@tonic-gate for (lp = flistp->head; lp; lp = lp->next) { 15737c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, lp->line); 15747c478bd9Sstevel@tonic-gate /* XXX shouldn't we use FTW_MOUNT ? */ 15757c478bd9Sstevel@tonic-gate if (nftw(path, cmpstat, 20, 0) == -1) { 15767c478bd9Sstevel@tonic-gate /* 15777c478bd9Sstevel@tonic-gate * Some files may not exist. 15787c478bd9Sstevel@tonic-gate * For example: etc/rtc_config on a x86 diskless system 15797c478bd9Sstevel@tonic-gate * Emit verbose message only 15807c478bd9Sstevel@tonic-gate */ 15817c478bd9Sstevel@tonic-gate if (bam_verbose) 15827c478bd9Sstevel@tonic-gate bam_print(NFTW_FAIL, path, strerror(errno)); 15837c478bd9Sstevel@tonic-gate } 15847c478bd9Sstevel@tonic-gate } 15857c478bd9Sstevel@tonic-gate } 15867c478bd9Sstevel@tonic-gate 15877c478bd9Sstevel@tonic-gate static void 15887c478bd9Sstevel@tonic-gate savenew(char *root) 15897c478bd9Sstevel@tonic-gate { 15907c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 15917c478bd9Sstevel@tonic-gate char path2[PATH_MAX]; 15927c478bd9Sstevel@tonic-gate size_t sz; 15937c478bd9Sstevel@tonic-gate char *nstat; 15947c478bd9Sstevel@tonic-gate int fd, wrote, error; 15957c478bd9Sstevel@tonic-gate 15967c478bd9Sstevel@tonic-gate nstat = NULL; 15977c478bd9Sstevel@tonic-gate sz = 0; 15987c478bd9Sstevel@tonic-gate error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz, 15997c478bd9Sstevel@tonic-gate NV_ENCODE_XDR, 0); 16007c478bd9Sstevel@tonic-gate if (error) { 16017c478bd9Sstevel@tonic-gate bam_error(PACK_FAIL, strerror(error)); 16027c478bd9Sstevel@tonic-gate return; 16037c478bd9Sstevel@tonic-gate } 16047c478bd9Sstevel@tonic-gate 16057c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP); 16067c478bd9Sstevel@tonic-gate fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE); 16077c478bd9Sstevel@tonic-gate if (fd == -1) { 16087c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, path, strerror(errno)); 16097c478bd9Sstevel@tonic-gate free(nstat); 16107c478bd9Sstevel@tonic-gate return; 16117c478bd9Sstevel@tonic-gate } 16127c478bd9Sstevel@tonic-gate wrote = write(fd, nstat, sz); 16137c478bd9Sstevel@tonic-gate if (wrote != sz) { 16147c478bd9Sstevel@tonic-gate bam_error(WRITE_FAIL, path, strerror(errno)); 16157c478bd9Sstevel@tonic-gate (void) close(fd); 16167c478bd9Sstevel@tonic-gate free(nstat); 16177c478bd9Sstevel@tonic-gate return; 16187c478bd9Sstevel@tonic-gate } 16197c478bd9Sstevel@tonic-gate (void) close(fd); 16207c478bd9Sstevel@tonic-gate free(nstat); 16217c478bd9Sstevel@tonic-gate 16227c478bd9Sstevel@tonic-gate (void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT); 16237c478bd9Sstevel@tonic-gate if (rename(path, path2) != 0) { 16247c478bd9Sstevel@tonic-gate bam_error(RENAME_FAIL, path2, strerror(errno)); 16257c478bd9Sstevel@tonic-gate } 16267c478bd9Sstevel@tonic-gate } 16277c478bd9Sstevel@tonic-gate 16287c478bd9Sstevel@tonic-gate static void 16297c478bd9Sstevel@tonic-gate clear_walk_args(void) 16307c478bd9Sstevel@tonic-gate { 16317c478bd9Sstevel@tonic-gate if (walk_arg.old_nvlp) 16327c478bd9Sstevel@tonic-gate nvlist_free(walk_arg.old_nvlp); 16337c478bd9Sstevel@tonic-gate if (walk_arg.new_nvlp) 16347c478bd9Sstevel@tonic-gate nvlist_free(walk_arg.new_nvlp); 16357c478bd9Sstevel@tonic-gate walk_arg.need_update = 0; 16367c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 16377c478bd9Sstevel@tonic-gate walk_arg.new_nvlp = NULL; 16387c478bd9Sstevel@tonic-gate } 16397c478bd9Sstevel@tonic-gate 16407c478bd9Sstevel@tonic-gate /* 16417c478bd9Sstevel@tonic-gate * Returns: 16427c478bd9Sstevel@tonic-gate * 0 - no update necessary 16437c478bd9Sstevel@tonic-gate * 1 - update required. 16447c478bd9Sstevel@tonic-gate * BAM_ERROR (-1) - An error occurred 16457c478bd9Sstevel@tonic-gate * 16467c478bd9Sstevel@tonic-gate * Special handling for check (-n): 16477c478bd9Sstevel@tonic-gate * ================================ 16487c478bd9Sstevel@tonic-gate * The check (-n) option produces parseable output. 16497c478bd9Sstevel@tonic-gate * To do this, we suppress all stdout messages unrelated 16507c478bd9Sstevel@tonic-gate * to out of sync files. 16517c478bd9Sstevel@tonic-gate * All stderr messages are still printed though. 16527c478bd9Sstevel@tonic-gate * 16537c478bd9Sstevel@tonic-gate */ 16547c478bd9Sstevel@tonic-gate static int 16557c478bd9Sstevel@tonic-gate update_required(char *root) 16567c478bd9Sstevel@tonic-gate { 16577c478bd9Sstevel@tonic-gate struct stat sb; 16587c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 16597c478bd9Sstevel@tonic-gate filelist_t flist; 16607c478bd9Sstevel@tonic-gate filelist_t *flistp = &flist; 16617c478bd9Sstevel@tonic-gate int need_update; 16627c478bd9Sstevel@tonic-gate 16637c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 16647c478bd9Sstevel@tonic-gate 16657c478bd9Sstevel@tonic-gate walk_arg.need_update = 0; 16667c478bd9Sstevel@tonic-gate 16677c478bd9Sstevel@tonic-gate /* 16687c478bd9Sstevel@tonic-gate * Without consulting stat data, check if we need update 16697c478bd9Sstevel@tonic-gate */ 16707c478bd9Sstevel@tonic-gate check_flags_and_files(root); 16717c478bd9Sstevel@tonic-gate 16727c478bd9Sstevel@tonic-gate /* 16737c478bd9Sstevel@tonic-gate * In certain deployment scenarios, filestat may not 16747c478bd9Sstevel@tonic-gate * exist. Ignore it during boot-archive SMF check. 16757c478bd9Sstevel@tonic-gate */ 16767c478bd9Sstevel@tonic-gate if (bam_smf_check) { 16777c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 16787c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) 16797c478bd9Sstevel@tonic-gate return (0); 16807c478bd9Sstevel@tonic-gate } 16817c478bd9Sstevel@tonic-gate 16827c478bd9Sstevel@tonic-gate /* 16837c478bd9Sstevel@tonic-gate * consult stat data only if we haven't made a decision 16847c478bd9Sstevel@tonic-gate * about update. If checking (-n) however, we always 16857c478bd9Sstevel@tonic-gate * need stat data (since we want to compare old and new) 16867c478bd9Sstevel@tonic-gate */ 16877c478bd9Sstevel@tonic-gate if (!walk_arg.need_update || bam_check) 16887c478bd9Sstevel@tonic-gate getoldstat(root); 16897c478bd9Sstevel@tonic-gate 16907c478bd9Sstevel@tonic-gate /* 16917c478bd9Sstevel@tonic-gate * read list of files 16927c478bd9Sstevel@tonic-gate */ 16937c478bd9Sstevel@tonic-gate if (read_list(root, flistp) != BAM_SUCCESS) { 16947c478bd9Sstevel@tonic-gate clear_walk_args(); 16957c478bd9Sstevel@tonic-gate return (BAM_ERROR); 16967c478bd9Sstevel@tonic-gate } 16977c478bd9Sstevel@tonic-gate 16987c478bd9Sstevel@tonic-gate assert(flistp->head && flistp->tail); 16997c478bd9Sstevel@tonic-gate 17007c478bd9Sstevel@tonic-gate /* 17017c478bd9Sstevel@tonic-gate * At this point either the update is required 17027c478bd9Sstevel@tonic-gate * or the decision is pending. In either case 17037c478bd9Sstevel@tonic-gate * we need to create new stat nvlist 17047c478bd9Sstevel@tonic-gate */ 17057c478bd9Sstevel@tonic-gate create_newstat(); 17067c478bd9Sstevel@tonic-gate 17077c478bd9Sstevel@tonic-gate /* 17087c478bd9Sstevel@tonic-gate * This walk does 2 things: 17097c478bd9Sstevel@tonic-gate * - gets new stat data for every file 17107c478bd9Sstevel@tonic-gate * - (optional) compare old and new stat data 17117c478bd9Sstevel@tonic-gate */ 17127c478bd9Sstevel@tonic-gate walk_list(root, &flist); 17137c478bd9Sstevel@tonic-gate 17147c478bd9Sstevel@tonic-gate /* done with the file list */ 17157c478bd9Sstevel@tonic-gate filelist_free(flistp); 17167c478bd9Sstevel@tonic-gate 17177c478bd9Sstevel@tonic-gate /* 17187c478bd9Sstevel@tonic-gate * if we didn't succeed in creating new stat data above 17197c478bd9Sstevel@tonic-gate * just return result of update check so that archive is built. 17207c478bd9Sstevel@tonic-gate */ 17217c478bd9Sstevel@tonic-gate if (walk_arg.new_nvlp == NULL) { 17227c478bd9Sstevel@tonic-gate bam_error(NO_NEW_STAT); 17237c478bd9Sstevel@tonic-gate need_update = walk_arg.need_update; 17247c478bd9Sstevel@tonic-gate clear_walk_args(); 17257c478bd9Sstevel@tonic-gate return (need_update ? 1 : 0); 17267c478bd9Sstevel@tonic-gate } 17277c478bd9Sstevel@tonic-gate 17287c478bd9Sstevel@tonic-gate 17297c478bd9Sstevel@tonic-gate /* 17307c478bd9Sstevel@tonic-gate * If no update required, discard newstat 17317c478bd9Sstevel@tonic-gate */ 17327c478bd9Sstevel@tonic-gate if (!walk_arg.need_update) { 17337c478bd9Sstevel@tonic-gate clear_walk_args(); 17347c478bd9Sstevel@tonic-gate return (0); 17357c478bd9Sstevel@tonic-gate } 17367c478bd9Sstevel@tonic-gate 17377c478bd9Sstevel@tonic-gate /* 17387c478bd9Sstevel@tonic-gate * At this point we need an update - so save new stat data 17397c478bd9Sstevel@tonic-gate * However, if only checking (-n), don't save new stat data. 17407c478bd9Sstevel@tonic-gate */ 17417c478bd9Sstevel@tonic-gate if (!bam_check) 17427c478bd9Sstevel@tonic-gate savenew(root); 17437c478bd9Sstevel@tonic-gate 17447c478bd9Sstevel@tonic-gate clear_walk_args(); 17457c478bd9Sstevel@tonic-gate 17467c478bd9Sstevel@tonic-gate return (1); 17477c478bd9Sstevel@tonic-gate } 17487c478bd9Sstevel@tonic-gate 17497c478bd9Sstevel@tonic-gate static error_t 17507c478bd9Sstevel@tonic-gate create_ramdisk(char *root) 17517c478bd9Sstevel@tonic-gate { 17527c478bd9Sstevel@tonic-gate char *cmdline, path[PATH_MAX]; 17537c478bd9Sstevel@tonic-gate size_t len; 17547c478bd9Sstevel@tonic-gate struct stat sb; 17557c478bd9Sstevel@tonic-gate 17567c478bd9Sstevel@tonic-gate /* 17577c478bd9Sstevel@tonic-gate * Setup command args for create_ramdisk.ksh 17587c478bd9Sstevel@tonic-gate */ 17597c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK); 17607c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) { 17617c478bd9Sstevel@tonic-gate bam_error(ARCH_EXEC_MISS, path, strerror(errno)); 17627c478bd9Sstevel@tonic-gate return (BAM_ERROR); 17637c478bd9Sstevel@tonic-gate } 17647c478bd9Sstevel@tonic-gate 17657c478bd9Sstevel@tonic-gate len = strlen(path) + strlen(root) + 10; /* room for space + -R */ 17667c478bd9Sstevel@tonic-gate cmdline = s_calloc(1, len); 17677c478bd9Sstevel@tonic-gate 17687c478bd9Sstevel@tonic-gate if (strlen(root) > 1) { 17697c478bd9Sstevel@tonic-gate (void) snprintf(cmdline, len, "%s -R %s", path, root); 17707c478bd9Sstevel@tonic-gate /* chop off / at the end */ 17717c478bd9Sstevel@tonic-gate cmdline[strlen(cmdline) - 1] = '\0'; 17727c478bd9Sstevel@tonic-gate } else 17737c478bd9Sstevel@tonic-gate (void) snprintf(cmdline, len, "%s", path); 17747c478bd9Sstevel@tonic-gate 17757c478bd9Sstevel@tonic-gate if (exec_cmd(cmdline, NULL, 0) != 0) { 17767c478bd9Sstevel@tonic-gate bam_error(ARCHIVE_FAIL, cmdline); 17777c478bd9Sstevel@tonic-gate free(cmdline); 17787c478bd9Sstevel@tonic-gate return (BAM_ERROR); 17797c478bd9Sstevel@tonic-gate } 17807c478bd9Sstevel@tonic-gate free(cmdline); 17817c478bd9Sstevel@tonic-gate 17827c478bd9Sstevel@tonic-gate /* 17837c478bd9Sstevel@tonic-gate * Verify that the archive has been created 17847c478bd9Sstevel@tonic-gate */ 17857c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE); 17867c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) { 17877c478bd9Sstevel@tonic-gate bam_error(ARCHIVE_NOT_CREATED, path); 17887c478bd9Sstevel@tonic-gate return (BAM_ERROR); 17897c478bd9Sstevel@tonic-gate } 17907c478bd9Sstevel@tonic-gate 17917c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 17927c478bd9Sstevel@tonic-gate } 17937c478bd9Sstevel@tonic-gate 17947c478bd9Sstevel@tonic-gate /* 17957c478bd9Sstevel@tonic-gate * Checks if target filesystem is on a ramdisk 17967c478bd9Sstevel@tonic-gate * 1 - is miniroot 17977c478bd9Sstevel@tonic-gate * 0 - is not 17987c478bd9Sstevel@tonic-gate * When in doubt assume it is not a ramdisk. 17997c478bd9Sstevel@tonic-gate */ 18007c478bd9Sstevel@tonic-gate static int 18017c478bd9Sstevel@tonic-gate is_ramdisk(char *root) 18027c478bd9Sstevel@tonic-gate { 18037c478bd9Sstevel@tonic-gate struct extmnttab mnt; 18047c478bd9Sstevel@tonic-gate FILE *fp; 18057c478bd9Sstevel@tonic-gate int found; 1806b610f78eSvikram char mntpt[PATH_MAX]; 1807b610f78eSvikram char *cp; 18087c478bd9Sstevel@tonic-gate 18097c478bd9Sstevel@tonic-gate /* 18107c478bd9Sstevel@tonic-gate * There are 3 situations where creating archive is 18117c478bd9Sstevel@tonic-gate * of dubious value: 1812b610f78eSvikram * - create boot_archive on a lofi-mounted boot_archive 18137c478bd9Sstevel@tonic-gate * - create it on a ramdisk which is the root filesystem 18147c478bd9Sstevel@tonic-gate * - create it on a ramdisk mounted somewhere else 18157c478bd9Sstevel@tonic-gate * The first is not easy to detect and checking for it is not 18167c478bd9Sstevel@tonic-gate * worth it. 18177c478bd9Sstevel@tonic-gate * The other two conditions are handled here 18187c478bd9Sstevel@tonic-gate */ 18197c478bd9Sstevel@tonic-gate 18207c478bd9Sstevel@tonic-gate fp = fopen(MNTTAB, "r"); 18217c478bd9Sstevel@tonic-gate if (fp == NULL) { 18227c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 18237c478bd9Sstevel@tonic-gate return (0); 18247c478bd9Sstevel@tonic-gate } 18257c478bd9Sstevel@tonic-gate 18267c478bd9Sstevel@tonic-gate resetmnttab(fp); 18277c478bd9Sstevel@tonic-gate 1828b610f78eSvikram /* 1829b610f78eSvikram * Remove any trailing / from the mount point 1830b610f78eSvikram */ 1831b610f78eSvikram (void) strlcpy(mntpt, root, sizeof (mntpt)); 1832b610f78eSvikram if (strcmp(root, "/") != 0) { 1833b610f78eSvikram cp = mntpt + strlen(mntpt) - 1; 1834b610f78eSvikram if (*cp == '/') 1835b610f78eSvikram *cp = '\0'; 1836b610f78eSvikram } 18377c478bd9Sstevel@tonic-gate found = 0; 18387c478bd9Sstevel@tonic-gate while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 1839b610f78eSvikram if (strcmp(mnt.mnt_mountp, mntpt) == 0) { 18407c478bd9Sstevel@tonic-gate found = 1; 18417c478bd9Sstevel@tonic-gate break; 18427c478bd9Sstevel@tonic-gate } 18437c478bd9Sstevel@tonic-gate } 18447c478bd9Sstevel@tonic-gate 18457c478bd9Sstevel@tonic-gate if (!found) { 18467c478bd9Sstevel@tonic-gate if (bam_verbose) 1847b610f78eSvikram bam_error(NOT_IN_MNTTAB, mntpt); 18487c478bd9Sstevel@tonic-gate (void) fclose(fp); 18497c478bd9Sstevel@tonic-gate return (0); 18507c478bd9Sstevel@tonic-gate } 18517c478bd9Sstevel@tonic-gate 18527c478bd9Sstevel@tonic-gate if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) { 18537c478bd9Sstevel@tonic-gate if (bam_verbose) 18547c478bd9Sstevel@tonic-gate bam_error(IS_RAMDISK, bam_root); 18557c478bd9Sstevel@tonic-gate (void) fclose(fp); 18567c478bd9Sstevel@tonic-gate return (1); 18577c478bd9Sstevel@tonic-gate } 18587c478bd9Sstevel@tonic-gate 18597c478bd9Sstevel@tonic-gate (void) fclose(fp); 18607c478bd9Sstevel@tonic-gate 18617c478bd9Sstevel@tonic-gate return (0); 18627c478bd9Sstevel@tonic-gate } 18637c478bd9Sstevel@tonic-gate 18647c478bd9Sstevel@tonic-gate static int 18657c478bd9Sstevel@tonic-gate is_newboot(char *root) 18667c478bd9Sstevel@tonic-gate { 18677c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 18687c478bd9Sstevel@tonic-gate struct stat sb; 18697c478bd9Sstevel@tonic-gate 18707c478bd9Sstevel@tonic-gate /* 18717c478bd9Sstevel@tonic-gate * We can't boot without MULTI_BOOT 18727c478bd9Sstevel@tonic-gate */ 18737c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT); 18747c478bd9Sstevel@tonic-gate if (stat(path, &sb) == -1) { 18757c478bd9Sstevel@tonic-gate if (bam_verbose) 18767c478bd9Sstevel@tonic-gate bam_print(FILE_MISS, path); 18777c478bd9Sstevel@tonic-gate return (0); 18787c478bd9Sstevel@tonic-gate } 18797c478bd9Sstevel@tonic-gate 18807c478bd9Sstevel@tonic-gate /* 18817c478bd9Sstevel@tonic-gate * We can't generate archive without GRUB_DIR 18827c478bd9Sstevel@tonic-gate */ 18837c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR); 18847c478bd9Sstevel@tonic-gate if (stat(path, &sb) == -1) { 18857c478bd9Sstevel@tonic-gate if (bam_verbose) 18867c478bd9Sstevel@tonic-gate bam_print(DIR_MISS, path); 18877c478bd9Sstevel@tonic-gate return (0); 18887c478bd9Sstevel@tonic-gate } 18897c478bd9Sstevel@tonic-gate 18907c478bd9Sstevel@tonic-gate return (1); 18917c478bd9Sstevel@tonic-gate } 18927c478bd9Sstevel@tonic-gate 18937c478bd9Sstevel@tonic-gate static int 18947c478bd9Sstevel@tonic-gate is_readonly(char *root) 18957c478bd9Sstevel@tonic-gate { 18967c478bd9Sstevel@tonic-gate struct statvfs vfs; 18977c478bd9Sstevel@tonic-gate 18987c478bd9Sstevel@tonic-gate /* 18997c478bd9Sstevel@tonic-gate * Check for RDONLY filesystem 19007c478bd9Sstevel@tonic-gate * When in doubt assume it is not readonly 19017c478bd9Sstevel@tonic-gate */ 19027c478bd9Sstevel@tonic-gate if (statvfs(root, &vfs) != 0) { 19037c478bd9Sstevel@tonic-gate if (bam_verbose) 19047c478bd9Sstevel@tonic-gate bam_error(STATVFS_FAIL, root, strerror(errno)); 19057c478bd9Sstevel@tonic-gate return (0); 19067c478bd9Sstevel@tonic-gate } 19077c478bd9Sstevel@tonic-gate 19087c478bd9Sstevel@tonic-gate if (vfs.f_flag & ST_RDONLY) { 19097c478bd9Sstevel@tonic-gate return (1); 19107c478bd9Sstevel@tonic-gate } 19117c478bd9Sstevel@tonic-gate 19127c478bd9Sstevel@tonic-gate return (0); 19137c478bd9Sstevel@tonic-gate } 19147c478bd9Sstevel@tonic-gate 19157c478bd9Sstevel@tonic-gate static error_t 19167c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt) 19177c478bd9Sstevel@tonic-gate { 19187c478bd9Sstevel@tonic-gate error_t ret; 19197c478bd9Sstevel@tonic-gate 19207c478bd9Sstevel@tonic-gate assert(root); 19217c478bd9Sstevel@tonic-gate assert(opt == NULL); 19227c478bd9Sstevel@tonic-gate 19237c478bd9Sstevel@tonic-gate /* 1924b610f78eSvikram * root must belong to a GRUB boot OS, 19257c478bd9Sstevel@tonic-gate * don't care on sparc except for diskless clients 19267c478bd9Sstevel@tonic-gate */ 19277c478bd9Sstevel@tonic-gate if (!is_newboot(root)) { 1928b610f78eSvikram /* 1929b610f78eSvikram * Emit message only if not in context of update_all. 1930b610f78eSvikram * If in update_all, emit only if verbose flag is set. 1931b610f78eSvikram */ 1932b610f78eSvikram if (!bam_update_all || bam_verbose) 1933b610f78eSvikram bam_print(NOT_GRUB_BOOT, root); 19347c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 19357c478bd9Sstevel@tonic-gate } 19367c478bd9Sstevel@tonic-gate 19377c478bd9Sstevel@tonic-gate /* 19388c1b6884Sszhou * If smf check is requested when / is writable (can happen 19398c1b6884Sszhou * on first reboot following an upgrade because service 19408c1b6884Sszhou * dependency is messed up), skip the check. 19418c1b6884Sszhou */ 19428c1b6884Sszhou if (bam_smf_check && !bam_root_readonly) 19438c1b6884Sszhou return (BAM_SUCCESS); 19448c1b6884Sszhou 19458c1b6884Sszhou /* 19468c1b6884Sszhou * root must be writable. This check applies to alternate 19478c1b6884Sszhou * root (-R option); bam_root_readonly applies to '/' only. 19487c478bd9Sstevel@tonic-gate * Note: statvfs() does not always report the truth 19497c478bd9Sstevel@tonic-gate */ 1950*61cb17bdSsetje if (!bam_smf_check && !bam_check && is_readonly(root)) { 19518c1b6884Sszhou if (bam_verbose) 19527c478bd9Sstevel@tonic-gate bam_print(RDONLY_FS, root); 19537c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 19547c478bd9Sstevel@tonic-gate } 19557c478bd9Sstevel@tonic-gate 19567c478bd9Sstevel@tonic-gate /* 19577c478bd9Sstevel@tonic-gate * Don't generate archive on ramdisk 19587c478bd9Sstevel@tonic-gate */ 19597c478bd9Sstevel@tonic-gate if (is_ramdisk(root)) { 19607c478bd9Sstevel@tonic-gate if (bam_verbose) 19617c478bd9Sstevel@tonic-gate bam_print(SKIP_RAMDISK); 19627c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 19637c478bd9Sstevel@tonic-gate } 19647c478bd9Sstevel@tonic-gate 19657c478bd9Sstevel@tonic-gate /* 19667c478bd9Sstevel@tonic-gate * Now check if updated is really needed 19677c478bd9Sstevel@tonic-gate */ 19687c478bd9Sstevel@tonic-gate ret = update_required(root); 19697c478bd9Sstevel@tonic-gate 19707c478bd9Sstevel@tonic-gate /* 19717c478bd9Sstevel@tonic-gate * The check command (-n) is *not* a dry run 19727c478bd9Sstevel@tonic-gate * It only checks if the archive is in sync. 19737c478bd9Sstevel@tonic-gate */ 19747c478bd9Sstevel@tonic-gate if (bam_check) { 19757c478bd9Sstevel@tonic-gate bam_exit((ret != 0) ? 1 : 0); 19767c478bd9Sstevel@tonic-gate } 19777c478bd9Sstevel@tonic-gate 19787c478bd9Sstevel@tonic-gate if (ret == 1) { 19797c478bd9Sstevel@tonic-gate /* create the ramdisk */ 19807c478bd9Sstevel@tonic-gate ret = create_ramdisk(root); 19817c478bd9Sstevel@tonic-gate } 19827c478bd9Sstevel@tonic-gate return (ret); 19837c478bd9Sstevel@tonic-gate } 19847c478bd9Sstevel@tonic-gate 1985b610f78eSvikram static void 1986b610f78eSvikram restore_grub_slice(void) 1987b610f78eSvikram { 1988b610f78eSvikram struct stat sb; 1989b610f78eSvikram char *mntpt, *physlice; 1990b610f78eSvikram int mnted; /* set if we did a mount */ 1991b610f78eSvikram char menupath[PATH_MAX], cmd[PATH_MAX]; 1992b610f78eSvikram 1993b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 1994b610f78eSvikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 1995b610f78eSvikram return; 1996b610f78eSvikram } 1997b610f78eSvikram 1998b610f78eSvikram /* 1999b610f78eSvikram * If we are doing an luactivate, don't attempt to restore GRUB or else 2000b610f78eSvikram * we may not be able to get to DCA boot environments. Let luactivate 2001b610f78eSvikram * handle GRUB/DCA installation 2002b610f78eSvikram */ 2003b610f78eSvikram if (stat(LU_ACTIVATE_FILE, &sb) == 0) { 2004b610f78eSvikram return; 2005b610f78eSvikram } 2006b610f78eSvikram 2007b610f78eSvikram mnted = 0; 2008b610f78eSvikram physlice = NULL; 200940541d5dSvikram mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL); 2010b610f78eSvikram if (mntpt == NULL) { 2011b610f78eSvikram bam_error(CANNOT_RESTORE_GRUB_SLICE); 2012b610f78eSvikram return; 2013b610f78eSvikram } 2014b610f78eSvikram 2015b610f78eSvikram (void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU); 2016b610f78eSvikram if (stat(menupath, &sb) == 0) { 201740541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2018b610f78eSvikram return; 2019b610f78eSvikram } 2020b610f78eSvikram 2021b610f78eSvikram /* 2022b610f78eSvikram * The menu is missing - we need to do a restore 2023b610f78eSvikram */ 2024b610f78eSvikram bam_print(RESTORING_GRUB); 2025b610f78eSvikram 2026b610f78eSvikram (void) snprintf(cmd, sizeof (cmd), "%s %s %s %s", 2027b610f78eSvikram INSTALLGRUB, STAGE1, STAGE2, physlice); 2028b610f78eSvikram 2029b610f78eSvikram if (exec_cmd(cmd, NULL, 0) != 0) { 2030b610f78eSvikram bam_error(RESTORE_GRUB_FAILED); 203140541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2032b610f78eSvikram return; 2033b610f78eSvikram } 2034b610f78eSvikram 2035b610f78eSvikram if (stat(GRUB_backup_menu, &sb) != 0) { 2036b610f78eSvikram bam_error(MISSING_BACKUP_MENU, 2037b610f78eSvikram GRUB_backup_menu, strerror(errno)); 203840541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2039b610f78eSvikram return; 2040b610f78eSvikram } 2041b610f78eSvikram 2042b610f78eSvikram (void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s", 2043b610f78eSvikram GRUB_backup_menu, menupath); 2044b610f78eSvikram 2045b610f78eSvikram if (exec_cmd(cmd, NULL, 0) != 0) { 2046b610f78eSvikram bam_error(RESTORE_MENU_FAILED, menupath); 204740541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2048b610f78eSvikram return; 2049b610f78eSvikram } 2050b610f78eSvikram 2051b610f78eSvikram /* Success */ 205240541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2053b610f78eSvikram } 2054b610f78eSvikram 20557c478bd9Sstevel@tonic-gate static error_t 20567c478bd9Sstevel@tonic-gate update_all(char *root, char *opt) 20577c478bd9Sstevel@tonic-gate { 20587c478bd9Sstevel@tonic-gate struct extmnttab mnt; 20597c478bd9Sstevel@tonic-gate struct stat sb; 20607c478bd9Sstevel@tonic-gate FILE *fp; 20617c478bd9Sstevel@tonic-gate char multibt[PATH_MAX]; 20627c478bd9Sstevel@tonic-gate error_t ret = BAM_SUCCESS; 20637c478bd9Sstevel@tonic-gate 206440541d5dSvikram assert(root); 20657c478bd9Sstevel@tonic-gate assert(opt == NULL); 20667c478bd9Sstevel@tonic-gate 206740541d5dSvikram if (bam_rootlen != 1 || *root != '/') { 206840541d5dSvikram elide_trailing_slash(root, multibt, sizeof (multibt)); 206940541d5dSvikram bam_error(ALT_ROOT_INVALID, multibt); 207040541d5dSvikram return (BAM_ERROR); 207140541d5dSvikram } 207240541d5dSvikram 20737c478bd9Sstevel@tonic-gate /* 20747c478bd9Sstevel@tonic-gate * First update archive for current root 20757c478bd9Sstevel@tonic-gate */ 20767c478bd9Sstevel@tonic-gate if (update_archive(root, opt) != BAM_SUCCESS) 20777c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 20787c478bd9Sstevel@tonic-gate 20797c478bd9Sstevel@tonic-gate /* 20807c478bd9Sstevel@tonic-gate * Now walk the mount table, performing archive update 20817c478bd9Sstevel@tonic-gate * for all mounted Newboot root filesystems 20827c478bd9Sstevel@tonic-gate */ 20837c478bd9Sstevel@tonic-gate fp = fopen(MNTTAB, "r"); 20847c478bd9Sstevel@tonic-gate if (fp == NULL) { 20857c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 2086b610f78eSvikram ret = BAM_ERROR; 2087b610f78eSvikram goto out; 20887c478bd9Sstevel@tonic-gate } 20897c478bd9Sstevel@tonic-gate 20907c478bd9Sstevel@tonic-gate resetmnttab(fp); 20917c478bd9Sstevel@tonic-gate 20927c478bd9Sstevel@tonic-gate while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 20937c478bd9Sstevel@tonic-gate if (mnt.mnt_special == NULL) 20947c478bd9Sstevel@tonic-gate continue; 20957c478bd9Sstevel@tonic-gate if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0) 20967c478bd9Sstevel@tonic-gate continue; 20977c478bd9Sstevel@tonic-gate if (strcmp(mnt.mnt_mountp, "/") == 0) 20987c478bd9Sstevel@tonic-gate continue; 20997c478bd9Sstevel@tonic-gate 21007c478bd9Sstevel@tonic-gate (void) snprintf(multibt, sizeof (multibt), "%s%s", 21017c478bd9Sstevel@tonic-gate mnt.mnt_mountp, MULTI_BOOT); 21027c478bd9Sstevel@tonic-gate 21037c478bd9Sstevel@tonic-gate if (stat(multibt, &sb) == -1) 21047c478bd9Sstevel@tonic-gate continue; 21057c478bd9Sstevel@tonic-gate 21067c478bd9Sstevel@tonic-gate /* 21077c478bd9Sstevel@tonic-gate * We put a trailing slash to be consistent with root = "/" 21087c478bd9Sstevel@tonic-gate * case, such that we don't have to print // in some cases. 21097c478bd9Sstevel@tonic-gate */ 21107c478bd9Sstevel@tonic-gate (void) snprintf(rootbuf, sizeof (rootbuf), "%s/", 21117c478bd9Sstevel@tonic-gate mnt.mnt_mountp); 21127c478bd9Sstevel@tonic-gate bam_rootlen = strlen(rootbuf); 21137c478bd9Sstevel@tonic-gate if (update_archive(rootbuf, opt) != BAM_SUCCESS) 21147c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 21157c478bd9Sstevel@tonic-gate } 21167c478bd9Sstevel@tonic-gate 21177c478bd9Sstevel@tonic-gate (void) fclose(fp); 21187c478bd9Sstevel@tonic-gate 2119b610f78eSvikram out: 2120b610f78eSvikram if (stat(GRUB_slice, &sb) == 0) { 2121b610f78eSvikram restore_grub_slice(); 2122b610f78eSvikram } 2123b610f78eSvikram 21247c478bd9Sstevel@tonic-gate return (ret); 21257c478bd9Sstevel@tonic-gate } 21267c478bd9Sstevel@tonic-gate 21277c478bd9Sstevel@tonic-gate static void 21287c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp) 21297c478bd9Sstevel@tonic-gate { 21307c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 21317c478bd9Sstevel@tonic-gate mp->start = lp; 21327c478bd9Sstevel@tonic-gate } else { 21337c478bd9Sstevel@tonic-gate mp->end->next = lp; 21348c1b6884Sszhou lp->prev = mp->end; 21357c478bd9Sstevel@tonic-gate } 21367c478bd9Sstevel@tonic-gate mp->end = lp; 21377c478bd9Sstevel@tonic-gate } 21387c478bd9Sstevel@tonic-gate 21398c1b6884Sszhou static void 21408c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp) 21418c1b6884Sszhou { 21428c1b6884Sszhou /* unlink from list */ 21438c1b6884Sszhou if (lp->prev) 21448c1b6884Sszhou lp->prev->next = lp->next; 21458c1b6884Sszhou else 21468c1b6884Sszhou mp->start = lp->next; 21478c1b6884Sszhou if (lp->next) 21488c1b6884Sszhou lp->next->prev = lp->prev; 21498c1b6884Sszhou else 21508c1b6884Sszhou mp->end = lp->prev; 21518c1b6884Sszhou } 21528c1b6884Sszhou 21538c1b6884Sszhou static entry_t * 21548c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end) 21558c1b6884Sszhou { 21568c1b6884Sszhou entry_t *ent, *prev; 21578c1b6884Sszhou 21588c1b6884Sszhou ent = s_calloc(1, sizeof (entry_t)); 21598c1b6884Sszhou ent->start = start; 21608c1b6884Sszhou ent->end = end; 21618c1b6884Sszhou 21628c1b6884Sszhou if (mp->entries == NULL) { 21638c1b6884Sszhou mp->entries = ent; 21648c1b6884Sszhou return (ent); 21658c1b6884Sszhou } 21668c1b6884Sszhou 21678c1b6884Sszhou prev = mp->entries; 21688c1b6884Sszhou while (prev->next) 21698c1b6884Sszhou prev = prev-> next; 21708c1b6884Sszhou prev->next = ent; 21718c1b6884Sszhou ent->prev = prev; 21728c1b6884Sszhou return (ent); 21738c1b6884Sszhou } 21748c1b6884Sszhou 21758c1b6884Sszhou static void 21768c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp) 21778c1b6884Sszhou { 21788c1b6884Sszhou if (ent) 21798c1b6884Sszhou ent->end = lp; 21808c1b6884Sszhou } 21818c1b6884Sszhou 21827c478bd9Sstevel@tonic-gate /* 21837c478bd9Sstevel@tonic-gate * A line in menu.lst looks like 21847c478bd9Sstevel@tonic-gate * [ ]*<cmd>[ \t=]*<arg>* 21857c478bd9Sstevel@tonic-gate */ 21867c478bd9Sstevel@tonic-gate static void 21877c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum) 21887c478bd9Sstevel@tonic-gate { 21897c478bd9Sstevel@tonic-gate /* 21907c478bd9Sstevel@tonic-gate * save state across calls. This is so that 21917c478bd9Sstevel@tonic-gate * header gets the right entry# after title has 21927c478bd9Sstevel@tonic-gate * been processed 21937c478bd9Sstevel@tonic-gate */ 21948c1b6884Sszhou static line_t *prev = NULL; 21958c1b6884Sszhou static entry_t *curr_ent = NULL; 21967c478bd9Sstevel@tonic-gate 21977c478bd9Sstevel@tonic-gate line_t *lp; 21987c478bd9Sstevel@tonic-gate char *cmd, *sep, *arg; 21997c478bd9Sstevel@tonic-gate char save, *cp, *line; 22007c478bd9Sstevel@tonic-gate menu_flag_t flag = BAM_INVALID; 22017c478bd9Sstevel@tonic-gate 22027c478bd9Sstevel@tonic-gate if (str == NULL) { 22037c478bd9Sstevel@tonic-gate return; 22047c478bd9Sstevel@tonic-gate } 22057c478bd9Sstevel@tonic-gate 22067c478bd9Sstevel@tonic-gate /* 22077c478bd9Sstevel@tonic-gate * First save a copy of the entire line. 22087c478bd9Sstevel@tonic-gate * We use this later to set the line field. 22097c478bd9Sstevel@tonic-gate */ 22107c478bd9Sstevel@tonic-gate line = s_strdup(str); 22117c478bd9Sstevel@tonic-gate 22127c478bd9Sstevel@tonic-gate /* Eat up leading whitespace */ 22137c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 22147c478bd9Sstevel@tonic-gate str++; 22157c478bd9Sstevel@tonic-gate 22167c478bd9Sstevel@tonic-gate if (*str == '#') { /* comment */ 22177c478bd9Sstevel@tonic-gate cmd = s_strdup("#"); 22187c478bd9Sstevel@tonic-gate sep = NULL; 22197c478bd9Sstevel@tonic-gate arg = s_strdup(str + 1); 22207c478bd9Sstevel@tonic-gate flag = BAM_COMMENT; 22217c478bd9Sstevel@tonic-gate } else if (*str == '\0') { /* blank line */ 22227c478bd9Sstevel@tonic-gate cmd = sep = arg = NULL; 22237c478bd9Sstevel@tonic-gate flag = BAM_EMPTY; 22247c478bd9Sstevel@tonic-gate } else { 22257c478bd9Sstevel@tonic-gate /* 22267c478bd9Sstevel@tonic-gate * '=' is not a documented separator in grub syntax. 22277c478bd9Sstevel@tonic-gate * However various development bits use '=' as a 22287c478bd9Sstevel@tonic-gate * separator. In addition, external users also 22297c478bd9Sstevel@tonic-gate * use = as a separator. So we will allow that usage. 22307c478bd9Sstevel@tonic-gate */ 22317c478bd9Sstevel@tonic-gate cp = str; 22327c478bd9Sstevel@tonic-gate while (*str != ' ' && *str != '\t' && *str != '=') { 22337c478bd9Sstevel@tonic-gate if (*str == '\0') { 22347c478bd9Sstevel@tonic-gate cmd = s_strdup(cp); 22357c478bd9Sstevel@tonic-gate sep = arg = NULL; 22367c478bd9Sstevel@tonic-gate break; 22377c478bd9Sstevel@tonic-gate } 22387c478bd9Sstevel@tonic-gate str++; 22397c478bd9Sstevel@tonic-gate } 22407c478bd9Sstevel@tonic-gate 22417c478bd9Sstevel@tonic-gate if (*str != '\0') { 22427c478bd9Sstevel@tonic-gate save = *str; 22437c478bd9Sstevel@tonic-gate *str = '\0'; 22447c478bd9Sstevel@tonic-gate cmd = s_strdup(cp); 22457c478bd9Sstevel@tonic-gate *str = save; 22467c478bd9Sstevel@tonic-gate 22477c478bd9Sstevel@tonic-gate str++; 22487c478bd9Sstevel@tonic-gate save = *str; 22497c478bd9Sstevel@tonic-gate *str = '\0'; 22507c478bd9Sstevel@tonic-gate sep = s_strdup(str - 1); 22517c478bd9Sstevel@tonic-gate *str = save; 22527c478bd9Sstevel@tonic-gate 22537c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 22547c478bd9Sstevel@tonic-gate str++; 22557c478bd9Sstevel@tonic-gate if (*str == '\0') 22567c478bd9Sstevel@tonic-gate arg = NULL; 22577c478bd9Sstevel@tonic-gate else 22587c478bd9Sstevel@tonic-gate arg = s_strdup(str); 22597c478bd9Sstevel@tonic-gate } 22607c478bd9Sstevel@tonic-gate } 22617c478bd9Sstevel@tonic-gate 22627c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 22637c478bd9Sstevel@tonic-gate 22647c478bd9Sstevel@tonic-gate lp->cmd = cmd; 22657c478bd9Sstevel@tonic-gate lp->sep = sep; 22667c478bd9Sstevel@tonic-gate lp->arg = arg; 22677c478bd9Sstevel@tonic-gate lp->line = line; 22687c478bd9Sstevel@tonic-gate lp->lineNum = ++(*lineNum); 22697c478bd9Sstevel@tonic-gate if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) { 22707c478bd9Sstevel@tonic-gate lp->entryNum = ++(*entryNum); 22717c478bd9Sstevel@tonic-gate lp->flags = BAM_TITLE; 22727c478bd9Sstevel@tonic-gate if (prev && prev->flags == BAM_COMMENT && 22738c1b6884Sszhou prev->arg && strcmp(prev->arg, BAM_HDR) == 0) { 22747c478bd9Sstevel@tonic-gate prev->entryNum = lp->entryNum; 22758c1b6884Sszhou curr_ent = boot_entry_new(mp, prev, lp); 22768c1b6884Sszhou } else { 22778c1b6884Sszhou curr_ent = boot_entry_new(mp, lp, lp); 22788c1b6884Sszhou } 22797c478bd9Sstevel@tonic-gate } else if (flag != BAM_INVALID) { 22807c478bd9Sstevel@tonic-gate /* 22817c478bd9Sstevel@tonic-gate * For header comments, the entry# is "fixed up" 22827c478bd9Sstevel@tonic-gate * by the subsequent title 22837c478bd9Sstevel@tonic-gate */ 22847c478bd9Sstevel@tonic-gate lp->entryNum = *entryNum; 22857c478bd9Sstevel@tonic-gate lp->flags = flag; 22867c478bd9Sstevel@tonic-gate } else { 22877c478bd9Sstevel@tonic-gate lp->entryNum = *entryNum; 22887c478bd9Sstevel@tonic-gate lp->flags = (*entryNum == ENTRY_INIT) ? BAM_GLOBAL : BAM_ENTRY; 22897c478bd9Sstevel@tonic-gate } 22907c478bd9Sstevel@tonic-gate 22918c1b6884Sszhou /* record default, old default, and entry line ranges */ 22928c1b6884Sszhou if (lp->flags == BAM_GLOBAL && 22938c1b6884Sszhou strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) { 22948c1b6884Sszhou mp->curdefault = lp; 22958c1b6884Sszhou } else if (lp->flags == BAM_COMMENT && 22968c1b6884Sszhou strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) { 22978c1b6884Sszhou mp->olddefault = lp; 22988c1b6884Sszhou } else if (lp->flags == BAM_ENTRY || 22998c1b6884Sszhou (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_FTR) == 0)) { 23008c1b6884Sszhou boot_entry_addline(curr_ent, lp); 23018c1b6884Sszhou } 23027c478bd9Sstevel@tonic-gate append_line(mp, lp); 23037c478bd9Sstevel@tonic-gate 23047c478bd9Sstevel@tonic-gate prev = lp; 23057c478bd9Sstevel@tonic-gate } 23067c478bd9Sstevel@tonic-gate 230740541d5dSvikram static void 230840541d5dSvikram update_numbering(menu_t *mp) 230940541d5dSvikram { 231040541d5dSvikram int lineNum; 231140541d5dSvikram int entryNum; 231240541d5dSvikram int old_default_value; 231340541d5dSvikram line_t *lp, *prev, *default_lp, *default_entry; 231440541d5dSvikram char buf[PATH_MAX]; 231540541d5dSvikram 231640541d5dSvikram if (mp->start == NULL) { 231740541d5dSvikram return; 231840541d5dSvikram } 231940541d5dSvikram 232040541d5dSvikram lineNum = LINE_INIT; 232140541d5dSvikram entryNum = ENTRY_INIT; 232240541d5dSvikram old_default_value = ENTRY_INIT; 232340541d5dSvikram lp = default_lp = default_entry = NULL; 232440541d5dSvikram 232540541d5dSvikram prev = NULL; 232640541d5dSvikram for (lp = mp->start; lp; prev = lp, lp = lp->next) { 232740541d5dSvikram lp->lineNum = ++lineNum; 232840541d5dSvikram 232940541d5dSvikram /* 233040541d5dSvikram * Get the value of the default command 233140541d5dSvikram */ 233240541d5dSvikram if (lp->entryNum == ENTRY_INIT && lp->cmd && 233340541d5dSvikram strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 && 233440541d5dSvikram lp->arg) { 233540541d5dSvikram old_default_value = atoi(lp->arg); 233640541d5dSvikram default_lp = lp; 233740541d5dSvikram } 233840541d5dSvikram 233940541d5dSvikram /* 234040541d5dSvikram * If not boot entry, nothing else to fix for this 234140541d5dSvikram * entry 234240541d5dSvikram */ 234340541d5dSvikram if (lp->entryNum == ENTRY_INIT) 234440541d5dSvikram continue; 234540541d5dSvikram 234640541d5dSvikram /* 234740541d5dSvikram * Record the position of the default entry. 234840541d5dSvikram * The following works because global 234940541d5dSvikram * commands like default and timeout should precede 235040541d5dSvikram * actual boot entries, so old_default_value 235140541d5dSvikram * is already known (or default cmd is missing). 235240541d5dSvikram */ 235340541d5dSvikram if (default_entry == NULL && 235440541d5dSvikram old_default_value != ENTRY_INIT && 235540541d5dSvikram lp->entryNum == old_default_value) { 235640541d5dSvikram default_entry = lp; 235740541d5dSvikram } 235840541d5dSvikram 235940541d5dSvikram /* 236040541d5dSvikram * Now fixup the entry number 236140541d5dSvikram */ 236240541d5dSvikram if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) { 236340541d5dSvikram lp->entryNum = ++entryNum; 236440541d5dSvikram /* fixup the bootadm header */ 236540541d5dSvikram if (prev && prev->flags == BAM_COMMENT && 236640541d5dSvikram prev->arg && strcmp(prev->arg, BAM_HDR) == 0) { 236740541d5dSvikram prev->entryNum = lp->entryNum; 236840541d5dSvikram } 236940541d5dSvikram } else { 237040541d5dSvikram lp->entryNum = entryNum; 237140541d5dSvikram } 237240541d5dSvikram } 237340541d5dSvikram 237440541d5dSvikram /* 237540541d5dSvikram * No default command in menu, simply return 237640541d5dSvikram */ 237740541d5dSvikram if (default_lp == NULL) { 237840541d5dSvikram return; 237940541d5dSvikram } 238040541d5dSvikram 238140541d5dSvikram free(default_lp->arg); 238240541d5dSvikram free(default_lp->line); 238340541d5dSvikram 238440541d5dSvikram if (default_entry == NULL) { 238540541d5dSvikram default_lp->arg = s_strdup("0"); 238640541d5dSvikram } else { 238740541d5dSvikram (void) snprintf(buf, sizeof (buf), "%d", 238840541d5dSvikram default_entry->entryNum); 238940541d5dSvikram default_lp->arg = s_strdup(buf); 239040541d5dSvikram } 239140541d5dSvikram 239240541d5dSvikram /* 239340541d5dSvikram * The following is required since only the line field gets 239440541d5dSvikram * written back to menu.lst 239540541d5dSvikram */ 239640541d5dSvikram (void) snprintf(buf, sizeof (buf), "%s%s%s", 239740541d5dSvikram menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg); 239840541d5dSvikram default_lp->line = s_strdup(buf); 239940541d5dSvikram } 240040541d5dSvikram 240140541d5dSvikram 24027c478bd9Sstevel@tonic-gate static menu_t * 24037c478bd9Sstevel@tonic-gate menu_read(char *menu_path) 24047c478bd9Sstevel@tonic-gate { 24057c478bd9Sstevel@tonic-gate FILE *fp; 24067c478bd9Sstevel@tonic-gate char buf[BAM_MAXLINE], *cp; 24077c478bd9Sstevel@tonic-gate menu_t *mp; 24087c478bd9Sstevel@tonic-gate int line, entry, len, n; 24097c478bd9Sstevel@tonic-gate 24107c478bd9Sstevel@tonic-gate mp = s_calloc(1, sizeof (menu_t)); 24117c478bd9Sstevel@tonic-gate 24127c478bd9Sstevel@tonic-gate fp = fopen(menu_path, "r"); 24137c478bd9Sstevel@tonic-gate if (fp == NULL) { /* Let the caller handle this error */ 24147c478bd9Sstevel@tonic-gate return (mp); 24157c478bd9Sstevel@tonic-gate } 24167c478bd9Sstevel@tonic-gate 24177c478bd9Sstevel@tonic-gate 24187c478bd9Sstevel@tonic-gate /* Note: GRUB boot entry number starts with 0 */ 24197c478bd9Sstevel@tonic-gate line = LINE_INIT; 24207c478bd9Sstevel@tonic-gate entry = ENTRY_INIT; 24217c478bd9Sstevel@tonic-gate cp = buf; 24227c478bd9Sstevel@tonic-gate len = sizeof (buf); 24237c478bd9Sstevel@tonic-gate while (s_fgets(cp, len, fp) != NULL) { 24247c478bd9Sstevel@tonic-gate n = strlen(cp); 24257c478bd9Sstevel@tonic-gate if (cp[n - 1] == '\\') { 24267c478bd9Sstevel@tonic-gate len -= n - 1; 24277c478bd9Sstevel@tonic-gate assert(len >= 2); 24287c478bd9Sstevel@tonic-gate cp += n - 1; 24297c478bd9Sstevel@tonic-gate continue; 24307c478bd9Sstevel@tonic-gate } 24317c478bd9Sstevel@tonic-gate line_parser(mp, buf, &line, &entry); 24327c478bd9Sstevel@tonic-gate cp = buf; 24337c478bd9Sstevel@tonic-gate len = sizeof (buf); 24347c478bd9Sstevel@tonic-gate } 24357c478bd9Sstevel@tonic-gate 24367c478bd9Sstevel@tonic-gate if (fclose(fp) == EOF) { 24377c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, menu_path, strerror(errno)); 24387c478bd9Sstevel@tonic-gate } 24397c478bd9Sstevel@tonic-gate 24407c478bd9Sstevel@tonic-gate return (mp); 24417c478bd9Sstevel@tonic-gate } 24427c478bd9Sstevel@tonic-gate 24437c478bd9Sstevel@tonic-gate static error_t 24447c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title) 24457c478bd9Sstevel@tonic-gate { 24467c478bd9Sstevel@tonic-gate char *eq; 24477c478bd9Sstevel@tonic-gate char *opt_dup; 24487c478bd9Sstevel@tonic-gate int entryNum; 24497c478bd9Sstevel@tonic-gate 24507c478bd9Sstevel@tonic-gate assert(mp); 24517c478bd9Sstevel@tonic-gate assert(mp->start); 24527c478bd9Sstevel@tonic-gate assert(opt); 24537c478bd9Sstevel@tonic-gate 24547c478bd9Sstevel@tonic-gate opt_dup = s_strdup(opt); 24557c478bd9Sstevel@tonic-gate 24567c478bd9Sstevel@tonic-gate if (entry) 24577c478bd9Sstevel@tonic-gate *entry = ENTRY_INIT; 24587c478bd9Sstevel@tonic-gate if (title) 24597c478bd9Sstevel@tonic-gate *title = NULL; 24607c478bd9Sstevel@tonic-gate 24617c478bd9Sstevel@tonic-gate eq = strchr(opt_dup, '='); 24627c478bd9Sstevel@tonic-gate if (eq == NULL) { 24637c478bd9Sstevel@tonic-gate bam_error(INVALID_OPT, opt); 24647c478bd9Sstevel@tonic-gate free(opt_dup); 24657c478bd9Sstevel@tonic-gate return (BAM_ERROR); 24667c478bd9Sstevel@tonic-gate } 24677c478bd9Sstevel@tonic-gate 24687c478bd9Sstevel@tonic-gate *eq = '\0'; 24697c478bd9Sstevel@tonic-gate if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) { 24707c478bd9Sstevel@tonic-gate assert(mp->end); 24717c478bd9Sstevel@tonic-gate entryNum = s_strtol(eq + 1); 24727c478bd9Sstevel@tonic-gate if (entryNum < 0 || entryNum > mp->end->entryNum) { 24737c478bd9Sstevel@tonic-gate bam_error(INVALID_ENTRY, eq + 1); 24747c478bd9Sstevel@tonic-gate free(opt_dup); 24757c478bd9Sstevel@tonic-gate return (BAM_ERROR); 24767c478bd9Sstevel@tonic-gate } 24777c478bd9Sstevel@tonic-gate *entry = entryNum; 24787c478bd9Sstevel@tonic-gate } else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) { 24797c478bd9Sstevel@tonic-gate *title = opt + (eq - opt_dup) + 1; 24807c478bd9Sstevel@tonic-gate } else { 24817c478bd9Sstevel@tonic-gate bam_error(INVALID_OPT, opt); 24827c478bd9Sstevel@tonic-gate free(opt_dup); 24837c478bd9Sstevel@tonic-gate return (BAM_ERROR); 24847c478bd9Sstevel@tonic-gate } 24857c478bd9Sstevel@tonic-gate 24867c478bd9Sstevel@tonic-gate free(opt_dup); 24877c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 24887c478bd9Sstevel@tonic-gate } 24897c478bd9Sstevel@tonic-gate 24907c478bd9Sstevel@tonic-gate /* 24917c478bd9Sstevel@tonic-gate * If invoked with no titles/entries (opt == NULL) 24927c478bd9Sstevel@tonic-gate * only title lines in file are printed. 24937c478bd9Sstevel@tonic-gate * 24947c478bd9Sstevel@tonic-gate * If invoked with a title or entry #, all 24957c478bd9Sstevel@tonic-gate * lines in *every* matching entry are listed 24967c478bd9Sstevel@tonic-gate */ 24977c478bd9Sstevel@tonic-gate static error_t 24987c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt) 24997c478bd9Sstevel@tonic-gate { 25007c478bd9Sstevel@tonic-gate line_t *lp; 25017c478bd9Sstevel@tonic-gate int entry = ENTRY_INIT; 25027c478bd9Sstevel@tonic-gate int found; 25037c478bd9Sstevel@tonic-gate char *title = NULL; 25047c478bd9Sstevel@tonic-gate 25057c478bd9Sstevel@tonic-gate assert(mp); 25067c478bd9Sstevel@tonic-gate assert(menu_path); 25077c478bd9Sstevel@tonic-gate 25087c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 25097c478bd9Sstevel@tonic-gate bam_error(NO_MENU, menu_path); 25107c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25117c478bd9Sstevel@tonic-gate } 25127c478bd9Sstevel@tonic-gate 25137c478bd9Sstevel@tonic-gate if (opt != NULL) { 25147c478bd9Sstevel@tonic-gate if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) { 25157c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25167c478bd9Sstevel@tonic-gate } 25177c478bd9Sstevel@tonic-gate assert((entry != ENTRY_INIT) ^ (title != NULL)); 25187c478bd9Sstevel@tonic-gate } else { 25197c478bd9Sstevel@tonic-gate (void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0); 25207c478bd9Sstevel@tonic-gate (void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0); 25217c478bd9Sstevel@tonic-gate } 25227c478bd9Sstevel@tonic-gate 25237c478bd9Sstevel@tonic-gate found = 0; 25247c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 25257c478bd9Sstevel@tonic-gate if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY) 25267c478bd9Sstevel@tonic-gate continue; 25277c478bd9Sstevel@tonic-gate if (opt == NULL && lp->flags == BAM_TITLE) { 25287c478bd9Sstevel@tonic-gate bam_print(PRINT_TITLE, lp->entryNum, 25297c478bd9Sstevel@tonic-gate lp->arg); 25307c478bd9Sstevel@tonic-gate found = 1; 25317c478bd9Sstevel@tonic-gate continue; 25327c478bd9Sstevel@tonic-gate } 25337c478bd9Sstevel@tonic-gate if (entry != ENTRY_INIT && lp->entryNum == entry) { 25347c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 25357c478bd9Sstevel@tonic-gate found = 1; 25367c478bd9Sstevel@tonic-gate continue; 25377c478bd9Sstevel@tonic-gate } 25387c478bd9Sstevel@tonic-gate 25397c478bd9Sstevel@tonic-gate /* 25407c478bd9Sstevel@tonic-gate * We set the entry value here so that all lines 25417c478bd9Sstevel@tonic-gate * in entry get printed. If we subsequently match 25427c478bd9Sstevel@tonic-gate * title in other entries, all lines in those 25437c478bd9Sstevel@tonic-gate * entries get printed as well. 25447c478bd9Sstevel@tonic-gate */ 25457c478bd9Sstevel@tonic-gate if (title && lp->flags == BAM_TITLE && lp->arg && 25467c478bd9Sstevel@tonic-gate strncmp(title, lp->arg, strlen(title)) == 0) { 25477c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 25487c478bd9Sstevel@tonic-gate entry = lp->entryNum; 25497c478bd9Sstevel@tonic-gate found = 1; 25507c478bd9Sstevel@tonic-gate continue; 25517c478bd9Sstevel@tonic-gate } 25527c478bd9Sstevel@tonic-gate } 25537c478bd9Sstevel@tonic-gate 25547c478bd9Sstevel@tonic-gate if (!found) { 25557c478bd9Sstevel@tonic-gate bam_error(NO_MATCH_ENTRY); 25567c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25577c478bd9Sstevel@tonic-gate } 25587c478bd9Sstevel@tonic-gate 25597c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 25607c478bd9Sstevel@tonic-gate } 25617c478bd9Sstevel@tonic-gate 25627c478bd9Sstevel@tonic-gate static int 25637c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp, 25647c478bd9Sstevel@tonic-gate char *title, 25657c478bd9Sstevel@tonic-gate char *root, 25667c478bd9Sstevel@tonic-gate char *kernel, 25677c478bd9Sstevel@tonic-gate char *module) 25687c478bd9Sstevel@tonic-gate { 25697c478bd9Sstevel@tonic-gate int lineNum, entryNum; 25707c478bd9Sstevel@tonic-gate char linebuf[BAM_MAXLINE]; 25717c478bd9Sstevel@tonic-gate 25727c478bd9Sstevel@tonic-gate assert(mp); 25737c478bd9Sstevel@tonic-gate 25747c478bd9Sstevel@tonic-gate if (title == NULL) { 2575ee3b8144Sszhou title = "Solaris"; /* default to Solaris */ 25767c478bd9Sstevel@tonic-gate } 25777c478bd9Sstevel@tonic-gate if (kernel == NULL) { 25787c478bd9Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]); 25797c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25807c478bd9Sstevel@tonic-gate } 25817c478bd9Sstevel@tonic-gate if (module == NULL) { 25827c478bd9Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]); 25837c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25847c478bd9Sstevel@tonic-gate } 25857c478bd9Sstevel@tonic-gate 25867c478bd9Sstevel@tonic-gate if (mp->start) { 25877c478bd9Sstevel@tonic-gate lineNum = mp->end->lineNum; 25887c478bd9Sstevel@tonic-gate entryNum = mp->end->entryNum; 25897c478bd9Sstevel@tonic-gate } else { 25907c478bd9Sstevel@tonic-gate lineNum = LINE_INIT; 25917c478bd9Sstevel@tonic-gate entryNum = ENTRY_INIT; 25927c478bd9Sstevel@tonic-gate } 25937c478bd9Sstevel@tonic-gate 25947c478bd9Sstevel@tonic-gate /* 25957c478bd9Sstevel@tonic-gate * No separator for comment (HDR/FTR) commands 25967c478bd9Sstevel@tonic-gate * The syntax for comments is #<comment> 25977c478bd9Sstevel@tonic-gate */ 25987c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 25997c478bd9Sstevel@tonic-gate menu_cmds[COMMENT_CMD], BAM_HDR); 26008c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 26017c478bd9Sstevel@tonic-gate 26027c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 26037c478bd9Sstevel@tonic-gate menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title); 26048c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 26057c478bd9Sstevel@tonic-gate 26068c1b6884Sszhou if (root) { 26077c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 26087c478bd9Sstevel@tonic-gate menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root); 26098c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 26107c478bd9Sstevel@tonic-gate } 26117c478bd9Sstevel@tonic-gate 26127c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 26137c478bd9Sstevel@tonic-gate menu_cmds[KERNEL_CMD], menu_cmds[SEP_CMD], kernel); 26148c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 26157c478bd9Sstevel@tonic-gate 26167c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 26177c478bd9Sstevel@tonic-gate menu_cmds[MODULE_CMD], menu_cmds[SEP_CMD], module); 26188c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 26197c478bd9Sstevel@tonic-gate 26207c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 26217c478bd9Sstevel@tonic-gate menu_cmds[COMMENT_CMD], BAM_FTR); 26228c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 26237c478bd9Sstevel@tonic-gate 26247c478bd9Sstevel@tonic-gate return (entryNum); 26257c478bd9Sstevel@tonic-gate } 26267c478bd9Sstevel@tonic-gate 26277c478bd9Sstevel@tonic-gate static error_t 26287c478bd9Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum) 26297c478bd9Sstevel@tonic-gate { 26308c1b6884Sszhou line_t *lp, *freed; 26318c1b6884Sszhou entry_t *ent, *tmp; 26327c478bd9Sstevel@tonic-gate int deleted; 26337c478bd9Sstevel@tonic-gate 26347c478bd9Sstevel@tonic-gate assert(entryNum != ENTRY_INIT); 26357c478bd9Sstevel@tonic-gate 26368c1b6884Sszhou ent = mp->entries; 26378c1b6884Sszhou while (ent) { 26388c1b6884Sszhou lp = ent->start; 26398c1b6884Sszhou /* check entry number and make sure it's a bootadm entry */ 26408c1b6884Sszhou if (lp->flags != BAM_COMMENT || 26418c1b6884Sszhou strcmp(lp->arg, BAM_HDR) != 0 || 26428c1b6884Sszhou (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) { 26438c1b6884Sszhou ent = ent->next; 26447c478bd9Sstevel@tonic-gate continue; 26457c478bd9Sstevel@tonic-gate } 26467c478bd9Sstevel@tonic-gate 26478c1b6884Sszhou /* free the entry content */ 26488c1b6884Sszhou do { 26498c1b6884Sszhou freed = lp; 26508c1b6884Sszhou lp = lp->next; /* prev stays the same */ 26518c1b6884Sszhou unlink_line(mp, freed); 26528c1b6884Sszhou line_free(freed); 26538c1b6884Sszhou } while (freed != ent->end); 26547c478bd9Sstevel@tonic-gate 26558c1b6884Sszhou /* free the entry_t structure */ 26568c1b6884Sszhou tmp = ent; 26578c1b6884Sszhou ent = ent->next; 26588c1b6884Sszhou if (tmp->prev) 26598c1b6884Sszhou tmp->prev->next = ent; 26607c478bd9Sstevel@tonic-gate else 26618c1b6884Sszhou mp->entries = ent; 26628c1b6884Sszhou if (ent) 26638c1b6884Sszhou ent->prev = tmp->prev; 26647c478bd9Sstevel@tonic-gate deleted = 1; 26657c478bd9Sstevel@tonic-gate } 26667c478bd9Sstevel@tonic-gate 26677c478bd9Sstevel@tonic-gate if (!deleted && entryNum != ALL_ENTRIES) { 26687c478bd9Sstevel@tonic-gate bam_error(NO_BOOTADM_MATCH); 26697c478bd9Sstevel@tonic-gate return (BAM_ERROR); 26707c478bd9Sstevel@tonic-gate } 26717c478bd9Sstevel@tonic-gate 267240541d5dSvikram /* 267340541d5dSvikram * Now that we have deleted an entry, update 267440541d5dSvikram * the entry numbering and the default cmd. 267540541d5dSvikram */ 267640541d5dSvikram update_numbering(mp); 267740541d5dSvikram 26787c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 26797c478bd9Sstevel@tonic-gate } 26807c478bd9Sstevel@tonic-gate 26817c478bd9Sstevel@tonic-gate static error_t 26827c478bd9Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt) 26837c478bd9Sstevel@tonic-gate { 26847c478bd9Sstevel@tonic-gate assert(mp); 26857c478bd9Sstevel@tonic-gate assert(opt == NULL); 26867c478bd9Sstevel@tonic-gate 26877c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 26887c478bd9Sstevel@tonic-gate bam_print(EMPTY_FILE, menu_path); 26897c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 26907c478bd9Sstevel@tonic-gate } 26917c478bd9Sstevel@tonic-gate 26927c478bd9Sstevel@tonic-gate if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) { 26937c478bd9Sstevel@tonic-gate return (BAM_ERROR); 26947c478bd9Sstevel@tonic-gate } 26957c478bd9Sstevel@tonic-gate 26967c478bd9Sstevel@tonic-gate return (BAM_WRITE); 26977c478bd9Sstevel@tonic-gate } 26987c478bd9Sstevel@tonic-gate 26997c478bd9Sstevel@tonic-gate static FILE * 27008c1b6884Sszhou open_diskmap(char *root) 27017c478bd9Sstevel@tonic-gate { 27027c478bd9Sstevel@tonic-gate FILE *fp; 27037c478bd9Sstevel@tonic-gate char cmd[PATH_MAX]; 27047c478bd9Sstevel@tonic-gate 27057c478bd9Sstevel@tonic-gate /* make sure we have a map file */ 27067c478bd9Sstevel@tonic-gate fp = fopen(GRUBDISK_MAP, "r"); 27077c478bd9Sstevel@tonic-gate if (fp == NULL) { 27087c478bd9Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), 27098c1b6884Sszhou "%s%s > /dev/null", root, CREATE_DISKMAP); 27107c478bd9Sstevel@tonic-gate (void) system(cmd); 27117c478bd9Sstevel@tonic-gate fp = fopen(GRUBDISK_MAP, "r"); 27127c478bd9Sstevel@tonic-gate } 27137c478bd9Sstevel@tonic-gate return (fp); 27147c478bd9Sstevel@tonic-gate } 27157c478bd9Sstevel@tonic-gate 27167c478bd9Sstevel@tonic-gate #define SECTOR_SIZE 512 27177c478bd9Sstevel@tonic-gate 27187c478bd9Sstevel@tonic-gate static int 27197c478bd9Sstevel@tonic-gate get_partition(char *device) 27207c478bd9Sstevel@tonic-gate { 27217c478bd9Sstevel@tonic-gate int i, fd, is_pcfs, partno = -1; 27227c478bd9Sstevel@tonic-gate struct mboot *mboot; 27237c478bd9Sstevel@tonic-gate char boot_sect[SECTOR_SIZE]; 27247c478bd9Sstevel@tonic-gate char *wholedisk, *slice; 27257c478bd9Sstevel@tonic-gate 27267c478bd9Sstevel@tonic-gate /* form whole disk (p0) */ 27277c478bd9Sstevel@tonic-gate slice = device + strlen(device) - 2; 27287c478bd9Sstevel@tonic-gate is_pcfs = (*slice != 's'); 27297c478bd9Sstevel@tonic-gate if (!is_pcfs) 27307c478bd9Sstevel@tonic-gate *slice = '\0'; 27317c478bd9Sstevel@tonic-gate wholedisk = s_calloc(1, strlen(device) + 3); 27327c478bd9Sstevel@tonic-gate (void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device); 27337c478bd9Sstevel@tonic-gate if (!is_pcfs) 27347c478bd9Sstevel@tonic-gate *slice = 's'; 27357c478bd9Sstevel@tonic-gate 27367c478bd9Sstevel@tonic-gate /* read boot sector */ 27377c478bd9Sstevel@tonic-gate fd = open(wholedisk, O_RDONLY); 27387c478bd9Sstevel@tonic-gate free(wholedisk); 27397c478bd9Sstevel@tonic-gate if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 27407c478bd9Sstevel@tonic-gate return (partno); 27417c478bd9Sstevel@tonic-gate } 27427c478bd9Sstevel@tonic-gate (void) close(fd); 27437c478bd9Sstevel@tonic-gate 27447c478bd9Sstevel@tonic-gate /* parse fdisk table */ 27457c478bd9Sstevel@tonic-gate mboot = (struct mboot *)((void *)boot_sect); 27467c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 27477c478bd9Sstevel@tonic-gate struct ipart *part = 27487c478bd9Sstevel@tonic-gate (struct ipart *)(uintptr_t)mboot->parts + i; 27497c478bd9Sstevel@tonic-gate if (is_pcfs) { /* looking for solaris boot part */ 27507c478bd9Sstevel@tonic-gate if (part->systid == 0xbe) { 27517c478bd9Sstevel@tonic-gate partno = i; 27527c478bd9Sstevel@tonic-gate break; 27537c478bd9Sstevel@tonic-gate } 27547c478bd9Sstevel@tonic-gate } else { /* look for solaris partition, old and new */ 27557c478bd9Sstevel@tonic-gate if (part->systid == SUNIXOS || 27567c478bd9Sstevel@tonic-gate part->systid == SUNIXOS2) { 27577c478bd9Sstevel@tonic-gate partno = i; 27587c478bd9Sstevel@tonic-gate break; 27597c478bd9Sstevel@tonic-gate } 27607c478bd9Sstevel@tonic-gate } 27617c478bd9Sstevel@tonic-gate } 27627c478bd9Sstevel@tonic-gate return (partno); 27637c478bd9Sstevel@tonic-gate } 27647c478bd9Sstevel@tonic-gate 27657c478bd9Sstevel@tonic-gate static char * 27667c478bd9Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev) 27677c478bd9Sstevel@tonic-gate { 27687c478bd9Sstevel@tonic-gate char *grubdisk; /* (hd#,#,#) */ 27697c478bd9Sstevel@tonic-gate char *slice; 27707c478bd9Sstevel@tonic-gate char *grubhd; 27717c478bd9Sstevel@tonic-gate int fdiskpart; 27727c478bd9Sstevel@tonic-gate int found = 0; 27737c478bd9Sstevel@tonic-gate char *devname, *ctdname = strstr(rootdev, "dsk/"); 27747c478bd9Sstevel@tonic-gate char linebuf[PATH_MAX]; 27757c478bd9Sstevel@tonic-gate 27767c478bd9Sstevel@tonic-gate if (ctdname == NULL) 27777c478bd9Sstevel@tonic-gate return (NULL); 27787c478bd9Sstevel@tonic-gate 27797c478bd9Sstevel@tonic-gate ctdname += strlen("dsk/"); 27807c478bd9Sstevel@tonic-gate slice = strrchr(ctdname, 's'); 27817c478bd9Sstevel@tonic-gate if (slice) 27827c478bd9Sstevel@tonic-gate *slice = '\0'; 27837c478bd9Sstevel@tonic-gate 27847c478bd9Sstevel@tonic-gate rewind(fp); 27857c478bd9Sstevel@tonic-gate while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) { 27867c478bd9Sstevel@tonic-gate grubhd = strtok(linebuf, " \t\n"); 27877c478bd9Sstevel@tonic-gate if (grubhd) 27887c478bd9Sstevel@tonic-gate devname = strtok(NULL, " \t\n"); 27897c478bd9Sstevel@tonic-gate else 27907c478bd9Sstevel@tonic-gate devname = NULL; 27917c478bd9Sstevel@tonic-gate if (devname && strcmp(devname, ctdname) == 0) { 27927c478bd9Sstevel@tonic-gate found = 1; 27937c478bd9Sstevel@tonic-gate break; 27947c478bd9Sstevel@tonic-gate } 27957c478bd9Sstevel@tonic-gate } 27967c478bd9Sstevel@tonic-gate 27977c478bd9Sstevel@tonic-gate if (slice) 27987c478bd9Sstevel@tonic-gate *slice = 's'; 27997c478bd9Sstevel@tonic-gate 28007c478bd9Sstevel@tonic-gate if (found == 0) { 28017c478bd9Sstevel@tonic-gate if (bam_verbose) 28027c478bd9Sstevel@tonic-gate bam_print(DISKMAP_FAIL_NONFATAL, rootdev); 28037c478bd9Sstevel@tonic-gate grubhd = "0"; /* assume disk 0 if can't match */ 28047c478bd9Sstevel@tonic-gate } 28057c478bd9Sstevel@tonic-gate 28067c478bd9Sstevel@tonic-gate fdiskpart = get_partition(rootdev); 28077c478bd9Sstevel@tonic-gate if (fdiskpart == -1) 28087c478bd9Sstevel@tonic-gate return (NULL); 28097c478bd9Sstevel@tonic-gate 28107c478bd9Sstevel@tonic-gate grubdisk = s_calloc(1, 10); 28117c478bd9Sstevel@tonic-gate if (slice) { 28127c478bd9Sstevel@tonic-gate (void) snprintf(grubdisk, 10, "(hd%s,%d,%c)", 28137c478bd9Sstevel@tonic-gate grubhd, fdiskpart, slice[1] + 'a' - '0'); 28147c478bd9Sstevel@tonic-gate } else 28157c478bd9Sstevel@tonic-gate (void) snprintf(grubdisk, 10, "(hd%s,%d)", 28167c478bd9Sstevel@tonic-gate grubhd, fdiskpart); 28177c478bd9Sstevel@tonic-gate 28187c478bd9Sstevel@tonic-gate /* if root not on bootdev, change GRUB disk to 0 */ 28197c478bd9Sstevel@tonic-gate if (!on_bootdev) 28207c478bd9Sstevel@tonic-gate grubdisk[3] = '0'; 28217c478bd9Sstevel@tonic-gate return (grubdisk); 28227c478bd9Sstevel@tonic-gate } 28237c478bd9Sstevel@tonic-gate 2824ee3b8144Sszhou static char * 2825ee3b8144Sszhou get_title(char *rootdir) 28267c478bd9Sstevel@tonic-gate { 28277c478bd9Sstevel@tonic-gate static char title[80]; /* from /etc/release */ 28288c1b6884Sszhou char *cp = NULL, release[PATH_MAX]; 28297c478bd9Sstevel@tonic-gate FILE *fp; 28307c478bd9Sstevel@tonic-gate 28317c478bd9Sstevel@tonic-gate /* open the /etc/release file */ 28327c478bd9Sstevel@tonic-gate (void) snprintf(release, sizeof (release), "%s/etc/release", rootdir); 28337c478bd9Sstevel@tonic-gate 28347c478bd9Sstevel@tonic-gate fp = fopen(release, "r"); 28357c478bd9Sstevel@tonic-gate if (fp == NULL) 2836ee3b8144Sszhou return (NULL); 28377c478bd9Sstevel@tonic-gate 28387c478bd9Sstevel@tonic-gate while (s_fgets(title, sizeof (title), fp) != NULL) { 28397c478bd9Sstevel@tonic-gate cp = strstr(title, "Solaris"); 28407c478bd9Sstevel@tonic-gate if (cp) 28417c478bd9Sstevel@tonic-gate break; 28427c478bd9Sstevel@tonic-gate } 28437c478bd9Sstevel@tonic-gate (void) fclose(fp); 28448c1b6884Sszhou return (cp == NULL ? "Solaris" : cp); 28457c478bd9Sstevel@tonic-gate } 28467c478bd9Sstevel@tonic-gate 28477c478bd9Sstevel@tonic-gate static char * 28487c478bd9Sstevel@tonic-gate get_special(char *mountp) 28497c478bd9Sstevel@tonic-gate { 28507c478bd9Sstevel@tonic-gate FILE *mntfp; 28517c478bd9Sstevel@tonic-gate struct mnttab mp = {0}, mpref = {0}; 28527c478bd9Sstevel@tonic-gate 28537c478bd9Sstevel@tonic-gate mntfp = fopen(MNTTAB, "r"); 28547c478bd9Sstevel@tonic-gate if (mntfp == NULL) { 28557c478bd9Sstevel@tonic-gate return (0); 28567c478bd9Sstevel@tonic-gate } 28577c478bd9Sstevel@tonic-gate 28587c478bd9Sstevel@tonic-gate if (*mountp == '\0') 28597c478bd9Sstevel@tonic-gate mpref.mnt_mountp = "/"; 28607c478bd9Sstevel@tonic-gate else 28617c478bd9Sstevel@tonic-gate mpref.mnt_mountp = mountp; 28627c478bd9Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 28637c478bd9Sstevel@tonic-gate (void) fclose(mntfp); 28647c478bd9Sstevel@tonic-gate return (NULL); 28657c478bd9Sstevel@tonic-gate } 28667c478bd9Sstevel@tonic-gate (void) fclose(mntfp); 28677c478bd9Sstevel@tonic-gate 28687c478bd9Sstevel@tonic-gate return (s_strdup(mp.mnt_special)); 28697c478bd9Sstevel@tonic-gate } 28707c478bd9Sstevel@tonic-gate 28717c478bd9Sstevel@tonic-gate static char * 28727c478bd9Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev) 28737c478bd9Sstevel@tonic-gate { 28747c478bd9Sstevel@tonic-gate FILE *fp; 28757c478bd9Sstevel@tonic-gate char *grubdisk; 28767c478bd9Sstevel@tonic-gate 28777c478bd9Sstevel@tonic-gate /* translate /dev/dsk name to grub disk name */ 28788c1b6884Sszhou fp = open_diskmap(""); 28797c478bd9Sstevel@tonic-gate if (fp == NULL) { 28807c478bd9Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdisk); 28817c478bd9Sstevel@tonic-gate return (NULL); 28827c478bd9Sstevel@tonic-gate } 28837c478bd9Sstevel@tonic-gate grubdisk = get_grubdisk(osdisk, fp, on_bootdev); 28847c478bd9Sstevel@tonic-gate (void) fclose(fp); 28857c478bd9Sstevel@tonic-gate return (grubdisk); 28867c478bd9Sstevel@tonic-gate } 28877c478bd9Sstevel@tonic-gate 28887c478bd9Sstevel@tonic-gate /* 28897c478bd9Sstevel@tonic-gate * Check if root is on the boot device 28907c478bd9Sstevel@tonic-gate * Return 0 (false) on error 28917c478bd9Sstevel@tonic-gate */ 28927c478bd9Sstevel@tonic-gate static int 28937c478bd9Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp) 28947c478bd9Sstevel@tonic-gate { 28957c478bd9Sstevel@tonic-gate int ret; 28967c478bd9Sstevel@tonic-gate char *grubhd, *bootp, *special; 28977c478bd9Sstevel@tonic-gate 28987c478bd9Sstevel@tonic-gate special = get_special(menu_root); 28997c478bd9Sstevel@tonic-gate if (special == NULL) 29007c478bd9Sstevel@tonic-gate return (0); 29017c478bd9Sstevel@tonic-gate bootp = strstr(special, "p0:boot"); 29027c478bd9Sstevel@tonic-gate if (bootp) 29037c478bd9Sstevel@tonic-gate *bootp = '\0'; 29047c478bd9Sstevel@tonic-gate grubhd = get_grubdisk(special, fp, 1); 29057c478bd9Sstevel@tonic-gate free(special); 29067c478bd9Sstevel@tonic-gate 29077c478bd9Sstevel@tonic-gate if (grubhd == NULL) 29087c478bd9Sstevel@tonic-gate return (0); 29097c478bd9Sstevel@tonic-gate ret = grubhd[3] == '0'; 29107c478bd9Sstevel@tonic-gate free(grubhd); 29117c478bd9Sstevel@tonic-gate return (ret); 29127c478bd9Sstevel@tonic-gate } 29137c478bd9Sstevel@tonic-gate 29148c1b6884Sszhou /* 29158c1b6884Sszhou * look for matching bootadm entry with specified parameters 29168c1b6884Sszhou * Here are the rules (based on existing usage): 29178c1b6884Sszhou * - If title is specified, match on title only 29188c1b6884Sszhou * - Else, match on grubdisk and module (don't care about kernel line). 29198c1b6884Sszhou * note that, if root_opt is non-zero, the absence of root line is 29208c1b6884Sszhou * considered a match. 29218c1b6884Sszhou */ 29228c1b6884Sszhou static entry_t * 29238c1b6884Sszhou find_boot_entry(menu_t *mp, char *title, char *root, char *module, 29248c1b6884Sszhou int root_opt, int *entry_num) 29258c1b6884Sszhou { 29268c1b6884Sszhou int i; 29278c1b6884Sszhou line_t *lp; 29288c1b6884Sszhou entry_t *ent; 29298c1b6884Sszhou 29308c1b6884Sszhou /* find matching entry */ 29318c1b6884Sszhou for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) { 29328c1b6884Sszhou lp = ent->start; 29338c1b6884Sszhou 29348c1b6884Sszhou /* first line of entry must be bootadm comment */ 29358c1b6884Sszhou lp = ent->start; 29368c1b6884Sszhou if (lp->flags != BAM_COMMENT || strcmp(lp->arg, BAM_HDR) != 0) { 29378c1b6884Sszhou continue; 29388c1b6884Sszhou } 29398c1b6884Sszhou 29408c1b6884Sszhou /* advance to title line */ 29418c1b6884Sszhou lp = lp->next; 29428c1b6884Sszhou if (title) { 29438c1b6884Sszhou if (lp->flags == BAM_TITLE && lp->arg && 29448c1b6884Sszhou strcmp(lp->arg, title) == 0) 29458c1b6884Sszhou break; 29468c1b6884Sszhou continue; /* check title only */ 29478c1b6884Sszhou } 29488c1b6884Sszhou 29498c1b6884Sszhou lp = lp->next; /* advance to root line */ 29508c1b6884Sszhou if (lp == NULL || strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) { 29518c1b6884Sszhou /* root command found, match grub disk */ 29528c1b6884Sszhou if (strcmp(lp->arg, root) != 0) { 29538c1b6884Sszhou continue; 29548c1b6884Sszhou } 29558c1b6884Sszhou lp = lp->next; /* advance to kernel line */ 29568c1b6884Sszhou } else { 29578c1b6884Sszhou /* no root command, see if root is optional */ 29588c1b6884Sszhou if (root_opt == 0) { 29598c1b6884Sszhou continue; 29608c1b6884Sszhou } 29618c1b6884Sszhou } 29628c1b6884Sszhou 29638c1b6884Sszhou if (lp == NULL || lp->next == NULL) { 29648c1b6884Sszhou continue; 29658c1b6884Sszhou } 29668c1b6884Sszhou 29678c1b6884Sszhou /* check for matching module entry (failsafe or normal) */ 29688c1b6884Sszhou lp = lp->next; /* advance to module line */ 29698c1b6884Sszhou if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) != 0 || 29708c1b6884Sszhou strcmp(lp->arg, module) != 0) { 29718c1b6884Sszhou continue; 29728c1b6884Sszhou } 29738c1b6884Sszhou break; /* match found */ 29748c1b6884Sszhou } 29758c1b6884Sszhou 29768c1b6884Sszhou *entry_num = i; 29778c1b6884Sszhou return (ent); 29788c1b6884Sszhou } 29798c1b6884Sszhou 29808c1b6884Sszhou static int 29818c1b6884Sszhou update_boot_entry(menu_t *mp, char *title, char *root, char *kernel, 29828c1b6884Sszhou char *module, int root_opt) 29838c1b6884Sszhou { 29848c1b6884Sszhou int i; 29858c1b6884Sszhou entry_t *ent; 29868c1b6884Sszhou line_t *lp; 29878c1b6884Sszhou char linebuf[BAM_MAXLINE]; 29888c1b6884Sszhou 29898c1b6884Sszhou /* note: don't match on title, it's updated on upgrade */ 29908c1b6884Sszhou ent = find_boot_entry(mp, NULL, root, module, root_opt, &i); 29918c1b6884Sszhou if (ent == NULL) 29928c1b6884Sszhou return (add_boot_entry(mp, title, root_opt ? NULL : root, 29938c1b6884Sszhou kernel, module)); 29948c1b6884Sszhou 29958c1b6884Sszhou /* replace title of exiting entry and delete root line */ 29968c1b6884Sszhou lp = ent->start; 29978c1b6884Sszhou lp = lp->next; /* title line */ 29988c1b6884Sszhou (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 29998c1b6884Sszhou menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title); 30008c1b6884Sszhou free(lp->arg); 30018c1b6884Sszhou free(lp->line); 30028c1b6884Sszhou lp->arg = s_strdup(title); 30038c1b6884Sszhou lp->line = s_strdup(linebuf); 30048c1b6884Sszhou 30058c1b6884Sszhou lp = lp->next; /* root line */ 30068c1b6884Sszhou if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) { 30078c1b6884Sszhou if (root_opt) { /* root line not needed */ 30088c1b6884Sszhou line_t *tmp = lp; 30098c1b6884Sszhou lp = lp->next; 30108c1b6884Sszhou unlink_line(mp, tmp); 30118c1b6884Sszhou line_free(tmp); 30128c1b6884Sszhou } else 30138c1b6884Sszhou lp = lp->next; 30148c1b6884Sszhou } 30158c1b6884Sszhou return (i); 30168c1b6884Sszhou } 30178c1b6884Sszhou 30187c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 30197c478bd9Sstevel@tonic-gate static error_t 30207c478bd9Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt) 30217c478bd9Sstevel@tonic-gate { 30227c478bd9Sstevel@tonic-gate FILE *fp; 30237c478bd9Sstevel@tonic-gate int entry; 30247c478bd9Sstevel@tonic-gate char *grubdisk, *title, *osdev, *osroot; 30258c1b6884Sszhou struct stat sbuf; 30268c1b6884Sszhou char failsafe[256]; 30277c478bd9Sstevel@tonic-gate 30287c478bd9Sstevel@tonic-gate assert(mp); 30297c478bd9Sstevel@tonic-gate assert(opt); 30307c478bd9Sstevel@tonic-gate 30317c478bd9Sstevel@tonic-gate osdev = strtok(opt, ","); 30327c478bd9Sstevel@tonic-gate osroot = strtok(NULL, ","); 30337c478bd9Sstevel@tonic-gate if (osroot == NULL) 30347c478bd9Sstevel@tonic-gate osroot = menu_root; 30357c478bd9Sstevel@tonic-gate title = get_title(osroot); 30367c478bd9Sstevel@tonic-gate 30377c478bd9Sstevel@tonic-gate /* translate /dev/dsk name to grub disk name */ 30388c1b6884Sszhou fp = open_diskmap(osroot); 30397c478bd9Sstevel@tonic-gate if (fp == NULL) { 30407c478bd9Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdev); 30417c478bd9Sstevel@tonic-gate return (BAM_ERROR); 30427c478bd9Sstevel@tonic-gate } 30437c478bd9Sstevel@tonic-gate grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp)); 30447c478bd9Sstevel@tonic-gate (void) fclose(fp); 30457c478bd9Sstevel@tonic-gate if (grubdisk == NULL) { 30467c478bd9Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdev); 30477c478bd9Sstevel@tonic-gate return (BAM_ERROR); 30487c478bd9Sstevel@tonic-gate } 30497c478bd9Sstevel@tonic-gate 30507c478bd9Sstevel@tonic-gate /* add the entry for normal Solaris */ 30518c1b6884Sszhou entry = update_boot_entry(mp, title, grubdisk, 30527c478bd9Sstevel@tonic-gate "/platform/i86pc/multiboot", 30538c1b6884Sszhou "/platform/i86pc/boot_archive", 30548c1b6884Sszhou osroot == menu_root); 30557c478bd9Sstevel@tonic-gate 30567c478bd9Sstevel@tonic-gate /* add the entry for failsafe archive */ 30578c1b6884Sszhou (void) snprintf(failsafe, sizeof (failsafe), 30588c1b6884Sszhou "%s/boot/x86.miniroot-safe", osroot); 30598c1b6884Sszhou if (stat(failsafe, &sbuf) == 0) 30608c1b6884Sszhou (void) update_boot_entry(mp, "Solaris failsafe", grubdisk, 30617c478bd9Sstevel@tonic-gate "/boot/multiboot kernel/unix -s", 30628c1b6884Sszhou "/boot/x86.miniroot-safe", 30638c1b6884Sszhou osroot == menu_root); 30647c478bd9Sstevel@tonic-gate free(grubdisk); 30657c478bd9Sstevel@tonic-gate 30667c478bd9Sstevel@tonic-gate if (entry == BAM_ERROR) { 30677c478bd9Sstevel@tonic-gate return (BAM_ERROR); 30687c478bd9Sstevel@tonic-gate } 30697c478bd9Sstevel@tonic-gate (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 30707c478bd9Sstevel@tonic-gate return (BAM_WRITE); 30717c478bd9Sstevel@tonic-gate } 30727c478bd9Sstevel@tonic-gate 3073b610f78eSvikram static char * 3074b610f78eSvikram read_grub_root(void) 3075b610f78eSvikram { 3076b610f78eSvikram FILE *fp; 3077b610f78eSvikram struct stat sb; 3078b610f78eSvikram char buf[BAM_MAXLINE]; 3079b610f78eSvikram char *rootstr; 3080b610f78eSvikram 3081b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 3082b610f78eSvikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 3083b610f78eSvikram return (NULL); 3084b610f78eSvikram } 3085b610f78eSvikram 3086b610f78eSvikram if (stat(GRUB_root, &sb) != 0) { 3087b610f78eSvikram bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno)); 3088b610f78eSvikram return (NULL); 3089b610f78eSvikram } 3090b610f78eSvikram 3091b610f78eSvikram fp = fopen(GRUB_root, "r"); 3092b610f78eSvikram if (fp == NULL) { 3093b610f78eSvikram bam_error(OPEN_FAIL, GRUB_root, strerror(errno)); 3094b610f78eSvikram return (NULL); 3095b610f78eSvikram } 3096b610f78eSvikram 3097b610f78eSvikram if (s_fgets(buf, sizeof (buf), fp) == NULL) { 3098b610f78eSvikram bam_error(EMPTY_FILE, GRUB_root, strerror(errno)); 3099b610f78eSvikram (void) fclose(fp); 3100b610f78eSvikram return (NULL); 3101b610f78eSvikram } 3102b610f78eSvikram 3103b610f78eSvikram /* 3104b610f78eSvikram * Copy buf here as check below may trash the buffer 3105b610f78eSvikram */ 3106b610f78eSvikram rootstr = s_strdup(buf); 3107b610f78eSvikram 3108b610f78eSvikram if (s_fgets(buf, sizeof (buf), fp) != NULL) { 3109b610f78eSvikram bam_error(BAD_ROOT_FILE, GRUB_root); 3110b610f78eSvikram free(rootstr); 3111b610f78eSvikram rootstr = NULL; 3112b610f78eSvikram } 3113b610f78eSvikram 3114b610f78eSvikram (void) fclose(fp); 3115b610f78eSvikram 3116b610f78eSvikram return (rootstr); 3117b610f78eSvikram } 3118b610f78eSvikram 31198c1b6884Sszhou static void 31208c1b6884Sszhou save_default_entry(menu_t *mp) 31218c1b6884Sszhou { 31228c1b6884Sszhou int lineNum, entryNum; 31238c1b6884Sszhou int entry = 0; /* default is 0 */ 31248c1b6884Sszhou char linebuf[BAM_MAXLINE]; 31258c1b6884Sszhou line_t *lp = mp->curdefault; 31268c1b6884Sszhou 31278c1b6884Sszhou if (lp) 31288c1b6884Sszhou entry = s_strtol(lp->arg); 31298c1b6884Sszhou 31308c1b6884Sszhou (void) snprintf(linebuf, sizeof (linebuf), "#%s%d", BAM_OLDDEF, entry); 31318c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 31328c1b6884Sszhou } 31338c1b6884Sszhou 31348c1b6884Sszhou static void 31358c1b6884Sszhou restore_default_entry(menu_t *mp) 31368c1b6884Sszhou { 31378c1b6884Sszhou int entry; 31388c1b6884Sszhou char *str; 31398c1b6884Sszhou line_t *lp = mp->olddefault; 31408c1b6884Sszhou 31418c1b6884Sszhou if (lp == NULL) 31428c1b6884Sszhou return; /* nothing to restore */ 31438c1b6884Sszhou 31448c1b6884Sszhou str = lp->arg + strlen(BAM_OLDDEF); 31458c1b6884Sszhou entry = s_strtol(str); 31468c1b6884Sszhou (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 31478c1b6884Sszhou 31488c1b6884Sszhou /* delete saved old default line */ 31498c1b6884Sszhou mp->olddefault = NULL; 31508c1b6884Sszhou unlink_line(mp, lp); 31518c1b6884Sszhou line_free(lp); 31528c1b6884Sszhou } 31538c1b6884Sszhou 31547c478bd9Sstevel@tonic-gate /* 31557c478bd9Sstevel@tonic-gate * This function is for supporting reboot with args. 31567c478bd9Sstevel@tonic-gate * The opt value can be: 31577c478bd9Sstevel@tonic-gate * NULL delete temp entry, if present 31587c478bd9Sstevel@tonic-gate * entry=# switches default entry to 1 31597c478bd9Sstevel@tonic-gate * else treated as boot-args and setup a temperary menu entry 31607c478bd9Sstevel@tonic-gate * and make it the default 31617c478bd9Sstevel@tonic-gate */ 31627c478bd9Sstevel@tonic-gate #define REBOOT_TITLE "Solaris_reboot_transient" 31637c478bd9Sstevel@tonic-gate 31648c1b6884Sszhou /*ARGSUSED*/ 31657c478bd9Sstevel@tonic-gate static error_t 31667c478bd9Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt) 31677c478bd9Sstevel@tonic-gate { 31687c478bd9Sstevel@tonic-gate int entry; 31697c478bd9Sstevel@tonic-gate char *grubdisk, *rootdev; 31707c478bd9Sstevel@tonic-gate char kernbuf[1024]; 3171b610f78eSvikram struct stat sb; 31727c478bd9Sstevel@tonic-gate 31737c478bd9Sstevel@tonic-gate assert(mp); 31747c478bd9Sstevel@tonic-gate 31758c1b6884Sszhou /* If no option, delete exiting reboot menu entry */ 31768c1b6884Sszhou if (opt == NULL) { 31778c1b6884Sszhou entry_t *ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL, 31788c1b6884Sszhou 0, &entry); 31798c1b6884Sszhou if (ent == NULL) /* not found is ok */ 31808c1b6884Sszhou return (BAM_SUCCESS); 31818c1b6884Sszhou (void) do_delete(mp, entry); 31828c1b6884Sszhou restore_default_entry(mp); 31838c1b6884Sszhou return (BAM_WRITE); 31848c1b6884Sszhou } 31858c1b6884Sszhou 31868c1b6884Sszhou /* if entry= is specified, set the default entry */ 31878c1b6884Sszhou if (strncmp(opt, "entry=", strlen("entry=")) == 0 && 31887c478bd9Sstevel@tonic-gate selector(mp, opt, &entry, NULL) == BAM_SUCCESS) { 31897c478bd9Sstevel@tonic-gate /* this is entry=# option */ 31907c478bd9Sstevel@tonic-gate return (set_global(mp, menu_cmds[DEFAULT_CMD], entry)); 31917c478bd9Sstevel@tonic-gate } 31927c478bd9Sstevel@tonic-gate 31937c478bd9Sstevel@tonic-gate /* 31947c478bd9Sstevel@tonic-gate * add a new menu entry base on opt and make it the default 3195b610f78eSvikram */ 3196b610f78eSvikram grubdisk = NULL; 3197b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 3198b610f78eSvikram /* 31997c478bd9Sstevel@tonic-gate * 1. First get root disk name from mnttab 32007c478bd9Sstevel@tonic-gate * 2. Translate disk name to grub name 32017c478bd9Sstevel@tonic-gate * 3. Add the new menu entry 32027c478bd9Sstevel@tonic-gate */ 32037c478bd9Sstevel@tonic-gate rootdev = get_special("/"); 32047c478bd9Sstevel@tonic-gate if (rootdev) { 32057c478bd9Sstevel@tonic-gate grubdisk = os_to_grubdisk(rootdev, 1); 32067c478bd9Sstevel@tonic-gate free(rootdev); 32077c478bd9Sstevel@tonic-gate } 3208b610f78eSvikram } else { 3209b610f78eSvikram /* 3210b610f78eSvikram * This is an LU BE. The GRUB_root file 3211b610f78eSvikram * contains entry for GRUB's "root" cmd. 3212b610f78eSvikram */ 3213b610f78eSvikram grubdisk = read_grub_root(); 3214b610f78eSvikram } 32157c478bd9Sstevel@tonic-gate if (grubdisk == NULL) { 3216b610f78eSvikram bam_error(REBOOT_WITH_ARGS_FAILED); 32177c478bd9Sstevel@tonic-gate return (BAM_ERROR); 32187c478bd9Sstevel@tonic-gate } 32197c478bd9Sstevel@tonic-gate 32207c478bd9Sstevel@tonic-gate /* add an entry for Solaris reboot */ 32217c478bd9Sstevel@tonic-gate (void) snprintf(kernbuf, sizeof (kernbuf), 32227c478bd9Sstevel@tonic-gate "/platform/i86pc/multiboot %s", opt); 32237c478bd9Sstevel@tonic-gate entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf, 32247c478bd9Sstevel@tonic-gate "/platform/i86pc/boot_archive"); 32257c478bd9Sstevel@tonic-gate free(grubdisk); 32267c478bd9Sstevel@tonic-gate 32277c478bd9Sstevel@tonic-gate if (entry == BAM_ERROR) { 3228b610f78eSvikram bam_error(REBOOT_WITH_ARGS_FAILED); 32297c478bd9Sstevel@tonic-gate return (BAM_ERROR); 32307c478bd9Sstevel@tonic-gate } 32318c1b6884Sszhou 32328c1b6884Sszhou save_default_entry(mp); 32337c478bd9Sstevel@tonic-gate (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 32347c478bd9Sstevel@tonic-gate return (BAM_WRITE); 32357c478bd9Sstevel@tonic-gate } 32367c478bd9Sstevel@tonic-gate 32377c478bd9Sstevel@tonic-gate static error_t 32387c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val) 32397c478bd9Sstevel@tonic-gate { 32407c478bd9Sstevel@tonic-gate line_t *lp, *found, *last; 32417c478bd9Sstevel@tonic-gate char *cp, *str; 32427c478bd9Sstevel@tonic-gate char prefix[BAM_MAXLINE]; 32437c478bd9Sstevel@tonic-gate size_t len; 32447c478bd9Sstevel@tonic-gate 32457c478bd9Sstevel@tonic-gate assert(mp); 32467c478bd9Sstevel@tonic-gate assert(globalcmd); 32477c478bd9Sstevel@tonic-gate 3248b610f78eSvikram if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) { 3249b610f78eSvikram if (val < 0 || mp->end == NULL || val > mp->end->entryNum) { 3250b610f78eSvikram (void) snprintf(prefix, sizeof (prefix), "%d", val); 3251b610f78eSvikram bam_error(INVALID_ENTRY, prefix); 3252b610f78eSvikram return (BAM_ERROR); 3253b610f78eSvikram } 3254b610f78eSvikram } 3255b610f78eSvikram 32567c478bd9Sstevel@tonic-gate found = last = NULL; 32577c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 32587c478bd9Sstevel@tonic-gate if (lp->flags != BAM_GLOBAL) 32597c478bd9Sstevel@tonic-gate continue; 32607c478bd9Sstevel@tonic-gate 32617c478bd9Sstevel@tonic-gate last = lp; /* track the last global found */ 32627c478bd9Sstevel@tonic-gate 32637c478bd9Sstevel@tonic-gate if (lp->cmd == NULL) { 32647c478bd9Sstevel@tonic-gate bam_error(NO_CMD, lp->lineNum); 32657c478bd9Sstevel@tonic-gate continue; 32667c478bd9Sstevel@tonic-gate } 32677c478bd9Sstevel@tonic-gate if (strcmp(globalcmd, lp->cmd) != 0) 32687c478bd9Sstevel@tonic-gate continue; 32697c478bd9Sstevel@tonic-gate 32707c478bd9Sstevel@tonic-gate if (found) { 32717c478bd9Sstevel@tonic-gate bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 32727c478bd9Sstevel@tonic-gate } 32737c478bd9Sstevel@tonic-gate found = lp; 32747c478bd9Sstevel@tonic-gate } 32757c478bd9Sstevel@tonic-gate 32767c478bd9Sstevel@tonic-gate if (found == NULL) { 32777c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 32787c478bd9Sstevel@tonic-gate if (last == NULL) { 32797c478bd9Sstevel@tonic-gate lp->next = mp->start; 32807c478bd9Sstevel@tonic-gate mp->start = lp; 32817c478bd9Sstevel@tonic-gate mp->end = (mp->end) ? mp->end : lp; 32827c478bd9Sstevel@tonic-gate } else { 32837c478bd9Sstevel@tonic-gate lp->next = last->next; 32847c478bd9Sstevel@tonic-gate last->next = lp; 32857c478bd9Sstevel@tonic-gate if (lp->next == NULL) 32867c478bd9Sstevel@tonic-gate mp->end = lp; 32877c478bd9Sstevel@tonic-gate } 32887c478bd9Sstevel@tonic-gate lp->flags = BAM_GLOBAL; /* other fields not needed for writes */ 32897c478bd9Sstevel@tonic-gate len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]); 32907c478bd9Sstevel@tonic-gate len += 10; /* val < 10 digits */ 32917c478bd9Sstevel@tonic-gate lp->line = s_calloc(1, len); 32927c478bd9Sstevel@tonic-gate (void) snprintf(lp->line, len, "%s%s%d", 32937c478bd9Sstevel@tonic-gate globalcmd, menu_cmds[SEP_CMD], val); 32947c478bd9Sstevel@tonic-gate return (BAM_WRITE); 32957c478bd9Sstevel@tonic-gate } 32967c478bd9Sstevel@tonic-gate 32977c478bd9Sstevel@tonic-gate /* 32987c478bd9Sstevel@tonic-gate * We are changing an existing entry. Retain any prefix whitespace, 32997c478bd9Sstevel@tonic-gate * but overwrite everything else. This preserves tabs added for 33007c478bd9Sstevel@tonic-gate * readability. 33017c478bd9Sstevel@tonic-gate */ 33027c478bd9Sstevel@tonic-gate str = found->line; 33037c478bd9Sstevel@tonic-gate cp = prefix; 33047c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 33057c478bd9Sstevel@tonic-gate *(cp++) = *(str++); 33067c478bd9Sstevel@tonic-gate *cp = '\0'; /* Terminate prefix */ 33077c478bd9Sstevel@tonic-gate len = strlen(prefix) + strlen(globalcmd); 33087c478bd9Sstevel@tonic-gate len += strlen(menu_cmds[SEP_CMD]) + 10; 33097c478bd9Sstevel@tonic-gate 33107c478bd9Sstevel@tonic-gate free(found->line); 33117c478bd9Sstevel@tonic-gate found->line = s_calloc(1, len); 33127c478bd9Sstevel@tonic-gate (void) snprintf(found->line, len, 33137c478bd9Sstevel@tonic-gate "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val); 33147c478bd9Sstevel@tonic-gate 33157c478bd9Sstevel@tonic-gate return (BAM_WRITE); /* need a write to menu */ 33167c478bd9Sstevel@tonic-gate } 33177c478bd9Sstevel@tonic-gate 33187c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 33197c478bd9Sstevel@tonic-gate static error_t 33207c478bd9Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt) 33217c478bd9Sstevel@tonic-gate { 33227c478bd9Sstevel@tonic-gate int optnum, optval; 33237c478bd9Sstevel@tonic-gate char *val; 33247c478bd9Sstevel@tonic-gate 33257c478bd9Sstevel@tonic-gate assert(mp); 33267c478bd9Sstevel@tonic-gate assert(opt); 33277c478bd9Sstevel@tonic-gate 33287c478bd9Sstevel@tonic-gate val = strchr(opt, '='); 33297c478bd9Sstevel@tonic-gate if (val == NULL) { 33307c478bd9Sstevel@tonic-gate bam_error(INVALID_ENTRY, opt); 33317c478bd9Sstevel@tonic-gate return (BAM_ERROR); 33327c478bd9Sstevel@tonic-gate } 33337c478bd9Sstevel@tonic-gate 33347c478bd9Sstevel@tonic-gate *val = '\0'; 33357c478bd9Sstevel@tonic-gate if (strcmp(opt, "default") == 0) { 33367c478bd9Sstevel@tonic-gate optnum = DEFAULT_CMD; 33377c478bd9Sstevel@tonic-gate } else if (strcmp(opt, "timeout") == 0) { 33387c478bd9Sstevel@tonic-gate optnum = TIMEOUT_CMD; 33397c478bd9Sstevel@tonic-gate } else { 33407c478bd9Sstevel@tonic-gate bam_error(INVALID_ENTRY, opt); 33417c478bd9Sstevel@tonic-gate return (BAM_ERROR); 33427c478bd9Sstevel@tonic-gate } 33437c478bd9Sstevel@tonic-gate 33447c478bd9Sstevel@tonic-gate optval = s_strtol(val + 1); 33457c478bd9Sstevel@tonic-gate *val = '='; 33467c478bd9Sstevel@tonic-gate return (set_global(mp, menu_cmds[optnum], optval)); 33477c478bd9Sstevel@tonic-gate } 33487c478bd9Sstevel@tonic-gate 33497c478bd9Sstevel@tonic-gate /* 33507c478bd9Sstevel@tonic-gate * The quiet argument suppresses messages. This is used 33517c478bd9Sstevel@tonic-gate * when invoked in the context of other commands (e.g. list_entry) 33527c478bd9Sstevel@tonic-gate */ 33537c478bd9Sstevel@tonic-gate static error_t 33547c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet) 33557c478bd9Sstevel@tonic-gate { 33567c478bd9Sstevel@tonic-gate line_t *lp; 33577c478bd9Sstevel@tonic-gate char *arg; 33587c478bd9Sstevel@tonic-gate int done, ret = BAM_SUCCESS; 33597c478bd9Sstevel@tonic-gate 33607c478bd9Sstevel@tonic-gate assert(mp); 33617c478bd9Sstevel@tonic-gate assert(menu_path); 33627c478bd9Sstevel@tonic-gate assert(globalcmd); 33637c478bd9Sstevel@tonic-gate 33647c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 33657c478bd9Sstevel@tonic-gate if (!quiet) 33667c478bd9Sstevel@tonic-gate bam_error(NO_MENU, menu_path); 33677c478bd9Sstevel@tonic-gate return (BAM_ERROR); 33687c478bd9Sstevel@tonic-gate } 33697c478bd9Sstevel@tonic-gate 33707c478bd9Sstevel@tonic-gate done = 0; 33717c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 33727c478bd9Sstevel@tonic-gate if (lp->flags != BAM_GLOBAL) 33737c478bd9Sstevel@tonic-gate continue; 33747c478bd9Sstevel@tonic-gate 33757c478bd9Sstevel@tonic-gate if (lp->cmd == NULL) { 33767c478bd9Sstevel@tonic-gate if (!quiet) 33777c478bd9Sstevel@tonic-gate bam_error(NO_CMD, lp->lineNum); 33787c478bd9Sstevel@tonic-gate continue; 33797c478bd9Sstevel@tonic-gate } 33807c478bd9Sstevel@tonic-gate 33817c478bd9Sstevel@tonic-gate if (strcmp(globalcmd, lp->cmd) != 0) 33827c478bd9Sstevel@tonic-gate continue; 33837c478bd9Sstevel@tonic-gate 33847c478bd9Sstevel@tonic-gate /* Found global. Check for duplicates */ 33857c478bd9Sstevel@tonic-gate if (done && !quiet) { 33867c478bd9Sstevel@tonic-gate bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 33877c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 33887c478bd9Sstevel@tonic-gate } 33897c478bd9Sstevel@tonic-gate 33907c478bd9Sstevel@tonic-gate arg = lp->arg ? lp->arg : ""; 33917c478bd9Sstevel@tonic-gate bam_print(GLOBAL_CMD, globalcmd, arg); 33927c478bd9Sstevel@tonic-gate done = 1; 33937c478bd9Sstevel@tonic-gate } 33947c478bd9Sstevel@tonic-gate 33957c478bd9Sstevel@tonic-gate if (!done && bam_verbose) 33967c478bd9Sstevel@tonic-gate bam_print(NO_ENTRY, globalcmd); 33977c478bd9Sstevel@tonic-gate 33987c478bd9Sstevel@tonic-gate return (ret); 33997c478bd9Sstevel@tonic-gate } 34007c478bd9Sstevel@tonic-gate 34017c478bd9Sstevel@tonic-gate static error_t 34027c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp) 34037c478bd9Sstevel@tonic-gate { 34047c478bd9Sstevel@tonic-gate return (list2file(root, MENU_TMP, GRUB_MENU, mp->start)); 34057c478bd9Sstevel@tonic-gate } 34067c478bd9Sstevel@tonic-gate 34077c478bd9Sstevel@tonic-gate static void 34087c478bd9Sstevel@tonic-gate line_free(line_t *lp) 34097c478bd9Sstevel@tonic-gate { 34107c478bd9Sstevel@tonic-gate if (lp == NULL) 34117c478bd9Sstevel@tonic-gate return; 34127c478bd9Sstevel@tonic-gate 34137c478bd9Sstevel@tonic-gate if (lp->cmd) 34147c478bd9Sstevel@tonic-gate free(lp->cmd); 34157c478bd9Sstevel@tonic-gate if (lp->sep) 34167c478bd9Sstevel@tonic-gate free(lp->sep); 34177c478bd9Sstevel@tonic-gate if (lp->arg) 34187c478bd9Sstevel@tonic-gate free(lp->arg); 34197c478bd9Sstevel@tonic-gate if (lp->line) 34207c478bd9Sstevel@tonic-gate free(lp->line); 34217c478bd9Sstevel@tonic-gate free(lp); 34227c478bd9Sstevel@tonic-gate } 34237c478bd9Sstevel@tonic-gate 34247c478bd9Sstevel@tonic-gate static void 34257c478bd9Sstevel@tonic-gate linelist_free(line_t *start) 34267c478bd9Sstevel@tonic-gate { 34277c478bd9Sstevel@tonic-gate line_t *lp; 34287c478bd9Sstevel@tonic-gate 34297c478bd9Sstevel@tonic-gate while (start) { 34307c478bd9Sstevel@tonic-gate lp = start; 34317c478bd9Sstevel@tonic-gate start = start->next; 34327c478bd9Sstevel@tonic-gate line_free(lp); 34337c478bd9Sstevel@tonic-gate } 34347c478bd9Sstevel@tonic-gate } 34357c478bd9Sstevel@tonic-gate 34367c478bd9Sstevel@tonic-gate static void 34377c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp) 34387c478bd9Sstevel@tonic-gate { 34397c478bd9Sstevel@tonic-gate linelist_free(flistp->head); 34407c478bd9Sstevel@tonic-gate flistp->head = NULL; 34417c478bd9Sstevel@tonic-gate flistp->tail = NULL; 34427c478bd9Sstevel@tonic-gate } 34437c478bd9Sstevel@tonic-gate 34447c478bd9Sstevel@tonic-gate static void 34457c478bd9Sstevel@tonic-gate menu_free(menu_t *mp) 34467c478bd9Sstevel@tonic-gate { 34478c1b6884Sszhou entry_t *ent, *tmp; 34487c478bd9Sstevel@tonic-gate assert(mp); 34497c478bd9Sstevel@tonic-gate 34507c478bd9Sstevel@tonic-gate if (mp->start) 34517c478bd9Sstevel@tonic-gate linelist_free(mp->start); 34528c1b6884Sszhou ent = mp->entries; 34538c1b6884Sszhou while (ent) { 34548c1b6884Sszhou tmp = ent; 34558c1b6884Sszhou ent = tmp->next; 34568c1b6884Sszhou free(tmp); 34578c1b6884Sszhou } 34587c478bd9Sstevel@tonic-gate 34598c1b6884Sszhou free(mp); 34607c478bd9Sstevel@tonic-gate } 34617c478bd9Sstevel@tonic-gate 34627c478bd9Sstevel@tonic-gate /* 34637c478bd9Sstevel@tonic-gate * Utility routines 34647c478bd9Sstevel@tonic-gate */ 34657c478bd9Sstevel@tonic-gate 34667c478bd9Sstevel@tonic-gate 34677c478bd9Sstevel@tonic-gate /* 34687c478bd9Sstevel@tonic-gate * Returns 0 on success 34697c478bd9Sstevel@tonic-gate * Any other value indicates an error 34707c478bd9Sstevel@tonic-gate */ 34717c478bd9Sstevel@tonic-gate static int 34727c478bd9Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize) 34737c478bd9Sstevel@tonic-gate { 34747c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 34757c478bd9Sstevel@tonic-gate int ret; 34767c478bd9Sstevel@tonic-gate FILE *ptr; 34777c478bd9Sstevel@tonic-gate size_t len; 34787c478bd9Sstevel@tonic-gate sigset_t set; 34797c478bd9Sstevel@tonic-gate void (*disp)(int); 34807c478bd9Sstevel@tonic-gate 34817c478bd9Sstevel@tonic-gate /* 34827c478bd9Sstevel@tonic-gate * For security 34837c478bd9Sstevel@tonic-gate * - only absolute paths are allowed 34847c478bd9Sstevel@tonic-gate * - set IFS to space and tab 34857c478bd9Sstevel@tonic-gate */ 34867c478bd9Sstevel@tonic-gate if (*cmdline != '/') { 34877c478bd9Sstevel@tonic-gate bam_error(ABS_PATH_REQ, cmdline); 34887c478bd9Sstevel@tonic-gate return (-1); 34897c478bd9Sstevel@tonic-gate } 34907c478bd9Sstevel@tonic-gate (void) putenv("IFS= \t"); 34917c478bd9Sstevel@tonic-gate 34927c478bd9Sstevel@tonic-gate /* 34937c478bd9Sstevel@tonic-gate * We may have been exec'ed with SIGCHLD blocked 34947c478bd9Sstevel@tonic-gate * unblock it here 34957c478bd9Sstevel@tonic-gate */ 34967c478bd9Sstevel@tonic-gate (void) sigemptyset(&set); 34977c478bd9Sstevel@tonic-gate (void) sigaddset(&set, SIGCHLD); 34987c478bd9Sstevel@tonic-gate if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { 34997c478bd9Sstevel@tonic-gate bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno)); 35007c478bd9Sstevel@tonic-gate return (-1); 35017c478bd9Sstevel@tonic-gate } 35027c478bd9Sstevel@tonic-gate 35037c478bd9Sstevel@tonic-gate /* 35047c478bd9Sstevel@tonic-gate * Set SIGCHLD disposition to SIG_DFL for popen/pclose 35057c478bd9Sstevel@tonic-gate */ 35067c478bd9Sstevel@tonic-gate disp = sigset(SIGCHLD, SIG_DFL); 35077c478bd9Sstevel@tonic-gate if (disp == SIG_ERR) { 35087c478bd9Sstevel@tonic-gate bam_error(FAILED_SIG, strerror(errno)); 35097c478bd9Sstevel@tonic-gate return (-1); 35107c478bd9Sstevel@tonic-gate } 35117c478bd9Sstevel@tonic-gate if (disp == SIG_HOLD) { 35127c478bd9Sstevel@tonic-gate bam_error(BLOCKED_SIG, cmdline); 35137c478bd9Sstevel@tonic-gate return (-1); 35147c478bd9Sstevel@tonic-gate } 35157c478bd9Sstevel@tonic-gate 35167c478bd9Sstevel@tonic-gate ptr = popen(cmdline, "r"); 35177c478bd9Sstevel@tonic-gate if (ptr == NULL) { 35187c478bd9Sstevel@tonic-gate bam_error(POPEN_FAIL, cmdline, strerror(errno)); 35197c478bd9Sstevel@tonic-gate return (-1); 35207c478bd9Sstevel@tonic-gate } 35217c478bd9Sstevel@tonic-gate 35227c478bd9Sstevel@tonic-gate /* 35237c478bd9Sstevel@tonic-gate * If we simply do a pclose() following a popen(), pclose() 35247c478bd9Sstevel@tonic-gate * will close the reader end of the pipe immediately even 35257c478bd9Sstevel@tonic-gate * if the child process has not started/exited. pclose() 35267c478bd9Sstevel@tonic-gate * does wait for cmd to terminate before returning though. 35277c478bd9Sstevel@tonic-gate * When the executed command writes its output to the pipe 35287c478bd9Sstevel@tonic-gate * there is no reader process and the command dies with 35297c478bd9Sstevel@tonic-gate * SIGPIPE. To avoid this we read repeatedly until read 35307c478bd9Sstevel@tonic-gate * terminates with EOF. This indicates that the command 35317c478bd9Sstevel@tonic-gate * (writer) has closed the pipe and we can safely do a 35327c478bd9Sstevel@tonic-gate * pclose(). 35337c478bd9Sstevel@tonic-gate * 35347c478bd9Sstevel@tonic-gate * Since pclose() does wait for the command to exit, 35357c478bd9Sstevel@tonic-gate * we can safely reap the exit status of the command 35367c478bd9Sstevel@tonic-gate * from the value returned by pclose() 35377c478bd9Sstevel@tonic-gate */ 35387c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), ptr) != NULL) { 35397c478bd9Sstevel@tonic-gate /* if (bam_verbose) XXX */ 35407c478bd9Sstevel@tonic-gate bam_print(PRINT_NO_NEWLINE, buf); 35417c478bd9Sstevel@tonic-gate if (output && osize > 0) { 35427c478bd9Sstevel@tonic-gate (void) snprintf(output, osize, "%s", buf); 35437c478bd9Sstevel@tonic-gate len = strlen(buf); 35447c478bd9Sstevel@tonic-gate output += len; 35457c478bd9Sstevel@tonic-gate osize -= len; 35467c478bd9Sstevel@tonic-gate } 35477c478bd9Sstevel@tonic-gate } 35487c478bd9Sstevel@tonic-gate 35497c478bd9Sstevel@tonic-gate ret = pclose(ptr); 35507c478bd9Sstevel@tonic-gate if (ret == -1) { 35517c478bd9Sstevel@tonic-gate bam_error(PCLOSE_FAIL, cmdline, strerror(errno)); 35527c478bd9Sstevel@tonic-gate return (-1); 35537c478bd9Sstevel@tonic-gate } 35547c478bd9Sstevel@tonic-gate 35557c478bd9Sstevel@tonic-gate if (WIFEXITED(ret)) { 35567c478bd9Sstevel@tonic-gate return (WEXITSTATUS(ret)); 35577c478bd9Sstevel@tonic-gate } else { 35587c478bd9Sstevel@tonic-gate bam_error(EXEC_FAIL, cmdline, ret); 35597c478bd9Sstevel@tonic-gate return (-1); 35607c478bd9Sstevel@tonic-gate } 35617c478bd9Sstevel@tonic-gate } 35627c478bd9Sstevel@tonic-gate 35637c478bd9Sstevel@tonic-gate /* 35647c478bd9Sstevel@tonic-gate * Since this function returns -1 on error 35657c478bd9Sstevel@tonic-gate * it cannot be used to convert -1. However, 35667c478bd9Sstevel@tonic-gate * that is sufficient for what we need. 35677c478bd9Sstevel@tonic-gate */ 35687c478bd9Sstevel@tonic-gate static long 35697c478bd9Sstevel@tonic-gate s_strtol(char *str) 35707c478bd9Sstevel@tonic-gate { 35717c478bd9Sstevel@tonic-gate long l; 35727c478bd9Sstevel@tonic-gate char *res = NULL; 35737c478bd9Sstevel@tonic-gate 35747c478bd9Sstevel@tonic-gate if (str == NULL) { 35757c478bd9Sstevel@tonic-gate return (-1); 35767c478bd9Sstevel@tonic-gate } 35777c478bd9Sstevel@tonic-gate 35787c478bd9Sstevel@tonic-gate errno = 0; 35797c478bd9Sstevel@tonic-gate l = strtol(str, &res, 10); 35807c478bd9Sstevel@tonic-gate if (errno || *res != '\0') { 35817c478bd9Sstevel@tonic-gate return (-1); 35827c478bd9Sstevel@tonic-gate } 35837c478bd9Sstevel@tonic-gate 35847c478bd9Sstevel@tonic-gate return (l); 35857c478bd9Sstevel@tonic-gate } 35867c478bd9Sstevel@tonic-gate 35877c478bd9Sstevel@tonic-gate /* 35887c478bd9Sstevel@tonic-gate * Wrapper around fputs, that adds a newline (since fputs doesn't) 35897c478bd9Sstevel@tonic-gate */ 35907c478bd9Sstevel@tonic-gate static int 35917c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp) 35927c478bd9Sstevel@tonic-gate { 35937c478bd9Sstevel@tonic-gate char linebuf[BAM_MAXLINE]; 35947c478bd9Sstevel@tonic-gate 35957c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s\n", str); 35967c478bd9Sstevel@tonic-gate return (fputs(linebuf, fp)); 35977c478bd9Sstevel@tonic-gate } 35987c478bd9Sstevel@tonic-gate 35997c478bd9Sstevel@tonic-gate /* 36007c478bd9Sstevel@tonic-gate * Wrapper around fgets, that strips newlines returned by fgets 36017c478bd9Sstevel@tonic-gate */ 36027c478bd9Sstevel@tonic-gate static char * 36037c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp) 36047c478bd9Sstevel@tonic-gate { 36057c478bd9Sstevel@tonic-gate int n; 36067c478bd9Sstevel@tonic-gate 36077c478bd9Sstevel@tonic-gate buf = fgets(buf, buflen, fp); 36087c478bd9Sstevel@tonic-gate if (buf) { 36097c478bd9Sstevel@tonic-gate n = strlen(buf); 36107c478bd9Sstevel@tonic-gate if (n == buflen - 1 && buf[n-1] != '\n') 36117c478bd9Sstevel@tonic-gate bam_error(TOO_LONG, buflen - 1, buf); 36127c478bd9Sstevel@tonic-gate buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1]; 36137c478bd9Sstevel@tonic-gate } 36147c478bd9Sstevel@tonic-gate 36157c478bd9Sstevel@tonic-gate return (buf); 36167c478bd9Sstevel@tonic-gate } 36177c478bd9Sstevel@tonic-gate 36187c478bd9Sstevel@tonic-gate static void * 36197c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz) 36207c478bd9Sstevel@tonic-gate { 36217c478bd9Sstevel@tonic-gate void *ptr; 36227c478bd9Sstevel@tonic-gate 36237c478bd9Sstevel@tonic-gate ptr = calloc(nelem, sz); 36247c478bd9Sstevel@tonic-gate if (ptr == NULL) { 36257c478bd9Sstevel@tonic-gate bam_error(NO_MEM, nelem*sz); 36267c478bd9Sstevel@tonic-gate bam_exit(1); 36277c478bd9Sstevel@tonic-gate } 36287c478bd9Sstevel@tonic-gate return (ptr); 36297c478bd9Sstevel@tonic-gate } 36307c478bd9Sstevel@tonic-gate 36317c478bd9Sstevel@tonic-gate static char * 36327c478bd9Sstevel@tonic-gate s_strdup(char *str) 36337c478bd9Sstevel@tonic-gate { 36347c478bd9Sstevel@tonic-gate char *ptr; 36357c478bd9Sstevel@tonic-gate 36367c478bd9Sstevel@tonic-gate if (str == NULL) 36377c478bd9Sstevel@tonic-gate return (NULL); 36387c478bd9Sstevel@tonic-gate 36397c478bd9Sstevel@tonic-gate ptr = strdup(str); 36407c478bd9Sstevel@tonic-gate if (ptr == NULL) { 36417c478bd9Sstevel@tonic-gate bam_error(NO_MEM, strlen(str) + 1); 36427c478bd9Sstevel@tonic-gate bam_exit(1); 36437c478bd9Sstevel@tonic-gate } 36447c478bd9Sstevel@tonic-gate return (ptr); 36457c478bd9Sstevel@tonic-gate } 36467c478bd9Sstevel@tonic-gate 36477c478bd9Sstevel@tonic-gate /* 36487c478bd9Sstevel@tonic-gate * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients) 36497c478bd9Sstevel@tonic-gate * Returns 0 otherwise 36507c478bd9Sstevel@tonic-gate */ 36517c478bd9Sstevel@tonic-gate static int 36527c478bd9Sstevel@tonic-gate is_amd64(void) 36537c478bd9Sstevel@tonic-gate { 36547c478bd9Sstevel@tonic-gate static int amd64 = -1; 36557c478bd9Sstevel@tonic-gate char isabuf[257]; /* from sysinfo(2) manpage */ 36567c478bd9Sstevel@tonic-gate 36577c478bd9Sstevel@tonic-gate if (amd64 != -1) 36587c478bd9Sstevel@tonic-gate return (amd64); 36597c478bd9Sstevel@tonic-gate 36607c478bd9Sstevel@tonic-gate if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 && 36617c478bd9Sstevel@tonic-gate strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) 36627c478bd9Sstevel@tonic-gate amd64 = 1; 36637c478bd9Sstevel@tonic-gate else if (strstr(isabuf, "i386") == NULL) 36647c478bd9Sstevel@tonic-gate amd64 = 1; /* diskless server */ 36657c478bd9Sstevel@tonic-gate else 36667c478bd9Sstevel@tonic-gate amd64 = 0; 36677c478bd9Sstevel@tonic-gate 36687c478bd9Sstevel@tonic-gate return (amd64); 36697c478bd9Sstevel@tonic-gate } 36707c478bd9Sstevel@tonic-gate 36717c478bd9Sstevel@tonic-gate static void 36727c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s) 36737c478bd9Sstevel@tonic-gate { 36747c478bd9Sstevel@tonic-gate line_t *lp; 36757c478bd9Sstevel@tonic-gate 36767c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 36777c478bd9Sstevel@tonic-gate lp->line = s_strdup(s); 36787c478bd9Sstevel@tonic-gate if (flistp->head == NULL) 36797c478bd9Sstevel@tonic-gate flistp->head = lp; 36807c478bd9Sstevel@tonic-gate else 36817c478bd9Sstevel@tonic-gate flistp->tail->next = lp; 36827c478bd9Sstevel@tonic-gate flistp->tail = lp; 36837c478bd9Sstevel@tonic-gate } 3684