17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55703ae89Stalley  * Common Development and Distribution License (the "License").
65703ae89Stalley  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
221f6eb021SLiane Praza  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
26*75987085SAndy Fiddaman /*
27*75987085SAndy Fiddaman  * Copyright 2023 Oxide Computer Company
28*75987085SAndy Fiddaman  */
29*75987085SAndy Fiddaman 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * svccfg - modify service configuration repository
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/wait.h>
37*75987085SAndy Fiddaman #include <sys/zone.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate #include <libintl.h>
417c478bd9Sstevel@tonic-gate #include <libscf.h>
427c478bd9Sstevel@tonic-gate #include <libscf_priv.h>
437c478bd9Sstevel@tonic-gate #include <libuutil.h>
447c478bd9Sstevel@tonic-gate #include <locale.h>
457c478bd9Sstevel@tonic-gate #include <signal.h>
467c478bd9Sstevel@tonic-gate #include <stdarg.h>
477c478bd9Sstevel@tonic-gate #include <stddef.h>
487c478bd9Sstevel@tonic-gate #include <stdio.h>
497c478bd9Sstevel@tonic-gate #include <stdlib.h>
507c478bd9Sstevel@tonic-gate #include <string.h>
517c478bd9Sstevel@tonic-gate #include <unistd.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include "svccfg.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
567c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
577c478bd9Sstevel@tonic-gate #endif /* TEXT_DOMAIN */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	MAX_CMD_LINE_SZ	2048
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static const char *myname;
627c478bd9Sstevel@tonic-gate int g_verbose = 0;
63*75987085SAndy Fiddaman int g_do_zone = 0;
64*75987085SAndy Fiddaman char g_zonename[ZONENAME_MAX];
657c478bd9Sstevel@tonic-gate const char *fmri;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static void
usage()687c478bd9Sstevel@tonic-gate usage()
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
71*75987085SAndy Fiddaman 	    "Usage:\tsvccfg [-v] [-z zone] [-s FMRI] [-f file]\n"
72*75987085SAndy Fiddaman 	    "\tsvccfg [-v] [-z zone] [-s FMRI] <command> [args]\n"));
737c478bd9Sstevel@tonic-gate 	exit(UU_EXIT_USAGE);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate void *
safe_malloc(size_t sz)777c478bd9Sstevel@tonic-gate safe_malloc(size_t sz)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	void *p;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if ((p = calloc(1, sz)) == NULL)
827c478bd9Sstevel@tonic-gate 		uu_die(gettext("Out of memory.\n"));
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	return (p);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate char *
safe_strdup(const char * cp)887c478bd9Sstevel@tonic-gate safe_strdup(const char *cp)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	char *result;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	result = strdup(cp);
937c478bd9Sstevel@tonic-gate 	if (result == NULL)
947c478bd9Sstevel@tonic-gate 		uu_die(gettext("Out of memory.\n"));
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	return (result);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * Send a message to the user.  If we're interactive, send it to stdout.
1017c478bd9Sstevel@tonic-gate  * Otherwise send it to stderr.
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate static void
vmessage(const char * fmt,va_list va)1047c478bd9Sstevel@tonic-gate vmessage(const char *fmt, va_list va)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	int interactive = est->sc_cmd_flags & SC_CMD_IACTIVE;
1077c478bd9Sstevel@tonic-gate 	FILE *strm = interactive ? stdout : stderr;
1087c478bd9Sstevel@tonic-gate 	const char *ptr;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (!interactive) {
1117c478bd9Sstevel@tonic-gate 		if (est->sc_cmd_file == NULL)
1127c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: ", myname);
1137c478bd9Sstevel@tonic-gate 		else
1147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s (%s, line %d): ", myname,
1157c478bd9Sstevel@tonic-gate 			    est->sc_cmd_filename, est->sc_cmd_lineno - 1);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (vfprintf(strm, fmt, va) < 0 && interactive)
1197c478bd9Sstevel@tonic-gate 		uu_die(gettext("printf() error"));
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	ptr = strchr(fmt, '\0');
1227c478bd9Sstevel@tonic-gate 	if (*(ptr - 1) != '\n')
1237c478bd9Sstevel@tonic-gate 		(void) fprintf(strm, ": %s.\n", strerror(errno));
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Display a warning.  Should usually be predicated by g_verbose.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
1307c478bd9Sstevel@tonic-gate void
warn(const char * fmt,...)1317c478bd9Sstevel@tonic-gate warn(const char *fmt, ...)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	va_list va;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	va_start(va, fmt);
1367c478bd9Sstevel@tonic-gate 	vmessage(fmt, va);
1377c478bd9Sstevel@tonic-gate 	va_end(va);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * Syntax error.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate void
synerr(int com)1447c478bd9Sstevel@tonic-gate synerr(int com)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	if (est->sc_cmd_flags & SC_CMD_IACTIVE) {
1477c478bd9Sstevel@tonic-gate 		help(com);
1487c478bd9Sstevel@tonic-gate 		return;
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	warn(gettext("Syntax error.\n"));
1525703ae89Stalley 
1535703ae89Stalley 	if ((est->sc_cmd_flags & SC_CMD_DONT_EXIT) == 0)
1545703ae89Stalley 		exit(1);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * Semantic error.  Display the warning and exit if we're not interactive.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
1617c478bd9Sstevel@tonic-gate void
semerr(const char * fmt,...)1627c478bd9Sstevel@tonic-gate semerr(const char *fmt, ...)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	va_list va;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	va_start(va, fmt);
1677c478bd9Sstevel@tonic-gate 	vmessage(fmt, va);
1687c478bd9Sstevel@tonic-gate 	va_end(va);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if ((est->sc_cmd_flags & (SC_CMD_IACTIVE | SC_CMD_DONT_EXIT)) == 0)
1717c478bd9Sstevel@tonic-gate 		exit(1);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1757c478bd9Sstevel@tonic-gate static void
initialize(int argc,char * argv[])1767c478bd9Sstevel@tonic-gate initialize(int argc, char *argv[])
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	myname = uu_setpname(argv[0]);
1797c478bd9Sstevel@tonic-gate 	(void) atexit(lscf_cleanup);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1827c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	(void) lxml_init();
1857c478bd9Sstevel@tonic-gate 	internal_init();
1867c478bd9Sstevel@tonic-gate 	engine_init();
1877c478bd9Sstevel@tonic-gate 	lscf_init();			/* must follow engine_init() */
1881f6eb021SLiane Praza 	tmpl_init();
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1927c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	char *cmd, *command_file = NULL;
1957c478bd9Sstevel@tonic-gate 	char *fmri = NULL;
1967c478bd9Sstevel@tonic-gate 	int c;
1977c478bd9Sstevel@tonic-gate 
198*75987085SAndy Fiddaman 	while ((c = getopt(argc, argv, "vf:s:z:")) != EOF) {
1997c478bd9Sstevel@tonic-gate 		switch (c) {
2007c478bd9Sstevel@tonic-gate 		case 'v':
2017c478bd9Sstevel@tonic-gate 			g_verbose = 1;
2027c478bd9Sstevel@tonic-gate 			break;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		case 's':
2057c478bd9Sstevel@tonic-gate 			fmri = optarg;
2067c478bd9Sstevel@tonic-gate 			break;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		case 'f':
2097c478bd9Sstevel@tonic-gate 			command_file = optarg;
2107c478bd9Sstevel@tonic-gate 			break;
2117c478bd9Sstevel@tonic-gate 
212*75987085SAndy Fiddaman 		case 'z':
213*75987085SAndy Fiddaman 			if (getzoneid() != GLOBAL_ZONEID) {
214*75987085SAndy Fiddaman 				uu_die(gettext("svccfg -z may only be used "
215*75987085SAndy Fiddaman 				    "from the global zone\n"));
216*75987085SAndy Fiddaman 			}
217*75987085SAndy Fiddaman 
218*75987085SAndy Fiddaman 			if (strlcpy(g_zonename, optarg, sizeof (g_zonename)) >=
219*75987085SAndy Fiddaman 			    sizeof (g_zonename)) {
220*75987085SAndy Fiddaman 				uu_die(gettext(
221*75987085SAndy Fiddaman 				    "The provided zone name is too long, "
222*75987085SAndy Fiddaman 				    "max %zd\n"), sizeof (g_zonename) - 1);
223*75987085SAndy Fiddaman 			}
224*75987085SAndy Fiddaman 			g_do_zone = 1;
225*75987085SAndy Fiddaman 			break;
226*75987085SAndy Fiddaman 
2277c478bd9Sstevel@tonic-gate 		default:
2287c478bd9Sstevel@tonic-gate 			usage();
2297c478bd9Sstevel@tonic-gate 			break;
2307c478bd9Sstevel@tonic-gate 		}
231*75987085SAndy Fiddaman 	}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	initialize(argc, argv);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (fmri != NULL)
2367c478bd9Sstevel@tonic-gate 		lscf_select(fmri);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (command_file != NULL)
2397c478bd9Sstevel@tonic-gate 		return (engine_source(command_file, 0));
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (optind == argc) {
2427c478bd9Sstevel@tonic-gate 		if (isatty(fileno(stdin)))
2437c478bd9Sstevel@tonic-gate 			return (engine_interp());
2447c478bd9Sstevel@tonic-gate 		else
2457c478bd9Sstevel@tonic-gate 			return (engine_source("-", 0));
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/*
2497c478bd9Sstevel@tonic-gate 	 * Knit together remaining arguments into a single statement.
2507c478bd9Sstevel@tonic-gate 	 */
2517c478bd9Sstevel@tonic-gate 	cmd = safe_malloc(MAX_CMD_LINE_SZ);
2527c478bd9Sstevel@tonic-gate 	for (c = optind; c < argc; c++) {
2537c478bd9Sstevel@tonic-gate 		(void) strlcat(cmd, argv[c], MAX_CMD_LINE_SZ);
2547c478bd9Sstevel@tonic-gate 		(void) strlcat(cmd, " ", MAX_CMD_LINE_SZ);
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	return (engine_exec(cmd));
2587c478bd9Sstevel@tonic-gate }
259