| 1 |
/* |
| 2 |
* Copyright (C) 2008-2011 Daisuke Aoyama <[email protected]>. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 15 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 18 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 20 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 21 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 22 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 23 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 24 |
* SUCH DAMAGE. |
| 25 |
* |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifdef HAVE_CONFIG_H |
| 29 |
#include "config.h" |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <inttypes.h> |
| 33 |
#include <stdint.h> |
| 34 |
|
| 35 |
#include <errno.h> |
| 36 |
#include <stdarg.h> |
| 37 |
#include <stdio.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include <string.h> |
| 40 |
#include <poll.h> |
| 41 |
#include <pthread.h> |
| 42 |
#ifdef HAVE_PTHREAD_NP_H |
| 43 |
#include <pthread_np.h> |
| 44 |
#endif |
| 45 |
#include <fcntl.h> |
| 46 |
#include <unistd.h> |
| 47 |
#include <signal.h> |
| 48 |
|
| 49 |
#include <sys/types.h> |
| 50 |
#include <sys/socket.h> |
| 51 |
#include <netinet/in.h> |
| 52 |
|
| 53 |
#include "istgt.h" |
| 54 |
#include "istgt_ver.h" |
| 55 |
#include "istgt_log.h" |
| 56 |
#include "istgt_conf.h" |
| 57 |
#include "istgt_sock.h" |
| 58 |
#include "istgt_misc.h" |
| 59 |
#include "istgt_crc32c.h" |
| 60 |
#include "istgt_iscsi.h" |
| 61 |
#include "istgt_lu.h" |
| 62 |
#include "istgt_proto.h" |
| 63 |
|
| 64 |
#ifdef ISTGT_USE_KQUEUE |
| 65 |
#include <sys/types.h> |
| 66 |
#include <sys/event.h> |
| 67 |
#include <sys/time.h> |
| 68 |
#endif |
| 69 |
|
| 70 |
#define POLLWAIT 3000 |
| 71 |
#define PORTNUMLEN 32 |
| 72 |
|
| 73 |
ISTGT g_istgt; |
| •74 |
const char *g_config_file; |
| 75 |
|
| 76 |
#if 0 |
| 77 |
void |
| 78 |
fatal(const char *msg, ...) |
| 79 |
{ |
| 80 |
va_list ap; |
| 81 |
va_start(ap, msg); |
| 82 |
vfprintf(stderr, msg, ap); |
| 83 |
va_end(ap); |
| 84 |
exit(EXIT_FAILURE); |
| 85 |
} |
| 86 |
#endif |
| 87 |
|
| 88 |
static int |
| 89 |
istgt_parse_portal(const char *portal, char **host, char **port) |
| 90 |
{ |
| 91 |
const char *p; |
| 92 |
int n; |
| 93 |
|
| 94 |
if (portal == NULL) { |
| 95 |
ISTGT_ERRLOG("portal error\n"); |
| 96 |
return -1; |
| 97 |
} |
| 98 |
|
| 99 |
if (portal[0] == '[') { |
| 100 |
/* IPv6 */ |
| 101 |
p = strchr(portal + 1, ']'); |
| 102 |
if (p == NULL) { |
| 103 |
ISTGT_ERRLOG("portal error\n"); |
| 104 |
return -1; |
| 105 |
} |
| 106 |
#if 0 |
| 107 |
n = p - (portal + 1); |
| 108 |
*host = xmalloc(n + 1); |
| 109 |
memcpy(*host, portal + 1, n); |
| 110 |
(*host)[n] = '\0'; |
| 111 |
p++; |
| 112 |
#else |
| 113 |
p++; |
| 114 |
n = p - portal; |
| 115 |
*host = xmalloc(n + 1); |
| 116 |
memcpy(*host, portal, n); |
| 117 |
(*host)[n] = '\0'; |
| 118 |
#endif |
| 119 |
if (p[0] == '\0') { |
| 120 |
*port = xmalloc(PORTNUMLEN); |
| 121 |
snprintf(*port, PORTNUMLEN, "%d", DEFAULT_PORT); |
| 122 |
} else { |
| 123 |
if (p[0] != ':') { |
| 124 |
ISTGT_ERRLOG("portal error\n"); |
| 125 |
xfree(*host); |
| 126 |
return -1; |
| 127 |
} |
| 128 |
*port = xstrdup(p + 1); |
| 129 |
} |
| 130 |
} else { |
| 131 |
/* IPv4 */ |
| 132 |
p = strchr(portal, ':'); |
| 133 |
if (p == NULL) { |
| 134 |
p = portal + strlen(portal); |
| 135 |
} |
| 136 |
n = p - portal; |
| 137 |
*host = xmalloc(n + 1); |
| 138 |
memcpy(*host, portal, n); |
| 139 |
(*host)[n] = '\0'; |
| 140 |
if (p[0] == '\0') { |
| 141 |
*port = xmalloc(PORTNUMLEN); |
| 142 |
snprintf(*port, PORTNUMLEN, "%d", DEFAULT_PORT); |
| 143 |
} else { |
| 144 |
if (p[0] != ':') { |
| 145 |
ISTGT_ERRLOG("portal error\n"); |
| 146 |
xfree(*host); |
| 147 |
return -1; |
| 148 |
} |
| 149 |
*port = xstrdup(p + 1); |
| 150 |
} |
| 151 |
} |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
static int |
| 156 |
istgt_add_portal(ISTGT_Ptr istgt, CF_SECTION *sp, int idx1) |
| 157 |
{ |
| 158 |
char *label, *portal, *host, *port; |
| 159 |
int idx; |
| 160 |
int rc; |
| 161 |
|
| 162 |
label = istgt_get_nmval(sp, "Portal", idx1, 0); |
| 163 |
portal = istgt_get_nmval(sp, "Portal", idx1, 1); |
| 164 |
if (label == NULL || portal == NULL) { |
| 165 |
ISTGT_ERRLOG("portal error\n"); |
| 166 |
return -1; |
| 167 |
} |
| 168 |
|
| 169 |
rc = istgt_parse_portal(portal, &host, &port); |
| 170 |
if (rc < 0) { |
| 171 |
ISTGT_ERRLOG("parse portal error\n"); |
| 172 |
return -1; |
| 173 |
} |
| 174 |
|
| 175 |
idx = istgt->nportal; |
| 176 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 177 |
"Index=%d, Host=%s, Port=%s, Tag=%d\n", |
| 178 |
idx, host, port, sp->num); |
| 179 |
if (idx < MAX_PORTAL) { |
| 180 |
istgt->portal[idx].label = xstrdup(label); |
| 181 |
istgt->portal[idx].host = host; |
| 182 |
istgt->portal[idx].port = port; |
| 183 |
istgt->portal[idx].idx = idx; |
| 184 |
istgt->portal[idx].tag = sp->num; |
| 185 |
istgt->portal[idx].sock = -1; |
| 186 |
idx++; |
| 187 |
istgt->nportal = idx; |
| 188 |
} else { |
| 189 |
ISTGT_ERRLOG("nportal(%d) >= MAX_PORTAL\n", idx); |
| 190 |
xfree(host); |
| 191 |
xfree(port); |
| 192 |
return -1; |
| 193 |
} |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
static int |
| 198 |
istgt_build_portal_array(ISTGT_Ptr istgt) |
| 199 |
{ |
| 200 |
CF_SECTION *sp; |
| 201 |
const char *val; |
| 202 |
int rc; |
| 203 |
int i; |
| 204 |
|
| 205 |
sp = istgt->config->section; |
| 206 |
while (sp != NULL) { |
| 207 |
if (sp->type == ST_PORTALGROUP) { |
| 208 |
if (sp->num == 0) { |
| 209 |
ISTGT_ERRLOG("Group 0 is invalid\n"); |
| 210 |
return -1; |
| 211 |
} |
| 212 |
if (sp->num > ISTGT_PG_TAG_MAX) { |
| 213 |
ISTGT_ERRLOG("tag %d is invalid\n", sp->num); |
| 214 |
return -1; |
| 215 |
} |
| 216 |
|
| 217 |
val = istgt_get_val(sp, "Comment"); |
| 218 |
if (val != NULL) { |
| 219 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 220 |
"Comment %s\n", val); |
| 221 |
} |
| 222 |
for (i = 0; ; i++) { |
| 223 |
val = istgt_get_nval(sp, "Portal", i); |
| 224 |
if (val == NULL) |
| 225 |
break; |
| 226 |
rc = istgt_add_portal(istgt, sp, i); |
| 227 |
if (rc < 0) { |
| 228 |
ISTGT_ERRLOG("add_portal() failed\n"); |
| 229 |
return -1; |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
sp = sp->next; |
| 234 |
} |
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
static void |
| 239 |
istgt_destroy_portal_array(ISTGT_Ptr istgt) |
| 240 |
{ |
| 241 |
int i; |
| 242 |
|
| 243 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_destroy_portal_array\n"); |
| 244 |
for (i = 0; i < istgt->nportal; i++) { |
| 245 |
xfree(istgt->portal[i].label); |
| 246 |
xfree(istgt->portal[i].host); |
| 247 |
xfree(istgt->portal[i].port); |
| 248 |
|
| 249 |
istgt->portal[i].label = NULL; |
| 250 |
istgt->portal[i].host = NULL; |
| 251 |
istgt->portal[i].port = NULL; |
| 252 |
istgt->portal[i].idx = i; |
| 253 |
istgt->portal[i].tag = 0; |
| 254 |
} |
| 255 |
istgt->nportal = 0; |
| 256 |
} |
| 257 |
|
| 258 |
static int |
| 259 |
istgt_open_portal(ISTGT_Ptr istgt) |
| 260 |
{ |
| 261 |
int port; |
| 262 |
int sock; |
| 263 |
int i; |
| 264 |
|
| 265 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_open_portal\n"); |
| 266 |
for (i = 0; i < istgt->nportal; i++) { |
| 267 |
if (istgt->portal[i].sock < 0) { |
| 268 |
ISTGT_TRACELOG(ISTGT_TRACE_NET, |
| 269 |
"open host %s, port %s, tag %d\n", |
| 270 |
istgt->portal[i].host, istgt->portal[i].port, |
| 271 |
istgt->portal[i].tag); |
| 272 |
port = (int)strtol(istgt->portal[i].port, NULL, 0); |
| 273 |
sock = istgt_listen(istgt->portal[i].host, port); |
| 274 |
if (sock < 0) { |
| 275 |
ISTGT_ERRLOG("listen error %.64s:%d\n", |
| 276 |
istgt->portal[i].host, port); |
| 277 |
return -1; |
| 278 |
} |
| 279 |
istgt->portal[i].sock = sock; |
| 280 |
} |
| 281 |
} |
| 282 |
return 0; |
| 283 |
} |
| 284 |
|
| 285 |
static int |
| 286 |
istgt_close_portal(ISTGT_Ptr istgt) |
| 287 |
{ |
| 288 |
int i; |
| 289 |
|
| 290 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_close_portal\n"); |
| 291 |
for (i = 0; i < istgt->nportal; i++) { |
| 292 |
if (istgt->portal[i].sock >= 0) { |
| 293 |
ISTGT_TRACELOG(ISTGT_TRACE_NET, |
| 294 |
"close host %s, port %s, tag %d\n", |
| 295 |
istgt->portal[i].host, istgt->portal[i].port, |
| 296 |
istgt->portal[i].tag); |
| 297 |
close(istgt->portal[i].sock); |
| 298 |
istgt->portal[i].sock = -1; |
| 299 |
} |
| 300 |
} |
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
static int |
| 305 |
istgt_add_initiator_group(ISTGT_Ptr istgt, CF_SECTION *sp) |
| 306 |
{ |
| 307 |
const char *val; |
| 308 |
int alloc_len; |
| 309 |
int idx; |
| 310 |
int names; |
| 311 |
int masks; |
| 312 |
int i; |
| 313 |
|
| 314 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "add initiator group %d\n", sp->num); |
| 315 |
|
| 316 |
val = istgt_get_val(sp, "Comment"); |
| 317 |
if (val != NULL) { |
| 318 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "Comment %s\n", val); |
| 319 |
} |
| 320 |
|
| 321 |
/* counts number of definition */ |
| 322 |
for (i = 0; ; i++) { |
| 323 |
val = istgt_get_nval(sp, "InitiatorName", i); |
| 324 |
if (val == NULL) |
| 325 |
break; |
| 326 |
} |
| 327 |
names = i; |
| 328 |
if (names > MAX_INITIATOR) { |
| 329 |
ISTGT_ERRLOG("%d > MAX_INITIATOR\n", names); |
| 330 |
return -1; |
| 331 |
} |
| 332 |
for (i = 0; ; i++) { |
| 333 |
val = istgt_get_nval(sp, "Netmask", i); |
| 334 |
if (val == NULL) |
| 335 |
break; |
| 336 |
} |
| 337 |
masks = i; |
| 338 |
if (masks > MAX_NETMASK) { |
| 339 |
ISTGT_ERRLOG("%d > MAX_NETMASK\n", masks); |
| 340 |
return -1; |
| 341 |
} |
| 342 |
|
| 343 |
idx = istgt->ninitiator_group; |
| 344 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 345 |
"Index=%d, Tag=%d, Names=%d, Masks=%d\n", |
| 346 |
idx, sp->num, names, masks); |
| 347 |
if (idx < MAX_INITIATOR_GROUP) { |
| 348 |
istgt->initiator_group[idx].ninitiators = names; |
| 349 |
alloc_len = sizeof (char *) * names; |
| 350 |
istgt->initiator_group[idx].initiators = xmalloc(alloc_len); |
| 351 |
istgt->initiator_group[idx].nnetmasks = masks; |
| 352 |
alloc_len = sizeof (char *) * masks; |
| 353 |
istgt->initiator_group[idx].netmasks = xmalloc(alloc_len); |
| 354 |
istgt->initiator_group[idx].idx = idx; |
| 355 |
istgt->initiator_group[idx].tag = sp->num; |
| 356 |
|
| 357 |
for (i = 0; i < names; i++) { |
| 358 |
val = istgt_get_nval(sp, "InitiatorName", i); |
| 359 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 360 |
"InitiatorName %s\n", val); |
| 361 |
istgt->initiator_group[idx].initiators[i] = xstrdup(val); |
| 362 |
} |
| 363 |
for (i = 0; i < masks; i++) { |
| 364 |
val = istgt_get_nval(sp, "Netmask", i); |
| 365 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "Netmask %s\n", val); |
| 366 |
istgt->initiator_group[idx].netmasks[i] = xstrdup(val); |
| 367 |
} |
| 368 |
|
| 369 |
idx++; |
| 370 |
istgt->ninitiator_group = idx; |
| 371 |
} else { |
| 372 |
ISTGT_ERRLOG("ninitiator_group(%d) >= MAX_INITIATOR_GROUP\n", idx); |
| 373 |
return -1; |
| 374 |
} |
| 375 |
return 0; |
| 376 |
} |
| 377 |
|
| 378 |
static int |
| 379 |
istgt_build_initiator_group_array(ISTGT_Ptr istgt) |
| 380 |
{ |
| 381 |
CF_SECTION *sp; |
| 382 |
int rc; |
| 383 |
|
| 384 |
sp = istgt->config->section; |
| 385 |
while (sp != NULL) { |
| 386 |
if (sp->type == ST_INITIATORGROUP) { |
| 387 |
if (sp->num == 0) { |
| 388 |
ISTGT_ERRLOG("Group 0 is invalid\n"); |
| 389 |
return -1; |
| 390 |
} |
| 391 |
rc = istgt_add_initiator_group(istgt, sp); |
| 392 |
if (rc < 0) { |
| 393 |
ISTGT_ERRLOG("add_initiator_group() failed\n"); |
| 394 |
return -1; |
| 395 |
} |
| 396 |
} |
| 397 |
sp = sp->next; |
| 398 |
} |
| 399 |
return 0; |
| 400 |
} |
| 401 |
|
| 402 |
static void |
| 403 |
istgt_destory_initiator_group_array(ISTGT_Ptr istgt) |
| 404 |
{ |
| 405 |
int i, j; |
| 406 |
|
| 407 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_destory_initiator_group_array\n"); |
| 408 |
for (i = 0; i < istgt->ninitiator_group; i++) { |
| 409 |
for (j = 0; j < istgt->initiator_group[i].ninitiators; j++) { |
| 410 |
xfree(istgt->initiator_group[i].initiators[j]); |
| 411 |
} |
| 412 |
xfree(istgt->initiator_group[i].initiators); |
| 413 |
for (j = 0; j < istgt->initiator_group[i].nnetmasks; j++) { |
| 414 |
xfree(istgt->initiator_group[i].netmasks[j]); |
| 415 |
} |
| 416 |
xfree(istgt->initiator_group[i].netmasks); |
| 417 |
|
| 418 |
istgt->initiator_group[i].ninitiators = 0; |
| 419 |
istgt->initiator_group[i].initiators = NULL; |
| 420 |
istgt->initiator_group[i].nnetmasks = 0; |
| 421 |
istgt->initiator_group[i].netmasks = NULL; |
| 422 |
istgt->initiator_group[i].idx = i; |
| 423 |
istgt->initiator_group[i].tag = 0; |
| 424 |
} |
| 425 |
istgt->ninitiator_group = 0; |
| 426 |
} |
| 427 |
|
| 428 |
static int |
| 429 |
istgt_build_uctl_portal(ISTGT_Ptr istgt) |
| 430 |
{ |
| 431 |
CF_SECTION *sp; |
| 432 |
const char *val; |
| 433 |
char *label, *portal, *host, *port; |
| 434 |
int tag; |
| 435 |
int idx; |
| 436 |
int rc; |
| 437 |
int i; |
| 438 |
|
| 439 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_build_uctl_portal\n"); |
| 440 |
|
| 441 |
sp = istgt_find_cf_section(istgt->config, "UnitControl"); |
| 442 |
if (sp == NULL) { |
| 443 |
ISTGT_ERRLOG("find_cf_section failed()\n"); |
| 444 |
return -1; |
| 445 |
} |
| 446 |
|
| 447 |
for (i = 0; ; i++) { |
| 448 |
val = istgt_get_nval(sp, "Portal", i); |
| 449 |
if (val == NULL) |
| 450 |
break; |
| 451 |
|
| 452 |
label = istgt_get_nmval(sp, "Portal", i, 0); |
| 453 |
portal = istgt_get_nmval(sp, "Portal", i, 1); |
| 454 |
if (label == NULL || portal == NULL) { |
| 455 |
ISTGT_ERRLOG("uctl portal error\n"); |
| 456 |
return -1; |
| 457 |
} |
| 458 |
|
| 459 |
rc = istgt_parse_portal(portal, &host, &port); |
| 460 |
if (rc < 0) { |
| 461 |
ISTGT_ERRLOG("parse uctl portal error\n"); |
| 462 |
return -1; |
| 463 |
} |
| 464 |
|
| 465 |
idx = istgt->nuctl_portal; |
| 466 |
tag = ISTGT_UC_TAG; |
| 467 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 468 |
"Index=%d, Host=%s, Port=%s, Tag=%d\n", |
| 469 |
idx, host, port, tag); |
| 470 |
if (idx < MAX_UCPORTAL) { |
| 471 |
istgt->uctl_portal[idx].label = xstrdup(label); |
| 472 |
istgt->uctl_portal[idx].host = host; |
| 473 |
istgt->uctl_portal[idx].port = port; |
| 474 |
istgt->uctl_portal[idx].idx = idx; |
| 475 |
istgt->uctl_portal[idx].tag = tag; |
| 476 |
istgt->uctl_portal[idx].sock = -1; |
| 477 |
idx++; |
| 478 |
istgt->nuctl_portal = idx; |
| 479 |
} else { |
| 480 |
ISTGT_ERRLOG("nportal(%d) >= MAX_UCPORTAL\n", idx); |
| 481 |
xfree(host); |
| 482 |
xfree(port); |
| 483 |
return -1; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
return 0; |
| 488 |
} |
| 489 |
|
| 490 |
static void |
| 491 |
istgt_destroy_uctl_portal(ISTGT_Ptr istgt) |
| 492 |
{ |
| 493 |
int i; |
| 494 |
|
| 495 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_destroy_uctl_portal\n"); |
| 496 |
for (i = 0; i < istgt->nportal; i++) { |
| 497 |
xfree(istgt->uctl_portal[i].label); |
| 498 |
xfree(istgt->uctl_portal[i].host); |
| 499 |
xfree(istgt->uctl_portal[i].port); |
| 500 |
|
| 501 |
istgt->uctl_portal[i].label = NULL; |
| 502 |
istgt->uctl_portal[i].host = NULL; |
| 503 |
istgt->uctl_portal[i].port = NULL; |
| 504 |
istgt->uctl_portal[i].idx = i; |
| 505 |
istgt->uctl_portal[i].tag = 0; |
| 506 |
} |
| 507 |
istgt->nuctl_portal = 0; |
| 508 |
} |
| 509 |
|
| 510 |
static int |
| 511 |
istgt_open_uctl_portal(ISTGT_Ptr istgt) |
| 512 |
{ |
| 513 |
int port; |
| 514 |
int sock; |
| 515 |
int i; |
| 516 |
|
| 517 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_open_uctl_portal\n"); |
| 518 |
for (i = 0; i < istgt->nuctl_portal; i++) { |
| 519 |
if (istgt->uctl_portal[i].sock < 0) { |
| 520 |
ISTGT_TRACELOG(ISTGT_TRACE_NET, |
| 521 |
"open host %s, port %s, tag %d\n", |
| 522 |
istgt->uctl_portal[i].host, |
| 523 |
istgt->uctl_portal[i].port, |
| 524 |
istgt->uctl_portal[i].tag); |
| 525 |
port = (int)strtol(istgt->uctl_portal[i].port, NULL, 0); |
| 526 |
sock = istgt_listen(istgt->uctl_portal[i].host, port); |
| 527 |
if (sock < 0) { |
| 528 |
ISTGT_ERRLOG("listen error %.64s:%d\n", |
| 529 |
istgt->uctl_portal[i].host, port); |
| 530 |
return -1; |
| 531 |
} |
| 532 |
istgt->uctl_portal[i].sock = sock; |
| 533 |
} |
| 534 |
} |
| 535 |
return 0; |
| 536 |
} |
| 537 |
|
| 538 |
static int |
| 539 |
istgt_close_uctl_portal(ISTGT_Ptr istgt) |
| 540 |
{ |
| 541 |
int i; |
| 542 |
|
| 543 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_close_uctl_portal\n"); |
| 544 |
for (i = 0; i < istgt->nuctl_portal; i++) { |
| 545 |
if (istgt->uctl_portal[i].sock >= 0) { |
| 546 |
ISTGT_TRACELOG(ISTGT_TRACE_NET, |
| 547 |
"close host %s, port %s, tag %d\n", |
| 548 |
istgt->uctl_portal[i].host, |
| 549 |
istgt->uctl_portal[i].port, |
| 550 |
istgt->uctl_portal[i].tag); |
| 551 |
close(istgt->uctl_portal[i].sock); |
| 552 |
istgt->uctl_portal[i].sock = -1; |
| 553 |
} |
| 554 |
} |
| 555 |
return 0; |
| 556 |
} |
| 557 |
|
| 558 |
static int |
| 559 |
istgt_write_pidfile(ISTGT_Ptr istgt) |
| 560 |
{ |
| 561 |
FILE *fp; |
| 562 |
pid_t pid; |
| 563 |
int rc; |
| 564 |
|
| 565 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_write_pidfile\n"); |
| 566 |
rc = remove(istgt->pidfile); |
| 567 |
if (rc != 0) { |
| 568 |
if (errno != ENOENT) { |
| 569 |
ISTGT_ERRLOG("pidfile remove error %d\n", errno); |
| 570 |
return -1; |
| 571 |
} |
| 572 |
} |
| 573 |
fp = fopen(istgt->pidfile, "w"); |
| 574 |
if (fp == NULL) { |
| 575 |
ISTGT_ERRLOG("pidfile open error %d\n", errno); |
| 576 |
return -1; |
| 577 |
} |
| 578 |
pid = getpid(); |
| 579 |
fprintf(fp, "%d\n", (int)pid); |
| 580 |
fclose(fp); |
| 581 |
return 0; |
| 582 |
} |
| 583 |
|
| 584 |
static void |
| 585 |
istgt_remove_pidfile(ISTGT_Ptr istgt) |
| 586 |
{ |
| 587 |
int rc; |
| 588 |
|
| 589 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_remove_pidfile\n"); |
| 590 |
rc = remove(istgt->pidfile); |
| 591 |
if (rc != 0) { |
| 592 |
ISTGT_ERRLOG("pidfile remove error %d\n", errno); |
| 593 |
/* ignore error */ |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
char * |
| 598 |
istgt_get_nmval(CF_SECTION *sp, const char *key, int idx1, int idx2) |
| 599 |
{ |
| 600 |
CF_ITEM *ip; |
| 601 |
CF_VALUE *vp; |
| 602 |
int i; |
| 603 |
|
| 604 |
ip = istgt_find_cf_nitem(sp, key, idx1); |
| 605 |
if (ip == NULL) |
| 606 |
return NULL; |
| 607 |
vp = ip->val; |
| 608 |
if (vp == NULL) |
| 609 |
return NULL; |
| 610 |
for (i = 0; vp != NULL; vp = vp->next) { |
| 611 |
if (i == idx2) |
| 612 |
return vp->value; |
| 613 |
i++; |
| 614 |
} |
| 615 |
return NULL; |
| 616 |
} |
| 617 |
|
| 618 |
char * |
| 619 |
istgt_get_nval(CF_SECTION *sp, const char *key, int idx) |
| 620 |
{ |
| 621 |
CF_ITEM *ip; |
| 622 |
CF_VALUE *vp; |
| 623 |
|
| 624 |
ip = istgt_find_cf_nitem(sp, key, idx); |
| 625 |
if (ip == NULL) |
| 626 |
return NULL; |
| 627 |
vp = ip->val; |
| 628 |
if (vp == NULL) |
| 629 |
return NULL; |
| 630 |
return vp->value; |
| 631 |
} |
| 632 |
|
| 633 |
char * |
| 634 |
istgt_get_val(CF_SECTION *sp, const char *key) |
| 635 |
{ |
| 636 |
return istgt_get_nval(sp, key, 0); |
| 637 |
} |
| 638 |
|
| 639 |
int |
| 640 |
istgt_get_nintval(CF_SECTION *sp, const char *key, int idx) |
| 641 |
{ |
| 642 |
const char *v; |
| 643 |
int value; |
| 644 |
|
| 645 |
v = istgt_get_nval(sp, key, idx); |
| 646 |
if (v == NULL) |
| 647 |
return -1; |
| 648 |
value = (int)strtol(v, NULL, 10); |
| 649 |
return value; |
| 650 |
} |
| 651 |
|
| 652 |
int |
| 653 |
istgt_get_intval(CF_SECTION *sp, const char *key) |
| 654 |
{ |
| 655 |
return istgt_get_nintval(sp, key, 0); |
| 656 |
} |
| 657 |
|
| 658 |
static const char * |
| 659 |
istgt_get_log_facility(CONFIG *config) |
| 660 |
{ |
| 661 |
CF_SECTION *sp; |
| 662 |
const char *logfacility; |
| 663 |
|
| 664 |
sp = istgt_find_cf_section(config, "Global"); |
| 665 |
if (sp == NULL) { |
| 666 |
return NULL; |
| 667 |
} |
| 668 |
logfacility = istgt_get_val(sp, "LogFacility"); |
| 669 |
if (logfacility == NULL) { |
| 670 |
logfacility = DEFAULT_LOG_FACILITY; |
| 671 |
} |
| 672 |
#if 0 |
| 673 |
if (g_trace_flag & ISTGT_TRACE_DEBUG) { |
| 674 |
fprintf(stderr, "LogFacility %s\n", logfacility); |
| 675 |
} |
| 676 |
#endif |
| 677 |
|
| 678 |
return logfacility; |
| 679 |
} |
| 680 |
|
| 681 |
static int |
| 682 |
istgt_init(ISTGT_Ptr istgt) |
| 683 |
{ |
| 684 |
CF_SECTION *sp; |
| 685 |
const char *ag_tag; |
| 686 |
const char *val; |
| 687 |
size_t stacksize; |
| 688 |
int ag_tag_i; |
| 689 |
int MaxSessions; |
| 690 |
int MaxConnections; |
| 691 |
int MaxOutstandingR2T; |
| 692 |
int DefaultTime2Wait; |
| 693 |
int DefaultTime2Retain; |
| 694 |
int FirstBurstLength; |
| 695 |
int MaxBurstLength; |
| 696 |
int MaxRecvDataSegmentLength; |
| 697 |
int InitialR2T; |
| 698 |
int ImmediateData; |
| 699 |
int DataPDUInOrder; |
| 700 |
int DataSequenceInOrder; |
| 701 |
int ErrorRecoveryLevel; |
| 702 |
int timeout; |
| 703 |
int nopininterval; |
| 704 |
int maxr2t; |
| 705 |
int rc; |
| 706 |
int i; |
| 707 |
|
| 708 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_init\n"); |
| 709 |
sp = istgt_find_cf_section(istgt->config, "Global"); |
| 710 |
if (sp == NULL) { |
| 711 |
ISTGT_ERRLOG("find_cf_section failed()\n"); |
| 712 |
return -1; |
| 713 |
} |
| 714 |
|
| 715 |
val = istgt_get_val(sp, "Comment"); |
| 716 |
if (val != NULL) { |
| 717 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "Comment %s\n", val); |
| 718 |
} |
| 719 |
|
| 720 |
val = istgt_get_val(sp, "PidFile"); |
| 721 |
if (val == NULL) { |
| 722 |
val = DEFAULT_PIDFILE; |
| 723 |
} |
| 724 |
istgt->pidfile = xstrdup(val); |
| 725 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "PidFile %s\n", |
| 726 |
istgt->pidfile); |
| 727 |
|
| 728 |
val = istgt_get_val(sp, "AuthFile"); |
| 729 |
if (val == NULL) { |
| 730 |
val = DEFAULT_AUTHFILE; |
| 731 |
} |
| 732 |
istgt->authfile = xstrdup(val); |
| 733 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "AuthFile %s\n", |
| 734 |
istgt->authfile); |
| 735 |
|
| 736 |
#if 0 |
| 737 |
val = istgt_get_val(sp, "MediaFile"); |
| 738 |
if (val == NULL) { |
| 739 |
val = DEFAULT_MEDIAFILE; |
| 740 |
} |
| 741 |
istgt->mediafile = xstrdup(val); |
| 742 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "MediaFile %s\n", |
| 743 |
istgt->mediafile); |
| 744 |
#endif |
| 745 |
|
| 746 |
#if 0 |
| 747 |
val = istgt_get_val(sp, "LiveFile"); |
| 748 |
if (val == NULL) { |
| 749 |
val = DEFAULT_LIVEFILE; |
| 750 |
} |
| 751 |
istgt->livefile = xstrdup(val); |
| 752 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "LiveFile %s\n", |
| 753 |
istgt->livefile); |
| 754 |
#endif |
| 755 |
|
| 756 |
val = istgt_get_val(sp, "MediaDirectory"); |
| 757 |
if (val == NULL) { |
| 758 |
val = DEFAULT_MEDIADIRECTORY; |
| 759 |
} |
| 760 |
istgt->mediadirectory = xstrdup(val); |
| 761 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "MediaDirectory %s\n", |
| 762 |
istgt->mediadirectory); |
| 763 |
|
| 764 |
val = istgt_get_val(sp, "NodeBase"); |
| 765 |
if (val == NULL) { |
| 766 |
val = DEFAULT_NODEBASE; |
| 767 |
} |
| 768 |
istgt->nodebase = xstrdup(val); |
| 769 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "NodeBase %s\n", |
| 770 |
istgt->nodebase); |
| 771 |
|
| 772 |
MaxSessions = istgt_get_intval(sp, "MaxSessions"); |
| 773 |
if (MaxSessions < 1) { |
| 774 |
MaxSessions = DEFAULT_MAX_SESSIONS; |
| 775 |
} |
| 776 |
istgt->MaxSessions = MaxSessions; |
| 777 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "MaxSessions %d\n", |
| 778 |
istgt->MaxSessions); |
| 779 |
|
| 780 |
MaxConnections = istgt_get_intval(sp, "MaxConnections"); |
| 781 |
if (MaxConnections < 1) { |
| 782 |
MaxConnections = DEFAULT_MAX_CONNECTIONS; |
| 783 |
} |
| 784 |
istgt->MaxConnections = MaxConnections; |
| 785 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "MaxConnections %d\n", |
| 786 |
istgt->MaxConnections); |
| 787 |
|
| 788 |
/* limited to 16bits - RFC3720(12.2) */ |
| 789 |
if (MaxSessions > 0xffff) { |
| 790 |
ISTGT_ERRLOG("over 65535 sessions are not supported\n"); |
| 791 |
return -1; |
| 792 |
} |
| 793 |
if (MaxConnections > 0xffff) { |
| 794 |
ISTGT_ERRLOG("over 65535 connections are not supported\n"); |
| 795 |
return -1; |
| 796 |
} |
| 797 |
|
| 798 |
MaxOutstandingR2T = istgt_get_intval(sp, "MaxOutstandingR2T"); |
| 799 |
if (MaxOutstandingR2T < 1) { |
| 800 |
MaxOutstandingR2T = DEFAULT_MAXOUTSTANDINGR2T; |
| 801 |
} |
| 802 |
istgt->MaxOutstandingR2T = MaxOutstandingR2T; |
| 803 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "MaxOutstandingR2T %d\n", |
| 804 |
istgt->MaxOutstandingR2T); |
| 805 |
|
| 806 |
DefaultTime2Wait = istgt_get_intval(sp, "DefaultTime2Wait"); |
| 807 |
if (DefaultTime2Wait < 0) { |
| 808 |
DefaultTime2Wait = DEFAULT_DEFAULTTIME2WAIT; |
| 809 |
} |
| 810 |
istgt->DefaultTime2Wait = DefaultTime2Wait; |
| 811 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "DefaultTime2Wait %d\n", |
| 812 |
istgt->DefaultTime2Wait); |
| 813 |
|
| 814 |
DefaultTime2Retain = istgt_get_intval(sp, "DefaultTime2Retain"); |
| 815 |
if (DefaultTime2Retain < 0) { |
| 816 |
DefaultTime2Retain = DEFAULT_DEFAULTTIME2RETAIN; |
| 817 |
} |
| 818 |
istgt->DefaultTime2Retain = DefaultTime2Retain; |
| 819 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "DefaultTime2Retain %d\n", |
| 820 |
istgt->DefaultTime2Retain); |
| 821 |
|
| 822 |
/* check size limit - RFC3720(12.15, 12.16, 12.17) */ |
| 823 |
if (istgt->MaxOutstandingR2T > 65535) { |
| 824 |
ISTGT_ERRLOG("MaxOutstandingR2T(%d) > 65535\n", |
| 825 |
istgt->MaxOutstandingR2T); |
| 826 |
return -1; |
| 827 |
} |
| 828 |
if (istgt->DefaultTime2Wait > 3600) { |
| 829 |
ISTGT_ERRLOG("DefaultTime2Wait(%d) > 3600\n", |
| 830 |
istgt->DefaultTime2Wait); |
| 831 |
return -1; |
| 832 |
} |
| 833 |
if (istgt->DefaultTime2Retain > 3600) { |
| 834 |
ISTGT_ERRLOG("DefaultTime2Retain(%d) > 3600\n", |
| 835 |
istgt->DefaultTime2Retain); |
| 836 |
return -1; |
| 837 |
} |
| 838 |
|
| 839 |
FirstBurstLength = istgt_get_intval(sp, "FirstBurstLength"); |
| 840 |
if (FirstBurstLength < 0) { |
| 841 |
FirstBurstLength = DEFAULT_FIRSTBURSTLENGTH; |
| 842 |
} |
| 843 |
istgt->FirstBurstLength = FirstBurstLength; |
| 844 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "FirstBurstLength %d\n", |
| 845 |
istgt->FirstBurstLength); |
| 846 |
|
| 847 |
MaxBurstLength = istgt_get_intval(sp, "MaxBurstLength"); |
| 848 |
if (MaxBurstLength < 0) { |
| 849 |
MaxBurstLength = DEFAULT_MAXBURSTLENGTH; |
| 850 |
} |
| 851 |
istgt->MaxBurstLength = MaxBurstLength; |
| 852 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "MaxBurstLength %d\n", |
| 853 |
istgt->MaxBurstLength); |
| 854 |
|
| 855 |
MaxRecvDataSegmentLength |
| 856 |
= istgt_get_intval(sp, "MaxRecvDataSegmentLength"); |
| 857 |
if (MaxRecvDataSegmentLength < 0) { |
| 858 |
MaxRecvDataSegmentLength = DEFAULT_MAXRECVDATASEGMENTLENGTH; |
| 859 |
} |
| 860 |
istgt->MaxRecvDataSegmentLength = MaxRecvDataSegmentLength; |
| 861 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "MaxRecvDataSegmentLength %d\n", |
| 862 |
istgt->MaxRecvDataSegmentLength); |
| 863 |
|
| 864 |
/* check size limit (up to 24bits - RFC3720(12.12)) */ |
| 865 |
if (istgt->MaxBurstLength < 512) { |
| 866 |
ISTGT_ERRLOG("MaxBurstLength(%d) < 512\n", |
| 867 |
istgt->MaxBurstLength); |
| 868 |
return -1; |
| 869 |
} |
| 870 |
if (istgt->FirstBurstLength < 512) { |
| 871 |
ISTGT_ERRLOG("FirstBurstLength(%d) < 512\n", |
| 872 |
istgt->FirstBurstLength); |
| 873 |
return -1; |
| 874 |
} |
| 875 |
if (istgt->FirstBurstLength > istgt->MaxBurstLength) { |
| 876 |
ISTGT_ERRLOG("FirstBurstLength(%d) > MaxBurstLength(%d)\n", |
| 877 |
istgt->FirstBurstLength, istgt->MaxBurstLength); |
| 878 |
return -1; |
| 879 |
} |
| 880 |
if (istgt->MaxBurstLength > 0x00ffffff) { |
| 881 |
ISTGT_ERRLOG("MaxBurstLength(%d) > 0x00ffffff\n", |
| 882 |
istgt->MaxBurstLength); |
| 883 |
return -1; |
| 884 |
} |
| 885 |
if (istgt->MaxRecvDataSegmentLength < 512) { |
| 886 |
ISTGT_ERRLOG("MaxRecvDataSegmentLength(%d) < 512\n", |
| 887 |
istgt->MaxRecvDataSegmentLength); |
| 888 |
return -1; |
| 889 |
} |
| 890 |
if (istgt->MaxRecvDataSegmentLength > 0x00ffffff) { |
| 891 |
ISTGT_ERRLOG("MaxRecvDataSegmentLength(%d) > 0x00ffffff\n", |
| 892 |
istgt->MaxRecvDataSegmentLength); |
| 893 |
return -1; |
| 894 |
} |
| 895 |
|
| 896 |
val = istgt_get_val(sp, "InitialR2T"); |
| 897 |
if (val == NULL) { |
| 898 |
InitialR2T = DEFAULT_INITIALR2T; |
| 899 |
} else if (strcasecmp(val, "Yes") == 0) { |
| 900 |
InitialR2T = 1; |
| 901 |
} else if (strcasecmp(val, "No") == 0) { |
| 902 |
#if 0 |
| 903 |
InitialR2T = 0; |
| 904 |
#else |
| 905 |
ISTGT_ERRLOG("not supported value %s\n", val); |
| 906 |
return -1; |
| 907 |
#endif |
| 908 |
} else { |
| 909 |
ISTGT_ERRLOG("unknown value %s\n", val); |
| 910 |
return -1; |
| 911 |
} |
| 912 |
istgt->InitialR2T = InitialR2T; |
| 913 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "InitialR2T %s\n", |
| 914 |
istgt->InitialR2T ? "Yes" : "No"); |
| 915 |
|
| 916 |
val = istgt_get_val(sp, "ImmediateData"); |
| 917 |
if (val == NULL) { |
| 918 |
ImmediateData = DEFAULT_IMMEDIATEDATA; |
| 919 |
} else if (strcasecmp(val, "Yes") == 0) { |
| 920 |
ImmediateData = 1; |
| 921 |
} else if (strcasecmp(val, "No") == 0) { |
| 922 |
ImmediateData = 0; |
| 923 |
} else { |
| 924 |
ISTGT_ERRLOG("unknown value %s\n", val); |
| 925 |
return -1; |
| 926 |
} |
| 927 |
istgt->ImmediateData = ImmediateData; |
| 928 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "ImmediateData %s\n", |
| 929 |
istgt->ImmediateData ? "Yes" : "No"); |
| 930 |
|
| 931 |
val = istgt_get_val(sp, "DataPDUInOrder"); |
| 932 |
if (val == NULL) { |
| 933 |
DataPDUInOrder = DEFAULT_DATAPDUINORDER; |
| 934 |
} else if (strcasecmp(val, "Yes") == 0) { |
| 935 |
DataPDUInOrder = 1; |
| 936 |
} else if (strcasecmp(val, "No") == 0) { |
| 937 |
#if 0 |
| 938 |
DataPDUInOrder = 0; |
| 939 |
#else |
| 940 |
ISTGT_ERRLOG("not supported value %s\n", val); |
| 941 |
return -1; |
| 942 |
#endif |
| 943 |
} else { |
| 944 |
ISTGT_ERRLOG("unknown value %s\n", val); |
| 945 |
return -1; |
| 946 |
} |
| 947 |
istgt->DataPDUInOrder = DataPDUInOrder; |
| 948 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "DataPDUInOrder %s\n", |
| 949 |
istgt->DataPDUInOrder ? "Yes" : "No"); |
| 950 |
|
| 951 |
val = istgt_get_val(sp, "DataSequenceInOrder"); |
| 952 |
if (val == NULL) { |
| 953 |
DataSequenceInOrder = DEFAULT_DATASEQUENCEINORDER; |
| 954 |
} else if (strcasecmp(val, "Yes") == 0) { |
| 955 |
DataSequenceInOrder = 1; |
| 956 |
} else if (strcasecmp(val, "No") == 0) { |
| 957 |
#if 0 |
| 958 |
DataSequenceInOrder = 0; |
| 959 |
#else |
| 960 |
ISTGT_ERRLOG("not supported value %s\n", val); |
| 961 |
return -1; |
| 962 |
#endif |
| 963 |
} else { |
| 964 |
ISTGT_ERRLOG("unknown value %s\n", val); |
| 965 |
return -1; |
| 966 |
} |
| 967 |
istgt->DataSequenceInOrder = DataSequenceInOrder; |
| 968 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "DataSequenceInOrder %s\n", |
| 969 |
istgt->DataSequenceInOrder ? "Yes" : "No"); |
| 970 |
|
| 971 |
ErrorRecoveryLevel = istgt_get_intval(sp, "ErrorRecoveryLevel"); |
| 972 |
if (ErrorRecoveryLevel < 0) { |
| 973 |
ErrorRecoveryLevel = DEFAULT_ERRORRECOVERYLEVEL; |
| 974 |
} else if (ErrorRecoveryLevel == 0) { |
| 975 |
ErrorRecoveryLevel = 0; |
| 976 |
} else if (ErrorRecoveryLevel == 1) { |
| 977 |
#if 0 |
| 978 |
ErrorRecoveryLevel = 1; |
| 979 |
#else |
| 980 |
ISTGT_ERRLOG("not supported value %d\n", ErrorRecoveryLevel); |
| 981 |
return -1; |
| 982 |
#endif |
| 983 |
} else if (ErrorRecoveryLevel == 2) { |
| 984 |
#if 0 |
| 985 |
ErrorRecoveryLevel = 2; |
| 986 |
#else |
| 987 |
ISTGT_ERRLOG("not supported value %d\n", ErrorRecoveryLevel); |
| 988 |
return -1; |
| 989 |
#endif |
| 990 |
} else { |
| 991 |
ISTGT_ERRLOG("not supported value %d\n", ErrorRecoveryLevel); |
| 992 |
return -1; |
| 993 |
} |
| 994 |
istgt->ErrorRecoveryLevel = ErrorRecoveryLevel; |
| 995 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "ErrorRecoveryLevel %d\n", |
| 996 |
istgt->ErrorRecoveryLevel); |
| 997 |
|
| 998 |
timeout = istgt_get_intval(sp, "Timeout"); |
| 999 |
if (timeout < 0) { |
| 1000 |
timeout = DEFAULT_TIMEOUT; |
| 1001 |
} |
| 1002 |
istgt->timeout = timeout; |
| 1003 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "Timeout %d\n", |
| 1004 |
istgt->timeout); |
| 1005 |
|
| 1006 |
nopininterval = istgt_get_intval(sp, "NopInInterval"); |
| 1007 |
if (nopininterval < 0) { |
| 1008 |
nopininterval = DEFAULT_NOPININTERVAL; |
| 1009 |
} |
| 1010 |
istgt->nopininterval = nopininterval; |
| 1011 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "NopInInterval %d\n", |
| 1012 |
istgt->nopininterval); |
| 1013 |
|
| 1014 |
maxr2t = istgt_get_intval(sp, "MaxR2T"); |
| 1015 |
if (maxr2t < 0) { |
| 1016 |
maxr2t = DEFAULT_MAXR2T; |
| 1017 |
} |
| 1018 |
if (maxr2t > MAX_R2T) { |
| 1019 |
ISTGT_ERRLOG("MaxR2T(%d) > %d\n", |
| 1020 |
maxr2t, MAX_R2T); |
| 1021 |
return -1; |
| 1022 |
} |
| 1023 |
istgt->maxr2t = maxr2t; |
| 1024 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "MaxR2T %d\n", |
| 1025 |
istgt->maxr2t); |
| 1026 |
|
| 1027 |
val = istgt_get_val(sp, "DiscoveryAuthMethod"); |
| 1028 |
if (val == NULL) { |
| 1029 |
istgt->no_discovery_auth = 0; |
| 1030 |
istgt->req_discovery_auth = 0; |
| 1031 |
istgt->req_discovery_auth_mutual = 0; |
| 1032 |
} else { |
| 1033 |
istgt->no_discovery_auth = 0; |
| 1034 |
for (i = 0; ; i++) { |
| 1035 |
val = istgt_get_nmval(sp, "DiscoveryAuthMethod", 0, i); |
| 1036 |
if (val == NULL) |
| 1037 |
break; |
| 1038 |
if (strcasecmp(val, "CHAP") == 0) { |
| 1039 |
istgt->req_discovery_auth = 1; |
| 1040 |
} else if (strcasecmp(val, "Mutual") == 0) { |
| 1041 |
istgt->req_discovery_auth_mutual = 1; |
| 1042 |
} else if (strcasecmp(val, "Auto") == 0) { |
| 1043 |
istgt->req_discovery_auth = 0; |
| 1044 |
istgt->req_discovery_auth_mutual = 0; |
| 1045 |
} else if (strcasecmp(val, "None") == 0) { |
| 1046 |
istgt->no_discovery_auth = 1; |
| 1047 |
istgt->req_discovery_auth = 0; |
| 1048 |
istgt->req_discovery_auth_mutual = 0; |
| 1049 |
} else { |
| 1050 |
ISTGT_ERRLOG("unknown auth\n"); |
| 1051 |
return -1; |
| 1052 |
} |
| 1053 |
} |
| 1054 |
if (istgt->req_discovery_auth_mutual && !istgt->req_discovery_auth) { |
| 1055 |
ISTGT_ERRLOG("Mutual but not CHAP\n"); |
| 1056 |
return -1; |
| 1057 |
} |
| 1058 |
} |
| 1059 |
if (istgt->no_discovery_auth != 0) { |
| 1060 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 1061 |
"DiscoveryAuthMethod None\n"); |
| 1062 |
} else if (istgt->req_discovery_auth == 0) { |
| 1063 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 1064 |
"DiscoveryAuthMethod Auto\n"); |
| 1065 |
} else { |
| 1066 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 1067 |
"DiscoveryAuthMethod %s %s\n", |
| 1068 |
istgt->req_discovery_auth ? "CHAP" : "", |
| 1069 |
istgt->req_discovery_auth_mutual ? "Mutual" : ""); |
| 1070 |
} |
| 1071 |
|
| 1072 |
val = istgt_get_val(sp, "DiscoveryAuthGroup"); |
| 1073 |
if (val == NULL) { |
| 1074 |
istgt->discovery_auth_group = 0; |
| 1075 |
} else { |
| 1076 |
ag_tag = val; |
| 1077 |
if (strcasecmp(ag_tag, "None") == 0) { |
| 1078 |
ag_tag_i = 0; |
| 1079 |
} else { |
| 1080 |
if (strncasecmp(ag_tag, "AuthGroup", |
| 1081 |
strlen("AuthGroup")) != 0 |
| 1082 |
|| sscanf(ag_tag, "%*[^0-9]%d", &ag_tag_i) != 1) { |
| 1083 |
ISTGT_ERRLOG("auth group error\n"); |
| 1084 |
return -1; |
| 1085 |
} |
| 1086 |
if (ag_tag_i == 0) { |
| 1087 |
ISTGT_ERRLOG("invalid auth group %d\n", |
| 1088 |
ag_tag_i); |
| 1089 |
return -1; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
istgt->discovery_auth_group = ag_tag_i; |
| 1093 |
} |
| 1094 |
if (istgt->discovery_auth_group == 0) { |
| 1095 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 1096 |
"DiscoveryAuthGroup None\n"); |
| 1097 |
} else { |
| 1098 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 1099 |
"DiscoveryAuthGroup AuthGroup%d\n", |
| 1100 |
istgt->discovery_auth_group); |
| 1101 |
} |
| 1102 |
|
| 1103 |
rc = istgt_init_uctl(istgt); |
| 1104 |
if (rc < 0) { |
| 1105 |
ISTGT_ERRLOG("istgt_init_uctl() failed\n"); |
| 1106 |
return -1; |
| 1107 |
} |
| 1108 |
rc = istgt_build_uctl_portal(istgt); |
| 1109 |
if (rc < 0) { |
| 1110 |
ISTGT_ERRLOG("istgt_build_uctl_portal() failed\n"); |
| 1111 |
return -1; |
| 1112 |
} |
| 1113 |
rc = istgt_build_portal_array(istgt); |
| 1114 |
if (rc < 0) { |
| 1115 |
ISTGT_ERRLOG("istgt_build_portal_array() failed\n"); |
| 1116 |
return -1; |
| 1117 |
} |
| 1118 |
rc = istgt_build_initiator_group_array(istgt); |
| 1119 |
if (rc < 0) { |
| 1120 |
ISTGT_ERRLOG("build_initiator_group_array() failed\n"); |
| 1121 |
return -1; |
| 1122 |
} |
| 1123 |
|
| 1124 |
rc = pthread_attr_init(&istgt->attr); |
| 1125 |
if (rc != 0) { |
| 1126 |
ISTGT_ERRLOG("pthread_attr_init() failed\n"); |
| 1127 |
return -1; |
| 1128 |
} |
| 1129 |
rc = pthread_attr_getstacksize(&istgt->attr, &stacksize); |
| 1130 |
if (rc != 0) { |
| 1131 |
ISTGT_ERRLOG("pthread_attr_getstacksize() failed\n"); |
| 1132 |
return -1; |
| 1133 |
} |
| 1134 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "current thread stack = %zd\n", stacksize); |
| 1135 |
if (stacksize < ISTGT_STACKSIZE) { |
| 1136 |
stacksize = ISTGT_STACKSIZE; |
| 1137 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "new thread stack = %zd\n", stacksize); |
| 1138 |
rc = pthread_attr_setstacksize(&istgt->attr, stacksize); |
| 1139 |
if (rc != 0) { |
| 1140 |
ISTGT_ERRLOG("pthread_attr_setstacksize() failed\n"); |
| 1141 |
return -1; |
| 1142 |
} |
| 1143 |
} |
| 1144 |
|
| 1145 |
rc = pthread_mutex_init(&istgt->mutex, NULL); |
| 1146 |
if (rc != 0) { |
| 1147 |
ISTGT_ERRLOG("mutex_init() failed\n"); |
| 1148 |
return -1; |
| 1149 |
} |
| 1150 |
|
| 1151 |
/* XXX TODO: add initializer */ |
| 1152 |
|
| 1153 |
istgt_set_state(istgt, ISTGT_STATE_INITIALIZED); |
| 1154 |
|
| 1155 |
return 0; |
| 1156 |
} |
| 1157 |
|
| 1158 |
#ifdef SIGINFO |
| 1159 |
static void |
| 1160 |
istgt_siginfo(int signo) |
| 1161 |
{ |
| 1162 |
/* nothing */ |
| 1163 |
} |
| 1164 |
#endif |
| 1165 |
|
| 1166 |
static void |
| 1167 |
istgt_sigwakeup(int signo) |
| 1168 |
{ |
| 1169 |
} |
| 1170 |
|
| 1171 |
#ifdef SIGIO |
| 1172 |
static void |
| 1173 |
istgt_sigio(int signo) |
| 1174 |
{ |
| 1175 |
} |
| 1176 |
#endif |
| 1177 |
|
| 1178 |
static void * |
| 1179 |
istgt_sighandler(void *arg) |
| 1180 |
{ |
| 1181 |
ISTGT_Ptr istgt = (ISTGT_Ptr) arg; |
| 1182 |
sigset_t signew; |
| 1183 |
int signo; |
| 1184 |
|
| 1185 |
sigemptyset(&signew); |
| 1186 |
sigaddset(&signew, SIGINT); |
| 1187 |
sigaddset(&signew, SIGTERM); |
| 1188 |
sigaddset(&signew, SIGQUIT); |
| 1189 |
sigaddset(&signew, SIGHUP); |
| 1190 |
#ifdef SIGINFO |
| 1191 |
sigaddset(&signew, SIGINFO); |
| 1192 |
#endif |
| 1193 |
sigaddset(&signew, SIGUSR1); |
| 1194 |
sigaddset(&signew, SIGUSR2); |
| 1195 |
#ifdef SIGIO |
| 1196 |
sigaddset(&signew, SIGIO); |
| 1197 |
#endif |
| 1198 |
|
| 1199 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "loop start\n"); |
| 1200 |
while (1) { |
| 1201 |
if (istgt_get_state(istgt) == ISTGT_STATE_EXITING |
| 1202 |
|| istgt_get_state(istgt) == ISTGT_STATE_SHUTDOWN) { |
| 1203 |
break; |
| 1204 |
} |
| 1205 |
sigwait(&signew, &signo); |
| 1206 |
switch (signo) { |
| 1207 |
case SIGINT: |
| 1208 |
printf("SIGINT catch\n"); |
| 1209 |
istgt_set_state(istgt, ISTGT_STATE_EXITING); |
| 1210 |
istgt_lu_set_all_state(istgt, ISTGT_STATE_EXITING); |
| 1211 |
break; |
| 1212 |
case SIGTERM: |
| 1213 |
printf("SIGTERM catch\n"); |
| 1214 |
istgt_set_state(istgt, ISTGT_STATE_EXITING); |
| 1215 |
istgt_lu_set_all_state(istgt, ISTGT_STATE_EXITING); |
| 1216 |
break; |
| 1217 |
case SIGQUIT: |
| 1218 |
printf("SIGQUIT catch\n"); |
| 1219 |
exit(EXIT_SUCCESS); |
| 1220 |
break; |
| 1221 |
case SIGHUP: |
| 1222 |
printf("SIGHUP catch\n"); |
| 1223 |
break; |
| 1224 |
#ifdef SIGINFO |
| 1225 |
case SIGINFO: |
| 1226 |
printf("SIGINFO catch\n"); |
| 1227 |
istgt_set_trace_flag(ISTGT_TRACE_ISCSI); |
| 1228 |
break; |
| 1229 |
#endif |
| 1230 |
case SIGUSR1: |
| 1231 |
printf("SIGUSR1 catch\n"); |
| 1232 |
istgt_set_trace_flag(ISTGT_TRACE_NONE); |
| 1233 |
break; |
| 1234 |
case SIGUSR2: |
| 1235 |
printf("SIGUSR2 catch\n"); |
| 1236 |
//istgt_set_trace_flag(ISTGT_TRACE_SCSI); |
| 1237 |
istgt_set_trace_flag(ISTGT_TRACE_ALL); |
| 1238 |
break; |
| 1239 |
#ifdef SIGIO |
| 1240 |
case SIGIO: |
| 1241 |
//printf("SIGIO catch\n"); |
| 1242 |
break; |
| 1243 |
#endif |
| 1244 |
default: |
| 1245 |
break; |
| 1246 |
} |
| 1247 |
} |
| 1248 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "loop ended\n"); |
| 1249 |
|
| 1250 |
return NULL; |
| 1251 |
} |
| 1252 |
|
| 1253 |
static int |
| 1254 |
istgt_acceptor(ISTGT_Ptr istgt) |
| 1255 |
{ |
| 1256 |
#ifdef ISTGT_USE_KQUEUE |
| 1257 |
int kq; |
| 1258 |
struct kevent kev; |
| 1259 |
struct timespec kev_timeout; |
| 1260 |
#else |
| 1261 |
struct pollfd fds[MAX_PORTAL + MAX_UCPORTAL]; |
| 1262 |
#endif /* ISTGT_USE_KQUEUE */ |
| 1263 |
struct sockaddr_storage sa; |
| 1264 |
socklen_t salen; |
| 1265 |
int sock; |
| 1266 |
int rc, n; |
| 1267 |
int ucidx; |
| 1268 |
int nidx; |
| 1269 |
int i; |
| 1270 |
|
| 1271 |
if (istgt_get_state(istgt) != ISTGT_STATE_INITIALIZED) { |
| 1272 |
ISTGT_ERRLOG("not initialized\n"); |
| 1273 |
return -1; |
| 1274 |
} |
| 1275 |
/* now running main thread */ |
| 1276 |
istgt_set_state(istgt, ISTGT_STATE_RUNNING); |
| 1277 |
|
| 1278 |
#ifdef ISTGT_USE_KQUEUE |
| 1279 |
kq = kqueue(); |
| 1280 |
if (kq == -1) { |
| 1281 |
ISTGT_ERRLOG("kqueue() failed\n"); |
| 1282 |
return -1; |
| 1283 |
} |
| 1284 |
for (i = 0; i < istgt->nportal; i++) { |
| 1285 |
EV_SET(&kev, istgt->portal[i].sock, |
| 1286 |
EVFILT_READ, EV_ADD, 0, 0, NULL); |
| 1287 |
rc = kevent(kq, &kev, 1, NULL, 0, NULL); |
| 1288 |
if (rc == -1) { |
| 1289 |
ISTGT_ERRLOG("kevent() failed\n"); |
| 1290 |
close(kq); |
| 1291 |
return -1; |
| 1292 |
} |
| 1293 |
} |
| 1294 |
ucidx = istgt->nportal; |
| 1295 |
for (i = 0; i < istgt->nuctl_portal; i++) { |
| 1296 |
EV_SET(&kev, istgt->uctl_portal[i].sock, |
| 1297 |
EVFILT_READ, EV_ADD, 0, 0, NULL); |
| 1298 |
rc = kevent(kq, &kev, 1, NULL, 0, NULL); |
| 1299 |
if (rc == -1) { |
| 1300 |
ISTGT_ERRLOG("kevent() failed\n"); |
| 1301 |
close(kq); |
| 1302 |
return -1; |
| 1303 |
} |
| 1304 |
} |
| 1305 |
nidx = istgt->nportal + istgt->nuctl_portal; |
| 1306 |
|
| 1307 |
EV_SET(&kev, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); |
| 1308 |
rc = kevent(kq, &kev, 1, NULL, 0, NULL); |
| 1309 |
if (rc == -1) { |
| 1310 |
ISTGT_ERRLOG("kevent() failed\n"); |
| 1311 |
close(kq); |
| 1312 |
return -1; |
| 1313 |
} |
| 1314 |
EV_SET(&kev, SIGTERM, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); |
| 1315 |
rc = kevent(kq, &kev, 1, NULL, 0, NULL); |
| 1316 |
if (rc == -1) { |
| 1317 |
ISTGT_ERRLOG("kevent() failed\n"); |
| 1318 |
close(kq); |
| 1319 |
return -1; |
| 1320 |
} |
| 1321 |
#else |
| 1322 |
memset(&fds, 0, sizeof fds); |
| 1323 |
for (i = 0; i < istgt->nportal; i++) { |
| 1324 |
fds[i].fd = istgt->portal[i].sock; |
| 1325 |
fds[i].events = POLLIN; |
| 1326 |
} |
| 1327 |
ucidx = istgt->nportal; |
| 1328 |
for (i = 0; i < istgt->nuctl_portal; i++) { |
| 1329 |
fds[ucidx + i].fd = istgt->uctl_portal[i].sock; |
| 1330 |
fds[ucidx + i].events = POLLIN; |
| 1331 |
} |
| 1332 |
nidx = istgt->nportal + istgt->nuctl_portal; |
| 1333 |
#endif /* ISTGT_USE_KQUEUE */ |
| 1334 |
|
| 1335 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "loop start\n"); |
| 1336 |
while (1) { |
| 1337 |
if (istgt_get_state(istgt) != ISTGT_STATE_RUNNING) { |
| 1338 |
break; |
| 1339 |
} |
| 1340 |
#ifdef ISTGT_USE_KQUEUE |
| 1341 |
//ISTGT_TRACELOG(ISTGT_TRACE_NET, "kevent %d\n", nidx); |
| 1342 |
kev_timeout.tv_sec = 10; |
| 1343 |
kev_timeout.tv_nsec = 0; |
| 1344 |
rc = kevent(kq, NULL, 0, &kev, 1, &kev_timeout); |
| 1345 |
if (rc == -1 && errno == EINTR) { |
| 1346 |
continue; |
| 1347 |
} |
| 1348 |
if (rc == -1) { |
| 1349 |
ISTGT_ERRLOG("kevent() failed\n"); |
| 1350 |
break; |
| 1351 |
} |
| 1352 |
if (rc == 0) { |
| 1353 |
/* idle timeout */ |
| 1354 |
continue; |
| 1355 |
} |
| 1356 |
if (kev.filter == EVFILT_SIGNAL) { |
| 1357 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "kevent SIGNAL\n"); |
| 1358 |
if (kev.ident == SIGINT || kev.ident == SIGTERM) { |
| 1359 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 1360 |
"kevent SIGNAL SIGINT/SIGTERM\n"); |
| 1361 |
break; |
| 1362 |
} |
| 1363 |
continue; |
| 1364 |
} |
| 1365 |
#else |
| 1366 |
//ISTGT_TRACELOG(ISTGT_TRACE_NET, "poll %d\n", nidx); |
| 1367 |
rc = poll(fds, nidx, POLLWAIT); |
| 1368 |
if (rc == -1 && errno == EINTR) { |
| 1369 |
continue; |
| 1370 |
} |
| 1371 |
if (rc == -1) { |
| 1372 |
ISTGT_ERRLOG("poll() failed\n"); |
| 1373 |
break; |
| 1374 |
} |
| 1375 |
if (rc == 0) { |
| 1376 |
/* no fds */ |
| 1377 |
continue; |
| 1378 |
} |
| 1379 |
#endif /* ISTGT_USE_KQUEUE */ |
| 1380 |
|
| 1381 |
n = rc; |
| 1382 |
for (i = 0; n != 0 && i < istgt->nportal; i++) { |
| 1383 |
#ifdef ISTGT_USE_KQUEUE |
| 1384 |
if (kev.ident == istgt->portal[i].sock) { |
| 1385 |
if (kev.flags) { |
| 1386 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 1387 |
"flags %x\n", |
| 1388 |
kev.flags); |
| 1389 |
} |
| 1390 |
#else |
| 1391 |
if (fds[i].revents) { |
| 1392 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 1393 |
"events %x\n", |
| 1394 |
fds[i].revents); |
| 1395 |
} |
| 1396 |
if (fds[i].revents & POLLIN) { |
| 1397 |
#endif /* ISTGT_USE_KQUEUE */ |
| 1398 |
n--; |
| 1399 |
memset(&sa, 0, sizeof(sa)); |
| 1400 |
salen = sizeof(sa); |
| 1401 |
#ifdef ISTGT_USE_KQUEUE |
| 1402 |
ISTGT_TRACELOG(ISTGT_TRACE_NET, "accept %d\n", |
| 1403 |
kev.ident); |
| 1404 |
rc = accept(kev.ident, (struct sockaddr *) &sa, &salen); |
| 1405 |
#else |
| 1406 |
ISTGT_TRACELOG(ISTGT_TRACE_NET, "accept %d\n", |
| 1407 |
fds[i].fd); |
| 1408 |
rc = accept(fds[i].fd, (struct sockaddr *) &sa, &salen); |
| 1409 |
#endif /* ISTGT_USE_KQUEUE */ |
| 1410 |
if (rc < 0) { |
| 1411 |
ISTGT_ERRLOG("accept error: %d\n", rc); |
| 1412 |
continue; |
| 1413 |
} |
| 1414 |
sock = rc; |
| 1415 |
#if 0 |
| 1416 |
rc = fcntl(sock, F_GETFL, 0); |
| 1417 |
if (rc == -1) { |
| 1418 |
ISTGT_ERRLOG("fcntl() failed\n"); |
| 1419 |
continue; |
| 1420 |
} |
| 1421 |
rc = fcntl(sock, F_SETFL, (rc | O_NONBLOCK)); |
| 1422 |
if (rc == -1) { |
| 1423 |
ISTGT_ERRLOG("fcntl() failed\n"); |
| 1424 |
continue; |
| 1425 |
} |
| 1426 |
#endif |
| 1427 |
rc = istgt_create_conn(istgt, |
| 1428 |
&istgt->portal[i], sock, |
| 1429 |
(struct sockaddr *) &sa, salen); |
| 1430 |
if (rc < 0) { |
| 1431 |
close(sock); |
| 1432 |
ISTGT_ERRLOG("istgt_create_conn() failed\n"); |
| 1433 |
continue; |
| 1434 |
} |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
/* check for control */ |
| 1439 |
for (i = 0; n != 0 && i < istgt->nuctl_portal; i++) { |
| 1440 |
#ifdef ISTGT_USE_KQUEUE |
| 1441 |
if (kev.ident == istgt->uctl_portal[i].sock) { |
| 1442 |
if (kev.flags) { |
| 1443 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 1444 |
"flags %x\n", |
| 1445 |
kev.flags); |
| 1446 |
} |
| 1447 |
#else |
| 1448 |
if (fds[ucidx + i].revents) { |
| 1449 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, |
| 1450 |
"events %x\n", |
| 1451 |
fds[ucidx + i].revents); |
| 1452 |
} |
| 1453 |
if (fds[ucidx + i].revents & POLLIN) { |
| 1454 |
#endif /* ISTGT_USE_KQUEUE */ |
| 1455 |
n--; |
| 1456 |
memset(&sa, 0, sizeof(sa)); |
| 1457 |
salen = sizeof(sa); |
| 1458 |
#ifdef ISTGT_USE_KQUEUE |
| 1459 |
ISTGT_TRACELOG(ISTGT_TRACE_NET, |
| 1460 |
"accept %d\n", kev.ident); |
| 1461 |
rc = accept(kev.ident, |
| 1462 |
(struct sockaddr *) &sa, &salen); |
| 1463 |
#else |
| 1464 |
ISTGT_TRACELOG(ISTGT_TRACE_NET, |
| 1465 |
"accept %d\n", fds[ucidx + i].fd); |
| 1466 |
rc = accept(fds[ucidx + i].fd, |
| 1467 |
(struct sockaddr *) &sa, &salen); |
| 1468 |
#endif /* ISTGT_USE_KQUEUE */ |
| 1469 |
if (rc < 0) { |
| 1470 |
ISTGT_ERRLOG("accept error: %d\n", rc); |
| 1471 |
continue; |
| 1472 |
} |
| 1473 |
sock = rc; |
| 1474 |
rc = istgt_create_uctl(istgt, |
| 1475 |
&istgt->uctl_portal[i], sock, |
| 1476 |
(struct sockaddr *) &sa, salen); |
| 1477 |
if (rc < 0) { |
| 1478 |
close(sock); |
| 1479 |
ISTGT_ERRLOG("istgt_create_uctl() failed\n"); |
| 1480 |
continue; |
| 1481 |
} |
| 1482 |
} |
| 1483 |
} |
| 1484 |
} |
| 1485 |
#ifdef ISTGT_USE_KQUEUE |
| 1486 |
close(kq); |
| 1487 |
#endif /* ISTGT_USE_KQUEUE */ |
| 1488 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "loop ended\n"); |
| 1489 |
istgt_set_state(istgt, ISTGT_STATE_EXITING); |
| 1490 |
|
| 1491 |
return 0; |
| 1492 |
} |
| 1493 |
|
| 1494 |
static void |
| 1495 |
usage(void) |
| 1496 |
{ |
| 1497 |
printf("istgt [options]\n"); |
| 1498 |
printf("options:\n"); |
| 1499 |
printf(" -c config config file (default %s)\n", DEFAULT_CONFIG); |
| 1500 |
printf(" -p pidfile use specific file\n"); |
| 1501 |
printf(" -l facility use specific syslog facility (default %s)\n", |
| 1502 |
DEFAULT_LOG_FACILITY); |
| 1503 |
printf(" -m mode operational mode (default %d, 0=traditional, " |
| 1504 |
"1=normal, 2=experimental)\n", DEFAULT_ISTGT_SWMODE); |
| 1505 |
printf(" -t flag trace flag (all, net, iscsi, scsi, lu)\n"); |
| 1506 |
printf(" -q quiet warnings\n"); |
| 1507 |
printf(" -D don't detach from tty\n"); |
| 1508 |
printf(" -H show this usage\n"); |
| 1509 |
printf(" -V show version\n"); |
| 1510 |
} |
| 1511 |
|
| 1512 |
int |
| 1513 |
main(int argc, char **argv) |
| 1514 |
{ |
| 1515 |
ISTGT_Ptr istgt; |
| 1516 |
const char *config_file = DEFAULT_CONFIG; |
| 1517 |
const char *pidfile = NULL; |
| 1518 |
const char *logfacility = NULL; |
| 1519 |
const char *logpriority = NULL; |
| 1520 |
CONFIG *config; |
| 1521 |
pthread_t sigthread; |
| 1522 |
struct sigaction sigact, sigoldact_pipe, sigoldact_info; |
| 1523 |
struct sigaction sigoldact_wakeup, sigoldact_io; |
| 1524 |
sigset_t signew, sigold; |
| 1525 |
int retry = 10; |
| 1526 |
int detach = 1; |
| 1527 |
int swmode; |
| 1528 |
int ch; |
| 1529 |
int rc; |
| 1530 |
|
| 1531 |
if (sizeof (ISCSI_BHS) != ISCSI_BHS_LEN) { |
| 1532 |
fprintf(stderr, "Internal Error\n"); |
| 1533 |
exit(EXIT_FAILURE); |
| 1534 |
} |
| 1535 |
|
| 1536 |
memset(&g_istgt, 0, sizeof g_istgt); |
| 1537 |
istgt = &g_istgt; |
| 1538 |
istgt_set_state(istgt, ISTGT_STATE_INVALID); |
| 1539 |
istgt->swmode = DEFAULT_ISTGT_SWMODE; |
| 1540 |
|
| 1541 |
while ((ch = getopt(argc, argv, "c:p:l:m:t:qDHV")) != -1) { |
| 1542 |
switch (ch) { |
| 1543 |
case 'c': |
| 1544 |
config_file = optarg; |
| 1545 |
break; |
| 1546 |
case 'p': |
| 1547 |
pidfile = optarg; |
| 1548 |
break; |
| 1549 |
case 'l': |
| 1550 |
logfacility = optarg; |
| 1551 |
break; |
| 1552 |
case 'm': |
| 1553 |
swmode = strtol(optarg, NULL, 10); |
| 1554 |
if (swmode == ISTGT_SWMODE_TRADITIONAL |
| 1555 |
|| swmode == ISTGT_SWMODE_NORMAL |
| 1556 |
|| swmode == ISTGT_SWMODE_EXPERIMENTAL) { |
| 1557 |
istgt->swmode = swmode; |
| 1558 |
} else { |
| 1559 |
fprintf(stderr, "unknown mode %x\n", swmode); |
| 1560 |
usage(); |
| 1561 |
exit(EXIT_FAILURE); |
| 1562 |
} |
| 1563 |
break; |
| 1564 |
case 't': |
| 1565 |
if (strcasecmp(optarg, "NET") == 0) { |
| 1566 |
istgt_set_trace_flag(ISTGT_TRACE_NET); |
| 1567 |
} else if (strcasecmp(optarg, "ISCSI") == 0) { |
| 1568 |
istgt_set_trace_flag(ISTGT_TRACE_ISCSI); |
| 1569 |
} else if (strcasecmp(optarg, "SCSI") == 0) { |
| 1570 |
istgt_set_trace_flag(ISTGT_TRACE_SCSI); |
| 1571 |
} else if (strcasecmp(optarg, "LU") == 0) { |
| 1572 |
istgt_set_trace_flag(ISTGT_TRACE_LU); |
| 1573 |
} else if (strcasecmp(optarg, "ALL") == 0) { |
| 1574 |
istgt_set_trace_flag(ISTGT_TRACE_ALL); |
| 1575 |
} else if (strcasecmp(optarg, "NONE") == 0) { |
| 1576 |
istgt_set_trace_flag(ISTGT_TRACE_NONE); |
| 1577 |
} else { |
| 1578 |
fprintf(stderr, "unknown flag\n"); |
| 1579 |
usage(); |
| 1580 |
exit(EXIT_FAILURE); |
| 1581 |
} |
| 1582 |
break; |
| 1583 |
case 'q': |
| 1584 |
g_warn_flag = 0; |
| 1585 |
break; |
| 1586 |
case 'D': |
| 1587 |
detach = 0; |
| 1588 |
break; |
| 1589 |
case 'V': |
| 1590 |
printf("istgt version %s\n", ISTGT_VERSION); |
| 1591 |
printf("istgt extra version %s\n", ISTGT_EXTRA_VERSION); |
| 1592 |
exit(EXIT_SUCCESS); |
| 1593 |
case 'H': |
| 1594 |
default: |
| 1595 |
usage(); |
| 1596 |
exit(EXIT_SUCCESS); |
| 1597 |
} |
| 1598 |
} |
| 1599 |
|
| 1600 |
/* read config files */ |
| 1601 |
config = istgt_allocate_config(); |
| •1602 |
g_config_file = config_file; |
| 1603 |
rc = istgt_read_config(config, config_file); |
| 1604 |
if (rc < 0) { |
| 1605 |
fprintf(stderr, "config error\n"); |
| 1606 |
exit(EXIT_FAILURE); |
| 1607 |
} |
| 1608 |
if (config->section == NULL) { |
| 1609 |
fprintf(stderr, "empty config\n"); |
| 1610 |
istgt_free_config(config); |
| 1611 |
exit(EXIT_FAILURE); |
| 1612 |
} |
| 1613 |
istgt->config = config; |
| 1614 |
//istgt_print_config(config); |
| 1615 |
|
| 1616 |
/* open log files */ |
| 1617 |
if (logfacility == NULL) { |
| 1618 |
logfacility = istgt_get_log_facility(config); |
| 1619 |
} |
| 1620 |
rc = istgt_set_log_facility(logfacility); |
| 1621 |
if (rc < 0) { |
| 1622 |
fprintf(stderr, "log facility error\n"); |
| 1623 |
istgt_free_config(config); |
| 1624 |
exit(EXIT_FAILURE); |
| 1625 |
} |
| 1626 |
if (logpriority == NULL) { |
| 1627 |
logpriority = DEFAULT_LOG_PRIORITY; |
| 1628 |
} |
| 1629 |
rc = istgt_set_log_priority(logpriority); |
| 1630 |
if (rc < 0) { |
| 1631 |
fprintf(stderr, "log priority error\n"); |
| 1632 |
istgt_free_config(config); |
| 1633 |
exit(EXIT_FAILURE); |
| 1634 |
} |
| 1635 |
istgt_open_log(); |
| 1636 |
|
| 1637 |
ISTGT_NOTICELOG("istgt version %s (%s)\n", ISTGT_VERSION, |
| 1638 |
ISTGT_EXTRA_VERSION); |
| 1639 |
switch (istgt->swmode) { |
| 1640 |
case ISTGT_SWMODE_TRADITIONAL: |
| 1641 |
ISTGT_NOTICELOG("traditional mode\n"); |
| 1642 |
break; |
| 1643 |
case ISTGT_SWMODE_NORMAL: |
| 1644 |
ISTGT_NOTICELOG("normal mode\n"); |
| 1645 |
break; |
| 1646 |
case ISTGT_SWMODE_EXPERIMENTAL: |
| 1647 |
ISTGT_NOTICELOG("experimental mode\n"); |
| 1648 |
break; |
| 1649 |
default: |
| 1650 |
break; |
| 1651 |
} |
| 1652 |
|
| 1653 |
#ifdef ISTGT_USE_CRC32C_TABLE |
| 1654 |
/* build crc32c table */ |
| 1655 |
istgt_init_crc32c_table(); |
| 1656 |
#endif /* ISTGT_USE_CRC32C_TABLE */ |
| 1657 |
|
| 1658 |
/* initialize sub modules */ |
| 1659 |
rc = istgt_init(istgt); |
| 1660 |
if (rc < 0) { |
| 1661 |
ISTGT_ERRLOG("istgt_init() failed\n"); |
| 1662 |
initialize_error: |
| 1663 |
istgt_close_log(); |
| 1664 |
istgt_free_config(config); |
| 1665 |
exit(EXIT_FAILURE); |
| 1666 |
} |
| •1667 |
rc = istgt_lu_init(istgt, 0); |
| 1668 |
if (rc < 0) { |
| 1669 |
ISTGT_ERRLOG("istgt_lu_init() failed\n"); |
| 1670 |
goto initialize_error; |
| 1671 |
} |
| 1672 |
rc = istgt_iscsi_init(istgt); |
| 1673 |
if (rc < 0) { |
| 1674 |
ISTGT_ERRLOG("istgt_iscsi_init() failed\n"); |
| 1675 |
goto initialize_error; |
| 1676 |
} |
| 1677 |
|
| 1678 |
/* override by command line */ |
| 1679 |
if (pidfile != NULL) { |
| 1680 |
xfree(istgt->pidfile); |
| 1681 |
istgt->pidfile = xstrdup(pidfile); |
| 1682 |
} |
| 1683 |
|
| 1684 |
/* detach from tty and run background */ |
| 1685 |
fflush(stdout); |
| 1686 |
if (detach) { |
| 1687 |
rc = daemon(0, 0); |
| 1688 |
if (rc < 0) { |
| 1689 |
ISTGT_ERRLOG("daemon() failed\n"); |
| 1690 |
goto initialize_error; |
| 1691 |
} |
| 1692 |
} |
| 1693 |
|
| 1694 |
/* setup signal handler thread */ |
| 1695 |
memset(&sigact, 0, sizeof sigact); |
| 1696 |
memset(&sigoldact_pipe, 0, sizeof sigoldact_pipe); |
| 1697 |
memset(&sigoldact_info, 0, sizeof sigoldact_info); |
| 1698 |
memset(&sigoldact_wakeup, 0, sizeof sigoldact_wakeup); |
| 1699 |
memset(&sigoldact_io, 0, sizeof sigoldact_io); |
| 1700 |
sigact.sa_handler = SIG_IGN; |
| 1701 |
sigemptyset(&sigact.sa_mask); |
| 1702 |
rc = sigaction(SIGPIPE, &sigact, &sigoldact_pipe); |
| 1703 |
if (rc < 0) { |
| 1704 |
ISTGT_ERRLOG("sigaction(SIGPIPE) failed\n"); |
| 1705 |
goto initialize_error; |
| 1706 |
} |
| 1707 |
#ifdef SIGINFO |
| 1708 |
sigact.sa_handler = istgt_siginfo; |
| 1709 |
sigemptyset(&sigact.sa_mask); |
| 1710 |
rc = sigaction(SIGINFO, &sigact, &sigoldact_info); |
| 1711 |
if (rc < 0) { |
| 1712 |
ISTGT_ERRLOG("sigaction(SIGINFO) failed\n"); |
| 1713 |
goto initialize_error; |
| 1714 |
} |
| 1715 |
#endif |
| 1716 |
#ifdef ISTGT_USE_SIGRT |
| 1717 |
if (ISTGT_SIGWAKEUP < SIGRTMIN |
| 1718 |
|| ISTGT_SIGWAKEUP > SIGRTMAX) { |
| 1719 |
ISTGT_ERRLOG("SIGRT error\n"); |
| 1720 |
goto initialize_error; |
| 1721 |
} |
| 1722 |
#endif /* ISTGT_USE_SIGRT */ |
| 1723 |
sigact.sa_handler = istgt_sigwakeup; |
| 1724 |
sigemptyset(&sigact.sa_mask); |
| 1725 |
rc = sigaction(ISTGT_SIGWAKEUP, &sigact, &sigoldact_wakeup); |
| 1726 |
if (rc < 0) { |
| 1727 |
ISTGT_ERRLOG("sigaction(ISTGT_SIGWAKEUP) failed\n"); |
| 1728 |
goto initialize_error; |
| 1729 |
} |
| 1730 |
#ifdef SIGIO |
| 1731 |
sigact.sa_handler = istgt_sigio; |
| 1732 |
sigemptyset(&sigact.sa_mask); |
| 1733 |
rc = sigaction(SIGIO, &sigact, &sigoldact_io); |
| 1734 |
if (rc < 0) { |
| 1735 |
ISTGT_ERRLOG("sigaction(SIGIO) failed\n"); |
| 1736 |
goto initialize_error; |
| 1737 |
} |
| 1738 |
#endif |
| 1739 |
pthread_sigmask(SIG_SETMASK, NULL, &signew); |
| 1740 |
sigaddset(&signew, SIGINT); |
| 1741 |
sigaddset(&signew, SIGTERM); |
| 1742 |
sigaddset(&signew, SIGQUIT); |
| 1743 |
sigaddset(&signew, SIGHUP); |
| 1744 |
#ifdef SIGINFO |
| 1745 |
sigaddset(&signew, SIGINFO); |
| 1746 |
#endif |
| 1747 |
sigaddset(&signew, SIGUSR1); |
| 1748 |
sigaddset(&signew, SIGUSR2); |
| 1749 |
#ifdef SIGIO |
| 1750 |
sigaddset(&signew, SIGIO); |
| 1751 |
#endif |
| 1752 |
sigaddset(&signew, ISTGT_SIGWAKEUP); |
| 1753 |
pthread_sigmask(SIG_SETMASK, &signew, &sigold); |
| 1754 |
#ifdef ISTGT_STACKSIZE |
| 1755 |
rc = pthread_create(&sigthread, &istgt->attr, &istgt_sighandler, |
| 1756 |
#else |
| 1757 |
rc = pthread_create(&sigthread, NULL, &istgt_sighandler, |
| 1758 |
#endif |
| 1759 |
(void *) istgt); |
| 1760 |
if (rc != 0) { |
| 1761 |
ISTGT_ERRLOG("pthread_create() failed\n"); |
| 1762 |
goto initialize_error; |
| 1763 |
} |
| 1764 |
#if 0 |
| 1765 |
rc = pthread_detach(sigthread); |
| 1766 |
if (rc != 0) { |
| 1767 |
ISTGT_ERRLOG("pthread_detach() failed\n"); |
| 1768 |
goto initialize_error; |
| 1769 |
} |
| 1770 |
#endif |
| 1771 |
#ifdef HAVE_PTHREAD_SET_NAME_NP |
| 1772 |
pthread_set_name_np(sigthread, "sigthread"); |
| 1773 |
pthread_set_name_np(pthread_self(), "mainthread"); |
| 1774 |
#endif |
| 1775 |
|
| 1776 |
/* create LUN threads for command queuing */ |
| •1777 |
rc = istgt_lu_create_threads(istgt, 0); |
| 1778 |
if (rc < 0) { |
| 1779 |
ISTGT_ERRLOG("lu_create_threads() failed\n"); |
| 1780 |
goto initialize_error; |
| 1781 |
} |
| 1782 |
rc = istgt_lu_set_all_state(istgt, ISTGT_STATE_RUNNING); |
| 1783 |
if (rc < 0) { |
| 1784 |
ISTGT_ERRLOG("lu_set_all_state() failed\n"); |
| 1785 |
goto initialize_error; |
| 1786 |
} |
| 1787 |
|
| 1788 |
/* open portals */ |
| 1789 |
rc = istgt_open_uctl_portal(istgt); |
| 1790 |
if (rc < 0) { |
| 1791 |
ISTGT_ERRLOG("istgt_open_uctl_portal() failed\n"); |
| 1792 |
goto initialize_error; |
| 1793 |
} |
| 1794 |
rc = istgt_open_portal(istgt); |
| 1795 |
if (rc < 0) { |
| 1796 |
ISTGT_ERRLOG("istgt_open_portal() failed\n"); |
| 1797 |
goto initialize_error; |
| 1798 |
} |
| 1799 |
|
| 1800 |
/* write pid */ |
| 1801 |
rc = istgt_write_pidfile(istgt); |
| 1802 |
if (rc < 0) { |
| 1803 |
ISTGT_ERRLOG("istgt_write_pid() failed\n"); |
| 1804 |
goto initialize_error; |
| 1805 |
} |
| 1806 |
|
| 1807 |
/* accept loop */ |
| 1808 |
rc = istgt_acceptor(istgt); |
| 1809 |
if (rc < 0) { |
| 1810 |
ISTGT_ERRLOG("istgt_acceptor() failed\n"); |
| 1811 |
istgt_close_portal(istgt); |
| 1812 |
istgt_close_uctl_portal(istgt); |
| 1813 |
istgt_iscsi_shutdown(istgt); |
| •1814 |
istgt_lu_shutdown(istgt, 0); |
| 1815 |
istgt_destory_initiator_group_array(istgt); |
| 1816 |
istgt_destroy_portal_array(istgt); |
| 1817 |
istgt_destroy_uctl_portal(istgt); |
| 1818 |
istgt_remove_pidfile(istgt); |
| 1819 |
istgt_close_log(); |
| 1820 |
istgt->config = NULL; |
| 1821 |
istgt_free_config(config); |
| 1822 |
exit(EXIT_FAILURE); |
| 1823 |
} |
| 1824 |
|
| 1825 |
/* wait threads */ |
| 1826 |
while (retry > 0) { |
| 1827 |
if (istgt_get_active_conns() == 0) { |
| 1828 |
break; |
| 1829 |
} |
| 1830 |
sleep(1); |
| 1831 |
retry--; |
| 1832 |
} |
| 1833 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "retry=%d\n", retry); |
| 1834 |
|
| 1835 |
ISTGT_NOTICELOG("istgt version %s (%s) exiting\n", ISTGT_VERSION, |
| 1836 |
ISTGT_EXTRA_VERSION); |
| 1837 |
|
| 1838 |
/* cleanup */ |
| 1839 |
istgt_close_portal(istgt); |
| 1840 |
istgt_close_uctl_portal(istgt); |
| 1841 |
istgt_iscsi_shutdown(istgt); |
| •1842 |
istgt_lu_shutdown(istgt, 0); |
| 1843 |
istgt_destory_initiator_group_array(istgt); |
| 1844 |
istgt_destroy_portal_array(istgt); |
| 1845 |
istgt_destroy_uctl_portal(istgt); |
| 1846 |
istgt_remove_pidfile(istgt); |
| 1847 |
istgt_close_log(); |
| 1848 |
istgt->config = NULL; |
| 1849 |
istgt_free_config(config); |
| 1850 |
istgt_set_state(istgt, ISTGT_STATE_SHUTDOWN); |
| 1851 |
|
| 1852 |
/* stop signal thread */ |
| 1853 |
rc = pthread_join(sigthread, NULL); |
| 1854 |
if (rc != 0) { |
| 1855 |
ISTGT_ERRLOG("pthread_join() failed\n"); |
| 1856 |
exit (EXIT_FAILURE); |
| 1857 |
} |
| 1858 |
|
| 1859 |
return 0; |
| 1860 |
} |