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 /*
2312418STruong.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
engine_cmd_getc(engine_state_t * E)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
engine_cmd_ungetc(engine_state_t * E,char c)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
engine_cmd_nputs(engine_state_t * E,char * c,size_t n)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
engine_exec(char * cmd)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
CPL_CHECK_FN(check_xml)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
CPL_MATCH_FN(complete_single_xml_file_arg)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
add_cmd_matches(WordCompletion * cpl,const char * line,int word_end,uint32_t scope)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
CPL_MATCH_FN(complete)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
engine_interp()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
engine_source(const char * name,boolean_t dont_exit)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
engine_init()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
50712667SSean.Wilcox@Sun.COM est->sc_miss_type = B_FALSE;
50811996SThomas.Whitten@Sun.COM est->sc_in_emi = 0;
50911996SThomas.Whitten@Sun.COM cp = getenv("SMF_FMRI");
51011996SThomas.Whitten@Sun.COM if ((cp != NULL) && (strcmp(cp, SCF_INSTANCE_EMI) == 0))
51111996SThomas.Whitten@Sun.COM est->sc_in_emi = 1;
51211996SThomas.Whitten@Sun.COM
51311996SThomas.Whitten@Sun.COM cp = smf_get_state(SCF_INSTANCE_FS_MINIMAL);
51411996SThomas.Whitten@Sun.COM if (cp && (strcmp(cp, SCF_STATE_STRING_ONLINE) == 0))
51511996SThomas.Whitten@Sun.COM est->sc_fs_minimal = B_TRUE;
51611996SThomas.Whitten@Sun.COM free((void *) cp);
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate
51911996SThomas.Whitten@Sun.COM static int
import_manifest_file(manifest_info_t * info,boolean_t validate,FILE * pout,uint_t flags)52011996SThomas.Whitten@Sun.COM import_manifest_file(manifest_info_t *info, boolean_t validate, FILE *pout,
52111996SThomas.Whitten@Sun.COM uint_t flags)
52211996SThomas.Whitten@Sun.COM {
52311996SThomas.Whitten@Sun.COM bundle_t *b;
52411996SThomas.Whitten@Sun.COM tmpl_errors_t *errs;
52511996SThomas.Whitten@Sun.COM const char *file;
52611996SThomas.Whitten@Sun.COM tmpl_validate_status_t vr;
52711996SThomas.Whitten@Sun.COM
52811996SThomas.Whitten@Sun.COM file = info->mi_path;
52911996SThomas.Whitten@Sun.COM
53011996SThomas.Whitten@Sun.COM /* Load the manifest */
53111996SThomas.Whitten@Sun.COM b = internal_bundle_new();
53211996SThomas.Whitten@Sun.COM
53311996SThomas.Whitten@Sun.COM if (lxml_get_bundle_file(b, file, SVCCFG_OP_IMPORT) != 0) {
53411996SThomas.Whitten@Sun.COM internal_bundle_free(b);
53511996SThomas.Whitten@Sun.COM return (-1);
53611996SThomas.Whitten@Sun.COM }
53711996SThomas.Whitten@Sun.COM
53811996SThomas.Whitten@Sun.COM /* Validate */
53911996SThomas.Whitten@Sun.COM if ((vr = tmpl_validate_bundle(b, &errs)) != TVS_SUCCESS) {
54011996SThomas.Whitten@Sun.COM char *prefix;
54111996SThomas.Whitten@Sun.COM
54211996SThomas.Whitten@Sun.COM if ((validate == 0) || (vr == TVS_WARN)) {
54311996SThomas.Whitten@Sun.COM prefix = gettext("Warning: ");
54411996SThomas.Whitten@Sun.COM } else {
54511996SThomas.Whitten@Sun.COM prefix = "";
54611996SThomas.Whitten@Sun.COM }
54711996SThomas.Whitten@Sun.COM tmpl_errors_print(stderr, errs, prefix);
54811996SThomas.Whitten@Sun.COM if (validate && (vr != TVS_WARN)) {
54911996SThomas.Whitten@Sun.COM tmpl_errors_destroy(errs);
55011996SThomas.Whitten@Sun.COM semerr(gettext("Import of %s failed.\n"),
55111996SThomas.Whitten@Sun.COM info->mi_path);
55211996SThomas.Whitten@Sun.COM if (pout != NULL) {
55311996SThomas.Whitten@Sun.COM (void) fprintf(pout, gettext("WARNING: svccfg "
55411996SThomas.Whitten@Sun.COM "import of %s failed.\n"), info->mi_path);
55511996SThomas.Whitten@Sun.COM }
55611996SThomas.Whitten@Sun.COM
55711996SThomas.Whitten@Sun.COM return (-1);
55811996SThomas.Whitten@Sun.COM }
55911996SThomas.Whitten@Sun.COM }
56011996SThomas.Whitten@Sun.COM tmpl_errors_destroy(errs);
56111996SThomas.Whitten@Sun.COM
56211996SThomas.Whitten@Sun.COM /* Import */
56311996SThomas.Whitten@Sun.COM if (lscf_bundle_import(b, file, flags) != 0) {
56411996SThomas.Whitten@Sun.COM internal_bundle_free(b);
56511996SThomas.Whitten@Sun.COM semerr(gettext("Import of %s failed.\n"), info->mi_path);
56611996SThomas.Whitten@Sun.COM if (pout != NULL) {
56711996SThomas.Whitten@Sun.COM (void) fprintf(pout, gettext("WARNING: svccfg import "
56811996SThomas.Whitten@Sun.COM "of %s failed.\n"), info->mi_path);
56911996SThomas.Whitten@Sun.COM }
57011996SThomas.Whitten@Sun.COM return (-1);
57111996SThomas.Whitten@Sun.COM }
57211996SThomas.Whitten@Sun.COM
57311996SThomas.Whitten@Sun.COM internal_bundle_free(b);
57411996SThomas.Whitten@Sun.COM
57511996SThomas.Whitten@Sun.COM if (info->mi_prop) {
57611996SThomas.Whitten@Sun.COM char *errstr;
57711996SThomas.Whitten@Sun.COM
57811996SThomas.Whitten@Sun.COM if (mhash_store_entry(g_hndl, info->mi_prop, file,
57911996SThomas.Whitten@Sun.COM info->mi_hash, APPLY_NONE, &errstr)) {
58011996SThomas.Whitten@Sun.COM if (errstr)
58111996SThomas.Whitten@Sun.COM semerr(gettext("Could not store hash for %s. "
58211996SThomas.Whitten@Sun.COM "%s\n"), info->mi_path, errstr);
58311996SThomas.Whitten@Sun.COM else
58411996SThomas.Whitten@Sun.COM semerr(gettext("Unknown error from "
58511996SThomas.Whitten@Sun.COM "mhash_store_entry() for %s\n"),
58611996SThomas.Whitten@Sun.COM info->mi_path);
58711996SThomas.Whitten@Sun.COM }
58811996SThomas.Whitten@Sun.COM
58911996SThomas.Whitten@Sun.COM }
59011996SThomas.Whitten@Sun.COM
59111996SThomas.Whitten@Sun.COM return (0);
59211996SThomas.Whitten@Sun.COM }
59311996SThomas.Whitten@Sun.COM
59411996SThomas.Whitten@Sun.COM /*
59511996SThomas.Whitten@Sun.COM * Return values:
59611996SThomas.Whitten@Sun.COM * 1 No manifests need to be imported.
59711996SThomas.Whitten@Sun.COM * 0 Success
59811996SThomas.Whitten@Sun.COM * -1 Error
59911996SThomas.Whitten@Sun.COM * -2 Syntax error
60011996SThomas.Whitten@Sun.COM */
6010Sstevel@tonic-gate int
engine_import(uu_list_t * args)6020Sstevel@tonic-gate engine_import(uu_list_t *args)
6030Sstevel@tonic-gate {
60411996SThomas.Whitten@Sun.COM int argc, i, o;
60511996SThomas.Whitten@Sun.COM int dont_exit;
60611996SThomas.Whitten@Sun.COM int failed_manifests;
60711996SThomas.Whitten@Sun.COM int total_manifests;
60811996SThomas.Whitten@Sun.COM char *file;
60911996SThomas.Whitten@Sun.COM char **argv = NULL;
6100Sstevel@tonic-gate string_list_t *slp;
6117887SLiane.Praza@Sun.COM boolean_t validate = B_FALSE;
6120Sstevel@tonic-gate uint_t flags = SCI_GENERALLAST;
61311996SThomas.Whitten@Sun.COM int dirarg = 0;
61411996SThomas.Whitten@Sun.COM int isdir;
61511996SThomas.Whitten@Sun.COM int rc = -1;
61611996SThomas.Whitten@Sun.COM struct stat sb;
61711996SThomas.Whitten@Sun.COM char **paths;
61811996SThomas.Whitten@Sun.COM manifest_info_t ***manifest_sets = NULL;
61911996SThomas.Whitten@Sun.COM manifest_info_t **manifests;
62011996SThomas.Whitten@Sun.COM char *progress_file = NULL;
62111996SThomas.Whitten@Sun.COM FILE *progress_out = NULL;
62211996SThomas.Whitten@Sun.COM int progress_count;
62311996SThomas.Whitten@Sun.COM int back_count;
62411996SThomas.Whitten@Sun.COM int count;
62511996SThomas.Whitten@Sun.COM int fm_flags;
6260Sstevel@tonic-gate
6270Sstevel@tonic-gate argc = uu_list_numnodes(args);
6280Sstevel@tonic-gate if (argc < 1)
6290Sstevel@tonic-gate return (-2);
6300Sstevel@tonic-gate
6310Sstevel@tonic-gate argv = calloc(argc + 1, sizeof (char *));
6320Sstevel@tonic-gate if (argv == NULL)
6330Sstevel@tonic-gate uu_die(gettext("Out of memory.\n"));
6340Sstevel@tonic-gate
6350Sstevel@tonic-gate for (slp = uu_list_first(args), i = 0;
6360Sstevel@tonic-gate slp != NULL;
6370Sstevel@tonic-gate slp = uu_list_next(args, slp), ++i)
6380Sstevel@tonic-gate argv[i] = slp->str;
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate argv[i] = NULL;
6410Sstevel@tonic-gate
6420Sstevel@tonic-gate opterr = 0;
6430Sstevel@tonic-gate optind = 0; /* Remember, no argv[0]. */
6440Sstevel@tonic-gate for (;;) {
64511996SThomas.Whitten@Sun.COM o = getopt(argc, argv, "np:V");
6460Sstevel@tonic-gate if (o == -1)
6470Sstevel@tonic-gate break;
6480Sstevel@tonic-gate
6490Sstevel@tonic-gate switch (o) {
6500Sstevel@tonic-gate case 'n':
6510Sstevel@tonic-gate flags |= SCI_NOREFRESH;
6520Sstevel@tonic-gate break;
6530Sstevel@tonic-gate
65411996SThomas.Whitten@Sun.COM case 'p':
65511996SThomas.Whitten@Sun.COM progress_file = optarg;
65611996SThomas.Whitten@Sun.COM break;
65711996SThomas.Whitten@Sun.COM
6580Sstevel@tonic-gate case 'V':
6597887SLiane.Praza@Sun.COM validate = B_TRUE;
6600Sstevel@tonic-gate break;
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate case '?':
6630Sstevel@tonic-gate free(argv);
6640Sstevel@tonic-gate return (-2);
6650Sstevel@tonic-gate
6660Sstevel@tonic-gate default:
6670Sstevel@tonic-gate bad_error("getopt", o);
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate argc -= optind;
67211996SThomas.Whitten@Sun.COM if (argc < 1) {
6730Sstevel@tonic-gate free(argv);
6740Sstevel@tonic-gate return (-2);
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate
67711996SThomas.Whitten@Sun.COM /* Open device for progress messages */
67811996SThomas.Whitten@Sun.COM if (progress_file != NULL) {
67911996SThomas.Whitten@Sun.COM if (strcmp(progress_file, "-") == 0) {
68011996SThomas.Whitten@Sun.COM progress_out = stdout;
68111996SThomas.Whitten@Sun.COM } else {
68211996SThomas.Whitten@Sun.COM progress_out = fopen(progress_file, "w");
68311996SThomas.Whitten@Sun.COM if (progress_out == NULL) {
68411996SThomas.Whitten@Sun.COM semerr(gettext("Unable to open %s for "
68511996SThomas.Whitten@Sun.COM "progress reporting. %s\n"),
68611996SThomas.Whitten@Sun.COM progress_file, strerror(errno));
68711996SThomas.Whitten@Sun.COM goto out;
68811996SThomas.Whitten@Sun.COM }
68911996SThomas.Whitten@Sun.COM setbuf(progress_out, NULL);
69011996SThomas.Whitten@Sun.COM }
69111996SThomas.Whitten@Sun.COM }
69211996SThomas.Whitten@Sun.COM
69311996SThomas.Whitten@Sun.COM paths = argv+optind;
69411996SThomas.Whitten@Sun.COM manifest_sets = safe_malloc(argc * sizeof (*manifest_sets));
6950Sstevel@tonic-gate
6967887SLiane.Praza@Sun.COM /* If we're in interactive mode, force strict validation. */
6977887SLiane.Praza@Sun.COM if (est->sc_cmd_flags & SC_CMD_IACTIVE)
6987887SLiane.Praza@Sun.COM validate = B_TRUE;
6997887SLiane.Praza@Sun.COM
7000Sstevel@tonic-gate lscf_prep_hndl();
7010Sstevel@tonic-gate
70211996SThomas.Whitten@Sun.COM /* Determine which manifests must be imported. */
70311996SThomas.Whitten@Sun.COM
70411996SThomas.Whitten@Sun.COM total_manifests = 0;
70511996SThomas.Whitten@Sun.COM for (i = 0; i < argc; i++) {
70611996SThomas.Whitten@Sun.COM file = *(paths + i);
70711996SThomas.Whitten@Sun.COM fm_flags = CHECKHASH;
70811996SThomas.Whitten@Sun.COM
70911996SThomas.Whitten@Sun.COM /* Determine if argument is a directory or file. */
71011996SThomas.Whitten@Sun.COM if (stat(file, &sb) == -1) {
71111996SThomas.Whitten@Sun.COM semerr(gettext("Unable to stat file %s. %s\n"), file,
71211996SThomas.Whitten@Sun.COM strerror(errno));
71311996SThomas.Whitten@Sun.COM goto out;
71411996SThomas.Whitten@Sun.COM }
71511996SThomas.Whitten@Sun.COM if (sb.st_mode & S_IFDIR) {
71611996SThomas.Whitten@Sun.COM fm_flags |= CHECKEXT;
71711996SThomas.Whitten@Sun.COM dirarg = 1;
71811996SThomas.Whitten@Sun.COM isdir = 1;
71911996SThomas.Whitten@Sun.COM } else if (sb.st_mode & S_IFREG) {
72011996SThomas.Whitten@Sun.COM isdir = 0;
72111996SThomas.Whitten@Sun.COM } else {
72211996SThomas.Whitten@Sun.COM semerr(gettext("%s is not a directory or regular "
72311996SThomas.Whitten@Sun.COM "file\n"), file);
72411996SThomas.Whitten@Sun.COM goto out;
72511996SThomas.Whitten@Sun.COM }
72611996SThomas.Whitten@Sun.COM
72711996SThomas.Whitten@Sun.COM /* Get list of manifests that we should import for this path. */
728*13112STony.Q.Nguyen@oracle.com if ((count = find_manifests(g_hndl, file, &manifests,
729*13112STony.Q.Nguyen@oracle.com fm_flags)) < 0) {
73011996SThomas.Whitten@Sun.COM if (isdir) {
73111996SThomas.Whitten@Sun.COM semerr(gettext("Could not hash directory %s\n"),
73211996SThomas.Whitten@Sun.COM file);
73311996SThomas.Whitten@Sun.COM } else {
73411996SThomas.Whitten@Sun.COM semerr(gettext("Could not hash file %s\n"),
73511996SThomas.Whitten@Sun.COM file);
73611996SThomas.Whitten@Sun.COM }
73711996SThomas.Whitten@Sun.COM free_manifest_array(manifests);
73811996SThomas.Whitten@Sun.COM goto out;
73911996SThomas.Whitten@Sun.COM }
74011996SThomas.Whitten@Sun.COM total_manifests += count;
74111996SThomas.Whitten@Sun.COM manifest_sets[i] = manifests;
7427887SLiane.Praza@Sun.COM }
7430Sstevel@tonic-gate
74411996SThomas.Whitten@Sun.COM if (total_manifests == 0) {
74511996SThomas.Whitten@Sun.COM /* No manifests to process. */
74611996SThomas.Whitten@Sun.COM if (g_verbose) {
74711996SThomas.Whitten@Sun.COM warn(gettext("No changes were necessary\n"));
74811996SThomas.Whitten@Sun.COM }
74911996SThomas.Whitten@Sun.COM rc = 1;
75011996SThomas.Whitten@Sun.COM goto out;
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate
75311996SThomas.Whitten@Sun.COM /*
75411996SThomas.Whitten@Sun.COM * If we're processing more than one file, we don't want to exit if
75511996SThomas.Whitten@Sun.COM * we encounter an error. We should go ahead and process all of
75611996SThomas.Whitten@Sun.COM * the manifests.
75711996SThomas.Whitten@Sun.COM */
75811996SThomas.Whitten@Sun.COM dont_exit = est->sc_cmd_flags & SC_CMD_DONT_EXIT;
75911996SThomas.Whitten@Sun.COM if (total_manifests > 1)
76011996SThomas.Whitten@Sun.COM est->sc_cmd_flags |= SC_CMD_DONT_EXIT;
76111996SThomas.Whitten@Sun.COM
76211996SThomas.Whitten@Sun.COM if (progress_out != NULL)
76311996SThomas.Whitten@Sun.COM (void) fprintf(progress_out,
76411996SThomas.Whitten@Sun.COM "Loading smf(5) service descriptions: ");
7657887SLiane.Praza@Sun.COM
76611996SThomas.Whitten@Sun.COM failed_manifests = 0;
76711996SThomas.Whitten@Sun.COM progress_count = 0;
76811996SThomas.Whitten@Sun.COM for (i = 0; i < argc; i++) {
76911996SThomas.Whitten@Sun.COM manifests = manifest_sets[i];
77011996SThomas.Whitten@Sun.COM if (manifests == NULL)
77111996SThomas.Whitten@Sun.COM continue;
77211996SThomas.Whitten@Sun.COM for (; *manifests != NULL; manifests++) {
77311996SThomas.Whitten@Sun.COM progress_count++;
77411996SThomas.Whitten@Sun.COM if (progress_out != NULL) {
77511996SThomas.Whitten@Sun.COM back_count = fprintf(progress_out, "%d/%d",
77611996SThomas.Whitten@Sun.COM progress_count, total_manifests);
77711996SThomas.Whitten@Sun.COM while (back_count-- > 0) {
77811996SThomas.Whitten@Sun.COM (void) fputc('\b', progress_out);
77911996SThomas.Whitten@Sun.COM }
78011996SThomas.Whitten@Sun.COM }
78111996SThomas.Whitten@Sun.COM if (import_manifest_file(*manifests, validate,
78211996SThomas.Whitten@Sun.COM progress_out, flags) != 0) {
78311996SThomas.Whitten@Sun.COM failed_manifests++;
78411996SThomas.Whitten@Sun.COM }
7857887SLiane.Praza@Sun.COM }
7867887SLiane.Praza@Sun.COM }
78711996SThomas.Whitten@Sun.COM if (progress_out != NULL)
78811996SThomas.Whitten@Sun.COM (void) fputc('\n', progress_out);
78911996SThomas.Whitten@Sun.COM
79011996SThomas.Whitten@Sun.COM if ((total_manifests > 1) && (dont_exit == 0))
79111996SThomas.Whitten@Sun.COM est->sc_cmd_flags &= ~SC_CMD_DONT_EXIT;
7927887SLiane.Praza@Sun.COM
79311996SThomas.Whitten@Sun.COM if (dirarg && total_manifests > 0) {
79411996SThomas.Whitten@Sun.COM char *msg;
79511996SThomas.Whitten@Sun.COM
79611996SThomas.Whitten@Sun.COM msg = "Loaded %d smf(5) service descriptions\n";
79711996SThomas.Whitten@Sun.COM warn(gettext(msg), progress_count);
79811996SThomas.Whitten@Sun.COM
79911996SThomas.Whitten@Sun.COM if (failed_manifests) {
80011996SThomas.Whitten@Sun.COM msg = "%d smf(5) service descriptions failed to load\n";
80111996SThomas.Whitten@Sun.COM warn(gettext(msg), failed_manifests);
80211996SThomas.Whitten@Sun.COM }
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate
80511996SThomas.Whitten@Sun.COM if (failed_manifests > 0)
80611996SThomas.Whitten@Sun.COM goto out;
8070Sstevel@tonic-gate
8080Sstevel@tonic-gate if (g_verbose)
8090Sstevel@tonic-gate warn(gettext("Successful import.\n"));
81011996SThomas.Whitten@Sun.COM rc = 0;
8110Sstevel@tonic-gate
81211996SThomas.Whitten@Sun.COM out:
81311996SThomas.Whitten@Sun.COM if ((progress_out != NULL) && (progress_out != stdout))
81411996SThomas.Whitten@Sun.COM (void) fclose(progress_out);
81511996SThomas.Whitten@Sun.COM free(argv);
81611996SThomas.Whitten@Sun.COM if (manifest_sets != NULL) {
81711996SThomas.Whitten@Sun.COM for (i = 0; i < argc; i++) {
81811996SThomas.Whitten@Sun.COM free_manifest_array(manifest_sets[i]);
81911996SThomas.Whitten@Sun.COM }
82011996SThomas.Whitten@Sun.COM free(manifest_sets);
82111996SThomas.Whitten@Sun.COM }
82211996SThomas.Whitten@Sun.COM return (rc);
82311996SThomas.Whitten@Sun.COM }
8240Sstevel@tonic-gate
82511996SThomas.Whitten@Sun.COM /*
82611996SThomas.Whitten@Sun.COM * Walk each service and get its manifest file.
82711996SThomas.Whitten@Sun.COM *
82811996SThomas.Whitten@Sun.COM * If the file exists check instance support, and cleanup any
82911996SThomas.Whitten@Sun.COM * stale instances.
83011996SThomas.Whitten@Sun.COM *
83111996SThomas.Whitten@Sun.COM * If the file doesn't exist tear down the service and/or instances
83211996SThomas.Whitten@Sun.COM * that are no longer supported by files.
83311996SThomas.Whitten@Sun.COM */
83411996SThomas.Whitten@Sun.COM int
engine_cleanup(int flags)83511996SThomas.Whitten@Sun.COM engine_cleanup(int flags)
83611996SThomas.Whitten@Sun.COM {
83711996SThomas.Whitten@Sun.COM boolean_t activity = B_TRUE;
83811996SThomas.Whitten@Sun.COM int r = -1;
8390Sstevel@tonic-gate
84011996SThomas.Whitten@Sun.COM lscf_prep_hndl();
84111996SThomas.Whitten@Sun.COM
84211996SThomas.Whitten@Sun.COM if (flags == 1) {
84311996SThomas.Whitten@Sun.COM activity = B_FALSE;
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate
84611996SThomas.Whitten@Sun.COM if (scf_walk_fmri(g_hndl, 0, NULL, SCF_WALK_SERVICE|SCF_WALK_NOINSTANCE,
84711996SThomas.Whitten@Sun.COM lscf_service_cleanup, (void *)activity, NULL,
84811996SThomas.Whitten@Sun.COM uu_warn) == SCF_SUCCESS)
84911996SThomas.Whitten@Sun.COM r = 0;
85011996SThomas.Whitten@Sun.COM
85111996SThomas.Whitten@Sun.COM (void) lscf_hash_cleanup();
85211996SThomas.Whitten@Sun.COM
85311996SThomas.Whitten@Sun.COM return (r);
8540Sstevel@tonic-gate }
8550Sstevel@tonic-gate
85612418STruong.Q.Nguyen@Sun.COM static int
apply_profile(manifest_info_t * info,int apply_changes)85712418STruong.Q.Nguyen@Sun.COM apply_profile(manifest_info_t *info, int apply_changes)
8580Sstevel@tonic-gate {
85912418STruong.Q.Nguyen@Sun.COM bundle_t *b = internal_bundle_new();
8600Sstevel@tonic-gate
86112418STruong.Q.Nguyen@Sun.COM if (lxml_get_bundle_file(b, info->mi_path, SVCCFG_OP_APPLY) != 0) {
8620Sstevel@tonic-gate internal_bundle_free(b);
8630Sstevel@tonic-gate return (-1);
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate
86610029SAntonello.Cruz@Sun.COM if (!apply_changes) { /* we don't want to apply, just test */
86710029SAntonello.Cruz@Sun.COM internal_bundle_free(b);
86810029SAntonello.Cruz@Sun.COM return (0);
86910029SAntonello.Cruz@Sun.COM }
87010029SAntonello.Cruz@Sun.COM
87112418STruong.Q.Nguyen@Sun.COM if (lscf_bundle_apply(b, info->mi_path) != 0) {
8720Sstevel@tonic-gate internal_bundle_free(b);
8730Sstevel@tonic-gate return (-1);
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate
8760Sstevel@tonic-gate internal_bundle_free(b);
8770Sstevel@tonic-gate
87812418STruong.Q.Nguyen@Sun.COM if (info->mi_prop) {
87911996SThomas.Whitten@Sun.COM apply_action_t apply;
8800Sstevel@tonic-gate char *errstr;
88111996SThomas.Whitten@Sun.COM
88211996SThomas.Whitten@Sun.COM apply = (est->sc_in_emi == 1) ? APPLY_LATE : APPLY_NONE;
88312418STruong.Q.Nguyen@Sun.COM if (mhash_store_entry(g_hndl, info->mi_prop, info->mi_path,
88412418STruong.Q.Nguyen@Sun.COM info->mi_hash, apply, &errstr)) {
8850Sstevel@tonic-gate semerr(errstr);
88611996SThomas.Whitten@Sun.COM }
8870Sstevel@tonic-gate }
8880Sstevel@tonic-gate
8890Sstevel@tonic-gate return (0);
8900Sstevel@tonic-gate }
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate int
engine_apply(const char * file,int apply_changes)89312418STruong.Q.Nguyen@Sun.COM engine_apply(const char *file, int apply_changes)
89412418STruong.Q.Nguyen@Sun.COM {
89512418STruong.Q.Nguyen@Sun.COM int rc = 0;
89612418STruong.Q.Nguyen@Sun.COM int isdir;
89712418STruong.Q.Nguyen@Sun.COM int dont_exit;
89812418STruong.Q.Nguyen@Sun.COM int profile_count;
89912418STruong.Q.Nguyen@Sun.COM int fm_flags;
90012418STruong.Q.Nguyen@Sun.COM struct stat sb;
90112418STruong.Q.Nguyen@Sun.COM manifest_info_t **profiles = NULL;
90212418STruong.Q.Nguyen@Sun.COM manifest_info_t **entry;
90312418STruong.Q.Nguyen@Sun.COM manifest_info_t *pfile;
90412418STruong.Q.Nguyen@Sun.COM
90512418STruong.Q.Nguyen@Sun.COM lscf_prep_hndl();
90612418STruong.Q.Nguyen@Sun.COM
90712418STruong.Q.Nguyen@Sun.COM /* Determine which profile(s) must be applied. */
90812418STruong.Q.Nguyen@Sun.COM
90912418STruong.Q.Nguyen@Sun.COM profile_count = 0;
91012418STruong.Q.Nguyen@Sun.COM fm_flags = BUNDLE_PROF | CHECKHASH;
91112418STruong.Q.Nguyen@Sun.COM
91212418STruong.Q.Nguyen@Sun.COM /* Determine if argument is a directory or file. */
91312418STruong.Q.Nguyen@Sun.COM if (stat(file, &sb) == -1) {
91412418STruong.Q.Nguyen@Sun.COM semerr(gettext("Unable to stat file %s. %s\n"), file,
91512418STruong.Q.Nguyen@Sun.COM strerror(errno));
91612418STruong.Q.Nguyen@Sun.COM rc = -1;
91712418STruong.Q.Nguyen@Sun.COM goto out;
91812418STruong.Q.Nguyen@Sun.COM }
91912418STruong.Q.Nguyen@Sun.COM
92012418STruong.Q.Nguyen@Sun.COM if (sb.st_mode & S_IFDIR) {
92112418STruong.Q.Nguyen@Sun.COM fm_flags |= CHECKEXT;
92212418STruong.Q.Nguyen@Sun.COM isdir = 1;
92312418STruong.Q.Nguyen@Sun.COM } else if (sb.st_mode & S_IFREG) {
92412418STruong.Q.Nguyen@Sun.COM isdir = 0;
92512418STruong.Q.Nguyen@Sun.COM } else {
92612418STruong.Q.Nguyen@Sun.COM semerr(gettext("%s is not a directory or regular "
92712418STruong.Q.Nguyen@Sun.COM "file\n"), file);
92812418STruong.Q.Nguyen@Sun.COM rc = -1;
92912418STruong.Q.Nguyen@Sun.COM goto out;
93012418STruong.Q.Nguyen@Sun.COM }
93112418STruong.Q.Nguyen@Sun.COM
93212418STruong.Q.Nguyen@Sun.COM /* Get list of profiles to be applied. */
933*13112STony.Q.Nguyen@oracle.com if ((profile_count = find_manifests(g_hndl, file, &profiles,
934*13112STony.Q.Nguyen@oracle.com fm_flags)) < 0) {
935*13112STony.Q.Nguyen@oracle.com
93612418STruong.Q.Nguyen@Sun.COM if (isdir) {
93712418STruong.Q.Nguyen@Sun.COM semerr(gettext("Could not hash directory %s\n"), file);
93812418STruong.Q.Nguyen@Sun.COM } else {
93912418STruong.Q.Nguyen@Sun.COM semerr(gettext("Could not hash file %s\n"), file);
94012418STruong.Q.Nguyen@Sun.COM }
94112418STruong.Q.Nguyen@Sun.COM rc = -1;
94212418STruong.Q.Nguyen@Sun.COM goto out;
94312418STruong.Q.Nguyen@Sun.COM }
94412418STruong.Q.Nguyen@Sun.COM
94512418STruong.Q.Nguyen@Sun.COM if (profile_count == 0) {
94612418STruong.Q.Nguyen@Sun.COM /* No profiles to process. */
94712418STruong.Q.Nguyen@Sun.COM if (g_verbose) {
94812418STruong.Q.Nguyen@Sun.COM warn(gettext("No changes were necessary\n"));
94912418STruong.Q.Nguyen@Sun.COM }
95012418STruong.Q.Nguyen@Sun.COM goto out;
95112418STruong.Q.Nguyen@Sun.COM }
95212418STruong.Q.Nguyen@Sun.COM
95312418STruong.Q.Nguyen@Sun.COM /*
95412418STruong.Q.Nguyen@Sun.COM * We don't want to exit if we encounter an error. We should go ahead
95512418STruong.Q.Nguyen@Sun.COM * and process all of the profiles.
95612418STruong.Q.Nguyen@Sun.COM */
95712418STruong.Q.Nguyen@Sun.COM dont_exit = est->sc_cmd_flags & SC_CMD_DONT_EXIT;
95812418STruong.Q.Nguyen@Sun.COM est->sc_cmd_flags |= SC_CMD_DONT_EXIT;
95912418STruong.Q.Nguyen@Sun.COM
96012418STruong.Q.Nguyen@Sun.COM for (entry = profiles; *entry != NULL; entry++) {
96112418STruong.Q.Nguyen@Sun.COM pfile = *entry;
96212418STruong.Q.Nguyen@Sun.COM
96312418STruong.Q.Nguyen@Sun.COM if (apply_profile(pfile, apply_changes) == 0) {
96412418STruong.Q.Nguyen@Sun.COM if (g_verbose) {
96512418STruong.Q.Nguyen@Sun.COM warn(gettext("Successfully applied: %s\n"),
96612418STruong.Q.Nguyen@Sun.COM pfile->mi_path);
96712418STruong.Q.Nguyen@Sun.COM }
96812418STruong.Q.Nguyen@Sun.COM } else {
96912418STruong.Q.Nguyen@Sun.COM warn(gettext("WARNING: Failed to apply %s\n"),
97012418STruong.Q.Nguyen@Sun.COM pfile->mi_path);
97112418STruong.Q.Nguyen@Sun.COM rc = -1;
97212418STruong.Q.Nguyen@Sun.COM }
97312418STruong.Q.Nguyen@Sun.COM }
97412418STruong.Q.Nguyen@Sun.COM
97512418STruong.Q.Nguyen@Sun.COM if (dont_exit == 0)
97612418STruong.Q.Nguyen@Sun.COM est->sc_cmd_flags &= ~SC_CMD_DONT_EXIT;
97712418STruong.Q.Nguyen@Sun.COM
97812418STruong.Q.Nguyen@Sun.COM /* exit(1) appropriately if any profile failed to be applied. */
97912418STruong.Q.Nguyen@Sun.COM if ((rc == -1) &&
98012418STruong.Q.Nguyen@Sun.COM (est->sc_cmd_flags & (SC_CMD_IACTIVE | SC_CMD_DONT_EXIT)) == 0) {
98112418STruong.Q.Nguyen@Sun.COM free_manifest_array(profiles);
98212418STruong.Q.Nguyen@Sun.COM exit(1);
98312418STruong.Q.Nguyen@Sun.COM }
98412418STruong.Q.Nguyen@Sun.COM
98512418STruong.Q.Nguyen@Sun.COM out:
98612418STruong.Q.Nguyen@Sun.COM free_manifest_array(profiles);
98712418STruong.Q.Nguyen@Sun.COM return (rc);
98812418STruong.Q.Nguyen@Sun.COM }
98912418STruong.Q.Nguyen@Sun.COM
99012418STruong.Q.Nguyen@Sun.COM int
engine_restore(const char * file)9915040Swesolows engine_restore(const char *file)
9925040Swesolows {
9935040Swesolows bundle_t *b;
9945040Swesolows
9955040Swesolows lscf_prep_hndl();
9965040Swesolows
9975040Swesolows b = internal_bundle_new();
9985040Swesolows
9995040Swesolows if (lxml_get_bundle_file(b, file, SVCCFG_OP_RESTORE) != 0) {
10005040Swesolows internal_bundle_free(b);
10015040Swesolows return (-1);
10025040Swesolows }
10035040Swesolows
10045040Swesolows if (lscf_bundle_import(b, file, SCI_NOSNAP) != 0) {
10055040Swesolows internal_bundle_free(b);
10065040Swesolows return (-1);
10075040Swesolows }
10085040Swesolows
10095040Swesolows internal_bundle_free(b);
10105040Swesolows
10115040Swesolows return (0);
10125040Swesolows }
10135040Swesolows
10145040Swesolows int
engine_set(uu_list_t * args)10150Sstevel@tonic-gate engine_set(uu_list_t *args)
10160Sstevel@tonic-gate {
10170Sstevel@tonic-gate uu_list_walk_t *walk;
10180Sstevel@tonic-gate string_list_t *slp;
10190Sstevel@tonic-gate
10200Sstevel@tonic-gate if (uu_list_first(args) == NULL) {
10210Sstevel@tonic-gate /* Display current options. */
10220Sstevel@tonic-gate if (!g_verbose)
10230Sstevel@tonic-gate (void) fputs("no", stdout);
10240Sstevel@tonic-gate (void) puts("verbose");
10250Sstevel@tonic-gate
10260Sstevel@tonic-gate return (0);
10270Sstevel@tonic-gate }
10280Sstevel@tonic-gate
10290Sstevel@tonic-gate walk = uu_list_walk_start(args, UU_DEFAULT);
10300Sstevel@tonic-gate if (walk == NULL)
10310Sstevel@tonic-gate uu_die(gettext("Couldn't read arguments"));
10320Sstevel@tonic-gate
10330Sstevel@tonic-gate /* Use getopt? */
10340Sstevel@tonic-gate for (slp = uu_list_walk_next(walk);
10350Sstevel@tonic-gate slp != NULL;
10360Sstevel@tonic-gate slp = uu_list_walk_next(walk)) {
10370Sstevel@tonic-gate if (slp->str[0] == '-') {
10380Sstevel@tonic-gate char *op;
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate for (op = &slp->str[1]; *op != '\0'; ++op) {
10410Sstevel@tonic-gate switch (*op) {
10420Sstevel@tonic-gate case 'v':
10430Sstevel@tonic-gate g_verbose = 1;
10440Sstevel@tonic-gate break;
10450Sstevel@tonic-gate
10460Sstevel@tonic-gate case 'V':
10470Sstevel@tonic-gate g_verbose = 0;
10480Sstevel@tonic-gate break;
10490Sstevel@tonic-gate
10500Sstevel@tonic-gate default:
10510Sstevel@tonic-gate warn(gettext("Unknown option -%c.\n"),
10520Sstevel@tonic-gate *op);
10530Sstevel@tonic-gate }
10540Sstevel@tonic-gate }
10550Sstevel@tonic-gate } else {
10560Sstevel@tonic-gate warn(gettext("No non-flag arguments defined.\n"));
10570Sstevel@tonic-gate }
10580Sstevel@tonic-gate }
10590Sstevel@tonic-gate
10600Sstevel@tonic-gate return (0);
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate
10630Sstevel@tonic-gate void
help(int com)10640Sstevel@tonic-gate help(int com)
10650Sstevel@tonic-gate {
10660Sstevel@tonic-gate int i;
10670Sstevel@tonic-gate
10680Sstevel@tonic-gate if (com == 0) {
10690Sstevel@tonic-gate warn(gettext("General commands: help set repository end\n"
10700Sstevel@tonic-gate "Manifest commands: inventory validate import export "
10710Sstevel@tonic-gate "archive\n"
10720Sstevel@tonic-gate "Profile commands: apply extract\n"
10737887SLiane.Praza@Sun.COM "Entity commands: list select unselect add delete "
10747887SLiane.Praza@Sun.COM "describe\n"
10750Sstevel@tonic-gate "Snapshot commands: listsnap selectsnap revert\n"
10765841Samaguire "Instance commands: refresh\n"
10770Sstevel@tonic-gate "Property group commands: listpg addpg delpg\n"
10780Sstevel@tonic-gate "Property commands: listprop setprop delprop editprop\n"
10790Sstevel@tonic-gate "Property value commands: addpropvalue delpropvalue "
108012967Sgavin.maltby@oracle.com "setenv unsetenv\n"
108112967Sgavin.maltby@oracle.com "Notification parameters: "
108212967Sgavin.maltby@oracle.com "listnotify setnotify delnotify\n"));
10830Sstevel@tonic-gate return;
10840Sstevel@tonic-gate }
10850Sstevel@tonic-gate
10860Sstevel@tonic-gate for (i = 0; help_messages[i].message != NULL; ++i) {
10870Sstevel@tonic-gate if (help_messages[i].token == com) {
10880Sstevel@tonic-gate warn(gettext("Usage: %s\n"),
10890Sstevel@tonic-gate gettext(help_messages[i].message));
10900Sstevel@tonic-gate return;
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate }
10930Sstevel@tonic-gate
10940Sstevel@tonic-gate warn(gettext("Unknown command.\n"));
10950Sstevel@tonic-gate }
1096