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