1204076Spjd/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
4204076Spjd * Copyright (c) 2009-2010 The FreeBSD Foundation
5219351Spjd * Copyright (c) 2010-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
6204076Spjd * All rights reserved.
7204076Spjd *
8204076Spjd * This software was developed by Pawel Jakub Dawidek under sponsorship from
9204076Spjd * the FreeBSD Foundation.
10204076Spjd *
11204076Spjd * Redistribution and use in source and binary forms, with or without
12204076Spjd * modification, are permitted provided that the following conditions
13204076Spjd * are met:
14204076Spjd * 1. Redistributions of source code must retain the above copyright
15204076Spjd *    notice, this list of conditions and the following disclaimer.
16204076Spjd * 2. Redistributions in binary form must reproduce the above copyright
17204076Spjd *    notice, this list of conditions and the following disclaimer in the
18204076Spjd *    documentation and/or other materials provided with the distribution.
19204076Spjd *
20204076Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21204076Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22204076Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23204076Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24204076Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25204076Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26204076Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27204076Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28204076Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29204076Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30204076Spjd * SUCH DAMAGE.
31204076Spjd */
32204076Spjd
33204076Spjd#include <sys/cdefs.h>
34204076Spjd__FBSDID("$FreeBSD: stable/11/sbin/hastd/hastd.c 368759 2020-12-18 12:24:33Z eugen $");
35204076Spjd
36204076Spjd#include <sys/param.h>
37204076Spjd#include <sys/linker.h>
38204076Spjd#include <sys/module.h>
39218044Spjd#include <sys/stat.h>
40204076Spjd#include <sys/wait.h>
41204076Spjd
42204076Spjd#include <err.h>
43204076Spjd#include <errno.h>
44204076Spjd#include <libutil.h>
45204076Spjd#include <signal.h>
46204076Spjd#include <stdbool.h>
47204076Spjd#include <stdio.h>
48204076Spjd#include <stdlib.h>
49204076Spjd#include <string.h>
50204076Spjd#include <sysexits.h>
51219813Spjd#include <time.h>
52204076Spjd#include <unistd.h>
53204076Spjd
54204076Spjd#include <activemap.h>
55204076Spjd#include <pjdlog.h>
56204076Spjd
57204076Spjd#include "control.h"
58212038Spjd#include "event.h"
59204076Spjd#include "hast.h"
60204076Spjd#include "hast_proto.h"
61204076Spjd#include "hastd.h"
62211977Spjd#include "hooks.h"
63204076Spjd#include "subr.h"
64204076Spjd
65204076Spjd/* Path to configuration file. */
66210886Spjdconst char *cfgpath = HAST_CONFIG;
67204076Spjd/* Hastd configuration. */
68204076Spjdstatic struct hastd_config *cfg;
69204076Spjd/* Was SIGINT or SIGTERM signal received? */
70204076Spjdbool sigexit_received = false;
71233679Strociny/* Path to pidfile. */
72233679Strocinystatic const char *pidfile;
73246922Spjd/* Pidfile handle. */
74204076Spjdstruct pidfh *pfh;
75229944Spjd/* Do we run in foreground? */
76229944Spjdstatic bool foreground;
77204076Spjd
78211977Spjd/* How often check for hooks running for too long. */
79213430Spjd#define	REPORT_INTERVAL	5
80211977Spjd
81204076Spjdstatic void
82204076Spjdusage(void)
83204076Spjd{
84204076Spjd
85204076Spjd	errx(EX_USAGE, "[-dFh] [-c config] [-P pidfile]");
86204076Spjd}
87204076Spjd
88204076Spjdstatic void
89204076Spjdg_gate_load(void)
90204076Spjd{
91204076Spjd
92204076Spjd	if (modfind("g_gate") == -1) {
93204076Spjd		/* Not present in kernel, try loading it. */
94204076Spjd		if (kldload("geom_gate") == -1 || modfind("g_gate") == -1) {
95204076Spjd			if (errno != EEXIST) {
96204076Spjd				pjdlog_exit(EX_OSERR,
97204076Spjd				    "Unable to load geom_gate module");
98204076Spjd			}
99204076Spjd		}
100204076Spjd	}
101204076Spjd}
102204076Spjd
103218041Spjdvoid
104218041Spjddescriptors_cleanup(struct hast_resource *res)
105218041Spjd{
106230457Spjd	struct hast_resource *tres, *tmres;
107222108Spjd	struct hastd_listen *lst;
108218041Spjd
109230457Spjd	TAILQ_FOREACH_SAFE(tres, &cfg->hc_resources, hr_next, tmres) {
110218041Spjd		if (tres == res) {
111218041Spjd			PJDLOG_VERIFY(res->hr_role == HAST_ROLE_SECONDARY ||
112218041Spjd			    (res->hr_remotein == NULL &&
113218041Spjd			     res->hr_remoteout == NULL));
114218041Spjd			continue;
115218041Spjd		}
116218041Spjd		if (tres->hr_remotein != NULL)
117218041Spjd			proto_close(tres->hr_remotein);
118218041Spjd		if (tres->hr_remoteout != NULL)
119218041Spjd			proto_close(tres->hr_remoteout);
120218370Spjd		if (tres->hr_ctrl != NULL)
121218370Spjd			proto_close(tres->hr_ctrl);
122218370Spjd		if (tres->hr_event != NULL)
123218370Spjd			proto_close(tres->hr_event);
124218370Spjd		if (tres->hr_conn != NULL)
125218370Spjd			proto_close(tres->hr_conn);
126230457Spjd		TAILQ_REMOVE(&cfg->hc_resources, tres, hr_next);
127230457Spjd		free(tres);
128218041Spjd	}
129218041Spjd	if (cfg->hc_controlin != NULL)
130218041Spjd		proto_close(cfg->hc_controlin);
131218041Spjd	proto_close(cfg->hc_controlconn);
132230457Spjd	while ((lst = TAILQ_FIRST(&cfg->hc_listen)) != NULL) {
133230457Spjd		TAILQ_REMOVE(&cfg->hc_listen, lst, hl_next);
134222108Spjd		if (lst->hl_conn != NULL)
135222108Spjd			proto_close(lst->hl_conn);
136230457Spjd		free(lst);
137222108Spjd	}
138218041Spjd	(void)pidfile_close(pfh);
139218041Spjd	hook_fini();
140218041Spjd	pjdlog_fini();
141218041Spjd}
142218041Spjd
143218044Spjdstatic const char *
144218044Spjddtype2str(mode_t mode)
145218044Spjd{
146218044Spjd
147218044Spjd	if (S_ISBLK(mode))
148218044Spjd		return ("block device");
149219864Spjd	else if (S_ISCHR(mode))
150218044Spjd		return ("character device");
151219864Spjd	else if (S_ISDIR(mode))
152218044Spjd		return ("directory");
153218044Spjd	else if (S_ISFIFO(mode))
154218044Spjd		return ("pipe or FIFO");
155219864Spjd	else if (S_ISLNK(mode))
156218044Spjd		return ("symbolic link");
157219864Spjd	else if (S_ISREG(mode))
158218044Spjd		return ("regular file");
159218044Spjd	else if (S_ISSOCK(mode))
160218044Spjd		return ("socket");
161219864Spjd	else if (S_ISWHT(mode))
162218044Spjd		return ("whiteout");
163218044Spjd	else
164218044Spjd		return ("unknown");
165218044Spjd}
166218044Spjd
167218044Spjdvoid
168218044Spjddescriptors_assert(const struct hast_resource *res, int pjdlogmode)
169218044Spjd{
170218044Spjd	char msg[256];
171218044Spjd	struct stat sb;
172218044Spjd	long maxfd;
173218044Spjd	bool isopen;
174218044Spjd	mode_t mode;
175218044Spjd	int fd;
176218044Spjd
177218044Spjd	/*
178218044Spjd	 * At this point descriptor to syslog socket is closed, so if we want
179218044Spjd	 * to log assertion message, we have to first store it in 'msg' local
180218044Spjd	 * buffer and then open syslog socket and log it.
181218044Spjd	 */
182218044Spjd	msg[0] = '\0';
183218044Spjd
184218044Spjd	maxfd = sysconf(_SC_OPEN_MAX);
185229945Spjd	if (maxfd == -1) {
186218373Spjd		pjdlog_init(pjdlogmode);
187218373Spjd		pjdlog_prefix_set("[%s] (%s) ", res->hr_name,
188218373Spjd		    role2str(res->hr_role));
189218044Spjd		pjdlog_errno(LOG_WARNING, "sysconf(_SC_OPEN_MAX) failed");
190218373Spjd		pjdlog_fini();
191218044Spjd		maxfd = 16384;
192218044Spjd	}
193218044Spjd	for (fd = 0; fd <= maxfd; fd++) {
194218044Spjd		if (fstat(fd, &sb) == 0) {
195218044Spjd			isopen = true;
196218044Spjd			mode = sb.st_mode;
197218044Spjd		} else if (errno == EBADF) {
198218044Spjd			isopen = false;
199218044Spjd			mode = 0;
200218044Spjd		} else {
201218375Spjd			(void)snprintf(msg, sizeof(msg),
202218044Spjd			    "Unable to fstat descriptor %d: %s", fd,
203218044Spjd			    strerror(errno));
204218374Spjd			break;
205218044Spjd		}
206218044Spjd		if (fd == STDIN_FILENO || fd == STDOUT_FILENO ||
207218044Spjd		    fd == STDERR_FILENO) {
208218044Spjd			if (!isopen) {
209218375Spjd				(void)snprintf(msg, sizeof(msg),
210218044Spjd				    "Descriptor %d (%s) is closed, but should be open.",
211218044Spjd				    fd, (fd == STDIN_FILENO ? "stdin" :
212218044Spjd				    (fd == STDOUT_FILENO ? "stdout" : "stderr")));
213218044Spjd				break;
214218044Spjd			}
215218044Spjd		} else if (fd == proto_descriptor(res->hr_event)) {
216218044Spjd			if (!isopen) {
217218375Spjd				(void)snprintf(msg, sizeof(msg),
218218044Spjd				    "Descriptor %d (event) is closed, but should be open.",
219218044Spjd				    fd);
220218044Spjd				break;
221218044Spjd			}
222218044Spjd			if (!S_ISSOCK(mode)) {
223218375Spjd				(void)snprintf(msg, sizeof(msg),
224218044Spjd				    "Descriptor %d (event) is %s, but should be %s.",
225218044Spjd				    fd, dtype2str(mode), dtype2str(S_IFSOCK));
226218044Spjd				break;
227218044Spjd			}
228218044Spjd		} else if (fd == proto_descriptor(res->hr_ctrl)) {
229218044Spjd			if (!isopen) {
230218375Spjd				(void)snprintf(msg, sizeof(msg),
231218044Spjd				    "Descriptor %d (ctrl) is closed, but should be open.",
232218044Spjd				    fd);
233218044Spjd				break;
234218044Spjd			}
235218044Spjd			if (!S_ISSOCK(mode)) {
236218375Spjd				(void)snprintf(msg, sizeof(msg),
237218044Spjd				    "Descriptor %d (ctrl) is %s, but should be %s.",
238218044Spjd				    fd, dtype2str(mode), dtype2str(S_IFSOCK));
239218044Spjd				break;
240218044Spjd			}
241219900Spjd		} else if (res->hr_role == HAST_ROLE_PRIMARY &&
242219900Spjd		    fd == proto_descriptor(res->hr_conn)) {
243218218Spjd			if (!isopen) {
244218375Spjd				(void)snprintf(msg, sizeof(msg),
245218218Spjd				    "Descriptor %d (conn) is closed, but should be open.",
246218218Spjd				    fd);
247218218Spjd				break;
248218218Spjd			}
249218218Spjd			if (!S_ISSOCK(mode)) {
250218375Spjd				(void)snprintf(msg, sizeof(msg),
251218218Spjd				    "Descriptor %d (conn) is %s, but should be %s.",
252218218Spjd				    fd, dtype2str(mode), dtype2str(S_IFSOCK));
253218218Spjd				break;
254218218Spjd			}
255218044Spjd		} else if (res->hr_role == HAST_ROLE_SECONDARY &&
256219900Spjd		    res->hr_conn != NULL &&
257219900Spjd		    fd == proto_descriptor(res->hr_conn)) {
258219900Spjd			if (isopen) {
259219900Spjd				(void)snprintf(msg, sizeof(msg),
260219900Spjd				    "Descriptor %d (conn) is open, but should be closed.",
261219900Spjd				    fd);
262219900Spjd				break;
263219900Spjd			}
264219900Spjd		} else if (res->hr_role == HAST_ROLE_SECONDARY &&
265218044Spjd		    fd == proto_descriptor(res->hr_remotein)) {
266218044Spjd			if (!isopen) {
267218375Spjd				(void)snprintf(msg, sizeof(msg),
268218044Spjd				    "Descriptor %d (remote in) is closed, but should be open.",
269218044Spjd				    fd);
270218044Spjd				break;
271218044Spjd			}
272218044Spjd			if (!S_ISSOCK(mode)) {
273218375Spjd				(void)snprintf(msg, sizeof(msg),
274218044Spjd				    "Descriptor %d (remote in) is %s, but should be %s.",
275218044Spjd				    fd, dtype2str(mode), dtype2str(S_IFSOCK));
276218044Spjd				break;
277218044Spjd			}
278218044Spjd		} else if (res->hr_role == HAST_ROLE_SECONDARY &&
279218044Spjd		    fd == proto_descriptor(res->hr_remoteout)) {
280218044Spjd			if (!isopen) {
281218375Spjd				(void)snprintf(msg, sizeof(msg),
282218044Spjd				    "Descriptor %d (remote out) is closed, but should be open.",
283218044Spjd				    fd);
284218044Spjd				break;
285218044Spjd			}
286218044Spjd			if (!S_ISSOCK(mode)) {
287218375Spjd				(void)snprintf(msg, sizeof(msg),
288218044Spjd				    "Descriptor %d (remote out) is %s, but should be %s.",
289218044Spjd				    fd, dtype2str(mode), dtype2str(S_IFSOCK));
290218044Spjd				break;
291218044Spjd			}
292218044Spjd		} else {
293218044Spjd			if (isopen) {
294218375Spjd				(void)snprintf(msg, sizeof(msg),
295218044Spjd				    "Descriptor %d is open (%s), but should be closed.",
296218044Spjd				    fd, dtype2str(mode));
297218044Spjd				break;
298218044Spjd			}
299218044Spjd		}
300218044Spjd	}
301218044Spjd	if (msg[0] != '\0') {
302218044Spjd		pjdlog_init(pjdlogmode);
303218044Spjd		pjdlog_prefix_set("[%s] (%s) ", res->hr_name,
304218044Spjd		    role2str(res->hr_role));
305218044Spjd		PJDLOG_ABORT("%s", msg);
306218044Spjd	}
307218044Spjd}
308218044Spjd
309204076Spjdstatic void
310207372Spjdchild_exit_log(unsigned int pid, int status)
311207372Spjd{
312207372Spjd
313207372Spjd	if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
314207372Spjd		pjdlog_debug(1, "Worker process exited gracefully (pid=%u).",
315207372Spjd		    pid);
316207372Spjd	} else if (WIFSIGNALED(status)) {
317207372Spjd		pjdlog_error("Worker process killed (pid=%u, signal=%d).",
318207372Spjd		    pid, WTERMSIG(status));
319207372Spjd	} else {
320207372Spjd		pjdlog_error("Worker process exited ungracefully (pid=%u, exitcode=%d).",
321207372Spjd		    pid, WIFEXITED(status) ? WEXITSTATUS(status) : -1);
322207372Spjd	}
323207372Spjd}
324207372Spjd
325207372Spjdstatic void
326204076Spjdchild_exit(void)
327204076Spjd{
328204076Spjd	struct hast_resource *res;
329204076Spjd	int status;
330204076Spjd	pid_t pid;
331204076Spjd
332204076Spjd	while ((pid = wait3(&status, WNOHANG, NULL)) > 0) {
333204076Spjd		/* Find resource related to the process that just exited. */
334204076Spjd		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
335204076Spjd			if (pid == res->hr_workerpid)
336204076Spjd				break;
337204076Spjd		}
338204076Spjd		if (res == NULL) {
339204076Spjd			/*
340204076Spjd			 * This can happen when new connection arrives and we
341211977Spjd			 * cancel child responsible for the old one or if this
342211977Spjd			 * was hook which we executed.
343204076Spjd			 */
344211977Spjd			hook_check_one(pid, status);
345204076Spjd			continue;
346204076Spjd		}
347204076Spjd		pjdlog_prefix_set("[%s] (%s) ", res->hr_name,
348204076Spjd		    role2str(res->hr_role));
349207372Spjd		child_exit_log(pid, status);
350213006Spjd		child_cleanup(res);
351204076Spjd		if (res->hr_role == HAST_ROLE_PRIMARY) {
352207372Spjd			/*
353207372Spjd			 * Restart child process if it was killed by signal
354207372Spjd			 * or exited because of temporary problem.
355207372Spjd			 */
356207372Spjd			if (WIFSIGNALED(status) ||
357207372Spjd			    (WIFEXITED(status) &&
358207372Spjd			     WEXITSTATUS(status) == EX_TEMPFAIL)) {
359207348Spjd				sleep(1);
360207348Spjd				pjdlog_info("Restarting worker process.");
361207348Spjd				hastd_primary(res);
362207348Spjd			} else {
363207348Spjd				res->hr_role = HAST_ROLE_INIT;
364207348Spjd				pjdlog_info("Changing resource role back to %s.",
365207348Spjd				    role2str(res->hr_role));
366207348Spjd			}
367204076Spjd		}
368204076Spjd		pjdlog_prefix_set("%s", "");
369204076Spjd	}
370204076Spjd}
371204076Spjd
372210886Spjdstatic bool
373210886Spjdresource_needs_restart(const struct hast_resource *res0,
374210886Spjd    const struct hast_resource *res1)
375210886Spjd{
376210886Spjd
377218138Spjd	PJDLOG_ASSERT(strcmp(res0->hr_name, res1->hr_name) == 0);
378210886Spjd
379210886Spjd	if (strcmp(res0->hr_provname, res1->hr_provname) != 0)
380210886Spjd		return (true);
381210886Spjd	if (strcmp(res0->hr_localpath, res1->hr_localpath) != 0)
382210886Spjd		return (true);
383210886Spjd	if (res0->hr_role == HAST_ROLE_INIT ||
384210886Spjd	    res0->hr_role == HAST_ROLE_SECONDARY) {
385210886Spjd		if (strcmp(res0->hr_remoteaddr, res1->hr_remoteaddr) != 0)
386210886Spjd			return (true);
387219818Spjd		if (strcmp(res0->hr_sourceaddr, res1->hr_sourceaddr) != 0)
388219818Spjd			return (true);
389210886Spjd		if (res0->hr_replication != res1->hr_replication)
390210886Spjd			return (true);
391219351Spjd		if (res0->hr_checksum != res1->hr_checksum)
392219351Spjd			return (true);
393219354Spjd		if (res0->hr_compression != res1->hr_compression)
394219354Spjd			return (true);
395210886Spjd		if (res0->hr_timeout != res1->hr_timeout)
396210886Spjd			return (true);
397211886Spjd		if (strcmp(res0->hr_exec, res1->hr_exec) != 0)
398211886Spjd			return (true);
399225830Spjd		/*
400225830Spjd		 * When metaflush has changed we don't really need restart,
401225830Spjd		 * but it is just easier this way.
402225830Spjd		 */
403225830Spjd		if (res0->hr_metaflush != res1->hr_metaflush)
404225830Spjd			return (true);
405210886Spjd	}
406210886Spjd	return (false);
407210886Spjd}
408210886Spjd
409210886Spjdstatic bool
410210886Spjdresource_needs_reload(const struct hast_resource *res0,
411210886Spjd    const struct hast_resource *res1)
412210886Spjd{
413210886Spjd
414218138Spjd	PJDLOG_ASSERT(strcmp(res0->hr_name, res1->hr_name) == 0);
415218138Spjd	PJDLOG_ASSERT(strcmp(res0->hr_provname, res1->hr_provname) == 0);
416218138Spjd	PJDLOG_ASSERT(strcmp(res0->hr_localpath, res1->hr_localpath) == 0);
417210886Spjd
418210886Spjd	if (res0->hr_role != HAST_ROLE_PRIMARY)
419210886Spjd		return (false);
420210886Spjd
421210886Spjd	if (strcmp(res0->hr_remoteaddr, res1->hr_remoteaddr) != 0)
422210886Spjd		return (true);
423219818Spjd	if (strcmp(res0->hr_sourceaddr, res1->hr_sourceaddr) != 0)
424219818Spjd		return (true);
425210886Spjd	if (res0->hr_replication != res1->hr_replication)
426210886Spjd		return (true);
427219351Spjd	if (res0->hr_checksum != res1->hr_checksum)
428219351Spjd		return (true);
429219354Spjd	if (res0->hr_compression != res1->hr_compression)
430219354Spjd		return (true);
431210886Spjd	if (res0->hr_timeout != res1->hr_timeout)
432210886Spjd		return (true);
433211886Spjd	if (strcmp(res0->hr_exec, res1->hr_exec) != 0)
434211886Spjd		return (true);
435225830Spjd	if (res0->hr_metaflush != res1->hr_metaflush)
436225830Spjd		return (true);
437210886Spjd	return (false);
438210886Spjd}
439210886Spjd
440204076Spjdstatic void
441217784Spjdresource_reload(const struct hast_resource *res)
442217784Spjd{
443217784Spjd	struct nv *nvin, *nvout;
444217784Spjd	int error;
445217784Spjd
446218138Spjd	PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
447217784Spjd
448217784Spjd	nvout = nv_alloc();
449221076Strociny	nv_add_uint8(nvout, CONTROL_RELOAD, "cmd");
450217784Spjd	nv_add_string(nvout, res->hr_remoteaddr, "remoteaddr");
451219818Spjd	nv_add_string(nvout, res->hr_sourceaddr, "sourceaddr");
452217784Spjd	nv_add_int32(nvout, (int32_t)res->hr_replication, "replication");
453219351Spjd	nv_add_int32(nvout, (int32_t)res->hr_checksum, "checksum");
454219354Spjd	nv_add_int32(nvout, (int32_t)res->hr_compression, "compression");
455217784Spjd	nv_add_int32(nvout, (int32_t)res->hr_timeout, "timeout");
456217784Spjd	nv_add_string(nvout, res->hr_exec, "exec");
457225830Spjd	nv_add_int32(nvout, (int32_t)res->hr_metaflush, "metaflush");
458217784Spjd	if (nv_error(nvout) != 0) {
459217784Spjd		nv_free(nvout);
460217784Spjd		pjdlog_error("Unable to allocate header for reload message.");
461217784Spjd		return;
462217784Spjd	}
463229945Spjd	if (hast_proto_send(res, res->hr_ctrl, nvout, NULL, 0) == -1) {
464217784Spjd		pjdlog_errno(LOG_ERR, "Unable to send reload message");
465217784Spjd		nv_free(nvout);
466217784Spjd		return;
467217784Spjd	}
468217784Spjd	nv_free(nvout);
469217784Spjd
470217784Spjd	/* Receive response. */
471229945Spjd	if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) == -1) {
472217784Spjd		pjdlog_errno(LOG_ERR, "Unable to receive reload reply");
473217784Spjd		return;
474217784Spjd	}
475217784Spjd	error = nv_get_int16(nvin, "error");
476217784Spjd	nv_free(nvin);
477217784Spjd	if (error != 0) {
478217784Spjd		pjdlog_common(LOG_ERR, 0, error, "Reload failed");
479217784Spjd		return;
480217784Spjd	}
481217784Spjd}
482217784Spjd
483217784Spjdstatic void
484204076Spjdhastd_reload(void)
485204076Spjd{
486210886Spjd	struct hastd_config *newcfg;
487210886Spjd	struct hast_resource *nres, *cres, *tres;
488222108Spjd	struct hastd_listen *nlst, *clst;
489226463Spjd	struct pidfh *newpfh;
490222108Spjd	unsigned int nlisten;
491210886Spjd	uint8_t role;
492226463Spjd	pid_t otherpid;
493204076Spjd
494210886Spjd	pjdlog_info("Reloading configuration...");
495210886Spjd
496226463Spjd	newpfh = NULL;
497226463Spjd
498210886Spjd	newcfg = yy_config_parse(cfgpath, false);
499210886Spjd	if (newcfg == NULL)
500210886Spjd		goto failed;
501210886Spjd
502210886Spjd	/*
503210886Spjd	 * Check if control address has changed.
504210886Spjd	 */
505210886Spjd	if (strcmp(cfg->hc_controladdr, newcfg->hc_controladdr) != 0) {
506210886Spjd		if (proto_server(newcfg->hc_controladdr,
507229945Spjd		    &newcfg->hc_controlconn) == -1) {
508210886Spjd			pjdlog_errno(LOG_ERR,
509210886Spjd			    "Unable to listen on control address %s",
510210886Spjd			    newcfg->hc_controladdr);
511210886Spjd			goto failed;
512210886Spjd		}
513210886Spjd	}
514210886Spjd	/*
515222108Spjd	 * Check if any listen address has changed.
516210886Spjd	 */
517222108Spjd	nlisten = 0;
518222108Spjd	TAILQ_FOREACH(nlst, &newcfg->hc_listen, hl_next) {
519222108Spjd		TAILQ_FOREACH(clst, &cfg->hc_listen, hl_next) {
520222108Spjd			if (strcmp(nlst->hl_addr, clst->hl_addr) == 0)
521222108Spjd				break;
522210886Spjd		}
523222108Spjd		if (clst != NULL && clst->hl_conn != NULL) {
524222108Spjd			pjdlog_info("Keep listening on address %s.",
525222108Spjd			    nlst->hl_addr);
526222108Spjd			nlst->hl_conn = clst->hl_conn;
527222108Spjd			nlisten++;
528222108Spjd		} else if (proto_server(nlst->hl_addr, &nlst->hl_conn) == 0) {
529222108Spjd			pjdlog_info("Listening on new address %s.",
530222108Spjd			    nlst->hl_addr);
531222108Spjd			nlisten++;
532222108Spjd		} else {
533222108Spjd			pjdlog_errno(LOG_WARNING,
534222108Spjd			    "Unable to listen on address %s", nlst->hl_addr);
535222108Spjd		}
536210886Spjd	}
537222108Spjd	if (nlisten == 0) {
538222108Spjd		pjdlog_error("No addresses to listen on.");
539222108Spjd		goto failed;
540222108Spjd	}
541226463Spjd	/*
542226463Spjd	 * Check if pidfile's path has changed.
543226463Spjd	 */
544233679Strociny	if (!foreground && pidfile == NULL &&
545233679Strociny	    strcmp(cfg->hc_pidfile, newcfg->hc_pidfile) != 0) {
546226463Spjd		newpfh = pidfile_open(newcfg->hc_pidfile, 0600, &otherpid);
547226463Spjd		if (newpfh == NULL) {
548226463Spjd			if (errno == EEXIST) {
549226463Spjd				pjdlog_errno(LOG_WARNING,
550226463Spjd				    "Another hastd is already running, pidfile: %s, pid: %jd.",
551226463Spjd				    newcfg->hc_pidfile, (intmax_t)otherpid);
552226463Spjd			} else {
553226463Spjd				pjdlog_errno(LOG_WARNING,
554226463Spjd				    "Unable to open or create pidfile %s",
555226463Spjd				    newcfg->hc_pidfile);
556226463Spjd			}
557229945Spjd		} else if (pidfile_write(newpfh) == -1) {
558226463Spjd			/* Write PID to a file. */
559226463Spjd			pjdlog_errno(LOG_WARNING,
560226463Spjd			    "Unable to write PID to file %s",
561226463Spjd			    newcfg->hc_pidfile);
562226463Spjd		} else {
563226463Spjd			pjdlog_debug(1, "PID stored in %s.",
564226463Spjd			    newcfg->hc_pidfile);
565226463Spjd		}
566226463Spjd	}
567222108Spjd
568222108Spjd	/* No failures from now on. */
569222108Spjd
570210886Spjd	/*
571222108Spjd	 * Switch to new control socket.
572210886Spjd	 */
573210886Spjd	if (newcfg->hc_controlconn != NULL) {
574210886Spjd		pjdlog_info("Control socket changed from %s to %s.",
575210886Spjd		    cfg->hc_controladdr, newcfg->hc_controladdr);
576210886Spjd		proto_close(cfg->hc_controlconn);
577210886Spjd		cfg->hc_controlconn = newcfg->hc_controlconn;
578210886Spjd		newcfg->hc_controlconn = NULL;
579210886Spjd		strlcpy(cfg->hc_controladdr, newcfg->hc_controladdr,
580210886Spjd		    sizeof(cfg->hc_controladdr));
581210886Spjd	}
582222108Spjd	/*
583226463Spjd	 * Switch to new pidfile.
584226463Spjd	 */
585229946Spjd	if (newpfh != NULL) {
586229946Spjd		pjdlog_info("Pidfile changed from %s to %s.", cfg->hc_pidfile,
587229946Spjd		    newcfg->hc_pidfile);
588229946Spjd		(void)pidfile_remove(pfh);
589229946Spjd		pfh = newpfh;
590229946Spjd		(void)strlcpy(cfg->hc_pidfile, newcfg->hc_pidfile,
591229946Spjd		    sizeof(cfg->hc_pidfile));
592229946Spjd	}
593226463Spjd	/*
594222108Spjd	 * Switch to new listen addresses. Close all that were removed.
595222108Spjd	 */
596222108Spjd	while ((clst = TAILQ_FIRST(&cfg->hc_listen)) != NULL) {
597222108Spjd		TAILQ_FOREACH(nlst, &newcfg->hc_listen, hl_next) {
598222108Spjd			if (strcmp(nlst->hl_addr, clst->hl_addr) == 0)
599222108Spjd				break;
600222108Spjd		}
601222108Spjd		if (nlst == NULL && clst->hl_conn != NULL) {
602222108Spjd			proto_close(clst->hl_conn);
603222108Spjd			pjdlog_info("No longer listening on address %s.",
604222108Spjd			    clst->hl_addr);
605222108Spjd		}
606222108Spjd		TAILQ_REMOVE(&cfg->hc_listen, clst, hl_next);
607222108Spjd		free(clst);
608210886Spjd	}
609222108Spjd	TAILQ_CONCAT(&cfg->hc_listen, &newcfg->hc_listen, hl_next);
610210886Spjd
611210886Spjd	/*
612210886Spjd	 * Stop and remove resources that were removed from the configuration.
613210886Spjd	 */
614210886Spjd	TAILQ_FOREACH_SAFE(cres, &cfg->hc_resources, hr_next, tres) {
615210886Spjd		TAILQ_FOREACH(nres, &newcfg->hc_resources, hr_next) {
616210886Spjd			if (strcmp(cres->hr_name, nres->hr_name) == 0)
617210886Spjd				break;
618210886Spjd		}
619210886Spjd		if (nres == NULL) {
620210886Spjd			control_set_role(cres, HAST_ROLE_INIT);
621210886Spjd			TAILQ_REMOVE(&cfg->hc_resources, cres, hr_next);
622210886Spjd			pjdlog_info("Resource %s removed.", cres->hr_name);
623210886Spjd			free(cres);
624210886Spjd		}
625210886Spjd	}
626210886Spjd	/*
627210886Spjd	 * Move new resources to the current configuration.
628210886Spjd	 */
629210886Spjd	TAILQ_FOREACH_SAFE(nres, &newcfg->hc_resources, hr_next, tres) {
630210886Spjd		TAILQ_FOREACH(cres, &cfg->hc_resources, hr_next) {
631210886Spjd			if (strcmp(cres->hr_name, nres->hr_name) == 0)
632210886Spjd				break;
633210886Spjd		}
634210886Spjd		if (cres == NULL) {
635210886Spjd			TAILQ_REMOVE(&newcfg->hc_resources, nres, hr_next);
636210886Spjd			TAILQ_INSERT_TAIL(&cfg->hc_resources, nres, hr_next);
637210886Spjd			pjdlog_info("Resource %s added.", nres->hr_name);
638210886Spjd		}
639210886Spjd	}
640210886Spjd	/*
641210886Spjd	 * Deal with modified resources.
642210886Spjd	 * Depending on what has changed exactly we might want to perform
643210886Spjd	 * different actions.
644210886Spjd	 *
645210886Spjd	 * We do full resource restart in the following situations:
646210886Spjd	 * Resource role is INIT or SECONDARY.
647210886Spjd	 * Resource role is PRIMARY and path to local component or provider
648210886Spjd	 * name has changed.
649210886Spjd	 * In case of PRIMARY, the worker process will be killed and restarted,
650210886Spjd	 * which also means removing /dev/hast/<name> provider and
651210886Spjd	 * recreating it.
652210886Spjd	 *
653210886Spjd	 * We do just reload (send SIGHUP to worker process) if we act as
654225830Spjd	 * PRIMARY, but only if remote address, source address, replication
655225830Spjd	 * mode, timeout, execution path or metaflush has changed.
656225830Spjd	 * For those, there is no need to restart worker process.
657210886Spjd	 * If PRIMARY receives SIGHUP, it will reconnect if remote address or
658225830Spjd	 * source address has changed or it will set new timeout if only timeout
659225830Spjd	 * has changed or it will update metaflush if only metaflush has
660225830Spjd	 * changed.
661210886Spjd	 */
662210886Spjd	TAILQ_FOREACH_SAFE(nres, &newcfg->hc_resources, hr_next, tres) {
663210886Spjd		TAILQ_FOREACH(cres, &cfg->hc_resources, hr_next) {
664210886Spjd			if (strcmp(cres->hr_name, nres->hr_name) == 0)
665210886Spjd				break;
666210886Spjd		}
667218138Spjd		PJDLOG_ASSERT(cres != NULL);
668210886Spjd		if (resource_needs_restart(cres, nres)) {
669210886Spjd			pjdlog_info("Resource %s configuration was modified, restarting it.",
670210886Spjd			    cres->hr_name);
671210886Spjd			role = cres->hr_role;
672210886Spjd			control_set_role(cres, HAST_ROLE_INIT);
673210886Spjd			TAILQ_REMOVE(&cfg->hc_resources, cres, hr_next);
674210886Spjd			free(cres);
675210886Spjd			TAILQ_REMOVE(&newcfg->hc_resources, nres, hr_next);
676210886Spjd			TAILQ_INSERT_TAIL(&cfg->hc_resources, nres, hr_next);
677210886Spjd			control_set_role(nres, role);
678210886Spjd		} else if (resource_needs_reload(cres, nres)) {
679210886Spjd			pjdlog_info("Resource %s configuration was modified, reloading it.",
680210886Spjd			    cres->hr_name);
681210886Spjd			strlcpy(cres->hr_remoteaddr, nres->hr_remoteaddr,
682210886Spjd			    sizeof(cres->hr_remoteaddr));
683219818Spjd			strlcpy(cres->hr_sourceaddr, nres->hr_sourceaddr,
684219818Spjd			    sizeof(cres->hr_sourceaddr));
685210886Spjd			cres->hr_replication = nres->hr_replication;
686219351Spjd			cres->hr_checksum = nres->hr_checksum;
687219354Spjd			cres->hr_compression = nres->hr_compression;
688210886Spjd			cres->hr_timeout = nres->hr_timeout;
689217729Spjd			strlcpy(cres->hr_exec, nres->hr_exec,
690217729Spjd			    sizeof(cres->hr_exec));
691225830Spjd			cres->hr_metaflush = nres->hr_metaflush;
692217784Spjd			if (cres->hr_workerpid != 0)
693217784Spjd				resource_reload(cres);
694210886Spjd		}
695210886Spjd	}
696210886Spjd
697210886Spjd	yy_config_free(newcfg);
698210886Spjd	pjdlog_info("Configuration reloaded successfully.");
699210886Spjd	return;
700210886Spjdfailed:
701210886Spjd	if (newcfg != NULL) {
702210886Spjd		if (newcfg->hc_controlconn != NULL)
703210886Spjd			proto_close(newcfg->hc_controlconn);
704222108Spjd		while ((nlst = TAILQ_FIRST(&newcfg->hc_listen)) != NULL) {
705222108Spjd			if (nlst->hl_conn != NULL) {
706222108Spjd				TAILQ_FOREACH(clst, &cfg->hc_listen, hl_next) {
707222108Spjd					if (strcmp(nlst->hl_addr,
708222108Spjd					    clst->hl_addr) == 0) {
709222108Spjd						break;
710222108Spjd					}
711222108Spjd				}
712222108Spjd				if (clst == NULL || clst->hl_conn == NULL)
713222108Spjd					proto_close(nlst->hl_conn);
714222108Spjd			}
715222108Spjd			TAILQ_REMOVE(&newcfg->hc_listen, nlst, hl_next);
716222108Spjd			free(nlst);
717222108Spjd		}
718210886Spjd		yy_config_free(newcfg);
719210886Spjd	}
720226463Spjd	if (newpfh != NULL)
721226463Spjd		(void)pidfile_remove(newpfh);
722210886Spjd	pjdlog_warning("Configuration not reloaded.");
723204076Spjd}
724204076Spjd
725204076Spjdstatic void
726211899Spjdterminate_workers(void)
727211899Spjd{
728211899Spjd	struct hast_resource *res;
729211899Spjd
730211899Spjd	pjdlog_info("Termination signal received, exiting.");
731211899Spjd	TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
732211899Spjd		if (res->hr_workerpid == 0)
733211899Spjd			continue;
734211899Spjd		pjdlog_info("Terminating worker process (resource=%s, role=%s, pid=%u).",
735211899Spjd		    res->hr_name, role2str(res->hr_role), res->hr_workerpid);
736211899Spjd		if (kill(res->hr_workerpid, SIGTERM) == 0)
737211899Spjd			continue;
738211899Spjd		pjdlog_errno(LOG_WARNING,
739211899Spjd		    "Unable to send signal to worker process (resource=%s, role=%s, pid=%u).",
740211899Spjd		    res->hr_name, role2str(res->hr_role), res->hr_workerpid);
741211899Spjd	}
742211899Spjd}
743211899Spjd
744211899Spjdstatic void
745222108Spjdlisten_accept(struct hastd_listen *lst)
746204076Spjd{
747204076Spjd	struct hast_resource *res;
748204076Spjd	struct proto_conn *conn;
749204076Spjd	struct nv *nvin, *nvout, *nverr;
750204076Spjd	const char *resname;
751204076Spjd	const unsigned char *token;
752204076Spjd	char laddr[256], raddr[256];
753246922Spjd	uint8_t version;
754204076Spjd	size_t size;
755204076Spjd	pid_t pid;
756204076Spjd	int status;
757204076Spjd
758222108Spjd	proto_local_address(lst->hl_conn, laddr, sizeof(laddr));
759204076Spjd	pjdlog_debug(1, "Accepting connection to %s.", laddr);
760204076Spjd
761229945Spjd	if (proto_accept(lst->hl_conn, &conn) == -1) {
762204076Spjd		pjdlog_errno(LOG_ERR, "Unable to accept connection %s", laddr);
763204076Spjd		return;
764204076Spjd	}
765204076Spjd
766204076Spjd	proto_local_address(conn, laddr, sizeof(laddr));
767204076Spjd	proto_remote_address(conn, raddr, sizeof(raddr));
768209185Spjd	pjdlog_info("Connection from %s to %s.", raddr, laddr);
769204076Spjd
770207371Spjd	/* Error in setting timeout is not critical, but why should it fail? */
771229945Spjd	if (proto_timeout(conn, HAST_TIMEOUT) == -1)
772207371Spjd		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
773207371Spjd
774204076Spjd	nvin = nvout = nverr = NULL;
775204076Spjd
776204076Spjd	/*
777204076Spjd	 * Before receiving any data see if remote host have access to any
778204076Spjd	 * resource.
779204076Spjd	 */
780204076Spjd	TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
781204076Spjd		if (proto_address_match(conn, res->hr_remoteaddr))
782204076Spjd			break;
783204076Spjd	}
784204076Spjd	if (res == NULL) {
785204076Spjd		pjdlog_error("Client %s isn't known.", raddr);
786204076Spjd		goto close;
787204076Spjd	}
788204076Spjd	/* Ok, remote host can access at least one resource. */
789204076Spjd
790229945Spjd	if (hast_proto_recv_hdr(conn, &nvin) == -1) {
791204076Spjd		pjdlog_errno(LOG_ERR, "Unable to receive header from %s",
792204076Spjd		    raddr);
793204076Spjd		goto close;
794204076Spjd	}
795204076Spjd
796204076Spjd	resname = nv_get_string(nvin, "resource");
797204076Spjd	if (resname == NULL) {
798204076Spjd		pjdlog_error("No 'resource' field in the header received from %s.",
799204076Spjd		    raddr);
800204076Spjd		goto close;
801204076Spjd	}
802204076Spjd	pjdlog_debug(2, "%s: resource=%s", raddr, resname);
803246922Spjd	version = nv_get_uint8(nvin, "version");
804246922Spjd	pjdlog_debug(2, "%s: version=%hhu", raddr, version);
805246922Spjd	if (version == 0) {
806246922Spjd		/*
807246922Spjd		 * If no version is sent, it means this is protocol version 1.
808246922Spjd		 */
809246922Spjd		version = 1;
810246922Spjd	}
811204076Spjd	token = nv_get_uint8_array(nvin, &size, "token");
812204076Spjd	/*
813229778Suqs	 * NULL token means that this is first connection.
814204076Spjd	 */
815204076Spjd	if (token != NULL && size != sizeof(res->hr_token)) {
816204076Spjd		pjdlog_error("Received token of invalid size from %s (expected %zu, got %zu).",
817204076Spjd		    raddr, sizeof(res->hr_token), size);
818204076Spjd		goto close;
819204076Spjd	}
820204076Spjd
821204076Spjd	/*
822204076Spjd	 * From now on we want to send errors to the remote node.
823204076Spjd	 */
824204076Spjd	nverr = nv_alloc();
825204076Spjd
826204076Spjd	/* Find resource related to this connection. */
827204076Spjd	TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
828204076Spjd		if (strcmp(resname, res->hr_name) == 0)
829204076Spjd			break;
830204076Spjd	}
831204076Spjd	/* Have we found the resource? */
832204076Spjd	if (res == NULL) {
833204076Spjd		pjdlog_error("No resource '%s' as requested by %s.",
834204076Spjd		    resname, raddr);
835204076Spjd		nv_add_stringf(nverr, "errmsg", "Resource not configured.");
836204076Spjd		goto fail;
837204076Spjd	}
838204076Spjd
839204076Spjd	/* Now that we know resource name setup log prefix. */
840204076Spjd	pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
841204076Spjd
842204076Spjd	/* Does the remote host have access to this resource? */
843204076Spjd	if (!proto_address_match(conn, res->hr_remoteaddr)) {
844204076Spjd		pjdlog_error("Client %s has no access to the resource.", raddr);
845204076Spjd		nv_add_stringf(nverr, "errmsg", "No access to the resource.");
846204076Spjd		goto fail;
847204076Spjd	}
848204076Spjd	/* Is the resource marked as secondary? */
849204076Spjd	if (res->hr_role != HAST_ROLE_SECONDARY) {
850220890Spjd		pjdlog_warning("We act as %s for the resource and not as %s as requested by %s.",
851204076Spjd		    role2str(res->hr_role), role2str(HAST_ROLE_SECONDARY),
852204076Spjd		    raddr);
853204076Spjd		nv_add_stringf(nverr, "errmsg",
854204076Spjd		    "Remote node acts as %s for the resource and not as %s.",
855204076Spjd		    role2str(res->hr_role), role2str(HAST_ROLE_SECONDARY));
856220898Spjd		if (res->hr_role == HAST_ROLE_PRIMARY) {
857220898Spjd			/*
858220898Spjd			 * If we act as primary request the other side to wait
859220899Spjd			 * for us a bit, as we might be finishing cleanups.
860220898Spjd			 */
861220898Spjd			nv_add_uint8(nverr, 1, "wait");
862220898Spjd		}
863204076Spjd		goto fail;
864204076Spjd	}
865204076Spjd	/* Does token (if exists) match? */
866204076Spjd	if (token != NULL && memcmp(token, res->hr_token,
867204076Spjd	    sizeof(res->hr_token)) != 0) {
868204076Spjd		pjdlog_error("Token received from %s doesn't match.", raddr);
869209185Spjd		nv_add_stringf(nverr, "errmsg", "Token doesn't match.");
870204076Spjd		goto fail;
871204076Spjd	}
872204076Spjd	/*
873204076Spjd	 * If there is no token, but we have half-open connection
874204076Spjd	 * (only remotein) or full connection (worker process is running)
875204076Spjd	 * we have to cancel those and accept the new connection.
876204076Spjd	 */
877204076Spjd	if (token == NULL) {
878218138Spjd		PJDLOG_ASSERT(res->hr_remoteout == NULL);
879204076Spjd		pjdlog_debug(1, "Initial connection from %s.", raddr);
880204076Spjd		if (res->hr_workerpid != 0) {
881218138Spjd			PJDLOG_ASSERT(res->hr_remotein == NULL);
882204076Spjd			pjdlog_debug(1,
883204076Spjd			    "Worker process exists (pid=%u), stopping it.",
884204076Spjd			    (unsigned int)res->hr_workerpid);
885204076Spjd			/* Stop child process. */
886229945Spjd			if (kill(res->hr_workerpid, SIGINT) == -1) {
887204076Spjd				pjdlog_errno(LOG_ERR,
888204076Spjd				    "Unable to stop worker process (pid=%u)",
889204076Spjd				    (unsigned int)res->hr_workerpid);
890204076Spjd				/*
891204076Spjd				 * Other than logging the problem we
892204076Spjd				 * ignore it - nothing smart to do.
893204076Spjd				 */
894204076Spjd			}
895204076Spjd			/* Wait for it to exit. */
896204076Spjd			else if ((pid = waitpid(res->hr_workerpid,
897204076Spjd			    &status, 0)) != res->hr_workerpid) {
898207372Spjd				/* We can only log the problem. */
899204076Spjd				pjdlog_errno(LOG_ERR,
900204076Spjd				    "Waiting for worker process (pid=%u) failed",
901204076Spjd				    (unsigned int)res->hr_workerpid);
902204076Spjd			} else {
903207372Spjd				child_exit_log(res->hr_workerpid, status);
904204076Spjd			}
905213006Spjd			child_cleanup(res);
906204076Spjd		} else if (res->hr_remotein != NULL) {
907204076Spjd			char oaddr[256];
908204076Spjd
909213981Spjd			proto_remote_address(res->hr_remotein, oaddr,
910213981Spjd			    sizeof(oaddr));
911204076Spjd			pjdlog_debug(1,
912204076Spjd			    "Canceling half-open connection from %s on connection from %s.",
913204076Spjd			    oaddr, raddr);
914204076Spjd			proto_close(res->hr_remotein);
915204076Spjd			res->hr_remotein = NULL;
916204076Spjd		}
917204076Spjd	}
918204076Spjd
919204076Spjd	/*
920204076Spjd	 * Checks and cleanups are done.
921204076Spjd	 */
922204076Spjd
923204076Spjd	if (token == NULL) {
924259196Strociny		if (version > HAST_PROTO_VERSION) {
925259196Strociny			pjdlog_info("Remote protocol version %hhu is not supported, falling back to version %hhu.",
926259196Strociny			    version, (unsigned char)HAST_PROTO_VERSION);
927259196Strociny			version = HAST_PROTO_VERSION;
928259196Strociny		}
929259196Strociny		pjdlog_debug(1, "Negotiated protocol version %hhu.", version);
930246922Spjd		res->hr_version = version;
931204076Spjd		arc4random_buf(res->hr_token, sizeof(res->hr_token));
932204076Spjd		nvout = nv_alloc();
933246922Spjd		nv_add_uint8(nvout, version, "version");
934204076Spjd		nv_add_uint8_array(nvout, res->hr_token,
935204076Spjd		    sizeof(res->hr_token), "token");
936204076Spjd		if (nv_error(nvout) != 0) {
937204076Spjd			pjdlog_common(LOG_ERR, 0, nv_error(nvout),
938204076Spjd			    "Unable to prepare return header for %s", raddr);
939204076Spjd			nv_add_stringf(nverr, "errmsg",
940204076Spjd			    "Remote node was unable to prepare return header: %s.",
941204076Spjd			    strerror(nv_error(nvout)));
942204076Spjd			goto fail;
943204076Spjd		}
944246922Spjd		if (hast_proto_send(res, conn, nvout, NULL, 0) == -1) {
945204076Spjd			int error = errno;
946204076Spjd
947204076Spjd			pjdlog_errno(LOG_ERR, "Unable to send response to %s",
948204076Spjd			    raddr);
949204076Spjd			nv_add_stringf(nverr, "errmsg",
950204076Spjd			    "Remote node was unable to send response: %s.",
951204076Spjd			    strerror(error));
952204076Spjd			goto fail;
953204076Spjd		}
954204076Spjd		res->hr_remotein = conn;
955204076Spjd		pjdlog_debug(1, "Incoming connection from %s configured.",
956204076Spjd		    raddr);
957204076Spjd	} else {
958204076Spjd		res->hr_remoteout = conn;
959204076Spjd		pjdlog_debug(1, "Outgoing connection to %s configured.", raddr);
960204076Spjd		hastd_secondary(res, nvin);
961204076Spjd	}
962204076Spjd	nv_free(nvin);
963204076Spjd	nv_free(nvout);
964204076Spjd	nv_free(nverr);
965204076Spjd	pjdlog_prefix_set("%s", "");
966204076Spjd	return;
967204076Spjdfail:
968204076Spjd	if (nv_error(nverr) != 0) {
969204076Spjd		pjdlog_common(LOG_ERR, 0, nv_error(nverr),
970204076Spjd		    "Unable to prepare error header for %s", raddr);
971204076Spjd		goto close;
972204076Spjd	}
973229945Spjd	if (hast_proto_send(NULL, conn, nverr, NULL, 0) == -1) {
974204076Spjd		pjdlog_errno(LOG_ERR, "Unable to send error to %s", raddr);
975204076Spjd		goto close;
976204076Spjd	}
977204076Spjdclose:
978204076Spjd	if (nvin != NULL)
979204076Spjd		nv_free(nvin);
980204076Spjd	if (nvout != NULL)
981204076Spjd		nv_free(nvout);
982204076Spjd	if (nverr != NULL)
983204076Spjd		nv_free(nverr);
984204076Spjd	proto_close(conn);
985204076Spjd	pjdlog_prefix_set("%s", "");
986204076Spjd}
987204076Spjd
988204076Spjdstatic void
989218218Spjdconnection_migrate(struct hast_resource *res)
990218218Spjd{
991218218Spjd	struct proto_conn *conn;
992218218Spjd	int16_t val = 0;
993218218Spjd
994219814Spjd	pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
995219814Spjd
996219900Spjd	PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
997219900Spjd
998229945Spjd	if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) {
999218218Spjd		pjdlog_errno(LOG_WARNING,
1000218218Spjd		    "Unable to receive connection command");
1001218218Spjd		return;
1002218218Spjd	}
1003219818Spjd	if (proto_client(res->hr_sourceaddr[0] != '\0' ? res->hr_sourceaddr : NULL,
1004229945Spjd	    res->hr_remoteaddr, &conn) == -1) {
1005218218Spjd		val = errno;
1006218218Spjd		pjdlog_errno(LOG_WARNING,
1007218218Spjd		    "Unable to create outgoing connection to %s",
1008218218Spjd		    res->hr_remoteaddr);
1009218218Spjd		goto out;
1010218218Spjd	}
1011229945Spjd	if (proto_connect(conn, -1) == -1) {
1012218218Spjd		val = errno;
1013218218Spjd		pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
1014218218Spjd		    res->hr_remoteaddr);
1015218218Spjd		proto_close(conn);
1016218218Spjd		goto out;
1017218218Spjd	}
1018218218Spjd	val = 0;
1019218218Spjdout:
1020229945Spjd	if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) {
1021218218Spjd		pjdlog_errno(LOG_WARNING,
1022218218Spjd		    "Unable to send reply to connection request");
1023218218Spjd	}
1024229945Spjd	if (val == 0 && proto_connection_send(res->hr_conn, conn) == -1)
1025218218Spjd		pjdlog_errno(LOG_WARNING, "Unable to send connection");
1026219814Spjd
1027219814Spjd	pjdlog_prefix_set("%s", "");
1028218218Spjd}
1029218218Spjd
1030218218Spjdstatic void
1031219837Spjdcheck_signals(void)
1032204076Spjd{
1033213009Spjd	struct timespec sigtimeout;
1034213009Spjd	sigset_t mask;
1035219837Spjd	int signo;
1036204076Spjd
1037213009Spjd	sigtimeout.tv_sec = 0;
1038213009Spjd	sigtimeout.tv_nsec = 0;
1039211977Spjd
1040213009Spjd	PJDLOG_VERIFY(sigemptyset(&mask) == 0);
1041213009Spjd	PJDLOG_VERIFY(sigaddset(&mask, SIGHUP) == 0);
1042213009Spjd	PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
1043213009Spjd	PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
1044213009Spjd	PJDLOG_VERIFY(sigaddset(&mask, SIGCHLD) == 0);
1045213009Spjd
1046219837Spjd	while ((signo = sigtimedwait(&mask, NULL, &sigtimeout)) != -1) {
1047219837Spjd		switch (signo) {
1048219837Spjd		case SIGINT:
1049219837Spjd		case SIGTERM:
1050219837Spjd			sigexit_received = true;
1051219837Spjd			terminate_workers();
1052219837Spjd			proto_close(cfg->hc_controlconn);
1053219837Spjd			exit(EX_OK);
1054219837Spjd			break;
1055219837Spjd		case SIGCHLD:
1056219837Spjd			child_exit();
1057219837Spjd			break;
1058219837Spjd		case SIGHUP:
1059219837Spjd			hastd_reload();
1060219837Spjd			break;
1061219837Spjd		default:
1062219837Spjd			PJDLOG_ABORT("Unexpected signal (%d).", signo);
1063219837Spjd		}
1064219837Spjd	}
1065219837Spjd}
1066219837Spjd
1067219837Spjdstatic void
1068219837Spjdmain_loop(void)
1069219837Spjd{
1070219837Spjd	struct hast_resource *res;
1071222108Spjd	struct hastd_listen *lst;
1072219837Spjd	struct timeval seltimeout;
1073219837Spjd	int fd, maxfd, ret;
1074219837Spjd	time_t lastcheck, now;
1075219837Spjd	fd_set rfds;
1076219837Spjd
1077219864Spjd	lastcheck = time(NULL);
1078219837Spjd	seltimeout.tv_sec = REPORT_INTERVAL;
1079219837Spjd	seltimeout.tv_usec = 0;
1080219837Spjd
1081204076Spjd	for (;;) {
1082219837Spjd		check_signals();
1083204076Spjd
1084209177Spjd		/* Setup descriptors for select(2). */
1085204076Spjd		FD_ZERO(&rfds);
1086212038Spjd		maxfd = fd = proto_descriptor(cfg->hc_controlconn);
1087218138Spjd		PJDLOG_ASSERT(fd >= 0);
1088212038Spjd		FD_SET(fd, &rfds);
1089222108Spjd		TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next) {
1090222108Spjd			if (lst->hl_conn == NULL)
1091222108Spjd				continue;
1092222108Spjd			fd = proto_descriptor(lst->hl_conn);
1093222108Spjd			PJDLOG_ASSERT(fd >= 0);
1094222108Spjd			FD_SET(fd, &rfds);
1095298393Saraujo			maxfd = MAX(fd, maxfd);
1096222108Spjd		}
1097212038Spjd		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
1098212038Spjd			if (res->hr_event == NULL)
1099212038Spjd				continue;
1100212038Spjd			fd = proto_descriptor(res->hr_event);
1101218138Spjd			PJDLOG_ASSERT(fd >= 0);
1102212038Spjd			FD_SET(fd, &rfds);
1103298393Saraujo			maxfd = MAX(fd, maxfd);
1104218218Spjd			if (res->hr_role == HAST_ROLE_PRIMARY) {
1105218218Spjd				/* Only primary workers asks for connections. */
1106219900Spjd				PJDLOG_ASSERT(res->hr_conn != NULL);
1107218218Spjd				fd = proto_descriptor(res->hr_conn);
1108218218Spjd				PJDLOG_ASSERT(fd >= 0);
1109218218Spjd				FD_SET(fd, &rfds);
1110298393Saraujo				maxfd = MAX(fd, maxfd);
1111219900Spjd			} else {
1112219900Spjd				PJDLOG_ASSERT(res->hr_conn == NULL);
1113218218Spjd			}
1114212038Spjd		}
1115204076Spjd
1116218138Spjd		PJDLOG_ASSERT(maxfd + 1 <= (int)FD_SETSIZE);
1117213009Spjd		ret = select(maxfd + 1, &rfds, NULL, NULL, &seltimeout);
1118219813Spjd		now = time(NULL);
1119219813Spjd		if (lastcheck + REPORT_INTERVAL <= now) {
1120213429Spjd			hook_check();
1121219813Spjd			lastcheck = now;
1122219813Spjd		}
1123219813Spjd		if (ret == 0) {
1124219813Spjd			/*
1125219813Spjd			 * select(2) timed out, so there should be no
1126219813Spjd			 * descriptors to check.
1127219813Spjd			 */
1128219813Spjd			continue;
1129219813Spjd		} else if (ret == -1) {
1130204076Spjd			if (errno == EINTR)
1131204076Spjd				continue;
1132204076Spjd			KEEP_ERRNO((void)pidfile_remove(pfh));
1133204076Spjd			pjdlog_exit(EX_OSERR, "select() failed");
1134204076Spjd		}
1135204076Spjd
1136219837Spjd		/*
1137219837Spjd		 * Check for signals before we do anything to update our
1138219837Spjd		 * info about terminated workers in the meantime.
1139219837Spjd		 */
1140219837Spjd		check_signals();
1141219837Spjd
1142212038Spjd		if (FD_ISSET(proto_descriptor(cfg->hc_controlconn), &rfds))
1143204076Spjd			control_handle(cfg);
1144222108Spjd		TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next) {
1145222108Spjd			if (lst->hl_conn == NULL)
1146222108Spjd				continue;
1147222108Spjd			if (FD_ISSET(proto_descriptor(lst->hl_conn), &rfds))
1148222108Spjd				listen_accept(lst);
1149222108Spjd		}
1150212038Spjd		TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
1151212038Spjd			if (res->hr_event == NULL)
1152212038Spjd				continue;
1153212038Spjd			if (FD_ISSET(proto_descriptor(res->hr_event), &rfds)) {
1154212038Spjd				if (event_recv(res) == 0)
1155212038Spjd					continue;
1156212038Spjd				/* The worker process exited? */
1157212038Spjd				proto_close(res->hr_event);
1158212038Spjd				res->hr_event = NULL;
1159219900Spjd				if (res->hr_conn != NULL) {
1160219900Spjd					proto_close(res->hr_conn);
1161219900Spjd					res->hr_conn = NULL;
1162219900Spjd				}
1163218218Spjd				continue;
1164212038Spjd			}
1165219900Spjd			if (res->hr_role == HAST_ROLE_PRIMARY) {
1166219900Spjd				PJDLOG_ASSERT(res->hr_conn != NULL);
1167219900Spjd				if (FD_ISSET(proto_descriptor(res->hr_conn),
1168219900Spjd				    &rfds)) {
1169219900Spjd					connection_migrate(res);
1170219900Spjd				}
1171219900Spjd			} else {
1172219900Spjd				PJDLOG_ASSERT(res->hr_conn == NULL);
1173218218Spjd			}
1174212038Spjd		}
1175204076Spjd	}
1176204076Spjd}
1177204076Spjd
1178213428Spjdstatic void
1179213428Spjddummy_sighandler(int sig __unused)
1180213428Spjd{
1181213428Spjd	/* Nothing to do. */
1182213428Spjd}
1183213428Spjd
1184204076Spjdint
1185204076Spjdmain(int argc, char *argv[])
1186204076Spjd{
1187222108Spjd	struct hastd_listen *lst;
1188204076Spjd	pid_t otherpid;
1189204076Spjd	int debuglevel;
1190213009Spjd	sigset_t mask;
1191204076Spjd
1192204076Spjd	foreground = false;
1193204076Spjd	debuglevel = 0;
1194204076Spjd
1195204076Spjd	for (;;) {
1196204076Spjd		int ch;
1197204076Spjd
1198204076Spjd		ch = getopt(argc, argv, "c:dFhP:");
1199204076Spjd		if (ch == -1)
1200204076Spjd			break;
1201204076Spjd		switch (ch) {
1202204076Spjd		case 'c':
1203204076Spjd			cfgpath = optarg;
1204204076Spjd			break;
1205204076Spjd		case 'd':
1206204076Spjd			debuglevel++;
1207204076Spjd			break;
1208204076Spjd		case 'F':
1209204076Spjd			foreground = true;
1210204076Spjd			break;
1211204076Spjd		case 'P':
1212204076Spjd			pidfile = optarg;
1213204076Spjd			break;
1214204076Spjd		case 'h':
1215204076Spjd		default:
1216204076Spjd			usage();
1217204076Spjd		}
1218204076Spjd	}
1219204076Spjd	argc -= optind;
1220204076Spjd	argv += optind;
1221204076Spjd
1222217965Spjd	pjdlog_init(PJDLOG_MODE_STD);
1223204076Spjd	pjdlog_debug_set(debuglevel);
1224204076Spjd
1225368759Seugen	closefrom(MAX(MAX(STDIN_FILENO, STDOUT_FILENO), STDERR_FILENO) + 1);
1226214273Spjd	g_gate_load();
1227214273Spjd
1228226461Spjd	/*
1229226461Spjd	 * When path to the configuration file is relative, obtain full path,
1230226461Spjd	 * so we can always find the file, even after daemonizing and changing
1231226461Spjd	 * working directory to /.
1232226461Spjd	 */
1233226461Spjd	if (cfgpath[0] != '/') {
1234226461Spjd		const char *newcfgpath;
1235226461Spjd
1236226461Spjd		newcfgpath = realpath(cfgpath, NULL);
1237226461Spjd		if (newcfgpath == NULL) {
1238226461Spjd			pjdlog_exit(EX_CONFIG,
1239226461Spjd			    "Unable to obtain full path of %s", cfgpath);
1240226461Spjd		}
1241226461Spjd		cfgpath = newcfgpath;
1242226461Spjd	}
1243226461Spjd
1244210883Spjd	cfg = yy_config_parse(cfgpath, true);
1245218138Spjd	PJDLOG_ASSERT(cfg != NULL);
1246204076Spjd
1247226463Spjd	if (pidfile != NULL) {
1248226463Spjd		if (strlcpy(cfg->hc_pidfile, pidfile,
1249226463Spjd		    sizeof(cfg->hc_pidfile)) >= sizeof(cfg->hc_pidfile)) {
1250226463Spjd			pjdlog_exitx(EX_CONFIG, "Pidfile path is too long.");
1251226463Spjd		}
1252226463Spjd	}
1253229944Spjd
1254233679Strociny	if (pidfile != NULL || !foreground) {
1255229944Spjd		pfh = pidfile_open(cfg->hc_pidfile, 0600, &otherpid);
1256229944Spjd		if (pfh == NULL) {
1257229944Spjd			if (errno == EEXIST) {
1258229944Spjd				pjdlog_exitx(EX_TEMPFAIL,
1259229944Spjd				    "Another hastd is already running, pidfile: %s, pid: %jd.",
1260229944Spjd				    cfg->hc_pidfile, (intmax_t)otherpid);
1261229944Spjd			}
1262229944Spjd			/*
1263229944Spjd			 * If we cannot create pidfile for other reasons,
1264229944Spjd			 * only warn.
1265229944Spjd			 */
1266229944Spjd			pjdlog_errno(LOG_WARNING,
1267229944Spjd			    "Unable to open or create pidfile %s",
1268229944Spjd			    cfg->hc_pidfile);
1269226463Spjd		}
1270226463Spjd	}
1271226463Spjd
1272213428Spjd	/*
1273217307Spjd	 * Restore default actions for interesting signals in case parent
1274217307Spjd	 * process (like init(8)) decided to ignore some of them (like SIGHUP).
1275217307Spjd	 */
1276217307Spjd	PJDLOG_VERIFY(signal(SIGHUP, SIG_DFL) != SIG_ERR);
1277217307Spjd	PJDLOG_VERIFY(signal(SIGINT, SIG_DFL) != SIG_ERR);
1278217307Spjd	PJDLOG_VERIFY(signal(SIGTERM, SIG_DFL) != SIG_ERR);
1279217307Spjd	/*
1280213428Spjd	 * Because SIGCHLD is ignored by default, setup dummy handler for it,
1281213428Spjd	 * so we can mask it.
1282213428Spjd	 */
1283213428Spjd	PJDLOG_VERIFY(signal(SIGCHLD, dummy_sighandler) != SIG_ERR);
1284217307Spjd
1285213009Spjd	PJDLOG_VERIFY(sigemptyset(&mask) == 0);
1286213009Spjd	PJDLOG_VERIFY(sigaddset(&mask, SIGHUP) == 0);
1287213009Spjd	PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
1288213009Spjd	PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
1289213009Spjd	PJDLOG_VERIFY(sigaddset(&mask, SIGCHLD) == 0);
1290213009Spjd	PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
1291204076Spjd
1292204076Spjd	/* Listen on control address. */
1293229945Spjd	if (proto_server(cfg->hc_controladdr, &cfg->hc_controlconn) == -1) {
1294204076Spjd		KEEP_ERRNO((void)pidfile_remove(pfh));
1295204076Spjd		pjdlog_exit(EX_OSERR, "Unable to listen on control address %s",
1296204076Spjd		    cfg->hc_controladdr);
1297204076Spjd	}
1298204076Spjd	/* Listen for remote connections. */
1299222108Spjd	TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next) {
1300229945Spjd		if (proto_server(lst->hl_addr, &lst->hl_conn) == -1) {
1301222108Spjd			KEEP_ERRNO((void)pidfile_remove(pfh));
1302222108Spjd			pjdlog_exit(EX_OSERR, "Unable to listen on address %s",
1303222108Spjd			    lst->hl_addr);
1304222108Spjd		}
1305204076Spjd	}
1306204076Spjd
1307204076Spjd	if (!foreground) {
1308229945Spjd		if (daemon(0, 0) == -1) {
1309204076Spjd			KEEP_ERRNO((void)pidfile_remove(pfh));
1310204076Spjd			pjdlog_exit(EX_OSERR, "Unable to daemonize");
1311204076Spjd		}
1312204076Spjd
1313204076Spjd		/* Start logging to syslog. */
1314204076Spjd		pjdlog_mode_set(PJDLOG_MODE_SYSLOG);
1315233679Strociny	}
1316233679Strociny	if (pidfile != NULL || !foreground) {
1317204076Spjd		/* Write PID to a file. */
1318229945Spjd		if (pidfile_write(pfh) == -1) {
1319204076Spjd			pjdlog_errno(LOG_WARNING,
1320226463Spjd			    "Unable to write PID to a file %s",
1321226463Spjd			    cfg->hc_pidfile);
1322226463Spjd		} else {
1323226463Spjd			pjdlog_debug(1, "PID stored in %s.", cfg->hc_pidfile);
1324204076Spjd		}
1325204076Spjd	}
1326204076Spjd
1327222108Spjd	pjdlog_info("Started successfully, running protocol version %d.",
1328222108Spjd	    HAST_PROTO_VERSION);
1329222108Spjd
1330222108Spjd	pjdlog_debug(1, "Listening on control address %s.",
1331222108Spjd	    cfg->hc_controladdr);
1332222108Spjd	TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next)
1333222108Spjd		pjdlog_info("Listening on address %s.", lst->hl_addr);
1334222108Spjd
1335211977Spjd	hook_init();
1336211977Spjd
1337204076Spjd	main_loop();
1338204076Spjd
1339204076Spjd	exit(0);
1340204076Spjd}
1341