xref: /onnv-gate/usr/src/cmd/stmfproxy/aluaadm/aluaadm.c (revision 10725:cd144ed668c5)
1*10725SJohn.Forte@Sun.COM /*
2*10725SJohn.Forte@Sun.COM  * CDDL HEADER START
3*10725SJohn.Forte@Sun.COM  *
4*10725SJohn.Forte@Sun.COM  * The contents of this file are subject to the terms of the
5*10725SJohn.Forte@Sun.COM  * Common Development and Distribution License (the "License").
6*10725SJohn.Forte@Sun.COM  * You may not use this file except in compliance with the License.
7*10725SJohn.Forte@Sun.COM  *
8*10725SJohn.Forte@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10725SJohn.Forte@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*10725SJohn.Forte@Sun.COM  * See the License for the specific language governing permissions
11*10725SJohn.Forte@Sun.COM  * and limitations under the License.
12*10725SJohn.Forte@Sun.COM  *
13*10725SJohn.Forte@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*10725SJohn.Forte@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10725SJohn.Forte@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*10725SJohn.Forte@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*10725SJohn.Forte@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*10725SJohn.Forte@Sun.COM  *
19*10725SJohn.Forte@Sun.COM  * CDDL HEADER END
20*10725SJohn.Forte@Sun.COM  */
21*10725SJohn.Forte@Sun.COM /*
22*10725SJohn.Forte@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23*10725SJohn.Forte@Sun.COM  * Use is subject to license terms.
24*10725SJohn.Forte@Sun.COM  */
25*10725SJohn.Forte@Sun.COM 
26*10725SJohn.Forte@Sun.COM #include <stdlib.h>
27*10725SJohn.Forte@Sun.COM #include <stdio.h>
28*10725SJohn.Forte@Sun.COM #include <strings.h>
29*10725SJohn.Forte@Sun.COM #include <sys/types.h>
30*10725SJohn.Forte@Sun.COM #include <unistd.h>
31*10725SJohn.Forte@Sun.COM #include <wchar.h>
32*10725SJohn.Forte@Sun.COM #include <libintl.h>
33*10725SJohn.Forte@Sun.COM #include <errno.h>
34*10725SJohn.Forte@Sun.COM #include <time.h>
35*10725SJohn.Forte@Sun.COM #include <string.h>
36*10725SJohn.Forte@Sun.COM #include <assert.h>
37*10725SJohn.Forte@Sun.COM #include <getopt.h>
38*10725SJohn.Forte@Sun.COM #include <cmdparse.h>
39*10725SJohn.Forte@Sun.COM #include <libstmf.h>
40*10725SJohn.Forte@Sun.COM #include <signal.h>
41*10725SJohn.Forte@Sun.COM #include <pthread.h>
42*10725SJohn.Forte@Sun.COM #include <locale.h>
43*10725SJohn.Forte@Sun.COM 
44*10725SJohn.Forte@Sun.COM static char *getExecBasename(char *);
45*10725SJohn.Forte@Sun.COM static int setLuStandbyFunc(int, char **, cmdOptions_t *, void *);
46*10725SJohn.Forte@Sun.COM static int disableAluaFunc(int, char **, cmdOptions_t *, void *);
47*10725SJohn.Forte@Sun.COM static int enableAluaFunc(int, char **, cmdOptions_t *, void *);
48*10725SJohn.Forte@Sun.COM 
49*10725SJohn.Forte@Sun.COM #define	OPERANDSTRING_LU	    "LU-name"
50*10725SJohn.Forte@Sun.COM #define	OPERANDSTRING_NODE_ID	    "node ID (0 or 1)"
51*10725SJohn.Forte@Sun.COM 
52*10725SJohn.Forte@Sun.COM #define	VERSION_STRING_MAJOR	    "1"
53*10725SJohn.Forte@Sun.COM #define	VERSION_STRING_MINOR	    "0"
54*10725SJohn.Forte@Sun.COM #define	VERSION_STRING_MAX_LEN	    10
55*10725SJohn.Forte@Sun.COM 
56*10725SJohn.Forte@Sun.COM #define	GUID_INPUT		    32
57*10725SJohn.Forte@Sun.COM 
58*10725SJohn.Forte@Sun.COM /* tables set up based on cmdparse instructions */
59*10725SJohn.Forte@Sun.COM 
60*10725SJohn.Forte@Sun.COM /* add new options here */
61*10725SJohn.Forte@Sun.COM optionTbl_t longOptions[] = {
62*10725SJohn.Forte@Sun.COM 	{NULL, 0, 0, 0}
63*10725SJohn.Forte@Sun.COM };
64*10725SJohn.Forte@Sun.COM 
65*10725SJohn.Forte@Sun.COM /*
66*10725SJohn.Forte@Sun.COM  * Add new subcommands here
67*10725SJohn.Forte@Sun.COM  */
68*10725SJohn.Forte@Sun.COM subCommandProps_t subcommands[] = {
69*10725SJohn.Forte@Sun.COM 	{"standby", setLuStandbyFunc, NULL, NULL, NULL,
70*10725SJohn.Forte@Sun.COM 		OPERAND_MANDATORY_SINGLE, OPERANDSTRING_LU, NULL},
71*10725SJohn.Forte@Sun.COM 	{"disable", disableAluaFunc, NULL, NULL, NULL,
72*10725SJohn.Forte@Sun.COM 		OPERAND_NONE, NULL, NULL},
73*10725SJohn.Forte@Sun.COM 	{"enable", enableAluaFunc, NULL, NULL, NULL,
74*10725SJohn.Forte@Sun.COM 		OPERAND_MANDATORY_SINGLE, OPERANDSTRING_NODE_ID, NULL},
75*10725SJohn.Forte@Sun.COM 	{NULL, 0, NULL, NULL, 0, NULL, 0, NULL, NULL}
76*10725SJohn.Forte@Sun.COM };
77*10725SJohn.Forte@Sun.COM 
78*10725SJohn.Forte@Sun.COM /* globals */
79*10725SJohn.Forte@Sun.COM char *cmdName;
80*10725SJohn.Forte@Sun.COM 
81*10725SJohn.Forte@Sun.COM /*
82*10725SJohn.Forte@Sun.COM  * setLuStandbyFunc
83*10725SJohn.Forte@Sun.COM  *
84*10725SJohn.Forte@Sun.COM  * Purpose: set lu to standby
85*10725SJohn.Forte@Sun.COM  *
86*10725SJohn.Forte@Sun.COM  */
87*10725SJohn.Forte@Sun.COM /*ARGSUSED*/
88*10725SJohn.Forte@Sun.COM static int
setLuStandbyFunc(int operandLen,char * operands[],cmdOptions_t * options,void * args)89*10725SJohn.Forte@Sun.COM setLuStandbyFunc(int operandLen, char *operands[], cmdOptions_t *options,
90*10725SJohn.Forte@Sun.COM     void *args)
91*10725SJohn.Forte@Sun.COM {
92*10725SJohn.Forte@Sun.COM 	char sGuid[GUID_INPUT + 1];
93*10725SJohn.Forte@Sun.COM 	stmfGuid inGuid;
94*10725SJohn.Forte@Sun.COM 	unsigned int guid[sizeof (stmfGuid)];
95*10725SJohn.Forte@Sun.COM 	int i;
96*10725SJohn.Forte@Sun.COM 	int ret = 0;
97*10725SJohn.Forte@Sun.COM 
98*10725SJohn.Forte@Sun.COM 	if (strlen(operands[0]) != GUID_INPUT) {
99*10725SJohn.Forte@Sun.COM 		(void) fprintf(stderr, "%s: %s: %s %d %s\n", cmdName,
100*10725SJohn.Forte@Sun.COM 		    operands[0], gettext("must be"), GUID_INPUT,
101*10725SJohn.Forte@Sun.COM 		    gettext("hexadecimal digits long"));
102*10725SJohn.Forte@Sun.COM 		return (1);
103*10725SJohn.Forte@Sun.COM 	}
104*10725SJohn.Forte@Sun.COM 
105*10725SJohn.Forte@Sun.COM 	bcopy(operands[0], sGuid, GUID_INPUT);
106*10725SJohn.Forte@Sun.COM 
107*10725SJohn.Forte@Sun.COM 	for (i = 0; i < GUID_INPUT; i++)
108*10725SJohn.Forte@Sun.COM 		sGuid[i] = tolower(sGuid[i]);
109*10725SJohn.Forte@Sun.COM 	sGuid[i] = 0;
110*10725SJohn.Forte@Sun.COM 
111*10725SJohn.Forte@Sun.COM 	(void) sscanf(sGuid, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
112*10725SJohn.Forte@Sun.COM 	    &guid[0], &guid[1], &guid[2], &guid[3], &guid[4], &guid[5],
113*10725SJohn.Forte@Sun.COM 	    &guid[6], &guid[7], &guid[8], &guid[9], &guid[10], &guid[11],
114*10725SJohn.Forte@Sun.COM 	    &guid[12], &guid[13], &guid[14], &guid[15]);
115*10725SJohn.Forte@Sun.COM 
116*10725SJohn.Forte@Sun.COM 	for (i = 0; i < sizeof (stmfGuid); i++) {
117*10725SJohn.Forte@Sun.COM 		inGuid.guid[i] = guid[i];
118*10725SJohn.Forte@Sun.COM 	}
119*10725SJohn.Forte@Sun.COM 
120*10725SJohn.Forte@Sun.COM 	ret = stmfLuStandby(&inGuid);
121*10725SJohn.Forte@Sun.COM 	if (ret != STMF_STATUS_SUCCESS) {
122*10725SJohn.Forte@Sun.COM 		switch (ret) {
123*10725SJohn.Forte@Sun.COM 			case STMF_ERROR_PERM:
124*10725SJohn.Forte@Sun.COM 				(void) fprintf(stderr, "%s: %s\n", cmdName,
125*10725SJohn.Forte@Sun.COM 				    gettext("permission denied"));
126*10725SJohn.Forte@Sun.COM 				break;
127*10725SJohn.Forte@Sun.COM 			case STMF_ERROR_SERVICE_NOT_FOUND:
128*10725SJohn.Forte@Sun.COM 				(void) fprintf(stderr, "%s: %s\n", cmdName,
129*10725SJohn.Forte@Sun.COM 				    gettext("STMF service not found"));
130*10725SJohn.Forte@Sun.COM 				break;
131*10725SJohn.Forte@Sun.COM 			case STMF_ERROR_NOT_FOUND:
132*10725SJohn.Forte@Sun.COM 				(void) fprintf(stderr, "%s: %s: %s\n", cmdName,
133*10725SJohn.Forte@Sun.COM 				    operands[0], gettext("not found"));
134*10725SJohn.Forte@Sun.COM 				break;
135*10725SJohn.Forte@Sun.COM 			case STMF_ERROR_SERVICE_DATA_VERSION:
136*10725SJohn.Forte@Sun.COM 				(void) fprintf(stderr, "%s: %s\n", cmdName,
137*10725SJohn.Forte@Sun.COM 				    gettext("STMF service version incorrect"));
138*10725SJohn.Forte@Sun.COM 				break;
139*10725SJohn.Forte@Sun.COM 			default:
140*10725SJohn.Forte@Sun.COM 				(void) fprintf(stderr, "%s: %s\n", cmdName,
141*10725SJohn.Forte@Sun.COM 				    gettext("unknown error"));
142*10725SJohn.Forte@Sun.COM 				break;
143*10725SJohn.Forte@Sun.COM 		}
144*10725SJohn.Forte@Sun.COM 	}
145*10725SJohn.Forte@Sun.COM 	return (ret);
146*10725SJohn.Forte@Sun.COM }
147*10725SJohn.Forte@Sun.COM 
148*10725SJohn.Forte@Sun.COM /*
149*10725SJohn.Forte@Sun.COM  * disableAluaFunc
150*10725SJohn.Forte@Sun.COM  *
151*10725SJohn.Forte@Sun.COM  * Purpose: disable alua mode
152*10725SJohn.Forte@Sun.COM  *
153*10725SJohn.Forte@Sun.COM  */
154*10725SJohn.Forte@Sun.COM /*ARGSUSED*/
155*10725SJohn.Forte@Sun.COM static int
disableAluaFunc(int operandLen,char * operands[],cmdOptions_t * options,void * args)156*10725SJohn.Forte@Sun.COM disableAluaFunc(int operandLen, char *operands[], cmdOptions_t *options,
157*10725SJohn.Forte@Sun.COM     void *args)
158*10725SJohn.Forte@Sun.COM {
159*10725SJohn.Forte@Sun.COM 	return (stmfSetAluaState(B_FALSE, 0));
160*10725SJohn.Forte@Sun.COM }
161*10725SJohn.Forte@Sun.COM 
162*10725SJohn.Forte@Sun.COM /*
163*10725SJohn.Forte@Sun.COM  * enableAluaFunc
164*10725SJohn.Forte@Sun.COM  *
165*10725SJohn.Forte@Sun.COM  * Purpose: enable alua mode
166*10725SJohn.Forte@Sun.COM  *
167*10725SJohn.Forte@Sun.COM  */
168*10725SJohn.Forte@Sun.COM /*ARGSUSED*/
169*10725SJohn.Forte@Sun.COM static int
enableAluaFunc(int operandLen,char * operands[],cmdOptions_t * options,void * args)170*10725SJohn.Forte@Sun.COM enableAluaFunc(int operandLen, char *operands[], cmdOptions_t *options,
171*10725SJohn.Forte@Sun.COM     void *args)
172*10725SJohn.Forte@Sun.COM {
173*10725SJohn.Forte@Sun.COM 	uint8_t node_id = 0;
174*10725SJohn.Forte@Sun.COM 	if (operands[0][1] == '1') {
175*10725SJohn.Forte@Sun.COM 		node_id = 1;
176*10725SJohn.Forte@Sun.COM 	}
177*10725SJohn.Forte@Sun.COM 	return (stmfSetAluaState(B_TRUE, node_id));
178*10725SJohn.Forte@Sun.COM }
179*10725SJohn.Forte@Sun.COM 
180*10725SJohn.Forte@Sun.COM 
181*10725SJohn.Forte@Sun.COM /*
182*10725SJohn.Forte@Sun.COM  * input:
183*10725SJohn.Forte@Sun.COM  *  execFullName - exec name of program (argv[0])
184*10725SJohn.Forte@Sun.COM  *
185*10725SJohn.Forte@Sun.COM  *  copied from usr/src/cmd/zoneadm/zoneadm.c in OS/Net
186*10725SJohn.Forte@Sun.COM  *  (changed name to lowerCamelCase to keep consistent with this file)
187*10725SJohn.Forte@Sun.COM  *
188*10725SJohn.Forte@Sun.COM  * Returns:
189*10725SJohn.Forte@Sun.COM  *  command name portion of execFullName
190*10725SJohn.Forte@Sun.COM  */
191*10725SJohn.Forte@Sun.COM static char *
getExecBasename(char * execFullname)192*10725SJohn.Forte@Sun.COM getExecBasename(char *execFullname)
193*10725SJohn.Forte@Sun.COM {
194*10725SJohn.Forte@Sun.COM 	char *lastSlash, *execBasename;
195*10725SJohn.Forte@Sun.COM 
196*10725SJohn.Forte@Sun.COM 	/* guard against '/' at end of command invocation */
197*10725SJohn.Forte@Sun.COM 	for (;;) {
198*10725SJohn.Forte@Sun.COM 		lastSlash = strrchr(execFullname, '/');
199*10725SJohn.Forte@Sun.COM 		if (lastSlash == NULL) {
200*10725SJohn.Forte@Sun.COM 			execBasename = execFullname;
201*10725SJohn.Forte@Sun.COM 			break;
202*10725SJohn.Forte@Sun.COM 		} else {
203*10725SJohn.Forte@Sun.COM 			execBasename = lastSlash + 1;
204*10725SJohn.Forte@Sun.COM 			if (*execBasename == '\0') {
205*10725SJohn.Forte@Sun.COM 				*lastSlash = '\0';
206*10725SJohn.Forte@Sun.COM 				continue;
207*10725SJohn.Forte@Sun.COM 			}
208*10725SJohn.Forte@Sun.COM 			break;
209*10725SJohn.Forte@Sun.COM 		}
210*10725SJohn.Forte@Sun.COM 	}
211*10725SJohn.Forte@Sun.COM 	return (execBasename);
212*10725SJohn.Forte@Sun.COM }
213*10725SJohn.Forte@Sun.COM 
214*10725SJohn.Forte@Sun.COM int
main(int argc,char * argv[])215*10725SJohn.Forte@Sun.COM main(int argc, char *argv[])
216*10725SJohn.Forte@Sun.COM {
217*10725SJohn.Forte@Sun.COM 	synTables_t synTables;
218*10725SJohn.Forte@Sun.COM 	char versionString[VERSION_STRING_MAX_LEN];
219*10725SJohn.Forte@Sun.COM 	int ret;
220*10725SJohn.Forte@Sun.COM 	int funcRet;
221*10725SJohn.Forte@Sun.COM 	void *subcommandArgs = NULL;
222*10725SJohn.Forte@Sun.COM 
223*10725SJohn.Forte@Sun.COM 	(void) setlocale(LC_ALL, "");
224*10725SJohn.Forte@Sun.COM 	(void) textdomain(TEXT_DOMAIN);
225*10725SJohn.Forte@Sun.COM 	/* set global command name */
226*10725SJohn.Forte@Sun.COM 	cmdName = getExecBasename(argv[0]);
227*10725SJohn.Forte@Sun.COM 
228*10725SJohn.Forte@Sun.COM 	(void) snprintf(versionString, VERSION_STRING_MAX_LEN, "%s.%s",
229*10725SJohn.Forte@Sun.COM 	    VERSION_STRING_MAJOR, VERSION_STRING_MINOR);
230*10725SJohn.Forte@Sun.COM 	synTables.versionString = versionString;
231*10725SJohn.Forte@Sun.COM 	synTables.longOptionTbl = &longOptions[0];
232*10725SJohn.Forte@Sun.COM 	synTables.subCommandPropsTbl = &subcommands[0];
233*10725SJohn.Forte@Sun.COM 
234*10725SJohn.Forte@Sun.COM 	ret = cmdParse(argc, argv, synTables, subcommandArgs, &funcRet);
235*10725SJohn.Forte@Sun.COM 	if (ret != 0) {
236*10725SJohn.Forte@Sun.COM 		return (ret);
237*10725SJohn.Forte@Sun.COM 	}
238*10725SJohn.Forte@Sun.COM 
239*10725SJohn.Forte@Sun.COM 	return (funcRet);
240*10725SJohn.Forte@Sun.COM } /* end main */
241