xref: /onnv-gate/usr/src/cmd/svc/svccfg/svccfg_main.c (revision 7887:b6618727fabf)
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
52643Stalley  * Common Development and Distribution License (the "License").
62643Stalley  * 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  */
210Sstevel@tonic-gate /*
22*7887SLiane.Praza@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * svccfg - modify service configuration repository
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/stat.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/wait.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <libintl.h>
360Sstevel@tonic-gate #include <libscf.h>
370Sstevel@tonic-gate #include <libscf_priv.h>
380Sstevel@tonic-gate #include <libuutil.h>
390Sstevel@tonic-gate #include <locale.h>
400Sstevel@tonic-gate #include <signal.h>
410Sstevel@tonic-gate #include <stdarg.h>
420Sstevel@tonic-gate #include <stddef.h>
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <stdlib.h>
450Sstevel@tonic-gate #include <string.h>
460Sstevel@tonic-gate #include <unistd.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include "svccfg.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #ifndef TEXT_DOMAIN
510Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
520Sstevel@tonic-gate #endif /* TEXT_DOMAIN */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define	MAX_CMD_LINE_SZ	2048
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static const char *myname;
570Sstevel@tonic-gate int g_verbose = 0;
580Sstevel@tonic-gate const char *fmri;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static void
usage()610Sstevel@tonic-gate usage()
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
640Sstevel@tonic-gate 	    "Usage:\tsvccfg [-v] [-s FMRI] [-f file]\n"
650Sstevel@tonic-gate 	    "\tsvccfg [-v] [-s FMRI] <command> [args]\n"));
660Sstevel@tonic-gate 	exit(UU_EXIT_USAGE);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate void *
safe_malloc(size_t sz)700Sstevel@tonic-gate safe_malloc(size_t sz)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	void *p;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	if ((p = calloc(1, sz)) == NULL)
750Sstevel@tonic-gate 		uu_die(gettext("Out of memory.\n"));
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	return (p);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate 
800Sstevel@tonic-gate char *
safe_strdup(const char * cp)810Sstevel@tonic-gate safe_strdup(const char *cp)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	char *result;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	result = strdup(cp);
860Sstevel@tonic-gate 	if (result == NULL)
870Sstevel@tonic-gate 		uu_die(gettext("Out of memory.\n"));
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	return (result);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * Send a message to the user.  If we're interactive, send it to stdout.
940Sstevel@tonic-gate  * Otherwise send it to stderr.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate static void
vmessage(const char * fmt,va_list va)970Sstevel@tonic-gate vmessage(const char *fmt, va_list va)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	int interactive = est->sc_cmd_flags & SC_CMD_IACTIVE;
1000Sstevel@tonic-gate 	FILE *strm = interactive ? stdout : stderr;
1010Sstevel@tonic-gate 	const char *ptr;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if (!interactive) {
1040Sstevel@tonic-gate 		if (est->sc_cmd_file == NULL)
1050Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: ", myname);
1060Sstevel@tonic-gate 		else
1070Sstevel@tonic-gate 			(void) fprintf(stderr, "%s (%s, line %d): ", myname,
1080Sstevel@tonic-gate 			    est->sc_cmd_filename, est->sc_cmd_lineno - 1);
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	if (vfprintf(strm, fmt, va) < 0 && interactive)
1120Sstevel@tonic-gate 		uu_die(gettext("printf() error"));
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	ptr = strchr(fmt, '\0');
1150Sstevel@tonic-gate 	if (*(ptr - 1) != '\n')
1160Sstevel@tonic-gate 		(void) fprintf(strm, ": %s.\n", strerror(errno));
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * Display a warning.  Should usually be predicated by g_verbose.
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate /* PRINTFLIKE1 */
1230Sstevel@tonic-gate void
warn(const char * fmt,...)1240Sstevel@tonic-gate warn(const char *fmt, ...)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate 	va_list va;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	va_start(va, fmt);
1290Sstevel@tonic-gate 	vmessage(fmt, va);
1300Sstevel@tonic-gate 	va_end(va);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * Syntax error.
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate void
synerr(int com)1370Sstevel@tonic-gate synerr(int com)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	if (est->sc_cmd_flags & SC_CMD_IACTIVE) {
1400Sstevel@tonic-gate 		help(com);
1410Sstevel@tonic-gate 		return;
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	warn(gettext("Syntax error.\n"));
1452643Stalley 
1462643Stalley 	if ((est->sc_cmd_flags & SC_CMD_DONT_EXIT) == 0)
1472643Stalley 		exit(1);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * Semantic error.  Display the warning and exit if we're not interactive.
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate /* PRINTFLIKE1 */
1540Sstevel@tonic-gate void
semerr(const char * fmt,...)1550Sstevel@tonic-gate semerr(const char *fmt, ...)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	va_list va;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	va_start(va, fmt);
1600Sstevel@tonic-gate 	vmessage(fmt, va);
1610Sstevel@tonic-gate 	va_end(va);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if ((est->sc_cmd_flags & (SC_CMD_IACTIVE | SC_CMD_DONT_EXIT)) == 0)
1640Sstevel@tonic-gate 		exit(1);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*ARGSUSED*/
1680Sstevel@tonic-gate static void
initialize(int argc,char * argv[])1690Sstevel@tonic-gate initialize(int argc, char *argv[])
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	myname = uu_setpname(argv[0]);
1720Sstevel@tonic-gate 	(void) atexit(lscf_cleanup);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1750Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	(void) lxml_init();
1780Sstevel@tonic-gate 	internal_init();
1790Sstevel@tonic-gate 	engine_init();
1800Sstevel@tonic-gate 	lscf_init();			/* must follow engine_init() */
181*7887SLiane.Praza@Sun.COM 	tmpl_init();
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate int
main(int argc,char * argv[])1850Sstevel@tonic-gate main(int argc, char *argv[])
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	char *cmd, *command_file = NULL;
1880Sstevel@tonic-gate 	char *fmri = NULL;
1890Sstevel@tonic-gate 	int c;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "vf:s:")) != EOF)
1920Sstevel@tonic-gate 		switch (c) {
1930Sstevel@tonic-gate 		case 'v':
1940Sstevel@tonic-gate 			g_verbose = 1;
1950Sstevel@tonic-gate 			break;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 		case 's':
1980Sstevel@tonic-gate 			fmri = optarg;
1990Sstevel@tonic-gate 			break;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		case 'f':
2020Sstevel@tonic-gate 			command_file = optarg;
2030Sstevel@tonic-gate 			break;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		default:
2060Sstevel@tonic-gate 			usage();
2070Sstevel@tonic-gate 			break;
2080Sstevel@tonic-gate 		}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	initialize(argc, argv);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	if (fmri != NULL)
2130Sstevel@tonic-gate 		lscf_select(fmri);
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	if (command_file != NULL)
2160Sstevel@tonic-gate 		return (engine_source(command_file, 0));
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (optind == argc) {
2190Sstevel@tonic-gate 		if (isatty(fileno(stdin)))
2200Sstevel@tonic-gate 			return (engine_interp());
2210Sstevel@tonic-gate 		else
2220Sstevel@tonic-gate 			return (engine_source("-", 0));
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	/*
2260Sstevel@tonic-gate 	 * Knit together remaining arguments into a single statement.
2270Sstevel@tonic-gate 	 */
2280Sstevel@tonic-gate 	cmd = safe_malloc(MAX_CMD_LINE_SZ);
2290Sstevel@tonic-gate 	for (c = optind; c < argc; c++) {
2300Sstevel@tonic-gate 		(void) strlcat(cmd, argv[c], MAX_CMD_LINE_SZ);
2310Sstevel@tonic-gate 		(void) strlcat(cmd, " ", MAX_CMD_LINE_SZ);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	return (engine_exec(cmd));
2350Sstevel@tonic-gate }
236