17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * bootadm(1M) is a new utility for managing bootability of 317c478bd9Sstevel@tonic-gate * Solaris *Newboot* environments. It has two primary tasks: 327c478bd9Sstevel@tonic-gate * - Allow end users to manage bootability of Newboot Solaris instances 337c478bd9Sstevel@tonic-gate * - Provide services to other subsystems in Solaris (primarily Install) 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate /* Headers */ 377c478bd9Sstevel@tonic-gate #include <stdio.h> 387c478bd9Sstevel@tonic-gate #include <errno.h> 397c478bd9Sstevel@tonic-gate #include <stdlib.h> 407c478bd9Sstevel@tonic-gate #include <string.h> 417c478bd9Sstevel@tonic-gate #include <unistd.h> 427c478bd9Sstevel@tonic-gate #include <sys/types.h> 437c478bd9Sstevel@tonic-gate #include <sys/stat.h> 447c478bd9Sstevel@tonic-gate #include <stdarg.h> 457c478bd9Sstevel@tonic-gate #include <limits.h> 467c478bd9Sstevel@tonic-gate #include <signal.h> 477c478bd9Sstevel@tonic-gate #include <sys/wait.h> 487c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 497c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 507c478bd9Sstevel@tonic-gate #include <libnvpair.h> 517c478bd9Sstevel@tonic-gate #include <ftw.h> 527c478bd9Sstevel@tonic-gate #include <fcntl.h> 537c478bd9Sstevel@tonic-gate #include <strings.h> 547c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 557c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #include <pwd.h> 587c478bd9Sstevel@tonic-gate #include <grp.h> 597c478bd9Sstevel@tonic-gate #include <device_info.h> 607c478bd9Sstevel@tonic-gate 61*b610f78eSvikram #include <sys/mount.h> 62*b610f78eSvikram 637c478bd9Sstevel@tonic-gate #include <libintl.h> 647c478bd9Sstevel@tonic-gate #include <locale.h> 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate #include <assert.h> 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate #include "message.h" 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN 717c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD" 727c478bd9Sstevel@tonic-gate #endif /* TEXT_DOMAIN */ 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate /* Type definitions */ 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* Primary subcmds */ 777c478bd9Sstevel@tonic-gate typedef enum { 787c478bd9Sstevel@tonic-gate BAM_MENU = 3, 797c478bd9Sstevel@tonic-gate BAM_ARCHIVE 807c478bd9Sstevel@tonic-gate } subcmd_t; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate /* GRUB menu per-line classification */ 837c478bd9Sstevel@tonic-gate typedef enum { 847c478bd9Sstevel@tonic-gate BAM_INVALID = 0, 857c478bd9Sstevel@tonic-gate BAM_EMPTY, 867c478bd9Sstevel@tonic-gate BAM_COMMENT, 877c478bd9Sstevel@tonic-gate BAM_GLOBAL, 887c478bd9Sstevel@tonic-gate BAM_ENTRY, 897c478bd9Sstevel@tonic-gate BAM_TITLE 907c478bd9Sstevel@tonic-gate } menu_flag_t; 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate /* struct for menu.lst contents */ 937c478bd9Sstevel@tonic-gate typedef struct line { 947c478bd9Sstevel@tonic-gate int lineNum; /* Line number in menu.lst */ 957c478bd9Sstevel@tonic-gate int entryNum; /* menu boot entry #. ENTRY_INIT if not applicable */ 967c478bd9Sstevel@tonic-gate char *cmd; 977c478bd9Sstevel@tonic-gate char *sep; 987c478bd9Sstevel@tonic-gate char *arg; 997c478bd9Sstevel@tonic-gate char *line; 1007c478bd9Sstevel@tonic-gate menu_flag_t flags; 1017c478bd9Sstevel@tonic-gate struct line *next; 1027c478bd9Sstevel@tonic-gate } line_t; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate typedef struct { 1057c478bd9Sstevel@tonic-gate line_t *start; 1067c478bd9Sstevel@tonic-gate line_t *end; 1077c478bd9Sstevel@tonic-gate } menu_t; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate typedef enum { 1107c478bd9Sstevel@tonic-gate OPT_ABSENT = 0, /* No option */ 1117c478bd9Sstevel@tonic-gate OPT_REQ, /* option required */ 1127c478bd9Sstevel@tonic-gate OPT_OPTIONAL /* option may or may not be present */ 1137c478bd9Sstevel@tonic-gate } option_t; 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate typedef enum { 116*b610f78eSvikram BAM_ERROR = -1, /* Must be negative. add_boot_entry() depends on it */ 1177c478bd9Sstevel@tonic-gate BAM_SUCCESS = 0, 1187c478bd9Sstevel@tonic-gate BAM_WRITE = 2 1197c478bd9Sstevel@tonic-gate } error_t; 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate typedef struct { 1227c478bd9Sstevel@tonic-gate char *subcmd; 1237c478bd9Sstevel@tonic-gate option_t option; 1247c478bd9Sstevel@tonic-gate error_t (*handler)(); 1257c478bd9Sstevel@tonic-gate } subcmd_defn_t; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate #define BAM_MAXLINE 8192 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate #define LINE_INIT 0 /* lineNum initial value */ 1317c478bd9Sstevel@tonic-gate #define ENTRY_INIT -1 /* entryNum initial value */ 1327c478bd9Sstevel@tonic-gate #define ALL_ENTRIES -2 /* selects all boot entries */ 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate #define GRUB_DIR "/boot/grub" 1357c478bd9Sstevel@tonic-gate #define MULTI_BOOT "/platform/i86pc/multiboot" 1367c478bd9Sstevel@tonic-gate #define BOOT_ARCHIVE "/platform/i86pc/boot_archive" 1377c478bd9Sstevel@tonic-gate #define GRUB_MENU "/boot/grub/menu.lst" 1387c478bd9Sstevel@tonic-gate #define MENU_TMP "/boot/grub/menu.lst.tmp" 1397c478bd9Sstevel@tonic-gate #define RAMDISK_SPECIAL "/ramdisk" 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /* lock related */ 1427c478bd9Sstevel@tonic-gate #define BAM_LOCK_FILE "/var/run/bootadm.lock" 1437c478bd9Sstevel@tonic-gate #define LOCK_FILE_PERMS (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate #define CREATE_RAMDISK "/boot/solaris/bin/create_ramdisk" 1467c478bd9Sstevel@tonic-gate #define CREATE_DISKMAP "/boot/solaris/bin/create_diskmap" 1477c478bd9Sstevel@tonic-gate #define GRUBDISK_MAP "/var/run/solaris_grubdisk.map" 1487c478bd9Sstevel@tonic-gate 149*b610f78eSvikram #define GRUB_slice "/etc/lu/GRUB_slice" 150*b610f78eSvikram #define GRUB_root "/etc/lu/GRUB_root" 151*b610f78eSvikram #define GRUB_backup_menu "/etc/lu/GRUB_backup_menu" 152*b610f78eSvikram #define GRUB_slice_mntpt "/tmp/GRUB_slice_mntpt" 153*b610f78eSvikram #define LU_ACTIVATE_FILE "/etc/lu/DelayUpdate/activate.sh" 154*b610f78eSvikram 155*b610f78eSvikram #define INSTALLGRUB "/sbin/installgrub" 156*b610f78eSvikram #define STAGE1 "/boot/grub/stage1" 157*b610f78eSvikram #define STAGE2 "/boot/grub/stage2" 158*b610f78eSvikram 159*b610f78eSvikram #define QUIET 1 160*b610f78eSvikram 161*b610f78eSvikram 1627c478bd9Sstevel@tonic-gate /* 1637c478bd9Sstevel@tonic-gate * Default file attributes 1647c478bd9Sstevel@tonic-gate */ 1657c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_MODE 0644 /* default permissions */ 1667c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_UID 0 /* user root */ 1677c478bd9Sstevel@tonic-gate #define DEFAULT_DEV_GID 3 /* group sys */ 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /* 1707c478bd9Sstevel@tonic-gate * Menu related 1717c478bd9Sstevel@tonic-gate * menu_cmd_t and menu_cmds must be kept in sync 1727c478bd9Sstevel@tonic-gate */ 1737c478bd9Sstevel@tonic-gate typedef enum { 1747c478bd9Sstevel@tonic-gate DEFAULT_CMD = 0, 1757c478bd9Sstevel@tonic-gate TIMEOUT_CMD, 1767c478bd9Sstevel@tonic-gate TITLE_CMD, 1777c478bd9Sstevel@tonic-gate ROOT_CMD, 1787c478bd9Sstevel@tonic-gate KERNEL_CMD, 1797c478bd9Sstevel@tonic-gate MODULE_CMD, 1807c478bd9Sstevel@tonic-gate SEP_CMD, 1817c478bd9Sstevel@tonic-gate COMMENT_CMD 1827c478bd9Sstevel@tonic-gate } menu_cmd_t; 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate static char *menu_cmds[] = { 1857c478bd9Sstevel@tonic-gate "default", /* DEFAULT_CMD */ 1867c478bd9Sstevel@tonic-gate "timeout", /* TIMEOUT_CMD */ 1877c478bd9Sstevel@tonic-gate "title", /* TITLE_CMD */ 1887c478bd9Sstevel@tonic-gate "root", /* ROOT_CMD */ 1897c478bd9Sstevel@tonic-gate "kernel", /* KERNEL_CMD */ 1907c478bd9Sstevel@tonic-gate "module", /* MODULE_CMD */ 1917c478bd9Sstevel@tonic-gate " ", /* SEP_CMD */ 1927c478bd9Sstevel@tonic-gate "#", /* COMMENT_CMD */ 1937c478bd9Sstevel@tonic-gate NULL 1947c478bd9Sstevel@tonic-gate }; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate #define OPT_ENTRY_NUM "entry" 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate /* 1997c478bd9Sstevel@tonic-gate * archive related 2007c478bd9Sstevel@tonic-gate */ 2017c478bd9Sstevel@tonic-gate typedef struct { 2027c478bd9Sstevel@tonic-gate line_t *head; 2037c478bd9Sstevel@tonic-gate line_t *tail; 2047c478bd9Sstevel@tonic-gate } filelist_t; 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate #define BOOT_FILE_LIST "boot/solaris/filelist.ramdisk" 2077c478bd9Sstevel@tonic-gate #define ETC_FILE_LIST "etc/boot/solaris/filelist.ramdisk" 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate #define FILE_STAT "boot/solaris/filestat.ramdisk" 2107c478bd9Sstevel@tonic-gate #define FILE_STAT_TMP "boot/solaris/filestat.ramdisk.tmp" 2117c478bd9Sstevel@tonic-gate #define DIR_PERMS (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) 2127c478bd9Sstevel@tonic-gate #define FILE_STAT_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate #define BAM_HDR "---------- ADDED BY BOOTADM - DO NOT EDIT ----------" 2157c478bd9Sstevel@tonic-gate #define BAM_FTR "---------------------END BOOTADM--------------------" 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate /* Globals */ 2197c478bd9Sstevel@tonic-gate static char *prog; 2207c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd; 2217c478bd9Sstevel@tonic-gate static char *bam_root; 2227c478bd9Sstevel@tonic-gate static int bam_rootlen; 2237c478bd9Sstevel@tonic-gate static int bam_root_readonly; 2247c478bd9Sstevel@tonic-gate static char *bam_subcmd; 2257c478bd9Sstevel@tonic-gate static char *bam_opt; 2267c478bd9Sstevel@tonic-gate static int bam_debug; 2277c478bd9Sstevel@tonic-gate static char **bam_argv; 2287c478bd9Sstevel@tonic-gate static int bam_argc; 2297c478bd9Sstevel@tonic-gate static int bam_force; 2307c478bd9Sstevel@tonic-gate static int bam_verbose; 2317c478bd9Sstevel@tonic-gate static int bam_check; 2327c478bd9Sstevel@tonic-gate static int bam_smf_check; 2337c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1; 2347c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/"; 235*b610f78eSvikram static int bam_update_all; 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate /* function prototypes */ 2387c478bd9Sstevel@tonic-gate static void parse_args_internal(int argc, char *argv[]); 2397c478bd9Sstevel@tonic-gate static void parse_args(int argc, char *argv[]); 2407c478bd9Sstevel@tonic-gate static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]); 2417c478bd9Sstevel@tonic-gate static error_t bam_archive(char *subcmd, char *opt); 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate static void bam_error(char *format, ...); 2447c478bd9Sstevel@tonic-gate static void bam_print(char *format, ...); 2457c478bd9Sstevel@tonic-gate static void bam_exit(int excode); 2467c478bd9Sstevel@tonic-gate static void bam_lock(void); 2477c478bd9Sstevel@tonic-gate static void bam_unlock(void); 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate static int exec_cmd(char *cmdline, char *output, int64_t osize); 2507c478bd9Sstevel@tonic-gate static error_t read_globals(menu_t *mp, char *menu_path, 2517c478bd9Sstevel@tonic-gate char *globalcmd, int quiet); 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate static menu_t *menu_read(char *menu_path); 2547c478bd9Sstevel@tonic-gate static error_t menu_write(char *root, menu_t *mp); 2557c478bd9Sstevel@tonic-gate static void linelist_free(line_t *start); 2567c478bd9Sstevel@tonic-gate static void menu_free(menu_t *mp); 2577c478bd9Sstevel@tonic-gate static void line_free(line_t *lp); 2587c478bd9Sstevel@tonic-gate static void filelist_free(filelist_t *flistp); 2597c478bd9Sstevel@tonic-gate static error_t list2file(char *root, char *tmp, 2607c478bd9Sstevel@tonic-gate char *final, line_t *start); 2617c478bd9Sstevel@tonic-gate static error_t list_entry(menu_t *mp, char *menu_path, char *opt); 2627c478bd9Sstevel@tonic-gate static error_t delete_entry(menu_t *mp, char *menu_path, char *opt); 2637c478bd9Sstevel@tonic-gate static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt); 2647c478bd9Sstevel@tonic-gate static error_t update_entry(menu_t *mp, char *root, char *opt); 2657c478bd9Sstevel@tonic-gate static error_t update_temp(menu_t *mp, char *root, char *opt); 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate static error_t update_archive(char *root, char *opt); 2687c478bd9Sstevel@tonic-gate static error_t list_archive(char *root, char *opt); 2697c478bd9Sstevel@tonic-gate static error_t update_all(char *root, char *opt); 2707c478bd9Sstevel@tonic-gate static error_t read_list(char *root, filelist_t *flistp); 2717c478bd9Sstevel@tonic-gate static error_t set_global(menu_t *mp, char *globalcmd, int val); 2727c478bd9Sstevel@tonic-gate static error_t set_option(menu_t *mp, char *globalcmd, char *opt); 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate static long s_strtol(char *str); 2757c478bd9Sstevel@tonic-gate static char *s_fgets(char *buf, int n, FILE *fp); 2767c478bd9Sstevel@tonic-gate static int s_fputs(char *str, FILE *fp); 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate static void *s_calloc(size_t nelem, size_t sz); 2797c478bd9Sstevel@tonic-gate static char *s_strdup(char *str); 2807c478bd9Sstevel@tonic-gate static int is_readonly(char *); 2817c478bd9Sstevel@tonic-gate static int is_amd64(void); 2827c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *); 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate #if defined(__sparc) 2857c478bd9Sstevel@tonic-gate static void sparc_abort(void); 2867c478bd9Sstevel@tonic-gate #endif 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate /* Menu related sub commands */ 2897c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = { 2907c478bd9Sstevel@tonic-gate "set_option", OPT_OPTIONAL, set_option, /* PUB */ 2917c478bd9Sstevel@tonic-gate "list_entry", OPT_OPTIONAL, list_entry, /* PUB */ 2927c478bd9Sstevel@tonic-gate "delete_all_entries", OPT_ABSENT, delete_all_entries, /* PVT */ 2937c478bd9Sstevel@tonic-gate "update_entry", OPT_REQ, update_entry, /* menu */ 2947c478bd9Sstevel@tonic-gate "update_temp", OPT_OPTIONAL, update_temp, /* reboot */ 2957c478bd9Sstevel@tonic-gate NULL, 0, NULL /* must be last */ 2967c478bd9Sstevel@tonic-gate }; 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate /* Archive related sub commands */ 2997c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = { 3007c478bd9Sstevel@tonic-gate "update", OPT_ABSENT, update_archive, /* PUB */ 3017c478bd9Sstevel@tonic-gate "update_all", OPT_ABSENT, update_all, /* PVT */ 3027c478bd9Sstevel@tonic-gate "list", OPT_OPTIONAL, list_archive, /* PUB */ 3037c478bd9Sstevel@tonic-gate NULL, 0, NULL /* must be last */ 3047c478bd9Sstevel@tonic-gate }; 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate static struct { 3077c478bd9Sstevel@tonic-gate nvlist_t *new_nvlp; 3087c478bd9Sstevel@tonic-gate nvlist_t *old_nvlp; 3097c478bd9Sstevel@tonic-gate int need_update; 3107c478bd9Sstevel@tonic-gate } walk_arg; 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate static void 3137c478bd9Sstevel@tonic-gate usage(void) 3147c478bd9Sstevel@tonic-gate { 3157c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "USAGE:\n"); 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate /* archive usage */ 3197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n", 3207c478bd9Sstevel@tonic-gate prog); 3217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog); 3227c478bd9Sstevel@tonic-gate #ifndef __sparc 3237c478bd9Sstevel@tonic-gate /* x86 only */ 3247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog); 3257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog); 3267c478bd9Sstevel@tonic-gate #endif 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate int 3307c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 3317c478bd9Sstevel@tonic-gate { 3327c478bd9Sstevel@tonic-gate error_t ret; 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 3357c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate if ((prog = strrchr(argv[0], '/')) == NULL) { 3387c478bd9Sstevel@tonic-gate prog = argv[0]; 3397c478bd9Sstevel@tonic-gate } else { 3407c478bd9Sstevel@tonic-gate prog++; 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate if (geteuid() != 0) { 3447c478bd9Sstevel@tonic-gate bam_error(MUST_BE_ROOT); 3457c478bd9Sstevel@tonic-gate bam_exit(1); 3467c478bd9Sstevel@tonic-gate } 3477c478bd9Sstevel@tonic-gate 3487c478bd9Sstevel@tonic-gate bam_lock(); 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate /* 3517c478bd9Sstevel@tonic-gate * Don't depend on caller's umask 3527c478bd9Sstevel@tonic-gate */ 3537c478bd9Sstevel@tonic-gate (void) umask(0022); 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate parse_args(argc, argv); 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate #if defined(__sparc) 3587c478bd9Sstevel@tonic-gate /* 3597c478bd9Sstevel@tonic-gate * There are only two valid invocations of bootadm 3607c478bd9Sstevel@tonic-gate * on SPARC: 3617c478bd9Sstevel@tonic-gate * 3627c478bd9Sstevel@tonic-gate * - SPARC diskless server creating boot_archive for i386 clients 3637c478bd9Sstevel@tonic-gate * - archive creation call during reboot of a SPARC system 3647c478bd9Sstevel@tonic-gate * 3657c478bd9Sstevel@tonic-gate * The latter should be a NOP 3667c478bd9Sstevel@tonic-gate */ 3677c478bd9Sstevel@tonic-gate if (bam_cmd != BAM_ARCHIVE) { 3687c478bd9Sstevel@tonic-gate sparc_abort(); 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate #endif 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate switch (bam_cmd) { 3737c478bd9Sstevel@tonic-gate case BAM_MENU: 3747c478bd9Sstevel@tonic-gate ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv); 3757c478bd9Sstevel@tonic-gate break; 3767c478bd9Sstevel@tonic-gate case BAM_ARCHIVE: 3777c478bd9Sstevel@tonic-gate ret = bam_archive(bam_subcmd, bam_opt); 3787c478bd9Sstevel@tonic-gate break; 3797c478bd9Sstevel@tonic-gate default: 3807c478bd9Sstevel@tonic-gate usage(); 3817c478bd9Sstevel@tonic-gate bam_exit(1); 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate if (ret != BAM_SUCCESS) 3857c478bd9Sstevel@tonic-gate bam_exit(1); 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate bam_unlock(); 3887c478bd9Sstevel@tonic-gate return (0); 3897c478bd9Sstevel@tonic-gate } 3907c478bd9Sstevel@tonic-gate 3917c478bd9Sstevel@tonic-gate #if defined(__sparc) 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate static void 3947c478bd9Sstevel@tonic-gate sparc_abort(void) 3957c478bd9Sstevel@tonic-gate { 3967c478bd9Sstevel@tonic-gate bam_error(NOT_ON_SPARC); 3977c478bd9Sstevel@tonic-gate bam_exit(1); 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate #endif 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate /* 4037c478bd9Sstevel@tonic-gate * Equivalence of public and internal commands: 4047c478bd9Sstevel@tonic-gate * update-archive -- -a update 4057c478bd9Sstevel@tonic-gate * list-archive -- -a list 4067c478bd9Sstevel@tonic-gate * set-menu -- -m set_option 4077c478bd9Sstevel@tonic-gate * list-menu -- -m list_entry 4087c478bd9Sstevel@tonic-gate * update-menu -- -m update_entry 4097c478bd9Sstevel@tonic-gate */ 4107c478bd9Sstevel@tonic-gate static struct cmd_map { 4117c478bd9Sstevel@tonic-gate char *bam_cmdname; 4127c478bd9Sstevel@tonic-gate int bam_cmd; 4137c478bd9Sstevel@tonic-gate char *bam_subcmd; 4147c478bd9Sstevel@tonic-gate } cmd_map[] = { 4157c478bd9Sstevel@tonic-gate { "update-archive", BAM_ARCHIVE, "update"}, 4167c478bd9Sstevel@tonic-gate { "list-archive", BAM_ARCHIVE, "list"}, 4177c478bd9Sstevel@tonic-gate { "set-menu", BAM_MENU, "set_option"}, 4187c478bd9Sstevel@tonic-gate { "list-menu", BAM_MENU, "list_entry"}, 4197c478bd9Sstevel@tonic-gate { "update-menu", BAM_MENU, "update_entry"}, 4207c478bd9Sstevel@tonic-gate { NULL, 0, NULL} 4217c478bd9Sstevel@tonic-gate }; 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate /* 4247c478bd9Sstevel@tonic-gate * Commands syntax published in bootadm(1M) are parsed here 4257c478bd9Sstevel@tonic-gate */ 4267c478bd9Sstevel@tonic-gate static void 4277c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[]) 4287c478bd9Sstevel@tonic-gate { 4297c478bd9Sstevel@tonic-gate struct cmd_map *cmp = cmd_map; 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate /* command conforming to the final spec */ 4327c478bd9Sstevel@tonic-gate if (argc > 1 && argv[1][0] != '-') { 4337c478bd9Sstevel@tonic-gate /* 4347c478bd9Sstevel@tonic-gate * Map commands to internal table. 4357c478bd9Sstevel@tonic-gate */ 4367c478bd9Sstevel@tonic-gate while (cmp->bam_cmdname) { 4377c478bd9Sstevel@tonic-gate if (strcmp(argv[1], cmp->bam_cmdname) == 0) { 4387c478bd9Sstevel@tonic-gate bam_cmd = cmp->bam_cmd; 4397c478bd9Sstevel@tonic-gate bam_subcmd = cmp->bam_subcmd; 4407c478bd9Sstevel@tonic-gate break; 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate cmp++; 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate if (cmp->bam_cmdname == NULL) { 4457c478bd9Sstevel@tonic-gate usage(); 4467c478bd9Sstevel@tonic-gate bam_exit(1); 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate argc--; 4497c478bd9Sstevel@tonic-gate argv++; 4507c478bd9Sstevel@tonic-gate } 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate parse_args_internal(argc, argv); 4537c478bd9Sstevel@tonic-gate } 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate /* 4567c478bd9Sstevel@tonic-gate * A combination of public and private commands are parsed here. 4577c478bd9Sstevel@tonic-gate * The internal syntax and the corresponding functionality are: 4587c478bd9Sstevel@tonic-gate * -a update -- update-archive 4597c478bd9Sstevel@tonic-gate * -a list -- list-archive 4607c478bd9Sstevel@tonic-gate * -a update-all -- (reboot to sync all mounted OS archive) 4617c478bd9Sstevel@tonic-gate * -m update_entry -- update-menu 4627c478bd9Sstevel@tonic-gate * -m list_entry -- list-menu 4637c478bd9Sstevel@tonic-gate * -m update_temp -- (reboot -- [boot-args]) 4647c478bd9Sstevel@tonic-gate * -m delete_all_entries -- (called from install) 4657c478bd9Sstevel@tonic-gate */ 4667c478bd9Sstevel@tonic-gate static void 4677c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[]) 4687c478bd9Sstevel@tonic-gate { 4697c478bd9Sstevel@tonic-gate int c, error; 4707c478bd9Sstevel@tonic-gate extern char *optarg; 4717c478bd9Sstevel@tonic-gate extern int optind, opterr; 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate /* Suppress error message from getopt */ 4747c478bd9Sstevel@tonic-gate opterr = 0; 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate error = 0; 4777c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "a:d:fm:no:vR:")) != -1) { 4787c478bd9Sstevel@tonic-gate switch (c) { 4797c478bd9Sstevel@tonic-gate case 'a': 4807c478bd9Sstevel@tonic-gate if (bam_cmd) { 4817c478bd9Sstevel@tonic-gate error = 1; 4827c478bd9Sstevel@tonic-gate bam_error(MULT_CMDS, c); 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate bam_cmd = BAM_ARCHIVE; 4857c478bd9Sstevel@tonic-gate bam_subcmd = optarg; 4867c478bd9Sstevel@tonic-gate break; 4877c478bd9Sstevel@tonic-gate case 'd': 4887c478bd9Sstevel@tonic-gate if (bam_debug) { 4897c478bd9Sstevel@tonic-gate error = 1; 4907c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate bam_debug = s_strtol(optarg); 4937c478bd9Sstevel@tonic-gate break; 4947c478bd9Sstevel@tonic-gate case 'f': 4957c478bd9Sstevel@tonic-gate if (bam_force) { 4967c478bd9Sstevel@tonic-gate error = 1; 4977c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 4987c478bd9Sstevel@tonic-gate } 4997c478bd9Sstevel@tonic-gate bam_force = 1; 5007c478bd9Sstevel@tonic-gate break; 5017c478bd9Sstevel@tonic-gate case 'm': 5027c478bd9Sstevel@tonic-gate if (bam_cmd) { 5037c478bd9Sstevel@tonic-gate error = 1; 5047c478bd9Sstevel@tonic-gate bam_error(MULT_CMDS, c); 5057c478bd9Sstevel@tonic-gate } 5067c478bd9Sstevel@tonic-gate bam_cmd = BAM_MENU; 5077c478bd9Sstevel@tonic-gate bam_subcmd = optarg; 5087c478bd9Sstevel@tonic-gate break; 5097c478bd9Sstevel@tonic-gate case 'n': 5107c478bd9Sstevel@tonic-gate if (bam_check) { 5117c478bd9Sstevel@tonic-gate error = 1; 5127c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate bam_check = 1; 5157c478bd9Sstevel@tonic-gate bam_smf_check = bam_root_readonly; 5167c478bd9Sstevel@tonic-gate break; 5177c478bd9Sstevel@tonic-gate case 'o': 5187c478bd9Sstevel@tonic-gate if (bam_opt) { 5197c478bd9Sstevel@tonic-gate error = 1; 5207c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate bam_opt = optarg; 5237c478bd9Sstevel@tonic-gate break; 5247c478bd9Sstevel@tonic-gate case 'v': 5257c478bd9Sstevel@tonic-gate if (bam_verbose) { 5267c478bd9Sstevel@tonic-gate error = 1; 5277c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5287c478bd9Sstevel@tonic-gate } 5297c478bd9Sstevel@tonic-gate bam_verbose = 1; 5307c478bd9Sstevel@tonic-gate break; 5317c478bd9Sstevel@tonic-gate case 'R': 5327c478bd9Sstevel@tonic-gate if (bam_root) { 5337c478bd9Sstevel@tonic-gate error = 1; 5347c478bd9Sstevel@tonic-gate bam_error(DUP_OPT, c); 5357c478bd9Sstevel@tonic-gate break; 5367c478bd9Sstevel@tonic-gate } else if (realpath(optarg, rootbuf) == NULL) { 5377c478bd9Sstevel@tonic-gate error = 1; 5387c478bd9Sstevel@tonic-gate bam_error(CANT_RESOLVE, optarg, 5397c478bd9Sstevel@tonic-gate strerror(errno)); 5407c478bd9Sstevel@tonic-gate break; 5417c478bd9Sstevel@tonic-gate } 5427c478bd9Sstevel@tonic-gate bam_root = rootbuf; 5437c478bd9Sstevel@tonic-gate if (rootbuf[strlen(rootbuf) - 1] != '/') 5447c478bd9Sstevel@tonic-gate (void) strcat(rootbuf, "/"); 5457c478bd9Sstevel@tonic-gate bam_rootlen = strlen(rootbuf); 5467c478bd9Sstevel@tonic-gate break; 5477c478bd9Sstevel@tonic-gate case '?': 5487c478bd9Sstevel@tonic-gate error = 1; 5497c478bd9Sstevel@tonic-gate bam_error(BAD_OPT, optopt); 5507c478bd9Sstevel@tonic-gate break; 5517c478bd9Sstevel@tonic-gate default : 5527c478bd9Sstevel@tonic-gate error = 1; 5537c478bd9Sstevel@tonic-gate bam_error(BAD_OPT, c); 5547c478bd9Sstevel@tonic-gate break; 5557c478bd9Sstevel@tonic-gate } 5567c478bd9Sstevel@tonic-gate } 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate /* 5597c478bd9Sstevel@tonic-gate * A command option must be specfied 5607c478bd9Sstevel@tonic-gate */ 5617c478bd9Sstevel@tonic-gate if (!bam_cmd) { 5627c478bd9Sstevel@tonic-gate if (bam_opt && strcmp(bam_opt, "all") == 0) { 5637c478bd9Sstevel@tonic-gate usage(); 5647c478bd9Sstevel@tonic-gate bam_exit(0); 5657c478bd9Sstevel@tonic-gate } 5667c478bd9Sstevel@tonic-gate bam_error(NEED_CMD); 5677c478bd9Sstevel@tonic-gate error = 1; 5687c478bd9Sstevel@tonic-gate } 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate if (error) { 5717c478bd9Sstevel@tonic-gate usage(); 5727c478bd9Sstevel@tonic-gate bam_exit(1); 5737c478bd9Sstevel@tonic-gate } 5747c478bd9Sstevel@tonic-gate 5757c478bd9Sstevel@tonic-gate if (optind > argc) { 5767c478bd9Sstevel@tonic-gate bam_error(INT_ERROR, "parse_args"); 5777c478bd9Sstevel@tonic-gate bam_exit(1); 5787c478bd9Sstevel@tonic-gate } else if (optind < argc) { 5797c478bd9Sstevel@tonic-gate bam_argv = &argv[optind]; 5807c478bd9Sstevel@tonic-gate bam_argc = argc - optind; 5817c478bd9Sstevel@tonic-gate } 5827c478bd9Sstevel@tonic-gate 5837c478bd9Sstevel@tonic-gate /* 5847c478bd9Sstevel@tonic-gate * -n implies verbose mode 5857c478bd9Sstevel@tonic-gate */ 5867c478bd9Sstevel@tonic-gate if (bam_check) 5877c478bd9Sstevel@tonic-gate bam_verbose = 1; 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate static error_t 5917c478bd9Sstevel@tonic-gate check_subcmd_and_options( 5927c478bd9Sstevel@tonic-gate char *subcmd, 5937c478bd9Sstevel@tonic-gate char *opt, 5947c478bd9Sstevel@tonic-gate subcmd_defn_t *table, 5957c478bd9Sstevel@tonic-gate error_t (**fp)()) 5967c478bd9Sstevel@tonic-gate { 5977c478bd9Sstevel@tonic-gate int i; 5987c478bd9Sstevel@tonic-gate 5997c478bd9Sstevel@tonic-gate if (subcmd == NULL) { 6007c478bd9Sstevel@tonic-gate bam_error(NEED_SUBCMD); 6017c478bd9Sstevel@tonic-gate return (BAM_ERROR); 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate if (bam_root == NULL) { 6057c478bd9Sstevel@tonic-gate bam_root = rootbuf; 6067c478bd9Sstevel@tonic-gate bam_rootlen = 1; 6077c478bd9Sstevel@tonic-gate } 6087c478bd9Sstevel@tonic-gate 6097c478bd9Sstevel@tonic-gate /* verify that subcmd is valid */ 6107c478bd9Sstevel@tonic-gate for (i = 0; table[i].subcmd != NULL; i++) { 6117c478bd9Sstevel@tonic-gate if (strcmp(table[i].subcmd, subcmd) == 0) 6127c478bd9Sstevel@tonic-gate break; 6137c478bd9Sstevel@tonic-gate } 6147c478bd9Sstevel@tonic-gate 6157c478bd9Sstevel@tonic-gate if (table[i].subcmd == NULL) { 6167c478bd9Sstevel@tonic-gate bam_error(INVALID_SUBCMD, subcmd); 6177c478bd9Sstevel@tonic-gate return (BAM_ERROR); 6187c478bd9Sstevel@tonic-gate } 6197c478bd9Sstevel@tonic-gate 6207c478bd9Sstevel@tonic-gate /* subcmd verifies that opt is appropriate */ 6217c478bd9Sstevel@tonic-gate if (table[i].option != OPT_OPTIONAL) { 6227c478bd9Sstevel@tonic-gate if ((table[i].option == OPT_REQ) ^ (opt != NULL)) { 6237c478bd9Sstevel@tonic-gate if (opt) 6247c478bd9Sstevel@tonic-gate bam_error(NO_OPT_REQ, subcmd); 6257c478bd9Sstevel@tonic-gate else 6267c478bd9Sstevel@tonic-gate bam_error(MISS_OPT, subcmd); 6277c478bd9Sstevel@tonic-gate return (BAM_ERROR); 6287c478bd9Sstevel@tonic-gate } 6297c478bd9Sstevel@tonic-gate } 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate *fp = table[i].handler; 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 6347c478bd9Sstevel@tonic-gate } 6357c478bd9Sstevel@tonic-gate 636*b610f78eSvikram 637*b610f78eSvikram static char * 638*b610f78eSvikram mount_grub_slice(int *mnted, char **physlice, int mode) 639*b610f78eSvikram { 640*b610f78eSvikram struct extmnttab mnt; 641*b610f78eSvikram struct stat sb; 642*b610f78eSvikram char buf[BAM_MAXLINE], dev[PATH_MAX], phys[PATH_MAX], fstype[32]; 643*b610f78eSvikram char *mntpt; 644*b610f78eSvikram int p, l, f; 645*b610f78eSvikram FILE *fp; 646*b610f78eSvikram 647*b610f78eSvikram assert(mnted); 648*b610f78eSvikram *mnted = 0; 649*b610f78eSvikram 650*b610f78eSvikram /* 651*b610f78eSvikram * physlice arg may be NULL 652*b610f78eSvikram */ 653*b610f78eSvikram if (physlice) 654*b610f78eSvikram *physlice = NULL; 655*b610f78eSvikram 656*b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 657*b610f78eSvikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 658*b610f78eSvikram return (NULL); 659*b610f78eSvikram } 660*b610f78eSvikram 661*b610f78eSvikram fp = fopen(GRUB_slice, "r"); 662*b610f78eSvikram if (fp == NULL) { 663*b610f78eSvikram bam_error(OPEN_FAIL, GRUB_slice, strerror(errno)); 664*b610f78eSvikram return (NULL); 665*b610f78eSvikram } 666*b610f78eSvikram 667*b610f78eSvikram dev[0] = fstype[0] = phys[0] = '\0'; 668*b610f78eSvikram p = sizeof ("PHYS_SLICE=") - 1; 669*b610f78eSvikram l = sizeof ("LOG_SLICE=") - 1; 670*b610f78eSvikram f = sizeof ("LOG_FSTYP=") - 1; 671*b610f78eSvikram while (s_fgets(buf, sizeof (buf), fp) != NULL) { 672*b610f78eSvikram if (strncmp(buf, "PHYS_SLICE=", p) == 0) { 673*b610f78eSvikram (void) strlcpy(phys, buf + p, sizeof (phys)); 674*b610f78eSvikram continue; 675*b610f78eSvikram } 676*b610f78eSvikram if (strncmp(buf, "LOG_SLICE=", l) == 0) { 677*b610f78eSvikram (void) strlcpy(dev, buf + l, sizeof (dev)); 678*b610f78eSvikram continue; 679*b610f78eSvikram } 680*b610f78eSvikram if (strncmp(buf, "LOG_FSTYP=", f) == 0) { 681*b610f78eSvikram (void) strlcpy(fstype, buf + f, sizeof (fstype)); 682*b610f78eSvikram continue; 683*b610f78eSvikram } 684*b610f78eSvikram } 685*b610f78eSvikram (void) fclose(fp); 686*b610f78eSvikram 687*b610f78eSvikram if (dev[0] == '\0' || fstype[0] == '\0' || phys[0] == '\0') { 688*b610f78eSvikram bam_error(BAD_SLICE_FILE, GRUB_slice); 689*b610f78eSvikram return (NULL); 690*b610f78eSvikram } 691*b610f78eSvikram 692*b610f78eSvikram if (mode != QUIET) 693*b610f78eSvikram bam_print(USING_GRUB_SLICE, dev); 694*b610f78eSvikram 695*b610f78eSvikram if (physlice) { 696*b610f78eSvikram *physlice = s_strdup(phys); 697*b610f78eSvikram } 698*b610f78eSvikram 699*b610f78eSvikram /* 700*b610f78eSvikram * Check if the slice is already mounted 701*b610f78eSvikram */ 702*b610f78eSvikram fp = fopen(MNTTAB, "r"); 703*b610f78eSvikram if (fp == NULL) { 704*b610f78eSvikram bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 705*b610f78eSvikram if (physlice) { 706*b610f78eSvikram free(*physlice); 707*b610f78eSvikram *physlice = NULL; 708*b610f78eSvikram } 709*b610f78eSvikram return (NULL); 710*b610f78eSvikram } 711*b610f78eSvikram 712*b610f78eSvikram resetmnttab(fp); 713*b610f78eSvikram 714*b610f78eSvikram mntpt = NULL; 715*b610f78eSvikram while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 716*b610f78eSvikram if (strcmp(mnt.mnt_special, dev) == 0) { 717*b610f78eSvikram mntpt = s_strdup(mnt.mnt_mountp); 718*b610f78eSvikram break; 719*b610f78eSvikram } 720*b610f78eSvikram } 721*b610f78eSvikram 722*b610f78eSvikram (void) fclose(fp); 723*b610f78eSvikram 724*b610f78eSvikram if (mntpt) { 725*b610f78eSvikram return (mntpt); 726*b610f78eSvikram } 727*b610f78eSvikram 728*b610f78eSvikram 729*b610f78eSvikram /* 730*b610f78eSvikram * GRUB slice is not mounted, we need to mount it now. 731*b610f78eSvikram * First create the mountpoint 732*b610f78eSvikram */ 733*b610f78eSvikram mntpt = s_calloc(1, PATH_MAX); 734*b610f78eSvikram (void) snprintf(mntpt, PATH_MAX, "%s.%d", GRUB_slice_mntpt, getpid()); 735*b610f78eSvikram if (mkdir(mntpt, 0755) == -1 && errno != EEXIST) { 736*b610f78eSvikram bam_error(MKDIR_FAILED, mntpt, strerror(errno)); 737*b610f78eSvikram free(mntpt); 738*b610f78eSvikram if (physlice) { 739*b610f78eSvikram free(*physlice); 740*b610f78eSvikram *physlice = NULL; 741*b610f78eSvikram } 742*b610f78eSvikram return (NULL); 743*b610f78eSvikram } 744*b610f78eSvikram 745*b610f78eSvikram if (mount(dev, mntpt, 0, fstype, NULL, 0, NULL, 0) != 0) { 746*b610f78eSvikram bam_error(MOUNT_FAILED, dev, fstype, mntpt, strerror(errno)); 747*b610f78eSvikram if (rmdir(mntpt) != 0) { 748*b610f78eSvikram bam_error(RMDIR_FAILED, mntpt, strerror(errno)); 749*b610f78eSvikram } 750*b610f78eSvikram free(mntpt); 751*b610f78eSvikram if (physlice) { 752*b610f78eSvikram free(*physlice); 753*b610f78eSvikram *physlice = NULL; 754*b610f78eSvikram } 755*b610f78eSvikram return (NULL); 756*b610f78eSvikram } 757*b610f78eSvikram 758*b610f78eSvikram *mnted = 1; 759*b610f78eSvikram return (mntpt); 760*b610f78eSvikram } 761*b610f78eSvikram 762*b610f78eSvikram static void 763*b610f78eSvikram umount_grub_slice(int mnted, char *mntpt, char *physlice) 764*b610f78eSvikram { 765*b610f78eSvikram /* 766*b610f78eSvikram * If we have not dealt with GRUB slice 767*b610f78eSvikram * we have nothing to do - just return. 768*b610f78eSvikram */ 769*b610f78eSvikram if (mntpt == NULL) 770*b610f78eSvikram return; 771*b610f78eSvikram 772*b610f78eSvikram 773*b610f78eSvikram /* 774*b610f78eSvikram * If we mounted the filesystem earlier in mount_grub_slice() 775*b610f78eSvikram * unmount it now. 776*b610f78eSvikram */ 777*b610f78eSvikram if (mnted) { 778*b610f78eSvikram if (umount(mntpt) != 0) { 779*b610f78eSvikram bam_error(UMOUNT_FAILED, mntpt, strerror(errno)); 780*b610f78eSvikram } 781*b610f78eSvikram if (rmdir(mntpt) != 0) { 782*b610f78eSvikram bam_error(RMDIR_FAILED, mntpt, strerror(errno)); 783*b610f78eSvikram } 784*b610f78eSvikram } 785*b610f78eSvikram if (physlice) 786*b610f78eSvikram free(physlice); 787*b610f78eSvikram free(mntpt); 788*b610f78eSvikram } 789*b610f78eSvikram 7907c478bd9Sstevel@tonic-gate static error_t 7917c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[]) 7927c478bd9Sstevel@tonic-gate { 7937c478bd9Sstevel@tonic-gate error_t ret; 7947c478bd9Sstevel@tonic-gate char menu_path[PATH_MAX]; 7957c478bd9Sstevel@tonic-gate menu_t *menu; 796*b610f78eSvikram char *mntpt, *menu_root; 797*b610f78eSvikram struct stat sb; 798*b610f78eSvikram int mnted; /* set if we did a mount */ 799*b610f78eSvikram int mode; 8007c478bd9Sstevel@tonic-gate error_t (*f)(menu_t *mp, char *menu_path, char *opt); 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate /* 8037c478bd9Sstevel@tonic-gate * Check arguments 8047c478bd9Sstevel@tonic-gate */ 8057c478bd9Sstevel@tonic-gate ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f); 8067c478bd9Sstevel@tonic-gate if (ret == BAM_ERROR) { 8077c478bd9Sstevel@tonic-gate return (BAM_ERROR); 8087c478bd9Sstevel@tonic-gate } 8097c478bd9Sstevel@tonic-gate 810*b610f78eSvikram if (strcmp(subcmd, "update_temp") == 0) 811*b610f78eSvikram mode = QUIET; 812*b610f78eSvikram else 813*b610f78eSvikram mode = 0; 814*b610f78eSvikram 815*b610f78eSvikram mntpt = NULL; 816*b610f78eSvikram mnted = 0; 817*b610f78eSvikram if (stat(GRUB_slice, &sb) == 0) { 818*b610f78eSvikram mntpt = mount_grub_slice(&mnted, NULL, mode); 819*b610f78eSvikram if (mntpt == NULL) { 820*b610f78eSvikram return (BAM_ERROR); 821*b610f78eSvikram } 822*b610f78eSvikram menu_root = mntpt; 823*b610f78eSvikram } else { 824*b610f78eSvikram menu_root = bam_root; 825*b610f78eSvikram } 826*b610f78eSvikram 8277c478bd9Sstevel@tonic-gate (void) snprintf(menu_path, sizeof (menu_path), "%s%s", 828*b610f78eSvikram menu_root, GRUB_MENU); 8297c478bd9Sstevel@tonic-gate 8307c478bd9Sstevel@tonic-gate menu = menu_read(menu_path); 8317c478bd9Sstevel@tonic-gate assert(menu); 8327c478bd9Sstevel@tonic-gate 8337c478bd9Sstevel@tonic-gate /* 8347c478bd9Sstevel@tonic-gate * Special handling for setting timeout and default 8357c478bd9Sstevel@tonic-gate */ 8367c478bd9Sstevel@tonic-gate if (strcmp(subcmd, "set_option") == 0) { 8377c478bd9Sstevel@tonic-gate if (largc != 1 || largv[0] == NULL) { 8387c478bd9Sstevel@tonic-gate usage(); 839*b610f78eSvikram umount_grub_slice(mnted, mntpt, NULL); 8407c478bd9Sstevel@tonic-gate return (BAM_ERROR); 8417c478bd9Sstevel@tonic-gate } 8427c478bd9Sstevel@tonic-gate opt = largv[0]; 8437c478bd9Sstevel@tonic-gate } else if (largc != 0) { 8447c478bd9Sstevel@tonic-gate usage(); 845*b610f78eSvikram umount_grub_slice(mnted, mntpt, NULL); 8467c478bd9Sstevel@tonic-gate return (BAM_ERROR); 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate 8497c478bd9Sstevel@tonic-gate /* 8507c478bd9Sstevel@tonic-gate * Once the sub-cmd handler has run 8517c478bd9Sstevel@tonic-gate * only the line field is guaranteed to have valid values 8527c478bd9Sstevel@tonic-gate */ 8537c478bd9Sstevel@tonic-gate if (strcmp(subcmd, "update_entry") == 0) 8547c478bd9Sstevel@tonic-gate ret = f(menu, bam_root, opt); 8557c478bd9Sstevel@tonic-gate else 8567c478bd9Sstevel@tonic-gate ret = f(menu, menu_path, opt); 8577c478bd9Sstevel@tonic-gate if (ret == BAM_WRITE) { 858*b610f78eSvikram ret = menu_write(menu_root, menu); 8597c478bd9Sstevel@tonic-gate } 8607c478bd9Sstevel@tonic-gate 8617c478bd9Sstevel@tonic-gate menu_free(menu); 8627c478bd9Sstevel@tonic-gate 863*b610f78eSvikram umount_grub_slice(mnted, mntpt, NULL); 864*b610f78eSvikram 8657c478bd9Sstevel@tonic-gate return (ret); 8667c478bd9Sstevel@tonic-gate } 8677c478bd9Sstevel@tonic-gate 8687c478bd9Sstevel@tonic-gate 8697c478bd9Sstevel@tonic-gate static error_t 8707c478bd9Sstevel@tonic-gate bam_archive( 8717c478bd9Sstevel@tonic-gate char *subcmd, 8727c478bd9Sstevel@tonic-gate char *opt) 8737c478bd9Sstevel@tonic-gate { 8747c478bd9Sstevel@tonic-gate error_t ret; 8757c478bd9Sstevel@tonic-gate error_t (*f)(char *root, char *opt); 8767c478bd9Sstevel@tonic-gate 8777c478bd9Sstevel@tonic-gate /* 8787c478bd9Sstevel@tonic-gate * Check arguments 8797c478bd9Sstevel@tonic-gate */ 8807c478bd9Sstevel@tonic-gate ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f); 8817c478bd9Sstevel@tonic-gate if (ret != BAM_SUCCESS) { 8827c478bd9Sstevel@tonic-gate return (BAM_ERROR); 8837c478bd9Sstevel@tonic-gate } 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate #if defined(__sparc) 8867c478bd9Sstevel@tonic-gate /* 8877c478bd9Sstevel@tonic-gate * A NOP if called on SPARC during reboot 8887c478bd9Sstevel@tonic-gate */ 8897c478bd9Sstevel@tonic-gate if (strcmp(subcmd, "update_all") == 0) 8907c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 8917c478bd9Sstevel@tonic-gate else if (strcmp(subcmd, "update") != 0) 8927c478bd9Sstevel@tonic-gate sparc_abort(); 8937c478bd9Sstevel@tonic-gate #endif 8947c478bd9Sstevel@tonic-gate 8957c478bd9Sstevel@tonic-gate /* 8967c478bd9Sstevel@tonic-gate * Check archive not supported with update_all 8977c478bd9Sstevel@tonic-gate * since it is awkward to display out-of-sync 8987c478bd9Sstevel@tonic-gate * information for each BE. 8997c478bd9Sstevel@tonic-gate */ 9007c478bd9Sstevel@tonic-gate if (bam_check && strcmp(subcmd, "update_all") == 0) { 9017c478bd9Sstevel@tonic-gate bam_error(CHECK_NOT_SUPPORTED, subcmd); 9027c478bd9Sstevel@tonic-gate return (BAM_ERROR); 9037c478bd9Sstevel@tonic-gate } 9047c478bd9Sstevel@tonic-gate 905*b610f78eSvikram if (strcmp(subcmd, "update_all") == 0) 906*b610f78eSvikram bam_update_all = 1; 907*b610f78eSvikram 908*b610f78eSvikram ret = f(bam_root, opt); 909*b610f78eSvikram 910*b610f78eSvikram bam_update_all = 0; 911*b610f78eSvikram 912*b610f78eSvikram return (ret); 9137c478bd9Sstevel@tonic-gate } 9147c478bd9Sstevel@tonic-gate 9157c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 9167c478bd9Sstevel@tonic-gate static void 9177c478bd9Sstevel@tonic-gate bam_error(char *format, ...) 9187c478bd9Sstevel@tonic-gate { 9197c478bd9Sstevel@tonic-gate va_list ap; 9207c478bd9Sstevel@tonic-gate 9217c478bd9Sstevel@tonic-gate va_start(ap, format); 9227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", prog); 9237c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, ap); 9247c478bd9Sstevel@tonic-gate va_end(ap); 9257c478bd9Sstevel@tonic-gate } 9267c478bd9Sstevel@tonic-gate 9277c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 9287c478bd9Sstevel@tonic-gate static void 9297c478bd9Sstevel@tonic-gate bam_print(char *format, ...) 9307c478bd9Sstevel@tonic-gate { 9317c478bd9Sstevel@tonic-gate va_list ap; 9327c478bd9Sstevel@tonic-gate 9337c478bd9Sstevel@tonic-gate va_start(ap, format); 9347c478bd9Sstevel@tonic-gate (void) vfprintf(stdout, format, ap); 9357c478bd9Sstevel@tonic-gate va_end(ap); 9367c478bd9Sstevel@tonic-gate } 9377c478bd9Sstevel@tonic-gate 9387c478bd9Sstevel@tonic-gate static void 9397c478bd9Sstevel@tonic-gate bam_exit(int excode) 9407c478bd9Sstevel@tonic-gate { 9417c478bd9Sstevel@tonic-gate bam_unlock(); 9427c478bd9Sstevel@tonic-gate exit(excode); 9437c478bd9Sstevel@tonic-gate } 9447c478bd9Sstevel@tonic-gate 9457c478bd9Sstevel@tonic-gate static void 9467c478bd9Sstevel@tonic-gate bam_lock(void) 9477c478bd9Sstevel@tonic-gate { 9487c478bd9Sstevel@tonic-gate struct flock lock; 9497c478bd9Sstevel@tonic-gate pid_t pid; 9507c478bd9Sstevel@tonic-gate 9517c478bd9Sstevel@tonic-gate bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS); 9527c478bd9Sstevel@tonic-gate if (bam_lock_fd < 0) { 9537c478bd9Sstevel@tonic-gate /* 9547c478bd9Sstevel@tonic-gate * We may be invoked early in boot for archive verification. 9557c478bd9Sstevel@tonic-gate * In this case, root is readonly and /var/run may not exist. 9567c478bd9Sstevel@tonic-gate * Proceed without the lock 9577c478bd9Sstevel@tonic-gate */ 9587c478bd9Sstevel@tonic-gate if (errno == EROFS || errno == ENOENT) { 9597c478bd9Sstevel@tonic-gate bam_root_readonly = 1; 9607c478bd9Sstevel@tonic-gate return; 9617c478bd9Sstevel@tonic-gate } 9627c478bd9Sstevel@tonic-gate 9637c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno)); 9647c478bd9Sstevel@tonic-gate bam_exit(1); 9657c478bd9Sstevel@tonic-gate } 9667c478bd9Sstevel@tonic-gate 9677c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 9687c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 9697c478bd9Sstevel@tonic-gate lock.l_start = 0; 9707c478bd9Sstevel@tonic-gate lock.l_len = 0; 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) { 9737c478bd9Sstevel@tonic-gate if (errno != EACCES && errno != EAGAIN) { 9747c478bd9Sstevel@tonic-gate bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 9757c478bd9Sstevel@tonic-gate (void) close(bam_lock_fd); 9767c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 9777c478bd9Sstevel@tonic-gate bam_exit(1); 9787c478bd9Sstevel@tonic-gate } 9797c478bd9Sstevel@tonic-gate pid = 0; 9807c478bd9Sstevel@tonic-gate (void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0); 9817c478bd9Sstevel@tonic-gate bam_print(FILE_LOCKED, pid); 9827c478bd9Sstevel@tonic-gate 9837c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 9847c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 9857c478bd9Sstevel@tonic-gate lock.l_start = 0; 9867c478bd9Sstevel@tonic-gate lock.l_len = 0; 9877c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) { 9887c478bd9Sstevel@tonic-gate bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 9897c478bd9Sstevel@tonic-gate (void) close(bam_lock_fd); 9907c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 9917c478bd9Sstevel@tonic-gate bam_exit(1); 9927c478bd9Sstevel@tonic-gate } 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate 9957c478bd9Sstevel@tonic-gate /* We own the lock now */ 9967c478bd9Sstevel@tonic-gate pid = getpid(); 9977c478bd9Sstevel@tonic-gate (void) write(bam_lock_fd, &pid, sizeof (pid)); 9987c478bd9Sstevel@tonic-gate } 9997c478bd9Sstevel@tonic-gate 10007c478bd9Sstevel@tonic-gate static void 10017c478bd9Sstevel@tonic-gate bam_unlock(void) 10027c478bd9Sstevel@tonic-gate { 10037c478bd9Sstevel@tonic-gate struct flock unlock; 10047c478bd9Sstevel@tonic-gate 10057c478bd9Sstevel@tonic-gate /* 10067c478bd9Sstevel@tonic-gate * NOP if we don't hold the lock 10077c478bd9Sstevel@tonic-gate */ 10087c478bd9Sstevel@tonic-gate if (bam_lock_fd < 0) { 10097c478bd9Sstevel@tonic-gate return; 10107c478bd9Sstevel@tonic-gate } 10117c478bd9Sstevel@tonic-gate 10127c478bd9Sstevel@tonic-gate unlock.l_type = F_UNLCK; 10137c478bd9Sstevel@tonic-gate unlock.l_whence = SEEK_SET; 10147c478bd9Sstevel@tonic-gate unlock.l_start = 0; 10157c478bd9Sstevel@tonic-gate unlock.l_len = 0; 10167c478bd9Sstevel@tonic-gate 10177c478bd9Sstevel@tonic-gate if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) { 10187c478bd9Sstevel@tonic-gate bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno)); 10197c478bd9Sstevel@tonic-gate } 10207c478bd9Sstevel@tonic-gate 10217c478bd9Sstevel@tonic-gate if (close(bam_lock_fd) == -1) { 10227c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno)); 10237c478bd9Sstevel@tonic-gate } 10247c478bd9Sstevel@tonic-gate bam_lock_fd = -1; 10257c478bd9Sstevel@tonic-gate } 10267c478bd9Sstevel@tonic-gate 10277c478bd9Sstevel@tonic-gate static error_t 10287c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt) 10297c478bd9Sstevel@tonic-gate { 10307c478bd9Sstevel@tonic-gate filelist_t flist; 10317c478bd9Sstevel@tonic-gate filelist_t *flistp = &flist; 10327c478bd9Sstevel@tonic-gate line_t *lp; 10337c478bd9Sstevel@tonic-gate 10347c478bd9Sstevel@tonic-gate assert(root); 10357c478bd9Sstevel@tonic-gate assert(opt == NULL); 10367c478bd9Sstevel@tonic-gate 10377c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 10387c478bd9Sstevel@tonic-gate if (read_list(root, flistp) != BAM_SUCCESS) { 10397c478bd9Sstevel@tonic-gate return (BAM_ERROR); 10407c478bd9Sstevel@tonic-gate } 10417c478bd9Sstevel@tonic-gate assert(flistp->head && flistp->tail); 10427c478bd9Sstevel@tonic-gate 10437c478bd9Sstevel@tonic-gate for (lp = flistp->head; lp; lp = lp->next) { 10447c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 10457c478bd9Sstevel@tonic-gate } 10467c478bd9Sstevel@tonic-gate 10477c478bd9Sstevel@tonic-gate filelist_free(flistp); 10487c478bd9Sstevel@tonic-gate 10497c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 10507c478bd9Sstevel@tonic-gate } 10517c478bd9Sstevel@tonic-gate 10527c478bd9Sstevel@tonic-gate /* 10537c478bd9Sstevel@tonic-gate * This routine writes a list of lines to a file. 10547c478bd9Sstevel@tonic-gate * The list is *not* freed 10557c478bd9Sstevel@tonic-gate */ 10567c478bd9Sstevel@tonic-gate static error_t 10577c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start) 10587c478bd9Sstevel@tonic-gate { 10597c478bd9Sstevel@tonic-gate char tmpfile[PATH_MAX]; 10607c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 10617c478bd9Sstevel@tonic-gate FILE *fp; 10627c478bd9Sstevel@tonic-gate int ret; 10637c478bd9Sstevel@tonic-gate struct stat sb; 10647c478bd9Sstevel@tonic-gate mode_t mode; 10657c478bd9Sstevel@tonic-gate uid_t root_uid; 10667c478bd9Sstevel@tonic-gate gid_t sys_gid; 10677c478bd9Sstevel@tonic-gate struct passwd *pw; 10687c478bd9Sstevel@tonic-gate struct group *gp; 10697c478bd9Sstevel@tonic-gate 10707c478bd9Sstevel@tonic-gate 10717c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, final); 10727c478bd9Sstevel@tonic-gate 10737c478bd9Sstevel@tonic-gate if (start == NULL) { 10747c478bd9Sstevel@tonic-gate if (stat(path, &sb) != -1) { 10757c478bd9Sstevel@tonic-gate bam_print(UNLINK_EMPTY, path); 10767c478bd9Sstevel@tonic-gate if (unlink(path) != 0) { 10777c478bd9Sstevel@tonic-gate bam_error(UNLINK_FAIL, path, strerror(errno)); 10787c478bd9Sstevel@tonic-gate return (BAM_ERROR); 10797c478bd9Sstevel@tonic-gate } else { 10807c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 10817c478bd9Sstevel@tonic-gate } 10827c478bd9Sstevel@tonic-gate } 10837c478bd9Sstevel@tonic-gate } 10847c478bd9Sstevel@tonic-gate 10857c478bd9Sstevel@tonic-gate /* 10867c478bd9Sstevel@tonic-gate * Preserve attributes of existing file if possible, 10877c478bd9Sstevel@tonic-gate * otherwise ask the system for uid/gid of root/sys. 10887c478bd9Sstevel@tonic-gate * If all fails, fall back on hard-coded defaults. 10897c478bd9Sstevel@tonic-gate */ 10907c478bd9Sstevel@tonic-gate if (stat(path, &sb) != -1) { 10917c478bd9Sstevel@tonic-gate mode = sb.st_mode; 10927c478bd9Sstevel@tonic-gate root_uid = sb.st_uid; 10937c478bd9Sstevel@tonic-gate sys_gid = sb.st_gid; 10947c478bd9Sstevel@tonic-gate } else { 10957c478bd9Sstevel@tonic-gate mode = DEFAULT_DEV_MODE; 10967c478bd9Sstevel@tonic-gate if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) { 10977c478bd9Sstevel@tonic-gate root_uid = pw->pw_uid; 10987c478bd9Sstevel@tonic-gate } else { 10997c478bd9Sstevel@tonic-gate if (bam_verbose) 11007c478bd9Sstevel@tonic-gate bam_error(CANT_FIND_USER, 11017c478bd9Sstevel@tonic-gate DEFAULT_DEV_USER, DEFAULT_DEV_UID); 11027c478bd9Sstevel@tonic-gate root_uid = (uid_t)DEFAULT_DEV_UID; 11037c478bd9Sstevel@tonic-gate } 11047c478bd9Sstevel@tonic-gate if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) { 11057c478bd9Sstevel@tonic-gate sys_gid = gp->gr_gid; 11067c478bd9Sstevel@tonic-gate } else { 11077c478bd9Sstevel@tonic-gate if (bam_verbose) 11087c478bd9Sstevel@tonic-gate bam_error(CANT_FIND_GROUP, 11097c478bd9Sstevel@tonic-gate DEFAULT_DEV_GROUP, DEFAULT_DEV_GID); 11107c478bd9Sstevel@tonic-gate sys_gid = (gid_t)DEFAULT_DEV_GID; 11117c478bd9Sstevel@tonic-gate } 11127c478bd9Sstevel@tonic-gate } 11137c478bd9Sstevel@tonic-gate 11147c478bd9Sstevel@tonic-gate (void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp); 11157c478bd9Sstevel@tonic-gate 11167c478bd9Sstevel@tonic-gate /* Truncate tmpfile first */ 11177c478bd9Sstevel@tonic-gate fp = fopen(tmpfile, "w"); 11187c478bd9Sstevel@tonic-gate if (fp == NULL) { 11197c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 11207c478bd9Sstevel@tonic-gate return (BAM_ERROR); 11217c478bd9Sstevel@tonic-gate } 11227c478bd9Sstevel@tonic-gate ret = fclose(fp); 11237c478bd9Sstevel@tonic-gate if (ret == EOF) { 11247c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 11257c478bd9Sstevel@tonic-gate return (BAM_ERROR); 11267c478bd9Sstevel@tonic-gate } 11277c478bd9Sstevel@tonic-gate 11287c478bd9Sstevel@tonic-gate /* Now open it in append mode */ 11297c478bd9Sstevel@tonic-gate fp = fopen(tmpfile, "a"); 11307c478bd9Sstevel@tonic-gate if (fp == NULL) { 11317c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, tmpfile, strerror(errno)); 11327c478bd9Sstevel@tonic-gate return (BAM_ERROR); 11337c478bd9Sstevel@tonic-gate } 11347c478bd9Sstevel@tonic-gate 11357c478bd9Sstevel@tonic-gate for (; start; start = start->next) { 11367c478bd9Sstevel@tonic-gate ret = s_fputs(start->line, fp); 11377c478bd9Sstevel@tonic-gate if (ret == EOF) { 11387c478bd9Sstevel@tonic-gate bam_error(WRITE_FAIL, tmpfile, strerror(errno)); 11397c478bd9Sstevel@tonic-gate (void) fclose(fp); 11407c478bd9Sstevel@tonic-gate return (BAM_ERROR); 11417c478bd9Sstevel@tonic-gate } 11427c478bd9Sstevel@tonic-gate } 11437c478bd9Sstevel@tonic-gate 11447c478bd9Sstevel@tonic-gate ret = fclose(fp); 11457c478bd9Sstevel@tonic-gate if (ret == EOF) { 11467c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, tmpfile, strerror(errno)); 11477c478bd9Sstevel@tonic-gate return (BAM_ERROR); 11487c478bd9Sstevel@tonic-gate } 11497c478bd9Sstevel@tonic-gate 11507c478bd9Sstevel@tonic-gate /* 1151de531be4Sjg * Set up desired attributes. Ignore failures on filesystems 1152de531be4Sjg * not supporting these operations - pcfs reports unsupported 1153de531be4Sjg * operations as EINVAL. 11547c478bd9Sstevel@tonic-gate */ 11557c478bd9Sstevel@tonic-gate ret = chmod(tmpfile, mode); 1156de531be4Sjg if (ret == -1 && 1157de531be4Sjg errno != EINVAL && errno != ENOTSUP) { 11587c478bd9Sstevel@tonic-gate bam_error(CHMOD_FAIL, tmpfile, strerror(errno)); 11597c478bd9Sstevel@tonic-gate return (BAM_ERROR); 11607c478bd9Sstevel@tonic-gate } 11617c478bd9Sstevel@tonic-gate 11627c478bd9Sstevel@tonic-gate ret = chown(tmpfile, root_uid, sys_gid); 1163de531be4Sjg if (ret == -1 && 1164de531be4Sjg errno != EINVAL && errno != ENOTSUP) { 11657c478bd9Sstevel@tonic-gate bam_error(CHOWN_FAIL, tmpfile, strerror(errno)); 11667c478bd9Sstevel@tonic-gate return (BAM_ERROR); 11677c478bd9Sstevel@tonic-gate } 11687c478bd9Sstevel@tonic-gate 11697c478bd9Sstevel@tonic-gate 11707c478bd9Sstevel@tonic-gate /* 11717c478bd9Sstevel@tonic-gate * Do an atomic rename 11727c478bd9Sstevel@tonic-gate */ 11737c478bd9Sstevel@tonic-gate ret = rename(tmpfile, path); 11747c478bd9Sstevel@tonic-gate if (ret != 0) { 11757c478bd9Sstevel@tonic-gate bam_error(RENAME_FAIL, path, strerror(errno)); 11767c478bd9Sstevel@tonic-gate return (BAM_ERROR); 11777c478bd9Sstevel@tonic-gate } 11787c478bd9Sstevel@tonic-gate 11797c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 11807c478bd9Sstevel@tonic-gate } 11817c478bd9Sstevel@tonic-gate 11827c478bd9Sstevel@tonic-gate /* 11837c478bd9Sstevel@tonic-gate * This function should always return 0 - since we want 11847c478bd9Sstevel@tonic-gate * to create stat data for *all* files in the list. 11857c478bd9Sstevel@tonic-gate */ 11867c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 11877c478bd9Sstevel@tonic-gate static int 11887c478bd9Sstevel@tonic-gate cmpstat( 11897c478bd9Sstevel@tonic-gate const char *file, 11907c478bd9Sstevel@tonic-gate const struct stat *stat, 11917c478bd9Sstevel@tonic-gate int flags, 11927c478bd9Sstevel@tonic-gate struct FTW *ftw) 11937c478bd9Sstevel@tonic-gate { 11947c478bd9Sstevel@tonic-gate uint_t sz; 11957c478bd9Sstevel@tonic-gate uint64_t *value; 11967c478bd9Sstevel@tonic-gate uint64_t filestat[2]; 11977c478bd9Sstevel@tonic-gate int error; 11987c478bd9Sstevel@tonic-gate 11997c478bd9Sstevel@tonic-gate /* 12007c478bd9Sstevel@tonic-gate * We only want regular files 12017c478bd9Sstevel@tonic-gate */ 12027c478bd9Sstevel@tonic-gate if (!S_ISREG(stat->st_mode)) 12037c478bd9Sstevel@tonic-gate return (0); 12047c478bd9Sstevel@tonic-gate 12057c478bd9Sstevel@tonic-gate /* 12067c478bd9Sstevel@tonic-gate * new_nvlp may be NULL if there were errors earlier 12077c478bd9Sstevel@tonic-gate * but this is not fatal to update determination. 12087c478bd9Sstevel@tonic-gate */ 12097c478bd9Sstevel@tonic-gate if (walk_arg.new_nvlp) { 12107c478bd9Sstevel@tonic-gate filestat[0] = stat->st_size; 12117c478bd9Sstevel@tonic-gate filestat[1] = stat->st_mtime; 12127c478bd9Sstevel@tonic-gate error = nvlist_add_uint64_array(walk_arg.new_nvlp, 12137c478bd9Sstevel@tonic-gate file + bam_rootlen, filestat, 2); 12147c478bd9Sstevel@tonic-gate if (error) 12157c478bd9Sstevel@tonic-gate bam_error(NVADD_FAIL, file, strerror(error)); 12167c478bd9Sstevel@tonic-gate } 12177c478bd9Sstevel@tonic-gate 12187c478bd9Sstevel@tonic-gate /* 12197c478bd9Sstevel@tonic-gate * The remaining steps are only required if we haven't made a 12207c478bd9Sstevel@tonic-gate * decision about update or if we are checking (-n) 12217c478bd9Sstevel@tonic-gate */ 12227c478bd9Sstevel@tonic-gate if (walk_arg.need_update && !bam_check) 12237c478bd9Sstevel@tonic-gate return (0); 12247c478bd9Sstevel@tonic-gate 12257c478bd9Sstevel@tonic-gate /* 12267c478bd9Sstevel@tonic-gate * If we are invoked as part of system/filesyste/boot-archive 12277c478bd9Sstevel@tonic-gate * SMF service, ignore amd64 modules unless we are booted amd64. 12287c478bd9Sstevel@tonic-gate */ 12297c478bd9Sstevel@tonic-gate if (bam_smf_check && !is_amd64() && strstr(file, "/amd64/") == 0) 12307c478bd9Sstevel@tonic-gate return (0); 12317c478bd9Sstevel@tonic-gate 12327c478bd9Sstevel@tonic-gate /* 12337c478bd9Sstevel@tonic-gate * We need an update if file doesn't exist in old archive 12347c478bd9Sstevel@tonic-gate */ 12357c478bd9Sstevel@tonic-gate if (walk_arg.old_nvlp == NULL || 12367c478bd9Sstevel@tonic-gate nvlist_lookup_uint64_array(walk_arg.old_nvlp, 12377c478bd9Sstevel@tonic-gate file + bam_rootlen, &value, &sz) != 0) { 12387c478bd9Sstevel@tonic-gate if (bam_smf_check) /* ignore new during smf check */ 12397c478bd9Sstevel@tonic-gate return (0); 12407c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 12417c478bd9Sstevel@tonic-gate if (bam_verbose) 12427c478bd9Sstevel@tonic-gate bam_print(PARSEABLE_NEW_FILE, file); 12437c478bd9Sstevel@tonic-gate return (0); 12447c478bd9Sstevel@tonic-gate } 12457c478bd9Sstevel@tonic-gate 12467c478bd9Sstevel@tonic-gate /* 12477c478bd9Sstevel@tonic-gate * File exists in old archive. Check if file has changed 12487c478bd9Sstevel@tonic-gate */ 12497c478bd9Sstevel@tonic-gate assert(sz == 2); 12507c478bd9Sstevel@tonic-gate bcopy(value, filestat, sizeof (filestat)); 12517c478bd9Sstevel@tonic-gate 12527c478bd9Sstevel@tonic-gate if (filestat[0] != stat->st_size || 12537c478bd9Sstevel@tonic-gate filestat[1] != stat->st_mtime) { 12547c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 12557c478bd9Sstevel@tonic-gate if (bam_verbose) 12567c478bd9Sstevel@tonic-gate if (bam_smf_check) 12577c478bd9Sstevel@tonic-gate bam_print(" %s\n", file); 12587c478bd9Sstevel@tonic-gate else 12597c478bd9Sstevel@tonic-gate bam_print(PARSEABLE_OUT_DATE, file); 12607c478bd9Sstevel@tonic-gate } 12617c478bd9Sstevel@tonic-gate 12627c478bd9Sstevel@tonic-gate return (0); 12637c478bd9Sstevel@tonic-gate } 12647c478bd9Sstevel@tonic-gate 12657c478bd9Sstevel@tonic-gate /* 12667c478bd9Sstevel@tonic-gate * Check flags and presence of required files. 12677c478bd9Sstevel@tonic-gate * The force flag and/or absence of files should 12687c478bd9Sstevel@tonic-gate * trigger an update. 12697c478bd9Sstevel@tonic-gate * Suppress stdout output if check (-n) option is set 12707c478bd9Sstevel@tonic-gate * (as -n should only produce parseable output.) 12717c478bd9Sstevel@tonic-gate */ 12727c478bd9Sstevel@tonic-gate static void 12737c478bd9Sstevel@tonic-gate check_flags_and_files(char *root) 12747c478bd9Sstevel@tonic-gate { 12757c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 12767c478bd9Sstevel@tonic-gate struct stat sb; 12777c478bd9Sstevel@tonic-gate 12787c478bd9Sstevel@tonic-gate /* 12797c478bd9Sstevel@tonic-gate * if force, create archive unconditionally 12807c478bd9Sstevel@tonic-gate */ 12817c478bd9Sstevel@tonic-gate if (bam_force) { 12827c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 12837c478bd9Sstevel@tonic-gate if (bam_verbose && !bam_check) 12847c478bd9Sstevel@tonic-gate bam_print(UPDATE_FORCE); 12857c478bd9Sstevel@tonic-gate return; 12867c478bd9Sstevel@tonic-gate } 12877c478bd9Sstevel@tonic-gate 12887c478bd9Sstevel@tonic-gate /* 12897c478bd9Sstevel@tonic-gate * If archive is missing, create archive 12907c478bd9Sstevel@tonic-gate */ 12917c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE); 12927c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) { 12937c478bd9Sstevel@tonic-gate if (bam_verbose && !bam_check) 12947c478bd9Sstevel@tonic-gate bam_print(UPDATE_ARCH_MISS, path); 12957c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 12967c478bd9Sstevel@tonic-gate return; 12977c478bd9Sstevel@tonic-gate } 12987c478bd9Sstevel@tonic-gate } 12997c478bd9Sstevel@tonic-gate 13007c478bd9Sstevel@tonic-gate static error_t 13017c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t *flistp, char *filelist) 13027c478bd9Sstevel@tonic-gate { 13037c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 13047c478bd9Sstevel@tonic-gate FILE *fp; 13057c478bd9Sstevel@tonic-gate char buf[BAM_MAXLINE]; 13067c478bd9Sstevel@tonic-gate 13077c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, filelist); 13087c478bd9Sstevel@tonic-gate 13097c478bd9Sstevel@tonic-gate fp = fopen(path, "r"); 13107c478bd9Sstevel@tonic-gate if (fp == NULL) { 13117c478bd9Sstevel@tonic-gate if (bam_debug) 13127c478bd9Sstevel@tonic-gate bam_error(FLIST_FAIL, path, strerror(errno)); 13137c478bd9Sstevel@tonic-gate return (BAM_ERROR); 13147c478bd9Sstevel@tonic-gate } 13157c478bd9Sstevel@tonic-gate while (s_fgets(buf, sizeof (buf), fp) != NULL) { 1316*b610f78eSvikram /* skip blank lines */ 1317*b610f78eSvikram if (strspn(buf, " \t") == strlen(buf)) 1318*b610f78eSvikram continue; 13197c478bd9Sstevel@tonic-gate append_to_flist(flistp, buf); 13207c478bd9Sstevel@tonic-gate } 13217c478bd9Sstevel@tonic-gate if (fclose(fp) != 0) { 13227c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, path, strerror(errno)); 13237c478bd9Sstevel@tonic-gate return (BAM_ERROR); 13247c478bd9Sstevel@tonic-gate } 13257c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 13267c478bd9Sstevel@tonic-gate } 13277c478bd9Sstevel@tonic-gate 13287c478bd9Sstevel@tonic-gate static error_t 13297c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t *flistp) 13307c478bd9Sstevel@tonic-gate { 13317c478bd9Sstevel@tonic-gate int rval; 13327c478bd9Sstevel@tonic-gate 13337c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 13347c478bd9Sstevel@tonic-gate 13357c478bd9Sstevel@tonic-gate /* 13367c478bd9Sstevel@tonic-gate * Read current lists of files - only the first is mandatory 13377c478bd9Sstevel@tonic-gate */ 13387c478bd9Sstevel@tonic-gate rval = read_one_list(root, flistp, BOOT_FILE_LIST); 13397c478bd9Sstevel@tonic-gate if (rval != BAM_SUCCESS) 13407c478bd9Sstevel@tonic-gate return (rval); 13417c478bd9Sstevel@tonic-gate (void) read_one_list(root, flistp, ETC_FILE_LIST); 13427c478bd9Sstevel@tonic-gate 13437c478bd9Sstevel@tonic-gate if (flistp->head == NULL) { 13447c478bd9Sstevel@tonic-gate bam_error(NO_FLIST); 13457c478bd9Sstevel@tonic-gate return (BAM_ERROR); 13467c478bd9Sstevel@tonic-gate } 13477c478bd9Sstevel@tonic-gate 13487c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 13497c478bd9Sstevel@tonic-gate } 13507c478bd9Sstevel@tonic-gate 13517c478bd9Sstevel@tonic-gate static void 13527c478bd9Sstevel@tonic-gate getoldstat(char *root) 13537c478bd9Sstevel@tonic-gate { 13547c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 13557c478bd9Sstevel@tonic-gate int fd, error; 13567c478bd9Sstevel@tonic-gate struct stat sb; 13577c478bd9Sstevel@tonic-gate char *ostat; 13587c478bd9Sstevel@tonic-gate 13597c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 13607c478bd9Sstevel@tonic-gate fd = open(path, O_RDONLY); 13617c478bd9Sstevel@tonic-gate if (fd == -1) { 13627c478bd9Sstevel@tonic-gate if (bam_verbose) 13637c478bd9Sstevel@tonic-gate bam_print(OPEN_FAIL, path, strerror(errno)); 13647c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 13657c478bd9Sstevel@tonic-gate return; 13667c478bd9Sstevel@tonic-gate } 13677c478bd9Sstevel@tonic-gate 13687c478bd9Sstevel@tonic-gate if (fstat(fd, &sb) != 0) { 13697c478bd9Sstevel@tonic-gate bam_error(STAT_FAIL, path, strerror(errno)); 13707c478bd9Sstevel@tonic-gate (void) close(fd); 13717c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 13727c478bd9Sstevel@tonic-gate return; 13737c478bd9Sstevel@tonic-gate } 13747c478bd9Sstevel@tonic-gate 13757c478bd9Sstevel@tonic-gate ostat = s_calloc(1, sb.st_size); 13767c478bd9Sstevel@tonic-gate 13777c478bd9Sstevel@tonic-gate if (read(fd, ostat, sb.st_size) != sb.st_size) { 13787c478bd9Sstevel@tonic-gate bam_error(READ_FAIL, path, strerror(errno)); 13797c478bd9Sstevel@tonic-gate (void) close(fd); 13807c478bd9Sstevel@tonic-gate free(ostat); 13817c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 13827c478bd9Sstevel@tonic-gate return; 13837c478bd9Sstevel@tonic-gate } 13847c478bd9Sstevel@tonic-gate 13857c478bd9Sstevel@tonic-gate (void) close(fd); 13867c478bd9Sstevel@tonic-gate 13877c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 13887c478bd9Sstevel@tonic-gate error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0); 13897c478bd9Sstevel@tonic-gate 13907c478bd9Sstevel@tonic-gate free(ostat); 13917c478bd9Sstevel@tonic-gate 13927c478bd9Sstevel@tonic-gate if (error) { 13937c478bd9Sstevel@tonic-gate bam_error(UNPACK_FAIL, path, strerror(error)); 13947c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 13957c478bd9Sstevel@tonic-gate walk_arg.need_update = 1; 13967c478bd9Sstevel@tonic-gate return; 13977c478bd9Sstevel@tonic-gate } 13987c478bd9Sstevel@tonic-gate } 13997c478bd9Sstevel@tonic-gate 14007c478bd9Sstevel@tonic-gate static void 14017c478bd9Sstevel@tonic-gate create_newstat(void) 14027c478bd9Sstevel@tonic-gate { 14037c478bd9Sstevel@tonic-gate int error; 14047c478bd9Sstevel@tonic-gate 14057c478bd9Sstevel@tonic-gate error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0); 14067c478bd9Sstevel@tonic-gate if (error) { 14077c478bd9Sstevel@tonic-gate /* 14087c478bd9Sstevel@tonic-gate * Not fatal - we can still create archive 14097c478bd9Sstevel@tonic-gate */ 14107c478bd9Sstevel@tonic-gate walk_arg.new_nvlp = NULL; 14117c478bd9Sstevel@tonic-gate bam_error(NVALLOC_FAIL, strerror(error)); 14127c478bd9Sstevel@tonic-gate } 14137c478bd9Sstevel@tonic-gate } 14147c478bd9Sstevel@tonic-gate 14157c478bd9Sstevel@tonic-gate static void 14167c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp) 14177c478bd9Sstevel@tonic-gate { 14187c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 14197c478bd9Sstevel@tonic-gate line_t *lp; 14207c478bd9Sstevel@tonic-gate 14217c478bd9Sstevel@tonic-gate for (lp = flistp->head; lp; lp = lp->next) { 14227c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, lp->line); 14237c478bd9Sstevel@tonic-gate /* XXX shouldn't we use FTW_MOUNT ? */ 14247c478bd9Sstevel@tonic-gate if (nftw(path, cmpstat, 20, 0) == -1) { 14257c478bd9Sstevel@tonic-gate /* 14267c478bd9Sstevel@tonic-gate * Some files may not exist. 14277c478bd9Sstevel@tonic-gate * For example: etc/rtc_config on a x86 diskless system 14287c478bd9Sstevel@tonic-gate * Emit verbose message only 14297c478bd9Sstevel@tonic-gate */ 14307c478bd9Sstevel@tonic-gate if (bam_verbose) 14317c478bd9Sstevel@tonic-gate bam_print(NFTW_FAIL, path, strerror(errno)); 14327c478bd9Sstevel@tonic-gate } 14337c478bd9Sstevel@tonic-gate } 14347c478bd9Sstevel@tonic-gate } 14357c478bd9Sstevel@tonic-gate 14367c478bd9Sstevel@tonic-gate static void 14377c478bd9Sstevel@tonic-gate savenew(char *root) 14387c478bd9Sstevel@tonic-gate { 14397c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 14407c478bd9Sstevel@tonic-gate char path2[PATH_MAX]; 14417c478bd9Sstevel@tonic-gate size_t sz; 14427c478bd9Sstevel@tonic-gate char *nstat; 14437c478bd9Sstevel@tonic-gate int fd, wrote, error; 14447c478bd9Sstevel@tonic-gate 14457c478bd9Sstevel@tonic-gate nstat = NULL; 14467c478bd9Sstevel@tonic-gate sz = 0; 14477c478bd9Sstevel@tonic-gate error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz, 14487c478bd9Sstevel@tonic-gate NV_ENCODE_XDR, 0); 14497c478bd9Sstevel@tonic-gate if (error) { 14507c478bd9Sstevel@tonic-gate bam_error(PACK_FAIL, strerror(error)); 14517c478bd9Sstevel@tonic-gate return; 14527c478bd9Sstevel@tonic-gate } 14537c478bd9Sstevel@tonic-gate 14547c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP); 14557c478bd9Sstevel@tonic-gate fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE); 14567c478bd9Sstevel@tonic-gate if (fd == -1) { 14577c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, path, strerror(errno)); 14587c478bd9Sstevel@tonic-gate free(nstat); 14597c478bd9Sstevel@tonic-gate return; 14607c478bd9Sstevel@tonic-gate } 14617c478bd9Sstevel@tonic-gate wrote = write(fd, nstat, sz); 14627c478bd9Sstevel@tonic-gate if (wrote != sz) { 14637c478bd9Sstevel@tonic-gate bam_error(WRITE_FAIL, path, strerror(errno)); 14647c478bd9Sstevel@tonic-gate (void) close(fd); 14657c478bd9Sstevel@tonic-gate free(nstat); 14667c478bd9Sstevel@tonic-gate return; 14677c478bd9Sstevel@tonic-gate } 14687c478bd9Sstevel@tonic-gate (void) close(fd); 14697c478bd9Sstevel@tonic-gate free(nstat); 14707c478bd9Sstevel@tonic-gate 14717c478bd9Sstevel@tonic-gate (void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT); 14727c478bd9Sstevel@tonic-gate if (rename(path, path2) != 0) { 14737c478bd9Sstevel@tonic-gate bam_error(RENAME_FAIL, path2, strerror(errno)); 14747c478bd9Sstevel@tonic-gate } 14757c478bd9Sstevel@tonic-gate } 14767c478bd9Sstevel@tonic-gate 14777c478bd9Sstevel@tonic-gate static void 14787c478bd9Sstevel@tonic-gate clear_walk_args(void) 14797c478bd9Sstevel@tonic-gate { 14807c478bd9Sstevel@tonic-gate if (walk_arg.old_nvlp) 14817c478bd9Sstevel@tonic-gate nvlist_free(walk_arg.old_nvlp); 14827c478bd9Sstevel@tonic-gate if (walk_arg.new_nvlp) 14837c478bd9Sstevel@tonic-gate nvlist_free(walk_arg.new_nvlp); 14847c478bd9Sstevel@tonic-gate walk_arg.need_update = 0; 14857c478bd9Sstevel@tonic-gate walk_arg.old_nvlp = NULL; 14867c478bd9Sstevel@tonic-gate walk_arg.new_nvlp = NULL; 14877c478bd9Sstevel@tonic-gate } 14887c478bd9Sstevel@tonic-gate 14897c478bd9Sstevel@tonic-gate /* 14907c478bd9Sstevel@tonic-gate * Returns: 14917c478bd9Sstevel@tonic-gate * 0 - no update necessary 14927c478bd9Sstevel@tonic-gate * 1 - update required. 14937c478bd9Sstevel@tonic-gate * BAM_ERROR (-1) - An error occurred 14947c478bd9Sstevel@tonic-gate * 14957c478bd9Sstevel@tonic-gate * Special handling for check (-n): 14967c478bd9Sstevel@tonic-gate * ================================ 14977c478bd9Sstevel@tonic-gate * The check (-n) option produces parseable output. 14987c478bd9Sstevel@tonic-gate * To do this, we suppress all stdout messages unrelated 14997c478bd9Sstevel@tonic-gate * to out of sync files. 15007c478bd9Sstevel@tonic-gate * All stderr messages are still printed though. 15017c478bd9Sstevel@tonic-gate * 15027c478bd9Sstevel@tonic-gate */ 15037c478bd9Sstevel@tonic-gate static int 15047c478bd9Sstevel@tonic-gate update_required(char *root) 15057c478bd9Sstevel@tonic-gate { 15067c478bd9Sstevel@tonic-gate struct stat sb; 15077c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 15087c478bd9Sstevel@tonic-gate filelist_t flist; 15097c478bd9Sstevel@tonic-gate filelist_t *flistp = &flist; 15107c478bd9Sstevel@tonic-gate int need_update; 15117c478bd9Sstevel@tonic-gate 15127c478bd9Sstevel@tonic-gate flistp->head = flistp->tail = NULL; 15137c478bd9Sstevel@tonic-gate 15147c478bd9Sstevel@tonic-gate walk_arg.need_update = 0; 15157c478bd9Sstevel@tonic-gate 15167c478bd9Sstevel@tonic-gate /* 15177c478bd9Sstevel@tonic-gate * Without consulting stat data, check if we need update 15187c478bd9Sstevel@tonic-gate */ 15197c478bd9Sstevel@tonic-gate check_flags_and_files(root); 15207c478bd9Sstevel@tonic-gate 15217c478bd9Sstevel@tonic-gate /* 15227c478bd9Sstevel@tonic-gate * In certain deployment scenarios, filestat may not 15237c478bd9Sstevel@tonic-gate * exist. Ignore it during boot-archive SMF check. 15247c478bd9Sstevel@tonic-gate */ 15257c478bd9Sstevel@tonic-gate if (bam_smf_check) { 15267c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT); 15277c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) 15287c478bd9Sstevel@tonic-gate return (0); 15297c478bd9Sstevel@tonic-gate } 15307c478bd9Sstevel@tonic-gate 15317c478bd9Sstevel@tonic-gate /* 15327c478bd9Sstevel@tonic-gate * consult stat data only if we haven't made a decision 15337c478bd9Sstevel@tonic-gate * about update. If checking (-n) however, we always 15347c478bd9Sstevel@tonic-gate * need stat data (since we want to compare old and new) 15357c478bd9Sstevel@tonic-gate */ 15367c478bd9Sstevel@tonic-gate if (!walk_arg.need_update || bam_check) 15377c478bd9Sstevel@tonic-gate getoldstat(root); 15387c478bd9Sstevel@tonic-gate 15397c478bd9Sstevel@tonic-gate /* 15407c478bd9Sstevel@tonic-gate * read list of files 15417c478bd9Sstevel@tonic-gate */ 15427c478bd9Sstevel@tonic-gate if (read_list(root, flistp) != BAM_SUCCESS) { 15437c478bd9Sstevel@tonic-gate clear_walk_args(); 15447c478bd9Sstevel@tonic-gate return (BAM_ERROR); 15457c478bd9Sstevel@tonic-gate } 15467c478bd9Sstevel@tonic-gate 15477c478bd9Sstevel@tonic-gate assert(flistp->head && flistp->tail); 15487c478bd9Sstevel@tonic-gate 15497c478bd9Sstevel@tonic-gate /* 15507c478bd9Sstevel@tonic-gate * At this point either the update is required 15517c478bd9Sstevel@tonic-gate * or the decision is pending. In either case 15527c478bd9Sstevel@tonic-gate * we need to create new stat nvlist 15537c478bd9Sstevel@tonic-gate */ 15547c478bd9Sstevel@tonic-gate create_newstat(); 15557c478bd9Sstevel@tonic-gate 15567c478bd9Sstevel@tonic-gate /* 15577c478bd9Sstevel@tonic-gate * This walk does 2 things: 15587c478bd9Sstevel@tonic-gate * - gets new stat data for every file 15597c478bd9Sstevel@tonic-gate * - (optional) compare old and new stat data 15607c478bd9Sstevel@tonic-gate */ 15617c478bd9Sstevel@tonic-gate walk_list(root, &flist); 15627c478bd9Sstevel@tonic-gate 15637c478bd9Sstevel@tonic-gate /* done with the file list */ 15647c478bd9Sstevel@tonic-gate filelist_free(flistp); 15657c478bd9Sstevel@tonic-gate 15667c478bd9Sstevel@tonic-gate /* 15677c478bd9Sstevel@tonic-gate * if we didn't succeed in creating new stat data above 15687c478bd9Sstevel@tonic-gate * just return result of update check so that archive is built. 15697c478bd9Sstevel@tonic-gate */ 15707c478bd9Sstevel@tonic-gate if (walk_arg.new_nvlp == NULL) { 15717c478bd9Sstevel@tonic-gate bam_error(NO_NEW_STAT); 15727c478bd9Sstevel@tonic-gate need_update = walk_arg.need_update; 15737c478bd9Sstevel@tonic-gate clear_walk_args(); 15747c478bd9Sstevel@tonic-gate return (need_update ? 1 : 0); 15757c478bd9Sstevel@tonic-gate } 15767c478bd9Sstevel@tonic-gate 15777c478bd9Sstevel@tonic-gate 15787c478bd9Sstevel@tonic-gate /* 15797c478bd9Sstevel@tonic-gate * If no update required, discard newstat 15807c478bd9Sstevel@tonic-gate */ 15817c478bd9Sstevel@tonic-gate if (!walk_arg.need_update) { 15827c478bd9Sstevel@tonic-gate clear_walk_args(); 15837c478bd9Sstevel@tonic-gate return (0); 15847c478bd9Sstevel@tonic-gate } 15857c478bd9Sstevel@tonic-gate 15867c478bd9Sstevel@tonic-gate /* 15877c478bd9Sstevel@tonic-gate * At this point we need an update - so save new stat data 15887c478bd9Sstevel@tonic-gate * However, if only checking (-n), don't save new stat data. 15897c478bd9Sstevel@tonic-gate */ 15907c478bd9Sstevel@tonic-gate if (!bam_check) 15917c478bd9Sstevel@tonic-gate savenew(root); 15927c478bd9Sstevel@tonic-gate 15937c478bd9Sstevel@tonic-gate clear_walk_args(); 15947c478bd9Sstevel@tonic-gate 15957c478bd9Sstevel@tonic-gate return (1); 15967c478bd9Sstevel@tonic-gate } 15977c478bd9Sstevel@tonic-gate 15987c478bd9Sstevel@tonic-gate static error_t 15997c478bd9Sstevel@tonic-gate create_ramdisk(char *root) 16007c478bd9Sstevel@tonic-gate { 16017c478bd9Sstevel@tonic-gate char *cmdline, path[PATH_MAX]; 16027c478bd9Sstevel@tonic-gate size_t len; 16037c478bd9Sstevel@tonic-gate struct stat sb; 16047c478bd9Sstevel@tonic-gate 16057c478bd9Sstevel@tonic-gate /* 16067c478bd9Sstevel@tonic-gate * Setup command args for create_ramdisk.ksh 16077c478bd9Sstevel@tonic-gate */ 16087c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK); 16097c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) { 16107c478bd9Sstevel@tonic-gate bam_error(ARCH_EXEC_MISS, path, strerror(errno)); 16117c478bd9Sstevel@tonic-gate return (BAM_ERROR); 16127c478bd9Sstevel@tonic-gate } 16137c478bd9Sstevel@tonic-gate 16147c478bd9Sstevel@tonic-gate len = strlen(path) + strlen(root) + 10; /* room for space + -R */ 16157c478bd9Sstevel@tonic-gate cmdline = s_calloc(1, len); 16167c478bd9Sstevel@tonic-gate 16177c478bd9Sstevel@tonic-gate if (strlen(root) > 1) { 16187c478bd9Sstevel@tonic-gate (void) snprintf(cmdline, len, "%s -R %s", path, root); 16197c478bd9Sstevel@tonic-gate /* chop off / at the end */ 16207c478bd9Sstevel@tonic-gate cmdline[strlen(cmdline) - 1] = '\0'; 16217c478bd9Sstevel@tonic-gate } else 16227c478bd9Sstevel@tonic-gate (void) snprintf(cmdline, len, "%s", path); 16237c478bd9Sstevel@tonic-gate 16247c478bd9Sstevel@tonic-gate if (exec_cmd(cmdline, NULL, 0) != 0) { 16257c478bd9Sstevel@tonic-gate bam_error(ARCHIVE_FAIL, cmdline); 16267c478bd9Sstevel@tonic-gate free(cmdline); 16277c478bd9Sstevel@tonic-gate return (BAM_ERROR); 16287c478bd9Sstevel@tonic-gate } 16297c478bd9Sstevel@tonic-gate free(cmdline); 16307c478bd9Sstevel@tonic-gate 16317c478bd9Sstevel@tonic-gate /* 16327c478bd9Sstevel@tonic-gate * Verify that the archive has been created 16337c478bd9Sstevel@tonic-gate */ 16347c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE); 16357c478bd9Sstevel@tonic-gate if (stat(path, &sb) != 0) { 16367c478bd9Sstevel@tonic-gate bam_error(ARCHIVE_NOT_CREATED, path); 16377c478bd9Sstevel@tonic-gate return (BAM_ERROR); 16387c478bd9Sstevel@tonic-gate } 16397c478bd9Sstevel@tonic-gate 16407c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 16417c478bd9Sstevel@tonic-gate } 16427c478bd9Sstevel@tonic-gate 16437c478bd9Sstevel@tonic-gate /* 16447c478bd9Sstevel@tonic-gate * Checks if target filesystem is on a ramdisk 16457c478bd9Sstevel@tonic-gate * 1 - is miniroot 16467c478bd9Sstevel@tonic-gate * 0 - is not 16477c478bd9Sstevel@tonic-gate * When in doubt assume it is not a ramdisk. 16487c478bd9Sstevel@tonic-gate */ 16497c478bd9Sstevel@tonic-gate static int 16507c478bd9Sstevel@tonic-gate is_ramdisk(char *root) 16517c478bd9Sstevel@tonic-gate { 16527c478bd9Sstevel@tonic-gate struct extmnttab mnt; 16537c478bd9Sstevel@tonic-gate FILE *fp; 16547c478bd9Sstevel@tonic-gate int found; 1655*b610f78eSvikram char mntpt[PATH_MAX]; 1656*b610f78eSvikram char *cp; 16577c478bd9Sstevel@tonic-gate 16587c478bd9Sstevel@tonic-gate /* 16597c478bd9Sstevel@tonic-gate * There are 3 situations where creating archive is 16607c478bd9Sstevel@tonic-gate * of dubious value: 1661*b610f78eSvikram * - create boot_archive on a lofi-mounted boot_archive 16627c478bd9Sstevel@tonic-gate * - create it on a ramdisk which is the root filesystem 16637c478bd9Sstevel@tonic-gate * - create it on a ramdisk mounted somewhere else 16647c478bd9Sstevel@tonic-gate * The first is not easy to detect and checking for it is not 16657c478bd9Sstevel@tonic-gate * worth it. 16667c478bd9Sstevel@tonic-gate * The other two conditions are handled here 16677c478bd9Sstevel@tonic-gate */ 16687c478bd9Sstevel@tonic-gate 16697c478bd9Sstevel@tonic-gate fp = fopen(MNTTAB, "r"); 16707c478bd9Sstevel@tonic-gate if (fp == NULL) { 16717c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 16727c478bd9Sstevel@tonic-gate return (0); 16737c478bd9Sstevel@tonic-gate } 16747c478bd9Sstevel@tonic-gate 16757c478bd9Sstevel@tonic-gate resetmnttab(fp); 16767c478bd9Sstevel@tonic-gate 1677*b610f78eSvikram /* 1678*b610f78eSvikram * Remove any trailing / from the mount point 1679*b610f78eSvikram */ 1680*b610f78eSvikram (void) strlcpy(mntpt, root, sizeof (mntpt)); 1681*b610f78eSvikram if (strcmp(root, "/") != 0) { 1682*b610f78eSvikram cp = mntpt + strlen(mntpt) - 1; 1683*b610f78eSvikram if (*cp == '/') 1684*b610f78eSvikram *cp = '\0'; 1685*b610f78eSvikram } 16867c478bd9Sstevel@tonic-gate found = 0; 16877c478bd9Sstevel@tonic-gate while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 1688*b610f78eSvikram if (strcmp(mnt.mnt_mountp, mntpt) == 0) { 16897c478bd9Sstevel@tonic-gate found = 1; 16907c478bd9Sstevel@tonic-gate break; 16917c478bd9Sstevel@tonic-gate } 16927c478bd9Sstevel@tonic-gate } 16937c478bd9Sstevel@tonic-gate 16947c478bd9Sstevel@tonic-gate if (!found) { 16957c478bd9Sstevel@tonic-gate if (bam_verbose) 1696*b610f78eSvikram bam_error(NOT_IN_MNTTAB, mntpt); 16977c478bd9Sstevel@tonic-gate (void) fclose(fp); 16987c478bd9Sstevel@tonic-gate return (0); 16997c478bd9Sstevel@tonic-gate } 17007c478bd9Sstevel@tonic-gate 17017c478bd9Sstevel@tonic-gate if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) { 17027c478bd9Sstevel@tonic-gate if (bam_verbose) 17037c478bd9Sstevel@tonic-gate bam_error(IS_RAMDISK, bam_root); 17047c478bd9Sstevel@tonic-gate (void) fclose(fp); 17057c478bd9Sstevel@tonic-gate return (1); 17067c478bd9Sstevel@tonic-gate } 17077c478bd9Sstevel@tonic-gate 17087c478bd9Sstevel@tonic-gate (void) fclose(fp); 17097c478bd9Sstevel@tonic-gate 17107c478bd9Sstevel@tonic-gate return (0); 17117c478bd9Sstevel@tonic-gate } 17127c478bd9Sstevel@tonic-gate 17137c478bd9Sstevel@tonic-gate static int 17147c478bd9Sstevel@tonic-gate is_newboot(char *root) 17157c478bd9Sstevel@tonic-gate { 17167c478bd9Sstevel@tonic-gate char path[PATH_MAX]; 17177c478bd9Sstevel@tonic-gate struct stat sb; 17187c478bd9Sstevel@tonic-gate 17197c478bd9Sstevel@tonic-gate /* 17207c478bd9Sstevel@tonic-gate * We can't boot without MULTI_BOOT 17217c478bd9Sstevel@tonic-gate */ 17227c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT); 17237c478bd9Sstevel@tonic-gate if (stat(path, &sb) == -1) { 17247c478bd9Sstevel@tonic-gate if (bam_verbose) 17257c478bd9Sstevel@tonic-gate bam_print(FILE_MISS, path); 17267c478bd9Sstevel@tonic-gate return (0); 17277c478bd9Sstevel@tonic-gate } 17287c478bd9Sstevel@tonic-gate 17297c478bd9Sstevel@tonic-gate /* 17307c478bd9Sstevel@tonic-gate * We can't generate archive without GRUB_DIR 17317c478bd9Sstevel@tonic-gate */ 17327c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR); 17337c478bd9Sstevel@tonic-gate if (stat(path, &sb) == -1) { 17347c478bd9Sstevel@tonic-gate if (bam_verbose) 17357c478bd9Sstevel@tonic-gate bam_print(DIR_MISS, path); 17367c478bd9Sstevel@tonic-gate return (0); 17377c478bd9Sstevel@tonic-gate } 17387c478bd9Sstevel@tonic-gate 17397c478bd9Sstevel@tonic-gate return (1); 17407c478bd9Sstevel@tonic-gate } 17417c478bd9Sstevel@tonic-gate 17427c478bd9Sstevel@tonic-gate static int 17437c478bd9Sstevel@tonic-gate is_readonly(char *root) 17447c478bd9Sstevel@tonic-gate { 17457c478bd9Sstevel@tonic-gate struct statvfs vfs; 17467c478bd9Sstevel@tonic-gate 17477c478bd9Sstevel@tonic-gate /* 17487c478bd9Sstevel@tonic-gate * Check for RDONLY filesystem 17497c478bd9Sstevel@tonic-gate * When in doubt assume it is not readonly 17507c478bd9Sstevel@tonic-gate */ 17517c478bd9Sstevel@tonic-gate if (statvfs(root, &vfs) != 0) { 17527c478bd9Sstevel@tonic-gate if (bam_verbose) 17537c478bd9Sstevel@tonic-gate bam_error(STATVFS_FAIL, root, strerror(errno)); 17547c478bd9Sstevel@tonic-gate return (0); 17557c478bd9Sstevel@tonic-gate } 17567c478bd9Sstevel@tonic-gate 17577c478bd9Sstevel@tonic-gate if (vfs.f_flag & ST_RDONLY) { 17587c478bd9Sstevel@tonic-gate return (1); 17597c478bd9Sstevel@tonic-gate } 17607c478bd9Sstevel@tonic-gate 17617c478bd9Sstevel@tonic-gate return (0); 17627c478bd9Sstevel@tonic-gate } 17637c478bd9Sstevel@tonic-gate 17647c478bd9Sstevel@tonic-gate static error_t 17657c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt) 17667c478bd9Sstevel@tonic-gate { 17677c478bd9Sstevel@tonic-gate error_t ret; 17687c478bd9Sstevel@tonic-gate 17697c478bd9Sstevel@tonic-gate assert(root); 17707c478bd9Sstevel@tonic-gate assert(opt == NULL); 17717c478bd9Sstevel@tonic-gate 17727c478bd9Sstevel@tonic-gate /* 1773*b610f78eSvikram * root must belong to a GRUB boot OS, 17747c478bd9Sstevel@tonic-gate * don't care on sparc except for diskless clients 17757c478bd9Sstevel@tonic-gate */ 17767c478bd9Sstevel@tonic-gate if (!is_newboot(root)) { 1777*b610f78eSvikram /* 1778*b610f78eSvikram * Emit message only if not in context of update_all. 1779*b610f78eSvikram * If in update_all, emit only if verbose flag is set. 1780*b610f78eSvikram */ 1781*b610f78eSvikram if (!bam_update_all || bam_verbose) 1782*b610f78eSvikram bam_print(NOT_GRUB_BOOT, root); 17837c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 17847c478bd9Sstevel@tonic-gate } 17857c478bd9Sstevel@tonic-gate 17867c478bd9Sstevel@tonic-gate /* 17877c478bd9Sstevel@tonic-gate * root must be writable 17887c478bd9Sstevel@tonic-gate * Note: statvfs() does not always report the truth 17897c478bd9Sstevel@tonic-gate */ 17907c478bd9Sstevel@tonic-gate if (is_readonly(root)) { 17917c478bd9Sstevel@tonic-gate if (!bam_smf_check && bam_verbose) 17927c478bd9Sstevel@tonic-gate bam_print(RDONLY_FS, root); 17937c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 17947c478bd9Sstevel@tonic-gate } 17957c478bd9Sstevel@tonic-gate 17967c478bd9Sstevel@tonic-gate /* 17977c478bd9Sstevel@tonic-gate * Don't generate archive on ramdisk 17987c478bd9Sstevel@tonic-gate */ 17997c478bd9Sstevel@tonic-gate if (is_ramdisk(root)) { 18007c478bd9Sstevel@tonic-gate if (bam_verbose) 18017c478bd9Sstevel@tonic-gate bam_print(SKIP_RAMDISK); 18027c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 18037c478bd9Sstevel@tonic-gate } 18047c478bd9Sstevel@tonic-gate 18057c478bd9Sstevel@tonic-gate /* 18067c478bd9Sstevel@tonic-gate * Now check if updated is really needed 18077c478bd9Sstevel@tonic-gate */ 18087c478bd9Sstevel@tonic-gate ret = update_required(root); 18097c478bd9Sstevel@tonic-gate 18107c478bd9Sstevel@tonic-gate /* 18117c478bd9Sstevel@tonic-gate * The check command (-n) is *not* a dry run 18127c478bd9Sstevel@tonic-gate * It only checks if the archive is in sync. 18137c478bd9Sstevel@tonic-gate */ 18147c478bd9Sstevel@tonic-gate if (bam_check) { 18157c478bd9Sstevel@tonic-gate bam_exit((ret != 0) ? 1 : 0); 18167c478bd9Sstevel@tonic-gate } 18177c478bd9Sstevel@tonic-gate 18187c478bd9Sstevel@tonic-gate if (ret == 1) { 18197c478bd9Sstevel@tonic-gate /* create the ramdisk */ 18207c478bd9Sstevel@tonic-gate ret = create_ramdisk(root); 18217c478bd9Sstevel@tonic-gate } 18227c478bd9Sstevel@tonic-gate return (ret); 18237c478bd9Sstevel@tonic-gate } 18247c478bd9Sstevel@tonic-gate 1825*b610f78eSvikram static void 1826*b610f78eSvikram restore_grub_slice(void) 1827*b610f78eSvikram { 1828*b610f78eSvikram struct stat sb; 1829*b610f78eSvikram char *mntpt, *physlice; 1830*b610f78eSvikram int mnted; /* set if we did a mount */ 1831*b610f78eSvikram char menupath[PATH_MAX], cmd[PATH_MAX]; 1832*b610f78eSvikram 1833*b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 1834*b610f78eSvikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 1835*b610f78eSvikram return; 1836*b610f78eSvikram } 1837*b610f78eSvikram 1838*b610f78eSvikram /* 1839*b610f78eSvikram * If we are doing an luactivate, don't attempt to restore GRUB or else 1840*b610f78eSvikram * we may not be able to get to DCA boot environments. Let luactivate 1841*b610f78eSvikram * handle GRUB/DCA installation 1842*b610f78eSvikram */ 1843*b610f78eSvikram if (stat(LU_ACTIVATE_FILE, &sb) == 0) { 1844*b610f78eSvikram return; 1845*b610f78eSvikram } 1846*b610f78eSvikram 1847*b610f78eSvikram mnted = 0; 1848*b610f78eSvikram physlice = NULL; 1849*b610f78eSvikram mntpt = mount_grub_slice(&mnted, &physlice, QUIET); 1850*b610f78eSvikram if (mntpt == NULL) { 1851*b610f78eSvikram bam_error(CANNOT_RESTORE_GRUB_SLICE); 1852*b610f78eSvikram return; 1853*b610f78eSvikram } 1854*b610f78eSvikram 1855*b610f78eSvikram (void) snprintf(menupath, sizeof (menupath), "%s%s", mntpt, GRUB_MENU); 1856*b610f78eSvikram if (stat(menupath, &sb) == 0) { 1857*b610f78eSvikram umount_grub_slice(mnted, mntpt, physlice); 1858*b610f78eSvikram return; 1859*b610f78eSvikram } 1860*b610f78eSvikram 1861*b610f78eSvikram /* 1862*b610f78eSvikram * The menu is missing - we need to do a restore 1863*b610f78eSvikram */ 1864*b610f78eSvikram bam_print(RESTORING_GRUB); 1865*b610f78eSvikram 1866*b610f78eSvikram (void) snprintf(cmd, sizeof (cmd), "%s %s %s %s", 1867*b610f78eSvikram INSTALLGRUB, STAGE1, STAGE2, physlice); 1868*b610f78eSvikram 1869*b610f78eSvikram if (exec_cmd(cmd, NULL, 0) != 0) { 1870*b610f78eSvikram bam_error(RESTORE_GRUB_FAILED); 1871*b610f78eSvikram umount_grub_slice(mnted, mntpt, physlice); 1872*b610f78eSvikram return; 1873*b610f78eSvikram } 1874*b610f78eSvikram 1875*b610f78eSvikram if (stat(GRUB_backup_menu, &sb) != 0) { 1876*b610f78eSvikram bam_error(MISSING_BACKUP_MENU, 1877*b610f78eSvikram GRUB_backup_menu, strerror(errno)); 1878*b610f78eSvikram umount_grub_slice(mnted, mntpt, physlice); 1879*b610f78eSvikram return; 1880*b610f78eSvikram } 1881*b610f78eSvikram 1882*b610f78eSvikram (void) snprintf(cmd, sizeof (cmd), "/bin/cp %s %s", 1883*b610f78eSvikram GRUB_backup_menu, menupath); 1884*b610f78eSvikram 1885*b610f78eSvikram if (exec_cmd(cmd, NULL, 0) != 0) { 1886*b610f78eSvikram bam_error(RESTORE_MENU_FAILED, menupath); 1887*b610f78eSvikram umount_grub_slice(mnted, mntpt, physlice); 1888*b610f78eSvikram return; 1889*b610f78eSvikram } 1890*b610f78eSvikram 1891*b610f78eSvikram /* Success */ 1892*b610f78eSvikram umount_grub_slice(mnted, mntpt, physlice); 1893*b610f78eSvikram } 1894*b610f78eSvikram 18957c478bd9Sstevel@tonic-gate static error_t 18967c478bd9Sstevel@tonic-gate update_all(char *root, char *opt) 18977c478bd9Sstevel@tonic-gate { 18987c478bd9Sstevel@tonic-gate struct extmnttab mnt; 18997c478bd9Sstevel@tonic-gate struct stat sb; 19007c478bd9Sstevel@tonic-gate FILE *fp; 19017c478bd9Sstevel@tonic-gate char multibt[PATH_MAX]; 19027c478bd9Sstevel@tonic-gate error_t ret = BAM_SUCCESS; 19037c478bd9Sstevel@tonic-gate 19047c478bd9Sstevel@tonic-gate assert(bam_rootlen == 1 && root[0] == '/'); 19057c478bd9Sstevel@tonic-gate assert(opt == NULL); 19067c478bd9Sstevel@tonic-gate 19077c478bd9Sstevel@tonic-gate /* 19087c478bd9Sstevel@tonic-gate * First update archive for current root 19097c478bd9Sstevel@tonic-gate */ 19107c478bd9Sstevel@tonic-gate if (update_archive(root, opt) != BAM_SUCCESS) 19117c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 19127c478bd9Sstevel@tonic-gate 19137c478bd9Sstevel@tonic-gate /* 19147c478bd9Sstevel@tonic-gate * Now walk the mount table, performing archive update 19157c478bd9Sstevel@tonic-gate * for all mounted Newboot root filesystems 19167c478bd9Sstevel@tonic-gate */ 19177c478bd9Sstevel@tonic-gate fp = fopen(MNTTAB, "r"); 19187c478bd9Sstevel@tonic-gate if (fp == NULL) { 19197c478bd9Sstevel@tonic-gate bam_error(OPEN_FAIL, MNTTAB, strerror(errno)); 1920*b610f78eSvikram ret = BAM_ERROR; 1921*b610f78eSvikram goto out; 19227c478bd9Sstevel@tonic-gate } 19237c478bd9Sstevel@tonic-gate 19247c478bd9Sstevel@tonic-gate resetmnttab(fp); 19257c478bd9Sstevel@tonic-gate 19267c478bd9Sstevel@tonic-gate while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) { 19277c478bd9Sstevel@tonic-gate if (mnt.mnt_special == NULL) 19287c478bd9Sstevel@tonic-gate continue; 19297c478bd9Sstevel@tonic-gate if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0) 19307c478bd9Sstevel@tonic-gate continue; 19317c478bd9Sstevel@tonic-gate if (strcmp(mnt.mnt_mountp, "/") == 0) 19327c478bd9Sstevel@tonic-gate continue; 19337c478bd9Sstevel@tonic-gate 19347c478bd9Sstevel@tonic-gate (void) snprintf(multibt, sizeof (multibt), "%s%s", 19357c478bd9Sstevel@tonic-gate mnt.mnt_mountp, MULTI_BOOT); 19367c478bd9Sstevel@tonic-gate 19377c478bd9Sstevel@tonic-gate if (stat(multibt, &sb) == -1) 19387c478bd9Sstevel@tonic-gate continue; 19397c478bd9Sstevel@tonic-gate 19407c478bd9Sstevel@tonic-gate /* 19417c478bd9Sstevel@tonic-gate * We put a trailing slash to be consistent with root = "/" 19427c478bd9Sstevel@tonic-gate * case, such that we don't have to print // in some cases. 19437c478bd9Sstevel@tonic-gate */ 19447c478bd9Sstevel@tonic-gate (void) snprintf(rootbuf, sizeof (rootbuf), "%s/", 19457c478bd9Sstevel@tonic-gate mnt.mnt_mountp); 19467c478bd9Sstevel@tonic-gate bam_rootlen = strlen(rootbuf); 19477c478bd9Sstevel@tonic-gate if (update_archive(rootbuf, opt) != BAM_SUCCESS) 19487c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 19497c478bd9Sstevel@tonic-gate } 19507c478bd9Sstevel@tonic-gate 19517c478bd9Sstevel@tonic-gate (void) fclose(fp); 19527c478bd9Sstevel@tonic-gate 1953*b610f78eSvikram out: 1954*b610f78eSvikram if (stat(GRUB_slice, &sb) == 0) { 1955*b610f78eSvikram restore_grub_slice(); 1956*b610f78eSvikram } 1957*b610f78eSvikram 19587c478bd9Sstevel@tonic-gate return (ret); 19597c478bd9Sstevel@tonic-gate } 19607c478bd9Sstevel@tonic-gate 19617c478bd9Sstevel@tonic-gate static void 19627c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp) 19637c478bd9Sstevel@tonic-gate { 19647c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 19657c478bd9Sstevel@tonic-gate mp->start = lp; 19667c478bd9Sstevel@tonic-gate } else { 19677c478bd9Sstevel@tonic-gate mp->end->next = lp; 19687c478bd9Sstevel@tonic-gate } 19697c478bd9Sstevel@tonic-gate mp->end = lp; 19707c478bd9Sstevel@tonic-gate } 19717c478bd9Sstevel@tonic-gate 19727c478bd9Sstevel@tonic-gate /* 19737c478bd9Sstevel@tonic-gate * A line in menu.lst looks like 19747c478bd9Sstevel@tonic-gate * [ ]*<cmd>[ \t=]*<arg>* 19757c478bd9Sstevel@tonic-gate */ 19767c478bd9Sstevel@tonic-gate static void 19777c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum) 19787c478bd9Sstevel@tonic-gate { 19797c478bd9Sstevel@tonic-gate /* 19807c478bd9Sstevel@tonic-gate * save state across calls. This is so that 19817c478bd9Sstevel@tonic-gate * header gets the right entry# after title has 19827c478bd9Sstevel@tonic-gate * been processed 19837c478bd9Sstevel@tonic-gate */ 19847c478bd9Sstevel@tonic-gate static line_t *prev; 19857c478bd9Sstevel@tonic-gate 19867c478bd9Sstevel@tonic-gate line_t *lp; 19877c478bd9Sstevel@tonic-gate char *cmd, *sep, *arg; 19887c478bd9Sstevel@tonic-gate char save, *cp, *line; 19897c478bd9Sstevel@tonic-gate menu_flag_t flag = BAM_INVALID; 19907c478bd9Sstevel@tonic-gate 19917c478bd9Sstevel@tonic-gate if (str == NULL) { 19927c478bd9Sstevel@tonic-gate return; 19937c478bd9Sstevel@tonic-gate } 19947c478bd9Sstevel@tonic-gate 19957c478bd9Sstevel@tonic-gate /* 19967c478bd9Sstevel@tonic-gate * First save a copy of the entire line. 19977c478bd9Sstevel@tonic-gate * We use this later to set the line field. 19987c478bd9Sstevel@tonic-gate */ 19997c478bd9Sstevel@tonic-gate line = s_strdup(str); 20007c478bd9Sstevel@tonic-gate 20017c478bd9Sstevel@tonic-gate /* Eat up leading whitespace */ 20027c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 20037c478bd9Sstevel@tonic-gate str++; 20047c478bd9Sstevel@tonic-gate 20057c478bd9Sstevel@tonic-gate if (*str == '#') { /* comment */ 20067c478bd9Sstevel@tonic-gate cmd = s_strdup("#"); 20077c478bd9Sstevel@tonic-gate sep = NULL; 20087c478bd9Sstevel@tonic-gate arg = s_strdup(str + 1); 20097c478bd9Sstevel@tonic-gate flag = BAM_COMMENT; 20107c478bd9Sstevel@tonic-gate } else if (*str == '\0') { /* blank line */ 20117c478bd9Sstevel@tonic-gate cmd = sep = arg = NULL; 20127c478bd9Sstevel@tonic-gate flag = BAM_EMPTY; 20137c478bd9Sstevel@tonic-gate } else { 20147c478bd9Sstevel@tonic-gate /* 20157c478bd9Sstevel@tonic-gate * '=' is not a documented separator in grub syntax. 20167c478bd9Sstevel@tonic-gate * However various development bits use '=' as a 20177c478bd9Sstevel@tonic-gate * separator. In addition, external users also 20187c478bd9Sstevel@tonic-gate * use = as a separator. So we will allow that usage. 20197c478bd9Sstevel@tonic-gate */ 20207c478bd9Sstevel@tonic-gate cp = str; 20217c478bd9Sstevel@tonic-gate while (*str != ' ' && *str != '\t' && *str != '=') { 20227c478bd9Sstevel@tonic-gate if (*str == '\0') { 20237c478bd9Sstevel@tonic-gate cmd = s_strdup(cp); 20247c478bd9Sstevel@tonic-gate sep = arg = NULL; 20257c478bd9Sstevel@tonic-gate break; 20267c478bd9Sstevel@tonic-gate } 20277c478bd9Sstevel@tonic-gate str++; 20287c478bd9Sstevel@tonic-gate } 20297c478bd9Sstevel@tonic-gate 20307c478bd9Sstevel@tonic-gate if (*str != '\0') { 20317c478bd9Sstevel@tonic-gate save = *str; 20327c478bd9Sstevel@tonic-gate *str = '\0'; 20337c478bd9Sstevel@tonic-gate cmd = s_strdup(cp); 20347c478bd9Sstevel@tonic-gate *str = save; 20357c478bd9Sstevel@tonic-gate 20367c478bd9Sstevel@tonic-gate str++; 20377c478bd9Sstevel@tonic-gate save = *str; 20387c478bd9Sstevel@tonic-gate *str = '\0'; 20397c478bd9Sstevel@tonic-gate sep = s_strdup(str - 1); 20407c478bd9Sstevel@tonic-gate *str = save; 20417c478bd9Sstevel@tonic-gate 20427c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 20437c478bd9Sstevel@tonic-gate str++; 20447c478bd9Sstevel@tonic-gate if (*str == '\0') 20457c478bd9Sstevel@tonic-gate arg = NULL; 20467c478bd9Sstevel@tonic-gate else 20477c478bd9Sstevel@tonic-gate arg = s_strdup(str); 20487c478bd9Sstevel@tonic-gate } 20497c478bd9Sstevel@tonic-gate } 20507c478bd9Sstevel@tonic-gate 20517c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 20527c478bd9Sstevel@tonic-gate 20537c478bd9Sstevel@tonic-gate lp->cmd = cmd; 20547c478bd9Sstevel@tonic-gate lp->sep = sep; 20557c478bd9Sstevel@tonic-gate lp->arg = arg; 20567c478bd9Sstevel@tonic-gate lp->line = line; 20577c478bd9Sstevel@tonic-gate lp->lineNum = ++(*lineNum); 20587c478bd9Sstevel@tonic-gate if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) { 20597c478bd9Sstevel@tonic-gate lp->entryNum = ++(*entryNum); 20607c478bd9Sstevel@tonic-gate lp->flags = BAM_TITLE; 20617c478bd9Sstevel@tonic-gate if (prev && prev->flags == BAM_COMMENT && 20627c478bd9Sstevel@tonic-gate prev->arg && strcmp(prev->arg, BAM_HDR) == 0) 20637c478bd9Sstevel@tonic-gate prev->entryNum = lp->entryNum; 20647c478bd9Sstevel@tonic-gate } else if (flag != BAM_INVALID) { 20657c478bd9Sstevel@tonic-gate /* 20667c478bd9Sstevel@tonic-gate * For header comments, the entry# is "fixed up" 20677c478bd9Sstevel@tonic-gate * by the subsequent title 20687c478bd9Sstevel@tonic-gate */ 20697c478bd9Sstevel@tonic-gate lp->entryNum = *entryNum; 20707c478bd9Sstevel@tonic-gate lp->flags = flag; 20717c478bd9Sstevel@tonic-gate } else { 20727c478bd9Sstevel@tonic-gate lp->entryNum = *entryNum; 20737c478bd9Sstevel@tonic-gate lp->flags = (*entryNum == ENTRY_INIT) ? BAM_GLOBAL : BAM_ENTRY; 20747c478bd9Sstevel@tonic-gate } 20757c478bd9Sstevel@tonic-gate 20767c478bd9Sstevel@tonic-gate append_line(mp, lp); 20777c478bd9Sstevel@tonic-gate 20787c478bd9Sstevel@tonic-gate prev = lp; 20797c478bd9Sstevel@tonic-gate } 20807c478bd9Sstevel@tonic-gate 20817c478bd9Sstevel@tonic-gate static menu_t * 20827c478bd9Sstevel@tonic-gate menu_read(char *menu_path) 20837c478bd9Sstevel@tonic-gate { 20847c478bd9Sstevel@tonic-gate FILE *fp; 20857c478bd9Sstevel@tonic-gate char buf[BAM_MAXLINE], *cp; 20867c478bd9Sstevel@tonic-gate menu_t *mp; 20877c478bd9Sstevel@tonic-gate int line, entry, len, n; 20887c478bd9Sstevel@tonic-gate 20897c478bd9Sstevel@tonic-gate mp = s_calloc(1, sizeof (menu_t)); 20907c478bd9Sstevel@tonic-gate 20917c478bd9Sstevel@tonic-gate fp = fopen(menu_path, "r"); 20927c478bd9Sstevel@tonic-gate if (fp == NULL) { /* Let the caller handle this error */ 20937c478bd9Sstevel@tonic-gate return (mp); 20947c478bd9Sstevel@tonic-gate } 20957c478bd9Sstevel@tonic-gate 20967c478bd9Sstevel@tonic-gate 20977c478bd9Sstevel@tonic-gate /* Note: GRUB boot entry number starts with 0 */ 20987c478bd9Sstevel@tonic-gate line = LINE_INIT; 20997c478bd9Sstevel@tonic-gate entry = ENTRY_INIT; 21007c478bd9Sstevel@tonic-gate cp = buf; 21017c478bd9Sstevel@tonic-gate len = sizeof (buf); 21027c478bd9Sstevel@tonic-gate while (s_fgets(cp, len, fp) != NULL) { 21037c478bd9Sstevel@tonic-gate n = strlen(cp); 21047c478bd9Sstevel@tonic-gate if (cp[n - 1] == '\\') { 21057c478bd9Sstevel@tonic-gate len -= n - 1; 21067c478bd9Sstevel@tonic-gate assert(len >= 2); 21077c478bd9Sstevel@tonic-gate cp += n - 1; 21087c478bd9Sstevel@tonic-gate continue; 21097c478bd9Sstevel@tonic-gate } 21107c478bd9Sstevel@tonic-gate line_parser(mp, buf, &line, &entry); 21117c478bd9Sstevel@tonic-gate cp = buf; 21127c478bd9Sstevel@tonic-gate len = sizeof (buf); 21137c478bd9Sstevel@tonic-gate } 21147c478bd9Sstevel@tonic-gate 21157c478bd9Sstevel@tonic-gate if (fclose(fp) == EOF) { 21167c478bd9Sstevel@tonic-gate bam_error(CLOSE_FAIL, menu_path, strerror(errno)); 21177c478bd9Sstevel@tonic-gate } 21187c478bd9Sstevel@tonic-gate 21197c478bd9Sstevel@tonic-gate return (mp); 21207c478bd9Sstevel@tonic-gate } 21217c478bd9Sstevel@tonic-gate 21227c478bd9Sstevel@tonic-gate static error_t 21237c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title) 21247c478bd9Sstevel@tonic-gate { 21257c478bd9Sstevel@tonic-gate char *eq; 21267c478bd9Sstevel@tonic-gate char *opt_dup; 21277c478bd9Sstevel@tonic-gate int entryNum; 21287c478bd9Sstevel@tonic-gate 21297c478bd9Sstevel@tonic-gate assert(mp); 21307c478bd9Sstevel@tonic-gate assert(mp->start); 21317c478bd9Sstevel@tonic-gate assert(opt); 21327c478bd9Sstevel@tonic-gate 21337c478bd9Sstevel@tonic-gate opt_dup = s_strdup(opt); 21347c478bd9Sstevel@tonic-gate 21357c478bd9Sstevel@tonic-gate if (entry) 21367c478bd9Sstevel@tonic-gate *entry = ENTRY_INIT; 21377c478bd9Sstevel@tonic-gate if (title) 21387c478bd9Sstevel@tonic-gate *title = NULL; 21397c478bd9Sstevel@tonic-gate 21407c478bd9Sstevel@tonic-gate eq = strchr(opt_dup, '='); 21417c478bd9Sstevel@tonic-gate if (eq == NULL) { 21427c478bd9Sstevel@tonic-gate bam_error(INVALID_OPT, opt); 21437c478bd9Sstevel@tonic-gate free(opt_dup); 21447c478bd9Sstevel@tonic-gate return (BAM_ERROR); 21457c478bd9Sstevel@tonic-gate } 21467c478bd9Sstevel@tonic-gate 21477c478bd9Sstevel@tonic-gate *eq = '\0'; 21487c478bd9Sstevel@tonic-gate if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) { 21497c478bd9Sstevel@tonic-gate assert(mp->end); 21507c478bd9Sstevel@tonic-gate entryNum = s_strtol(eq + 1); 21517c478bd9Sstevel@tonic-gate if (entryNum < 0 || entryNum > mp->end->entryNum) { 21527c478bd9Sstevel@tonic-gate bam_error(INVALID_ENTRY, eq + 1); 21537c478bd9Sstevel@tonic-gate free(opt_dup); 21547c478bd9Sstevel@tonic-gate return (BAM_ERROR); 21557c478bd9Sstevel@tonic-gate } 21567c478bd9Sstevel@tonic-gate *entry = entryNum; 21577c478bd9Sstevel@tonic-gate } else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) { 21587c478bd9Sstevel@tonic-gate *title = opt + (eq - opt_dup) + 1; 21597c478bd9Sstevel@tonic-gate } else { 21607c478bd9Sstevel@tonic-gate bam_error(INVALID_OPT, opt); 21617c478bd9Sstevel@tonic-gate free(opt_dup); 21627c478bd9Sstevel@tonic-gate return (BAM_ERROR); 21637c478bd9Sstevel@tonic-gate } 21647c478bd9Sstevel@tonic-gate 21657c478bd9Sstevel@tonic-gate free(opt_dup); 21667c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 21677c478bd9Sstevel@tonic-gate } 21687c478bd9Sstevel@tonic-gate 21697c478bd9Sstevel@tonic-gate /* 21707c478bd9Sstevel@tonic-gate * If invoked with no titles/entries (opt == NULL) 21717c478bd9Sstevel@tonic-gate * only title lines in file are printed. 21727c478bd9Sstevel@tonic-gate * 21737c478bd9Sstevel@tonic-gate * If invoked with a title or entry #, all 21747c478bd9Sstevel@tonic-gate * lines in *every* matching entry are listed 21757c478bd9Sstevel@tonic-gate */ 21767c478bd9Sstevel@tonic-gate static error_t 21777c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt) 21787c478bd9Sstevel@tonic-gate { 21797c478bd9Sstevel@tonic-gate line_t *lp; 21807c478bd9Sstevel@tonic-gate int entry = ENTRY_INIT; 21817c478bd9Sstevel@tonic-gate int found; 21827c478bd9Sstevel@tonic-gate char *title = NULL; 21837c478bd9Sstevel@tonic-gate 21847c478bd9Sstevel@tonic-gate assert(mp); 21857c478bd9Sstevel@tonic-gate assert(menu_path); 21867c478bd9Sstevel@tonic-gate 21877c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 21887c478bd9Sstevel@tonic-gate bam_error(NO_MENU, menu_path); 21897c478bd9Sstevel@tonic-gate return (BAM_ERROR); 21907c478bd9Sstevel@tonic-gate } 21917c478bd9Sstevel@tonic-gate 21927c478bd9Sstevel@tonic-gate if (opt != NULL) { 21937c478bd9Sstevel@tonic-gate if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) { 21947c478bd9Sstevel@tonic-gate return (BAM_ERROR); 21957c478bd9Sstevel@tonic-gate } 21967c478bd9Sstevel@tonic-gate assert((entry != ENTRY_INIT) ^ (title != NULL)); 21977c478bd9Sstevel@tonic-gate } else { 21987c478bd9Sstevel@tonic-gate (void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0); 21997c478bd9Sstevel@tonic-gate (void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0); 22007c478bd9Sstevel@tonic-gate } 22017c478bd9Sstevel@tonic-gate 22027c478bd9Sstevel@tonic-gate found = 0; 22037c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 22047c478bd9Sstevel@tonic-gate if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY) 22057c478bd9Sstevel@tonic-gate continue; 22067c478bd9Sstevel@tonic-gate if (opt == NULL && lp->flags == BAM_TITLE) { 22077c478bd9Sstevel@tonic-gate bam_print(PRINT_TITLE, lp->entryNum, 22087c478bd9Sstevel@tonic-gate lp->arg); 22097c478bd9Sstevel@tonic-gate found = 1; 22107c478bd9Sstevel@tonic-gate continue; 22117c478bd9Sstevel@tonic-gate } 22127c478bd9Sstevel@tonic-gate if (entry != ENTRY_INIT && lp->entryNum == entry) { 22137c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 22147c478bd9Sstevel@tonic-gate found = 1; 22157c478bd9Sstevel@tonic-gate continue; 22167c478bd9Sstevel@tonic-gate } 22177c478bd9Sstevel@tonic-gate 22187c478bd9Sstevel@tonic-gate /* 22197c478bd9Sstevel@tonic-gate * We set the entry value here so that all lines 22207c478bd9Sstevel@tonic-gate * in entry get printed. If we subsequently match 22217c478bd9Sstevel@tonic-gate * title in other entries, all lines in those 22227c478bd9Sstevel@tonic-gate * entries get printed as well. 22237c478bd9Sstevel@tonic-gate */ 22247c478bd9Sstevel@tonic-gate if (title && lp->flags == BAM_TITLE && lp->arg && 22257c478bd9Sstevel@tonic-gate strncmp(title, lp->arg, strlen(title)) == 0) { 22267c478bd9Sstevel@tonic-gate bam_print(PRINT, lp->line); 22277c478bd9Sstevel@tonic-gate entry = lp->entryNum; 22287c478bd9Sstevel@tonic-gate found = 1; 22297c478bd9Sstevel@tonic-gate continue; 22307c478bd9Sstevel@tonic-gate } 22317c478bd9Sstevel@tonic-gate } 22327c478bd9Sstevel@tonic-gate 22337c478bd9Sstevel@tonic-gate if (!found) { 22347c478bd9Sstevel@tonic-gate bam_error(NO_MATCH_ENTRY); 22357c478bd9Sstevel@tonic-gate return (BAM_ERROR); 22367c478bd9Sstevel@tonic-gate } 22377c478bd9Sstevel@tonic-gate 22387c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 22397c478bd9Sstevel@tonic-gate } 22407c478bd9Sstevel@tonic-gate 22417c478bd9Sstevel@tonic-gate static int 22427c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp, 22437c478bd9Sstevel@tonic-gate char *title, 22447c478bd9Sstevel@tonic-gate char *root, 22457c478bd9Sstevel@tonic-gate char *kernel, 22467c478bd9Sstevel@tonic-gate char *module) 22477c478bd9Sstevel@tonic-gate { 22487c478bd9Sstevel@tonic-gate menu_t dummy; 22497c478bd9Sstevel@tonic-gate int lineNum, entryNum; 22507c478bd9Sstevel@tonic-gate char linebuf[BAM_MAXLINE]; 22517c478bd9Sstevel@tonic-gate 22527c478bd9Sstevel@tonic-gate assert(mp); 22537c478bd9Sstevel@tonic-gate 22547c478bd9Sstevel@tonic-gate if (title == NULL) { 22557c478bd9Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[TITLE_CMD]); 22567c478bd9Sstevel@tonic-gate return (BAM_ERROR); 22577c478bd9Sstevel@tonic-gate } 22587c478bd9Sstevel@tonic-gate if (root == NULL) { 22597c478bd9Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[ROOT_CMD]); 22607c478bd9Sstevel@tonic-gate return (BAM_ERROR); 22617c478bd9Sstevel@tonic-gate } 22627c478bd9Sstevel@tonic-gate if (kernel == NULL) { 22637c478bd9Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]); 22647c478bd9Sstevel@tonic-gate return (BAM_ERROR); 22657c478bd9Sstevel@tonic-gate } 22667c478bd9Sstevel@tonic-gate if (module == NULL) { 22677c478bd9Sstevel@tonic-gate bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]); 22687c478bd9Sstevel@tonic-gate return (BAM_ERROR); 22697c478bd9Sstevel@tonic-gate } 22707c478bd9Sstevel@tonic-gate 22717c478bd9Sstevel@tonic-gate if (mp->start) { 22727c478bd9Sstevel@tonic-gate lineNum = mp->end->lineNum; 22737c478bd9Sstevel@tonic-gate entryNum = mp->end->entryNum; 22747c478bd9Sstevel@tonic-gate } else { 22757c478bd9Sstevel@tonic-gate lineNum = LINE_INIT; 22767c478bd9Sstevel@tonic-gate entryNum = ENTRY_INIT; 22777c478bd9Sstevel@tonic-gate } 22787c478bd9Sstevel@tonic-gate 22797c478bd9Sstevel@tonic-gate /* 22807c478bd9Sstevel@tonic-gate * No separator for comment (HDR/FTR) commands 22817c478bd9Sstevel@tonic-gate * The syntax for comments is #<comment> 22827c478bd9Sstevel@tonic-gate */ 22837c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 22847c478bd9Sstevel@tonic-gate menu_cmds[COMMENT_CMD], BAM_HDR); 22857c478bd9Sstevel@tonic-gate dummy.start = dummy.end = NULL; 22867c478bd9Sstevel@tonic-gate line_parser(&dummy, linebuf, &lineNum, &entryNum); 22877c478bd9Sstevel@tonic-gate if (dummy.start == NULL || dummy.start->flags != BAM_COMMENT) { 22887c478bd9Sstevel@tonic-gate line_free(dummy.start); 22897c478bd9Sstevel@tonic-gate bam_error(INVALID_HDR, BAM_HDR); 22907c478bd9Sstevel@tonic-gate return (BAM_ERROR); 22917c478bd9Sstevel@tonic-gate } 22927c478bd9Sstevel@tonic-gate assert(dummy.start == dummy.end); 22937c478bd9Sstevel@tonic-gate append_line(mp, dummy.start); 22947c478bd9Sstevel@tonic-gate 22957c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 22967c478bd9Sstevel@tonic-gate menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title); 22977c478bd9Sstevel@tonic-gate dummy.start = dummy.end = NULL; 22987c478bd9Sstevel@tonic-gate line_parser(&dummy, linebuf, &lineNum, &entryNum); 22997c478bd9Sstevel@tonic-gate if (dummy.start == NULL || dummy.start->flags != BAM_TITLE) { 23007c478bd9Sstevel@tonic-gate line_free(dummy.start); 23017c478bd9Sstevel@tonic-gate bam_error(INVALID_TITLE, title); 23027c478bd9Sstevel@tonic-gate return (BAM_ERROR); 23037c478bd9Sstevel@tonic-gate } 23047c478bd9Sstevel@tonic-gate assert(dummy.start == dummy.end); 23057c478bd9Sstevel@tonic-gate append_line(mp, dummy.start); 23067c478bd9Sstevel@tonic-gate 23077c478bd9Sstevel@tonic-gate 23087c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 23097c478bd9Sstevel@tonic-gate menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root); 23107c478bd9Sstevel@tonic-gate dummy.start = dummy.end = NULL; 23117c478bd9Sstevel@tonic-gate line_parser(&dummy, linebuf, &lineNum, &entryNum); 23127c478bd9Sstevel@tonic-gate if (dummy.start == NULL || dummy.start->flags != BAM_ENTRY) { 23137c478bd9Sstevel@tonic-gate line_free(dummy.start); 23147c478bd9Sstevel@tonic-gate bam_error(INVALID_ROOT, root); 23157c478bd9Sstevel@tonic-gate return (BAM_ERROR); 23167c478bd9Sstevel@tonic-gate } 23177c478bd9Sstevel@tonic-gate assert(dummy.start == dummy.end); 23187c478bd9Sstevel@tonic-gate append_line(mp, dummy.start); 23197c478bd9Sstevel@tonic-gate 23207c478bd9Sstevel@tonic-gate 23217c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 23227c478bd9Sstevel@tonic-gate menu_cmds[KERNEL_CMD], menu_cmds[SEP_CMD], kernel); 23237c478bd9Sstevel@tonic-gate dummy.start = dummy.end = NULL; 23247c478bd9Sstevel@tonic-gate line_parser(&dummy, linebuf, &lineNum, &entryNum); 23257c478bd9Sstevel@tonic-gate if (dummy.start == NULL || dummy.start->flags != BAM_ENTRY) { 23267c478bd9Sstevel@tonic-gate line_free(dummy.start); 23277c478bd9Sstevel@tonic-gate bam_error(INVALID_KERNEL, kernel); 23287c478bd9Sstevel@tonic-gate return (BAM_ERROR); 23297c478bd9Sstevel@tonic-gate } 23307c478bd9Sstevel@tonic-gate assert(dummy.start == dummy.end); 23317c478bd9Sstevel@tonic-gate append_line(mp, dummy.start); 23327c478bd9Sstevel@tonic-gate 23337c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s%s", 23347c478bd9Sstevel@tonic-gate menu_cmds[MODULE_CMD], menu_cmds[SEP_CMD], module); 23357c478bd9Sstevel@tonic-gate dummy.start = dummy.end = NULL; 23367c478bd9Sstevel@tonic-gate line_parser(&dummy, linebuf, &lineNum, &entryNum); 23377c478bd9Sstevel@tonic-gate if (dummy.start == NULL || dummy.start->flags != BAM_ENTRY) { 23387c478bd9Sstevel@tonic-gate line_free(dummy.start); 23397c478bd9Sstevel@tonic-gate bam_error(INVALID_MODULE, module); 23407c478bd9Sstevel@tonic-gate return (BAM_ERROR); 23417c478bd9Sstevel@tonic-gate } 23427c478bd9Sstevel@tonic-gate assert(dummy.start == dummy.end); 23437c478bd9Sstevel@tonic-gate append_line(mp, dummy.start); 23447c478bd9Sstevel@tonic-gate 23457c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s%s", 23467c478bd9Sstevel@tonic-gate menu_cmds[COMMENT_CMD], BAM_FTR); 23477c478bd9Sstevel@tonic-gate dummy.start = dummy.end = NULL; 23487c478bd9Sstevel@tonic-gate line_parser(&dummy, linebuf, &lineNum, &entryNum); 23497c478bd9Sstevel@tonic-gate if (dummy.start == NULL || dummy.start->flags != BAM_COMMENT) { 23507c478bd9Sstevel@tonic-gate line_free(dummy.start); 23517c478bd9Sstevel@tonic-gate bam_error(INVALID_FOOTER, BAM_FTR); 23527c478bd9Sstevel@tonic-gate return (BAM_ERROR); 23537c478bd9Sstevel@tonic-gate } 23547c478bd9Sstevel@tonic-gate assert(dummy.start == dummy.end); 23557c478bd9Sstevel@tonic-gate append_line(mp, dummy.start); 23567c478bd9Sstevel@tonic-gate 23577c478bd9Sstevel@tonic-gate return (entryNum); 23587c478bd9Sstevel@tonic-gate } 23597c478bd9Sstevel@tonic-gate 23607c478bd9Sstevel@tonic-gate static error_t 23617c478bd9Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum) 23627c478bd9Sstevel@tonic-gate { 23637c478bd9Sstevel@tonic-gate int bootadm_entry = 0; 23647c478bd9Sstevel@tonic-gate line_t *lp, *prev, *save; 23657c478bd9Sstevel@tonic-gate int deleted; 23667c478bd9Sstevel@tonic-gate 23677c478bd9Sstevel@tonic-gate assert(entryNum != ENTRY_INIT); 23687c478bd9Sstevel@tonic-gate 23697c478bd9Sstevel@tonic-gate deleted = 0; 23707c478bd9Sstevel@tonic-gate prev = NULL; 23717c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; ) { 23727c478bd9Sstevel@tonic-gate 23737c478bd9Sstevel@tonic-gate if (lp->entryNum == ENTRY_INIT) { 23747c478bd9Sstevel@tonic-gate prev = lp; 23757c478bd9Sstevel@tonic-gate lp = lp->next; 23767c478bd9Sstevel@tonic-gate continue; 23777c478bd9Sstevel@tonic-gate } 23787c478bd9Sstevel@tonic-gate 23797c478bd9Sstevel@tonic-gate if (entryNum != ALL_ENTRIES && lp->entryNum != entryNum) { 23807c478bd9Sstevel@tonic-gate prev = lp; 23817c478bd9Sstevel@tonic-gate lp = lp->next; 23827c478bd9Sstevel@tonic-gate continue; 23837c478bd9Sstevel@tonic-gate } 23847c478bd9Sstevel@tonic-gate 23857c478bd9Sstevel@tonic-gate /* 23867c478bd9Sstevel@tonic-gate * can only delete bootadm entries 23877c478bd9Sstevel@tonic-gate */ 23887c478bd9Sstevel@tonic-gate if (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_HDR) == 0) { 23897c478bd9Sstevel@tonic-gate bootadm_entry = 1; 23907c478bd9Sstevel@tonic-gate } 23917c478bd9Sstevel@tonic-gate 23927c478bd9Sstevel@tonic-gate if (!bootadm_entry) { 23937c478bd9Sstevel@tonic-gate prev = lp; 23947c478bd9Sstevel@tonic-gate lp = lp->next; 23957c478bd9Sstevel@tonic-gate continue; 23967c478bd9Sstevel@tonic-gate } 23977c478bd9Sstevel@tonic-gate 23987c478bd9Sstevel@tonic-gate if (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_FTR) == 0) 23997c478bd9Sstevel@tonic-gate bootadm_entry = 0; 24007c478bd9Sstevel@tonic-gate 24017c478bd9Sstevel@tonic-gate if (prev == NULL) 24027c478bd9Sstevel@tonic-gate mp->start = lp->next; 24037c478bd9Sstevel@tonic-gate else 24047c478bd9Sstevel@tonic-gate prev->next = lp->next; 24057c478bd9Sstevel@tonic-gate if (mp->end == lp) 24067c478bd9Sstevel@tonic-gate mp->end = prev; 24077c478bd9Sstevel@tonic-gate save = lp->next; 24087c478bd9Sstevel@tonic-gate line_free(lp); 24097c478bd9Sstevel@tonic-gate lp = save; /* prev stays the same */ 24107c478bd9Sstevel@tonic-gate 24117c478bd9Sstevel@tonic-gate deleted = 1; 24127c478bd9Sstevel@tonic-gate } 24137c478bd9Sstevel@tonic-gate 24147c478bd9Sstevel@tonic-gate if (!deleted && entryNum != ALL_ENTRIES) { 24157c478bd9Sstevel@tonic-gate bam_error(NO_BOOTADM_MATCH); 24167c478bd9Sstevel@tonic-gate return (BAM_ERROR); 24177c478bd9Sstevel@tonic-gate } 24187c478bd9Sstevel@tonic-gate 24197c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 24207c478bd9Sstevel@tonic-gate } 24217c478bd9Sstevel@tonic-gate 24227c478bd9Sstevel@tonic-gate static error_t 24237c478bd9Sstevel@tonic-gate delete_entry(menu_t *mp, char *menu_path, char *opt) 24247c478bd9Sstevel@tonic-gate { 24257c478bd9Sstevel@tonic-gate int entry = ENTRY_INIT; 24267c478bd9Sstevel@tonic-gate char *title = NULL; 24277c478bd9Sstevel@tonic-gate line_t *lp; 24287c478bd9Sstevel@tonic-gate 24297c478bd9Sstevel@tonic-gate assert(mp); 24307c478bd9Sstevel@tonic-gate assert(opt); 24317c478bd9Sstevel@tonic-gate 24327c478bd9Sstevel@tonic-gate /* 24337c478bd9Sstevel@tonic-gate * Do a quick check. If the file is empty 24347c478bd9Sstevel@tonic-gate * we have nothing to delete 24357c478bd9Sstevel@tonic-gate */ 24367c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 24377c478bd9Sstevel@tonic-gate bam_print(EMPTY_FILE, menu_path); 24387c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 24397c478bd9Sstevel@tonic-gate } 24407c478bd9Sstevel@tonic-gate 24417c478bd9Sstevel@tonic-gate if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) { 24427c478bd9Sstevel@tonic-gate return (BAM_ERROR); 24437c478bd9Sstevel@tonic-gate } 24447c478bd9Sstevel@tonic-gate assert((entry != ENTRY_INIT) ^ (title != NULL)); 24457c478bd9Sstevel@tonic-gate 24467c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 24477c478bd9Sstevel@tonic-gate if (entry != ENTRY_INIT) 24487c478bd9Sstevel@tonic-gate break; 24497c478bd9Sstevel@tonic-gate assert(title); 24507c478bd9Sstevel@tonic-gate if (lp->flags == BAM_TITLE && 24517c478bd9Sstevel@tonic-gate lp->arg && strcmp(lp->arg, title) == 0) { 24527c478bd9Sstevel@tonic-gate entry = lp->entryNum; 24537c478bd9Sstevel@tonic-gate break; 24547c478bd9Sstevel@tonic-gate } 24557c478bd9Sstevel@tonic-gate } 24567c478bd9Sstevel@tonic-gate 24577c478bd9Sstevel@tonic-gate if (entry == ENTRY_INIT) { 24587c478bd9Sstevel@tonic-gate bam_error(NO_MATCH, title); 24597c478bd9Sstevel@tonic-gate return (BAM_ERROR); 24607c478bd9Sstevel@tonic-gate } 24617c478bd9Sstevel@tonic-gate 24627c478bd9Sstevel@tonic-gate if (do_delete(mp, entry) != BAM_SUCCESS) { 24637c478bd9Sstevel@tonic-gate return (BAM_ERROR); 24647c478bd9Sstevel@tonic-gate } 24657c478bd9Sstevel@tonic-gate 24667c478bd9Sstevel@tonic-gate return (BAM_WRITE); 24677c478bd9Sstevel@tonic-gate } 24687c478bd9Sstevel@tonic-gate 24697c478bd9Sstevel@tonic-gate static error_t 24707c478bd9Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt) 24717c478bd9Sstevel@tonic-gate { 24727c478bd9Sstevel@tonic-gate assert(mp); 24737c478bd9Sstevel@tonic-gate assert(opt == NULL); 24747c478bd9Sstevel@tonic-gate 24757c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 24767c478bd9Sstevel@tonic-gate bam_print(EMPTY_FILE, menu_path); 24777c478bd9Sstevel@tonic-gate return (BAM_SUCCESS); 24787c478bd9Sstevel@tonic-gate } 24797c478bd9Sstevel@tonic-gate 24807c478bd9Sstevel@tonic-gate if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) { 24817c478bd9Sstevel@tonic-gate return (BAM_ERROR); 24827c478bd9Sstevel@tonic-gate } 24837c478bd9Sstevel@tonic-gate 24847c478bd9Sstevel@tonic-gate return (BAM_WRITE); 24857c478bd9Sstevel@tonic-gate } 24867c478bd9Sstevel@tonic-gate 24877c478bd9Sstevel@tonic-gate static FILE * 24887c478bd9Sstevel@tonic-gate open_diskmap(void) 24897c478bd9Sstevel@tonic-gate { 24907c478bd9Sstevel@tonic-gate FILE *fp; 24917c478bd9Sstevel@tonic-gate char cmd[PATH_MAX]; 24927c478bd9Sstevel@tonic-gate 24937c478bd9Sstevel@tonic-gate /* make sure we have a map file */ 24947c478bd9Sstevel@tonic-gate fp = fopen(GRUBDISK_MAP, "r"); 24957c478bd9Sstevel@tonic-gate if (fp == NULL) { 24967c478bd9Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), 24977c478bd9Sstevel@tonic-gate "%s > /dev/null", CREATE_DISKMAP); 24987c478bd9Sstevel@tonic-gate (void) system(cmd); 24997c478bd9Sstevel@tonic-gate fp = fopen(GRUBDISK_MAP, "r"); 25007c478bd9Sstevel@tonic-gate } 25017c478bd9Sstevel@tonic-gate return (fp); 25027c478bd9Sstevel@tonic-gate } 25037c478bd9Sstevel@tonic-gate 25047c478bd9Sstevel@tonic-gate #define SECTOR_SIZE 512 25057c478bd9Sstevel@tonic-gate 25067c478bd9Sstevel@tonic-gate static int 25077c478bd9Sstevel@tonic-gate get_partition(char *device) 25087c478bd9Sstevel@tonic-gate { 25097c478bd9Sstevel@tonic-gate int i, fd, is_pcfs, partno = -1; 25107c478bd9Sstevel@tonic-gate struct mboot *mboot; 25117c478bd9Sstevel@tonic-gate char boot_sect[SECTOR_SIZE]; 25127c478bd9Sstevel@tonic-gate char *wholedisk, *slice; 25137c478bd9Sstevel@tonic-gate 25147c478bd9Sstevel@tonic-gate /* form whole disk (p0) */ 25157c478bd9Sstevel@tonic-gate slice = device + strlen(device) - 2; 25167c478bd9Sstevel@tonic-gate is_pcfs = (*slice != 's'); 25177c478bd9Sstevel@tonic-gate if (!is_pcfs) 25187c478bd9Sstevel@tonic-gate *slice = '\0'; 25197c478bd9Sstevel@tonic-gate wholedisk = s_calloc(1, strlen(device) + 3); 25207c478bd9Sstevel@tonic-gate (void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device); 25217c478bd9Sstevel@tonic-gate if (!is_pcfs) 25227c478bd9Sstevel@tonic-gate *slice = 's'; 25237c478bd9Sstevel@tonic-gate 25247c478bd9Sstevel@tonic-gate /* read boot sector */ 25257c478bd9Sstevel@tonic-gate fd = open(wholedisk, O_RDONLY); 25267c478bd9Sstevel@tonic-gate free(wholedisk); 25277c478bd9Sstevel@tonic-gate if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) { 25287c478bd9Sstevel@tonic-gate return (partno); 25297c478bd9Sstevel@tonic-gate } 25307c478bd9Sstevel@tonic-gate (void) close(fd); 25317c478bd9Sstevel@tonic-gate 25327c478bd9Sstevel@tonic-gate /* parse fdisk table */ 25337c478bd9Sstevel@tonic-gate mboot = (struct mboot *)((void *)boot_sect); 25347c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 25357c478bd9Sstevel@tonic-gate struct ipart *part = 25367c478bd9Sstevel@tonic-gate (struct ipart *)(uintptr_t)mboot->parts + i; 25377c478bd9Sstevel@tonic-gate if (is_pcfs) { /* looking for solaris boot part */ 25387c478bd9Sstevel@tonic-gate if (part->systid == 0xbe) { 25397c478bd9Sstevel@tonic-gate partno = i; 25407c478bd9Sstevel@tonic-gate break; 25417c478bd9Sstevel@tonic-gate } 25427c478bd9Sstevel@tonic-gate } else { /* look for solaris partition, old and new */ 25437c478bd9Sstevel@tonic-gate if (part->systid == SUNIXOS || 25447c478bd9Sstevel@tonic-gate part->systid == SUNIXOS2) { 25457c478bd9Sstevel@tonic-gate partno = i; 25467c478bd9Sstevel@tonic-gate break; 25477c478bd9Sstevel@tonic-gate } 25487c478bd9Sstevel@tonic-gate } 25497c478bd9Sstevel@tonic-gate } 25507c478bd9Sstevel@tonic-gate return (partno); 25517c478bd9Sstevel@tonic-gate } 25527c478bd9Sstevel@tonic-gate 25537c478bd9Sstevel@tonic-gate static char * 25547c478bd9Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev) 25557c478bd9Sstevel@tonic-gate { 25567c478bd9Sstevel@tonic-gate char *grubdisk; /* (hd#,#,#) */ 25577c478bd9Sstevel@tonic-gate char *slice; 25587c478bd9Sstevel@tonic-gate char *grubhd; 25597c478bd9Sstevel@tonic-gate int fdiskpart; 25607c478bd9Sstevel@tonic-gate int found = 0; 25617c478bd9Sstevel@tonic-gate char *devname, *ctdname = strstr(rootdev, "dsk/"); 25627c478bd9Sstevel@tonic-gate char linebuf[PATH_MAX]; 25637c478bd9Sstevel@tonic-gate 25647c478bd9Sstevel@tonic-gate if (ctdname == NULL) 25657c478bd9Sstevel@tonic-gate return (NULL); 25667c478bd9Sstevel@tonic-gate 25677c478bd9Sstevel@tonic-gate ctdname += strlen("dsk/"); 25687c478bd9Sstevel@tonic-gate slice = strrchr(ctdname, 's'); 25697c478bd9Sstevel@tonic-gate if (slice) 25707c478bd9Sstevel@tonic-gate *slice = '\0'; 25717c478bd9Sstevel@tonic-gate 25727c478bd9Sstevel@tonic-gate rewind(fp); 25737c478bd9Sstevel@tonic-gate while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) { 25747c478bd9Sstevel@tonic-gate grubhd = strtok(linebuf, " \t\n"); 25757c478bd9Sstevel@tonic-gate if (grubhd) 25767c478bd9Sstevel@tonic-gate devname = strtok(NULL, " \t\n"); 25777c478bd9Sstevel@tonic-gate else 25787c478bd9Sstevel@tonic-gate devname = NULL; 25797c478bd9Sstevel@tonic-gate if (devname && strcmp(devname, ctdname) == 0) { 25807c478bd9Sstevel@tonic-gate found = 1; 25817c478bd9Sstevel@tonic-gate break; 25827c478bd9Sstevel@tonic-gate } 25837c478bd9Sstevel@tonic-gate } 25847c478bd9Sstevel@tonic-gate 25857c478bd9Sstevel@tonic-gate if (slice) 25867c478bd9Sstevel@tonic-gate *slice = 's'; 25877c478bd9Sstevel@tonic-gate 25887c478bd9Sstevel@tonic-gate if (found == 0) { 25897c478bd9Sstevel@tonic-gate if (bam_verbose) 25907c478bd9Sstevel@tonic-gate bam_print(DISKMAP_FAIL_NONFATAL, rootdev); 25917c478bd9Sstevel@tonic-gate grubhd = "0"; /* assume disk 0 if can't match */ 25927c478bd9Sstevel@tonic-gate } 25937c478bd9Sstevel@tonic-gate 25947c478bd9Sstevel@tonic-gate fdiskpart = get_partition(rootdev); 25957c478bd9Sstevel@tonic-gate if (fdiskpart == -1) 25967c478bd9Sstevel@tonic-gate return (NULL); 25977c478bd9Sstevel@tonic-gate 25987c478bd9Sstevel@tonic-gate grubdisk = s_calloc(1, 10); 25997c478bd9Sstevel@tonic-gate if (slice) { 26007c478bd9Sstevel@tonic-gate (void) snprintf(grubdisk, 10, "(hd%s,%d,%c)", 26017c478bd9Sstevel@tonic-gate grubhd, fdiskpart, slice[1] + 'a' - '0'); 26027c478bd9Sstevel@tonic-gate } else 26037c478bd9Sstevel@tonic-gate (void) snprintf(grubdisk, 10, "(hd%s,%d)", 26047c478bd9Sstevel@tonic-gate grubhd, fdiskpart); 26057c478bd9Sstevel@tonic-gate 26067c478bd9Sstevel@tonic-gate /* if root not on bootdev, change GRUB disk to 0 */ 26077c478bd9Sstevel@tonic-gate if (!on_bootdev) 26087c478bd9Sstevel@tonic-gate grubdisk[3] = '0'; 26097c478bd9Sstevel@tonic-gate return (grubdisk); 26107c478bd9Sstevel@tonic-gate } 26117c478bd9Sstevel@tonic-gate 26127c478bd9Sstevel@tonic-gate static char *get_title(char *rootdir) 26137c478bd9Sstevel@tonic-gate { 26147c478bd9Sstevel@tonic-gate static char title[80]; /* from /etc/release */ 26157c478bd9Sstevel@tonic-gate char *cp, release[PATH_MAX]; 26167c478bd9Sstevel@tonic-gate FILE *fp; 26177c478bd9Sstevel@tonic-gate 26187c478bd9Sstevel@tonic-gate /* open the /etc/release file */ 26197c478bd9Sstevel@tonic-gate (void) snprintf(release, sizeof (release), "%s/etc/release", rootdir); 26207c478bd9Sstevel@tonic-gate 26217c478bd9Sstevel@tonic-gate fp = fopen(release, "r"); 26227c478bd9Sstevel@tonic-gate if (fp == NULL) 26237c478bd9Sstevel@tonic-gate return ("Solaris"); /* default to Solaris */ 26247c478bd9Sstevel@tonic-gate 26257c478bd9Sstevel@tonic-gate while (s_fgets(title, sizeof (title), fp) != NULL) { 26267c478bd9Sstevel@tonic-gate cp = strstr(title, "Solaris"); 26277c478bd9Sstevel@tonic-gate if (cp) 26287c478bd9Sstevel@tonic-gate break; 26297c478bd9Sstevel@tonic-gate } 26307c478bd9Sstevel@tonic-gate (void) fclose(fp); 26317c478bd9Sstevel@tonic-gate return (cp); 26327c478bd9Sstevel@tonic-gate } 26337c478bd9Sstevel@tonic-gate 26347c478bd9Sstevel@tonic-gate static char * 26357c478bd9Sstevel@tonic-gate get_special(char *mountp) 26367c478bd9Sstevel@tonic-gate { 26377c478bd9Sstevel@tonic-gate FILE *mntfp; 26387c478bd9Sstevel@tonic-gate struct mnttab mp = {0}, mpref = {0}; 26397c478bd9Sstevel@tonic-gate 26407c478bd9Sstevel@tonic-gate mntfp = fopen(MNTTAB, "r"); 26417c478bd9Sstevel@tonic-gate if (mntfp == NULL) { 26427c478bd9Sstevel@tonic-gate return (0); 26437c478bd9Sstevel@tonic-gate } 26447c478bd9Sstevel@tonic-gate 26457c478bd9Sstevel@tonic-gate if (*mountp == '\0') 26467c478bd9Sstevel@tonic-gate mpref.mnt_mountp = "/"; 26477c478bd9Sstevel@tonic-gate else 26487c478bd9Sstevel@tonic-gate mpref.mnt_mountp = mountp; 26497c478bd9Sstevel@tonic-gate if (getmntany(mntfp, &mp, &mpref) != 0) { 26507c478bd9Sstevel@tonic-gate (void) fclose(mntfp); 26517c478bd9Sstevel@tonic-gate return (NULL); 26527c478bd9Sstevel@tonic-gate } 26537c478bd9Sstevel@tonic-gate (void) fclose(mntfp); 26547c478bd9Sstevel@tonic-gate 26557c478bd9Sstevel@tonic-gate return (s_strdup(mp.mnt_special)); 26567c478bd9Sstevel@tonic-gate } 26577c478bd9Sstevel@tonic-gate 26587c478bd9Sstevel@tonic-gate static char * 26597c478bd9Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev) 26607c478bd9Sstevel@tonic-gate { 26617c478bd9Sstevel@tonic-gate FILE *fp; 26627c478bd9Sstevel@tonic-gate char *grubdisk; 26637c478bd9Sstevel@tonic-gate 26647c478bd9Sstevel@tonic-gate /* translate /dev/dsk name to grub disk name */ 26657c478bd9Sstevel@tonic-gate fp = open_diskmap(); 26667c478bd9Sstevel@tonic-gate if (fp == NULL) { 26677c478bd9Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdisk); 26687c478bd9Sstevel@tonic-gate return (NULL); 26697c478bd9Sstevel@tonic-gate } 26707c478bd9Sstevel@tonic-gate grubdisk = get_grubdisk(osdisk, fp, on_bootdev); 26717c478bd9Sstevel@tonic-gate (void) fclose(fp); 26727c478bd9Sstevel@tonic-gate return (grubdisk); 26737c478bd9Sstevel@tonic-gate } 26747c478bd9Sstevel@tonic-gate 26757c478bd9Sstevel@tonic-gate /* 26767c478bd9Sstevel@tonic-gate * Check if root is on the boot device 26777c478bd9Sstevel@tonic-gate * Return 0 (false) on error 26787c478bd9Sstevel@tonic-gate */ 26797c478bd9Sstevel@tonic-gate static int 26807c478bd9Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp) 26817c478bd9Sstevel@tonic-gate { 26827c478bd9Sstevel@tonic-gate int ret; 26837c478bd9Sstevel@tonic-gate char *grubhd, *bootp, *special; 26847c478bd9Sstevel@tonic-gate 26857c478bd9Sstevel@tonic-gate special = get_special(menu_root); 26867c478bd9Sstevel@tonic-gate if (special == NULL) 26877c478bd9Sstevel@tonic-gate return (0); 26887c478bd9Sstevel@tonic-gate bootp = strstr(special, "p0:boot"); 26897c478bd9Sstevel@tonic-gate if (bootp) 26907c478bd9Sstevel@tonic-gate *bootp = '\0'; 26917c478bd9Sstevel@tonic-gate grubhd = get_grubdisk(special, fp, 1); 26927c478bd9Sstevel@tonic-gate free(special); 26937c478bd9Sstevel@tonic-gate 26947c478bd9Sstevel@tonic-gate if (grubhd == NULL) 26957c478bd9Sstevel@tonic-gate return (0); 26967c478bd9Sstevel@tonic-gate ret = grubhd[3] == '0'; 26977c478bd9Sstevel@tonic-gate free(grubhd); 26987c478bd9Sstevel@tonic-gate return (ret); 26997c478bd9Sstevel@tonic-gate } 27007c478bd9Sstevel@tonic-gate 27017c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 27027c478bd9Sstevel@tonic-gate static error_t 27037c478bd9Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt) 27047c478bd9Sstevel@tonic-gate { 27057c478bd9Sstevel@tonic-gate FILE *fp; 27067c478bd9Sstevel@tonic-gate int entry; 27077c478bd9Sstevel@tonic-gate line_t *lp; 27087c478bd9Sstevel@tonic-gate char *grubdisk, *title, *osdev, *osroot; 27097c478bd9Sstevel@tonic-gate int bootadm_entry, entry_to_delete; 27107c478bd9Sstevel@tonic-gate 27117c478bd9Sstevel@tonic-gate assert(mp); 27127c478bd9Sstevel@tonic-gate assert(opt); 27137c478bd9Sstevel@tonic-gate 27147c478bd9Sstevel@tonic-gate osdev = strtok(opt, ","); 27157c478bd9Sstevel@tonic-gate osroot = strtok(NULL, ","); 27167c478bd9Sstevel@tonic-gate if (osroot == NULL) 27177c478bd9Sstevel@tonic-gate osroot = menu_root; 27187c478bd9Sstevel@tonic-gate title = get_title(osroot); 27197c478bd9Sstevel@tonic-gate 27207c478bd9Sstevel@tonic-gate /* translate /dev/dsk name to grub disk name */ 27217c478bd9Sstevel@tonic-gate fp = open_diskmap(); 27227c478bd9Sstevel@tonic-gate if (fp == NULL) { 27237c478bd9Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdev); 27247c478bd9Sstevel@tonic-gate return (BAM_ERROR); 27257c478bd9Sstevel@tonic-gate } 27267c478bd9Sstevel@tonic-gate grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp)); 27277c478bd9Sstevel@tonic-gate (void) fclose(fp); 27287c478bd9Sstevel@tonic-gate if (grubdisk == NULL) { 27297c478bd9Sstevel@tonic-gate bam_error(DISKMAP_FAIL, osdev); 27307c478bd9Sstevel@tonic-gate return (BAM_ERROR); 27317c478bd9Sstevel@tonic-gate } 27327c478bd9Sstevel@tonic-gate 27337c478bd9Sstevel@tonic-gate /* delete existing entries with matching grub hd name */ 27347c478bd9Sstevel@tonic-gate for (;;) { 27357c478bd9Sstevel@tonic-gate entry_to_delete = -1; 27367c478bd9Sstevel@tonic-gate bootadm_entry = 0; 27377c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 27387c478bd9Sstevel@tonic-gate /* 27397c478bd9Sstevel@tonic-gate * can only delete bootadm entries 27407c478bd9Sstevel@tonic-gate */ 27417c478bd9Sstevel@tonic-gate if (lp->flags == BAM_COMMENT) { 27427c478bd9Sstevel@tonic-gate if (strcmp(lp->arg, BAM_HDR) == 0) 27437c478bd9Sstevel@tonic-gate bootadm_entry = 1; 27447c478bd9Sstevel@tonic-gate else if (strcmp(lp->arg, BAM_FTR) == 0) 27457c478bd9Sstevel@tonic-gate bootadm_entry = 0; 27467c478bd9Sstevel@tonic-gate } 27477c478bd9Sstevel@tonic-gate 27487c478bd9Sstevel@tonic-gate if (bootadm_entry && lp->flags == BAM_ENTRY && 27497c478bd9Sstevel@tonic-gate strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0 && 27507c478bd9Sstevel@tonic-gate strcmp(lp->arg, grubdisk) == 0) { 27517c478bd9Sstevel@tonic-gate entry_to_delete = lp->entryNum; 27527c478bd9Sstevel@tonic-gate } 27537c478bd9Sstevel@tonic-gate } 27547c478bd9Sstevel@tonic-gate if (entry_to_delete == -1) 27557c478bd9Sstevel@tonic-gate break; 27567c478bd9Sstevel@tonic-gate (void) do_delete(mp, entry_to_delete); 27577c478bd9Sstevel@tonic-gate } 27587c478bd9Sstevel@tonic-gate 27597c478bd9Sstevel@tonic-gate /* add the entry for normal Solaris */ 27607c478bd9Sstevel@tonic-gate entry = add_boot_entry(mp, title, grubdisk, 27617c478bd9Sstevel@tonic-gate "/platform/i86pc/multiboot", 27627c478bd9Sstevel@tonic-gate "/platform/i86pc/boot_archive"); 27637c478bd9Sstevel@tonic-gate 27647c478bd9Sstevel@tonic-gate /* add the entry for failsafe archive */ 27657c478bd9Sstevel@tonic-gate (void) add_boot_entry(mp, "Solaris failsafe", grubdisk, 27667c478bd9Sstevel@tonic-gate "/boot/multiboot kernel/unix -s", 27677c478bd9Sstevel@tonic-gate "/boot/x86.miniroot-safe"); 27687c478bd9Sstevel@tonic-gate free(grubdisk); 27697c478bd9Sstevel@tonic-gate 27707c478bd9Sstevel@tonic-gate if (entry == BAM_ERROR) { 27717c478bd9Sstevel@tonic-gate return (BAM_ERROR); 27727c478bd9Sstevel@tonic-gate } 27737c478bd9Sstevel@tonic-gate (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 27747c478bd9Sstevel@tonic-gate return (BAM_WRITE); 27757c478bd9Sstevel@tonic-gate } 27767c478bd9Sstevel@tonic-gate 2777*b610f78eSvikram static char * 2778*b610f78eSvikram read_grub_root(void) 2779*b610f78eSvikram { 2780*b610f78eSvikram FILE *fp; 2781*b610f78eSvikram struct stat sb; 2782*b610f78eSvikram char buf[BAM_MAXLINE]; 2783*b610f78eSvikram char *rootstr; 2784*b610f78eSvikram 2785*b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 2786*b610f78eSvikram bam_error(MISSING_SLICE_FILE, GRUB_slice, strerror(errno)); 2787*b610f78eSvikram return (NULL); 2788*b610f78eSvikram } 2789*b610f78eSvikram 2790*b610f78eSvikram if (stat(GRUB_root, &sb) != 0) { 2791*b610f78eSvikram bam_error(MISSING_ROOT_FILE, GRUB_root, strerror(errno)); 2792*b610f78eSvikram return (NULL); 2793*b610f78eSvikram } 2794*b610f78eSvikram 2795*b610f78eSvikram fp = fopen(GRUB_root, "r"); 2796*b610f78eSvikram if (fp == NULL) { 2797*b610f78eSvikram bam_error(OPEN_FAIL, GRUB_root, strerror(errno)); 2798*b610f78eSvikram return (NULL); 2799*b610f78eSvikram } 2800*b610f78eSvikram 2801*b610f78eSvikram if (s_fgets(buf, sizeof (buf), fp) == NULL) { 2802*b610f78eSvikram bam_error(EMPTY_FILE, GRUB_root, strerror(errno)); 2803*b610f78eSvikram (void) fclose(fp); 2804*b610f78eSvikram return (NULL); 2805*b610f78eSvikram } 2806*b610f78eSvikram 2807*b610f78eSvikram /* 2808*b610f78eSvikram * Copy buf here as check below may trash the buffer 2809*b610f78eSvikram */ 2810*b610f78eSvikram rootstr = s_strdup(buf); 2811*b610f78eSvikram 2812*b610f78eSvikram if (s_fgets(buf, sizeof (buf), fp) != NULL) { 2813*b610f78eSvikram bam_error(BAD_ROOT_FILE, GRUB_root); 2814*b610f78eSvikram free(rootstr); 2815*b610f78eSvikram rootstr = NULL; 2816*b610f78eSvikram } 2817*b610f78eSvikram 2818*b610f78eSvikram (void) fclose(fp); 2819*b610f78eSvikram 2820*b610f78eSvikram return (rootstr); 2821*b610f78eSvikram } 2822*b610f78eSvikram 28237c478bd9Sstevel@tonic-gate /* 28247c478bd9Sstevel@tonic-gate * This function is for supporting reboot with args. 28257c478bd9Sstevel@tonic-gate * The opt value can be: 28267c478bd9Sstevel@tonic-gate * NULL delete temp entry, if present 28277c478bd9Sstevel@tonic-gate * entry=# switches default entry to 1 28287c478bd9Sstevel@tonic-gate * else treated as boot-args and setup a temperary menu entry 28297c478bd9Sstevel@tonic-gate * and make it the default 28307c478bd9Sstevel@tonic-gate */ 28317c478bd9Sstevel@tonic-gate #define REBOOT_TITLE "Solaris_reboot_transient" 28327c478bd9Sstevel@tonic-gate 28337c478bd9Sstevel@tonic-gate static error_t 28347c478bd9Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt) 28357c478bd9Sstevel@tonic-gate { 28367c478bd9Sstevel@tonic-gate int entry; 28377c478bd9Sstevel@tonic-gate char *grubdisk, *rootdev; 28387c478bd9Sstevel@tonic-gate char kernbuf[1024]; 2839*b610f78eSvikram struct stat sb; 28407c478bd9Sstevel@tonic-gate 28417c478bd9Sstevel@tonic-gate assert(mp); 28427c478bd9Sstevel@tonic-gate 28437c478bd9Sstevel@tonic-gate if (opt != NULL && 28447c478bd9Sstevel@tonic-gate strncmp(opt, "entry=", strlen("entry=")) == 0 && 28457c478bd9Sstevel@tonic-gate selector(mp, opt, &entry, NULL) == BAM_SUCCESS) { 28467c478bd9Sstevel@tonic-gate /* this is entry=# option */ 28477c478bd9Sstevel@tonic-gate return (set_global(mp, menu_cmds[DEFAULT_CMD], entry)); 28487c478bd9Sstevel@tonic-gate } 28497c478bd9Sstevel@tonic-gate 28507c478bd9Sstevel@tonic-gate /* If no option, delete exiting reboot menu entry */ 28517c478bd9Sstevel@tonic-gate if (opt == NULL) 28527c478bd9Sstevel@tonic-gate return (delete_entry(mp, menupath, "title="REBOOT_TITLE)); 28537c478bd9Sstevel@tonic-gate 28547c478bd9Sstevel@tonic-gate /* 28557c478bd9Sstevel@tonic-gate * add a new menu entry base on opt and make it the default 2856*b610f78eSvikram */ 2857*b610f78eSvikram grubdisk = NULL; 2858*b610f78eSvikram if (stat(GRUB_slice, &sb) != 0) { 2859*b610f78eSvikram /* 28607c478bd9Sstevel@tonic-gate * 1. First get root disk name from mnttab 28617c478bd9Sstevel@tonic-gate * 2. Translate disk name to grub name 28627c478bd9Sstevel@tonic-gate * 3. Add the new menu entry 28637c478bd9Sstevel@tonic-gate */ 28647c478bd9Sstevel@tonic-gate rootdev = get_special("/"); 28657c478bd9Sstevel@tonic-gate if (rootdev) { 28667c478bd9Sstevel@tonic-gate grubdisk = os_to_grubdisk(rootdev, 1); 28677c478bd9Sstevel@tonic-gate free(rootdev); 28687c478bd9Sstevel@tonic-gate } 2869*b610f78eSvikram } else { 2870*b610f78eSvikram /* 2871*b610f78eSvikram * This is an LU BE. The GRUB_root file 2872*b610f78eSvikram * contains entry for GRUB's "root" cmd. 2873*b610f78eSvikram */ 2874*b610f78eSvikram grubdisk = read_grub_root(); 2875*b610f78eSvikram } 28767c478bd9Sstevel@tonic-gate if (grubdisk == NULL) { 2877*b610f78eSvikram bam_error(REBOOT_WITH_ARGS_FAILED); 28787c478bd9Sstevel@tonic-gate return (BAM_ERROR); 28797c478bd9Sstevel@tonic-gate } 28807c478bd9Sstevel@tonic-gate 28817c478bd9Sstevel@tonic-gate /* add an entry for Solaris reboot */ 28827c478bd9Sstevel@tonic-gate (void) snprintf(kernbuf, sizeof (kernbuf), 28837c478bd9Sstevel@tonic-gate "/platform/i86pc/multiboot %s", opt); 28847c478bd9Sstevel@tonic-gate entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf, 28857c478bd9Sstevel@tonic-gate "/platform/i86pc/boot_archive"); 28867c478bd9Sstevel@tonic-gate free(grubdisk); 28877c478bd9Sstevel@tonic-gate 28887c478bd9Sstevel@tonic-gate if (entry == BAM_ERROR) { 2889*b610f78eSvikram bam_error(REBOOT_WITH_ARGS_FAILED); 28907c478bd9Sstevel@tonic-gate return (BAM_ERROR); 28917c478bd9Sstevel@tonic-gate } 28927c478bd9Sstevel@tonic-gate (void) set_global(mp, menu_cmds[DEFAULT_CMD], entry); 28937c478bd9Sstevel@tonic-gate return (BAM_WRITE); 28947c478bd9Sstevel@tonic-gate } 28957c478bd9Sstevel@tonic-gate 28967c478bd9Sstevel@tonic-gate static error_t 28977c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val) 28987c478bd9Sstevel@tonic-gate { 28997c478bd9Sstevel@tonic-gate line_t *lp, *found, *last; 29007c478bd9Sstevel@tonic-gate char *cp, *str; 29017c478bd9Sstevel@tonic-gate char prefix[BAM_MAXLINE]; 29027c478bd9Sstevel@tonic-gate size_t len; 29037c478bd9Sstevel@tonic-gate 29047c478bd9Sstevel@tonic-gate assert(mp); 29057c478bd9Sstevel@tonic-gate assert(globalcmd); 29067c478bd9Sstevel@tonic-gate 2907*b610f78eSvikram if (strcmp(globalcmd, menu_cmds[DEFAULT_CMD]) == 0) { 2908*b610f78eSvikram if (val < 0 || mp->end == NULL || val > mp->end->entryNum) { 2909*b610f78eSvikram (void) snprintf(prefix, sizeof (prefix), "%d", val); 2910*b610f78eSvikram bam_error(INVALID_ENTRY, prefix); 2911*b610f78eSvikram return (BAM_ERROR); 2912*b610f78eSvikram } 2913*b610f78eSvikram } 2914*b610f78eSvikram 29157c478bd9Sstevel@tonic-gate found = last = NULL; 29167c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 29177c478bd9Sstevel@tonic-gate if (lp->flags != BAM_GLOBAL) 29187c478bd9Sstevel@tonic-gate continue; 29197c478bd9Sstevel@tonic-gate 29207c478bd9Sstevel@tonic-gate last = lp; /* track the last global found */ 29217c478bd9Sstevel@tonic-gate 29227c478bd9Sstevel@tonic-gate if (lp->cmd == NULL) { 29237c478bd9Sstevel@tonic-gate bam_error(NO_CMD, lp->lineNum); 29247c478bd9Sstevel@tonic-gate continue; 29257c478bd9Sstevel@tonic-gate } 29267c478bd9Sstevel@tonic-gate if (strcmp(globalcmd, lp->cmd) != 0) 29277c478bd9Sstevel@tonic-gate continue; 29287c478bd9Sstevel@tonic-gate 29297c478bd9Sstevel@tonic-gate if (found) { 29307c478bd9Sstevel@tonic-gate bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 29317c478bd9Sstevel@tonic-gate } 29327c478bd9Sstevel@tonic-gate found = lp; 29337c478bd9Sstevel@tonic-gate } 29347c478bd9Sstevel@tonic-gate 29357c478bd9Sstevel@tonic-gate if (found == NULL) { 29367c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 29377c478bd9Sstevel@tonic-gate if (last == NULL) { 29387c478bd9Sstevel@tonic-gate lp->next = mp->start; 29397c478bd9Sstevel@tonic-gate mp->start = lp; 29407c478bd9Sstevel@tonic-gate mp->end = (mp->end) ? mp->end : lp; 29417c478bd9Sstevel@tonic-gate } else { 29427c478bd9Sstevel@tonic-gate lp->next = last->next; 29437c478bd9Sstevel@tonic-gate last->next = lp; 29447c478bd9Sstevel@tonic-gate if (lp->next == NULL) 29457c478bd9Sstevel@tonic-gate mp->end = lp; 29467c478bd9Sstevel@tonic-gate } 29477c478bd9Sstevel@tonic-gate lp->flags = BAM_GLOBAL; /* other fields not needed for writes */ 29487c478bd9Sstevel@tonic-gate len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]); 29497c478bd9Sstevel@tonic-gate len += 10; /* val < 10 digits */ 29507c478bd9Sstevel@tonic-gate lp->line = s_calloc(1, len); 29517c478bd9Sstevel@tonic-gate (void) snprintf(lp->line, len, "%s%s%d", 29527c478bd9Sstevel@tonic-gate globalcmd, menu_cmds[SEP_CMD], val); 29537c478bd9Sstevel@tonic-gate return (BAM_WRITE); 29547c478bd9Sstevel@tonic-gate } 29557c478bd9Sstevel@tonic-gate 29567c478bd9Sstevel@tonic-gate /* 29577c478bd9Sstevel@tonic-gate * We are changing an existing entry. Retain any prefix whitespace, 29587c478bd9Sstevel@tonic-gate * but overwrite everything else. This preserves tabs added for 29597c478bd9Sstevel@tonic-gate * readability. 29607c478bd9Sstevel@tonic-gate */ 29617c478bd9Sstevel@tonic-gate str = found->line; 29627c478bd9Sstevel@tonic-gate cp = prefix; 29637c478bd9Sstevel@tonic-gate while (*str == ' ' || *str == '\t') 29647c478bd9Sstevel@tonic-gate *(cp++) = *(str++); 29657c478bd9Sstevel@tonic-gate *cp = '\0'; /* Terminate prefix */ 29667c478bd9Sstevel@tonic-gate len = strlen(prefix) + strlen(globalcmd); 29677c478bd9Sstevel@tonic-gate len += strlen(menu_cmds[SEP_CMD]) + 10; 29687c478bd9Sstevel@tonic-gate 29697c478bd9Sstevel@tonic-gate free(found->line); 29707c478bd9Sstevel@tonic-gate found->line = s_calloc(1, len); 29717c478bd9Sstevel@tonic-gate (void) snprintf(found->line, len, 29727c478bd9Sstevel@tonic-gate "%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val); 29737c478bd9Sstevel@tonic-gate 29747c478bd9Sstevel@tonic-gate return (BAM_WRITE); /* need a write to menu */ 29757c478bd9Sstevel@tonic-gate } 29767c478bd9Sstevel@tonic-gate 29777c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 29787c478bd9Sstevel@tonic-gate static error_t 29797c478bd9Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt) 29807c478bd9Sstevel@tonic-gate { 29817c478bd9Sstevel@tonic-gate int optnum, optval; 29827c478bd9Sstevel@tonic-gate char *val; 29837c478bd9Sstevel@tonic-gate 29847c478bd9Sstevel@tonic-gate assert(mp); 29857c478bd9Sstevel@tonic-gate assert(opt); 29867c478bd9Sstevel@tonic-gate 29877c478bd9Sstevel@tonic-gate val = strchr(opt, '='); 29887c478bd9Sstevel@tonic-gate if (val == NULL) { 29897c478bd9Sstevel@tonic-gate bam_error(INVALID_ENTRY, opt); 29907c478bd9Sstevel@tonic-gate return (BAM_ERROR); 29917c478bd9Sstevel@tonic-gate } 29927c478bd9Sstevel@tonic-gate 29937c478bd9Sstevel@tonic-gate *val = '\0'; 29947c478bd9Sstevel@tonic-gate if (strcmp(opt, "default") == 0) { 29957c478bd9Sstevel@tonic-gate optnum = DEFAULT_CMD; 29967c478bd9Sstevel@tonic-gate } else if (strcmp(opt, "timeout") == 0) { 29977c478bd9Sstevel@tonic-gate optnum = TIMEOUT_CMD; 29987c478bd9Sstevel@tonic-gate } else { 29997c478bd9Sstevel@tonic-gate bam_error(INVALID_ENTRY, opt); 30007c478bd9Sstevel@tonic-gate return (BAM_ERROR); 30017c478bd9Sstevel@tonic-gate } 30027c478bd9Sstevel@tonic-gate 30037c478bd9Sstevel@tonic-gate optval = s_strtol(val + 1); 30047c478bd9Sstevel@tonic-gate *val = '='; 30057c478bd9Sstevel@tonic-gate return (set_global(mp, menu_cmds[optnum], optval)); 30067c478bd9Sstevel@tonic-gate } 30077c478bd9Sstevel@tonic-gate 30087c478bd9Sstevel@tonic-gate /* 30097c478bd9Sstevel@tonic-gate * The quiet argument suppresses messages. This is used 30107c478bd9Sstevel@tonic-gate * when invoked in the context of other commands (e.g. list_entry) 30117c478bd9Sstevel@tonic-gate */ 30127c478bd9Sstevel@tonic-gate static error_t 30137c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet) 30147c478bd9Sstevel@tonic-gate { 30157c478bd9Sstevel@tonic-gate line_t *lp; 30167c478bd9Sstevel@tonic-gate char *arg; 30177c478bd9Sstevel@tonic-gate int done, ret = BAM_SUCCESS; 30187c478bd9Sstevel@tonic-gate 30197c478bd9Sstevel@tonic-gate assert(mp); 30207c478bd9Sstevel@tonic-gate assert(menu_path); 30217c478bd9Sstevel@tonic-gate assert(globalcmd); 30227c478bd9Sstevel@tonic-gate 30237c478bd9Sstevel@tonic-gate if (mp->start == NULL) { 30247c478bd9Sstevel@tonic-gate if (!quiet) 30257c478bd9Sstevel@tonic-gate bam_error(NO_MENU, menu_path); 30267c478bd9Sstevel@tonic-gate return (BAM_ERROR); 30277c478bd9Sstevel@tonic-gate } 30287c478bd9Sstevel@tonic-gate 30297c478bd9Sstevel@tonic-gate done = 0; 30307c478bd9Sstevel@tonic-gate for (lp = mp->start; lp; lp = lp->next) { 30317c478bd9Sstevel@tonic-gate if (lp->flags != BAM_GLOBAL) 30327c478bd9Sstevel@tonic-gate continue; 30337c478bd9Sstevel@tonic-gate 30347c478bd9Sstevel@tonic-gate if (lp->cmd == NULL) { 30357c478bd9Sstevel@tonic-gate if (!quiet) 30367c478bd9Sstevel@tonic-gate bam_error(NO_CMD, lp->lineNum); 30377c478bd9Sstevel@tonic-gate continue; 30387c478bd9Sstevel@tonic-gate } 30397c478bd9Sstevel@tonic-gate 30407c478bd9Sstevel@tonic-gate if (strcmp(globalcmd, lp->cmd) != 0) 30417c478bd9Sstevel@tonic-gate continue; 30427c478bd9Sstevel@tonic-gate 30437c478bd9Sstevel@tonic-gate /* Found global. Check for duplicates */ 30447c478bd9Sstevel@tonic-gate if (done && !quiet) { 30457c478bd9Sstevel@tonic-gate bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root); 30467c478bd9Sstevel@tonic-gate ret = BAM_ERROR; 30477c478bd9Sstevel@tonic-gate } 30487c478bd9Sstevel@tonic-gate 30497c478bd9Sstevel@tonic-gate arg = lp->arg ? lp->arg : ""; 30507c478bd9Sstevel@tonic-gate bam_print(GLOBAL_CMD, globalcmd, arg); 30517c478bd9Sstevel@tonic-gate done = 1; 30527c478bd9Sstevel@tonic-gate } 30537c478bd9Sstevel@tonic-gate 30547c478bd9Sstevel@tonic-gate if (!done && bam_verbose) 30557c478bd9Sstevel@tonic-gate bam_print(NO_ENTRY, globalcmd); 30567c478bd9Sstevel@tonic-gate 30577c478bd9Sstevel@tonic-gate return (ret); 30587c478bd9Sstevel@tonic-gate } 30597c478bd9Sstevel@tonic-gate 30607c478bd9Sstevel@tonic-gate static error_t 30617c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp) 30627c478bd9Sstevel@tonic-gate { 30637c478bd9Sstevel@tonic-gate return (list2file(root, MENU_TMP, GRUB_MENU, mp->start)); 30647c478bd9Sstevel@tonic-gate } 30657c478bd9Sstevel@tonic-gate 30667c478bd9Sstevel@tonic-gate static void 30677c478bd9Sstevel@tonic-gate line_free(line_t *lp) 30687c478bd9Sstevel@tonic-gate { 30697c478bd9Sstevel@tonic-gate if (lp == NULL) 30707c478bd9Sstevel@tonic-gate return; 30717c478bd9Sstevel@tonic-gate 30727c478bd9Sstevel@tonic-gate if (lp->cmd) 30737c478bd9Sstevel@tonic-gate free(lp->cmd); 30747c478bd9Sstevel@tonic-gate if (lp->sep) 30757c478bd9Sstevel@tonic-gate free(lp->sep); 30767c478bd9Sstevel@tonic-gate if (lp->arg) 30777c478bd9Sstevel@tonic-gate free(lp->arg); 30787c478bd9Sstevel@tonic-gate if (lp->line) 30797c478bd9Sstevel@tonic-gate free(lp->line); 30807c478bd9Sstevel@tonic-gate free(lp); 30817c478bd9Sstevel@tonic-gate } 30827c478bd9Sstevel@tonic-gate 30837c478bd9Sstevel@tonic-gate static void 30847c478bd9Sstevel@tonic-gate linelist_free(line_t *start) 30857c478bd9Sstevel@tonic-gate { 30867c478bd9Sstevel@tonic-gate line_t *lp; 30877c478bd9Sstevel@tonic-gate 30887c478bd9Sstevel@tonic-gate while (start) { 30897c478bd9Sstevel@tonic-gate lp = start; 30907c478bd9Sstevel@tonic-gate start = start->next; 30917c478bd9Sstevel@tonic-gate line_free(lp); 30927c478bd9Sstevel@tonic-gate } 30937c478bd9Sstevel@tonic-gate } 30947c478bd9Sstevel@tonic-gate 30957c478bd9Sstevel@tonic-gate static void 30967c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp) 30977c478bd9Sstevel@tonic-gate { 30987c478bd9Sstevel@tonic-gate linelist_free(flistp->head); 30997c478bd9Sstevel@tonic-gate flistp->head = NULL; 31007c478bd9Sstevel@tonic-gate flistp->tail = NULL; 31017c478bd9Sstevel@tonic-gate } 31027c478bd9Sstevel@tonic-gate 31037c478bd9Sstevel@tonic-gate static void 31047c478bd9Sstevel@tonic-gate menu_free(menu_t *mp) 31057c478bd9Sstevel@tonic-gate { 31067c478bd9Sstevel@tonic-gate assert(mp); 31077c478bd9Sstevel@tonic-gate 31087c478bd9Sstevel@tonic-gate if (mp->start) 31097c478bd9Sstevel@tonic-gate linelist_free(mp->start); 31107c478bd9Sstevel@tonic-gate free(mp); 31117c478bd9Sstevel@tonic-gate 31127c478bd9Sstevel@tonic-gate } 31137c478bd9Sstevel@tonic-gate 31147c478bd9Sstevel@tonic-gate /* 31157c478bd9Sstevel@tonic-gate * Utility routines 31167c478bd9Sstevel@tonic-gate */ 31177c478bd9Sstevel@tonic-gate 31187c478bd9Sstevel@tonic-gate 31197c478bd9Sstevel@tonic-gate /* 31207c478bd9Sstevel@tonic-gate * Returns 0 on success 31217c478bd9Sstevel@tonic-gate * Any other value indicates an error 31227c478bd9Sstevel@tonic-gate */ 31237c478bd9Sstevel@tonic-gate static int 31247c478bd9Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize) 31257c478bd9Sstevel@tonic-gate { 31267c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 31277c478bd9Sstevel@tonic-gate int ret; 31287c478bd9Sstevel@tonic-gate FILE *ptr; 31297c478bd9Sstevel@tonic-gate size_t len; 31307c478bd9Sstevel@tonic-gate sigset_t set; 31317c478bd9Sstevel@tonic-gate void (*disp)(int); 31327c478bd9Sstevel@tonic-gate 31337c478bd9Sstevel@tonic-gate /* 31347c478bd9Sstevel@tonic-gate * For security 31357c478bd9Sstevel@tonic-gate * - only absolute paths are allowed 31367c478bd9Sstevel@tonic-gate * - set IFS to space and tab 31377c478bd9Sstevel@tonic-gate */ 31387c478bd9Sstevel@tonic-gate if (*cmdline != '/') { 31397c478bd9Sstevel@tonic-gate bam_error(ABS_PATH_REQ, cmdline); 31407c478bd9Sstevel@tonic-gate return (-1); 31417c478bd9Sstevel@tonic-gate } 31427c478bd9Sstevel@tonic-gate (void) putenv("IFS= \t"); 31437c478bd9Sstevel@tonic-gate 31447c478bd9Sstevel@tonic-gate /* 31457c478bd9Sstevel@tonic-gate * We may have been exec'ed with SIGCHLD blocked 31467c478bd9Sstevel@tonic-gate * unblock it here 31477c478bd9Sstevel@tonic-gate */ 31487c478bd9Sstevel@tonic-gate (void) sigemptyset(&set); 31497c478bd9Sstevel@tonic-gate (void) sigaddset(&set, SIGCHLD); 31507c478bd9Sstevel@tonic-gate if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) { 31517c478bd9Sstevel@tonic-gate bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno)); 31527c478bd9Sstevel@tonic-gate return (-1); 31537c478bd9Sstevel@tonic-gate } 31547c478bd9Sstevel@tonic-gate 31557c478bd9Sstevel@tonic-gate /* 31567c478bd9Sstevel@tonic-gate * Set SIGCHLD disposition to SIG_DFL for popen/pclose 31577c478bd9Sstevel@tonic-gate */ 31587c478bd9Sstevel@tonic-gate disp = sigset(SIGCHLD, SIG_DFL); 31597c478bd9Sstevel@tonic-gate if (disp == SIG_ERR) { 31607c478bd9Sstevel@tonic-gate bam_error(FAILED_SIG, strerror(errno)); 31617c478bd9Sstevel@tonic-gate return (-1); 31627c478bd9Sstevel@tonic-gate } 31637c478bd9Sstevel@tonic-gate if (disp == SIG_HOLD) { 31647c478bd9Sstevel@tonic-gate bam_error(BLOCKED_SIG, cmdline); 31657c478bd9Sstevel@tonic-gate return (-1); 31667c478bd9Sstevel@tonic-gate } 31677c478bd9Sstevel@tonic-gate 31687c478bd9Sstevel@tonic-gate ptr = popen(cmdline, "r"); 31697c478bd9Sstevel@tonic-gate if (ptr == NULL) { 31707c478bd9Sstevel@tonic-gate bam_error(POPEN_FAIL, cmdline, strerror(errno)); 31717c478bd9Sstevel@tonic-gate return (-1); 31727c478bd9Sstevel@tonic-gate } 31737c478bd9Sstevel@tonic-gate 31747c478bd9Sstevel@tonic-gate /* 31757c478bd9Sstevel@tonic-gate * If we simply do a pclose() following a popen(), pclose() 31767c478bd9Sstevel@tonic-gate * will close the reader end of the pipe immediately even 31777c478bd9Sstevel@tonic-gate * if the child process has not started/exited. pclose() 31787c478bd9Sstevel@tonic-gate * does wait for cmd to terminate before returning though. 31797c478bd9Sstevel@tonic-gate * When the executed command writes its output to the pipe 31807c478bd9Sstevel@tonic-gate * there is no reader process and the command dies with 31817c478bd9Sstevel@tonic-gate * SIGPIPE. To avoid this we read repeatedly until read 31827c478bd9Sstevel@tonic-gate * terminates with EOF. This indicates that the command 31837c478bd9Sstevel@tonic-gate * (writer) has closed the pipe and we can safely do a 31847c478bd9Sstevel@tonic-gate * pclose(). 31857c478bd9Sstevel@tonic-gate * 31867c478bd9Sstevel@tonic-gate * Since pclose() does wait for the command to exit, 31877c478bd9Sstevel@tonic-gate * we can safely reap the exit status of the command 31887c478bd9Sstevel@tonic-gate * from the value returned by pclose() 31897c478bd9Sstevel@tonic-gate */ 31907c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), ptr) != NULL) { 31917c478bd9Sstevel@tonic-gate /* if (bam_verbose) XXX */ 31927c478bd9Sstevel@tonic-gate bam_print(PRINT_NO_NEWLINE, buf); 31937c478bd9Sstevel@tonic-gate if (output && osize > 0) { 31947c478bd9Sstevel@tonic-gate (void) snprintf(output, osize, "%s", buf); 31957c478bd9Sstevel@tonic-gate len = strlen(buf); 31967c478bd9Sstevel@tonic-gate output += len; 31977c478bd9Sstevel@tonic-gate osize -= len; 31987c478bd9Sstevel@tonic-gate } 31997c478bd9Sstevel@tonic-gate } 32007c478bd9Sstevel@tonic-gate 32017c478bd9Sstevel@tonic-gate ret = pclose(ptr); 32027c478bd9Sstevel@tonic-gate if (ret == -1) { 32037c478bd9Sstevel@tonic-gate bam_error(PCLOSE_FAIL, cmdline, strerror(errno)); 32047c478bd9Sstevel@tonic-gate return (-1); 32057c478bd9Sstevel@tonic-gate } 32067c478bd9Sstevel@tonic-gate 32077c478bd9Sstevel@tonic-gate if (WIFEXITED(ret)) { 32087c478bd9Sstevel@tonic-gate return (WEXITSTATUS(ret)); 32097c478bd9Sstevel@tonic-gate } else { 32107c478bd9Sstevel@tonic-gate bam_error(EXEC_FAIL, cmdline, ret); 32117c478bd9Sstevel@tonic-gate return (-1); 32127c478bd9Sstevel@tonic-gate } 32137c478bd9Sstevel@tonic-gate } 32147c478bd9Sstevel@tonic-gate 32157c478bd9Sstevel@tonic-gate /* 32167c478bd9Sstevel@tonic-gate * Since this function returns -1 on error 32177c478bd9Sstevel@tonic-gate * it cannot be used to convert -1. However, 32187c478bd9Sstevel@tonic-gate * that is sufficient for what we need. 32197c478bd9Sstevel@tonic-gate */ 32207c478bd9Sstevel@tonic-gate static long 32217c478bd9Sstevel@tonic-gate s_strtol(char *str) 32227c478bd9Sstevel@tonic-gate { 32237c478bd9Sstevel@tonic-gate long l; 32247c478bd9Sstevel@tonic-gate char *res = NULL; 32257c478bd9Sstevel@tonic-gate 32267c478bd9Sstevel@tonic-gate if (str == NULL) { 32277c478bd9Sstevel@tonic-gate return (-1); 32287c478bd9Sstevel@tonic-gate } 32297c478bd9Sstevel@tonic-gate 32307c478bd9Sstevel@tonic-gate errno = 0; 32317c478bd9Sstevel@tonic-gate l = strtol(str, &res, 10); 32327c478bd9Sstevel@tonic-gate if (errno || *res != '\0') { 32337c478bd9Sstevel@tonic-gate return (-1); 32347c478bd9Sstevel@tonic-gate } 32357c478bd9Sstevel@tonic-gate 32367c478bd9Sstevel@tonic-gate return (l); 32377c478bd9Sstevel@tonic-gate } 32387c478bd9Sstevel@tonic-gate 32397c478bd9Sstevel@tonic-gate /* 32407c478bd9Sstevel@tonic-gate * Wrapper around fputs, that adds a newline (since fputs doesn't) 32417c478bd9Sstevel@tonic-gate */ 32427c478bd9Sstevel@tonic-gate static int 32437c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp) 32447c478bd9Sstevel@tonic-gate { 32457c478bd9Sstevel@tonic-gate char linebuf[BAM_MAXLINE]; 32467c478bd9Sstevel@tonic-gate 32477c478bd9Sstevel@tonic-gate (void) snprintf(linebuf, sizeof (linebuf), "%s\n", str); 32487c478bd9Sstevel@tonic-gate return (fputs(linebuf, fp)); 32497c478bd9Sstevel@tonic-gate } 32507c478bd9Sstevel@tonic-gate 32517c478bd9Sstevel@tonic-gate /* 32527c478bd9Sstevel@tonic-gate * Wrapper around fgets, that strips newlines returned by fgets 32537c478bd9Sstevel@tonic-gate */ 32547c478bd9Sstevel@tonic-gate static char * 32557c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp) 32567c478bd9Sstevel@tonic-gate { 32577c478bd9Sstevel@tonic-gate int n; 32587c478bd9Sstevel@tonic-gate 32597c478bd9Sstevel@tonic-gate buf = fgets(buf, buflen, fp); 32607c478bd9Sstevel@tonic-gate if (buf) { 32617c478bd9Sstevel@tonic-gate n = strlen(buf); 32627c478bd9Sstevel@tonic-gate if (n == buflen - 1 && buf[n-1] != '\n') 32637c478bd9Sstevel@tonic-gate bam_error(TOO_LONG, buflen - 1, buf); 32647c478bd9Sstevel@tonic-gate buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1]; 32657c478bd9Sstevel@tonic-gate } 32667c478bd9Sstevel@tonic-gate 32677c478bd9Sstevel@tonic-gate return (buf); 32687c478bd9Sstevel@tonic-gate } 32697c478bd9Sstevel@tonic-gate 32707c478bd9Sstevel@tonic-gate static void * 32717c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz) 32727c478bd9Sstevel@tonic-gate { 32737c478bd9Sstevel@tonic-gate void *ptr; 32747c478bd9Sstevel@tonic-gate 32757c478bd9Sstevel@tonic-gate ptr = calloc(nelem, sz); 32767c478bd9Sstevel@tonic-gate if (ptr == NULL) { 32777c478bd9Sstevel@tonic-gate bam_error(NO_MEM, nelem*sz); 32787c478bd9Sstevel@tonic-gate bam_exit(1); 32797c478bd9Sstevel@tonic-gate } 32807c478bd9Sstevel@tonic-gate return (ptr); 32817c478bd9Sstevel@tonic-gate } 32827c478bd9Sstevel@tonic-gate 32837c478bd9Sstevel@tonic-gate static char * 32847c478bd9Sstevel@tonic-gate s_strdup(char *str) 32857c478bd9Sstevel@tonic-gate { 32867c478bd9Sstevel@tonic-gate char *ptr; 32877c478bd9Sstevel@tonic-gate 32887c478bd9Sstevel@tonic-gate if (str == NULL) 32897c478bd9Sstevel@tonic-gate return (NULL); 32907c478bd9Sstevel@tonic-gate 32917c478bd9Sstevel@tonic-gate ptr = strdup(str); 32927c478bd9Sstevel@tonic-gate if (ptr == NULL) { 32937c478bd9Sstevel@tonic-gate bam_error(NO_MEM, strlen(str) + 1); 32947c478bd9Sstevel@tonic-gate bam_exit(1); 32957c478bd9Sstevel@tonic-gate } 32967c478bd9Sstevel@tonic-gate return (ptr); 32977c478bd9Sstevel@tonic-gate } 32987c478bd9Sstevel@tonic-gate 32997c478bd9Sstevel@tonic-gate /* 33007c478bd9Sstevel@tonic-gate * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients) 33017c478bd9Sstevel@tonic-gate * Returns 0 otherwise 33027c478bd9Sstevel@tonic-gate */ 33037c478bd9Sstevel@tonic-gate static int 33047c478bd9Sstevel@tonic-gate is_amd64(void) 33057c478bd9Sstevel@tonic-gate { 33067c478bd9Sstevel@tonic-gate static int amd64 = -1; 33077c478bd9Sstevel@tonic-gate char isabuf[257]; /* from sysinfo(2) manpage */ 33087c478bd9Sstevel@tonic-gate 33097c478bd9Sstevel@tonic-gate if (amd64 != -1) 33107c478bd9Sstevel@tonic-gate return (amd64); 33117c478bd9Sstevel@tonic-gate 33127c478bd9Sstevel@tonic-gate if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 && 33137c478bd9Sstevel@tonic-gate strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0) 33147c478bd9Sstevel@tonic-gate amd64 = 1; 33157c478bd9Sstevel@tonic-gate else if (strstr(isabuf, "i386") == NULL) 33167c478bd9Sstevel@tonic-gate amd64 = 1; /* diskless server */ 33177c478bd9Sstevel@tonic-gate else 33187c478bd9Sstevel@tonic-gate amd64 = 0; 33197c478bd9Sstevel@tonic-gate 33207c478bd9Sstevel@tonic-gate return (amd64); 33217c478bd9Sstevel@tonic-gate } 33227c478bd9Sstevel@tonic-gate 33237c478bd9Sstevel@tonic-gate static void 33247c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s) 33257c478bd9Sstevel@tonic-gate { 33267c478bd9Sstevel@tonic-gate line_t *lp; 33277c478bd9Sstevel@tonic-gate 33287c478bd9Sstevel@tonic-gate lp = s_calloc(1, sizeof (line_t)); 33297c478bd9Sstevel@tonic-gate lp->line = s_strdup(s); 33307c478bd9Sstevel@tonic-gate if (flistp->head == NULL) 33317c478bd9Sstevel@tonic-gate flistp->head = lp; 33327c478bd9Sstevel@tonic-gate else 33337c478bd9Sstevel@tonic-gate flistp->tail->next = lp; 33347c478bd9Sstevel@tonic-gate flistp->tail = lp; 33357c478bd9Sstevel@tonic-gate } 3336