1/* $NetBSD: osd-target.c,v 1.1 2009/06/21 21:20:31 agc Exp $ */ 2 3/* 4 * Copyright � 2006 Alistair Crooks. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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 16 * permission. 17 * 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. 29 */ 30#include <sys/cdefs.h> 31 32#ifndef lint 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 $"); 36#endif 37#include "config.h" 38 39#define EXTERN 40 41#ifdef HAVE_SIGNAL_H 42#include <signal.h> 43#endif 44 45#include <stdio.h> 46#include <stdlib.h> 47 48#ifdef HAVE_STRING_H 49#include <string.h> 50#endif 51 52#include <unistd.h> 53 54#include "iscsi.h" 55#include "iscsiutil.h" 56#include "target.h" 57#include "device.h" 58 59#include "conffile.h" 60#include "storage.h" 61 62/* 63 * Globals 64 */ 65 66static int g_main_pid; 67static globals_t g; 68 69/* 70 * Control-C handler 71 */ 72 73/* ARGSUSED0 */ 74static void 75handler(int s) 76{ 77 if (ISCSI_GETPID != g_main_pid) 78 return; 79 if (target_shutdown(&g) != 0) { 80 iscsi_err(__FILE__, __LINE__, "target_shutdown() failed\n"); 81 return; 82 } 83 return; 84} 85 86int 87main(int argc, char **argv) 88{ 89 const char *cf; 90 targv_t tv; 91 devv_t dv; 92 extv_t ev; 93 char TargetName[1024]; 94 int detach_me_harder; 95 int i; 96 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)); 101 102 /* set defaults */ 103 (void) strlcpy(TargetName, DEFAULT_TARGET_NAME, sizeof(TargetName)); 104 g.port = ISCSI_PORT; 105 detach_me_harder = 1; 106 107 cf = _PATH_OSD_TARGETS; 108 109 while ((i = getopt(argc, argv, "Dd:p:t:v:")) != -1) { 110 switch (i) { 111 case 'D': 112 detach_me_harder = 0; 113 break; 114 case 'd': 115 device_set_var("directory", optarg); 116 break; 117 case 'f': 118 cf = optarg; 119 break; 120 case 'p': 121 g.port = (uint16_t) atoi(optarg); 122 break; 123 case 't': 124 (void) strlcpy(TargetName, optarg, sizeof(TargetName)); 125 break; 126 case 'v': 127 if (strcmp(optarg, "net") == 0) { 128 set_debug("net"); 129 } else if (strcmp(optarg, "iscsi") == 0) { 130 set_debug("iscsi"); 131 } else if (strcmp(optarg, "scsi") == 0) { 132 set_debug("scsi"); 133 } else if (strcmp(optarg, "osd") == 0) { 134 set_debug("osd"); 135 } else if (strcmp(optarg, "all") == 0) { 136 set_debug("all"); 137 } 138 break; 139 } 140 } 141 142 if (!read_conf_file(cf, &tv, &dv, &ev)) { 143 (void) fprintf(stderr, "Error: can't open `%s'\n", cf); 144 return EXIT_FAILURE; 145 } 146 147 (void) signal(SIGPIPE, SIG_IGN); 148 149 (void) signal(SIGINT, handler); 150 g_main_pid = ISCSI_GETPID; 151 152 if (tv.c == 0) { 153 (void) fprintf(stderr, "No targets to initialise\n"); 154 return EXIT_FAILURE; 155 } 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"); 160 exit(EXIT_FAILURE); 161 } 162 } 163 164#ifdef HAVE_DAEMON 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"); 168 exit(EXIT_FAILURE); 169 } 170#endif 171 172 /* write pid to a file */ 173 write_pid_file(_PATH_OSD_PID_FILE); 174 175 /* Wait for connections */ 176 if (target_listen(&g) != 0) { 177 iscsi_err(__FILE__, __LINE__, "target_listen() failed\n"); 178 } 179 180 return EXIT_SUCCESS; 181} 182