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