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: releng/10.2/usr.sbin/iscsid/iscsid.c 280250 2015-03-19 12:32:48Z rwatson $"); 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> 40280250Srwatson#include <sys/capsicum.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 163279001Smav int error, sockbuf; 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 } 240279001Smav sockbuf = SOCKBUF_SIZE; 241279001Smav if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_RCVBUF, 242279001Smav &sockbuf, sizeof(sockbuf)) == -1) 243279001Smav log_warn("setsockopt(SO_RCVBUF) failed"); 244279001Smav sockbuf = SOCKBUF_SIZE; 245279001Smav if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_SNDBUF, 246279001Smav &sockbuf, sizeof(sockbuf)) == -1) 247279001Smav log_warn("setsockopt(SO_SNDBUF) failed"); 248255570Strasz if (from_ai != NULL) { 249255570Strasz error = bind(conn->conn_socket, from_ai->ai_addr, 250255570Strasz from_ai->ai_addrlen); 251255636Strasz if (error != 0) { 252255636Strasz fail(conn, strerror(errno)); 253255570Strasz log_err(1, "failed to bind to %s", from_addr); 254255636Strasz } 255255570Strasz } 256255570Strasz log_debugx("connecting to %s", to_addr); 257255570Strasz error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen); 258255570Strasz if (error != 0) { 259255570Strasz fail(conn, strerror(errno)); 260255570Strasz log_err(1, "failed to connect to %s", to_addr); 261255570Strasz } 262255570Strasz 263255570Strasz return (conn); 264255570Strasz} 265255570Strasz 266255570Straszstatic void 267255570Straszhandoff(struct connection *conn) 268255570Strasz{ 269255678Strasz struct iscsi_daemon_handoff idh; 270255570Strasz int error; 271255570Strasz 272255570Strasz log_debugx("handing off connection to the kernel"); 273255570Strasz 274255678Strasz memset(&idh, 0, sizeof(idh)); 275255678Strasz idh.idh_session_id = conn->conn_session_id; 276255678Strasz idh.idh_socket = conn->conn_socket; 277255678Strasz strlcpy(idh.idh_target_alias, conn->conn_target_alias, 278255678Strasz sizeof(idh.idh_target_alias)); 279268703Smav idh.idh_tsih = conn->conn_tsih; 280255678Strasz idh.idh_statsn = conn->conn_statsn; 281255678Strasz idh.idh_header_digest = conn->conn_header_digest; 282255678Strasz idh.idh_data_digest = conn->conn_data_digest; 283255678Strasz idh.idh_initial_r2t = conn->conn_initial_r2t; 284255678Strasz idh.idh_immediate_data = conn->conn_immediate_data; 285255678Strasz idh.idh_max_data_segment_length = conn->conn_max_data_segment_length; 286255678Strasz idh.idh_max_burst_length = conn->conn_max_burst_length; 287255678Strasz idh.idh_first_burst_length = conn->conn_first_burst_length; 288255570Strasz 289255678Strasz error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh); 290255570Strasz if (error != 0) 291255570Strasz log_err(1, "ISCSIDHANDOFF"); 292255570Strasz} 293255570Strasz 294255570Straszvoid 295255570Straszfail(const struct connection *conn, const char *reason) 296255570Strasz{ 297255678Strasz struct iscsi_daemon_fail idf; 298255570Strasz int error; 299255570Strasz 300255678Strasz memset(&idf, 0, sizeof(idf)); 301255678Strasz idf.idf_session_id = conn->conn_session_id; 302255678Strasz strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason)); 303255570Strasz 304255678Strasz error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf); 305255570Strasz if (error != 0) 306255570Strasz log_err(1, "ISCSIDFAIL"); 307255570Strasz} 308255570Strasz 309255570Strasz/* 310255570Strasz * XXX: I CANT INTO LATIN 311255570Strasz */ 312255570Straszstatic void 313255570Straszcapsicate(struct connection *conn) 314255570Strasz{ 315255570Strasz int error; 316255570Strasz cap_rights_t rights; 317255570Strasz#ifdef ICL_KERNEL_PROXY 318255570Strasz const unsigned long cmds[] = { ISCSIDCONNECT, ISCSIDSEND, ISCSIDRECEIVE, 319269065Smav ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE, ISCSISMODIFY }; 320255570Strasz#else 321255570Strasz const unsigned long cmds[] = { ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, 322269065Smav ISCSISREMOVE, ISCSISMODIFY }; 323255570Strasz#endif 324255570Strasz 325255570Strasz cap_rights_init(&rights, CAP_IOCTL); 326255570Strasz error = cap_rights_limit(conn->conn_iscsi_fd, &rights); 327255570Strasz if (error != 0 && errno != ENOSYS) 328255570Strasz log_err(1, "cap_rights_limit"); 329255570Strasz 330255570Strasz error = cap_ioctls_limit(conn->conn_iscsi_fd, cmds, 331255570Strasz sizeof(cmds) / sizeof(cmds[0])); 332255570Strasz if (error != 0 && errno != ENOSYS) 333255570Strasz log_err(1, "cap_ioctls_limit"); 334255570Strasz 335255570Strasz error = cap_enter(); 336255570Strasz if (error != 0 && errno != ENOSYS) 337255570Strasz log_err(1, "cap_enter"); 338255570Strasz 339255570Strasz if (cap_sandboxed()) 340255570Strasz log_debugx("Capsicum capability mode enabled"); 341255570Strasz else 342255570Strasz log_warnx("Capsicum capability mode not supported"); 343255570Strasz} 344255570Strasz 345255570Straszbool 346255570Strasztimed_out(void) 347255570Strasz{ 348255570Strasz 349255570Strasz return (sigalrm_received); 350255570Strasz} 351255570Strasz 352255570Straszstatic void 353255570Straszsigalrm_handler(int dummy __unused) 354255570Strasz{ 355255570Strasz /* 356255570Strasz * It would be easiest to just log an error and exit. We can't 357255570Strasz * do this, though, because log_errx() is not signal safe, since 358255570Strasz * it calls syslog(3). Instead, set a flag checked by pdu_send() 359255570Strasz * and pdu_receive(), to call log_errx() there. Should they fail 360255570Strasz * to notice, we'll exit here one second later. 361255570Strasz */ 362255570Strasz if (sigalrm_received) { 363255570Strasz /* 364255570Strasz * Oh well. Just give up and quit. 365255570Strasz */ 366255570Strasz _exit(2); 367255570Strasz } 368255570Strasz 369255570Strasz sigalrm_received = true; 370255570Strasz} 371255570Strasz 372255570Straszstatic void 373255570Straszset_timeout(int timeout) 374255570Strasz{ 375255570Strasz struct sigaction sa; 376255570Strasz struct itimerval itv; 377255570Strasz int error; 378255570Strasz 379255570Strasz if (timeout <= 0) { 380255570Strasz log_debugx("session timeout disabled"); 381255570Strasz return; 382255570Strasz } 383255570Strasz 384255570Strasz bzero(&sa, sizeof(sa)); 385255570Strasz sa.sa_handler = sigalrm_handler; 386255570Strasz sigfillset(&sa.sa_mask); 387255570Strasz error = sigaction(SIGALRM, &sa, NULL); 388255570Strasz if (error != 0) 389255570Strasz log_err(1, "sigaction"); 390255570Strasz 391255570Strasz /* 392255570Strasz * First SIGALRM will arive after conf_timeout seconds. 393255570Strasz * If we do nothing, another one will arrive a second later. 394255570Strasz */ 395255570Strasz bzero(&itv, sizeof(itv)); 396255570Strasz itv.it_interval.tv_sec = 1; 397255570Strasz itv.it_value.tv_sec = timeout; 398255570Strasz 399255570Strasz log_debugx("setting session timeout to %d seconds", 400255570Strasz timeout); 401255570Strasz error = setitimer(ITIMER_REAL, &itv, NULL); 402255570Strasz if (error != 0) 403255570Strasz log_err(1, "setitimer"); 404255570Strasz} 405255570Strasz 406255570Straszstatic void 407262846Straszsigchld_handler(int dummy __unused) 408262846Strasz{ 409262846Strasz 410262846Strasz /* 411262846Strasz * The only purpose of this handler is to make SIGCHLD 412262846Strasz * interrupt the ISCSIDWAIT ioctl(2), so we can call 413262846Strasz * wait_for_children(). 414262846Strasz */ 415262846Strasz} 416262846Strasz 417262846Straszstatic void 418262846Straszregister_sigchld(void) 419262846Strasz{ 420262846Strasz struct sigaction sa; 421262846Strasz int error; 422262846Strasz 423262846Strasz bzero(&sa, sizeof(sa)); 424262846Strasz sa.sa_handler = sigchld_handler; 425262846Strasz sigfillset(&sa.sa_mask); 426262846Strasz error = sigaction(SIGCHLD, &sa, NULL); 427262846Strasz if (error != 0) 428262846Strasz log_err(1, "sigaction"); 429262846Strasz 430262846Strasz} 431262846Strasz 432262846Straszstatic void 433255678Straszhandle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout) 434255570Strasz{ 435255570Strasz struct connection *conn; 436255570Strasz 437255570Strasz log_set_peer_addr(request->idr_conf.isc_target_addr); 438255570Strasz if (request->idr_conf.isc_target[0] != '\0') { 439255570Strasz log_set_peer_name(request->idr_conf.isc_target); 440255570Strasz setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target); 441255570Strasz } else { 442255570Strasz setproctitle("%s", request->idr_conf.isc_target_addr); 443255570Strasz } 444255570Strasz 445268703Smav conn = connection_new(request->idr_session_id, request->idr_isid, 446268703Smav request->idr_tsih, &request->idr_conf, iscsi_fd); 447255570Strasz set_timeout(timeout); 448255570Strasz capsicate(conn); 449255570Strasz login(conn); 450255570Strasz if (conn->conn_conf.isc_discovery != 0) 451255570Strasz discovery(conn); 452255570Strasz else 453255570Strasz handoff(conn); 454255570Strasz 455255570Strasz log_debugx("nothing more to do; exiting"); 456255570Strasz exit (0); 457255570Strasz} 458255570Strasz 459255570Straszstatic int 460255570Straszwait_for_children(bool block) 461255570Strasz{ 462255570Strasz pid_t pid; 463255570Strasz int status; 464255570Strasz int num = 0; 465255570Strasz 466255570Strasz for (;;) { 467255570Strasz /* 468255570Strasz * If "block" is true, wait for at least one process. 469255570Strasz */ 470255570Strasz if (block && num == 0) 471255570Strasz pid = wait4(-1, &status, 0, NULL); 472255570Strasz else 473255570Strasz pid = wait4(-1, &status, WNOHANG, NULL); 474255570Strasz if (pid <= 0) 475255570Strasz break; 476255570Strasz if (WIFSIGNALED(status)) { 477255570Strasz log_warnx("child process %d terminated with signal %d", 478255570Strasz pid, WTERMSIG(status)); 479255570Strasz } else if (WEXITSTATUS(status) != 0) { 480255570Strasz log_warnx("child process %d terminated with exit status %d", 481255570Strasz pid, WEXITSTATUS(status)); 482255570Strasz } else { 483255570Strasz log_debugx("child process %d terminated gracefully", pid); 484255570Strasz } 485255570Strasz num++; 486255570Strasz } 487255570Strasz 488255570Strasz return (num); 489255570Strasz} 490255570Strasz 491255570Straszint 492255570Straszmain(int argc, char **argv) 493255570Strasz{ 494255570Strasz int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno, 495255570Strasz timeout = 60; 496255570Strasz bool dont_daemonize = false; 497255570Strasz struct pidfh *pidfh; 498255570Strasz pid_t pid, otherpid; 499255570Strasz const char *pidfile_path = DEFAULT_PIDFILE; 500255678Strasz struct iscsi_daemon_request request; 501255570Strasz 502255570Strasz while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) { 503255570Strasz switch (ch) { 504255570Strasz case 'P': 505255570Strasz pidfile_path = optarg; 506255570Strasz break; 507255570Strasz case 'd': 508255570Strasz dont_daemonize = true; 509255570Strasz debug++; 510255570Strasz break; 511255570Strasz case 'l': 512255570Strasz debug = atoi(optarg); 513255570Strasz break; 514255570Strasz case 'm': 515255570Strasz maxproc = atoi(optarg); 516255570Strasz break; 517255570Strasz case 't': 518255570Strasz timeout = atoi(optarg); 519255570Strasz break; 520255570Strasz case '?': 521255570Strasz default: 522255570Strasz usage(); 523255570Strasz } 524255570Strasz } 525255570Strasz argc -= optind; 526255570Strasz if (argc != 0) 527255570Strasz usage(); 528255570Strasz 529255570Strasz log_init(debug); 530255570Strasz 531255570Strasz pidfh = pidfile_open(pidfile_path, 0600, &otherpid); 532255570Strasz if (pidfh == NULL) { 533255570Strasz if (errno == EEXIST) 534255570Strasz log_errx(1, "daemon already running, pid: %jd.", 535255570Strasz (intmax_t)otherpid); 536255570Strasz log_err(1, "cannot open or create pidfile \"%s\"", 537255570Strasz pidfile_path); 538255570Strasz } 539255570Strasz 540255570Strasz iscsi_fd = open(ISCSI_PATH, O_RDWR); 541255665Strasz if (iscsi_fd < 0 && errno == ENOENT) { 542255570Strasz saved_errno = errno; 543255570Strasz retval = kldload("iscsi"); 544255570Strasz if (retval != -1) 545255570Strasz iscsi_fd = open(ISCSI_PATH, O_RDWR); 546255570Strasz else 547255570Strasz errno = saved_errno; 548255570Strasz } 549255570Strasz if (iscsi_fd < 0) 550255570Strasz log_err(1, "failed to open %s", ISCSI_PATH); 551255570Strasz 552255570Strasz if (dont_daemonize == false) { 553255570Strasz if (daemon(0, 0) == -1) { 554255570Strasz log_warn("cannot daemonize"); 555255570Strasz pidfile_remove(pidfh); 556255570Strasz exit(1); 557255570Strasz } 558255570Strasz } 559255570Strasz 560255570Strasz pidfile_write(pidfh); 561255570Strasz 562262846Strasz register_sigchld(); 563262846Strasz 564255570Strasz for (;;) { 565255570Strasz log_debugx("waiting for request from the kernel"); 566255570Strasz 567255678Strasz memset(&request, 0, sizeof(request)); 568255678Strasz error = ioctl(iscsi_fd, ISCSIDWAIT, &request); 569255570Strasz if (error != 0) { 570255570Strasz if (errno == EINTR) { 571255570Strasz nchildren -= wait_for_children(false); 572255570Strasz assert(nchildren >= 0); 573255570Strasz continue; 574255570Strasz } 575255570Strasz 576255570Strasz log_err(1, "ISCSIDWAIT"); 577255570Strasz } 578255570Strasz 579255570Strasz if (dont_daemonize) { 580255570Strasz log_debugx("not forking due to -d flag; " 581255570Strasz "will exit after servicing a single request"); 582255570Strasz } else { 583255570Strasz nchildren -= wait_for_children(false); 584255570Strasz assert(nchildren >= 0); 585255570Strasz 586255570Strasz while (maxproc > 0 && nchildren >= maxproc) { 587255570Strasz log_debugx("maxproc limit of %d child processes hit; " 588255570Strasz "waiting for child process to exit", maxproc); 589255570Strasz nchildren -= wait_for_children(true); 590255570Strasz assert(nchildren >= 0); 591255570Strasz } 592255570Strasz log_debugx("incoming connection; forking child process #%d", 593255570Strasz nchildren); 594255570Strasz nchildren++; 595255570Strasz 596255570Strasz pid = fork(); 597255570Strasz if (pid < 0) 598255570Strasz log_err(1, "fork"); 599255570Strasz if (pid > 0) 600255570Strasz continue; 601255570Strasz } 602255570Strasz 603255570Strasz pidfile_close(pidfh); 604255678Strasz handle_request(iscsi_fd, &request, timeout); 605255570Strasz } 606255570Strasz 607255570Strasz return (0); 608255570Strasz} 609