xref: /titanic_50/usr/src/cmd/boot/bootadm/bootadm.c (revision de531be4fe5e08fa2c0a1bf94a37c68e827ad275)
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 {
1147c478bd9Sstevel@tonic-gate     BAM_ERROR = -1,
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 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * Default file attributes
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_MODE	0644	/* default permissions */
1517c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_UID		0	/* user root */
1527c478bd9Sstevel@tonic-gate #define	DEFAULT_DEV_GID		3	/* group sys */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * Menu related
1567c478bd9Sstevel@tonic-gate  * menu_cmd_t and menu_cmds must be kept in sync
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate typedef enum {
1597c478bd9Sstevel@tonic-gate 	DEFAULT_CMD = 0,
1607c478bd9Sstevel@tonic-gate 	TIMEOUT_CMD,
1617c478bd9Sstevel@tonic-gate 	TITLE_CMD,
1627c478bd9Sstevel@tonic-gate 	ROOT_CMD,
1637c478bd9Sstevel@tonic-gate 	KERNEL_CMD,
1647c478bd9Sstevel@tonic-gate 	MODULE_CMD,
1657c478bd9Sstevel@tonic-gate 	SEP_CMD,
1667c478bd9Sstevel@tonic-gate 	COMMENT_CMD
1677c478bd9Sstevel@tonic-gate } menu_cmd_t;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static char *menu_cmds[] = {
1707c478bd9Sstevel@tonic-gate 	"default",	/* DEFAULT_CMD */
1717c478bd9Sstevel@tonic-gate 	"timeout",	/* TIMEOUT_CMD */
1727c478bd9Sstevel@tonic-gate 	"title",	/* TITLE_CMD */
1737c478bd9Sstevel@tonic-gate 	"root",		/* ROOT_CMD */
1747c478bd9Sstevel@tonic-gate 	"kernel",	/* KERNEL_CMD */
1757c478bd9Sstevel@tonic-gate 	"module",	/* MODULE_CMD */
1767c478bd9Sstevel@tonic-gate 	" ",		/* SEP_CMD */
1777c478bd9Sstevel@tonic-gate 	"#",		/* COMMENT_CMD */
1787c478bd9Sstevel@tonic-gate 	NULL
1797c478bd9Sstevel@tonic-gate };
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate #define	OPT_ENTRY_NUM	"entry"
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * archive related
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate typedef struct {
1877c478bd9Sstevel@tonic-gate 	line_t *head;
1887c478bd9Sstevel@tonic-gate 	line_t *tail;
1897c478bd9Sstevel@tonic-gate } filelist_t;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate #define	BOOT_FILE_LIST	"boot/solaris/filelist.ramdisk"
1927c478bd9Sstevel@tonic-gate #define	ETC_FILE_LIST	"etc/boot/solaris/filelist.ramdisk"
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate #define	FILE_STAT	"boot/solaris/filestat.ramdisk"
1957c478bd9Sstevel@tonic-gate #define	FILE_STAT_TMP	"boot/solaris/filestat.ramdisk.tmp"
1967c478bd9Sstevel@tonic-gate #define	DIR_PERMS	(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
1977c478bd9Sstevel@tonic-gate #define	FILE_STAT_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate #define	BAM_HDR		"---------- ADDED BY BOOTADM - DO NOT EDIT ----------"
2007c478bd9Sstevel@tonic-gate #define	BAM_FTR		"---------------------END BOOTADM--------------------"
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /* Globals */
2047c478bd9Sstevel@tonic-gate static char *prog;
2057c478bd9Sstevel@tonic-gate static subcmd_t bam_cmd;
2067c478bd9Sstevel@tonic-gate static char *bam_root;
2077c478bd9Sstevel@tonic-gate static int bam_rootlen;
2087c478bd9Sstevel@tonic-gate static int bam_root_readonly;
2097c478bd9Sstevel@tonic-gate static char *bam_subcmd;
2107c478bd9Sstevel@tonic-gate static char *bam_opt;
2117c478bd9Sstevel@tonic-gate static int bam_debug;
2127c478bd9Sstevel@tonic-gate static char **bam_argv;
2137c478bd9Sstevel@tonic-gate static int bam_argc;
2147c478bd9Sstevel@tonic-gate static int bam_force;
2157c478bd9Sstevel@tonic-gate static int bam_verbose;
2167c478bd9Sstevel@tonic-gate static int bam_check;
2177c478bd9Sstevel@tonic-gate static int bam_smf_check;
2187c478bd9Sstevel@tonic-gate static int bam_lock_fd = -1;
2197c478bd9Sstevel@tonic-gate static char rootbuf[PATH_MAX] = "/";
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /* function prototypes */
2227c478bd9Sstevel@tonic-gate static void parse_args_internal(int argc, char *argv[]);
2237c478bd9Sstevel@tonic-gate static void parse_args(int argc, char *argv[]);
2247c478bd9Sstevel@tonic-gate static error_t bam_menu(char *subcmd, char *opt, int argc, char *argv[]);
2257c478bd9Sstevel@tonic-gate static error_t bam_archive(char *subcmd, char *opt);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static void bam_error(char *format, ...);
2287c478bd9Sstevel@tonic-gate static void bam_print(char *format, ...);
2297c478bd9Sstevel@tonic-gate static void bam_exit(int excode);
2307c478bd9Sstevel@tonic-gate static void bam_lock(void);
2317c478bd9Sstevel@tonic-gate static void bam_unlock(void);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate static int exec_cmd(char *cmdline, char *output, int64_t osize);
2347c478bd9Sstevel@tonic-gate static error_t read_globals(menu_t *mp, char *menu_path,
2357c478bd9Sstevel@tonic-gate     char *globalcmd, int quiet);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate static menu_t *menu_read(char *menu_path);
2387c478bd9Sstevel@tonic-gate static error_t menu_write(char *root, menu_t *mp);
2397c478bd9Sstevel@tonic-gate static void linelist_free(line_t *start);
2407c478bd9Sstevel@tonic-gate static void menu_free(menu_t *mp);
2417c478bd9Sstevel@tonic-gate static void line_free(line_t *lp);
2427c478bd9Sstevel@tonic-gate static void filelist_free(filelist_t *flistp);
2437c478bd9Sstevel@tonic-gate static error_t list2file(char *root, char *tmp,
2447c478bd9Sstevel@tonic-gate     char *final, line_t *start);
2457c478bd9Sstevel@tonic-gate static error_t list_entry(menu_t *mp, char *menu_path, char *opt);
2467c478bd9Sstevel@tonic-gate static error_t delete_entry(menu_t *mp, char *menu_path, char *opt);
2477c478bd9Sstevel@tonic-gate static error_t delete_all_entries(menu_t *mp, char *menu_path, char *opt);
2487c478bd9Sstevel@tonic-gate static error_t update_entry(menu_t *mp, char *root, char *opt);
2497c478bd9Sstevel@tonic-gate static error_t update_temp(menu_t *mp, char *root, char *opt);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate static error_t update_archive(char *root, char *opt);
2527c478bd9Sstevel@tonic-gate static error_t list_archive(char *root, char *opt);
2537c478bd9Sstevel@tonic-gate static error_t update_all(char *root, char *opt);
2547c478bd9Sstevel@tonic-gate static error_t read_list(char *root, filelist_t  *flistp);
2557c478bd9Sstevel@tonic-gate static error_t set_global(menu_t *mp, char *globalcmd, int val);
2567c478bd9Sstevel@tonic-gate static error_t set_option(menu_t *mp, char *globalcmd, char *opt);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate static long s_strtol(char *str);
2597c478bd9Sstevel@tonic-gate static char *s_fgets(char *buf, int n, FILE *fp);
2607c478bd9Sstevel@tonic-gate static int s_fputs(char *str, FILE *fp);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate static void *s_calloc(size_t nelem, size_t sz);
2637c478bd9Sstevel@tonic-gate static char *s_strdup(char *str);
2647c478bd9Sstevel@tonic-gate static int is_readonly(char *);
2657c478bd9Sstevel@tonic-gate static int is_amd64(void);
2667c478bd9Sstevel@tonic-gate static void append_to_flist(filelist_t *, char *);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate #if defined(__sparc)
2697c478bd9Sstevel@tonic-gate static void sparc_abort(void);
2707c478bd9Sstevel@tonic-gate #endif
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /* Menu related sub commands */
2737c478bd9Sstevel@tonic-gate static subcmd_defn_t menu_subcmds[] = {
2747c478bd9Sstevel@tonic-gate 	"set_option",		OPT_OPTIONAL,	set_option,	/* PUB */
2757c478bd9Sstevel@tonic-gate 	"list_entry",		OPT_OPTIONAL,	list_entry,	/* PUB */
2767c478bd9Sstevel@tonic-gate 	"delete_all_entries",	OPT_ABSENT,	delete_all_entries, /* PVT */
2777c478bd9Sstevel@tonic-gate 	"update_entry",		OPT_REQ,	update_entry,	/* menu */
2787c478bd9Sstevel@tonic-gate 	"update_temp",		OPT_OPTIONAL,	update_temp,	/* reboot */
2797c478bd9Sstevel@tonic-gate 	NULL,			0,		NULL	/* must be last */
2807c478bd9Sstevel@tonic-gate };
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /* Archive related sub commands */
2837c478bd9Sstevel@tonic-gate static subcmd_defn_t arch_subcmds[] = {
2847c478bd9Sstevel@tonic-gate 	"update",		OPT_ABSENT,	update_archive, /* PUB */
2857c478bd9Sstevel@tonic-gate 	"update_all",		OPT_ABSENT,	update_all,	/* PVT */
2867c478bd9Sstevel@tonic-gate 	"list",			OPT_OPTIONAL,	list_archive,	/* PUB */
2877c478bd9Sstevel@tonic-gate 	NULL,			0,		NULL	/* must be last */
2887c478bd9Sstevel@tonic-gate };
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate static struct {
2917c478bd9Sstevel@tonic-gate 	nvlist_t *new_nvlp;
2927c478bd9Sstevel@tonic-gate 	nvlist_t *old_nvlp;
2937c478bd9Sstevel@tonic-gate 	int need_update;
2947c478bd9Sstevel@tonic-gate } walk_arg;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate static void
2977c478bd9Sstevel@tonic-gate usage(void)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "USAGE:\n");
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	/* archive usage */
3037c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s update-archive [-vn] [-R altroot]\n",
3047c478bd9Sstevel@tonic-gate 	    prog);
3057c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-archive [-R altroot]\n", prog);
3067c478bd9Sstevel@tonic-gate #ifndef __sparc
3077c478bd9Sstevel@tonic-gate 	/* x86 only */
3087c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s set-menu [-R altroot] key=value\n", prog);
3097c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s list-menu [-R altroot]\n", prog);
3107c478bd9Sstevel@tonic-gate #endif
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate int
3147c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	error_t ret;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3197c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
3227c478bd9Sstevel@tonic-gate 		prog = argv[0];
3237c478bd9Sstevel@tonic-gate 	} else {
3247c478bd9Sstevel@tonic-gate 		prog++;
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if (geteuid() != 0) {
3287c478bd9Sstevel@tonic-gate 		bam_error(MUST_BE_ROOT);
3297c478bd9Sstevel@tonic-gate 		bam_exit(1);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	bam_lock();
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/*
3357c478bd9Sstevel@tonic-gate 	 * Don't depend on caller's umask
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 	(void) umask(0022);
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate #if defined(__sparc)
3427c478bd9Sstevel@tonic-gate 	/*
3437c478bd9Sstevel@tonic-gate 	 * There are only two valid invocations of bootadm
3447c478bd9Sstevel@tonic-gate 	 * on SPARC:
3457c478bd9Sstevel@tonic-gate 	 *
3467c478bd9Sstevel@tonic-gate 	 *	- SPARC diskless server creating boot_archive for i386 clients
3477c478bd9Sstevel@tonic-gate 	 *	- archive creation call during reboot of a SPARC system
3487c478bd9Sstevel@tonic-gate 	 *
3497c478bd9Sstevel@tonic-gate 	 *	The latter should be a NOP
3507c478bd9Sstevel@tonic-gate 	 */
3517c478bd9Sstevel@tonic-gate 	if (bam_cmd != BAM_ARCHIVE) {
3527c478bd9Sstevel@tonic-gate 		sparc_abort();
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate #endif
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	switch (bam_cmd) {
3577c478bd9Sstevel@tonic-gate 		case BAM_MENU:
3587c478bd9Sstevel@tonic-gate 			ret = bam_menu(bam_subcmd, bam_opt, bam_argc, bam_argv);
3597c478bd9Sstevel@tonic-gate 			break;
3607c478bd9Sstevel@tonic-gate 		case BAM_ARCHIVE:
3617c478bd9Sstevel@tonic-gate 			ret = bam_archive(bam_subcmd, bam_opt);
3627c478bd9Sstevel@tonic-gate 			break;
3637c478bd9Sstevel@tonic-gate 		default:
3647c478bd9Sstevel@tonic-gate 			usage();
3657c478bd9Sstevel@tonic-gate 			bam_exit(1);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS)
3697c478bd9Sstevel@tonic-gate 		bam_exit(1);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	bam_unlock();
3727c478bd9Sstevel@tonic-gate 	return (0);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate #if defined(__sparc)
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate static void
3787c478bd9Sstevel@tonic-gate sparc_abort(void)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	bam_error(NOT_ON_SPARC);
3817c478bd9Sstevel@tonic-gate 	bam_exit(1);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate #endif
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate /*
3877c478bd9Sstevel@tonic-gate  * Equivalence of public and internal commands:
3887c478bd9Sstevel@tonic-gate  *	update-archive  -- -a update
3897c478bd9Sstevel@tonic-gate  *	list-archive	-- -a list
3907c478bd9Sstevel@tonic-gate  *	set-menu	-- -m set_option
3917c478bd9Sstevel@tonic-gate  *	list-menu	-- -m list_entry
3927c478bd9Sstevel@tonic-gate  *	update-menu	-- -m update_entry
3937c478bd9Sstevel@tonic-gate  */
3947c478bd9Sstevel@tonic-gate static struct cmd_map {
3957c478bd9Sstevel@tonic-gate 	char *bam_cmdname;
3967c478bd9Sstevel@tonic-gate 	int bam_cmd;
3977c478bd9Sstevel@tonic-gate 	char *bam_subcmd;
3987c478bd9Sstevel@tonic-gate } cmd_map[] = {
3997c478bd9Sstevel@tonic-gate 	{ "update-archive",	BAM_ARCHIVE,	"update"},
4007c478bd9Sstevel@tonic-gate 	{ "list-archive",	BAM_ARCHIVE,	"list"},
4017c478bd9Sstevel@tonic-gate 	{ "set-menu",		BAM_MENU,	"set_option"},
4027c478bd9Sstevel@tonic-gate 	{ "list-menu",		BAM_MENU,	"list_entry"},
4037c478bd9Sstevel@tonic-gate 	{ "update-menu",	BAM_MENU,	"update_entry"},
4047c478bd9Sstevel@tonic-gate 	{ NULL,			0,		NULL}
4057c478bd9Sstevel@tonic-gate };
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate  * Commands syntax published in bootadm(1M) are parsed here
4097c478bd9Sstevel@tonic-gate  */
4107c478bd9Sstevel@tonic-gate static void
4117c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	struct cmd_map *cmp = cmd_map;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	/* command conforming to the final spec */
4167c478bd9Sstevel@tonic-gate 	if (argc > 1 && argv[1][0] != '-') {
4177c478bd9Sstevel@tonic-gate 		/*
4187c478bd9Sstevel@tonic-gate 		 * Map commands to internal table.
4197c478bd9Sstevel@tonic-gate 		 */
4207c478bd9Sstevel@tonic-gate 		while (cmp->bam_cmdname) {
4217c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], cmp->bam_cmdname) == 0) {
4227c478bd9Sstevel@tonic-gate 				bam_cmd = cmp->bam_cmd;
4237c478bd9Sstevel@tonic-gate 				bam_subcmd = cmp->bam_subcmd;
4247c478bd9Sstevel@tonic-gate 				break;
4257c478bd9Sstevel@tonic-gate 			}
4267c478bd9Sstevel@tonic-gate 			cmp++;
4277c478bd9Sstevel@tonic-gate 		}
4287c478bd9Sstevel@tonic-gate 		if (cmp->bam_cmdname == NULL) {
4297c478bd9Sstevel@tonic-gate 			usage();
4307c478bd9Sstevel@tonic-gate 			bam_exit(1);
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 		argc--;
4337c478bd9Sstevel@tonic-gate 		argv++;
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	parse_args_internal(argc, argv);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate /*
4407c478bd9Sstevel@tonic-gate  * A combination of public and private commands are parsed here.
4417c478bd9Sstevel@tonic-gate  * The internal syntax and the corresponding functionality are:
4427c478bd9Sstevel@tonic-gate  *	-a update	-- update-archive
4437c478bd9Sstevel@tonic-gate  *	-a list		-- list-archive
4447c478bd9Sstevel@tonic-gate  *	-a update-all	-- (reboot to sync all mounted OS archive)
4457c478bd9Sstevel@tonic-gate  *	-m update_entry	-- update-menu
4467c478bd9Sstevel@tonic-gate  *	-m list_entry	-- list-menu
4477c478bd9Sstevel@tonic-gate  *	-m update_temp	-- (reboot -- [boot-args])
4487c478bd9Sstevel@tonic-gate  *	-m delete_all_entries -- (called from install)
4497c478bd9Sstevel@tonic-gate  */
4507c478bd9Sstevel@tonic-gate static void
4517c478bd9Sstevel@tonic-gate parse_args_internal(int argc, char *argv[])
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate 	int c, error;
4547c478bd9Sstevel@tonic-gate 	extern char *optarg;
4557c478bd9Sstevel@tonic-gate 	extern int optind, opterr;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/* Suppress error message from getopt */
4587c478bd9Sstevel@tonic-gate 	opterr = 0;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	error = 0;
4617c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "a:d:fm:no:vR:")) != -1) {
4627c478bd9Sstevel@tonic-gate 		switch (c) {
4637c478bd9Sstevel@tonic-gate 		case 'a':
4647c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
4657c478bd9Sstevel@tonic-gate 				error = 1;
4667c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
4677c478bd9Sstevel@tonic-gate 			}
4687c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_ARCHIVE;
4697c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
4707c478bd9Sstevel@tonic-gate 			break;
4717c478bd9Sstevel@tonic-gate 		case 'd':
4727c478bd9Sstevel@tonic-gate 			if (bam_debug) {
4737c478bd9Sstevel@tonic-gate 				error = 1;
4747c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4757c478bd9Sstevel@tonic-gate 			}
4767c478bd9Sstevel@tonic-gate 			bam_debug = s_strtol(optarg);
4777c478bd9Sstevel@tonic-gate 			break;
4787c478bd9Sstevel@tonic-gate 		case 'f':
4797c478bd9Sstevel@tonic-gate 			if (bam_force) {
4807c478bd9Sstevel@tonic-gate 				error = 1;
4817c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4827c478bd9Sstevel@tonic-gate 			}
4837c478bd9Sstevel@tonic-gate 			bam_force = 1;
4847c478bd9Sstevel@tonic-gate 			break;
4857c478bd9Sstevel@tonic-gate 		case 'm':
4867c478bd9Sstevel@tonic-gate 			if (bam_cmd) {
4877c478bd9Sstevel@tonic-gate 				error = 1;
4887c478bd9Sstevel@tonic-gate 				bam_error(MULT_CMDS, c);
4897c478bd9Sstevel@tonic-gate 			}
4907c478bd9Sstevel@tonic-gate 			bam_cmd = BAM_MENU;
4917c478bd9Sstevel@tonic-gate 			bam_subcmd = optarg;
4927c478bd9Sstevel@tonic-gate 			break;
4937c478bd9Sstevel@tonic-gate 		case 'n':
4947c478bd9Sstevel@tonic-gate 			if (bam_check) {
4957c478bd9Sstevel@tonic-gate 				error = 1;
4967c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
4977c478bd9Sstevel@tonic-gate 			}
4987c478bd9Sstevel@tonic-gate 			bam_check = 1;
4997c478bd9Sstevel@tonic-gate 			bam_smf_check = bam_root_readonly;
5007c478bd9Sstevel@tonic-gate 			break;
5017c478bd9Sstevel@tonic-gate 		case 'o':
5027c478bd9Sstevel@tonic-gate 			if (bam_opt) {
5037c478bd9Sstevel@tonic-gate 				error = 1;
5047c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5057c478bd9Sstevel@tonic-gate 			}
5067c478bd9Sstevel@tonic-gate 			bam_opt = optarg;
5077c478bd9Sstevel@tonic-gate 			break;
5087c478bd9Sstevel@tonic-gate 		case 'v':
5097c478bd9Sstevel@tonic-gate 			if (bam_verbose) {
5107c478bd9Sstevel@tonic-gate 				error = 1;
5117c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5127c478bd9Sstevel@tonic-gate 			}
5137c478bd9Sstevel@tonic-gate 			bam_verbose = 1;
5147c478bd9Sstevel@tonic-gate 			break;
5157c478bd9Sstevel@tonic-gate 		case 'R':
5167c478bd9Sstevel@tonic-gate 			if (bam_root) {
5177c478bd9Sstevel@tonic-gate 				error = 1;
5187c478bd9Sstevel@tonic-gate 				bam_error(DUP_OPT, c);
5197c478bd9Sstevel@tonic-gate 				break;
5207c478bd9Sstevel@tonic-gate 			} else if (realpath(optarg, rootbuf) == NULL) {
5217c478bd9Sstevel@tonic-gate 				error = 1;
5227c478bd9Sstevel@tonic-gate 				bam_error(CANT_RESOLVE, optarg,
5237c478bd9Sstevel@tonic-gate 				    strerror(errno));
5247c478bd9Sstevel@tonic-gate 				break;
5257c478bd9Sstevel@tonic-gate 			}
5267c478bd9Sstevel@tonic-gate 			bam_root = rootbuf;
5277c478bd9Sstevel@tonic-gate 			if (rootbuf[strlen(rootbuf) - 1] != '/')
5287c478bd9Sstevel@tonic-gate 				(void) strcat(rootbuf, "/");
5297c478bd9Sstevel@tonic-gate 			bam_rootlen = strlen(rootbuf);
5307c478bd9Sstevel@tonic-gate 			break;
5317c478bd9Sstevel@tonic-gate 		case '?':
5327c478bd9Sstevel@tonic-gate 			error = 1;
5337c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, optopt);
5347c478bd9Sstevel@tonic-gate 			break;
5357c478bd9Sstevel@tonic-gate 		default :
5367c478bd9Sstevel@tonic-gate 			error = 1;
5377c478bd9Sstevel@tonic-gate 			bam_error(BAD_OPT, c);
5387c478bd9Sstevel@tonic-gate 			break;
5397c478bd9Sstevel@tonic-gate 		}
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/*
5437c478bd9Sstevel@tonic-gate 	 * A command option must be specfied
5447c478bd9Sstevel@tonic-gate 	 */
5457c478bd9Sstevel@tonic-gate 	if (!bam_cmd) {
5467c478bd9Sstevel@tonic-gate 		if (bam_opt && strcmp(bam_opt, "all") == 0) {
5477c478bd9Sstevel@tonic-gate 			usage();
5487c478bd9Sstevel@tonic-gate 			bam_exit(0);
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 		bam_error(NEED_CMD);
5517c478bd9Sstevel@tonic-gate 		error = 1;
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (error) {
5557c478bd9Sstevel@tonic-gate 		usage();
5567c478bd9Sstevel@tonic-gate 		bam_exit(1);
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	if (optind > argc) {
5607c478bd9Sstevel@tonic-gate 		bam_error(INT_ERROR, "parse_args");
5617c478bd9Sstevel@tonic-gate 		bam_exit(1);
5627c478bd9Sstevel@tonic-gate 	} else if (optind < argc) {
5637c478bd9Sstevel@tonic-gate 		bam_argv = &argv[optind];
5647c478bd9Sstevel@tonic-gate 		bam_argc = argc - optind;
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	/*
5687c478bd9Sstevel@tonic-gate 	 * -n implies verbose mode
5697c478bd9Sstevel@tonic-gate 	 */
5707c478bd9Sstevel@tonic-gate 	if (bam_check)
5717c478bd9Sstevel@tonic-gate 		bam_verbose = 1;
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate static error_t
5757c478bd9Sstevel@tonic-gate check_subcmd_and_options(
5767c478bd9Sstevel@tonic-gate 	char *subcmd,
5777c478bd9Sstevel@tonic-gate 	char *opt,
5787c478bd9Sstevel@tonic-gate 	subcmd_defn_t *table,
5797c478bd9Sstevel@tonic-gate 	error_t (**fp)())
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	int i;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (subcmd == NULL) {
5847c478bd9Sstevel@tonic-gate 		bam_error(NEED_SUBCMD);
5857c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	if (bam_root == NULL) {
5897c478bd9Sstevel@tonic-gate 		bam_root = rootbuf;
5907c478bd9Sstevel@tonic-gate 		bam_rootlen = 1;
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	/* verify that subcmd is valid */
5947c478bd9Sstevel@tonic-gate 	for (i = 0; table[i].subcmd != NULL; i++) {
5957c478bd9Sstevel@tonic-gate 		if (strcmp(table[i].subcmd, subcmd) == 0)
5967c478bd9Sstevel@tonic-gate 			break;
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	if (table[i].subcmd == NULL) {
6007c478bd9Sstevel@tonic-gate 		bam_error(INVALID_SUBCMD, subcmd);
6017c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	/* subcmd verifies that opt is appropriate */
6057c478bd9Sstevel@tonic-gate 	if (table[i].option != OPT_OPTIONAL) {
6067c478bd9Sstevel@tonic-gate 		if ((table[i].option == OPT_REQ) ^ (opt != NULL)) {
6077c478bd9Sstevel@tonic-gate 			if (opt)
6087c478bd9Sstevel@tonic-gate 				bam_error(NO_OPT_REQ, subcmd);
6097c478bd9Sstevel@tonic-gate 			else
6107c478bd9Sstevel@tonic-gate 				bam_error(MISS_OPT, subcmd);
6117c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	*fp = table[i].handler;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate static error_t
6217c478bd9Sstevel@tonic-gate bam_menu(char *subcmd, char *opt, int largc, char *largv[])
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	error_t ret;
6247c478bd9Sstevel@tonic-gate 	char menu_path[PATH_MAX];
6257c478bd9Sstevel@tonic-gate 	menu_t *menu;
6267c478bd9Sstevel@tonic-gate 	error_t (*f)(menu_t *mp, char *menu_path, char *opt);
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	/*
6297c478bd9Sstevel@tonic-gate 	 * Check arguments
6307c478bd9Sstevel@tonic-gate 	 */
6317c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, menu_subcmds, &f);
6327c478bd9Sstevel@tonic-gate 	if (ret == BAM_ERROR) {
6337c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	(void) snprintf(menu_path, sizeof (menu_path), "%s%s",
6377c478bd9Sstevel@tonic-gate 	    bam_root, GRUB_MENU);
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	menu = menu_read(menu_path);
6407c478bd9Sstevel@tonic-gate 	assert(menu);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	/*
6437c478bd9Sstevel@tonic-gate 	 * Special handling for setting timeout and default
6447c478bd9Sstevel@tonic-gate 	 */
6457c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "set_option") == 0) {
6467c478bd9Sstevel@tonic-gate 		if (largc != 1 || largv[0] == NULL) {
6477c478bd9Sstevel@tonic-gate 			usage();
6487c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
6497c478bd9Sstevel@tonic-gate 		}
6507c478bd9Sstevel@tonic-gate 		opt = largv[0];
6517c478bd9Sstevel@tonic-gate 	} else if (largc != 0) {
6527c478bd9Sstevel@tonic-gate 		usage();
6537c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	/*
6577c478bd9Sstevel@tonic-gate 	 * Once the sub-cmd handler has run
6587c478bd9Sstevel@tonic-gate 	 * only the line field is guaranteed to have valid values
6597c478bd9Sstevel@tonic-gate 	 */
6607c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "update_entry") == 0)
6617c478bd9Sstevel@tonic-gate 		ret = f(menu, bam_root, opt);
6627c478bd9Sstevel@tonic-gate 	else
6637c478bd9Sstevel@tonic-gate 		ret = f(menu, menu_path, opt);
6647c478bd9Sstevel@tonic-gate 	if (ret == BAM_WRITE) {
6657c478bd9Sstevel@tonic-gate 		ret = menu_write(bam_root, menu);
6667c478bd9Sstevel@tonic-gate 	}
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	menu_free(menu);
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	return (ret);
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate static error_t
6757c478bd9Sstevel@tonic-gate bam_archive(
6767c478bd9Sstevel@tonic-gate 	char *subcmd,
6777c478bd9Sstevel@tonic-gate 	char *opt)
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate 	error_t ret;
6807c478bd9Sstevel@tonic-gate 	error_t (*f)(char *root, char *opt);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/*
6837c478bd9Sstevel@tonic-gate 	 * Check arguments
6847c478bd9Sstevel@tonic-gate 	 */
6857c478bd9Sstevel@tonic-gate 	ret = check_subcmd_and_options(subcmd, opt, arch_subcmds, &f);
6867c478bd9Sstevel@tonic-gate 	if (ret != BAM_SUCCESS) {
6877c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate #if defined(__sparc)
6917c478bd9Sstevel@tonic-gate 	/*
6927c478bd9Sstevel@tonic-gate 	 * A NOP if called on SPARC during reboot
6937c478bd9Sstevel@tonic-gate 	 */
6947c478bd9Sstevel@tonic-gate 	if (strcmp(subcmd, "update_all") == 0)
6957c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
6967c478bd9Sstevel@tonic-gate 	else if (strcmp(subcmd, "update") != 0)
6977c478bd9Sstevel@tonic-gate 		sparc_abort();
6987c478bd9Sstevel@tonic-gate #endif
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/*
7017c478bd9Sstevel@tonic-gate 	 * Check archive not supported with update_all
7027c478bd9Sstevel@tonic-gate 	 * since it is awkward to display out-of-sync
7037c478bd9Sstevel@tonic-gate 	 * information for each BE.
7047c478bd9Sstevel@tonic-gate 	 */
7057c478bd9Sstevel@tonic-gate 	if (bam_check && strcmp(subcmd, "update_all") == 0) {
7067c478bd9Sstevel@tonic-gate 		bam_error(CHECK_NOT_SUPPORTED, subcmd);
7077c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	return (f(bam_root, opt));
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
7147c478bd9Sstevel@tonic-gate static void
7157c478bd9Sstevel@tonic-gate bam_error(char *format, ...)
7167c478bd9Sstevel@tonic-gate {
7177c478bd9Sstevel@tonic-gate 	va_list ap;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	va_start(ap, format);
7207c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", prog);
7217c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, ap);
7227c478bd9Sstevel@tonic-gate 	va_end(ap);
7237c478bd9Sstevel@tonic-gate }
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
7267c478bd9Sstevel@tonic-gate static void
7277c478bd9Sstevel@tonic-gate bam_print(char *format, ...)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate 	va_list ap;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	va_start(ap, format);
7327c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
7337c478bd9Sstevel@tonic-gate 	va_end(ap);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate static void
7377c478bd9Sstevel@tonic-gate bam_exit(int excode)
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate 	bam_unlock();
7407c478bd9Sstevel@tonic-gate 	exit(excode);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate static void
7447c478bd9Sstevel@tonic-gate bam_lock(void)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	struct flock lock;
7477c478bd9Sstevel@tonic-gate 	pid_t pid;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	bam_lock_fd = open(BAM_LOCK_FILE, O_CREAT|O_RDWR, LOCK_FILE_PERMS);
7507c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
7517c478bd9Sstevel@tonic-gate 		/*
7527c478bd9Sstevel@tonic-gate 		 * We may be invoked early in boot for archive verification.
7537c478bd9Sstevel@tonic-gate 		 * In this case, root is readonly and /var/run may not exist.
7547c478bd9Sstevel@tonic-gate 		 * Proceed without the lock
7557c478bd9Sstevel@tonic-gate 		 */
7567c478bd9Sstevel@tonic-gate 		if (errno == EROFS || errno == ENOENT) {
7577c478bd9Sstevel@tonic-gate 			bam_root_readonly = 1;
7587c478bd9Sstevel@tonic-gate 			return;
7597c478bd9Sstevel@tonic-gate 		}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, BAM_LOCK_FILE, strerror(errno));
7627c478bd9Sstevel@tonic-gate 		bam_exit(1);
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
7667c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
7677c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
7687c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &lock) == -1) {
7717c478bd9Sstevel@tonic-gate 		if (errno != EACCES && errno != EAGAIN) {
7727c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
7737c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
7747c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
7757c478bd9Sstevel@tonic-gate 			bam_exit(1);
7767c478bd9Sstevel@tonic-gate 		}
7777c478bd9Sstevel@tonic-gate 		pid = 0;
7787c478bd9Sstevel@tonic-gate 		(void) pread(bam_lock_fd, &pid, sizeof (pid_t), 0);
7797c478bd9Sstevel@tonic-gate 		bam_print(FILE_LOCKED, pid);
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 		lock.l_type = F_WRLCK;
7827c478bd9Sstevel@tonic-gate 		lock.l_whence = SEEK_SET;
7837c478bd9Sstevel@tonic-gate 		lock.l_start = 0;
7847c478bd9Sstevel@tonic-gate 		lock.l_len = 0;
7857c478bd9Sstevel@tonic-gate 		if (fcntl(bam_lock_fd, F_SETLKW, &lock) == -1) {
7867c478bd9Sstevel@tonic-gate 			bam_error(LOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
7877c478bd9Sstevel@tonic-gate 			(void) close(bam_lock_fd);
7887c478bd9Sstevel@tonic-gate 			bam_lock_fd = -1;
7897c478bd9Sstevel@tonic-gate 			bam_exit(1);
7907c478bd9Sstevel@tonic-gate 		}
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	/* We own the lock now */
7947c478bd9Sstevel@tonic-gate 	pid = getpid();
7957c478bd9Sstevel@tonic-gate 	(void) write(bam_lock_fd, &pid, sizeof (pid));
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate static void
7997c478bd9Sstevel@tonic-gate bam_unlock(void)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate 	struct flock unlock;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	/*
8047c478bd9Sstevel@tonic-gate 	 * NOP if we don't hold the lock
8057c478bd9Sstevel@tonic-gate 	 */
8067c478bd9Sstevel@tonic-gate 	if (bam_lock_fd < 0) {
8077c478bd9Sstevel@tonic-gate 		return;
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
8117c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
8127c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
8137c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	if (fcntl(bam_lock_fd, F_SETLK, &unlock) == -1) {
8167c478bd9Sstevel@tonic-gate 		bam_error(UNLOCK_FAIL, BAM_LOCK_FILE, strerror(errno));
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	if (close(bam_lock_fd) == -1) {
8207c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, BAM_LOCK_FILE, strerror(errno));
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 	bam_lock_fd = -1;
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate static error_t
8267c478bd9Sstevel@tonic-gate list_archive(char *root, char *opt)
8277c478bd9Sstevel@tonic-gate {
8287c478bd9Sstevel@tonic-gate 	filelist_t flist;
8297c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
8307c478bd9Sstevel@tonic-gate 	line_t *lp;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	assert(root);
8337c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
8367c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
8377c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
8427c478bd9Sstevel@tonic-gate 		bam_print(PRINT, lp->line);
8437c478bd9Sstevel@tonic-gate 	}
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate /*
8517c478bd9Sstevel@tonic-gate  * This routine writes a list of lines to a file.
8527c478bd9Sstevel@tonic-gate  * The list is *not* freed
8537c478bd9Sstevel@tonic-gate  */
8547c478bd9Sstevel@tonic-gate static error_t
8557c478bd9Sstevel@tonic-gate list2file(char *root, char *tmp, char *final, line_t *start)
8567c478bd9Sstevel@tonic-gate {
8577c478bd9Sstevel@tonic-gate 	char tmpfile[PATH_MAX];
8587c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
8597c478bd9Sstevel@tonic-gate 	FILE *fp;
8607c478bd9Sstevel@tonic-gate 	int ret;
8617c478bd9Sstevel@tonic-gate 	struct stat sb;
8627c478bd9Sstevel@tonic-gate 	mode_t mode;
8637c478bd9Sstevel@tonic-gate 	uid_t root_uid;
8647c478bd9Sstevel@tonic-gate 	gid_t sys_gid;
8657c478bd9Sstevel@tonic-gate 	struct passwd *pw;
8667c478bd9Sstevel@tonic-gate 	struct group *gp;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, final);
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	if (start == NULL) {
8727c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != -1) {
8737c478bd9Sstevel@tonic-gate 			bam_print(UNLINK_EMPTY, path);
8747c478bd9Sstevel@tonic-gate 			if (unlink(path) != 0) {
8757c478bd9Sstevel@tonic-gate 				bam_error(UNLINK_FAIL, path, strerror(errno));
8767c478bd9Sstevel@tonic-gate 				return (BAM_ERROR);
8777c478bd9Sstevel@tonic-gate 			} else {
8787c478bd9Sstevel@tonic-gate 				return (BAM_SUCCESS);
8797c478bd9Sstevel@tonic-gate 			}
8807c478bd9Sstevel@tonic-gate 		}
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	/*
8847c478bd9Sstevel@tonic-gate 	 * Preserve attributes of existing file if possible,
8857c478bd9Sstevel@tonic-gate 	 * otherwise ask the system for uid/gid of root/sys.
8867c478bd9Sstevel@tonic-gate 	 * If all fails, fall back on hard-coded defaults.
8877c478bd9Sstevel@tonic-gate 	 */
8887c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != -1) {
8897c478bd9Sstevel@tonic-gate 		mode = sb.st_mode;
8907c478bd9Sstevel@tonic-gate 		root_uid = sb.st_uid;
8917c478bd9Sstevel@tonic-gate 		sys_gid = sb.st_gid;
8927c478bd9Sstevel@tonic-gate 	} else {
8937c478bd9Sstevel@tonic-gate 		mode = DEFAULT_DEV_MODE;
8947c478bd9Sstevel@tonic-gate 		if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
8957c478bd9Sstevel@tonic-gate 			root_uid = pw->pw_uid;
8967c478bd9Sstevel@tonic-gate 		} else {
8977c478bd9Sstevel@tonic-gate 			if (bam_verbose)
8987c478bd9Sstevel@tonic-gate 				bam_error(CANT_FIND_USER,
8997c478bd9Sstevel@tonic-gate 				    DEFAULT_DEV_USER, DEFAULT_DEV_UID);
9007c478bd9Sstevel@tonic-gate 			root_uid = (uid_t)DEFAULT_DEV_UID;
9017c478bd9Sstevel@tonic-gate 		}
9027c478bd9Sstevel@tonic-gate 		if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
9037c478bd9Sstevel@tonic-gate 			sys_gid = gp->gr_gid;
9047c478bd9Sstevel@tonic-gate 		} else {
9057c478bd9Sstevel@tonic-gate 			if (bam_verbose)
9067c478bd9Sstevel@tonic-gate 				bam_error(CANT_FIND_GROUP,
9077c478bd9Sstevel@tonic-gate 				    DEFAULT_DEV_GROUP, DEFAULT_DEV_GID);
9087c478bd9Sstevel@tonic-gate 			sys_gid = (gid_t)DEFAULT_DEV_GID;
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpfile, sizeof (tmpfile), "%s%s", root, tmp);
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	/* Truncate tmpfile first */
9157c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "w");
9167c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
9177c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
9187c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate 	ret = fclose(fp);
9217c478bd9Sstevel@tonic-gate 	if (ret == EOF) {
9227c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
9237c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	/* Now open it in append mode */
9277c478bd9Sstevel@tonic-gate 	fp = fopen(tmpfile, "a");
9287c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
9297c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, tmpfile, strerror(errno));
9307c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	for (; start; start = start->next) {
9347c478bd9Sstevel@tonic-gate 		ret = s_fputs(start->line, fp);
9357c478bd9Sstevel@tonic-gate 		if (ret == EOF) {
9367c478bd9Sstevel@tonic-gate 			bam_error(WRITE_FAIL, tmpfile, strerror(errno));
9377c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
9387c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
9397c478bd9Sstevel@tonic-gate 		}
9407c478bd9Sstevel@tonic-gate 	}
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	ret = fclose(fp);
9437c478bd9Sstevel@tonic-gate 	if (ret == EOF) {
9447c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, tmpfile, strerror(errno));
9457c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	/*
949*de531be4Sjg 	 * Set up desired attributes.  Ignore failures on filesystems
950*de531be4Sjg 	 * not supporting these operations - pcfs reports unsupported
951*de531be4Sjg 	 * operations as EINVAL.
9527c478bd9Sstevel@tonic-gate 	 */
9537c478bd9Sstevel@tonic-gate 	ret = chmod(tmpfile, mode);
954*de531be4Sjg 	if (ret == -1 &&
955*de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
9567c478bd9Sstevel@tonic-gate 		bam_error(CHMOD_FAIL, tmpfile, strerror(errno));
9577c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	ret = chown(tmpfile, root_uid, sys_gid);
961*de531be4Sjg 	if (ret == -1 &&
962*de531be4Sjg 	    errno != EINVAL && errno != ENOTSUP) {
9637c478bd9Sstevel@tonic-gate 		bam_error(CHOWN_FAIL, tmpfile, strerror(errno));
9647c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	/*
9697c478bd9Sstevel@tonic-gate 	 * Do an atomic rename
9707c478bd9Sstevel@tonic-gate 	 */
9717c478bd9Sstevel@tonic-gate 	ret = rename(tmpfile, path);
9727c478bd9Sstevel@tonic-gate 	if (ret != 0) {
9737c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path, strerror(errno));
9747c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate /*
9817c478bd9Sstevel@tonic-gate  * This function should always return 0 - since we want
9827c478bd9Sstevel@tonic-gate  * to create stat data for *all* files in the list.
9837c478bd9Sstevel@tonic-gate  */
9847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9857c478bd9Sstevel@tonic-gate static int
9867c478bd9Sstevel@tonic-gate cmpstat(
9877c478bd9Sstevel@tonic-gate 	const char *file,
9887c478bd9Sstevel@tonic-gate 	const struct stat *stat,
9897c478bd9Sstevel@tonic-gate 	int flags,
9907c478bd9Sstevel@tonic-gate 	struct FTW *ftw)
9917c478bd9Sstevel@tonic-gate {
9927c478bd9Sstevel@tonic-gate 	uint_t sz;
9937c478bd9Sstevel@tonic-gate 	uint64_t *value;
9947c478bd9Sstevel@tonic-gate 	uint64_t filestat[2];
9957c478bd9Sstevel@tonic-gate 	int error;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	/*
9987c478bd9Sstevel@tonic-gate 	 * We only want regular files
9997c478bd9Sstevel@tonic-gate 	 */
10007c478bd9Sstevel@tonic-gate 	if (!S_ISREG(stat->st_mode))
10017c478bd9Sstevel@tonic-gate 		return (0);
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	/*
10047c478bd9Sstevel@tonic-gate 	 * new_nvlp may be NULL if there were errors earlier
10057c478bd9Sstevel@tonic-gate 	 * but this is not fatal to update determination.
10067c478bd9Sstevel@tonic-gate 	 */
10077c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp) {
10087c478bd9Sstevel@tonic-gate 		filestat[0] = stat->st_size;
10097c478bd9Sstevel@tonic-gate 		filestat[1] = stat->st_mtime;
10107c478bd9Sstevel@tonic-gate 		error = nvlist_add_uint64_array(walk_arg.new_nvlp,
10117c478bd9Sstevel@tonic-gate 		    file + bam_rootlen, filestat, 2);
10127c478bd9Sstevel@tonic-gate 		if (error)
10137c478bd9Sstevel@tonic-gate 			bam_error(NVADD_FAIL, file, strerror(error));
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	/*
10177c478bd9Sstevel@tonic-gate 	 * The remaining steps are only required if we haven't made a
10187c478bd9Sstevel@tonic-gate 	 * decision about update or if we are checking (-n)
10197c478bd9Sstevel@tonic-gate 	 */
10207c478bd9Sstevel@tonic-gate 	if (walk_arg.need_update && !bam_check)
10217c478bd9Sstevel@tonic-gate 		return (0);
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	/*
10247c478bd9Sstevel@tonic-gate 	 * If we are invoked as part of system/filesyste/boot-archive
10257c478bd9Sstevel@tonic-gate 	 * SMF service, ignore amd64 modules unless we are booted amd64.
10267c478bd9Sstevel@tonic-gate 	 */
10277c478bd9Sstevel@tonic-gate 	if (bam_smf_check && !is_amd64() && strstr(file, "/amd64/") == 0)
10287c478bd9Sstevel@tonic-gate 		return (0);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	/*
10317c478bd9Sstevel@tonic-gate 	 * We need an update if file doesn't exist in old archive
10327c478bd9Sstevel@tonic-gate 	 */
10337c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp == NULL ||
10347c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64_array(walk_arg.old_nvlp,
10357c478bd9Sstevel@tonic-gate 	    file + bam_rootlen, &value, &sz) != 0) {
10367c478bd9Sstevel@tonic-gate 		if (bam_smf_check)	/* ignore new during smf check */
10377c478bd9Sstevel@tonic-gate 			return (0);
10387c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
10397c478bd9Sstevel@tonic-gate 		if (bam_verbose)
10407c478bd9Sstevel@tonic-gate 			bam_print(PARSEABLE_NEW_FILE, file);
10417c478bd9Sstevel@tonic-gate 		return (0);
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	/*
10457c478bd9Sstevel@tonic-gate 	 * File exists in old archive. Check if file has changed
10467c478bd9Sstevel@tonic-gate 	 */
10477c478bd9Sstevel@tonic-gate 	assert(sz == 2);
10487c478bd9Sstevel@tonic-gate 	bcopy(value, filestat, sizeof (filestat));
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	if (filestat[0] != stat->st_size ||
10517c478bd9Sstevel@tonic-gate 	    filestat[1] != stat->st_mtime) {
10527c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
10537c478bd9Sstevel@tonic-gate 		if (bam_verbose)
10547c478bd9Sstevel@tonic-gate 			if (bam_smf_check)
10557c478bd9Sstevel@tonic-gate 				bam_print("    %s\n", file);
10567c478bd9Sstevel@tonic-gate 			else
10577c478bd9Sstevel@tonic-gate 				bam_print(PARSEABLE_OUT_DATE, file);
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	return (0);
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate /*
10647c478bd9Sstevel@tonic-gate  * Check flags and presence of required files.
10657c478bd9Sstevel@tonic-gate  * The force flag and/or absence of files should
10667c478bd9Sstevel@tonic-gate  * trigger an update.
10677c478bd9Sstevel@tonic-gate  * Suppress stdout output if check (-n) option is set
10687c478bd9Sstevel@tonic-gate  * (as -n should only produce parseable output.)
10697c478bd9Sstevel@tonic-gate  */
10707c478bd9Sstevel@tonic-gate static void
10717c478bd9Sstevel@tonic-gate check_flags_and_files(char *root)
10727c478bd9Sstevel@tonic-gate {
10737c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
10747c478bd9Sstevel@tonic-gate 	struct stat sb;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	/*
10777c478bd9Sstevel@tonic-gate 	 * if force, create archive unconditionally
10787c478bd9Sstevel@tonic-gate 	 */
10797c478bd9Sstevel@tonic-gate 	if (bam_force) {
10807c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
10817c478bd9Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
10827c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_FORCE);
10837c478bd9Sstevel@tonic-gate 		return;
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	/*
10877c478bd9Sstevel@tonic-gate 	 * If archive is missing, create archive
10887c478bd9Sstevel@tonic-gate 	 */
10897c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE);
10907c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
10917c478bd9Sstevel@tonic-gate 		if (bam_verbose && !bam_check)
10927c478bd9Sstevel@tonic-gate 			bam_print(UPDATE_ARCH_MISS, path);
10937c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
10947c478bd9Sstevel@tonic-gate 		return;
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate }
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate static error_t
10997c478bd9Sstevel@tonic-gate read_one_list(char *root, filelist_t  *flistp, char *filelist)
11007c478bd9Sstevel@tonic-gate {
11017c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
11027c478bd9Sstevel@tonic-gate 	FILE *fp;
11037c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE];
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, filelist);
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	fp = fopen(path, "r");
11087c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
11097c478bd9Sstevel@tonic-gate 		if (bam_debug)
11107c478bd9Sstevel@tonic-gate 			bam_error(FLIST_FAIL, path, strerror(errno));
11117c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
11127c478bd9Sstevel@tonic-gate 	}
11137c478bd9Sstevel@tonic-gate 	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
11147c478bd9Sstevel@tonic-gate 		append_to_flist(flistp, buf);
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 	if (fclose(fp) != 0) {
11177c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, path, strerror(errno));
11187c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
11197c478bd9Sstevel@tonic-gate 	}
11207c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
11217c478bd9Sstevel@tonic-gate }
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate static error_t
11247c478bd9Sstevel@tonic-gate read_list(char *root, filelist_t  *flistp)
11257c478bd9Sstevel@tonic-gate {
11267c478bd9Sstevel@tonic-gate 	int rval;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	/*
11317c478bd9Sstevel@tonic-gate 	 * Read current lists of files - only the first is mandatory
11327c478bd9Sstevel@tonic-gate 	 */
11337c478bd9Sstevel@tonic-gate 	rval = read_one_list(root, flistp, BOOT_FILE_LIST);
11347c478bd9Sstevel@tonic-gate 	if (rval != BAM_SUCCESS)
11357c478bd9Sstevel@tonic-gate 		return (rval);
11367c478bd9Sstevel@tonic-gate 	(void) read_one_list(root, flistp, ETC_FILE_LIST);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL) {
11397c478bd9Sstevel@tonic-gate 		bam_error(NO_FLIST);
11407c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
11417c478bd9Sstevel@tonic-gate 	}
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate static void
11477c478bd9Sstevel@tonic-gate getoldstat(char *root)
11487c478bd9Sstevel@tonic-gate {
11497c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
11507c478bd9Sstevel@tonic-gate 	int fd, error;
11517c478bd9Sstevel@tonic-gate 	struct stat sb;
11527c478bd9Sstevel@tonic-gate 	char *ostat;
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
11557c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDONLY);
11567c478bd9Sstevel@tonic-gate 	if (fd == -1) {
11577c478bd9Sstevel@tonic-gate 		if (bam_verbose)
11587c478bd9Sstevel@tonic-gate 			bam_print(OPEN_FAIL, path, strerror(errno));
11597c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
11607c478bd9Sstevel@tonic-gate 		return;
11617c478bd9Sstevel@tonic-gate 	}
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sb) != 0) {
11647c478bd9Sstevel@tonic-gate 		bam_error(STAT_FAIL, path, strerror(errno));
11657c478bd9Sstevel@tonic-gate 		(void) close(fd);
11667c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
11677c478bd9Sstevel@tonic-gate 		return;
11687c478bd9Sstevel@tonic-gate 	}
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	ostat = s_calloc(1, sb.st_size);
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	if (read(fd, ostat, sb.st_size) != sb.st_size) {
11737c478bd9Sstevel@tonic-gate 		bam_error(READ_FAIL, path, strerror(errno));
11747c478bd9Sstevel@tonic-gate 		(void) close(fd);
11757c478bd9Sstevel@tonic-gate 		free(ostat);
11767c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
11777c478bd9Sstevel@tonic-gate 		return;
11787c478bd9Sstevel@tonic-gate 	}
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	(void) close(fd);
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
11837c478bd9Sstevel@tonic-gate 	error = nvlist_unpack(ostat, sb.st_size, &walk_arg.old_nvlp, 0);
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	free(ostat);
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	if (error) {
11887c478bd9Sstevel@tonic-gate 		bam_error(UNPACK_FAIL, path, strerror(error));
11897c478bd9Sstevel@tonic-gate 		walk_arg.old_nvlp = NULL;
11907c478bd9Sstevel@tonic-gate 		walk_arg.need_update = 1;
11917c478bd9Sstevel@tonic-gate 		return;
11927c478bd9Sstevel@tonic-gate 	}
11937c478bd9Sstevel@tonic-gate }
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate static void
11967c478bd9Sstevel@tonic-gate create_newstat(void)
11977c478bd9Sstevel@tonic-gate {
11987c478bd9Sstevel@tonic-gate 	int error;
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	error = nvlist_alloc(&walk_arg.new_nvlp, NV_UNIQUE_NAME, 0);
12017c478bd9Sstevel@tonic-gate 	if (error) {
12027c478bd9Sstevel@tonic-gate 		/*
12037c478bd9Sstevel@tonic-gate 		 * Not fatal - we can still create archive
12047c478bd9Sstevel@tonic-gate 		 */
12057c478bd9Sstevel@tonic-gate 		walk_arg.new_nvlp = NULL;
12067c478bd9Sstevel@tonic-gate 		bam_error(NVALLOC_FAIL, strerror(error));
12077c478bd9Sstevel@tonic-gate 	}
12087c478bd9Sstevel@tonic-gate }
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate static void
12117c478bd9Sstevel@tonic-gate walk_list(char *root, filelist_t *flistp)
12127c478bd9Sstevel@tonic-gate {
12137c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
12147c478bd9Sstevel@tonic-gate 	line_t *lp;
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	for (lp = flistp->head; lp; lp = lp->next) {
12177c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, lp->line);
12187c478bd9Sstevel@tonic-gate 		/* XXX shouldn't we use FTW_MOUNT ? */
12197c478bd9Sstevel@tonic-gate 		if (nftw(path, cmpstat, 20, 0) == -1) {
12207c478bd9Sstevel@tonic-gate 			/*
12217c478bd9Sstevel@tonic-gate 			 * Some files may not exist.
12227c478bd9Sstevel@tonic-gate 			 * For example: etc/rtc_config on a x86 diskless system
12237c478bd9Sstevel@tonic-gate 			 * Emit verbose message only
12247c478bd9Sstevel@tonic-gate 			 */
12257c478bd9Sstevel@tonic-gate 			if (bam_verbose)
12267c478bd9Sstevel@tonic-gate 				bam_print(NFTW_FAIL, path, strerror(errno));
12277c478bd9Sstevel@tonic-gate 		}
12287c478bd9Sstevel@tonic-gate 	}
12297c478bd9Sstevel@tonic-gate }
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate static void
12327c478bd9Sstevel@tonic-gate savenew(char *root)
12337c478bd9Sstevel@tonic-gate {
12347c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
12357c478bd9Sstevel@tonic-gate 	char path2[PATH_MAX];
12367c478bd9Sstevel@tonic-gate 	size_t sz;
12377c478bd9Sstevel@tonic-gate 	char *nstat;
12387c478bd9Sstevel@tonic-gate 	int fd, wrote, error;
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	nstat = NULL;
12417c478bd9Sstevel@tonic-gate 	sz = 0;
12427c478bd9Sstevel@tonic-gate 	error = nvlist_pack(walk_arg.new_nvlp, &nstat, &sz,
12437c478bd9Sstevel@tonic-gate 	    NV_ENCODE_XDR, 0);
12447c478bd9Sstevel@tonic-gate 	if (error) {
12457c478bd9Sstevel@tonic-gate 		bam_error(PACK_FAIL, strerror(error));
12467c478bd9Sstevel@tonic-gate 		return;
12477c478bd9Sstevel@tonic-gate 	}
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT_TMP);
12507c478bd9Sstevel@tonic-gate 	fd = open(path, O_RDWR|O_CREAT|O_TRUNC, FILE_STAT_MODE);
12517c478bd9Sstevel@tonic-gate 	if (fd == -1) {
12527c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, path, strerror(errno));
12537c478bd9Sstevel@tonic-gate 		free(nstat);
12547c478bd9Sstevel@tonic-gate 		return;
12557c478bd9Sstevel@tonic-gate 	}
12567c478bd9Sstevel@tonic-gate 	wrote = write(fd, nstat, sz);
12577c478bd9Sstevel@tonic-gate 	if (wrote != sz) {
12587c478bd9Sstevel@tonic-gate 		bam_error(WRITE_FAIL, path, strerror(errno));
12597c478bd9Sstevel@tonic-gate 		(void) close(fd);
12607c478bd9Sstevel@tonic-gate 		free(nstat);
12617c478bd9Sstevel@tonic-gate 		return;
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate 	(void) close(fd);
12647c478bd9Sstevel@tonic-gate 	free(nstat);
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	(void) snprintf(path2, sizeof (path2), "%s%s", root, FILE_STAT);
12677c478bd9Sstevel@tonic-gate 	if (rename(path, path2) != 0) {
12687c478bd9Sstevel@tonic-gate 		bam_error(RENAME_FAIL, path2, strerror(errno));
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate }
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate static void
12737c478bd9Sstevel@tonic-gate clear_walk_args(void)
12747c478bd9Sstevel@tonic-gate {
12757c478bd9Sstevel@tonic-gate 	if (walk_arg.old_nvlp)
12767c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.old_nvlp);
12777c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp)
12787c478bd9Sstevel@tonic-gate 		nvlist_free(walk_arg.new_nvlp);
12797c478bd9Sstevel@tonic-gate 	walk_arg.need_update = 0;
12807c478bd9Sstevel@tonic-gate 	walk_arg.old_nvlp = NULL;
12817c478bd9Sstevel@tonic-gate 	walk_arg.new_nvlp = NULL;
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate /*
12857c478bd9Sstevel@tonic-gate  * Returns:
12867c478bd9Sstevel@tonic-gate  *	0 - no update necessary
12877c478bd9Sstevel@tonic-gate  *	1 - update required.
12887c478bd9Sstevel@tonic-gate  *	BAM_ERROR (-1) - An error occurred
12897c478bd9Sstevel@tonic-gate  *
12907c478bd9Sstevel@tonic-gate  * Special handling for check (-n):
12917c478bd9Sstevel@tonic-gate  * ================================
12927c478bd9Sstevel@tonic-gate  * The check (-n) option produces parseable output.
12937c478bd9Sstevel@tonic-gate  * To do this, we suppress all stdout messages unrelated
12947c478bd9Sstevel@tonic-gate  * to out of sync files.
12957c478bd9Sstevel@tonic-gate  * All stderr messages are still printed though.
12967c478bd9Sstevel@tonic-gate  *
12977c478bd9Sstevel@tonic-gate  */
12987c478bd9Sstevel@tonic-gate static int
12997c478bd9Sstevel@tonic-gate update_required(char *root)
13007c478bd9Sstevel@tonic-gate {
13017c478bd9Sstevel@tonic-gate 	struct stat sb;
13027c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
13037c478bd9Sstevel@tonic-gate 	filelist_t flist;
13047c478bd9Sstevel@tonic-gate 	filelist_t *flistp = &flist;
13057c478bd9Sstevel@tonic-gate 	int need_update;
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	flistp->head = flistp->tail = NULL;
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	walk_arg.need_update = 0;
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	/*
13127c478bd9Sstevel@tonic-gate 	 * Without consulting stat data, check if we need update
13137c478bd9Sstevel@tonic-gate 	 */
13147c478bd9Sstevel@tonic-gate 	check_flags_and_files(root);
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	/*
13177c478bd9Sstevel@tonic-gate 	 * In certain deployment scenarios, filestat may not
13187c478bd9Sstevel@tonic-gate 	 * exist. Ignore it during boot-archive SMF check.
13197c478bd9Sstevel@tonic-gate 	 */
13207c478bd9Sstevel@tonic-gate 	if (bam_smf_check) {
13217c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s%s", root, FILE_STAT);
13227c478bd9Sstevel@tonic-gate 		if (stat(path, &sb) != 0)
13237c478bd9Sstevel@tonic-gate 			return (0);
13247c478bd9Sstevel@tonic-gate 	}
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	/*
13277c478bd9Sstevel@tonic-gate 	 * consult stat data only if we haven't made a decision
13287c478bd9Sstevel@tonic-gate 	 * about update. If checking (-n) however, we always
13297c478bd9Sstevel@tonic-gate 	 * need stat data (since we want to compare old and new)
13307c478bd9Sstevel@tonic-gate 	 */
13317c478bd9Sstevel@tonic-gate 	if (!walk_arg.need_update || bam_check)
13327c478bd9Sstevel@tonic-gate 		getoldstat(root);
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	/*
13357c478bd9Sstevel@tonic-gate 	 * read list of files
13367c478bd9Sstevel@tonic-gate 	 */
13377c478bd9Sstevel@tonic-gate 	if (read_list(root, flistp) != BAM_SUCCESS) {
13387c478bd9Sstevel@tonic-gate 		clear_walk_args();
13397c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	assert(flistp->head && flistp->tail);
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	/*
13457c478bd9Sstevel@tonic-gate 	 * At this point either the update is required
13467c478bd9Sstevel@tonic-gate 	 * or the decision is pending. In either case
13477c478bd9Sstevel@tonic-gate 	 * we need to create new stat nvlist
13487c478bd9Sstevel@tonic-gate 	 */
13497c478bd9Sstevel@tonic-gate 	create_newstat();
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	/*
13527c478bd9Sstevel@tonic-gate 	 * This walk does 2 things:
13537c478bd9Sstevel@tonic-gate 	 *  	- gets new stat data for every file
13547c478bd9Sstevel@tonic-gate 	 *	- (optional) compare old and new stat data
13557c478bd9Sstevel@tonic-gate 	 */
13567c478bd9Sstevel@tonic-gate 	walk_list(root, &flist);
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	/* done with the file list */
13597c478bd9Sstevel@tonic-gate 	filelist_free(flistp);
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	/*
13627c478bd9Sstevel@tonic-gate 	 * if we didn't succeed in  creating new stat data above
13637c478bd9Sstevel@tonic-gate 	 * just return result of update check so that archive is built.
13647c478bd9Sstevel@tonic-gate 	 */
13657c478bd9Sstevel@tonic-gate 	if (walk_arg.new_nvlp == NULL) {
13667c478bd9Sstevel@tonic-gate 		bam_error(NO_NEW_STAT);
13677c478bd9Sstevel@tonic-gate 		need_update = walk_arg.need_update;
13687c478bd9Sstevel@tonic-gate 		clear_walk_args();
13697c478bd9Sstevel@tonic-gate 		return (need_update ? 1 : 0);
13707c478bd9Sstevel@tonic-gate 	}
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	/*
13747c478bd9Sstevel@tonic-gate 	 * If no update required, discard newstat
13757c478bd9Sstevel@tonic-gate 	 */
13767c478bd9Sstevel@tonic-gate 	if (!walk_arg.need_update) {
13777c478bd9Sstevel@tonic-gate 		clear_walk_args();
13787c478bd9Sstevel@tonic-gate 		return (0);
13797c478bd9Sstevel@tonic-gate 	}
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	/*
13827c478bd9Sstevel@tonic-gate 	 * At this point we need an update - so save new stat data
13837c478bd9Sstevel@tonic-gate 	 * However, if only checking (-n), don't save new stat data.
13847c478bd9Sstevel@tonic-gate 	 */
13857c478bd9Sstevel@tonic-gate 	if (!bam_check)
13867c478bd9Sstevel@tonic-gate 		savenew(root);
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	clear_walk_args();
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	return (1);
13917c478bd9Sstevel@tonic-gate }
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate static error_t
13947c478bd9Sstevel@tonic-gate create_ramdisk(char *root)
13957c478bd9Sstevel@tonic-gate {
13967c478bd9Sstevel@tonic-gate 	char *cmdline, path[PATH_MAX];
13977c478bd9Sstevel@tonic-gate 	size_t len;
13987c478bd9Sstevel@tonic-gate 	struct stat sb;
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	/*
14017c478bd9Sstevel@tonic-gate 	 * Setup command args for create_ramdisk.ksh
14027c478bd9Sstevel@tonic-gate 	 */
14037c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, CREATE_RAMDISK);
14047c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
14057c478bd9Sstevel@tonic-gate 		bam_error(ARCH_EXEC_MISS, path, strerror(errno));
14067c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
14077c478bd9Sstevel@tonic-gate 	}
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	len = strlen(path) + strlen(root) + 10;	/* room for space + -R */
14107c478bd9Sstevel@tonic-gate 	cmdline = s_calloc(1, len);
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	if (strlen(root) > 1) {
14137c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s -R %s", path, root);
14147c478bd9Sstevel@tonic-gate 		/* chop off / at the end */
14157c478bd9Sstevel@tonic-gate 		cmdline[strlen(cmdline) - 1] = '\0';
14167c478bd9Sstevel@tonic-gate 	} else
14177c478bd9Sstevel@tonic-gate 		(void) snprintf(cmdline, len, "%s", path);
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	if (exec_cmd(cmdline, NULL, 0) != 0) {
14207c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_FAIL, cmdline);
14217c478bd9Sstevel@tonic-gate 		free(cmdline);
14227c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
14237c478bd9Sstevel@tonic-gate 	}
14247c478bd9Sstevel@tonic-gate 	free(cmdline);
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	/*
14277c478bd9Sstevel@tonic-gate 	 * Verify that the archive has been created
14287c478bd9Sstevel@tonic-gate 	 */
14297c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, BOOT_ARCHIVE);
14307c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) != 0) {
14317c478bd9Sstevel@tonic-gate 		bam_error(ARCHIVE_NOT_CREATED, path);
14327c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
14337c478bd9Sstevel@tonic-gate 	}
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
14367c478bd9Sstevel@tonic-gate }
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate /*
14397c478bd9Sstevel@tonic-gate  * Checks if target filesystem is on a ramdisk
14407c478bd9Sstevel@tonic-gate  * 1 - is miniroot
14417c478bd9Sstevel@tonic-gate  * 0 - is not
14427c478bd9Sstevel@tonic-gate  * When in doubt assume it is not a ramdisk.
14437c478bd9Sstevel@tonic-gate  */
14447c478bd9Sstevel@tonic-gate static int
14457c478bd9Sstevel@tonic-gate is_ramdisk(char *root)
14467c478bd9Sstevel@tonic-gate {
14477c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
14487c478bd9Sstevel@tonic-gate 	FILE *fp;
14497c478bd9Sstevel@tonic-gate 	int found;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	/*
14527c478bd9Sstevel@tonic-gate 	 * There are 3 situations where creating archive is
14537c478bd9Sstevel@tonic-gate 	 * of dubious value:
14547c478bd9Sstevel@tonic-gate 	 *	- create boot_archive on a boot_archive
14557c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk which is the root filesystem
14567c478bd9Sstevel@tonic-gate 	 *	- create it on a ramdisk mounted somewhere else
14577c478bd9Sstevel@tonic-gate 	 * The first is not easy to detect and checking for it is not
14587c478bd9Sstevel@tonic-gate 	 * worth it.
14597c478bd9Sstevel@tonic-gate 	 * The other two conditions are handled here
14607c478bd9Sstevel@tonic-gate 	 */
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
14637c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
14647c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
14657c478bd9Sstevel@tonic-gate 		return (0);
14667c478bd9Sstevel@tonic-gate 	}
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	found = 0;
14717c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
14727c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, root) == 0) {
14737c478bd9Sstevel@tonic-gate 			found = 1;
14747c478bd9Sstevel@tonic-gate 			break;
14757c478bd9Sstevel@tonic-gate 		}
14767c478bd9Sstevel@tonic-gate 	}
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	if (!found) {
14797c478bd9Sstevel@tonic-gate 		if (bam_verbose)
14807c478bd9Sstevel@tonic-gate 			bam_error(NOT_IN_MNTTAB, root);
14817c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
14827c478bd9Sstevel@tonic-gate 		return (0);
14837c478bd9Sstevel@tonic-gate 	}
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	if (strstr(mnt.mnt_special, RAMDISK_SPECIAL) != NULL) {
14867c478bd9Sstevel@tonic-gate 		if (bam_verbose)
14877c478bd9Sstevel@tonic-gate 			bam_error(IS_RAMDISK, bam_root);
14887c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
14897c478bd9Sstevel@tonic-gate 		return (1);
14907c478bd9Sstevel@tonic-gate 	}
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	return (0);
14957c478bd9Sstevel@tonic-gate }
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate static int
14987c478bd9Sstevel@tonic-gate is_newboot(char *root)
14997c478bd9Sstevel@tonic-gate {
15007c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
15017c478bd9Sstevel@tonic-gate 	struct stat sb;
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	/*
15047c478bd9Sstevel@tonic-gate 	 * We can't boot without MULTI_BOOT
15057c478bd9Sstevel@tonic-gate 	 */
15067c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, MULTI_BOOT);
15077c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
15087c478bd9Sstevel@tonic-gate 		if (bam_verbose)
15097c478bd9Sstevel@tonic-gate 			bam_print(FILE_MISS, path);
15107c478bd9Sstevel@tonic-gate 		return (0);
15117c478bd9Sstevel@tonic-gate 	}
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	/*
15147c478bd9Sstevel@tonic-gate 	 * We can't generate archive without GRUB_DIR
15157c478bd9Sstevel@tonic-gate 	 */
15167c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", root, GRUB_DIR);
15177c478bd9Sstevel@tonic-gate 	if (stat(path, &sb) == -1) {
15187c478bd9Sstevel@tonic-gate 		if (bam_verbose)
15197c478bd9Sstevel@tonic-gate 			bam_print(DIR_MISS, path);
15207c478bd9Sstevel@tonic-gate 		return (0);
15217c478bd9Sstevel@tonic-gate 	}
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	return (1);
15247c478bd9Sstevel@tonic-gate }
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate static int
15277c478bd9Sstevel@tonic-gate is_readonly(char *root)
15287c478bd9Sstevel@tonic-gate {
15297c478bd9Sstevel@tonic-gate 	struct statvfs vfs;
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 	/*
15327c478bd9Sstevel@tonic-gate 	 * Check for RDONLY filesystem
15337c478bd9Sstevel@tonic-gate 	 * When in doubt assume it is not readonly
15347c478bd9Sstevel@tonic-gate 	 */
15357c478bd9Sstevel@tonic-gate 	if (statvfs(root, &vfs) != 0) {
15367c478bd9Sstevel@tonic-gate 		if (bam_verbose)
15377c478bd9Sstevel@tonic-gate 			bam_error(STATVFS_FAIL, root, strerror(errno));
15387c478bd9Sstevel@tonic-gate 		return (0);
15397c478bd9Sstevel@tonic-gate 	}
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	if (vfs.f_flag & ST_RDONLY) {
15427c478bd9Sstevel@tonic-gate 		return (1);
15437c478bd9Sstevel@tonic-gate 	}
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	return (0);
15467c478bd9Sstevel@tonic-gate }
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate static error_t
15497c478bd9Sstevel@tonic-gate update_archive(char *root, char *opt)
15507c478bd9Sstevel@tonic-gate {
15517c478bd9Sstevel@tonic-gate 	error_t ret;
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	assert(root);
15547c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	/*
15577c478bd9Sstevel@tonic-gate 	 * root must belong to a newboot OS,
15587c478bd9Sstevel@tonic-gate 	 * don't care on sparc except for diskless clients
15597c478bd9Sstevel@tonic-gate 	 */
15607c478bd9Sstevel@tonic-gate 	if (!is_newboot(root)) {
15617c478bd9Sstevel@tonic-gate 		if (bam_verbose)
15627c478bd9Sstevel@tonic-gate 			bam_print(NOT_NEWBOOT);
15637c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
15647c478bd9Sstevel@tonic-gate 	}
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	/*
15677c478bd9Sstevel@tonic-gate 	 * root must be writable
15687c478bd9Sstevel@tonic-gate 	 * Note: statvfs() does not always report the truth
15697c478bd9Sstevel@tonic-gate 	 */
15707c478bd9Sstevel@tonic-gate 	if (is_readonly(root)) {
15717c478bd9Sstevel@tonic-gate 		if (!bam_smf_check && bam_verbose)
15727c478bd9Sstevel@tonic-gate 			bam_print(RDONLY_FS, root);
15737c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
15747c478bd9Sstevel@tonic-gate 	}
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 	/*
15777c478bd9Sstevel@tonic-gate 	 * Don't generate archive on ramdisk
15787c478bd9Sstevel@tonic-gate 	 */
15797c478bd9Sstevel@tonic-gate 	if (is_ramdisk(root)) {
15807c478bd9Sstevel@tonic-gate 		if (bam_verbose)
15817c478bd9Sstevel@tonic-gate 			bam_print(SKIP_RAMDISK);
15827c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
15837c478bd9Sstevel@tonic-gate 	}
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 	/*
15867c478bd9Sstevel@tonic-gate 	 * Now check if updated is really needed
15877c478bd9Sstevel@tonic-gate 	 */
15887c478bd9Sstevel@tonic-gate 	ret = update_required(root);
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	/*
15917c478bd9Sstevel@tonic-gate 	 * The check command (-n) is *not* a dry run
15927c478bd9Sstevel@tonic-gate 	 * It only checks if the archive is in sync.
15937c478bd9Sstevel@tonic-gate 	 */
15947c478bd9Sstevel@tonic-gate 	if (bam_check) {
15957c478bd9Sstevel@tonic-gate 		bam_exit((ret != 0) ? 1 : 0);
15967c478bd9Sstevel@tonic-gate 	}
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	if (ret == 1) {
15997c478bd9Sstevel@tonic-gate 		/* create the ramdisk */
16007c478bd9Sstevel@tonic-gate 		ret = create_ramdisk(root);
16017c478bd9Sstevel@tonic-gate 	}
16027c478bd9Sstevel@tonic-gate 	return (ret);
16037c478bd9Sstevel@tonic-gate }
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate static error_t
16067c478bd9Sstevel@tonic-gate update_all(char *root, char *opt)
16077c478bd9Sstevel@tonic-gate {
16087c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
16097c478bd9Sstevel@tonic-gate 	struct stat sb;
16107c478bd9Sstevel@tonic-gate 	FILE *fp;
16117c478bd9Sstevel@tonic-gate 	char multibt[PATH_MAX];
16127c478bd9Sstevel@tonic-gate 	error_t ret = BAM_SUCCESS;
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	assert(bam_rootlen == 1 && root[0] == '/');
16157c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	/*
16187c478bd9Sstevel@tonic-gate 	 * First update archive for current root
16197c478bd9Sstevel@tonic-gate 	 */
16207c478bd9Sstevel@tonic-gate 	if (update_archive(root, opt) != BAM_SUCCESS)
16217c478bd9Sstevel@tonic-gate 		ret = BAM_ERROR;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	/*
16247c478bd9Sstevel@tonic-gate 	 * Now walk the mount table, performing archive update
16257c478bd9Sstevel@tonic-gate 	 * for all mounted Newboot root filesystems
16267c478bd9Sstevel@tonic-gate 	 */
16277c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
16287c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
16297c478bd9Sstevel@tonic-gate 		bam_error(OPEN_FAIL, MNTTAB, strerror(errno));
16307c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
16317c478bd9Sstevel@tonic-gate 	}
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (mnt)) == 0) {
16367c478bd9Sstevel@tonic-gate 		if (mnt.mnt_special == NULL)
16377c478bd9Sstevel@tonic-gate 			continue;
16387c478bd9Sstevel@tonic-gate 		if (strncmp(mnt.mnt_special, "/dev/", strlen("/dev/")) != 0)
16397c478bd9Sstevel@tonic-gate 			continue;
16407c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, "/") == 0)
16417c478bd9Sstevel@tonic-gate 			continue;
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 		(void) snprintf(multibt, sizeof (multibt), "%s%s",
16447c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp, MULTI_BOOT);
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 		if (stat(multibt, &sb) == -1)
16477c478bd9Sstevel@tonic-gate 			continue;
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 		/*
16507c478bd9Sstevel@tonic-gate 		 * We put a trailing slash to be consistent with root = "/"
16517c478bd9Sstevel@tonic-gate 		 * case, such that we don't have to print // in some cases.
16527c478bd9Sstevel@tonic-gate 		 */
16537c478bd9Sstevel@tonic-gate 		(void) snprintf(rootbuf, sizeof (rootbuf), "%s/",
16547c478bd9Sstevel@tonic-gate 		    mnt.mnt_mountp);
16557c478bd9Sstevel@tonic-gate 		bam_rootlen = strlen(rootbuf);
16567c478bd9Sstevel@tonic-gate 		if (update_archive(rootbuf, opt) != BAM_SUCCESS)
16577c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
16587c478bd9Sstevel@tonic-gate 	}
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	return (ret);
16637c478bd9Sstevel@tonic-gate }
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate static void
16667c478bd9Sstevel@tonic-gate append_line(menu_t *mp, line_t *lp)
16677c478bd9Sstevel@tonic-gate {
16687c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
16697c478bd9Sstevel@tonic-gate 		mp->start = lp;
16707c478bd9Sstevel@tonic-gate 	} else {
16717c478bd9Sstevel@tonic-gate 		mp->end->next = lp;
16727c478bd9Sstevel@tonic-gate 	}
16737c478bd9Sstevel@tonic-gate 	mp->end = lp;
16747c478bd9Sstevel@tonic-gate }
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate /*
16777c478bd9Sstevel@tonic-gate  * A line in menu.lst looks like
16787c478bd9Sstevel@tonic-gate  * [ ]*<cmd>[ \t=]*<arg>*
16797c478bd9Sstevel@tonic-gate  */
16807c478bd9Sstevel@tonic-gate static void
16817c478bd9Sstevel@tonic-gate line_parser(menu_t *mp, char *str, int *lineNum, int *entryNum)
16827c478bd9Sstevel@tonic-gate {
16837c478bd9Sstevel@tonic-gate 	/*
16847c478bd9Sstevel@tonic-gate 	 * save state across calls. This is so that
16857c478bd9Sstevel@tonic-gate 	 * header gets the right entry# after title has
16867c478bd9Sstevel@tonic-gate 	 * been processed
16877c478bd9Sstevel@tonic-gate 	 */
16887c478bd9Sstevel@tonic-gate 	static line_t *prev;
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	line_t	*lp;
16917c478bd9Sstevel@tonic-gate 	char *cmd, *sep, *arg;
16927c478bd9Sstevel@tonic-gate 	char save, *cp, *line;
16937c478bd9Sstevel@tonic-gate 	menu_flag_t flag = BAM_INVALID;
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate 	if (str == NULL) {
16967c478bd9Sstevel@tonic-gate 		return;
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	/*
17007c478bd9Sstevel@tonic-gate 	 * First save a copy of the entire line.
17017c478bd9Sstevel@tonic-gate 	 * We use this later to set the line field.
17027c478bd9Sstevel@tonic-gate 	 */
17037c478bd9Sstevel@tonic-gate 	line = s_strdup(str);
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	/* Eat up leading whitespace */
17067c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
17077c478bd9Sstevel@tonic-gate 		str++;
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	if (*str == '#') {		/* comment */
17107c478bd9Sstevel@tonic-gate 		cmd = s_strdup("#");
17117c478bd9Sstevel@tonic-gate 		sep = NULL;
17127c478bd9Sstevel@tonic-gate 		arg = s_strdup(str + 1);
17137c478bd9Sstevel@tonic-gate 		flag = BAM_COMMENT;
17147c478bd9Sstevel@tonic-gate 	} else if (*str == '\0') {	/* blank line */
17157c478bd9Sstevel@tonic-gate 		cmd = sep = arg = NULL;
17167c478bd9Sstevel@tonic-gate 		flag = BAM_EMPTY;
17177c478bd9Sstevel@tonic-gate 	} else {
17187c478bd9Sstevel@tonic-gate 		/*
17197c478bd9Sstevel@tonic-gate 		 * '=' is not a documented separator in grub syntax.
17207c478bd9Sstevel@tonic-gate 		 * However various development bits use '=' as a
17217c478bd9Sstevel@tonic-gate 		 * separator. In addition, external users also
17227c478bd9Sstevel@tonic-gate 		 * use = as a separator. So we will allow that usage.
17237c478bd9Sstevel@tonic-gate 		 */
17247c478bd9Sstevel@tonic-gate 		cp = str;
17257c478bd9Sstevel@tonic-gate 		while (*str != ' ' && *str != '\t' && *str != '=') {
17267c478bd9Sstevel@tonic-gate 			if (*str == '\0') {
17277c478bd9Sstevel@tonic-gate 				cmd = s_strdup(cp);
17287c478bd9Sstevel@tonic-gate 				sep = arg = NULL;
17297c478bd9Sstevel@tonic-gate 				break;
17307c478bd9Sstevel@tonic-gate 			}
17317c478bd9Sstevel@tonic-gate 			str++;
17327c478bd9Sstevel@tonic-gate 		}
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 		if (*str != '\0') {
17357c478bd9Sstevel@tonic-gate 			save = *str;
17367c478bd9Sstevel@tonic-gate 			*str = '\0';
17377c478bd9Sstevel@tonic-gate 			cmd = s_strdup(cp);
17387c478bd9Sstevel@tonic-gate 			*str = save;
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 			str++;
17417c478bd9Sstevel@tonic-gate 			save = *str;
17427c478bd9Sstevel@tonic-gate 			*str = '\0';
17437c478bd9Sstevel@tonic-gate 			sep = s_strdup(str - 1);
17447c478bd9Sstevel@tonic-gate 			*str = save;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 			while (*str == ' ' || *str == '\t')
17477c478bd9Sstevel@tonic-gate 				str++;
17487c478bd9Sstevel@tonic-gate 			if (*str == '\0')
17497c478bd9Sstevel@tonic-gate 				arg = NULL;
17507c478bd9Sstevel@tonic-gate 			else
17517c478bd9Sstevel@tonic-gate 				arg = s_strdup(str);
17527c478bd9Sstevel@tonic-gate 		}
17537c478bd9Sstevel@tonic-gate 	}
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	lp->cmd = cmd;
17587c478bd9Sstevel@tonic-gate 	lp->sep = sep;
17597c478bd9Sstevel@tonic-gate 	lp->arg = arg;
17607c478bd9Sstevel@tonic-gate 	lp->line = line;
17617c478bd9Sstevel@tonic-gate 	lp->lineNum = ++(*lineNum);
17627c478bd9Sstevel@tonic-gate 	if (cmd && strcmp(cmd, menu_cmds[TITLE_CMD]) == 0) {
17637c478bd9Sstevel@tonic-gate 		lp->entryNum = ++(*entryNum);
17647c478bd9Sstevel@tonic-gate 		lp->flags = BAM_TITLE;
17657c478bd9Sstevel@tonic-gate 		if (prev && prev->flags == BAM_COMMENT &&
17667c478bd9Sstevel@tonic-gate 		    prev->arg && strcmp(prev->arg, BAM_HDR) == 0)
17677c478bd9Sstevel@tonic-gate 			prev->entryNum = lp->entryNum;
17687c478bd9Sstevel@tonic-gate 	} else if (flag != BAM_INVALID) {
17697c478bd9Sstevel@tonic-gate 		/*
17707c478bd9Sstevel@tonic-gate 		 * For header comments, the entry# is "fixed up"
17717c478bd9Sstevel@tonic-gate 		 * by the subsequent title
17727c478bd9Sstevel@tonic-gate 		 */
17737c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
17747c478bd9Sstevel@tonic-gate 		lp->flags = flag;
17757c478bd9Sstevel@tonic-gate 	} else {
17767c478bd9Sstevel@tonic-gate 		lp->entryNum = *entryNum;
17777c478bd9Sstevel@tonic-gate 		lp->flags = (*entryNum == ENTRY_INIT) ? BAM_GLOBAL : BAM_ENTRY;
17787c478bd9Sstevel@tonic-gate 	}
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	append_line(mp, lp);
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	prev = lp;
17837c478bd9Sstevel@tonic-gate }
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate static menu_t *
17867c478bd9Sstevel@tonic-gate menu_read(char *menu_path)
17877c478bd9Sstevel@tonic-gate {
17887c478bd9Sstevel@tonic-gate 	FILE *fp;
17897c478bd9Sstevel@tonic-gate 	char buf[BAM_MAXLINE], *cp;
17907c478bd9Sstevel@tonic-gate 	menu_t *mp;
17917c478bd9Sstevel@tonic-gate 	int line, entry, len, n;
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	mp = s_calloc(1, sizeof (menu_t));
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	fp = fopen(menu_path, "r");
17967c478bd9Sstevel@tonic-gate 	if (fp == NULL) { /* Let the caller handle this error */
17977c478bd9Sstevel@tonic-gate 		return (mp);
17987c478bd9Sstevel@tonic-gate 	}
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	/* Note: GRUB boot entry number starts with 0 */
18027c478bd9Sstevel@tonic-gate 	line = LINE_INIT;
18037c478bd9Sstevel@tonic-gate 	entry = ENTRY_INIT;
18047c478bd9Sstevel@tonic-gate 	cp = buf;
18057c478bd9Sstevel@tonic-gate 	len = sizeof (buf);
18067c478bd9Sstevel@tonic-gate 	while (s_fgets(cp, len, fp) != NULL) {
18077c478bd9Sstevel@tonic-gate 		n = strlen(cp);
18087c478bd9Sstevel@tonic-gate 		if (cp[n - 1] == '\\') {
18097c478bd9Sstevel@tonic-gate 			len -= n - 1;
18107c478bd9Sstevel@tonic-gate 			assert(len >= 2);
18117c478bd9Sstevel@tonic-gate 			cp += n - 1;
18127c478bd9Sstevel@tonic-gate 			continue;
18137c478bd9Sstevel@tonic-gate 		}
18147c478bd9Sstevel@tonic-gate 		line_parser(mp, buf, &line, &entry);
18157c478bd9Sstevel@tonic-gate 		cp = buf;
18167c478bd9Sstevel@tonic-gate 		len = sizeof (buf);
18177c478bd9Sstevel@tonic-gate 	}
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
18207c478bd9Sstevel@tonic-gate 		bam_error(CLOSE_FAIL, menu_path, strerror(errno));
18217c478bd9Sstevel@tonic-gate 	}
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	return (mp);
18247c478bd9Sstevel@tonic-gate }
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate static error_t
18277c478bd9Sstevel@tonic-gate selector(menu_t *mp, char *opt, int *entry, char **title)
18287c478bd9Sstevel@tonic-gate {
18297c478bd9Sstevel@tonic-gate 	char *eq;
18307c478bd9Sstevel@tonic-gate 	char *opt_dup;
18317c478bd9Sstevel@tonic-gate 	int entryNum;
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	assert(mp);
18347c478bd9Sstevel@tonic-gate 	assert(mp->start);
18357c478bd9Sstevel@tonic-gate 	assert(opt);
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	opt_dup = s_strdup(opt);
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	if (entry)
18407c478bd9Sstevel@tonic-gate 		*entry = ENTRY_INIT;
18417c478bd9Sstevel@tonic-gate 	if (title)
18427c478bd9Sstevel@tonic-gate 		*title = NULL;
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	eq = strchr(opt_dup, '=');
18457c478bd9Sstevel@tonic-gate 	if (eq == NULL) {
18467c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
18477c478bd9Sstevel@tonic-gate 		free(opt_dup);
18487c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18497c478bd9Sstevel@tonic-gate 	}
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	*eq = '\0';
18527c478bd9Sstevel@tonic-gate 	if (entry && strcmp(opt_dup, OPT_ENTRY_NUM) == 0) {
18537c478bd9Sstevel@tonic-gate 		assert(mp->end);
18547c478bd9Sstevel@tonic-gate 		entryNum = s_strtol(eq + 1);
18557c478bd9Sstevel@tonic-gate 		if (entryNum < 0 || entryNum > mp->end->entryNum) {
18567c478bd9Sstevel@tonic-gate 			bam_error(INVALID_ENTRY, eq + 1);
18577c478bd9Sstevel@tonic-gate 			free(opt_dup);
18587c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
18597c478bd9Sstevel@tonic-gate 		}
18607c478bd9Sstevel@tonic-gate 		*entry = entryNum;
18617c478bd9Sstevel@tonic-gate 	} else if (title && strcmp(opt_dup, menu_cmds[TITLE_CMD]) == 0) {
18627c478bd9Sstevel@tonic-gate 		*title = opt + (eq - opt_dup) + 1;
18637c478bd9Sstevel@tonic-gate 	} else {
18647c478bd9Sstevel@tonic-gate 		bam_error(INVALID_OPT, opt);
18657c478bd9Sstevel@tonic-gate 		free(opt_dup);
18667c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18677c478bd9Sstevel@tonic-gate 	}
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	free(opt_dup);
18707c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
18717c478bd9Sstevel@tonic-gate }
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate /*
18747c478bd9Sstevel@tonic-gate  * If invoked with no titles/entries (opt == NULL)
18757c478bd9Sstevel@tonic-gate  * only title lines in file are printed.
18767c478bd9Sstevel@tonic-gate  *
18777c478bd9Sstevel@tonic-gate  * If invoked with a title or entry #, all
18787c478bd9Sstevel@tonic-gate  * lines in *every* matching entry are listed
18797c478bd9Sstevel@tonic-gate  */
18807c478bd9Sstevel@tonic-gate static error_t
18817c478bd9Sstevel@tonic-gate list_entry(menu_t *mp, char *menu_path, char *opt)
18827c478bd9Sstevel@tonic-gate {
18837c478bd9Sstevel@tonic-gate 	line_t *lp;
18847c478bd9Sstevel@tonic-gate 	int entry = ENTRY_INIT;
18857c478bd9Sstevel@tonic-gate 	int found;
18867c478bd9Sstevel@tonic-gate 	char *title = NULL;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	assert(mp);
18897c478bd9Sstevel@tonic-gate 	assert(menu_path);
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
18927c478bd9Sstevel@tonic-gate 		bam_error(NO_MENU, menu_path);
18937c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
18947c478bd9Sstevel@tonic-gate 	}
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	if (opt != NULL) {
18977c478bd9Sstevel@tonic-gate 		if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
18987c478bd9Sstevel@tonic-gate 			return (BAM_ERROR);
18997c478bd9Sstevel@tonic-gate 		}
19007c478bd9Sstevel@tonic-gate 		assert((entry != ENTRY_INIT) ^ (title != NULL));
19017c478bd9Sstevel@tonic-gate 	} else {
19027c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[DEFAULT_CMD], 0);
19037c478bd9Sstevel@tonic-gate 		(void) read_globals(mp, menu_path, menu_cmds[TIMEOUT_CMD], 0);
19047c478bd9Sstevel@tonic-gate 	}
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	found = 0;
19077c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
19087c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT || lp->flags == BAM_EMPTY)
19097c478bd9Sstevel@tonic-gate 			continue;
19107c478bd9Sstevel@tonic-gate 		if (opt == NULL && lp->flags == BAM_TITLE) {
19117c478bd9Sstevel@tonic-gate 			bam_print(PRINT_TITLE, lp->entryNum,
19127c478bd9Sstevel@tonic-gate 			    lp->arg);
19137c478bd9Sstevel@tonic-gate 			found = 1;
19147c478bd9Sstevel@tonic-gate 			continue;
19157c478bd9Sstevel@tonic-gate 		}
19167c478bd9Sstevel@tonic-gate 		if (entry != ENTRY_INIT && lp->entryNum == entry) {
19177c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
19187c478bd9Sstevel@tonic-gate 			found = 1;
19197c478bd9Sstevel@tonic-gate 			continue;
19207c478bd9Sstevel@tonic-gate 		}
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 		/*
19237c478bd9Sstevel@tonic-gate 		 * We set the entry value here so that all lines
19247c478bd9Sstevel@tonic-gate 		 * in entry get printed. If we subsequently match
19257c478bd9Sstevel@tonic-gate 		 * title in other entries, all lines in those
19267c478bd9Sstevel@tonic-gate 		 * entries get printed as well.
19277c478bd9Sstevel@tonic-gate 		 */
19287c478bd9Sstevel@tonic-gate 		if (title && lp->flags == BAM_TITLE && lp->arg &&
19297c478bd9Sstevel@tonic-gate 		    strncmp(title, lp->arg, strlen(title)) == 0) {
19307c478bd9Sstevel@tonic-gate 			bam_print(PRINT, lp->line);
19317c478bd9Sstevel@tonic-gate 			entry = lp->entryNum;
19327c478bd9Sstevel@tonic-gate 			found = 1;
19337c478bd9Sstevel@tonic-gate 			continue;
19347c478bd9Sstevel@tonic-gate 		}
19357c478bd9Sstevel@tonic-gate 	}
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 	if (!found) {
19387c478bd9Sstevel@tonic-gate 		bam_error(NO_MATCH_ENTRY);
19397c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
19407c478bd9Sstevel@tonic-gate 	}
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
19437c478bd9Sstevel@tonic-gate }
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate static int
19467c478bd9Sstevel@tonic-gate add_boot_entry(menu_t *mp,
19477c478bd9Sstevel@tonic-gate 	char *title,
19487c478bd9Sstevel@tonic-gate 	char *root,
19497c478bd9Sstevel@tonic-gate 	char *kernel,
19507c478bd9Sstevel@tonic-gate 	char *module)
19517c478bd9Sstevel@tonic-gate {
19527c478bd9Sstevel@tonic-gate 	menu_t dummy;
19537c478bd9Sstevel@tonic-gate 	int lineNum, entryNum;
19547c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	assert(mp);
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	if (title == NULL) {
19597c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[TITLE_CMD]);
19607c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
19617c478bd9Sstevel@tonic-gate 	}
19627c478bd9Sstevel@tonic-gate 	if (root == NULL) {
19637c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[ROOT_CMD]);
19647c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
19657c478bd9Sstevel@tonic-gate 	}
19667c478bd9Sstevel@tonic-gate 	if (kernel == NULL) {
19677c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[KERNEL_CMD]);
19687c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
19697c478bd9Sstevel@tonic-gate 	}
19707c478bd9Sstevel@tonic-gate 	if (module == NULL) {
19717c478bd9Sstevel@tonic-gate 		bam_error(SUBOPT_MISS, menu_cmds[MODULE_CMD]);
19727c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
19737c478bd9Sstevel@tonic-gate 	}
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 	if (mp->start) {
19767c478bd9Sstevel@tonic-gate 		lineNum = mp->end->lineNum;
19777c478bd9Sstevel@tonic-gate 		entryNum = mp->end->entryNum;
19787c478bd9Sstevel@tonic-gate 	} else {
19797c478bd9Sstevel@tonic-gate 		lineNum = LINE_INIT;
19807c478bd9Sstevel@tonic-gate 		entryNum = ENTRY_INIT;
19817c478bd9Sstevel@tonic-gate 	}
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	/*
19847c478bd9Sstevel@tonic-gate 	 * No separator for comment (HDR/FTR) commands
19857c478bd9Sstevel@tonic-gate 	 * The syntax for comments is #<comment>
19867c478bd9Sstevel@tonic-gate 	 */
19877c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
19887c478bd9Sstevel@tonic-gate 	    menu_cmds[COMMENT_CMD], BAM_HDR);
19897c478bd9Sstevel@tonic-gate 	dummy.start = dummy.end = NULL;
19907c478bd9Sstevel@tonic-gate 	line_parser(&dummy, linebuf, &lineNum, &entryNum);
19917c478bd9Sstevel@tonic-gate 	if (dummy.start == NULL || dummy.start->flags != BAM_COMMENT) {
19927c478bd9Sstevel@tonic-gate 		line_free(dummy.start);
19937c478bd9Sstevel@tonic-gate 		bam_error(INVALID_HDR, BAM_HDR);
19947c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
19957c478bd9Sstevel@tonic-gate 	}
19967c478bd9Sstevel@tonic-gate 	assert(dummy.start == dummy.end);
19977c478bd9Sstevel@tonic-gate 	append_line(mp, dummy.start);
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
20007c478bd9Sstevel@tonic-gate 	    menu_cmds[TITLE_CMD], menu_cmds[SEP_CMD], title);
20017c478bd9Sstevel@tonic-gate 	dummy.start = dummy.end = NULL;
20027c478bd9Sstevel@tonic-gate 	line_parser(&dummy, linebuf, &lineNum, &entryNum);
20037c478bd9Sstevel@tonic-gate 	if (dummy.start == NULL || dummy.start->flags != BAM_TITLE) {
20047c478bd9Sstevel@tonic-gate 		line_free(dummy.start);
20057c478bd9Sstevel@tonic-gate 		bam_error(INVALID_TITLE, title);
20067c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
20077c478bd9Sstevel@tonic-gate 	}
20087c478bd9Sstevel@tonic-gate 	assert(dummy.start == dummy.end);
20097c478bd9Sstevel@tonic-gate 	append_line(mp, dummy.start);
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
20137c478bd9Sstevel@tonic-gate 	    menu_cmds[ROOT_CMD], menu_cmds[SEP_CMD], root);
20147c478bd9Sstevel@tonic-gate 	dummy.start = dummy.end = NULL;
20157c478bd9Sstevel@tonic-gate 	line_parser(&dummy, linebuf, &lineNum, &entryNum);
20167c478bd9Sstevel@tonic-gate 	if (dummy.start == NULL || dummy.start->flags != BAM_ENTRY) {
20177c478bd9Sstevel@tonic-gate 		line_free(dummy.start);
20187c478bd9Sstevel@tonic-gate 		bam_error(INVALID_ROOT, root);
20197c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
20207c478bd9Sstevel@tonic-gate 	}
20217c478bd9Sstevel@tonic-gate 	assert(dummy.start == dummy.end);
20227c478bd9Sstevel@tonic-gate 	append_line(mp, dummy.start);
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
20267c478bd9Sstevel@tonic-gate 	    menu_cmds[KERNEL_CMD], menu_cmds[SEP_CMD], kernel);
20277c478bd9Sstevel@tonic-gate 	dummy.start = dummy.end = NULL;
20287c478bd9Sstevel@tonic-gate 	line_parser(&dummy, linebuf, &lineNum, &entryNum);
20297c478bd9Sstevel@tonic-gate 	if (dummy.start == NULL || dummy.start->flags != BAM_ENTRY) {
20307c478bd9Sstevel@tonic-gate 		line_free(dummy.start);
20317c478bd9Sstevel@tonic-gate 		bam_error(INVALID_KERNEL, kernel);
20327c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
20337c478bd9Sstevel@tonic-gate 	}
20347c478bd9Sstevel@tonic-gate 	assert(dummy.start == dummy.end);
20357c478bd9Sstevel@tonic-gate 	append_line(mp, dummy.start);
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s%s",
20387c478bd9Sstevel@tonic-gate 	    menu_cmds[MODULE_CMD], menu_cmds[SEP_CMD], module);
20397c478bd9Sstevel@tonic-gate 	dummy.start = dummy.end = NULL;
20407c478bd9Sstevel@tonic-gate 	line_parser(&dummy, linebuf, &lineNum, &entryNum);
20417c478bd9Sstevel@tonic-gate 	if (dummy.start == NULL || dummy.start->flags != BAM_ENTRY) {
20427c478bd9Sstevel@tonic-gate 		line_free(dummy.start);
20437c478bd9Sstevel@tonic-gate 		bam_error(INVALID_MODULE, module);
20447c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
20457c478bd9Sstevel@tonic-gate 	}
20467c478bd9Sstevel@tonic-gate 	assert(dummy.start == dummy.end);
20477c478bd9Sstevel@tonic-gate 	append_line(mp, dummy.start);
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s%s",
20507c478bd9Sstevel@tonic-gate 	    menu_cmds[COMMENT_CMD], BAM_FTR);
20517c478bd9Sstevel@tonic-gate 	dummy.start = dummy.end = NULL;
20527c478bd9Sstevel@tonic-gate 	line_parser(&dummy, linebuf, &lineNum, &entryNum);
20537c478bd9Sstevel@tonic-gate 	if (dummy.start == NULL || dummy.start->flags != BAM_COMMENT) {
20547c478bd9Sstevel@tonic-gate 		line_free(dummy.start);
20557c478bd9Sstevel@tonic-gate 		bam_error(INVALID_FOOTER, BAM_FTR);
20567c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
20577c478bd9Sstevel@tonic-gate 	}
20587c478bd9Sstevel@tonic-gate 	assert(dummy.start == dummy.end);
20597c478bd9Sstevel@tonic-gate 	append_line(mp, dummy.start);
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 	return (entryNum);
20627c478bd9Sstevel@tonic-gate }
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate static error_t
20657c478bd9Sstevel@tonic-gate do_delete(menu_t *mp, int entryNum)
20667c478bd9Sstevel@tonic-gate {
20677c478bd9Sstevel@tonic-gate 	int bootadm_entry = 0;
20687c478bd9Sstevel@tonic-gate 	line_t *lp, *prev, *save;
20697c478bd9Sstevel@tonic-gate 	int deleted;
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	assert(entryNum != ENTRY_INIT);
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate 	deleted = 0;
20747c478bd9Sstevel@tonic-gate 	prev = NULL;
20757c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; ) {
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 		if (lp->entryNum == ENTRY_INIT) {
20787c478bd9Sstevel@tonic-gate 			prev = lp;
20797c478bd9Sstevel@tonic-gate 			lp = lp->next;
20807c478bd9Sstevel@tonic-gate 			continue;
20817c478bd9Sstevel@tonic-gate 		}
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 		if (entryNum != ALL_ENTRIES && lp->entryNum != entryNum) {
20847c478bd9Sstevel@tonic-gate 			prev = lp;
20857c478bd9Sstevel@tonic-gate 			lp = lp->next;
20867c478bd9Sstevel@tonic-gate 			continue;
20877c478bd9Sstevel@tonic-gate 		}
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 		/*
20907c478bd9Sstevel@tonic-gate 		 * can only delete bootadm entries
20917c478bd9Sstevel@tonic-gate 		 */
20927c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_HDR) == 0) {
20937c478bd9Sstevel@tonic-gate 			bootadm_entry = 1;
20947c478bd9Sstevel@tonic-gate 		}
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 		if (!bootadm_entry) {
20977c478bd9Sstevel@tonic-gate 			prev = lp;
20987c478bd9Sstevel@tonic-gate 			lp = lp->next;
20997c478bd9Sstevel@tonic-gate 			continue;
21007c478bd9Sstevel@tonic-gate 		}
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_COMMENT && strcmp(lp->arg, BAM_FTR) == 0)
21037c478bd9Sstevel@tonic-gate 			bootadm_entry = 0;
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 		if (prev == NULL)
21067c478bd9Sstevel@tonic-gate 			mp->start = lp->next;
21077c478bd9Sstevel@tonic-gate 		else
21087c478bd9Sstevel@tonic-gate 			prev->next = lp->next;
21097c478bd9Sstevel@tonic-gate 		if (mp->end == lp)
21107c478bd9Sstevel@tonic-gate 			mp->end = prev;
21117c478bd9Sstevel@tonic-gate 		save = lp->next;
21127c478bd9Sstevel@tonic-gate 		line_free(lp);
21137c478bd9Sstevel@tonic-gate 		lp = save;	/* prev stays the same */
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 		deleted = 1;
21167c478bd9Sstevel@tonic-gate 	}
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 	if (!deleted && entryNum != ALL_ENTRIES) {
21197c478bd9Sstevel@tonic-gate 		bam_error(NO_BOOTADM_MATCH);
21207c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
21217c478bd9Sstevel@tonic-gate 	}
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	return (BAM_SUCCESS);
21247c478bd9Sstevel@tonic-gate }
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate static error_t
21277c478bd9Sstevel@tonic-gate delete_entry(menu_t *mp, char *menu_path, char *opt)
21287c478bd9Sstevel@tonic-gate {
21297c478bd9Sstevel@tonic-gate 	int entry = ENTRY_INIT;
21307c478bd9Sstevel@tonic-gate 	char *title = NULL;
21317c478bd9Sstevel@tonic-gate 	line_t *lp;
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 	assert(mp);
21347c478bd9Sstevel@tonic-gate 	assert(opt);
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 	/*
21377c478bd9Sstevel@tonic-gate 	 * Do a quick check. If the file is empty
21387c478bd9Sstevel@tonic-gate 	 * we have nothing to delete
21397c478bd9Sstevel@tonic-gate 	 */
21407c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
21417c478bd9Sstevel@tonic-gate 		bam_print(EMPTY_FILE, menu_path);
21427c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
21437c478bd9Sstevel@tonic-gate 	}
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 	if (selector(mp, opt, &entry, &title) != BAM_SUCCESS) {
21467c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
21477c478bd9Sstevel@tonic-gate 	}
21487c478bd9Sstevel@tonic-gate 	assert((entry != ENTRY_INIT) ^ (title != NULL));
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
21517c478bd9Sstevel@tonic-gate 		if (entry != ENTRY_INIT)
21527c478bd9Sstevel@tonic-gate 			break;
21537c478bd9Sstevel@tonic-gate 		assert(title);
21547c478bd9Sstevel@tonic-gate 		if (lp->flags == BAM_TITLE &&
21557c478bd9Sstevel@tonic-gate 		    lp->arg && strcmp(lp->arg, title) == 0) {
21567c478bd9Sstevel@tonic-gate 			entry = lp->entryNum;
21577c478bd9Sstevel@tonic-gate 			break;
21587c478bd9Sstevel@tonic-gate 		}
21597c478bd9Sstevel@tonic-gate 	}
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	if (entry == ENTRY_INIT) {
21627c478bd9Sstevel@tonic-gate 		bam_error(NO_MATCH, title);
21637c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
21647c478bd9Sstevel@tonic-gate 	}
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 	if (do_delete(mp, entry) != BAM_SUCCESS) {
21677c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
21687c478bd9Sstevel@tonic-gate 	}
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
21717c478bd9Sstevel@tonic-gate }
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate static error_t
21747c478bd9Sstevel@tonic-gate delete_all_entries(menu_t *mp, char *menu_path, char *opt)
21757c478bd9Sstevel@tonic-gate {
21767c478bd9Sstevel@tonic-gate 	assert(mp);
21777c478bd9Sstevel@tonic-gate 	assert(opt == NULL);
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
21807c478bd9Sstevel@tonic-gate 		bam_print(EMPTY_FILE, menu_path);
21817c478bd9Sstevel@tonic-gate 		return (BAM_SUCCESS);
21827c478bd9Sstevel@tonic-gate 	}
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 	if (do_delete(mp, ALL_ENTRIES) != BAM_SUCCESS) {
21857c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
21867c478bd9Sstevel@tonic-gate 	}
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
21897c478bd9Sstevel@tonic-gate }
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate static FILE *
21927c478bd9Sstevel@tonic-gate open_diskmap(void)
21937c478bd9Sstevel@tonic-gate {
21947c478bd9Sstevel@tonic-gate 	FILE *fp;
21957c478bd9Sstevel@tonic-gate 	char cmd[PATH_MAX];
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 	/* make sure we have a map file */
21987c478bd9Sstevel@tonic-gate 	fp = fopen(GRUBDISK_MAP, "r");
21997c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
22007c478bd9Sstevel@tonic-gate 		(void) snprintf(cmd, sizeof (cmd),
22017c478bd9Sstevel@tonic-gate 		    "%s > /dev/null", CREATE_DISKMAP);
22027c478bd9Sstevel@tonic-gate 		(void) system(cmd);
22037c478bd9Sstevel@tonic-gate 		fp = fopen(GRUBDISK_MAP, "r");
22047c478bd9Sstevel@tonic-gate 	}
22057c478bd9Sstevel@tonic-gate 	return (fp);
22067c478bd9Sstevel@tonic-gate }
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate #define	SECTOR_SIZE	512
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate static int
22117c478bd9Sstevel@tonic-gate get_partition(char *device)
22127c478bd9Sstevel@tonic-gate {
22137c478bd9Sstevel@tonic-gate 	int i, fd, is_pcfs, partno = -1;
22147c478bd9Sstevel@tonic-gate 	struct mboot *mboot;
22157c478bd9Sstevel@tonic-gate 	char boot_sect[SECTOR_SIZE];
22167c478bd9Sstevel@tonic-gate 	char *wholedisk, *slice;
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate 	/* form whole disk (p0) */
22197c478bd9Sstevel@tonic-gate 	slice = device + strlen(device) - 2;
22207c478bd9Sstevel@tonic-gate 	is_pcfs = (*slice != 's');
22217c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
22227c478bd9Sstevel@tonic-gate 		*slice = '\0';
22237c478bd9Sstevel@tonic-gate 	wholedisk = s_calloc(1, strlen(device) + 3);
22247c478bd9Sstevel@tonic-gate 	(void) snprintf(wholedisk, strlen(device) + 3, "%sp0", device);
22257c478bd9Sstevel@tonic-gate 	if (!is_pcfs)
22267c478bd9Sstevel@tonic-gate 		*slice = 's';
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 	/* read boot sector */
22297c478bd9Sstevel@tonic-gate 	fd = open(wholedisk, O_RDONLY);
22307c478bd9Sstevel@tonic-gate 	free(wholedisk);
22317c478bd9Sstevel@tonic-gate 	if (fd == -1 || read(fd, boot_sect, SECTOR_SIZE) != SECTOR_SIZE) {
22327c478bd9Sstevel@tonic-gate 		return (partno);
22337c478bd9Sstevel@tonic-gate 	}
22347c478bd9Sstevel@tonic-gate 	(void) close(fd);
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 	/* parse fdisk table */
22377c478bd9Sstevel@tonic-gate 	mboot = (struct mboot *)((void *)boot_sect);
22387c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
22397c478bd9Sstevel@tonic-gate 		struct ipart *part =
22407c478bd9Sstevel@tonic-gate 		    (struct ipart *)(uintptr_t)mboot->parts + i;
22417c478bd9Sstevel@tonic-gate 		if (is_pcfs) {	/* looking for solaris boot part */
22427c478bd9Sstevel@tonic-gate 			if (part->systid == 0xbe) {
22437c478bd9Sstevel@tonic-gate 				partno = i;
22447c478bd9Sstevel@tonic-gate 				break;
22457c478bd9Sstevel@tonic-gate 			}
22467c478bd9Sstevel@tonic-gate 		} else {	/* look for solaris partition, old and new */
22477c478bd9Sstevel@tonic-gate 			if (part->systid == SUNIXOS ||
22487c478bd9Sstevel@tonic-gate 			    part->systid == SUNIXOS2) {
22497c478bd9Sstevel@tonic-gate 				partno = i;
22507c478bd9Sstevel@tonic-gate 				break;
22517c478bd9Sstevel@tonic-gate 			}
22527c478bd9Sstevel@tonic-gate 		}
22537c478bd9Sstevel@tonic-gate 	}
22547c478bd9Sstevel@tonic-gate 	return (partno);
22557c478bd9Sstevel@tonic-gate }
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate static char *
22587c478bd9Sstevel@tonic-gate get_grubdisk(char *rootdev, FILE *fp, int on_bootdev)
22597c478bd9Sstevel@tonic-gate {
22607c478bd9Sstevel@tonic-gate 	char *grubdisk;	/* (hd#,#,#) */
22617c478bd9Sstevel@tonic-gate 	char *slice;
22627c478bd9Sstevel@tonic-gate 	char *grubhd;
22637c478bd9Sstevel@tonic-gate 	int fdiskpart;
22647c478bd9Sstevel@tonic-gate 	int found = 0;
22657c478bd9Sstevel@tonic-gate 	char *devname, *ctdname = strstr(rootdev, "dsk/");
22667c478bd9Sstevel@tonic-gate 	char linebuf[PATH_MAX];
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 	if (ctdname == NULL)
22697c478bd9Sstevel@tonic-gate 		return (NULL);
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 	ctdname += strlen("dsk/");
22727c478bd9Sstevel@tonic-gate 	slice = strrchr(ctdname, 's');
22737c478bd9Sstevel@tonic-gate 	if (slice)
22747c478bd9Sstevel@tonic-gate 		*slice = '\0';
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 	rewind(fp);
22777c478bd9Sstevel@tonic-gate 	while (s_fgets(linebuf, sizeof (linebuf), fp) != NULL) {
22787c478bd9Sstevel@tonic-gate 		grubhd = strtok(linebuf, " \t\n");
22797c478bd9Sstevel@tonic-gate 		if (grubhd)
22807c478bd9Sstevel@tonic-gate 			devname = strtok(NULL, " \t\n");
22817c478bd9Sstevel@tonic-gate 		else
22827c478bd9Sstevel@tonic-gate 			devname = NULL;
22837c478bd9Sstevel@tonic-gate 		if (devname && strcmp(devname, ctdname) == 0) {
22847c478bd9Sstevel@tonic-gate 			found = 1;
22857c478bd9Sstevel@tonic-gate 			break;
22867c478bd9Sstevel@tonic-gate 		}
22877c478bd9Sstevel@tonic-gate 	}
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 	if (slice)
22907c478bd9Sstevel@tonic-gate 		*slice = 's';
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 	if (found == 0) {
22937c478bd9Sstevel@tonic-gate 		if (bam_verbose)
22947c478bd9Sstevel@tonic-gate 			bam_print(DISKMAP_FAIL_NONFATAL, rootdev);
22957c478bd9Sstevel@tonic-gate 		grubhd = "0";	/* assume disk 0 if can't match */
22967c478bd9Sstevel@tonic-gate 	}
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	fdiskpart = get_partition(rootdev);
22997c478bd9Sstevel@tonic-gate 	if (fdiskpart == -1)
23007c478bd9Sstevel@tonic-gate 		return (NULL);
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate 	grubdisk = s_calloc(1, 10);
23037c478bd9Sstevel@tonic-gate 	if (slice) {
23047c478bd9Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d,%c)",
23057c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart, slice[1] + 'a' - '0');
23067c478bd9Sstevel@tonic-gate 	} else
23077c478bd9Sstevel@tonic-gate 		(void) snprintf(grubdisk, 10, "(hd%s,%d)",
23087c478bd9Sstevel@tonic-gate 		    grubhd, fdiskpart);
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 	/* if root not on bootdev, change GRUB disk to 0 */
23117c478bd9Sstevel@tonic-gate 	if (!on_bootdev)
23127c478bd9Sstevel@tonic-gate 		grubdisk[3] = '0';
23137c478bd9Sstevel@tonic-gate 	return (grubdisk);
23147c478bd9Sstevel@tonic-gate }
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate static char *get_title(char *rootdir)
23177c478bd9Sstevel@tonic-gate {
23187c478bd9Sstevel@tonic-gate 	static char title[80];	/* from /etc/release */
23197c478bd9Sstevel@tonic-gate 	char *cp, release[PATH_MAX];
23207c478bd9Sstevel@tonic-gate 	FILE *fp;
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 	/* open the /etc/release file */
23237c478bd9Sstevel@tonic-gate 	(void) snprintf(release, sizeof (release), "%s/etc/release", rootdir);
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	fp = fopen(release, "r");
23267c478bd9Sstevel@tonic-gate 	if (fp == NULL)
23277c478bd9Sstevel@tonic-gate 		return ("Solaris");	/* default to Solaris */
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate 	while (s_fgets(title, sizeof (title), fp) != NULL) {
23307c478bd9Sstevel@tonic-gate 		cp = strstr(title, "Solaris");
23317c478bd9Sstevel@tonic-gate 		if (cp)
23327c478bd9Sstevel@tonic-gate 			break;
23337c478bd9Sstevel@tonic-gate 	}
23347c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
23357c478bd9Sstevel@tonic-gate 	return (cp);
23367c478bd9Sstevel@tonic-gate }
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate static char *
23397c478bd9Sstevel@tonic-gate get_special(char *mountp)
23407c478bd9Sstevel@tonic-gate {
23417c478bd9Sstevel@tonic-gate 	FILE *mntfp;
23427c478bd9Sstevel@tonic-gate 	struct mnttab mp = {0}, mpref = {0};
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 	mntfp = fopen(MNTTAB, "r");
23457c478bd9Sstevel@tonic-gate 	if (mntfp == NULL) {
23467c478bd9Sstevel@tonic-gate 		return (0);
23477c478bd9Sstevel@tonic-gate 	}
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 	if (*mountp == '\0')
23507c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = "/";
23517c478bd9Sstevel@tonic-gate 	else
23527c478bd9Sstevel@tonic-gate 		mpref.mnt_mountp = mountp;
23537c478bd9Sstevel@tonic-gate 	if (getmntany(mntfp, &mp, &mpref) != 0) {
23547c478bd9Sstevel@tonic-gate 		(void) fclose(mntfp);
23557c478bd9Sstevel@tonic-gate 		return (NULL);
23567c478bd9Sstevel@tonic-gate 	}
23577c478bd9Sstevel@tonic-gate 	(void) fclose(mntfp);
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 	return (s_strdup(mp.mnt_special));
23607c478bd9Sstevel@tonic-gate }
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate static char *
23637c478bd9Sstevel@tonic-gate os_to_grubdisk(char *osdisk, int on_bootdev)
23647c478bd9Sstevel@tonic-gate {
23657c478bd9Sstevel@tonic-gate 	FILE *fp;
23667c478bd9Sstevel@tonic-gate 	char *grubdisk;
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
23697c478bd9Sstevel@tonic-gate 	fp = open_diskmap();
23707c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
23717c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdisk);
23727c478bd9Sstevel@tonic-gate 		return (NULL);
23737c478bd9Sstevel@tonic-gate 	}
23747c478bd9Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdisk, fp, on_bootdev);
23757c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
23767c478bd9Sstevel@tonic-gate 	return (grubdisk);
23777c478bd9Sstevel@tonic-gate }
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate /*
23807c478bd9Sstevel@tonic-gate  * Check if root is on the boot device
23817c478bd9Sstevel@tonic-gate  * Return 0 (false) on error
23827c478bd9Sstevel@tonic-gate  */
23837c478bd9Sstevel@tonic-gate static int
23847c478bd9Sstevel@tonic-gate menu_on_bootdev(char *menu_root, FILE *fp)
23857c478bd9Sstevel@tonic-gate {
23867c478bd9Sstevel@tonic-gate 	int ret;
23877c478bd9Sstevel@tonic-gate 	char *grubhd, *bootp, *special;
23887c478bd9Sstevel@tonic-gate 
23897c478bd9Sstevel@tonic-gate 	special = get_special(menu_root);
23907c478bd9Sstevel@tonic-gate 	if (special == NULL)
23917c478bd9Sstevel@tonic-gate 		return (0);
23927c478bd9Sstevel@tonic-gate 	bootp = strstr(special, "p0:boot");
23937c478bd9Sstevel@tonic-gate 	if (bootp)
23947c478bd9Sstevel@tonic-gate 		*bootp = '\0';
23957c478bd9Sstevel@tonic-gate 	grubhd = get_grubdisk(special, fp, 1);
23967c478bd9Sstevel@tonic-gate 	free(special);
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 	if (grubhd == NULL)
23997c478bd9Sstevel@tonic-gate 		return (0);
24007c478bd9Sstevel@tonic-gate 	ret = grubhd[3] == '0';
24017c478bd9Sstevel@tonic-gate 	free(grubhd);
24027c478bd9Sstevel@tonic-gate 	return (ret);
24037c478bd9Sstevel@tonic-gate }
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24067c478bd9Sstevel@tonic-gate static error_t
24077c478bd9Sstevel@tonic-gate update_entry(menu_t *mp, char *menu_root, char *opt)
24087c478bd9Sstevel@tonic-gate {
24097c478bd9Sstevel@tonic-gate 	FILE *fp;
24107c478bd9Sstevel@tonic-gate 	int entry;
24117c478bd9Sstevel@tonic-gate 	line_t *lp;
24127c478bd9Sstevel@tonic-gate 	char *grubdisk, *title, *osdev, *osroot;
24137c478bd9Sstevel@tonic-gate 	int bootadm_entry, entry_to_delete;
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 	assert(mp);
24167c478bd9Sstevel@tonic-gate 	assert(opt);
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 	osdev = strtok(opt, ",");
24197c478bd9Sstevel@tonic-gate 	osroot = strtok(NULL, ",");
24207c478bd9Sstevel@tonic-gate 	if (osroot == NULL)
24217c478bd9Sstevel@tonic-gate 		osroot = menu_root;
24227c478bd9Sstevel@tonic-gate 	title = get_title(osroot);
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 	/* translate /dev/dsk name to grub disk name */
24257c478bd9Sstevel@tonic-gate 	fp = open_diskmap();
24267c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
24277c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
24287c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
24297c478bd9Sstevel@tonic-gate 	}
24307c478bd9Sstevel@tonic-gate 	grubdisk = get_grubdisk(osdev, fp, menu_on_bootdev(menu_root, fp));
24317c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
24327c478bd9Sstevel@tonic-gate 	if (grubdisk == NULL) {
24337c478bd9Sstevel@tonic-gate 		bam_error(DISKMAP_FAIL, osdev);
24347c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
24357c478bd9Sstevel@tonic-gate 	}
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	/* delete existing entries with matching grub hd name */
24387c478bd9Sstevel@tonic-gate 	for (;;) {
24397c478bd9Sstevel@tonic-gate 		entry_to_delete = -1;
24407c478bd9Sstevel@tonic-gate 		bootadm_entry = 0;
24417c478bd9Sstevel@tonic-gate 		for (lp = mp->start; lp; lp = lp->next) {
24427c478bd9Sstevel@tonic-gate 			/*
24437c478bd9Sstevel@tonic-gate 			 * can only delete bootadm entries
24447c478bd9Sstevel@tonic-gate 			 */
24457c478bd9Sstevel@tonic-gate 			if (lp->flags == BAM_COMMENT) {
24467c478bd9Sstevel@tonic-gate 				if (strcmp(lp->arg, BAM_HDR) == 0)
24477c478bd9Sstevel@tonic-gate 					bootadm_entry = 1;
24487c478bd9Sstevel@tonic-gate 				else if (strcmp(lp->arg, BAM_FTR) == 0)
24497c478bd9Sstevel@tonic-gate 					bootadm_entry = 0;
24507c478bd9Sstevel@tonic-gate 			}
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 			if (bootadm_entry && lp->flags == BAM_ENTRY &&
24537c478bd9Sstevel@tonic-gate 			    strcmp(lp->cmd, menu_cmds[ROOT_CMD]) == 0 &&
24547c478bd9Sstevel@tonic-gate 			    strcmp(lp->arg, grubdisk) == 0) {
24557c478bd9Sstevel@tonic-gate 				entry_to_delete = lp->entryNum;
24567c478bd9Sstevel@tonic-gate 			}
24577c478bd9Sstevel@tonic-gate 		}
24587c478bd9Sstevel@tonic-gate 		if (entry_to_delete == -1)
24597c478bd9Sstevel@tonic-gate 			break;
24607c478bd9Sstevel@tonic-gate 		(void) do_delete(mp, entry_to_delete);
24617c478bd9Sstevel@tonic-gate 	}
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 	/* add the entry for normal Solaris */
24647c478bd9Sstevel@tonic-gate 	entry = add_boot_entry(mp, title, grubdisk,
24657c478bd9Sstevel@tonic-gate 	    "/platform/i86pc/multiboot",
24667c478bd9Sstevel@tonic-gate 	    "/platform/i86pc/boot_archive");
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 	/* add the entry for failsafe archive */
24697c478bd9Sstevel@tonic-gate 	(void) add_boot_entry(mp, "Solaris failsafe", grubdisk,
24707c478bd9Sstevel@tonic-gate 	    "/boot/multiboot kernel/unix -s",
24717c478bd9Sstevel@tonic-gate 	    "/boot/x86.miniroot-safe");
24727c478bd9Sstevel@tonic-gate 	free(grubdisk);
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
24757c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
24767c478bd9Sstevel@tonic-gate 	}
24777c478bd9Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
24787c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
24797c478bd9Sstevel@tonic-gate }
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate /*
24827c478bd9Sstevel@tonic-gate  * This function is for supporting reboot with args.
24837c478bd9Sstevel@tonic-gate  * The opt value can be:
24847c478bd9Sstevel@tonic-gate  * NULL		delete temp entry, if present
24857c478bd9Sstevel@tonic-gate  * entry=#	switches default entry to 1
24867c478bd9Sstevel@tonic-gate  * else		treated as boot-args and setup a temperary menu entry
24877c478bd9Sstevel@tonic-gate  *		and make it the default
24887c478bd9Sstevel@tonic-gate  */
24897c478bd9Sstevel@tonic-gate #define	REBOOT_TITLE	"Solaris_reboot_transient"
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate static error_t
24927c478bd9Sstevel@tonic-gate update_temp(menu_t *mp, char *menupath, char *opt)
24937c478bd9Sstevel@tonic-gate {
24947c478bd9Sstevel@tonic-gate 	int entry;
24957c478bd9Sstevel@tonic-gate 	char *grubdisk, *rootdev;
24967c478bd9Sstevel@tonic-gate 	char kernbuf[1024];
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate 	assert(mp);
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate 	if (opt != NULL &&
25017c478bd9Sstevel@tonic-gate 	    strncmp(opt, "entry=", strlen("entry=")) == 0 &&
25027c478bd9Sstevel@tonic-gate 	    selector(mp, opt, &entry, NULL) == BAM_SUCCESS) {
25037c478bd9Sstevel@tonic-gate 		/* this is entry=# option */
25047c478bd9Sstevel@tonic-gate 		return (set_global(mp, menu_cmds[DEFAULT_CMD], entry));
25057c478bd9Sstevel@tonic-gate 	}
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 	/* If no option, delete exiting reboot menu entry */
25087c478bd9Sstevel@tonic-gate 	if (opt == NULL)
25097c478bd9Sstevel@tonic-gate 		return (delete_entry(mp, menupath, "title="REBOOT_TITLE));
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate 	/*
25127c478bd9Sstevel@tonic-gate 	 * add a new menu entry base on opt and make it the default
25137c478bd9Sstevel@tonic-gate 	 * 1. First get root disk name from mnttab
25147c478bd9Sstevel@tonic-gate 	 * 2. Translate disk name to grub name
25157c478bd9Sstevel@tonic-gate 	 * 3. Add the new menu entry
25167c478bd9Sstevel@tonic-gate 	 */
25177c478bd9Sstevel@tonic-gate 	rootdev = get_special("/");
25187c478bd9Sstevel@tonic-gate 	if (rootdev) {
25197c478bd9Sstevel@tonic-gate 		grubdisk = os_to_grubdisk(rootdev, 1);
25207c478bd9Sstevel@tonic-gate 		free(rootdev);
25217c478bd9Sstevel@tonic-gate 	}
25227c478bd9Sstevel@tonic-gate 	if (grubdisk == NULL) {
25237c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
25247c478bd9Sstevel@tonic-gate 	}
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 	/* add an entry for Solaris reboot */
25277c478bd9Sstevel@tonic-gate 	(void) snprintf(kernbuf, sizeof (kernbuf),
25287c478bd9Sstevel@tonic-gate 	    "/platform/i86pc/multiboot %s", opt);
25297c478bd9Sstevel@tonic-gate 	entry = add_boot_entry(mp, REBOOT_TITLE, grubdisk, kernbuf,
25307c478bd9Sstevel@tonic-gate 	    "/platform/i86pc/boot_archive");
25317c478bd9Sstevel@tonic-gate 	free(grubdisk);
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 	if (entry == BAM_ERROR) {
25347c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
25357c478bd9Sstevel@tonic-gate 	}
25367c478bd9Sstevel@tonic-gate 	(void) set_global(mp, menu_cmds[DEFAULT_CMD], entry);
25377c478bd9Sstevel@tonic-gate 	return (BAM_WRITE);
25387c478bd9Sstevel@tonic-gate }
25397c478bd9Sstevel@tonic-gate 
25407c478bd9Sstevel@tonic-gate static error_t
25417c478bd9Sstevel@tonic-gate set_global(menu_t *mp, char *globalcmd, int val)
25427c478bd9Sstevel@tonic-gate {
25437c478bd9Sstevel@tonic-gate 	line_t *lp, *found, *last;
25447c478bd9Sstevel@tonic-gate 	char *cp, *str;
25457c478bd9Sstevel@tonic-gate 	char prefix[BAM_MAXLINE];
25467c478bd9Sstevel@tonic-gate 	size_t len;
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate 	assert(mp);
25497c478bd9Sstevel@tonic-gate 	assert(globalcmd);
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	found = last = NULL;
25527c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
25537c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
25547c478bd9Sstevel@tonic-gate 			continue;
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 		last = lp; /* track the last global found */
25577c478bd9Sstevel@tonic-gate 
25587c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
25597c478bd9Sstevel@tonic-gate 			bam_error(NO_CMD, lp->lineNum);
25607c478bd9Sstevel@tonic-gate 			continue;
25617c478bd9Sstevel@tonic-gate 		}
25627c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
25637c478bd9Sstevel@tonic-gate 			continue;
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 		if (found) {
25667c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
25677c478bd9Sstevel@tonic-gate 		}
25687c478bd9Sstevel@tonic-gate 		found = lp;
25697c478bd9Sstevel@tonic-gate 	}
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate 	if (found == NULL) {
25727c478bd9Sstevel@tonic-gate 		lp = s_calloc(1, sizeof (line_t));
25737c478bd9Sstevel@tonic-gate 		if (last == NULL) {
25747c478bd9Sstevel@tonic-gate 			lp->next = mp->start;
25757c478bd9Sstevel@tonic-gate 			mp->start = lp;
25767c478bd9Sstevel@tonic-gate 			mp->end = (mp->end) ? mp->end : lp;
25777c478bd9Sstevel@tonic-gate 		} else {
25787c478bd9Sstevel@tonic-gate 			lp->next = last->next;
25797c478bd9Sstevel@tonic-gate 			last->next = lp;
25807c478bd9Sstevel@tonic-gate 			if (lp->next == NULL)
25817c478bd9Sstevel@tonic-gate 				mp->end = lp;
25827c478bd9Sstevel@tonic-gate 		}
25837c478bd9Sstevel@tonic-gate 		lp->flags = BAM_GLOBAL; /* other fields not needed for writes */
25847c478bd9Sstevel@tonic-gate 		len = strlen(globalcmd) + strlen(menu_cmds[SEP_CMD]);
25857c478bd9Sstevel@tonic-gate 		len += 10;	/* val < 10 digits */
25867c478bd9Sstevel@tonic-gate 		lp->line = s_calloc(1, len);
25877c478bd9Sstevel@tonic-gate 		(void) snprintf(lp->line, len, "%s%s%d",
25887c478bd9Sstevel@tonic-gate 		    globalcmd, menu_cmds[SEP_CMD], val);
25897c478bd9Sstevel@tonic-gate 		return (BAM_WRITE);
25907c478bd9Sstevel@tonic-gate 	}
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	/*
25937c478bd9Sstevel@tonic-gate 	 * We are changing an existing entry. Retain any prefix whitespace,
25947c478bd9Sstevel@tonic-gate 	 * but overwrite everything else. This preserves tabs added for
25957c478bd9Sstevel@tonic-gate 	 * readability.
25967c478bd9Sstevel@tonic-gate 	 */
25977c478bd9Sstevel@tonic-gate 	str = found->line;
25987c478bd9Sstevel@tonic-gate 	cp = prefix;
25997c478bd9Sstevel@tonic-gate 	while (*str == ' ' || *str == '\t')
26007c478bd9Sstevel@tonic-gate 		*(cp++) = *(str++);
26017c478bd9Sstevel@tonic-gate 	*cp = '\0'; /* Terminate prefix */
26027c478bd9Sstevel@tonic-gate 	len = strlen(prefix) + strlen(globalcmd);
26037c478bd9Sstevel@tonic-gate 	len += strlen(menu_cmds[SEP_CMD]) + 10;
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	free(found->line);
26067c478bd9Sstevel@tonic-gate 	found->line = s_calloc(1, len);
26077c478bd9Sstevel@tonic-gate 	(void) snprintf(found->line, len,
26087c478bd9Sstevel@tonic-gate 		"%s%s%s%d", prefix, globalcmd, menu_cmds[SEP_CMD], val);
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate 	return (BAM_WRITE); /* need a write to menu */
26117c478bd9Sstevel@tonic-gate }
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate /*ARGSUSED*/
26147c478bd9Sstevel@tonic-gate static error_t
26157c478bd9Sstevel@tonic-gate set_option(menu_t *mp, char *menu_path, char *opt)
26167c478bd9Sstevel@tonic-gate {
26177c478bd9Sstevel@tonic-gate 	int optnum, optval;
26187c478bd9Sstevel@tonic-gate 	char *val;
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 	assert(mp);
26217c478bd9Sstevel@tonic-gate 	assert(opt);
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate 	val = strchr(opt, '=');
26247c478bd9Sstevel@tonic-gate 	if (val == NULL) {
26257c478bd9Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
26267c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26277c478bd9Sstevel@tonic-gate 	}
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	*val = '\0';
26307c478bd9Sstevel@tonic-gate 	if (strcmp(opt, "default") == 0) {
26317c478bd9Sstevel@tonic-gate 		optnum = DEFAULT_CMD;
26327c478bd9Sstevel@tonic-gate 	} else if (strcmp(opt, "timeout") == 0) {
26337c478bd9Sstevel@tonic-gate 		optnum = TIMEOUT_CMD;
26347c478bd9Sstevel@tonic-gate 	} else {
26357c478bd9Sstevel@tonic-gate 		bam_error(INVALID_ENTRY, opt);
26367c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26377c478bd9Sstevel@tonic-gate 	}
26387c478bd9Sstevel@tonic-gate 
26397c478bd9Sstevel@tonic-gate 	optval = s_strtol(val + 1);
26407c478bd9Sstevel@tonic-gate 	*val = '=';
26417c478bd9Sstevel@tonic-gate 	return (set_global(mp, menu_cmds[optnum], optval));
26427c478bd9Sstevel@tonic-gate }
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate /*
26457c478bd9Sstevel@tonic-gate  * The quiet argument suppresses messages. This is used
26467c478bd9Sstevel@tonic-gate  * when invoked in the context of other commands (e.g. list_entry)
26477c478bd9Sstevel@tonic-gate  */
26487c478bd9Sstevel@tonic-gate static error_t
26497c478bd9Sstevel@tonic-gate read_globals(menu_t *mp, char *menu_path, char *globalcmd, int quiet)
26507c478bd9Sstevel@tonic-gate {
26517c478bd9Sstevel@tonic-gate 	line_t *lp;
26527c478bd9Sstevel@tonic-gate 	char *arg;
26537c478bd9Sstevel@tonic-gate 	int done, ret = BAM_SUCCESS;
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 	assert(mp);
26567c478bd9Sstevel@tonic-gate 	assert(menu_path);
26577c478bd9Sstevel@tonic-gate 	assert(globalcmd);
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 	if (mp->start == NULL) {
26607c478bd9Sstevel@tonic-gate 		if (!quiet)
26617c478bd9Sstevel@tonic-gate 			bam_error(NO_MENU, menu_path);
26627c478bd9Sstevel@tonic-gate 		return (BAM_ERROR);
26637c478bd9Sstevel@tonic-gate 	}
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate 	done = 0;
26667c478bd9Sstevel@tonic-gate 	for (lp = mp->start; lp; lp = lp->next) {
26677c478bd9Sstevel@tonic-gate 		if (lp->flags != BAM_GLOBAL)
26687c478bd9Sstevel@tonic-gate 			continue;
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate 		if (lp->cmd == NULL) {
26717c478bd9Sstevel@tonic-gate 			if (!quiet)
26727c478bd9Sstevel@tonic-gate 				bam_error(NO_CMD, lp->lineNum);
26737c478bd9Sstevel@tonic-gate 			continue;
26747c478bd9Sstevel@tonic-gate 		}
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 		if (strcmp(globalcmd, lp->cmd) != 0)
26777c478bd9Sstevel@tonic-gate 			continue;
26787c478bd9Sstevel@tonic-gate 
26797c478bd9Sstevel@tonic-gate 		/* Found global. Check for duplicates */
26807c478bd9Sstevel@tonic-gate 		if (done && !quiet) {
26817c478bd9Sstevel@tonic-gate 			bam_error(DUP_CMD, globalcmd, lp->lineNum, bam_root);
26827c478bd9Sstevel@tonic-gate 			ret = BAM_ERROR;
26837c478bd9Sstevel@tonic-gate 		}
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 		arg = lp->arg ? lp->arg : "";
26867c478bd9Sstevel@tonic-gate 		bam_print(GLOBAL_CMD, globalcmd, arg);
26877c478bd9Sstevel@tonic-gate 		done = 1;
26887c478bd9Sstevel@tonic-gate 	}
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 	if (!done && bam_verbose)
26917c478bd9Sstevel@tonic-gate 		bam_print(NO_ENTRY, globalcmd);
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate 	return (ret);
26947c478bd9Sstevel@tonic-gate }
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate static error_t
26977c478bd9Sstevel@tonic-gate menu_write(char *root, menu_t *mp)
26987c478bd9Sstevel@tonic-gate {
26997c478bd9Sstevel@tonic-gate 	return (list2file(root, MENU_TMP, GRUB_MENU, mp->start));
27007c478bd9Sstevel@tonic-gate }
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate static void
27037c478bd9Sstevel@tonic-gate line_free(line_t *lp)
27047c478bd9Sstevel@tonic-gate {
27057c478bd9Sstevel@tonic-gate 	if (lp == NULL)
27067c478bd9Sstevel@tonic-gate 		return;
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 	if (lp->cmd)
27097c478bd9Sstevel@tonic-gate 		free(lp->cmd);
27107c478bd9Sstevel@tonic-gate 	if (lp->sep)
27117c478bd9Sstevel@tonic-gate 		free(lp->sep);
27127c478bd9Sstevel@tonic-gate 	if (lp->arg)
27137c478bd9Sstevel@tonic-gate 		free(lp->arg);
27147c478bd9Sstevel@tonic-gate 	if (lp->line)
27157c478bd9Sstevel@tonic-gate 		free(lp->line);
27167c478bd9Sstevel@tonic-gate 	free(lp);
27177c478bd9Sstevel@tonic-gate }
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate static void
27207c478bd9Sstevel@tonic-gate linelist_free(line_t *start)
27217c478bd9Sstevel@tonic-gate {
27227c478bd9Sstevel@tonic-gate 	line_t *lp;
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 	while (start) {
27257c478bd9Sstevel@tonic-gate 		lp = start;
27267c478bd9Sstevel@tonic-gate 		start = start->next;
27277c478bd9Sstevel@tonic-gate 		line_free(lp);
27287c478bd9Sstevel@tonic-gate 	}
27297c478bd9Sstevel@tonic-gate }
27307c478bd9Sstevel@tonic-gate 
27317c478bd9Sstevel@tonic-gate static void
27327c478bd9Sstevel@tonic-gate filelist_free(filelist_t *flistp)
27337c478bd9Sstevel@tonic-gate {
27347c478bd9Sstevel@tonic-gate 	linelist_free(flistp->head);
27357c478bd9Sstevel@tonic-gate 	flistp->head = NULL;
27367c478bd9Sstevel@tonic-gate 	flistp->tail = NULL;
27377c478bd9Sstevel@tonic-gate }
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate static void
27407c478bd9Sstevel@tonic-gate menu_free(menu_t *mp)
27417c478bd9Sstevel@tonic-gate {
27427c478bd9Sstevel@tonic-gate 	assert(mp);
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 	if (mp->start)
27457c478bd9Sstevel@tonic-gate 		linelist_free(mp->start);
27467c478bd9Sstevel@tonic-gate 	free(mp);
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate }
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate /*
27517c478bd9Sstevel@tonic-gate  * Utility routines
27527c478bd9Sstevel@tonic-gate  */
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 
27557c478bd9Sstevel@tonic-gate /*
27567c478bd9Sstevel@tonic-gate  * Returns 0 on success
27577c478bd9Sstevel@tonic-gate  * Any other value indicates an error
27587c478bd9Sstevel@tonic-gate  */
27597c478bd9Sstevel@tonic-gate static int
27607c478bd9Sstevel@tonic-gate exec_cmd(char *cmdline, char *output, int64_t osize)
27617c478bd9Sstevel@tonic-gate {
27627c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
27637c478bd9Sstevel@tonic-gate 	int ret;
27647c478bd9Sstevel@tonic-gate 	FILE *ptr;
27657c478bd9Sstevel@tonic-gate 	size_t len;
27667c478bd9Sstevel@tonic-gate 	sigset_t set;
27677c478bd9Sstevel@tonic-gate 	void (*disp)(int);
27687c478bd9Sstevel@tonic-gate 
27697c478bd9Sstevel@tonic-gate 	/*
27707c478bd9Sstevel@tonic-gate 	 * For security
27717c478bd9Sstevel@tonic-gate 	 * - only absolute paths are allowed
27727c478bd9Sstevel@tonic-gate 	 * - set IFS to space and tab
27737c478bd9Sstevel@tonic-gate 	 */
27747c478bd9Sstevel@tonic-gate 	if (*cmdline != '/') {
27757c478bd9Sstevel@tonic-gate 		bam_error(ABS_PATH_REQ, cmdline);
27767c478bd9Sstevel@tonic-gate 		return (-1);
27777c478bd9Sstevel@tonic-gate 	}
27787c478bd9Sstevel@tonic-gate 	(void) putenv("IFS= \t");
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	/*
27817c478bd9Sstevel@tonic-gate 	 * We may have been exec'ed with SIGCHLD blocked
27827c478bd9Sstevel@tonic-gate 	 * unblock it here
27837c478bd9Sstevel@tonic-gate 	 */
27847c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
27857c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
27867c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
27877c478bd9Sstevel@tonic-gate 		bam_error(CANT_UNBLOCK_SIGCHLD, strerror(errno));
27887c478bd9Sstevel@tonic-gate 		return (-1);
27897c478bd9Sstevel@tonic-gate 	}
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	/*
27927c478bd9Sstevel@tonic-gate 	 * Set SIGCHLD disposition to SIG_DFL for popen/pclose
27937c478bd9Sstevel@tonic-gate 	 */
27947c478bd9Sstevel@tonic-gate 	disp = sigset(SIGCHLD, SIG_DFL);
27957c478bd9Sstevel@tonic-gate 	if (disp == SIG_ERR) {
27967c478bd9Sstevel@tonic-gate 		bam_error(FAILED_SIG, strerror(errno));
27977c478bd9Sstevel@tonic-gate 		return (-1);
27987c478bd9Sstevel@tonic-gate 	}
27997c478bd9Sstevel@tonic-gate 	if (disp == SIG_HOLD) {
28007c478bd9Sstevel@tonic-gate 		bam_error(BLOCKED_SIG, cmdline);
28017c478bd9Sstevel@tonic-gate 		return (-1);
28027c478bd9Sstevel@tonic-gate 	}
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate 	ptr = popen(cmdline, "r");
28057c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
28067c478bd9Sstevel@tonic-gate 		bam_error(POPEN_FAIL, cmdline, strerror(errno));
28077c478bd9Sstevel@tonic-gate 		return (-1);
28087c478bd9Sstevel@tonic-gate 	}
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate 	/*
28117c478bd9Sstevel@tonic-gate 	 * If we simply do a pclose() following a popen(), pclose()
28127c478bd9Sstevel@tonic-gate 	 * will close the reader end of the pipe immediately even
28137c478bd9Sstevel@tonic-gate 	 * if the child process has not started/exited. pclose()
28147c478bd9Sstevel@tonic-gate 	 * does wait for cmd to terminate before returning though.
28157c478bd9Sstevel@tonic-gate 	 * When the executed command writes its output to the pipe
28167c478bd9Sstevel@tonic-gate 	 * there is no reader process and the command dies with
28177c478bd9Sstevel@tonic-gate 	 * SIGPIPE. To avoid this we read repeatedly until read
28187c478bd9Sstevel@tonic-gate 	 * terminates with EOF. This indicates that the command
28197c478bd9Sstevel@tonic-gate 	 * (writer) has closed the pipe and we can safely do a
28207c478bd9Sstevel@tonic-gate 	 * pclose().
28217c478bd9Sstevel@tonic-gate 	 *
28227c478bd9Sstevel@tonic-gate 	 * Since pclose() does wait for the command to exit,
28237c478bd9Sstevel@tonic-gate 	 * we can safely reap the exit status of the command
28247c478bd9Sstevel@tonic-gate 	 * from the value returned by pclose()
28257c478bd9Sstevel@tonic-gate 	 */
28267c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), ptr) != NULL) {
28277c478bd9Sstevel@tonic-gate 		/* if (bam_verbose)  XXX */
28287c478bd9Sstevel@tonic-gate 			bam_print(PRINT_NO_NEWLINE, buf);
28297c478bd9Sstevel@tonic-gate 		if (output && osize > 0) {
28307c478bd9Sstevel@tonic-gate 			(void) snprintf(output, osize, "%s", buf);
28317c478bd9Sstevel@tonic-gate 			len = strlen(buf);
28327c478bd9Sstevel@tonic-gate 			output += len;
28337c478bd9Sstevel@tonic-gate 			osize -= len;
28347c478bd9Sstevel@tonic-gate 		}
28357c478bd9Sstevel@tonic-gate 	}
28367c478bd9Sstevel@tonic-gate 
28377c478bd9Sstevel@tonic-gate 	ret = pclose(ptr);
28387c478bd9Sstevel@tonic-gate 	if (ret == -1) {
28397c478bd9Sstevel@tonic-gate 		bam_error(PCLOSE_FAIL, cmdline, strerror(errno));
28407c478bd9Sstevel@tonic-gate 		return (-1);
28417c478bd9Sstevel@tonic-gate 	}
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 	if (WIFEXITED(ret)) {
28447c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(ret));
28457c478bd9Sstevel@tonic-gate 	} else {
28467c478bd9Sstevel@tonic-gate 		bam_error(EXEC_FAIL, cmdline, ret);
28477c478bd9Sstevel@tonic-gate 		return (-1);
28487c478bd9Sstevel@tonic-gate 	}
28497c478bd9Sstevel@tonic-gate }
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate /*
28527c478bd9Sstevel@tonic-gate  * Since this function returns -1 on error
28537c478bd9Sstevel@tonic-gate  * it cannot be used to convert -1. However,
28547c478bd9Sstevel@tonic-gate  * that is sufficient for what we need.
28557c478bd9Sstevel@tonic-gate  */
28567c478bd9Sstevel@tonic-gate static long
28577c478bd9Sstevel@tonic-gate s_strtol(char *str)
28587c478bd9Sstevel@tonic-gate {
28597c478bd9Sstevel@tonic-gate 	long l;
28607c478bd9Sstevel@tonic-gate 	char *res = NULL;
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate 	if (str == NULL) {
28637c478bd9Sstevel@tonic-gate 		return (-1);
28647c478bd9Sstevel@tonic-gate 	}
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 	errno = 0;
28677c478bd9Sstevel@tonic-gate 	l = strtol(str, &res, 10);
28687c478bd9Sstevel@tonic-gate 	if (errno || *res != '\0') {
28697c478bd9Sstevel@tonic-gate 		return (-1);
28707c478bd9Sstevel@tonic-gate 	}
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 	return (l);
28737c478bd9Sstevel@tonic-gate }
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate /*
28767c478bd9Sstevel@tonic-gate  * Wrapper around fputs, that adds a newline (since fputs doesn't)
28777c478bd9Sstevel@tonic-gate  */
28787c478bd9Sstevel@tonic-gate static int
28797c478bd9Sstevel@tonic-gate s_fputs(char *str, FILE *fp)
28807c478bd9Sstevel@tonic-gate {
28817c478bd9Sstevel@tonic-gate 	char linebuf[BAM_MAXLINE];
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate 	(void) snprintf(linebuf, sizeof (linebuf), "%s\n", str);
28847c478bd9Sstevel@tonic-gate 	return (fputs(linebuf, fp));
28857c478bd9Sstevel@tonic-gate }
28867c478bd9Sstevel@tonic-gate 
28877c478bd9Sstevel@tonic-gate /*
28887c478bd9Sstevel@tonic-gate  * Wrapper around fgets, that strips newlines returned by fgets
28897c478bd9Sstevel@tonic-gate  */
28907c478bd9Sstevel@tonic-gate static char *
28917c478bd9Sstevel@tonic-gate s_fgets(char *buf, int buflen, FILE *fp)
28927c478bd9Sstevel@tonic-gate {
28937c478bd9Sstevel@tonic-gate 	int n;
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 	buf = fgets(buf, buflen, fp);
28967c478bd9Sstevel@tonic-gate 	if (buf) {
28977c478bd9Sstevel@tonic-gate 		n = strlen(buf);
28987c478bd9Sstevel@tonic-gate 		if (n == buflen - 1 && buf[n-1] != '\n')
28997c478bd9Sstevel@tonic-gate 			bam_error(TOO_LONG, buflen - 1, buf);
29007c478bd9Sstevel@tonic-gate 		buf[n-1] = (buf[n-1] == '\n') ? '\0' : buf[n-1];
29017c478bd9Sstevel@tonic-gate 	}
29027c478bd9Sstevel@tonic-gate 
29037c478bd9Sstevel@tonic-gate 	return (buf);
29047c478bd9Sstevel@tonic-gate }
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate static void *
29077c478bd9Sstevel@tonic-gate s_calloc(size_t nelem, size_t sz)
29087c478bd9Sstevel@tonic-gate {
29097c478bd9Sstevel@tonic-gate 	void *ptr;
29107c478bd9Sstevel@tonic-gate 
29117c478bd9Sstevel@tonic-gate 	ptr = calloc(nelem, sz);
29127c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
29137c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, nelem*sz);
29147c478bd9Sstevel@tonic-gate 		bam_exit(1);
29157c478bd9Sstevel@tonic-gate 	}
29167c478bd9Sstevel@tonic-gate 	return (ptr);
29177c478bd9Sstevel@tonic-gate }
29187c478bd9Sstevel@tonic-gate 
29197c478bd9Sstevel@tonic-gate static char *
29207c478bd9Sstevel@tonic-gate s_strdup(char *str)
29217c478bd9Sstevel@tonic-gate {
29227c478bd9Sstevel@tonic-gate 	char *ptr;
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate 	if (str == NULL)
29257c478bd9Sstevel@tonic-gate 		return (NULL);
29267c478bd9Sstevel@tonic-gate 
29277c478bd9Sstevel@tonic-gate 	ptr = strdup(str);
29287c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
29297c478bd9Sstevel@tonic-gate 		bam_error(NO_MEM, strlen(str) + 1);
29307c478bd9Sstevel@tonic-gate 		bam_exit(1);
29317c478bd9Sstevel@tonic-gate 	}
29327c478bd9Sstevel@tonic-gate 	return (ptr);
29337c478bd9Sstevel@tonic-gate }
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate /*
29367c478bd9Sstevel@tonic-gate  * Returns 1 if amd64 (or sparc, for syncing x86 diskless clients)
29377c478bd9Sstevel@tonic-gate  * Returns 0 otherwise
29387c478bd9Sstevel@tonic-gate  */
29397c478bd9Sstevel@tonic-gate static int
29407c478bd9Sstevel@tonic-gate is_amd64(void)
29417c478bd9Sstevel@tonic-gate {
29427c478bd9Sstevel@tonic-gate 	static int amd64 = -1;
29437c478bd9Sstevel@tonic-gate 	char isabuf[257];	/* from sysinfo(2) manpage */
29447c478bd9Sstevel@tonic-gate 
29457c478bd9Sstevel@tonic-gate 	if (amd64 != -1)
29467c478bd9Sstevel@tonic-gate 		return (amd64);
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_ISALIST, isabuf, sizeof (isabuf)) > 0 &&
29497c478bd9Sstevel@tonic-gate 	    strncmp(isabuf, "amd64 ", strlen("amd64 ")) == 0)
29507c478bd9Sstevel@tonic-gate 		amd64 = 1;
29517c478bd9Sstevel@tonic-gate 	else if (strstr(isabuf, "i386") == NULL)
29527c478bd9Sstevel@tonic-gate 		amd64 = 1;		/* diskless server */
29537c478bd9Sstevel@tonic-gate 	else
29547c478bd9Sstevel@tonic-gate 		amd64 = 0;
29557c478bd9Sstevel@tonic-gate 
29567c478bd9Sstevel@tonic-gate 	return (amd64);
29577c478bd9Sstevel@tonic-gate }
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate static void
29607c478bd9Sstevel@tonic-gate append_to_flist(filelist_t *flistp, char *s)
29617c478bd9Sstevel@tonic-gate {
29627c478bd9Sstevel@tonic-gate 	line_t *lp;
29637c478bd9Sstevel@tonic-gate 
29647c478bd9Sstevel@tonic-gate 	lp = s_calloc(1, sizeof (line_t));
29657c478bd9Sstevel@tonic-gate 	lp->line = s_strdup(s);
29667c478bd9Sstevel@tonic-gate 	if (flistp->head == NULL)
29677c478bd9Sstevel@tonic-gate 		flistp->head = lp;
29687c478bd9Sstevel@tonic-gate 	else
29697c478bd9Sstevel@tonic-gate 		flistp->tail->next = lp;
29707c478bd9Sstevel@tonic-gate 	flistp->tail = lp;
29717c478bd9Sstevel@tonic-gate }
2972