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