xref: /titanic_53/usr/src/cmd/fcinfo/fcinfo.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 <fcinfo.h>
27*fcf3ce44SJohn Forte 
28*fcf3ce44SJohn Forte 
29*fcf3ce44SJohn Forte 
30*fcf3ce44SJohn Forte #define	VERSION_STRING_MAX_LEN	10
31*fcf3ce44SJohn Forte /*
32*fcf3ce44SJohn Forte  * Version number:
33*fcf3ce44SJohn Forte  *  MAJOR - This should only change when there is an incompatible change made
34*fcf3ce44SJohn Forte  *  to the interfaces or the output.
35*fcf3ce44SJohn Forte  *
36*fcf3ce44SJohn Forte  *  MINOR - This should change whenever there is a new command or new feature
37*fcf3ce44SJohn Forte  *  with no incompatible change.
38*fcf3ce44SJohn Forte  */
39*fcf3ce44SJohn Forte #define	VERSION_STRING_MAJOR	    "1"
40*fcf3ce44SJohn Forte #define	VERSION_STRING_MINOR	    "0"
41*fcf3ce44SJohn Forte 
42*fcf3ce44SJohn Forte #define	OPTIONSTRING1		"HBA Port WWN"
43*fcf3ce44SJohn Forte #define	OPTIONSTRING2		"HBA Node WWN"
44*fcf3ce44SJohn Forte /* forward declarations */
45*fcf3ce44SJohn Forte static int listHbaPortFunc(int, char **, cmdOptions_t *, void *);
46*fcf3ce44SJohn Forte static int listRemotePortFunc(int, char **, cmdOptions_t *, void *);
47*fcf3ce44SJohn Forte static int listLogicalUnitFunc(int, char **, cmdOptions_t *, void *);
48*fcf3ce44SJohn Forte static int npivCreatePortFunc(int, char **, cmdOptions_t *, void *);
49*fcf3ce44SJohn Forte static int npivDeletePortFunc(int, char **, cmdOptions_t *, void *);
50*fcf3ce44SJohn Forte static int npivCreatePortListFunc(int, char **, cmdOptions_t *, void *);
51*fcf3ce44SJohn Forte static int npivListHbaPortFunc(int, char **, cmdOptions_t *, void *);
52*fcf3ce44SJohn Forte static int npivListRemotePortFunc(int, char **, cmdOptions_t *, void *);
53*fcf3ce44SJohn Forte static char *getExecBasename(char *);
54*fcf3ce44SJohn Forte 
55*fcf3ce44SJohn Forte /*
56*fcf3ce44SJohn Forte  * Add new options here
57*fcf3ce44SJohn Forte  *
58*fcf3ce44SJohn Forte  * Optional option-arguments are not allowed by CLIP
59*fcf3ce44SJohn Forte  */
60*fcf3ce44SJohn Forte optionTbl_t fcinfolongOptions[] = {
61*fcf3ce44SJohn Forte 	{"port", required_argument,	'p', OPTIONSTRING1},
62*fcf3ce44SJohn Forte 	{"target", no_argument,		't', NULL},
63*fcf3ce44SJohn Forte 	{"initiator", no_argument,	'i', NULL},
64*fcf3ce44SJohn Forte 	{"linkstat", no_argument,	'l', NULL},
65*fcf3ce44SJohn Forte 	{"scsi-target", no_argument,	's', NULL},
66*fcf3ce44SJohn Forte 	{"verbose", no_argument,	'v', NULL},
67*fcf3ce44SJohn Forte 	{NULL, 0, 0}
68*fcf3ce44SJohn Forte };
69*fcf3ce44SJohn Forte 
70*fcf3ce44SJohn Forte optionTbl_t fcadmlongOptions[] = {
71*fcf3ce44SJohn Forte 	{"port", required_argument,	'p', OPTIONSTRING1},
72*fcf3ce44SJohn Forte 	{"node", required_argument,	'n', OPTIONSTRING2},
73*fcf3ce44SJohn Forte 	{"linkstat", no_argument,	'l', NULL},
74*fcf3ce44SJohn Forte 	{"scsi-target", no_argument,	's', NULL},
75*fcf3ce44SJohn Forte 	{NULL, 0, 0}
76*fcf3ce44SJohn Forte };
77*fcf3ce44SJohn Forte 
78*fcf3ce44SJohn Forte /*
79*fcf3ce44SJohn Forte  * Add new subcommands here
80*fcf3ce44SJohn Forte  */
81*fcf3ce44SJohn Forte subCommandProps_t fcinfosubcommands[] = {
82*fcf3ce44SJohn Forte 	{"hba-port", listHbaPortFunc, "itl", NULL, NULL,
83*fcf3ce44SJohn Forte 		OPERAND_OPTIONAL_MULTIPLE, "WWN"},
84*fcf3ce44SJohn Forte 	{"remote-port", listRemotePortFunc, "lsp", "p", NULL,
85*fcf3ce44SJohn Forte 		OPERAND_OPTIONAL_MULTIPLE, "WWN"},
86*fcf3ce44SJohn Forte 	{"logical-unit", listLogicalUnitFunc, "v", NULL, NULL,
87*fcf3ce44SJohn Forte 		OPERAND_OPTIONAL_MULTIPLE, "OS Device Path"},
88*fcf3ce44SJohn Forte 	{"lu", listLogicalUnitFunc, "v", NULL, NULL,
89*fcf3ce44SJohn Forte 		OPERAND_OPTIONAL_MULTIPLE, "OS Device Path"},
90*fcf3ce44SJohn Forte 	{NULL, 0, NULL, NULL, NULL, 0, NULL, NULL}
91*fcf3ce44SJohn Forte };
92*fcf3ce44SJohn Forte 
93*fcf3ce44SJohn Forte subCommandProps_t fcadmsubcommands[] = {
94*fcf3ce44SJohn Forte 	{"create-npiv-port",
95*fcf3ce44SJohn Forte 	    npivCreatePortFunc, "pn", NULL, NULL,
96*fcf3ce44SJohn Forte 	    OPERAND_MANDATORY_SINGLE,  "WWN"},
97*fcf3ce44SJohn Forte 	{"delete-npiv-port",
98*fcf3ce44SJohn Forte 	    npivDeletePortFunc, "p", "p", NULL,
99*fcf3ce44SJohn Forte 	    OPERAND_MANDATORY_SINGLE,  "WWN"},
100*fcf3ce44SJohn Forte 	{"hba-port",
101*fcf3ce44SJohn Forte 	    npivListHbaPortFunc, "l", NULL, NULL,
102*fcf3ce44SJohn Forte 	    OPERAND_OPTIONAL_MULTIPLE, "WWN"},
103*fcf3ce44SJohn Forte 	{"remote-port",
104*fcf3ce44SJohn Forte 	    npivListRemotePortFunc, "psl", "p", NULL,
105*fcf3ce44SJohn Forte 	    OPERAND_OPTIONAL_MULTIPLE, "WWN"},
106*fcf3ce44SJohn Forte 	{"create-port-list",
107*fcf3ce44SJohn Forte 	    npivCreatePortListFunc, NULL, NULL, NULL,
108*fcf3ce44SJohn Forte 	    OPERAND_NONE, NULL},
109*fcf3ce44SJohn Forte 	{NULL, 0, NULL, NULL, NULL, 0, NULL, NULL}
110*fcf3ce44SJohn Forte };
111*fcf3ce44SJohn Forte /*
112*fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
113*fcf3ce44SJohn Forte  */
114*fcf3ce44SJohn Forte /*ARGSUSED*/
115*fcf3ce44SJohn Forte static int
116*fcf3ce44SJohn Forte listHbaPortFunc(int objects, char *argv[], cmdOptions_t *options, void *addArgs)
117*fcf3ce44SJohn Forte {
118*fcf3ce44SJohn Forte 	return (fc_util_list_hbaport(objects, argv, options));
119*fcf3ce44SJohn Forte }
120*fcf3ce44SJohn Forte 
121*fcf3ce44SJohn Forte /*
122*fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
123*fcf3ce44SJohn Forte  */
124*fcf3ce44SJohn Forte /*ARGSUSED*/
125*fcf3ce44SJohn Forte static int
126*fcf3ce44SJohn Forte listRemotePortFunc(int objects, char *argv[], cmdOptions_t *options,
127*fcf3ce44SJohn Forte     void *addArgs)
128*fcf3ce44SJohn Forte {
129*fcf3ce44SJohn Forte 	return (fc_util_list_remoteport(objects, argv, options));
130*fcf3ce44SJohn Forte }
131*fcf3ce44SJohn Forte 
132*fcf3ce44SJohn Forte /*
133*fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
134*fcf3ce44SJohn Forte  */
135*fcf3ce44SJohn Forte /*ARGSUSED*/
136*fcf3ce44SJohn Forte static int
137*fcf3ce44SJohn Forte listLogicalUnitFunc(int objects, char *argv[], cmdOptions_t *options,
138*fcf3ce44SJohn Forte     void *addArgs)
139*fcf3ce44SJohn Forte {
140*fcf3ce44SJohn Forte 	return (fc_util_list_logicalunit(objects, argv, options));
141*fcf3ce44SJohn Forte }
142*fcf3ce44SJohn Forte 
143*fcf3ce44SJohn Forte /*
144*fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
145*fcf3ce44SJohn Forte  */
146*fcf3ce44SJohn Forte /*ARGSUSED*/
147*fcf3ce44SJohn Forte static int
148*fcf3ce44SJohn Forte npivCreatePortFunc(int objects, char *argv[],
149*fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
150*fcf3ce44SJohn Forte 	return (fc_util_create_npivport(objects, argv, options));
151*fcf3ce44SJohn Forte }
152*fcf3ce44SJohn Forte 
153*fcf3ce44SJohn Forte static int
154*fcf3ce44SJohn Forte npivCreatePortListFunc(int objects, char *argv[],
155*fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
156*fcf3ce44SJohn Forte 	if ((objects == 0) && addArgs && options && argv) {
157*fcf3ce44SJohn Forte 		objects = 1;
158*fcf3ce44SJohn Forte 	}
159*fcf3ce44SJohn Forte 	return (fc_util_create_portlist());
160*fcf3ce44SJohn Forte }
161*fcf3ce44SJohn Forte 
162*fcf3ce44SJohn Forte /*
163*fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
164*fcf3ce44SJohn Forte  */
165*fcf3ce44SJohn Forte /*ARGSUSED*/
166*fcf3ce44SJohn Forte static int
167*fcf3ce44SJohn Forte npivDeletePortFunc(int objects, char *argv[],
168*fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
169*fcf3ce44SJohn Forte 	return (fc_util_delete_npivport(objects, argv, options));
170*fcf3ce44SJohn Forte }
171*fcf3ce44SJohn Forte 
172*fcf3ce44SJohn Forte /*
173*fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
174*fcf3ce44SJohn Forte  */
175*fcf3ce44SJohn Forte /*ARGSUSED*/
176*fcf3ce44SJohn Forte static int
177*fcf3ce44SJohn Forte npivListHbaPortFunc(int objects, char *argv[],
178*fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
179*fcf3ce44SJohn Forte 	return (fc_util_list_hbaport(objects, argv, options));
180*fcf3ce44SJohn Forte }
181*fcf3ce44SJohn Forte 
182*fcf3ce44SJohn Forte /*
183*fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
184*fcf3ce44SJohn Forte  */
185*fcf3ce44SJohn Forte /*ARGSUSED*/
186*fcf3ce44SJohn Forte static int
187*fcf3ce44SJohn Forte npivListRemotePortFunc(int objects, char *argv[],
188*fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
189*fcf3ce44SJohn Forte 	return (fc_util_list_remoteport(objects, argv, options));
190*fcf3ce44SJohn Forte }
191*fcf3ce44SJohn Forte 
192*fcf3ce44SJohn Forte /*
193*fcf3ce44SJohn Forte  * input:
194*fcf3ce44SJohn Forte  *  execFullName - exec name of program (argv[0])
195*fcf3ce44SJohn Forte  *
196*fcf3ce44SJohn Forte  * Returns:
197*fcf3ce44SJohn Forte  *  command name portion of execFullName
198*fcf3ce44SJohn Forte  */
199*fcf3ce44SJohn Forte static char *
200*fcf3ce44SJohn Forte getExecBasename(char *execFullname)
201*fcf3ce44SJohn Forte {
202*fcf3ce44SJohn Forte 	char *lastSlash, *execBasename;
203*fcf3ce44SJohn Forte 
204*fcf3ce44SJohn Forte 	/* guard against '/' at end of command invocation */
205*fcf3ce44SJohn Forte 	for (;;) {
206*fcf3ce44SJohn Forte 		lastSlash = strrchr(execFullname, '/');
207*fcf3ce44SJohn Forte 		if (lastSlash == NULL) {
208*fcf3ce44SJohn Forte 			execBasename = execFullname;
209*fcf3ce44SJohn Forte 			break;
210*fcf3ce44SJohn Forte 		} else {
211*fcf3ce44SJohn Forte 			execBasename = lastSlash + 1;
212*fcf3ce44SJohn Forte 			if (*execBasename == '\0') {
213*fcf3ce44SJohn Forte 				*lastSlash = '\0';
214*fcf3ce44SJohn Forte 				continue;
215*fcf3ce44SJohn Forte 			}
216*fcf3ce44SJohn Forte 			break;
217*fcf3ce44SJohn Forte 		}
218*fcf3ce44SJohn Forte 	}
219*fcf3ce44SJohn Forte 	return (execBasename);
220*fcf3ce44SJohn Forte }
221*fcf3ce44SJohn Forte 
222*fcf3ce44SJohn Forte /*
223*fcf3ce44SJohn Forte  * main calls a parser that checks syntax of the input command against
224*fcf3ce44SJohn Forte  * various rules tables.
225*fcf3ce44SJohn Forte  *
226*fcf3ce44SJohn Forte  * The parser provides usage feedback based upon same tables by calling
227*fcf3ce44SJohn Forte  * two usage functions, usage and subUsage, handling command and subcommand
228*fcf3ce44SJohn Forte  * usage respectively.
229*fcf3ce44SJohn Forte  *
230*fcf3ce44SJohn Forte  * The parser handles all printing of usage syntactical errors
231*fcf3ce44SJohn Forte  *
232*fcf3ce44SJohn Forte  * When syntax is successfully validated, the parser calls the associated
233*fcf3ce44SJohn Forte  * function using the subcommands table functions.
234*fcf3ce44SJohn Forte  *
235*fcf3ce44SJohn Forte  * Syntax is as follows:
236*fcf3ce44SJohn Forte  *	command subcommand [options] resource-type [<object>]
237*fcf3ce44SJohn Forte  *
238*fcf3ce44SJohn Forte  * The return value from the function is placed in funcRet
239*fcf3ce44SJohn Forte  */
240*fcf3ce44SJohn Forte int
241*fcf3ce44SJohn Forte main(int argc, char *argv[])
242*fcf3ce44SJohn Forte {
243*fcf3ce44SJohn Forte 	synTables_t synTables;
244*fcf3ce44SJohn Forte 	char versionString[VERSION_STRING_MAX_LEN];
245*fcf3ce44SJohn Forte 	int ret;
246*fcf3ce44SJohn Forte 	int funcRet;
247*fcf3ce44SJohn Forte 	void *subcommandArgs = NULL;
248*fcf3ce44SJohn Forte 
249*fcf3ce44SJohn Forte 	/* set global command name */
250*fcf3ce44SJohn Forte 	cmdName = getExecBasename(argv[0]);
251*fcf3ce44SJohn Forte 
252*fcf3ce44SJohn Forte 	sprintf(versionString, "%s.%s",
253*fcf3ce44SJohn Forte 	    VERSION_STRING_MAJOR, VERSION_STRING_MINOR);
254*fcf3ce44SJohn Forte 	synTables.versionString = versionString;
255*fcf3ce44SJohn Forte 	if (strcmp(cmdName, "fcadm") == 0) {
256*fcf3ce44SJohn Forte 		synTables.longOptionTbl = &fcadmlongOptions[0];
257*fcf3ce44SJohn Forte 		synTables.subCommandPropsTbl = &fcadmsubcommands[0];
258*fcf3ce44SJohn Forte 	} else {
259*fcf3ce44SJohn Forte 		synTables.longOptionTbl = &fcinfolongOptions[0];
260*fcf3ce44SJohn Forte 		synTables.subCommandPropsTbl = &fcinfosubcommands[0];
261*fcf3ce44SJohn Forte 	}
262*fcf3ce44SJohn Forte 
263*fcf3ce44SJohn Forte 	/* call the CLI parser */
264*fcf3ce44SJohn Forte 	ret = cmdParse(argc, argv, synTables, subcommandArgs, &funcRet);
265*fcf3ce44SJohn Forte 	if (ret == 1) {
266*fcf3ce44SJohn Forte 		fprintf(stdout, "%s %s(1M)\n",
267*fcf3ce44SJohn Forte 		    gettext("For more information, please see"), cmdName);
268*fcf3ce44SJohn Forte 		return (1);
269*fcf3ce44SJohn Forte 	} else if (ret == -1) {
270*fcf3ce44SJohn Forte 		perror(cmdName);
271*fcf3ce44SJohn Forte 		return (1);
272*fcf3ce44SJohn Forte 	}
273*fcf3ce44SJohn Forte 
274*fcf3ce44SJohn Forte 	if (funcRet != 0) {
275*fcf3ce44SJohn Forte 		return (1);
276*fcf3ce44SJohn Forte 	}
277*fcf3ce44SJohn Forte 	return (0);
278*fcf3ce44SJohn Forte }
279