iscsid.c revision 269065
1255570Strasz/*- 2255570Strasz * Copyright (c) 2012 The FreeBSD Foundation 3255570Strasz * All rights reserved. 4255570Strasz * 5255570Strasz * This software was developed by Edward Tomasz Napierala under sponsorship 6255570Strasz * from the FreeBSD Foundation. 7255570Strasz * 8255570Strasz * Redistribution and use in source and binary forms, with or without 9255570Strasz * modification, are permitted provided that the following conditions 10255570Strasz * are met: 11255570Strasz * 1. Redistributions of source code must retain the above copyright 12255570Strasz * notice, this list of conditions and the following disclaimer. 13255570Strasz * 2. Redistributions in binary form must reproduce the above copyright 14255570Strasz * notice, this list of conditions and the following disclaimer in the 15255570Strasz * documentation and/or other materials provided with the distribution. 16255570Strasz * 17255570Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18255570Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19255570Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20255570Strasz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21255570Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22255570Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23255570Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24255570Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25255570Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26255570Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27255570Strasz * SUCH DAMAGE. 28255570Strasz * 29255570Strasz * $FreeBSD: stable/10/usr.sbin/iscsid/iscsid.c 269065 2014-07-24 15:31:45Z mav $ 30255570Strasz */ 31255570Strasz 32255570Strasz#include <sys/types.h> 33255570Strasz#include <sys/time.h> 34255570Strasz#include <sys/ioctl.h> 35255570Strasz#include <sys/param.h> 36255570Strasz#include <sys/linker.h> 37255570Strasz#include <sys/socket.h> 38255570Strasz#include <sys/capability.h> 39255570Strasz#include <sys/wait.h> 40255570Strasz#include <assert.h> 41255570Strasz#include <errno.h> 42255570Strasz#include <fcntl.h> 43255570Strasz#include <netdb.h> 44255570Strasz#include <signal.h> 45255570Strasz#include <stdbool.h> 46255570Strasz#include <stdint.h> 47255570Strasz#include <stdio.h> 48255570Strasz#include <stdlib.h> 49255570Strasz#include <string.h> 50255570Strasz#include <unistd.h> 51255570Strasz 52255570Strasz#include <libutil.h> 53255570Strasz 54255570Strasz#include "iscsid.h" 55255570Strasz 56255570Straszstatic volatile bool sigalrm_received = false; 57255570Strasz 58255570Straszstatic int nchildren = 0; 59255570Strasz 60255570Straszstatic void 61255570Straszusage(void) 62255570Strasz{ 63255570Strasz 64255570Strasz fprintf(stderr, "usage: iscsid [-P pidfile][-d][-m maxproc][-t timeout]\n"); 65255570Strasz exit(1); 66255570Strasz} 67255570Strasz 68255570Straszchar * 69255570Straszchecked_strdup(const char *s) 70255570Strasz{ 71255570Strasz char *c; 72255570Strasz 73255570Strasz c = strdup(s); 74255570Strasz if (c == NULL) 75255570Strasz log_err(1, "strdup"); 76255570Strasz return (c); 77255570Strasz} 78255570Strasz 79255636Straszstatic void 80255636Straszresolve_addr(const struct connection *conn, const char *address, 81255636Strasz struct addrinfo **ai, bool initiator_side) 82255570Strasz{ 83255570Strasz struct addrinfo hints; 84255570Strasz char *arg, *addr, *ch; 85255570Strasz const char *port; 86255570Strasz int error, colons = 0; 87255570Strasz 88255570Strasz arg = checked_strdup(address); 89255570Strasz 90255570Strasz if (arg[0] == '\0') { 91255636Strasz fail(conn, "empty address"); 92255636Strasz log_errx(1, "empty address"); 93255570Strasz } 94255570Strasz if (arg[0] == '[') { 95255570Strasz /* 96255570Strasz * IPv6 address in square brackets, perhaps with port. 97255570Strasz */ 98255570Strasz arg++; 99255570Strasz addr = strsep(&arg, "]"); 100255570Strasz if (arg == NULL) { 101255636Strasz fail(conn, "malformed address"); 102255636Strasz log_errx(1, "malformed address %s", address); 103255570Strasz } 104255570Strasz if (arg[0] == '\0') { 105255636Strasz port = NULL; 106255570Strasz } else if (arg[0] == ':') { 107255570Strasz port = arg + 1; 108255570Strasz } else { 109255636Strasz fail(conn, "malformed address"); 110255636Strasz log_errx(1, "malformed address %s", address); 111255570Strasz } 112255570Strasz } else { 113255570Strasz /* 114255570Strasz * Either IPv6 address without brackets - and without 115255570Strasz * a port - or IPv4 address. Just count the colons. 116255570Strasz */ 117255570Strasz for (ch = arg; *ch != '\0'; ch++) { 118255570Strasz if (*ch == ':') 119255570Strasz colons++; 120255570Strasz } 121255570Strasz if (colons > 1) { 122255570Strasz addr = arg; 123255636Strasz port = NULL; 124255570Strasz } else { 125255570Strasz addr = strsep(&arg, ":"); 126255570Strasz if (arg == NULL) 127255636Strasz port = NULL; 128255570Strasz else 129255570Strasz port = arg; 130255570Strasz } 131255570Strasz } 132255570Strasz 133255636Strasz if (port == NULL && !initiator_side) 134255636Strasz port = "3260"; 135255636Strasz 136255570Strasz memset(&hints, 0, sizeof(hints)); 137255570Strasz hints.ai_family = PF_UNSPEC; 138255570Strasz hints.ai_socktype = SOCK_STREAM; 139255636Strasz hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV; 140255636Strasz if (initiator_side) 141255636Strasz hints.ai_flags |= AI_PASSIVE; 142255570Strasz 143255570Strasz error = getaddrinfo(addr, port, &hints, ai); 144255570Strasz if (error != 0) { 145255636Strasz fail(conn, gai_strerror(error)); 146255636Strasz log_errx(1, "getaddrinfo for %s failed: %s", 147255570Strasz address, gai_strerror(error)); 148255570Strasz } 149255570Strasz} 150255570Strasz 151255570Straszstatic struct connection * 152268703Smavconnection_new(unsigned int session_id, const uint8_t isid[8], uint16_t tsih, 153268703Smav const struct iscsi_session_conf *conf, int iscsi_fd) 154255570Strasz{ 155255570Strasz struct connection *conn; 156255570Strasz struct addrinfo *from_ai, *to_ai; 157255570Strasz const char *from_addr, *to_addr; 158255570Strasz#ifdef ICL_KERNEL_PROXY 159255678Strasz struct iscsi_daemon_connect idc; 160255570Strasz#endif 161255570Strasz int error; 162255570Strasz 163255570Strasz conn = calloc(1, sizeof(*conn)); 164255570Strasz if (conn == NULL) 165255570Strasz log_err(1, "calloc"); 166255570Strasz 167255570Strasz /* 168255570Strasz * Default values, from RFC 3720, section 12. 169255570Strasz */ 170255570Strasz conn->conn_header_digest = CONN_DIGEST_NONE; 171255570Strasz conn->conn_data_digest = CONN_DIGEST_NONE; 172255570Strasz conn->conn_initial_r2t = true; 173255570Strasz conn->conn_immediate_data = true; 174255570Strasz conn->conn_max_data_segment_length = 8192; 175255570Strasz conn->conn_max_burst_length = 262144; 176255570Strasz conn->conn_first_burst_length = 65536; 177255570Strasz 178255570Strasz conn->conn_session_id = session_id; 179268703Smav memcpy(&conn->conn_isid, isid, sizeof(conn->conn_isid)); 180268703Smav conn->conn_tsih = tsih; 181255636Strasz conn->conn_iscsi_fd = iscsi_fd; 182255636Strasz 183255570Strasz /* 184255570Strasz * XXX: Should we sanitize this somehow? 185255570Strasz */ 186255570Strasz memcpy(&conn->conn_conf, conf, sizeof(conn->conn_conf)); 187255570Strasz 188255570Strasz from_addr = conn->conn_conf.isc_initiator_addr; 189255570Strasz to_addr = conn->conn_conf.isc_target_addr; 190255570Strasz 191255636Strasz if (from_addr[0] != '\0') 192255636Strasz resolve_addr(conn, from_addr, &from_ai, true); 193255636Strasz else 194255570Strasz from_ai = NULL; 195255570Strasz 196255636Strasz resolve_addr(conn, to_addr, &to_ai, false); 197255570Strasz 198255570Strasz#ifdef ICL_KERNEL_PROXY 199265526Strasz if (conn->conn_conf.isc_iser) { 200265526Strasz memset(&idc, 0, sizeof(idc)); 201265526Strasz idc.idc_session_id = conn->conn_session_id; 202265526Strasz if (conn->conn_conf.isc_iser) 203265526Strasz idc.idc_iser = 1; 204265526Strasz idc.idc_domain = to_ai->ai_family; 205265526Strasz idc.idc_socktype = to_ai->ai_socktype; 206265526Strasz idc.idc_protocol = to_ai->ai_protocol; 207265526Strasz if (from_ai != NULL) { 208265526Strasz idc.idc_from_addr = from_ai->ai_addr; 209265526Strasz idc.idc_from_addrlen = from_ai->ai_addrlen; 210265526Strasz } 211265526Strasz idc.idc_to_addr = to_ai->ai_addr; 212265526Strasz idc.idc_to_addrlen = to_ai->ai_addrlen; 213255570Strasz 214265526Strasz log_debugx("connecting to %s using ICL kernel proxy", to_addr); 215265526Strasz error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc); 216265526Strasz if (error != 0) { 217265526Strasz fail(conn, strerror(errno)); 218265526Strasz log_err(1, "failed to connect to %s " 219265526Strasz "using ICL kernel proxy: ISCSIDCONNECT", to_addr); 220265526Strasz } 221255570Strasz 222265526Strasz return (conn); 223255570Strasz } 224265526Strasz#endif /* ICL_KERNEL_PROXY */ 225255570Strasz 226255636Strasz if (conn->conn_conf.isc_iser) { 227255636Strasz fail(conn, "iSER not supported"); 228255570Strasz log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY " 229255570Strasz "does not support iSER"); 230255636Strasz } 231255570Strasz 232255570Strasz conn->conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype, 233255570Strasz to_ai->ai_protocol); 234255636Strasz if (conn->conn_socket < 0) { 235255636Strasz fail(conn, strerror(errno)); 236255570Strasz log_err(1, "failed to create socket for %s", from_addr); 237255636Strasz } 238255570Strasz if (from_ai != NULL) { 239255570Strasz error = bind(conn->conn_socket, from_ai->ai_addr, 240255570Strasz from_ai->ai_addrlen); 241255636Strasz if (error != 0) { 242255636Strasz fail(conn, strerror(errno)); 243255570Strasz log_err(1, "failed to bind to %s", from_addr); 244255636Strasz } 245255570Strasz } 246255570Strasz log_debugx("connecting to %s", to_addr); 247255570Strasz error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen); 248255570Strasz if (error != 0) { 249255570Strasz fail(conn, strerror(errno)); 250255570Strasz log_err(1, "failed to connect to %s", to_addr); 251255570Strasz } 252255570Strasz 253255570Strasz return (conn); 254255570Strasz} 255255570Strasz 256255570Straszstatic void 257255570Straszhandoff(struct connection *conn) 258255570Strasz{ 259255678Strasz struct iscsi_daemon_handoff idh; 260255570Strasz int error; 261255570Strasz 262255570Strasz log_debugx("handing off connection to the kernel"); 263255570Strasz 264255678Strasz memset(&idh, 0, sizeof(idh)); 265255678Strasz idh.idh_session_id = conn->conn_session_id; 266255678Strasz idh.idh_socket = conn->conn_socket; 267255678Strasz strlcpy(idh.idh_target_alias, conn->conn_target_alias, 268255678Strasz sizeof(idh.idh_target_alias)); 269268703Smav idh.idh_tsih = conn->conn_tsih; 270255678Strasz idh.idh_statsn = conn->conn_statsn; 271255678Strasz idh.idh_header_digest = conn->conn_header_digest; 272255678Strasz idh.idh_data_digest = conn->conn_data_digest; 273255678Strasz idh.idh_initial_r2t = conn->conn_initial_r2t; 274255678Strasz idh.idh_immediate_data = conn->conn_immediate_data; 275255678Strasz idh.idh_max_data_segment_length = conn->conn_max_data_segment_length; 276255678Strasz idh.idh_max_burst_length = conn->conn_max_burst_length; 277255678Strasz idh.idh_first_burst_length = conn->conn_first_burst_length; 278255570Strasz 279255678Strasz error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh); 280255570Strasz if (error != 0) 281255570Strasz log_err(1, "ISCSIDHANDOFF"); 282255570Strasz} 283255570Strasz 284255570Straszvoid 285255570Straszfail(const struct connection *conn, const char *reason) 286255570Strasz{ 287255678Strasz struct iscsi_daemon_fail idf; 288255570Strasz int error; 289255570Strasz 290255678Strasz memset(&idf, 0, sizeof(idf)); 291255678Strasz idf.idf_session_id = conn->conn_session_id; 292255678Strasz strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason)); 293255570Strasz 294255678Strasz error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf); 295255570Strasz if (error != 0) 296255570Strasz log_err(1, "ISCSIDFAIL"); 297255570Strasz} 298255570Strasz 299255570Strasz/* 300255570Strasz * XXX: I CANT INTO LATIN 301255570Strasz */ 302255570Straszstatic void 303255570Straszcapsicate(struct connection *conn) 304255570Strasz{ 305255570Strasz int error; 306255570Strasz cap_rights_t rights; 307255570Strasz#ifdef ICL_KERNEL_PROXY 308255570Strasz const unsigned long cmds[] = { ISCSIDCONNECT, ISCSIDSEND, ISCSIDRECEIVE, 309269065Smav ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE, ISCSISMODIFY }; 310255570Strasz#else 311255570Strasz const unsigned long cmds[] = { ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, 312269065Smav ISCSISREMOVE, ISCSISMODIFY }; 313255570Strasz#endif 314255570Strasz 315255570Strasz cap_rights_init(&rights, CAP_IOCTL); 316255570Strasz error = cap_rights_limit(conn->conn_iscsi_fd, &rights); 317255570Strasz if (error != 0 && errno != ENOSYS) 318255570Strasz log_err(1, "cap_rights_limit"); 319255570Strasz 320255570Strasz error = cap_ioctls_limit(conn->conn_iscsi_fd, cmds, 321255570Strasz sizeof(cmds) / sizeof(cmds[0])); 322255570Strasz if (error != 0 && errno != ENOSYS) 323255570Strasz log_err(1, "cap_ioctls_limit"); 324255570Strasz 325255570Strasz error = cap_enter(); 326255570Strasz if (error != 0 && errno != ENOSYS) 327255570Strasz log_err(1, "cap_enter"); 328255570Strasz 329255570Strasz if (cap_sandboxed()) 330255570Strasz log_debugx("Capsicum capability mode enabled"); 331255570Strasz else 332255570Strasz log_warnx("Capsicum capability mode not supported"); 333255570Strasz} 334255570Strasz 335255570Straszbool 336255570Strasztimed_out(void) 337255570Strasz{ 338255570Strasz 339255570Strasz return (sigalrm_received); 340255570Strasz} 341255570Strasz 342255570Straszstatic void 343255570Straszsigalrm_handler(int dummy __unused) 344255570Strasz{ 345255570Strasz /* 346255570Strasz * It would be easiest to just log an error and exit. We can't 347255570Strasz * do this, though, because log_errx() is not signal safe, since 348255570Strasz * it calls syslog(3). Instead, set a flag checked by pdu_send() 349255570Strasz * and pdu_receive(), to call log_errx() there. Should they fail 350255570Strasz * to notice, we'll exit here one second later. 351255570Strasz */ 352255570Strasz if (sigalrm_received) { 353255570Strasz /* 354255570Strasz * Oh well. Just give up and quit. 355255570Strasz */ 356255570Strasz _exit(2); 357255570Strasz } 358255570Strasz 359255570Strasz sigalrm_received = true; 360255570Strasz} 361255570Strasz 362255570Straszstatic void 363255570Straszset_timeout(int timeout) 364255570Strasz{ 365255570Strasz struct sigaction sa; 366255570Strasz struct itimerval itv; 367255570Strasz int error; 368255570Strasz 369255570Strasz if (timeout <= 0) { 370255570Strasz log_debugx("session timeout disabled"); 371255570Strasz return; 372255570Strasz } 373255570Strasz 374255570Strasz bzero(&sa, sizeof(sa)); 375255570Strasz sa.sa_handler = sigalrm_handler; 376255570Strasz sigfillset(&sa.sa_mask); 377255570Strasz error = sigaction(SIGALRM, &sa, NULL); 378255570Strasz if (error != 0) 379255570Strasz log_err(1, "sigaction"); 380255570Strasz 381255570Strasz /* 382255570Strasz * First SIGALRM will arive after conf_timeout seconds. 383255570Strasz * If we do nothing, another one will arrive a second later. 384255570Strasz */ 385255570Strasz bzero(&itv, sizeof(itv)); 386255570Strasz itv.it_interval.tv_sec = 1; 387255570Strasz itv.it_value.tv_sec = timeout; 388255570Strasz 389255570Strasz log_debugx("setting session timeout to %d seconds", 390255570Strasz timeout); 391255570Strasz error = setitimer(ITIMER_REAL, &itv, NULL); 392255570Strasz if (error != 0) 393255570Strasz log_err(1, "setitimer"); 394255570Strasz} 395255570Strasz 396255570Straszstatic void 397262846Straszsigchld_handler(int dummy __unused) 398262846Strasz{ 399262846Strasz 400262846Strasz /* 401262846Strasz * The only purpose of this handler is to make SIGCHLD 402262846Strasz * interrupt the ISCSIDWAIT ioctl(2), so we can call 403262846Strasz * wait_for_children(). 404262846Strasz */ 405262846Strasz} 406262846Strasz 407262846Straszstatic void 408262846Straszregister_sigchld(void) 409262846Strasz{ 410262846Strasz struct sigaction sa; 411262846Strasz int error; 412262846Strasz 413262846Strasz bzero(&sa, sizeof(sa)); 414262846Strasz sa.sa_handler = sigchld_handler; 415262846Strasz sigfillset(&sa.sa_mask); 416262846Strasz error = sigaction(SIGCHLD, &sa, NULL); 417262846Strasz if (error != 0) 418262846Strasz log_err(1, "sigaction"); 419262846Strasz 420262846Strasz} 421262846Strasz 422262846Straszstatic void 423255678Straszhandle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout) 424255570Strasz{ 425255570Strasz struct connection *conn; 426255570Strasz 427255570Strasz log_set_peer_addr(request->idr_conf.isc_target_addr); 428255570Strasz if (request->idr_conf.isc_target[0] != '\0') { 429255570Strasz log_set_peer_name(request->idr_conf.isc_target); 430255570Strasz setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target); 431255570Strasz } else { 432255570Strasz setproctitle("%s", request->idr_conf.isc_target_addr); 433255570Strasz } 434255570Strasz 435268703Smav conn = connection_new(request->idr_session_id, request->idr_isid, 436268703Smav request->idr_tsih, &request->idr_conf, iscsi_fd); 437255570Strasz set_timeout(timeout); 438255570Strasz capsicate(conn); 439255570Strasz login(conn); 440255570Strasz if (conn->conn_conf.isc_discovery != 0) 441255570Strasz discovery(conn); 442255570Strasz else 443255570Strasz handoff(conn); 444255570Strasz 445255570Strasz log_debugx("nothing more to do; exiting"); 446255570Strasz exit (0); 447255570Strasz} 448255570Strasz 449255570Straszstatic int 450255570Straszwait_for_children(bool block) 451255570Strasz{ 452255570Strasz pid_t pid; 453255570Strasz int status; 454255570Strasz int num = 0; 455255570Strasz 456255570Strasz for (;;) { 457255570Strasz /* 458255570Strasz * If "block" is true, wait for at least one process. 459255570Strasz */ 460255570Strasz if (block && num == 0) 461255570Strasz pid = wait4(-1, &status, 0, NULL); 462255570Strasz else 463255570Strasz pid = wait4(-1, &status, WNOHANG, NULL); 464255570Strasz if (pid <= 0) 465255570Strasz break; 466255570Strasz if (WIFSIGNALED(status)) { 467255570Strasz log_warnx("child process %d terminated with signal %d", 468255570Strasz pid, WTERMSIG(status)); 469255570Strasz } else if (WEXITSTATUS(status) != 0) { 470255570Strasz log_warnx("child process %d terminated with exit status %d", 471255570Strasz pid, WEXITSTATUS(status)); 472255570Strasz } else { 473255570Strasz log_debugx("child process %d terminated gracefully", pid); 474255570Strasz } 475255570Strasz num++; 476255570Strasz } 477255570Strasz 478255570Strasz return (num); 479255570Strasz} 480255570Strasz 481255570Straszint 482255570Straszmain(int argc, char **argv) 483255570Strasz{ 484255570Strasz int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno, 485255570Strasz timeout = 60; 486255570Strasz bool dont_daemonize = false; 487255570Strasz struct pidfh *pidfh; 488255570Strasz pid_t pid, otherpid; 489255570Strasz const char *pidfile_path = DEFAULT_PIDFILE; 490255678Strasz struct iscsi_daemon_request request; 491255570Strasz 492255570Strasz while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) { 493255570Strasz switch (ch) { 494255570Strasz case 'P': 495255570Strasz pidfile_path = optarg; 496255570Strasz break; 497255570Strasz case 'd': 498255570Strasz dont_daemonize = true; 499255570Strasz debug++; 500255570Strasz break; 501255570Strasz case 'l': 502255570Strasz debug = atoi(optarg); 503255570Strasz break; 504255570Strasz case 'm': 505255570Strasz maxproc = atoi(optarg); 506255570Strasz break; 507255570Strasz case 't': 508255570Strasz timeout = atoi(optarg); 509255570Strasz break; 510255570Strasz case '?': 511255570Strasz default: 512255570Strasz usage(); 513255570Strasz } 514255570Strasz } 515255570Strasz argc -= optind; 516255570Strasz if (argc != 0) 517255570Strasz usage(); 518255570Strasz 519255570Strasz log_init(debug); 520255570Strasz 521255570Strasz pidfh = pidfile_open(pidfile_path, 0600, &otherpid); 522255570Strasz if (pidfh == NULL) { 523255570Strasz if (errno == EEXIST) 524255570Strasz log_errx(1, "daemon already running, pid: %jd.", 525255570Strasz (intmax_t)otherpid); 526255570Strasz log_err(1, "cannot open or create pidfile \"%s\"", 527255570Strasz pidfile_path); 528255570Strasz } 529255570Strasz 530255570Strasz iscsi_fd = open(ISCSI_PATH, O_RDWR); 531255665Strasz if (iscsi_fd < 0 && errno == ENOENT) { 532255570Strasz saved_errno = errno; 533255570Strasz retval = kldload("iscsi"); 534255570Strasz if (retval != -1) 535255570Strasz iscsi_fd = open(ISCSI_PATH, O_RDWR); 536255570Strasz else 537255570Strasz errno = saved_errno; 538255570Strasz } 539255570Strasz if (iscsi_fd < 0) 540255570Strasz log_err(1, "failed to open %s", ISCSI_PATH); 541255570Strasz 542255570Strasz if (dont_daemonize == false) { 543255570Strasz if (daemon(0, 0) == -1) { 544255570Strasz log_warn("cannot daemonize"); 545255570Strasz pidfile_remove(pidfh); 546255570Strasz exit(1); 547255570Strasz } 548255570Strasz } 549255570Strasz 550255570Strasz pidfile_write(pidfh); 551255570Strasz 552262846Strasz register_sigchld(); 553262846Strasz 554255570Strasz for (;;) { 555255570Strasz log_debugx("waiting for request from the kernel"); 556255570Strasz 557255678Strasz memset(&request, 0, sizeof(request)); 558255678Strasz error = ioctl(iscsi_fd, ISCSIDWAIT, &request); 559255570Strasz if (error != 0) { 560255570Strasz if (errno == EINTR) { 561255570Strasz nchildren -= wait_for_children(false); 562255570Strasz assert(nchildren >= 0); 563255570Strasz continue; 564255570Strasz } 565255570Strasz 566255570Strasz log_err(1, "ISCSIDWAIT"); 567255570Strasz } 568255570Strasz 569255570Strasz if (dont_daemonize) { 570255570Strasz log_debugx("not forking due to -d flag; " 571255570Strasz "will exit after servicing a single request"); 572255570Strasz } else { 573255570Strasz nchildren -= wait_for_children(false); 574255570Strasz assert(nchildren >= 0); 575255570Strasz 576255570Strasz while (maxproc > 0 && nchildren >= maxproc) { 577255570Strasz log_debugx("maxproc limit of %d child processes hit; " 578255570Strasz "waiting for child process to exit", maxproc); 579255570Strasz nchildren -= wait_for_children(true); 580255570Strasz assert(nchildren >= 0); 581255570Strasz } 582255570Strasz log_debugx("incoming connection; forking child process #%d", 583255570Strasz nchildren); 584255570Strasz nchildren++; 585255570Strasz 586255570Strasz pid = fork(); 587255570Strasz if (pid < 0) 588255570Strasz log_err(1, "fork"); 589255570Strasz if (pid > 0) 590255570Strasz continue; 591255570Strasz } 592255570Strasz 593255570Strasz pidfile_close(pidfh); 594255678Strasz handle_request(iscsi_fd, &request, timeout); 595255570Strasz } 596255570Strasz 597255570Strasz return (0); 598255570Strasz} 599