xref: /onnv-gate/usr/src/cmd/svc/svccfg/svccfg_engine.c (revision 12418:1acff790c718)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51061Scasper  * Common Development and Distribution License (the "License").
61061Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
215040Swesolows 
220Sstevel@tonic-gate /*
23*12418STruong.Q.Nguyen@Sun.COM  *  Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * svccfg(1) interpreter and command execution engine.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/mman.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <assert.h>
350Sstevel@tonic-gate #include <errno.h>
3611996SThomas.Whitten@Sun.COM #include <fcntl.h>
370Sstevel@tonic-gate #include <libintl.h>
380Sstevel@tonic-gate #include <libtecla.h>
390Sstevel@tonic-gate #include <md5.h>
400Sstevel@tonic-gate #include <string.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <unistd.h>
430Sstevel@tonic-gate 
4411996SThomas.Whitten@Sun.COM #include "manifest_find.h"
450Sstevel@tonic-gate #include "manifest_hash.h"
460Sstevel@tonic-gate #include "svccfg.h"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #define	MS_PER_US		1000
490Sstevel@tonic-gate 
500Sstevel@tonic-gate engine_state_t *est;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * Replacement lex(1) character retrieval routines.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate int
560Sstevel@tonic-gate engine_cmd_getc(engine_state_t *E)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	if (E->sc_cmd_file != NULL)
590Sstevel@tonic-gate 		return (getc(E->sc_cmd_file));
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	if (E->sc_cmd_flags & SC_CMD_EOF)
620Sstevel@tonic-gate 		return (EOF);
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	if (E->sc_cmd_bufoff < E->sc_cmd_bufsz)
650Sstevel@tonic-gate 		return (*(E->sc_cmd_buf + E->sc_cmd_bufoff++));
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	if (!(E->sc_cmd_flags & SC_CMD_IACTIVE)) {
680Sstevel@tonic-gate 		E->sc_cmd_flags |= SC_CMD_EOF;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 		return (EOF);
710Sstevel@tonic-gate 	} else {
720Sstevel@tonic-gate #ifdef NATIVE_BUILD
730Sstevel@tonic-gate 		return (EOF);
740Sstevel@tonic-gate #else
750Sstevel@tonic-gate 		extern int parens;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 		if (parens <= 0) {
780Sstevel@tonic-gate 			E->sc_cmd_flags |= SC_CMD_EOF;
790Sstevel@tonic-gate 			return (EOF);
800Sstevel@tonic-gate 		}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 		for (;;) {
830Sstevel@tonic-gate 			E->sc_cmd_buf = gl_get_line(E->sc_gl, "> ", NULL, -1);
840Sstevel@tonic-gate 			if (E->sc_cmd_buf != NULL)
850Sstevel@tonic-gate 				break;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 			switch (gl_return_status(E->sc_gl)) {
880Sstevel@tonic-gate 			case GLR_SIGNAL:
890Sstevel@tonic-gate 				gl_abandon_line(E->sc_gl);
900Sstevel@tonic-gate 				continue;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 			case GLR_EOF:
930Sstevel@tonic-gate 				E->sc_cmd_flags |= SC_CMD_EOF;
940Sstevel@tonic-gate 				return (EOF);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 			case GLR_ERROR:
970Sstevel@tonic-gate 				uu_die(gettext("Error reading terminal: %s.\n"),
980Sstevel@tonic-gate 				    gl_error_message(E->sc_gl, NULL, 0));
990Sstevel@tonic-gate 				/* NOTREACHED */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 			default:
1020Sstevel@tonic-gate #ifndef NDEBUG
1030Sstevel@tonic-gate 				(void) fprintf(stderr, "%s:%d: gl_get_line() "
1040Sstevel@tonic-gate 				    "returned unexpected value %d.\n", __FILE__,
1050Sstevel@tonic-gate 				    __LINE__, gl_return_status(E->sc_gl));
1060Sstevel@tonic-gate #endif
1070Sstevel@tonic-gate 				abort();
1080Sstevel@tonic-gate 			}
1090Sstevel@tonic-gate 		}
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 		E->sc_cmd_bufsz = strlen(E->sc_cmd_buf);
1120Sstevel@tonic-gate 		E->sc_cmd_bufoff = 1;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 		return (E->sc_cmd_buf[0]);
1150Sstevel@tonic-gate #endif	/* NATIVE_BUILD */
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate int
1200Sstevel@tonic-gate engine_cmd_ungetc(engine_state_t *E, char c)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	if (E->sc_cmd_file != NULL)
1230Sstevel@tonic-gate 		return (ungetc(c, E->sc_cmd_file));
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	if (E->sc_cmd_buf != NULL)
1260Sstevel@tonic-gate 		*(E->sc_cmd_buf + --E->sc_cmd_bufoff) = c;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	return (c);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*ARGSUSED*/
1320Sstevel@tonic-gate void
1330Sstevel@tonic-gate engine_cmd_nputs(engine_state_t *E, char *c, size_t n)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	/* our lexer shouldn't need this state */
1360Sstevel@tonic-gate 	exit(11);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate int
1400Sstevel@tonic-gate engine_exec(char *cmd)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	est->sc_cmd_buf = cmd;
1430Sstevel@tonic-gate 	est->sc_cmd_bufsz = strlen(cmd) + 1;
1440Sstevel@tonic-gate 	est->sc_cmd_bufoff = 0;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	(void) yyparse();
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	return (0);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate #ifndef NATIVE_BUILD
1520Sstevel@tonic-gate /* ARGSUSED */
1530Sstevel@tonic-gate static
1540Sstevel@tonic-gate CPL_CHECK_FN(check_xml)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	const char *ext;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	if (strlen(pathname) < 4)
1590Sstevel@tonic-gate 		return (0);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	ext = pathname + strlen(pathname) - 4;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	return (strcmp(ext, ".xml") == 0 ? 1 : 0);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate static const char * const whitespace = " \t";
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static
1690Sstevel@tonic-gate CPL_MATCH_FN(complete_single_xml_file_arg)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	const char *arg1 = data;
1720Sstevel@tonic-gate 	int arg1end_i, ret;
1730Sstevel@tonic-gate 	CplFileConf *cfc;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	arg1end_i = arg1 + strcspn(arg1, whitespace) - line;
1760Sstevel@tonic-gate 	if (arg1end_i < word_end)
1770Sstevel@tonic-gate 		return (0);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	cfc = new_CplFileConf();
1800Sstevel@tonic-gate 	if (cfc == NULL) {
1810Sstevel@tonic-gate 		cpl_record_error(cpl, "Out of memory.");
1820Sstevel@tonic-gate 		return (1);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	cfc_set_check_fn(cfc, check_xml, NULL);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	ret = cpl_file_completions(cpl, cfc, line, word_end);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	(void) del_CplFileConf(cfc);
1900Sstevel@tonic-gate 	return (ret);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate static struct cmd_info {
1940Sstevel@tonic-gate 	const char	*name;
1950Sstevel@tonic-gate 	uint32_t	flags;
1960Sstevel@tonic-gate 	CplMatchFn	*complete_args_f;
1970Sstevel@tonic-gate } cmds[] = {
1980Sstevel@tonic-gate 	{ "validate", CS_GLOBAL, complete_single_xml_file_arg },
1990Sstevel@tonic-gate 	{ "import", CS_GLOBAL, complete_single_xml_file_arg },
20011996SThomas.Whitten@Sun.COM 	{ "cleanup", CS_GLOBAL, NULL},
2010Sstevel@tonic-gate 	{ "export", CS_GLOBAL, NULL },
2020Sstevel@tonic-gate 	{ "archive", CS_GLOBAL, NULL },
2030Sstevel@tonic-gate 	{ "apply", CS_GLOBAL, complete_single_xml_file_arg },
2040Sstevel@tonic-gate 	{ "extract", CS_GLOBAL, NULL },
2050Sstevel@tonic-gate 	{ "repository", CS_GLOBAL, NULL },
2060Sstevel@tonic-gate 	{ "inventory", CS_GLOBAL, complete_single_xml_file_arg },
2070Sstevel@tonic-gate 	{ "set", CS_GLOBAL, NULL },
2080Sstevel@tonic-gate 	{ "end", CS_GLOBAL, NULL },
2090Sstevel@tonic-gate 	{ "exit", CS_GLOBAL, NULL },
2100Sstevel@tonic-gate 	{ "quit", CS_GLOBAL, NULL },
2110Sstevel@tonic-gate 	{ "help", CS_GLOBAL, NULL },
2120Sstevel@tonic-gate 	{ "delete", CS_GLOBAL, NULL },
2130Sstevel@tonic-gate 	{ "select", CS_GLOBAL, complete_select },
2140Sstevel@tonic-gate 	{ "unselect", CS_SVC | CS_INST | CS_SNAP, NULL },
2150Sstevel@tonic-gate 	{ "list", CS_SCOPE | CS_SVC | CS_SNAP, NULL },
2160Sstevel@tonic-gate 	{ "add", CS_SCOPE | CS_SVC, NULL },
2170Sstevel@tonic-gate 	{ "listpg", CS_SVC | CS_INST | CS_SNAP, NULL },
2180Sstevel@tonic-gate 	{ "addpg", CS_SVC | CS_INST, NULL },
2190Sstevel@tonic-gate 	{ "delpg", CS_SVC | CS_INST, NULL },
2207475SPhilippe.Jung@Sun.COM 	{ "delhash", CS_GLOBAL, complete_single_xml_file_arg },
2210Sstevel@tonic-gate 	{ "listprop", CS_SVC | CS_INST | CS_SNAP, NULL },
2220Sstevel@tonic-gate 	{ "setprop", CS_SVC | CS_INST, NULL },
2230Sstevel@tonic-gate 	{ "delprop", CS_SVC | CS_INST, NULL },
2240Sstevel@tonic-gate 	{ "editprop", CS_SVC | CS_INST, NULL },
2257887SLiane.Praza@Sun.COM 	{ "describe", CS_SVC | CS_INST | CS_SNAP, NULL },
2260Sstevel@tonic-gate 	{ "listsnap", CS_INST | CS_SNAP, NULL },
2270Sstevel@tonic-gate 	{ "selectsnap", CS_INST | CS_SNAP, NULL },
2280Sstevel@tonic-gate 	{ "revert", CS_INST | CS_SNAP, NULL },
2295841Samaguire 	{ "refresh", CS_INST, NULL },
2300Sstevel@tonic-gate 	{ NULL }
2310Sstevel@tonic-gate };
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate int
2340Sstevel@tonic-gate add_cmd_matches(WordCompletion *cpl, const char *line, int word_end,
2350Sstevel@tonic-gate     uint32_t scope)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	int word_start, err;
2380Sstevel@tonic-gate 	size_t len;
2390Sstevel@tonic-gate 	const char *bol;
2400Sstevel@tonic-gate 	struct cmd_info *cip;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	word_start = strspn(line, whitespace);
2430Sstevel@tonic-gate 	len = word_end - word_start;
2440Sstevel@tonic-gate 	bol = line + word_end - len;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	for (cip = cmds; cip->name != NULL; ++cip) {
2470Sstevel@tonic-gate 		if ((cip->flags & scope) == 0)
2480Sstevel@tonic-gate 			continue;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 		if (strncmp(cip->name, bol, len) == 0) {
2510Sstevel@tonic-gate 			err = cpl_add_completion(cpl, line, word_start,
2520Sstevel@tonic-gate 			    word_end, cip->name + len, "", " ");
2530Sstevel@tonic-gate 			if (err != 0)
2540Sstevel@tonic-gate 				return (err);
2550Sstevel@tonic-gate 		}
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	return (0);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate  * Suggest completions.  We must first determine if the cursor is in command
2630Sstevel@tonic-gate  * position or in argument position.  If the former, complete_command() finds
2640Sstevel@tonic-gate  * matching commands.  If the latter, we tail-call the command-specific
2650Sstevel@tonic-gate  * argument-completion routine in the cmds table.
2660Sstevel@tonic-gate  */
2670Sstevel@tonic-gate /* ARGSUSED */
2680Sstevel@tonic-gate static
2690Sstevel@tonic-gate CPL_MATCH_FN(complete)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	const char *arg0, *arg1;
2720Sstevel@tonic-gate 	size_t arg0len;
2730Sstevel@tonic-gate 	struct cmd_info *cip;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	arg0 = line + strspn(line, whitespace);
2760Sstevel@tonic-gate 	arg0len = strcspn(arg0, whitespace);
2770Sstevel@tonic-gate 	if ((arg0 + arg0len) - line >= word_end ||
2780Sstevel@tonic-gate 	    (arg0[arg0len] != ' ' && arg0[arg0len] != '\t'))
2790Sstevel@tonic-gate 		return (complete_command(cpl, (void *)arg0, line, word_end));
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	arg1 = arg0 + arg0len;
2820Sstevel@tonic-gate 	arg1 += strspn(arg1, whitespace);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	for (cip = cmds; cip->name != NULL; ++cip) {
2850Sstevel@tonic-gate 		if (strlen(cip->name) != arg0len)
2860Sstevel@tonic-gate 			continue;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		if (strncmp(cip->name, arg0, arg0len) != 0)
2890Sstevel@tonic-gate 			continue;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 		if (cip->complete_args_f == NULL)
2920Sstevel@tonic-gate 			break;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		return (cip->complete_args_f(cpl, (void *)arg1, line,
2950Sstevel@tonic-gate 		    word_end));
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	return (0);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate #endif	/* NATIVE_BUILD */
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate int
3030Sstevel@tonic-gate engine_interp()
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate #ifdef NATIVE_BUILD
3060Sstevel@tonic-gate 	uu_die("native build does not support interactive mode.");
3070Sstevel@tonic-gate #else
3080Sstevel@tonic-gate 	char *selfmri;
3090Sstevel@tonic-gate 	size_t sfsz;
3100Sstevel@tonic-gate 	int r;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	extern int parens;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	est->sc_gl = new_GetLine(512, 8000);
3170Sstevel@tonic-gate 	if (est->sc_gl == NULL)
3180Sstevel@tonic-gate 		uu_die(gettext("Out of memory.\n"));
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/* The longest string is "[snapname]fmri[:instname]> ". */
3210Sstevel@tonic-gate 	sfsz = 1 + max_scf_name_len + 1 + max_scf_fmri_len + 2 +
3220Sstevel@tonic-gate 	    max_scf_name_len + 1 + 2 + 1;
3230Sstevel@tonic-gate 	selfmri = safe_malloc(sfsz);
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	r = gl_customize_completion(est->sc_gl, NULL, complete);
3260Sstevel@tonic-gate 	assert(r == 0);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	for (;;) {
3290Sstevel@tonic-gate 		lscf_get_selection_str(selfmri, sfsz - 2);
3300Sstevel@tonic-gate 		(void) strcat(selfmri, "> ");
3310Sstevel@tonic-gate 		est->sc_cmd_buf = gl_get_line(est->sc_gl, selfmri, NULL, -1);
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 		if (est->sc_cmd_buf == NULL) {
3340Sstevel@tonic-gate 			switch (gl_return_status(est->sc_gl)) {
3350Sstevel@tonic-gate 			case GLR_SIGNAL:
3360Sstevel@tonic-gate 				gl_abandon_line(est->sc_gl);
3370Sstevel@tonic-gate 				continue;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 			case GLR_EOF:
3400Sstevel@tonic-gate 				break;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 			case GLR_ERROR:
3430Sstevel@tonic-gate 				uu_die(gettext("Error reading terminal: %s.\n"),
3440Sstevel@tonic-gate 				    gl_error_message(est->sc_gl, NULL, 0));
3450Sstevel@tonic-gate 				/* NOTREACHED */
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 			default:
3480Sstevel@tonic-gate #ifndef NDEBUG
3490Sstevel@tonic-gate 				(void) fprintf(stderr, "%s:%d: gl_get_line() "
3500Sstevel@tonic-gate 				    "returned unexpected value %d.\n", __FILE__,
3510Sstevel@tonic-gate 				    __LINE__, gl_return_status(est->sc_gl));
3520Sstevel@tonic-gate #endif
3530Sstevel@tonic-gate 				abort();
3540Sstevel@tonic-gate 			}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 			break;
3570Sstevel@tonic-gate 		}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 		parens = 0;
3600Sstevel@tonic-gate 		est->sc_cmd_bufsz = strlen(est->sc_cmd_buf);
3610Sstevel@tonic-gate 		est->sc_cmd_bufoff = 0;
3620Sstevel@tonic-gate 		est->sc_cmd_flags = SC_CMD_IACTIVE;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 		(void) yyparse();
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	free(selfmri);
3680Sstevel@tonic-gate 	est->sc_gl = del_GetLine(est->sc_gl);	/* returns NULL */
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate #endif	/* NATIVE_BUILD */
3710Sstevel@tonic-gate 	return (0);
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate int
3750Sstevel@tonic-gate engine_source(const char *name, boolean_t dont_exit)
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate 	engine_state_t *old = est;
3780Sstevel@tonic-gate 	struct stat st;
3790Sstevel@tonic-gate 	int ret;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	est = uu_zalloc(sizeof (engine_state_t));
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	/* first, copy the stuff set up in engine_init */
3840Sstevel@tonic-gate 	est->sc_repo_pid = old->sc_repo_pid;
3850Sstevel@tonic-gate 	if (old->sc_repo_filename != NULL)
3860Sstevel@tonic-gate 		est->sc_repo_filename = safe_strdup(old->sc_repo_filename);
3870Sstevel@tonic-gate 	if (old->sc_repo_doordir != NULL)
3880Sstevel@tonic-gate 		est->sc_repo_doordir = safe_strdup(old->sc_repo_doordir);
3890Sstevel@tonic-gate 	if (old->sc_repo_doorname != NULL)
3900Sstevel@tonic-gate 		est->sc_repo_doorname = safe_strdup(old->sc_repo_doorname);
3910Sstevel@tonic-gate 	if (old->sc_repo_server != NULL)
3920Sstevel@tonic-gate 		est->sc_repo_server = safe_strdup(old->sc_repo_server);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	/* set up the new guy */
3950Sstevel@tonic-gate 	est->sc_cmd_lineno = 1;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (dont_exit)
3980Sstevel@tonic-gate 		est->sc_cmd_flags |= SC_CMD_DONT_EXIT;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	if (strcmp(name, "-") == 0) {
4010Sstevel@tonic-gate 		est->sc_cmd_file = stdin;
4020Sstevel@tonic-gate 		est->sc_cmd_filename = "<stdin>";
4030Sstevel@tonic-gate 	} else {
4040Sstevel@tonic-gate 		errno = 0;
4050Sstevel@tonic-gate 		est->sc_cmd_filename = name;
4060Sstevel@tonic-gate 		est->sc_cmd_file = fopen(name, "r");
4070Sstevel@tonic-gate 		if (est->sc_cmd_file == NULL) {
4080Sstevel@tonic-gate 			if (errno == 0)
4090Sstevel@tonic-gate 				semerr(gettext("No free stdio streams.\n"));
4100Sstevel@tonic-gate 			else
4110Sstevel@tonic-gate 				semerr(gettext("Could not open %s"), name);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 			ret = -1;
4140Sstevel@tonic-gate 			goto fail;
4150Sstevel@tonic-gate 		}
4160Sstevel@tonic-gate 
4175040Swesolows 		do {
4180Sstevel@tonic-gate 			ret = fstat(fileno(est->sc_cmd_file), &st);
4195040Swesolows 		} while (ret != 0 && errno == EINTR);
4200Sstevel@tonic-gate 		if (ret != 0) {
4210Sstevel@tonic-gate 			(void) fclose(est->sc_cmd_file);
4220Sstevel@tonic-gate 			est->sc_cmd_file = NULL;	/* for semerr() */
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 			semerr(gettext("Could not stat %s"), name);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 			ret = -1;
4270Sstevel@tonic-gate 			goto fail;
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
4310Sstevel@tonic-gate 			(void) fclose(est->sc_cmd_file);
4320Sstevel@tonic-gate 			est->sc_cmd_file = NULL;	/* for semerr() */
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 			semerr(gettext("%s is not a regular file.\n"), name);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 			ret = -1;
4370Sstevel@tonic-gate 			goto fail;
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	(void) yyparse();
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	if (est->sc_cmd_file != stdin)
4440Sstevel@tonic-gate 		(void) fclose(est->sc_cmd_file);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	ret = 0;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate fail:
4490Sstevel@tonic-gate 	if (est->sc_repo_pid != old->sc_repo_pid)
4500Sstevel@tonic-gate 		lscf_cleanup();		/* clean up any new repository */
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	if (est->sc_repo_filename != NULL)
4530Sstevel@tonic-gate 		free((void *)est->sc_repo_filename);
4540Sstevel@tonic-gate 	if (est->sc_repo_doordir != NULL)
4550Sstevel@tonic-gate 		free((void *)est->sc_repo_doordir);
4560Sstevel@tonic-gate 	if (est->sc_repo_doorname != NULL)
4570Sstevel@tonic-gate 		free((void *)est->sc_repo_doorname);
4580Sstevel@tonic-gate 	if (est->sc_repo_server != NULL)
4590Sstevel@tonic-gate 		free((void *)est->sc_repo_server);
4600Sstevel@tonic-gate 	free(est);
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	est = old;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	return (ret);
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate /*
4680Sstevel@tonic-gate  * Initialize svccfg state.  We recognize four environment variables:
4690Sstevel@tonic-gate  *
4700Sstevel@tonic-gate  * SVCCFG_REPOSITORY	Create a private instance of svc.configd(1M) to answer
4710Sstevel@tonic-gate  *			requests for the specified repository file.
4720Sstevel@tonic-gate  * SVCCFG_DOOR_PATH	Directory for door creation.
4730Sstevel@tonic-gate  *
4740Sstevel@tonic-gate  * SVCCFG_DOOR		Rendezvous via an alternative repository door.
4750Sstevel@tonic-gate  *
4760Sstevel@tonic-gate  * SVCCFG_CONFIGD_PATH	Resolvable path to alternative svc.configd(1M) binary.
4770Sstevel@tonic-gate  */
4780Sstevel@tonic-gate void
4790Sstevel@tonic-gate engine_init()
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate 	const char *cp;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	est = uu_zalloc(sizeof (engine_state_t));
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	est->sc_cmd_lineno = 1;
4860Sstevel@tonic-gate 	est->sc_repo_pid = -1;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	cp = getenv("SVCCFG_REPOSITORY");
4890Sstevel@tonic-gate 	est->sc_repo_filename = cp ? safe_strdup(cp) : NULL;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	cp = getenv("SVCCFG_DOOR_PATH");
4920Sstevel@tonic-gate 	est->sc_repo_doordir = cp ? cp : "/var/run";
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	cp = getenv("SVCCFG_DOOR");
4950Sstevel@tonic-gate 	if (cp != NULL) {
4960Sstevel@tonic-gate 		if (est->sc_repo_filename != NULL) {
4970Sstevel@tonic-gate 			uu_warn(gettext("SVCCFG_DOOR unused when "
4980Sstevel@tonic-gate 			    "SVCCFG_REPOSITORY specified\n"));
4990Sstevel@tonic-gate 		} else {
5000Sstevel@tonic-gate 			est->sc_repo_doorname = safe_strdup(cp);
5010Sstevel@tonic-gate 		}
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	cp = getenv("SVCCFG_CONFIGD_PATH");
5050Sstevel@tonic-gate 	est->sc_repo_server = cp ? cp : "/lib/svc/bin/svc.configd";
50611996SThomas.Whitten@Sun.COM 
50711996SThomas.Whitten@Sun.COM 	est->sc_in_emi = 0;
50811996SThomas.Whitten@Sun.COM 	cp = getenv("SMF_FMRI");
50911996SThomas.Whitten@Sun.COM 	if ((cp != NULL) && (strcmp(cp, SCF_INSTANCE_EMI) == 0))
51011996SThomas.Whitten@Sun.COM 		est->sc_in_emi = 1;
51111996SThomas.Whitten@Sun.COM 
51211996SThomas.Whitten@Sun.COM 	cp = smf_get_state(SCF_INSTANCE_FS_MINIMAL);
51311996SThomas.Whitten@Sun.COM 	if (cp && (strcmp(cp, SCF_STATE_STRING_ONLINE) == 0))
51411996SThomas.Whitten@Sun.COM 		est->sc_fs_minimal = B_TRUE;
51511996SThomas.Whitten@Sun.COM 	free((void *) cp);
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate 
51811996SThomas.Whitten@Sun.COM static int
51911996SThomas.Whitten@Sun.COM import_manifest_file(manifest_info_t *info, boolean_t validate, FILE *pout,
52011996SThomas.Whitten@Sun.COM     uint_t flags)
52111996SThomas.Whitten@Sun.COM {
52211996SThomas.Whitten@Sun.COM 	bundle_t *b;
52311996SThomas.Whitten@Sun.COM 	tmpl_errors_t *errs;
52411996SThomas.Whitten@Sun.COM 	const char *file;
52511996SThomas.Whitten@Sun.COM 	tmpl_validate_status_t vr;
52611996SThomas.Whitten@Sun.COM 
52711996SThomas.Whitten@Sun.COM 	file = info->mi_path;
52811996SThomas.Whitten@Sun.COM 
52911996SThomas.Whitten@Sun.COM 	/* Load the manifest */
53011996SThomas.Whitten@Sun.COM 	b = internal_bundle_new();
53111996SThomas.Whitten@Sun.COM 
53211996SThomas.Whitten@Sun.COM 	if (lxml_get_bundle_file(b, file, SVCCFG_OP_IMPORT) != 0) {
53311996SThomas.Whitten@Sun.COM 		internal_bundle_free(b);
53411996SThomas.Whitten@Sun.COM 		return (-1);
53511996SThomas.Whitten@Sun.COM 	}
53611996SThomas.Whitten@Sun.COM 
53711996SThomas.Whitten@Sun.COM 	/* Validate */
53811996SThomas.Whitten@Sun.COM 	if ((vr = tmpl_validate_bundle(b, &errs)) != TVS_SUCCESS) {
53911996SThomas.Whitten@Sun.COM 		char *prefix;
54011996SThomas.Whitten@Sun.COM 
54111996SThomas.Whitten@Sun.COM 		if ((validate == 0) || (vr == TVS_WARN)) {
54211996SThomas.Whitten@Sun.COM 			prefix = gettext("Warning: ");
54311996SThomas.Whitten@Sun.COM 		} else {
54411996SThomas.Whitten@Sun.COM 			prefix = "";
54511996SThomas.Whitten@Sun.COM 		}
54611996SThomas.Whitten@Sun.COM 		tmpl_errors_print(stderr, errs, prefix);
54711996SThomas.Whitten@Sun.COM 		if (validate && (vr != TVS_WARN)) {
54811996SThomas.Whitten@Sun.COM 			tmpl_errors_destroy(errs);
54911996SThomas.Whitten@Sun.COM 			semerr(gettext("Import of %s failed.\n"),
55011996SThomas.Whitten@Sun.COM 			    info->mi_path);
55111996SThomas.Whitten@Sun.COM 			if (pout != NULL) {
55211996SThomas.Whitten@Sun.COM 				(void) fprintf(pout, gettext("WARNING: svccfg "
55311996SThomas.Whitten@Sun.COM 				    "import of %s failed.\n"), info->mi_path);
55411996SThomas.Whitten@Sun.COM 			}
55511996SThomas.Whitten@Sun.COM 
55611996SThomas.Whitten@Sun.COM 			return (-1);
55711996SThomas.Whitten@Sun.COM 		}
55811996SThomas.Whitten@Sun.COM 	}
55911996SThomas.Whitten@Sun.COM 	tmpl_errors_destroy(errs);
56011996SThomas.Whitten@Sun.COM 
56111996SThomas.Whitten@Sun.COM 	/* Import */
56211996SThomas.Whitten@Sun.COM 	if (lscf_bundle_import(b, file, flags) != 0) {
56311996SThomas.Whitten@Sun.COM 		internal_bundle_free(b);
56411996SThomas.Whitten@Sun.COM 		semerr(gettext("Import of %s failed.\n"), info->mi_path);
56511996SThomas.Whitten@Sun.COM 		if (pout != NULL) {
56611996SThomas.Whitten@Sun.COM 			(void) fprintf(pout, gettext("WARNING: svccfg import "
56711996SThomas.Whitten@Sun.COM 			    "of %s failed.\n"), info->mi_path);
56811996SThomas.Whitten@Sun.COM 		}
56911996SThomas.Whitten@Sun.COM 		return (-1);
57011996SThomas.Whitten@Sun.COM 	}
57111996SThomas.Whitten@Sun.COM 
57211996SThomas.Whitten@Sun.COM 	internal_bundle_free(b);
57311996SThomas.Whitten@Sun.COM 
57411996SThomas.Whitten@Sun.COM 	if (info->mi_prop) {
57511996SThomas.Whitten@Sun.COM 		char *errstr;
57611996SThomas.Whitten@Sun.COM 
57711996SThomas.Whitten@Sun.COM 		if (mhash_store_entry(g_hndl, info->mi_prop, file,
57811996SThomas.Whitten@Sun.COM 		    info->mi_hash, APPLY_NONE, &errstr)) {
57911996SThomas.Whitten@Sun.COM 			if (errstr)
58011996SThomas.Whitten@Sun.COM 				semerr(gettext("Could not store hash for %s. "
58111996SThomas.Whitten@Sun.COM 				    "%s\n"), info->mi_path, errstr);
58211996SThomas.Whitten@Sun.COM 			else
58311996SThomas.Whitten@Sun.COM 				semerr(gettext("Unknown error from "
58411996SThomas.Whitten@Sun.COM 				    "mhash_store_entry() for %s\n"),
58511996SThomas.Whitten@Sun.COM 				    info->mi_path);
58611996SThomas.Whitten@Sun.COM 		}
58711996SThomas.Whitten@Sun.COM 
58811996SThomas.Whitten@Sun.COM 	}
58911996SThomas.Whitten@Sun.COM 
59011996SThomas.Whitten@Sun.COM 	return (0);
59111996SThomas.Whitten@Sun.COM }
59211996SThomas.Whitten@Sun.COM 
59311996SThomas.Whitten@Sun.COM /*
59411996SThomas.Whitten@Sun.COM  * Return values:
59511996SThomas.Whitten@Sun.COM  *	1	No manifests need to be imported.
59611996SThomas.Whitten@Sun.COM  *	0	Success
59711996SThomas.Whitten@Sun.COM  *	-1	Error
59811996SThomas.Whitten@Sun.COM  *	-2	Syntax error
59911996SThomas.Whitten@Sun.COM  */
6000Sstevel@tonic-gate int
6010Sstevel@tonic-gate engine_import(uu_list_t *args)
6020Sstevel@tonic-gate {
60311996SThomas.Whitten@Sun.COM 	int argc, i, o;
60411996SThomas.Whitten@Sun.COM 	int dont_exit;
60511996SThomas.Whitten@Sun.COM 	int failed_manifests;
60611996SThomas.Whitten@Sun.COM 	int total_manifests;
60711996SThomas.Whitten@Sun.COM 	char *file;
60811996SThomas.Whitten@Sun.COM 	char **argv = NULL;
6090Sstevel@tonic-gate 	string_list_t *slp;
6107887SLiane.Praza@Sun.COM 	boolean_t validate = B_FALSE;
6110Sstevel@tonic-gate 	uint_t flags = SCI_GENERALLAST;
61211996SThomas.Whitten@Sun.COM 	int dirarg = 0;
61311996SThomas.Whitten@Sun.COM 	int isdir;
61411996SThomas.Whitten@Sun.COM 	int rc = -1;
61511996SThomas.Whitten@Sun.COM 	struct stat sb;
61611996SThomas.Whitten@Sun.COM 	char **paths;
61711996SThomas.Whitten@Sun.COM 	manifest_info_t ***manifest_sets = NULL;
61811996SThomas.Whitten@Sun.COM 	manifest_info_t **manifests;
61911996SThomas.Whitten@Sun.COM 	char *progress_file = NULL;
62011996SThomas.Whitten@Sun.COM 	FILE *progress_out = NULL;
62111996SThomas.Whitten@Sun.COM 	int progress_count;
62211996SThomas.Whitten@Sun.COM 	int back_count;
62311996SThomas.Whitten@Sun.COM 	int count;
62411996SThomas.Whitten@Sun.COM 	int fm_flags;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	argc = uu_list_numnodes(args);
6270Sstevel@tonic-gate 	if (argc < 1)
6280Sstevel@tonic-gate 		return (-2);
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	argv = calloc(argc + 1, sizeof (char *));
6310Sstevel@tonic-gate 	if (argv == NULL)
6320Sstevel@tonic-gate 		uu_die(gettext("Out of memory.\n"));
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	for (slp = uu_list_first(args), i = 0;
6350Sstevel@tonic-gate 	    slp != NULL;
6360Sstevel@tonic-gate 	    slp = uu_list_next(args, slp), ++i)
6370Sstevel@tonic-gate 		argv[i] = slp->str;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	argv[i] = NULL;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	opterr = 0;
6420Sstevel@tonic-gate 	optind = 0;				/* Remember, no argv[0]. */
6430Sstevel@tonic-gate 	for (;;) {
64411996SThomas.Whitten@Sun.COM 		o = getopt(argc, argv, "np:V");
6450Sstevel@tonic-gate 		if (o == -1)
6460Sstevel@tonic-gate 			break;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		switch (o) {
6490Sstevel@tonic-gate 		case 'n':
6500Sstevel@tonic-gate 			flags |= SCI_NOREFRESH;
6510Sstevel@tonic-gate 			break;
6520Sstevel@tonic-gate 
65311996SThomas.Whitten@Sun.COM 		case 'p':
65411996SThomas.Whitten@Sun.COM 			progress_file = optarg;
65511996SThomas.Whitten@Sun.COM 			break;
65611996SThomas.Whitten@Sun.COM 
6570Sstevel@tonic-gate 		case 'V':
6587887SLiane.Praza@Sun.COM 			validate = B_TRUE;
6590Sstevel@tonic-gate 			break;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 		case '?':
6620Sstevel@tonic-gate 			free(argv);
6630Sstevel@tonic-gate 			return (-2);
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 		default:
6660Sstevel@tonic-gate 			bad_error("getopt", o);
6670Sstevel@tonic-gate 		}
6680Sstevel@tonic-gate 	}
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	argc -= optind;
67111996SThomas.Whitten@Sun.COM 	if (argc < 1) {
6720Sstevel@tonic-gate 		free(argv);
6730Sstevel@tonic-gate 		return (-2);
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 
67611996SThomas.Whitten@Sun.COM 	/* Open device for progress messages */
67711996SThomas.Whitten@Sun.COM 	if (progress_file != NULL) {
67811996SThomas.Whitten@Sun.COM 		if (strcmp(progress_file, "-") == 0) {
67911996SThomas.Whitten@Sun.COM 			progress_out = stdout;
68011996SThomas.Whitten@Sun.COM 		} else {
68111996SThomas.Whitten@Sun.COM 			progress_out = fopen(progress_file, "w");
68211996SThomas.Whitten@Sun.COM 			if (progress_out == NULL) {
68311996SThomas.Whitten@Sun.COM 				semerr(gettext("Unable to open %s for "
68411996SThomas.Whitten@Sun.COM 				    "progress reporting.  %s\n"),
68511996SThomas.Whitten@Sun.COM 				    progress_file, strerror(errno));
68611996SThomas.Whitten@Sun.COM 				goto out;
68711996SThomas.Whitten@Sun.COM 			}
68811996SThomas.Whitten@Sun.COM 			setbuf(progress_out, NULL);
68911996SThomas.Whitten@Sun.COM 		}
69011996SThomas.Whitten@Sun.COM 	}
69111996SThomas.Whitten@Sun.COM 
69211996SThomas.Whitten@Sun.COM 	paths = argv+optind;
69311996SThomas.Whitten@Sun.COM 	manifest_sets = safe_malloc(argc * sizeof (*manifest_sets));
6940Sstevel@tonic-gate 
6957887SLiane.Praza@Sun.COM 	/* If we're in interactive mode, force strict validation. */
6967887SLiane.Praza@Sun.COM 	if (est->sc_cmd_flags & SC_CMD_IACTIVE)
6977887SLiane.Praza@Sun.COM 		validate = B_TRUE;
6987887SLiane.Praza@Sun.COM 
6990Sstevel@tonic-gate 	lscf_prep_hndl();
7000Sstevel@tonic-gate 
70111996SThomas.Whitten@Sun.COM 	/* Determine which manifests must be imported. */
70211996SThomas.Whitten@Sun.COM 
70311996SThomas.Whitten@Sun.COM 	total_manifests = 0;
70411996SThomas.Whitten@Sun.COM 	for (i = 0; i < argc; i++) {
70511996SThomas.Whitten@Sun.COM 		file = *(paths + i);
70611996SThomas.Whitten@Sun.COM 		fm_flags = CHECKHASH;
70711996SThomas.Whitten@Sun.COM 
70811996SThomas.Whitten@Sun.COM 		/* Determine if argument is a directory or file. */
70911996SThomas.Whitten@Sun.COM 		if (stat(file, &sb) == -1) {
71011996SThomas.Whitten@Sun.COM 			semerr(gettext("Unable to stat file %s.  %s\n"), file,
71111996SThomas.Whitten@Sun.COM 			    strerror(errno));
71211996SThomas.Whitten@Sun.COM 			goto out;
71311996SThomas.Whitten@Sun.COM 		}
71411996SThomas.Whitten@Sun.COM 		if (sb.st_mode & S_IFDIR) {
71511996SThomas.Whitten@Sun.COM 			fm_flags |= CHECKEXT;
71611996SThomas.Whitten@Sun.COM 			dirarg = 1;
71711996SThomas.Whitten@Sun.COM 			isdir = 1;
71811996SThomas.Whitten@Sun.COM 		} else if (sb.st_mode & S_IFREG) {
71911996SThomas.Whitten@Sun.COM 			isdir = 0;
72011996SThomas.Whitten@Sun.COM 		} else {
72111996SThomas.Whitten@Sun.COM 			semerr(gettext("%s is not a directory or regular "
72211996SThomas.Whitten@Sun.COM 			    "file\n"), file);
72311996SThomas.Whitten@Sun.COM 			goto out;
72411996SThomas.Whitten@Sun.COM 		}
72511996SThomas.Whitten@Sun.COM 
72611996SThomas.Whitten@Sun.COM 		/* Get list of manifests that we should import for this path. */
72711996SThomas.Whitten@Sun.COM 		if ((count = find_manifests(file, &manifests, fm_flags)) < 0) {
72811996SThomas.Whitten@Sun.COM 			if (isdir) {
72911996SThomas.Whitten@Sun.COM 				semerr(gettext("Could not hash directory %s\n"),
73011996SThomas.Whitten@Sun.COM 				    file);
73111996SThomas.Whitten@Sun.COM 			} else {
73211996SThomas.Whitten@Sun.COM 				semerr(gettext("Could not hash file %s\n"),
73311996SThomas.Whitten@Sun.COM 				    file);
73411996SThomas.Whitten@Sun.COM 			}
73511996SThomas.Whitten@Sun.COM 			free_manifest_array(manifests);
73611996SThomas.Whitten@Sun.COM 			goto out;
73711996SThomas.Whitten@Sun.COM 		}
73811996SThomas.Whitten@Sun.COM 		total_manifests += count;
73911996SThomas.Whitten@Sun.COM 		manifest_sets[i] = manifests;
7407887SLiane.Praza@Sun.COM 	}
7410Sstevel@tonic-gate 
74211996SThomas.Whitten@Sun.COM 	if (total_manifests == 0) {
74311996SThomas.Whitten@Sun.COM 		/* No manifests to process. */
74411996SThomas.Whitten@Sun.COM 		if (g_verbose) {
74511996SThomas.Whitten@Sun.COM 			warn(gettext("No changes were necessary\n"));
74611996SThomas.Whitten@Sun.COM 		}
74711996SThomas.Whitten@Sun.COM 		rc = 1;
74811996SThomas.Whitten@Sun.COM 		goto out;
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 
75111996SThomas.Whitten@Sun.COM 	/*
75211996SThomas.Whitten@Sun.COM 	 * If we're processing more than one file, we don't want to exit if
75311996SThomas.Whitten@Sun.COM 	 * we encounter an error.  We should go ahead and process all of
75411996SThomas.Whitten@Sun.COM 	 * the manifests.
75511996SThomas.Whitten@Sun.COM 	 */
75611996SThomas.Whitten@Sun.COM 	dont_exit = est->sc_cmd_flags & SC_CMD_DONT_EXIT;
75711996SThomas.Whitten@Sun.COM 	if (total_manifests > 1)
75811996SThomas.Whitten@Sun.COM 		est->sc_cmd_flags |= SC_CMD_DONT_EXIT;
75911996SThomas.Whitten@Sun.COM 
76011996SThomas.Whitten@Sun.COM 	if (progress_out != NULL)
76111996SThomas.Whitten@Sun.COM 		(void) fprintf(progress_out,
76211996SThomas.Whitten@Sun.COM 		    "Loading smf(5) service descriptions: ");
7637887SLiane.Praza@Sun.COM 
76411996SThomas.Whitten@Sun.COM 	failed_manifests = 0;
76511996SThomas.Whitten@Sun.COM 	progress_count = 0;
76611996SThomas.Whitten@Sun.COM 	for (i = 0; i < argc; i++) {
76711996SThomas.Whitten@Sun.COM 		manifests = manifest_sets[i];
76811996SThomas.Whitten@Sun.COM 		if (manifests == NULL)
76911996SThomas.Whitten@Sun.COM 			continue;
77011996SThomas.Whitten@Sun.COM 		for (; *manifests != NULL; manifests++) {
77111996SThomas.Whitten@Sun.COM 			progress_count++;
77211996SThomas.Whitten@Sun.COM 			if (progress_out != NULL) {
77311996SThomas.Whitten@Sun.COM 				back_count = fprintf(progress_out, "%d/%d",
77411996SThomas.Whitten@Sun.COM 				    progress_count, total_manifests);
77511996SThomas.Whitten@Sun.COM 				while (back_count-- > 0) {
77611996SThomas.Whitten@Sun.COM 					(void) fputc('\b', progress_out);
77711996SThomas.Whitten@Sun.COM 				}
77811996SThomas.Whitten@Sun.COM 			}
77911996SThomas.Whitten@Sun.COM 			if (import_manifest_file(*manifests, validate,
78011996SThomas.Whitten@Sun.COM 			    progress_out, flags) != 0) {
78111996SThomas.Whitten@Sun.COM 				failed_manifests++;
78211996SThomas.Whitten@Sun.COM 			}
7837887SLiane.Praza@Sun.COM 		}
7847887SLiane.Praza@Sun.COM 	}
78511996SThomas.Whitten@Sun.COM 	if (progress_out != NULL)
78611996SThomas.Whitten@Sun.COM 		(void) fputc('\n', progress_out);
78711996SThomas.Whitten@Sun.COM 
78811996SThomas.Whitten@Sun.COM 	if ((total_manifests > 1) && (dont_exit == 0))
78911996SThomas.Whitten@Sun.COM 		est->sc_cmd_flags &= ~SC_CMD_DONT_EXIT;
7907887SLiane.Praza@Sun.COM 
79111996SThomas.Whitten@Sun.COM 	if (dirarg && total_manifests > 0) {
79211996SThomas.Whitten@Sun.COM 		char *msg;
79311996SThomas.Whitten@Sun.COM 
79411996SThomas.Whitten@Sun.COM 		msg = "Loaded %d smf(5) service descriptions\n";
79511996SThomas.Whitten@Sun.COM 		warn(gettext(msg), progress_count);
79611996SThomas.Whitten@Sun.COM 
79711996SThomas.Whitten@Sun.COM 		if (failed_manifests) {
79811996SThomas.Whitten@Sun.COM 			msg = "%d smf(5) service descriptions failed to load\n";
79911996SThomas.Whitten@Sun.COM 			warn(gettext(msg), failed_manifests);
80011996SThomas.Whitten@Sun.COM 		}
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 
80311996SThomas.Whitten@Sun.COM 	if (failed_manifests > 0)
80411996SThomas.Whitten@Sun.COM 		goto out;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	if (g_verbose)
8070Sstevel@tonic-gate 		warn(gettext("Successful import.\n"));
80811996SThomas.Whitten@Sun.COM 	rc = 0;
8090Sstevel@tonic-gate 
81011996SThomas.Whitten@Sun.COM out:
81111996SThomas.Whitten@Sun.COM 	if ((progress_out != NULL) && (progress_out != stdout))
81211996SThomas.Whitten@Sun.COM 		(void) fclose(progress_out);
81311996SThomas.Whitten@Sun.COM 	free(argv);
81411996SThomas.Whitten@Sun.COM 	if (manifest_sets != NULL) {
81511996SThomas.Whitten@Sun.COM 		for (i = 0; i < argc; i++) {
81611996SThomas.Whitten@Sun.COM 			free_manifest_array(manifest_sets[i]);
81711996SThomas.Whitten@Sun.COM 		}
81811996SThomas.Whitten@Sun.COM 		free(manifest_sets);
81911996SThomas.Whitten@Sun.COM 	}
82011996SThomas.Whitten@Sun.COM 	return (rc);
82111996SThomas.Whitten@Sun.COM }
8220Sstevel@tonic-gate 
82311996SThomas.Whitten@Sun.COM /*
82411996SThomas.Whitten@Sun.COM  * Walk each service and get its manifest file.
82511996SThomas.Whitten@Sun.COM  *
82611996SThomas.Whitten@Sun.COM  * If the file exists check instance support, and cleanup any
82711996SThomas.Whitten@Sun.COM  * stale instances.
82811996SThomas.Whitten@Sun.COM  *
82911996SThomas.Whitten@Sun.COM  * If the file doesn't exist tear down the service and/or instances
83011996SThomas.Whitten@Sun.COM  * that are no longer supported by files.
83111996SThomas.Whitten@Sun.COM  */
83211996SThomas.Whitten@Sun.COM int
83311996SThomas.Whitten@Sun.COM engine_cleanup(int flags)
83411996SThomas.Whitten@Sun.COM {
83511996SThomas.Whitten@Sun.COM 	boolean_t		activity = B_TRUE;
83611996SThomas.Whitten@Sun.COM 	int			r = -1;
8370Sstevel@tonic-gate 
83811996SThomas.Whitten@Sun.COM 	lscf_prep_hndl();
83911996SThomas.Whitten@Sun.COM 
84011996SThomas.Whitten@Sun.COM 	if (flags == 1) {
84111996SThomas.Whitten@Sun.COM 		activity = B_FALSE;
8420Sstevel@tonic-gate 	}
8430Sstevel@tonic-gate 
84411996SThomas.Whitten@Sun.COM 	if (scf_walk_fmri(g_hndl, 0, NULL, SCF_WALK_SERVICE|SCF_WALK_NOINSTANCE,
84511996SThomas.Whitten@Sun.COM 	    lscf_service_cleanup, (void *)activity, NULL,
84611996SThomas.Whitten@Sun.COM 	    uu_warn) == SCF_SUCCESS)
84711996SThomas.Whitten@Sun.COM 		r = 0;
84811996SThomas.Whitten@Sun.COM 
84911996SThomas.Whitten@Sun.COM 	(void) lscf_hash_cleanup();
85011996SThomas.Whitten@Sun.COM 
85111996SThomas.Whitten@Sun.COM 	return (r);
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate 
854*12418STruong.Q.Nguyen@Sun.COM static int
855*12418STruong.Q.Nguyen@Sun.COM apply_profile(manifest_info_t *info, int apply_changes)
8560Sstevel@tonic-gate {
857*12418STruong.Q.Nguyen@Sun.COM 	bundle_t *b = internal_bundle_new();
8580Sstevel@tonic-gate 
859*12418STruong.Q.Nguyen@Sun.COM 	if (lxml_get_bundle_file(b, info->mi_path, SVCCFG_OP_APPLY) != 0) {
8600Sstevel@tonic-gate 		internal_bundle_free(b);
8610Sstevel@tonic-gate 		return (-1);
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate 
86410029SAntonello.Cruz@Sun.COM 	if (!apply_changes) {	/* we don't want to apply, just test */
86510029SAntonello.Cruz@Sun.COM 		internal_bundle_free(b);
86610029SAntonello.Cruz@Sun.COM 		return (0);
86710029SAntonello.Cruz@Sun.COM 	}
86810029SAntonello.Cruz@Sun.COM 
869*12418STruong.Q.Nguyen@Sun.COM 	if (lscf_bundle_apply(b, info->mi_path) != 0) {
8700Sstevel@tonic-gate 		internal_bundle_free(b);
8710Sstevel@tonic-gate 		return (-1);
8720Sstevel@tonic-gate 	}
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	internal_bundle_free(b);
8750Sstevel@tonic-gate 
876*12418STruong.Q.Nguyen@Sun.COM 	if (info->mi_prop) {
87711996SThomas.Whitten@Sun.COM 		apply_action_t apply;
8780Sstevel@tonic-gate 		char *errstr;
87911996SThomas.Whitten@Sun.COM 
88011996SThomas.Whitten@Sun.COM 		apply = (est->sc_in_emi == 1) ? APPLY_LATE : APPLY_NONE;
881*12418STruong.Q.Nguyen@Sun.COM 		if (mhash_store_entry(g_hndl, info->mi_prop, info->mi_path,
882*12418STruong.Q.Nguyen@Sun.COM 		    info->mi_hash, apply, &errstr)) {
8830Sstevel@tonic-gate 			semerr(errstr);
88411996SThomas.Whitten@Sun.COM 		}
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	return (0);
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate int
891*12418STruong.Q.Nguyen@Sun.COM engine_apply(const char *file, int apply_changes)
892*12418STruong.Q.Nguyen@Sun.COM {
893*12418STruong.Q.Nguyen@Sun.COM 	int rc = 0;
894*12418STruong.Q.Nguyen@Sun.COM 	int isdir;
895*12418STruong.Q.Nguyen@Sun.COM 	int dont_exit;
896*12418STruong.Q.Nguyen@Sun.COM 	int profile_count;
897*12418STruong.Q.Nguyen@Sun.COM 	int fm_flags;
898*12418STruong.Q.Nguyen@Sun.COM 	struct stat sb;
899*12418STruong.Q.Nguyen@Sun.COM 	manifest_info_t **profiles = NULL;
900*12418STruong.Q.Nguyen@Sun.COM 	manifest_info_t **entry;
901*12418STruong.Q.Nguyen@Sun.COM 	manifest_info_t *pfile;
902*12418STruong.Q.Nguyen@Sun.COM 
903*12418STruong.Q.Nguyen@Sun.COM 	lscf_prep_hndl();
904*12418STruong.Q.Nguyen@Sun.COM 
905*12418STruong.Q.Nguyen@Sun.COM 	/* Determine which profile(s) must be applied. */
906*12418STruong.Q.Nguyen@Sun.COM 
907*12418STruong.Q.Nguyen@Sun.COM 	profile_count = 0;
908*12418STruong.Q.Nguyen@Sun.COM 	fm_flags = BUNDLE_PROF | CHECKHASH;
909*12418STruong.Q.Nguyen@Sun.COM 
910*12418STruong.Q.Nguyen@Sun.COM 	/* Determine if argument is a directory or file. */
911*12418STruong.Q.Nguyen@Sun.COM 	if (stat(file, &sb) == -1) {
912*12418STruong.Q.Nguyen@Sun.COM 		semerr(gettext("Unable to stat file %s.  %s\n"), file,
913*12418STruong.Q.Nguyen@Sun.COM 		    strerror(errno));
914*12418STruong.Q.Nguyen@Sun.COM 		rc = -1;
915*12418STruong.Q.Nguyen@Sun.COM 		goto out;
916*12418STruong.Q.Nguyen@Sun.COM 	}
917*12418STruong.Q.Nguyen@Sun.COM 
918*12418STruong.Q.Nguyen@Sun.COM 	if (sb.st_mode & S_IFDIR) {
919*12418STruong.Q.Nguyen@Sun.COM 		fm_flags |= CHECKEXT;
920*12418STruong.Q.Nguyen@Sun.COM 		isdir = 1;
921*12418STruong.Q.Nguyen@Sun.COM 	} else if (sb.st_mode & S_IFREG) {
922*12418STruong.Q.Nguyen@Sun.COM 		isdir = 0;
923*12418STruong.Q.Nguyen@Sun.COM 	} else {
924*12418STruong.Q.Nguyen@Sun.COM 		semerr(gettext("%s is not a directory or regular "
925*12418STruong.Q.Nguyen@Sun.COM 		    "file\n"), file);
926*12418STruong.Q.Nguyen@Sun.COM 		rc = -1;
927*12418STruong.Q.Nguyen@Sun.COM 		goto out;
928*12418STruong.Q.Nguyen@Sun.COM 	}
929*12418STruong.Q.Nguyen@Sun.COM 
930*12418STruong.Q.Nguyen@Sun.COM 	/* Get list of profiles to be applied. */
931*12418STruong.Q.Nguyen@Sun.COM 	if ((profile_count = find_manifests(file, &profiles, fm_flags)) < 0) {
932*12418STruong.Q.Nguyen@Sun.COM 		if (isdir) {
933*12418STruong.Q.Nguyen@Sun.COM 			semerr(gettext("Could not hash directory %s\n"), file);
934*12418STruong.Q.Nguyen@Sun.COM 		} else {
935*12418STruong.Q.Nguyen@Sun.COM 			semerr(gettext("Could not hash file %s\n"), file);
936*12418STruong.Q.Nguyen@Sun.COM 		}
937*12418STruong.Q.Nguyen@Sun.COM 		rc = -1;
938*12418STruong.Q.Nguyen@Sun.COM 		goto out;
939*12418STruong.Q.Nguyen@Sun.COM 	}
940*12418STruong.Q.Nguyen@Sun.COM 
941*12418STruong.Q.Nguyen@Sun.COM 	if (profile_count == 0) {
942*12418STruong.Q.Nguyen@Sun.COM 		/* No profiles to process. */
943*12418STruong.Q.Nguyen@Sun.COM 		if (g_verbose) {
944*12418STruong.Q.Nguyen@Sun.COM 			warn(gettext("No changes were necessary\n"));
945*12418STruong.Q.Nguyen@Sun.COM 		}
946*12418STruong.Q.Nguyen@Sun.COM 		goto out;
947*12418STruong.Q.Nguyen@Sun.COM 	}
948*12418STruong.Q.Nguyen@Sun.COM 
949*12418STruong.Q.Nguyen@Sun.COM 	/*
950*12418STruong.Q.Nguyen@Sun.COM 	 * We don't want to exit if we encounter an error.  We should go ahead
951*12418STruong.Q.Nguyen@Sun.COM 	 * and process all of the profiles.
952*12418STruong.Q.Nguyen@Sun.COM 	 */
953*12418STruong.Q.Nguyen@Sun.COM 	dont_exit = est->sc_cmd_flags & SC_CMD_DONT_EXIT;
954*12418STruong.Q.Nguyen@Sun.COM 	est->sc_cmd_flags |= SC_CMD_DONT_EXIT;
955*12418STruong.Q.Nguyen@Sun.COM 
956*12418STruong.Q.Nguyen@Sun.COM 	for (entry = profiles; *entry != NULL; entry++) {
957*12418STruong.Q.Nguyen@Sun.COM 		pfile = *entry;
958*12418STruong.Q.Nguyen@Sun.COM 
959*12418STruong.Q.Nguyen@Sun.COM 		if (apply_profile(pfile, apply_changes) == 0) {
960*12418STruong.Q.Nguyen@Sun.COM 			if (g_verbose) {
961*12418STruong.Q.Nguyen@Sun.COM 				warn(gettext("Successfully applied: %s\n"),
962*12418STruong.Q.Nguyen@Sun.COM 				    pfile->mi_path);
963*12418STruong.Q.Nguyen@Sun.COM 			}
964*12418STruong.Q.Nguyen@Sun.COM 		} else {
965*12418STruong.Q.Nguyen@Sun.COM 			warn(gettext("WARNING: Failed to apply %s\n"),
966*12418STruong.Q.Nguyen@Sun.COM 			    pfile->mi_path);
967*12418STruong.Q.Nguyen@Sun.COM 			rc = -1;
968*12418STruong.Q.Nguyen@Sun.COM 		}
969*12418STruong.Q.Nguyen@Sun.COM 	}
970*12418STruong.Q.Nguyen@Sun.COM 
971*12418STruong.Q.Nguyen@Sun.COM 	if (dont_exit == 0)
972*12418STruong.Q.Nguyen@Sun.COM 		est->sc_cmd_flags &= ~SC_CMD_DONT_EXIT;
973*12418STruong.Q.Nguyen@Sun.COM 
974*12418STruong.Q.Nguyen@Sun.COM 	/* exit(1) appropriately if any profile failed to be applied. */
975*12418STruong.Q.Nguyen@Sun.COM 	if ((rc == -1) &&
976*12418STruong.Q.Nguyen@Sun.COM 	    (est->sc_cmd_flags & (SC_CMD_IACTIVE | SC_CMD_DONT_EXIT)) == 0) {
977*12418STruong.Q.Nguyen@Sun.COM 		free_manifest_array(profiles);
978*12418STruong.Q.Nguyen@Sun.COM 		exit(1);
979*12418STruong.Q.Nguyen@Sun.COM 	}
980*12418STruong.Q.Nguyen@Sun.COM 
981*12418STruong.Q.Nguyen@Sun.COM out:
982*12418STruong.Q.Nguyen@Sun.COM 	free_manifest_array(profiles);
983*12418STruong.Q.Nguyen@Sun.COM 	return (rc);
984*12418STruong.Q.Nguyen@Sun.COM }
985*12418STruong.Q.Nguyen@Sun.COM 
986*12418STruong.Q.Nguyen@Sun.COM int
9875040Swesolows engine_restore(const char *file)
9885040Swesolows {
9895040Swesolows 	bundle_t *b;
9905040Swesolows 
9915040Swesolows 	lscf_prep_hndl();
9925040Swesolows 
9935040Swesolows 	b = internal_bundle_new();
9945040Swesolows 
9955040Swesolows 	if (lxml_get_bundle_file(b, file, SVCCFG_OP_RESTORE) != 0) {
9965040Swesolows 		internal_bundle_free(b);
9975040Swesolows 		return (-1);
9985040Swesolows 	}
9995040Swesolows 
10005040Swesolows 	if (lscf_bundle_import(b, file, SCI_NOSNAP) != 0) {
10015040Swesolows 		internal_bundle_free(b);
10025040Swesolows 		return (-1);
10035040Swesolows 	}
10045040Swesolows 
10055040Swesolows 	internal_bundle_free(b);
10065040Swesolows 
10075040Swesolows 	return (0);
10085040Swesolows }
10095040Swesolows 
10105040Swesolows int
10110Sstevel@tonic-gate engine_set(uu_list_t *args)
10120Sstevel@tonic-gate {
10130Sstevel@tonic-gate 	uu_list_walk_t *walk;
10140Sstevel@tonic-gate 	string_list_t *slp;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	if (uu_list_first(args) == NULL) {
10170Sstevel@tonic-gate 		/* Display current options. */
10180Sstevel@tonic-gate 		if (!g_verbose)
10190Sstevel@tonic-gate 			(void) fputs("no", stdout);
10200Sstevel@tonic-gate 		(void) puts("verbose");
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 		return (0);
10230Sstevel@tonic-gate 	}
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	walk = uu_list_walk_start(args, UU_DEFAULT);
10260Sstevel@tonic-gate 	if (walk == NULL)
10270Sstevel@tonic-gate 		uu_die(gettext("Couldn't read arguments"));
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	/* Use getopt? */
10300Sstevel@tonic-gate 	for (slp = uu_list_walk_next(walk);
10310Sstevel@tonic-gate 	    slp != NULL;
10320Sstevel@tonic-gate 	    slp = uu_list_walk_next(walk)) {
10330Sstevel@tonic-gate 		if (slp->str[0] == '-') {
10340Sstevel@tonic-gate 			char *op;
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 			for (op = &slp->str[1]; *op != '\0'; ++op) {
10370Sstevel@tonic-gate 				switch (*op) {
10380Sstevel@tonic-gate 				case 'v':
10390Sstevel@tonic-gate 					g_verbose = 1;
10400Sstevel@tonic-gate 					break;
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 				case 'V':
10430Sstevel@tonic-gate 					g_verbose = 0;
10440Sstevel@tonic-gate 					break;
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 				default:
10470Sstevel@tonic-gate 					warn(gettext("Unknown option -%c.\n"),
10480Sstevel@tonic-gate 					    *op);
10490Sstevel@tonic-gate 				}
10500Sstevel@tonic-gate 			}
10510Sstevel@tonic-gate 		} else {
10520Sstevel@tonic-gate 			warn(gettext("No non-flag arguments defined.\n"));
10530Sstevel@tonic-gate 		}
10540Sstevel@tonic-gate 	}
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	return (0);
10570Sstevel@tonic-gate }
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate void
10600Sstevel@tonic-gate help(int com)
10610Sstevel@tonic-gate {
10620Sstevel@tonic-gate 	int i;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	if (com == 0) {
10650Sstevel@tonic-gate 		warn(gettext("General commands:	 help set repository end\n"
10660Sstevel@tonic-gate 		    "Manifest commands:	 inventory validate import export "
10670Sstevel@tonic-gate 		    "archive\n"
10680Sstevel@tonic-gate 		    "Profile commands:	 apply extract\n"
10697887SLiane.Praza@Sun.COM 		    "Entity commands:	 list select unselect add delete "
10707887SLiane.Praza@Sun.COM 		    "describe\n"
10710Sstevel@tonic-gate 		    "Snapshot commands:	 listsnap selectsnap revert\n"
10725841Samaguire 		    "Instance commands:	 refresh\n"
10730Sstevel@tonic-gate 		    "Property group commands: listpg addpg delpg\n"
10740Sstevel@tonic-gate 		    "Property commands:	 listprop setprop delprop editprop\n"
10750Sstevel@tonic-gate 		    "Property value commands: addpropvalue delpropvalue "
10760Sstevel@tonic-gate 		    "setenv unsetenv\n"));
10770Sstevel@tonic-gate 		return;
10780Sstevel@tonic-gate 	}
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	for (i = 0; help_messages[i].message != NULL; ++i) {
10810Sstevel@tonic-gate 		if (help_messages[i].token == com) {
10820Sstevel@tonic-gate 			warn(gettext("Usage: %s\n"),
10830Sstevel@tonic-gate 			    gettext(help_messages[i].message));
10840Sstevel@tonic-gate 			return;
10850Sstevel@tonic-gate 		}
10860Sstevel@tonic-gate 	}
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	warn(gettext("Unknown command.\n"));
10890Sstevel@tonic-gate }
1090