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