xref: /titanic_50/usr/src/cmd/iscsitsvc/iscsitsvc.c (revision a6d42e7d71324c5193c3b94d57d96ba2925d52e1)
1*a6d42e7dSPeter Dunlap /*
2*a6d42e7dSPeter Dunlap  * CDDL HEADER START
3*a6d42e7dSPeter Dunlap  *
4*a6d42e7dSPeter Dunlap  * The contents of this file are subject to the terms of the
5*a6d42e7dSPeter Dunlap  * Common Development and Distribution License (the "License").
6*a6d42e7dSPeter Dunlap  * You may not use this file except in compliance with the License.
7*a6d42e7dSPeter Dunlap  *
8*a6d42e7dSPeter Dunlap  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*a6d42e7dSPeter Dunlap  * or http://www.opensolaris.org/os/licensing.
10*a6d42e7dSPeter Dunlap  * See the License for the specific language governing permissions
11*a6d42e7dSPeter Dunlap  * and limitations under the License.
12*a6d42e7dSPeter Dunlap  *
13*a6d42e7dSPeter Dunlap  * When distributing Covered Code, include this CDDL HEADER in each
14*a6d42e7dSPeter Dunlap  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*a6d42e7dSPeter Dunlap  * If applicable, add the following below this CDDL HEADER, with the
16*a6d42e7dSPeter Dunlap  * fields enclosed by brackets "[]" replaced with your own identifying
17*a6d42e7dSPeter Dunlap  * information: Portions Copyright [yyyy] [name of copyright owner]
18*a6d42e7dSPeter Dunlap  *
19*a6d42e7dSPeter Dunlap  * CDDL HEADER END
20*a6d42e7dSPeter Dunlap  */
21*a6d42e7dSPeter Dunlap /*
22*a6d42e7dSPeter Dunlap  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*a6d42e7dSPeter Dunlap  * Use is subject to license terms.
24*a6d42e7dSPeter Dunlap  */
25*a6d42e7dSPeter Dunlap 
26*a6d42e7dSPeter Dunlap #include <stdlib.h>
27*a6d42e7dSPeter Dunlap #include <stdio.h>
28*a6d42e7dSPeter Dunlap #include <strings.h>
29*a6d42e7dSPeter Dunlap #include <sys/types.h>
30*a6d42e7dSPeter Dunlap #include <unistd.h>
31*a6d42e7dSPeter Dunlap #include <stropts.h>
32*a6d42e7dSPeter Dunlap #include <libintl.h>
33*a6d42e7dSPeter Dunlap #include <errno.h>
34*a6d42e7dSPeter Dunlap #include <time.h>
35*a6d42e7dSPeter Dunlap #include <string.h>
36*a6d42e7dSPeter Dunlap #include <assert.h>
37*a6d42e7dSPeter Dunlap #include <getopt.h>
38*a6d42e7dSPeter Dunlap #include <cmdparse.h>
39*a6d42e7dSPeter Dunlap #include <signal.h>
40*a6d42e7dSPeter Dunlap #include <pthread.h>
41*a6d42e7dSPeter Dunlap #include <fcntl.h>
42*a6d42e7dSPeter Dunlap #include <locale.h>
43*a6d42e7dSPeter Dunlap #include <sys/systeminfo.h>
44*a6d42e7dSPeter Dunlap 
45*a6d42e7dSPeter Dunlap #include <libiscsit.h>
46*a6d42e7dSPeter Dunlap #include <sys/iscsit/iscsit_common.h>
47*a6d42e7dSPeter Dunlap 
48*a6d42e7dSPeter Dunlap static int it_enable(int, char **, cmdOptions_t *, void *);
49*a6d42e7dSPeter Dunlap static int it_disable(int, char **, cmdOptions_t *, void *);
50*a6d42e7dSPeter Dunlap 
51*a6d42e7dSPeter Dunlap /*
52*a6d42e7dSPeter Dunlap  *  MAJOR - This should only change when there is an incompatible change made
53*a6d42e7dSPeter Dunlap  *  to the interfaces or the output.
54*a6d42e7dSPeter Dunlap  *
55*a6d42e7dSPeter Dunlap  *  MINOR - This should change whenever there is a new command or new feature
56*a6d42e7dSPeter Dunlap  *  with no incompatible change.
57*a6d42e7dSPeter Dunlap  */
58*a6d42e7dSPeter Dunlap #define	VERSION_STRING_MAJOR	    "1"
59*a6d42e7dSPeter Dunlap #define	VERSION_STRING_MINOR	    "0"
60*a6d42e7dSPeter Dunlap #define	VERSION_STRING_MAX_LEN	    10
61*a6d42e7dSPeter Dunlap 
62*a6d42e7dSPeter Dunlap /* 10 ms sleep in nanoseconds */
63*a6d42e7dSPeter Dunlap #define	TEN_MS_NANOSLEEP  10000000
64*a6d42e7dSPeter Dunlap 
65*a6d42e7dSPeter Dunlap /* tables set up based on cmdparse instructions */
66*a6d42e7dSPeter Dunlap 
67*a6d42e7dSPeter Dunlap /* add new options here */
68*a6d42e7dSPeter Dunlap optionTbl_t longOptions[] = {
69*a6d42e7dSPeter Dunlap 	{NULL, 0, 0, 0}
70*a6d42e7dSPeter Dunlap };
71*a6d42e7dSPeter Dunlap 
72*a6d42e7dSPeter Dunlap /*
73*a6d42e7dSPeter Dunlap  * Add new subcommands here
74*a6d42e7dSPeter Dunlap  */
75*a6d42e7dSPeter Dunlap subCommandProps_t subcommands[] = {
76*a6d42e7dSPeter Dunlap 	{"start", it_enable, NULL, NULL, NULL, OPERAND_NONE, NULL},
77*a6d42e7dSPeter Dunlap 	{"stop", it_disable, NULL, NULL, NULL, OPERAND_NONE, NULL},
78*a6d42e7dSPeter Dunlap 	{NULL, 0, NULL, NULL, 0, NULL, 0, NULL}
79*a6d42e7dSPeter Dunlap };
80*a6d42e7dSPeter Dunlap 
81*a6d42e7dSPeter Dunlap /* globals */
82*a6d42e7dSPeter Dunlap char *cmdName;
83*a6d42e7dSPeter Dunlap 
84*a6d42e7dSPeter Dunlap /*
85*a6d42e7dSPeter Dunlap  * Opens the iSCSI Target Node
86*a6d42e7dSPeter Dunlap  *
87*a6d42e7dSPeter Dunlap  * fd - Return the iscsit file descriptor
88*a6d42e7dSPeter Dunlap  */
89*a6d42e7dSPeter Dunlap static int
it_open(int * fd)90*a6d42e7dSPeter Dunlap it_open(int *fd)
91*a6d42e7dSPeter Dunlap {
92*a6d42e7dSPeter Dunlap 
93*a6d42e7dSPeter Dunlap 	int ret = ITADM_SUCCESS;
94*a6d42e7dSPeter Dunlap 
95*a6d42e7dSPeter Dunlap 	*fd = open(ISCSIT_NODE, O_RDONLY);
96*a6d42e7dSPeter Dunlap 	if (*fd < 0) {
97*a6d42e7dSPeter Dunlap 		if (errno == EPERM) {
98*a6d42e7dSPeter Dunlap 			(void) fprintf(stdout, "open failed: EPERM");
99*a6d42e7dSPeter Dunlap 			ret = ITADM_PERM;
100*a6d42e7dSPeter Dunlap 		} else {
101*a6d42e7dSPeter Dunlap 			(void) fprintf(stdout, "open failed: INVALID");
102*a6d42e7dSPeter Dunlap 			ret = ITADM_INVALID;
103*a6d42e7dSPeter Dunlap 		}
104*a6d42e7dSPeter Dunlap 	}
105*a6d42e7dSPeter Dunlap 
106*a6d42e7dSPeter Dunlap 	return (ret);
107*a6d42e7dSPeter Dunlap }
108*a6d42e7dSPeter Dunlap 
109*a6d42e7dSPeter Dunlap /*
110*a6d42e7dSPeter Dunlap  * Enables the iSCSI Target
111*a6d42e7dSPeter Dunlap  */
112*a6d42e7dSPeter Dunlap /*ARGSUSED*/
113*a6d42e7dSPeter Dunlap static int
it_enable(int operandLen,char * operands[],cmdOptions_t * options,void * args)114*a6d42e7dSPeter Dunlap it_enable(int operandLen, char *operands[], cmdOptions_t *options,
115*a6d42e7dSPeter Dunlap     void *args)
116*a6d42e7dSPeter Dunlap {
117*a6d42e7dSPeter Dunlap 	int	ret;
118*a6d42e7dSPeter Dunlap 	int	fd;
119*a6d42e7dSPeter Dunlap 	char	buf[256];
120*a6d42e7dSPeter Dunlap 	uint32_t *buflenp;
121*a6d42e7dSPeter Dunlap 	char	*fqhnp;
122*a6d42e7dSPeter Dunlap 	iscsit_hostinfo_t hostinfo;
123*a6d42e7dSPeter Dunlap 
124*a6d42e7dSPeter Dunlap 	(void) fprintf(stdout, "%s: %s\n", cmdName,
125*a6d42e7dSPeter Dunlap 	    gettext("Requesting to enable iscsi target"));
126*a6d42e7dSPeter Dunlap 
127*a6d42e7dSPeter Dunlap 	bzero(buf, 256);
128*a6d42e7dSPeter Dunlap 	bzero(hostinfo.fqhn, sizeof (hostinfo.fqhn));
129*a6d42e7dSPeter Dunlap 
130*a6d42e7dSPeter Dunlap 	/* Open the iscsi target node */
131*a6d42e7dSPeter Dunlap 	if ((ret = it_open(&fd)) != ITADM_SUCCESS) {
132*a6d42e7dSPeter Dunlap 		(void) fprintf(stdout, "Unable to open device %s", ISCSIT_NODE);
133*a6d42e7dSPeter Dunlap 		return (ret);
134*a6d42e7dSPeter Dunlap 	}
135*a6d42e7dSPeter Dunlap 
136*a6d42e7dSPeter Dunlap 	(void) fprintf(stdout, "it_enable [fd=%d]\n", fd);
137*a6d42e7dSPeter Dunlap 	/* enable the iscsi target */
138*a6d42e7dSPeter Dunlap 	buflenp = (uint32_t *)((void *)&buf);
139*a6d42e7dSPeter Dunlap 	*buflenp = strlen("target_name") + 1;
140*a6d42e7dSPeter Dunlap 	(void) strncpy(buf + sizeof (uint32_t), "target_name",
141*a6d42e7dSPeter Dunlap 	    256 - sizeof (uint32_t));
142*a6d42e7dSPeter Dunlap 
143*a6d42e7dSPeter Dunlap 	fqhnp = &hostinfo.fqhn[0];
144*a6d42e7dSPeter Dunlap 
145*a6d42e7dSPeter Dunlap 	ret = sysinfo(SI_HOSTNAME, fqhnp, 256);
146*a6d42e7dSPeter Dunlap 
147*a6d42e7dSPeter Dunlap 	if ((ret != -1) && (ret < sizeof (hostinfo.fqhn))) {
148*a6d42e7dSPeter Dunlap 		fqhnp += ret;
149*a6d42e7dSPeter Dunlap 		hostinfo.length = ret;
150*a6d42e7dSPeter Dunlap 		hostinfo.fqhn[ret-1] = '.';
151*a6d42e7dSPeter Dunlap 		hostinfo.length += sysinfo(SI_SRPC_DOMAIN, fqhnp,
152*a6d42e7dSPeter Dunlap 		    sizeof (hostinfo.fqhn) - ret);
153*a6d42e7dSPeter Dunlap 	}
154*a6d42e7dSPeter Dunlap 
155*a6d42e7dSPeter Dunlap 	(void) fprintf(stdout, "it_enable: fqhn = '%s'\n", hostinfo.fqhn);
156*a6d42e7dSPeter Dunlap 
157*a6d42e7dSPeter Dunlap 	if ((ret = ioctl(fd, ISCSIT_IOC_ENABLE_SVC, &hostinfo)) != 0) {
158*a6d42e7dSPeter Dunlap 		(void) fprintf(stdout, "Unable to issue ioctl: %d", errno);
159*a6d42e7dSPeter Dunlap 		return (ret);
160*a6d42e7dSPeter Dunlap 	}
161*a6d42e7dSPeter Dunlap 	return (ITADM_SUCCESS);
162*a6d42e7dSPeter Dunlap }
163*a6d42e7dSPeter Dunlap 
164*a6d42e7dSPeter Dunlap 
165*a6d42e7dSPeter Dunlap /*
166*a6d42e7dSPeter Dunlap  * Disable the iSCSI target
167*a6d42e7dSPeter Dunlap  */
168*a6d42e7dSPeter Dunlap /* ARGSUSED */
169*a6d42e7dSPeter Dunlap static int
it_disable(int operandLen,char * operands[],cmdOptions_t * options,void * args)170*a6d42e7dSPeter Dunlap it_disable(int operandLen, char *operands[], cmdOptions_t *options,
171*a6d42e7dSPeter Dunlap     void *args)
172*a6d42e7dSPeter Dunlap {
173*a6d42e7dSPeter Dunlap 	int	ret;
174*a6d42e7dSPeter Dunlap 	int	fd;
175*a6d42e7dSPeter Dunlap 
176*a6d42e7dSPeter Dunlap 	(void) fprintf(stdout, "%s: %s\n", cmdName,
177*a6d42e7dSPeter Dunlap 	    gettext("Requesting to disable iscsi target"));
178*a6d42e7dSPeter Dunlap 
179*a6d42e7dSPeter Dunlap 	/* Open the iscsi target node */
180*a6d42e7dSPeter Dunlap 	if ((ret = it_open(&fd)) != ITADM_SUCCESS) {
181*a6d42e7dSPeter Dunlap 		return (ret);
182*a6d42e7dSPeter Dunlap 	}
183*a6d42e7dSPeter Dunlap 
184*a6d42e7dSPeter Dunlap 	/* disable the iSCSI target */
185*a6d42e7dSPeter Dunlap 	if ((ret = ioctl(fd, ISCSIT_IOC_DISABLE_SVC, NULL)) != 0) {
186*a6d42e7dSPeter Dunlap 		return (ret);
187*a6d42e7dSPeter Dunlap 	}
188*a6d42e7dSPeter Dunlap 	return (ITADM_SUCCESS);
189*a6d42e7dSPeter Dunlap }
190*a6d42e7dSPeter Dunlap 
191*a6d42e7dSPeter Dunlap /*
192*a6d42e7dSPeter Dunlap  * input:
193*a6d42e7dSPeter Dunlap  *  execFullName - exec name of program (argv[0])
194*a6d42e7dSPeter Dunlap  *
195*a6d42e7dSPeter Dunlap  *  copied from usr/src/cmd/zoneadm/zoneadm.c in OS/Net
196*a6d42e7dSPeter Dunlap  *  (changed name to lowerCamelCase to keep consistent with this file)
197*a6d42e7dSPeter Dunlap  *
198*a6d42e7dSPeter Dunlap  * Returns:
199*a6d42e7dSPeter Dunlap  *  command name portion of execFullName
200*a6d42e7dSPeter Dunlap  */
201*a6d42e7dSPeter Dunlap static char *
getExecBasename(char * execFullname)202*a6d42e7dSPeter Dunlap getExecBasename(char *execFullname)
203*a6d42e7dSPeter Dunlap {
204*a6d42e7dSPeter Dunlap 	char *lastSlash, *execBasename;
205*a6d42e7dSPeter Dunlap 
206*a6d42e7dSPeter Dunlap 	/* guard against '/' at end of command invocation */
207*a6d42e7dSPeter Dunlap 	for (;;) {
208*a6d42e7dSPeter Dunlap 		lastSlash = strrchr(execFullname, '/');
209*a6d42e7dSPeter Dunlap 		if (lastSlash == NULL) {
210*a6d42e7dSPeter Dunlap 			execBasename = execFullname;
211*a6d42e7dSPeter Dunlap 			break;
212*a6d42e7dSPeter Dunlap 		} else {
213*a6d42e7dSPeter Dunlap 			execBasename = lastSlash + 1;
214*a6d42e7dSPeter Dunlap 			if (*execBasename == '\0') {
215*a6d42e7dSPeter Dunlap 				*lastSlash = '\0';
216*a6d42e7dSPeter Dunlap 				continue;
217*a6d42e7dSPeter Dunlap 			}
218*a6d42e7dSPeter Dunlap 			break;
219*a6d42e7dSPeter Dunlap 		}
220*a6d42e7dSPeter Dunlap 	}
221*a6d42e7dSPeter Dunlap 	return (execBasename);
222*a6d42e7dSPeter Dunlap }
223*a6d42e7dSPeter Dunlap 
224*a6d42e7dSPeter Dunlap int
main(int argc,char * argv[])225*a6d42e7dSPeter Dunlap main(int argc, char *argv[])
226*a6d42e7dSPeter Dunlap {
227*a6d42e7dSPeter Dunlap 	synTables_t synTables;
228*a6d42e7dSPeter Dunlap 	char versionString[VERSION_STRING_MAX_LEN];
229*a6d42e7dSPeter Dunlap 	int ret;
230*a6d42e7dSPeter Dunlap 	int funcRet;
231*a6d42e7dSPeter Dunlap 	void *subcommandArgs = NULL;
232*a6d42e7dSPeter Dunlap 
233*a6d42e7dSPeter Dunlap 	(void) setlocale(LC_ALL, "");
234*a6d42e7dSPeter Dunlap 	/* set global command name */
235*a6d42e7dSPeter Dunlap 	cmdName = getExecBasename(argv[0]);
236*a6d42e7dSPeter Dunlap 
237*a6d42e7dSPeter Dunlap 	(void) snprintf(versionString, VERSION_STRING_MAX_LEN, "%s.%s",
238*a6d42e7dSPeter Dunlap 	    VERSION_STRING_MAJOR, VERSION_STRING_MINOR);
239*a6d42e7dSPeter Dunlap 	synTables.versionString = versionString;
240*a6d42e7dSPeter Dunlap 	synTables.longOptionTbl = &longOptions[0];
241*a6d42e7dSPeter Dunlap 	synTables.subCommandPropsTbl = &subcommands[0];
242*a6d42e7dSPeter Dunlap 
243*a6d42e7dSPeter Dunlap 	ret = cmdParse(argc, argv, synTables, subcommandArgs, &funcRet);
244*a6d42e7dSPeter Dunlap 	if (ret != 0) {
245*a6d42e7dSPeter Dunlap 		return (ret);
246*a6d42e7dSPeter Dunlap 	}
247*a6d42e7dSPeter Dunlap 
248*a6d42e7dSPeter Dunlap 	return (funcRet);
249*a6d42e7dSPeter Dunlap } /* end main */
250