1*5e01dafbSagc /* $NetBSD: osd-target.c,v 1.2 2009/06/30 02:44:52 agc Exp $ */
2534fa804Sagc
3534fa804Sagc /*
4534fa804Sagc * Copyright � 2006 Alistair Crooks. All rights reserved.
5534fa804Sagc *
6534fa804Sagc * Redistribution and use in source and binary forms, with or without
7534fa804Sagc * modification, are permitted provided that the following conditions
8534fa804Sagc * are met:
9534fa804Sagc * 1. Redistributions of source code must retain the above copyright
10534fa804Sagc * notice, this list of conditions and the following disclaimer.
11534fa804Sagc * 2. Redistributions in binary form must reproduce the above copyright
12534fa804Sagc * notice, this list of conditions and the following disclaimer in the
13534fa804Sagc * documentation and/or other materials provided with the distribution.
14534fa804Sagc * 3. The name of the author may not be used to endorse or promote
15534fa804Sagc * products derived from this software without specific prior written
16534fa804Sagc * permission.
17534fa804Sagc *
18534fa804Sagc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19534fa804Sagc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20534fa804Sagc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21534fa804Sagc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22534fa804Sagc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23534fa804Sagc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24534fa804Sagc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25534fa804Sagc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26534fa804Sagc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27534fa804Sagc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28534fa804Sagc * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29534fa804Sagc */
30534fa804Sagc #include <sys/cdefs.h>
31534fa804Sagc
32534fa804Sagc #ifndef lint
33534fa804Sagc __COPYRIGHT("@(#) Copyright � 2006 \
34534fa804Sagc The NetBSD Foundation, Inc. All rights reserved.");
35*5e01dafbSagc __RCSID("$NetBSD: osd-target.c,v 1.2 2009/06/30 02:44:52 agc Exp $");
36534fa804Sagc #endif
37534fa804Sagc #include "config.h"
38534fa804Sagc
39534fa804Sagc #define EXTERN
40534fa804Sagc
41534fa804Sagc #ifdef HAVE_SIGNAL_H
42534fa804Sagc #include <signal.h>
43534fa804Sagc #endif
44534fa804Sagc
45534fa804Sagc #include <stdio.h>
46534fa804Sagc #include <stdlib.h>
47534fa804Sagc
48534fa804Sagc #ifdef HAVE_STRING_H
49534fa804Sagc #include <string.h>
50534fa804Sagc #endif
51534fa804Sagc
52534fa804Sagc #include <unistd.h>
53534fa804Sagc
54534fa804Sagc #include "iscsi.h"
55534fa804Sagc #include "iscsiutil.h"
56534fa804Sagc #include "target.h"
57534fa804Sagc #include "device.h"
58534fa804Sagc
59534fa804Sagc #include "conffile.h"
60534fa804Sagc #include "storage.h"
61534fa804Sagc
62534fa804Sagc /*
63534fa804Sagc * Globals
64534fa804Sagc */
65534fa804Sagc
66534fa804Sagc static int g_main_pid;
67534fa804Sagc static globals_t g;
68534fa804Sagc
69534fa804Sagc /*
70534fa804Sagc * Control-C handler
71534fa804Sagc */
72534fa804Sagc
73534fa804Sagc /* ARGSUSED0 */
74534fa804Sagc static void
handler(int s)75534fa804Sagc handler(int s)
76534fa804Sagc {
77534fa804Sagc if (ISCSI_GETPID != g_main_pid)
78534fa804Sagc return;
79534fa804Sagc if (target_shutdown(&g) != 0) {
80*5e01dafbSagc iscsi_err(__FILE__, __LINE__, "target_shutdown() failed\n");
81534fa804Sagc return;
82534fa804Sagc }
83534fa804Sagc return;
84534fa804Sagc }
85534fa804Sagc
86534fa804Sagc int
main(int argc,char ** argv)87534fa804Sagc main(int argc, char **argv)
88534fa804Sagc {
89534fa804Sagc const char *cf;
90534fa804Sagc targv_t tv;
91534fa804Sagc devv_t dv;
92534fa804Sagc extv_t ev;
93534fa804Sagc char TargetName[1024];
94534fa804Sagc int detach_me_harder;
95534fa804Sagc int i;
96534fa804Sagc
97534fa804Sagc (void) memset(&g, 0x0, sizeof(g));
98534fa804Sagc (void) memset(&tv, 0x0, sizeof(tv));
99534fa804Sagc (void) memset(&dv, 0x0, sizeof(dv));
100534fa804Sagc (void) memset(&ev, 0x0, sizeof(ev));
101534fa804Sagc
102534fa804Sagc /* set defaults */
103534fa804Sagc (void) strlcpy(TargetName, DEFAULT_TARGET_NAME, sizeof(TargetName));
104534fa804Sagc g.port = ISCSI_PORT;
105534fa804Sagc detach_me_harder = 1;
106534fa804Sagc
107534fa804Sagc cf = _PATH_OSD_TARGETS;
108534fa804Sagc
109534fa804Sagc while ((i = getopt(argc, argv, "Dd:p:t:v:")) != -1) {
110534fa804Sagc switch (i) {
111534fa804Sagc case 'D':
112534fa804Sagc detach_me_harder = 0;
113534fa804Sagc break;
114534fa804Sagc case 'd':
115534fa804Sagc device_set_var("directory", optarg);
116534fa804Sagc break;
117534fa804Sagc case 'f':
118534fa804Sagc cf = optarg;
119534fa804Sagc break;
120534fa804Sagc case 'p':
121534fa804Sagc g.port = (uint16_t) atoi(optarg);
122534fa804Sagc break;
123534fa804Sagc case 't':
124534fa804Sagc (void) strlcpy(TargetName, optarg, sizeof(TargetName));
125534fa804Sagc break;
126534fa804Sagc case 'v':
127534fa804Sagc if (strcmp(optarg, "net") == 0) {
128534fa804Sagc set_debug("net");
129534fa804Sagc } else if (strcmp(optarg, "iscsi") == 0) {
130534fa804Sagc set_debug("iscsi");
131534fa804Sagc } else if (strcmp(optarg, "scsi") == 0) {
132534fa804Sagc set_debug("scsi");
133534fa804Sagc } else if (strcmp(optarg, "osd") == 0) {
134534fa804Sagc set_debug("osd");
135534fa804Sagc } else if (strcmp(optarg, "all") == 0) {
136534fa804Sagc set_debug("all");
137534fa804Sagc }
138534fa804Sagc break;
139534fa804Sagc }
140534fa804Sagc }
141534fa804Sagc
142534fa804Sagc if (!read_conf_file(cf, &tv, &dv, &ev)) {
143534fa804Sagc (void) fprintf(stderr, "Error: can't open `%s'\n", cf);
144534fa804Sagc return EXIT_FAILURE;
145534fa804Sagc }
146534fa804Sagc
147534fa804Sagc (void) signal(SIGPIPE, SIG_IGN);
148534fa804Sagc
149534fa804Sagc (void) signal(SIGINT, handler);
150534fa804Sagc g_main_pid = ISCSI_GETPID;
151534fa804Sagc
152534fa804Sagc if (tv.c == 0) {
153534fa804Sagc (void) fprintf(stderr, "No targets to initialise\n");
154534fa804Sagc return EXIT_FAILURE;
155534fa804Sagc }
156534fa804Sagc /* Initialize target */
157534fa804Sagc for (i = optind ; i < argc ; i++) {
158534fa804Sagc if (target_init(&g, &tv, TargetName, i) != 0) {
159*5e01dafbSagc iscsi_err(__FILE__, __LINE__, "target_init() failed\n");
160534fa804Sagc exit(EXIT_FAILURE);
161534fa804Sagc }
162534fa804Sagc }
163534fa804Sagc
164534fa804Sagc #ifdef HAVE_DAEMON
165534fa804Sagc /* if we are supposed to be a daemon, detach from controlling tty */
166534fa804Sagc if (detach_me_harder && daemon(0, 0) < 0) {
167*5e01dafbSagc iscsi_err(__FILE__, __LINE__, "daemon() failed\n");
168534fa804Sagc exit(EXIT_FAILURE);
169534fa804Sagc }
170534fa804Sagc #endif
171534fa804Sagc
172534fa804Sagc /* write pid to a file */
173534fa804Sagc write_pid_file(_PATH_OSD_PID_FILE);
174534fa804Sagc
175534fa804Sagc /* Wait for connections */
176534fa804Sagc if (target_listen(&g) != 0) {
177*5e01dafbSagc iscsi_err(__FILE__, __LINE__, "target_listen() failed\n");
178534fa804Sagc }
179534fa804Sagc
180534fa804Sagc return EXIT_SUCCESS;
181534fa804Sagc }
182