xref: /illumos-gate/usr/src/cmd/sasinfo/sasinfo.c (revision ddb365bfc9e868ad24ccdcb0dc91af18b10df082)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <errno.h>
27 #include <zone.h>
28 #include <sasinfo.h>
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 /* globals */
43 static char *cmdName;
44 
45 /* forward declarations */
46 static int listHbaFunc(int, char **, cmdOptions_t *, void *);
47 static int listHbaPortFunc(int, char **, cmdOptions_t *, void *);
48 static int listExpanderFunc(int, char **, cmdOptions_t *, void *);
49 static int listTargetPortFunc(int, char **, cmdOptions_t *, void *);
50 static int listLogicalUnitFunc(int, char **, cmdOptions_t *, void *);
51 static char *getExecBasename(char *);
52 
53 /*
54  * Add new options here
55  *
56  * Optional option-arguments are not allowed by CLIP
57  */
58 optionTbl_t sasinfolongOptions[] = {
59 	{"hba", required_argument,	'a', "HBA Name"},
60 	{"hba-port", required_argument,	'p', "HBA Port Name"},
61 	{"phy", no_argument,		'y', NULL},
62 	{"phy-linkstat", no_argument,	'l', NULL},
63 	{"scsi-target", no_argument,	's', NULL},
64 	{"verbose", no_argument,	'v', NULL},
65 	{"target", no_argument,	't', NULL},
66 	{NULL, 0, 0}
67 };
68 
69 /*
70  * Add new subcommands here
71  */
72 subCommandProps_t sasinfosubcommands[] = {
73 	{"hba", listHbaFunc, "v", NULL, NULL,
74 		OPERAND_OPTIONAL_MULTIPLE, "HBA Name"},
75 	{"hba-port", listHbaPortFunc, "ylva", NULL, NULL,
76 		OPERAND_OPTIONAL_MULTIPLE, "HBA Port Name"},
77 	{"expander", listExpanderFunc, "ptv", NULL, NULL,
78 		OPERAND_OPTIONAL_MULTIPLE, "Expander Device SAS Address"},
79 	{"target-port", listTargetPortFunc, "sv", NULL, "sv",
80 		OPERAND_OPTIONAL_MULTIPLE, "Target Port SAS Address"},
81 	{"logical-unit", listLogicalUnitFunc, "v", NULL, NULL,
82 		OPERAND_OPTIONAL_MULTIPLE, "OS Device Name"},
83 	{"lu", listLogicalUnitFunc, "v", NULL, NULL,
84 		OPERAND_OPTIONAL_MULTIPLE, "OS Device Name"},
85 	{NULL, 0, NULL, NULL, NULL, 0, NULL, NULL}
86 };
87 
88 /*
89  * Pass in options/arguments, rest of arguments
90  */
91 /*ARGSUSED*/
92 static int
93 listHbaFunc(int objects, char *argv[], cmdOptions_t *options, void *addArgs)
94 {
95 	return (sas_util_list_hba(objects, argv, options));
96 }
97 
98 /*ARGSUSED*/
99 static int
100 listHbaPortFunc(int objects, char *argv[], cmdOptions_t *options, void *addArgs)
101 {
102 	return (sas_util_list_hbaport(objects, argv, options));
103 }
104 
105 /*
106  * Pass in options/arguments, rest of arguments
107  */
108 /*ARGSUSED*/
109 static int
110 listExpanderFunc(int objects, char *argv[], cmdOptions_t *options,
111     void *addArgs)
112 {
113 	return (sas_util_list_expander(objects, argv, options));
114 }
115 
116 /*ARGSUSED*/
117 static int
118 listTargetPortFunc(int objects, char *argv[], cmdOptions_t *options,
119     void *addArgs)
120 {
121 	return (sas_util_list_targetport(objects, argv, options));
122 }
123 
124 /*
125  * Pass in options/arguments, rest of arguments
126  */
127 /*ARGSUSED*/
128 static int
129 listLogicalUnitFunc(int objects, char *argv[], cmdOptions_t *options,
130     void *addArgs)
131 {
132 	return (sas_util_list_logicalunit(objects, argv, options));
133 }
134 
135 /*
136  * input:
137  *  execFullName - exec name of program (argv[0])
138  *
139  * Returns:
140  *  command name portion of execFullName
141  */
142 static char *
143 getExecBasename(char *execFullname)
144 {
145 	char *lastSlash, *execBasename;
146 
147 	/* guard against '/' at end of command invocation */
148 	for (;;) {
149 		lastSlash = strrchr(execFullname, '/');
150 		if (lastSlash == NULL) {
151 			execBasename = execFullname;
152 			break;
153 		} else {
154 			execBasename = lastSlash + 1;
155 			if (*execBasename == '\0') {
156 				*lastSlash = '\0';
157 				continue;
158 			}
159 			break;
160 		}
161 	}
162 	return (execBasename);
163 }
164 
165 /*
166  * main calls a parser that checks syntax of the input command against
167  * various rules tables.
168  *
169  * The return value from the function is placed in funcRet
170  */
171 int
172 main(int argc, char *argv[])
173 {
174 	synTables_t synTables;
175 	char versionString[VERSION_STRING_MAX_LEN];
176 	int ret;
177 	int funcRet;
178 	void *subcommandArgs = NULL;
179 
180 	/* to support locale */
181 	(void) setlocale(LC_ALL, "");
182 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
183 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
184 #endif
185 	(void) textdomain(TEXT_DOMAIN);
186 
187 	/* set global command name */
188 	cmdName = getExecBasename(argv[0]);
189 
190 	/* check if is global zone */
191 	if (getzoneid() != GLOBAL_ZONEID) {
192 		(void) fprintf(stdout, "%s %s\n",
193 		    cmdName, gettext("does not support non-global zone."));
194 		return (1);
195 	}
196 
197 	(void) snprintf(versionString, sizeof (versionString), "%s.%s",
198 	    VERSION_STRING_MAJOR, VERSION_STRING_MINOR);
199 	synTables.versionString = versionString;
200 
201 	synTables.longOptionTbl = &sasinfolongOptions[0];
202 	synTables.subCommandPropsTbl = &sasinfosubcommands[0];
203 
204 	/* call the CLI parser */
205 	ret = cmdParse(argc, argv, synTables, subcommandArgs, &funcRet);
206 	if (ret == 1) {
207 		(void) fprintf(stdout, "%s %s(8)\n",
208 		    gettext("For more information, please see"), cmdName);
209 		return (1);
210 	} else if (ret == -1) {
211 		(void) fprintf(stderr, "%s %s\n", cmdName, strerror(errno));
212 		return (1);
213 	}
214 
215 	if (funcRet != 0) {
216 		return (1);
217 	}
218 	return (0);
219 }
220