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