186 free(ag->ag_name); 187 free(ag); 188} 189 190struct auth_group * 191auth_group_find(struct conf *conf, const char *name) 192{ 193 struct auth_group *ag; 194 195 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 196 if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0) 197 return (ag); 198 } 199 200 return (NULL); 201} 202 203static void 204auth_check_secret_length(struct auth *auth) 205{ 206 size_t len; 207 208 len = strlen(auth->a_secret); 209 if (len > 16) { 210 if (auth->a_auth_group->ag_name != NULL) 211 log_warnx("secret for user \"%s\", auth-group \"%s\", " 212 "is too long; it should be at most 16 characters " 213 "long", auth->a_user, auth->a_auth_group->ag_name); 214 else 215 log_warnx("secret for user \"%s\", target \"%s\", " 216 "is too long; it should be at most 16 characters " 217 "long", auth->a_user, 218 auth->a_auth_group->ag_target->t_iqn); 219 } 220 if (len < 12) { 221 if (auth->a_auth_group->ag_name != NULL) 222 log_warnx("secret for user \"%s\", auth-group \"%s\", " 223 "is too short; it should be at least 12 characters " 224 "long", auth->a_user, 225 auth->a_auth_group->ag_name); 226 else 227 log_warnx("secret for user \"%s\", target \"%s\", " 228 "is too short; it should be at least 16 characters " 229 "long", auth->a_user, 230 auth->a_auth_group->ag_target->t_iqn); 231 } 232 233 if (auth->a_mutual_secret != NULL) { 234 len = strlen(auth->a_secret); 235 if (len > 16) { 236 if (auth->a_auth_group->ag_name != NULL) 237 log_warnx("mutual secret for user \"%s\", " 238 "auth-group \"%s\", is too long; it should " 239 "be at most 16 characters long", 240 auth->a_user, auth->a_auth_group->ag_name); 241 else 242 log_warnx("mutual secret for user \"%s\", " 243 "target \"%s\", is too long; it should " 244 "be at most 16 characters long", 245 auth->a_user, 246 auth->a_auth_group->ag_target->t_iqn); 247 } 248 if (len < 12) { 249 if (auth->a_auth_group->ag_name != NULL) 250 log_warnx("mutual secret for user \"%s\", " 251 "auth-group \"%s\", is too short; it " 252 "should be at least 12 characters long", 253 auth->a_user, auth->a_auth_group->ag_name); 254 else 255 log_warnx("mutual secret for user \"%s\", " 256 "target \"%s\", is too short; it should be " 257 "at least 16 characters long", 258 auth->a_user, 259 auth->a_auth_group->ag_target->t_iqn); 260 } 261 } 262} 263 264const struct auth * 265auth_new_chap(struct auth_group *ag, const char *user, 266 const char *secret) 267{ 268 struct auth *auth; 269 270 if (ag->ag_type == AG_TYPE_UNKNOWN) 271 ag->ag_type = AG_TYPE_CHAP; 272 if (ag->ag_type != AG_TYPE_CHAP) { 273 if (ag->ag_name != NULL) 274 log_warnx("cannot mix \"chap\" authentication with " 275 "other types for auth-group \"%s\"", ag->ag_name); 276 else 277 log_warnx("cannot mix \"chap\" authentication with " 278 "other types for target \"%s\"", 279 ag->ag_target->t_iqn); 280 return (NULL); 281 } 282 283 auth = auth_new(ag); 284 auth->a_user = checked_strdup(user); 285 auth->a_secret = checked_strdup(secret); 286 287 auth_check_secret_length(auth); 288 289 return (auth); 290} 291 292const struct auth * 293auth_new_chap_mutual(struct auth_group *ag, const char *user, 294 const char *secret, const char *user2, const char *secret2) 295{ 296 struct auth *auth; 297 298 if (ag->ag_type == AG_TYPE_UNKNOWN) 299 ag->ag_type = AG_TYPE_CHAP_MUTUAL; 300 if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) { 301 if (ag->ag_name != NULL) 302 log_warnx("cannot mix \"chap-mutual\" authentication " 303 "with other types for auth-group \"%s\"", 304 ag->ag_name); 305 else 306 log_warnx("cannot mix \"chap-mutual\" authentication " 307 "with other types for target \"%s\"", 308 ag->ag_target->t_iqn); 309 return (NULL); 310 } 311 312 auth = auth_new(ag); 313 auth->a_user = checked_strdup(user); 314 auth->a_secret = checked_strdup(secret); 315 auth->a_mutual_user = checked_strdup(user2); 316 auth->a_mutual_secret = checked_strdup(secret2); 317 318 auth_check_secret_length(auth); 319 320 return (auth); 321} 322 323static struct portal * 324portal_new(struct portal_group *pg) 325{ 326 struct portal *portal; 327 328 portal = calloc(1, sizeof(*portal)); 329 if (portal == NULL) 330 log_err(1, "calloc"); 331 TAILQ_INIT(&portal->p_targets); 332 portal->p_portal_group = pg; 333 TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next); 334 return (portal); 335} 336 337static void 338portal_delete(struct portal *portal) 339{ 340 TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next); 341 freeaddrinfo(portal->p_ai); 342 free(portal->p_listen); 343 free(portal); 344} 345 346struct portal_group * 347portal_group_new(struct conf *conf, const char *name) 348{ 349 struct portal_group *pg; 350 351 pg = portal_group_find(conf, name); 352 if (pg != NULL) { 353 log_warnx("duplicated portal-group \"%s\"", name); 354 return (NULL); 355 } 356 357 pg = calloc(1, sizeof(*pg)); 358 if (pg == NULL) 359 log_err(1, "calloc"); 360 pg->pg_name = checked_strdup(name); 361 TAILQ_INIT(&pg->pg_portals); 362 pg->pg_conf = conf; 363 conf->conf_last_portal_group_tag++; 364 pg->pg_tag = conf->conf_last_portal_group_tag; 365 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next); 366 367 return (pg); 368} 369 370void 371portal_group_delete(struct portal_group *pg) 372{ 373 struct portal *portal, *tmp; 374 375 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next); 376 377 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp) 378 portal_delete(portal); 379 free(pg->pg_name); 380 free(pg); 381} 382 383struct portal_group * 384portal_group_find(struct conf *conf, const char *name) 385{ 386 struct portal_group *pg; 387 388 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 389 if (strcmp(pg->pg_name, name) == 0) 390 return (pg); 391 } 392 393 return (NULL); 394} 395 396int 397portal_group_add_listen(struct portal_group *pg, const char *value, bool iser) 398{ 399 struct addrinfo hints; 400 struct portal *portal; 401 char *addr, *ch, *arg; 402 const char *port; 403 int error, colons = 0; 404 405#ifndef ICL_KERNEL_PROXY 406 if (iser) { 407 log_warnx("ctld(8) compiled without ICL_KERNEL_PROXY " 408 "does not support iSER protocol"); 409 return (-1); 410 } 411#endif 412 413 portal = portal_new(pg); 414 portal->p_listen = checked_strdup(value); 415 portal->p_iser = iser; 416 417 arg = portal->p_listen; 418 if (arg[0] == '\0') { 419 log_warnx("empty listen address"); 420 free(portal->p_listen); 421 free(portal); 422 return (1); 423 } 424 if (arg[0] == '[') { 425 /* 426 * IPv6 address in square brackets, perhaps with port. 427 */ 428 arg++; 429 addr = strsep(&arg, "]"); 430 if (arg == NULL) { 431 log_warnx("invalid listen address %s", 432 portal->p_listen); 433 free(portal->p_listen); 434 free(portal); 435 return (1); 436 } 437 if (arg[0] == '\0') { 438 port = "3260"; 439 } else if (arg[0] == ':') { 440 port = arg + 1; 441 } else { 442 log_warnx("invalid listen address %s", 443 portal->p_listen); 444 free(portal->p_listen); 445 free(portal); 446 return (1); 447 } 448 } else { 449 /* 450 * Either IPv6 address without brackets - and without 451 * a port - or IPv4 address. Just count the colons. 452 */ 453 for (ch = arg; *ch != '\0'; ch++) { 454 if (*ch == ':') 455 colons++; 456 } 457 if (colons > 1) { 458 addr = arg; 459 port = "3260"; 460 } else { 461 addr = strsep(&arg, ":"); 462 if (arg == NULL) 463 port = "3260"; 464 else 465 port = arg; 466 } 467 } 468 469 memset(&hints, 0, sizeof(hints)); 470 hints.ai_family = PF_UNSPEC; 471 hints.ai_socktype = SOCK_STREAM; 472 hints.ai_flags = AI_PASSIVE; 473 474 error = getaddrinfo(addr, port, &hints, &portal->p_ai); 475 if (error != 0) { 476 log_warnx("getaddrinfo for %s failed: %s", 477 portal->p_listen, gai_strerror(error)); 478 free(portal->p_listen); 479 free(portal); 480 return (1); 481 } 482 483 /* 484 * XXX: getaddrinfo(3) may return multiple addresses; we should turn 485 * those into multiple portals. 486 */ 487 488 return (0); 489} 490 491static bool 492valid_hex(const char ch) 493{ 494 switch (ch) { 495 case '0': 496 case '1': 497 case '2': 498 case '3': 499 case '4': 500 case '5': 501 case '6': 502 case '7': 503 case '8': 504 case '9': 505 case 'a': 506 case 'A': 507 case 'b': 508 case 'B': 509 case 'c': 510 case 'C': 511 case 'd': 512 case 'D': 513 case 'e': 514 case 'E': 515 case 'f': 516 case 'F': 517 return (true); 518 default: 519 return (false); 520 } 521} 522 523bool 524valid_iscsi_name(const char *name) 525{ 526 int i; 527 528 if (strlen(name) >= MAX_NAME_LEN) { 529 log_warnx("overlong name for target \"%s\"; max length allowed " 530 "by iSCSI specification is %d characters", 531 name, MAX_NAME_LEN); 532 return (false); 533 } 534 535 /* 536 * In the cases below, we don't return an error, just in case the admin 537 * was right, and we're wrong. 538 */ 539 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) { 540 for (i = strlen("iqn."); name[i] != '\0'; i++) { 541 /* 542 * XXX: We should verify UTF-8 normalisation, as defined 543 * by 3.2.6.2: iSCSI Name Encoding. 544 */ 545 if (isalnum(name[i])) 546 continue; 547 if (name[i] == '-' || name[i] == '.' || name[i] == ':') 548 continue; 549 log_warnx("invalid character \"%c\" in target name " 550 "\"%s\"; allowed characters are letters, digits, " 551 "'-', '.', and ':'", name[i], name); 552 break; 553 } 554 /* 555 * XXX: Check more stuff: valid date and a valid reversed domain. 556 */ 557 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) { 558 if (strlen(name) != strlen("eui.") + 16) 559 log_warnx("invalid target name \"%s\"; the \"eui.\" " 560 "should be followed by exactly 16 hexadecimal " 561 "digits", name); 562 for (i = strlen("eui."); name[i] != '\0'; i++) { 563 if (!valid_hex(name[i])) { 564 log_warnx("invalid character \"%c\" in target " 565 "name \"%s\"; allowed characters are 1-9 " 566 "and A-F", name[i], name); 567 break; 568 } 569 } 570 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) { 571 if (strlen(name) > strlen("naa.") + 32) 572 log_warnx("invalid target name \"%s\"; the \"naa.\" " 573 "should be followed by at most 32 hexadecimal " 574 "digits", name); 575 for (i = strlen("naa."); name[i] != '\0'; i++) { 576 if (!valid_hex(name[i])) { 577 log_warnx("invalid character \"%c\" in target " 578 "name \"%s\"; allowed characters are 1-9 " 579 "and A-F", name[i], name); 580 break; 581 } 582 } 583 } else { 584 log_warnx("invalid target name \"%s\"; should start with " 585 "either \".iqn\", \"eui.\", or \"naa.\"", 586 name); 587 } 588 return (true); 589} 590 591struct target * 592target_new(struct conf *conf, const char *iqn) 593{ 594 struct target *targ; 595 int i, len; 596 597 targ = target_find(conf, iqn); 598 if (targ != NULL) { 599 log_warnx("duplicated target \"%s\"", iqn); 600 return (NULL); 601 } 602 if (valid_iscsi_name(iqn) == false) { 603 log_warnx("target name \"%s\" is invalid", iqn); 604 return (NULL); 605 } 606 targ = calloc(1, sizeof(*targ)); 607 if (targ == NULL) 608 log_err(1, "calloc"); 609 targ->t_iqn = checked_strdup(iqn); 610 611 /* 612 * RFC 3722 requires us to normalize the name to lowercase. 613 */ 614 len = strlen(iqn); 615 for (i = 0; i < len; i++) 616 targ->t_iqn[i] = tolower(targ->t_iqn[i]); 617 618 TAILQ_INIT(&targ->t_luns); 619 targ->t_conf = conf; 620 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next); 621 622 return (targ); 623} 624 625void 626target_delete(struct target *targ) 627{ 628 struct lun *lun, *tmp; 629 630 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next); 631 632 TAILQ_FOREACH_SAFE(lun, &targ->t_luns, l_next, tmp) 633 lun_delete(lun); 634 free(targ->t_iqn); 635 free(targ); 636} 637 638struct target * 639target_find(struct conf *conf, const char *iqn) 640{ 641 struct target *targ; 642 643 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 644 if (strcasecmp(targ->t_iqn, iqn) == 0) 645 return (targ); 646 } 647 648 return (NULL); 649} 650 651struct lun * 652lun_new(struct target *targ, int lun_id) 653{ 654 struct lun *lun; 655 656 lun = lun_find(targ, lun_id); 657 if (lun != NULL) { 658 log_warnx("duplicated lun %d for target \"%s\"", 659 lun_id, targ->t_iqn); 660 return (NULL); 661 } 662 663 lun = calloc(1, sizeof(*lun)); 664 if (lun == NULL) 665 log_err(1, "calloc"); 666 lun->l_lun = lun_id; 667 TAILQ_INIT(&lun->l_options); 668 lun->l_target = targ; 669 TAILQ_INSERT_TAIL(&targ->t_luns, lun, l_next); 670 671 return (lun); 672} 673 674void 675lun_delete(struct lun *lun) 676{ 677 struct lun_option *lo, *tmp; 678 679 TAILQ_REMOVE(&lun->l_target->t_luns, lun, l_next); 680 681 TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp) 682 lun_option_delete(lo); 683 free(lun->l_backend); 684 free(lun->l_device_id); 685 free(lun->l_path); 686 free(lun->l_serial); 687 free(lun); 688} 689 690struct lun * 691lun_find(struct target *targ, int lun_id) 692{ 693 struct lun *lun; 694 695 TAILQ_FOREACH(lun, &targ->t_luns, l_next) { 696 if (lun->l_lun == lun_id) 697 return (lun); 698 } 699 700 return (NULL); 701} 702 703void 704lun_set_backend(struct lun *lun, const char *value) 705{ 706 free(lun->l_backend); 707 lun->l_backend = checked_strdup(value); 708} 709 710void 711lun_set_blocksize(struct lun *lun, size_t value) 712{ 713 714 lun->l_blocksize = value; 715} 716 717void 718lun_set_device_id(struct lun *lun, const char *value) 719{ 720 free(lun->l_device_id); 721 lun->l_device_id = checked_strdup(value); 722} 723 724void 725lun_set_path(struct lun *lun, const char *value) 726{ 727 free(lun->l_path); 728 lun->l_path = checked_strdup(value); 729} 730 731void 732lun_set_serial(struct lun *lun, const char *value) 733{ 734 free(lun->l_serial); 735 lun->l_serial = checked_strdup(value); 736} 737 738void 739lun_set_size(struct lun *lun, size_t value) 740{ 741 742 lun->l_size = value; 743} 744 745void 746lun_set_ctl_lun(struct lun *lun, uint32_t value) 747{ 748 749 lun->l_ctl_lun = value; 750} 751 752struct lun_option * 753lun_option_new(struct lun *lun, const char *name, const char *value) 754{ 755 struct lun_option *lo; 756 757 lo = lun_option_find(lun, name); 758 if (lo != NULL) { 759 log_warnx("duplicated lun option %s for lun %d, target \"%s\"", 760 name, lun->l_lun, lun->l_target->t_iqn); 761 return (NULL); 762 } 763 764 lo = calloc(1, sizeof(*lo)); 765 if (lo == NULL) 766 log_err(1, "calloc"); 767 lo->lo_name = checked_strdup(name); 768 lo->lo_value = checked_strdup(value); 769 lo->lo_lun = lun; 770 TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next); 771 772 return (lo); 773} 774 775void 776lun_option_delete(struct lun_option *lo) 777{ 778 779 TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next); 780 781 free(lo->lo_name); 782 free(lo->lo_value); 783 free(lo); 784} 785 786struct lun_option * 787lun_option_find(struct lun *lun, const char *name) 788{ 789 struct lun_option *lo; 790 791 TAILQ_FOREACH(lo, &lun->l_options, lo_next) { 792 if (strcmp(lo->lo_name, name) == 0) 793 return (lo); 794 } 795 796 return (NULL); 797} 798 799void 800lun_option_set(struct lun_option *lo, const char *value) 801{ 802 803 free(lo->lo_value); 804 lo->lo_value = checked_strdup(value); 805} 806 807static struct connection * 808connection_new(struct portal *portal, int fd, const char *host) 809{ 810 struct connection *conn; 811 812 conn = calloc(1, sizeof(*conn)); 813 if (conn == NULL) 814 log_err(1, "calloc"); 815 conn->conn_portal = portal; 816 conn->conn_socket = fd; 817 conn->conn_initiator_addr = checked_strdup(host); 818 819 /* 820 * Default values, from RFC 3720, section 12. 821 */ 822 conn->conn_max_data_segment_length = 8192; 823 conn->conn_max_burst_length = 262144; 824 conn->conn_immediate_data = true; 825 826 return (conn); 827} 828 829#if 0 830static void 831conf_print(struct conf *conf) 832{ 833 struct auth_group *ag; 834 struct auth *auth;
| 283 free(ag->ag_name); 284 free(ag); 285} 286 287struct auth_group * 288auth_group_find(struct conf *conf, const char *name) 289{ 290 struct auth_group *ag; 291 292 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 293 if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0) 294 return (ag); 295 } 296 297 return (NULL); 298} 299 300static void 301auth_check_secret_length(struct auth *auth) 302{ 303 size_t len; 304 305 len = strlen(auth->a_secret); 306 if (len > 16) { 307 if (auth->a_auth_group->ag_name != NULL) 308 log_warnx("secret for user \"%s\", auth-group \"%s\", " 309 "is too long; it should be at most 16 characters " 310 "long", auth->a_user, auth->a_auth_group->ag_name); 311 else 312 log_warnx("secret for user \"%s\", target \"%s\", " 313 "is too long; it should be at most 16 characters " 314 "long", auth->a_user, 315 auth->a_auth_group->ag_target->t_iqn); 316 } 317 if (len < 12) { 318 if (auth->a_auth_group->ag_name != NULL) 319 log_warnx("secret for user \"%s\", auth-group \"%s\", " 320 "is too short; it should be at least 12 characters " 321 "long", auth->a_user, 322 auth->a_auth_group->ag_name); 323 else 324 log_warnx("secret for user \"%s\", target \"%s\", " 325 "is too short; it should be at least 16 characters " 326 "long", auth->a_user, 327 auth->a_auth_group->ag_target->t_iqn); 328 } 329 330 if (auth->a_mutual_secret != NULL) { 331 len = strlen(auth->a_secret); 332 if (len > 16) { 333 if (auth->a_auth_group->ag_name != NULL) 334 log_warnx("mutual secret for user \"%s\", " 335 "auth-group \"%s\", is too long; it should " 336 "be at most 16 characters long", 337 auth->a_user, auth->a_auth_group->ag_name); 338 else 339 log_warnx("mutual secret for user \"%s\", " 340 "target \"%s\", is too long; it should " 341 "be at most 16 characters long", 342 auth->a_user, 343 auth->a_auth_group->ag_target->t_iqn); 344 } 345 if (len < 12) { 346 if (auth->a_auth_group->ag_name != NULL) 347 log_warnx("mutual secret for user \"%s\", " 348 "auth-group \"%s\", is too short; it " 349 "should be at least 12 characters long", 350 auth->a_user, auth->a_auth_group->ag_name); 351 else 352 log_warnx("mutual secret for user \"%s\", " 353 "target \"%s\", is too short; it should be " 354 "at least 16 characters long", 355 auth->a_user, 356 auth->a_auth_group->ag_target->t_iqn); 357 } 358 } 359} 360 361const struct auth * 362auth_new_chap(struct auth_group *ag, const char *user, 363 const char *secret) 364{ 365 struct auth *auth; 366 367 if (ag->ag_type == AG_TYPE_UNKNOWN) 368 ag->ag_type = AG_TYPE_CHAP; 369 if (ag->ag_type != AG_TYPE_CHAP) { 370 if (ag->ag_name != NULL) 371 log_warnx("cannot mix \"chap\" authentication with " 372 "other types for auth-group \"%s\"", ag->ag_name); 373 else 374 log_warnx("cannot mix \"chap\" authentication with " 375 "other types for target \"%s\"", 376 ag->ag_target->t_iqn); 377 return (NULL); 378 } 379 380 auth = auth_new(ag); 381 auth->a_user = checked_strdup(user); 382 auth->a_secret = checked_strdup(secret); 383 384 auth_check_secret_length(auth); 385 386 return (auth); 387} 388 389const struct auth * 390auth_new_chap_mutual(struct auth_group *ag, const char *user, 391 const char *secret, const char *user2, const char *secret2) 392{ 393 struct auth *auth; 394 395 if (ag->ag_type == AG_TYPE_UNKNOWN) 396 ag->ag_type = AG_TYPE_CHAP_MUTUAL; 397 if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) { 398 if (ag->ag_name != NULL) 399 log_warnx("cannot mix \"chap-mutual\" authentication " 400 "with other types for auth-group \"%s\"", 401 ag->ag_name); 402 else 403 log_warnx("cannot mix \"chap-mutual\" authentication " 404 "with other types for target \"%s\"", 405 ag->ag_target->t_iqn); 406 return (NULL); 407 } 408 409 auth = auth_new(ag); 410 auth->a_user = checked_strdup(user); 411 auth->a_secret = checked_strdup(secret); 412 auth->a_mutual_user = checked_strdup(user2); 413 auth->a_mutual_secret = checked_strdup(secret2); 414 415 auth_check_secret_length(auth); 416 417 return (auth); 418} 419 420static struct portal * 421portal_new(struct portal_group *pg) 422{ 423 struct portal *portal; 424 425 portal = calloc(1, sizeof(*portal)); 426 if (portal == NULL) 427 log_err(1, "calloc"); 428 TAILQ_INIT(&portal->p_targets); 429 portal->p_portal_group = pg; 430 TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next); 431 return (portal); 432} 433 434static void 435portal_delete(struct portal *portal) 436{ 437 TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next); 438 freeaddrinfo(portal->p_ai); 439 free(portal->p_listen); 440 free(portal); 441} 442 443struct portal_group * 444portal_group_new(struct conf *conf, const char *name) 445{ 446 struct portal_group *pg; 447 448 pg = portal_group_find(conf, name); 449 if (pg != NULL) { 450 log_warnx("duplicated portal-group \"%s\"", name); 451 return (NULL); 452 } 453 454 pg = calloc(1, sizeof(*pg)); 455 if (pg == NULL) 456 log_err(1, "calloc"); 457 pg->pg_name = checked_strdup(name); 458 TAILQ_INIT(&pg->pg_portals); 459 pg->pg_conf = conf; 460 conf->conf_last_portal_group_tag++; 461 pg->pg_tag = conf->conf_last_portal_group_tag; 462 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next); 463 464 return (pg); 465} 466 467void 468portal_group_delete(struct portal_group *pg) 469{ 470 struct portal *portal, *tmp; 471 472 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next); 473 474 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp) 475 portal_delete(portal); 476 free(pg->pg_name); 477 free(pg); 478} 479 480struct portal_group * 481portal_group_find(struct conf *conf, const char *name) 482{ 483 struct portal_group *pg; 484 485 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 486 if (strcmp(pg->pg_name, name) == 0) 487 return (pg); 488 } 489 490 return (NULL); 491} 492 493int 494portal_group_add_listen(struct portal_group *pg, const char *value, bool iser) 495{ 496 struct addrinfo hints; 497 struct portal *portal; 498 char *addr, *ch, *arg; 499 const char *port; 500 int error, colons = 0; 501 502#ifndef ICL_KERNEL_PROXY 503 if (iser) { 504 log_warnx("ctld(8) compiled without ICL_KERNEL_PROXY " 505 "does not support iSER protocol"); 506 return (-1); 507 } 508#endif 509 510 portal = portal_new(pg); 511 portal->p_listen = checked_strdup(value); 512 portal->p_iser = iser; 513 514 arg = portal->p_listen; 515 if (arg[0] == '\0') { 516 log_warnx("empty listen address"); 517 free(portal->p_listen); 518 free(portal); 519 return (1); 520 } 521 if (arg[0] == '[') { 522 /* 523 * IPv6 address in square brackets, perhaps with port. 524 */ 525 arg++; 526 addr = strsep(&arg, "]"); 527 if (arg == NULL) { 528 log_warnx("invalid listen address %s", 529 portal->p_listen); 530 free(portal->p_listen); 531 free(portal); 532 return (1); 533 } 534 if (arg[0] == '\0') { 535 port = "3260"; 536 } else if (arg[0] == ':') { 537 port = arg + 1; 538 } else { 539 log_warnx("invalid listen address %s", 540 portal->p_listen); 541 free(portal->p_listen); 542 free(portal); 543 return (1); 544 } 545 } else { 546 /* 547 * Either IPv6 address without brackets - and without 548 * a port - or IPv4 address. Just count the colons. 549 */ 550 for (ch = arg; *ch != '\0'; ch++) { 551 if (*ch == ':') 552 colons++; 553 } 554 if (colons > 1) { 555 addr = arg; 556 port = "3260"; 557 } else { 558 addr = strsep(&arg, ":"); 559 if (arg == NULL) 560 port = "3260"; 561 else 562 port = arg; 563 } 564 } 565 566 memset(&hints, 0, sizeof(hints)); 567 hints.ai_family = PF_UNSPEC; 568 hints.ai_socktype = SOCK_STREAM; 569 hints.ai_flags = AI_PASSIVE; 570 571 error = getaddrinfo(addr, port, &hints, &portal->p_ai); 572 if (error != 0) { 573 log_warnx("getaddrinfo for %s failed: %s", 574 portal->p_listen, gai_strerror(error)); 575 free(portal->p_listen); 576 free(portal); 577 return (1); 578 } 579 580 /* 581 * XXX: getaddrinfo(3) may return multiple addresses; we should turn 582 * those into multiple portals. 583 */ 584 585 return (0); 586} 587 588static bool 589valid_hex(const char ch) 590{ 591 switch (ch) { 592 case '0': 593 case '1': 594 case '2': 595 case '3': 596 case '4': 597 case '5': 598 case '6': 599 case '7': 600 case '8': 601 case '9': 602 case 'a': 603 case 'A': 604 case 'b': 605 case 'B': 606 case 'c': 607 case 'C': 608 case 'd': 609 case 'D': 610 case 'e': 611 case 'E': 612 case 'f': 613 case 'F': 614 return (true); 615 default: 616 return (false); 617 } 618} 619 620bool 621valid_iscsi_name(const char *name) 622{ 623 int i; 624 625 if (strlen(name) >= MAX_NAME_LEN) { 626 log_warnx("overlong name for target \"%s\"; max length allowed " 627 "by iSCSI specification is %d characters", 628 name, MAX_NAME_LEN); 629 return (false); 630 } 631 632 /* 633 * In the cases below, we don't return an error, just in case the admin 634 * was right, and we're wrong. 635 */ 636 if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) { 637 for (i = strlen("iqn."); name[i] != '\0'; i++) { 638 /* 639 * XXX: We should verify UTF-8 normalisation, as defined 640 * by 3.2.6.2: iSCSI Name Encoding. 641 */ 642 if (isalnum(name[i])) 643 continue; 644 if (name[i] == '-' || name[i] == '.' || name[i] == ':') 645 continue; 646 log_warnx("invalid character \"%c\" in target name " 647 "\"%s\"; allowed characters are letters, digits, " 648 "'-', '.', and ':'", name[i], name); 649 break; 650 } 651 /* 652 * XXX: Check more stuff: valid date and a valid reversed domain. 653 */ 654 } else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) { 655 if (strlen(name) != strlen("eui.") + 16) 656 log_warnx("invalid target name \"%s\"; the \"eui.\" " 657 "should be followed by exactly 16 hexadecimal " 658 "digits", name); 659 for (i = strlen("eui."); name[i] != '\0'; i++) { 660 if (!valid_hex(name[i])) { 661 log_warnx("invalid character \"%c\" in target " 662 "name \"%s\"; allowed characters are 1-9 " 663 "and A-F", name[i], name); 664 break; 665 } 666 } 667 } else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) { 668 if (strlen(name) > strlen("naa.") + 32) 669 log_warnx("invalid target name \"%s\"; the \"naa.\" " 670 "should be followed by at most 32 hexadecimal " 671 "digits", name); 672 for (i = strlen("naa."); name[i] != '\0'; i++) { 673 if (!valid_hex(name[i])) { 674 log_warnx("invalid character \"%c\" in target " 675 "name \"%s\"; allowed characters are 1-9 " 676 "and A-F", name[i], name); 677 break; 678 } 679 } 680 } else { 681 log_warnx("invalid target name \"%s\"; should start with " 682 "either \".iqn\", \"eui.\", or \"naa.\"", 683 name); 684 } 685 return (true); 686} 687 688struct target * 689target_new(struct conf *conf, const char *iqn) 690{ 691 struct target *targ; 692 int i, len; 693 694 targ = target_find(conf, iqn); 695 if (targ != NULL) { 696 log_warnx("duplicated target \"%s\"", iqn); 697 return (NULL); 698 } 699 if (valid_iscsi_name(iqn) == false) { 700 log_warnx("target name \"%s\" is invalid", iqn); 701 return (NULL); 702 } 703 targ = calloc(1, sizeof(*targ)); 704 if (targ == NULL) 705 log_err(1, "calloc"); 706 targ->t_iqn = checked_strdup(iqn); 707 708 /* 709 * RFC 3722 requires us to normalize the name to lowercase. 710 */ 711 len = strlen(iqn); 712 for (i = 0; i < len; i++) 713 targ->t_iqn[i] = tolower(targ->t_iqn[i]); 714 715 TAILQ_INIT(&targ->t_luns); 716 targ->t_conf = conf; 717 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next); 718 719 return (targ); 720} 721 722void 723target_delete(struct target *targ) 724{ 725 struct lun *lun, *tmp; 726 727 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next); 728 729 TAILQ_FOREACH_SAFE(lun, &targ->t_luns, l_next, tmp) 730 lun_delete(lun); 731 free(targ->t_iqn); 732 free(targ); 733} 734 735struct target * 736target_find(struct conf *conf, const char *iqn) 737{ 738 struct target *targ; 739 740 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 741 if (strcasecmp(targ->t_iqn, iqn) == 0) 742 return (targ); 743 } 744 745 return (NULL); 746} 747 748struct lun * 749lun_new(struct target *targ, int lun_id) 750{ 751 struct lun *lun; 752 753 lun = lun_find(targ, lun_id); 754 if (lun != NULL) { 755 log_warnx("duplicated lun %d for target \"%s\"", 756 lun_id, targ->t_iqn); 757 return (NULL); 758 } 759 760 lun = calloc(1, sizeof(*lun)); 761 if (lun == NULL) 762 log_err(1, "calloc"); 763 lun->l_lun = lun_id; 764 TAILQ_INIT(&lun->l_options); 765 lun->l_target = targ; 766 TAILQ_INSERT_TAIL(&targ->t_luns, lun, l_next); 767 768 return (lun); 769} 770 771void 772lun_delete(struct lun *lun) 773{ 774 struct lun_option *lo, *tmp; 775 776 TAILQ_REMOVE(&lun->l_target->t_luns, lun, l_next); 777 778 TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp) 779 lun_option_delete(lo); 780 free(lun->l_backend); 781 free(lun->l_device_id); 782 free(lun->l_path); 783 free(lun->l_serial); 784 free(lun); 785} 786 787struct lun * 788lun_find(struct target *targ, int lun_id) 789{ 790 struct lun *lun; 791 792 TAILQ_FOREACH(lun, &targ->t_luns, l_next) { 793 if (lun->l_lun == lun_id) 794 return (lun); 795 } 796 797 return (NULL); 798} 799 800void 801lun_set_backend(struct lun *lun, const char *value) 802{ 803 free(lun->l_backend); 804 lun->l_backend = checked_strdup(value); 805} 806 807void 808lun_set_blocksize(struct lun *lun, size_t value) 809{ 810 811 lun->l_blocksize = value; 812} 813 814void 815lun_set_device_id(struct lun *lun, const char *value) 816{ 817 free(lun->l_device_id); 818 lun->l_device_id = checked_strdup(value); 819} 820 821void 822lun_set_path(struct lun *lun, const char *value) 823{ 824 free(lun->l_path); 825 lun->l_path = checked_strdup(value); 826} 827 828void 829lun_set_serial(struct lun *lun, const char *value) 830{ 831 free(lun->l_serial); 832 lun->l_serial = checked_strdup(value); 833} 834 835void 836lun_set_size(struct lun *lun, size_t value) 837{ 838 839 lun->l_size = value; 840} 841 842void 843lun_set_ctl_lun(struct lun *lun, uint32_t value) 844{ 845 846 lun->l_ctl_lun = value; 847} 848 849struct lun_option * 850lun_option_new(struct lun *lun, const char *name, const char *value) 851{ 852 struct lun_option *lo; 853 854 lo = lun_option_find(lun, name); 855 if (lo != NULL) { 856 log_warnx("duplicated lun option %s for lun %d, target \"%s\"", 857 name, lun->l_lun, lun->l_target->t_iqn); 858 return (NULL); 859 } 860 861 lo = calloc(1, sizeof(*lo)); 862 if (lo == NULL) 863 log_err(1, "calloc"); 864 lo->lo_name = checked_strdup(name); 865 lo->lo_value = checked_strdup(value); 866 lo->lo_lun = lun; 867 TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next); 868 869 return (lo); 870} 871 872void 873lun_option_delete(struct lun_option *lo) 874{ 875 876 TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next); 877 878 free(lo->lo_name); 879 free(lo->lo_value); 880 free(lo); 881} 882 883struct lun_option * 884lun_option_find(struct lun *lun, const char *name) 885{ 886 struct lun_option *lo; 887 888 TAILQ_FOREACH(lo, &lun->l_options, lo_next) { 889 if (strcmp(lo->lo_name, name) == 0) 890 return (lo); 891 } 892 893 return (NULL); 894} 895 896void 897lun_option_set(struct lun_option *lo, const char *value) 898{ 899 900 free(lo->lo_value); 901 lo->lo_value = checked_strdup(value); 902} 903 904static struct connection * 905connection_new(struct portal *portal, int fd, const char *host) 906{ 907 struct connection *conn; 908 909 conn = calloc(1, sizeof(*conn)); 910 if (conn == NULL) 911 log_err(1, "calloc"); 912 conn->conn_portal = portal; 913 conn->conn_socket = fd; 914 conn->conn_initiator_addr = checked_strdup(host); 915 916 /* 917 * Default values, from RFC 3720, section 12. 918 */ 919 conn->conn_max_data_segment_length = 8192; 920 conn->conn_max_burst_length = 262144; 921 conn->conn_immediate_data = true; 922 923 return (conn); 924} 925 926#if 0 927static void 928conf_print(struct conf *conf) 929{ 930 struct auth_group *ag; 931 struct auth *auth;
|
847 fprintf(stderr, "}\n"); 848 } 849 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 850 fprintf(stderr, "portal-group %s {\n", pg->pg_name); 851 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 852 fprintf(stderr, "\t listen %s\n", portal->p_listen); 853 fprintf(stderr, "}\n"); 854 } 855 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 856 fprintf(stderr, "target %s {\n", targ->t_iqn); 857 if (targ->t_alias != NULL) 858 fprintf(stderr, "\t alias %s\n", targ->t_alias); 859 TAILQ_FOREACH(lun, &targ->t_luns, l_next) { 860 fprintf(stderr, "\tlun %d {\n", lun->l_lun); 861 fprintf(stderr, "\t\tpath %s\n", lun->l_path); 862 TAILQ_FOREACH(lo, &lun->l_options, lo_next) 863 fprintf(stderr, "\t\toption %s %s\n", 864 lo->lo_name, lo->lo_value); 865 fprintf(stderr, "\t}\n"); 866 } 867 fprintf(stderr, "}\n"); 868 } 869} 870#endif 871 872static int 873conf_verify_lun(struct lun *lun) 874{ 875 const struct lun *lun2; 876 const struct target *targ2; 877 878 if (lun->l_backend == NULL) 879 lun_set_backend(lun, "block"); 880 if (strcmp(lun->l_backend, "block") == 0) { 881 if (lun->l_path == NULL) { 882 log_warnx("missing path for lun %d, target \"%s\"", 883 lun->l_lun, lun->l_target->t_iqn); 884 return (1); 885 } 886 } else if (strcmp(lun->l_backend, "ramdisk") == 0) { 887 if (lun->l_size == 0) { 888 log_warnx("missing size for ramdisk-backed lun %d, " 889 "target \"%s\"", lun->l_lun, lun->l_target->t_iqn); 890 return (1); 891 } 892 if (lun->l_path != NULL) { 893 log_warnx("path must not be specified " 894 "for ramdisk-backed lun %d, target \"%s\"", 895 lun->l_lun, lun->l_target->t_iqn); 896 return (1); 897 } 898 } 899 if (lun->l_lun < 0 || lun->l_lun > 255) { 900 log_warnx("invalid lun number for lun %d, target \"%s\"; " 901 "must be between 0 and 255", lun->l_lun, 902 lun->l_target->t_iqn); 903 return (1); 904 } 905 if (lun->l_blocksize == 0) { 906 lun_set_blocksize(lun, DEFAULT_BLOCKSIZE); 907 } else if (lun->l_blocksize < 0) { 908 log_warnx("invalid blocksize for lun %d, target \"%s\"; " 909 "must be larger than 0", lun->l_lun, lun->l_target->t_iqn); 910 return (1); 911 } 912 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) { 913 log_warnx("invalid size for lun %d, target \"%s\"; " 914 "must be multiple of blocksize", lun->l_lun, 915 lun->l_target->t_iqn); 916 return (1); 917 } 918 TAILQ_FOREACH(targ2, &lun->l_target->t_conf->conf_targets, t_next) { 919 TAILQ_FOREACH(lun2, &targ2->t_luns, l_next) { 920 if (lun == lun2) 921 continue; 922 if (lun->l_path != NULL && lun2->l_path != NULL && 923 strcmp(lun->l_path, lun2->l_path) == 0) { 924 log_debugx("WARNING: path \"%s\" duplicated " 925 "between lun %d, target \"%s\", and " 926 "lun %d, target \"%s\"", lun->l_path, 927 lun->l_lun, lun->l_target->t_iqn, 928 lun2->l_lun, lun2->l_target->t_iqn); 929 } 930 } 931 } 932 933 return (0); 934} 935 936int 937conf_verify(struct conf *conf) 938{ 939 struct auth_group *ag; 940 struct portal_group *pg; 941 struct target *targ; 942 struct lun *lun; 943 bool found_lun0; 944 int error; 945 946 if (conf->conf_pidfile_path == NULL) 947 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE); 948 949 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 950 if (targ->t_auth_group == NULL) { 951 log_warnx("missing authentication for target \"%s\"; " 952 "must specify either \"auth-group\", \"chap\", " 953 "or \"chap-mutual\"", targ->t_iqn); 954 return (1); 955 } 956 if (targ->t_portal_group == NULL) { 957 targ->t_portal_group = portal_group_find(conf, 958 "default"); 959 assert(targ->t_portal_group != NULL); 960 } 961 found_lun0 = false; 962 TAILQ_FOREACH(lun, &targ->t_luns, l_next) { 963 error = conf_verify_lun(lun); 964 if (error != 0) 965 return (error); 966 if (lun->l_lun == 0) 967 found_lun0 = true; 968 } 969 if (!found_lun0) { 970 log_warnx("mandatory LUN 0 not configured " 971 "for target \"%s\"", targ->t_iqn); 972 return (1); 973 } 974 } 975 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 976 assert(pg->pg_name != NULL); 977 if (pg->pg_discovery_auth_group == NULL) { 978 pg->pg_discovery_auth_group = 979 auth_group_find(conf, "no-access"); 980 assert(pg->pg_discovery_auth_group != NULL); 981 } 982 983 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 984 if (targ->t_portal_group == pg) 985 break; 986 } 987 if (targ == NULL) { 988 if (strcmp(pg->pg_name, "default") != 0) 989 log_warnx("portal-group \"%s\" not assigned " 990 "to any target", pg->pg_name); 991 pg->pg_unassigned = true; 992 } else 993 pg->pg_unassigned = false; 994 } 995 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 996 if (ag->ag_name == NULL) 997 assert(ag->ag_target != NULL); 998 else 999 assert(ag->ag_target == NULL); 1000 1001 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1002 if (targ->t_auth_group == ag) 1003 break; 1004 } 1005 if (targ == NULL && ag->ag_name != NULL && 1006 strcmp(ag->ag_name, "no-authentication") != 0 && 1007 strcmp(ag->ag_name, "no-access") != 0) { 1008 log_warnx("auth-group \"%s\" not assigned " 1009 "to any target", ag->ag_name); 1010 } 1011 } 1012 1013 return (0); 1014} 1015 1016static int 1017conf_apply(struct conf *oldconf, struct conf *newconf) 1018{ 1019 struct target *oldtarg, *newtarg, *tmptarg; 1020 struct lun *oldlun, *newlun, *tmplun; 1021 struct portal_group *oldpg, *newpg; 1022 struct portal *oldp, *newp; 1023 pid_t otherpid; 1024 int changed, cumulated_error = 0, error; 1025#ifndef ICL_KERNEL_PROXY 1026 int one = 1; 1027#endif 1028 1029 if (oldconf->conf_debug != newconf->conf_debug) { 1030 log_debugx("changing debug level to %d", newconf->conf_debug); 1031 log_init(newconf->conf_debug); 1032 } 1033 1034 if (oldconf->conf_pidfh != NULL) { 1035 assert(oldconf->conf_pidfile_path != NULL); 1036 if (newconf->conf_pidfile_path != NULL && 1037 strcmp(oldconf->conf_pidfile_path, 1038 newconf->conf_pidfile_path) == 0) { 1039 newconf->conf_pidfh = oldconf->conf_pidfh; 1040 oldconf->conf_pidfh = NULL; 1041 } else { 1042 log_debugx("removing pidfile %s", 1043 oldconf->conf_pidfile_path); 1044 pidfile_remove(oldconf->conf_pidfh); 1045 oldconf->conf_pidfh = NULL; 1046 } 1047 } 1048 1049 if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) { 1050 log_debugx("opening pidfile %s", newconf->conf_pidfile_path); 1051 newconf->conf_pidfh = 1052 pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid); 1053 if (newconf->conf_pidfh == NULL) { 1054 if (errno == EEXIST) 1055 log_errx(1, "daemon already running, pid: %jd.", 1056 (intmax_t)otherpid); 1057 log_err(1, "cannot open or create pidfile \"%s\"", 1058 newconf->conf_pidfile_path); 1059 } 1060 } 1061 1062 TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) { 1063 /* 1064 * First, remove any targets present in the old configuration 1065 * and missing in the new one. 1066 */ 1067 newtarg = target_find(newconf, oldtarg->t_iqn); 1068 if (newtarg == NULL) { 1069 TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, 1070 tmplun) { 1071 log_debugx("target %s not found in new " 1072 "configuration; removing its lun %d, " 1073 "backed by CTL lun %d", 1074 oldtarg->t_iqn, oldlun->l_lun, 1075 oldlun->l_ctl_lun); 1076 error = kernel_lun_remove(oldlun); 1077 if (error != 0) { 1078 log_warnx("failed to remove lun %d, " 1079 "target %s, CTL lun %d", 1080 oldlun->l_lun, oldtarg->t_iqn, 1081 oldlun->l_ctl_lun); 1082 cumulated_error++; 1083 } 1084 lun_delete(oldlun); 1085 } 1086 target_delete(oldtarg); 1087 continue; 1088 } 1089 1090 /* 1091 * Second, remove any LUNs present in the old target 1092 * and missing in the new one. 1093 */ 1094 TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, tmplun) { 1095 newlun = lun_find(newtarg, oldlun->l_lun); 1096 if (newlun == NULL) { 1097 log_debugx("lun %d, target %s, CTL lun %d " 1098 "not found in new configuration; " 1099 "removing", oldlun->l_lun, oldtarg->t_iqn, 1100 oldlun->l_ctl_lun); 1101 error = kernel_lun_remove(oldlun); 1102 if (error != 0) { 1103 log_warnx("failed to remove lun %d, " 1104 "target %s, CTL lun %d", 1105 oldlun->l_lun, oldtarg->t_iqn, 1106 oldlun->l_ctl_lun); 1107 cumulated_error++; 1108 } 1109 lun_delete(oldlun); 1110 continue; 1111 } 1112 1113 /* 1114 * Also remove the LUNs changed by more than size. 1115 */ 1116 changed = 0; 1117 assert(oldlun->l_backend != NULL); 1118 assert(newlun->l_backend != NULL); 1119 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) { 1120 log_debugx("backend for lun %d, target %s, " 1121 "CTL lun %d changed; removing", 1122 oldlun->l_lun, oldtarg->t_iqn, 1123 oldlun->l_ctl_lun); 1124 changed = 1; 1125 } 1126 if (oldlun->l_blocksize != newlun->l_blocksize) { 1127 log_debugx("blocksize for lun %d, target %s, " 1128 "CTL lun %d changed; removing", 1129 oldlun->l_lun, oldtarg->t_iqn, 1130 oldlun->l_ctl_lun); 1131 changed = 1; 1132 } 1133 if (newlun->l_device_id != NULL && 1134 (oldlun->l_device_id == NULL || 1135 strcmp(oldlun->l_device_id, newlun->l_device_id) != 1136 0)) { 1137 log_debugx("device-id for lun %d, target %s, " 1138 "CTL lun %d changed; removing", 1139 oldlun->l_lun, oldtarg->t_iqn, 1140 oldlun->l_ctl_lun); 1141 changed = 1; 1142 } 1143 if (newlun->l_path != NULL && 1144 (oldlun->l_path == NULL || 1145 strcmp(oldlun->l_path, newlun->l_path) != 0)) { 1146 log_debugx("path for lun %d, target %s, " 1147 "CTL lun %d, changed; removing", 1148 oldlun->l_lun, oldtarg->t_iqn, 1149 oldlun->l_ctl_lun); 1150 changed = 1; 1151 } 1152 if (newlun->l_serial != NULL && 1153 (oldlun->l_serial == NULL || 1154 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) { 1155 log_debugx("serial for lun %d, target %s, " 1156 "CTL lun %d changed; removing", 1157 oldlun->l_lun, oldtarg->t_iqn, 1158 oldlun->l_ctl_lun); 1159 changed = 1; 1160 } 1161 if (changed) { 1162 error = kernel_lun_remove(oldlun); 1163 if (error != 0) { 1164 log_warnx("failed to remove lun %d, " 1165 "target %s, CTL lun %d", 1166 oldlun->l_lun, oldtarg->t_iqn, 1167 oldlun->l_ctl_lun); 1168 cumulated_error++; 1169 } 1170 lun_delete(oldlun); 1171 continue; 1172 } 1173 1174 lun_set_ctl_lun(newlun, oldlun->l_ctl_lun); 1175 } 1176 } 1177 1178 /* 1179 * Now add new targets or modify existing ones. 1180 */ 1181 TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) { 1182 oldtarg = target_find(oldconf, newtarg->t_iqn); 1183 1184 TAILQ_FOREACH(newlun, &newtarg->t_luns, l_next) { 1185 if (oldtarg != NULL) { 1186 oldlun = lun_find(oldtarg, newlun->l_lun); 1187 if (oldlun != NULL) { 1188 if (newlun->l_size != oldlun->l_size) { 1189 log_debugx("resizing lun %d, " 1190 "target %s, CTL lun %d", 1191 newlun->l_lun, 1192 newtarg->t_iqn, 1193 newlun->l_ctl_lun); 1194 error = 1195 kernel_lun_resize(newlun); 1196 if (error != 0) { 1197 log_warnx("failed to " 1198 "resize lun %d, " 1199 "target %s, " 1200 "CTL lun %d", 1201 newlun->l_lun, 1202 newtarg->t_iqn, 1203 newlun->l_lun); 1204 cumulated_error++; 1205 } 1206 } 1207 continue; 1208 } 1209 } 1210 log_debugx("adding lun %d, target %s", 1211 newlun->l_lun, newtarg->t_iqn); 1212 error = kernel_lun_add(newlun); 1213 if (error != 0) { 1214 log_warnx("failed to add lun %d, target %s", 1215 newlun->l_lun, newtarg->t_iqn); 1216 cumulated_error++; 1217 } 1218 } 1219 } 1220 1221 /* 1222 * Go through the new portals, opening the sockets as neccessary. 1223 */ 1224 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) { 1225 if (newpg->pg_unassigned) { 1226 log_debugx("not listening on portal-group \"%s\", " 1227 "not assigned to any target", 1228 newpg->pg_name); 1229 continue; 1230 } 1231 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) { 1232 /* 1233 * Try to find already open portal and reuse 1234 * the listening socket. We don't care about 1235 * what portal or portal group that was, what 1236 * matters is the listening address. 1237 */ 1238 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, 1239 pg_next) { 1240 TAILQ_FOREACH(oldp, &oldpg->pg_portals, 1241 p_next) { 1242 if (strcmp(newp->p_listen, 1243 oldp->p_listen) == 0 && 1244 oldp->p_socket > 0) { 1245 newp->p_socket = 1246 oldp->p_socket; 1247 oldp->p_socket = 0; 1248 break; 1249 } 1250 } 1251 } 1252 if (newp->p_socket > 0) { 1253 /* 1254 * We're done with this portal. 1255 */ 1256 continue; 1257 } 1258 1259#ifdef ICL_KERNEL_PROXY 1260 log_debugx("listening on %s, portal-group \"%s\" using ICL proxy", 1261 newp->p_listen, newpg->pg_name); 1262 kernel_listen(newp->p_ai, newp->p_iser); 1263#else 1264 assert(newp->p_iser == false); 1265 1266 log_debugx("listening on %s, portal-group \"%s\"", 1267 newp->p_listen, newpg->pg_name); 1268 newp->p_socket = socket(newp->p_ai->ai_family, 1269 newp->p_ai->ai_socktype, 1270 newp->p_ai->ai_protocol); 1271 if (newp->p_socket < 0) { 1272 log_warn("socket(2) failed for %s", 1273 newp->p_listen); 1274 cumulated_error++; 1275 continue; 1276 } 1277 error = setsockopt(newp->p_socket, SOL_SOCKET, 1278 SO_REUSEADDR, &one, sizeof(one)); 1279 if (error != 0) { 1280 log_warn("setsockopt(SO_REUSEADDR) failed " 1281 "for %s", newp->p_listen); 1282 close(newp->p_socket); 1283 newp->p_socket = 0; 1284 cumulated_error++; 1285 continue; 1286 } 1287 error = bind(newp->p_socket, newp->p_ai->ai_addr, 1288 newp->p_ai->ai_addrlen); 1289 if (error != 0) { 1290 log_warn("bind(2) failed for %s", 1291 newp->p_listen); 1292 close(newp->p_socket); 1293 newp->p_socket = 0; 1294 cumulated_error++; 1295 continue; 1296 } 1297 error = listen(newp->p_socket, -1); 1298 if (error != 0) { 1299 log_warn("listen(2) failed for %s", 1300 newp->p_listen); 1301 close(newp->p_socket); 1302 newp->p_socket = 0; 1303 cumulated_error++; 1304 continue; 1305 } 1306#endif /* !ICL_KERNEL_PROXY */ 1307 } 1308 } 1309 1310 /* 1311 * Go through the no longer used sockets, closing them. 1312 */ 1313 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) { 1314 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) { 1315 if (oldp->p_socket <= 0) 1316 continue; 1317 log_debugx("closing socket for %s, portal-group \"%s\"", 1318 oldp->p_listen, oldpg->pg_name); 1319 close(oldp->p_socket); 1320 oldp->p_socket = 0; 1321 } 1322 } 1323 1324 return (cumulated_error); 1325} 1326 1327bool 1328timed_out(void) 1329{ 1330 1331 return (sigalrm_received); 1332} 1333 1334static void 1335sigalrm_handler(int dummy __unused) 1336{ 1337 /* 1338 * It would be easiest to just log an error and exit. We can't 1339 * do this, though, because log_errx() is not signal safe, since 1340 * it calls syslog(3). Instead, set a flag checked by pdu_send() 1341 * and pdu_receive(), to call log_errx() there. Should they fail 1342 * to notice, we'll exit here one second later. 1343 */ 1344 if (sigalrm_received) { 1345 /* 1346 * Oh well. Just give up and quit. 1347 */ 1348 _exit(2); 1349 } 1350 1351 sigalrm_received = true; 1352} 1353 1354static void 1355set_timeout(const struct conf *conf) 1356{ 1357 struct sigaction sa; 1358 struct itimerval itv; 1359 int error; 1360 1361 if (conf->conf_timeout <= 0) { 1362 log_debugx("session timeout disabled"); 1363 return; 1364 } 1365 1366 bzero(&sa, sizeof(sa)); 1367 sa.sa_handler = sigalrm_handler; 1368 sigfillset(&sa.sa_mask); 1369 error = sigaction(SIGALRM, &sa, NULL); 1370 if (error != 0) 1371 log_err(1, "sigaction"); 1372 1373 /* 1374 * First SIGALRM will arive after conf_timeout seconds. 1375 * If we do nothing, another one will arrive a second later. 1376 */ 1377 bzero(&itv, sizeof(itv)); 1378 itv.it_interval.tv_sec = 1; 1379 itv.it_value.tv_sec = conf->conf_timeout; 1380 1381 log_debugx("setting session timeout to %d seconds", 1382 conf->conf_timeout); 1383 error = setitimer(ITIMER_REAL, &itv, NULL); 1384 if (error != 0) 1385 log_err(1, "setitimer"); 1386} 1387 1388static int 1389wait_for_children(bool block) 1390{ 1391 pid_t pid; 1392 int status; 1393 int num = 0; 1394 1395 for (;;) { 1396 /* 1397 * If "block" is true, wait for at least one process. 1398 */ 1399 if (block && num == 0) 1400 pid = wait4(-1, &status, 0, NULL); 1401 else 1402 pid = wait4(-1, &status, WNOHANG, NULL); 1403 if (pid <= 0) 1404 break; 1405 if (WIFSIGNALED(status)) { 1406 log_warnx("child process %d terminated with signal %d", 1407 pid, WTERMSIG(status)); 1408 } else if (WEXITSTATUS(status) != 0) { 1409 log_warnx("child process %d terminated with exit status %d", 1410 pid, WEXITSTATUS(status)); 1411 } else { 1412 log_debugx("child process %d terminated gracefully", pid); 1413 } 1414 num++; 1415 } 1416 1417 return (num); 1418} 1419 1420static void 1421handle_connection(struct portal *portal, int fd, bool dont_fork) 1422{ 1423 struct connection *conn; 1424#ifndef ICL_KERNEL_PROXY 1425 struct sockaddr_storage ss; 1426 socklen_t sslen = sizeof(ss); 1427 int error; 1428#endif 1429 pid_t pid; 1430 char host[NI_MAXHOST + 1]; 1431 struct conf *conf; 1432 1433 conf = portal->p_portal_group->pg_conf; 1434 1435 if (dont_fork) { 1436 log_debugx("incoming connection; not forking due to -d flag"); 1437 } else { 1438 nchildren -= wait_for_children(false); 1439 assert(nchildren >= 0); 1440 1441 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) { 1442 log_debugx("maxproc limit of %d child processes hit; " 1443 "waiting for child process to exit", conf->conf_maxproc); 1444 nchildren -= wait_for_children(true); 1445 assert(nchildren >= 0); 1446 } 1447 log_debugx("incoming connection; forking child process #%d", 1448 nchildren); 1449 nchildren++; 1450 pid = fork(); 1451 if (pid < 0) 1452 log_err(1, "fork"); 1453 if (pid > 0) { 1454 close(fd); 1455 return; 1456 } 1457 } 1458 pidfile_close(conf->conf_pidfh); 1459 1460#ifdef ICL_KERNEL_PROXY 1461 /* 1462 * XXX 1463 */ 1464 log_set_peer_addr("XXX"); 1465#else 1466 error = getpeername(fd, (struct sockaddr *)&ss, &sslen); 1467 if (error != 0) 1468 log_err(1, "getpeername"); 1469 error = getnameinfo((struct sockaddr *)&ss, sslen, 1470 host, sizeof(host), NULL, 0, NI_NUMERICHOST); 1471 if (error != 0) 1472 log_errx(1, "getaddrinfo: %s", gai_strerror(error)); 1473 1474 log_debugx("accepted connection from %s; portal group \"%s\"", 1475 host, portal->p_portal_group->pg_name); 1476 log_set_peer_addr(host); 1477 setproctitle("%s", host); 1478#endif 1479 1480 conn = connection_new(portal, fd, host); 1481 set_timeout(conf); 1482 kernel_capsicate(); 1483 login(conn); 1484 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 1485 kernel_handoff(conn); 1486 log_debugx("connection handed off to the kernel"); 1487 } else { 1488 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY); 1489 discovery(conn); 1490 } 1491 log_debugx("nothing more to do; exiting"); 1492 exit(0); 1493} 1494 1495#ifndef ICL_KERNEL_PROXY 1496static int 1497fd_add(int fd, fd_set *fdset, int nfds) 1498{ 1499 1500 /* 1501 * Skip sockets which we failed to bind. 1502 */ 1503 if (fd <= 0) 1504 return (nfds); 1505 1506 FD_SET(fd, fdset); 1507 if (fd > nfds) 1508 nfds = fd; 1509 return (nfds); 1510} 1511#endif 1512 1513static void 1514main_loop(struct conf *conf, bool dont_fork) 1515{ 1516 struct portal_group *pg; 1517 struct portal *portal; 1518#ifdef ICL_KERNEL_PROXY 1519 int connection_id; 1520#else 1521 fd_set fdset; 1522 int error, nfds, client_fd; 1523#endif 1524 1525 pidfile_write(conf->conf_pidfh); 1526 1527 for (;;) { 1528 if (sighup_received || sigterm_received) 1529 return; 1530 1531#ifdef ICL_KERNEL_PROXY 1532 connection_id = kernel_accept(); 1533 if (connection_id == 0) 1534 continue; 1535 1536 /* 1537 * XXX: This is obviously temporary. 1538 */ 1539 pg = TAILQ_FIRST(&conf->conf_portal_groups); 1540 portal = TAILQ_FIRST(&pg->pg_portals); 1541 1542 handle_connection(portal, connection_id, dont_fork); 1543#else 1544 FD_ZERO(&fdset); 1545 nfds = 0; 1546 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1547 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 1548 nfds = fd_add(portal->p_socket, &fdset, nfds); 1549 } 1550 error = select(nfds + 1, &fdset, NULL, NULL, NULL); 1551 if (error <= 0) { 1552 if (errno == EINTR) 1553 return; 1554 log_err(1, "select"); 1555 } 1556 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1557 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 1558 if (!FD_ISSET(portal->p_socket, &fdset)) 1559 continue; 1560 client_fd = accept(portal->p_socket, NULL, 0); 1561 if (client_fd < 0) 1562 log_err(1, "accept"); 1563 handle_connection(portal, client_fd, dont_fork); 1564 break; 1565 } 1566 } 1567#endif /* !ICL_KERNEL_PROXY */ 1568 } 1569} 1570 1571static void 1572sighup_handler(int dummy __unused) 1573{ 1574 1575 sighup_received = true; 1576} 1577 1578static void 1579sigterm_handler(int dummy __unused) 1580{ 1581 1582 sigterm_received = true; 1583} 1584 1585static void 1586register_signals(void) 1587{ 1588 struct sigaction sa; 1589 int error; 1590 1591 bzero(&sa, sizeof(sa)); 1592 sa.sa_handler = sighup_handler; 1593 sigfillset(&sa.sa_mask); 1594 error = sigaction(SIGHUP, &sa, NULL); 1595 if (error != 0) 1596 log_err(1, "sigaction"); 1597 1598 sa.sa_handler = sigterm_handler; 1599 error = sigaction(SIGTERM, &sa, NULL); 1600 if (error != 0) 1601 log_err(1, "sigaction"); 1602 1603 sa.sa_handler = sigterm_handler; 1604 error = sigaction(SIGINT, &sa, NULL); 1605 if (error != 0) 1606 log_err(1, "sigaction"); 1607} 1608 1609int 1610main(int argc, char **argv) 1611{ 1612 struct conf *oldconf, *newconf, *tmpconf; 1613 const char *config_path = DEFAULT_CONFIG_PATH; 1614 int debug = 0, ch, error; 1615 bool dont_daemonize = false; 1616 1617 while ((ch = getopt(argc, argv, "df:")) != -1) { 1618 switch (ch) { 1619 case 'd': 1620 dont_daemonize = true; 1621 debug++; 1622 break; 1623 case 'f': 1624 config_path = optarg; 1625 break; 1626 case '?': 1627 default: 1628 usage(); 1629 } 1630 } 1631 argc -= optind; 1632 if (argc != 0) 1633 usage(); 1634 1635 log_init(debug); 1636 kernel_init(); 1637 1638 oldconf = conf_new_from_kernel(); 1639 newconf = conf_new_from_file(config_path); 1640 if (newconf == NULL) 1641 log_errx(1, "configuration error, exiting"); 1642 if (debug > 0) { 1643 oldconf->conf_debug = debug; 1644 newconf->conf_debug = debug; 1645 } 1646 1647#ifdef ICL_KERNEL_PROXY 1648 log_debugx("enabling CTL iSCSI port"); 1649 error = kernel_port_on(); 1650 if (error != 0) 1651 log_errx(1, "failed to enable CTL iSCSI port, exiting"); 1652#endif 1653 1654 error = conf_apply(oldconf, newconf); 1655 if (error != 0) 1656 log_errx(1, "failed to apply configuration, exiting"); 1657 conf_delete(oldconf); 1658 oldconf = NULL; 1659 1660 register_signals(); 1661 1662#ifndef ICL_KERNEL_PROXY 1663 log_debugx("enabling CTL iSCSI port"); 1664 error = kernel_port_on(); 1665 if (error != 0) 1666 log_errx(1, "failed to enable CTL iSCSI port, exiting"); 1667#endif 1668 1669 if (dont_daemonize == false) { 1670 log_debugx("daemonizing"); 1671 if (daemon(0, 0) == -1) { 1672 log_warn("cannot daemonize"); 1673 pidfile_remove(newconf->conf_pidfh); 1674 exit(1); 1675 } 1676 } 1677 1678 for (;;) { 1679 main_loop(newconf, dont_daemonize); 1680 if (sighup_received) { 1681 sighup_received = false; 1682 log_debugx("received SIGHUP, reloading configuration"); 1683 tmpconf = conf_new_from_file(config_path); 1684 if (tmpconf == NULL) { 1685 log_warnx("configuration error, " 1686 "continuing with old configuration"); 1687 } else { 1688 if (debug > 0) 1689 tmpconf->conf_debug = debug; 1690 oldconf = newconf; 1691 newconf = tmpconf; 1692 error = conf_apply(oldconf, newconf); 1693 if (error != 0) 1694 log_warnx("failed to reload " 1695 "configuration"); 1696 conf_delete(oldconf); 1697 oldconf = NULL; 1698 } 1699 } else if (sigterm_received) { 1700 log_debugx("exiting on signal; " 1701 "reloading empty configuration"); 1702 1703 log_debugx("disabling CTL iSCSI port " 1704 "and terminating all connections"); 1705 error = kernel_port_off(); 1706 if (error != 0) 1707 log_warnx("failed to disable CTL iSCSI port"); 1708 1709 oldconf = newconf; 1710 newconf = conf_new(); 1711 if (debug > 0) 1712 newconf->conf_debug = debug; 1713 error = conf_apply(oldconf, newconf); 1714 if (error != 0) 1715 log_warnx("failed to apply configuration"); 1716 1717 log_warnx("exiting on signal"); 1718 exit(0); 1719 } else { 1720 nchildren -= wait_for_children(false); 1721 assert(nchildren >= 0); 1722 } 1723 } 1724 /* NOTREACHED */ 1725}
| 952 fprintf(stderr, "}\n"); 953 } 954 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 955 fprintf(stderr, "portal-group %s {\n", pg->pg_name); 956 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 957 fprintf(stderr, "\t listen %s\n", portal->p_listen); 958 fprintf(stderr, "}\n"); 959 } 960 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 961 fprintf(stderr, "target %s {\n", targ->t_iqn); 962 if (targ->t_alias != NULL) 963 fprintf(stderr, "\t alias %s\n", targ->t_alias); 964 TAILQ_FOREACH(lun, &targ->t_luns, l_next) { 965 fprintf(stderr, "\tlun %d {\n", lun->l_lun); 966 fprintf(stderr, "\t\tpath %s\n", lun->l_path); 967 TAILQ_FOREACH(lo, &lun->l_options, lo_next) 968 fprintf(stderr, "\t\toption %s %s\n", 969 lo->lo_name, lo->lo_value); 970 fprintf(stderr, "\t}\n"); 971 } 972 fprintf(stderr, "}\n"); 973 } 974} 975#endif 976 977static int 978conf_verify_lun(struct lun *lun) 979{ 980 const struct lun *lun2; 981 const struct target *targ2; 982 983 if (lun->l_backend == NULL) 984 lun_set_backend(lun, "block"); 985 if (strcmp(lun->l_backend, "block") == 0) { 986 if (lun->l_path == NULL) { 987 log_warnx("missing path for lun %d, target \"%s\"", 988 lun->l_lun, lun->l_target->t_iqn); 989 return (1); 990 } 991 } else if (strcmp(lun->l_backend, "ramdisk") == 0) { 992 if (lun->l_size == 0) { 993 log_warnx("missing size for ramdisk-backed lun %d, " 994 "target \"%s\"", lun->l_lun, lun->l_target->t_iqn); 995 return (1); 996 } 997 if (lun->l_path != NULL) { 998 log_warnx("path must not be specified " 999 "for ramdisk-backed lun %d, target \"%s\"", 1000 lun->l_lun, lun->l_target->t_iqn); 1001 return (1); 1002 } 1003 } 1004 if (lun->l_lun < 0 || lun->l_lun > 255) { 1005 log_warnx("invalid lun number for lun %d, target \"%s\"; " 1006 "must be between 0 and 255", lun->l_lun, 1007 lun->l_target->t_iqn); 1008 return (1); 1009 } 1010 if (lun->l_blocksize == 0) { 1011 lun_set_blocksize(lun, DEFAULT_BLOCKSIZE); 1012 } else if (lun->l_blocksize < 0) { 1013 log_warnx("invalid blocksize for lun %d, target \"%s\"; " 1014 "must be larger than 0", lun->l_lun, lun->l_target->t_iqn); 1015 return (1); 1016 } 1017 if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) { 1018 log_warnx("invalid size for lun %d, target \"%s\"; " 1019 "must be multiple of blocksize", lun->l_lun, 1020 lun->l_target->t_iqn); 1021 return (1); 1022 } 1023 TAILQ_FOREACH(targ2, &lun->l_target->t_conf->conf_targets, t_next) { 1024 TAILQ_FOREACH(lun2, &targ2->t_luns, l_next) { 1025 if (lun == lun2) 1026 continue; 1027 if (lun->l_path != NULL && lun2->l_path != NULL && 1028 strcmp(lun->l_path, lun2->l_path) == 0) { 1029 log_debugx("WARNING: path \"%s\" duplicated " 1030 "between lun %d, target \"%s\", and " 1031 "lun %d, target \"%s\"", lun->l_path, 1032 lun->l_lun, lun->l_target->t_iqn, 1033 lun2->l_lun, lun2->l_target->t_iqn); 1034 } 1035 } 1036 } 1037 1038 return (0); 1039} 1040 1041int 1042conf_verify(struct conf *conf) 1043{ 1044 struct auth_group *ag; 1045 struct portal_group *pg; 1046 struct target *targ; 1047 struct lun *lun; 1048 bool found_lun0; 1049 int error; 1050 1051 if (conf->conf_pidfile_path == NULL) 1052 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE); 1053 1054 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1055 if (targ->t_auth_group == NULL) { 1056 log_warnx("missing authentication for target \"%s\"; " 1057 "must specify either \"auth-group\", \"chap\", " 1058 "or \"chap-mutual\"", targ->t_iqn); 1059 return (1); 1060 } 1061 if (targ->t_portal_group == NULL) { 1062 targ->t_portal_group = portal_group_find(conf, 1063 "default"); 1064 assert(targ->t_portal_group != NULL); 1065 } 1066 found_lun0 = false; 1067 TAILQ_FOREACH(lun, &targ->t_luns, l_next) { 1068 error = conf_verify_lun(lun); 1069 if (error != 0) 1070 return (error); 1071 if (lun->l_lun == 0) 1072 found_lun0 = true; 1073 } 1074 if (!found_lun0) { 1075 log_warnx("mandatory LUN 0 not configured " 1076 "for target \"%s\"", targ->t_iqn); 1077 return (1); 1078 } 1079 } 1080 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1081 assert(pg->pg_name != NULL); 1082 if (pg->pg_discovery_auth_group == NULL) { 1083 pg->pg_discovery_auth_group = 1084 auth_group_find(conf, "no-access"); 1085 assert(pg->pg_discovery_auth_group != NULL); 1086 } 1087 1088 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1089 if (targ->t_portal_group == pg) 1090 break; 1091 } 1092 if (targ == NULL) { 1093 if (strcmp(pg->pg_name, "default") != 0) 1094 log_warnx("portal-group \"%s\" not assigned " 1095 "to any target", pg->pg_name); 1096 pg->pg_unassigned = true; 1097 } else 1098 pg->pg_unassigned = false; 1099 } 1100 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) { 1101 if (ag->ag_name == NULL) 1102 assert(ag->ag_target != NULL); 1103 else 1104 assert(ag->ag_target == NULL); 1105 1106 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) { 1107 if (targ->t_auth_group == ag) 1108 break; 1109 } 1110 if (targ == NULL && ag->ag_name != NULL && 1111 strcmp(ag->ag_name, "no-authentication") != 0 && 1112 strcmp(ag->ag_name, "no-access") != 0) { 1113 log_warnx("auth-group \"%s\" not assigned " 1114 "to any target", ag->ag_name); 1115 } 1116 } 1117 1118 return (0); 1119} 1120 1121static int 1122conf_apply(struct conf *oldconf, struct conf *newconf) 1123{ 1124 struct target *oldtarg, *newtarg, *tmptarg; 1125 struct lun *oldlun, *newlun, *tmplun; 1126 struct portal_group *oldpg, *newpg; 1127 struct portal *oldp, *newp; 1128 pid_t otherpid; 1129 int changed, cumulated_error = 0, error; 1130#ifndef ICL_KERNEL_PROXY 1131 int one = 1; 1132#endif 1133 1134 if (oldconf->conf_debug != newconf->conf_debug) { 1135 log_debugx("changing debug level to %d", newconf->conf_debug); 1136 log_init(newconf->conf_debug); 1137 } 1138 1139 if (oldconf->conf_pidfh != NULL) { 1140 assert(oldconf->conf_pidfile_path != NULL); 1141 if (newconf->conf_pidfile_path != NULL && 1142 strcmp(oldconf->conf_pidfile_path, 1143 newconf->conf_pidfile_path) == 0) { 1144 newconf->conf_pidfh = oldconf->conf_pidfh; 1145 oldconf->conf_pidfh = NULL; 1146 } else { 1147 log_debugx("removing pidfile %s", 1148 oldconf->conf_pidfile_path); 1149 pidfile_remove(oldconf->conf_pidfh); 1150 oldconf->conf_pidfh = NULL; 1151 } 1152 } 1153 1154 if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) { 1155 log_debugx("opening pidfile %s", newconf->conf_pidfile_path); 1156 newconf->conf_pidfh = 1157 pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid); 1158 if (newconf->conf_pidfh == NULL) { 1159 if (errno == EEXIST) 1160 log_errx(1, "daemon already running, pid: %jd.", 1161 (intmax_t)otherpid); 1162 log_err(1, "cannot open or create pidfile \"%s\"", 1163 newconf->conf_pidfile_path); 1164 } 1165 } 1166 1167 TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) { 1168 /* 1169 * First, remove any targets present in the old configuration 1170 * and missing in the new one. 1171 */ 1172 newtarg = target_find(newconf, oldtarg->t_iqn); 1173 if (newtarg == NULL) { 1174 TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, 1175 tmplun) { 1176 log_debugx("target %s not found in new " 1177 "configuration; removing its lun %d, " 1178 "backed by CTL lun %d", 1179 oldtarg->t_iqn, oldlun->l_lun, 1180 oldlun->l_ctl_lun); 1181 error = kernel_lun_remove(oldlun); 1182 if (error != 0) { 1183 log_warnx("failed to remove lun %d, " 1184 "target %s, CTL lun %d", 1185 oldlun->l_lun, oldtarg->t_iqn, 1186 oldlun->l_ctl_lun); 1187 cumulated_error++; 1188 } 1189 lun_delete(oldlun); 1190 } 1191 target_delete(oldtarg); 1192 continue; 1193 } 1194 1195 /* 1196 * Second, remove any LUNs present in the old target 1197 * and missing in the new one. 1198 */ 1199 TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, tmplun) { 1200 newlun = lun_find(newtarg, oldlun->l_lun); 1201 if (newlun == NULL) { 1202 log_debugx("lun %d, target %s, CTL lun %d " 1203 "not found in new configuration; " 1204 "removing", oldlun->l_lun, oldtarg->t_iqn, 1205 oldlun->l_ctl_lun); 1206 error = kernel_lun_remove(oldlun); 1207 if (error != 0) { 1208 log_warnx("failed to remove lun %d, " 1209 "target %s, CTL lun %d", 1210 oldlun->l_lun, oldtarg->t_iqn, 1211 oldlun->l_ctl_lun); 1212 cumulated_error++; 1213 } 1214 lun_delete(oldlun); 1215 continue; 1216 } 1217 1218 /* 1219 * Also remove the LUNs changed by more than size. 1220 */ 1221 changed = 0; 1222 assert(oldlun->l_backend != NULL); 1223 assert(newlun->l_backend != NULL); 1224 if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) { 1225 log_debugx("backend for lun %d, target %s, " 1226 "CTL lun %d changed; removing", 1227 oldlun->l_lun, oldtarg->t_iqn, 1228 oldlun->l_ctl_lun); 1229 changed = 1; 1230 } 1231 if (oldlun->l_blocksize != newlun->l_blocksize) { 1232 log_debugx("blocksize for lun %d, target %s, " 1233 "CTL lun %d changed; removing", 1234 oldlun->l_lun, oldtarg->t_iqn, 1235 oldlun->l_ctl_lun); 1236 changed = 1; 1237 } 1238 if (newlun->l_device_id != NULL && 1239 (oldlun->l_device_id == NULL || 1240 strcmp(oldlun->l_device_id, newlun->l_device_id) != 1241 0)) { 1242 log_debugx("device-id for lun %d, target %s, " 1243 "CTL lun %d changed; removing", 1244 oldlun->l_lun, oldtarg->t_iqn, 1245 oldlun->l_ctl_lun); 1246 changed = 1; 1247 } 1248 if (newlun->l_path != NULL && 1249 (oldlun->l_path == NULL || 1250 strcmp(oldlun->l_path, newlun->l_path) != 0)) { 1251 log_debugx("path for lun %d, target %s, " 1252 "CTL lun %d, changed; removing", 1253 oldlun->l_lun, oldtarg->t_iqn, 1254 oldlun->l_ctl_lun); 1255 changed = 1; 1256 } 1257 if (newlun->l_serial != NULL && 1258 (oldlun->l_serial == NULL || 1259 strcmp(oldlun->l_serial, newlun->l_serial) != 0)) { 1260 log_debugx("serial for lun %d, target %s, " 1261 "CTL lun %d changed; removing", 1262 oldlun->l_lun, oldtarg->t_iqn, 1263 oldlun->l_ctl_lun); 1264 changed = 1; 1265 } 1266 if (changed) { 1267 error = kernel_lun_remove(oldlun); 1268 if (error != 0) { 1269 log_warnx("failed to remove lun %d, " 1270 "target %s, CTL lun %d", 1271 oldlun->l_lun, oldtarg->t_iqn, 1272 oldlun->l_ctl_lun); 1273 cumulated_error++; 1274 } 1275 lun_delete(oldlun); 1276 continue; 1277 } 1278 1279 lun_set_ctl_lun(newlun, oldlun->l_ctl_lun); 1280 } 1281 } 1282 1283 /* 1284 * Now add new targets or modify existing ones. 1285 */ 1286 TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) { 1287 oldtarg = target_find(oldconf, newtarg->t_iqn); 1288 1289 TAILQ_FOREACH(newlun, &newtarg->t_luns, l_next) { 1290 if (oldtarg != NULL) { 1291 oldlun = lun_find(oldtarg, newlun->l_lun); 1292 if (oldlun != NULL) { 1293 if (newlun->l_size != oldlun->l_size) { 1294 log_debugx("resizing lun %d, " 1295 "target %s, CTL lun %d", 1296 newlun->l_lun, 1297 newtarg->t_iqn, 1298 newlun->l_ctl_lun); 1299 error = 1300 kernel_lun_resize(newlun); 1301 if (error != 0) { 1302 log_warnx("failed to " 1303 "resize lun %d, " 1304 "target %s, " 1305 "CTL lun %d", 1306 newlun->l_lun, 1307 newtarg->t_iqn, 1308 newlun->l_lun); 1309 cumulated_error++; 1310 } 1311 } 1312 continue; 1313 } 1314 } 1315 log_debugx("adding lun %d, target %s", 1316 newlun->l_lun, newtarg->t_iqn); 1317 error = kernel_lun_add(newlun); 1318 if (error != 0) { 1319 log_warnx("failed to add lun %d, target %s", 1320 newlun->l_lun, newtarg->t_iqn); 1321 cumulated_error++; 1322 } 1323 } 1324 } 1325 1326 /* 1327 * Go through the new portals, opening the sockets as neccessary. 1328 */ 1329 TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) { 1330 if (newpg->pg_unassigned) { 1331 log_debugx("not listening on portal-group \"%s\", " 1332 "not assigned to any target", 1333 newpg->pg_name); 1334 continue; 1335 } 1336 TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) { 1337 /* 1338 * Try to find already open portal and reuse 1339 * the listening socket. We don't care about 1340 * what portal or portal group that was, what 1341 * matters is the listening address. 1342 */ 1343 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, 1344 pg_next) { 1345 TAILQ_FOREACH(oldp, &oldpg->pg_portals, 1346 p_next) { 1347 if (strcmp(newp->p_listen, 1348 oldp->p_listen) == 0 && 1349 oldp->p_socket > 0) { 1350 newp->p_socket = 1351 oldp->p_socket; 1352 oldp->p_socket = 0; 1353 break; 1354 } 1355 } 1356 } 1357 if (newp->p_socket > 0) { 1358 /* 1359 * We're done with this portal. 1360 */ 1361 continue; 1362 } 1363 1364#ifdef ICL_KERNEL_PROXY 1365 log_debugx("listening on %s, portal-group \"%s\" using ICL proxy", 1366 newp->p_listen, newpg->pg_name); 1367 kernel_listen(newp->p_ai, newp->p_iser); 1368#else 1369 assert(newp->p_iser == false); 1370 1371 log_debugx("listening on %s, portal-group \"%s\"", 1372 newp->p_listen, newpg->pg_name); 1373 newp->p_socket = socket(newp->p_ai->ai_family, 1374 newp->p_ai->ai_socktype, 1375 newp->p_ai->ai_protocol); 1376 if (newp->p_socket < 0) { 1377 log_warn("socket(2) failed for %s", 1378 newp->p_listen); 1379 cumulated_error++; 1380 continue; 1381 } 1382 error = setsockopt(newp->p_socket, SOL_SOCKET, 1383 SO_REUSEADDR, &one, sizeof(one)); 1384 if (error != 0) { 1385 log_warn("setsockopt(SO_REUSEADDR) failed " 1386 "for %s", newp->p_listen); 1387 close(newp->p_socket); 1388 newp->p_socket = 0; 1389 cumulated_error++; 1390 continue; 1391 } 1392 error = bind(newp->p_socket, newp->p_ai->ai_addr, 1393 newp->p_ai->ai_addrlen); 1394 if (error != 0) { 1395 log_warn("bind(2) failed for %s", 1396 newp->p_listen); 1397 close(newp->p_socket); 1398 newp->p_socket = 0; 1399 cumulated_error++; 1400 continue; 1401 } 1402 error = listen(newp->p_socket, -1); 1403 if (error != 0) { 1404 log_warn("listen(2) failed for %s", 1405 newp->p_listen); 1406 close(newp->p_socket); 1407 newp->p_socket = 0; 1408 cumulated_error++; 1409 continue; 1410 } 1411#endif /* !ICL_KERNEL_PROXY */ 1412 } 1413 } 1414 1415 /* 1416 * Go through the no longer used sockets, closing them. 1417 */ 1418 TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) { 1419 TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) { 1420 if (oldp->p_socket <= 0) 1421 continue; 1422 log_debugx("closing socket for %s, portal-group \"%s\"", 1423 oldp->p_listen, oldpg->pg_name); 1424 close(oldp->p_socket); 1425 oldp->p_socket = 0; 1426 } 1427 } 1428 1429 return (cumulated_error); 1430} 1431 1432bool 1433timed_out(void) 1434{ 1435 1436 return (sigalrm_received); 1437} 1438 1439static void 1440sigalrm_handler(int dummy __unused) 1441{ 1442 /* 1443 * It would be easiest to just log an error and exit. We can't 1444 * do this, though, because log_errx() is not signal safe, since 1445 * it calls syslog(3). Instead, set a flag checked by pdu_send() 1446 * and pdu_receive(), to call log_errx() there. Should they fail 1447 * to notice, we'll exit here one second later. 1448 */ 1449 if (sigalrm_received) { 1450 /* 1451 * Oh well. Just give up and quit. 1452 */ 1453 _exit(2); 1454 } 1455 1456 sigalrm_received = true; 1457} 1458 1459static void 1460set_timeout(const struct conf *conf) 1461{ 1462 struct sigaction sa; 1463 struct itimerval itv; 1464 int error; 1465 1466 if (conf->conf_timeout <= 0) { 1467 log_debugx("session timeout disabled"); 1468 return; 1469 } 1470 1471 bzero(&sa, sizeof(sa)); 1472 sa.sa_handler = sigalrm_handler; 1473 sigfillset(&sa.sa_mask); 1474 error = sigaction(SIGALRM, &sa, NULL); 1475 if (error != 0) 1476 log_err(1, "sigaction"); 1477 1478 /* 1479 * First SIGALRM will arive after conf_timeout seconds. 1480 * If we do nothing, another one will arrive a second later. 1481 */ 1482 bzero(&itv, sizeof(itv)); 1483 itv.it_interval.tv_sec = 1; 1484 itv.it_value.tv_sec = conf->conf_timeout; 1485 1486 log_debugx("setting session timeout to %d seconds", 1487 conf->conf_timeout); 1488 error = setitimer(ITIMER_REAL, &itv, NULL); 1489 if (error != 0) 1490 log_err(1, "setitimer"); 1491} 1492 1493static int 1494wait_for_children(bool block) 1495{ 1496 pid_t pid; 1497 int status; 1498 int num = 0; 1499 1500 for (;;) { 1501 /* 1502 * If "block" is true, wait for at least one process. 1503 */ 1504 if (block && num == 0) 1505 pid = wait4(-1, &status, 0, NULL); 1506 else 1507 pid = wait4(-1, &status, WNOHANG, NULL); 1508 if (pid <= 0) 1509 break; 1510 if (WIFSIGNALED(status)) { 1511 log_warnx("child process %d terminated with signal %d", 1512 pid, WTERMSIG(status)); 1513 } else if (WEXITSTATUS(status) != 0) { 1514 log_warnx("child process %d terminated with exit status %d", 1515 pid, WEXITSTATUS(status)); 1516 } else { 1517 log_debugx("child process %d terminated gracefully", pid); 1518 } 1519 num++; 1520 } 1521 1522 return (num); 1523} 1524 1525static void 1526handle_connection(struct portal *portal, int fd, bool dont_fork) 1527{ 1528 struct connection *conn; 1529#ifndef ICL_KERNEL_PROXY 1530 struct sockaddr_storage ss; 1531 socklen_t sslen = sizeof(ss); 1532 int error; 1533#endif 1534 pid_t pid; 1535 char host[NI_MAXHOST + 1]; 1536 struct conf *conf; 1537 1538 conf = portal->p_portal_group->pg_conf; 1539 1540 if (dont_fork) { 1541 log_debugx("incoming connection; not forking due to -d flag"); 1542 } else { 1543 nchildren -= wait_for_children(false); 1544 assert(nchildren >= 0); 1545 1546 while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) { 1547 log_debugx("maxproc limit of %d child processes hit; " 1548 "waiting for child process to exit", conf->conf_maxproc); 1549 nchildren -= wait_for_children(true); 1550 assert(nchildren >= 0); 1551 } 1552 log_debugx("incoming connection; forking child process #%d", 1553 nchildren); 1554 nchildren++; 1555 pid = fork(); 1556 if (pid < 0) 1557 log_err(1, "fork"); 1558 if (pid > 0) { 1559 close(fd); 1560 return; 1561 } 1562 } 1563 pidfile_close(conf->conf_pidfh); 1564 1565#ifdef ICL_KERNEL_PROXY 1566 /* 1567 * XXX 1568 */ 1569 log_set_peer_addr("XXX"); 1570#else 1571 error = getpeername(fd, (struct sockaddr *)&ss, &sslen); 1572 if (error != 0) 1573 log_err(1, "getpeername"); 1574 error = getnameinfo((struct sockaddr *)&ss, sslen, 1575 host, sizeof(host), NULL, 0, NI_NUMERICHOST); 1576 if (error != 0) 1577 log_errx(1, "getaddrinfo: %s", gai_strerror(error)); 1578 1579 log_debugx("accepted connection from %s; portal group \"%s\"", 1580 host, portal->p_portal_group->pg_name); 1581 log_set_peer_addr(host); 1582 setproctitle("%s", host); 1583#endif 1584 1585 conn = connection_new(portal, fd, host); 1586 set_timeout(conf); 1587 kernel_capsicate(); 1588 login(conn); 1589 if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { 1590 kernel_handoff(conn); 1591 log_debugx("connection handed off to the kernel"); 1592 } else { 1593 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY); 1594 discovery(conn); 1595 } 1596 log_debugx("nothing more to do; exiting"); 1597 exit(0); 1598} 1599 1600#ifndef ICL_KERNEL_PROXY 1601static int 1602fd_add(int fd, fd_set *fdset, int nfds) 1603{ 1604 1605 /* 1606 * Skip sockets which we failed to bind. 1607 */ 1608 if (fd <= 0) 1609 return (nfds); 1610 1611 FD_SET(fd, fdset); 1612 if (fd > nfds) 1613 nfds = fd; 1614 return (nfds); 1615} 1616#endif 1617 1618static void 1619main_loop(struct conf *conf, bool dont_fork) 1620{ 1621 struct portal_group *pg; 1622 struct portal *portal; 1623#ifdef ICL_KERNEL_PROXY 1624 int connection_id; 1625#else 1626 fd_set fdset; 1627 int error, nfds, client_fd; 1628#endif 1629 1630 pidfile_write(conf->conf_pidfh); 1631 1632 for (;;) { 1633 if (sighup_received || sigterm_received) 1634 return; 1635 1636#ifdef ICL_KERNEL_PROXY 1637 connection_id = kernel_accept(); 1638 if (connection_id == 0) 1639 continue; 1640 1641 /* 1642 * XXX: This is obviously temporary. 1643 */ 1644 pg = TAILQ_FIRST(&conf->conf_portal_groups); 1645 portal = TAILQ_FIRST(&pg->pg_portals); 1646 1647 handle_connection(portal, connection_id, dont_fork); 1648#else 1649 FD_ZERO(&fdset); 1650 nfds = 0; 1651 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1652 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) 1653 nfds = fd_add(portal->p_socket, &fdset, nfds); 1654 } 1655 error = select(nfds + 1, &fdset, NULL, NULL, NULL); 1656 if (error <= 0) { 1657 if (errno == EINTR) 1658 return; 1659 log_err(1, "select"); 1660 } 1661 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) { 1662 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) { 1663 if (!FD_ISSET(portal->p_socket, &fdset)) 1664 continue; 1665 client_fd = accept(portal->p_socket, NULL, 0); 1666 if (client_fd < 0) 1667 log_err(1, "accept"); 1668 handle_connection(portal, client_fd, dont_fork); 1669 break; 1670 } 1671 } 1672#endif /* !ICL_KERNEL_PROXY */ 1673 } 1674} 1675 1676static void 1677sighup_handler(int dummy __unused) 1678{ 1679 1680 sighup_received = true; 1681} 1682 1683static void 1684sigterm_handler(int dummy __unused) 1685{ 1686 1687 sigterm_received = true; 1688} 1689 1690static void 1691register_signals(void) 1692{ 1693 struct sigaction sa; 1694 int error; 1695 1696 bzero(&sa, sizeof(sa)); 1697 sa.sa_handler = sighup_handler; 1698 sigfillset(&sa.sa_mask); 1699 error = sigaction(SIGHUP, &sa, NULL); 1700 if (error != 0) 1701 log_err(1, "sigaction"); 1702 1703 sa.sa_handler = sigterm_handler; 1704 error = sigaction(SIGTERM, &sa, NULL); 1705 if (error != 0) 1706 log_err(1, "sigaction"); 1707 1708 sa.sa_handler = sigterm_handler; 1709 error = sigaction(SIGINT, &sa, NULL); 1710 if (error != 0) 1711 log_err(1, "sigaction"); 1712} 1713 1714int 1715main(int argc, char **argv) 1716{ 1717 struct conf *oldconf, *newconf, *tmpconf; 1718 const char *config_path = DEFAULT_CONFIG_PATH; 1719 int debug = 0, ch, error; 1720 bool dont_daemonize = false; 1721 1722 while ((ch = getopt(argc, argv, "df:")) != -1) { 1723 switch (ch) { 1724 case 'd': 1725 dont_daemonize = true; 1726 debug++; 1727 break; 1728 case 'f': 1729 config_path = optarg; 1730 break; 1731 case '?': 1732 default: 1733 usage(); 1734 } 1735 } 1736 argc -= optind; 1737 if (argc != 0) 1738 usage(); 1739 1740 log_init(debug); 1741 kernel_init(); 1742 1743 oldconf = conf_new_from_kernel(); 1744 newconf = conf_new_from_file(config_path); 1745 if (newconf == NULL) 1746 log_errx(1, "configuration error, exiting"); 1747 if (debug > 0) { 1748 oldconf->conf_debug = debug; 1749 newconf->conf_debug = debug; 1750 } 1751 1752#ifdef ICL_KERNEL_PROXY 1753 log_debugx("enabling CTL iSCSI port"); 1754 error = kernel_port_on(); 1755 if (error != 0) 1756 log_errx(1, "failed to enable CTL iSCSI port, exiting"); 1757#endif 1758 1759 error = conf_apply(oldconf, newconf); 1760 if (error != 0) 1761 log_errx(1, "failed to apply configuration, exiting"); 1762 conf_delete(oldconf); 1763 oldconf = NULL; 1764 1765 register_signals(); 1766 1767#ifndef ICL_KERNEL_PROXY 1768 log_debugx("enabling CTL iSCSI port"); 1769 error = kernel_port_on(); 1770 if (error != 0) 1771 log_errx(1, "failed to enable CTL iSCSI port, exiting"); 1772#endif 1773 1774 if (dont_daemonize == false) { 1775 log_debugx("daemonizing"); 1776 if (daemon(0, 0) == -1) { 1777 log_warn("cannot daemonize"); 1778 pidfile_remove(newconf->conf_pidfh); 1779 exit(1); 1780 } 1781 } 1782 1783 for (;;) { 1784 main_loop(newconf, dont_daemonize); 1785 if (sighup_received) { 1786 sighup_received = false; 1787 log_debugx("received SIGHUP, reloading configuration"); 1788 tmpconf = conf_new_from_file(config_path); 1789 if (tmpconf == NULL) { 1790 log_warnx("configuration error, " 1791 "continuing with old configuration"); 1792 } else { 1793 if (debug > 0) 1794 tmpconf->conf_debug = debug; 1795 oldconf = newconf; 1796 newconf = tmpconf; 1797 error = conf_apply(oldconf, newconf); 1798 if (error != 0) 1799 log_warnx("failed to reload " 1800 "configuration"); 1801 conf_delete(oldconf); 1802 oldconf = NULL; 1803 } 1804 } else if (sigterm_received) { 1805 log_debugx("exiting on signal; " 1806 "reloading empty configuration"); 1807 1808 log_debugx("disabling CTL iSCSI port " 1809 "and terminating all connections"); 1810 error = kernel_port_off(); 1811 if (error != 0) 1812 log_warnx("failed to disable CTL iSCSI port"); 1813 1814 oldconf = newconf; 1815 newconf = conf_new(); 1816 if (debug > 0) 1817 newconf->conf_debug = debug; 1818 error = conf_apply(oldconf, newconf); 1819 if (error != 0) 1820 log_warnx("failed to apply configuration"); 1821 1822 log_warnx("exiting on signal"); 1823 exit(0); 1824 } else { 1825 nchildren -= wait_for_children(false); 1826 assert(nchildren >= 0); 1827 } 1828 } 1829 /* NOTREACHED */ 1830}
|