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 */ 30255570Strasz 31270888Strasz#include <sys/cdefs.h> 32270888Strasz__FBSDID("$FreeBSD$"); 33270888Strasz 34255570Strasz#include <sys/types.h> 35255570Strasz#include <sys/time.h> 36255570Strasz#include <sys/ioctl.h> 37255570Strasz#include <sys/param.h> 38255570Strasz#include <sys/linker.h> 39255570Strasz#include <sys/socket.h> 40255570Strasz#include <sys/capability.h> 41255570Strasz#include <sys/wait.h> 42255570Strasz#include <assert.h> 43255570Strasz#include <errno.h> 44255570Strasz#include <fcntl.h> 45255570Strasz#include <netdb.h> 46255570Strasz#include <signal.h> 47255570Strasz#include <stdbool.h> 48255570Strasz#include <stdint.h> 49255570Strasz#include <stdio.h> 50255570Strasz#include <stdlib.h> 51255570Strasz#include <string.h> 52255570Strasz#include <unistd.h> 53255570Strasz 54255570Strasz#include <libutil.h> 55255570Strasz 56255570Strasz#include "iscsid.h" 57255570Strasz 58255570Straszstatic volatile bool sigalrm_received = false; 59255570Strasz 60255570Straszstatic int nchildren = 0; 61255570Strasz 62255570Straszstatic void 63255570Straszusage(void) 64255570Strasz{ 65255570Strasz 66255570Strasz fprintf(stderr, "usage: iscsid [-P pidfile][-d][-m maxproc][-t timeout]\n"); 67255570Strasz exit(1); 68255570Strasz} 69255570Strasz 70255570Straszchar * 71255570Straszchecked_strdup(const char *s) 72255570Strasz{ 73255570Strasz char *c; 74255570Strasz 75255570Strasz c = strdup(s); 76255570Strasz if (c == NULL) 77255570Strasz log_err(1, "strdup"); 78255570Strasz return (c); 79255570Strasz} 80255570Strasz 81255636Straszstatic void 82255636Straszresolve_addr(const struct connection *conn, const char *address, 83255636Strasz struct addrinfo **ai, bool initiator_side) 84255570Strasz{ 85255570Strasz struct addrinfo hints; 86255570Strasz char *arg, *addr, *ch; 87255570Strasz const char *port; 88255570Strasz int error, colons = 0; 89255570Strasz 90255570Strasz arg = checked_strdup(address); 91255570Strasz 92255570Strasz if (arg[0] == '\0') { 93255636Strasz fail(conn, "empty address"); 94255636Strasz log_errx(1, "empty address"); 95255570Strasz } 96255570Strasz if (arg[0] == '[') { 97255570Strasz /* 98255570Strasz * IPv6 address in square brackets, perhaps with port. 99255570Strasz */ 100255570Strasz arg++; 101255570Strasz addr = strsep(&arg, "]"); 102255570Strasz if (arg == NULL) { 103255636Strasz fail(conn, "malformed address"); 104255636Strasz log_errx(1, "malformed address %s", address); 105255570Strasz } 106255570Strasz if (arg[0] == '\0') { 107255636Strasz port = NULL; 108255570Strasz } else if (arg[0] == ':') { 109255570Strasz port = arg + 1; 110255570Strasz } else { 111255636Strasz fail(conn, "malformed address"); 112255636Strasz log_errx(1, "malformed address %s", address); 113255570Strasz } 114255570Strasz } else { 115255570Strasz /* 116255570Strasz * Either IPv6 address without brackets - and without 117255570Strasz * a port - or IPv4 address. Just count the colons. 118255570Strasz */ 119255570Strasz for (ch = arg; *ch != '\0'; ch++) { 120255570Strasz if (*ch == ':') 121255570Strasz colons++; 122255570Strasz } 123255570Strasz if (colons > 1) { 124255570Strasz addr = arg; 125255636Strasz port = NULL; 126255570Strasz } else { 127255570Strasz addr = strsep(&arg, ":"); 128255570Strasz if (arg == NULL) 129255636Strasz port = NULL; 130255570Strasz else 131255570Strasz port = arg; 132255570Strasz } 133255570Strasz } 134255570Strasz 135255636Strasz if (port == NULL && !initiator_side) 136255636Strasz port = "3260"; 137255636Strasz 138255570Strasz memset(&hints, 0, sizeof(hints)); 139255570Strasz hints.ai_family = PF_UNSPEC; 140255570Strasz hints.ai_socktype = SOCK_STREAM; 141255636Strasz hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV; 142255636Strasz if (initiator_side) 143255636Strasz hints.ai_flags |= AI_PASSIVE; 144255570Strasz 145255570Strasz error = getaddrinfo(addr, port, &hints, ai); 146255570Strasz if (error != 0) { 147255636Strasz fail(conn, gai_strerror(error)); 148255636Strasz log_errx(1, "getaddrinfo for %s failed: %s", 149255570Strasz address, gai_strerror(error)); 150255570Strasz } 151255570Strasz} 152255570Strasz 153255570Straszstatic struct connection * 154268703Smavconnection_new(unsigned int session_id, const uint8_t isid[8], uint16_t tsih, 155268703Smav const struct iscsi_session_conf *conf, int iscsi_fd) 156255570Strasz{ 157255570Strasz struct connection *conn; 158255570Strasz struct addrinfo *from_ai, *to_ai; 159255570Strasz const char *from_addr, *to_addr; 160255570Strasz#ifdef ICL_KERNEL_PROXY 161255678Strasz struct iscsi_daemon_connect idc; 162255570Strasz#endif 163255570Strasz int error; 164255570Strasz 165255570Strasz conn = calloc(1, sizeof(*conn)); 166255570Strasz if (conn == NULL) 167255570Strasz log_err(1, "calloc"); 168255570Strasz 169255570Strasz /* 170255570Strasz * Default values, from RFC 3720, section 12. 171255570Strasz */ 172255570Strasz conn->conn_header_digest = CONN_DIGEST_NONE; 173255570Strasz conn->conn_data_digest = CONN_DIGEST_NONE; 174255570Strasz conn->conn_initial_r2t = true; 175255570Strasz conn->conn_immediate_data = true; 176255570Strasz conn->conn_max_data_segment_length = 8192; 177255570Strasz conn->conn_max_burst_length = 262144; 178255570Strasz conn->conn_first_burst_length = 65536; 179255570Strasz 180255570Strasz conn->conn_session_id = session_id; 181268703Smav memcpy(&conn->conn_isid, isid, sizeof(conn->conn_isid)); 182268703Smav conn->conn_tsih = tsih; 183255636Strasz conn->conn_iscsi_fd = iscsi_fd; 184255636Strasz 185255570Strasz /* 186255570Strasz * XXX: Should we sanitize this somehow? 187255570Strasz */ 188255570Strasz memcpy(&conn->conn_conf, conf, sizeof(conn->conn_conf)); 189255570Strasz 190255570Strasz from_addr = conn->conn_conf.isc_initiator_addr; 191255570Strasz to_addr = conn->conn_conf.isc_target_addr; 192255570Strasz 193255636Strasz if (from_addr[0] != '\0') 194255636Strasz resolve_addr(conn, from_addr, &from_ai, true); 195255636Strasz else 196255570Strasz from_ai = NULL; 197255570Strasz 198255636Strasz resolve_addr(conn, to_addr, &to_ai, false); 199255570Strasz 200255570Strasz#ifdef ICL_KERNEL_PROXY 201265526Strasz if (conn->conn_conf.isc_iser) { 202265526Strasz memset(&idc, 0, sizeof(idc)); 203265526Strasz idc.idc_session_id = conn->conn_session_id; 204265526Strasz if (conn->conn_conf.isc_iser) 205265526Strasz idc.idc_iser = 1; 206265526Strasz idc.idc_domain = to_ai->ai_family; 207265526Strasz idc.idc_socktype = to_ai->ai_socktype; 208265526Strasz idc.idc_protocol = to_ai->ai_protocol; 209265526Strasz if (from_ai != NULL) { 210265526Strasz idc.idc_from_addr = from_ai->ai_addr; 211265526Strasz idc.idc_from_addrlen = from_ai->ai_addrlen; 212265526Strasz } 213265526Strasz idc.idc_to_addr = to_ai->ai_addr; 214265526Strasz idc.idc_to_addrlen = to_ai->ai_addrlen; 215255570Strasz 216265526Strasz log_debugx("connecting to %s using ICL kernel proxy", to_addr); 217265526Strasz error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc); 218265526Strasz if (error != 0) { 219265526Strasz fail(conn, strerror(errno)); 220265526Strasz log_err(1, "failed to connect to %s " 221265526Strasz "using ICL kernel proxy: ISCSIDCONNECT", to_addr); 222265526Strasz } 223255570Strasz 224265526Strasz return (conn); 225255570Strasz } 226265526Strasz#endif /* ICL_KERNEL_PROXY */ 227255570Strasz 228255636Strasz if (conn->conn_conf.isc_iser) { 229255636Strasz fail(conn, "iSER not supported"); 230255570Strasz log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY " 231255570Strasz "does not support iSER"); 232255636Strasz } 233255570Strasz 234255570Strasz conn->conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype, 235255570Strasz to_ai->ai_protocol); 236255636Strasz if (conn->conn_socket < 0) { 237255636Strasz fail(conn, strerror(errno)); 238255570Strasz log_err(1, "failed to create socket for %s", from_addr); 239255636Strasz } 240255570Strasz if (from_ai != NULL) { 241255570Strasz error = bind(conn->conn_socket, from_ai->ai_addr, 242255570Strasz from_ai->ai_addrlen); 243255636Strasz if (error != 0) { 244255636Strasz fail(conn, strerror(errno)); 245255570Strasz log_err(1, "failed to bind to %s", from_addr); 246255636Strasz } 247255570Strasz } 248255570Strasz log_debugx("connecting to %s", to_addr); 249255570Strasz error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen); 250255570Strasz if (error != 0) { 251255570Strasz fail(conn, strerror(errno)); 252255570Strasz log_err(1, "failed to connect to %s", to_addr); 253255570Strasz } 254255570Strasz 255255570Strasz return (conn); 256255570Strasz} 257255570Strasz 258255570Straszstatic void 259255570Straszhandoff(struct connection *conn) 260255570Strasz{ 261255678Strasz struct iscsi_daemon_handoff idh; 262255570Strasz int error; 263255570Strasz 264255570Strasz log_debugx("handing off connection to the kernel"); 265255570Strasz 266255678Strasz memset(&idh, 0, sizeof(idh)); 267255678Strasz idh.idh_session_id = conn->conn_session_id; 268255678Strasz idh.idh_socket = conn->conn_socket; 269255678Strasz strlcpy(idh.idh_target_alias, conn->conn_target_alias, 270255678Strasz sizeof(idh.idh_target_alias)); 271268703Smav idh.idh_tsih = conn->conn_tsih; 272255678Strasz idh.idh_statsn = conn->conn_statsn; 273255678Strasz idh.idh_header_digest = conn->conn_header_digest; 274255678Strasz idh.idh_data_digest = conn->conn_data_digest; 275255678Strasz idh.idh_initial_r2t = conn->conn_initial_r2t; 276255678Strasz idh.idh_immediate_data = conn->conn_immediate_data; 277255678Strasz idh.idh_max_data_segment_length = conn->conn_max_data_segment_length; 278255678Strasz idh.idh_max_burst_length = conn->conn_max_burst_length; 279255678Strasz idh.idh_first_burst_length = conn->conn_first_burst_length; 280255570Strasz 281255678Strasz error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh); 282255570Strasz if (error != 0) 283255570Strasz log_err(1, "ISCSIDHANDOFF"); 284255570Strasz} 285255570Strasz 286255570Straszvoid 287255570Straszfail(const struct connection *conn, const char *reason) 288255570Strasz{ 289255678Strasz struct iscsi_daemon_fail idf; 290255570Strasz int error; 291255570Strasz 292255678Strasz memset(&idf, 0, sizeof(idf)); 293255678Strasz idf.idf_session_id = conn->conn_session_id; 294255678Strasz strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason)); 295255570Strasz 296255678Strasz error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf); 297255570Strasz if (error != 0) 298255570Strasz log_err(1, "ISCSIDFAIL"); 299255570Strasz} 300255570Strasz 301255570Strasz/* 302255570Strasz * XXX: I CANT INTO LATIN 303255570Strasz */ 304255570Straszstatic void 305255570Straszcapsicate(struct connection *conn) 306255570Strasz{ 307255570Strasz int error; 308255570Strasz cap_rights_t rights; 309255570Strasz#ifdef ICL_KERNEL_PROXY 310255570Strasz const unsigned long cmds[] = { ISCSIDCONNECT, ISCSIDSEND, ISCSIDRECEIVE, 311269065Smav ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE, ISCSISMODIFY }; 312255570Strasz#else 313255570Strasz const unsigned long cmds[] = { ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, 314269065Smav ISCSISREMOVE, ISCSISMODIFY }; 315255570Strasz#endif 316255570Strasz 317255570Strasz cap_rights_init(&rights, CAP_IOCTL); 318255570Strasz error = cap_rights_limit(conn->conn_iscsi_fd, &rights); 319255570Strasz if (error != 0 && errno != ENOSYS) 320255570Strasz log_err(1, "cap_rights_limit"); 321255570Strasz 322255570Strasz error = cap_ioctls_limit(conn->conn_iscsi_fd, cmds, 323255570Strasz sizeof(cmds) / sizeof(cmds[0])); 324255570Strasz if (error != 0 && errno != ENOSYS) 325255570Strasz log_err(1, "cap_ioctls_limit"); 326255570Strasz 327255570Strasz error = cap_enter(); 328255570Strasz if (error != 0 && errno != ENOSYS) 329255570Strasz log_err(1, "cap_enter"); 330255570Strasz 331255570Strasz if (cap_sandboxed()) 332255570Strasz log_debugx("Capsicum capability mode enabled"); 333255570Strasz else 334255570Strasz log_warnx("Capsicum capability mode not supported"); 335255570Strasz} 336255570Strasz 337255570Straszbool 338255570Strasztimed_out(void) 339255570Strasz{ 340255570Strasz 341255570Strasz return (sigalrm_received); 342255570Strasz} 343255570Strasz 344255570Straszstatic void 345255570Straszsigalrm_handler(int dummy __unused) 346255570Strasz{ 347255570Strasz /* 348255570Strasz * It would be easiest to just log an error and exit. We can't 349255570Strasz * do this, though, because log_errx() is not signal safe, since 350255570Strasz * it calls syslog(3). Instead, set a flag checked by pdu_send() 351255570Strasz * and pdu_receive(), to call log_errx() there. Should they fail 352255570Strasz * to notice, we'll exit here one second later. 353255570Strasz */ 354255570Strasz if (sigalrm_received) { 355255570Strasz /* 356255570Strasz * Oh well. Just give up and quit. 357255570Strasz */ 358255570Strasz _exit(2); 359255570Strasz } 360255570Strasz 361255570Strasz sigalrm_received = true; 362255570Strasz} 363255570Strasz 364255570Straszstatic void 365255570Straszset_timeout(int timeout) 366255570Strasz{ 367255570Strasz struct sigaction sa; 368255570Strasz struct itimerval itv; 369255570Strasz int error; 370255570Strasz 371255570Strasz if (timeout <= 0) { 372255570Strasz log_debugx("session timeout disabled"); 373255570Strasz return; 374255570Strasz } 375255570Strasz 376255570Strasz bzero(&sa, sizeof(sa)); 377255570Strasz sa.sa_handler = sigalrm_handler; 378255570Strasz sigfillset(&sa.sa_mask); 379255570Strasz error = sigaction(SIGALRM, &sa, NULL); 380255570Strasz if (error != 0) 381255570Strasz log_err(1, "sigaction"); 382255570Strasz 383255570Strasz /* 384255570Strasz * First SIGALRM will arive after conf_timeout seconds. 385255570Strasz * If we do nothing, another one will arrive a second later. 386255570Strasz */ 387255570Strasz bzero(&itv, sizeof(itv)); 388255570Strasz itv.it_interval.tv_sec = 1; 389255570Strasz itv.it_value.tv_sec = timeout; 390255570Strasz 391255570Strasz log_debugx("setting session timeout to %d seconds", 392255570Strasz timeout); 393255570Strasz error = setitimer(ITIMER_REAL, &itv, NULL); 394255570Strasz if (error != 0) 395255570Strasz log_err(1, "setitimer"); 396255570Strasz} 397255570Strasz 398255570Straszstatic void 399262846Straszsigchld_handler(int dummy __unused) 400262846Strasz{ 401262846Strasz 402262846Strasz /* 403262846Strasz * The only purpose of this handler is to make SIGCHLD 404262846Strasz * interrupt the ISCSIDWAIT ioctl(2), so we can call 405262846Strasz * wait_for_children(). 406262846Strasz */ 407262846Strasz} 408262846Strasz 409262846Straszstatic void 410262846Straszregister_sigchld(void) 411262846Strasz{ 412262846Strasz struct sigaction sa; 413262846Strasz int error; 414262846Strasz 415262846Strasz bzero(&sa, sizeof(sa)); 416262846Strasz sa.sa_handler = sigchld_handler; 417262846Strasz sigfillset(&sa.sa_mask); 418262846Strasz error = sigaction(SIGCHLD, &sa, NULL); 419262846Strasz if (error != 0) 420262846Strasz log_err(1, "sigaction"); 421262846Strasz 422262846Strasz} 423262846Strasz 424262846Straszstatic void 425255678Straszhandle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout) 426255570Strasz{ 427255570Strasz struct connection *conn; 428255570Strasz 429255570Strasz log_set_peer_addr(request->idr_conf.isc_target_addr); 430255570Strasz if (request->idr_conf.isc_target[0] != '\0') { 431255570Strasz log_set_peer_name(request->idr_conf.isc_target); 432255570Strasz setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target); 433255570Strasz } else { 434255570Strasz setproctitle("%s", request->idr_conf.isc_target_addr); 435255570Strasz } 436255570Strasz 437268703Smav conn = connection_new(request->idr_session_id, request->idr_isid, 438268703Smav request->idr_tsih, &request->idr_conf, iscsi_fd); 439255570Strasz set_timeout(timeout); 440255570Strasz capsicate(conn); 441255570Strasz login(conn); 442255570Strasz if (conn->conn_conf.isc_discovery != 0) 443255570Strasz discovery(conn); 444255570Strasz else 445255570Strasz handoff(conn); 446255570Strasz 447255570Strasz log_debugx("nothing more to do; exiting"); 448255570Strasz exit (0); 449255570Strasz} 450255570Strasz 451255570Straszstatic int 452255570Straszwait_for_children(bool block) 453255570Strasz{ 454255570Strasz pid_t pid; 455255570Strasz int status; 456255570Strasz int num = 0; 457255570Strasz 458255570Strasz for (;;) { 459255570Strasz /* 460255570Strasz * If "block" is true, wait for at least one process. 461255570Strasz */ 462255570Strasz if (block && num == 0) 463255570Strasz pid = wait4(-1, &status, 0, NULL); 464255570Strasz else 465255570Strasz pid = wait4(-1, &status, WNOHANG, NULL); 466255570Strasz if (pid <= 0) 467255570Strasz break; 468255570Strasz if (WIFSIGNALED(status)) { 469255570Strasz log_warnx("child process %d terminated with signal %d", 470255570Strasz pid, WTERMSIG(status)); 471255570Strasz } else if (WEXITSTATUS(status) != 0) { 472255570Strasz log_warnx("child process %d terminated with exit status %d", 473255570Strasz pid, WEXITSTATUS(status)); 474255570Strasz } else { 475255570Strasz log_debugx("child process %d terminated gracefully", pid); 476255570Strasz } 477255570Strasz num++; 478255570Strasz } 479255570Strasz 480255570Strasz return (num); 481255570Strasz} 482255570Strasz 483255570Straszint 484255570Straszmain(int argc, char **argv) 485255570Strasz{ 486255570Strasz int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno, 487255570Strasz timeout = 60; 488255570Strasz bool dont_daemonize = false; 489255570Strasz struct pidfh *pidfh; 490255570Strasz pid_t pid, otherpid; 491255570Strasz const char *pidfile_path = DEFAULT_PIDFILE; 492255678Strasz struct iscsi_daemon_request request; 493255570Strasz 494255570Strasz while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) { 495255570Strasz switch (ch) { 496255570Strasz case 'P': 497255570Strasz pidfile_path = optarg; 498255570Strasz break; 499255570Strasz case 'd': 500255570Strasz dont_daemonize = true; 501255570Strasz debug++; 502255570Strasz break; 503255570Strasz case 'l': 504255570Strasz debug = atoi(optarg); 505255570Strasz break; 506255570Strasz case 'm': 507255570Strasz maxproc = atoi(optarg); 508255570Strasz break; 509255570Strasz case 't': 510255570Strasz timeout = atoi(optarg); 511255570Strasz break; 512255570Strasz case '?': 513255570Strasz default: 514255570Strasz usage(); 515255570Strasz } 516255570Strasz } 517255570Strasz argc -= optind; 518255570Strasz if (argc != 0) 519255570Strasz usage(); 520255570Strasz 521255570Strasz log_init(debug); 522255570Strasz 523255570Strasz pidfh = pidfile_open(pidfile_path, 0600, &otherpid); 524255570Strasz if (pidfh == NULL) { 525255570Strasz if (errno == EEXIST) 526255570Strasz log_errx(1, "daemon already running, pid: %jd.", 527255570Strasz (intmax_t)otherpid); 528255570Strasz log_err(1, "cannot open or create pidfile \"%s\"", 529255570Strasz pidfile_path); 530255570Strasz } 531255570Strasz 532255570Strasz iscsi_fd = open(ISCSI_PATH, O_RDWR); 533255665Strasz if (iscsi_fd < 0 && errno == ENOENT) { 534255570Strasz saved_errno = errno; 535255570Strasz retval = kldload("iscsi"); 536255570Strasz if (retval != -1) 537255570Strasz iscsi_fd = open(ISCSI_PATH, O_RDWR); 538255570Strasz else 539255570Strasz errno = saved_errno; 540255570Strasz } 541255570Strasz if (iscsi_fd < 0) 542255570Strasz log_err(1, "failed to open %s", ISCSI_PATH); 543255570Strasz 544255570Strasz if (dont_daemonize == false) { 545255570Strasz if (daemon(0, 0) == -1) { 546255570Strasz log_warn("cannot daemonize"); 547255570Strasz pidfile_remove(pidfh); 548255570Strasz exit(1); 549255570Strasz } 550255570Strasz } 551255570Strasz 552255570Strasz pidfile_write(pidfh); 553255570Strasz 554262846Strasz register_sigchld(); 555262846Strasz 556255570Strasz for (;;) { 557255570Strasz log_debugx("waiting for request from the kernel"); 558255570Strasz 559255678Strasz memset(&request, 0, sizeof(request)); 560255678Strasz error = ioctl(iscsi_fd, ISCSIDWAIT, &request); 561255570Strasz if (error != 0) { 562255570Strasz if (errno == EINTR) { 563255570Strasz nchildren -= wait_for_children(false); 564255570Strasz assert(nchildren >= 0); 565255570Strasz continue; 566255570Strasz } 567255570Strasz 568255570Strasz log_err(1, "ISCSIDWAIT"); 569255570Strasz } 570255570Strasz 571255570Strasz if (dont_daemonize) { 572255570Strasz log_debugx("not forking due to -d flag; " 573255570Strasz "will exit after servicing a single request"); 574255570Strasz } else { 575255570Strasz nchildren -= wait_for_children(false); 576255570Strasz assert(nchildren >= 0); 577255570Strasz 578255570Strasz while (maxproc > 0 && nchildren >= maxproc) { 579255570Strasz log_debugx("maxproc limit of %d child processes hit; " 580255570Strasz "waiting for child process to exit", maxproc); 581255570Strasz nchildren -= wait_for_children(true); 582255570Strasz assert(nchildren >= 0); 583255570Strasz } 584255570Strasz log_debugx("incoming connection; forking child process #%d", 585255570Strasz nchildren); 586255570Strasz nchildren++; 587255570Strasz 588255570Strasz pid = fork(); 589255570Strasz if (pid < 0) 590255570Strasz log_err(1, "fork"); 591255570Strasz if (pid > 0) 592255570Strasz continue; 593255570Strasz } 594255570Strasz 595255570Strasz pidfile_close(pidfh); 596255678Strasz handle_request(iscsi_fd, &request, timeout); 597255570Strasz } 598255570Strasz 599255570Strasz return (0); 600255570Strasz} 601