1*5e01dafbSagc /* $NetBSD: iscsi-target.c,v 1.2 2009/06/30 02:44:53 agc Exp $ */
2b6364952Sagc
3b6364952Sagc /*-
4b6364952Sagc * Copyright (c) 2009 The NetBSD Foundation, Inc.
5b6364952Sagc * All rights reserved.
6b6364952Sagc *
7b6364952Sagc * This code is derived from software contributed to The NetBSD Foundation
8b6364952Sagc * by Alistair Crooks (agc@netbsd.org)
9b6364952Sagc *
10b6364952Sagc * Redistribution and use in source and binary forms, with or without
11b6364952Sagc * modification, are permitted provided that the following conditions
12b6364952Sagc * are met:
13b6364952Sagc * 1. Redistributions of source code must retain the above copyright
14b6364952Sagc * notice, this list of conditions and the following disclaimer.
15b6364952Sagc * 2. Redistributions in binary form must reproduce the above copyright
16b6364952Sagc * notice, this list of conditions and the following disclaimer in the
17b6364952Sagc * documentation and/or other materials provided with the distribution.
18b6364952Sagc *
19b6364952Sagc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20b6364952Sagc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21b6364952Sagc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22b6364952Sagc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23b6364952Sagc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24b6364952Sagc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25b6364952Sagc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26b6364952Sagc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27b6364952Sagc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28b6364952Sagc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29b6364952Sagc * POSSIBILITY OF SUCH DAMAGE.
30b6364952Sagc */
31b6364952Sagc #include "config.h"
32b6364952Sagc
33b6364952Sagc #ifdef HAVE_SIGNAL_H
34b6364952Sagc #include <signal.h>
35b6364952Sagc #endif
36b6364952Sagc
37b6364952Sagc #include <ctype.h>
38b6364952Sagc #include <stdio.h>
39b6364952Sagc #include <stdlib.h>
40b6364952Sagc
41b6364952Sagc #ifdef HAVE_STRING_H
42b6364952Sagc #include <string.h>
43b6364952Sagc #endif
44b6364952Sagc
45b6364952Sagc #include <unistd.h>
46b6364952Sagc
47b6364952Sagc #include "iscsi.h"
48b6364952Sagc
49b6364952Sagc
50b6364952Sagc int
main(int argc,char ** argv)51b6364952Sagc main(int argc, char **argv)
52b6364952Sagc {
53b6364952Sagc iscsi_target_t tgt;
54b6364952Sagc char buf[32];
55b6364952Sagc int detach_me_harder;
56b6364952Sagc int i;
57b6364952Sagc
58b6364952Sagc detach_me_harder = 1;
59b6364952Sagc iscsi_target_set_defaults(&tgt);
60b6364952Sagc while ((i = getopt(argc, argv, "46b:Df:p:s:t:Vv:")) != -1) {
61b6364952Sagc switch (i) {
62b6364952Sagc case '4':
63b6364952Sagc case '6':
64b6364952Sagc (void) snprintf(buf, sizeof(buf), "%d", i);
65b6364952Sagc iscsi_target_setvar(&tgt, "address family", buf);
66b6364952Sagc break;
67b6364952Sagc case 'b':
68b6364952Sagc iscsi_target_setvar(&tgt, "blocklen", optarg);
69b6364952Sagc break;
70b6364952Sagc case 'D':
71b6364952Sagc detach_me_harder = 0;
72b6364952Sagc break;
73b6364952Sagc case 'f':
74b6364952Sagc iscsi_target_setvar(&tgt, "configfile", optarg);
75b6364952Sagc break;
76b6364952Sagc case 'p':
77b6364952Sagc iscsi_target_setvar(&tgt, "target port", optarg);
78b6364952Sagc break;
79b6364952Sagc case 's':
80b6364952Sagc if ((i = atoi(optarg)) > 0 && i < 128) {
81b6364952Sagc iscsi_target_setvar(&tgt, "max sessions", optarg);
82b6364952Sagc }
83b6364952Sagc break;
84b6364952Sagc case 't':
85b6364952Sagc iscsi_target_setvar(&tgt, "iqn", optarg);
86b6364952Sagc break;
87b6364952Sagc case 'V':
88b6364952Sagc (void) printf("\"%s\" %s\nPlease send all bug reports to %s\n", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_BUGREPORT);
89b6364952Sagc exit(EXIT_SUCCESS);
90b6364952Sagc /* NOTREACHED */
91b6364952Sagc case 'v':
92b6364952Sagc if (strcmp(optarg, "net") == 0) {
93b6364952Sagc iscsi_target_setvar(&tgt, "debug", "net");
94b6364952Sagc } else if (strcmp(optarg, "iscsi") == 0) {
95b6364952Sagc iscsi_target_setvar(&tgt, "debug", "iscsi");
96b6364952Sagc } else if (strcmp(optarg, "scsi") == 0) {
97b6364952Sagc iscsi_target_setvar(&tgt, "debug", "scsi");
98b6364952Sagc } else if (strcmp(optarg, "all") == 0) {
99b6364952Sagc iscsi_target_setvar(&tgt, "debug", "all");
100b6364952Sagc }
101b6364952Sagc break;
102b6364952Sagc }
103b6364952Sagc }
104b6364952Sagc
105b6364952Sagc (void) signal(SIGPIPE, SIG_IGN);
106b6364952Sagc
107b6364952Sagc /* Initialize target */
108b6364952Sagc if (iscsi_target_start(&tgt) != 0) {
109b6364952Sagc (void) fprintf(stderr, "iscsi_target_start() failed\n");
110b6364952Sagc exit(EXIT_FAILURE);
111b6364952Sagc }
112b6364952Sagc
113b6364952Sagc #ifdef HAVE_DAEMON
114b6364952Sagc /* if we are supposed to be a daemon, detach from controlling tty */
115b6364952Sagc if (detach_me_harder && daemon(0, 0) < 0) {
116b6364952Sagc (void) fprintf(stderr, "daemon() failed\n");
117b6364952Sagc exit(EXIT_FAILURE);
118b6364952Sagc }
119b6364952Sagc #endif
120b6364952Sagc
121b6364952Sagc /* write pid to a file */
122b6364952Sagc iscsi_target_write_pidfile(NULL);
123b6364952Sagc
124b6364952Sagc /* Wait for connections */
125b6364952Sagc if (iscsi_target_listen(&tgt) != 0) {
126b6364952Sagc (void) fprintf(stderr, "iscsi_target_listen() failed\n");
127b6364952Sagc }
128b6364952Sagc
129b6364952Sagc return EXIT_SUCCESS;
130b6364952Sagc }
131