15455Sdg/*-
21541Srgrimes * Copyright (c) 2009-2010 The FreeBSD Foundation
31541Srgrimes * Copyright (c) 2010-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
41541Srgrimes * All rights reserved.
51541Srgrimes *
61541Srgrimes * This software was developed by Pawel Jakub Dawidek under sponsorship from
71541Srgrimes * the FreeBSD Foundation.
81541Srgrimes *
91541Srgrimes * Redistribution and use in source and binary forms, with or without
101541Srgrimes * modification, are permitted provided that the following conditions
111541Srgrimes * are met:
121541Srgrimes * 1. Redistributions of source code must retain the above copyright
131541Srgrimes *    notice, this list of conditions and the following disclaimer.
141541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151541Srgrimes *    notice, this list of conditions and the following disclaimer in the
161541Srgrimes *    documentation and/or other materials provided with the distribution.
1758705Scharnier *
181541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
191541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
221541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281541Srgrimes * SUCH DAMAGE.
291541Srgrimes */
301541Srgrimes
311541Srgrimes#include <sys/cdefs.h>
321541Srgrimes__FBSDID("$FreeBSD$");
331541Srgrimes
341541Srgrimes#include <sys/param.h>
351541Srgrimes#include <sys/linker.h>
361817Sdg#include <sys/module.h>
371541Srgrimes#include <sys/stat.h>
381541Srgrimes#include <sys/wait.h>
391541Srgrimes
401541Srgrimes#include <err.h>
411541Srgrimes#include <errno.h>
421541Srgrimes#include <libutil.h>
435455Sdg#include <signal.h>
441541Srgrimes#include <stdbool.h>
451541Srgrimes#include <stdio.h>
461541Srgrimes#include <stdlib.h>
471541Srgrimes#include <string.h>
481541Srgrimes#include <sysexits.h>
495455Sdg#include <time.h>
505455Sdg#include <unistd.h>
515455Sdg
521541Srgrimes#include <activemap.h>
535455Sdg#include <pjdlog.h>
541541Srgrimes
551541Srgrimes#include "control.h"
561541Srgrimes#include "event.h"
571541Srgrimes#include "hast.h"
581541Srgrimes#include "hast_proto.h"
591541Srgrimes#include "hastd.h"
601541Srgrimes#include "hooks.h"
611541Srgrimes#include "subr.h"
621541Srgrimes
631817Sdg/* Path to configuration file. */
6450477Speterconst char *cfgpath = HAST_CONFIG;
651541Srgrimes/* Hastd configuration. */
661541Srgrimesstatic struct hastd_config *cfg;
671541Srgrimes/* Was SIGINT or SIGTERM signal received? */
681541Srgrimesbool sigexit_received = false;
691541Srgrimes/* Path to pidfile. */
701541Srgrimesstatic const char *pidfile;
711541Srgrimes/* Pidfile handle. */
721541Srgrimesstruct pidfh *pfh;
7387157Sluigi/* Do we run in foreground? */
7476166Smarkmstatic bool foreground;
7576166Smarkm
762112Swollman/* How often check for hooks running for too long. */
776129Sdg#define	REPORT_INTERVAL	5
781541Srgrimes
791541Srgrimesstatic void
8012662Sdgusage(void)
8112662Sdg{
8212662Sdg
8312662Sdg	errx(EX_USAGE, "[-dFh] [-c config] [-P pidfile]");
841541Srgrimes}
851541Srgrimes
8612726Sbdestatic void
871541Srgrimesg_gate_load(void)
8819830Sdyson{
8919830Sdyson
9019830Sdyson	if (modfind("g_gate") == -1) {
9119830Sdyson		/* Not present in kernel, try loading it. */
9219830Sdyson		if (kldload("geom_gate") == -1 || modfind("g_gate") == -1) {
932112Swollman			if (errno != EEXIST) {
941541Srgrimes				pjdlog_exit(EX_OSERR,
951541Srgrimes				    "Unable to load geom_gate module");
961541Srgrimes			}
971541Srgrimes		}
9812259Sdg	}
991541Srgrimes}
1008876Srgrimes
1015455Sdgvoid
1025455Sdgdescriptors_cleanup(struct hast_resource *res)
10370480Salfred{
1041541Srgrimes	struct hast_resource *tres, *tmres;
1055455Sdg	struct hastd_listen *lst;
10670480Salfred
1071541Srgrimes	TAILQ_FOREACH_SAFE(tres, &cfg->hc_resources, hr_next, tmres) {
1081541Srgrimes		if (tres == res) {
1091541Srgrimes			PJDLOG_VERIFY(res->hr_role == HAST_ROLE_SECONDARY ||
11098686Salc			    (res->hr_remotein == NULL &&
11113490Sdyson			     res->hr_remoteout == NULL));
1121541Srgrimes			continue;
1135455Sdg		}
1141541Srgrimes		if (tres->hr_remotein != NULL)
1155455Sdg			proto_close(tres->hr_remotein);
1161541Srgrimes		if (tres->hr_remoteout != NULL)
1171541Srgrimes			proto_close(tres->hr_remoteout);
1181541Srgrimes		if (tres->hr_ctrl != NULL)
11947841Sdt			proto_close(tres->hr_ctrl);
12047841Sdt		if (tres->hr_event != NULL)
12147841Sdt			proto_close(tres->hr_event);
12247841Sdt		if (tres->hr_conn != NULL)
12347841Sdt			proto_close(tres->hr_conn);
12447841Sdt		TAILQ_REMOVE(&cfg->hc_resources, tres, hr_next);
12547841Sdt		free(tres);
12670480Salfred	}
12747841Sdt	if (cfg->hc_controlin != NULL)
12847841Sdt		proto_close(cfg->hc_controlin);
12970480Salfred	proto_close(cfg->hc_controlconn);
13047841Sdt	while ((lst = TAILQ_FIRST(&cfg->hc_listen)) != NULL) {
13147841Sdt		TAILQ_REMOVE(&cfg->hc_listen, lst, hl_next);
13247841Sdt		if (lst->hl_conn != NULL)
13398686Salc			proto_close(lst->hl_conn);
13447841Sdt		free(lst);
13547841Sdt	}
13647841Sdt	(void)pidfile_close(pfh);
13747841Sdt	hook_fini();
13847841Sdt	pjdlog_fini();
13947841Sdt}
14047841Sdt
14147841Sdtstatic const char *
1421541Srgrimesdtype2str(mode_t mode)
1431541Srgrimes{
1441541Srgrimes
1458876Srgrimes	if (S_ISBLK(mode))
1465455Sdg		return ("block device");
14770480Salfred	else if (S_ISCHR(mode))
14870480Salfred		return ("character device");
1491541Srgrimes	else if (S_ISDIR(mode))
1505455Sdg		return ("directory");
15170480Salfred	else if (S_ISFIFO(mode))
1525455Sdg		return ("pipe or FIFO");
1531541Srgrimes	else if (S_ISLNK(mode))
15479224Sdillon		return ("symbolic link");
15579224Sdillon	else if (S_ISREG(mode))
1561541Srgrimes		return ("regular file");
1571541Srgrimes	else if (S_ISSOCK(mode))
1581541Srgrimes		return ("socket");
1595455Sdg	else if (S_ISWHT(mode))
1605455Sdg		return ("whiteout");
1611541Srgrimes	else
1621541Srgrimes		return ("unknown");
1631541Srgrimes}
1645455Sdg
1655455Sdgvoid
1665455Sdgdescriptors_assert(const struct hast_resource *res, int pjdlogmode)
1671541Srgrimes{
1681541Srgrimes	char msg[256];
16933758Sdyson	struct stat sb;
1701541Srgrimes	long maxfd;
1711541Srgrimes	bool isopen;
1721541Srgrimes	mode_t mode;
1731541Srgrimes	int fd;
1741541Srgrimes
17513490Sdyson	/*
17613490Sdyson	 * At this point descriptor to syslog socket is closed, so if we want
1771541Srgrimes	 * to log assertion message, we have to first store it in 'msg' local
1781541Srgrimes	 * buffer and then open syslog socket and log it.
1791541Srgrimes	 */
1805455Sdg	msg[0] = '\0';
1815455Sdg
1825455Sdg	maxfd = sysconf(_SC_OPEN_MAX);
1838876Srgrimes	if (maxfd == -1) {
1845455Sdg		pjdlog_init(pjdlogmode);
1855455Sdg		pjdlog_prefix_set("[%s] (%s) ", res->hr_name,
1865455Sdg		    role2str(res->hr_role));
1875455Sdg		pjdlog_errno(LOG_WARNING, "sysconf(_SC_OPEN_MAX) failed");
1885455Sdg		pjdlog_fini();
1895455Sdg		maxfd = 16384;
1905455Sdg	}
1915455Sdg	for (fd = 0; fd <= maxfd; fd++) {
1928876Srgrimes		if (fstat(fd, &sb) == 0) {
1935455Sdg			isopen = true;
1945455Sdg			mode = sb.st_mode;
1951541Srgrimes		} else if (errno == EBADF) {
1965455Sdg			isopen = false;
1975455Sdg			mode = 0;
1981541Srgrimes		} else {
19933109Sdyson			(void)snprintf(msg, sizeof(msg),
20033109Sdyson			    "Unable to fstat descriptor %d: %s", fd,
20110548Sdyson			    strerror(errno));
202102382Salc			break;
2036585Sdg		}
20442957Sdillon		if (fd == STDIN_FILENO || fd == STDOUT_FILENO ||
20542957Sdillon		    fd == STDERR_FILENO) {
2061541Srgrimes			if (!isopen) {
2075455Sdg				(void)snprintf(msg, sizeof(msg),
2081541Srgrimes				    "Descriptor %d (%s) is closed, but should be open.",
2095455Sdg				    fd, (fd == STDIN_FILENO ? "stdin" :
2101541Srgrimes				    (fd == STDOUT_FILENO ? "stdout" : "stderr")));
21198226Salc				break;
2121541Srgrimes			}
2135455Sdg		} else if (fd == proto_descriptor(res->hr_event)) {
2141541Srgrimes			if (!isopen) {
2151541Srgrimes				(void)snprintf(msg, sizeof(msg),
2161541Srgrimes				    "Descriptor %d (event) is closed, but should be open.",
2171541Srgrimes				    fd);
2181541Srgrimes				break;
2191541Srgrimes			}
2201541Srgrimes			if (!S_ISSOCK(mode)) {
2211541Srgrimes				(void)snprintf(msg, sizeof(msg),
22242957Sdillon				    "Descriptor %d (event) is %s, but should be %s.",
22342957Sdillon				    fd, dtype2str(mode), dtype2str(S_IFSOCK));
2241541Srgrimes				break;
2258876Srgrimes			}
2265455Sdg		} else if (fd == proto_descriptor(res->hr_ctrl)) {
2275455Sdg			if (!isopen) {
22870480Salfred				(void)snprintf(msg, sizeof(msg),
2295455Sdg				    "Descriptor %d (ctrl) is closed, but should be open.",
2301541Srgrimes				    fd);
23171571Sjhb				break;
2321541Srgrimes			}
2331541Srgrimes			if (!S_ISSOCK(mode)) {
2341541Srgrimes				(void)snprintf(msg, sizeof(msg),
2351541Srgrimes				    "Descriptor %d (ctrl) is %s, but should be %s.",
2361541Srgrimes				    fd, dtype2str(mode), dtype2str(S_IFSOCK));
2371541Srgrimes				break;
2381541Srgrimes			}
2391541Srgrimes		} else if (res->hr_role == HAST_ROLE_PRIMARY &&
2401541Srgrimes		    fd == proto_descriptor(res->hr_conn)) {
2411541Srgrimes			if (!isopen) {
2421541Srgrimes				(void)snprintf(msg, sizeof(msg),
2431541Srgrimes				    "Descriptor %d (conn) is closed, but should be open.",
24470480Salfred				    fd);
2451541Srgrimes				break;
2461541Srgrimes			}
2478876Srgrimes			if (!S_ISSOCK(mode)) {
24832702Sdyson				(void)snprintf(msg, sizeof(msg),
24970478Salfred				    "Descriptor %d (conn) is %s, but should be %s.",
2505455Sdg				    fd, dtype2str(mode), dtype2str(S_IFSOCK));
25170478Salfred				break;
2521541Srgrimes			}
25370478Salfred		} else if (res->hr_role == HAST_ROLE_SECONDARY &&
2545455Sdg		    res->hr_conn != NULL &&
2551541Srgrimes		    fd == proto_descriptor(res->hr_conn)) {
25679224Sdillon			if (isopen) {
25776827Salfred				(void)snprintf(msg, sizeof(msg),
2581541Srgrimes				    "Descriptor %d (conn) is open, but should be closed.",
2591541Srgrimes				    fd);
2601541Srgrimes				break;
2611541Srgrimes			}
26213490Sdyson		} else if (res->hr_role == HAST_ROLE_SECONDARY &&
2631541Srgrimes		    fd == proto_descriptor(res->hr_remotein)) {
2641541Srgrimes			if (!isopen) {
2651541Srgrimes				(void)snprintf(msg, sizeof(msg),
2661541Srgrimes				    "Descriptor %d (remote in) is closed, but should be open.",
2671541Srgrimes				    fd);
26832702Sdyson				break;
2691541Srgrimes			}
2701541Srgrimes			if (!S_ISSOCK(mode)) {
27170478Salfred				(void)snprintf(msg, sizeof(msg),
2721541Srgrimes				    "Descriptor %d (remote in) is %s, but should be %s.",
2735455Sdg				    fd, dtype2str(mode), dtype2str(S_IFSOCK));
2741541Srgrimes				break;
2751541Srgrimes			}
2761541Srgrimes		} else if (res->hr_role == HAST_ROLE_SECONDARY &&
27742957Sdillon		    fd == proto_descriptor(res->hr_remoteout)) {
2781541Srgrimes			if (!isopen) {
27942957Sdillon				(void)snprintf(msg, sizeof(msg),
28042957Sdillon				    "Descriptor %d (remote out) is closed, but should be open.",
28142957Sdillon				    fd);
28242957Sdillon				break;
2831541Srgrimes			}
28442957Sdillon			if (!S_ISSOCK(mode)) {
28542957Sdillon				(void)snprintf(msg, sizeof(msg),
28642957Sdillon				    "Descriptor %d (remote out) is %s, but should be %s.",
2871541Srgrimes				    fd, dtype2str(mode), dtype2str(S_IFSOCK));
28842957Sdillon				break;
28942957Sdillon			}
29042957Sdillon		} else {
29142957Sdillon			if (isopen) {
29242957Sdillon				(void)snprintf(msg, sizeof(msg),
29342957Sdillon				    "Descriptor %d is open (%s), but should be closed.",
29442957Sdillon				    fd, dtype2str(mode));
29542957Sdillon				break;
29678592Sbmilekic			}
29778592Sbmilekic		}
29878592Sbmilekic	}
2991541Srgrimes	if (msg[0] != '\0') {
3001541Srgrimes		pjdlog_init(pjdlogmode);
30142957Sdillon		pjdlog_prefix_set("[%s] (%s) ", res->hr_name,
30270480Salfred		    role2str(res->hr_role));
30370480Salfred		PJDLOG_ABORT("%s", msg);
30442957Sdillon	}
3051541Srgrimes}
30670480Salfred
3075455Sdgstatic void
3085455Sdgchild_exit_log(unsigned int pid, int status)
3095455Sdg{
31098455Sjeff
3111541Srgrimes	if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
31279224Sdillon		pjdlog_debug(1, "Worker process exited gracefully (pid=%u).",
31379224Sdillon		    pid);
3141541Srgrimes	} else if (WIFSIGNALED(status)) {
3151541Srgrimes		pjdlog_error("Worker process killed (pid=%u, signal=%d).",
3161541Srgrimes		    pid, WTERMSIG(status));
3171541Srgrimes	} else {
3185455Sdg		pjdlog_error("Worker process exited ungracefully (pid=%u, exitcode=%d).",
3195455Sdg		    pid, WIFEXITED(status) ? WEXITSTATUS(status) : -1);
3205455Sdg	}
3211541Srgrimes}
3221541Srgrimes
32333758Sdysonstatic void
3241541Srgrimeschild_exit(void)
32578592Sbmilekic{
32687157Sluigi	struct hast_resource *res;
32787157Sluigi	int status;
32887157Sluigi	pid_t pid;
32987157Sluigi
33087157Sluigi	while ((pid = wait3(&status, WNOHANG, NULL)) > 0) {
33187157Sluigi		/* Find resource related to the process that just exited. */
33287157Sluigi		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
33376827Salfred			if (pid == res->hr_workerpid)
3347066Sdg				break;
33542957Sdillon		}
33648409Speter		if (res == NULL) {
33748409Speter			/*
33876827Salfred			 * This can happen when new connection arrives and we
3391541Srgrimes			 * cancel child responsible for the old one or if this
34015367Sdyson			 * was hook which we executed.
3411541Srgrimes			 */
34213490Sdyson			hook_check_one(pid, status);
34313490Sdyson			continue;
3441541Srgrimes		}
34598455Sjeff		pjdlog_prefix_set("[%s] (%s) ", res->hr_name,
34698455Sjeff		    role2str(res->hr_role));
34798455Sjeff		child_exit_log(pid, status);
34898455Sjeff		child_cleanup(res);
34998455Sjeff		if (res->hr_role == HAST_ROLE_PRIMARY) {
35098455Sjeff			/*
35198455Sjeff			 * Restart child process if it was killed by signal
35298455Sjeff			 * or exited because of temporary problem.
35398455Sjeff			 */
35498455Sjeff			if (WIFSIGNALED(status) ||
35598455Sjeff			    (WIFEXITED(status) &&
35698455Sjeff			     WEXITSTATUS(status) == EX_TEMPFAIL)) {
35798455Sjeff				sleep(1);
35898455Sjeff				pjdlog_info("Restarting worker process.");
35998455Sjeff				hastd_primary(res);
36098455Sjeff			} else {
36198455Sjeff				res->hr_role = HAST_ROLE_INIT;
36298455Sjeff				pjdlog_info("Changing resource role back to %s.",
3631541Srgrimes				    role2str(res->hr_role));
36415809Sdyson			}
36598450Sjeff		}
36698450Sjeff		pjdlog_prefix_set("%s", "");
3671541Srgrimes	}
3685455Sdg}
3695455Sdg
3705455Sdgstatic bool
3711541Srgrimesresource_needs_restart(const struct hast_resource *res0,
3721541Srgrimes    const struct hast_resource *res1)
37342957Sdillon{
37444793Salc
37515809Sdyson	PJDLOG_ASSERT(strcmp(res0->hr_name, res1->hr_name) == 0);
37644793Salc
37715809Sdyson	if (strcmp(res0->hr_provname, res1->hr_provname) != 0)
37815809Sdyson		return (true);
37991946Stegge	if (strcmp(res0->hr_localpath, res1->hr_localpath) != 0)
38091946Stegge		return (true);
38191946Stegge	if (res0->hr_role == HAST_ROLE_INIT ||
38291946Stegge	    res0->hr_role == HAST_ROLE_SECONDARY) {
38391946Stegge		if (strcmp(res0->hr_remoteaddr, res1->hr_remoteaddr) != 0)
38491946Stegge			return (true);
38591946Stegge		if (strcmp(res0->hr_sourceaddr, res1->hr_sourceaddr) != 0)
38691946Stegge			return (true);
38791946Stegge		if (res0->hr_replication != res1->hr_replication)
38891946Stegge			return (true);
389100796Salc		if (res0->hr_checksum != res1->hr_checksum)
39091946Stegge			return (true);
391100796Salc		if (res0->hr_compression != res1->hr_compression)
39291946Stegge			return (true);
3931541Srgrimes		if (res0->hr_timeout != res1->hr_timeout)
3941541Srgrimes			return (true);
39576827Salfred		if (strcmp(res0->hr_exec, res1->hr_exec) != 0)
3961541Srgrimes			return (true);
39798455Sjeff		/*
398102382Salc		 * When metaflush has changed we don't really need restart,
39938799Sdfr		 * but it is just easier this way.
4006585Sdg		 */
4011541Srgrimes		if (res0->hr_metaflush != res1->hr_metaflush)
4021541Srgrimes			return (true);
4031541Srgrimes	}
4045455Sdg	return (false);
4055455Sdg}
4065455Sdg
4075455Sdgstatic bool
4081541Srgrimesresource_needs_reload(const struct hast_resource *res0,
4091541Srgrimes    const struct hast_resource *res1)
4101541Srgrimes{
41144793Salc
4121541Srgrimes	PJDLOG_ASSERT(strcmp(res0->hr_name, res1->hr_name) == 0);
41344793Salc	PJDLOG_ASSERT(strcmp(res0->hr_provname, res1->hr_provname) == 0);
4141541Srgrimes	PJDLOG_ASSERT(strcmp(res0->hr_localpath, res1->hr_localpath) == 0);
41520993Sdyson
41620993Sdyson	if (res0->hr_role != HAST_ROLE_PRIMARY)
4171541Srgrimes		return (false);
4185455Sdg
4195455Sdg	if (strcmp(res0->hr_remoteaddr, res1->hr_remoteaddr) != 0)
4205455Sdg		return (true);
4211541Srgrimes	if (strcmp(res0->hr_sourceaddr, res1->hr_sourceaddr) != 0)
4221541Srgrimes		return (true);
42312767Sdyson	if (res0->hr_replication != res1->hr_replication)
42499985Salc		return (true);
42513490Sdyson	if (res0->hr_checksum != res1->hr_checksum)
42638799Sdfr		return (true);
42799985Salc	if (res0->hr_compression != res1->hr_compression)
42842957Sdillon		return (true);
42942957Sdillon	if (res0->hr_timeout != res1->hr_timeout)
43042957Sdillon		return (true);
43160755Speter	if (strcmp(res0->hr_exec, res1->hr_exec) != 0)
432101634Salc		return (true);
4331541Srgrimes	if (res0->hr_metaflush != res1->hr_metaflush)
4341541Srgrimes		return (true);
4351541Srgrimes	return (false);
4365455Sdg}
43776827Salfred
43876827Salfredstatic void
43976827Salfredresource_reload(const struct hast_resource *res)
4401541Srgrimes{
4411541Srgrimes	struct nv *nvin, *nvout;
4421541Srgrimes	int error;
44342957Sdillon
4441541Srgrimes	PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
4451541Srgrimes
4461541Srgrimes	nvout = nv_alloc();
4471541Srgrimes	nv_add_uint8(nvout, CONTROL_RELOAD, "cmd");
44842957Sdillon	nv_add_string(nvout, res->hr_remoteaddr, "remoteaddr");
4491541Srgrimes	nv_add_string(nvout, res->hr_sourceaddr, "sourceaddr");
4508876Srgrimes	nv_add_int32(nvout, (int32_t)res->hr_replication, "replication");
4515455Sdg	nv_add_int32(nvout, (int32_t)res->hr_checksum, "checksum");
4525455Sdg	nv_add_int32(nvout, (int32_t)res->hr_compression, "compression");
4535455Sdg	nv_add_int32(nvout, (int32_t)res->hr_timeout, "timeout");
4541541Srgrimes	nv_add_string(nvout, res->hr_exec, "exec");
4555455Sdg	nv_add_int32(nvout, (int32_t)res->hr_metaflush, "metaflush");
4561541Srgrimes	if (nv_error(nvout) != 0) {
4571541Srgrimes		nv_free(nvout);
4581541Srgrimes		pjdlog_error("Unable to allocate header for reload message.");
4591541Srgrimes		return;
4601541Srgrimes	}
4615455Sdg	if (hast_proto_send(res, res->hr_ctrl, nvout, NULL, 0) == -1) {
4625455Sdg		pjdlog_errno(LOG_ERR, "Unable to send reload message");
4631541Srgrimes		nv_free(nvout);
4641541Srgrimes		return;
46533758Sdyson	}
4661541Srgrimes	nv_free(nvout);
4671541Srgrimes
4681541Srgrimes	/* Receive response. */
4691541Srgrimes	if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) == -1) {
4701541Srgrimes		pjdlog_errno(LOG_ERR, "Unable to receive reload reply");
4711541Srgrimes		return;
47299754Salc	}
47399754Salc	error = nv_get_int16(nvin, "error");
4741541Srgrimes	nv_free(nvin);
47599754Salc	if (error != 0) {
4761541Srgrimes		pjdlog_common(LOG_ERR, 0, error, "Reload failed");
4771541Srgrimes		return;
4781541Srgrimes	}
4791541Srgrimes}
4801541Srgrimes
48142957Sdillonstatic void
4821541Srgrimeshastd_reload(void)
4839507Sdg{
4841541Srgrimes	struct hastd_config *newcfg;
4851541Srgrimes	struct hast_resource *nres, *cres, *tres;
4868876Srgrimes	struct hastd_listen *nlst, *clst;
4875455Sdg	struct pidfh *newpfh;
4885455Sdg	unsigned int nlisten;
4895455Sdg	uint8_t role;
4905455Sdg	pid_t otherpid;
4911541Srgrimes
49276827Salfred	pjdlog_info("Reloading configuration...");
4931541Srgrimes
4941541Srgrimes	newpfh = NULL;
49599754Salc
49699754Salc	newcfg = yy_config_parse(cfgpath, false);
49799754Salc	if (newcfg == NULL)
49899754Salc		goto failed;
4991541Srgrimes
5001541Srgrimes	/*
5011541Srgrimes	 * Check if control address has changed.
5021541Srgrimes	 */
50342957Sdillon	if (strcmp(cfg->hc_controladdr, newcfg->hc_controladdr) != 0) {
50442957Sdillon		if (proto_server(newcfg->hc_controladdr,
50542957Sdillon		    &newcfg->hc_controlconn) == -1) {
50642957Sdillon			pjdlog_errno(LOG_ERR,
50742957Sdillon			    "Unable to listen on control address %s",
50842957Sdillon			    newcfg->hc_controladdr);
5091541Srgrimes			goto failed;
5108876Srgrimes		}
5115455Sdg	}
5121541Srgrimes	/*
5131541Srgrimes	 * Check if any listen address has changed.
51470480Salfred	 */
5151541Srgrimes	nlisten = 0;
51632702Sdyson	TAILQ_FOREACH(nlst, &newcfg->hc_listen, hl_next) {
5171541Srgrimes		TAILQ_FOREACH(clst, &cfg->hc_listen, hl_next) {
5181541Srgrimes			if (strcmp(nlst->hl_addr, clst->hl_addr) == 0)
5191541Srgrimes				break;
52027899Sdyson		}
5215455Sdg		if (clst != NULL && clst->hl_conn != NULL) {
52213490Sdyson			pjdlog_info("Keep listening on address %s.",
5231541Srgrimes			    nlst->hl_addr);
5241541Srgrimes			nlst->hl_conn = clst->hl_conn;
5251541Srgrimes			nlisten++;
526		} else if (proto_server(nlst->hl_addr, &nlst->hl_conn) == 0) {
527			pjdlog_info("Listening on new address %s.",
528			    nlst->hl_addr);
529			nlisten++;
530		} else {
531			pjdlog_errno(LOG_WARNING,
532			    "Unable to listen on address %s", nlst->hl_addr);
533		}
534	}
535	if (nlisten == 0) {
536		pjdlog_error("No addresses to listen on.");
537		goto failed;
538	}
539	/*
540	 * Check if pidfile's path has changed.
541	 */
542	if (!foreground && pidfile == NULL &&
543	    strcmp(cfg->hc_pidfile, newcfg->hc_pidfile) != 0) {
544		newpfh = pidfile_open(newcfg->hc_pidfile, 0600, &otherpid);
545		if (newpfh == NULL) {
546			if (errno == EEXIST) {
547				pjdlog_errno(LOG_WARNING,
548				    "Another hastd is already running, pidfile: %s, pid: %jd.",
549				    newcfg->hc_pidfile, (intmax_t)otherpid);
550			} else {
551				pjdlog_errno(LOG_WARNING,
552				    "Unable to open or create pidfile %s",
553				    newcfg->hc_pidfile);
554			}
555		} else if (pidfile_write(newpfh) == -1) {
556			/* Write PID to a file. */
557			pjdlog_errno(LOG_WARNING,
558			    "Unable to write PID to file %s",
559			    newcfg->hc_pidfile);
560		} else {
561			pjdlog_debug(1, "PID stored in %s.",
562			    newcfg->hc_pidfile);
563		}
564	}
565
566	/* No failures from now on. */
567
568	/*
569	 * Switch to new control socket.
570	 */
571	if (newcfg->hc_controlconn != NULL) {
572		pjdlog_info("Control socket changed from %s to %s.",
573		    cfg->hc_controladdr, newcfg->hc_controladdr);
574		proto_close(cfg->hc_controlconn);
575		cfg->hc_controlconn = newcfg->hc_controlconn;
576		newcfg->hc_controlconn = NULL;
577		strlcpy(cfg->hc_controladdr, newcfg->hc_controladdr,
578		    sizeof(cfg->hc_controladdr));
579	}
580	/*
581	 * Switch to new pidfile.
582	 */
583	if (newpfh != NULL) {
584		pjdlog_info("Pidfile changed from %s to %s.", cfg->hc_pidfile,
585		    newcfg->hc_pidfile);
586		(void)pidfile_remove(pfh);
587		pfh = newpfh;
588		(void)strlcpy(cfg->hc_pidfile, newcfg->hc_pidfile,
589		    sizeof(cfg->hc_pidfile));
590	}
591	/*
592	 * Switch to new listen addresses. Close all that were removed.
593	 */
594	while ((clst = TAILQ_FIRST(&cfg->hc_listen)) != NULL) {
595		TAILQ_FOREACH(nlst, &newcfg->hc_listen, hl_next) {
596			if (strcmp(nlst->hl_addr, clst->hl_addr) == 0)
597				break;
598		}
599		if (nlst == NULL && clst->hl_conn != NULL) {
600			proto_close(clst->hl_conn);
601			pjdlog_info("No longer listening on address %s.",
602			    clst->hl_addr);
603		}
604		TAILQ_REMOVE(&cfg->hc_listen, clst, hl_next);
605		free(clst);
606	}
607	TAILQ_CONCAT(&cfg->hc_listen, &newcfg->hc_listen, hl_next);
608
609	/*
610	 * Stop and remove resources that were removed from the configuration.
611	 */
612	TAILQ_FOREACH_SAFE(cres, &cfg->hc_resources, hr_next, tres) {
613		TAILQ_FOREACH(nres, &newcfg->hc_resources, hr_next) {
614			if (strcmp(cres->hr_name, nres->hr_name) == 0)
615				break;
616		}
617		if (nres == NULL) {
618			control_set_role(cres, HAST_ROLE_INIT);
619			TAILQ_REMOVE(&cfg->hc_resources, cres, hr_next);
620			pjdlog_info("Resource %s removed.", cres->hr_name);
621			free(cres);
622		}
623	}
624	/*
625	 * Move new resources to the current configuration.
626	 */
627	TAILQ_FOREACH_SAFE(nres, &newcfg->hc_resources, hr_next, tres) {
628		TAILQ_FOREACH(cres, &cfg->hc_resources, hr_next) {
629			if (strcmp(cres->hr_name, nres->hr_name) == 0)
630				break;
631		}
632		if (cres == NULL) {
633			TAILQ_REMOVE(&newcfg->hc_resources, nres, hr_next);
634			TAILQ_INSERT_TAIL(&cfg->hc_resources, nres, hr_next);
635			pjdlog_info("Resource %s added.", nres->hr_name);
636		}
637	}
638	/*
639	 * Deal with modified resources.
640	 * Depending on what has changed exactly we might want to perform
641	 * different actions.
642	 *
643	 * We do full resource restart in the following situations:
644	 * Resource role is INIT or SECONDARY.
645	 * Resource role is PRIMARY and path to local component or provider
646	 * name has changed.
647	 * In case of PRIMARY, the worker process will be killed and restarted,
648	 * which also means removing /dev/hast/<name> provider and
649	 * recreating it.
650	 *
651	 * We do just reload (send SIGHUP to worker process) if we act as
652	 * PRIMARY, but only if remote address, source address, replication
653	 * mode, timeout, execution path or metaflush has changed.
654	 * For those, there is no need to restart worker process.
655	 * If PRIMARY receives SIGHUP, it will reconnect if remote address or
656	 * source address has changed or it will set new timeout if only timeout
657	 * has changed or it will update metaflush if only metaflush has
658	 * changed.
659	 */
660	TAILQ_FOREACH_SAFE(nres, &newcfg->hc_resources, hr_next, tres) {
661		TAILQ_FOREACH(cres, &cfg->hc_resources, hr_next) {
662			if (strcmp(cres->hr_name, nres->hr_name) == 0)
663				break;
664		}
665		PJDLOG_ASSERT(cres != NULL);
666		if (resource_needs_restart(cres, nres)) {
667			pjdlog_info("Resource %s configuration was modified, restarting it.",
668			    cres->hr_name);
669			role = cres->hr_role;
670			control_set_role(cres, HAST_ROLE_INIT);
671			TAILQ_REMOVE(&cfg->hc_resources, cres, hr_next);
672			free(cres);
673			TAILQ_REMOVE(&newcfg->hc_resources, nres, hr_next);
674			TAILQ_INSERT_TAIL(&cfg->hc_resources, nres, hr_next);
675			control_set_role(nres, role);
676		} else if (resource_needs_reload(cres, nres)) {
677			pjdlog_info("Resource %s configuration was modified, reloading it.",
678			    cres->hr_name);
679			strlcpy(cres->hr_remoteaddr, nres->hr_remoteaddr,
680			    sizeof(cres->hr_remoteaddr));
681			strlcpy(cres->hr_sourceaddr, nres->hr_sourceaddr,
682			    sizeof(cres->hr_sourceaddr));
683			cres->hr_replication = nres->hr_replication;
684			cres->hr_checksum = nres->hr_checksum;
685			cres->hr_compression = nres->hr_compression;
686			cres->hr_timeout = nres->hr_timeout;
687			strlcpy(cres->hr_exec, nres->hr_exec,
688			    sizeof(cres->hr_exec));
689			cres->hr_metaflush = nres->hr_metaflush;
690			if (cres->hr_workerpid != 0)
691				resource_reload(cres);
692		}
693	}
694
695	yy_config_free(newcfg);
696	pjdlog_info("Configuration reloaded successfully.");
697	return;
698failed:
699	if (newcfg != NULL) {
700		if (newcfg->hc_controlconn != NULL)
701			proto_close(newcfg->hc_controlconn);
702		while ((nlst = TAILQ_FIRST(&newcfg->hc_listen)) != NULL) {
703			if (nlst->hl_conn != NULL) {
704				TAILQ_FOREACH(clst, &cfg->hc_listen, hl_next) {
705					if (strcmp(nlst->hl_addr,
706					    clst->hl_addr) == 0) {
707						break;
708					}
709				}
710				if (clst == NULL || clst->hl_conn == NULL)
711					proto_close(nlst->hl_conn);
712			}
713			TAILQ_REMOVE(&newcfg->hc_listen, nlst, hl_next);
714			free(nlst);
715		}
716		yy_config_free(newcfg);
717	}
718	if (newpfh != NULL)
719		(void)pidfile_remove(newpfh);
720	pjdlog_warning("Configuration not reloaded.");
721}
722
723static void
724terminate_workers(void)
725{
726	struct hast_resource *res;
727
728	pjdlog_info("Termination signal received, exiting.");
729	TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
730		if (res->hr_workerpid == 0)
731			continue;
732		pjdlog_info("Terminating worker process (resource=%s, role=%s, pid=%u).",
733		    res->hr_name, role2str(res->hr_role), res->hr_workerpid);
734		if (kill(res->hr_workerpid, SIGTERM) == 0)
735			continue;
736		pjdlog_errno(LOG_WARNING,
737		    "Unable to send signal to worker process (resource=%s, role=%s, pid=%u).",
738		    res->hr_name, role2str(res->hr_role), res->hr_workerpid);
739	}
740}
741
742static void
743listen_accept(struct hastd_listen *lst)
744{
745	struct hast_resource *res;
746	struct proto_conn *conn;
747	struct nv *nvin, *nvout, *nverr;
748	const char *resname;
749	const unsigned char *token;
750	char laddr[256], raddr[256];
751	uint8_t version;
752	size_t size;
753	pid_t pid;
754	int status;
755
756	proto_local_address(lst->hl_conn, laddr, sizeof(laddr));
757	pjdlog_debug(1, "Accepting connection to %s.", laddr);
758
759	if (proto_accept(lst->hl_conn, &conn) == -1) {
760		pjdlog_errno(LOG_ERR, "Unable to accept connection %s", laddr);
761		return;
762	}
763
764	proto_local_address(conn, laddr, sizeof(laddr));
765	proto_remote_address(conn, raddr, sizeof(raddr));
766	pjdlog_info("Connection from %s to %s.", raddr, laddr);
767
768	/* Error in setting timeout is not critical, but why should it fail? */
769	if (proto_timeout(conn, HAST_TIMEOUT) == -1)
770		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
771
772	nvin = nvout = nverr = NULL;
773
774	/*
775	 * Before receiving any data see if remote host have access to any
776	 * resource.
777	 */
778	TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
779		if (proto_address_match(conn, res->hr_remoteaddr))
780			break;
781	}
782	if (res == NULL) {
783		pjdlog_error("Client %s isn't known.", raddr);
784		goto close;
785	}
786	/* Ok, remote host can access at least one resource. */
787
788	if (hast_proto_recv_hdr(conn, &nvin) == -1) {
789		pjdlog_errno(LOG_ERR, "Unable to receive header from %s",
790		    raddr);
791		goto close;
792	}
793
794	resname = nv_get_string(nvin, "resource");
795	if (resname == NULL) {
796		pjdlog_error("No 'resource' field in the header received from %s.",
797		    raddr);
798		goto close;
799	}
800	pjdlog_debug(2, "%s: resource=%s", raddr, resname);
801	version = nv_get_uint8(nvin, "version");
802	pjdlog_debug(2, "%s: version=%hhu", raddr, version);
803	if (version == 0) {
804		/*
805		 * If no version is sent, it means this is protocol version 1.
806		 */
807		version = 1;
808	}
809	token = nv_get_uint8_array(nvin, &size, "token");
810	/*
811	 * NULL token means that this is first connection.
812	 */
813	if (token != NULL && size != sizeof(res->hr_token)) {
814		pjdlog_error("Received token of invalid size from %s (expected %zu, got %zu).",
815		    raddr, sizeof(res->hr_token), size);
816		goto close;
817	}
818
819	/*
820	 * From now on we want to send errors to the remote node.
821	 */
822	nverr = nv_alloc();
823
824	/* Find resource related to this connection. */
825	TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
826		if (strcmp(resname, res->hr_name) == 0)
827			break;
828	}
829	/* Have we found the resource? */
830	if (res == NULL) {
831		pjdlog_error("No resource '%s' as requested by %s.",
832		    resname, raddr);
833		nv_add_stringf(nverr, "errmsg", "Resource not configured.");
834		goto fail;
835	}
836
837	/* Now that we know resource name setup log prefix. */
838	pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
839
840	/* Does the remote host have access to this resource? */
841	if (!proto_address_match(conn, res->hr_remoteaddr)) {
842		pjdlog_error("Client %s has no access to the resource.", raddr);
843		nv_add_stringf(nverr, "errmsg", "No access to the resource.");
844		goto fail;
845	}
846	/* Is the resource marked as secondary? */
847	if (res->hr_role != HAST_ROLE_SECONDARY) {
848		pjdlog_warning("We act as %s for the resource and not as %s as requested by %s.",
849		    role2str(res->hr_role), role2str(HAST_ROLE_SECONDARY),
850		    raddr);
851		nv_add_stringf(nverr, "errmsg",
852		    "Remote node acts as %s for the resource and not as %s.",
853		    role2str(res->hr_role), role2str(HAST_ROLE_SECONDARY));
854		if (res->hr_role == HAST_ROLE_PRIMARY) {
855			/*
856			 * If we act as primary request the other side to wait
857			 * for us a bit, as we might be finishing cleanups.
858			 */
859			nv_add_uint8(nverr, 1, "wait");
860		}
861		goto fail;
862	}
863	/* Does token (if exists) match? */
864	if (token != NULL && memcmp(token, res->hr_token,
865	    sizeof(res->hr_token)) != 0) {
866		pjdlog_error("Token received from %s doesn't match.", raddr);
867		nv_add_stringf(nverr, "errmsg", "Token doesn't match.");
868		goto fail;
869	}
870	/*
871	 * If there is no token, but we have half-open connection
872	 * (only remotein) or full connection (worker process is running)
873	 * we have to cancel those and accept the new connection.
874	 */
875	if (token == NULL) {
876		PJDLOG_ASSERT(res->hr_remoteout == NULL);
877		pjdlog_debug(1, "Initial connection from %s.", raddr);
878		if (res->hr_workerpid != 0) {
879			PJDLOG_ASSERT(res->hr_remotein == NULL);
880			pjdlog_debug(1,
881			    "Worker process exists (pid=%u), stopping it.",
882			    (unsigned int)res->hr_workerpid);
883			/* Stop child process. */
884			if (kill(res->hr_workerpid, SIGINT) == -1) {
885				pjdlog_errno(LOG_ERR,
886				    "Unable to stop worker process (pid=%u)",
887				    (unsigned int)res->hr_workerpid);
888				/*
889				 * Other than logging the problem we
890				 * ignore it - nothing smart to do.
891				 */
892			}
893			/* Wait for it to exit. */
894			else if ((pid = waitpid(res->hr_workerpid,
895			    &status, 0)) != res->hr_workerpid) {
896				/* We can only log the problem. */
897				pjdlog_errno(LOG_ERR,
898				    "Waiting for worker process (pid=%u) failed",
899				    (unsigned int)res->hr_workerpid);
900			} else {
901				child_exit_log(res->hr_workerpid, status);
902			}
903			child_cleanup(res);
904		} else if (res->hr_remotein != NULL) {
905			char oaddr[256];
906
907			proto_remote_address(res->hr_remotein, oaddr,
908			    sizeof(oaddr));
909			pjdlog_debug(1,
910			    "Canceling half-open connection from %s on connection from %s.",
911			    oaddr, raddr);
912			proto_close(res->hr_remotein);
913			res->hr_remotein = NULL;
914		}
915	}
916
917	/*
918	 * Checks and cleanups are done.
919	 */
920
921	if (token == NULL) {
922		if (version > HAST_PROTO_VERSION) {
923			pjdlog_info("Remote protocol version %hhu is not supported, falling back to version %hhu.",
924			    version, (unsigned char)HAST_PROTO_VERSION);
925			version = HAST_PROTO_VERSION;
926		}
927		pjdlog_debug(1, "Negotiated protocol version %hhu.", version);
928		res->hr_version = version;
929		arc4random_buf(res->hr_token, sizeof(res->hr_token));
930		nvout = nv_alloc();
931		nv_add_uint8(nvout, version, "version");
932		nv_add_uint8_array(nvout, res->hr_token,
933		    sizeof(res->hr_token), "token");
934		if (nv_error(nvout) != 0) {
935			pjdlog_common(LOG_ERR, 0, nv_error(nvout),
936			    "Unable to prepare return header for %s", raddr);
937			nv_add_stringf(nverr, "errmsg",
938			    "Remote node was unable to prepare return header: %s.",
939			    strerror(nv_error(nvout)));
940			goto fail;
941		}
942		if (hast_proto_send(res, conn, nvout, NULL, 0) == -1) {
943			int error = errno;
944
945			pjdlog_errno(LOG_ERR, "Unable to send response to %s",
946			    raddr);
947			nv_add_stringf(nverr, "errmsg",
948			    "Remote node was unable to send response: %s.",
949			    strerror(error));
950			goto fail;
951		}
952		res->hr_remotein = conn;
953		pjdlog_debug(1, "Incoming connection from %s configured.",
954		    raddr);
955	} else {
956		res->hr_remoteout = conn;
957		pjdlog_debug(1, "Outgoing connection to %s configured.", raddr);
958		hastd_secondary(res, nvin);
959	}
960	nv_free(nvin);
961	nv_free(nvout);
962	nv_free(nverr);
963	pjdlog_prefix_set("%s", "");
964	return;
965fail:
966	if (nv_error(nverr) != 0) {
967		pjdlog_common(LOG_ERR, 0, nv_error(nverr),
968		    "Unable to prepare error header for %s", raddr);
969		goto close;
970	}
971	if (hast_proto_send(NULL, conn, nverr, NULL, 0) == -1) {
972		pjdlog_errno(LOG_ERR, "Unable to send error to %s", raddr);
973		goto close;
974	}
975close:
976	if (nvin != NULL)
977		nv_free(nvin);
978	if (nvout != NULL)
979		nv_free(nvout);
980	if (nverr != NULL)
981		nv_free(nverr);
982	proto_close(conn);
983	pjdlog_prefix_set("%s", "");
984}
985
986static void
987connection_migrate(struct hast_resource *res)
988{
989	struct proto_conn *conn;
990	int16_t val = 0;
991
992	pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
993
994	PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
995
996	if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) {
997		pjdlog_errno(LOG_WARNING,
998		    "Unable to receive connection command");
999		return;
1000	}
1001	if (proto_client(res->hr_sourceaddr[0] != '\0' ? res->hr_sourceaddr : NULL,
1002	    res->hr_remoteaddr, &conn) == -1) {
1003		val = errno;
1004		pjdlog_errno(LOG_WARNING,
1005		    "Unable to create outgoing connection to %s",
1006		    res->hr_remoteaddr);
1007		goto out;
1008	}
1009	if (proto_connect(conn, -1) == -1) {
1010		val = errno;
1011		pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
1012		    res->hr_remoteaddr);
1013		proto_close(conn);
1014		goto out;
1015	}
1016	val = 0;
1017out:
1018	if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) {
1019		pjdlog_errno(LOG_WARNING,
1020		    "Unable to send reply to connection request");
1021	}
1022	if (val == 0 && proto_connection_send(res->hr_conn, conn) == -1)
1023		pjdlog_errno(LOG_WARNING, "Unable to send connection");
1024
1025	pjdlog_prefix_set("%s", "");
1026}
1027
1028static void
1029check_signals(void)
1030{
1031	struct timespec sigtimeout;
1032	sigset_t mask;
1033	int signo;
1034
1035	sigtimeout.tv_sec = 0;
1036	sigtimeout.tv_nsec = 0;
1037
1038	PJDLOG_VERIFY(sigemptyset(&mask) == 0);
1039	PJDLOG_VERIFY(sigaddset(&mask, SIGHUP) == 0);
1040	PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
1041	PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
1042	PJDLOG_VERIFY(sigaddset(&mask, SIGCHLD) == 0);
1043
1044	while ((signo = sigtimedwait(&mask, NULL, &sigtimeout)) != -1) {
1045		switch (signo) {
1046		case SIGINT:
1047		case SIGTERM:
1048			sigexit_received = true;
1049			terminate_workers();
1050			proto_close(cfg->hc_controlconn);
1051			exit(EX_OK);
1052			break;
1053		case SIGCHLD:
1054			child_exit();
1055			break;
1056		case SIGHUP:
1057			hastd_reload();
1058			break;
1059		default:
1060			PJDLOG_ABORT("Unexpected signal (%d).", signo);
1061		}
1062	}
1063}
1064
1065static void
1066main_loop(void)
1067{
1068	struct hast_resource *res;
1069	struct hastd_listen *lst;
1070	struct timeval seltimeout;
1071	int fd, maxfd, ret;
1072	time_t lastcheck, now;
1073	fd_set rfds;
1074
1075	lastcheck = time(NULL);
1076	seltimeout.tv_sec = REPORT_INTERVAL;
1077	seltimeout.tv_usec = 0;
1078
1079	for (;;) {
1080		check_signals();
1081
1082		/* Setup descriptors for select(2). */
1083		FD_ZERO(&rfds);
1084		maxfd = fd = proto_descriptor(cfg->hc_controlconn);
1085		PJDLOG_ASSERT(fd >= 0);
1086		FD_SET(fd, &rfds);
1087		TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next) {
1088			if (lst->hl_conn == NULL)
1089				continue;
1090			fd = proto_descriptor(lst->hl_conn);
1091			PJDLOG_ASSERT(fd >= 0);
1092			FD_SET(fd, &rfds);
1093			maxfd = fd > maxfd ? fd : maxfd;
1094		}
1095		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
1096			if (res->hr_event == NULL)
1097				continue;
1098			fd = proto_descriptor(res->hr_event);
1099			PJDLOG_ASSERT(fd >= 0);
1100			FD_SET(fd, &rfds);
1101			maxfd = fd > maxfd ? fd : maxfd;
1102			if (res->hr_role == HAST_ROLE_PRIMARY) {
1103				/* Only primary workers asks for connections. */
1104				PJDLOG_ASSERT(res->hr_conn != NULL);
1105				fd = proto_descriptor(res->hr_conn);
1106				PJDLOG_ASSERT(fd >= 0);
1107				FD_SET(fd, &rfds);
1108				maxfd = fd > maxfd ? fd : maxfd;
1109			} else {
1110				PJDLOG_ASSERT(res->hr_conn == NULL);
1111			}
1112		}
1113
1114		PJDLOG_ASSERT(maxfd + 1 <= (int)FD_SETSIZE);
1115		ret = select(maxfd + 1, &rfds, NULL, NULL, &seltimeout);
1116		now = time(NULL);
1117		if (lastcheck + REPORT_INTERVAL <= now) {
1118			hook_check();
1119			lastcheck = now;
1120		}
1121		if (ret == 0) {
1122			/*
1123			 * select(2) timed out, so there should be no
1124			 * descriptors to check.
1125			 */
1126			continue;
1127		} else if (ret == -1) {
1128			if (errno == EINTR)
1129				continue;
1130			KEEP_ERRNO((void)pidfile_remove(pfh));
1131			pjdlog_exit(EX_OSERR, "select() failed");
1132		}
1133
1134		/*
1135		 * Check for signals before we do anything to update our
1136		 * info about terminated workers in the meantime.
1137		 */
1138		check_signals();
1139
1140		if (FD_ISSET(proto_descriptor(cfg->hc_controlconn), &rfds))
1141			control_handle(cfg);
1142		TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next) {
1143			if (lst->hl_conn == NULL)
1144				continue;
1145			if (FD_ISSET(proto_descriptor(lst->hl_conn), &rfds))
1146				listen_accept(lst);
1147		}
1148		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
1149			if (res->hr_event == NULL)
1150				continue;
1151			if (FD_ISSET(proto_descriptor(res->hr_event), &rfds)) {
1152				if (event_recv(res) == 0)
1153					continue;
1154				/* The worker process exited? */
1155				proto_close(res->hr_event);
1156				res->hr_event = NULL;
1157				if (res->hr_conn != NULL) {
1158					proto_close(res->hr_conn);
1159					res->hr_conn = NULL;
1160				}
1161				continue;
1162			}
1163			if (res->hr_role == HAST_ROLE_PRIMARY) {
1164				PJDLOG_ASSERT(res->hr_conn != NULL);
1165				if (FD_ISSET(proto_descriptor(res->hr_conn),
1166				    &rfds)) {
1167					connection_migrate(res);
1168				}
1169			} else {
1170				PJDLOG_ASSERT(res->hr_conn == NULL);
1171			}
1172		}
1173	}
1174}
1175
1176static void
1177dummy_sighandler(int sig __unused)
1178{
1179	/* Nothing to do. */
1180}
1181
1182int
1183main(int argc, char *argv[])
1184{
1185	struct hastd_listen *lst;
1186	pid_t otherpid;
1187	int debuglevel;
1188	sigset_t mask;
1189
1190	foreground = false;
1191	debuglevel = 0;
1192
1193	for (;;) {
1194		int ch;
1195
1196		ch = getopt(argc, argv, "c:dFhP:");
1197		if (ch == -1)
1198			break;
1199		switch (ch) {
1200		case 'c':
1201			cfgpath = optarg;
1202			break;
1203		case 'd':
1204			debuglevel++;
1205			break;
1206		case 'F':
1207			foreground = true;
1208			break;
1209		case 'P':
1210			pidfile = optarg;
1211			break;
1212		case 'h':
1213		default:
1214			usage();
1215		}
1216	}
1217	argc -= optind;
1218	argv += optind;
1219
1220	pjdlog_init(PJDLOG_MODE_STD);
1221	pjdlog_debug_set(debuglevel);
1222
1223	g_gate_load();
1224
1225	/*
1226	 * When path to the configuration file is relative, obtain full path,
1227	 * so we can always find the file, even after daemonizing and changing
1228	 * working directory to /.
1229	 */
1230	if (cfgpath[0] != '/') {
1231		const char *newcfgpath;
1232
1233		newcfgpath = realpath(cfgpath, NULL);
1234		if (newcfgpath == NULL) {
1235			pjdlog_exit(EX_CONFIG,
1236			    "Unable to obtain full path of %s", cfgpath);
1237		}
1238		cfgpath = newcfgpath;
1239	}
1240
1241	cfg = yy_config_parse(cfgpath, true);
1242	PJDLOG_ASSERT(cfg != NULL);
1243
1244	if (pidfile != NULL) {
1245		if (strlcpy(cfg->hc_pidfile, pidfile,
1246		    sizeof(cfg->hc_pidfile)) >= sizeof(cfg->hc_pidfile)) {
1247			pjdlog_exitx(EX_CONFIG, "Pidfile path is too long.");
1248		}
1249	}
1250
1251	if (pidfile != NULL || !foreground) {
1252		pfh = pidfile_open(cfg->hc_pidfile, 0600, &otherpid);
1253		if (pfh == NULL) {
1254			if (errno == EEXIST) {
1255				pjdlog_exitx(EX_TEMPFAIL,
1256				    "Another hastd is already running, pidfile: %s, pid: %jd.",
1257				    cfg->hc_pidfile, (intmax_t)otherpid);
1258			}
1259			/*
1260			 * If we cannot create pidfile for other reasons,
1261			 * only warn.
1262			 */
1263			pjdlog_errno(LOG_WARNING,
1264			    "Unable to open or create pidfile %s",
1265			    cfg->hc_pidfile);
1266		}
1267	}
1268
1269	/*
1270	 * Restore default actions for interesting signals in case parent
1271	 * process (like init(8)) decided to ignore some of them (like SIGHUP).
1272	 */
1273	PJDLOG_VERIFY(signal(SIGHUP, SIG_DFL) != SIG_ERR);
1274	PJDLOG_VERIFY(signal(SIGINT, SIG_DFL) != SIG_ERR);
1275	PJDLOG_VERIFY(signal(SIGTERM, SIG_DFL) != SIG_ERR);
1276	/*
1277	 * Because SIGCHLD is ignored by default, setup dummy handler for it,
1278	 * so we can mask it.
1279	 */
1280	PJDLOG_VERIFY(signal(SIGCHLD, dummy_sighandler) != SIG_ERR);
1281
1282	PJDLOG_VERIFY(sigemptyset(&mask) == 0);
1283	PJDLOG_VERIFY(sigaddset(&mask, SIGHUP) == 0);
1284	PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
1285	PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
1286	PJDLOG_VERIFY(sigaddset(&mask, SIGCHLD) == 0);
1287	PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
1288
1289	/* Listen on control address. */
1290	if (proto_server(cfg->hc_controladdr, &cfg->hc_controlconn) == -1) {
1291		KEEP_ERRNO((void)pidfile_remove(pfh));
1292		pjdlog_exit(EX_OSERR, "Unable to listen on control address %s",
1293		    cfg->hc_controladdr);
1294	}
1295	/* Listen for remote connections. */
1296	TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next) {
1297		if (proto_server(lst->hl_addr, &lst->hl_conn) == -1) {
1298			KEEP_ERRNO((void)pidfile_remove(pfh));
1299			pjdlog_exit(EX_OSERR, "Unable to listen on address %s",
1300			    lst->hl_addr);
1301		}
1302	}
1303
1304	if (!foreground) {
1305		if (daemon(0, 0) == -1) {
1306			KEEP_ERRNO((void)pidfile_remove(pfh));
1307			pjdlog_exit(EX_OSERR, "Unable to daemonize");
1308		}
1309
1310		/* Start logging to syslog. */
1311		pjdlog_mode_set(PJDLOG_MODE_SYSLOG);
1312	}
1313	if (pidfile != NULL || !foreground) {
1314		/* Write PID to a file. */
1315		if (pidfile_write(pfh) == -1) {
1316			pjdlog_errno(LOG_WARNING,
1317			    "Unable to write PID to a file %s",
1318			    cfg->hc_pidfile);
1319		} else {
1320			pjdlog_debug(1, "PID stored in %s.", cfg->hc_pidfile);
1321		}
1322	}
1323
1324	pjdlog_info("Started successfully, running protocol version %d.",
1325	    HAST_PROTO_VERSION);
1326
1327	pjdlog_debug(1, "Listening on control address %s.",
1328	    cfg->hc_controladdr);
1329	TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next)
1330		pjdlog_info("Listening on address %s.", lst->hl_addr);
1331
1332	hook_init();
1333
1334	main_loop();
1335
1336	exit(0);
1337}
1338