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 5fbac2b2bSvikram * Common Development and Distribution License (the "License"). 6fbac2b2bSvikram * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22fbac2b2bSvikram * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * bootadm(1M) is a new utility for managing bootability of 307c478bd9Sstevel@tonic-gate * Solaris *Newboot* environments. It has two primary tasks: 317c478bd9Sstevel@tonic-gate * - Allow end users to manage bootability of Newboot Solaris instances 327c478bd9Sstevel@tonic-gate * - Provide services to other subsystems in Solaris (primarily Install) 337c478bd9Sstevel@tonic-gate */ 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate /* Headers */ 367c478bd9Sstevel@tonic-gate #include <stdio.h> 377c478bd9Sstevel@tonic-gate #include <errno.h> 387c478bd9Sstevel@tonic-gate #include <stdlib.h> 397c478bd9Sstevel@tonic-gate #include <string.h> 407c478bd9Sstevel@tonic-gate #include <unistd.h> 417c478bd9Sstevel@tonic-gate #include <sys/types.h> 427c478bd9Sstevel@tonic-gate #include <sys/stat.h> 437c478bd9Sstevel@tonic-gate #include <stdarg.h> 447c478bd9Sstevel@tonic-gate #include <limits.h> 457c478bd9Sstevel@tonic-gate #include <signal.h> 467c478bd9Sstevel@tonic-gate #include <sys/wait.h> 477c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 487c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 497c478bd9Sstevel@tonic-gate #include <libnvpair.h> 507c478bd9Sstevel@tonic-gate #include <ftw.h> 517c478bd9Sstevel@tonic-gate #include <fcntl.h> 527c478bd9Sstevel@tonic-gate #include <strings.h> 537c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 547c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate #include <pwd.h> 577c478bd9Sstevel@tonic-gate #include <grp.h> 587c478bd9Sstevel@tonic-gate #include <device_info.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #include <libintl.h> 617c478bd9Sstevel@tonic-gate #include <locale.h> 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate #include <assert.h> 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate #include "message.h" 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN 687c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD" 697c478bd9Sstevel@tonic-gate #endif /* TEXT_DOMAIN */ 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /* Type definitions */ 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate /* Primary subcmds */ 747c478bd9Sstevel@tonic-gate typedef enum { 757c478bd9Sstevel@tonic-gate BAM_MENU = 3, 767c478bd9Sstevel@tonic-gate BAM_ARCHIVE 777c478bd9Sstevel@tonic-gate } subcmd_t; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate /* GRUB menu per-line classification */ 807c478bd9Sstevel@tonic-gate typedef enum { 817c478bd9Sstevel@tonic-gate BAM_INVALID = 0, 827c478bd9Sstevel@tonic-gate BAM_EMPTY, 837c478bd9Sstevel@tonic-gate BAM_COMMENT, 847c478bd9Sstevel@tonic-gate BAM_GLOBAL, 857c478bd9Sstevel@tonic-gate BAM_ENTRY, 867c478bd9Sstevel@tonic-gate BAM_TITLE 877c478bd9Sstevel@tonic-gate } menu_flag_t; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate /* struct for menu.lst contents */ 907c478bd9Sstevel@tonic-gate typedef struct line { 917c478bd9Sstevel@tonic-gate int lineNum; /* Line number in menu.lst */ 927c478bd9Sstevel@tonic-gate int entryNum; /* menu boot entry #. ENTRY_INIT if not applicable */ 937c478bd9Sstevel@tonic-gate char *cmd; 947c478bd9Sstevel@tonic-gate char *sep; 957c478bd9Sstevel@tonic-gate char *arg; 967c478bd9Sstevel@tonic-gate char *line; 977c478bd9Sstevel@tonic-gate menu_flag_t flags; 987c478bd9Sstevel@tonic-gate struct line *next; 998c1b6884Sszhou struct line *prev; 1007c478bd9Sstevel@tonic-gate } line_t; 1017c478bd9Sstevel@tonic-gate 1028c1b6884Sszhou typedef struct entry { 1038c1b6884Sszhou struct entry *next; 1048c1b6884Sszhou struct entry *prev; 1058c1b6884Sszhou line_t *start; 1068c1b6884Sszhou line_t *end; 1078c1b6884Sszhou } entry_t; 1088c1b6884Sszhou 1097c478bd9Sstevel@tonic-gate typedef struct { 1107c478bd9Sstevel@tonic-gate line_t *start; 1117c478bd9Sstevel@tonic-gate line_t *end; 1128c1b6884Sszhou line_t *curdefault; /* line containing default */ 1138c1b6884Sszhou line_t *olddefault; /* old default line (commented) */ 1148c1b6884Sszhou entry_t *entries; /* os entries */ 1157c478bd9Sstevel@tonic-gate } menu_t; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate typedef enum { 1187c478bd9Sstevel@tonic-gate OPT_ABSENT = 0, /* No option */ 1197c478bd9Sstevel@tonic-gate OPT_REQ, /* option required */ 1207c478bd9Sstevel@tonic-gate OPT_OPTIONAL /* option may or may not be present */ 1217c478bd9Sstevel@tonic-gate } option_t; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate typedef enum { 124b610f78eSvikram BAM_ERROR = -1, /* Must be negative. add_boot_entry() depends on it */ 1257c478bd9Sstevel@tonic-gate BAM_SUCCESS = 0, 1267c478bd9Sstevel@tonic-gate BAM_WRITE = 2 1277c478bd9Sstevel@tonic-gate } error_t; 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate typedef struct { 1307c478bd9Sstevel@tonic-gate char *subcmd; 1317c478bd9Sstevel@tonic-gate option_t option; 1327c478bd9Sstevel@tonic-gate error_t (*handler)(); 133*1a97e40eSvikram int unpriv; /* is this an unprivileged command */ 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" 164fbac2b2bSvikram #define GRUB_fdisk "/etc/lu/GRUB_fdisk" 165fbac2b2bSvikram #define GRUB_fdisk_target "/etc/lu/GRUB_fdisk_target" 166b610f78eSvikram 167b610f78eSvikram #define INSTALLGRUB "/sbin/installgrub" 168b610f78eSvikram #define STAGE1 "/boot/grub/stage1" 169b610f78eSvikram #define STAGE2 "/boot/grub/stage2" 170b610f78eSvikram 1717c478bd9Sstevel@tonic-gate /* 1727c478bd9Sstevel@tonic-gate * Default file attributes 1737c478bd9Sstevel@tonic-gate */ 1747c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_MODE 0644 /* default permissions */ 1757c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_UID 0 /* user root */ 1767c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_GID 3 /* group sys */ 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate /* 1797c478bd9Sstevel@tonic-gate * Menu related 1807c478bd9Sstevel@tonic-gate * menu_cmd_t and menu_cmds must be kept in sync 1817c478bd9Sstevel@tonic-gate */ 1827c478bd9Sstevel@tonic-gate typedef enum { 1837c478bd9Sstevel@tonic-gate DEFAULT_CMD = 0, 1847c478bd9Sstevel@tonic-gate TIMEOUT_CMD, 1857c478bd9Sstevel@tonic-gate TITLE_CMD, 1867c478bd9Sstevel@tonic-gate ROOT_CMD, 1877c478bd9Sstevel@tonic-gate KERNEL_CMD, 1887c478bd9Sstevel@tonic-gate MODULE_CMD, 1897c478bd9Sstevel@tonic-gate SEP_CMD, 1907c478bd9Sstevel@tonic-gate COMMENT_CMD 1917c478bd9Sstevel@tonic-gate } menu_cmd_t; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate static char *menu_cmds[] = { 1947c478bd9Sstevel@tonic-gate "default", /* DEFAULT_CMD */ 1957c478bd9Sstevel@tonic-gate "timeout", /* TIMEOUT_CMD */ 1967c478bd9Sstevel@tonic-gate "title", /* TITLE_CMD */ 1977c478bd9Sstevel@tonic-gate "root", /* ROOT_CMD */ 1987c478bd9Sstevel@tonic-gate "kernel", /* KERNEL_CMD */ 1997c478bd9Sstevel@tonic-gate "module", /* MODULE_CMD */ 2007c478bd9Sstevel@tonic-gate " ", /* SEP_CMD */ 2017c478bd9Sstevel@tonic-gate "#", /* COMMENT_CMD */ 2027c478bd9Sstevel@tonic-gate NULL 2037c478bd9Sstevel@tonic-gate }; 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate #define OPT_ENTRY_NUM "entry" 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate /* 2087c478bd9Sstevel@tonic-gate * archive related 2097c478bd9Sstevel@tonic-gate */ 2107c478bd9Sstevel@tonic-gate typedef struct { 2117c478bd9Sstevel@tonic-gate line_t *head; 2127c478bd9Sstevel@tonic-gate line_t *tail; 2137c478bd9Sstevel@tonic-gate } filelist_t; 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate #define BOOT_FILE_LIST "boot/solaris/filelist.ramdisk" 2167c478bd9Sstevel@tonic-gate #define ETC_FILE_LIST "etc/boot/solaris/filelist.ramdisk" 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate #define FILE_STAT "boot/solaris/filestat.ramdisk" 2197c478bd9Sstevel@tonic-gate #define FILE_STAT_TMP "boot/solaris/filestat.ramdisk.tmp" 2207c478bd9Sstevel@tonic-gate #define DIR_PERMS (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) 2217c478bd9Sstevel@tonic-gate #define FILE_STAT_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate #define BAM_HDR "---------- ADDED BY BOOTADM - DO NOT EDIT ----------" 2247c478bd9Sstevel@tonic-gate #define BAM_FTR "---------------------END BOOTADM--------------------" 2258c1b6884Sszhou #define BAM_OLDDEF "BOOTADM SAVED DEFAULT: " 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate /* Globals */ 2287c478bd9Sstevel@tonic-gate static char *prog; 2297c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd; 2307c478bd9Sstevel@tonic-gate static char *bam_root; 2317c478bd9Sstevel@tonic-gate static int bam_rootlen; 2327c478bd9Sstevel@tonic-gate static int bam_root_readonly; 23340541d5dSvikram static int bam_alt_root; 2347c478bd9Sstevel@tonic-gate static char *bam_subcmd; 2357c478bd9Sstevel@tonic-gate static char *bam_opt; 2367c478bd9Sstevel@tonic-gate static int bam_debug; 2377c478bd9Sstevel@tonic-gate static char **bam_argv; 2387c478bd9Sstevel@tonic-gate static int bam_argc; 2397c478bd9Sstevel@tonic-gate static int bam_force; 2407c478bd9Sstevel@tonic-gate static int bam_verbose; 2417c478bd9Sstevel@tonic-gate static int bam_check; 2427c478bd9Sstevel@tonic-gate static int bam_smf_check; 2437c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1; 2447c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/"; 245b610f78eSvikram static int bam_update_all; 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate /* function prototypes */ 2487c478bd9Sstevel@tonic-gate static void parse_args_internal(int argc, char *argv[]); 2497c478bd9Sstevel@tonic-gate static void parse_args(int argc, char *argv[]); 2507c478bd9Sstevel@tonic-gate static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]); 2517c478bd9Sstevel@tonic-gate static error_t bam_archive(char *subcmd, char *opt); 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate static void bam_error(char *format, ...); 2547c478bd9Sstevel@tonic-gate static void bam_print(char *format, ...); 2557c478bd9Sstevel@tonic-gate static void bam_exit(int excode); 2567c478bd9Sstevel@tonic-gate static void bam_lock(void); 2577c478bd9Sstevel@tonic-gate static void bam_unlock(void); 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate static int exec_cmd(char *cmdline, char *output, int64_t osize); 2607c478bd9Sstevel@tonic-gate static error_t read_globals(menu_t *mp, char *menu_path, 2617c478bd9Sstevel@tonic-gate char *globalcmd, int quiet); 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate static menu_t *menu_read(char *menu_path); 2647c478bd9Sstevel@tonic-gate static error_t menu_write(char *root, menu_t *mp); 2657c478bd9Sstevel@tonic-gate static void linelist_free(line_t *start); 2667c478bd9Sstevel@tonic-gate static void menu_free(menu_t *mp); 2677c478bd9Sstevel@tonic-gate static void line_free(line_t *lp); 2687c478bd9Sstevel@tonic-gate static void filelist_free(filelist_t *flistp); 2697c478bd9Sstevel@tonic-gate static error_t list2file(char *root, char *tmp, 2707c478bd9Sstevel@tonic-gate char *final, line_t *start); 2717c478bd9Sstevel@tonic-gate static error_t list_entry(menu_t *mp, char *menu_path, char *opt); 2727c478bd9Sstevel@tonic-gate static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt); 2737c478bd9Sstevel@tonic-gate static error_t update_entry(menu_t *mp, char *root, char *opt); 2747c478bd9Sstevel@tonic-gate static error_t update_temp(menu_t *mp, char *root, char *opt); 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate static error_t update_archive(char *root, char *opt); 2777c478bd9Sstevel@tonic-gate static error_t list_archive(char *root, char *opt); 2787c478bd9Sstevel@tonic-gate static error_t update_all(char *root, char *opt); 2797c478bd9Sstevel@tonic-gate static error_t read_list(char *root, filelist_t *flistp); 2807c478bd9Sstevel@tonic-gate static error_t set_global(menu_t *mp, char *globalcmd, int val); 2817c478bd9Sstevel@tonic-gate static error_t set_option(menu_t *mp, char *globalcmd, char *opt); 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate static long s_strtol(char *str); 2847c478bd9Sstevel@tonic-gate static char *s_fgets(char *buf, int n, FILE *fp); 2857c478bd9Sstevel@tonic-gate static int s_fputs(char *str, FILE *fp); 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate static void *s_calloc(size_t nelem, size_t sz); 2887c478bd9Sstevel@tonic-gate static char *s_strdup(char *str); 2897c478bd9Sstevel@tonic-gate static int is_readonly(char *); 2907c478bd9Sstevel@tonic-gate static int is_amd64(void); 2917c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *); 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate #if defined(__sparc) 2947c478bd9Sstevel@tonic-gate static void sparc_abort(void); 2957c478bd9Sstevel@tonic-gate #endif 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate /* Menu related sub commands */ 2987c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = { 299*1a97e40eSvikram "set_option", OPT_OPTIONAL, set_option, 0, /* PUB */ 300*1a97e40eSvikram "list_entry", OPT_OPTIONAL, list_entry, 1, /* PUB */ 301*1a97e40eSvikram "delete_all_entries", OPT_ABSENT, delete_all_entries, 0, /* PVT */ 302*1a97e40eSvikram "update_entry", OPT_REQ, update_entry, 0, /* menu */ 303*1a97e40eSvikram "update_temp", OPT_OPTIONAL, update_temp, 0, /* reboot */ 304*1a97e40eSvikram NULL, 0, NULL, 0 /* must be last */ 3057c478bd9Sstevel@tonic-gate }; 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate /* Archive related sub commands */ 3087c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = { 309*1a97e40eSvikram "update", OPT_ABSENT, update_archive, 0, /* PUB */ 310*1a97e40eSvikram "update_all", OPT_ABSENT, update_all, 0, /* PVT */ 311*1a97e40eSvikram "list", OPT_OPTIONAL, list_archive, 1, /* PUB */ 312*1a97e40eSvikram NULL, 0, NULL, 0 /* must be last */ 3137c478bd9Sstevel@tonic-gate }; 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate static struct { 3167c478bd9Sstevel@tonic-gate nvlist_t *new_nvlp; 3177c478bd9Sstevel@tonic-gate nvlist_t *old_nvlp; 3187c478bd9Sstevel@tonic-gate int need_update; 3197c478bd9Sstevel@tonic-gate } walk_arg; 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate static void 3227c478bd9Sstevel@tonic-gate usage(void) 3237c478bd9Sstevel@tonic-gate { 3247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "USAGE:\n"); 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate /* archive usage */ 3287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n", 3297c478bd9Sstevel@tonic-gate prog); 3307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog); 3317c478bd9Sstevel@tonic-gate #ifndef __sparc 3327c478bd9Sstevel@tonic-gate /* x86 only */ 3337c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog); 3347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog); 3357c478bd9Sstevel@tonic-gate #endif 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate int 3397c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 3407c478bd9Sstevel@tonic-gate { 3417c478bd9Sstevel@tonic-gate error_t ret; 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 3447c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate if ((prog = strrchr(argv[0], '/')) == NULL) { 3477c478bd9Sstevel@tonic-gate prog = argv[0]; 3487c478bd9Sstevel@tonic-gate } else { 3497c478bd9Sstevel@tonic-gate prog++; 3507c478bd9Sstevel@tonic-gate } 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate /* 3547c478bd9Sstevel@tonic-gate * Don't depend on caller's umask 3557c478bd9Sstevel@tonic-gate */ 3567c478bd9Sstevel@tonic-gate (void) umask(0022); 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate parse_args(argc, argv); 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate #if defined(__sparc) 3617c478bd9Sstevel@tonic-gate /* 3627c478bd9Sstevel@tonic-gate * There are only two valid invocations of bootadm 3637c478bd9Sstevel@tonic-gate * on SPARC: 3647c478bd9Sstevel@tonic-gate * 3657c478bd9Sstevel@tonic-gate * - SPARC diskless server creating boot_archive for i386 clients 3667c478bd9Sstevel@tonic-gate * - archive creation call during reboot of a SPARC system 3677c478bd9Sstevel@tonic-gate * 3687c478bd9Sstevel@tonic-gate * The latter should be a NOP 3697c478bd9Sstevel@tonic-gate */ 3707c478bd9Sstevel@tonic-gate if (bam_cmd != BAM_ARCHIVE) { 3717c478bd9Sstevel@tonic-gate sparc_abort(); 3727c478bd9Sstevel@tonic-gate } 3737c478bd9Sstevel@tonic-gate #endif 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate switch (bam_cmd) { 3767c478bd9Sstevel@tonic-gate case BAM_MENU: 3777c478bd9Sstevel@tonic-gate ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv); 3787c478bd9Sstevel@tonic-gate break; 3797c478bd9Sstevel@tonic-gate case BAM_ARCHIVE: 3807c478bd9Sstevel@tonic-gate ret = bam_archive(bam_subcmd, bam_opt); 3817c478bd9Sstevel@tonic-gate break; 3827c478bd9Sstevel@tonic-gate default: 3837c478bd9Sstevel@tonic-gate usage(); 3847c478bd9Sstevel@tonic-gate bam_exit(1); 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate if (ret != BAM_SUCCESS) 3887c478bd9Sstevel@tonic-gate bam_exit(1); 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate bam_unlock(); 3917c478bd9Sstevel@tonic-gate return (0); 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate #if defined(__sparc) 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate static void 3977c478bd9Sstevel@tonic-gate sparc_abort(void) 3987c478bd9Sstevel@tonic-gate { 3997c478bd9Sstevel@tonic-gate bam_error(NOT_ON_SPARC); 4007c478bd9Sstevel@tonic-gate bam_exit(1); 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate #endif 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate /* 4067c478bd9Sstevel@tonic-gate * Equivalence of public and internal commands: 4077c478bd9Sstevel@tonic-gate * update-archive -- -a update 4087c478bd9Sstevel@tonic-gate * list-archive -- -a list 4097c478bd9Sstevel@tonic-gate * set-menu -- -m set_option 4107c478bd9Sstevel@tonic-gate * list-menu -- -m list_entry 4117c478bd9Sstevel@tonic-gate * update-menu -- -m update_entry 4127c478bd9Sstevel@tonic-gate */ 4137c478bd9Sstevel@tonic-gate static struct cmd_map { 4147c478bd9Sstevel@tonic-gate char *bam_cmdname; 4157c478bd9Sstevel@tonic-gate int bam_cmd; 4167c478bd9Sstevel@tonic-gate char *bam_subcmd; 4177c478bd9Sstevel@tonic-gate } cmd_map[] = { 4187c478bd9Sstevel@tonic-gate { "update-archive", BAM_ARCHIVE, "update"}, 4197c478bd9Sstevel@tonic-gate { "list-archive", BAM_ARCHIVE, "list"}, 4207c478bd9Sstevel@tonic-gate { "set-menu", BAM_MENU, "set_option"}, 4217c478bd9Sstevel@tonic-gate { "list-menu", BAM_MENU, "list_entry"}, 4227c478bd9Sstevel@tonic-gate { "update-menu", BAM_MENU, "update_entry"}, 4237c478bd9Sstevel@tonic-gate { NULL, 0, NULL} 4247c478bd9Sstevel@tonic-gate }; 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate /* 4277c478bd9Sstevel@tonic-gate * Commands syntax published in bootadm(1M) are parsed here 4287c478bd9Sstevel@tonic-gate */ 4297c478bd9Sstevel@tonic-gate static void 4307c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[]) 4317c478bd9Sstevel@tonic-gate { 4327c478bd9Sstevel@tonic-gate struct cmd_map *cmp = cmd_map; 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate /* command conforming to the final spec */ 4357c478bd9Sstevel@tonic-gate if (argc > 1 && argv[1][0] != '-') { 4367c478bd9Sstevel@tonic-gate /* 4377c478bd9Sstevel@tonic-gate * Map commands to internal table. 4387c478bd9Sstevel@tonic-gate */ 4397c478bd9Sstevel@tonic-gate while (cmp->bam_cmdname) { 4407c478bd9Sstevel@tonic-gate if (strcmp(argv[1], cmp->bam_cmdname) == 0) { 4417c478bd9Sstevel@tonic-gate bam_cmd = cmp->bam_cmd; 4427c478bd9Sstevel@tonic-gate bam_subcmd = cmp->bam_subcmd; 4437c478bd9Sstevel@tonic-gate break; 4447c478bd9Sstevel@tonic-gate } 4457c478bd9Sstevel@tonic-gate cmp++; 4467c478bd9Sstevel@tonic-gate } 4477c478bd9Sstevel@tonic-gate if (cmp->bam_cmdname == NULL) { 4487c478bd9Sstevel@tonic-gate usage(); 4497c478bd9Sstevel@tonic-gate bam_exit(1); 4507c478bd9Sstevel@tonic-gate } 4517c478bd9Sstevel@tonic-gate argc--; 4527c478bd9Sstevel@tonic-gate argv++; 4537c478bd9Sstevel@tonic-gate } 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate parse_args_internal(argc, argv); 4567c478bd9Sstevel@tonic-gate } 4577c478bd9Sstevel@tonic-gate 4587c478bd9Sstevel@tonic-gate /* 4597c478bd9Sstevel@tonic-gate * A combination of public and private commands are parsed here. 4607c478bd9Sstevel@tonic-gate * The internal syntax and the corresponding functionality are: 4617c478bd9Sstevel@tonic-gate * -a update -- update-archive 4627c478bd9Sstevel@tonic-gate * -a list -- list-archive 4637c478bd9Sstevel@tonic-gate * -a update-all -- (reboot to sync all mounted OS archive) 4647c478bd9Sstevel@tonic-gate * -m update_entry -- update-menu 4657c478bd9Sstevel@tonic-gate * -m list_entry -- list-menu 4667c478bd9Sstevel@tonic-gate * -m update_temp -- (reboot -- [boot-args]) 4677c478bd9Sstevel@tonic-gate * -m delete_all_entries -- (called from install) 4687c478bd9Sstevel@tonic-gate */ 4697c478bd9Sstevel@tonic-gate static void 4707c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[]) 4717c478bd9Sstevel@tonic-gate { 4727c478bd9Sstevel@tonic-gate int c, error; 4737c478bd9Sstevel@tonic-gate extern char *optarg; 4747c478bd9Sstevel@tonic-gate extern int optind, opterr; 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate /* Suppress error message from getopt */ 4777c478bd9Sstevel@tonic-gate opterr = 0; 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate error = 0; 4808c1b6884Sszhou while ((c = getopt(argc, argv, "a:d:fm:no:vCR:")) != -1) { 4817c478bd9Sstevel@tonic-gate switch (c) { 4827c478bd9Sstevel@tonic-gate case 'a': 4837c478bd9Sstevel@tonic-gate if (bam_cmd) { 4847c478bd9Sstevel@tonic-gate error = 1; 4857c478bd9Sstevel@tonic-gate bam_error(MULT_CMDS, c); 4867c478bd9Sstevel@tonic-gate } 4877c478bd9Sstevel@tonic-gate bam_cmd = BAM_ARCHIVE; 4887c478bd9Sstevel@tonic-gate bam_subcmd = optarg; 4897c478bd9Sstevel@tonic-gate break; 4907c478bd9Sstevel@tonic-gate case 'd': 4917c478bd9Sstevel@tonic-gate if (bam_debug) { 4927c478bd9Sstevel@tonic-gate error = 1; 4937c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 4947c478bd9Sstevel@tonic-gate } 4957c478bd9Sstevel@tonic-gate bam_debug = s_strtol(optarg); 4967c478bd9Sstevel@tonic-gate break; 4977c478bd9Sstevel@tonic-gate case 'f': 4987c478bd9Sstevel@tonic-gate if (bam_force) { 4997c478bd9Sstevel@tonic-gate error = 1; 5007c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5017c478bd9Sstevel@tonic-gate } 5027c478bd9Sstevel@tonic-gate bam_force = 1; 5037c478bd9Sstevel@tonic-gate break; 5047c478bd9Sstevel@tonic-gate case 'm': 5057c478bd9Sstevel@tonic-gate if (bam_cmd) { 5067c478bd9Sstevel@tonic-gate error = 1; 5077c478bd9Sstevel@tonic-gate bam_error(MULT_CMDS, c); 5087c478bd9Sstevel@tonic-gate } 5097c478bd9Sstevel@tonic-gate bam_cmd = BAM_MENU; 5107c478bd9Sstevel@tonic-gate bam_subcmd = optarg; 5117c478bd9Sstevel@tonic-gate break; 5127c478bd9Sstevel@tonic-gate case 'n': 5137c478bd9Sstevel@tonic-gate if (bam_check) { 5147c478bd9Sstevel@tonic-gate error = 1; 5157c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5167c478bd9Sstevel@tonic-gate } 5177c478bd9Sstevel@tonic-gate bam_check = 1; 5187c478bd9Sstevel@tonic-gate break; 5197c478bd9Sstevel@tonic-gate case 'o': 5207c478bd9Sstevel@tonic-gate if (bam_opt) { 5217c478bd9Sstevel@tonic-gate error = 1; 5227c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5237c478bd9Sstevel@tonic-gate } 5247c478bd9Sstevel@tonic-gate bam_opt = optarg; 5257c478bd9Sstevel@tonic-gate break; 5267c478bd9Sstevel@tonic-gate case 'v': 5277c478bd9Sstevel@tonic-gate if (bam_verbose) { 5287c478bd9Sstevel@tonic-gate error = 1; 5297c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5307c478bd9Sstevel@tonic-gate } 5317c478bd9Sstevel@tonic-gate bam_verbose = 1; 5327c478bd9Sstevel@tonic-gate break; 5338c1b6884Sszhou case 'C': 5348c1b6884Sszhou bam_smf_check = 1; 5358c1b6884Sszhou break; 5367c478bd9Sstevel@tonic-gate case 'R': 5377c478bd9Sstevel@tonic-gate if (bam_root) { 5387c478bd9Sstevel@tonic-gate error = 1; 5397c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5407c478bd9Sstevel@tonic-gate break; 5417c478bd9Sstevel@tonic-gate } else if (realpath(optarg, rootbuf) == NULL) { 5427c478bd9Sstevel@tonic-gate error = 1; 5437c478bd9Sstevel@tonic-gate bam_error(CANT_RESOLVE, optarg, 5447c478bd9Sstevel@tonic-gate strerror(errno)); 5457c478bd9Sstevel@tonic-gate break; 5467c478bd9Sstevel@tonic-gate } 54740541d5dSvikram bam_alt_root = 1; 5487c478bd9Sstevel@tonic-gate bam_root = rootbuf; 5497c478bd9Sstevel@tonic-gate bam_rootlen = strlen(rootbuf); 5507c478bd9Sstevel@tonic-gate break; 5517c478bd9Sstevel@tonic-gate case '?': 5527c478bd9Sstevel@tonic-gate error = 1; 5537c478bd9Sstevel@tonic-gate bam_error(BAD_OPT, optopt); 5547c478bd9Sstevel@tonic-gate break; 5557c478bd9Sstevel@tonic-gate default : 5567c478bd9Sstevel@tonic-gate error = 1; 5577c478bd9Sstevel@tonic-gate bam_error(BAD_OPT, c); 5587c478bd9Sstevel@tonic-gate break; 5597c478bd9Sstevel@tonic-gate } 5607c478bd9Sstevel@tonic-gate } 5617c478bd9Sstevel@tonic-gate 5627c478bd9Sstevel@tonic-gate /* 5637c478bd9Sstevel@tonic-gate * A command option must be specfied 5647c478bd9Sstevel@tonic-gate */ 5657c478bd9Sstevel@tonic-gate if (!bam_cmd) { 5667c478bd9Sstevel@tonic-gate if (bam_opt && strcmp(bam_opt, "all") == 0) { 5677c478bd9Sstevel@tonic-gate usage(); 5687c478bd9Sstevel@tonic-gate bam_exit(0); 5697c478bd9Sstevel@tonic-gate } 5707c478bd9Sstevel@tonic-gate bam_error(NEED_CMD); 5717c478bd9Sstevel@tonic-gate error = 1; 5727c478bd9Sstevel@tonic-gate } 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate if (error) { 5757c478bd9Sstevel@tonic-gate usage(); 5767c478bd9Sstevel@tonic-gate bam_exit(1); 5777c478bd9Sstevel@tonic-gate } 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate if (optind > argc) { 5807c478bd9Sstevel@tonic-gate bam_error(INT_ERROR, "parse_args"); 5817c478bd9Sstevel@tonic-gate bam_exit(1); 5827c478bd9Sstevel@tonic-gate } else if (optind < argc) { 5837c478bd9Sstevel@tonic-gate bam_argv = &argv[optind]; 5847c478bd9Sstevel@tonic-gate bam_argc = argc - optind; 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate /* 5887c478bd9Sstevel@tonic-gate * -n implies verbose mode 5897c478bd9Sstevel@tonic-gate */ 5907c478bd9Sstevel@tonic-gate if (bam_check) 5917c478bd9Sstevel@tonic-gate bam_verbose = 1; 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate static error_t 5957c478bd9Sstevel@tonic-gate check_subcmd_and_options( 5967c478bd9Sstevel@tonic-gate char *subcmd, 5977c478bd9Sstevel@tonic-gate char *opt, 5987c478bd9Sstevel@tonic-gate subcmd_defn_t *table, 5997c478bd9Sstevel@tonic-gate error_t (**fp)()) 6007c478bd9Sstevel@tonic-gate { 6017c478bd9Sstevel@tonic-gate int i; 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate if (subcmd == NULL) { 6047c478bd9Sstevel@tonic-gate bam_error(NEED_SUBCMD); 6057c478bd9Sstevel@tonic-gate return (BAM_ERROR); 6067c478bd9Sstevel@tonic-gate } 6077c478bd9Sstevel@tonic-gate 608*1a97e40eSvikram if (bam_argc != 0 || bam_argv) { 609*1a97e40eSvikram if (strcmp(subcmd, "set_option") != 0 || bam_argc != 1) { 610*1a97e40eSvikram bam_error(TRAILING_ARGS); 611*1a97e40eSvikram usage(); 612*1a97e40eSvikram return (BAM_ERROR); 613*1a97e40eSvikram } 614*1a97e40eSvikram } 615*1a97e40eSvikram 6167c478bd9Sstevel@tonic-gate if (bam_root == NULL) { 6177c478bd9Sstevel@tonic-gate bam_root = rootbuf; 6187c478bd9Sstevel@tonic-gate bam_rootlen = 1; 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate 6217c478bd9Sstevel@tonic-gate /* verify that subcmd is valid */ 6227c478bd9Sstevel@tonic-gate for (i = 0; table[i].subcmd != NULL; i++) { 6237c478bd9Sstevel@tonic-gate if (strcmp(table[i].subcmd, subcmd) == 0) 6247c478bd9Sstevel@tonic-gate break; 6257c478bd9Sstevel@tonic-gate } 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate if (table[i].subcmd == NULL) { 6287c478bd9Sstevel@tonic-gate bam_error(INVALID_SUBCMD, subcmd); 6297c478bd9Sstevel@tonic-gate return (BAM_ERROR); 6307c478bd9Sstevel@tonic-gate } 6317c478bd9Sstevel@tonic-gate 632*1a97e40eSvikram if (table[i].unpriv == 0 && geteuid() != 0) { 633*1a97e40eSvikram bam_error(MUST_BE_ROOT); 634*1a97e40eSvikram return (BAM_ERROR); 635*1a97e40eSvikram } 636*1a97e40eSvikram 637*1a97e40eSvikram /* 638*1a97e40eSvikram * Currently only privileged commands need a lock 639*1a97e40eSvikram */ 640*1a97e40eSvikram if (table[i].unpriv == 0) 641*1a97e40eSvikram bam_lock(); 642*1a97e40eSvikram 6437c478bd9Sstevel@tonic-gate /* subcmd verifies that opt is appropriate */ 6447c478bd9Sstevel@tonic-gate if (table[i].option != OPT_OPTIONAL) { 6457c478bd9Sstevel@tonic-gate if ((table[i].option == OPT_REQ) ^ (opt != NULL)) { 6467c478bd9Sstevel@tonic-gate if (opt) 6477c478bd9Sstevel@tonic-gate bam_error(NO_OPT_REQ, subcmd); 6487c478bd9Sstevel@tonic-gate else 6497c478bd9Sstevel@tonic-gate bam_error(MISS_OPT, subcmd); 6507c478bd9Sstevel@tonic-gate return (BAM_ERROR); 6517c478bd9Sstevel@tonic-gate } 6527c478bd9Sstevel@tonic-gate } 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate *fp = table[i].handler; 6557c478bd9Sstevel@tonic-gate 6567c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 6577c478bd9Sstevel@tonic-gate } 6587c478bd9Sstevel@tonic-gate 659b610f78eSvikram 660b610f78eSvikram static char * 66140541d5dSvikram mount_grub_slice(int *mnted, char **physlice, char **logslice, char **fs_type) 662b610f78eSvikram { 663b610f78eSvikram struct extmnttab mnt; 664b610f78eSvikram struct stat sb; 665b610f78eSvikram char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32]; 666f0f2c544Svikram char cmd[PATH_MAX]; 667b610f78eSvikram char *mntpt; 668b610f78eSvikram int p, l, f; 669b610f78eSvikram FILE *fp; 670b610f78eSvikram 671b610f78eSvikram assert(mnted); 672b610f78eSvikram *mnted = 0; 673b610f78eSvikram 674b610f78eSvikram /* 67540541d5dSvikram * physlice, logslice, fs_type args may be NULL 676b610f78eSvikram */ 677b610f78eSvikram if (physlice) 678b610f78eSvikram *physlice = NULL; 67940541d5dSvikram if (logslice) 68040541d5dSvikram *logslice = NULL; 68140541d5dSvikram if (fs_type) 68240541d5dSvikram *fs_type = NULL; 683b610f78eSvikram 684b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 685b610f78eSvikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 686b610f78eSvikram return (NULL); 687b610f78eSvikram } 688b610f78eSvikram 689b610f78eSvikram fp = fopen(GRUB_slice, "r"); 690b610f78eSvikram if (fp == NULL) { 691b610f78eSvikram bam_error(OPEN_FAIL, GRUB_slice, strerror(errno)); 692b610f78eSvikram return (NULL); 693b610f78eSvikram } 694b610f78eSvikram 695b610f78eSvikram dev[0] = fstype[0] = phys[0] = '\0'; 696b610f78eSvikram p = sizeof ("PHYS_SLICE=") - 1; 697b610f78eSvikram l = sizeof ("LOG_SLICE=") - 1; 698b610f78eSvikram f = sizeof ("LOG_FSTYP=") - 1; 699b610f78eSvikram while (s_fgets(buf, sizeof (buf), fp) != NULL) { 700b610f78eSvikram if (strncmp(buf, "PHYS_SLICE=", p) == 0) { 701b610f78eSvikram (void) strlcpy(phys, buf + p, sizeof (phys)); 702b610f78eSvikram continue; 703b610f78eSvikram } 704b610f78eSvikram if (strncmp(buf, "LOG_SLICE=", l) == 0) { 705b610f78eSvikram (void) strlcpy(dev, buf + l, sizeof (dev)); 706b610f78eSvikram continue; 707b610f78eSvikram } 708b610f78eSvikram if (strncmp(buf, "LOG_FSTYP=", f) == 0) { 709b610f78eSvikram (void) strlcpy(fstype, buf + f, sizeof (fstype)); 710b610f78eSvikram continue; 711b610f78eSvikram } 712b610f78eSvikram } 713b610f78eSvikram (void) fclose(fp); 714b610f78eSvikram 715b610f78eSvikram if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') { 716b610f78eSvikram bam_error(BAD_SLICE_FILE, GRUB_slice); 717b610f78eSvikram return (NULL); 718b610f78eSvikram } 719b610f78eSvikram 720b610f78eSvikram if (physlice) { 721b610f78eSvikram *physlice = s_strdup(phys); 722b610f78eSvikram } 72340541d5dSvikram if (logslice) { 72440541d5dSvikram *logslice = s_strdup(dev); 72540541d5dSvikram } 72640541d5dSvikram if (fs_type) { 72740541d5dSvikram *fs_type = s_strdup(fstype); 72840541d5dSvikram } 729b610f78eSvikram 730b610f78eSvikram /* 731b610f78eSvikram * Check if the slice is already mounted 732b610f78eSvikram */ 733b610f78eSvikram fp = fopen(MNTTAB, "r"); 734b610f78eSvikram if (fp == NULL) { 735b610f78eSvikram bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 73640541d5dSvikram goto error; 737b610f78eSvikram } 738b610f78eSvikram 739b610f78eSvikram resetmnttab(fp); 740b610f78eSvikram 741b610f78eSvikram mntpt = NULL; 742b610f78eSvikram while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 743b610f78eSvikram if (strcmp(mnt.mnt_special, dev) == 0) { 744b610f78eSvikram mntpt = s_strdup(mnt.mnt_mountp); 745b610f78eSvikram break; 746b610f78eSvikram } 747b610f78eSvikram } 748b610f78eSvikram 749b610f78eSvikram (void) fclose(fp); 750b610f78eSvikram 751b610f78eSvikram if (mntpt) { 752b610f78eSvikram return (mntpt); 753b610f78eSvikram } 754b610f78eSvikram 755b610f78eSvikram 756b610f78eSvikram /* 757b610f78eSvikram * GRUB slice is not mounted, we need to mount it now. 758b610f78eSvikram * First create the mountpoint 759b610f78eSvikram */ 760b610f78eSvikram mntpt = s_calloc(1, PATH_MAX); 761b610f78eSvikram (void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid()); 762b610f78eSvikram if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) { 763b610f78eSvikram bam_error(MKDIR_FAILED, mntpt, strerror(errno)); 764b610f78eSvikram free(mntpt); 76540541d5dSvikram goto error; 766b610f78eSvikram } 767b610f78eSvikram 768f0f2c544Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/mount -F %s %s %s", 769f0f2c544Svikram fstype, dev, mntpt); 770f0f2c544Svikram 771f0f2c544Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 77240541d5dSvikram bam_error(MOUNT_FAILED, dev, fstype); 773b610f78eSvikram if (rmdir(mntpt) != 0) { 774b610f78eSvikram bam_error(RMDIR_FAILED, mntpt, strerror(errno)); 775b610f78eSvikram } 776b610f78eSvikram free(mntpt); 77740541d5dSvikram goto error; 778b610f78eSvikram } 779b610f78eSvikram 780b610f78eSvikram *mnted = 1; 781b610f78eSvikram return (mntpt); 78240541d5dSvikram 78340541d5dSvikram error: 78440541d5dSvikram if (physlice) { 78540541d5dSvikram free(*physlice); 78640541d5dSvikram *physlice = NULL; 78740541d5dSvikram } 78840541d5dSvikram if (logslice) { 78940541d5dSvikram free(*logslice); 79040541d5dSvikram *logslice = NULL; 79140541d5dSvikram } 79240541d5dSvikram if (fs_type) { 79340541d5dSvikram free(*fs_type); 79440541d5dSvikram *fs_type = NULL; 79540541d5dSvikram } 79640541d5dSvikram return (NULL); 797b610f78eSvikram } 798b610f78eSvikram 799b610f78eSvikram static void 80040541d5dSvikram umount_grub_slice( 80140541d5dSvikram int mnted, 80240541d5dSvikram char *mntpt, 80340541d5dSvikram char *physlice, 80440541d5dSvikram char *logslice, 80540541d5dSvikram char *fs_type) 806b610f78eSvikram { 807f0f2c544Svikram char cmd[PATH_MAX]; 808f0f2c544Svikram 809b610f78eSvikram /* 810b610f78eSvikram * If we have not dealt with GRUB slice 811b610f78eSvikram * we have nothing to do - just return. 812b610f78eSvikram */ 813b610f78eSvikram if (mntpt == NULL) 814b610f78eSvikram return; 815b610f78eSvikram 816b610f78eSvikram 817b610f78eSvikram /* 818b610f78eSvikram * If we mounted the filesystem earlier in mount_grub_slice() 819b610f78eSvikram * unmount it now. 820b610f78eSvikram */ 821b610f78eSvikram if (mnted) { 822f0f2c544Svikram (void) snprintf(cmd, sizeof (cmd), "/sbin/umount %s", 823f0f2c544Svikram mntpt); 824f0f2c544Svikram if (exec_cmd(cmd, NULL, 0) != 0) { 825f0f2c544Svikram bam_error(UMOUNT_FAILED, mntpt); 826b610f78eSvikram } 827b610f78eSvikram if (rmdir(mntpt) != 0) { 828b610f78eSvikram bam_error(RMDIR_FAILED, mntpt, strerror(errno)); 829b610f78eSvikram } 830b610f78eSvikram } 83140541d5dSvikram 832b610f78eSvikram if (physlice) 833b610f78eSvikram free(physlice); 83440541d5dSvikram if (logslice) 83540541d5dSvikram free(logslice); 83640541d5dSvikram if (fs_type) 83740541d5dSvikram free(fs_type); 83840541d5dSvikram 839b610f78eSvikram free(mntpt); 840b610f78eSvikram } 841b610f78eSvikram 84240541d5dSvikram static char * 84340541d5dSvikram use_stubboot(void) 84440541d5dSvikram { 84540541d5dSvikram int mnted; 84640541d5dSvikram struct stat sb; 84740541d5dSvikram struct extmnttab mnt; 84840541d5dSvikram FILE *fp; 84940541d5dSvikram char cmd[PATH_MAX]; 85040541d5dSvikram 85140541d5dSvikram if (stat(STUBBOOT, &sb) != 0) { 85240541d5dSvikram bam_error(STUBBOOT_DIR_NOT_FOUND); 85340541d5dSvikram return (NULL); 85440541d5dSvikram } 85540541d5dSvikram 85640541d5dSvikram /* 85740541d5dSvikram * Check if stubboot is mounted. If not, mount it 85840541d5dSvikram */ 85940541d5dSvikram fp = fopen(MNTTAB, "r"); 86040541d5dSvikram if (fp == NULL) { 86140541d5dSvikram bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 86240541d5dSvikram return (NULL); 86340541d5dSvikram } 86440541d5dSvikram 86540541d5dSvikram resetmnttab(fp); 86640541d5dSvikram 86740541d5dSvikram mnted = 0; 86840541d5dSvikram while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 86940541d5dSvikram if (strcmp(mnt.mnt_mountp, STUBBOOT) == 0) { 87040541d5dSvikram mnted = 1; 87140541d5dSvikram break; 87240541d5dSvikram } 87340541d5dSvikram } 87440541d5dSvikram 87540541d5dSvikram (void) fclose(fp); 87640541d5dSvikram 87740541d5dSvikram if (mnted) 87840541d5dSvikram return (STUBBOOT); 87940541d5dSvikram 88040541d5dSvikram /* 88140541d5dSvikram * Stubboot is not mounted, mount it now. 88240541d5dSvikram * It should exist in /etc/vfstab 88340541d5dSvikram */ 88440541d5dSvikram (void) snprintf(cmd, sizeof (cmd), "/sbin/mount %s", 88540541d5dSvikram STUBBOOT); 88640541d5dSvikram if (exec_cmd(cmd, NULL, 0) != 0) { 88740541d5dSvikram bam_error(MOUNT_MNTPT_FAILED, STUBBOOT); 88840541d5dSvikram return (NULL); 88940541d5dSvikram } 89040541d5dSvikram 89140541d5dSvikram return (STUBBOOT); 89240541d5dSvikram } 89340541d5dSvikram 89440541d5dSvikram static void 89540541d5dSvikram disp_active_menu_locn(char *menu_path, char *logslice, char *fstype, int mnted) 89640541d5dSvikram { 89740541d5dSvikram /* 89840541d5dSvikram * Check if we did a temp mount of an unmounted device. 89940541d5dSvikram * If yes, print the block device and fstype for that device 90040541d5dSvikram * else it is already mounted, so we print the path to the GRUB menu. 90140541d5dSvikram */ 90240541d5dSvikram if (mnted) { 90340541d5dSvikram bam_print(GRUB_MENU_DEVICE, logslice); 90440541d5dSvikram bam_print(GRUB_MENU_FSTYPE, fstype); 90540541d5dSvikram } else { 90640541d5dSvikram bam_print(GRUB_MENU_PATH, menu_path); 90740541d5dSvikram } 90840541d5dSvikram } 90940541d5dSvikram 91040541d5dSvikram /* 91140541d5dSvikram * NOTE: A single "/" is also considered a trailing slash and will 91240541d5dSvikram * be deleted. 91340541d5dSvikram */ 91440541d5dSvikram static void 91540541d5dSvikram elide_trailing_slash(const char *src, char *dst, size_t dstsize) 91640541d5dSvikram { 91740541d5dSvikram size_t dstlen; 91840541d5dSvikram 91940541d5dSvikram assert(src); 92040541d5dSvikram assert(dst); 92140541d5dSvikram 92240541d5dSvikram (void) strlcpy(dst, src, dstsize); 92340541d5dSvikram 92440541d5dSvikram dstlen = strlen(dst); 92540541d5dSvikram if (dst[dstlen - 1] == '/') { 92640541d5dSvikram dst[dstlen - 1] = '\0'; 92740541d5dSvikram } 92840541d5dSvikram } 92940541d5dSvikram 9307c478bd9Sstevel@tonic-gate static error_t 9317c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[]) 9327c478bd9Sstevel@tonic-gate { 9337c478bd9Sstevel@tonic-gate error_t ret; 9347c478bd9Sstevel@tonic-gate char menu_path[PATH_MAX]; 9357c478bd9Sstevel@tonic-gate menu_t *menu; 93640541d5dSvikram char *mntpt, *menu_root, *logslice, *fstype; 937b610f78eSvikram struct stat sb; 938b610f78eSvikram int mnted; /* set if we did a mount */ 9397c478bd9Sstevel@tonic-gate error_t (*f)(menu_t *mp, char *menu_path, char *opt); 9407c478bd9Sstevel@tonic-gate 9417c478bd9Sstevel@tonic-gate /* 9427c478bd9Sstevel@tonic-gate * Check arguments 9437c478bd9Sstevel@tonic-gate */ 9447c478bd9Sstevel@tonic-gate ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f); 9457c478bd9Sstevel@tonic-gate if (ret == BAM_ERROR) { 9467c478bd9Sstevel@tonic-gate return (BAM_ERROR); 9477c478bd9Sstevel@tonic-gate } 9487c478bd9Sstevel@tonic-gate 949b610f78eSvikram mntpt = NULL; 950b610f78eSvikram mnted = 0; 95140541d5dSvikram logslice = fstype = NULL; 95240541d5dSvikram 95340541d5dSvikram /* 95440541d5dSvikram * If the user provides an alternate root, we 95540541d5dSvikram * assume they know what they are doing and we 95640541d5dSvikram * use it. Else we check if there is an 95740541d5dSvikram * alternate location (other than /boot/grub) 95840541d5dSvikram * for the GRUB menu 95940541d5dSvikram */ 96040541d5dSvikram if (bam_alt_root) { 96140541d5dSvikram menu_root = bam_root; 96240541d5dSvikram } else if (stat(GRUB_slice, &sb) == 0) { 96340541d5dSvikram mntpt = mount_grub_slice(&mnted, NULL, &logslice, &fstype); 964b610f78eSvikram menu_root = mntpt; 96540541d5dSvikram } else if (stat(STUBBOOT, &sb) == 0) { 96640541d5dSvikram menu_root = use_stubboot(); 967b610f78eSvikram } else { 968b610f78eSvikram menu_root = bam_root; 969b610f78eSvikram } 970b610f78eSvikram 97140541d5dSvikram if (menu_root == NULL) { 97240541d5dSvikram bam_error(CANNOT_LOCATE_GRUB_MENU); 97340541d5dSvikram return (BAM_ERROR); 97440541d5dSvikram } 97540541d5dSvikram 97640541d5dSvikram elide_trailing_slash(menu_root, menu_path, sizeof (menu_path)); 97740541d5dSvikram (void) strlcat(menu_path, GRUB_MENU, sizeof (menu_path)); 97840541d5dSvikram 97940541d5dSvikram /* 98040541d5dSvikram * If listing the menu, display the active menu 98140541d5dSvikram * location 98240541d5dSvikram */ 98340541d5dSvikram if (strcmp(subcmd, "list_entry") == 0) { 98440541d5dSvikram disp_active_menu_locn(menu_path, logslice, fstype, mnted); 98540541d5dSvikram } 9867c478bd9Sstevel@tonic-gate 9877c478bd9Sstevel@tonic-gate menu = menu_read(menu_path); 9887c478bd9Sstevel@tonic-gate assert(menu); 9897c478bd9Sstevel@tonic-gate 9907c478bd9Sstevel@tonic-gate /* 9917c478bd9Sstevel@tonic-gate * Special handling for setting timeout and default 9927c478bd9Sstevel@tonic-gate */ 9937c478bd9Sstevel@tonic-gate if (strcmp(subcmd, "set_option") == 0) { 9947c478bd9Sstevel@tonic-gate if (largc != 1 || largv[0] == NULL) { 9957c478bd9Sstevel@tonic-gate usage(); 99640541d5dSvikram menu_free(menu); 99740541d5dSvikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 9987c478bd9Sstevel@tonic-gate return (BAM_ERROR); 9997c478bd9Sstevel@tonic-gate } 10007c478bd9Sstevel@tonic-gate opt = largv[0]; 10017c478bd9Sstevel@tonic-gate } else if (largc != 0) { 10027c478bd9Sstevel@tonic-gate usage(); 100340541d5dSvikram menu_free(menu); 100440541d5dSvikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 10057c478bd9Sstevel@tonic-gate return (BAM_ERROR); 10067c478bd9Sstevel@tonic-gate } 10077c478bd9Sstevel@tonic-gate 10087c478bd9Sstevel@tonic-gate /* 10097c478bd9Sstevel@tonic-gate * Once the sub-cmd handler has run 10107c478bd9Sstevel@tonic-gate * only the line field is guaranteed to have valid values 10117c478bd9Sstevel@tonic-gate */ 10127c478bd9Sstevel@tonic-gate if (strcmp(subcmd, "update_entry") == 0) 10137c478bd9Sstevel@tonic-gate ret = f(menu, bam_root, opt); 10147c478bd9Sstevel@tonic-gate else 10157c478bd9Sstevel@tonic-gate ret = f(menu, menu_path, opt); 10167c478bd9Sstevel@tonic-gate if (ret == BAM_WRITE) { 1017b610f78eSvikram ret = menu_write(menu_root, menu); 10187c478bd9Sstevel@tonic-gate } 10197c478bd9Sstevel@tonic-gate 10207c478bd9Sstevel@tonic-gate menu_free(menu); 10217c478bd9Sstevel@tonic-gate 102240541d5dSvikram umount_grub_slice(mnted, mntpt, NULL, logslice, fstype); 1023b610f78eSvikram 10247c478bd9Sstevel@tonic-gate return (ret); 10257c478bd9Sstevel@tonic-gate } 10267c478bd9Sstevel@tonic-gate 10277c478bd9Sstevel@tonic-gate 10287c478bd9Sstevel@tonic-gate static error_t 10297c478bd9Sstevel@tonic-gate bam_archive( 10307c478bd9Sstevel@tonic-gate char *subcmd, 10317c478bd9Sstevel@tonic-gate char *opt) 10327c478bd9Sstevel@tonic-gate { 10337c478bd9Sstevel@tonic-gate error_t ret; 10347c478bd9Sstevel@tonic-gate error_t (*f)(char *root, char *opt); 10357c478bd9Sstevel@tonic-gate 10368c1b6884Sszhou /* 10378c1b6884Sszhou * Add trailing / for archive subcommands 10388c1b6884Sszhou */ 10398c1b6884Sszhou if (rootbuf[strlen(rootbuf) - 1] != '/') 10408c1b6884Sszhou (void) strcat(rootbuf, "/"); 10418c1b6884Sszhou bam_rootlen = strlen(rootbuf); 10428c1b6884Sszhou 10437c478bd9Sstevel@tonic-gate /* 10447c478bd9Sstevel@tonic-gate * Check arguments 10457c478bd9Sstevel@tonic-gate */ 10467c478bd9Sstevel@tonic-gate ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f); 10477c478bd9Sstevel@tonic-gate if (ret != BAM_SUCCESS) { 10487c478bd9Sstevel@tonic-gate return (BAM_ERROR); 10497c478bd9Sstevel@tonic-gate } 10507c478bd9Sstevel@tonic-gate 10517c478bd9Sstevel@tonic-gate #if defined(__sparc) 10527c478bd9Sstevel@tonic-gate /* 10537c478bd9Sstevel@tonic-gate * A NOP if called on SPARC during reboot 10547c478bd9Sstevel@tonic-gate */ 10557c478bd9Sstevel@tonic-gate if (strcmp(subcmd, "update_all") == 0) 10567c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 10577c478bd9Sstevel@tonic-gate else if (strcmp(subcmd, "update") != 0) 10587c478bd9Sstevel@tonic-gate sparc_abort(); 10597c478bd9Sstevel@tonic-gate #endif 10607c478bd9Sstevel@tonic-gate 10617c478bd9Sstevel@tonic-gate /* 10627c478bd9Sstevel@tonic-gate * Check archive not supported with update_all 10637c478bd9Sstevel@tonic-gate * since it is awkward to display out-of-sync 10647c478bd9Sstevel@tonic-gate * information for each BE. 10657c478bd9Sstevel@tonic-gate */ 10667c478bd9Sstevel@tonic-gate if (bam_check && strcmp(subcmd, "update_all") == 0) { 10677c478bd9Sstevel@tonic-gate bam_error(CHECK_NOT_SUPPORTED, subcmd); 10687c478bd9Sstevel@tonic-gate return (BAM_ERROR); 10697c478bd9Sstevel@tonic-gate } 10707c478bd9Sstevel@tonic-gate 1071b610f78eSvikram if (strcmp(subcmd, "update_all") == 0) 1072b610f78eSvikram bam_update_all = 1; 1073b610f78eSvikram 1074b610f78eSvikram ret = f(bam_root, opt); 1075b610f78eSvikram 1076b610f78eSvikram bam_update_all = 0; 1077b610f78eSvikram 1078b610f78eSvikram return (ret); 10797c478bd9Sstevel@tonic-gate } 10807c478bd9Sstevel@tonic-gate 10817c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 10827c478bd9Sstevel@tonic-gate static void 10837c478bd9Sstevel@tonic-gate bam_error(char *format, ...) 10847c478bd9Sstevel@tonic-gate { 10857c478bd9Sstevel@tonic-gate va_list ap; 10867c478bd9Sstevel@tonic-gate 10877c478bd9Sstevel@tonic-gate va_start(ap, format); 10887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", prog); 10897c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, ap); 10907c478bd9Sstevel@tonic-gate va_end(ap); 10917c478bd9Sstevel@tonic-gate } 10927c478bd9Sstevel@tonic-gate 10937c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 10947c478bd9Sstevel@tonic-gate static void 10957c478bd9Sstevel@tonic-gate bam_print(char *format, ...) 10967c478bd9Sstevel@tonic-gate { 10977c478bd9Sstevel@tonic-gate va_list ap; 10987c478bd9Sstevel@tonic-gate 10997c478bd9Sstevel@tonic-gate va_start(ap, format); 11007c478bd9Sstevel@tonic-gate (void) vfprintf(stdout, format, ap); 11017c478bd9Sstevel@tonic-gate va_end(ap); 11027c478bd9Sstevel@tonic-gate } 11037c478bd9Sstevel@tonic-gate 11047c478bd9Sstevel@tonic-gate static void 11057c478bd9Sstevel@tonic-gate bam_exit(int excode) 11067c478bd9Sstevel@tonic-gate { 11077c478bd9Sstevel@tonic-gate bam_unlock(); 11087c478bd9Sstevel@tonic-gate exit(excode); 11097c478bd9Sstevel@tonic-gate } 11107c478bd9Sstevel@tonic-gate 11117c478bd9Sstevel@tonic-gate static void 11127c478bd9Sstevel@tonic-gate bam_lock(void) 11137c478bd9Sstevel@tonic-gate { 11147c478bd9Sstevel@tonic-gate struct flock lock; 11157c478bd9Sstevel@tonic-gate pid_t pid; 11167c478bd9Sstevel@tonic-gate 11177c478bd9Sstevel@tonic-gate bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS); 11187c478bd9Sstevel@tonic-gate if (bam_lock_fd < 0) { 11197c478bd9Sstevel@tonic-gate /* 11207c478bd9Sstevel@tonic-gate * We may be invoked early in boot for archive verification. 11217c478bd9Sstevel@tonic-gate * In this case, root is readonly and /var/run may not exist. 11227c478bd9Sstevel@tonic-gate * Proceed without the lock 11237c478bd9Sstevel@tonic-gate */ 11247c478bd9Sstevel@tonic-gate if (errno == EROFS || errno == ENOENT) { 11257c478bd9Sstevel@tonic-gate bam_root_readonly = 1; 11267c478bd9Sstevel@tonic-gate return; 11277c478bd9Sstevel@tonic-gate } 11287c478bd9Sstevel@tonic-gate 11297c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno)); 11307c478bd9Sstevel@tonic-gate bam_exit(1); 11317c478bd9Sstevel@tonic-gate } 11327c478bd9Sstevel@tonic-gate 11337c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 11347c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 11357c478bd9Sstevel@tonic-gate lock.l_start = 0; 11367c478bd9Sstevel@tonic-gate lock.l_len = 0; 11377c478bd9Sstevel@tonic-gate 11387c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) { 11397c478bd9Sstevel@tonic-gate if (errno != EACCES && errno != EAGAIN) { 11407c478bd9Sstevel@tonic-gate bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11417c478bd9Sstevel@tonic-gate (void) close(bam_lock_fd); 11427c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 11437c478bd9Sstevel@tonic-gate bam_exit(1); 11447c478bd9Sstevel@tonic-gate } 11457c478bd9Sstevel@tonic-gate pid = 0; 11467c478bd9Sstevel@tonic-gate (void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0); 11477c478bd9Sstevel@tonic-gate bam_print(FILE_LOCKED, pid); 11487c478bd9Sstevel@tonic-gate 11497c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 11507c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 11517c478bd9Sstevel@tonic-gate lock.l_start = 0; 11527c478bd9Sstevel@tonic-gate lock.l_len = 0; 11537c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) { 11547c478bd9Sstevel@tonic-gate bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11557c478bd9Sstevel@tonic-gate (void) close(bam_lock_fd); 11567c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 11577c478bd9Sstevel@tonic-gate bam_exit(1); 11587c478bd9Sstevel@tonic-gate } 11597c478bd9Sstevel@tonic-gate } 11607c478bd9Sstevel@tonic-gate 11617c478bd9Sstevel@tonic-gate /* We own the lock now */ 11627c478bd9Sstevel@tonic-gate pid = getpid(); 11637c478bd9Sstevel@tonic-gate (void) write(bam_lock_fd, &pid, sizeof (pid)); 11647c478bd9Sstevel@tonic-gate } 11657c478bd9Sstevel@tonic-gate 11667c478bd9Sstevel@tonic-gate static void 11677c478bd9Sstevel@tonic-gate bam_unlock(void) 11687c478bd9Sstevel@tonic-gate { 11697c478bd9Sstevel@tonic-gate struct flock unlock; 11707c478bd9Sstevel@tonic-gate 11717c478bd9Sstevel@tonic-gate /* 11727c478bd9Sstevel@tonic-gate * NOP if we don't hold the lock 11737c478bd9Sstevel@tonic-gate */ 11747c478bd9Sstevel@tonic-gate if (bam_lock_fd < 0) { 11757c478bd9Sstevel@tonic-gate return; 11767c478bd9Sstevel@tonic-gate } 11777c478bd9Sstevel@tonic-gate 11787c478bd9Sstevel@tonic-gate unlock.l_type = F_UNLCK; 11797c478bd9Sstevel@tonic-gate unlock.l_whence = SEEK_SET; 11807c478bd9Sstevel@tonic-gate unlock.l_start = 0; 11817c478bd9Sstevel@tonic-gate unlock.l_len = 0; 11827c478bd9Sstevel@tonic-gate 11837c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) { 11847c478bd9Sstevel@tonic-gate bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 11857c478bd9Sstevel@tonic-gate } 11867c478bd9Sstevel@tonic-gate 11877c478bd9Sstevel@tonic-gate if (close(bam_lock_fd) == -1) { 11887c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno)); 11897c478bd9Sstevel@tonic-gate } 11907c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 11917c478bd9Sstevel@tonic-gate } 11927c478bd9Sstevel@tonic-gate 11937c478bd9Sstevel@tonic-gate static error_t 11947c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt) 11957c478bd9Sstevel@tonic-gate { 11967c478bd9Sstevel@tonic-gate filelist_t flist; 11977c478bd9Sstevel@tonic-gate filelist_t *flistp = &flist; 11987c478bd9Sstevel@tonic-gate line_t *lp; 11997c478bd9Sstevel@tonic-gate 12007c478bd9Sstevel@tonic-gate assert(root); 12017c478bd9Sstevel@tonic-gate assert(opt == NULL); 12027c478bd9Sstevel@tonic-gate 12037c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 12047c478bd9Sstevel@tonic-gate if (read_list(root, flistp) != BAM_SUCCESS) { 12057c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12067c478bd9Sstevel@tonic-gate } 12077c478bd9Sstevel@tonic-gate assert(flistp->head && flistp->tail); 12087c478bd9Sstevel@tonic-gate 12097c478bd9Sstevel@tonic-gate for (lp = flistp->head; lp; lp = lp->next) { 12107c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 12117c478bd9Sstevel@tonic-gate } 12127c478bd9Sstevel@tonic-gate 12137c478bd9Sstevel@tonic-gate filelist_free(flistp); 12147c478bd9Sstevel@tonic-gate 12157c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 12167c478bd9Sstevel@tonic-gate } 12177c478bd9Sstevel@tonic-gate 12187c478bd9Sstevel@tonic-gate /* 12197c478bd9Sstevel@tonic-gate * This routine writes a list of lines to a file. 12207c478bd9Sstevel@tonic-gate * The list is *not* freed 12217c478bd9Sstevel@tonic-gate */ 12227c478bd9Sstevel@tonic-gate static error_t 12237c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start) 12247c478bd9Sstevel@tonic-gate { 12257c478bd9Sstevel@tonic-gate char tmpfile[PATH_MAX]; 12267c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 12277c478bd9Sstevel@tonic-gate FILE *fp; 12287c478bd9Sstevel@tonic-gate int ret; 12297c478bd9Sstevel@tonic-gate struct stat sb; 12307c478bd9Sstevel@tonic-gate mode_t mode; 12317c478bd9Sstevel@tonic-gate uid_t root_uid; 12327c478bd9Sstevel@tonic-gate gid_t sys_gid; 12337c478bd9Sstevel@tonic-gate struct passwd *pw; 12347c478bd9Sstevel@tonic-gate struct group *gp; 12357c478bd9Sstevel@tonic-gate 12367c478bd9Sstevel@tonic-gate 12377c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, final); 12387c478bd9Sstevel@tonic-gate 12397c478bd9Sstevel@tonic-gate if (start == NULL) { 12407c478bd9Sstevel@tonic-gate if (stat(path, &sb) != -1) { 12417c478bd9Sstevel@tonic-gate bam_print(UNLINK_EMPTY, path); 12427c478bd9Sstevel@tonic-gate if (unlink(path) != 0) { 12437c478bd9Sstevel@tonic-gate bam_error(UNLINK_FAIL, path, strerror(errno)); 12447c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12457c478bd9Sstevel@tonic-gate } else { 12467c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 12477c478bd9Sstevel@tonic-gate } 12487c478bd9Sstevel@tonic-gate } 12497c478bd9Sstevel@tonic-gate } 12507c478bd9Sstevel@tonic-gate 12517c478bd9Sstevel@tonic-gate /* 12527c478bd9Sstevel@tonic-gate * Preserve attributes of existing file if possible, 12537c478bd9Sstevel@tonic-gate * otherwise ask the system for uid/gid of root/sys. 12547c478bd9Sstevel@tonic-gate * If all fails, fall back on hard-coded defaults. 12557c478bd9Sstevel@tonic-gate */ 12567c478bd9Sstevel@tonic-gate if (stat(path, &sb) != -1) { 12577c478bd9Sstevel@tonic-gate mode = sb.st_mode; 12587c478bd9Sstevel@tonic-gate root_uid = sb.st_uid; 12597c478bd9Sstevel@tonic-gate sys_gid = sb.st_gid; 12607c478bd9Sstevel@tonic-gate } else { 12617c478bd9Sstevel@tonic-gate mode = DEFAULT_DEV_MODE; 12627c478bd9Sstevel@tonic-gate if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) { 12637c478bd9Sstevel@tonic-gate root_uid = pw->pw_uid; 12647c478bd9Sstevel@tonic-gate } else { 12657c478bd9Sstevel@tonic-gate if (bam_verbose) 12667c478bd9Sstevel@tonic-gate bam_error(CANT_FIND_USER, 12677c478bd9Sstevel@tonic-gate DEFAULT_DEV_USER, DEFAULT_DEV_UID); 12687c478bd9Sstevel@tonic-gate root_uid = (uid_t)DEFAULT_DEV_UID; 12697c478bd9Sstevel@tonic-gate } 12707c478bd9Sstevel@tonic-gate if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) { 12717c478bd9Sstevel@tonic-gate sys_gid = gp->gr_gid; 12727c478bd9Sstevel@tonic-gate } else { 12737c478bd9Sstevel@tonic-gate if (bam_verbose) 12747c478bd9Sstevel@tonic-gate bam_error(CANT_FIND_GROUP, 12757c478bd9Sstevel@tonic-gate DEFAULT_DEV_GROUP, DEFAULT_DEV_GID); 12767c478bd9Sstevel@tonic-gate sys_gid = (gid_t)DEFAULT_DEV_GID; 12777c478bd9Sstevel@tonic-gate } 12787c478bd9Sstevel@tonic-gate } 12797c478bd9Sstevel@tonic-gate 12807c478bd9Sstevel@tonic-gate (void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp); 12817c478bd9Sstevel@tonic-gate 12827c478bd9Sstevel@tonic-gate /* Truncate tmpfile first */ 12837c478bd9Sstevel@tonic-gate fp = fopen(tmpfile, "w"); 12847c478bd9Sstevel@tonic-gate if (fp == NULL) { 12857c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 12867c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12877c478bd9Sstevel@tonic-gate } 12887c478bd9Sstevel@tonic-gate ret = fclose(fp); 12897c478bd9Sstevel@tonic-gate if (ret == EOF) { 12907c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 12917c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12927c478bd9Sstevel@tonic-gate } 12937c478bd9Sstevel@tonic-gate 12947c478bd9Sstevel@tonic-gate /* Now open it in append mode */ 12957c478bd9Sstevel@tonic-gate fp = fopen(tmpfile, "a"); 12967c478bd9Sstevel@tonic-gate if (fp == NULL) { 12977c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 12987c478bd9Sstevel@tonic-gate return (BAM_ERROR); 12997c478bd9Sstevel@tonic-gate } 13007c478bd9Sstevel@tonic-gate 13017c478bd9Sstevel@tonic-gate for (; start; start = start->next) { 13027c478bd9Sstevel@tonic-gate ret = s_fputs(start->line, fp); 13037c478bd9Sstevel@tonic-gate if (ret == EOF) { 13047c478bd9Sstevel@tonic-gate bam_error(WRITE_FAIL, tmpfile, strerror(errno)); 13057c478bd9Sstevel@tonic-gate (void) fclose(fp); 13067c478bd9Sstevel@tonic-gate return (BAM_ERROR); 13077c478bd9Sstevel@tonic-gate } 13087c478bd9Sstevel@tonic-gate } 13097c478bd9Sstevel@tonic-gate 13107c478bd9Sstevel@tonic-gate ret = fclose(fp); 13117c478bd9Sstevel@tonic-gate if (ret == EOF) { 13127c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 13137c478bd9Sstevel@tonic-gate return (BAM_ERROR); 13147c478bd9Sstevel@tonic-gate } 13157c478bd9Sstevel@tonic-gate 13167c478bd9Sstevel@tonic-gate /* 1317de531be4Sjg * Set up desired attributes. Ignore failures on filesystems 1318de531be4Sjg * not supporting these operations - pcfs reports unsupported 1319de531be4Sjg * operations as EINVAL. 13207c478bd9Sstevel@tonic-gate */ 13217c478bd9Sstevel@tonic-gate ret = chmod(tmpfile, mode); 1322de531be4Sjg if (ret == -1 && 1323de531be4Sjg errno != EINVAL && errno != ENOTSUP) { 13247c478bd9Sstevel@tonic-gate bam_error(CHMOD_FAIL, tmpfile, strerror(errno)); 13257c478bd9Sstevel@tonic-gate return (BAM_ERROR); 13267c478bd9Sstevel@tonic-gate } 13277c478bd9Sstevel@tonic-gate 13287c478bd9Sstevel@tonic-gate ret = chown(tmpfile, root_uid, sys_gid); 1329de531be4Sjg if (ret == -1 && 1330de531be4Sjg errno != EINVAL && errno != ENOTSUP) { 13317c478bd9Sstevel@tonic-gate bam_error(CHOWN_FAIL, tmpfile, strerror(errno)); 13327c478bd9Sstevel@tonic-gate return (BAM_ERROR); 13337c478bd9Sstevel@tonic-gate } 13347c478bd9Sstevel@tonic-gate 13357c478bd9Sstevel@tonic-gate 13367c478bd9Sstevel@tonic-gate /* 13377c478bd9Sstevel@tonic-gate * Do an atomic rename 13387c478bd9Sstevel@tonic-gate */ 13397c478bd9Sstevel@tonic-gate ret = rename(tmpfile, path); 13407c478bd9Sstevel@tonic-gate if (ret != 0) { 13417c478bd9Sstevel@tonic-gate bam_error(RENAME_FAIL, path, strerror(errno)); 13427c478bd9Sstevel@tonic-gate return (BAM_ERROR); 13437c478bd9Sstevel@tonic-gate } 13447c478bd9Sstevel@tonic-gate 13457c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 13467c478bd9Sstevel@tonic-gate } 13477c478bd9Sstevel@tonic-gate 13487c478bd9Sstevel@tonic-gate /* 13497c478bd9Sstevel@tonic-gate * This function should always return 0 - since we want 13507c478bd9Sstevel@tonic-gate * to create stat data for *all* files in the list. 13517c478bd9Sstevel@tonic-gate */ 13527c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 13537c478bd9Sstevel@tonic-gate static int 13547c478bd9Sstevel@tonic-gate cmpstat( 13557c478bd9Sstevel@tonic-gate const char *file, 13567c478bd9Sstevel@tonic-gate const struct stat *stat, 13577c478bd9Sstevel@tonic-gate int flags, 13587c478bd9Sstevel@tonic-gate struct FTW *ftw) 13597c478bd9Sstevel@tonic-gate { 13607c478bd9Sstevel@tonic-gate uint_t sz; 13617c478bd9Sstevel@tonic-gate uint64_t *value; 13627c478bd9Sstevel@tonic-gate uint64_t filestat[2]; 13637c478bd9Sstevel@tonic-gate int error; 13647c478bd9Sstevel@tonic-gate 13657c478bd9Sstevel@tonic-gate /* 13667c478bd9Sstevel@tonic-gate * We only want regular files 13677c478bd9Sstevel@tonic-gate */ 13687c478bd9Sstevel@tonic-gate if (!S_ISREG(stat->st_mode)) 13697c478bd9Sstevel@tonic-gate return (0); 13707c478bd9Sstevel@tonic-gate 13717c478bd9Sstevel@tonic-gate /* 13727c478bd9Sstevel@tonic-gate * new_nvlp may be NULL if there were errors earlier 13737c478bd9Sstevel@tonic-gate * but this is not fatal to update determination. 13747c478bd9Sstevel@tonic-gate */ 13757c478bd9Sstevel@tonic-gate if (walk_arg.new_nvlp) { 13767c478bd9Sstevel@tonic-gate filestat[0] = stat->st_size; 13777c478bd9Sstevel@tonic-gate filestat[1] = stat->st_mtime; 13787c478bd9Sstevel@tonic-gate error = nvlist_add_uint64_array(walk_arg.new_nvlp, 13797c478bd9Sstevel@tonic-gate file + bam_rootlen, filestat, 2); 13807c478bd9Sstevel@tonic-gate if (error) 13817c478bd9Sstevel@tonic-gate bam_error(NVADD_FAIL, file, strerror(error)); 13827c478bd9Sstevel@tonic-gate } 13837c478bd9Sstevel@tonic-gate 13847c478bd9Sstevel@tonic-gate /* 13857c478bd9Sstevel@tonic-gate * The remaining steps are only required if we haven't made a 13867c478bd9Sstevel@tonic-gate * decision about update or if we are checking (-n) 13877c478bd9Sstevel@tonic-gate */ 13887c478bd9Sstevel@tonic-gate if (walk_arg.need_update && !bam_check) 13897c478bd9Sstevel@tonic-gate return (0); 13907c478bd9Sstevel@tonic-gate 13917c478bd9Sstevel@tonic-gate /* 13927c478bd9Sstevel@tonic-gate * If we are invoked as part of system/filesyste/boot-archive 13937c478bd9Sstevel@tonic-gate * SMF service, ignore amd64 modules unless we are booted amd64. 13947c478bd9Sstevel@tonic-gate */ 13958c1b6884Sszhou if (bam_smf_check && !is_amd64() && strstr(file, "/amd64/") != 0) 13967c478bd9Sstevel@tonic-gate return (0); 13977c478bd9Sstevel@tonic-gate 13987c478bd9Sstevel@tonic-gate /* 13997c478bd9Sstevel@tonic-gate * We need an update if file doesn't exist in old archive 14007c478bd9Sstevel@tonic-gate */ 14017c478bd9Sstevel@tonic-gate if (walk_arg.old_nvlp == NULL || 14027c478bd9Sstevel@tonic-gate nvlist_lookup_uint64_array(walk_arg.old_nvlp, 14037c478bd9Sstevel@tonic-gate file + bam_rootlen, &value, &sz) != 0) { 14047c478bd9Sstevel@tonic-gate if (bam_smf_check) /* ignore new during smf check */ 14057c478bd9Sstevel@tonic-gate return (0); 14067c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 14077c478bd9Sstevel@tonic-gate if (bam_verbose) 14087c478bd9Sstevel@tonic-gate bam_print(PARSEABLE_NEW_FILE, file); 14097c478bd9Sstevel@tonic-gate return (0); 14107c478bd9Sstevel@tonic-gate } 14117c478bd9Sstevel@tonic-gate 14127c478bd9Sstevel@tonic-gate /* 14137c478bd9Sstevel@tonic-gate * File exists in old archive. Check if file has changed 14147c478bd9Sstevel@tonic-gate */ 14157c478bd9Sstevel@tonic-gate assert(sz == 2); 14167c478bd9Sstevel@tonic-gate bcopy(value, filestat, sizeof (filestat)); 14177c478bd9Sstevel@tonic-gate 14187c478bd9Sstevel@tonic-gate if (filestat[0] != stat->st_size || 14197c478bd9Sstevel@tonic-gate filestat[1] != stat->st_mtime) { 14207c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 14217c478bd9Sstevel@tonic-gate if (bam_verbose) 14227c478bd9Sstevel@tonic-gate if (bam_smf_check) 14237c478bd9Sstevel@tonic-gate bam_print(" %s\n", file); 14247c478bd9Sstevel@tonic-gate else 14257c478bd9Sstevel@tonic-gate bam_print(PARSEABLE_OUT_DATE, file); 14267c478bd9Sstevel@tonic-gate } 14277c478bd9Sstevel@tonic-gate 14287c478bd9Sstevel@tonic-gate return (0); 14297c478bd9Sstevel@tonic-gate } 14307c478bd9Sstevel@tonic-gate 14317c478bd9Sstevel@tonic-gate /* 14327c478bd9Sstevel@tonic-gate * Check flags and presence of required files. 14337c478bd9Sstevel@tonic-gate * The force flag and/or absence of files should 14347c478bd9Sstevel@tonic-gate * trigger an update. 14357c478bd9Sstevel@tonic-gate * Suppress stdout output if check (-n) option is set 14367c478bd9Sstevel@tonic-gate * (as -n should only produce parseable output.) 14377c478bd9Sstevel@tonic-gate */ 14387c478bd9Sstevel@tonic-gate static void 14397c478bd9Sstevel@tonic-gate check_flags_and_files(char *root) 14407c478bd9Sstevel@tonic-gate { 14417c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 14427c478bd9Sstevel@tonic-gate struct stat sb; 14437c478bd9Sstevel@tonic-gate 14447c478bd9Sstevel@tonic-gate /* 14457c478bd9Sstevel@tonic-gate * if force, create archive unconditionally 14467c478bd9Sstevel@tonic-gate */ 14477c478bd9Sstevel@tonic-gate if (bam_force) { 14487c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 14497c478bd9Sstevel@tonic-gate if (bam_verbose && !bam_check) 14507c478bd9Sstevel@tonic-gate bam_print(UPDATE_FORCE); 14517c478bd9Sstevel@tonic-gate return; 14527c478bd9Sstevel@tonic-gate } 14537c478bd9Sstevel@tonic-gate 14547c478bd9Sstevel@tonic-gate /* 14557c478bd9Sstevel@tonic-gate * If archive is missing, create archive 14567c478bd9Sstevel@tonic-gate */ 14577c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE); 14587c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) { 14597c478bd9Sstevel@tonic-gate if (bam_verbose && !bam_check) 14607c478bd9Sstevel@tonic-gate bam_print(UPDATE_ARCH_MISS, path); 14617c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 14627c478bd9Sstevel@tonic-gate return; 14637c478bd9Sstevel@tonic-gate } 14647c478bd9Sstevel@tonic-gate } 14657c478bd9Sstevel@tonic-gate 14667c478bd9Sstevel@tonic-gate static error_t 14677c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t *flistp, char *filelist) 14687c478bd9Sstevel@tonic-gate { 14697c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 14707c478bd9Sstevel@tonic-gate FILE *fp; 14717c478bd9Sstevel@tonic-gate char buf[BAM_MAXLINE]; 14727c478bd9Sstevel@tonic-gate 14737c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, filelist); 14747c478bd9Sstevel@tonic-gate 14757c478bd9Sstevel@tonic-gate fp = fopen(path, "r"); 14767c478bd9Sstevel@tonic-gate if (fp == NULL) { 14777c478bd9Sstevel@tonic-gate if (bam_debug) 14787c478bd9Sstevel@tonic-gate bam_error(FLIST_FAIL, path, strerror(errno)); 14797c478bd9Sstevel@tonic-gate return (BAM_ERROR); 14807c478bd9Sstevel@tonic-gate } 14817c478bd9Sstevel@tonic-gate while (s_fgets(buf, sizeof (buf), fp) != NULL) { 1482b610f78eSvikram /* skip blank lines */ 1483b610f78eSvikram if (strspn(buf, " \t") == strlen(buf)) 1484b610f78eSvikram continue; 14857c478bd9Sstevel@tonic-gate append_to_flist(flistp, buf); 14867c478bd9Sstevel@tonic-gate } 14877c478bd9Sstevel@tonic-gate if (fclose(fp) != 0) { 14887c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, path, strerror(errno)); 14897c478bd9Sstevel@tonic-gate return (BAM_ERROR); 14907c478bd9Sstevel@tonic-gate } 14917c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 14927c478bd9Sstevel@tonic-gate } 14937c478bd9Sstevel@tonic-gate 14947c478bd9Sstevel@tonic-gate static error_t 14957c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t *flistp) 14967c478bd9Sstevel@tonic-gate { 14977c478bd9Sstevel@tonic-gate int rval; 14987c478bd9Sstevel@tonic-gate 14997c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 15007c478bd9Sstevel@tonic-gate 15017c478bd9Sstevel@tonic-gate /* 15027c478bd9Sstevel@tonic-gate * Read current lists of files - only the first is mandatory 15037c478bd9Sstevel@tonic-gate */ 15047c478bd9Sstevel@tonic-gate rval = read_one_list(root, flistp, BOOT_FILE_LIST); 15057c478bd9Sstevel@tonic-gate if (rval != BAM_SUCCESS) 15067c478bd9Sstevel@tonic-gate return (rval); 15077c478bd9Sstevel@tonic-gate (void) read_one_list(root, flistp, ETC_FILE_LIST); 15087c478bd9Sstevel@tonic-gate 15097c478bd9Sstevel@tonic-gate if (flistp->head == NULL) { 15107c478bd9Sstevel@tonic-gate bam_error(NO_FLIST); 15117c478bd9Sstevel@tonic-gate return (BAM_ERROR); 15127c478bd9Sstevel@tonic-gate } 15137c478bd9Sstevel@tonic-gate 15147c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 15157c478bd9Sstevel@tonic-gate } 15167c478bd9Sstevel@tonic-gate 15177c478bd9Sstevel@tonic-gate static void 15187c478bd9Sstevel@tonic-gate getoldstat(char *root) 15197c478bd9Sstevel@tonic-gate { 15207c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 15217c478bd9Sstevel@tonic-gate int fd, error; 15227c478bd9Sstevel@tonic-gate struct stat sb; 15237c478bd9Sstevel@tonic-gate char *ostat; 15247c478bd9Sstevel@tonic-gate 15257c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 15267c478bd9Sstevel@tonic-gate fd = open(path, O_RDONLY); 15277c478bd9Sstevel@tonic-gate if (fd == -1) { 15287c478bd9Sstevel@tonic-gate if (bam_verbose) 15297c478bd9Sstevel@tonic-gate bam_print(OPEN_FAIL, path, strerror(errno)); 15307c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 15317c478bd9Sstevel@tonic-gate return; 15327c478bd9Sstevel@tonic-gate } 15337c478bd9Sstevel@tonic-gate 15347c478bd9Sstevel@tonic-gate if (fstat(fd, &sb) != 0) { 15357c478bd9Sstevel@tonic-gate bam_error(STAT_FAIL, path, strerror(errno)); 15367c478bd9Sstevel@tonic-gate (void) close(fd); 15377c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 15387c478bd9Sstevel@tonic-gate return; 15397c478bd9Sstevel@tonic-gate } 15407c478bd9Sstevel@tonic-gate 15417c478bd9Sstevel@tonic-gate ostat = s_calloc(1, sb.st_size); 15427c478bd9Sstevel@tonic-gate 15437c478bd9Sstevel@tonic-gate if (read(fd, ostat, sb.st_size) != sb.st_size) { 15447c478bd9Sstevel@tonic-gate bam_error(READ_FAIL, path, strerror(errno)); 15457c478bd9Sstevel@tonic-gate (void) close(fd); 15467c478bd9Sstevel@tonic-gate free(ostat); 15477c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 15487c478bd9Sstevel@tonic-gate return; 15497c478bd9Sstevel@tonic-gate } 15507c478bd9Sstevel@tonic-gate 15517c478bd9Sstevel@tonic-gate (void) close(fd); 15527c478bd9Sstevel@tonic-gate 15537c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 15547c478bd9Sstevel@tonic-gate error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0); 15557c478bd9Sstevel@tonic-gate 15567c478bd9Sstevel@tonic-gate free(ostat); 15577c478bd9Sstevel@tonic-gate 15587c478bd9Sstevel@tonic-gate if (error) { 15597c478bd9Sstevel@tonic-gate bam_error(UNPACK_FAIL, path, strerror(error)); 15607c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 15617c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 15627c478bd9Sstevel@tonic-gate return; 15637c478bd9Sstevel@tonic-gate } 15647c478bd9Sstevel@tonic-gate } 15657c478bd9Sstevel@tonic-gate 15667c478bd9Sstevel@tonic-gate static void 15677c478bd9Sstevel@tonic-gate create_newstat(void) 15687c478bd9Sstevel@tonic-gate { 15697c478bd9Sstevel@tonic-gate int error; 15707c478bd9Sstevel@tonic-gate 15717c478bd9Sstevel@tonic-gate error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0); 15727c478bd9Sstevel@tonic-gate if (error) { 15737c478bd9Sstevel@tonic-gate /* 15747c478bd9Sstevel@tonic-gate * Not fatal - we can still create archive 15757c478bd9Sstevel@tonic-gate */ 15767c478bd9Sstevel@tonic-gate walk_arg.new_nvlp = NULL; 15777c478bd9Sstevel@tonic-gate bam_error(NVALLOC_FAIL, strerror(error)); 15787c478bd9Sstevel@tonic-gate } 15797c478bd9Sstevel@tonic-gate } 15807c478bd9Sstevel@tonic-gate 15817c478bd9Sstevel@tonic-gate static void 15827c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp) 15837c478bd9Sstevel@tonic-gate { 15847c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 15857c478bd9Sstevel@tonic-gate line_t *lp; 15867c478bd9Sstevel@tonic-gate 15877c478bd9Sstevel@tonic-gate for (lp = flistp->head; lp; lp = lp->next) { 15887c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, lp->line); 15897c478bd9Sstevel@tonic-gate /* XXX shouldn't we use FTW_MOUNT ? */ 15907c478bd9Sstevel@tonic-gate if (nftw(path, cmpstat, 20, 0) == -1) { 15917c478bd9Sstevel@tonic-gate /* 15927c478bd9Sstevel@tonic-gate * Some files may not exist. 15937c478bd9Sstevel@tonic-gate * For example: etc/rtc_config on a x86 diskless system 15947c478bd9Sstevel@tonic-gate * Emit verbose message only 15957c478bd9Sstevel@tonic-gate */ 15967c478bd9Sstevel@tonic-gate if (bam_verbose) 15977c478bd9Sstevel@tonic-gate bam_print(NFTW_FAIL, path, strerror(errno)); 15987c478bd9Sstevel@tonic-gate } 15997c478bd9Sstevel@tonic-gate } 16007c478bd9Sstevel@tonic-gate } 16017c478bd9Sstevel@tonic-gate 16027c478bd9Sstevel@tonic-gate static void 16037c478bd9Sstevel@tonic-gate savenew(char *root) 16047c478bd9Sstevel@tonic-gate { 16057c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 16067c478bd9Sstevel@tonic-gate char path2[PATH_MAX]; 16077c478bd9Sstevel@tonic-gate size_t sz; 16087c478bd9Sstevel@tonic-gate char *nstat; 16097c478bd9Sstevel@tonic-gate int fd, wrote, error; 16107c478bd9Sstevel@tonic-gate 16117c478bd9Sstevel@tonic-gate nstat = NULL; 16127c478bd9Sstevel@tonic-gate sz = 0; 16137c478bd9Sstevel@tonic-gate error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz, 16147c478bd9Sstevel@tonic-gate NV_ENCODE_XDR, 0); 16157c478bd9Sstevel@tonic-gate if (error) { 16167c478bd9Sstevel@tonic-gate bam_error(PACK_FAIL, strerror(error)); 16177c478bd9Sstevel@tonic-gate return; 16187c478bd9Sstevel@tonic-gate } 16197c478bd9Sstevel@tonic-gate 16207c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP); 16217c478bd9Sstevel@tonic-gate fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE); 16227c478bd9Sstevel@tonic-gate if (fd == -1) { 16237c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, path, strerror(errno)); 16247c478bd9Sstevel@tonic-gate free(nstat); 16257c478bd9Sstevel@tonic-gate return; 16267c478bd9Sstevel@tonic-gate } 16277c478bd9Sstevel@tonic-gate wrote = write(fd, nstat, sz); 16287c478bd9Sstevel@tonic-gate if (wrote != sz) { 16297c478bd9Sstevel@tonic-gate bam_error(WRITE_FAIL, path, strerror(errno)); 16307c478bd9Sstevel@tonic-gate (void) close(fd); 16317c478bd9Sstevel@tonic-gate free(nstat); 16327c478bd9Sstevel@tonic-gate return; 16337c478bd9Sstevel@tonic-gate } 16347c478bd9Sstevel@tonic-gate (void) close(fd); 16357c478bd9Sstevel@tonic-gate free(nstat); 16367c478bd9Sstevel@tonic-gate 16377c478bd9Sstevel@tonic-gate (void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT); 16387c478bd9Sstevel@tonic-gate if (rename(path, path2) != 0) { 16397c478bd9Sstevel@tonic-gate bam_error(RENAME_FAIL, path2, strerror(errno)); 16407c478bd9Sstevel@tonic-gate } 16417c478bd9Sstevel@tonic-gate } 16427c478bd9Sstevel@tonic-gate 16437c478bd9Sstevel@tonic-gate static void 16447c478bd9Sstevel@tonic-gate clear_walk_args(void) 16457c478bd9Sstevel@tonic-gate { 16467c478bd9Sstevel@tonic-gate if (walk_arg.old_nvlp) 16477c478bd9Sstevel@tonic-gate nvlist_free(walk_arg.old_nvlp); 16487c478bd9Sstevel@tonic-gate if (walk_arg.new_nvlp) 16497c478bd9Sstevel@tonic-gate nvlist_free(walk_arg.new_nvlp); 16507c478bd9Sstevel@tonic-gate walk_arg.need_update = 0; 16517c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 16527c478bd9Sstevel@tonic-gate walk_arg.new_nvlp = NULL; 16537c478bd9Sstevel@tonic-gate } 16547c478bd9Sstevel@tonic-gate 16557c478bd9Sstevel@tonic-gate /* 16567c478bd9Sstevel@tonic-gate * Returns: 16577c478bd9Sstevel@tonic-gate * 0 - no update necessary 16587c478bd9Sstevel@tonic-gate * 1 - update required. 16597c478bd9Sstevel@tonic-gate * BAM_ERROR (-1) - An error occurred 16607c478bd9Sstevel@tonic-gate * 16617c478bd9Sstevel@tonic-gate * Special handling for check (-n): 16627c478bd9Sstevel@tonic-gate * ================================ 16637c478bd9Sstevel@tonic-gate * The check (-n) option produces parseable output. 16647c478bd9Sstevel@tonic-gate * To do this, we suppress all stdout messages unrelated 16657c478bd9Sstevel@tonic-gate * to out of sync files. 16667c478bd9Sstevel@tonic-gate * All stderr messages are still printed though. 16677c478bd9Sstevel@tonic-gate * 16687c478bd9Sstevel@tonic-gate */ 16697c478bd9Sstevel@tonic-gate static int 16707c478bd9Sstevel@tonic-gate update_required(char *root) 16717c478bd9Sstevel@tonic-gate { 16727c478bd9Sstevel@tonic-gate struct stat sb; 16737c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 16747c478bd9Sstevel@tonic-gate filelist_t flist; 16757c478bd9Sstevel@tonic-gate filelist_t *flistp = &flist; 16767c478bd9Sstevel@tonic-gate int need_update; 16777c478bd9Sstevel@tonic-gate 16787c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 16797c478bd9Sstevel@tonic-gate 16807c478bd9Sstevel@tonic-gate walk_arg.need_update = 0; 16817c478bd9Sstevel@tonic-gate 16827c478bd9Sstevel@tonic-gate /* 16837c478bd9Sstevel@tonic-gate * Without consulting stat data, check if we need update 16847c478bd9Sstevel@tonic-gate */ 16857c478bd9Sstevel@tonic-gate check_flags_and_files(root); 16867c478bd9Sstevel@tonic-gate 16877c478bd9Sstevel@tonic-gate /* 16887c478bd9Sstevel@tonic-gate * In certain deployment scenarios, filestat may not 16897c478bd9Sstevel@tonic-gate * exist. Ignore it during boot-archive SMF check. 16907c478bd9Sstevel@tonic-gate */ 16917c478bd9Sstevel@tonic-gate if (bam_smf_check) { 16927c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 16937c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) 16947c478bd9Sstevel@tonic-gate return (0); 16957c478bd9Sstevel@tonic-gate } 16967c478bd9Sstevel@tonic-gate 16977c478bd9Sstevel@tonic-gate /* 16987c478bd9Sstevel@tonic-gate * consult stat data only if we haven't made a decision 16997c478bd9Sstevel@tonic-gate * about update. If checking (-n) however, we always 17007c478bd9Sstevel@tonic-gate * need stat data (since we want to compare old and new) 17017c478bd9Sstevel@tonic-gate */ 17027c478bd9Sstevel@tonic-gate if (!walk_arg.need_update || bam_check) 17037c478bd9Sstevel@tonic-gate getoldstat(root); 17047c478bd9Sstevel@tonic-gate 17057c478bd9Sstevel@tonic-gate /* 17067c478bd9Sstevel@tonic-gate * read list of files 17077c478bd9Sstevel@tonic-gate */ 17087c478bd9Sstevel@tonic-gate if (read_list(root, flistp) != BAM_SUCCESS) { 17097c478bd9Sstevel@tonic-gate clear_walk_args(); 17107c478bd9Sstevel@tonic-gate return (BAM_ERROR); 17117c478bd9Sstevel@tonic-gate } 17127c478bd9Sstevel@tonic-gate 17137c478bd9Sstevel@tonic-gate assert(flistp->head && flistp->tail); 17147c478bd9Sstevel@tonic-gate 17157c478bd9Sstevel@tonic-gate /* 17167c478bd9Sstevel@tonic-gate * At this point either the update is required 17177c478bd9Sstevel@tonic-gate * or the decision is pending. In either case 17187c478bd9Sstevel@tonic-gate * we need to create new stat nvlist 17197c478bd9Sstevel@tonic-gate */ 17207c478bd9Sstevel@tonic-gate create_newstat(); 17217c478bd9Sstevel@tonic-gate 17227c478bd9Sstevel@tonic-gate /* 17237c478bd9Sstevel@tonic-gate * This walk does 2 things: 17247c478bd9Sstevel@tonic-gate * - gets new stat data for every file 17257c478bd9Sstevel@tonic-gate * - (optional) compare old and new stat data 17267c478bd9Sstevel@tonic-gate */ 17277c478bd9Sstevel@tonic-gate walk_list(root, &flist); 17287c478bd9Sstevel@tonic-gate 17297c478bd9Sstevel@tonic-gate /* done with the file list */ 17307c478bd9Sstevel@tonic-gate filelist_free(flistp); 17317c478bd9Sstevel@tonic-gate 17327c478bd9Sstevel@tonic-gate /* 17337c478bd9Sstevel@tonic-gate * if we didn't succeed in creating new stat data above 17347c478bd9Sstevel@tonic-gate * just return result of update check so that archive is built. 17357c478bd9Sstevel@tonic-gate */ 17367c478bd9Sstevel@tonic-gate if (walk_arg.new_nvlp == NULL) { 17377c478bd9Sstevel@tonic-gate bam_error(NO_NEW_STAT); 17387c478bd9Sstevel@tonic-gate need_update = walk_arg.need_update; 17397c478bd9Sstevel@tonic-gate clear_walk_args(); 17407c478bd9Sstevel@tonic-gate return (need_update ? 1 : 0); 17417c478bd9Sstevel@tonic-gate } 17427c478bd9Sstevel@tonic-gate 17437c478bd9Sstevel@tonic-gate 17447c478bd9Sstevel@tonic-gate /* 17457c478bd9Sstevel@tonic-gate * If no update required, discard newstat 17467c478bd9Sstevel@tonic-gate */ 17477c478bd9Sstevel@tonic-gate if (!walk_arg.need_update) { 17487c478bd9Sstevel@tonic-gate clear_walk_args(); 17497c478bd9Sstevel@tonic-gate return (0); 17507c478bd9Sstevel@tonic-gate } 17517c478bd9Sstevel@tonic-gate 17527c478bd9Sstevel@tonic-gate /* 17537c478bd9Sstevel@tonic-gate * At this point we need an update - so save new stat data 17547c478bd9Sstevel@tonic-gate * However, if only checking (-n), don't save new stat data. 17557c478bd9Sstevel@tonic-gate */ 17567c478bd9Sstevel@tonic-gate if (!bam_check) 17577c478bd9Sstevel@tonic-gate savenew(root); 17587c478bd9Sstevel@tonic-gate 17597c478bd9Sstevel@tonic-gate clear_walk_args(); 17607c478bd9Sstevel@tonic-gate 17617c478bd9Sstevel@tonic-gate return (1); 17627c478bd9Sstevel@tonic-gate } 17637c478bd9Sstevel@tonic-gate 17647c478bd9Sstevel@tonic-gate static error_t 17657c478bd9Sstevel@tonic-gate create_ramdisk(char *root) 17667c478bd9Sstevel@tonic-gate { 17677c478bd9Sstevel@tonic-gate char *cmdline, path[PATH_MAX]; 17687c478bd9Sstevel@tonic-gate size_t len; 17697c478bd9Sstevel@tonic-gate struct stat sb; 17707c478bd9Sstevel@tonic-gate 17717c478bd9Sstevel@tonic-gate /* 17727c478bd9Sstevel@tonic-gate * Setup command args for create_ramdisk.ksh 17737c478bd9Sstevel@tonic-gate */ 17747c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK); 17757c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) { 17767c478bd9Sstevel@tonic-gate bam_error(ARCH_EXEC_MISS, path, strerror(errno)); 17777c478bd9Sstevel@tonic-gate return (BAM_ERROR); 17787c478bd9Sstevel@tonic-gate } 17797c478bd9Sstevel@tonic-gate 17807c478bd9Sstevel@tonic-gate len = strlen(path) + strlen(root) + 10; /* room for space + -R */ 17817c478bd9Sstevel@tonic-gate cmdline = s_calloc(1, len); 17827c478bd9Sstevel@tonic-gate 17837c478bd9Sstevel@tonic-gate if (strlen(root) > 1) { 17847c478bd9Sstevel@tonic-gate (void) snprintf(cmdline, len, "%s -R %s", path, root); 17857c478bd9Sstevel@tonic-gate /* chop off / at the end */ 17867c478bd9Sstevel@tonic-gate cmdline[strlen(cmdline) - 1] = '\0'; 17877c478bd9Sstevel@tonic-gate } else 17887c478bd9Sstevel@tonic-gate (void) snprintf(cmdline, len, "%s", path); 17897c478bd9Sstevel@tonic-gate 17907c478bd9Sstevel@tonic-gate if (exec_cmd(cmdline, NULL, 0) != 0) { 17917c478bd9Sstevel@tonic-gate bam_error(ARCHIVE_FAIL, cmdline); 17927c478bd9Sstevel@tonic-gate free(cmdline); 17937c478bd9Sstevel@tonic-gate return (BAM_ERROR); 17947c478bd9Sstevel@tonic-gate } 17957c478bd9Sstevel@tonic-gate free(cmdline); 17967c478bd9Sstevel@tonic-gate 17977c478bd9Sstevel@tonic-gate /* 17987c478bd9Sstevel@tonic-gate * Verify that the archive has been created 17997c478bd9Sstevel@tonic-gate */ 18007c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE); 18017c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) { 18027c478bd9Sstevel@tonic-gate bam_error(ARCHIVE_NOT_CREATED, path); 18037c478bd9Sstevel@tonic-gate return (BAM_ERROR); 18047c478bd9Sstevel@tonic-gate } 18057c478bd9Sstevel@tonic-gate 18067c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 18077c478bd9Sstevel@tonic-gate } 18087c478bd9Sstevel@tonic-gate 18097c478bd9Sstevel@tonic-gate /* 18107c478bd9Sstevel@tonic-gate * Checks if target filesystem is on a ramdisk 18117c478bd9Sstevel@tonic-gate * 1 - is miniroot 18127c478bd9Sstevel@tonic-gate * 0 - is not 18137c478bd9Sstevel@tonic-gate * When in doubt assume it is not a ramdisk. 18147c478bd9Sstevel@tonic-gate */ 18157c478bd9Sstevel@tonic-gate static int 18167c478bd9Sstevel@tonic-gate is_ramdisk(char *root) 18177c478bd9Sstevel@tonic-gate { 18187c478bd9Sstevel@tonic-gate struct extmnttab mnt; 18197c478bd9Sstevel@tonic-gate FILE *fp; 18207c478bd9Sstevel@tonic-gate int found; 1821b610f78eSvikram char mntpt[PATH_MAX]; 1822b610f78eSvikram char *cp; 18237c478bd9Sstevel@tonic-gate 18247c478bd9Sstevel@tonic-gate /* 18257c478bd9Sstevel@tonic-gate * There are 3 situations where creating archive is 18267c478bd9Sstevel@tonic-gate * of dubious value: 1827b610f78eSvikram * - create boot_archive on a lofi-mounted boot_archive 18287c478bd9Sstevel@tonic-gate * - create it on a ramdisk which is the root filesystem 18297c478bd9Sstevel@tonic-gate * - create it on a ramdisk mounted somewhere else 18307c478bd9Sstevel@tonic-gate * The first is not easy to detect and checking for it is not 18317c478bd9Sstevel@tonic-gate * worth it. 18327c478bd9Sstevel@tonic-gate * The other two conditions are handled here 18337c478bd9Sstevel@tonic-gate */ 18347c478bd9Sstevel@tonic-gate 18357c478bd9Sstevel@tonic-gate fp = fopen(MNTTAB, "r"); 18367c478bd9Sstevel@tonic-gate if (fp == NULL) { 18377c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 18387c478bd9Sstevel@tonic-gate return (0); 18397c478bd9Sstevel@tonic-gate } 18407c478bd9Sstevel@tonic-gate 18417c478bd9Sstevel@tonic-gate resetmnttab(fp); 18427c478bd9Sstevel@tonic-gate 1843b610f78eSvikram /* 1844b610f78eSvikram * Remove any trailing / from the mount point 1845b610f78eSvikram */ 1846b610f78eSvikram (void) strlcpy(mntpt, root, sizeof (mntpt)); 1847b610f78eSvikram if (strcmp(root, "/") != 0) { 1848b610f78eSvikram cp = mntpt + strlen(mntpt) - 1; 1849b610f78eSvikram if (*cp == '/') 1850b610f78eSvikram *cp = '\0'; 1851b610f78eSvikram } 18527c478bd9Sstevel@tonic-gate found = 0; 18537c478bd9Sstevel@tonic-gate while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 1854b610f78eSvikram if (strcmp(mnt.mnt_mountp, mntpt) == 0) { 18557c478bd9Sstevel@tonic-gate found = 1; 18567c478bd9Sstevel@tonic-gate break; 18577c478bd9Sstevel@tonic-gate } 18587c478bd9Sstevel@tonic-gate } 18597c478bd9Sstevel@tonic-gate 18607c478bd9Sstevel@tonic-gate if (!found) { 18617c478bd9Sstevel@tonic-gate if (bam_verbose) 1862b610f78eSvikram bam_error(NOT_IN_MNTTAB, mntpt); 18637c478bd9Sstevel@tonic-gate (void) fclose(fp); 18647c478bd9Sstevel@tonic-gate return (0); 18657c478bd9Sstevel@tonic-gate } 18667c478bd9Sstevel@tonic-gate 18677c478bd9Sstevel@tonic-gate if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) { 18687c478bd9Sstevel@tonic-gate if (bam_verbose) 18697c478bd9Sstevel@tonic-gate bam_error(IS_RAMDISK, bam_root); 18707c478bd9Sstevel@tonic-gate (void) fclose(fp); 18717c478bd9Sstevel@tonic-gate return (1); 18727c478bd9Sstevel@tonic-gate } 18737c478bd9Sstevel@tonic-gate 18747c478bd9Sstevel@tonic-gate (void) fclose(fp); 18757c478bd9Sstevel@tonic-gate 18767c478bd9Sstevel@tonic-gate return (0); 18777c478bd9Sstevel@tonic-gate } 18787c478bd9Sstevel@tonic-gate 18797c478bd9Sstevel@tonic-gate static int 18807c478bd9Sstevel@tonic-gate is_newboot(char *root) 18817c478bd9Sstevel@tonic-gate { 18827c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 18837c478bd9Sstevel@tonic-gate struct stat sb; 18847c478bd9Sstevel@tonic-gate 18857c478bd9Sstevel@tonic-gate /* 18867c478bd9Sstevel@tonic-gate * We can't boot without MULTI_BOOT 18877c478bd9Sstevel@tonic-gate */ 18887c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT); 18897c478bd9Sstevel@tonic-gate if (stat(path, &sb) == -1) { 18907c478bd9Sstevel@tonic-gate if (bam_verbose) 18917c478bd9Sstevel@tonic-gate bam_print(FILE_MISS, path); 18927c478bd9Sstevel@tonic-gate return (0); 18937c478bd9Sstevel@tonic-gate } 18947c478bd9Sstevel@tonic-gate 18957c478bd9Sstevel@tonic-gate /* 18967c478bd9Sstevel@tonic-gate * We can't generate archive without GRUB_DIR 18977c478bd9Sstevel@tonic-gate */ 18987c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR); 18997c478bd9Sstevel@tonic-gate if (stat(path, &sb) == -1) { 19007c478bd9Sstevel@tonic-gate if (bam_verbose) 19017c478bd9Sstevel@tonic-gate bam_print(DIR_MISS, path); 19027c478bd9Sstevel@tonic-gate return (0); 19037c478bd9Sstevel@tonic-gate } 19047c478bd9Sstevel@tonic-gate 19057c478bd9Sstevel@tonic-gate return (1); 19067c478bd9Sstevel@tonic-gate } 19077c478bd9Sstevel@tonic-gate 19087c478bd9Sstevel@tonic-gate static int 19097c478bd9Sstevel@tonic-gate is_readonly(char *root) 19107c478bd9Sstevel@tonic-gate { 19117c478bd9Sstevel@tonic-gate struct statvfs vfs; 19127c478bd9Sstevel@tonic-gate 19137c478bd9Sstevel@tonic-gate /* 19147c478bd9Sstevel@tonic-gate * Check for RDONLY filesystem 19157c478bd9Sstevel@tonic-gate * When in doubt assume it is not readonly 19167c478bd9Sstevel@tonic-gate */ 19177c478bd9Sstevel@tonic-gate if (statvfs(root, &vfs) != 0) { 19187c478bd9Sstevel@tonic-gate if (bam_verbose) 19197c478bd9Sstevel@tonic-gate bam_error(STATVFS_FAIL, root, strerror(errno)); 19207c478bd9Sstevel@tonic-gate return (0); 19217c478bd9Sstevel@tonic-gate } 19227c478bd9Sstevel@tonic-gate 19237c478bd9Sstevel@tonic-gate if (vfs.f_flag & ST_RDONLY) { 19247c478bd9Sstevel@tonic-gate return (1); 19257c478bd9Sstevel@tonic-gate } 19267c478bd9Sstevel@tonic-gate 19277c478bd9Sstevel@tonic-gate return (0); 19287c478bd9Sstevel@tonic-gate } 19297c478bd9Sstevel@tonic-gate 19307c478bd9Sstevel@tonic-gate static error_t 19317c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt) 19327c478bd9Sstevel@tonic-gate { 19337c478bd9Sstevel@tonic-gate error_t ret; 19347c478bd9Sstevel@tonic-gate 19357c478bd9Sstevel@tonic-gate assert(root); 19367c478bd9Sstevel@tonic-gate assert(opt == NULL); 19377c478bd9Sstevel@tonic-gate 19387c478bd9Sstevel@tonic-gate /* 1939b610f78eSvikram * root must belong to a GRUB boot OS, 19407c478bd9Sstevel@tonic-gate * don't care on sparc except for diskless clients 19417c478bd9Sstevel@tonic-gate */ 19427c478bd9Sstevel@tonic-gate if (!is_newboot(root)) { 1943b610f78eSvikram /* 1944b610f78eSvikram * Emit message only if not in context of update_all. 1945b610f78eSvikram * If in update_all, emit only if verbose flag is set. 1946b610f78eSvikram */ 1947b610f78eSvikram if (!bam_update_all || bam_verbose) 1948b610f78eSvikram bam_print(NOT_GRUB_BOOT, root); 19497c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 19507c478bd9Sstevel@tonic-gate } 19517c478bd9Sstevel@tonic-gate 19527c478bd9Sstevel@tonic-gate /* 19538c1b6884Sszhou * If smf check is requested when / is writable (can happen 19548c1b6884Sszhou * on first reboot following an upgrade because service 19558c1b6884Sszhou * dependency is messed up), skip the check. 19568c1b6884Sszhou */ 19578c1b6884Sszhou if (bam_smf_check && !bam_root_readonly) 19588c1b6884Sszhou return (BAM_SUCCESS); 19598c1b6884Sszhou 19608c1b6884Sszhou /* 19618c1b6884Sszhou * root must be writable. This check applies to alternate 19628c1b6884Sszhou * root (-R option); bam_root_readonly applies to '/' only. 19637c478bd9Sstevel@tonic-gate * Note: statvfs() does not always report the truth 19647c478bd9Sstevel@tonic-gate */ 196561cb17bdSsetje if (!bam_smf_check && !bam_check && is_readonly(root)) { 19668c1b6884Sszhou if (bam_verbose) 19677c478bd9Sstevel@tonic-gate bam_print(RDONLY_FS, root); 19687c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 19697c478bd9Sstevel@tonic-gate } 19707c478bd9Sstevel@tonic-gate 19717c478bd9Sstevel@tonic-gate /* 19727c478bd9Sstevel@tonic-gate * Don't generate archive on ramdisk 19737c478bd9Sstevel@tonic-gate */ 19747c478bd9Sstevel@tonic-gate if (is_ramdisk(root)) { 19757c478bd9Sstevel@tonic-gate if (bam_verbose) 19767c478bd9Sstevel@tonic-gate bam_print(SKIP_RAMDISK); 19777c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 19787c478bd9Sstevel@tonic-gate } 19797c478bd9Sstevel@tonic-gate 19807c478bd9Sstevel@tonic-gate /* 19817c478bd9Sstevel@tonic-gate * Now check if updated is really needed 19827c478bd9Sstevel@tonic-gate */ 19837c478bd9Sstevel@tonic-gate ret = update_required(root); 19847c478bd9Sstevel@tonic-gate 19857c478bd9Sstevel@tonic-gate /* 19867c478bd9Sstevel@tonic-gate * The check command (-n) is *not* a dry run 19877c478bd9Sstevel@tonic-gate * It only checks if the archive is in sync. 19887c478bd9Sstevel@tonic-gate */ 19897c478bd9Sstevel@tonic-gate if (bam_check) { 19907c478bd9Sstevel@tonic-gate bam_exit((ret != 0) ? 1 : 0); 19917c478bd9Sstevel@tonic-gate } 19927c478bd9Sstevel@tonic-gate 19937c478bd9Sstevel@tonic-gate if (ret == 1) { 19947c478bd9Sstevel@tonic-gate /* create the ramdisk */ 19957c478bd9Sstevel@tonic-gate ret = create_ramdisk(root); 19967c478bd9Sstevel@tonic-gate } 19977c478bd9Sstevel@tonic-gate return (ret); 19987c478bd9Sstevel@tonic-gate } 19997c478bd9Sstevel@tonic-gate 2000fbac2b2bSvikram static void 2001fbac2b2bSvikram update_fdisk(void) 2002fbac2b2bSvikram { 2003fbac2b2bSvikram struct stat sb; 2004fbac2b2bSvikram char cmd[PATH_MAX]; 2005fbac2b2bSvikram int ret1, ret2; 2006fbac2b2bSvikram 2007fbac2b2bSvikram assert(stat(GRUB_fdisk, &sb) == 0); 2008fbac2b2bSvikram assert(stat(GRUB_fdisk_target, &sb) == 0); 2009fbac2b2bSvikram 2010fbac2b2bSvikram (void) snprintf(cmd, sizeof (cmd), "/sbin/fdisk -F %s `/bin/cat %s`", 2011fbac2b2bSvikram GRUB_fdisk, GRUB_fdisk_target); 2012fbac2b2bSvikram 2013fbac2b2bSvikram bam_print(UPDATING_FDISK); 2014fbac2b2bSvikram if (exec_cmd(cmd, NULL, 0) != 0) { 2015fbac2b2bSvikram bam_error(FDISK_UPDATE_FAILED); 2016fbac2b2bSvikram } 2017fbac2b2bSvikram 2018fbac2b2bSvikram /* 2019fbac2b2bSvikram * We are done, remove the files. 2020fbac2b2bSvikram */ 2021fbac2b2bSvikram ret1 = unlink(GRUB_fdisk); 2022fbac2b2bSvikram ret2 = unlink(GRUB_fdisk_target); 2023fbac2b2bSvikram if (ret1 != 0 || ret2 != 0) { 2024fbac2b2bSvikram bam_error(FILE_REMOVE_FAILED, GRUB_fdisk, GRUB_fdisk_target); 2025fbac2b2bSvikram } 2026fbac2b2bSvikram } 2027fbac2b2bSvikram 2028b610f78eSvikram static void 2029b610f78eSvikram restore_grub_slice(void) 2030b610f78eSvikram { 2031b610f78eSvikram struct stat sb; 2032b610f78eSvikram char *mntpt, *physlice; 2033b610f78eSvikram int mnted; /* set if we did a mount */ 2034b610f78eSvikram char menupath[PATH_MAX], cmd[PATH_MAX]; 2035b610f78eSvikram 2036b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 2037b610f78eSvikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 2038b610f78eSvikram return; 2039b610f78eSvikram } 2040b610f78eSvikram 2041b610f78eSvikram /* 2042b610f78eSvikram * If we are doing an luactivate, don't attempt to restore GRUB or else 2043b610f78eSvikram * we may not be able to get to DCA boot environments. Let luactivate 2044b610f78eSvikram * handle GRUB/DCA installation 2045b610f78eSvikram */ 2046b610f78eSvikram if (stat(LU_ACTIVATE_FILE, &sb) == 0) { 2047b610f78eSvikram return; 2048b610f78eSvikram } 2049b610f78eSvikram 2050b610f78eSvikram mnted = 0; 2051b610f78eSvikram physlice = NULL; 205240541d5dSvikram mntpt = mount_grub_slice(&mnted, &physlice, NULL, NULL); 2053b610f78eSvikram if (mntpt == NULL) { 2054b610f78eSvikram bam_error(CANNOT_RESTORE_GRUB_SLICE); 2055b610f78eSvikram return; 2056b610f78eSvikram } 2057b610f78eSvikram 2058b610f78eSvikram (void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU); 2059b610f78eSvikram if (stat(menupath, &sb) == 0) { 206040541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2061b610f78eSvikram return; 2062b610f78eSvikram } 2063b610f78eSvikram 2064b610f78eSvikram /* 2065b610f78eSvikram * The menu is missing - we need to do a restore 2066b610f78eSvikram */ 2067b610f78eSvikram bam_print(RESTORING_GRUB); 2068b610f78eSvikram 2069b610f78eSvikram (void) snprintf(cmd, sizeof (cmd), "%s %s %s %s", 2070b610f78eSvikram INSTALLGRUB, STAGE1, STAGE2, physlice); 2071b610f78eSvikram 2072b610f78eSvikram if (exec_cmd(cmd, NULL, 0) != 0) { 2073b610f78eSvikram bam_error(RESTORE_GRUB_FAILED); 207440541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2075b610f78eSvikram return; 2076b610f78eSvikram } 2077b610f78eSvikram 2078b610f78eSvikram if (stat(GRUB_backup_menu, &sb) != 0) { 2079b610f78eSvikram bam_error(MISSING_BACKUP_MENU, 2080b610f78eSvikram GRUB_backup_menu, strerror(errno)); 208140541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2082b610f78eSvikram return; 2083b610f78eSvikram } 2084b610f78eSvikram 2085b610f78eSvikram (void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s", 2086b610f78eSvikram GRUB_backup_menu, menupath); 2087b610f78eSvikram 2088b610f78eSvikram if (exec_cmd(cmd, NULL, 0) != 0) { 2089b610f78eSvikram bam_error(RESTORE_MENU_FAILED, menupath); 209040541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2091b610f78eSvikram return; 2092b610f78eSvikram } 2093b610f78eSvikram 2094b610f78eSvikram /* Success */ 209540541d5dSvikram umount_grub_slice(mnted, mntpt, physlice, NULL, NULL); 2096b610f78eSvikram } 2097b610f78eSvikram 20987c478bd9Sstevel@tonic-gate static error_t 20997c478bd9Sstevel@tonic-gate update_all(char *root, char *opt) 21007c478bd9Sstevel@tonic-gate { 21017c478bd9Sstevel@tonic-gate struct extmnttab mnt; 21027c478bd9Sstevel@tonic-gate struct stat sb; 21037c478bd9Sstevel@tonic-gate FILE *fp; 21047c478bd9Sstevel@tonic-gate char multibt[PATH_MAX]; 21057c478bd9Sstevel@tonic-gate error_t ret = BAM_SUCCESS; 2106fbac2b2bSvikram int ret1, ret2; 21077c478bd9Sstevel@tonic-gate 210840541d5dSvikram assert(root); 21097c478bd9Sstevel@tonic-gate assert(opt == NULL); 21107c478bd9Sstevel@tonic-gate 211140541d5dSvikram if (bam_rootlen != 1 || *root != '/') { 211240541d5dSvikram elide_trailing_slash(root, multibt, sizeof (multibt)); 211340541d5dSvikram bam_error(ALT_ROOT_INVALID, multibt); 211440541d5dSvikram return (BAM_ERROR); 211540541d5dSvikram } 211640541d5dSvikram 21177c478bd9Sstevel@tonic-gate /* 21187c478bd9Sstevel@tonic-gate * First update archive for current root 21197c478bd9Sstevel@tonic-gate */ 21207c478bd9Sstevel@tonic-gate if (update_archive(root, opt) != BAM_SUCCESS) 21217c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 21227c478bd9Sstevel@tonic-gate 21237c478bd9Sstevel@tonic-gate /* 21247c478bd9Sstevel@tonic-gate * Now walk the mount table, performing archive update 21257c478bd9Sstevel@tonic-gate * for all mounted Newboot root filesystems 21267c478bd9Sstevel@tonic-gate */ 21277c478bd9Sstevel@tonic-gate fp = fopen(MNTTAB, "r"); 21287c478bd9Sstevel@tonic-gate if (fp == NULL) { 21297c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 2130b610f78eSvikram ret = BAM_ERROR; 2131b610f78eSvikram goto out; 21327c478bd9Sstevel@tonic-gate } 21337c478bd9Sstevel@tonic-gate 21347c478bd9Sstevel@tonic-gate resetmnttab(fp); 21357c478bd9Sstevel@tonic-gate 21367c478bd9Sstevel@tonic-gate while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 21377c478bd9Sstevel@tonic-gate if (mnt.mnt_special == NULL) 21387c478bd9Sstevel@tonic-gate continue; 21397c478bd9Sstevel@tonic-gate if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0) 21407c478bd9Sstevel@tonic-gate continue; 21417c478bd9Sstevel@tonic-gate if (strcmp(mnt.mnt_mountp, "/") == 0) 21427c478bd9Sstevel@tonic-gate continue; 21437c478bd9Sstevel@tonic-gate 21447c478bd9Sstevel@tonic-gate (void) snprintf(multibt, sizeof (multibt), "%s%s", 21457c478bd9Sstevel@tonic-gate mnt.mnt_mountp, MULTI_BOOT); 21467c478bd9Sstevel@tonic-gate 21477c478bd9Sstevel@tonic-gate if (stat(multibt, &sb) == -1) 21487c478bd9Sstevel@tonic-gate continue; 21497c478bd9Sstevel@tonic-gate 21507c478bd9Sstevel@tonic-gate /* 21517c478bd9Sstevel@tonic-gate * We put a trailing slash to be consistent with root = "/" 21527c478bd9Sstevel@tonic-gate * case, such that we don't have to print // in some cases. 21537c478bd9Sstevel@tonic-gate */ 21547c478bd9Sstevel@tonic-gate (void) snprintf(rootbuf, sizeof (rootbuf), "%s/", 21557c478bd9Sstevel@tonic-gate mnt.mnt_mountp); 21567c478bd9Sstevel@tonic-gate bam_rootlen = strlen(rootbuf); 21577c478bd9Sstevel@tonic-gate if (update_archive(rootbuf, opt) != BAM_SUCCESS) 21587c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 21597c478bd9Sstevel@tonic-gate } 21607c478bd9Sstevel@tonic-gate 21617c478bd9Sstevel@tonic-gate (void) fclose(fp); 21627c478bd9Sstevel@tonic-gate 2163b610f78eSvikram out: 2164b610f78eSvikram if (stat(GRUB_slice, &sb) == 0) { 2165b610f78eSvikram restore_grub_slice(); 2166b610f78eSvikram } 2167b610f78eSvikram 2168fbac2b2bSvikram /* 2169fbac2b2bSvikram * Update fdisk table as we go down. Updating it when 2170fbac2b2bSvikram * the system is running will confuse biosdev. 2171fbac2b2bSvikram */ 2172fbac2b2bSvikram ret1 = stat(GRUB_fdisk, &sb); 2173fbac2b2bSvikram ret2 = stat(GRUB_fdisk_target, &sb); 2174fbac2b2bSvikram if ((ret1 == 0) && (ret2 == 0)) { 2175fbac2b2bSvikram update_fdisk(); 2176fbac2b2bSvikram } else if ((ret1 == 0) ^ (ret2 == 0)) { 2177fbac2b2bSvikram /* 2178fbac2b2bSvikram * It is an error for one file to be 2179fbac2b2bSvikram * present and the other absent. 2180fbac2b2bSvikram * It is normal for both files to be 2181fbac2b2bSvikram * absent - it indicates that no fdisk 2182fbac2b2bSvikram * update is required. 2183fbac2b2bSvikram */ 2184fbac2b2bSvikram bam_error(MISSING_FDISK_FILE, 2185fbac2b2bSvikram ret1 ? GRUB_fdisk : GRUB_fdisk_target); 2186fbac2b2bSvikram ret = BAM_ERROR; 2187fbac2b2bSvikram } 2188fbac2b2bSvikram 21897c478bd9Sstevel@tonic-gate return (ret); 21907c478bd9Sstevel@tonic-gate } 21917c478bd9Sstevel@tonic-gate 21927c478bd9Sstevel@tonic-gate static void 21937c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp) 21947c478bd9Sstevel@tonic-gate { 21957c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 21967c478bd9Sstevel@tonic-gate mp->start = lp; 21977c478bd9Sstevel@tonic-gate } else { 21987c478bd9Sstevel@tonic-gate mp->end->next = lp; 21998c1b6884Sszhou lp->prev = mp->end; 22007c478bd9Sstevel@tonic-gate } 22017c478bd9Sstevel@tonic-gate mp->end = lp; 22027c478bd9Sstevel@tonic-gate } 22037c478bd9Sstevel@tonic-gate 22048c1b6884Sszhou static void 22058c1b6884Sszhou unlink_line(menu_t *mp, line_t *lp) 22068c1b6884Sszhou { 22078c1b6884Sszhou /* unlink from list */ 22088c1b6884Sszhou if (lp->prev) 22098c1b6884Sszhou lp->prev->next = lp->next; 22108c1b6884Sszhou else 22118c1b6884Sszhou mp->start = lp->next; 22128c1b6884Sszhou if (lp->next) 22138c1b6884Sszhou lp->next->prev = lp->prev; 22148c1b6884Sszhou else 22158c1b6884Sszhou mp->end = lp->prev; 22168c1b6884Sszhou } 22178c1b6884Sszhou 22188c1b6884Sszhou static entry_t * 22198c1b6884Sszhou boot_entry_new(menu_t *mp, line_t *start, line_t *end) 22208c1b6884Sszhou { 22218c1b6884Sszhou entry_t *ent, *prev; 22228c1b6884Sszhou 22238c1b6884Sszhou ent = s_calloc(1, sizeof (entry_t)); 22248c1b6884Sszhou ent->start = start; 22258c1b6884Sszhou ent->end = end; 22268c1b6884Sszhou 22278c1b6884Sszhou if (mp->entries == NULL) { 22288c1b6884Sszhou mp->entries = ent; 22298c1b6884Sszhou return (ent); 22308c1b6884Sszhou } 22318c1b6884Sszhou 22328c1b6884Sszhou prev = mp->entries; 22338c1b6884Sszhou while (prev->next) 22348c1b6884Sszhou prev = prev-> next; 22358c1b6884Sszhou prev->next = ent; 22368c1b6884Sszhou ent->prev = prev; 22378c1b6884Sszhou return (ent); 22388c1b6884Sszhou } 22398c1b6884Sszhou 22408c1b6884Sszhou static void 22418c1b6884Sszhou boot_entry_addline(entry_t *ent, line_t *lp) 22428c1b6884Sszhou { 22438c1b6884Sszhou if (ent) 22448c1b6884Sszhou ent->end = lp; 22458c1b6884Sszhou } 22468c1b6884Sszhou 22477c478bd9Sstevel@tonic-gate /* 22487c478bd9Sstevel@tonic-gate * A line in menu.lst looks like 22497c478bd9Sstevel@tonic-gate * [ ]*<cmd>[ \t=]*<arg>* 22507c478bd9Sstevel@tonic-gate */ 22517c478bd9Sstevel@tonic-gate static void 22527c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum) 22537c478bd9Sstevel@tonic-gate { 22547c478bd9Sstevel@tonic-gate /* 22557c478bd9Sstevel@tonic-gate * save state across calls. This is so that 22567c478bd9Sstevel@tonic-gate * header gets the right entry# after title has 22577c478bd9Sstevel@tonic-gate * been processed 22587c478bd9Sstevel@tonic-gate */ 22598c1b6884Sszhou static line_t *prev = NULL; 22608c1b6884Sszhou static entry_t *curr_ent = NULL; 22617c478bd9Sstevel@tonic-gate 22627c478bd9Sstevel@tonic-gate line_t *lp; 22637c478bd9Sstevel@tonic-gate char *cmd, *sep, *arg; 22647c478bd9Sstevel@tonic-gate char save, *cp, *line; 22657c478bd9Sstevel@tonic-gate menu_flag_t flag = BAM_INVALID; 22667c478bd9Sstevel@tonic-gate 22677c478bd9Sstevel@tonic-gate if (str == NULL) { 22687c478bd9Sstevel@tonic-gate return; 22697c478bd9Sstevel@tonic-gate } 22707c478bd9Sstevel@tonic-gate 22717c478bd9Sstevel@tonic-gate /* 22727c478bd9Sstevel@tonic-gate * First save a copy of the entire line. 22737c478bd9Sstevel@tonic-gate * We use this later to set the line field. 22747c478bd9Sstevel@tonic-gate */ 22757c478bd9Sstevel@tonic-gate line = s_strdup(str); 22767c478bd9Sstevel@tonic-gate 22777c478bd9Sstevel@tonic-gate /* Eat up leading whitespace */ 22787c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 22797c478bd9Sstevel@tonic-gate str++; 22807c478bd9Sstevel@tonic-gate 22817c478bd9Sstevel@tonic-gate if (*str == '#') { /* comment */ 22827c478bd9Sstevel@tonic-gate cmd = s_strdup("#"); 22837c478bd9Sstevel@tonic-gate sep = NULL; 22847c478bd9Sstevel@tonic-gate arg = s_strdup(str + 1); 22857c478bd9Sstevel@tonic-gate flag = BAM_COMMENT; 22867c478bd9Sstevel@tonic-gate } else if (*str == '\0') { /* blank line */ 22877c478bd9Sstevel@tonic-gate cmd = sep = arg = NULL; 22887c478bd9Sstevel@tonic-gate flag = BAM_EMPTY; 22897c478bd9Sstevel@tonic-gate } else { 22907c478bd9Sstevel@tonic-gate /* 22917c478bd9Sstevel@tonic-gate * '=' is not a documented separator in grub syntax. 22927c478bd9Sstevel@tonic-gate * However various development bits use '=' as a 22937c478bd9Sstevel@tonic-gate * separator. In addition, external users also 22947c478bd9Sstevel@tonic-gate * use = as a separator. So we will allow that usage. 22957c478bd9Sstevel@tonic-gate */ 22967c478bd9Sstevel@tonic-gate cp = str; 22977c478bd9Sstevel@tonic-gate while (*str != ' ' && *str != '\t' && *str != '=') { 22987c478bd9Sstevel@tonic-gate if (*str == '\0') { 22997c478bd9Sstevel@tonic-gate cmd = s_strdup(cp); 23007c478bd9Sstevel@tonic-gate sep = arg = NULL; 23017c478bd9Sstevel@tonic-gate break; 23027c478bd9Sstevel@tonic-gate } 23037c478bd9Sstevel@tonic-gate str++; 23047c478bd9Sstevel@tonic-gate } 23057c478bd9Sstevel@tonic-gate 23067c478bd9Sstevel@tonic-gate if (*str != '\0') { 23077c478bd9Sstevel@tonic-gate save = *str; 23087c478bd9Sstevel@tonic-gate *str = '\0'; 23097c478bd9Sstevel@tonic-gate cmd = s_strdup(cp); 23107c478bd9Sstevel@tonic-gate *str = save; 23117c478bd9Sstevel@tonic-gate 23127c478bd9Sstevel@tonic-gate str++; 23137c478bd9Sstevel@tonic-gate save = *str; 23147c478bd9Sstevel@tonic-gate *str = '\0'; 23157c478bd9Sstevel@tonic-gate sep = s_strdup(str - 1); 23167c478bd9Sstevel@tonic-gate *str = save; 23177c478bd9Sstevel@tonic-gate 23187c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 23197c478bd9Sstevel@tonic-gate str++; 23207c478bd9Sstevel@tonic-gate if (*str == '\0') 23217c478bd9Sstevel@tonic-gate arg = NULL; 23227c478bd9Sstevel@tonic-gate else 23237c478bd9Sstevel@tonic-gate arg = s_strdup(str); 23247c478bd9Sstevel@tonic-gate } 23257c478bd9Sstevel@tonic-gate } 23267c478bd9Sstevel@tonic-gate 23277c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 23287c478bd9Sstevel@tonic-gate 23297c478bd9Sstevel@tonic-gate lp->cmd = cmd; 23307c478bd9Sstevel@tonic-gate lp->sep = sep; 23317c478bd9Sstevel@tonic-gate lp->arg = arg; 23327c478bd9Sstevel@tonic-gate lp->line = line; 23337c478bd9Sstevel@tonic-gate lp->lineNum = ++(*lineNum); 23347c478bd9Sstevel@tonic-gate if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) { 23357c478bd9Sstevel@tonic-gate lp->entryNum = ++(*entryNum); 23367c478bd9Sstevel@tonic-gate lp->flags = BAM_TITLE; 23377c478bd9Sstevel@tonic-gate if (prev && prev->flags == BAM_COMMENT && 23388c1b6884Sszhou prev->arg && strcmp(prev->arg, BAM_HDR) == 0) { 23397c478bd9Sstevel@tonic-gate prev->entryNum = lp->entryNum; 23408c1b6884Sszhou curr_ent = boot_entry_new(mp, prev, lp); 23418c1b6884Sszhou } else { 23428c1b6884Sszhou curr_ent = boot_entry_new(mp, lp, lp); 23438c1b6884Sszhou } 23447c478bd9Sstevel@tonic-gate } else if (flag != BAM_INVALID) { 23457c478bd9Sstevel@tonic-gate /* 23467c478bd9Sstevel@tonic-gate * For header comments, the entry# is "fixed up" 23477c478bd9Sstevel@tonic-gate * by the subsequent title 23487c478bd9Sstevel@tonic-gate */ 23497c478bd9Sstevel@tonic-gate lp->entryNum = *entryNum; 23507c478bd9Sstevel@tonic-gate lp->flags = flag; 23517c478bd9Sstevel@tonic-gate } else { 23527c478bd9Sstevel@tonic-gate lp->entryNum = *entryNum; 23537c478bd9Sstevel@tonic-gate lp->flags = (*entryNum == ENTRY_INIT) ? BAM_GLOBAL : BAM_ENTRY; 23547c478bd9Sstevel@tonic-gate } 23557c478bd9Sstevel@tonic-gate 23568c1b6884Sszhou /* record default, old default, and entry line ranges */ 23578c1b6884Sszhou if (lp->flags == BAM_GLOBAL && 23588c1b6884Sszhou strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0) { 23598c1b6884Sszhou mp->curdefault = lp; 23608c1b6884Sszhou } else if (lp->flags == BAM_COMMENT && 23618c1b6884Sszhou strncmp(lp->arg, BAM_OLDDEF, strlen(BAM_OLDDEF)) == 0) { 23628c1b6884Sszhou mp->olddefault = lp; 23638c1b6884Sszhou } else if (lp->flags == BAM_ENTRY || 23648c1b6884Sszhou (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_FTR) == 0)) { 23658c1b6884Sszhou boot_entry_addline(curr_ent, lp); 23668c1b6884Sszhou } 23677c478bd9Sstevel@tonic-gate append_line(mp, lp); 23687c478bd9Sstevel@tonic-gate 23697c478bd9Sstevel@tonic-gate prev = lp; 23707c478bd9Sstevel@tonic-gate } 23717c478bd9Sstevel@tonic-gate 237240541d5dSvikram static void 237340541d5dSvikram update_numbering(menu_t *mp) 237440541d5dSvikram { 237540541d5dSvikram int lineNum; 237640541d5dSvikram int entryNum; 237740541d5dSvikram int old_default_value; 237840541d5dSvikram line_t *lp, *prev, *default_lp, *default_entry; 237940541d5dSvikram char buf[PATH_MAX]; 238040541d5dSvikram 238140541d5dSvikram if (mp->start == NULL) { 238240541d5dSvikram return; 238340541d5dSvikram } 238440541d5dSvikram 238540541d5dSvikram lineNum = LINE_INIT; 238640541d5dSvikram entryNum = ENTRY_INIT; 238740541d5dSvikram old_default_value = ENTRY_INIT; 238840541d5dSvikram lp = default_lp = default_entry = NULL; 238940541d5dSvikram 239040541d5dSvikram prev = NULL; 239140541d5dSvikram for (lp = mp->start; lp; prev = lp, lp = lp->next) { 239240541d5dSvikram lp->lineNum = ++lineNum; 239340541d5dSvikram 239440541d5dSvikram /* 239540541d5dSvikram * Get the value of the default command 239640541d5dSvikram */ 239740541d5dSvikram if (lp->entryNum == ENTRY_INIT && lp->cmd && 239840541d5dSvikram strcmp(lp->cmd, menu_cmds[DEFAULT_CMD]) == 0 && 239940541d5dSvikram lp->arg) { 240040541d5dSvikram old_default_value = atoi(lp->arg); 240140541d5dSvikram default_lp = lp; 240240541d5dSvikram } 240340541d5dSvikram 240440541d5dSvikram /* 240540541d5dSvikram * If not boot entry, nothing else to fix for this 240640541d5dSvikram * entry 240740541d5dSvikram */ 240840541d5dSvikram if (lp->entryNum == ENTRY_INIT) 240940541d5dSvikram continue; 241040541d5dSvikram 241140541d5dSvikram /* 241240541d5dSvikram * Record the position of the default entry. 241340541d5dSvikram * The following works because global 241440541d5dSvikram * commands like default and timeout should precede 241540541d5dSvikram * actual boot entries, so old_default_value 241640541d5dSvikram * is already known (or default cmd is missing). 241740541d5dSvikram */ 241840541d5dSvikram if (default_entry == NULL && 241940541d5dSvikram old_default_value != ENTRY_INIT && 242040541d5dSvikram lp->entryNum == old_default_value) { 242140541d5dSvikram default_entry = lp; 242240541d5dSvikram } 242340541d5dSvikram 242440541d5dSvikram /* 242540541d5dSvikram * Now fixup the entry number 242640541d5dSvikram */ 242740541d5dSvikram if (lp->cmd && strcmp(lp->cmd, menu_cmds[TITLE_CMD]) == 0) { 242840541d5dSvikram lp->entryNum = ++entryNum; 242940541d5dSvikram /* fixup the bootadm header */ 243040541d5dSvikram if (prev && prev->flags == BAM_COMMENT && 243140541d5dSvikram prev->arg && strcmp(prev->arg, BAM_HDR) == 0) { 243240541d5dSvikram prev->entryNum = lp->entryNum; 243340541d5dSvikram } 243440541d5dSvikram } else { 243540541d5dSvikram lp->entryNum = entryNum; 243640541d5dSvikram } 243740541d5dSvikram } 243840541d5dSvikram 243940541d5dSvikram /* 244040541d5dSvikram * No default command in menu, simply return 244140541d5dSvikram */ 244240541d5dSvikram if (default_lp == NULL) { 244340541d5dSvikram return; 244440541d5dSvikram } 244540541d5dSvikram 244640541d5dSvikram free(default_lp->arg); 244740541d5dSvikram free(default_lp->line); 244840541d5dSvikram 244940541d5dSvikram if (default_entry == NULL) { 245040541d5dSvikram default_lp->arg = s_strdup("0"); 245140541d5dSvikram } else { 245240541d5dSvikram (void) snprintf(buf, sizeof (buf), "%d", 245340541d5dSvikram default_entry->entryNum); 245440541d5dSvikram default_lp->arg = s_strdup(buf); 245540541d5dSvikram } 245640541d5dSvikram 245740541d5dSvikram /* 245840541d5dSvikram * The following is required since only the line field gets 245940541d5dSvikram * written back to menu.lst 246040541d5dSvikram */ 246140541d5dSvikram (void) snprintf(buf, sizeof (buf), "%s%s%s", 246240541d5dSvikram menu_cmds[DEFAULT_CMD], menu_cmds[SEP_CMD], default_lp->arg); 246340541d5dSvikram default_lp->line = s_strdup(buf); 246440541d5dSvikram } 246540541d5dSvikram 246640541d5dSvikram 24677c478bd9Sstevel@tonic-gate static menu_t * 24687c478bd9Sstevel@tonic-gate menu_read(char *menu_path) 24697c478bd9Sstevel@tonic-gate { 24707c478bd9Sstevel@tonic-gate FILE *fp; 24717c478bd9Sstevel@tonic-gate char buf[BAM_MAXLINE], *cp; 24727c478bd9Sstevel@tonic-gate menu_t *mp; 24737c478bd9Sstevel@tonic-gate int line, entry, len, n; 24747c478bd9Sstevel@tonic-gate 24757c478bd9Sstevel@tonic-gate mp = s_calloc(1, sizeof (menu_t)); 24767c478bd9Sstevel@tonic-gate 24777c478bd9Sstevel@tonic-gate fp = fopen(menu_path, "r"); 24787c478bd9Sstevel@tonic-gate if (fp == NULL) { /* Let the caller handle this error */ 24797c478bd9Sstevel@tonic-gate return (mp); 24807c478bd9Sstevel@tonic-gate } 24817c478bd9Sstevel@tonic-gate 24827c478bd9Sstevel@tonic-gate 24837c478bd9Sstevel@tonic-gate /* Note: GRUB boot entry number starts with 0 */ 24847c478bd9Sstevel@tonic-gate line = LINE_INIT; 24857c478bd9Sstevel@tonic-gate entry = ENTRY_INIT; 24867c478bd9Sstevel@tonic-gate cp = buf; 24877c478bd9Sstevel@tonic-gate len = sizeof (buf); 24887c478bd9Sstevel@tonic-gate while (s_fgets(cp, len, fp) != NULL) { 24897c478bd9Sstevel@tonic-gate n = strlen(cp); 24907c478bd9Sstevel@tonic-gate if (cp[n - 1] == '\\') { 24917c478bd9Sstevel@tonic-gate len -= n - 1; 24927c478bd9Sstevel@tonic-gate assert(len >= 2); 24937c478bd9Sstevel@tonic-gate cp += n - 1; 24947c478bd9Sstevel@tonic-gate continue; 24957c478bd9Sstevel@tonic-gate } 24967c478bd9Sstevel@tonic-gate line_parser(mp, buf, &line, &entry); 24977c478bd9Sstevel@tonic-gate cp = buf; 24987c478bd9Sstevel@tonic-gate len = sizeof (buf); 24997c478bd9Sstevel@tonic-gate } 25007c478bd9Sstevel@tonic-gate 25017c478bd9Sstevel@tonic-gate if (fclose(fp) == EOF) { 25027c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, menu_path, strerror(errno)); 25037c478bd9Sstevel@tonic-gate } 25047c478bd9Sstevel@tonic-gate 25057c478bd9Sstevel@tonic-gate return (mp); 25067c478bd9Sstevel@tonic-gate } 25077c478bd9Sstevel@tonic-gate 25087c478bd9Sstevel@tonic-gate static error_t 25097c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title) 25107c478bd9Sstevel@tonic-gate { 25117c478bd9Sstevel@tonic-gate char *eq; 25127c478bd9Sstevel@tonic-gate char *opt_dup; 25137c478bd9Sstevel@tonic-gate int entryNum; 25147c478bd9Sstevel@tonic-gate 25157c478bd9Sstevel@tonic-gate assert(mp); 25167c478bd9Sstevel@tonic-gate assert(mp->start); 25177c478bd9Sstevel@tonic-gate assert(opt); 25187c478bd9Sstevel@tonic-gate 25197c478bd9Sstevel@tonic-gate opt_dup = s_strdup(opt); 25207c478bd9Sstevel@tonic-gate 25217c478bd9Sstevel@tonic-gate if (entry) 25227c478bd9Sstevel@tonic-gate *entry = ENTRY_INIT; 25237c478bd9Sstevel@tonic-gate if (title) 25247c478bd9Sstevel@tonic-gate *title = NULL; 25257c478bd9Sstevel@tonic-gate 25267c478bd9Sstevel@tonic-gate eq = strchr(opt_dup, '='); 25277c478bd9Sstevel@tonic-gate if (eq == NULL) { 25287c478bd9Sstevel@tonic-gate bam_error(INVALID_OPT, opt); 25297c478bd9Sstevel@tonic-gate free(opt_dup); 25307c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25317c478bd9Sstevel@tonic-gate } 25327c478bd9Sstevel@tonic-gate 25337c478bd9Sstevel@tonic-gate *eq = '\0'; 25347c478bd9Sstevel@tonic-gate if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) { 25357c478bd9Sstevel@tonic-gate assert(mp->end); 25367c478bd9Sstevel@tonic-gate entryNum = s_strtol(eq + 1); 25377c478bd9Sstevel@tonic-gate if (entryNum < 0 || entryNum > mp->end->entryNum) { 25387c478bd9Sstevel@tonic-gate bam_error(INVALID_ENTRY, eq + 1); 25397c478bd9Sstevel@tonic-gate free(opt_dup); 25407c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25417c478bd9Sstevel@tonic-gate } 25427c478bd9Sstevel@tonic-gate *entry = entryNum; 25437c478bd9Sstevel@tonic-gate } else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) { 25447c478bd9Sstevel@tonic-gate *title = opt + (eq - opt_dup) + 1; 25457c478bd9Sstevel@tonic-gate } else { 25467c478bd9Sstevel@tonic-gate bam_error(INVALID_OPT, opt); 25477c478bd9Sstevel@tonic-gate free(opt_dup); 25487c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25497c478bd9Sstevel@tonic-gate } 25507c478bd9Sstevel@tonic-gate 25517c478bd9Sstevel@tonic-gate free(opt_dup); 25527c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 25537c478bd9Sstevel@tonic-gate } 25547c478bd9Sstevel@tonic-gate 25557c478bd9Sstevel@tonic-gate /* 25567c478bd9Sstevel@tonic-gate * If invoked with no titles/entries (opt == NULL) 25577c478bd9Sstevel@tonic-gate * only title lines in file are printed. 25587c478bd9Sstevel@tonic-gate * 25597c478bd9Sstevel@tonic-gate * If invoked with a title or entry #, all 25607c478bd9Sstevel@tonic-gate * lines in *every* matching entry are listed 25617c478bd9Sstevel@tonic-gate */ 25627c478bd9Sstevel@tonic-gate static error_t 25637c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt) 25647c478bd9Sstevel@tonic-gate { 25657c478bd9Sstevel@tonic-gate line_t *lp; 25667c478bd9Sstevel@tonic-gate int entry = ENTRY_INIT; 25677c478bd9Sstevel@tonic-gate int found; 25687c478bd9Sstevel@tonic-gate char *title = NULL; 25697c478bd9Sstevel@tonic-gate 25707c478bd9Sstevel@tonic-gate assert(mp); 25717c478bd9Sstevel@tonic-gate assert(menu_path); 25727c478bd9Sstevel@tonic-gate 25737c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 25747c478bd9Sstevel@tonic-gate bam_error(NO_MENU, menu_path); 25757c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25767c478bd9Sstevel@tonic-gate } 25777c478bd9Sstevel@tonic-gate 25787c478bd9Sstevel@tonic-gate if (opt != NULL) { 25797c478bd9Sstevel@tonic-gate if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) { 25807c478bd9Sstevel@tonic-gate return (BAM_ERROR); 25817c478bd9Sstevel@tonic-gate } 25827c478bd9Sstevel@tonic-gate assert((entry != ENTRY_INIT) ^ (title != NULL)); 25837c478bd9Sstevel@tonic-gate } else { 25847c478bd9Sstevel@tonic-gate (void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0); 25857c478bd9Sstevel@tonic-gate (void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0); 25867c478bd9Sstevel@tonic-gate } 25877c478bd9Sstevel@tonic-gate 25887c478bd9Sstevel@tonic-gate found = 0; 25897c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 25907c478bd9Sstevel@tonic-gate if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY) 25917c478bd9Sstevel@tonic-gate continue; 25927c478bd9Sstevel@tonic-gate if (opt == NULL && lp->flags == BAM_TITLE) { 25937c478bd9Sstevel@tonic-gate bam_print(PRINT_TITLE, lp->entryNum, 25947c478bd9Sstevel@tonic-gate lp->arg); 25957c478bd9Sstevel@tonic-gate found = 1; 25967c478bd9Sstevel@tonic-gate continue; 25977c478bd9Sstevel@tonic-gate } 25987c478bd9Sstevel@tonic-gate if (entry != ENTRY_INIT && lp->entryNum == entry) { 25997c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 26007c478bd9Sstevel@tonic-gate found = 1; 26017c478bd9Sstevel@tonic-gate continue; 26027c478bd9Sstevel@tonic-gate } 26037c478bd9Sstevel@tonic-gate 26047c478bd9Sstevel@tonic-gate /* 26057c478bd9Sstevel@tonic-gate * We set the entry value here so that all lines 26067c478bd9Sstevel@tonic-gate * in entry get printed. If we subsequently match 26077c478bd9Sstevel@tonic-gate * title in other entries, all lines in those 26087c478bd9Sstevel@tonic-gate * entries get printed as well. 26097c478bd9Sstevel@tonic-gate */ 26107c478bd9Sstevel@tonic-gate if (title && lp->flags == BAM_TITLE && lp->arg && 26117c478bd9Sstevel@tonic-gate strncmp(title, lp->arg, strlen(title)) == 0) { 26127c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 26137c478bd9Sstevel@tonic-gate entry = lp->entryNum; 26147c478bd9Sstevel@tonic-gate found = 1; 26157c478bd9Sstevel@tonic-gate continue; 26167c478bd9Sstevel@tonic-gate } 26177c478bd9Sstevel@tonic-gate } 26187c478bd9Sstevel@tonic-gate 26197c478bd9Sstevel@tonic-gate if (!found) { 26207c478bd9Sstevel@tonic-gate bam_error(NO_MATCH_ENTRY); 26217c478bd9Sstevel@tonic-gate return (BAM_ERROR); 26227c478bd9Sstevel@tonic-gate } 26237c478bd9Sstevel@tonic-gate 26247c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 26257c478bd9Sstevel@tonic-gate } 26267c478bd9Sstevel@tonic-gate 26277c478bd9Sstevel@tonic-gate static int 26287c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp, 26297c478bd9Sstevel@tonic-gate char *title, 26307c478bd9Sstevel@tonic-gate char *root, 26317c478bd9Sstevel@tonic-gate char *kernel, 26327c478bd9Sstevel@tonic-gate char *module) 26337c478bd9Sstevel@tonic-gate { 26347c478bd9Sstevel@tonic-gate int lineNum, entryNum; 26357c478bd9Sstevel@tonic-gate char linebuf[BAM_MAXLINE]; 26367c478bd9Sstevel@tonic-gate 26377c478bd9Sstevel@tonic-gate assert(mp); 26387c478bd9Sstevel@tonic-gate 26397c478bd9Sstevel@tonic-gate if (title == NULL) { 2640ee3b8144Sszhou title = "Solaris"; /* default to Solaris */ 26417c478bd9Sstevel@tonic-gate } 26427c478bd9Sstevel@tonic-gate if (kernel == NULL) { 26437c478bd9Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]); 26447c478bd9Sstevel@tonic-gate return (BAM_ERROR); 26457c478bd9Sstevel@tonic-gate } 26467c478bd9Sstevel@tonic-gate if (module == NULL) { 26477c478bd9Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]); 26487c478bd9Sstevel@tonic-gate return (BAM_ERROR); 26497c478bd9Sstevel@tonic-gate } 26507c478bd9Sstevel@tonic-gate 26517c478bd9Sstevel@tonic-gate if (mp->start) { 26527c478bd9Sstevel@tonic-gate lineNum = mp->end->lineNum; 26537c478bd9Sstevel@tonic-gate entryNum = mp->end->entryNum; 26547c478bd9Sstevel@tonic-gate } else { 26557c478bd9Sstevel@tonic-gate lineNum = LINE_INIT; 26567c478bd9Sstevel@tonic-gate entryNum = ENTRY_INIT; 26577c478bd9Sstevel@tonic-gate } 26587c478bd9Sstevel@tonic-gate 26597c478bd9Sstevel@tonic-gate /* 26607c478bd9Sstevel@tonic-gate * No separator for comment (HDR/FTR) commands 26617c478bd9Sstevel@tonic-gate * The syntax for comments is #<comment> 26627c478bd9Sstevel@tonic-gate */ 26637c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 26647c478bd9Sstevel@tonic-gate menu_cmds[COMMENT_CMD], BAM_HDR); 26658c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 26667c478bd9Sstevel@tonic-gate 26677c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 26687c478bd9Sstevel@tonic-gate menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title); 26698c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 26707c478bd9Sstevel@tonic-gate 26718c1b6884Sszhou if (root) { 26728c1b6884Sszhou (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 26738c1b6884Sszhou menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root); 26748c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 26757c478bd9Sstevel@tonic-gate } 26767c478bd9Sstevel@tonic-gate 26777c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 26787c478bd9Sstevel@tonic-gate menu_cmds[KERNEL_CMD], menu_cmds[SEP_CMD], kernel); 26798c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 26807c478bd9Sstevel@tonic-gate 26817c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 26827c478bd9Sstevel@tonic-gate menu_cmds[MODULE_CMD], menu_cmds[SEP_CMD], module); 26838c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 26847c478bd9Sstevel@tonic-gate 26857c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 26867c478bd9Sstevel@tonic-gate menu_cmds[COMMENT_CMD], BAM_FTR); 26878c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 26887c478bd9Sstevel@tonic-gate 26897c478bd9Sstevel@tonic-gate return (entryNum); 26907c478bd9Sstevel@tonic-gate } 26917c478bd9Sstevel@tonic-gate 26927c478bd9Sstevel@tonic-gate static error_t 26937c478bd9Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum) 26947c478bd9Sstevel@tonic-gate { 26958c1b6884Sszhou line_t *lp, *freed; 26968c1b6884Sszhou entry_t *ent, *tmp; 26977c478bd9Sstevel@tonic-gate int deleted; 26987c478bd9Sstevel@tonic-gate 26997c478bd9Sstevel@tonic-gate assert(entryNum != ENTRY_INIT); 27007c478bd9Sstevel@tonic-gate 27018c1b6884Sszhou ent = mp->entries; 27028c1b6884Sszhou while (ent) { 27038c1b6884Sszhou lp = ent->start; 27048c1b6884Sszhou /* check entry number and make sure it's a bootadm entry */ 27058c1b6884Sszhou if (lp->flags != BAM_COMMENT || 27068c1b6884Sszhou strcmp(lp->arg, BAM_HDR) != 0 || 27078c1b6884Sszhou (entryNum != ALL_ENTRIES && lp->entryNum != entryNum)) { 27088c1b6884Sszhou ent = ent->next; 27097c478bd9Sstevel@tonic-gate continue; 27107c478bd9Sstevel@tonic-gate } 27117c478bd9Sstevel@tonic-gate 27128c1b6884Sszhou /* free the entry content */ 27138c1b6884Sszhou do { 27148c1b6884Sszhou freed = lp; 27158c1b6884Sszhou lp = lp->next; /* prev stays the same */ 27168c1b6884Sszhou unlink_line(mp, freed); 27178c1b6884Sszhou line_free(freed); 27188c1b6884Sszhou } while (freed != ent->end); 27198c1b6884Sszhou 27208c1b6884Sszhou /* free the entry_t structure */ 27218c1b6884Sszhou tmp = ent; 27228c1b6884Sszhou ent = ent->next; 27238c1b6884Sszhou if (tmp->prev) 27248c1b6884Sszhou tmp->prev->next = ent; 27257c478bd9Sstevel@tonic-gate else 27268c1b6884Sszhou mp->entries = ent; 27278c1b6884Sszhou if (ent) 27288c1b6884Sszhou ent->prev = tmp->prev; 27297c478bd9Sstevel@tonic-gate deleted = 1; 27307c478bd9Sstevel@tonic-gate } 27317c478bd9Sstevel@tonic-gate 27327c478bd9Sstevel@tonic-gate if (!deleted && entryNum != ALL_ENTRIES) { 27337c478bd9Sstevel@tonic-gate bam_error(NO_BOOTADM_MATCH); 27347c478bd9Sstevel@tonic-gate return (BAM_ERROR); 27357c478bd9Sstevel@tonic-gate } 27367c478bd9Sstevel@tonic-gate 273740541d5dSvikram /* 273840541d5dSvikram * Now that we have deleted an entry, update 273940541d5dSvikram * the entry numbering and the default cmd. 274040541d5dSvikram */ 274140541d5dSvikram update_numbering(mp); 274240541d5dSvikram 27437c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 27447c478bd9Sstevel@tonic-gate } 27457c478bd9Sstevel@tonic-gate 27467c478bd9Sstevel@tonic-gate static error_t 27477c478bd9Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt) 27487c478bd9Sstevel@tonic-gate { 27497c478bd9Sstevel@tonic-gate assert(mp); 27507c478bd9Sstevel@tonic-gate assert(opt == NULL); 27517c478bd9Sstevel@tonic-gate 27527c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 27537c478bd9Sstevel@tonic-gate bam_print(EMPTY_FILE, menu_path); 27547c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 27557c478bd9Sstevel@tonic-gate } 27567c478bd9Sstevel@tonic-gate 27577c478bd9Sstevel@tonic-gate if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) { 27587c478bd9Sstevel@tonic-gate return (BAM_ERROR); 27597c478bd9Sstevel@tonic-gate } 27607c478bd9Sstevel@tonic-gate 27617c478bd9Sstevel@tonic-gate return (BAM_WRITE); 27627c478bd9Sstevel@tonic-gate } 27637c478bd9Sstevel@tonic-gate 27647c478bd9Sstevel@tonic-gate static FILE * 27658c1b6884Sszhou open_diskmap(char *root) 27667c478bd9Sstevel@tonic-gate { 27677c478bd9Sstevel@tonic-gate FILE *fp; 27687c478bd9Sstevel@tonic-gate char cmd[PATH_MAX]; 27697c478bd9Sstevel@tonic-gate 27707c478bd9Sstevel@tonic-gate /* make sure we have a map file */ 27717c478bd9Sstevel@tonic-gate fp = fopen(GRUBDISK_MAP, "r"); 27727c478bd9Sstevel@tonic-gate if (fp == NULL) { 27737c478bd9Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), 27748c1b6884Sszhou "%s%s > /dev/null", root, CREATE_DISKMAP); 27757c478bd9Sstevel@tonic-gate (void) system(cmd); 27767c478bd9Sstevel@tonic-gate fp = fopen(GRUBDISK_MAP, "r"); 27777c478bd9Sstevel@tonic-gate } 27787c478bd9Sstevel@tonic-gate return (fp); 27797c478bd9Sstevel@tonic-gate } 27807c478bd9Sstevel@tonic-gate 27817c478bd9Sstevel@tonic-gate #define SECTOR_SIZE 512 27827c478bd9Sstevel@tonic-gate 27837c478bd9Sstevel@tonic-gate static int 27847c478bd9Sstevel@tonic-gate get_partition(char *device) 27857c478bd9Sstevel@tonic-gate { 27867c478bd9Sstevel@tonic-gate int i, fd, is_pcfs, partno = -1; 27877c478bd9Sstevel@tonic-gate struct mboot *mboot; 27887c478bd9Sstevel@tonic-gate char boot_sect[SECTOR_SIZE]; 27897c478bd9Sstevel@tonic-gate char *wholedisk, *slice; 27907c478bd9Sstevel@tonic-gate 27917c478bd9Sstevel@tonic-gate /* form whole disk (p0) */ 27927c478bd9Sstevel@tonic-gate slice = device + strlen(device) - 2; 27937c478bd9Sstevel@tonic-gate is_pcfs = (*slice != 's'); 27947c478bd9Sstevel@tonic-gate if (!is_pcfs) 27957c478bd9Sstevel@tonic-gate *slice = '\0'; 27967c478bd9Sstevel@tonic-gate wholedisk = s_calloc(1, strlen(device) + 3); 27977c478bd9Sstevel@tonic-gate (void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device); 27987c478bd9Sstevel@tonic-gate if (!is_pcfs) 27997c478bd9Sstevel@tonic-gate *slice = 's'; 28007c478bd9Sstevel@tonic-gate 28017c478bd9Sstevel@tonic-gate /* read boot sector */ 28027c478bd9Sstevel@tonic-gate fd = open(wholedisk, O_RDONLY); 28037c478bd9Sstevel@tonic-gate free(wholedisk); 28047c478bd9Sstevel@tonic-gate if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 28057c478bd9Sstevel@tonic-gate return (partno); 28067c478bd9Sstevel@tonic-gate } 28077c478bd9Sstevel@tonic-gate (void) close(fd); 28087c478bd9Sstevel@tonic-gate 28097c478bd9Sstevel@tonic-gate /* parse fdisk table */ 28107c478bd9Sstevel@tonic-gate mboot = (struct mboot *)((void *)boot_sect); 28117c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 28127c478bd9Sstevel@tonic-gate struct ipart *part = 28137c478bd9Sstevel@tonic-gate (struct ipart *)(uintptr_t)mboot->parts + i; 28147c478bd9Sstevel@tonic-gate if (is_pcfs) { /* looking for solaris boot part */ 28157c478bd9Sstevel@tonic-gate if (part->systid == 0xbe) { 28167c478bd9Sstevel@tonic-gate partno = i; 28177c478bd9Sstevel@tonic-gate break; 28187c478bd9Sstevel@tonic-gate } 28197c478bd9Sstevel@tonic-gate } else { /* look for solaris partition, old and new */ 28207c478bd9Sstevel@tonic-gate if (part->systid == SUNIXOS || 28217c478bd9Sstevel@tonic-gate part->systid == SUNIXOS2) { 28227c478bd9Sstevel@tonic-gate partno = i; 28237c478bd9Sstevel@tonic-gate break; 28247c478bd9Sstevel@tonic-gate } 28257c478bd9Sstevel@tonic-gate } 28267c478bd9Sstevel@tonic-gate } 28277c478bd9Sstevel@tonic-gate return (partno); 28287c478bd9Sstevel@tonic-gate } 28297c478bd9Sstevel@tonic-gate 28307c478bd9Sstevel@tonic-gate static char * 28317c478bd9Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev) 28327c478bd9Sstevel@tonic-gate { 28337c478bd9Sstevel@tonic-gate char *grubdisk; /* (hd#,#,#) */ 28347c478bd9Sstevel@tonic-gate char *slice; 28357c478bd9Sstevel@tonic-gate char *grubhd; 28367c478bd9Sstevel@tonic-gate int fdiskpart; 28377c478bd9Sstevel@tonic-gate int found = 0; 28387c478bd9Sstevel@tonic-gate char *devname, *ctdname = strstr(rootdev, "dsk/"); 28397c478bd9Sstevel@tonic-gate char linebuf[PATH_MAX]; 28407c478bd9Sstevel@tonic-gate 28417c478bd9Sstevel@tonic-gate if (ctdname == NULL) 28427c478bd9Sstevel@tonic-gate return (NULL); 28437c478bd9Sstevel@tonic-gate 28447c478bd9Sstevel@tonic-gate ctdname += strlen("dsk/"); 28457c478bd9Sstevel@tonic-gate slice = strrchr(ctdname, 's'); 28467c478bd9Sstevel@tonic-gate if (slice) 28477c478bd9Sstevel@tonic-gate *slice = '\0'; 28487c478bd9Sstevel@tonic-gate 28497c478bd9Sstevel@tonic-gate rewind(fp); 28507c478bd9Sstevel@tonic-gate while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) { 28517c478bd9Sstevel@tonic-gate grubhd = strtok(linebuf, " \t\n"); 28527c478bd9Sstevel@tonic-gate if (grubhd) 28537c478bd9Sstevel@tonic-gate devname = strtok(NULL, " \t\n"); 28547c478bd9Sstevel@tonic-gate else 28557c478bd9Sstevel@tonic-gate devname = NULL; 28567c478bd9Sstevel@tonic-gate if (devname && strcmp(devname, ctdname) == 0) { 28577c478bd9Sstevel@tonic-gate found = 1; 28587c478bd9Sstevel@tonic-gate break; 28597c478bd9Sstevel@tonic-gate } 28607c478bd9Sstevel@tonic-gate } 28617c478bd9Sstevel@tonic-gate 28627c478bd9Sstevel@tonic-gate if (slice) 28637c478bd9Sstevel@tonic-gate *slice = 's'; 28647c478bd9Sstevel@tonic-gate 28657c478bd9Sstevel@tonic-gate if (found == 0) { 28667c478bd9Sstevel@tonic-gate if (bam_verbose) 28677c478bd9Sstevel@tonic-gate bam_print(DISKMAP_FAIL_NONFATAL, rootdev); 28687c478bd9Sstevel@tonic-gate grubhd = "0"; /* assume disk 0 if can't match */ 28697c478bd9Sstevel@tonic-gate } 28707c478bd9Sstevel@tonic-gate 28717c478bd9Sstevel@tonic-gate fdiskpart = get_partition(rootdev); 28727c478bd9Sstevel@tonic-gate if (fdiskpart == -1) 28737c478bd9Sstevel@tonic-gate return (NULL); 28747c478bd9Sstevel@tonic-gate 28757c478bd9Sstevel@tonic-gate grubdisk = s_calloc(1, 10); 28767c478bd9Sstevel@tonic-gate if (slice) { 28777c478bd9Sstevel@tonic-gate (void) snprintf(grubdisk, 10, "(hd%s,%d,%c)", 28787c478bd9Sstevel@tonic-gate grubhd, fdiskpart, slice[1] + 'a' - '0'); 28797c478bd9Sstevel@tonic-gate } else 28807c478bd9Sstevel@tonic-gate (void) snprintf(grubdisk, 10, "(hd%s,%d)", 28817c478bd9Sstevel@tonic-gate grubhd, fdiskpart); 28827c478bd9Sstevel@tonic-gate 28837c478bd9Sstevel@tonic-gate /* if root not on bootdev, change GRUB disk to 0 */ 28847c478bd9Sstevel@tonic-gate if (!on_bootdev) 28857c478bd9Sstevel@tonic-gate grubdisk[3] = '0'; 28867c478bd9Sstevel@tonic-gate return (grubdisk); 28877c478bd9Sstevel@tonic-gate } 28887c478bd9Sstevel@tonic-gate 2889ee3b8144Sszhou static char * 2890ee3b8144Sszhou get_title(char *rootdir) 28917c478bd9Sstevel@tonic-gate { 28927c478bd9Sstevel@tonic-gate static char title[80]; /* from /etc/release */ 28938c1b6884Sszhou char *cp = NULL, release[PATH_MAX]; 28947c478bd9Sstevel@tonic-gate FILE *fp; 28957c478bd9Sstevel@tonic-gate 28967c478bd9Sstevel@tonic-gate /* open the /etc/release file */ 28977c478bd9Sstevel@tonic-gate (void) snprintf(release, sizeof (release), "%s/etc/release", rootdir); 28987c478bd9Sstevel@tonic-gate 28997c478bd9Sstevel@tonic-gate fp = fopen(release, "r"); 29007c478bd9Sstevel@tonic-gate if (fp == NULL) 2901ee3b8144Sszhou return (NULL); 29027c478bd9Sstevel@tonic-gate 29037c478bd9Sstevel@tonic-gate while (s_fgets(title, sizeof (title), fp) != NULL) { 29047c478bd9Sstevel@tonic-gate cp = strstr(title, "Solaris"); 29057c478bd9Sstevel@tonic-gate if (cp) 29067c478bd9Sstevel@tonic-gate break; 29077c478bd9Sstevel@tonic-gate } 29087c478bd9Sstevel@tonic-gate (void) fclose(fp); 29098c1b6884Sszhou return (cp == NULL ? "Solaris" : cp); 29107c478bd9Sstevel@tonic-gate } 29117c478bd9Sstevel@tonic-gate 29127c478bd9Sstevel@tonic-gate static char * 29137c478bd9Sstevel@tonic-gate get_special(char *mountp) 29147c478bd9Sstevel@tonic-gate { 29157c478bd9Sstevel@tonic-gate FILE *mntfp; 29167c478bd9Sstevel@tonic-gate struct mnttab mp = {0}, mpref = {0}; 29177c478bd9Sstevel@tonic-gate 29187c478bd9Sstevel@tonic-gate mntfp = fopen(MNTTAB, "r"); 29197c478bd9Sstevel@tonic-gate if (mntfp == NULL) { 29207c478bd9Sstevel@tonic-gate return (0); 29217c478bd9Sstevel@tonic-gate } 29227c478bd9Sstevel@tonic-gate 29237c478bd9Sstevel@tonic-gate if (*mountp == '\0') 29247c478bd9Sstevel@tonic-gate mpref.mnt_mountp = "/"; 29257c478bd9Sstevel@tonic-gate else 29267c478bd9Sstevel@tonic-gate mpref.mnt_mountp = mountp; 29277c478bd9Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 29287c478bd9Sstevel@tonic-gate (void) fclose(mntfp); 29297c478bd9Sstevel@tonic-gate return (NULL); 29307c478bd9Sstevel@tonic-gate } 29317c478bd9Sstevel@tonic-gate (void) fclose(mntfp); 29327c478bd9Sstevel@tonic-gate 29337c478bd9Sstevel@tonic-gate return (s_strdup(mp.mnt_special)); 29347c478bd9Sstevel@tonic-gate } 29357c478bd9Sstevel@tonic-gate 29367c478bd9Sstevel@tonic-gate static char * 29377c478bd9Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev) 29387c478bd9Sstevel@tonic-gate { 29397c478bd9Sstevel@tonic-gate FILE *fp; 29407c478bd9Sstevel@tonic-gate char *grubdisk; 29417c478bd9Sstevel@tonic-gate 29427c478bd9Sstevel@tonic-gate /* translate /dev/dsk name to grub disk name */ 29438c1b6884Sszhou fp = open_diskmap(""); 29447c478bd9Sstevel@tonic-gate if (fp == NULL) { 29457c478bd9Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdisk); 29467c478bd9Sstevel@tonic-gate return (NULL); 29477c478bd9Sstevel@tonic-gate } 29487c478bd9Sstevel@tonic-gate grubdisk = get_grubdisk(osdisk, fp, on_bootdev); 29497c478bd9Sstevel@tonic-gate (void) fclose(fp); 29507c478bd9Sstevel@tonic-gate return (grubdisk); 29517c478bd9Sstevel@tonic-gate } 29527c478bd9Sstevel@tonic-gate 29537c478bd9Sstevel@tonic-gate /* 29547c478bd9Sstevel@tonic-gate * Check if root is on the boot device 29557c478bd9Sstevel@tonic-gate * Return 0 (false) on error 29567c478bd9Sstevel@tonic-gate */ 29577c478bd9Sstevel@tonic-gate static int 29587c478bd9Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp) 29597c478bd9Sstevel@tonic-gate { 29607c478bd9Sstevel@tonic-gate int ret; 29617c478bd9Sstevel@tonic-gate char *grubhd, *bootp, *special; 29627c478bd9Sstevel@tonic-gate 29637c478bd9Sstevel@tonic-gate special = get_special(menu_root); 29647c478bd9Sstevel@tonic-gate if (special == NULL) 29657c478bd9Sstevel@tonic-gate return (0); 29667c478bd9Sstevel@tonic-gate bootp = strstr(special, "p0:boot"); 29677c478bd9Sstevel@tonic-gate if (bootp) 29687c478bd9Sstevel@tonic-gate *bootp = '\0'; 29697c478bd9Sstevel@tonic-gate grubhd = get_grubdisk(special, fp, 1); 29707c478bd9Sstevel@tonic-gate free(special); 29717c478bd9Sstevel@tonic-gate 29727c478bd9Sstevel@tonic-gate if (grubhd == NULL) 29737c478bd9Sstevel@tonic-gate return (0); 29747c478bd9Sstevel@tonic-gate ret = grubhd[3] == '0'; 29757c478bd9Sstevel@tonic-gate free(grubhd); 29767c478bd9Sstevel@tonic-gate return (ret); 29777c478bd9Sstevel@tonic-gate } 29787c478bd9Sstevel@tonic-gate 29798c1b6884Sszhou /* 29808c1b6884Sszhou * look for matching bootadm entry with specified parameters 29818c1b6884Sszhou * Here are the rules (based on existing usage): 29828c1b6884Sszhou * - If title is specified, match on title only 29838c1b6884Sszhou * - Else, match on grubdisk and module (don't care about kernel line). 29848c1b6884Sszhou * note that, if root_opt is non-zero, the absence of root line is 29858c1b6884Sszhou * considered a match. 29868c1b6884Sszhou */ 29878c1b6884Sszhou static entry_t * 29888c1b6884Sszhou find_boot_entry(menu_t *mp, char *title, char *root, char *module, 29898c1b6884Sszhou int root_opt, int *entry_num) 29908c1b6884Sszhou { 29918c1b6884Sszhou int i; 29928c1b6884Sszhou line_t *lp; 29938c1b6884Sszhou entry_t *ent; 29948c1b6884Sszhou 29958c1b6884Sszhou /* find matching entry */ 29968c1b6884Sszhou for (i = 0, ent = mp->entries; ent; i++, ent = ent->next) { 29978c1b6884Sszhou lp = ent->start; 29988c1b6884Sszhou 29998c1b6884Sszhou /* first line of entry must be bootadm comment */ 30008c1b6884Sszhou lp = ent->start; 30018c1b6884Sszhou if (lp->flags != BAM_COMMENT || strcmp(lp->arg, BAM_HDR) != 0) { 30028c1b6884Sszhou continue; 30038c1b6884Sszhou } 30048c1b6884Sszhou 30058c1b6884Sszhou /* advance to title line */ 30068c1b6884Sszhou lp = lp->next; 30078c1b6884Sszhou if (title) { 30088c1b6884Sszhou if (lp->flags == BAM_TITLE && lp->arg && 30098c1b6884Sszhou strcmp(lp->arg, title) == 0) 30108c1b6884Sszhou break; 30118c1b6884Sszhou continue; /* check title only */ 30128c1b6884Sszhou } 30138c1b6884Sszhou 30148c1b6884Sszhou lp = lp->next; /* advance to root line */ 30158c1b6884Sszhou if (lp == NULL || strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) { 30168c1b6884Sszhou /* root command found, match grub disk */ 30178c1b6884Sszhou if (strcmp(lp->arg, root) != 0) { 30188c1b6884Sszhou continue; 30198c1b6884Sszhou } 30208c1b6884Sszhou lp = lp->next; /* advance to kernel line */ 30218c1b6884Sszhou } else { 30228c1b6884Sszhou /* no root command, see if root is optional */ 30238c1b6884Sszhou if (root_opt == 0) { 30248c1b6884Sszhou continue; 30258c1b6884Sszhou } 30268c1b6884Sszhou } 30278c1b6884Sszhou 30288c1b6884Sszhou if (lp == NULL || lp->next == NULL) { 30298c1b6884Sszhou continue; 30308c1b6884Sszhou } 30318c1b6884Sszhou 30328c1b6884Sszhou /* check for matching module entry (failsafe or normal) */ 30338c1b6884Sszhou lp = lp->next; /* advance to module line */ 30348c1b6884Sszhou if (strcmp(lp->cmd, menu_cmds[MODULE_CMD]) != 0 || 30358c1b6884Sszhou strcmp(lp->arg, module) != 0) { 30368c1b6884Sszhou continue; 30378c1b6884Sszhou } 30388c1b6884Sszhou break; /* match found */ 30398c1b6884Sszhou } 30408c1b6884Sszhou 30418c1b6884Sszhou *entry_num = i; 30428c1b6884Sszhou return (ent); 30438c1b6884Sszhou } 30448c1b6884Sszhou 30458c1b6884Sszhou static int 30468c1b6884Sszhou update_boot_entry(menu_t *mp, char *title, char *root, char *kernel, 30478c1b6884Sszhou char *module, int root_opt) 30488c1b6884Sszhou { 30498c1b6884Sszhou int i; 30508c1b6884Sszhou entry_t *ent; 30518c1b6884Sszhou line_t *lp; 30528c1b6884Sszhou char linebuf[BAM_MAXLINE]; 30538c1b6884Sszhou 30548c1b6884Sszhou /* note: don't match on title, it's updated on upgrade */ 30558c1b6884Sszhou ent = find_boot_entry(mp, NULL, root, module, root_opt, &i); 30568c1b6884Sszhou if (ent == NULL) 30578c1b6884Sszhou return (add_boot_entry(mp, title, root_opt ? NULL : root, 30588c1b6884Sszhou kernel, module)); 30598c1b6884Sszhou 30608c1b6884Sszhou /* replace title of exiting entry and delete root line */ 30618c1b6884Sszhou lp = ent->start; 30628c1b6884Sszhou lp = lp->next; /* title line */ 30638c1b6884Sszhou (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 30648c1b6884Sszhou menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title); 30658c1b6884Sszhou free(lp->arg); 30668c1b6884Sszhou free(lp->line); 30678c1b6884Sszhou lp->arg = s_strdup(title); 30688c1b6884Sszhou lp->line = s_strdup(linebuf); 30698c1b6884Sszhou 30708c1b6884Sszhou lp = lp->next; /* root line */ 30718c1b6884Sszhou if (strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0) { 30728c1b6884Sszhou if (root_opt) { /* root line not needed */ 30738c1b6884Sszhou line_t *tmp = lp; 30748c1b6884Sszhou lp = lp->next; 30758c1b6884Sszhou unlink_line(mp, tmp); 30768c1b6884Sszhou line_free(tmp); 30778c1b6884Sszhou } else 30788c1b6884Sszhou lp = lp->next; 30798c1b6884Sszhou } 30808c1b6884Sszhou return (i); 30818c1b6884Sszhou } 30828c1b6884Sszhou 30837c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 30847c478bd9Sstevel@tonic-gate static error_t 30857c478bd9Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt) 30867c478bd9Sstevel@tonic-gate { 30877c478bd9Sstevel@tonic-gate FILE *fp; 30887c478bd9Sstevel@tonic-gate int entry; 30897c478bd9Sstevel@tonic-gate char *grubdisk, *title, *osdev, *osroot; 30908c1b6884Sszhou struct stat sbuf; 30918c1b6884Sszhou char failsafe[256]; 30927c478bd9Sstevel@tonic-gate 30937c478bd9Sstevel@tonic-gate assert(mp); 30947c478bd9Sstevel@tonic-gate assert(opt); 30957c478bd9Sstevel@tonic-gate 30967c478bd9Sstevel@tonic-gate osdev = strtok(opt, ","); 30977c478bd9Sstevel@tonic-gate osroot = strtok(NULL, ","); 30987c478bd9Sstevel@tonic-gate if (osroot == NULL) 30997c478bd9Sstevel@tonic-gate osroot = menu_root; 31007c478bd9Sstevel@tonic-gate title = get_title(osroot); 31017c478bd9Sstevel@tonic-gate 31027c478bd9Sstevel@tonic-gate /* translate /dev/dsk name to grub disk name */ 31038c1b6884Sszhou fp = open_diskmap(osroot); 31047c478bd9Sstevel@tonic-gate if (fp == NULL) { 31057c478bd9Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdev); 31067c478bd9Sstevel@tonic-gate return (BAM_ERROR); 31077c478bd9Sstevel@tonic-gate } 31087c478bd9Sstevel@tonic-gate grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp)); 31097c478bd9Sstevel@tonic-gate (void) fclose(fp); 31107c478bd9Sstevel@tonic-gate if (grubdisk == NULL) { 31117c478bd9Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdev); 31127c478bd9Sstevel@tonic-gate return (BAM_ERROR); 31137c478bd9Sstevel@tonic-gate } 31147c478bd9Sstevel@tonic-gate 31157c478bd9Sstevel@tonic-gate /* add the entry for normal Solaris */ 31168c1b6884Sszhou entry = update_boot_entry(mp, title, grubdisk, 31177c478bd9Sstevel@tonic-gate "/platform/i86pc/multiboot", 31188c1b6884Sszhou "/platform/i86pc/boot_archive", 31198c1b6884Sszhou osroot == menu_root); 31207c478bd9Sstevel@tonic-gate 31217c478bd9Sstevel@tonic-gate /* add the entry for failsafe archive */ 31228c1b6884Sszhou (void) snprintf(failsafe, sizeof (failsafe), 31238c1b6884Sszhou "%s/boot/x86.miniroot-safe", osroot); 31248c1b6884Sszhou if (stat(failsafe, &sbuf) == 0) 31258c1b6884Sszhou (void) update_boot_entry(mp, "Solaris failsafe", grubdisk, 31268c1b6884Sszhou "/boot/multiboot kernel/unix -s", 31278c1b6884Sszhou "/boot/x86.miniroot-safe", 31288c1b6884Sszhou osroot == menu_root); 31297c478bd9Sstevel@tonic-gate free(grubdisk); 31307c478bd9Sstevel@tonic-gate 31317c478bd9Sstevel@tonic-gate if (entry == BAM_ERROR) { 31327c478bd9Sstevel@tonic-gate return (BAM_ERROR); 31337c478bd9Sstevel@tonic-gate } 31347c478bd9Sstevel@tonic-gate (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 31357c478bd9Sstevel@tonic-gate return (BAM_WRITE); 31367c478bd9Sstevel@tonic-gate } 31377c478bd9Sstevel@tonic-gate 3138b610f78eSvikram static char * 3139b610f78eSvikram read_grub_root(void) 3140b610f78eSvikram { 3141b610f78eSvikram FILE *fp; 3142b610f78eSvikram struct stat sb; 3143b610f78eSvikram char buf[BAM_MAXLINE]; 3144b610f78eSvikram char *rootstr; 3145b610f78eSvikram 3146b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 3147b610f78eSvikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 3148b610f78eSvikram return (NULL); 3149b610f78eSvikram } 3150b610f78eSvikram 3151b610f78eSvikram if (stat(GRUB_root, &sb) != 0) { 3152b610f78eSvikram bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno)); 3153b610f78eSvikram return (NULL); 3154b610f78eSvikram } 3155b610f78eSvikram 3156b610f78eSvikram fp = fopen(GRUB_root, "r"); 3157b610f78eSvikram if (fp == NULL) { 3158b610f78eSvikram bam_error(OPEN_FAIL, GRUB_root, strerror(errno)); 3159b610f78eSvikram return (NULL); 3160b610f78eSvikram } 3161b610f78eSvikram 3162b610f78eSvikram if (s_fgets(buf, sizeof (buf), fp) == NULL) { 3163b610f78eSvikram bam_error(EMPTY_FILE, GRUB_root, strerror(errno)); 3164b610f78eSvikram (void) fclose(fp); 3165b610f78eSvikram return (NULL); 3166b610f78eSvikram } 3167b610f78eSvikram 3168b610f78eSvikram /* 3169b610f78eSvikram * Copy buf here as check below may trash the buffer 3170b610f78eSvikram */ 3171b610f78eSvikram rootstr = s_strdup(buf); 3172b610f78eSvikram 3173b610f78eSvikram if (s_fgets(buf, sizeof (buf), fp) != NULL) { 3174b610f78eSvikram bam_error(BAD_ROOT_FILE, GRUB_root); 3175b610f78eSvikram free(rootstr); 3176b610f78eSvikram rootstr = NULL; 3177b610f78eSvikram } 3178b610f78eSvikram 3179b610f78eSvikram (void) fclose(fp); 3180b610f78eSvikram 3181b610f78eSvikram return (rootstr); 3182b610f78eSvikram } 3183b610f78eSvikram 31848c1b6884Sszhou static void 31858c1b6884Sszhou save_default_entry(menu_t *mp) 31868c1b6884Sszhou { 31878c1b6884Sszhou int lineNum, entryNum; 31888c1b6884Sszhou int entry = 0; /* default is 0 */ 31898c1b6884Sszhou char linebuf[BAM_MAXLINE]; 31908c1b6884Sszhou line_t *lp = mp->curdefault; 31918c1b6884Sszhou 31928c1b6884Sszhou if (lp) 31938c1b6884Sszhou entry = s_strtol(lp->arg); 31948c1b6884Sszhou 31958c1b6884Sszhou (void) snprintf(linebuf, sizeof (linebuf), "#%s%d", BAM_OLDDEF, entry); 31968c1b6884Sszhou line_parser(mp, linebuf, &lineNum, &entryNum); 31978c1b6884Sszhou } 31988c1b6884Sszhou 31998c1b6884Sszhou static void 32008c1b6884Sszhou restore_default_entry(menu_t *mp) 32018c1b6884Sszhou { 32028c1b6884Sszhou int entry; 32038c1b6884Sszhou char *str; 32048c1b6884Sszhou line_t *lp = mp->olddefault; 32058c1b6884Sszhou 32068c1b6884Sszhou if (lp == NULL) 32078c1b6884Sszhou return; /* nothing to restore */ 32088c1b6884Sszhou 32098c1b6884Sszhou str = lp->arg + strlen(BAM_OLDDEF); 32108c1b6884Sszhou entry = s_strtol(str); 32118c1b6884Sszhou (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 32128c1b6884Sszhou 32138c1b6884Sszhou /* delete saved old default line */ 32148c1b6884Sszhou mp->olddefault = NULL; 32158c1b6884Sszhou unlink_line(mp, lp); 32168c1b6884Sszhou line_free(lp); 32178c1b6884Sszhou } 32188c1b6884Sszhou 32197c478bd9Sstevel@tonic-gate /* 32207c478bd9Sstevel@tonic-gate * This function is for supporting reboot with args. 32217c478bd9Sstevel@tonic-gate * The opt value can be: 32227c478bd9Sstevel@tonic-gate * NULL delete temp entry, if present 32237c478bd9Sstevel@tonic-gate * entry=# switches default entry to 1 32247c478bd9Sstevel@tonic-gate * else treated as boot-args and setup a temperary menu entry 32257c478bd9Sstevel@tonic-gate * and make it the default 32267c478bd9Sstevel@tonic-gate */ 32277c478bd9Sstevel@tonic-gate #define REBOOT_TITLE "Solaris_reboot_transient" 32287c478bd9Sstevel@tonic-gate 32298c1b6884Sszhou /*ARGSUSED*/ 32307c478bd9Sstevel@tonic-gate static error_t 32317c478bd9Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt) 32327c478bd9Sstevel@tonic-gate { 32337c478bd9Sstevel@tonic-gate int entry; 32347c478bd9Sstevel@tonic-gate char *grubdisk, *rootdev; 32357c478bd9Sstevel@tonic-gate char kernbuf[1024]; 3236b610f78eSvikram struct stat sb; 32377c478bd9Sstevel@tonic-gate 32387c478bd9Sstevel@tonic-gate assert(mp); 32397c478bd9Sstevel@tonic-gate 32408c1b6884Sszhou /* If no option, delete exiting reboot menu entry */ 32418c1b6884Sszhou if (opt == NULL) { 32428c1b6884Sszhou entry_t *ent = find_boot_entry(mp, REBOOT_TITLE, NULL, NULL, 32438c1b6884Sszhou 0, &entry); 32448c1b6884Sszhou if (ent == NULL) /* not found is ok */ 32458c1b6884Sszhou return (BAM_SUCCESS); 32468c1b6884Sszhou (void) do_delete(mp, entry); 32478c1b6884Sszhou restore_default_entry(mp); 32488c1b6884Sszhou return (BAM_WRITE); 32498c1b6884Sszhou } 32508c1b6884Sszhou 32518c1b6884Sszhou /* if entry= is specified, set the default entry */ 32528c1b6884Sszhou if (strncmp(opt, "entry=", strlen("entry=")) == 0 && 32537c478bd9Sstevel@tonic-gate selector(mp, opt, &entry, NULL) == BAM_SUCCESS) { 32547c478bd9Sstevel@tonic-gate /* this is entry=# option */ 32557c478bd9Sstevel@tonic-gate return (set_global(mp, menu_cmds[DEFAULT_CMD], entry)); 32567c478bd9Sstevel@tonic-gate } 32577c478bd9Sstevel@tonic-gate 32587c478bd9Sstevel@tonic-gate /* 32597c478bd9Sstevel@tonic-gate * add a new menu entry base on opt and make it the default 32607c478bd9Sstevel@tonic-gate */ 3261b610f78eSvikram grubdisk = NULL; 3262b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 3263b610f78eSvikram /* 3264b610f78eSvikram * 1. First get root disk name from mnttab 3265b610f78eSvikram * 2. Translate disk name to grub name 3266b610f78eSvikram * 3. Add the new menu entry 3267b610f78eSvikram */ 3268b610f78eSvikram rootdev = get_special("/"); 3269b610f78eSvikram if (rootdev) { 3270b610f78eSvikram grubdisk = os_to_grubdisk(rootdev, 1); 3271b610f78eSvikram free(rootdev); 3272b610f78eSvikram } 3273b610f78eSvikram } else { 3274b610f78eSvikram /* 3275b610f78eSvikram * This is an LU BE. The GRUB_root file 3276b610f78eSvikram * contains entry for GRUB's "root" cmd. 3277b610f78eSvikram */ 3278b610f78eSvikram grubdisk = read_grub_root(); 32797c478bd9Sstevel@tonic-gate } 32807c478bd9Sstevel@tonic-gate if (grubdisk == NULL) { 3281b610f78eSvikram bam_error(REBOOT_WITH_ARGS_FAILED); 32827c478bd9Sstevel@tonic-gate return (BAM_ERROR); 32837c478bd9Sstevel@tonic-gate } 32847c478bd9Sstevel@tonic-gate 32857c478bd9Sstevel@tonic-gate /* add an entry for Solaris reboot */ 32867c478bd9Sstevel@tonic-gate (void) snprintf(kernbuf, sizeof (kernbuf), 32877c478bd9Sstevel@tonic-gate "/platform/i86pc/multiboot %s", opt); 32887c478bd9Sstevel@tonic-gate entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf, 32897c478bd9Sstevel@tonic-gate "/platform/i86pc/boot_archive"); 32907c478bd9Sstevel@tonic-gate free(grubdisk); 32917c478bd9Sstevel@tonic-gate 32927c478bd9Sstevel@tonic-gate if (entry == BAM_ERROR) { 3293b610f78eSvikram bam_error(REBOOT_WITH_ARGS_FAILED); 32947c478bd9Sstevel@tonic-gate return (BAM_ERROR); 32957c478bd9Sstevel@tonic-gate } 32968c1b6884Sszhou 32978c1b6884Sszhou save_default_entry(mp); 32987c478bd9Sstevel@tonic-gate (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 32997c478bd9Sstevel@tonic-gate return (BAM_WRITE); 33007c478bd9Sstevel@tonic-gate } 33017c478bd9Sstevel@tonic-gate 33027c478bd9Sstevel@tonic-gate static error_t 33037c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val) 33047c478bd9Sstevel@tonic-gate { 33057c478bd9Sstevel@tonic-gate line_t *lp, *found, *last; 33067c478bd9Sstevel@tonic-gate char *cp, *str; 33077c478bd9Sstevel@tonic-gate char prefix[BAM_MAXLINE]; 33087c478bd9Sstevel@tonic-gate size_t len; 33097c478bd9Sstevel@tonic-gate 33107c478bd9Sstevel@tonic-gate assert(mp); 33117c478bd9Sstevel@tonic-gate assert(globalcmd); 33127c478bd9Sstevel@tonic-gate 3313b610f78eSvikram if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) { 3314b610f78eSvikram if (val < 0 || mp->end == NULL || val > mp->end->entryNum) { 3315b610f78eSvikram (void) snprintf(prefix, sizeof (prefix), "%d", val); 3316b610f78eSvikram bam_error(INVALID_ENTRY, prefix); 3317b610f78eSvikram return (BAM_ERROR); 3318b610f78eSvikram } 3319b610f78eSvikram } 3320b610f78eSvikram 33217c478bd9Sstevel@tonic-gate found = last = NULL; 33227c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 33237c478bd9Sstevel@tonic-gate if (lp->flags != BAM_GLOBAL) 33247c478bd9Sstevel@tonic-gate continue; 33257c478bd9Sstevel@tonic-gate 33267c478bd9Sstevel@tonic-gate last = lp; /* track the last global found */ 33277c478bd9Sstevel@tonic-gate 33287c478bd9Sstevel@tonic-gate if (lp->cmd == NULL) { 33297c478bd9Sstevel@tonic-gate bam_error(NO_CMD, lp->lineNum); 33307c478bd9Sstevel@tonic-gate continue; 33317c478bd9Sstevel@tonic-gate } 33327c478bd9Sstevel@tonic-gate if (strcmp(globalcmd, lp->cmd) != 0) 33337c478bd9Sstevel@tonic-gate continue; 33347c478bd9Sstevel@tonic-gate 33357c478bd9Sstevel@tonic-gate if (found) { 33367c478bd9Sstevel@tonic-gate bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 33377c478bd9Sstevel@tonic-gate } 33387c478bd9Sstevel@tonic-gate found = lp; 33397c478bd9Sstevel@tonic-gate } 33407c478bd9Sstevel@tonic-gate 33417c478bd9Sstevel@tonic-gate if (found == NULL) { 33427c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 33437c478bd9Sstevel@tonic-gate if (last == NULL) { 33447c478bd9Sstevel@tonic-gate lp->next = mp->start; 33457c478bd9Sstevel@tonic-gate mp->start = lp; 33467c478bd9Sstevel@tonic-gate mp->end = (mp->end) ? mp->end : lp; 33477c478bd9Sstevel@tonic-gate } else { 33487c478bd9Sstevel@tonic-gate lp->next = last->next; 33497c478bd9Sstevel@tonic-gate last->next = lp; 33507c478bd9Sstevel@tonic-gate if (lp->next == NULL) 33517c478bd9Sstevel@tonic-gate mp->end = lp; 33527c478bd9Sstevel@tonic-gate } 33537c478bd9Sstevel@tonic-gate lp->flags = BAM_GLOBAL; /* other fields not needed for writes */ 33547c478bd9Sstevel@tonic-gate len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]); 33557c478bd9Sstevel@tonic-gate len += 10; /* val < 10 digits */ 33567c478bd9Sstevel@tonic-gate lp->line = s_calloc(1, len); 33577c478bd9Sstevel@tonic-gate (void) snprintf(lp->line, len, "%s%s%d", 33587c478bd9Sstevel@tonic-gate globalcmd, menu_cmds[SEP_CMD], val); 33597c478bd9Sstevel@tonic-gate return (BAM_WRITE); 33607c478bd9Sstevel@tonic-gate } 33617c478bd9Sstevel@tonic-gate 33627c478bd9Sstevel@tonic-gate /* 33637c478bd9Sstevel@tonic-gate * We are changing an existing entry. Retain any prefix whitespace, 33647c478bd9Sstevel@tonic-gate * but overwrite everything else. This preserves tabs added for 33657c478bd9Sstevel@tonic-gate * readability. 33667c478bd9Sstevel@tonic-gate */ 33677c478bd9Sstevel@tonic-gate str = found->line; 33687c478bd9Sstevel@tonic-gate cp = prefix; 33697c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 33707c478bd9Sstevel@tonic-gate *(cp++) = *(str++); 33717c478bd9Sstevel@tonic-gate *cp = '\0'; /* Terminate prefix */ 33727c478bd9Sstevel@tonic-gate len = strlen(prefix) + strlen(globalcmd); 33737c478bd9Sstevel@tonic-gate len += strlen(menu_cmds[SEP_CMD]) + 10; 33747c478bd9Sstevel@tonic-gate 33757c478bd9Sstevel@tonic-gate free(found->line); 33767c478bd9Sstevel@tonic-gate found->line = s_calloc(1, len); 33777c478bd9Sstevel@tonic-gate (void) snprintf(found->line, len, 33787c478bd9Sstevel@tonic-gate "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val); 33797c478bd9Sstevel@tonic-gate 33807c478bd9Sstevel@tonic-gate return (BAM_WRITE); /* need a write to menu */ 33817c478bd9Sstevel@tonic-gate } 33827c478bd9Sstevel@tonic-gate 33837c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 33847c478bd9Sstevel@tonic-gate static error_t 33857c478bd9Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt) 33867c478bd9Sstevel@tonic-gate { 33877c478bd9Sstevel@tonic-gate int optnum, optval; 33887c478bd9Sstevel@tonic-gate char *val; 33897c478bd9Sstevel@tonic-gate 33907c478bd9Sstevel@tonic-gate assert(mp); 33917c478bd9Sstevel@tonic-gate assert(opt); 33927c478bd9Sstevel@tonic-gate 33937c478bd9Sstevel@tonic-gate val = strchr(opt, '='); 33947c478bd9Sstevel@tonic-gate if (val == NULL) { 33957c478bd9Sstevel@tonic-gate bam_error(INVALID_ENTRY, opt); 33967c478bd9Sstevel@tonic-gate return (BAM_ERROR); 33977c478bd9Sstevel@tonic-gate } 33987c478bd9Sstevel@tonic-gate 33997c478bd9Sstevel@tonic-gate *val = '\0'; 34007c478bd9Sstevel@tonic-gate if (strcmp(opt, "default") == 0) { 34017c478bd9Sstevel@tonic-gate optnum = DEFAULT_CMD; 34027c478bd9Sstevel@tonic-gate } else if (strcmp(opt, "timeout") == 0) { 34037c478bd9Sstevel@tonic-gate optnum = TIMEOUT_CMD; 34047c478bd9Sstevel@tonic-gate } else { 34057c478bd9Sstevel@tonic-gate bam_error(INVALID_ENTRY, opt); 34067c478bd9Sstevel@tonic-gate return (BAM_ERROR); 34077c478bd9Sstevel@tonic-gate } 34087c478bd9Sstevel@tonic-gate 34097c478bd9Sstevel@tonic-gate optval = s_strtol(val + 1); 34107c478bd9Sstevel@tonic-gate *val = '='; 34117c478bd9Sstevel@tonic-gate return (set_global(mp, menu_cmds[optnum], optval)); 34127c478bd9Sstevel@tonic-gate } 34137c478bd9Sstevel@tonic-gate 34147c478bd9Sstevel@tonic-gate /* 34157c478bd9Sstevel@tonic-gate * The quiet argument suppresses messages. This is used 34167c478bd9Sstevel@tonic-gate * when invoked in the context of other commands (e.g. list_entry) 34177c478bd9Sstevel@tonic-gate */ 34187c478bd9Sstevel@tonic-gate static error_t 34197c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet) 34207c478bd9Sstevel@tonic-gate { 34217c478bd9Sstevel@tonic-gate line_t *lp; 34227c478bd9Sstevel@tonic-gate char *arg; 34237c478bd9Sstevel@tonic-gate int done, ret = BAM_SUCCESS; 34247c478bd9Sstevel@tonic-gate 34257c478bd9Sstevel@tonic-gate assert(mp); 34267c478bd9Sstevel@tonic-gate assert(menu_path); 34277c478bd9Sstevel@tonic-gate assert(globalcmd); 34287c478bd9Sstevel@tonic-gate 34297c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 34307c478bd9Sstevel@tonic-gate if (!quiet) 34317c478bd9Sstevel@tonic-gate bam_error(NO_MENU, menu_path); 34327c478bd9Sstevel@tonic-gate return (BAM_ERROR); 34337c478bd9Sstevel@tonic-gate } 34347c478bd9Sstevel@tonic-gate 34357c478bd9Sstevel@tonic-gate done = 0; 34367c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 34377c478bd9Sstevel@tonic-gate if (lp->flags != BAM_GLOBAL) 34387c478bd9Sstevel@tonic-gate continue; 34397c478bd9Sstevel@tonic-gate 34407c478bd9Sstevel@tonic-gate if (lp->cmd == NULL) { 34417c478bd9Sstevel@tonic-gate if (!quiet) 34427c478bd9Sstevel@tonic-gate bam_error(NO_CMD, lp->lineNum); 34437c478bd9Sstevel@tonic-gate continue; 34447c478bd9Sstevel@tonic-gate } 34457c478bd9Sstevel@tonic-gate 34467c478bd9Sstevel@tonic-gate if (strcmp(globalcmd, lp->cmd) != 0) 34477c478bd9Sstevel@tonic-gate continue; 34487c478bd9Sstevel@tonic-gate 34497c478bd9Sstevel@tonic-gate /* Found global. Check for duplicates */ 34507c478bd9Sstevel@tonic-gate if (done && !quiet) { 34517c478bd9Sstevel@tonic-gate bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 34527c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 34537c478bd9Sstevel@tonic-gate } 34547c478bd9Sstevel@tonic-gate 34557c478bd9Sstevel@tonic-gate arg = lp->arg ? lp->arg : ""; 34567c478bd9Sstevel@tonic-gate bam_print(GLOBAL_CMD, globalcmd, arg); 34577c478bd9Sstevel@tonic-gate done = 1; 34587c478bd9Sstevel@tonic-gate } 34597c478bd9Sstevel@tonic-gate 34607c478bd9Sstevel@tonic-gate if (!done && bam_verbose) 34617c478bd9Sstevel@tonic-gate bam_print(NO_ENTRY, globalcmd); 34627c478bd9Sstevel@tonic-gate 34637c478bd9Sstevel@tonic-gate return (ret); 34647c478bd9Sstevel@tonic-gate } 34657c478bd9Sstevel@tonic-gate 34667c478bd9Sstevel@tonic-gate static error_t 34677c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp) 34687c478bd9Sstevel@tonic-gate { 34697c478bd9Sstevel@tonic-gate return (list2file(root, MENU_TMP, GRUB_MENU, mp->start)); 34707c478bd9Sstevel@tonic-gate } 34717c478bd9Sstevel@tonic-gate 34727c478bd9Sstevel@tonic-gate static void 34737c478bd9Sstevel@tonic-gate line_free(line_t *lp) 34747c478bd9Sstevel@tonic-gate { 34757c478bd9Sstevel@tonic-gate if (lp == NULL) 34767c478bd9Sstevel@tonic-gate return; 34777c478bd9Sstevel@tonic-gate 34787c478bd9Sstevel@tonic-gate if (lp->cmd) 34797c478bd9Sstevel@tonic-gate free(lp->cmd); 34807c478bd9Sstevel@tonic-gate if (lp->sep) 34817c478bd9Sstevel@tonic-gate free(lp->sep); 34827c478bd9Sstevel@tonic-gate if (lp->arg) 34837c478bd9Sstevel@tonic-gate free(lp->arg); 34847c478bd9Sstevel@tonic-gate if (lp->line) 34857c478bd9Sstevel@tonic-gate free(lp->line); 34867c478bd9Sstevel@tonic-gate free(lp); 34877c478bd9Sstevel@tonic-gate } 34887c478bd9Sstevel@tonic-gate 34897c478bd9Sstevel@tonic-gate static void 34907c478bd9Sstevel@tonic-gate linelist_free(line_t *start) 34917c478bd9Sstevel@tonic-gate { 34927c478bd9Sstevel@tonic-gate line_t *lp; 34937c478bd9Sstevel@tonic-gate 34947c478bd9Sstevel@tonic-gate while (start) { 34957c478bd9Sstevel@tonic-gate lp = start; 34967c478bd9Sstevel@tonic-gate start = start->next; 34977c478bd9Sstevel@tonic-gate line_free(lp); 34987c478bd9Sstevel@tonic-gate } 34997c478bd9Sstevel@tonic-gate } 35007c478bd9Sstevel@tonic-gate 35017c478bd9Sstevel@tonic-gate static void 35027c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp) 35037c478bd9Sstevel@tonic-gate { 35047c478bd9Sstevel@tonic-gate linelist_free(flistp->head); 35057c478bd9Sstevel@tonic-gate flistp->head = NULL; 35067c478bd9Sstevel@tonic-gate flistp->tail = NULL; 35077c478bd9Sstevel@tonic-gate } 35087c478bd9Sstevel@tonic-gate 35097c478bd9Sstevel@tonic-gate static void 35107c478bd9Sstevel@tonic-gate menu_free(menu_t *mp) 35117c478bd9Sstevel@tonic-gate { 35128c1b6884Sszhou entry_t *ent, *tmp; 35137c478bd9Sstevel@tonic-gate assert(mp); 35147c478bd9Sstevel@tonic-gate 35157c478bd9Sstevel@tonic-gate if (mp->start) 35167c478bd9Sstevel@tonic-gate linelist_free(mp->start); 35178c1b6884Sszhou ent = mp->entries; 35188c1b6884Sszhou while (ent) { 35198c1b6884Sszhou tmp = ent; 35208c1b6884Sszhou ent = tmp->next; 35218c1b6884Sszhou free(tmp); 35228c1b6884Sszhou } 35237c478bd9Sstevel@tonic-gate 35248c1b6884Sszhou free(mp); 35257c478bd9Sstevel@tonic-gate } 35267c478bd9Sstevel@tonic-gate 35277c478bd9Sstevel@tonic-gate /* 35287c478bd9Sstevel@tonic-gate * Utility routines 35297c478bd9Sstevel@tonic-gate */ 35307c478bd9Sstevel@tonic-gate 35317c478bd9Sstevel@tonic-gate 35327c478bd9Sstevel@tonic-gate /* 35337c478bd9Sstevel@tonic-gate * Returns 0 on success 35347c478bd9Sstevel@tonic-gate * Any other value indicates an error 35357c478bd9Sstevel@tonic-gate */ 35367c478bd9Sstevel@tonic-gate static int 35377c478bd9Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize) 35387c478bd9Sstevel@tonic-gate { 35397c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 35407c478bd9Sstevel@tonic-gate int ret; 35417c478bd9Sstevel@tonic-gate FILE *ptr; 35427c478bd9Sstevel@tonic-gate size_t len; 35437c478bd9Sstevel@tonic-gate sigset_t set; 35447c478bd9Sstevel@tonic-gate void (*disp)(int); 35457c478bd9Sstevel@tonic-gate 35467c478bd9Sstevel@tonic-gate /* 35477c478bd9Sstevel@tonic-gate * For security 35487c478bd9Sstevel@tonic-gate * - only absolute paths are allowed 35497c478bd9Sstevel@tonic-gate * - set IFS to space and tab 35507c478bd9Sstevel@tonic-gate */ 35517c478bd9Sstevel@tonic-gate if (*cmdline != '/') { 35527c478bd9Sstevel@tonic-gate bam_error(ABS_PATH_REQ, cmdline); 35537c478bd9Sstevel@tonic-gate return (-1); 35547c478bd9Sstevel@tonic-gate } 35557c478bd9Sstevel@tonic-gate (void) putenv("IFS= \t"); 35567c478bd9Sstevel@tonic-gate 35577c478bd9Sstevel@tonic-gate /* 35587c478bd9Sstevel@tonic-gate * We may have been exec'ed with SIGCHLD blocked 35597c478bd9Sstevel@tonic-gate * unblock it here 35607c478bd9Sstevel@tonic-gate */ 35617c478bd9Sstevel@tonic-gate (void) sigemptyset(&set); 35627c478bd9Sstevel@tonic-gate (void) sigaddset(&set, SIGCHLD); 35637c478bd9Sstevel@tonic-gate if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { 35647c478bd9Sstevel@tonic-gate bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno)); 35657c478bd9Sstevel@tonic-gate return (-1); 35667c478bd9Sstevel@tonic-gate } 35677c478bd9Sstevel@tonic-gate 35687c478bd9Sstevel@tonic-gate /* 35697c478bd9Sstevel@tonic-gate * Set SIGCHLD disposition to SIG_DFL for popen/pclose 35707c478bd9Sstevel@tonic-gate */ 35717c478bd9Sstevel@tonic-gate disp = sigset(SIGCHLD, SIG_DFL); 35727c478bd9Sstevel@tonic-gate if (disp == SIG_ERR) { 35737c478bd9Sstevel@tonic-gate bam_error(FAILED_SIG, strerror(errno)); 35747c478bd9Sstevel@tonic-gate return (-1); 35757c478bd9Sstevel@tonic-gate } 35767c478bd9Sstevel@tonic-gate if (disp == SIG_HOLD) { 35777c478bd9Sstevel@tonic-gate bam_error(BLOCKED_SIG, cmdline); 35787c478bd9Sstevel@tonic-gate return (-1); 35797c478bd9Sstevel@tonic-gate } 35807c478bd9Sstevel@tonic-gate 35817c478bd9Sstevel@tonic-gate ptr = popen(cmdline, "r"); 35827c478bd9Sstevel@tonic-gate if (ptr == NULL) { 35837c478bd9Sstevel@tonic-gate bam_error(POPEN_FAIL, cmdline, strerror(errno)); 35847c478bd9Sstevel@tonic-gate return (-1); 35857c478bd9Sstevel@tonic-gate } 35867c478bd9Sstevel@tonic-gate 35877c478bd9Sstevel@tonic-gate /* 35887c478bd9Sstevel@tonic-gate * If we simply do a pclose() following a popen(), pclose() 35897c478bd9Sstevel@tonic-gate * will close the reader end of the pipe immediately even 35907c478bd9Sstevel@tonic-gate * if the child process has not started/exited. pclose() 35917c478bd9Sstevel@tonic-gate * does wait for cmd to terminate before returning though. 35927c478bd9Sstevel@tonic-gate * When the executed command writes its output to the pipe 35937c478bd9Sstevel@tonic-gate * there is no reader process and the command dies with 35947c478bd9Sstevel@tonic-gate * SIGPIPE. To avoid this we read repeatedly until read 35957c478bd9Sstevel@tonic-gate * terminates with EOF. This indicates that the command 35967c478bd9Sstevel@tonic-gate * (writer) has closed the pipe and we can safely do a 35977c478bd9Sstevel@tonic-gate * pclose(). 35987c478bd9Sstevel@tonic-gate * 35997c478bd9Sstevel@tonic-gate * Since pclose() does wait for the command to exit, 36007c478bd9Sstevel@tonic-gate * we can safely reap the exit status of the command 36017c478bd9Sstevel@tonic-gate * from the value returned by pclose() 36027c478bd9Sstevel@tonic-gate */ 36037c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), ptr) != NULL) { 36047c478bd9Sstevel@tonic-gate /* if (bam_verbose) XXX */ 36057c478bd9Sstevel@tonic-gate bam_print(PRINT_NO_NEWLINE, buf); 36067c478bd9Sstevel@tonic-gate if (output && osize > 0) { 36077c478bd9Sstevel@tonic-gate (void) snprintf(output, osize, "%s", buf); 36087c478bd9Sstevel@tonic-gate len = strlen(buf); 36097c478bd9Sstevel@tonic-gate output += len; 36107c478bd9Sstevel@tonic-gate osize -= len; 36117c478bd9Sstevel@tonic-gate } 36127c478bd9Sstevel@tonic-gate } 36137c478bd9Sstevel@tonic-gate 36147c478bd9Sstevel@tonic-gate ret = pclose(ptr); 36157c478bd9Sstevel@tonic-gate if (ret == -1) { 36167c478bd9Sstevel@tonic-gate bam_error(PCLOSE_FAIL, cmdline, strerror(errno)); 36177c478bd9Sstevel@tonic-gate return (-1); 36187c478bd9Sstevel@tonic-gate } 36197c478bd9Sstevel@tonic-gate 36207c478bd9Sstevel@tonic-gate if (WIFEXITED(ret)) { 36217c478bd9Sstevel@tonic-gate return (WEXITSTATUS(ret)); 36227c478bd9Sstevel@tonic-gate } else { 36237c478bd9Sstevel@tonic-gate bam_error(EXEC_FAIL, cmdline, ret); 36247c478bd9Sstevel@tonic-gate return (-1); 36257c478bd9Sstevel@tonic-gate } 36267c478bd9Sstevel@tonic-gate } 36277c478bd9Sstevel@tonic-gate 36287c478bd9Sstevel@tonic-gate /* 36297c478bd9Sstevel@tonic-gate * Since this function returns -1 on error 36307c478bd9Sstevel@tonic-gate * it cannot be used to convert -1. However, 36317c478bd9Sstevel@tonic-gate * that is sufficient for what we need. 36327c478bd9Sstevel@tonic-gate */ 36337c478bd9Sstevel@tonic-gate static long 36347c478bd9Sstevel@tonic-gate s_strtol(char *str) 36357c478bd9Sstevel@tonic-gate { 36367c478bd9Sstevel@tonic-gate long l; 36377c478bd9Sstevel@tonic-gate char *res = NULL; 36387c478bd9Sstevel@tonic-gate 36397c478bd9Sstevel@tonic-gate if (str == NULL) { 36407c478bd9Sstevel@tonic-gate return (-1); 36417c478bd9Sstevel@tonic-gate } 36427c478bd9Sstevel@tonic-gate 36437c478bd9Sstevel@tonic-gate errno = 0; 36447c478bd9Sstevel@tonic-gate l = strtol(str, &res, 10); 36457c478bd9Sstevel@tonic-gate if (errno || *res != '\0') { 36467c478bd9Sstevel@tonic-gate return (-1); 36477c478bd9Sstevel@tonic-gate } 36487c478bd9Sstevel@tonic-gate 36497c478bd9Sstevel@tonic-gate return (l); 36507c478bd9Sstevel@tonic-gate } 36517c478bd9Sstevel@tonic-gate 36527c478bd9Sstevel@tonic-gate /* 36537c478bd9Sstevel@tonic-gate * Wrapper around fputs, that adds a newline (since fputs doesn't) 36547c478bd9Sstevel@tonic-gate */ 36557c478bd9Sstevel@tonic-gate static int 36567c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp) 36577c478bd9Sstevel@tonic-gate { 36587c478bd9Sstevel@tonic-gate char linebuf[BAM_MAXLINE]; 36597c478bd9Sstevel@tonic-gate 36607c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s\n", str); 36617c478bd9Sstevel@tonic-gate return (fputs(linebuf, fp)); 36627c478bd9Sstevel@tonic-gate } 36637c478bd9Sstevel@tonic-gate 36647c478bd9Sstevel@tonic-gate /* 36657c478bd9Sstevel@tonic-gate * Wrapper around fgets, that strips newlines returned by fgets 36667c478bd9Sstevel@tonic-gate */ 36677c478bd9Sstevel@tonic-gate static char * 36687c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp) 36697c478bd9Sstevel@tonic-gate { 36707c478bd9Sstevel@tonic-gate int n; 36717c478bd9Sstevel@tonic-gate 36727c478bd9Sstevel@tonic-gate buf = fgets(buf, buflen, fp); 36737c478bd9Sstevel@tonic-gate if (buf) { 36747c478bd9Sstevel@tonic-gate n = strlen(buf); 36757c478bd9Sstevel@tonic-gate if (n == buflen - 1 && buf[n-1] != '\n') 36767c478bd9Sstevel@tonic-gate bam_error(TOO_LONG, buflen - 1, buf); 36777c478bd9Sstevel@tonic-gate buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1]; 36787c478bd9Sstevel@tonic-gate } 36797c478bd9Sstevel@tonic-gate 36807c478bd9Sstevel@tonic-gate return (buf); 36817c478bd9Sstevel@tonic-gate } 36827c478bd9Sstevel@tonic-gate 36837c478bd9Sstevel@tonic-gate static void * 36847c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz) 36857c478bd9Sstevel@tonic-gate { 36867c478bd9Sstevel@tonic-gate void *ptr; 36877c478bd9Sstevel@tonic-gate 36887c478bd9Sstevel@tonic-gate ptr = calloc(nelem, sz); 36897c478bd9Sstevel@tonic-gate if (ptr == NULL) { 36907c478bd9Sstevel@tonic-gate bam_error(NO_MEM, nelem*sz); 36917c478bd9Sstevel@tonic-gate bam_exit(1); 36927c478bd9Sstevel@tonic-gate } 36937c478bd9Sstevel@tonic-gate return (ptr); 36947c478bd9Sstevel@tonic-gate } 36957c478bd9Sstevel@tonic-gate 36967c478bd9Sstevel@tonic-gate static char * 36977c478bd9Sstevel@tonic-gate s_strdup(char *str) 36987c478bd9Sstevel@tonic-gate { 36997c478bd9Sstevel@tonic-gate char *ptr; 37007c478bd9Sstevel@tonic-gate 37017c478bd9Sstevel@tonic-gate if (str == NULL) 37027c478bd9Sstevel@tonic-gate return (NULL); 37037c478bd9Sstevel@tonic-gate 37047c478bd9Sstevel@tonic-gate ptr = strdup(str); 37057c478bd9Sstevel@tonic-gate if (ptr == NULL) { 37067c478bd9Sstevel@tonic-gate bam_error(NO_MEM, strlen(str) + 1); 37077c478bd9Sstevel@tonic-gate bam_exit(1); 37087c478bd9Sstevel@tonic-gate } 37097c478bd9Sstevel@tonic-gate return (ptr); 37107c478bd9Sstevel@tonic-gate } 37117c478bd9Sstevel@tonic-gate 37127c478bd9Sstevel@tonic-gate /* 37137c478bd9Sstevel@tonic-gate * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients) 37147c478bd9Sstevel@tonic-gate * Returns 0 otherwise 37157c478bd9Sstevel@tonic-gate */ 37167c478bd9Sstevel@tonic-gate static int 37177c478bd9Sstevel@tonic-gate is_amd64(void) 37187c478bd9Sstevel@tonic-gate { 37197c478bd9Sstevel@tonic-gate static int amd64 = -1; 37207c478bd9Sstevel@tonic-gate char isabuf[257]; /* from sysinfo(2) manpage */ 37217c478bd9Sstevel@tonic-gate 37227c478bd9Sstevel@tonic-gate if (amd64 != -1) 37237c478bd9Sstevel@tonic-gate return (amd64); 37247c478bd9Sstevel@tonic-gate 37257c478bd9Sstevel@tonic-gate if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 && 37267c478bd9Sstevel@tonic-gate strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) 37277c478bd9Sstevel@tonic-gate amd64 = 1; 37287c478bd9Sstevel@tonic-gate else if (strstr(isabuf, "i386") == NULL) 37297c478bd9Sstevel@tonic-gate amd64 = 1; /* diskless server */ 37307c478bd9Sstevel@tonic-gate else 37317c478bd9Sstevel@tonic-gate amd64 = 0; 37327c478bd9Sstevel@tonic-gate 37337c478bd9Sstevel@tonic-gate return (amd64); 37347c478bd9Sstevel@tonic-gate } 37357c478bd9Sstevel@tonic-gate 37367c478bd9Sstevel@tonic-gate static void 37377c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s) 37387c478bd9Sstevel@tonic-gate { 37397c478bd9Sstevel@tonic-gate line_t *lp; 37407c478bd9Sstevel@tonic-gate 37417c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 37427c478bd9Sstevel@tonic-gate lp->line = s_strdup(s); 37437c478bd9Sstevel@tonic-gate if (flistp->head == NULL) 37447c478bd9Sstevel@tonic-gate flistp->head = lp; 37457c478bd9Sstevel@tonic-gate else 37467c478bd9Sstevel@tonic-gate flistp->tail->next = lp; 37477c478bd9Sstevel@tonic-gate flistp->tail = lp; 37487c478bd9Sstevel@tonic-gate } 3749