1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * svccfg - modify service configuration repository 31*0Sstevel@tonic-gate */ 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include <sys/stat.h> 34*0Sstevel@tonic-gate #include <sys/types.h> 35*0Sstevel@tonic-gate #include <sys/wait.h> 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate #include <errno.h> 38*0Sstevel@tonic-gate #include <libintl.h> 39*0Sstevel@tonic-gate #include <libscf.h> 40*0Sstevel@tonic-gate #include <libscf_priv.h> 41*0Sstevel@tonic-gate #include <libuutil.h> 42*0Sstevel@tonic-gate #include <locale.h> 43*0Sstevel@tonic-gate #include <signal.h> 44*0Sstevel@tonic-gate #include <stdarg.h> 45*0Sstevel@tonic-gate #include <stddef.h> 46*0Sstevel@tonic-gate #include <stdio.h> 47*0Sstevel@tonic-gate #include <stdlib.h> 48*0Sstevel@tonic-gate #include <string.h> 49*0Sstevel@tonic-gate #include <unistd.h> 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate #include "svccfg.h" 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate #ifndef TEXT_DOMAIN 54*0Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD" 55*0Sstevel@tonic-gate #endif /* TEXT_DOMAIN */ 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate #define MAX_CMD_LINE_SZ 2048 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate static const char *myname; 60*0Sstevel@tonic-gate int g_verbose = 0; 61*0Sstevel@tonic-gate const char *fmri; 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate static void 64*0Sstevel@tonic-gate usage() 65*0Sstevel@tonic-gate { 66*0Sstevel@tonic-gate (void) fprintf(stderr, gettext( 67*0Sstevel@tonic-gate "Usage:\tsvccfg [-v] [-s FMRI] [-f file]\n" 68*0Sstevel@tonic-gate "\tsvccfg [-v] [-s FMRI] <command> [args]\n")); 69*0Sstevel@tonic-gate exit(UU_EXIT_USAGE); 70*0Sstevel@tonic-gate } 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate void * 73*0Sstevel@tonic-gate safe_malloc(size_t sz) 74*0Sstevel@tonic-gate { 75*0Sstevel@tonic-gate void *p; 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate if ((p = calloc(1, sz)) == NULL) 78*0Sstevel@tonic-gate uu_die(gettext("Out of memory.\n")); 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate return (p); 81*0Sstevel@tonic-gate } 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate char * 84*0Sstevel@tonic-gate safe_strdup(const char *cp) 85*0Sstevel@tonic-gate { 86*0Sstevel@tonic-gate char *result; 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate result = strdup(cp); 89*0Sstevel@tonic-gate if (result == NULL) 90*0Sstevel@tonic-gate uu_die(gettext("Out of memory.\n")); 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate return (result); 93*0Sstevel@tonic-gate } 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate /* 96*0Sstevel@tonic-gate * Send a message to the user. If we're interactive, send it to stdout. 97*0Sstevel@tonic-gate * Otherwise send it to stderr. 98*0Sstevel@tonic-gate */ 99*0Sstevel@tonic-gate static void 100*0Sstevel@tonic-gate vmessage(const char *fmt, va_list va) 101*0Sstevel@tonic-gate { 102*0Sstevel@tonic-gate int interactive = est->sc_cmd_flags & SC_CMD_IACTIVE; 103*0Sstevel@tonic-gate FILE *strm = interactive ? stdout : stderr; 104*0Sstevel@tonic-gate const char *ptr; 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate if (!interactive) { 107*0Sstevel@tonic-gate if (est->sc_cmd_file == NULL) 108*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", myname); 109*0Sstevel@tonic-gate else 110*0Sstevel@tonic-gate (void) fprintf(stderr, "%s (%s, line %d): ", myname, 111*0Sstevel@tonic-gate est->sc_cmd_filename, est->sc_cmd_lineno - 1); 112*0Sstevel@tonic-gate } 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate if (vfprintf(strm, fmt, va) < 0 && interactive) 115*0Sstevel@tonic-gate uu_die(gettext("printf() error")); 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate ptr = strchr(fmt, '\0'); 118*0Sstevel@tonic-gate if (*(ptr - 1) != '\n') 119*0Sstevel@tonic-gate (void) fprintf(strm, ": %s.\n", strerror(errno)); 120*0Sstevel@tonic-gate } 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate /* 123*0Sstevel@tonic-gate * Display a warning. Should usually be predicated by g_verbose. 124*0Sstevel@tonic-gate */ 125*0Sstevel@tonic-gate /* PRINTFLIKE1 */ 126*0Sstevel@tonic-gate void 127*0Sstevel@tonic-gate warn(const char *fmt, ...) 128*0Sstevel@tonic-gate { 129*0Sstevel@tonic-gate va_list va; 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate va_start(va, fmt); 132*0Sstevel@tonic-gate vmessage(fmt, va); 133*0Sstevel@tonic-gate va_end(va); 134*0Sstevel@tonic-gate } 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate /* 137*0Sstevel@tonic-gate * Syntax error. 138*0Sstevel@tonic-gate */ 139*0Sstevel@tonic-gate void 140*0Sstevel@tonic-gate synerr(int com) 141*0Sstevel@tonic-gate { 142*0Sstevel@tonic-gate if (est->sc_cmd_flags & SC_CMD_IACTIVE) { 143*0Sstevel@tonic-gate help(com); 144*0Sstevel@tonic-gate return; 145*0Sstevel@tonic-gate } 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate warn(gettext("Syntax error.\n")); 148*0Sstevel@tonic-gate exit(1); 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate /* 152*0Sstevel@tonic-gate * Semantic error. Display the warning and exit if we're not interactive. 153*0Sstevel@tonic-gate */ 154*0Sstevel@tonic-gate /* PRINTFLIKE1 */ 155*0Sstevel@tonic-gate void 156*0Sstevel@tonic-gate semerr(const char *fmt, ...) 157*0Sstevel@tonic-gate { 158*0Sstevel@tonic-gate va_list va; 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate va_start(va, fmt); 161*0Sstevel@tonic-gate vmessage(fmt, va); 162*0Sstevel@tonic-gate va_end(va); 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate if ((est->sc_cmd_flags & (SC_CMD_IACTIVE | SC_CMD_DONT_EXIT)) == 0) 165*0Sstevel@tonic-gate exit(1); 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate /*ARGSUSED*/ 169*0Sstevel@tonic-gate static void 170*0Sstevel@tonic-gate initialize(int argc, char *argv[]) 171*0Sstevel@tonic-gate { 172*0Sstevel@tonic-gate myname = uu_setpname(argv[0]); 173*0Sstevel@tonic-gate (void) atexit(lscf_cleanup); 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 176*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate (void) lxml_init(); 179*0Sstevel@tonic-gate internal_init(); 180*0Sstevel@tonic-gate engine_init(); 181*0Sstevel@tonic-gate lscf_init(); /* must follow engine_init() */ 182*0Sstevel@tonic-gate } 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate int 185*0Sstevel@tonic-gate main(int argc, char *argv[]) 186*0Sstevel@tonic-gate { 187*0Sstevel@tonic-gate char *cmd, *command_file = NULL; 188*0Sstevel@tonic-gate char *fmri = NULL; 189*0Sstevel@tonic-gate int c; 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate while ((c = getopt(argc, argv, "vf:s:")) != EOF) 192*0Sstevel@tonic-gate switch (c) { 193*0Sstevel@tonic-gate case 'v': 194*0Sstevel@tonic-gate g_verbose = 1; 195*0Sstevel@tonic-gate break; 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate case 's': 198*0Sstevel@tonic-gate fmri = optarg; 199*0Sstevel@tonic-gate break; 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate case 'f': 202*0Sstevel@tonic-gate command_file = optarg; 203*0Sstevel@tonic-gate break; 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate default: 206*0Sstevel@tonic-gate usage(); 207*0Sstevel@tonic-gate break; 208*0Sstevel@tonic-gate } 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate initialize(argc, argv); 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate if (fmri != NULL) 213*0Sstevel@tonic-gate lscf_select(fmri); 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate if (command_file != NULL) 216*0Sstevel@tonic-gate return (engine_source(command_file, 0)); 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate if (optind == argc) { 219*0Sstevel@tonic-gate if (isatty(fileno(stdin))) 220*0Sstevel@tonic-gate return (engine_interp()); 221*0Sstevel@tonic-gate else 222*0Sstevel@tonic-gate return (engine_source("-", 0)); 223*0Sstevel@tonic-gate } 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate /* 226*0Sstevel@tonic-gate * Knit together remaining arguments into a single statement. 227*0Sstevel@tonic-gate */ 228*0Sstevel@tonic-gate cmd = safe_malloc(MAX_CMD_LINE_SZ); 229*0Sstevel@tonic-gate for (c = optind; c < argc; c++) { 230*0Sstevel@tonic-gate (void) strlcat(cmd, argv[c], MAX_CMD_LINE_SZ); 231*0Sstevel@tonic-gate (void) strlcat(cmd, " ", MAX_CMD_LINE_SZ); 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate return (engine_exec(cmd)); 235*0Sstevel@tonic-gate } 236