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