hastd.c revision 204076
1204076Spjd/*-
2204076Spjd * Copyright (c) 2009-2010 The FreeBSD Foundation
3204076Spjd * All rights reserved.
4204076Spjd *
5204076Spjd * This software was developed by Pawel Jakub Dawidek under sponsorship from
6204076Spjd * the FreeBSD Foundation.
7204076Spjd *
8204076Spjd * Redistribution and use in source and binary forms, with or without
9204076Spjd * modification, are permitted provided that the following conditions
10204076Spjd * are met:
11204076Spjd * 1. Redistributions of source code must retain the above copyright
12204076Spjd *    notice, this list of conditions and the following disclaimer.
13204076Spjd * 2. Redistributions in binary form must reproduce the above copyright
14204076Spjd *    notice, this list of conditions and the following disclaimer in the
15204076Spjd *    documentation and/or other materials provided with the distribution.
16204076Spjd *
17204076Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18204076Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19204076Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20204076Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21204076Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22204076Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23204076Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24204076Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25204076Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26204076Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27204076Spjd * SUCH DAMAGE.
28204076Spjd */
29204076Spjd
30204076Spjd#include <sys/cdefs.h>
31204076Spjd__FBSDID("$FreeBSD: head/sbin/hastd/hastd.c 204076 2010-02-18 23:16:19Z pjd $");
32204076Spjd
33204076Spjd#include <sys/param.h>
34204076Spjd#include <sys/linker.h>
35204076Spjd#include <sys/module.h>
36204076Spjd#include <sys/wait.h>
37204076Spjd
38204076Spjd#include <assert.h>
39204076Spjd#include <err.h>
40204076Spjd#include <errno.h>
41204076Spjd#include <libutil.h>
42204076Spjd#include <signal.h>
43204076Spjd#include <stdbool.h>
44204076Spjd#include <stdio.h>
45204076Spjd#include <stdlib.h>
46204076Spjd#include <string.h>
47204076Spjd#include <sysexits.h>
48204076Spjd#include <unistd.h>
49204076Spjd
50204076Spjd#include <activemap.h>
51204076Spjd#include <pjdlog.h>
52204076Spjd
53204076Spjd#include "control.h"
54204076Spjd#include "hast.h"
55204076Spjd#include "hast_proto.h"
56204076Spjd#include "hastd.h"
57204076Spjd#include "subr.h"
58204076Spjd
59204076Spjd/* Path to configuration file. */
60204076Spjdstatic const char *cfgpath = HAST_CONFIG;
61204076Spjd/* Hastd configuration. */
62204076Spjdstatic struct hastd_config *cfg;
63204076Spjd/* Was SIGCHLD signal received? */
64204076Spjdstatic bool sigchld_received = false;
65204076Spjd/* Was SIGHUP signal received? */
66204076Spjdstatic bool sighup_received = false;
67204076Spjd/* Was SIGINT or SIGTERM signal received? */
68204076Spjdbool sigexit_received = false;
69204076Spjd/* PID file handle. */
70204076Spjdstruct pidfh *pfh;
71204076Spjd
72204076Spjdstatic void
73204076Spjdusage(void)
74204076Spjd{
75204076Spjd
76204076Spjd	errx(EX_USAGE, "[-dFh] [-c config] [-P pidfile]");
77204076Spjd}
78204076Spjd
79204076Spjdstatic void
80204076Spjdsighandler(int sig)
81204076Spjd{
82204076Spjd
83204076Spjd	switch (sig) {
84204076Spjd	case SIGCHLD:
85204076Spjd		sigchld_received = true;
86204076Spjd		break;
87204076Spjd	case SIGHUP:
88204076Spjd		sighup_received = true;
89204076Spjd		break;
90204076Spjd	default:
91204076Spjd		assert(!"invalid condition");
92204076Spjd	}
93204076Spjd}
94204076Spjd
95204076Spjdstatic void
96204076Spjdg_gate_load(void)
97204076Spjd{
98204076Spjd
99204076Spjd	if (modfind("g_gate") == -1) {
100204076Spjd		/* Not present in kernel, try loading it. */
101204076Spjd		if (kldload("geom_gate") == -1 || modfind("g_gate") == -1) {
102204076Spjd			if (errno != EEXIST) {
103204076Spjd				pjdlog_exit(EX_OSERR,
104204076Spjd				    "Unable to load geom_gate module");
105204076Spjd			}
106204076Spjd		}
107204076Spjd	}
108204076Spjd}
109204076Spjd
110204076Spjdstatic void
111204076Spjdchild_exit(void)
112204076Spjd{
113204076Spjd	struct hast_resource *res;
114204076Spjd	int status;
115204076Spjd	pid_t pid;
116204076Spjd
117204076Spjd	while ((pid = wait3(&status, WNOHANG, NULL)) > 0) {
118204076Spjd		/* Find resource related to the process that just exited. */
119204076Spjd		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
120204076Spjd			if (pid == res->hr_workerpid)
121204076Spjd				break;
122204076Spjd		}
123204076Spjd		if (res == NULL) {
124204076Spjd			/*
125204076Spjd			 * This can happen when new connection arrives and we
126204076Spjd			 * cancel child responsible for the old one.
127204076Spjd			 */
128204076Spjd			continue;
129204076Spjd		}
130204076Spjd		pjdlog_prefix_set("[%s] (%s) ", res->hr_name,
131204076Spjd		    role2str(res->hr_role));
132204076Spjd		if (WEXITSTATUS(status) == 0) {
133204076Spjd			pjdlog_debug(1,
134204076Spjd			    "Worker process exited gracefully (pid=%u).",
135204076Spjd			    (unsigned int)pid);
136204076Spjd		} else {
137204076Spjd			pjdlog_error("Worker process failed (pid=%u, status=%d).",
138204076Spjd			    (unsigned int)pid, WEXITSTATUS(status));
139204076Spjd		}
140204076Spjd		res->hr_workerpid = 0;
141204076Spjd		if (res->hr_role == HAST_ROLE_PRIMARY) {
142204076Spjd			sleep(1);
143204076Spjd			pjdlog_info("Restarting worker process.");
144204076Spjd			hastd_primary(res);
145204076Spjd		}
146204076Spjd		pjdlog_prefix_set("%s", "");
147204076Spjd	}
148204076Spjd}
149204076Spjd
150204076Spjdstatic void
151204076Spjdhastd_reload(void)
152204076Spjd{
153204076Spjd
154204076Spjd	/* TODO */
155204076Spjd	pjdlog_warning("Configuration reload is not implemented.");
156204076Spjd}
157204076Spjd
158204076Spjdstatic void
159204076Spjdlisten_accept(void)
160204076Spjd{
161204076Spjd	struct hast_resource *res;
162204076Spjd	struct proto_conn *conn;
163204076Spjd	struct nv *nvin, *nvout, *nverr;
164204076Spjd	const char *resname;
165204076Spjd	const unsigned char *token;
166204076Spjd	char laddr[256], raddr[256];
167204076Spjd	size_t size;
168204076Spjd	pid_t pid;
169204076Spjd	int status;
170204076Spjd
171204076Spjd	proto_local_address(cfg->hc_listenconn, laddr, sizeof(laddr));
172204076Spjd	pjdlog_debug(1, "Accepting connection to %s.", laddr);
173204076Spjd
174204076Spjd	if (proto_accept(cfg->hc_listenconn, &conn) < 0) {
175204076Spjd		pjdlog_errno(LOG_ERR, "Unable to accept connection %s", laddr);
176204076Spjd		return;
177204076Spjd	}
178204076Spjd
179204076Spjd	proto_local_address(conn, laddr, sizeof(laddr));
180204076Spjd	proto_remote_address(conn, raddr, sizeof(raddr));
181204076Spjd	pjdlog_info("Connection from %s to %s.", laddr, raddr);
182204076Spjd
183204076Spjd	nvin = nvout = nverr = NULL;
184204076Spjd
185204076Spjd	/*
186204076Spjd	 * Before receiving any data see if remote host have access to any
187204076Spjd	 * resource.
188204076Spjd	 */
189204076Spjd	TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
190204076Spjd		if (proto_address_match(conn, res->hr_remoteaddr))
191204076Spjd			break;
192204076Spjd	}
193204076Spjd	if (res == NULL) {
194204076Spjd		pjdlog_error("Client %s isn't known.", raddr);
195204076Spjd		goto close;
196204076Spjd	}
197204076Spjd	/* Ok, remote host can access at least one resource. */
198204076Spjd
199204076Spjd	if (hast_proto_recv_hdr(conn, &nvin) < 0) {
200204076Spjd		pjdlog_errno(LOG_ERR, "Unable to receive header from %s",
201204076Spjd		    raddr);
202204076Spjd		goto close;
203204076Spjd	}
204204076Spjd
205204076Spjd	resname = nv_get_string(nvin, "resource");
206204076Spjd	if (resname == NULL) {
207204076Spjd		pjdlog_error("No 'resource' field in the header received from %s.",
208204076Spjd		    raddr);
209204076Spjd		goto close;
210204076Spjd	}
211204076Spjd	pjdlog_debug(2, "%s: resource=%s", raddr, resname);
212204076Spjd	token = nv_get_uint8_array(nvin, &size, "token");
213204076Spjd	/*
214204076Spjd	 * NULL token means that this is first conection.
215204076Spjd	 */
216204076Spjd	if (token != NULL && size != sizeof(res->hr_token)) {
217204076Spjd		pjdlog_error("Received token of invalid size from %s (expected %zu, got %zu).",
218204076Spjd		    raddr, sizeof(res->hr_token), size);
219204076Spjd		goto close;
220204076Spjd	}
221204076Spjd
222204076Spjd	/*
223204076Spjd	 * From now on we want to send errors to the remote node.
224204076Spjd	 */
225204076Spjd	nverr = nv_alloc();
226204076Spjd
227204076Spjd	/* Find resource related to this connection. */
228204076Spjd	TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
229204076Spjd		if (strcmp(resname, res->hr_name) == 0)
230204076Spjd			break;
231204076Spjd	}
232204076Spjd	/* Have we found the resource? */
233204076Spjd	if (res == NULL) {
234204076Spjd		pjdlog_error("No resource '%s' as requested by %s.",
235204076Spjd		    resname, raddr);
236204076Spjd		nv_add_stringf(nverr, "errmsg", "Resource not configured.");
237204076Spjd		goto fail;
238204076Spjd	}
239204076Spjd
240204076Spjd	/* Now that we know resource name setup log prefix. */
241204076Spjd	pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
242204076Spjd
243204076Spjd	/* Does the remote host have access to this resource? */
244204076Spjd	if (!proto_address_match(conn, res->hr_remoteaddr)) {
245204076Spjd		pjdlog_error("Client %s has no access to the resource.", raddr);
246204076Spjd		nv_add_stringf(nverr, "errmsg", "No access to the resource.");
247204076Spjd		goto fail;
248204076Spjd	}
249204076Spjd	/* Is the resource marked as secondary? */
250204076Spjd	if (res->hr_role != HAST_ROLE_SECONDARY) {
251204076Spjd		pjdlog_error("We act as %s for the resource and not as %s as requested by %s.",
252204076Spjd		    role2str(res->hr_role), role2str(HAST_ROLE_SECONDARY),
253204076Spjd		    raddr);
254204076Spjd		nv_add_stringf(nverr, "errmsg",
255204076Spjd		    "Remote node acts as %s for the resource and not as %s.",
256204076Spjd		    role2str(res->hr_role), role2str(HAST_ROLE_SECONDARY));
257204076Spjd		goto fail;
258204076Spjd	}
259204076Spjd	/* Does token (if exists) match? */
260204076Spjd	if (token != NULL && memcmp(token, res->hr_token,
261204076Spjd	    sizeof(res->hr_token)) != 0) {
262204076Spjd		pjdlog_error("Token received from %s doesn't match.", raddr);
263204076Spjd		nv_add_stringf(nverr, "errmsg", "Toke doesn't match.");
264204076Spjd		goto fail;
265204076Spjd	}
266204076Spjd	/*
267204076Spjd	 * If there is no token, but we have half-open connection
268204076Spjd	 * (only remotein) or full connection (worker process is running)
269204076Spjd	 * we have to cancel those and accept the new connection.
270204076Spjd	 */
271204076Spjd	if (token == NULL) {
272204076Spjd		assert(res->hr_remoteout == NULL);
273204076Spjd		pjdlog_debug(1, "Initial connection from %s.", raddr);
274204076Spjd		if (res->hr_workerpid != 0) {
275204076Spjd			assert(res->hr_remotein == NULL);
276204076Spjd			pjdlog_debug(1,
277204076Spjd			    "Worker process exists (pid=%u), stopping it.",
278204076Spjd			    (unsigned int)res->hr_workerpid);
279204076Spjd			/* Stop child process. */
280204076Spjd			if (kill(res->hr_workerpid, SIGINT) < 0) {
281204076Spjd				pjdlog_errno(LOG_ERR,
282204076Spjd				    "Unable to stop worker process (pid=%u)",
283204076Spjd				    (unsigned int)res->hr_workerpid);
284204076Spjd				/*
285204076Spjd				 * Other than logging the problem we
286204076Spjd				 * ignore it - nothing smart to do.
287204076Spjd				 */
288204076Spjd			}
289204076Spjd			/* Wait for it to exit. */
290204076Spjd			else if ((pid = waitpid(res->hr_workerpid,
291204076Spjd			    &status, 0)) != res->hr_workerpid) {
292204076Spjd				pjdlog_errno(LOG_ERR,
293204076Spjd				    "Waiting for worker process (pid=%u) failed",
294204076Spjd				    (unsigned int)res->hr_workerpid);
295204076Spjd				/* See above. */
296204076Spjd			} else if (status != 0) {
297204076Spjd				pjdlog_error("Worker process (pid=%u) exited ungracefully: status=%d.",
298204076Spjd				    (unsigned int)res->hr_workerpid, status);
299204076Spjd				/* See above. */
300204076Spjd			} else {
301204076Spjd				pjdlog_debug(1,
302204076Spjd				    "Worker process (pid=%u) exited gracefully.",
303204076Spjd				    (unsigned int)res->hr_workerpid);
304204076Spjd			}
305204076Spjd			res->hr_workerpid = 0;
306204076Spjd		} else if (res->hr_remotein != NULL) {
307204076Spjd			char oaddr[256];
308204076Spjd
309204076Spjd			proto_remote_address(conn, oaddr, sizeof(oaddr));
310204076Spjd			pjdlog_debug(1,
311204076Spjd			    "Canceling half-open connection from %s on connection from %s.",
312204076Spjd			    oaddr, raddr);
313204076Spjd			proto_close(res->hr_remotein);
314204076Spjd			res->hr_remotein = NULL;
315204076Spjd		}
316204076Spjd	}
317204076Spjd
318204076Spjd	/*
319204076Spjd	 * Checks and cleanups are done.
320204076Spjd	 */
321204076Spjd
322204076Spjd	if (token == NULL) {
323204076Spjd		arc4random_buf(res->hr_token, sizeof(res->hr_token));
324204076Spjd		nvout = nv_alloc();
325204076Spjd		nv_add_uint8_array(nvout, res->hr_token,
326204076Spjd		    sizeof(res->hr_token), "token");
327204076Spjd		if (nv_error(nvout) != 0) {
328204076Spjd			pjdlog_common(LOG_ERR, 0, nv_error(nvout),
329204076Spjd			    "Unable to prepare return header for %s", raddr);
330204076Spjd			nv_add_stringf(nverr, "errmsg",
331204076Spjd			    "Remote node was unable to prepare return header: %s.",
332204076Spjd			    strerror(nv_error(nvout)));
333204076Spjd			goto fail;
334204076Spjd		}
335204076Spjd		if (hast_proto_send(NULL, conn, nvout, NULL, 0) < 0) {
336204076Spjd			int error = errno;
337204076Spjd
338204076Spjd			pjdlog_errno(LOG_ERR, "Unable to send response to %s",
339204076Spjd			    raddr);
340204076Spjd			nv_add_stringf(nverr, "errmsg",
341204076Spjd			    "Remote node was unable to send response: %s.",
342204076Spjd			    strerror(error));
343204076Spjd			goto fail;
344204076Spjd		}
345204076Spjd		res->hr_remotein = conn;
346204076Spjd		pjdlog_debug(1, "Incoming connection from %s configured.",
347204076Spjd		    raddr);
348204076Spjd	} else {
349204076Spjd		res->hr_remoteout = conn;
350204076Spjd		pjdlog_debug(1, "Outgoing connection to %s configured.", raddr);
351204076Spjd		hastd_secondary(res, nvin);
352204076Spjd	}
353204076Spjd	nv_free(nvin);
354204076Spjd	nv_free(nvout);
355204076Spjd	nv_free(nverr);
356204076Spjd	pjdlog_prefix_set("%s", "");
357204076Spjd	return;
358204076Spjdfail:
359204076Spjd	if (nv_error(nverr) != 0) {
360204076Spjd		pjdlog_common(LOG_ERR, 0, nv_error(nverr),
361204076Spjd		    "Unable to prepare error header for %s", raddr);
362204076Spjd		goto close;
363204076Spjd	}
364204076Spjd	if (hast_proto_send(NULL, conn, nverr, NULL, 0) < 0) {
365204076Spjd		pjdlog_errno(LOG_ERR, "Unable to send error to %s", raddr);
366204076Spjd		goto close;
367204076Spjd	}
368204076Spjdclose:
369204076Spjd	if (nvin != NULL)
370204076Spjd		nv_free(nvin);
371204076Spjd	if (nvout != NULL)
372204076Spjd		nv_free(nvout);
373204076Spjd	if (nverr != NULL)
374204076Spjd		nv_free(nverr);
375204076Spjd	proto_close(conn);
376204076Spjd	pjdlog_prefix_set("%s", "");
377204076Spjd}
378204076Spjd
379204076Spjdstatic void
380204076Spjdmain_loop(void)
381204076Spjd{
382204076Spjd	fd_set rfds, wfds;
383204076Spjd	int fd, maxfd, ret;
384204076Spjd
385204076Spjd	for (;;) {
386204076Spjd		if (sigchld_received) {
387204076Spjd			sigchld_received = false;
388204076Spjd			child_exit();
389204076Spjd		}
390204076Spjd		if (sighup_received) {
391204076Spjd			sighup_received = false;
392204076Spjd			hastd_reload();
393204076Spjd		}
394204076Spjd
395204076Spjd		maxfd = 0;
396204076Spjd		FD_ZERO(&rfds);
397204076Spjd		FD_ZERO(&wfds);
398204076Spjd
399204076Spjd		/* Setup descriptors for select(2). */
400204076Spjd#define	SETUP_FD(conn)	do {						\
401204076Spjd	fd = proto_descriptor(conn);					\
402204076Spjd	if (fd >= 0) {							\
403204076Spjd		maxfd = fd > maxfd ? fd : maxfd;			\
404204076Spjd		FD_SET(fd, &rfds);					\
405204076Spjd		FD_SET(fd, &wfds);					\
406204076Spjd	}								\
407204076Spjd} while (0)
408204076Spjd		SETUP_FD(cfg->hc_controlconn);
409204076Spjd		SETUP_FD(cfg->hc_listenconn);
410204076Spjd#undef	SETUP_FD
411204076Spjd
412204076Spjd		ret = select(maxfd + 1, &rfds, &wfds, NULL, NULL);
413204076Spjd		if (ret == -1) {
414204076Spjd			if (errno == EINTR)
415204076Spjd				continue;
416204076Spjd			KEEP_ERRNO((void)pidfile_remove(pfh));
417204076Spjd			pjdlog_exit(EX_OSERR, "select() failed");
418204076Spjd		}
419204076Spjd
420204076Spjd#define	ISSET_FD(conn)	\
421204076Spjd	(FD_ISSET((fd = proto_descriptor(conn)), &rfds) || FD_ISSET(fd, &wfds))
422204076Spjd		if (ISSET_FD(cfg->hc_controlconn))
423204076Spjd			control_handle(cfg);
424204076Spjd		if (ISSET_FD(cfg->hc_listenconn))
425204076Spjd			listen_accept();
426204076Spjd#undef	ISSET_FD
427204076Spjd	}
428204076Spjd}
429204076Spjd
430204076Spjdint
431204076Spjdmain(int argc, char *argv[])
432204076Spjd{
433204076Spjd	const char *pidfile;
434204076Spjd	pid_t otherpid;
435204076Spjd	bool foreground;
436204076Spjd	int debuglevel;
437204076Spjd
438204076Spjd	g_gate_load();
439204076Spjd
440204076Spjd	foreground = false;
441204076Spjd	debuglevel = 0;
442204076Spjd	pidfile = HASTD_PIDFILE;
443204076Spjd
444204076Spjd	for (;;) {
445204076Spjd		int ch;
446204076Spjd
447204076Spjd		ch = getopt(argc, argv, "c:dFhP:");
448204076Spjd		if (ch == -1)
449204076Spjd			break;
450204076Spjd		switch (ch) {
451204076Spjd		case 'c':
452204076Spjd			cfgpath = optarg;
453204076Spjd			break;
454204076Spjd		case 'd':
455204076Spjd			debuglevel++;
456204076Spjd			break;
457204076Spjd		case 'F':
458204076Spjd			foreground = true;
459204076Spjd			break;
460204076Spjd		case 'P':
461204076Spjd			pidfile = optarg;
462204076Spjd			break;
463204076Spjd		case 'h':
464204076Spjd		default:
465204076Spjd			usage();
466204076Spjd		}
467204076Spjd	}
468204076Spjd	argc -= optind;
469204076Spjd	argv += optind;
470204076Spjd
471204076Spjd	pjdlog_debug_set(debuglevel);
472204076Spjd
473204076Spjd	pfh = pidfile_open(pidfile, 0600, &otherpid);
474204076Spjd	if (pfh == NULL) {
475204076Spjd		if (errno == EEXIST) {
476204076Spjd			pjdlog_exitx(EX_TEMPFAIL,
477204076Spjd			    "Another hastd is already running, pid: %jd.",
478204076Spjd			    (intmax_t)otherpid);
479204076Spjd		}
480204076Spjd		/* If we cannot create pidfile from other reasons, only warn. */
481204076Spjd		pjdlog_errno(LOG_WARNING, "Cannot open or create pidfile");
482204076Spjd	}
483204076Spjd
484204076Spjd	cfg = yy_config_parse(cfgpath);
485204076Spjd	assert(cfg != NULL);
486204076Spjd
487204076Spjd	signal(SIGHUP, sighandler);
488204076Spjd	signal(SIGCHLD, sighandler);
489204076Spjd
490204076Spjd	/* Listen on control address. */
491204076Spjd	if (proto_server(cfg->hc_controladdr, &cfg->hc_controlconn) < 0) {
492204076Spjd		KEEP_ERRNO((void)pidfile_remove(pfh));
493204076Spjd		pjdlog_exit(EX_OSERR, "Unable to listen on control address %s",
494204076Spjd		    cfg->hc_controladdr);
495204076Spjd	}
496204076Spjd	/* Listen for remote connections. */
497204076Spjd	if (proto_server(cfg->hc_listenaddr, &cfg->hc_listenconn) < 0) {
498204076Spjd		KEEP_ERRNO((void)pidfile_remove(pfh));
499204076Spjd		pjdlog_exit(EX_OSERR, "Unable to listen on address %s",
500204076Spjd		    cfg->hc_listenaddr);
501204076Spjd	}
502204076Spjd
503204076Spjd	if (!foreground) {
504204076Spjd		if (daemon(0, 0) < 0) {
505204076Spjd			KEEP_ERRNO((void)pidfile_remove(pfh));
506204076Spjd			pjdlog_exit(EX_OSERR, "Unable to daemonize");
507204076Spjd		}
508204076Spjd
509204076Spjd		/* Start logging to syslog. */
510204076Spjd		pjdlog_mode_set(PJDLOG_MODE_SYSLOG);
511204076Spjd
512204076Spjd		/* Write PID to a file. */
513204076Spjd		if (pidfile_write(pfh) < 0) {
514204076Spjd			pjdlog_errno(LOG_WARNING,
515204076Spjd			    "Unable to write PID to a file");
516204076Spjd		}
517204076Spjd	}
518204076Spjd
519204076Spjd	main_loop();
520204076Spjd
521204076Spjd	exit(0);
522204076Spjd}
523