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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
29 #include <sys/types.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
[] = {
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
, 0, NULL
, 0, NULL
}
85 * Opens the iSCSI Target Node
87 * fd - Return the iscsit file descriptor
93 int ret
= ITADM_SUCCESS
;
95 *fd
= open(ISCSIT_NODE
, O_RDONLY
);
98 (void) fprintf(stdout
, "open failed: EPERM");
101 (void) fprintf(stdout
, "open failed: INVALID");
110 * Enables the iSCSI Target
114 it_enable(int operandLen
, char *operands
[], cmdOptions_t
*options
,
122 iscsit_hostinfo_t hostinfo
;
124 (void) fprintf(stdout
, "%s: %s\n", cmdName
,
125 gettext("Requesting to enable iscsi target"));
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
);
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
))) {
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
);
161 return (ITADM_SUCCESS
);
166 * Disable the iSCSI target
170 it_disable(int operandLen
, char *operands
[], cmdOptions_t
*options
,
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
) {
184 /* disable the iSCSI target */
185 if ((ret
= ioctl(fd
, ISCSIT_IOC_DISABLE_SVC
, NULL
)) != 0) {
188 return (ITADM_SUCCESS
);
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)
199 * command name portion of execFullName
202 getExecBasename(char *execFullname
)
204 char *lastSlash
, *execBasename
;
206 /* guard against '/' at end of command invocation */
208 lastSlash
= strrchr(execFullname
, '/');
209 if (lastSlash
== NULL
) {
210 execBasename
= execFullname
;
213 execBasename
= lastSlash
+ 1;
214 if (*execBasename
== '\0') {
221 return (execBasename
);
225 main(int argc
, char *argv
[])
227 synTables_t synTables
;
228 char versionString
[VERSION_STRING_MAX_LEN
];
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
);