xref: /onnv-gate/usr/src/cmd/sasinfo/sasinfo.c (revision 10652:9d0aff74d6fd)
1*10652SHyon.Kim@Sun.COM /*
2*10652SHyon.Kim@Sun.COM  * CDDL HEADER START
3*10652SHyon.Kim@Sun.COM  *
4*10652SHyon.Kim@Sun.COM  * The contents of this file are subject to the terms of the
5*10652SHyon.Kim@Sun.COM  * Common Development and Distribution License (the "License").
6*10652SHyon.Kim@Sun.COM  * You may not use this file except in compliance with the License.
7*10652SHyon.Kim@Sun.COM  *
8*10652SHyon.Kim@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10652SHyon.Kim@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*10652SHyon.Kim@Sun.COM  * See the License for the specific language governing permissions
11*10652SHyon.Kim@Sun.COM  * and limitations under the License.
12*10652SHyon.Kim@Sun.COM  *
13*10652SHyon.Kim@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*10652SHyon.Kim@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10652SHyon.Kim@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*10652SHyon.Kim@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*10652SHyon.Kim@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*10652SHyon.Kim@Sun.COM  *
19*10652SHyon.Kim@Sun.COM  * CDDL HEADER END
20*10652SHyon.Kim@Sun.COM  */
21*10652SHyon.Kim@Sun.COM /*
22*10652SHyon.Kim@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23*10652SHyon.Kim@Sun.COM  * Use is subject to license terms.
24*10652SHyon.Kim@Sun.COM  */
25*10652SHyon.Kim@Sun.COM 
26*10652SHyon.Kim@Sun.COM #include <errno.h>
27*10652SHyon.Kim@Sun.COM #include <zone.h>
28*10652SHyon.Kim@Sun.COM #include <sasinfo.h>
29*10652SHyon.Kim@Sun.COM 
30*10652SHyon.Kim@Sun.COM #define	VERSION_STRING_MAX_LEN	10
31*10652SHyon.Kim@Sun.COM /*
32*10652SHyon.Kim@Sun.COM  * Version number:
33*10652SHyon.Kim@Sun.COM  *  MAJOR - This should only change when there is an incompatible change made
34*10652SHyon.Kim@Sun.COM  *  to the interfaces or the output.
35*10652SHyon.Kim@Sun.COM  *
36*10652SHyon.Kim@Sun.COM  *  MINOR - This should change whenever there is a new command or new feature
37*10652SHyon.Kim@Sun.COM  *  with no incompatible change.
38*10652SHyon.Kim@Sun.COM  */
39*10652SHyon.Kim@Sun.COM #define	VERSION_STRING_MAJOR	    "1"
40*10652SHyon.Kim@Sun.COM #define	VERSION_STRING_MINOR	    "0"
41*10652SHyon.Kim@Sun.COM 
42*10652SHyon.Kim@Sun.COM /* globals */
43*10652SHyon.Kim@Sun.COM static char *cmdName;
44*10652SHyon.Kim@Sun.COM 
45*10652SHyon.Kim@Sun.COM /* forward declarations */
46*10652SHyon.Kim@Sun.COM static int listHbaFunc(int, char **, cmdOptions_t *, void *);
47*10652SHyon.Kim@Sun.COM static int listHbaPortFunc(int, char **, cmdOptions_t *, void *);
48*10652SHyon.Kim@Sun.COM static int listExpanderFunc(int, char **, cmdOptions_t *, void *);
49*10652SHyon.Kim@Sun.COM static int listTargetPortFunc(int, char **, cmdOptions_t *, void *);
50*10652SHyon.Kim@Sun.COM static int listLogicalUnitFunc(int, char **, cmdOptions_t *, void *);
51*10652SHyon.Kim@Sun.COM static char *getExecBasename(char *);
52*10652SHyon.Kim@Sun.COM 
53*10652SHyon.Kim@Sun.COM /*
54*10652SHyon.Kim@Sun.COM  * Add new options here
55*10652SHyon.Kim@Sun.COM  *
56*10652SHyon.Kim@Sun.COM  * Optional option-arguments are not allowed by CLIP
57*10652SHyon.Kim@Sun.COM  */
58*10652SHyon.Kim@Sun.COM optionTbl_t sasinfolongOptions[] = {
59*10652SHyon.Kim@Sun.COM 	{"hba", required_argument,	'a', "HBA Name"},
60*10652SHyon.Kim@Sun.COM 	{"hba-port", required_argument,	'p', "HBA Port Name"},
61*10652SHyon.Kim@Sun.COM 	{"phy", no_argument,		'y', NULL},
62*10652SHyon.Kim@Sun.COM 	{"phy-linkstat", no_argument,	'l', NULL},
63*10652SHyon.Kim@Sun.COM 	{"scsi-target", no_argument,	's', NULL},
64*10652SHyon.Kim@Sun.COM 	{"verbose", no_argument,	'v', NULL},
65*10652SHyon.Kim@Sun.COM 	{"target", no_argument,	't', NULL},
66*10652SHyon.Kim@Sun.COM 	{NULL, 0, 0}
67*10652SHyon.Kim@Sun.COM };
68*10652SHyon.Kim@Sun.COM 
69*10652SHyon.Kim@Sun.COM /*
70*10652SHyon.Kim@Sun.COM  * Add new subcommands here
71*10652SHyon.Kim@Sun.COM  */
72*10652SHyon.Kim@Sun.COM subCommandProps_t sasinfosubcommands[] = {
73*10652SHyon.Kim@Sun.COM 	{"hba", listHbaFunc, "v", NULL, NULL,
74*10652SHyon.Kim@Sun.COM 		OPERAND_OPTIONAL_MULTIPLE, "HBA Name"},
75*10652SHyon.Kim@Sun.COM 	{"hba-port", listHbaPortFunc, "ylva", NULL, NULL,
76*10652SHyon.Kim@Sun.COM 		OPERAND_OPTIONAL_MULTIPLE, "HBA Port Name"},
77*10652SHyon.Kim@Sun.COM 	{"expander", listExpanderFunc, "ptv", NULL, NULL,
78*10652SHyon.Kim@Sun.COM 		OPERAND_OPTIONAL_MULTIPLE, "Expander Device SAS Address"},
79*10652SHyon.Kim@Sun.COM 	{"target-port", listTargetPortFunc, "sv", NULL, "sv",
80*10652SHyon.Kim@Sun.COM 		OPERAND_OPTIONAL_MULTIPLE, "Target Port SAS Address"},
81*10652SHyon.Kim@Sun.COM 	{"logical-unit", listLogicalUnitFunc, "v", NULL, NULL,
82*10652SHyon.Kim@Sun.COM 		OPERAND_OPTIONAL_MULTIPLE, "OS Device Name"},
83*10652SHyon.Kim@Sun.COM 	{"lu", listLogicalUnitFunc, "v", NULL, NULL,
84*10652SHyon.Kim@Sun.COM 		OPERAND_OPTIONAL_MULTIPLE, "OS Device Name"},
85*10652SHyon.Kim@Sun.COM 	{NULL, 0, NULL, NULL, NULL, 0, NULL, NULL}
86*10652SHyon.Kim@Sun.COM };
87*10652SHyon.Kim@Sun.COM 
88*10652SHyon.Kim@Sun.COM /*
89*10652SHyon.Kim@Sun.COM  * Pass in options/arguments, rest of arguments
90*10652SHyon.Kim@Sun.COM  */
91*10652SHyon.Kim@Sun.COM /*ARGSUSED*/
92*10652SHyon.Kim@Sun.COM static int
listHbaFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)93*10652SHyon.Kim@Sun.COM listHbaFunc(int objects, char *argv[], cmdOptions_t *options, void *addArgs)
94*10652SHyon.Kim@Sun.COM {
95*10652SHyon.Kim@Sun.COM 	return (sas_util_list_hba(objects, argv, options));
96*10652SHyon.Kim@Sun.COM }
97*10652SHyon.Kim@Sun.COM 
98*10652SHyon.Kim@Sun.COM /*ARGSUSED*/
99*10652SHyon.Kim@Sun.COM static int
listHbaPortFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)100*10652SHyon.Kim@Sun.COM listHbaPortFunc(int objects, char *argv[], cmdOptions_t *options, void *addArgs)
101*10652SHyon.Kim@Sun.COM {
102*10652SHyon.Kim@Sun.COM 	return (sas_util_list_hbaport(objects, argv, options));
103*10652SHyon.Kim@Sun.COM }
104*10652SHyon.Kim@Sun.COM 
105*10652SHyon.Kim@Sun.COM /*
106*10652SHyon.Kim@Sun.COM  * Pass in options/arguments, rest of arguments
107*10652SHyon.Kim@Sun.COM  */
108*10652SHyon.Kim@Sun.COM /*ARGSUSED*/
109*10652SHyon.Kim@Sun.COM static int
listExpanderFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)110*10652SHyon.Kim@Sun.COM listExpanderFunc(int objects, char *argv[], cmdOptions_t *options,
111*10652SHyon.Kim@Sun.COM     void *addArgs)
112*10652SHyon.Kim@Sun.COM {
113*10652SHyon.Kim@Sun.COM 	return (sas_util_list_expander(objects, argv, options));
114*10652SHyon.Kim@Sun.COM }
115*10652SHyon.Kim@Sun.COM 
116*10652SHyon.Kim@Sun.COM /*ARGSUSED*/
117*10652SHyon.Kim@Sun.COM static int
listTargetPortFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)118*10652SHyon.Kim@Sun.COM listTargetPortFunc(int objects, char *argv[], cmdOptions_t *options,
119*10652SHyon.Kim@Sun.COM     void *addArgs)
120*10652SHyon.Kim@Sun.COM {
121*10652SHyon.Kim@Sun.COM 	return (sas_util_list_targetport(objects, argv, options));
122*10652SHyon.Kim@Sun.COM }
123*10652SHyon.Kim@Sun.COM 
124*10652SHyon.Kim@Sun.COM /*
125*10652SHyon.Kim@Sun.COM  * Pass in options/arguments, rest of arguments
126*10652SHyon.Kim@Sun.COM  */
127*10652SHyon.Kim@Sun.COM /*ARGSUSED*/
128*10652SHyon.Kim@Sun.COM static int
listLogicalUnitFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)129*10652SHyon.Kim@Sun.COM listLogicalUnitFunc(int objects, char *argv[], cmdOptions_t *options,
130*10652SHyon.Kim@Sun.COM     void *addArgs)
131*10652SHyon.Kim@Sun.COM {
132*10652SHyon.Kim@Sun.COM 	return (sas_util_list_logicalunit(objects, argv, options));
133*10652SHyon.Kim@Sun.COM }
134*10652SHyon.Kim@Sun.COM 
135*10652SHyon.Kim@Sun.COM /*
136*10652SHyon.Kim@Sun.COM  * input:
137*10652SHyon.Kim@Sun.COM  *  execFullName - exec name of program (argv[0])
138*10652SHyon.Kim@Sun.COM  *
139*10652SHyon.Kim@Sun.COM  * Returns:
140*10652SHyon.Kim@Sun.COM  *  command name portion of execFullName
141*10652SHyon.Kim@Sun.COM  */
142*10652SHyon.Kim@Sun.COM static char *
getExecBasename(char * execFullname)143*10652SHyon.Kim@Sun.COM getExecBasename(char *execFullname)
144*10652SHyon.Kim@Sun.COM {
145*10652SHyon.Kim@Sun.COM 	char *lastSlash, *execBasename;
146*10652SHyon.Kim@Sun.COM 
147*10652SHyon.Kim@Sun.COM 	/* guard against '/' at end of command invocation */
148*10652SHyon.Kim@Sun.COM 	for (;;) {
149*10652SHyon.Kim@Sun.COM 		lastSlash = strrchr(execFullname, '/');
150*10652SHyon.Kim@Sun.COM 		if (lastSlash == NULL) {
151*10652SHyon.Kim@Sun.COM 			execBasename = execFullname;
152*10652SHyon.Kim@Sun.COM 			break;
153*10652SHyon.Kim@Sun.COM 		} else {
154*10652SHyon.Kim@Sun.COM 			execBasename = lastSlash + 1;
155*10652SHyon.Kim@Sun.COM 			if (*execBasename == '\0') {
156*10652SHyon.Kim@Sun.COM 				*lastSlash = '\0';
157*10652SHyon.Kim@Sun.COM 				continue;
158*10652SHyon.Kim@Sun.COM 			}
159*10652SHyon.Kim@Sun.COM 			break;
160*10652SHyon.Kim@Sun.COM 		}
161*10652SHyon.Kim@Sun.COM 	}
162*10652SHyon.Kim@Sun.COM 	return (execBasename);
163*10652SHyon.Kim@Sun.COM }
164*10652SHyon.Kim@Sun.COM 
165*10652SHyon.Kim@Sun.COM /*
166*10652SHyon.Kim@Sun.COM  * main calls a parser that checks syntax of the input command against
167*10652SHyon.Kim@Sun.COM  * various rules tables.
168*10652SHyon.Kim@Sun.COM  *
169*10652SHyon.Kim@Sun.COM  * The return value from the function is placed in funcRet
170*10652SHyon.Kim@Sun.COM  */
171*10652SHyon.Kim@Sun.COM int
main(int argc,char * argv[])172*10652SHyon.Kim@Sun.COM main(int argc, char *argv[])
173*10652SHyon.Kim@Sun.COM {
174*10652SHyon.Kim@Sun.COM 	synTables_t synTables;
175*10652SHyon.Kim@Sun.COM 	char versionString[VERSION_STRING_MAX_LEN];
176*10652SHyon.Kim@Sun.COM 	int ret;
177*10652SHyon.Kim@Sun.COM 	int funcRet;
178*10652SHyon.Kim@Sun.COM 	void *subcommandArgs = NULL;
179*10652SHyon.Kim@Sun.COM 
180*10652SHyon.Kim@Sun.COM 	/* to support locale */
181*10652SHyon.Kim@Sun.COM 	(void) setlocale(LC_ALL, "");
182*10652SHyon.Kim@Sun.COM #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
183*10652SHyon.Kim@Sun.COM #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
184*10652SHyon.Kim@Sun.COM #endif
185*10652SHyon.Kim@Sun.COM 	(void) textdomain(TEXT_DOMAIN);
186*10652SHyon.Kim@Sun.COM 
187*10652SHyon.Kim@Sun.COM 	/* set global command name */
188*10652SHyon.Kim@Sun.COM 	cmdName = getExecBasename(argv[0]);
189*10652SHyon.Kim@Sun.COM 
190*10652SHyon.Kim@Sun.COM 	/* check if is global zone */
191*10652SHyon.Kim@Sun.COM 	if (getzoneid() != GLOBAL_ZONEID) {
192*10652SHyon.Kim@Sun.COM 		(void *) fprintf(stdout, "%s %s\n",
193*10652SHyon.Kim@Sun.COM 		    cmdName, gettext("does not support non-global zone."));
194*10652SHyon.Kim@Sun.COM 		return (1);
195*10652SHyon.Kim@Sun.COM 	}
196*10652SHyon.Kim@Sun.COM 
197*10652SHyon.Kim@Sun.COM 	(void *) snprintf(versionString, sizeof (versionString), "%s.%s",
198*10652SHyon.Kim@Sun.COM 	    VERSION_STRING_MAJOR, VERSION_STRING_MINOR);
199*10652SHyon.Kim@Sun.COM 	synTables.versionString = versionString;
200*10652SHyon.Kim@Sun.COM 
201*10652SHyon.Kim@Sun.COM 	synTables.longOptionTbl = &sasinfolongOptions[0];
202*10652SHyon.Kim@Sun.COM 	synTables.subCommandPropsTbl = &sasinfosubcommands[0];
203*10652SHyon.Kim@Sun.COM 
204*10652SHyon.Kim@Sun.COM 	/* call the CLI parser */
205*10652SHyon.Kim@Sun.COM 	ret = cmdParse(argc, argv, synTables, subcommandArgs, &funcRet);
206*10652SHyon.Kim@Sun.COM 	if (ret == 1) {
207*10652SHyon.Kim@Sun.COM 		(void *) fprintf(stdout, "%s %s(1M)\n",
208*10652SHyon.Kim@Sun.COM 		    gettext("For more information, please see"), cmdName);
209*10652SHyon.Kim@Sun.COM 		return (1);
210*10652SHyon.Kim@Sun.COM 	} else if (ret == -1) {
211*10652SHyon.Kim@Sun.COM 		(void *) fprintf(stderr, "%s %s\n",
212*10652SHyon.Kim@Sun.COM 		    cmdName, strerror(errno));
213*10652SHyon.Kim@Sun.COM 		return (1);
214*10652SHyon.Kim@Sun.COM 	}
215*10652SHyon.Kim@Sun.COM 
216*10652SHyon.Kim@Sun.COM 	if (funcRet != 0) {
217*10652SHyon.Kim@Sun.COM 		return (1);
218*10652SHyon.Kim@Sun.COM 	}
219*10652SHyon.Kim@Sun.COM 	return (0);
220*10652SHyon.Kim@Sun.COM }
221