xref: /titanic_50/usr/src/cmd/stmfsvc/stmfsvc.c (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
1*fcf3ce44SJohn Forte /*
2*fcf3ce44SJohn Forte  * CDDL HEADER START
3*fcf3ce44SJohn Forte  *
4*fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5*fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6*fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7*fcf3ce44SJohn Forte  *
8*fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10*fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11*fcf3ce44SJohn Forte  * and limitations under the License.
12*fcf3ce44SJohn Forte  *
13*fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14*fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16*fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17*fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18*fcf3ce44SJohn Forte  *
19*fcf3ce44SJohn Forte  * CDDL HEADER END
20*fcf3ce44SJohn Forte  */
21*fcf3ce44SJohn Forte /*
22*fcf3ce44SJohn Forte  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*fcf3ce44SJohn Forte  * Use is subject to license terms.
24*fcf3ce44SJohn Forte  */
25*fcf3ce44SJohn Forte 
26*fcf3ce44SJohn Forte #include <stdlib.h>
27*fcf3ce44SJohn Forte #include <stdio.h>
28*fcf3ce44SJohn Forte #include <strings.h>
29*fcf3ce44SJohn Forte #include <sys/types.h>
30*fcf3ce44SJohn Forte #include <unistd.h>
31*fcf3ce44SJohn Forte #include <libintl.h>
32*fcf3ce44SJohn Forte #include <errno.h>
33*fcf3ce44SJohn Forte #include <time.h>
34*fcf3ce44SJohn Forte #include <string.h>
35*fcf3ce44SJohn Forte #include <assert.h>
36*fcf3ce44SJohn Forte #include <getopt.h>
37*fcf3ce44SJohn Forte #include <cmdparse.h>
38*fcf3ce44SJohn Forte #include <libstmf.h>
39*fcf3ce44SJohn Forte #include <signal.h>
40*fcf3ce44SJohn Forte #include <pthread.h>
41*fcf3ce44SJohn Forte #include <locale.h>
42*fcf3ce44SJohn Forte 
43*fcf3ce44SJohn Forte static int svcStart(int, char **, cmdOptions_t *, void *);
44*fcf3ce44SJohn Forte static int svcStop(int, char **, cmdOptions_t *, void *);
45*fcf3ce44SJohn Forte static int online();
46*fcf3ce44SJohn Forte 
47*fcf3ce44SJohn Forte /*
48*fcf3ce44SJohn Forte  *  MAJOR - This should only change when there is an incompatible change made
49*fcf3ce44SJohn Forte  *  to the interfaces or the output.
50*fcf3ce44SJohn Forte  *
51*fcf3ce44SJohn Forte  *  MINOR - This should change whenever there is a new command or new feature
52*fcf3ce44SJohn Forte  *  with no incompatible change.
53*fcf3ce44SJohn Forte  */
54*fcf3ce44SJohn Forte #define	VERSION_STRING_MAJOR	    "1"
55*fcf3ce44SJohn Forte #define	VERSION_STRING_MINOR	    "0"
56*fcf3ce44SJohn Forte #define	VERSION_STRING_MAX_LEN	    10
57*fcf3ce44SJohn Forte 
58*fcf3ce44SJohn Forte /* 10 ms sleep in nanoseconds */
59*fcf3ce44SJohn Forte #define	TEN_MS_NANOSLEEP  10000000
60*fcf3ce44SJohn Forte 
61*fcf3ce44SJohn Forte /* tables set up based on cmdparse instructions */
62*fcf3ce44SJohn Forte 
63*fcf3ce44SJohn Forte /* add new options here */
64*fcf3ce44SJohn Forte optionTbl_t longOptions[] = {
65*fcf3ce44SJohn Forte 	{NULL, 0, 0, 0}
66*fcf3ce44SJohn Forte };
67*fcf3ce44SJohn Forte 
68*fcf3ce44SJohn Forte /*
69*fcf3ce44SJohn Forte  * Add new subcommands here
70*fcf3ce44SJohn Forte  */
71*fcf3ce44SJohn Forte subCommandProps_t subcommands[] = {
72*fcf3ce44SJohn Forte 	{"start", svcStart, NULL, NULL, NULL, OPERAND_NONE, NULL},
73*fcf3ce44SJohn Forte 	{"stop", svcStop, NULL, NULL, NULL, OPERAND_NONE, NULL},
74*fcf3ce44SJohn Forte 	{NULL, 0, NULL, NULL, 0, NULL, 0, NULL}
75*fcf3ce44SJohn Forte };
76*fcf3ce44SJohn Forte 
77*fcf3ce44SJohn Forte /* globals */
78*fcf3ce44SJohn Forte char *cmdName;
79*fcf3ce44SJohn Forte 
80*fcf3ce44SJohn Forte /*
81*fcf3ce44SJohn Forte  * svcStop
82*fcf3ce44SJohn Forte  *
83*fcf3ce44SJohn Forte  * Offlines the stmf service
84*fcf3ce44SJohn Forte  *
85*fcf3ce44SJohn Forte  */
86*fcf3ce44SJohn Forte /*ARGSUSED*/
87*fcf3ce44SJohn Forte static int
88*fcf3ce44SJohn Forte svcStop(int operandLen, char *operands[], cmdOptions_t *options,
89*fcf3ce44SJohn Forte     void *args)
90*fcf3ce44SJohn Forte {
91*fcf3ce44SJohn Forte 	int stmfRet;
92*fcf3ce44SJohn Forte 	int ret = 0;
93*fcf3ce44SJohn Forte 	stmfState state;
94*fcf3ce44SJohn Forte 	boolean_t serviceOffline = B_FALSE;
95*fcf3ce44SJohn Forte 	struct timespec rqtp;
96*fcf3ce44SJohn Forte 
97*fcf3ce44SJohn Forte 	bzero(&rqtp, sizeof (rqtp));
98*fcf3ce44SJohn Forte 
99*fcf3ce44SJohn Forte 	rqtp.tv_nsec = TEN_MS_NANOSLEEP;
100*fcf3ce44SJohn Forte 
101*fcf3ce44SJohn Forte 	if ((stmfRet = stmfOffline()) != STMF_STATUS_SUCCESS) {
102*fcf3ce44SJohn Forte 		switch (stmfRet) {
103*fcf3ce44SJohn Forte 			case STMF_ERROR_PERM:
104*fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n", cmdName,
105*fcf3ce44SJohn Forte 				    gettext("permission denied"));
106*fcf3ce44SJohn Forte 				break;
107*fcf3ce44SJohn Forte 			case STMF_ERROR_SERVICE_NOT_FOUND:
108*fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n", cmdName,
109*fcf3ce44SJohn Forte 				    gettext("STMF service not found"));
110*fcf3ce44SJohn Forte 				break;
111*fcf3ce44SJohn Forte 			case STMF_ERROR_SERVICE_OFFLINE:
112*fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n", cmdName,
113*fcf3ce44SJohn Forte 				    gettext("STMF service already offline"));
114*fcf3ce44SJohn Forte 				break;
115*fcf3ce44SJohn Forte 			default:
116*fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n", cmdName,
117*fcf3ce44SJohn Forte 				    gettext("unable to offline service"));
118*fcf3ce44SJohn Forte 				break;
119*fcf3ce44SJohn Forte 		}
120*fcf3ce44SJohn Forte 		return (1);
121*fcf3ce44SJohn Forte 	}
122*fcf3ce44SJohn Forte 
123*fcf3ce44SJohn Forte 	/* wait for service offline */
124*fcf3ce44SJohn Forte 	while (!serviceOffline) {
125*fcf3ce44SJohn Forte 		stmfRet = stmfGetState(&state);
126*fcf3ce44SJohn Forte 		if (stmfRet != STMF_STATUS_SUCCESS) {
127*fcf3ce44SJohn Forte 			ret = 1;
128*fcf3ce44SJohn Forte 			break;
129*fcf3ce44SJohn Forte 		}
130*fcf3ce44SJohn Forte 		if (state.operationalState == STMF_SERVICE_STATE_OFFLINE) {
131*fcf3ce44SJohn Forte 			serviceOffline = B_TRUE;
132*fcf3ce44SJohn Forte 		} else {
133*fcf3ce44SJohn Forte 			(void) nanosleep(&rqtp, NULL);
134*fcf3ce44SJohn Forte 		}
135*fcf3ce44SJohn Forte 	}
136*fcf3ce44SJohn Forte 
137*fcf3ce44SJohn Forte 	return (ret);
138*fcf3ce44SJohn Forte }
139*fcf3ce44SJohn Forte 
140*fcf3ce44SJohn Forte /*
141*fcf3ce44SJohn Forte  * loadConfig
142*fcf3ce44SJohn Forte  *
143*fcf3ce44SJohn Forte  * Loads the stmf config from the SMF repository
144*fcf3ce44SJohn Forte  *
145*fcf3ce44SJohn Forte  */
146*fcf3ce44SJohn Forte /*ARGSUSED*/
147*fcf3ce44SJohn Forte static int
148*fcf3ce44SJohn Forte svcStart(int operandLen, char *operands[], cmdOptions_t *options,
149*fcf3ce44SJohn Forte     void *args)
150*fcf3ce44SJohn Forte {
151*fcf3ce44SJohn Forte 	int stmfRet;
152*fcf3ce44SJohn Forte 	int ret = 0;
153*fcf3ce44SJohn Forte 	if ((stmfRet = stmfLoadConfig()) != STMF_STATUS_SUCCESS) {
154*fcf3ce44SJohn Forte 		switch (stmfRet) {
155*fcf3ce44SJohn Forte 			case STMF_ERROR_PERM:
156*fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n", cmdName,
157*fcf3ce44SJohn Forte 				    gettext("permission denied"));
158*fcf3ce44SJohn Forte 				break;
159*fcf3ce44SJohn Forte 			case STMF_ERROR_SERVICE_NOT_FOUND:
160*fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n", cmdName,
161*fcf3ce44SJohn Forte 				    gettext("STMF service not found"));
162*fcf3ce44SJohn Forte 				break;
163*fcf3ce44SJohn Forte 			case STMF_ERROR_SERVICE_ONLINE:
164*fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n", cmdName,
165*fcf3ce44SJohn Forte 				    gettext("STMF service must be offline"));
166*fcf3ce44SJohn Forte 				break;
167*fcf3ce44SJohn Forte 			default:
168*fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n", cmdName,
169*fcf3ce44SJohn Forte 				    gettext("unable to load config"));
170*fcf3ce44SJohn Forte 				break;
171*fcf3ce44SJohn Forte 		}
172*fcf3ce44SJohn Forte 		return (1);
173*fcf3ce44SJohn Forte 	}
174*fcf3ce44SJohn Forte 	ret = online();
175*fcf3ce44SJohn Forte 	return (ret);
176*fcf3ce44SJohn Forte 
177*fcf3ce44SJohn Forte }
178*fcf3ce44SJohn Forte 
179*fcf3ce44SJohn Forte /*
180*fcf3ce44SJohn Forte  * online
181*fcf3ce44SJohn Forte  *
182*fcf3ce44SJohn Forte  * Onlines the stmf service
183*fcf3ce44SJohn Forte  *
184*fcf3ce44SJohn Forte  */
185*fcf3ce44SJohn Forte /*ARGSUSED*/
186*fcf3ce44SJohn Forte static int
187*fcf3ce44SJohn Forte online()
188*fcf3ce44SJohn Forte {
189*fcf3ce44SJohn Forte 	int stmfRet;
190*fcf3ce44SJohn Forte 	int ret = 0;
191*fcf3ce44SJohn Forte 	stmfState state;
192*fcf3ce44SJohn Forte 	boolean_t serviceOnline = B_FALSE;
193*fcf3ce44SJohn Forte 	struct timespec rqtp;
194*fcf3ce44SJohn Forte 
195*fcf3ce44SJohn Forte 	bzero(&rqtp, sizeof (rqtp));
196*fcf3ce44SJohn Forte 
197*fcf3ce44SJohn Forte 	rqtp.tv_nsec = TEN_MS_NANOSLEEP;
198*fcf3ce44SJohn Forte 
199*fcf3ce44SJohn Forte 	if ((stmfRet = stmfOnline()) != STMF_STATUS_SUCCESS) {
200*fcf3ce44SJohn Forte 		switch (stmfRet) {
201*fcf3ce44SJohn Forte 			case STMF_ERROR_PERM:
202*fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n", cmdName,
203*fcf3ce44SJohn Forte 				    gettext("permission denied"));
204*fcf3ce44SJohn Forte 				break;
205*fcf3ce44SJohn Forte 			case STMF_ERROR_SERVICE_NOT_FOUND:
206*fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n", cmdName,
207*fcf3ce44SJohn Forte 				    gettext("STMF service not found"));
208*fcf3ce44SJohn Forte 				break;
209*fcf3ce44SJohn Forte 			case STMF_ERROR_SERVICE_ONLINE:
210*fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n", cmdName,
211*fcf3ce44SJohn Forte 				    gettext("STMF service already online"));
212*fcf3ce44SJohn Forte 				break;
213*fcf3ce44SJohn Forte 			default:
214*fcf3ce44SJohn Forte 				(void) fprintf(stderr, "%s: %s\n", cmdName,
215*fcf3ce44SJohn Forte 				    gettext("unable to online service"));
216*fcf3ce44SJohn Forte 				break;
217*fcf3ce44SJohn Forte 		}
218*fcf3ce44SJohn Forte 		return (1);
219*fcf3ce44SJohn Forte 	}
220*fcf3ce44SJohn Forte 
221*fcf3ce44SJohn Forte 	/* wait for service online */
222*fcf3ce44SJohn Forte 	while (!serviceOnline) {
223*fcf3ce44SJohn Forte 		stmfRet = stmfGetState(&state);
224*fcf3ce44SJohn Forte 		if (stmfRet != STMF_STATUS_SUCCESS) {
225*fcf3ce44SJohn Forte 			ret = 1;
226*fcf3ce44SJohn Forte 			break;
227*fcf3ce44SJohn Forte 		}
228*fcf3ce44SJohn Forte 		if (state.operationalState == STMF_SERVICE_STATE_ONLINE) {
229*fcf3ce44SJohn Forte 			serviceOnline = B_TRUE;
230*fcf3ce44SJohn Forte 		} else {
231*fcf3ce44SJohn Forte 			(void) nanosleep(&rqtp, NULL);
232*fcf3ce44SJohn Forte 		}
233*fcf3ce44SJohn Forte 	}
234*fcf3ce44SJohn Forte 
235*fcf3ce44SJohn Forte 	return (ret);
236*fcf3ce44SJohn Forte }
237*fcf3ce44SJohn Forte 
238*fcf3ce44SJohn Forte 
239*fcf3ce44SJohn Forte /*
240*fcf3ce44SJohn Forte  * input:
241*fcf3ce44SJohn Forte  *  execFullName - exec name of program (argv[0])
242*fcf3ce44SJohn Forte  *
243*fcf3ce44SJohn Forte  *  copied from usr/src/cmd/zoneadm/zoneadm.c in OS/Net
244*fcf3ce44SJohn Forte  *  (changed name to lowerCamelCase to keep consistent with this file)
245*fcf3ce44SJohn Forte  *
246*fcf3ce44SJohn Forte  * Returns:
247*fcf3ce44SJohn Forte  *  command name portion of execFullName
248*fcf3ce44SJohn Forte  */
249*fcf3ce44SJohn Forte static char *
250*fcf3ce44SJohn Forte getExecBasename(char *execFullname)
251*fcf3ce44SJohn Forte {
252*fcf3ce44SJohn Forte 	char *lastSlash, *execBasename;
253*fcf3ce44SJohn Forte 
254*fcf3ce44SJohn Forte 	/* guard against '/' at end of command invocation */
255*fcf3ce44SJohn Forte 	for (;;) {
256*fcf3ce44SJohn Forte 		lastSlash = strrchr(execFullname, '/');
257*fcf3ce44SJohn Forte 		if (lastSlash == NULL) {
258*fcf3ce44SJohn Forte 			execBasename = execFullname;
259*fcf3ce44SJohn Forte 			break;
260*fcf3ce44SJohn Forte 		} else {
261*fcf3ce44SJohn Forte 			execBasename = lastSlash + 1;
262*fcf3ce44SJohn Forte 			if (*execBasename == '\0') {
263*fcf3ce44SJohn Forte 				*lastSlash = '\0';
264*fcf3ce44SJohn Forte 				continue;
265*fcf3ce44SJohn Forte 			}
266*fcf3ce44SJohn Forte 			break;
267*fcf3ce44SJohn Forte 		}
268*fcf3ce44SJohn Forte 	}
269*fcf3ce44SJohn Forte 	return (execBasename);
270*fcf3ce44SJohn Forte }
271*fcf3ce44SJohn Forte 
272*fcf3ce44SJohn Forte int
273*fcf3ce44SJohn Forte main(int argc, char *argv[])
274*fcf3ce44SJohn Forte {
275*fcf3ce44SJohn Forte 	synTables_t synTables;
276*fcf3ce44SJohn Forte 	char versionString[VERSION_STRING_MAX_LEN];
277*fcf3ce44SJohn Forte 	int ret;
278*fcf3ce44SJohn Forte 	int funcRet;
279*fcf3ce44SJohn Forte 	void *subcommandArgs = NULL;
280*fcf3ce44SJohn Forte 
281*fcf3ce44SJohn Forte 	(void) setlocale(LC_ALL, "");
282*fcf3ce44SJohn Forte 	/* set global command name */
283*fcf3ce44SJohn Forte 	cmdName = getExecBasename(argv[0]);
284*fcf3ce44SJohn Forte 
285*fcf3ce44SJohn Forte 	(void) snprintf(versionString, VERSION_STRING_MAX_LEN, "%s.%s",
286*fcf3ce44SJohn Forte 	    VERSION_STRING_MAJOR, VERSION_STRING_MINOR);
287*fcf3ce44SJohn Forte 	synTables.versionString = versionString;
288*fcf3ce44SJohn Forte 	synTables.longOptionTbl = &longOptions[0];
289*fcf3ce44SJohn Forte 	synTables.subCommandPropsTbl = &subcommands[0];
290*fcf3ce44SJohn Forte 
291*fcf3ce44SJohn Forte 	ret = cmdParse(argc, argv, synTables, subcommandArgs, &funcRet);
292*fcf3ce44SJohn Forte 	if (ret != 0) {
293*fcf3ce44SJohn Forte 		return (ret);
294*fcf3ce44SJohn Forte 	}
295*fcf3ce44SJohn Forte 
296*fcf3ce44SJohn Forte 	return (funcRet);
297*fcf3ce44SJohn Forte } /* end main */
298