xref: /freebsd/usr.sbin/iscsid/iscsid.c (revision f9fd7337f63698f33239c58c07bf430198235a22)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 The FreeBSD Foundation
5  *
6  * This software was developed by Edward Tomasz Napierala under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <sys/ioctl.h>
38 #include <sys/param.h>
39 #include <sys/linker.h>
40 #include <sys/socket.h>
41 #include <sys/capsicum.h>
42 #include <sys/wait.h>
43 #include <netinet/in.h>
44 #include <assert.h>
45 #include <capsicum_helpers.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <libutil.h>
49 #include <netdb.h>
50 #include <signal.h>
51 #include <stdbool.h>
52 #include <stdint.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 #include "iscsid.h"
59 
60 static volatile bool sigalrm_received = false;
61 
62 static int nchildren = 0;
63 
64 static void
65 usage(void)
66 {
67 
68 	fprintf(stderr, "usage: iscsid [-P pidfile][-d][-m maxproc][-t timeout]\n");
69 	exit(1);
70 }
71 
72 char *
73 checked_strdup(const char *s)
74 {
75 	char *c;
76 
77 	c = strdup(s);
78 	if (c == NULL)
79 		log_err(1, "strdup");
80 	return (c);
81 }
82 
83 static void
84 resolve_addr(const struct connection *conn, const char *address,
85     struct addrinfo **ai, bool initiator_side)
86 {
87 	struct addrinfo hints;
88 	char *arg, *addr, *ch;
89 	const char *port;
90 	int error, colons = 0;
91 
92 	arg = checked_strdup(address);
93 
94 	if (arg[0] == '\0') {
95 		fail(conn, "empty address");
96 		log_errx(1, "empty address");
97 	}
98 	if (arg[0] == '[') {
99 		/*
100 		 * IPv6 address in square brackets, perhaps with port.
101 		 */
102 		arg++;
103 		addr = strsep(&arg, "]");
104 		if (arg == NULL) {
105 			fail(conn, "malformed address");
106 			log_errx(1, "malformed address %s", address);
107 		}
108 		if (arg[0] == '\0') {
109 			port = NULL;
110 		} else if (arg[0] == ':') {
111 			port = arg + 1;
112 		} else {
113 			fail(conn, "malformed address");
114 			log_errx(1, "malformed address %s", address);
115 		}
116 	} else {
117 		/*
118 		 * Either IPv6 address without brackets - and without
119 		 * a port - or IPv4 address.  Just count the colons.
120 		 */
121 		for (ch = arg; *ch != '\0'; ch++) {
122 			if (*ch == ':')
123 				colons++;
124 		}
125 		if (colons > 1) {
126 			addr = arg;
127 			port = NULL;
128 		} else {
129 			addr = strsep(&arg, ":");
130 			if (arg == NULL)
131 				port = NULL;
132 			else
133 				port = arg;
134 		}
135 	}
136 
137 	if (port == NULL && !initiator_side)
138 		port = "3260";
139 
140 	memset(&hints, 0, sizeof(hints));
141 	hints.ai_family = PF_UNSPEC;
142 	hints.ai_socktype = SOCK_STREAM;
143 	hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
144 	if (initiator_side)
145 		hints.ai_flags |= AI_PASSIVE;
146 
147 	error = getaddrinfo(addr, port, &hints, ai);
148 	if (error != 0) {
149 		fail(conn, gai_strerror(error));
150 		log_errx(1, "getaddrinfo for %s failed: %s",
151 		    address, gai_strerror(error));
152 	}
153 }
154 
155 static struct connection *
156 connection_new(int iscsi_fd, const struct iscsi_daemon_request *request)
157 {
158 	struct connection *conn;
159 	struct iscsi_session_limits *isl;
160 	struct addrinfo *from_ai, *to_ai;
161 	const char *from_addr, *to_addr;
162 #ifdef ICL_KERNEL_PROXY
163 	struct iscsi_daemon_connect idc;
164 #endif
165 	int error, sockbuf;
166 
167 	conn = calloc(1, sizeof(*conn));
168 	if (conn == NULL)
169 		log_err(1, "calloc");
170 
171 	/*
172 	 * Default values, from RFC 3720, section 12.
173 	 */
174 	conn->conn_protocol_level = 0;
175 	conn->conn_header_digest = CONN_DIGEST_NONE;
176 	conn->conn_data_digest = CONN_DIGEST_NONE;
177 	conn->conn_initial_r2t = true;
178 	conn->conn_immediate_data = true;
179 	conn->conn_max_recv_data_segment_length = 8192;
180 	conn->conn_max_send_data_segment_length = 8192;
181 	conn->conn_max_burst_length = 262144;
182 	conn->conn_first_burst_length = 65536;
183 	conn->conn_iscsi_fd = iscsi_fd;
184 
185 	conn->conn_session_id = request->idr_session_id;
186 	memcpy(&conn->conn_conf, &request->idr_conf, sizeof(conn->conn_conf));
187 	memcpy(&conn->conn_isid, &request->idr_isid, sizeof(conn->conn_isid));
188 	conn->conn_tsih = request->idr_tsih;
189 
190 	/*
191 	 * Read the driver limits and provide reasonable defaults for the ones
192 	 * the driver doesn't care about.  If a max_snd_dsl is not explicitly
193 	 * provided by the driver then we'll make sure both conn->max_snd_dsl
194 	 * and isl->max_snd_dsl are set to the rcv_dsl.  This preserves historic
195 	 * behavior.
196 	 */
197 	isl = &conn->conn_limits;
198 	memcpy(isl, &request->idr_limits, sizeof(*isl));
199 	if (isl->isl_max_recv_data_segment_length == 0)
200 		isl->isl_max_recv_data_segment_length = (1 << 24) - 1;
201 	if (isl->isl_max_send_data_segment_length == 0)
202 		isl->isl_max_send_data_segment_length =
203 		    isl->isl_max_recv_data_segment_length;
204 	if (isl->isl_max_burst_length == 0)
205 		isl->isl_max_burst_length = (1 << 24) - 1;
206 	if (isl->isl_first_burst_length == 0)
207 		isl->isl_first_burst_length = (1 << 24) - 1;
208 	if (isl->isl_first_burst_length > isl->isl_max_burst_length)
209 		isl->isl_first_burst_length = isl->isl_max_burst_length;
210 
211 	/*
212 	 * Limit default send length in case it won't be negotiated.
213 	 * We can't do it for other limits, since they may affect both
214 	 * sender and receiver operation, and we must obey defaults.
215 	 */
216 	if (conn->conn_max_send_data_segment_length >
217 	    isl->isl_max_send_data_segment_length) {
218 		conn->conn_max_send_data_segment_length =
219 		    isl->isl_max_send_data_segment_length;
220 	}
221 
222 	from_addr = conn->conn_conf.isc_initiator_addr;
223 	to_addr = conn->conn_conf.isc_target_addr;
224 
225 	if (from_addr[0] != '\0')
226 		resolve_addr(conn, from_addr, &from_ai, true);
227 	else
228 		from_ai = NULL;
229 
230 	resolve_addr(conn, to_addr, &to_ai, false);
231 
232 #ifdef ICL_KERNEL_PROXY
233 	if (conn->conn_conf.isc_iser) {
234 		memset(&idc, 0, sizeof(idc));
235 		idc.idc_session_id = conn->conn_session_id;
236 		if (conn->conn_conf.isc_iser)
237 			idc.idc_iser = 1;
238 		idc.idc_domain = to_ai->ai_family;
239 		idc.idc_socktype = to_ai->ai_socktype;
240 		idc.idc_protocol = to_ai->ai_protocol;
241 		if (from_ai != NULL) {
242 			idc.idc_from_addr = from_ai->ai_addr;
243 			idc.idc_from_addrlen = from_ai->ai_addrlen;
244 		}
245 		idc.idc_to_addr = to_ai->ai_addr;
246 		idc.idc_to_addrlen = to_ai->ai_addrlen;
247 
248 		log_debugx("connecting to %s using ICL kernel proxy", to_addr);
249 		error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc);
250 		if (error != 0) {
251 			fail(conn, strerror(errno));
252 			log_err(1, "failed to connect to %s "
253 			    "using ICL kernel proxy: ISCSIDCONNECT", to_addr);
254 		}
255 
256 		return (conn);
257 	}
258 #endif /* ICL_KERNEL_PROXY */
259 
260 	if (conn->conn_conf.isc_iser) {
261 		fail(conn, "iSER not supported");
262 		log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY "
263 		    "does not support iSER");
264 	}
265 
266 	conn->conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype,
267 	    to_ai->ai_protocol);
268 	if (conn->conn_socket < 0) {
269 		fail(conn, strerror(errno));
270 		log_err(1, "failed to create socket for %s", from_addr);
271 	}
272 	sockbuf = SOCKBUF_SIZE;
273 	if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_RCVBUF,
274 	    &sockbuf, sizeof(sockbuf)) == -1)
275 		log_warn("setsockopt(SO_RCVBUF) failed");
276 	sockbuf = SOCKBUF_SIZE;
277 	if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_SNDBUF,
278 	    &sockbuf, sizeof(sockbuf)) == -1)
279 		log_warn("setsockopt(SO_SNDBUF) failed");
280 	if (conn->conn_conf.isc_dscp != -1) {
281 		int tos = conn->conn_conf.isc_dscp << 2;
282 		if (to_ai->ai_family == AF_INET) {
283 			if (setsockopt(conn->conn_socket,
284 			    IPPROTO_IP, IP_TOS,
285 			    &tos, sizeof(tos)) == -1)
286 				log_warn("setsockopt(IP_TOS) "
287 				    "failed for %s",
288 				    from_addr);
289 		} else
290 		if (to_ai->ai_family == AF_INET6) {
291 			if (setsockopt(conn->conn_socket,
292 			    IPPROTO_IPV6, IPV6_TCLASS,
293 			    &tos, sizeof(tos)) == -1)
294 				log_warn("setsockopt(IPV6_TCLASS) "
295 				    "failed for %s",
296 				    from_addr);
297 		}
298 	}
299 	if (conn->conn_conf.isc_pcp != -1) {
300 		int pcp = conn->conn_conf.isc_pcp;
301 		if (to_ai->ai_family == AF_INET) {
302 			if (setsockopt(conn->conn_socket,
303 			    IPPROTO_IP, IP_VLAN_PCP,
304 			    &pcp, sizeof(pcp)) == -1)
305 				log_warn("setsockopt(IP_VLAN_PCP) "
306 				    "failed for %s",
307 				    from_addr);
308 		} else
309 		if (to_ai->ai_family == AF_INET6) {
310 			if (setsockopt(conn->conn_socket,
311 			    IPPROTO_IPV6, IPV6_VLAN_PCP,
312 			    &pcp, sizeof(pcp)) == -1)
313 				log_warn("setsockopt(IPV6_VLAN_PCP) "
314 				    "failed for %s",
315 				    from_addr);
316 		}
317 	}
318 	if (from_ai != NULL) {
319 		error = bind(conn->conn_socket, from_ai->ai_addr,
320 		    from_ai->ai_addrlen);
321 		if (error != 0) {
322 			fail(conn, strerror(errno));
323 			log_err(1, "failed to bind to %s", from_addr);
324 		}
325 	}
326 	log_debugx("connecting to %s", to_addr);
327 	error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen);
328 	if (error != 0) {
329 		fail(conn, strerror(errno));
330 		log_err(1, "failed to connect to %s", to_addr);
331 	}
332 
333 	return (conn);
334 }
335 
336 static void
337 handoff(struct connection *conn)
338 {
339 	struct iscsi_daemon_handoff idh;
340 	int error;
341 
342 	log_debugx("handing off connection to the kernel");
343 
344 	memset(&idh, 0, sizeof(idh));
345 	idh.idh_session_id = conn->conn_session_id;
346 	idh.idh_socket = conn->conn_socket;
347 	strlcpy(idh.idh_target_alias, conn->conn_target_alias,
348 	    sizeof(idh.idh_target_alias));
349 	idh.idh_tsih = conn->conn_tsih;
350 	idh.idh_statsn = conn->conn_statsn;
351 	idh.idh_protocol_level = conn->conn_protocol_level;
352 	idh.idh_header_digest = conn->conn_header_digest;
353 	idh.idh_data_digest = conn->conn_data_digest;
354 	idh.idh_initial_r2t = conn->conn_initial_r2t;
355 	idh.idh_immediate_data = conn->conn_immediate_data;
356 	idh.idh_max_recv_data_segment_length =
357 	    conn->conn_max_recv_data_segment_length;
358 	idh.idh_max_send_data_segment_length =
359 	    conn->conn_max_send_data_segment_length;
360 	idh.idh_max_burst_length = conn->conn_max_burst_length;
361 	idh.idh_first_burst_length = conn->conn_first_burst_length;
362 
363 	error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh);
364 	if (error != 0)
365 		log_err(1, "ISCSIDHANDOFF");
366 }
367 
368 void
369 fail(const struct connection *conn, const char *reason)
370 {
371 	struct iscsi_daemon_fail idf;
372 	int error, saved_errno;
373 
374 	saved_errno = errno;
375 
376 	memset(&idf, 0, sizeof(idf));
377 	idf.idf_session_id = conn->conn_session_id;
378 	strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason));
379 
380 	error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf);
381 	if (error != 0)
382 		log_err(1, "ISCSIDFAIL");
383 
384 	errno = saved_errno;
385 }
386 
387 /*
388  * XXX: I CANT INTO LATIN
389  */
390 static void
391 capsicate(struct connection *conn)
392 {
393 	cap_rights_t rights;
394 #ifdef ICL_KERNEL_PROXY
395 	const unsigned long cmds[] = { ISCSIDCONNECT, ISCSIDSEND, ISCSIDRECEIVE,
396 	    ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE, ISCSISMODIFY };
397 #else
398 	const unsigned long cmds[] = { ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD,
399 	    ISCSISREMOVE, ISCSISMODIFY };
400 #endif
401 
402 	cap_rights_init(&rights, CAP_IOCTL);
403 	if (caph_rights_limit(conn->conn_iscsi_fd, &rights) < 0)
404 		log_err(1, "cap_rights_limit");
405 
406 	if (caph_ioctls_limit(conn->conn_iscsi_fd, cmds, nitems(cmds)) < 0)
407 		log_err(1, "cap_ioctls_limit");
408 
409 	if (caph_enter() != 0)
410 		log_err(1, "cap_enter");
411 
412 	if (cap_sandboxed())
413 		log_debugx("Capsicum capability mode enabled");
414 	else
415 		log_warnx("Capsicum capability mode not supported");
416 }
417 
418 bool
419 timed_out(void)
420 {
421 
422 	return (sigalrm_received);
423 }
424 
425 static void
426 sigalrm_handler(int dummy __unused)
427 {
428 	/*
429 	 * It would be easiest to just log an error and exit.  We can't
430 	 * do this, though, because log_errx() is not signal safe, since
431 	 * it calls syslog(3).  Instead, set a flag checked by pdu_send()
432 	 * and pdu_receive(), to call log_errx() there.  Should they fail
433 	 * to notice, we'll exit here one second later.
434 	 */
435 	if (sigalrm_received) {
436 		/*
437 		 * Oh well.  Just give up and quit.
438 		 */
439 		_exit(2);
440 	}
441 
442 	sigalrm_received = true;
443 }
444 
445 static void
446 set_timeout(int timeout)
447 {
448 	struct sigaction sa;
449 	struct itimerval itv;
450 	int error;
451 
452 	if (timeout <= 0) {
453 		log_debugx("session timeout disabled");
454 		return;
455 	}
456 
457 	bzero(&sa, sizeof(sa));
458 	sa.sa_handler = sigalrm_handler;
459 	sigfillset(&sa.sa_mask);
460 	error = sigaction(SIGALRM, &sa, NULL);
461 	if (error != 0)
462 		log_err(1, "sigaction");
463 
464 	/*
465 	 * First SIGALRM will arive after conf_timeout seconds.
466 	 * If we do nothing, another one will arrive a second later.
467 	 */
468 	bzero(&itv, sizeof(itv));
469 	itv.it_interval.tv_sec = 1;
470 	itv.it_value.tv_sec = timeout;
471 
472 	log_debugx("setting session timeout to %d seconds",
473 	    timeout);
474 	error = setitimer(ITIMER_REAL, &itv, NULL);
475 	if (error != 0)
476 		log_err(1, "setitimer");
477 }
478 
479 static void
480 sigchld_handler(int dummy __unused)
481 {
482 
483 	/*
484 	 * The only purpose of this handler is to make SIGCHLD
485 	 * interrupt the ISCSIDWAIT ioctl(2), so we can call
486 	 * wait_for_children().
487 	 */
488 }
489 
490 static void
491 register_sigchld(void)
492 {
493 	struct sigaction sa;
494 	int error;
495 
496 	bzero(&sa, sizeof(sa));
497 	sa.sa_handler = sigchld_handler;
498 	sigfillset(&sa.sa_mask);
499 	error = sigaction(SIGCHLD, &sa, NULL);
500 	if (error != 0)
501 		log_err(1, "sigaction");
502 
503 }
504 
505 static void
506 handle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout)
507 {
508 	struct connection *conn;
509 
510 	log_set_peer_addr(request->idr_conf.isc_target_addr);
511 	if (request->idr_conf.isc_target[0] != '\0') {
512 		log_set_peer_name(request->idr_conf.isc_target);
513 		setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target);
514 	} else {
515 		setproctitle("%s", request->idr_conf.isc_target_addr);
516 	}
517 
518 	conn = connection_new(iscsi_fd, request);
519 	set_timeout(timeout);
520 	capsicate(conn);
521 	login(conn);
522 	if (conn->conn_conf.isc_discovery != 0)
523 		discovery(conn);
524 	else
525 		handoff(conn);
526 
527 	log_debugx("nothing more to do; exiting");
528 	exit (0);
529 }
530 
531 static int
532 wait_for_children(bool block)
533 {
534 	pid_t pid;
535 	int status;
536 	int num = 0;
537 
538 	for (;;) {
539 		/*
540 		 * If "block" is true, wait for at least one process.
541 		 */
542 		if (block && num == 0)
543 			pid = wait4(-1, &status, 0, NULL);
544 		else
545 			pid = wait4(-1, &status, WNOHANG, NULL);
546 		if (pid <= 0)
547 			break;
548 		if (WIFSIGNALED(status)) {
549 			log_warnx("child process %d terminated with signal %d",
550 			    pid, WTERMSIG(status));
551 		} else if (WEXITSTATUS(status) != 0) {
552 			log_warnx("child process %d terminated with exit status %d",
553 			    pid, WEXITSTATUS(status));
554 		} else {
555 			log_debugx("child process %d terminated gracefully", pid);
556 		}
557 		num++;
558 	}
559 
560 	return (num);
561 }
562 
563 int
564 main(int argc, char **argv)
565 {
566 	int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno,
567 	    timeout = 60;
568 	bool dont_daemonize = false;
569 	struct pidfh *pidfh;
570 	pid_t pid, otherpid;
571 	const char *pidfile_path = DEFAULT_PIDFILE;
572 	struct iscsi_daemon_request request;
573 
574 	while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) {
575 		switch (ch) {
576 		case 'P':
577 			pidfile_path = optarg;
578 			break;
579 		case 'd':
580 			dont_daemonize = true;
581 			debug++;
582 			break;
583 		case 'l':
584 			debug = atoi(optarg);
585 			break;
586 		case 'm':
587 			maxproc = atoi(optarg);
588 			break;
589 		case 't':
590 			timeout = atoi(optarg);
591 			break;
592 		case '?':
593 		default:
594 			usage();
595 		}
596 	}
597 	argc -= optind;
598 	if (argc != 0)
599 		usage();
600 
601 	log_init(debug);
602 
603 	pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
604 	if (pidfh == NULL) {
605 		if (errno == EEXIST)
606 			log_errx(1, "daemon already running, pid: %jd.",
607 			    (intmax_t)otherpid);
608 		log_err(1, "cannot open or create pidfile \"%s\"",
609 		    pidfile_path);
610 	}
611 
612 	iscsi_fd = open(ISCSI_PATH, O_RDWR);
613 	if (iscsi_fd < 0 && errno == ENOENT) {
614 		saved_errno = errno;
615 		retval = kldload("iscsi");
616 		if (retval != -1)
617 			iscsi_fd = open(ISCSI_PATH, O_RDWR);
618 		else
619 			errno = saved_errno;
620 	}
621 	if (iscsi_fd < 0)
622 		log_err(1, "failed to open %s", ISCSI_PATH);
623 
624 	if (dont_daemonize == false) {
625 		if (daemon(0, 0) == -1) {
626 			log_warn("cannot daemonize");
627 			pidfile_remove(pidfh);
628 			exit(1);
629 		}
630 	}
631 
632 	pidfile_write(pidfh);
633 
634 	register_sigchld();
635 
636 	for (;;) {
637 		log_debugx("waiting for request from the kernel");
638 
639 		memset(&request, 0, sizeof(request));
640 		error = ioctl(iscsi_fd, ISCSIDWAIT, &request);
641 		if (error != 0) {
642 			if (errno == EINTR) {
643 				nchildren -= wait_for_children(false);
644 				assert(nchildren >= 0);
645 				continue;
646 			}
647 
648 			log_err(1, "ISCSIDWAIT");
649 		}
650 
651 		if (dont_daemonize) {
652 			log_debugx("not forking due to -d flag; "
653 			    "will exit after servicing a single request");
654 		} else {
655 			nchildren -= wait_for_children(false);
656 			assert(nchildren >= 0);
657 
658 			while (maxproc > 0 && nchildren >= maxproc) {
659 				log_debugx("maxproc limit of %d child processes hit; "
660 				    "waiting for child process to exit", maxproc);
661 				nchildren -= wait_for_children(true);
662 				assert(nchildren >= 0);
663 			}
664 			log_debugx("incoming connection; forking child process #%d",
665 			    nchildren);
666 			nchildren++;
667 
668 			pid = fork();
669 			if (pid < 0)
670 				log_err(1, "fork");
671 			if (pid > 0)
672 				continue;
673 		}
674 
675 		pidfile_close(pidfh);
676 		handle_request(iscsi_fd, &request, timeout);
677 	}
678 
679 	return (0);
680 }
681