xref: /illumos-gate/usr/src/cmd/svc/svccfg/svccfg_main.c (revision 5703ae8931e75b07e7f34f6ed5b484dbb05a83d4)
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
5*5703ae89Stalley  * Common Development and Distribution License (the "License").
6*5703ae89Stalley  * 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 /*
22*5703ae89Stalley  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * svccfg - modify service configuration repository
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/wait.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <libintl.h>
387c478bd9Sstevel@tonic-gate #include <libscf.h>
397c478bd9Sstevel@tonic-gate #include <libscf_priv.h>
407c478bd9Sstevel@tonic-gate #include <libuutil.h>
417c478bd9Sstevel@tonic-gate #include <locale.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include <stdarg.h>
447c478bd9Sstevel@tonic-gate #include <stddef.h>
457c478bd9Sstevel@tonic-gate #include <stdio.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <string.h>
487c478bd9Sstevel@tonic-gate #include <unistd.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include "svccfg.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
537c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
547c478bd9Sstevel@tonic-gate #endif /* TEXT_DOMAIN */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	MAX_CMD_LINE_SZ	2048
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static const char *myname;
597c478bd9Sstevel@tonic-gate int g_verbose = 0;
607c478bd9Sstevel@tonic-gate const char *fmri;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static void
637c478bd9Sstevel@tonic-gate usage()
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
667c478bd9Sstevel@tonic-gate 	    "Usage:\tsvccfg [-v] [-s FMRI] [-f file]\n"
677c478bd9Sstevel@tonic-gate 	    "\tsvccfg [-v] [-s FMRI] <command> [args]\n"));
687c478bd9Sstevel@tonic-gate 	exit(UU_EXIT_USAGE);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate void *
727c478bd9Sstevel@tonic-gate safe_malloc(size_t sz)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	void *p;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if ((p = calloc(1, sz)) == NULL)
777c478bd9Sstevel@tonic-gate 		uu_die(gettext("Out of memory.\n"));
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	return (p);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate char *
837c478bd9Sstevel@tonic-gate safe_strdup(const char *cp)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	char *result;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	result = strdup(cp);
887c478bd9Sstevel@tonic-gate 	if (result == NULL)
897c478bd9Sstevel@tonic-gate 		uu_die(gettext("Out of memory.\n"));
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	return (result);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Send a message to the user.  If we're interactive, send it to stdout.
967c478bd9Sstevel@tonic-gate  * Otherwise send it to stderr.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate static void
997c478bd9Sstevel@tonic-gate vmessage(const char *fmt, va_list va)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	int interactive = est->sc_cmd_flags & SC_CMD_IACTIVE;
1027c478bd9Sstevel@tonic-gate 	FILE *strm = interactive ? stdout : stderr;
1037c478bd9Sstevel@tonic-gate 	const char *ptr;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (!interactive) {
1067c478bd9Sstevel@tonic-gate 		if (est->sc_cmd_file == NULL)
1077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: ", myname);
1087c478bd9Sstevel@tonic-gate 		else
1097c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s (%s, line %d): ", myname,
1107c478bd9Sstevel@tonic-gate 			    est->sc_cmd_filename, est->sc_cmd_lineno - 1);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (vfprintf(strm, fmt, va) < 0 && interactive)
1147c478bd9Sstevel@tonic-gate 		uu_die(gettext("printf() error"));
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	ptr = strchr(fmt, '\0');
1177c478bd9Sstevel@tonic-gate 	if (*(ptr - 1) != '\n')
1187c478bd9Sstevel@tonic-gate 		(void) fprintf(strm, ": %s.\n", strerror(errno));
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Display a warning.  Should usually be predicated by g_verbose.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
1257c478bd9Sstevel@tonic-gate void
1267c478bd9Sstevel@tonic-gate warn(const char *fmt, ...)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	va_list va;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	va_start(va, fmt);
1317c478bd9Sstevel@tonic-gate 	vmessage(fmt, va);
1327c478bd9Sstevel@tonic-gate 	va_end(va);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * Syntax error.
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate void
1397c478bd9Sstevel@tonic-gate synerr(int com)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	if (est->sc_cmd_flags & SC_CMD_IACTIVE) {
1427c478bd9Sstevel@tonic-gate 		help(com);
1437c478bd9Sstevel@tonic-gate 		return;
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	warn(gettext("Syntax error.\n"));
147*5703ae89Stalley 
148*5703ae89Stalley 	if ((est->sc_cmd_flags & SC_CMD_DONT_EXIT) == 0)
149*5703ae89Stalley 		exit(1);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Semantic error.  Display the warning and exit if we're not interactive.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
1567c478bd9Sstevel@tonic-gate void
1577c478bd9Sstevel@tonic-gate semerr(const char *fmt, ...)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	va_list va;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	va_start(va, fmt);
1627c478bd9Sstevel@tonic-gate 	vmessage(fmt, va);
1637c478bd9Sstevel@tonic-gate 	va_end(va);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if ((est->sc_cmd_flags & (SC_CMD_IACTIVE | SC_CMD_DONT_EXIT)) == 0)
1667c478bd9Sstevel@tonic-gate 		exit(1);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1707c478bd9Sstevel@tonic-gate static void
1717c478bd9Sstevel@tonic-gate initialize(int argc, char *argv[])
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	myname = uu_setpname(argv[0]);
1747c478bd9Sstevel@tonic-gate 	(void) atexit(lscf_cleanup);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1777c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	(void) lxml_init();
1807c478bd9Sstevel@tonic-gate 	internal_init();
1817c478bd9Sstevel@tonic-gate 	engine_init();
1827c478bd9Sstevel@tonic-gate 	lscf_init();			/* must follow engine_init() */
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate int
1867c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	char *cmd, *command_file = NULL;
1897c478bd9Sstevel@tonic-gate 	char *fmri = NULL;
1907c478bd9Sstevel@tonic-gate 	int c;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "vf:s:")) != EOF)
1937c478bd9Sstevel@tonic-gate 		switch (c) {
1947c478bd9Sstevel@tonic-gate 		case 'v':
1957c478bd9Sstevel@tonic-gate 			g_verbose = 1;
1967c478bd9Sstevel@tonic-gate 			break;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		case 's':
1997c478bd9Sstevel@tonic-gate 			fmri = optarg;
2007c478bd9Sstevel@tonic-gate 			break;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		case 'f':
2037c478bd9Sstevel@tonic-gate 			command_file = optarg;
2047c478bd9Sstevel@tonic-gate 			break;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		default:
2077c478bd9Sstevel@tonic-gate 			usage();
2087c478bd9Sstevel@tonic-gate 			break;
2097c478bd9Sstevel@tonic-gate 		}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	initialize(argc, argv);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (fmri != NULL)
2147c478bd9Sstevel@tonic-gate 		lscf_select(fmri);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (command_file != NULL)
2177c478bd9Sstevel@tonic-gate 		return (engine_source(command_file, 0));
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if (optind == argc) {
2207c478bd9Sstevel@tonic-gate 		if (isatty(fileno(stdin)))
2217c478bd9Sstevel@tonic-gate 			return (engine_interp());
2227c478bd9Sstevel@tonic-gate 		else
2237c478bd9Sstevel@tonic-gate 			return (engine_source("-", 0));
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * Knit together remaining arguments into a single statement.
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 	cmd = safe_malloc(MAX_CMD_LINE_SZ);
2307c478bd9Sstevel@tonic-gate 	for (c = optind; c < argc; c++) {
2317c478bd9Sstevel@tonic-gate 		(void) strlcat(cmd, argv[c], MAX_CMD_LINE_SZ);
2327c478bd9Sstevel@tonic-gate 		(void) strlcat(cmd, " ", MAX_CMD_LINE_SZ);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	return (engine_exec(cmd));
2367c478bd9Sstevel@tonic-gate }
237