1 /* $NetBSD: osd-target.c,v 1.1 2009/06/21 21:20:31 agc Exp $ */
4 * Copyright © 2006 Alistair Crooks. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright © 2006 \
34 The NetBSD Foundation, Inc. All rights reserved.");
35 __RCSID("$NetBSD: osd-target.c,v 1.1 2009/06/21 21:20:31 agc Exp $");
55 #include "iscsiutil.h"
66 static int g_main_pid
;
77 if (ISCSI_GETPID
!= g_main_pid
)
79 if (target_shutdown(&g
) != 0) {
80 iscsi_err(__FILE__
, __LINE__
, "target_shutdown() failed\n");
87 main(int argc
, char **argv
)
93 char TargetName
[1024];
97 (void) memset(&g
, 0x0, sizeof(g
));
98 (void) memset(&tv
, 0x0, sizeof(tv
));
99 (void) memset(&dv
, 0x0, sizeof(dv
));
100 (void) memset(&ev
, 0x0, sizeof(ev
));
103 (void) strlcpy(TargetName
, DEFAULT_TARGET_NAME
, sizeof(TargetName
));
105 detach_me_harder
= 1;
107 cf
= _PATH_OSD_TARGETS
;
109 while ((i
= getopt(argc
, argv
, "Dd:p:t:v:")) != -1) {
112 detach_me_harder
= 0;
115 device_set_var("directory", optarg
);
121 g
.port
= (uint16_t) atoi(optarg
);
124 (void) strlcpy(TargetName
, optarg
, sizeof(TargetName
));
127 if (strcmp(optarg
, "net") == 0) {
129 } else if (strcmp(optarg
, "iscsi") == 0) {
131 } else if (strcmp(optarg
, "scsi") == 0) {
133 } else if (strcmp(optarg
, "osd") == 0) {
135 } else if (strcmp(optarg
, "all") == 0) {
142 if (!read_conf_file(cf
, &tv
, &dv
, &ev
)) {
143 (void) fprintf(stderr
, "Error: can't open `%s'\n", cf
);
147 (void) signal(SIGPIPE
, SIG_IGN
);
149 (void) signal(SIGINT
, handler
);
150 g_main_pid
= ISCSI_GETPID
;
153 (void) fprintf(stderr
, "No targets to initialise\n");
156 /* Initialize target */
157 for (i
= optind
; i
< argc
; i
++) {
158 if (target_init(&g
, &tv
, TargetName
, i
) != 0) {
159 iscsi_err(__FILE__
, __LINE__
, "target_init() failed\n");
165 /* if we are supposed to be a daemon, detach from controlling tty */
166 if (detach_me_harder
&& daemon(0, 0) < 0) {
167 iscsi_err(__FILE__
, __LINE__
, "daemon() failed\n");
172 /* write pid to a file */
173 write_pid_file(_PATH_OSD_PID_FILE
);
175 /* Wait for connections */
176 if (target_listen(&g
) != 0) {
177 iscsi_err(__FILE__
, __LINE__
, "target_listen() failed\n");