xref: /freebsd/usr.sbin/ctld/ctld.c (revision e76ce4484dcc72dca65172fa46f8b83a9f773fe6)
1009ea47eSEdward Tomasz Napierala /*-
2009ea47eSEdward Tomasz Napierala  * Copyright (c) 2012 The FreeBSD Foundation
3009ea47eSEdward Tomasz Napierala  * All rights reserved.
4009ea47eSEdward Tomasz Napierala  *
5009ea47eSEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
6009ea47eSEdward Tomasz Napierala  * from the FreeBSD Foundation.
7009ea47eSEdward Tomasz Napierala  *
8009ea47eSEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
9009ea47eSEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
10009ea47eSEdward Tomasz Napierala  * are met:
11009ea47eSEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
12009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
13009ea47eSEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
14009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
15009ea47eSEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
16009ea47eSEdward Tomasz Napierala  *
17009ea47eSEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18009ea47eSEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19009ea47eSEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20009ea47eSEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21009ea47eSEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22009ea47eSEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23009ea47eSEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24009ea47eSEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25009ea47eSEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26009ea47eSEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27009ea47eSEdward Tomasz Napierala  * SUCH DAMAGE.
28009ea47eSEdward Tomasz Napierala  *
29009ea47eSEdward Tomasz Napierala  * $FreeBSD$
30009ea47eSEdward Tomasz Napierala  */
31009ea47eSEdward Tomasz Napierala 
32009ea47eSEdward Tomasz Napierala #include <sys/types.h>
33009ea47eSEdward Tomasz Napierala #include <sys/time.h>
34009ea47eSEdward Tomasz Napierala #include <sys/socket.h>
35009ea47eSEdward Tomasz Napierala #include <sys/wait.h>
36009ea47eSEdward Tomasz Napierala #include <netinet/in.h>
37009ea47eSEdward Tomasz Napierala #include <assert.h>
38009ea47eSEdward Tomasz Napierala #include <ctype.h>
39009ea47eSEdward Tomasz Napierala #include <errno.h>
40009ea47eSEdward Tomasz Napierala #include <netdb.h>
41009ea47eSEdward Tomasz Napierala #include <signal.h>
42009ea47eSEdward Tomasz Napierala #include <stdbool.h>
43009ea47eSEdward Tomasz Napierala #include <stdio.h>
44009ea47eSEdward Tomasz Napierala #include <stdint.h>
45009ea47eSEdward Tomasz Napierala #include <stdlib.h>
46009ea47eSEdward Tomasz Napierala #include <string.h>
47009ea47eSEdward Tomasz Napierala #include <unistd.h>
48009ea47eSEdward Tomasz Napierala 
49009ea47eSEdward Tomasz Napierala #include "ctld.h"
50009ea47eSEdward Tomasz Napierala 
51009ea47eSEdward Tomasz Napierala static volatile bool sighup_received = false;
52009ea47eSEdward Tomasz Napierala static volatile bool sigterm_received = false;
53009ea47eSEdward Tomasz Napierala static volatile bool sigalrm_received = false;
54009ea47eSEdward Tomasz Napierala 
55009ea47eSEdward Tomasz Napierala static int nchildren = 0;
56009ea47eSEdward Tomasz Napierala 
57009ea47eSEdward Tomasz Napierala static void
58009ea47eSEdward Tomasz Napierala usage(void)
59009ea47eSEdward Tomasz Napierala {
60009ea47eSEdward Tomasz Napierala 
61009ea47eSEdward Tomasz Napierala 	fprintf(stderr, "usage: ctld [-d][-f config-file]\n");
62009ea47eSEdward Tomasz Napierala 	exit(1);
63009ea47eSEdward Tomasz Napierala }
64009ea47eSEdward Tomasz Napierala 
65009ea47eSEdward Tomasz Napierala char *
66009ea47eSEdward Tomasz Napierala checked_strdup(const char *s)
67009ea47eSEdward Tomasz Napierala {
68009ea47eSEdward Tomasz Napierala 	char *c;
69009ea47eSEdward Tomasz Napierala 
70009ea47eSEdward Tomasz Napierala 	c = strdup(s);
71009ea47eSEdward Tomasz Napierala 	if (c == NULL)
72009ea47eSEdward Tomasz Napierala 		log_err(1, "strdup");
73009ea47eSEdward Tomasz Napierala 	return (c);
74009ea47eSEdward Tomasz Napierala }
75009ea47eSEdward Tomasz Napierala 
76009ea47eSEdward Tomasz Napierala struct conf *
77009ea47eSEdward Tomasz Napierala conf_new(void)
78009ea47eSEdward Tomasz Napierala {
79009ea47eSEdward Tomasz Napierala 	struct conf *conf;
80009ea47eSEdward Tomasz Napierala 
81009ea47eSEdward Tomasz Napierala 	conf = calloc(1, sizeof(*conf));
82009ea47eSEdward Tomasz Napierala 	if (conf == NULL)
83009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
84009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_targets);
85009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_auth_groups);
86009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&conf->conf_portal_groups);
87009ea47eSEdward Tomasz Napierala 
88009ea47eSEdward Tomasz Napierala 	conf->conf_debug = 0;
89009ea47eSEdward Tomasz Napierala 	conf->conf_timeout = 60;
90009ea47eSEdward Tomasz Napierala 	conf->conf_maxproc = 30;
91009ea47eSEdward Tomasz Napierala 
92009ea47eSEdward Tomasz Napierala 	return (conf);
93009ea47eSEdward Tomasz Napierala }
94009ea47eSEdward Tomasz Napierala 
95009ea47eSEdward Tomasz Napierala void
96009ea47eSEdward Tomasz Napierala conf_delete(struct conf *conf)
97009ea47eSEdward Tomasz Napierala {
98009ea47eSEdward Tomasz Napierala 	struct target *targ, *tmp;
99009ea47eSEdward Tomasz Napierala 	struct auth_group *ag, *cagtmp;
100009ea47eSEdward Tomasz Napierala 	struct portal_group *pg, *cpgtmp;
101009ea47eSEdward Tomasz Napierala 
102009ea47eSEdward Tomasz Napierala 	assert(conf->conf_pidfh == NULL);
103009ea47eSEdward Tomasz Napierala 
104009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
105009ea47eSEdward Tomasz Napierala 		target_delete(targ);
106009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
107009ea47eSEdward Tomasz Napierala 		auth_group_delete(ag);
108009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
109009ea47eSEdward Tomasz Napierala 		portal_group_delete(pg);
110009ea47eSEdward Tomasz Napierala 	free(conf->conf_pidfile_path);
111009ea47eSEdward Tomasz Napierala 	free(conf);
112009ea47eSEdward Tomasz Napierala }
113009ea47eSEdward Tomasz Napierala 
114009ea47eSEdward Tomasz Napierala static struct auth *
115009ea47eSEdward Tomasz Napierala auth_new(struct auth_group *ag)
116009ea47eSEdward Tomasz Napierala {
117009ea47eSEdward Tomasz Napierala 	struct auth *auth;
118009ea47eSEdward Tomasz Napierala 
119009ea47eSEdward Tomasz Napierala 	auth = calloc(1, sizeof(*auth));
120009ea47eSEdward Tomasz Napierala 	if (auth == NULL)
121009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
122009ea47eSEdward Tomasz Napierala 	auth->a_auth_group = ag;
123009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_auths, auth, a_next);
124009ea47eSEdward Tomasz Napierala 	return (auth);
125009ea47eSEdward Tomasz Napierala }
126009ea47eSEdward Tomasz Napierala 
127009ea47eSEdward Tomasz Napierala static void
128009ea47eSEdward Tomasz Napierala auth_delete(struct auth *auth)
129009ea47eSEdward Tomasz Napierala {
130009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&auth->a_auth_group->ag_auths, auth, a_next);
131009ea47eSEdward Tomasz Napierala 
132009ea47eSEdward Tomasz Napierala 	free(auth->a_user);
133009ea47eSEdward Tomasz Napierala 	free(auth->a_secret);
134009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_user);
135009ea47eSEdward Tomasz Napierala 	free(auth->a_mutual_secret);
136009ea47eSEdward Tomasz Napierala 	free(auth);
137009ea47eSEdward Tomasz Napierala }
138009ea47eSEdward Tomasz Napierala 
139009ea47eSEdward Tomasz Napierala const struct auth *
140009ea47eSEdward Tomasz Napierala auth_find(struct auth_group *ag, const char *user)
141009ea47eSEdward Tomasz Napierala {
142009ea47eSEdward Tomasz Napierala 	const struct auth *auth;
143009ea47eSEdward Tomasz Napierala 
144009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(auth, &ag->ag_auths, a_next) {
145009ea47eSEdward Tomasz Napierala 		if (strcmp(auth->a_user, user) == 0)
146009ea47eSEdward Tomasz Napierala 			return (auth);
147009ea47eSEdward Tomasz Napierala 	}
148009ea47eSEdward Tomasz Napierala 
149009ea47eSEdward Tomasz Napierala 	return (NULL);
150009ea47eSEdward Tomasz Napierala }
151009ea47eSEdward Tomasz Napierala 
1521658628dSEdward Tomasz Napierala static void
1531658628dSEdward Tomasz Napierala auth_check_secret_length(struct auth *auth)
1541658628dSEdward Tomasz Napierala {
1551658628dSEdward Tomasz Napierala 	size_t len;
1561658628dSEdward Tomasz Napierala 
1571658628dSEdward Tomasz Napierala 	len = strlen(auth->a_secret);
1581658628dSEdward Tomasz Napierala 	if (len > 16) {
1591658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1601658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1611658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1621658628dSEdward Tomasz Napierala 			    "long", auth->a_user, auth->a_auth_group->ag_name);
1631658628dSEdward Tomasz Napierala 		else
1641658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
1651658628dSEdward Tomasz Napierala 			    "is too long; it should be at most 16 characters "
1661658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
167f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
1681658628dSEdward Tomasz Napierala 	}
1691658628dSEdward Tomasz Napierala 	if (len < 12) {
1701658628dSEdward Tomasz Napierala 		if (auth->a_auth_group->ag_name != NULL)
1711658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", auth-group \"%s\", "
1721658628dSEdward Tomasz Napierala 			    "is too short; it should be at least 12 characters "
1731658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
1741658628dSEdward Tomasz Napierala 			    auth->a_auth_group->ag_name);
1751658628dSEdward Tomasz Napierala 		else
1761658628dSEdward Tomasz Napierala 			log_warnx("secret for user \"%s\", target \"%s\", "
1771658628dSEdward Tomasz Napierala 			    "is too short; it should be at least 16 characters "
1781658628dSEdward Tomasz Napierala 			    "long", auth->a_user,
179f7ae5bf8SEdward Tomasz Napierala 			    auth->a_auth_group->ag_target->t_name);
1801658628dSEdward Tomasz Napierala 	}
1811658628dSEdward Tomasz Napierala 
1821658628dSEdward Tomasz Napierala 	if (auth->a_mutual_secret != NULL) {
1831658628dSEdward Tomasz Napierala 		len = strlen(auth->a_secret);
1841658628dSEdward Tomasz Napierala 		if (len > 16) {
1851658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
1861658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
1871658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too long; it should "
1881658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
1891658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
1901658628dSEdward Tomasz Napierala 			else
1911658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
1921658628dSEdward Tomasz Napierala 				    "target \"%s\", is too long; it should "
1931658628dSEdward Tomasz Napierala 				    "be at most 16 characters long",
1941658628dSEdward Tomasz Napierala 				    auth->a_user,
195f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
1961658628dSEdward Tomasz Napierala 		}
1971658628dSEdward Tomasz Napierala 		if (len < 12) {
1981658628dSEdward Tomasz Napierala 			if (auth->a_auth_group->ag_name != NULL)
1991658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2001658628dSEdward Tomasz Napierala 				    "auth-group \"%s\", is too short; it "
2011658628dSEdward Tomasz Napierala 				    "should be at least 12 characters long",
2021658628dSEdward Tomasz Napierala 				    auth->a_user, auth->a_auth_group->ag_name);
2031658628dSEdward Tomasz Napierala 			else
2041658628dSEdward Tomasz Napierala 				log_warnx("mutual secret for user \"%s\", "
2051658628dSEdward Tomasz Napierala 				    "target \"%s\", is too short; it should be "
2061658628dSEdward Tomasz Napierala 				    "at least 16 characters long",
2071658628dSEdward Tomasz Napierala 				    auth->a_user,
208f7ae5bf8SEdward Tomasz Napierala 				    auth->a_auth_group->ag_target->t_name);
2091658628dSEdward Tomasz Napierala 		}
2101658628dSEdward Tomasz Napierala 	}
2111658628dSEdward Tomasz Napierala }
2121658628dSEdward Tomasz Napierala 
2131658628dSEdward Tomasz Napierala const struct auth *
2141658628dSEdward Tomasz Napierala auth_new_chap(struct auth_group *ag, const char *user,
2151658628dSEdward Tomasz Napierala     const char *secret)
2161658628dSEdward Tomasz Napierala {
2171658628dSEdward Tomasz Napierala 	struct auth *auth;
2181658628dSEdward Tomasz Napierala 
2191658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2201658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP;
2211658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP) {
2221658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2231658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2241658628dSEdward Tomasz Napierala 			    "other types for auth-group \"%s\"", ag->ag_name);
2251658628dSEdward Tomasz Napierala 		else
2261658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap\" authentication with "
2271658628dSEdward Tomasz Napierala 			    "other types for target \"%s\"",
228f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2291658628dSEdward Tomasz Napierala 		return (NULL);
2301658628dSEdward Tomasz Napierala 	}
2311658628dSEdward Tomasz Napierala 
2321658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2331658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2341658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2351658628dSEdward Tomasz Napierala 
2361658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2371658628dSEdward Tomasz Napierala 
2381658628dSEdward Tomasz Napierala 	return (auth);
2391658628dSEdward Tomasz Napierala }
2401658628dSEdward Tomasz Napierala 
2411658628dSEdward Tomasz Napierala const struct auth *
2421658628dSEdward Tomasz Napierala auth_new_chap_mutual(struct auth_group *ag, const char *user,
2431658628dSEdward Tomasz Napierala     const char *secret, const char *user2, const char *secret2)
2441658628dSEdward Tomasz Napierala {
2451658628dSEdward Tomasz Napierala 	struct auth *auth;
2461658628dSEdward Tomasz Napierala 
2471658628dSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN)
2481658628dSEdward Tomasz Napierala 		ag->ag_type = AG_TYPE_CHAP_MUTUAL;
2491658628dSEdward Tomasz Napierala 	if (ag->ag_type != AG_TYPE_CHAP_MUTUAL) {
2501658628dSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
2511658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2521658628dSEdward Tomasz Napierala 			    "with other types for auth-group \"%s\"",
2531658628dSEdward Tomasz Napierala 			    ag->ag_name);
2541658628dSEdward Tomasz Napierala 		else
2551658628dSEdward Tomasz Napierala 			log_warnx("cannot mix \"chap-mutual\" authentication "
2561658628dSEdward Tomasz Napierala 			    "with other types for target \"%s\"",
257f7ae5bf8SEdward Tomasz Napierala 			    ag->ag_target->t_name);
2581658628dSEdward Tomasz Napierala 		return (NULL);
2591658628dSEdward Tomasz Napierala 	}
2601658628dSEdward Tomasz Napierala 
2611658628dSEdward Tomasz Napierala 	auth = auth_new(ag);
2621658628dSEdward Tomasz Napierala 	auth->a_user = checked_strdup(user);
2631658628dSEdward Tomasz Napierala 	auth->a_secret = checked_strdup(secret);
2641658628dSEdward Tomasz Napierala 	auth->a_mutual_user = checked_strdup(user2);
2651658628dSEdward Tomasz Napierala 	auth->a_mutual_secret = checked_strdup(secret2);
2661658628dSEdward Tomasz Napierala 
2671658628dSEdward Tomasz Napierala 	auth_check_secret_length(auth);
2681658628dSEdward Tomasz Napierala 
2691658628dSEdward Tomasz Napierala 	return (auth);
2701658628dSEdward Tomasz Napierala }
2711658628dSEdward Tomasz Napierala 
2728cb2e958SEdward Tomasz Napierala const struct auth_name *
2738cb2e958SEdward Tomasz Napierala auth_name_new(struct auth_group *ag, const char *name)
2748cb2e958SEdward Tomasz Napierala {
2758cb2e958SEdward Tomasz Napierala 	struct auth_name *an;
2768cb2e958SEdward Tomasz Napierala 
2778cb2e958SEdward Tomasz Napierala 	an = calloc(1, sizeof(*an));
2788cb2e958SEdward Tomasz Napierala 	if (an == NULL)
2798cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
2808cb2e958SEdward Tomasz Napierala 	an->an_auth_group = ag;
2818cb2e958SEdward Tomasz Napierala 	an->an_initator_name = checked_strdup(name);
2828cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_names, an, an_next);
2838cb2e958SEdward Tomasz Napierala 	return (an);
2848cb2e958SEdward Tomasz Napierala }
2858cb2e958SEdward Tomasz Napierala 
2868cb2e958SEdward Tomasz Napierala static void
2878cb2e958SEdward Tomasz Napierala auth_name_delete(struct auth_name *an)
2888cb2e958SEdward Tomasz Napierala {
2898cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&an->an_auth_group->ag_names, an, an_next);
2908cb2e958SEdward Tomasz Napierala 
2918cb2e958SEdward Tomasz Napierala 	free(an->an_initator_name);
2928cb2e958SEdward Tomasz Napierala 	free(an);
2938cb2e958SEdward Tomasz Napierala }
2948cb2e958SEdward Tomasz Napierala 
2958cb2e958SEdward Tomasz Napierala bool
2968cb2e958SEdward Tomasz Napierala auth_name_defined(const struct auth_group *ag)
2978cb2e958SEdward Tomasz Napierala {
2988cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_names))
2998cb2e958SEdward Tomasz Napierala 		return (false);
3008cb2e958SEdward Tomasz Napierala 	return (true);
3018cb2e958SEdward Tomasz Napierala }
3028cb2e958SEdward Tomasz Napierala 
3038cb2e958SEdward Tomasz Napierala const struct auth_name *
3048cb2e958SEdward Tomasz Napierala auth_name_find(const struct auth_group *ag, const char *name)
3058cb2e958SEdward Tomasz Napierala {
3068cb2e958SEdward Tomasz Napierala 	const struct auth_name *auth_name;
3078cb2e958SEdward Tomasz Napierala 
3088cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH(auth_name, &ag->ag_names, an_next) {
3098cb2e958SEdward Tomasz Napierala 		if (strcmp(auth_name->an_initator_name, name) == 0)
3108cb2e958SEdward Tomasz Napierala 			return (auth_name);
3118cb2e958SEdward Tomasz Napierala 	}
3128cb2e958SEdward Tomasz Napierala 
3138cb2e958SEdward Tomasz Napierala 	return (NULL);
3148cb2e958SEdward Tomasz Napierala }
3158cb2e958SEdward Tomasz Napierala 
3168cb2e958SEdward Tomasz Napierala const struct auth_portal *
3178cb2e958SEdward Tomasz Napierala auth_portal_new(struct auth_group *ag, const char *portal)
3188cb2e958SEdward Tomasz Napierala {
3198cb2e958SEdward Tomasz Napierala 	struct auth_portal *ap;
3208cb2e958SEdward Tomasz Napierala 
3218cb2e958SEdward Tomasz Napierala 	ap = calloc(1, sizeof(*ap));
3228cb2e958SEdward Tomasz Napierala 	if (ap == NULL)
3238cb2e958SEdward Tomasz Napierala 		log_err(1, "calloc");
3248cb2e958SEdward Tomasz Napierala 	ap->ap_auth_group = ag;
3258cb2e958SEdward Tomasz Napierala 	ap->ap_initator_portal = checked_strdup(portal);
3268cb2e958SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&ag->ag_portals, ap, ap_next);
3278cb2e958SEdward Tomasz Napierala 	return (ap);
3288cb2e958SEdward Tomasz Napierala }
3298cb2e958SEdward Tomasz Napierala 
3308cb2e958SEdward Tomasz Napierala static void
3318cb2e958SEdward Tomasz Napierala auth_portal_delete(struct auth_portal *ap)
3328cb2e958SEdward Tomasz Napierala {
3338cb2e958SEdward Tomasz Napierala 	TAILQ_REMOVE(&ap->ap_auth_group->ag_portals, ap, ap_next);
3348cb2e958SEdward Tomasz Napierala 
3358cb2e958SEdward Tomasz Napierala 	free(ap->ap_initator_portal);
3368cb2e958SEdward Tomasz Napierala 	free(ap);
3378cb2e958SEdward Tomasz Napierala }
3388cb2e958SEdward Tomasz Napierala 
3398cb2e958SEdward Tomasz Napierala bool
3408cb2e958SEdward Tomasz Napierala auth_portal_defined(const struct auth_group *ag)
3418cb2e958SEdward Tomasz Napierala {
3428cb2e958SEdward Tomasz Napierala 	if (TAILQ_EMPTY(&ag->ag_portals))
3438cb2e958SEdward Tomasz Napierala 		return (false);
3448cb2e958SEdward Tomasz Napierala 	return (true);
3458cb2e958SEdward Tomasz Napierala }
3468cb2e958SEdward Tomasz Napierala 
3478cb2e958SEdward Tomasz Napierala const struct auth_portal *
3488cb2e958SEdward Tomasz Napierala auth_portal_find(const struct auth_group *ag, const char *portal)
3498cb2e958SEdward Tomasz Napierala {
3508cb2e958SEdward Tomasz Napierala 	const struct auth_portal *auth_portal;
3518cb2e958SEdward Tomasz Napierala 
3528cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH(auth_portal, &ag->ag_portals, ap_next) {
3538cb2e958SEdward Tomasz Napierala 		if (strcmp(auth_portal->ap_initator_portal, portal) == 0)
3548cb2e958SEdward Tomasz Napierala 			return (auth_portal);
3558cb2e958SEdward Tomasz Napierala 	}
3568cb2e958SEdward Tomasz Napierala 
3578cb2e958SEdward Tomasz Napierala 	return (NULL);
3588cb2e958SEdward Tomasz Napierala }
3598cb2e958SEdward Tomasz Napierala 
360009ea47eSEdward Tomasz Napierala struct auth_group *
361009ea47eSEdward Tomasz Napierala auth_group_new(struct conf *conf, const char *name)
362009ea47eSEdward Tomasz Napierala {
363009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
364009ea47eSEdward Tomasz Napierala 
365009ea47eSEdward Tomasz Napierala 	if (name != NULL) {
366009ea47eSEdward Tomasz Napierala 		ag = auth_group_find(conf, name);
367009ea47eSEdward Tomasz Napierala 		if (ag != NULL) {
368009ea47eSEdward Tomasz Napierala 			log_warnx("duplicated auth-group \"%s\"", name);
369009ea47eSEdward Tomasz Napierala 			return (NULL);
370009ea47eSEdward Tomasz Napierala 		}
371009ea47eSEdward Tomasz Napierala 	}
372009ea47eSEdward Tomasz Napierala 
373009ea47eSEdward Tomasz Napierala 	ag = calloc(1, sizeof(*ag));
374009ea47eSEdward Tomasz Napierala 	if (ag == NULL)
375009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
376009ea47eSEdward Tomasz Napierala 	if (name != NULL)
377009ea47eSEdward Tomasz Napierala 		ag->ag_name = checked_strdup(name);
378009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_auths);
3798cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_names);
3808cb2e958SEdward Tomasz Napierala 	TAILQ_INIT(&ag->ag_portals);
381009ea47eSEdward Tomasz Napierala 	ag->ag_conf = conf;
382009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_auth_groups, ag, ag_next);
383009ea47eSEdward Tomasz Napierala 
384009ea47eSEdward Tomasz Napierala 	return (ag);
385009ea47eSEdward Tomasz Napierala }
386009ea47eSEdward Tomasz Napierala 
387009ea47eSEdward Tomasz Napierala void
388009ea47eSEdward Tomasz Napierala auth_group_delete(struct auth_group *ag)
389009ea47eSEdward Tomasz Napierala {
3908cb2e958SEdward Tomasz Napierala 	struct auth *auth, *auth_tmp;
3918cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name, *auth_name_tmp;
3928cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal, *auth_portal_tmp;
393009ea47eSEdward Tomasz Napierala 
394009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&ag->ag_conf->conf_auth_groups, ag, ag_next);
395009ea47eSEdward Tomasz Napierala 
3968cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth, &ag->ag_auths, a_next, auth_tmp)
397009ea47eSEdward Tomasz Napierala 		auth_delete(auth);
3988cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_name, &ag->ag_names, an_next, auth_name_tmp)
3998cb2e958SEdward Tomasz Napierala 		auth_name_delete(auth_name);
4008cb2e958SEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(auth_portal, &ag->ag_portals, ap_next,
4018cb2e958SEdward Tomasz Napierala 	    auth_portal_tmp)
4028cb2e958SEdward Tomasz Napierala 		auth_portal_delete(auth_portal);
403009ea47eSEdward Tomasz Napierala 	free(ag->ag_name);
404009ea47eSEdward Tomasz Napierala 	free(ag);
405009ea47eSEdward Tomasz Napierala }
406009ea47eSEdward Tomasz Napierala 
407009ea47eSEdward Tomasz Napierala struct auth_group *
408009ea47eSEdward Tomasz Napierala auth_group_find(struct conf *conf, const char *name)
409009ea47eSEdward Tomasz Napierala {
410009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
411009ea47eSEdward Tomasz Napierala 
412009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
413009ea47eSEdward Tomasz Napierala 		if (ag->ag_name != NULL && strcmp(ag->ag_name, name) == 0)
414009ea47eSEdward Tomasz Napierala 			return (ag);
415009ea47eSEdward Tomasz Napierala 	}
416009ea47eSEdward Tomasz Napierala 
417009ea47eSEdward Tomasz Napierala 	return (NULL);
418009ea47eSEdward Tomasz Napierala }
419009ea47eSEdward Tomasz Napierala 
420df9900fbSEdward Tomasz Napierala static int
421df9900fbSEdward Tomasz Napierala auth_group_set_type(struct auth_group *ag, int type)
422df9900fbSEdward Tomasz Napierala {
423df9900fbSEdward Tomasz Napierala 
424df9900fbSEdward Tomasz Napierala 	if (ag->ag_type == AG_TYPE_UNKNOWN) {
425df9900fbSEdward Tomasz Napierala 		ag->ag_type = type;
426df9900fbSEdward Tomasz Napierala 		return (0);
427df9900fbSEdward Tomasz Napierala 	}
428df9900fbSEdward Tomasz Napierala 
429df9900fbSEdward Tomasz Napierala 	if (ag->ag_type == type)
430df9900fbSEdward Tomasz Napierala 		return (0);
431df9900fbSEdward Tomasz Napierala 
432df9900fbSEdward Tomasz Napierala 	return (1);
433df9900fbSEdward Tomasz Napierala }
434df9900fbSEdward Tomasz Napierala 
435df9900fbSEdward Tomasz Napierala int
436df9900fbSEdward Tomasz Napierala auth_group_set_type_str(struct auth_group *ag, const char *str)
437df9900fbSEdward Tomasz Napierala {
438df9900fbSEdward Tomasz Napierala 	int error, type;
439df9900fbSEdward Tomasz Napierala 
440df9900fbSEdward Tomasz Napierala 	if (strcmp(str, "none") == 0) {
441df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_NO_AUTHENTICATION;
442*e76ce448SEdward Tomasz Napierala 	} else if (strcmp(str, "deny") == 0) {
443*e76ce448SEdward Tomasz Napierala 		type = AG_TYPE_DENY;
444df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap") == 0) {
445df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP;
446df9900fbSEdward Tomasz Napierala 	} else if (strcmp(str, "chap-mutual") == 0) {
447df9900fbSEdward Tomasz Napierala 		type = AG_TYPE_CHAP_MUTUAL;
448df9900fbSEdward Tomasz Napierala 	} else {
449df9900fbSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
450df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for auth-group "
451df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_name);
452df9900fbSEdward Tomasz Napierala 		else
453df9900fbSEdward Tomasz Napierala 			log_warnx("invalid auth-type \"%s\" for target "
454df9900fbSEdward Tomasz Napierala 			    "\"%s\"", str, ag->ag_target->t_name);
455df9900fbSEdward Tomasz Napierala 		return (1);
456df9900fbSEdward Tomasz Napierala 	}
457df9900fbSEdward Tomasz Napierala 
458df9900fbSEdward Tomasz Napierala 	error = auth_group_set_type(ag, type);
459df9900fbSEdward Tomasz Napierala 	if (error != 0) {
460df9900fbSEdward Tomasz Napierala 		if (ag->ag_name != NULL)
461df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for "
462df9900fbSEdward Tomasz Napierala 			    "auth-group \"%s\"; already has a different "
463df9900fbSEdward Tomasz Napierala 			    "type", str, ag->ag_name);
464df9900fbSEdward Tomasz Napierala 		else
465df9900fbSEdward Tomasz Napierala 			log_warnx("cannot set auth-type to \"%s\" for target "
466df9900fbSEdward Tomasz Napierala 			    "\"%s\"; already has a different type",
467df9900fbSEdward Tomasz Napierala 			    str, ag->ag_target->t_name);
468df9900fbSEdward Tomasz Napierala 		return (1);
469df9900fbSEdward Tomasz Napierala 	}
470df9900fbSEdward Tomasz Napierala 
471df9900fbSEdward Tomasz Napierala 	return (error);
472df9900fbSEdward Tomasz Napierala }
473df9900fbSEdward Tomasz Napierala 
474009ea47eSEdward Tomasz Napierala static struct portal *
475009ea47eSEdward Tomasz Napierala portal_new(struct portal_group *pg)
476009ea47eSEdward Tomasz Napierala {
477009ea47eSEdward Tomasz Napierala 	struct portal *portal;
478009ea47eSEdward Tomasz Napierala 
479009ea47eSEdward Tomasz Napierala 	portal = calloc(1, sizeof(*portal));
480009ea47eSEdward Tomasz Napierala 	if (portal == NULL)
481009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
482009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&portal->p_targets);
483009ea47eSEdward Tomasz Napierala 	portal->p_portal_group = pg;
484009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&pg->pg_portals, portal, p_next);
485009ea47eSEdward Tomasz Napierala 	return (portal);
486009ea47eSEdward Tomasz Napierala }
487009ea47eSEdward Tomasz Napierala 
488009ea47eSEdward Tomasz Napierala static void
489009ea47eSEdward Tomasz Napierala portal_delete(struct portal *portal)
490009ea47eSEdward Tomasz Napierala {
491009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&portal->p_portal_group->pg_portals, portal, p_next);
492009ea47eSEdward Tomasz Napierala 	freeaddrinfo(portal->p_ai);
493009ea47eSEdward Tomasz Napierala 	free(portal->p_listen);
494009ea47eSEdward Tomasz Napierala 	free(portal);
495009ea47eSEdward Tomasz Napierala }
496009ea47eSEdward Tomasz Napierala 
497009ea47eSEdward Tomasz Napierala struct portal_group *
498009ea47eSEdward Tomasz Napierala portal_group_new(struct conf *conf, const char *name)
499009ea47eSEdward Tomasz Napierala {
500009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
501009ea47eSEdward Tomasz Napierala 
502009ea47eSEdward Tomasz Napierala 	pg = portal_group_find(conf, name);
503009ea47eSEdward Tomasz Napierala 	if (pg != NULL) {
504009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated portal-group \"%s\"", name);
505009ea47eSEdward Tomasz Napierala 		return (NULL);
506009ea47eSEdward Tomasz Napierala 	}
507009ea47eSEdward Tomasz Napierala 
508009ea47eSEdward Tomasz Napierala 	pg = calloc(1, sizeof(*pg));
509009ea47eSEdward Tomasz Napierala 	if (pg == NULL)
510009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
511009ea47eSEdward Tomasz Napierala 	pg->pg_name = checked_strdup(name);
512009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&pg->pg_portals);
513009ea47eSEdward Tomasz Napierala 	pg->pg_conf = conf;
514009ea47eSEdward Tomasz Napierala 	conf->conf_last_portal_group_tag++;
515009ea47eSEdward Tomasz Napierala 	pg->pg_tag = conf->conf_last_portal_group_tag;
516009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
517009ea47eSEdward Tomasz Napierala 
518009ea47eSEdward Tomasz Napierala 	return (pg);
519009ea47eSEdward Tomasz Napierala }
520009ea47eSEdward Tomasz Napierala 
521009ea47eSEdward Tomasz Napierala void
522009ea47eSEdward Tomasz Napierala portal_group_delete(struct portal_group *pg)
523009ea47eSEdward Tomasz Napierala {
524009ea47eSEdward Tomasz Napierala 	struct portal *portal, *tmp;
525009ea47eSEdward Tomasz Napierala 
526009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
527009ea47eSEdward Tomasz Napierala 
528009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
529009ea47eSEdward Tomasz Napierala 		portal_delete(portal);
530009ea47eSEdward Tomasz Napierala 	free(pg->pg_name);
531009ea47eSEdward Tomasz Napierala 	free(pg);
532009ea47eSEdward Tomasz Napierala }
533009ea47eSEdward Tomasz Napierala 
534009ea47eSEdward Tomasz Napierala struct portal_group *
535009ea47eSEdward Tomasz Napierala portal_group_find(struct conf *conf, const char *name)
536009ea47eSEdward Tomasz Napierala {
537009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
538009ea47eSEdward Tomasz Napierala 
539009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
540009ea47eSEdward Tomasz Napierala 		if (strcmp(pg->pg_name, name) == 0)
541009ea47eSEdward Tomasz Napierala 			return (pg);
542009ea47eSEdward Tomasz Napierala 	}
543009ea47eSEdward Tomasz Napierala 
544009ea47eSEdward Tomasz Napierala 	return (NULL);
545009ea47eSEdward Tomasz Napierala }
546009ea47eSEdward Tomasz Napierala 
547009ea47eSEdward Tomasz Napierala int
548009ea47eSEdward Tomasz Napierala portal_group_add_listen(struct portal_group *pg, const char *value, bool iser)
549009ea47eSEdward Tomasz Napierala {
550009ea47eSEdward Tomasz Napierala 	struct addrinfo hints;
551009ea47eSEdward Tomasz Napierala 	struct portal *portal;
552009ea47eSEdward Tomasz Napierala 	char *addr, *ch, *arg;
553009ea47eSEdward Tomasz Napierala 	const char *port;
554009ea47eSEdward Tomasz Napierala 	int error, colons = 0;
555009ea47eSEdward Tomasz Napierala 
556009ea47eSEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY
557009ea47eSEdward Tomasz Napierala 	if (iser) {
558009ea47eSEdward Tomasz Napierala 		log_warnx("ctld(8) compiled without ICL_KERNEL_PROXY "
559009ea47eSEdward Tomasz Napierala 		    "does not support iSER protocol");
560009ea47eSEdward Tomasz Napierala 		return (-1);
561009ea47eSEdward Tomasz Napierala 	}
562009ea47eSEdward Tomasz Napierala #endif
563009ea47eSEdward Tomasz Napierala 
564009ea47eSEdward Tomasz Napierala 	portal = portal_new(pg);
565009ea47eSEdward Tomasz Napierala 	portal->p_listen = checked_strdup(value);
566009ea47eSEdward Tomasz Napierala 	portal->p_iser = iser;
567009ea47eSEdward Tomasz Napierala 
568009ea47eSEdward Tomasz Napierala 	arg = portal->p_listen;
569009ea47eSEdward Tomasz Napierala 	if (arg[0] == '\0') {
570009ea47eSEdward Tomasz Napierala 		log_warnx("empty listen address");
571009ea47eSEdward Tomasz Napierala 		free(portal->p_listen);
572009ea47eSEdward Tomasz Napierala 		free(portal);
573009ea47eSEdward Tomasz Napierala 		return (1);
574009ea47eSEdward Tomasz Napierala 	}
575009ea47eSEdward Tomasz Napierala 	if (arg[0] == '[') {
576009ea47eSEdward Tomasz Napierala 		/*
577009ea47eSEdward Tomasz Napierala 		 * IPv6 address in square brackets, perhaps with port.
578009ea47eSEdward Tomasz Napierala 		 */
579009ea47eSEdward Tomasz Napierala 		arg++;
580009ea47eSEdward Tomasz Napierala 		addr = strsep(&arg, "]");
581009ea47eSEdward Tomasz Napierala 		if (arg == NULL) {
582009ea47eSEdward Tomasz Napierala 			log_warnx("invalid listen address %s",
583009ea47eSEdward Tomasz Napierala 			    portal->p_listen);
584009ea47eSEdward Tomasz Napierala 			free(portal->p_listen);
585009ea47eSEdward Tomasz Napierala 			free(portal);
586009ea47eSEdward Tomasz Napierala 			return (1);
587009ea47eSEdward Tomasz Napierala 		}
588009ea47eSEdward Tomasz Napierala 		if (arg[0] == '\0') {
589009ea47eSEdward Tomasz Napierala 			port = "3260";
590009ea47eSEdward Tomasz Napierala 		} else if (arg[0] == ':') {
591009ea47eSEdward Tomasz Napierala 			port = arg + 1;
592009ea47eSEdward Tomasz Napierala 		} else {
593009ea47eSEdward Tomasz Napierala 			log_warnx("invalid listen address %s",
594009ea47eSEdward Tomasz Napierala 			    portal->p_listen);
595009ea47eSEdward Tomasz Napierala 			free(portal->p_listen);
596009ea47eSEdward Tomasz Napierala 			free(portal);
597009ea47eSEdward Tomasz Napierala 			return (1);
598009ea47eSEdward Tomasz Napierala 		}
599009ea47eSEdward Tomasz Napierala 	} else {
600009ea47eSEdward Tomasz Napierala 		/*
601009ea47eSEdward Tomasz Napierala 		 * Either IPv6 address without brackets - and without
602009ea47eSEdward Tomasz Napierala 		 * a port - or IPv4 address.  Just count the colons.
603009ea47eSEdward Tomasz Napierala 		 */
604009ea47eSEdward Tomasz Napierala 		for (ch = arg; *ch != '\0'; ch++) {
605009ea47eSEdward Tomasz Napierala 			if (*ch == ':')
606009ea47eSEdward Tomasz Napierala 				colons++;
607009ea47eSEdward Tomasz Napierala 		}
608009ea47eSEdward Tomasz Napierala 		if (colons > 1) {
609009ea47eSEdward Tomasz Napierala 			addr = arg;
610009ea47eSEdward Tomasz Napierala 			port = "3260";
611009ea47eSEdward Tomasz Napierala 		} else {
612009ea47eSEdward Tomasz Napierala 			addr = strsep(&arg, ":");
613009ea47eSEdward Tomasz Napierala 			if (arg == NULL)
614009ea47eSEdward Tomasz Napierala 				port = "3260";
615009ea47eSEdward Tomasz Napierala 			else
616009ea47eSEdward Tomasz Napierala 				port = arg;
617009ea47eSEdward Tomasz Napierala 		}
618009ea47eSEdward Tomasz Napierala 	}
619009ea47eSEdward Tomasz Napierala 
620009ea47eSEdward Tomasz Napierala 	memset(&hints, 0, sizeof(hints));
621009ea47eSEdward Tomasz Napierala 	hints.ai_family = PF_UNSPEC;
622009ea47eSEdward Tomasz Napierala 	hints.ai_socktype = SOCK_STREAM;
623009ea47eSEdward Tomasz Napierala 	hints.ai_flags = AI_PASSIVE;
624009ea47eSEdward Tomasz Napierala 
625009ea47eSEdward Tomasz Napierala 	error = getaddrinfo(addr, port, &hints, &portal->p_ai);
626009ea47eSEdward Tomasz Napierala 	if (error != 0) {
627009ea47eSEdward Tomasz Napierala 		log_warnx("getaddrinfo for %s failed: %s",
628009ea47eSEdward Tomasz Napierala 		    portal->p_listen, gai_strerror(error));
629009ea47eSEdward Tomasz Napierala 		free(portal->p_listen);
630009ea47eSEdward Tomasz Napierala 		free(portal);
631009ea47eSEdward Tomasz Napierala 		return (1);
632009ea47eSEdward Tomasz Napierala 	}
633009ea47eSEdward Tomasz Napierala 
634009ea47eSEdward Tomasz Napierala 	/*
635009ea47eSEdward Tomasz Napierala 	 * XXX: getaddrinfo(3) may return multiple addresses; we should turn
636009ea47eSEdward Tomasz Napierala 	 *	those into multiple portals.
637009ea47eSEdward Tomasz Napierala 	 */
638009ea47eSEdward Tomasz Napierala 
639009ea47eSEdward Tomasz Napierala 	return (0);
640009ea47eSEdward Tomasz Napierala }
641009ea47eSEdward Tomasz Napierala 
642009ea47eSEdward Tomasz Napierala static bool
643009ea47eSEdward Tomasz Napierala valid_hex(const char ch)
644009ea47eSEdward Tomasz Napierala {
645009ea47eSEdward Tomasz Napierala 	switch (ch) {
646009ea47eSEdward Tomasz Napierala 	case '0':
647009ea47eSEdward Tomasz Napierala 	case '1':
648009ea47eSEdward Tomasz Napierala 	case '2':
649009ea47eSEdward Tomasz Napierala 	case '3':
650009ea47eSEdward Tomasz Napierala 	case '4':
651009ea47eSEdward Tomasz Napierala 	case '5':
652009ea47eSEdward Tomasz Napierala 	case '6':
653009ea47eSEdward Tomasz Napierala 	case '7':
654009ea47eSEdward Tomasz Napierala 	case '8':
655009ea47eSEdward Tomasz Napierala 	case '9':
656009ea47eSEdward Tomasz Napierala 	case 'a':
657009ea47eSEdward Tomasz Napierala 	case 'A':
658009ea47eSEdward Tomasz Napierala 	case 'b':
659009ea47eSEdward Tomasz Napierala 	case 'B':
660009ea47eSEdward Tomasz Napierala 	case 'c':
661009ea47eSEdward Tomasz Napierala 	case 'C':
662009ea47eSEdward Tomasz Napierala 	case 'd':
663009ea47eSEdward Tomasz Napierala 	case 'D':
664009ea47eSEdward Tomasz Napierala 	case 'e':
665009ea47eSEdward Tomasz Napierala 	case 'E':
666009ea47eSEdward Tomasz Napierala 	case 'f':
667009ea47eSEdward Tomasz Napierala 	case 'F':
668009ea47eSEdward Tomasz Napierala 		return (true);
669009ea47eSEdward Tomasz Napierala 	default:
670009ea47eSEdward Tomasz Napierala 		return (false);
671009ea47eSEdward Tomasz Napierala 	}
672009ea47eSEdward Tomasz Napierala }
673009ea47eSEdward Tomasz Napierala 
674009ea47eSEdward Tomasz Napierala bool
675009ea47eSEdward Tomasz Napierala valid_iscsi_name(const char *name)
676009ea47eSEdward Tomasz Napierala {
677009ea47eSEdward Tomasz Napierala 	int i;
678009ea47eSEdward Tomasz Napierala 
679009ea47eSEdward Tomasz Napierala 	if (strlen(name) >= MAX_NAME_LEN) {
680009ea47eSEdward Tomasz Napierala 		log_warnx("overlong name for target \"%s\"; max length allowed "
681009ea47eSEdward Tomasz Napierala 		    "by iSCSI specification is %d characters",
682009ea47eSEdward Tomasz Napierala 		    name, MAX_NAME_LEN);
683009ea47eSEdward Tomasz Napierala 		return (false);
684009ea47eSEdward Tomasz Napierala 	}
685009ea47eSEdward Tomasz Napierala 
686009ea47eSEdward Tomasz Napierala 	/*
687009ea47eSEdward Tomasz Napierala 	 * In the cases below, we don't return an error, just in case the admin
688009ea47eSEdward Tomasz Napierala 	 * was right, and we're wrong.
689009ea47eSEdward Tomasz Napierala 	 */
690009ea47eSEdward Tomasz Napierala 	if (strncasecmp(name, "iqn.", strlen("iqn.")) == 0) {
691009ea47eSEdward Tomasz Napierala 		for (i = strlen("iqn."); name[i] != '\0'; i++) {
692009ea47eSEdward Tomasz Napierala 			/*
693009ea47eSEdward Tomasz Napierala 			 * XXX: We should verify UTF-8 normalisation, as defined
694009ea47eSEdward Tomasz Napierala 			 * 	by 3.2.6.2: iSCSI Name Encoding.
695009ea47eSEdward Tomasz Napierala 			 */
696009ea47eSEdward Tomasz Napierala 			if (isalnum(name[i]))
697009ea47eSEdward Tomasz Napierala 				continue;
698009ea47eSEdward Tomasz Napierala 			if (name[i] == '-' || name[i] == '.' || name[i] == ':')
699009ea47eSEdward Tomasz Napierala 				continue;
700009ea47eSEdward Tomasz Napierala 			log_warnx("invalid character \"%c\" in target name "
701009ea47eSEdward Tomasz Napierala 			    "\"%s\"; allowed characters are letters, digits, "
702009ea47eSEdward Tomasz Napierala 			    "'-', '.', and ':'", name[i], name);
703009ea47eSEdward Tomasz Napierala 			break;
704009ea47eSEdward Tomasz Napierala 		}
705009ea47eSEdward Tomasz Napierala 		/*
706009ea47eSEdward Tomasz Napierala 		 * XXX: Check more stuff: valid date and a valid reversed domain.
707009ea47eSEdward Tomasz Napierala 		 */
708009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "eui.", strlen("eui.")) == 0) {
709009ea47eSEdward Tomasz Napierala 		if (strlen(name) != strlen("eui.") + 16)
710009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"eui.\" "
711009ea47eSEdward Tomasz Napierala 			    "should be followed by exactly 16 hexadecimal "
712009ea47eSEdward Tomasz Napierala 			    "digits", name);
713009ea47eSEdward Tomasz Napierala 		for (i = strlen("eui."); name[i] != '\0'; i++) {
714009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
715009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
716009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
717009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
718009ea47eSEdward Tomasz Napierala 				break;
719009ea47eSEdward Tomasz Napierala 			}
720009ea47eSEdward Tomasz Napierala 		}
721009ea47eSEdward Tomasz Napierala 	} else if (strncasecmp(name, "naa.", strlen("naa.")) == 0) {
722009ea47eSEdward Tomasz Napierala 		if (strlen(name) > strlen("naa.") + 32)
723009ea47eSEdward Tomasz Napierala 			log_warnx("invalid target name \"%s\"; the \"naa.\" "
724009ea47eSEdward Tomasz Napierala 			    "should be followed by at most 32 hexadecimal "
725009ea47eSEdward Tomasz Napierala 			    "digits", name);
726009ea47eSEdward Tomasz Napierala 		for (i = strlen("naa."); name[i] != '\0'; i++) {
727009ea47eSEdward Tomasz Napierala 			if (!valid_hex(name[i])) {
728009ea47eSEdward Tomasz Napierala 				log_warnx("invalid character \"%c\" in target "
729009ea47eSEdward Tomasz Napierala 				    "name \"%s\"; allowed characters are 1-9 "
730009ea47eSEdward Tomasz Napierala 				    "and A-F", name[i], name);
731009ea47eSEdward Tomasz Napierala 				break;
732009ea47eSEdward Tomasz Napierala 			}
733009ea47eSEdward Tomasz Napierala 		}
734009ea47eSEdward Tomasz Napierala 	} else {
735009ea47eSEdward Tomasz Napierala 		log_warnx("invalid target name \"%s\"; should start with "
736009ea47eSEdward Tomasz Napierala 		    "either \".iqn\", \"eui.\", or \"naa.\"",
737009ea47eSEdward Tomasz Napierala 		    name);
738009ea47eSEdward Tomasz Napierala 	}
739009ea47eSEdward Tomasz Napierala 	return (true);
740009ea47eSEdward Tomasz Napierala }
741009ea47eSEdward Tomasz Napierala 
742009ea47eSEdward Tomasz Napierala struct target *
743f7ae5bf8SEdward Tomasz Napierala target_new(struct conf *conf, const char *name)
744009ea47eSEdward Tomasz Napierala {
745009ea47eSEdward Tomasz Napierala 	struct target *targ;
746009ea47eSEdward Tomasz Napierala 	int i, len;
747009ea47eSEdward Tomasz Napierala 
748f7ae5bf8SEdward Tomasz Napierala 	targ = target_find(conf, name);
749009ea47eSEdward Tomasz Napierala 	if (targ != NULL) {
750f7ae5bf8SEdward Tomasz Napierala 		log_warnx("duplicated target \"%s\"", name);
751009ea47eSEdward Tomasz Napierala 		return (NULL);
752009ea47eSEdward Tomasz Napierala 	}
753f7ae5bf8SEdward Tomasz Napierala 	if (valid_iscsi_name(name) == false) {
754f7ae5bf8SEdward Tomasz Napierala 		log_warnx("target name \"%s\" is invalid", name);
755009ea47eSEdward Tomasz Napierala 		return (NULL);
756009ea47eSEdward Tomasz Napierala 	}
757009ea47eSEdward Tomasz Napierala 	targ = calloc(1, sizeof(*targ));
758009ea47eSEdward Tomasz Napierala 	if (targ == NULL)
759009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
760f7ae5bf8SEdward Tomasz Napierala 	targ->t_name = checked_strdup(name);
761009ea47eSEdward Tomasz Napierala 
762009ea47eSEdward Tomasz Napierala 	/*
763009ea47eSEdward Tomasz Napierala 	 * RFC 3722 requires us to normalize the name to lowercase.
764009ea47eSEdward Tomasz Napierala 	 */
765f7ae5bf8SEdward Tomasz Napierala 	len = strlen(name);
766009ea47eSEdward Tomasz Napierala 	for (i = 0; i < len; i++)
767f7ae5bf8SEdward Tomasz Napierala 		targ->t_name[i] = tolower(targ->t_name[i]);
768009ea47eSEdward Tomasz Napierala 
769009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&targ->t_luns);
770009ea47eSEdward Tomasz Napierala 	targ->t_conf = conf;
771009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
772009ea47eSEdward Tomasz Napierala 
773009ea47eSEdward Tomasz Napierala 	return (targ);
774009ea47eSEdward Tomasz Napierala }
775009ea47eSEdward Tomasz Napierala 
776009ea47eSEdward Tomasz Napierala void
777009ea47eSEdward Tomasz Napierala target_delete(struct target *targ)
778009ea47eSEdward Tomasz Napierala {
779009ea47eSEdward Tomasz Napierala 	struct lun *lun, *tmp;
780009ea47eSEdward Tomasz Napierala 
781009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
782009ea47eSEdward Tomasz Napierala 
783009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(lun, &targ->t_luns, l_next, tmp)
784009ea47eSEdward Tomasz Napierala 		lun_delete(lun);
785f7ae5bf8SEdward Tomasz Napierala 	free(targ->t_name);
786009ea47eSEdward Tomasz Napierala 	free(targ);
787009ea47eSEdward Tomasz Napierala }
788009ea47eSEdward Tomasz Napierala 
789009ea47eSEdward Tomasz Napierala struct target *
790f7ae5bf8SEdward Tomasz Napierala target_find(struct conf *conf, const char *name)
791009ea47eSEdward Tomasz Napierala {
792009ea47eSEdward Tomasz Napierala 	struct target *targ;
793009ea47eSEdward Tomasz Napierala 
794009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
795f7ae5bf8SEdward Tomasz Napierala 		if (strcasecmp(targ->t_name, name) == 0)
796009ea47eSEdward Tomasz Napierala 			return (targ);
797009ea47eSEdward Tomasz Napierala 	}
798009ea47eSEdward Tomasz Napierala 
799009ea47eSEdward Tomasz Napierala 	return (NULL);
800009ea47eSEdward Tomasz Napierala }
801009ea47eSEdward Tomasz Napierala 
802009ea47eSEdward Tomasz Napierala struct lun *
803009ea47eSEdward Tomasz Napierala lun_new(struct target *targ, int lun_id)
804009ea47eSEdward Tomasz Napierala {
805009ea47eSEdward Tomasz Napierala 	struct lun *lun;
806009ea47eSEdward Tomasz Napierala 
807009ea47eSEdward Tomasz Napierala 	lun = lun_find(targ, lun_id);
808009ea47eSEdward Tomasz Napierala 	if (lun != NULL) {
809009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated lun %d for target \"%s\"",
810f7ae5bf8SEdward Tomasz Napierala 		    lun_id, targ->t_name);
811009ea47eSEdward Tomasz Napierala 		return (NULL);
812009ea47eSEdward Tomasz Napierala 	}
813009ea47eSEdward Tomasz Napierala 
814009ea47eSEdward Tomasz Napierala 	lun = calloc(1, sizeof(*lun));
815009ea47eSEdward Tomasz Napierala 	if (lun == NULL)
816009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
817009ea47eSEdward Tomasz Napierala 	lun->l_lun = lun_id;
818009ea47eSEdward Tomasz Napierala 	TAILQ_INIT(&lun->l_options);
819009ea47eSEdward Tomasz Napierala 	lun->l_target = targ;
820009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&targ->t_luns, lun, l_next);
821009ea47eSEdward Tomasz Napierala 
822009ea47eSEdward Tomasz Napierala 	return (lun);
823009ea47eSEdward Tomasz Napierala }
824009ea47eSEdward Tomasz Napierala 
825009ea47eSEdward Tomasz Napierala void
826009ea47eSEdward Tomasz Napierala lun_delete(struct lun *lun)
827009ea47eSEdward Tomasz Napierala {
828009ea47eSEdward Tomasz Napierala 	struct lun_option *lo, *tmp;
829009ea47eSEdward Tomasz Napierala 
830009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&lun->l_target->t_luns, lun, l_next);
831009ea47eSEdward Tomasz Napierala 
832009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(lo, &lun->l_options, lo_next, tmp)
833009ea47eSEdward Tomasz Napierala 		lun_option_delete(lo);
834009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
835009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
836009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
837009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
838009ea47eSEdward Tomasz Napierala 	free(lun);
839009ea47eSEdward Tomasz Napierala }
840009ea47eSEdward Tomasz Napierala 
841009ea47eSEdward Tomasz Napierala struct lun *
842009ea47eSEdward Tomasz Napierala lun_find(struct target *targ, int lun_id)
843009ea47eSEdward Tomasz Napierala {
844009ea47eSEdward Tomasz Napierala 	struct lun *lun;
845009ea47eSEdward Tomasz Napierala 
846009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
847009ea47eSEdward Tomasz Napierala 		if (lun->l_lun == lun_id)
848009ea47eSEdward Tomasz Napierala 			return (lun);
849009ea47eSEdward Tomasz Napierala 	}
850009ea47eSEdward Tomasz Napierala 
851009ea47eSEdward Tomasz Napierala 	return (NULL);
852009ea47eSEdward Tomasz Napierala }
853009ea47eSEdward Tomasz Napierala 
854009ea47eSEdward Tomasz Napierala void
855009ea47eSEdward Tomasz Napierala lun_set_backend(struct lun *lun, const char *value)
856009ea47eSEdward Tomasz Napierala {
857009ea47eSEdward Tomasz Napierala 	free(lun->l_backend);
858009ea47eSEdward Tomasz Napierala 	lun->l_backend = checked_strdup(value);
859009ea47eSEdward Tomasz Napierala }
860009ea47eSEdward Tomasz Napierala 
861009ea47eSEdward Tomasz Napierala void
862009ea47eSEdward Tomasz Napierala lun_set_blocksize(struct lun *lun, size_t value)
863009ea47eSEdward Tomasz Napierala {
864009ea47eSEdward Tomasz Napierala 
865009ea47eSEdward Tomasz Napierala 	lun->l_blocksize = value;
866009ea47eSEdward Tomasz Napierala }
867009ea47eSEdward Tomasz Napierala 
868009ea47eSEdward Tomasz Napierala void
869009ea47eSEdward Tomasz Napierala lun_set_device_id(struct lun *lun, const char *value)
870009ea47eSEdward Tomasz Napierala {
871009ea47eSEdward Tomasz Napierala 	free(lun->l_device_id);
872009ea47eSEdward Tomasz Napierala 	lun->l_device_id = checked_strdup(value);
873009ea47eSEdward Tomasz Napierala }
874009ea47eSEdward Tomasz Napierala 
875009ea47eSEdward Tomasz Napierala void
876009ea47eSEdward Tomasz Napierala lun_set_path(struct lun *lun, const char *value)
877009ea47eSEdward Tomasz Napierala {
878009ea47eSEdward Tomasz Napierala 	free(lun->l_path);
879009ea47eSEdward Tomasz Napierala 	lun->l_path = checked_strdup(value);
880009ea47eSEdward Tomasz Napierala }
881009ea47eSEdward Tomasz Napierala 
882009ea47eSEdward Tomasz Napierala void
883009ea47eSEdward Tomasz Napierala lun_set_serial(struct lun *lun, const char *value)
884009ea47eSEdward Tomasz Napierala {
885009ea47eSEdward Tomasz Napierala 	free(lun->l_serial);
886009ea47eSEdward Tomasz Napierala 	lun->l_serial = checked_strdup(value);
887009ea47eSEdward Tomasz Napierala }
888009ea47eSEdward Tomasz Napierala 
889009ea47eSEdward Tomasz Napierala void
890009ea47eSEdward Tomasz Napierala lun_set_size(struct lun *lun, size_t value)
891009ea47eSEdward Tomasz Napierala {
892009ea47eSEdward Tomasz Napierala 
893009ea47eSEdward Tomasz Napierala 	lun->l_size = value;
894009ea47eSEdward Tomasz Napierala }
895009ea47eSEdward Tomasz Napierala 
896009ea47eSEdward Tomasz Napierala void
897009ea47eSEdward Tomasz Napierala lun_set_ctl_lun(struct lun *lun, uint32_t value)
898009ea47eSEdward Tomasz Napierala {
899009ea47eSEdward Tomasz Napierala 
900009ea47eSEdward Tomasz Napierala 	lun->l_ctl_lun = value;
901009ea47eSEdward Tomasz Napierala }
902009ea47eSEdward Tomasz Napierala 
903009ea47eSEdward Tomasz Napierala struct lun_option *
904009ea47eSEdward Tomasz Napierala lun_option_new(struct lun *lun, const char *name, const char *value)
905009ea47eSEdward Tomasz Napierala {
906009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
907009ea47eSEdward Tomasz Napierala 
908009ea47eSEdward Tomasz Napierala 	lo = lun_option_find(lun, name);
909009ea47eSEdward Tomasz Napierala 	if (lo != NULL) {
910009ea47eSEdward Tomasz Napierala 		log_warnx("duplicated lun option %s for lun %d, target \"%s\"",
911f7ae5bf8SEdward Tomasz Napierala 		    name, lun->l_lun, lun->l_target->t_name);
912009ea47eSEdward Tomasz Napierala 		return (NULL);
913009ea47eSEdward Tomasz Napierala 	}
914009ea47eSEdward Tomasz Napierala 
915009ea47eSEdward Tomasz Napierala 	lo = calloc(1, sizeof(*lo));
916009ea47eSEdward Tomasz Napierala 	if (lo == NULL)
917009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
918009ea47eSEdward Tomasz Napierala 	lo->lo_name = checked_strdup(name);
919009ea47eSEdward Tomasz Napierala 	lo->lo_value = checked_strdup(value);
920009ea47eSEdward Tomasz Napierala 	lo->lo_lun = lun;
921009ea47eSEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&lun->l_options, lo, lo_next);
922009ea47eSEdward Tomasz Napierala 
923009ea47eSEdward Tomasz Napierala 	return (lo);
924009ea47eSEdward Tomasz Napierala }
925009ea47eSEdward Tomasz Napierala 
926009ea47eSEdward Tomasz Napierala void
927009ea47eSEdward Tomasz Napierala lun_option_delete(struct lun_option *lo)
928009ea47eSEdward Tomasz Napierala {
929009ea47eSEdward Tomasz Napierala 
930009ea47eSEdward Tomasz Napierala 	TAILQ_REMOVE(&lo->lo_lun->l_options, lo, lo_next);
931009ea47eSEdward Tomasz Napierala 
932009ea47eSEdward Tomasz Napierala 	free(lo->lo_name);
933009ea47eSEdward Tomasz Napierala 	free(lo->lo_value);
934009ea47eSEdward Tomasz Napierala 	free(lo);
935009ea47eSEdward Tomasz Napierala }
936009ea47eSEdward Tomasz Napierala 
937009ea47eSEdward Tomasz Napierala struct lun_option *
938009ea47eSEdward Tomasz Napierala lun_option_find(struct lun *lun, const char *name)
939009ea47eSEdward Tomasz Napierala {
940009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
941009ea47eSEdward Tomasz Napierala 
942009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(lo, &lun->l_options, lo_next) {
943009ea47eSEdward Tomasz Napierala 		if (strcmp(lo->lo_name, name) == 0)
944009ea47eSEdward Tomasz Napierala 			return (lo);
945009ea47eSEdward Tomasz Napierala 	}
946009ea47eSEdward Tomasz Napierala 
947009ea47eSEdward Tomasz Napierala 	return (NULL);
948009ea47eSEdward Tomasz Napierala }
949009ea47eSEdward Tomasz Napierala 
950009ea47eSEdward Tomasz Napierala void
951009ea47eSEdward Tomasz Napierala lun_option_set(struct lun_option *lo, const char *value)
952009ea47eSEdward Tomasz Napierala {
953009ea47eSEdward Tomasz Napierala 
954009ea47eSEdward Tomasz Napierala 	free(lo->lo_value);
955009ea47eSEdward Tomasz Napierala 	lo->lo_value = checked_strdup(value);
956009ea47eSEdward Tomasz Napierala }
957009ea47eSEdward Tomasz Napierala 
958009ea47eSEdward Tomasz Napierala static struct connection *
959009ea47eSEdward Tomasz Napierala connection_new(struct portal *portal, int fd, const char *host)
960009ea47eSEdward Tomasz Napierala {
961009ea47eSEdward Tomasz Napierala 	struct connection *conn;
962009ea47eSEdward Tomasz Napierala 
963009ea47eSEdward Tomasz Napierala 	conn = calloc(1, sizeof(*conn));
964009ea47eSEdward Tomasz Napierala 	if (conn == NULL)
965009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
966009ea47eSEdward Tomasz Napierala 	conn->conn_portal = portal;
967009ea47eSEdward Tomasz Napierala 	conn->conn_socket = fd;
968009ea47eSEdward Tomasz Napierala 	conn->conn_initiator_addr = checked_strdup(host);
969009ea47eSEdward Tomasz Napierala 
970009ea47eSEdward Tomasz Napierala 	/*
971009ea47eSEdward Tomasz Napierala 	 * Default values, from RFC 3720, section 12.
972009ea47eSEdward Tomasz Napierala 	 */
973009ea47eSEdward Tomasz Napierala 	conn->conn_max_data_segment_length = 8192;
974009ea47eSEdward Tomasz Napierala 	conn->conn_max_burst_length = 262144;
975009ea47eSEdward Tomasz Napierala 	conn->conn_immediate_data = true;
976009ea47eSEdward Tomasz Napierala 
977009ea47eSEdward Tomasz Napierala 	return (conn);
978009ea47eSEdward Tomasz Napierala }
979009ea47eSEdward Tomasz Napierala 
980009ea47eSEdward Tomasz Napierala #if 0
981009ea47eSEdward Tomasz Napierala static void
982009ea47eSEdward Tomasz Napierala conf_print(struct conf *conf)
983009ea47eSEdward Tomasz Napierala {
984009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
985009ea47eSEdward Tomasz Napierala 	struct auth *auth;
9868cb2e958SEdward Tomasz Napierala 	struct auth_name *auth_name;
9878cb2e958SEdward Tomasz Napierala 	struct auth_portal *auth_portal;
988009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
989009ea47eSEdward Tomasz Napierala 	struct portal *portal;
990009ea47eSEdward Tomasz Napierala 	struct target *targ;
991009ea47eSEdward Tomasz Napierala 	struct lun *lun;
992009ea47eSEdward Tomasz Napierala 	struct lun_option *lo;
993009ea47eSEdward Tomasz Napierala 
994009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
995009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "auth-group %s {\n", ag->ag_name);
996009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(auth, &ag->ag_auths, a_next)
997009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t chap-mutual %s %s %s %s\n",
998009ea47eSEdward Tomasz Napierala 			    auth->a_user, auth->a_secret,
999009ea47eSEdward Tomasz Napierala 			    auth->a_mutual_user, auth->a_mutual_secret);
10008cb2e958SEdward Tomasz Napierala 		TAILQ_FOREACH(auth_name, &ag->ag_names, an_next)
10018cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-name %s\n",
10028cb2e958SEdward Tomasz Napierala 			    auth_name->an_initator_name);
10038cb2e958SEdward Tomasz Napierala 		TAILQ_FOREACH(auth_portal, &ag->ag_portals, an_next)
10048cb2e958SEdward Tomasz Napierala 			fprintf(stderr, "\t initiator-portal %s\n",
10058cb2e958SEdward Tomasz Napierala 			    auth_portal->an_initator_portal);
1006009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1007009ea47eSEdward Tomasz Napierala 	}
1008009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1009009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "portal-group %s {\n", pg->pg_name);
1010009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1011009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t listen %s\n", portal->p_listen);
1012009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1013009ea47eSEdward Tomasz Napierala 	}
1014009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1015f7ae5bf8SEdward Tomasz Napierala 		fprintf(stderr, "target %s {\n", targ->t_name);
1016009ea47eSEdward Tomasz Napierala 		if (targ->t_alias != NULL)
1017009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t alias %s\n", targ->t_alias);
1018009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
1019009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\tlun %d {\n", lun->l_lun);
1020009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t\tpath %s\n", lun->l_path);
1021009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(lo, &lun->l_options, lo_next)
1022009ea47eSEdward Tomasz Napierala 				fprintf(stderr, "\t\toption %s %s\n",
1023009ea47eSEdward Tomasz Napierala 				    lo->lo_name, lo->lo_value);
1024009ea47eSEdward Tomasz Napierala 			fprintf(stderr, "\t}\n");
1025009ea47eSEdward Tomasz Napierala 		}
1026009ea47eSEdward Tomasz Napierala 		fprintf(stderr, "}\n");
1027009ea47eSEdward Tomasz Napierala 	}
1028009ea47eSEdward Tomasz Napierala }
1029009ea47eSEdward Tomasz Napierala #endif
1030009ea47eSEdward Tomasz Napierala 
10312fabfaa5SEdward Tomasz Napierala static int
10322fabfaa5SEdward Tomasz Napierala conf_verify_lun(struct lun *lun)
10332fabfaa5SEdward Tomasz Napierala {
10342fabfaa5SEdward Tomasz Napierala 	const struct lun *lun2;
1035a19eebb1SEdward Tomasz Napierala 	const struct target *targ2;
10362fabfaa5SEdward Tomasz Napierala 
10372fabfaa5SEdward Tomasz Napierala 	if (lun->l_backend == NULL)
10382fabfaa5SEdward Tomasz Napierala 		lun_set_backend(lun, "block");
10392fabfaa5SEdward Tomasz Napierala 	if (strcmp(lun->l_backend, "block") == 0) {
10402fabfaa5SEdward Tomasz Napierala 		if (lun->l_path == NULL) {
10412fabfaa5SEdward Tomasz Napierala 			log_warnx("missing path for lun %d, target \"%s\"",
1042f7ae5bf8SEdward Tomasz Napierala 			    lun->l_lun, lun->l_target->t_name);
10432fabfaa5SEdward Tomasz Napierala 			return (1);
10442fabfaa5SEdward Tomasz Napierala 		}
10452fabfaa5SEdward Tomasz Napierala 	} else if (strcmp(lun->l_backend, "ramdisk") == 0) {
10462fabfaa5SEdward Tomasz Napierala 		if (lun->l_size == 0) {
10472fabfaa5SEdward Tomasz Napierala 			log_warnx("missing size for ramdisk-backed lun %d, "
1048f7ae5bf8SEdward Tomasz Napierala 			    "target \"%s\"", lun->l_lun, lun->l_target->t_name);
10492fabfaa5SEdward Tomasz Napierala 			return (1);
10502fabfaa5SEdward Tomasz Napierala 		}
10512fabfaa5SEdward Tomasz Napierala 		if (lun->l_path != NULL) {
10522fabfaa5SEdward Tomasz Napierala 			log_warnx("path must not be specified "
10532fabfaa5SEdward Tomasz Napierala 			    "for ramdisk-backed lun %d, target \"%s\"",
1054f7ae5bf8SEdward Tomasz Napierala 			    lun->l_lun, lun->l_target->t_name);
10552fabfaa5SEdward Tomasz Napierala 			return (1);
10562fabfaa5SEdward Tomasz Napierala 		}
10572fabfaa5SEdward Tomasz Napierala 	}
10582fabfaa5SEdward Tomasz Napierala 	if (lun->l_lun < 0 || lun->l_lun > 255) {
10592fabfaa5SEdward Tomasz Napierala 		log_warnx("invalid lun number for lun %d, target \"%s\"; "
10602fabfaa5SEdward Tomasz Napierala 		    "must be between 0 and 255", lun->l_lun,
1061f7ae5bf8SEdward Tomasz Napierala 		    lun->l_target->t_name);
10622fabfaa5SEdward Tomasz Napierala 		return (1);
10632fabfaa5SEdward Tomasz Napierala 	}
10642fabfaa5SEdward Tomasz Napierala 	if (lun->l_blocksize == 0) {
10652fabfaa5SEdward Tomasz Napierala 		lun_set_blocksize(lun, DEFAULT_BLOCKSIZE);
10662fabfaa5SEdward Tomasz Napierala 	} else if (lun->l_blocksize < 0) {
10672fabfaa5SEdward Tomasz Napierala 		log_warnx("invalid blocksize for lun %d, target \"%s\"; "
1068f7ae5bf8SEdward Tomasz Napierala 		    "must be larger than 0", lun->l_lun, lun->l_target->t_name);
10692fabfaa5SEdward Tomasz Napierala 		return (1);
10702fabfaa5SEdward Tomasz Napierala 	}
10712fabfaa5SEdward Tomasz Napierala 	if (lun->l_size != 0 && lun->l_size % lun->l_blocksize != 0) {
10722fabfaa5SEdward Tomasz Napierala 		log_warnx("invalid size for lun %d, target \"%s\"; "
10732fabfaa5SEdward Tomasz Napierala 		    "must be multiple of blocksize", lun->l_lun,
1074f7ae5bf8SEdward Tomasz Napierala 		    lun->l_target->t_name);
10752fabfaa5SEdward Tomasz Napierala 		return (1);
10762fabfaa5SEdward Tomasz Napierala 	}
1077a19eebb1SEdward Tomasz Napierala 	TAILQ_FOREACH(targ2, &lun->l_target->t_conf->conf_targets, t_next) {
1078a19eebb1SEdward Tomasz Napierala 		TAILQ_FOREACH(lun2, &targ2->t_luns, l_next) {
1079a19eebb1SEdward Tomasz Napierala 			if (lun == lun2)
1080a19eebb1SEdward Tomasz Napierala 				continue;
1081a19eebb1SEdward Tomasz Napierala 			if (lun->l_path != NULL && lun2->l_path != NULL &&
1082a19eebb1SEdward Tomasz Napierala 			    strcmp(lun->l_path, lun2->l_path) == 0) {
1083a19eebb1SEdward Tomasz Napierala 				log_debugx("WARNING: path \"%s\" duplicated "
1084a19eebb1SEdward Tomasz Napierala 				    "between lun %d, target \"%s\", and "
1085a19eebb1SEdward Tomasz Napierala 				    "lun %d, target \"%s\"", lun->l_path,
1086f7ae5bf8SEdward Tomasz Napierala 				    lun->l_lun, lun->l_target->t_name,
1087f7ae5bf8SEdward Tomasz Napierala 				    lun2->l_lun, lun2->l_target->t_name);
1088a19eebb1SEdward Tomasz Napierala 			}
1089a19eebb1SEdward Tomasz Napierala 		}
1090a19eebb1SEdward Tomasz Napierala 	}
10912fabfaa5SEdward Tomasz Napierala 
10922fabfaa5SEdward Tomasz Napierala 	return (0);
10932fabfaa5SEdward Tomasz Napierala }
10942fabfaa5SEdward Tomasz Napierala 
1095009ea47eSEdward Tomasz Napierala int
1096009ea47eSEdward Tomasz Napierala conf_verify(struct conf *conf)
1097009ea47eSEdward Tomasz Napierala {
1098009ea47eSEdward Tomasz Napierala 	struct auth_group *ag;
1099009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
1100009ea47eSEdward Tomasz Napierala 	struct target *targ;
11012fabfaa5SEdward Tomasz Napierala 	struct lun *lun;
1102009ea47eSEdward Tomasz Napierala 	bool found_lun0;
11032fabfaa5SEdward Tomasz Napierala 	int error;
1104009ea47eSEdward Tomasz Napierala 
1105009ea47eSEdward Tomasz Napierala 	if (conf->conf_pidfile_path == NULL)
1106009ea47eSEdward Tomasz Napierala 		conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1107009ea47eSEdward Tomasz Napierala 
1108009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1109009ea47eSEdward Tomasz Napierala 		if (targ->t_auth_group == NULL) {
1110e7977549SEdward Tomasz Napierala 			targ->t_auth_group = auth_group_find(conf,
1111e7977549SEdward Tomasz Napierala 			    "default");
1112e7977549SEdward Tomasz Napierala 			assert(targ->t_auth_group != NULL);
1113009ea47eSEdward Tomasz Napierala 		}
1114009ea47eSEdward Tomasz Napierala 		if (targ->t_portal_group == NULL) {
1115009ea47eSEdward Tomasz Napierala 			targ->t_portal_group = portal_group_find(conf,
1116009ea47eSEdward Tomasz Napierala 			    "default");
1117009ea47eSEdward Tomasz Napierala 			assert(targ->t_portal_group != NULL);
1118009ea47eSEdward Tomasz Napierala 		}
1119009ea47eSEdward Tomasz Napierala 		found_lun0 = false;
1120009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(lun, &targ->t_luns, l_next) {
11212fabfaa5SEdward Tomasz Napierala 			error = conf_verify_lun(lun);
11222fabfaa5SEdward Tomasz Napierala 			if (error != 0)
11232fabfaa5SEdward Tomasz Napierala 				return (error);
1124009ea47eSEdward Tomasz Napierala 			if (lun->l_lun == 0)
1125009ea47eSEdward Tomasz Napierala 				found_lun0 = true;
1126009ea47eSEdward Tomasz Napierala 		}
1127009ea47eSEdward Tomasz Napierala 		if (!found_lun0) {
1128009ea47eSEdward Tomasz Napierala 			log_warnx("mandatory LUN 0 not configured "
1129f7ae5bf8SEdward Tomasz Napierala 			    "for target \"%s\"", targ->t_name);
1130009ea47eSEdward Tomasz Napierala 			return (1);
1131009ea47eSEdward Tomasz Napierala 		}
1132009ea47eSEdward Tomasz Napierala 	}
1133009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1134009ea47eSEdward Tomasz Napierala 		assert(pg->pg_name != NULL);
1135009ea47eSEdward Tomasz Napierala 		if (pg->pg_discovery_auth_group == NULL) {
1136009ea47eSEdward Tomasz Napierala 			pg->pg_discovery_auth_group =
1137ccb1f04dSEdward Tomasz Napierala 			    auth_group_find(conf, "default");
1138009ea47eSEdward Tomasz Napierala 			assert(pg->pg_discovery_auth_group != NULL);
1139009ea47eSEdward Tomasz Napierala 		}
1140009ea47eSEdward Tomasz Napierala 
1141009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1142009ea47eSEdward Tomasz Napierala 			if (targ->t_portal_group == pg)
1143009ea47eSEdward Tomasz Napierala 				break;
1144009ea47eSEdward Tomasz Napierala 		}
1145009ea47eSEdward Tomasz Napierala 		if (targ == NULL) {
1146009ea47eSEdward Tomasz Napierala 			if (strcmp(pg->pg_name, "default") != 0)
1147009ea47eSEdward Tomasz Napierala 				log_warnx("portal-group \"%s\" not assigned "
1148009ea47eSEdward Tomasz Napierala 				    "to any target", pg->pg_name);
1149009ea47eSEdward Tomasz Napierala 			pg->pg_unassigned = true;
1150009ea47eSEdward Tomasz Napierala 		} else
1151009ea47eSEdward Tomasz Napierala 			pg->pg_unassigned = false;
1152009ea47eSEdward Tomasz Napierala 	}
1153009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {
1154009ea47eSEdward Tomasz Napierala 		if (ag->ag_name == NULL)
1155009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target != NULL);
1156009ea47eSEdward Tomasz Napierala 		else
1157009ea47eSEdward Tomasz Napierala 			assert(ag->ag_target == NULL);
1158009ea47eSEdward Tomasz Napierala 
1159009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1160009ea47eSEdward Tomasz Napierala 			if (targ->t_auth_group == ag)
1161009ea47eSEdward Tomasz Napierala 				break;
1162009ea47eSEdward Tomasz Napierala 		}
1163009ea47eSEdward Tomasz Napierala 		if (targ == NULL && ag->ag_name != NULL &&
1164ccb1f04dSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "default") != 0 &&
1165009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-authentication") != 0 &&
1166009ea47eSEdward Tomasz Napierala 		    strcmp(ag->ag_name, "no-access") != 0) {
1167009ea47eSEdward Tomasz Napierala 			log_warnx("auth-group \"%s\" not assigned "
1168009ea47eSEdward Tomasz Napierala 			    "to any target", ag->ag_name);
1169009ea47eSEdward Tomasz Napierala 		}
1170009ea47eSEdward Tomasz Napierala 	}
1171009ea47eSEdward Tomasz Napierala 
1172009ea47eSEdward Tomasz Napierala 	return (0);
1173009ea47eSEdward Tomasz Napierala }
1174009ea47eSEdward Tomasz Napierala 
1175009ea47eSEdward Tomasz Napierala static int
1176009ea47eSEdward Tomasz Napierala conf_apply(struct conf *oldconf, struct conf *newconf)
1177009ea47eSEdward Tomasz Napierala {
1178009ea47eSEdward Tomasz Napierala 	struct target *oldtarg, *newtarg, *tmptarg;
1179009ea47eSEdward Tomasz Napierala 	struct lun *oldlun, *newlun, *tmplun;
1180009ea47eSEdward Tomasz Napierala 	struct portal_group *oldpg, *newpg;
1181009ea47eSEdward Tomasz Napierala 	struct portal *oldp, *newp;
1182009ea47eSEdward Tomasz Napierala 	pid_t otherpid;
1183009ea47eSEdward Tomasz Napierala 	int changed, cumulated_error = 0, error;
1184009ea47eSEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY
1185009ea47eSEdward Tomasz Napierala 	int one = 1;
1186009ea47eSEdward Tomasz Napierala #endif
1187009ea47eSEdward Tomasz Napierala 
1188009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_debug != newconf->conf_debug) {
1189009ea47eSEdward Tomasz Napierala 		log_debugx("changing debug level to %d", newconf->conf_debug);
1190009ea47eSEdward Tomasz Napierala 		log_init(newconf->conf_debug);
1191009ea47eSEdward Tomasz Napierala 	}
1192009ea47eSEdward Tomasz Napierala 
1193009ea47eSEdward Tomasz Napierala 	if (oldconf->conf_pidfh != NULL) {
1194009ea47eSEdward Tomasz Napierala 		assert(oldconf->conf_pidfile_path != NULL);
1195009ea47eSEdward Tomasz Napierala 		if (newconf->conf_pidfile_path != NULL &&
1196009ea47eSEdward Tomasz Napierala 		    strcmp(oldconf->conf_pidfile_path,
1197009ea47eSEdward Tomasz Napierala 		    newconf->conf_pidfile_path) == 0) {
1198009ea47eSEdward Tomasz Napierala 			newconf->conf_pidfh = oldconf->conf_pidfh;
1199009ea47eSEdward Tomasz Napierala 			oldconf->conf_pidfh = NULL;
1200009ea47eSEdward Tomasz Napierala 		} else {
1201009ea47eSEdward Tomasz Napierala 			log_debugx("removing pidfile %s",
1202009ea47eSEdward Tomasz Napierala 			    oldconf->conf_pidfile_path);
1203009ea47eSEdward Tomasz Napierala 			pidfile_remove(oldconf->conf_pidfh);
1204009ea47eSEdward Tomasz Napierala 			oldconf->conf_pidfh = NULL;
1205009ea47eSEdward Tomasz Napierala 		}
1206009ea47eSEdward Tomasz Napierala 	}
1207009ea47eSEdward Tomasz Napierala 
1208009ea47eSEdward Tomasz Napierala 	if (newconf->conf_pidfh == NULL && newconf->conf_pidfile_path != NULL) {
1209009ea47eSEdward Tomasz Napierala 		log_debugx("opening pidfile %s", newconf->conf_pidfile_path);
1210009ea47eSEdward Tomasz Napierala 		newconf->conf_pidfh =
1211009ea47eSEdward Tomasz Napierala 		    pidfile_open(newconf->conf_pidfile_path, 0600, &otherpid);
1212009ea47eSEdward Tomasz Napierala 		if (newconf->conf_pidfh == NULL) {
1213009ea47eSEdward Tomasz Napierala 			if (errno == EEXIST)
1214009ea47eSEdward Tomasz Napierala 				log_errx(1, "daemon already running, pid: %jd.",
1215009ea47eSEdward Tomasz Napierala 				    (intmax_t)otherpid);
1216009ea47eSEdward Tomasz Napierala 			log_err(1, "cannot open or create pidfile \"%s\"",
1217009ea47eSEdward Tomasz Napierala 			    newconf->conf_pidfile_path);
1218009ea47eSEdward Tomasz Napierala 		}
1219009ea47eSEdward Tomasz Napierala 	}
1220009ea47eSEdward Tomasz Napierala 
1221009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) {
1222009ea47eSEdward Tomasz Napierala 		/*
1223009ea47eSEdward Tomasz Napierala 		 * First, remove any targets present in the old configuration
1224009ea47eSEdward Tomasz Napierala 		 * and missing in the new one.
1225009ea47eSEdward Tomasz Napierala 		 */
1226f7ae5bf8SEdward Tomasz Napierala 		newtarg = target_find(newconf, oldtarg->t_name);
1227009ea47eSEdward Tomasz Napierala 		if (newtarg == NULL) {
1228009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next,
1229009ea47eSEdward Tomasz Napierala 			    tmplun) {
1230009ea47eSEdward Tomasz Napierala 				log_debugx("target %s not found in the "
1231009ea47eSEdward Tomasz Napierala 				    "configuration file; removing its lun %d, "
1232009ea47eSEdward Tomasz Napierala 				    "backed by CTL lun %d",
1233f7ae5bf8SEdward Tomasz Napierala 				    oldtarg->t_name, oldlun->l_lun,
1234009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1235009ea47eSEdward Tomasz Napierala 				error = kernel_lun_remove(oldlun);
1236009ea47eSEdward Tomasz Napierala 				if (error != 0) {
1237009ea47eSEdward Tomasz Napierala 					log_warnx("failed to remove lun %d, "
1238009ea47eSEdward Tomasz Napierala 					    "target %s, CTL lun %d",
1239f7ae5bf8SEdward Tomasz Napierala 					    oldlun->l_lun, oldtarg->t_name,
1240009ea47eSEdward Tomasz Napierala 					    oldlun->l_ctl_lun);
1241009ea47eSEdward Tomasz Napierala 					cumulated_error++;
1242009ea47eSEdward Tomasz Napierala 				}
1243009ea47eSEdward Tomasz Napierala 				lun_delete(oldlun);
1244009ea47eSEdward Tomasz Napierala 			}
1245009ea47eSEdward Tomasz Napierala 			target_delete(oldtarg);
1246009ea47eSEdward Tomasz Napierala 			continue;
1247009ea47eSEdward Tomasz Napierala 		}
1248009ea47eSEdward Tomasz Napierala 
1249009ea47eSEdward Tomasz Napierala 		/*
1250009ea47eSEdward Tomasz Napierala 		 * Second, remove any LUNs present in the old target
1251009ea47eSEdward Tomasz Napierala 		 * and missing in the new one.
1252009ea47eSEdward Tomasz Napierala 		 */
1253009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH_SAFE(oldlun, &oldtarg->t_luns, l_next, tmplun) {
1254009ea47eSEdward Tomasz Napierala 			newlun = lun_find(newtarg, oldlun->l_lun);
1255009ea47eSEdward Tomasz Napierala 			if (newlun == NULL) {
1256009ea47eSEdward Tomasz Napierala 				log_debugx("lun %d, target %s, CTL lun %d "
1257009ea47eSEdward Tomasz Napierala 				    "not found in the configuration file; "
1258f7ae5bf8SEdward Tomasz Napierala 				    "removing", oldlun->l_lun, oldtarg->t_name,
1259009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1260009ea47eSEdward Tomasz Napierala 				error = kernel_lun_remove(oldlun);
1261009ea47eSEdward Tomasz Napierala 				if (error != 0) {
1262009ea47eSEdward Tomasz Napierala 					log_warnx("failed to remove lun %d, "
1263009ea47eSEdward Tomasz Napierala 					    "target %s, CTL lun %d",
1264f7ae5bf8SEdward Tomasz Napierala 					    oldlun->l_lun, oldtarg->t_name,
1265009ea47eSEdward Tomasz Napierala 					    oldlun->l_ctl_lun);
1266009ea47eSEdward Tomasz Napierala 					cumulated_error++;
1267009ea47eSEdward Tomasz Napierala 				}
1268009ea47eSEdward Tomasz Napierala 				lun_delete(oldlun);
1269009ea47eSEdward Tomasz Napierala 				continue;
1270009ea47eSEdward Tomasz Napierala 			}
1271009ea47eSEdward Tomasz Napierala 
1272009ea47eSEdward Tomasz Napierala 			/*
1273009ea47eSEdward Tomasz Napierala 			 * Also remove the LUNs changed by more than size.
1274009ea47eSEdward Tomasz Napierala 			 */
1275009ea47eSEdward Tomasz Napierala 			changed = 0;
1276009ea47eSEdward Tomasz Napierala 			assert(oldlun->l_backend != NULL);
1277009ea47eSEdward Tomasz Napierala 			assert(newlun->l_backend != NULL);
1278009ea47eSEdward Tomasz Napierala 			if (strcmp(newlun->l_backend, oldlun->l_backend) != 0) {
1279009ea47eSEdward Tomasz Napierala 				log_debugx("backend for lun %d, target %s, "
1280009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1281f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1282009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1283009ea47eSEdward Tomasz Napierala 				changed = 1;
1284009ea47eSEdward Tomasz Napierala 			}
1285009ea47eSEdward Tomasz Napierala 			if (oldlun->l_blocksize != newlun->l_blocksize) {
1286009ea47eSEdward Tomasz Napierala 				log_debugx("blocksize for lun %d, target %s, "
1287009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1288f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1289009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1290009ea47eSEdward Tomasz Napierala 				changed = 1;
1291009ea47eSEdward Tomasz Napierala 			}
1292009ea47eSEdward Tomasz Napierala 			if (newlun->l_device_id != NULL &&
1293009ea47eSEdward Tomasz Napierala 			    (oldlun->l_device_id == NULL ||
1294009ea47eSEdward Tomasz Napierala 			     strcmp(oldlun->l_device_id, newlun->l_device_id) !=
1295009ea47eSEdward Tomasz Napierala 			     0)) {
1296009ea47eSEdward Tomasz Napierala 				log_debugx("device-id for lun %d, target %s, "
1297009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1298f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1299009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1300009ea47eSEdward Tomasz Napierala 				changed = 1;
1301009ea47eSEdward Tomasz Napierala 			}
1302009ea47eSEdward Tomasz Napierala 			if (newlun->l_path != NULL &&
1303009ea47eSEdward Tomasz Napierala 			    (oldlun->l_path == NULL ||
1304009ea47eSEdward Tomasz Napierala 			     strcmp(oldlun->l_path, newlun->l_path) != 0)) {
1305009ea47eSEdward Tomasz Napierala 				log_debugx("path for lun %d, target %s, "
1306009ea47eSEdward Tomasz Napierala 				    "CTL lun %d, changed; removing",
1307f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1308009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1309009ea47eSEdward Tomasz Napierala 				changed = 1;
1310009ea47eSEdward Tomasz Napierala 			}
1311009ea47eSEdward Tomasz Napierala 			if (newlun->l_serial != NULL &&
1312009ea47eSEdward Tomasz Napierala 			    (oldlun->l_serial == NULL ||
1313009ea47eSEdward Tomasz Napierala 			     strcmp(oldlun->l_serial, newlun->l_serial) != 0)) {
1314009ea47eSEdward Tomasz Napierala 				log_debugx("serial for lun %d, target %s, "
1315009ea47eSEdward Tomasz Napierala 				    "CTL lun %d changed; removing",
1316f7ae5bf8SEdward Tomasz Napierala 				    oldlun->l_lun, oldtarg->t_name,
1317009ea47eSEdward Tomasz Napierala 				    oldlun->l_ctl_lun);
1318009ea47eSEdward Tomasz Napierala 				changed = 1;
1319009ea47eSEdward Tomasz Napierala 			}
1320009ea47eSEdward Tomasz Napierala 			if (changed) {
1321009ea47eSEdward Tomasz Napierala 				error = kernel_lun_remove(oldlun);
1322009ea47eSEdward Tomasz Napierala 				if (error != 0) {
1323009ea47eSEdward Tomasz Napierala 					log_warnx("failed to remove lun %d, "
1324009ea47eSEdward Tomasz Napierala 					    "target %s, CTL lun %d",
1325f7ae5bf8SEdward Tomasz Napierala 					    oldlun->l_lun, oldtarg->t_name,
1326009ea47eSEdward Tomasz Napierala 					    oldlun->l_ctl_lun);
1327009ea47eSEdward Tomasz Napierala 					cumulated_error++;
1328009ea47eSEdward Tomasz Napierala 				}
1329009ea47eSEdward Tomasz Napierala 				lun_delete(oldlun);
1330009ea47eSEdward Tomasz Napierala 				continue;
1331009ea47eSEdward Tomasz Napierala 			}
1332009ea47eSEdward Tomasz Napierala 
1333009ea47eSEdward Tomasz Napierala 			lun_set_ctl_lun(newlun, oldlun->l_ctl_lun);
1334009ea47eSEdward Tomasz Napierala 		}
1335009ea47eSEdward Tomasz Napierala 	}
1336009ea47eSEdward Tomasz Napierala 
1337009ea47eSEdward Tomasz Napierala 	/*
1338009ea47eSEdward Tomasz Napierala 	 * Now add new targets or modify existing ones.
1339009ea47eSEdward Tomasz Napierala 	 */
1340009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) {
1341f7ae5bf8SEdward Tomasz Napierala 		oldtarg = target_find(oldconf, newtarg->t_name);
1342009ea47eSEdward Tomasz Napierala 
1343009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(newlun, &newtarg->t_luns, l_next) {
1344009ea47eSEdward Tomasz Napierala 			if (oldtarg != NULL) {
1345009ea47eSEdward Tomasz Napierala 				oldlun = lun_find(oldtarg, newlun->l_lun);
1346009ea47eSEdward Tomasz Napierala 				if (oldlun != NULL) {
1347009ea47eSEdward Tomasz Napierala 					if (newlun->l_size != oldlun->l_size) {
1348009ea47eSEdward Tomasz Napierala 						log_debugx("resizing lun %d, "
1349009ea47eSEdward Tomasz Napierala 						    "target %s, CTL lun %d",
1350009ea47eSEdward Tomasz Napierala 						    newlun->l_lun,
1351f7ae5bf8SEdward Tomasz Napierala 						    newtarg->t_name,
1352009ea47eSEdward Tomasz Napierala 						    newlun->l_ctl_lun);
1353009ea47eSEdward Tomasz Napierala 						error =
1354009ea47eSEdward Tomasz Napierala 						    kernel_lun_resize(newlun);
1355009ea47eSEdward Tomasz Napierala 						if (error != 0) {
1356009ea47eSEdward Tomasz Napierala 							log_warnx("failed to "
1357009ea47eSEdward Tomasz Napierala 							    "resize lun %d, "
1358009ea47eSEdward Tomasz Napierala 							    "target %s, "
1359009ea47eSEdward Tomasz Napierala 							    "CTL lun %d",
1360009ea47eSEdward Tomasz Napierala 							    newlun->l_lun,
1361f7ae5bf8SEdward Tomasz Napierala 							    newtarg->t_name,
1362009ea47eSEdward Tomasz Napierala 							    newlun->l_lun);
1363009ea47eSEdward Tomasz Napierala 							cumulated_error++;
1364009ea47eSEdward Tomasz Napierala 						}
1365009ea47eSEdward Tomasz Napierala 					}
1366009ea47eSEdward Tomasz Napierala 					continue;
1367009ea47eSEdward Tomasz Napierala 				}
1368009ea47eSEdward Tomasz Napierala 			}
1369009ea47eSEdward Tomasz Napierala 			log_debugx("adding lun %d, target %s",
1370f7ae5bf8SEdward Tomasz Napierala 			    newlun->l_lun, newtarg->t_name);
1371009ea47eSEdward Tomasz Napierala 			error = kernel_lun_add(newlun);
1372009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1373009ea47eSEdward Tomasz Napierala 				log_warnx("failed to add lun %d, target %s",
1374f7ae5bf8SEdward Tomasz Napierala 				    newlun->l_lun, newtarg->t_name);
1375009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1376009ea47eSEdward Tomasz Napierala 			}
1377009ea47eSEdward Tomasz Napierala 		}
1378009ea47eSEdward Tomasz Napierala 	}
1379009ea47eSEdward Tomasz Napierala 
1380009ea47eSEdward Tomasz Napierala 	/*
1381009ea47eSEdward Tomasz Napierala 	 * Go through the new portals, opening the sockets as neccessary.
1382009ea47eSEdward Tomasz Napierala 	 */
1383009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(newpg, &newconf->conf_portal_groups, pg_next) {
1384009ea47eSEdward Tomasz Napierala 		if (newpg->pg_unassigned) {
1385009ea47eSEdward Tomasz Napierala 			log_debugx("not listening on portal-group \"%s\", "
1386009ea47eSEdward Tomasz Napierala 			    "not assigned to any target",
1387009ea47eSEdward Tomasz Napierala 			    newpg->pg_name);
1388009ea47eSEdward Tomasz Napierala 			continue;
1389009ea47eSEdward Tomasz Napierala 		}
1390009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(newp, &newpg->pg_portals, p_next) {
1391009ea47eSEdward Tomasz Napierala 			/*
1392009ea47eSEdward Tomasz Napierala 			 * Try to find already open portal and reuse
1393009ea47eSEdward Tomasz Napierala 			 * the listening socket.  We don't care about
1394009ea47eSEdward Tomasz Napierala 			 * what portal or portal group that was, what
1395009ea47eSEdward Tomasz Napierala 			 * matters is the listening address.
1396009ea47eSEdward Tomasz Napierala 			 */
1397009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups,
1398009ea47eSEdward Tomasz Napierala 			    pg_next) {
1399009ea47eSEdward Tomasz Napierala 				TAILQ_FOREACH(oldp, &oldpg->pg_portals,
1400009ea47eSEdward Tomasz Napierala 				    p_next) {
1401009ea47eSEdward Tomasz Napierala 					if (strcmp(newp->p_listen,
1402009ea47eSEdward Tomasz Napierala 					    oldp->p_listen) == 0 &&
1403009ea47eSEdward Tomasz Napierala 					    oldp->p_socket > 0) {
1404009ea47eSEdward Tomasz Napierala 						newp->p_socket =
1405009ea47eSEdward Tomasz Napierala 						    oldp->p_socket;
1406009ea47eSEdward Tomasz Napierala 						oldp->p_socket = 0;
1407009ea47eSEdward Tomasz Napierala 						break;
1408009ea47eSEdward Tomasz Napierala 					}
1409009ea47eSEdward Tomasz Napierala 				}
1410009ea47eSEdward Tomasz Napierala 			}
1411009ea47eSEdward Tomasz Napierala 			if (newp->p_socket > 0) {
1412009ea47eSEdward Tomasz Napierala 				/*
1413009ea47eSEdward Tomasz Napierala 				 * We're done with this portal.
1414009ea47eSEdward Tomasz Napierala 				 */
1415009ea47eSEdward Tomasz Napierala 				continue;
1416009ea47eSEdward Tomasz Napierala 			}
1417009ea47eSEdward Tomasz Napierala 
1418009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1419009ea47eSEdward Tomasz Napierala 			log_debugx("listening on %s, portal-group \"%s\" using ICL proxy",
1420009ea47eSEdward Tomasz Napierala 			    newp->p_listen, newpg->pg_name);
1421009ea47eSEdward Tomasz Napierala 			kernel_listen(newp->p_ai, newp->p_iser);
1422009ea47eSEdward Tomasz Napierala #else
1423009ea47eSEdward Tomasz Napierala 			assert(newp->p_iser == false);
1424009ea47eSEdward Tomasz Napierala 
1425009ea47eSEdward Tomasz Napierala 			log_debugx("listening on %s, portal-group \"%s\"",
1426009ea47eSEdward Tomasz Napierala 			    newp->p_listen, newpg->pg_name);
1427009ea47eSEdward Tomasz Napierala 			newp->p_socket = socket(newp->p_ai->ai_family,
1428009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_socktype,
1429009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_protocol);
1430009ea47eSEdward Tomasz Napierala 			if (newp->p_socket < 0) {
1431009ea47eSEdward Tomasz Napierala 				log_warn("socket(2) failed for %s",
1432009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1433009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1434009ea47eSEdward Tomasz Napierala 				continue;
1435009ea47eSEdward Tomasz Napierala 			}
1436009ea47eSEdward Tomasz Napierala 			error = setsockopt(newp->p_socket, SOL_SOCKET,
1437009ea47eSEdward Tomasz Napierala 			    SO_REUSEADDR, &one, sizeof(one));
1438009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1439009ea47eSEdward Tomasz Napierala 				log_warn("setsockopt(SO_REUSEADDR) failed "
1440009ea47eSEdward Tomasz Napierala 				    "for %s", newp->p_listen);
1441009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1442009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1443009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1444009ea47eSEdward Tomasz Napierala 				continue;
1445009ea47eSEdward Tomasz Napierala 			}
1446009ea47eSEdward Tomasz Napierala 			error = bind(newp->p_socket, newp->p_ai->ai_addr,
1447009ea47eSEdward Tomasz Napierala 			    newp->p_ai->ai_addrlen);
1448009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1449009ea47eSEdward Tomasz Napierala 				log_warn("bind(2) failed for %s",
1450009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1451009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1452009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1453009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1454009ea47eSEdward Tomasz Napierala 				continue;
1455009ea47eSEdward Tomasz Napierala 			}
1456009ea47eSEdward Tomasz Napierala 			error = listen(newp->p_socket, -1);
1457009ea47eSEdward Tomasz Napierala 			if (error != 0) {
1458009ea47eSEdward Tomasz Napierala 				log_warn("listen(2) failed for %s",
1459009ea47eSEdward Tomasz Napierala 				    newp->p_listen);
1460009ea47eSEdward Tomasz Napierala 				close(newp->p_socket);
1461009ea47eSEdward Tomasz Napierala 				newp->p_socket = 0;
1462009ea47eSEdward Tomasz Napierala 				cumulated_error++;
1463009ea47eSEdward Tomasz Napierala 				continue;
1464009ea47eSEdward Tomasz Napierala 			}
1465009ea47eSEdward Tomasz Napierala #endif /* !ICL_KERNEL_PROXY */
1466009ea47eSEdward Tomasz Napierala 		}
1467009ea47eSEdward Tomasz Napierala 	}
1468009ea47eSEdward Tomasz Napierala 
1469009ea47eSEdward Tomasz Napierala 	/*
1470009ea47eSEdward Tomasz Napierala 	 * Go through the no longer used sockets, closing them.
1471009ea47eSEdward Tomasz Napierala 	 */
1472009ea47eSEdward Tomasz Napierala 	TAILQ_FOREACH(oldpg, &oldconf->conf_portal_groups, pg_next) {
1473009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(oldp, &oldpg->pg_portals, p_next) {
1474009ea47eSEdward Tomasz Napierala 			if (oldp->p_socket <= 0)
1475009ea47eSEdward Tomasz Napierala 				continue;
1476009ea47eSEdward Tomasz Napierala 			log_debugx("closing socket for %s, portal-group \"%s\"",
1477009ea47eSEdward Tomasz Napierala 			    oldp->p_listen, oldpg->pg_name);
1478009ea47eSEdward Tomasz Napierala 			close(oldp->p_socket);
1479009ea47eSEdward Tomasz Napierala 			oldp->p_socket = 0;
1480009ea47eSEdward Tomasz Napierala 		}
1481009ea47eSEdward Tomasz Napierala 	}
1482009ea47eSEdward Tomasz Napierala 
1483009ea47eSEdward Tomasz Napierala 	return (cumulated_error);
1484009ea47eSEdward Tomasz Napierala }
1485009ea47eSEdward Tomasz Napierala 
1486009ea47eSEdward Tomasz Napierala bool
1487009ea47eSEdward Tomasz Napierala timed_out(void)
1488009ea47eSEdward Tomasz Napierala {
1489009ea47eSEdward Tomasz Napierala 
1490009ea47eSEdward Tomasz Napierala 	return (sigalrm_received);
1491009ea47eSEdward Tomasz Napierala }
1492009ea47eSEdward Tomasz Napierala 
1493009ea47eSEdward Tomasz Napierala static void
1494009ea47eSEdward Tomasz Napierala sigalrm_handler(int dummy __unused)
1495009ea47eSEdward Tomasz Napierala {
1496009ea47eSEdward Tomasz Napierala 	/*
1497009ea47eSEdward Tomasz Napierala 	 * It would be easiest to just log an error and exit.  We can't
1498009ea47eSEdward Tomasz Napierala 	 * do this, though, because log_errx() is not signal safe, since
1499009ea47eSEdward Tomasz Napierala 	 * it calls syslog(3).  Instead, set a flag checked by pdu_send()
1500009ea47eSEdward Tomasz Napierala 	 * and pdu_receive(), to call log_errx() there.  Should they fail
1501009ea47eSEdward Tomasz Napierala 	 * to notice, we'll exit here one second later.
1502009ea47eSEdward Tomasz Napierala 	 */
1503009ea47eSEdward Tomasz Napierala 	if (sigalrm_received) {
1504009ea47eSEdward Tomasz Napierala 		/*
1505009ea47eSEdward Tomasz Napierala 		 * Oh well.  Just give up and quit.
1506009ea47eSEdward Tomasz Napierala 		 */
1507009ea47eSEdward Tomasz Napierala 		_exit(2);
1508009ea47eSEdward Tomasz Napierala 	}
1509009ea47eSEdward Tomasz Napierala 
1510009ea47eSEdward Tomasz Napierala 	sigalrm_received = true;
1511009ea47eSEdward Tomasz Napierala }
1512009ea47eSEdward Tomasz Napierala 
1513009ea47eSEdward Tomasz Napierala static void
1514009ea47eSEdward Tomasz Napierala set_timeout(const struct conf *conf)
1515009ea47eSEdward Tomasz Napierala {
1516009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
1517009ea47eSEdward Tomasz Napierala 	struct itimerval itv;
1518009ea47eSEdward Tomasz Napierala 	int error;
1519009ea47eSEdward Tomasz Napierala 
1520009ea47eSEdward Tomasz Napierala 	if (conf->conf_timeout <= 0) {
1521009ea47eSEdward Tomasz Napierala 		log_debugx("session timeout disabled");
1522009ea47eSEdward Tomasz Napierala 		return;
1523009ea47eSEdward Tomasz Napierala 	}
1524009ea47eSEdward Tomasz Napierala 
1525009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
1526009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigalrm_handler;
1527009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
1528009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGALRM, &sa, NULL);
1529009ea47eSEdward Tomasz Napierala 	if (error != 0)
1530009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
1531009ea47eSEdward Tomasz Napierala 
1532009ea47eSEdward Tomasz Napierala 	/*
1533009ea47eSEdward Tomasz Napierala 	 * First SIGALRM will arive after conf_timeout seconds.
1534009ea47eSEdward Tomasz Napierala 	 * If we do nothing, another one will arrive a second later.
1535009ea47eSEdward Tomasz Napierala 	 */
1536009ea47eSEdward Tomasz Napierala 	bzero(&itv, sizeof(itv));
1537009ea47eSEdward Tomasz Napierala 	itv.it_interval.tv_sec = 1;
1538009ea47eSEdward Tomasz Napierala 	itv.it_value.tv_sec = conf->conf_timeout;
1539009ea47eSEdward Tomasz Napierala 
1540009ea47eSEdward Tomasz Napierala 	log_debugx("setting session timeout to %d seconds",
1541009ea47eSEdward Tomasz Napierala 	    conf->conf_timeout);
1542009ea47eSEdward Tomasz Napierala 	error = setitimer(ITIMER_REAL, &itv, NULL);
1543009ea47eSEdward Tomasz Napierala 	if (error != 0)
1544009ea47eSEdward Tomasz Napierala 		log_err(1, "setitimer");
1545009ea47eSEdward Tomasz Napierala }
1546009ea47eSEdward Tomasz Napierala 
1547009ea47eSEdward Tomasz Napierala static int
1548009ea47eSEdward Tomasz Napierala wait_for_children(bool block)
1549009ea47eSEdward Tomasz Napierala {
1550009ea47eSEdward Tomasz Napierala 	pid_t pid;
1551009ea47eSEdward Tomasz Napierala 	int status;
1552009ea47eSEdward Tomasz Napierala 	int num = 0;
1553009ea47eSEdward Tomasz Napierala 
1554009ea47eSEdward Tomasz Napierala 	for (;;) {
1555009ea47eSEdward Tomasz Napierala 		/*
1556009ea47eSEdward Tomasz Napierala 		 * If "block" is true, wait for at least one process.
1557009ea47eSEdward Tomasz Napierala 		 */
1558009ea47eSEdward Tomasz Napierala 		if (block && num == 0)
1559009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, 0, NULL);
1560009ea47eSEdward Tomasz Napierala 		else
1561009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, WNOHANG, NULL);
1562009ea47eSEdward Tomasz Napierala 		if (pid <= 0)
1563009ea47eSEdward Tomasz Napierala 			break;
1564009ea47eSEdward Tomasz Napierala 		if (WIFSIGNALED(status)) {
1565009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with signal %d",
1566009ea47eSEdward Tomasz Napierala 			    pid, WTERMSIG(status));
1567009ea47eSEdward Tomasz Napierala 		} else if (WEXITSTATUS(status) != 0) {
1568009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with exit status %d",
1569009ea47eSEdward Tomasz Napierala 			    pid, WEXITSTATUS(status));
1570009ea47eSEdward Tomasz Napierala 		} else {
1571009ea47eSEdward Tomasz Napierala 			log_debugx("child process %d terminated gracefully", pid);
1572009ea47eSEdward Tomasz Napierala 		}
1573009ea47eSEdward Tomasz Napierala 		num++;
1574009ea47eSEdward Tomasz Napierala 	}
1575009ea47eSEdward Tomasz Napierala 
1576009ea47eSEdward Tomasz Napierala 	return (num);
1577009ea47eSEdward Tomasz Napierala }
1578009ea47eSEdward Tomasz Napierala 
1579009ea47eSEdward Tomasz Napierala static void
1580009ea47eSEdward Tomasz Napierala handle_connection(struct portal *portal, int fd, bool dont_fork)
1581009ea47eSEdward Tomasz Napierala {
1582009ea47eSEdward Tomasz Napierala 	struct connection *conn;
1583009ea47eSEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY
1584009ea47eSEdward Tomasz Napierala 	struct sockaddr_storage ss;
1585009ea47eSEdward Tomasz Napierala 	socklen_t sslen = sizeof(ss);
1586009ea47eSEdward Tomasz Napierala 	int error;
1587009ea47eSEdward Tomasz Napierala #endif
1588009ea47eSEdward Tomasz Napierala 	pid_t pid;
1589009ea47eSEdward Tomasz Napierala 	char host[NI_MAXHOST + 1];
1590009ea47eSEdward Tomasz Napierala 	struct conf *conf;
1591009ea47eSEdward Tomasz Napierala 
1592009ea47eSEdward Tomasz Napierala 	conf = portal->p_portal_group->pg_conf;
1593009ea47eSEdward Tomasz Napierala 
1594009ea47eSEdward Tomasz Napierala 	if (dont_fork) {
1595009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; not forking due to -d flag");
1596009ea47eSEdward Tomasz Napierala 	} else {
1597009ea47eSEdward Tomasz Napierala 		nchildren -= wait_for_children(false);
1598009ea47eSEdward Tomasz Napierala 		assert(nchildren >= 0);
1599009ea47eSEdward Tomasz Napierala 
1600009ea47eSEdward Tomasz Napierala 		while (conf->conf_maxproc > 0 && nchildren >= conf->conf_maxproc) {
1601009ea47eSEdward Tomasz Napierala 			log_debugx("maxproc limit of %d child processes hit; "
1602009ea47eSEdward Tomasz Napierala 			    "waiting for child process to exit", conf->conf_maxproc);
1603009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(true);
1604009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
1605009ea47eSEdward Tomasz Napierala 		}
1606009ea47eSEdward Tomasz Napierala 		log_debugx("incoming connection; forking child process #%d",
1607009ea47eSEdward Tomasz Napierala 		    nchildren);
1608009ea47eSEdward Tomasz Napierala 		nchildren++;
1609009ea47eSEdward Tomasz Napierala 		pid = fork();
1610009ea47eSEdward Tomasz Napierala 		if (pid < 0)
1611009ea47eSEdward Tomasz Napierala 			log_err(1, "fork");
1612009ea47eSEdward Tomasz Napierala 		if (pid > 0) {
1613009ea47eSEdward Tomasz Napierala 			close(fd);
1614009ea47eSEdward Tomasz Napierala 			return;
1615009ea47eSEdward Tomasz Napierala 		}
1616009ea47eSEdward Tomasz Napierala 	}
1617009ea47eSEdward Tomasz Napierala 	pidfile_close(conf->conf_pidfh);
1618009ea47eSEdward Tomasz Napierala 
1619009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1620009ea47eSEdward Tomasz Napierala 	/*
1621009ea47eSEdward Tomasz Napierala 	 * XXX
1622009ea47eSEdward Tomasz Napierala 	 */
1623009ea47eSEdward Tomasz Napierala 	log_set_peer_addr("XXX");
1624009ea47eSEdward Tomasz Napierala #else
1625009ea47eSEdward Tomasz Napierala 	error = getpeername(fd, (struct sockaddr *)&ss, &sslen);
1626009ea47eSEdward Tomasz Napierala 	if (error != 0)
1627009ea47eSEdward Tomasz Napierala 		log_err(1, "getpeername");
1628009ea47eSEdward Tomasz Napierala 	error = getnameinfo((struct sockaddr *)&ss, sslen,
1629009ea47eSEdward Tomasz Napierala 	    host, sizeof(host), NULL, 0, NI_NUMERICHOST);
1630009ea47eSEdward Tomasz Napierala 	if (error != 0)
1631009ea47eSEdward Tomasz Napierala 		log_errx(1, "getaddrinfo: %s", gai_strerror(error));
1632009ea47eSEdward Tomasz Napierala 
1633009ea47eSEdward Tomasz Napierala 	log_debugx("accepted connection from %s; portal group \"%s\"",
1634009ea47eSEdward Tomasz Napierala 	    host, portal->p_portal_group->pg_name);
1635009ea47eSEdward Tomasz Napierala 	log_set_peer_addr(host);
1636009ea47eSEdward Tomasz Napierala 	setproctitle("%s", host);
1637009ea47eSEdward Tomasz Napierala #endif
1638009ea47eSEdward Tomasz Napierala 
1639009ea47eSEdward Tomasz Napierala 	conn = connection_new(portal, fd, host);
1640009ea47eSEdward Tomasz Napierala 	set_timeout(conf);
1641009ea47eSEdward Tomasz Napierala 	kernel_capsicate();
1642009ea47eSEdward Tomasz Napierala 	login(conn);
1643009ea47eSEdward Tomasz Napierala 	if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
1644009ea47eSEdward Tomasz Napierala 		kernel_handoff(conn);
1645009ea47eSEdward Tomasz Napierala 		log_debugx("connection handed off to the kernel");
1646009ea47eSEdward Tomasz Napierala 	} else {
1647009ea47eSEdward Tomasz Napierala 		assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
1648009ea47eSEdward Tomasz Napierala 		discovery(conn);
1649009ea47eSEdward Tomasz Napierala 	}
1650009ea47eSEdward Tomasz Napierala 	log_debugx("nothing more to do; exiting");
1651009ea47eSEdward Tomasz Napierala 	exit(0);
1652009ea47eSEdward Tomasz Napierala }
1653009ea47eSEdward Tomasz Napierala 
1654009ea47eSEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY
1655009ea47eSEdward Tomasz Napierala static int
1656009ea47eSEdward Tomasz Napierala fd_add(int fd, fd_set *fdset, int nfds)
1657009ea47eSEdward Tomasz Napierala {
1658009ea47eSEdward Tomasz Napierala 
1659009ea47eSEdward Tomasz Napierala 	/*
1660009ea47eSEdward Tomasz Napierala 	 * Skip sockets which we failed to bind.
1661009ea47eSEdward Tomasz Napierala 	 */
1662009ea47eSEdward Tomasz Napierala 	if (fd <= 0)
1663009ea47eSEdward Tomasz Napierala 		return (nfds);
1664009ea47eSEdward Tomasz Napierala 
1665009ea47eSEdward Tomasz Napierala 	FD_SET(fd, fdset);
1666009ea47eSEdward Tomasz Napierala 	if (fd > nfds)
1667009ea47eSEdward Tomasz Napierala 		nfds = fd;
1668009ea47eSEdward Tomasz Napierala 	return (nfds);
1669009ea47eSEdward Tomasz Napierala }
1670009ea47eSEdward Tomasz Napierala #endif
1671009ea47eSEdward Tomasz Napierala 
1672009ea47eSEdward Tomasz Napierala static void
1673009ea47eSEdward Tomasz Napierala main_loop(struct conf *conf, bool dont_fork)
1674009ea47eSEdward Tomasz Napierala {
1675009ea47eSEdward Tomasz Napierala 	struct portal_group *pg;
1676009ea47eSEdward Tomasz Napierala 	struct portal *portal;
1677009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1678009ea47eSEdward Tomasz Napierala 	int connection_id;
1679009ea47eSEdward Tomasz Napierala #else
1680009ea47eSEdward Tomasz Napierala 	fd_set fdset;
1681009ea47eSEdward Tomasz Napierala 	int error, nfds, client_fd;
1682009ea47eSEdward Tomasz Napierala #endif
1683009ea47eSEdward Tomasz Napierala 
1684009ea47eSEdward Tomasz Napierala 	pidfile_write(conf->conf_pidfh);
1685009ea47eSEdward Tomasz Napierala 
1686009ea47eSEdward Tomasz Napierala 	for (;;) {
1687009ea47eSEdward Tomasz Napierala 		if (sighup_received || sigterm_received)
1688009ea47eSEdward Tomasz Napierala 			return;
1689009ea47eSEdward Tomasz Napierala 
1690009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1691009ea47eSEdward Tomasz Napierala 		connection_id = kernel_accept();
1692009ea47eSEdward Tomasz Napierala 		if (connection_id == 0)
1693009ea47eSEdward Tomasz Napierala 			continue;
1694009ea47eSEdward Tomasz Napierala 
1695009ea47eSEdward Tomasz Napierala 		/*
1696009ea47eSEdward Tomasz Napierala 		 * XXX: This is obviously temporary.
1697009ea47eSEdward Tomasz Napierala 		 */
1698009ea47eSEdward Tomasz Napierala 		pg = TAILQ_FIRST(&conf->conf_portal_groups);
1699009ea47eSEdward Tomasz Napierala 		portal = TAILQ_FIRST(&pg->pg_portals);
1700009ea47eSEdward Tomasz Napierala 
1701009ea47eSEdward Tomasz Napierala 		handle_connection(portal, connection_id, dont_fork);
1702009ea47eSEdward Tomasz Napierala #else
1703009ea47eSEdward Tomasz Napierala 		FD_ZERO(&fdset);
1704009ea47eSEdward Tomasz Napierala 		nfds = 0;
1705009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1706009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(portal, &pg->pg_portals, p_next)
1707009ea47eSEdward Tomasz Napierala 				nfds = fd_add(portal->p_socket, &fdset, nfds);
1708009ea47eSEdward Tomasz Napierala 		}
1709009ea47eSEdward Tomasz Napierala 		error = select(nfds + 1, &fdset, NULL, NULL, NULL);
1710009ea47eSEdward Tomasz Napierala 		if (error <= 0) {
1711009ea47eSEdward Tomasz Napierala 			if (errno == EINTR)
1712009ea47eSEdward Tomasz Napierala 				return;
1713009ea47eSEdward Tomasz Napierala 			log_err(1, "select");
1714009ea47eSEdward Tomasz Napierala 		}
1715009ea47eSEdward Tomasz Napierala 		TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1716009ea47eSEdward Tomasz Napierala 			TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
1717009ea47eSEdward Tomasz Napierala 				if (!FD_ISSET(portal->p_socket, &fdset))
1718009ea47eSEdward Tomasz Napierala 					continue;
1719009ea47eSEdward Tomasz Napierala 				client_fd = accept(portal->p_socket, NULL, 0);
1720009ea47eSEdward Tomasz Napierala 				if (client_fd < 0)
1721009ea47eSEdward Tomasz Napierala 					log_err(1, "accept");
1722009ea47eSEdward Tomasz Napierala 				handle_connection(portal, client_fd, dont_fork);
1723009ea47eSEdward Tomasz Napierala 				break;
1724009ea47eSEdward Tomasz Napierala 			}
1725009ea47eSEdward Tomasz Napierala 		}
1726009ea47eSEdward Tomasz Napierala #endif /* !ICL_KERNEL_PROXY */
1727009ea47eSEdward Tomasz Napierala 	}
1728009ea47eSEdward Tomasz Napierala }
1729009ea47eSEdward Tomasz Napierala 
1730009ea47eSEdward Tomasz Napierala static void
1731009ea47eSEdward Tomasz Napierala sighup_handler(int dummy __unused)
1732009ea47eSEdward Tomasz Napierala {
1733009ea47eSEdward Tomasz Napierala 
1734009ea47eSEdward Tomasz Napierala 	sighup_received = true;
1735009ea47eSEdward Tomasz Napierala }
1736009ea47eSEdward Tomasz Napierala 
1737009ea47eSEdward Tomasz Napierala static void
1738009ea47eSEdward Tomasz Napierala sigterm_handler(int dummy __unused)
1739009ea47eSEdward Tomasz Napierala {
1740009ea47eSEdward Tomasz Napierala 
1741009ea47eSEdward Tomasz Napierala 	sigterm_received = true;
1742009ea47eSEdward Tomasz Napierala }
1743009ea47eSEdward Tomasz Napierala 
1744009ea47eSEdward Tomasz Napierala static void
1745009ea47eSEdward Tomasz Napierala register_signals(void)
1746009ea47eSEdward Tomasz Napierala {
1747009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
1748009ea47eSEdward Tomasz Napierala 	int error;
1749009ea47eSEdward Tomasz Napierala 
1750009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
1751009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sighup_handler;
1752009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
1753009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGHUP, &sa, NULL);
1754009ea47eSEdward Tomasz Napierala 	if (error != 0)
1755009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
1756009ea47eSEdward Tomasz Napierala 
1757009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
1758009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGTERM, &sa, NULL);
1759009ea47eSEdward Tomasz Napierala 	if (error != 0)
1760009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
1761009ea47eSEdward Tomasz Napierala 
1762009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigterm_handler;
1763009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGINT, &sa, NULL);
1764009ea47eSEdward Tomasz Napierala 	if (error != 0)
1765009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
1766009ea47eSEdward Tomasz Napierala }
1767009ea47eSEdward Tomasz Napierala 
1768009ea47eSEdward Tomasz Napierala int
1769009ea47eSEdward Tomasz Napierala main(int argc, char **argv)
1770009ea47eSEdward Tomasz Napierala {
1771009ea47eSEdward Tomasz Napierala 	struct conf *oldconf, *newconf, *tmpconf;
1772009ea47eSEdward Tomasz Napierala 	const char *config_path = DEFAULT_CONFIG_PATH;
1773009ea47eSEdward Tomasz Napierala 	int debug = 0, ch, error;
1774009ea47eSEdward Tomasz Napierala 	bool dont_daemonize = false;
1775009ea47eSEdward Tomasz Napierala 
1776009ea47eSEdward Tomasz Napierala 	while ((ch = getopt(argc, argv, "df:")) != -1) {
1777009ea47eSEdward Tomasz Napierala 		switch (ch) {
1778009ea47eSEdward Tomasz Napierala 		case 'd':
1779009ea47eSEdward Tomasz Napierala 			dont_daemonize = true;
1780009ea47eSEdward Tomasz Napierala 			debug++;
1781009ea47eSEdward Tomasz Napierala 			break;
1782009ea47eSEdward Tomasz Napierala 		case 'f':
1783009ea47eSEdward Tomasz Napierala 			config_path = optarg;
1784009ea47eSEdward Tomasz Napierala 			break;
1785009ea47eSEdward Tomasz Napierala 		case '?':
1786009ea47eSEdward Tomasz Napierala 		default:
1787009ea47eSEdward Tomasz Napierala 			usage();
1788009ea47eSEdward Tomasz Napierala 		}
1789009ea47eSEdward Tomasz Napierala 	}
1790009ea47eSEdward Tomasz Napierala 	argc -= optind;
1791009ea47eSEdward Tomasz Napierala 	if (argc != 0)
1792009ea47eSEdward Tomasz Napierala 		usage();
1793009ea47eSEdward Tomasz Napierala 
1794009ea47eSEdward Tomasz Napierala 	log_init(debug);
1795009ea47eSEdward Tomasz Napierala 	kernel_init();
1796009ea47eSEdward Tomasz Napierala 
1797009ea47eSEdward Tomasz Napierala 	oldconf = conf_new_from_kernel();
1798009ea47eSEdward Tomasz Napierala 	newconf = conf_new_from_file(config_path);
1799009ea47eSEdward Tomasz Napierala 	if (newconf == NULL)
1800009ea47eSEdward Tomasz Napierala 		log_errx(1, "configuration error, exiting");
1801009ea47eSEdward Tomasz Napierala 	if (debug > 0) {
1802009ea47eSEdward Tomasz Napierala 		oldconf->conf_debug = debug;
1803009ea47eSEdward Tomasz Napierala 		newconf->conf_debug = debug;
1804009ea47eSEdward Tomasz Napierala 	}
1805009ea47eSEdward Tomasz Napierala 
1806009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
1807009ea47eSEdward Tomasz Napierala 	log_debugx("enabling CTL iSCSI port");
1808009ea47eSEdward Tomasz Napierala 	error = kernel_port_on();
1809009ea47eSEdward Tomasz Napierala 	if (error != 0)
1810009ea47eSEdward Tomasz Napierala 		log_errx(1, "failed to enable CTL iSCSI port, exiting");
1811009ea47eSEdward Tomasz Napierala #endif
1812009ea47eSEdward Tomasz Napierala 
1813009ea47eSEdward Tomasz Napierala 	error = conf_apply(oldconf, newconf);
1814009ea47eSEdward Tomasz Napierala 	if (error != 0)
1815009ea47eSEdward Tomasz Napierala 		log_errx(1, "failed to apply configuration, exiting");
1816009ea47eSEdward Tomasz Napierala 	conf_delete(oldconf);
1817009ea47eSEdward Tomasz Napierala 	oldconf = NULL;
1818009ea47eSEdward Tomasz Napierala 
1819009ea47eSEdward Tomasz Napierala 	register_signals();
1820009ea47eSEdward Tomasz Napierala 
1821009ea47eSEdward Tomasz Napierala #ifndef ICL_KERNEL_PROXY
1822009ea47eSEdward Tomasz Napierala 	log_debugx("enabling CTL iSCSI port");
1823009ea47eSEdward Tomasz Napierala 	error = kernel_port_on();
1824009ea47eSEdward Tomasz Napierala 	if (error != 0)
1825009ea47eSEdward Tomasz Napierala 		log_errx(1, "failed to enable CTL iSCSI port, exiting");
1826009ea47eSEdward Tomasz Napierala #endif
1827009ea47eSEdward Tomasz Napierala 
18285d5a95f1SEdward Tomasz Napierala 	if (dont_daemonize == false) {
18295d5a95f1SEdward Tomasz Napierala 		log_debugx("daemonizing");
18305d5a95f1SEdward Tomasz Napierala 		if (daemon(0, 0) == -1) {
18315d5a95f1SEdward Tomasz Napierala 			log_warn("cannot daemonize");
18325d5a95f1SEdward Tomasz Napierala 			pidfile_remove(newconf->conf_pidfh);
18335d5a95f1SEdward Tomasz Napierala 			exit(1);
18345d5a95f1SEdward Tomasz Napierala 		}
18355d5a95f1SEdward Tomasz Napierala 	}
18365d5a95f1SEdward Tomasz Napierala 
1837009ea47eSEdward Tomasz Napierala 	for (;;) {
1838009ea47eSEdward Tomasz Napierala 		main_loop(newconf, dont_daemonize);
1839009ea47eSEdward Tomasz Napierala 		if (sighup_received) {
1840009ea47eSEdward Tomasz Napierala 			sighup_received = false;
1841009ea47eSEdward Tomasz Napierala 			log_debugx("received SIGHUP, reloading configuration");
1842009ea47eSEdward Tomasz Napierala 			tmpconf = conf_new_from_file(config_path);
1843009ea47eSEdward Tomasz Napierala 			if (tmpconf == NULL) {
1844009ea47eSEdward Tomasz Napierala 				log_warnx("configuration error, "
1845009ea47eSEdward Tomasz Napierala 				    "continuing with old configuration");
1846009ea47eSEdward Tomasz Napierala 			} else {
1847009ea47eSEdward Tomasz Napierala 				if (debug > 0)
1848009ea47eSEdward Tomasz Napierala 					tmpconf->conf_debug = debug;
1849009ea47eSEdward Tomasz Napierala 				oldconf = newconf;
1850009ea47eSEdward Tomasz Napierala 				newconf = tmpconf;
1851009ea47eSEdward Tomasz Napierala 				error = conf_apply(oldconf, newconf);
1852009ea47eSEdward Tomasz Napierala 				if (error != 0)
1853009ea47eSEdward Tomasz Napierala 					log_warnx("failed to reload "
1854009ea47eSEdward Tomasz Napierala 					    "configuration");
1855009ea47eSEdward Tomasz Napierala 				conf_delete(oldconf);
1856009ea47eSEdward Tomasz Napierala 				oldconf = NULL;
1857009ea47eSEdward Tomasz Napierala 			}
1858009ea47eSEdward Tomasz Napierala 		} else if (sigterm_received) {
1859009ea47eSEdward Tomasz Napierala 			log_debugx("exiting on signal; "
1860009ea47eSEdward Tomasz Napierala 			    "reloading empty configuration");
1861009ea47eSEdward Tomasz Napierala 
1862009ea47eSEdward Tomasz Napierala 			log_debugx("disabling CTL iSCSI port "
1863009ea47eSEdward Tomasz Napierala 			    "and terminating all connections");
1864009ea47eSEdward Tomasz Napierala 			error = kernel_port_off();
1865009ea47eSEdward Tomasz Napierala 			if (error != 0)
1866009ea47eSEdward Tomasz Napierala 				log_warnx("failed to disable CTL iSCSI port");
1867009ea47eSEdward Tomasz Napierala 
1868009ea47eSEdward Tomasz Napierala 			oldconf = newconf;
1869009ea47eSEdward Tomasz Napierala 			newconf = conf_new();
1870009ea47eSEdward Tomasz Napierala 			if (debug > 0)
1871009ea47eSEdward Tomasz Napierala 				newconf->conf_debug = debug;
1872009ea47eSEdward Tomasz Napierala 			error = conf_apply(oldconf, newconf);
1873009ea47eSEdward Tomasz Napierala 			if (error != 0)
1874009ea47eSEdward Tomasz Napierala 				log_warnx("failed to apply configuration");
1875009ea47eSEdward Tomasz Napierala 
1876009ea47eSEdward Tomasz Napierala 			log_warnx("exiting on signal");
1877009ea47eSEdward Tomasz Napierala 			exit(0);
1878009ea47eSEdward Tomasz Napierala 		} else {
1879009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(false);
1880009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
1881009ea47eSEdward Tomasz Napierala 		}
1882009ea47eSEdward Tomasz Napierala 	}
1883009ea47eSEdward Tomasz Napierala 	/* NOTREACHED */
1884009ea47eSEdward Tomasz Napierala }
1885