dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / iscsitsvc / iscsitsvc.c
blob8005b08812704d672f53614125dd7f3a98a27bc4
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
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>
45 #include <libiscsit.h>
46 #include <sys/iscsit/iscsit_common.h>
48 static int it_enable(int, char **, cmdOptions_t *, void *);
49 static int it_disable(int, char **, cmdOptions_t *, void *);
52 * MAJOR - This should only change when there is an incompatible change made
53 * to the interfaces or the output.
55 * MINOR - This should change whenever there is a new command or new feature
56 * with no incompatible change.
58 #define VERSION_STRING_MAJOR "1"
59 #define VERSION_STRING_MINOR "0"
60 #define VERSION_STRING_MAX_LEN 10
62 /* 10 ms sleep in nanoseconds */
63 #define TEN_MS_NANOSLEEP 10000000
65 /* tables set up based on cmdparse instructions */
67 /* add new options here */
68 optionTbl_t longOptions[] = {
69 {NULL, 0, 0, 0}
73 * Add new subcommands here
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, NULL, 0, NULL}
81 /* globals */
82 char *cmdName;
85 * Opens the iSCSI Target Node
87 * fd - Return the iscsit file descriptor
89 static int
90 it_open(int *fd)
93 int ret = ITADM_SUCCESS;
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;
106 return (ret);
110 * Enables the iSCSI Target
112 /*ARGSUSED*/
113 static int
114 it_enable(int operandLen, char *operands[], cmdOptions_t *options,
115 void *args)
117 int ret;
118 int fd;
119 char buf[256];
120 uint32_t *buflenp;
121 char *fqhnp;
122 iscsit_hostinfo_t hostinfo;
124 (void) fprintf(stdout, "%s: %s\n", cmdName,
125 gettext("Requesting to enable iscsi target"));
127 bzero(buf, 256);
128 bzero(hostinfo.fqhn, sizeof (hostinfo.fqhn));
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);
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));
143 fqhnp = &hostinfo.fqhn[0];
145 ret = sysinfo(SI_HOSTNAME, fqhnp, 256);
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);
155 (void) fprintf(stdout, "it_enable: fqhn = '%s'\n", hostinfo.fqhn);
157 if ((ret = ioctl(fd, ISCSIT_IOC_ENABLE_SVC, &hostinfo)) != 0) {
158 (void) fprintf(stdout, "Unable to issue ioctl: %d", errno);
159 return (ret);
161 return (ITADM_SUCCESS);
166 * Disable the iSCSI target
168 /* ARGSUSED */
169 static int
170 it_disable(int operandLen, char *operands[], cmdOptions_t *options,
171 void *args)
173 int ret;
174 int fd;
176 (void) fprintf(stdout, "%s: %s\n", cmdName,
177 gettext("Requesting to disable iscsi target"));
179 /* Open the iscsi target node */
180 if ((ret = it_open(&fd)) != ITADM_SUCCESS) {
181 return (ret);
184 /* disable the iSCSI target */
185 if ((ret = ioctl(fd, ISCSIT_IOC_DISABLE_SVC, NULL)) != 0) {
186 return (ret);
188 return (ITADM_SUCCESS);
192 * input:
193 * execFullName - exec name of program (argv[0])
195 * copied from usr/src/cmd/zoneadm/zoneadm.c in OS/Net
196 * (changed name to lowerCamelCase to keep consistent with this file)
198 * Returns:
199 * command name portion of execFullName
201 static char *
202 getExecBasename(char *execFullname)
204 char *lastSlash, *execBasename;
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;
218 break;
221 return (execBasename);
225 main(int argc, char *argv[])
227 synTables_t synTables;
228 char versionString[VERSION_STRING_MAX_LEN];
229 int ret;
230 int funcRet;
231 void *subcommandArgs = NULL;
233 (void) setlocale(LC_ALL, "");
234 /* set global command name */
235 cmdName = getExecBasename(argv[0]);
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];
243 ret = cmdParse(argc, argv, synTables, subcommandArgs, &funcRet);
244 if (ret != 0) {
245 return (ret);
248 return (funcRet);
249 } /* end main */