2010-02-18 23:16:19 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2009-2010 The FreeBSD Foundation
|
2011-03-06 22:56:14 +00:00
|
|
|
* Copyright (c) 2010-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
|
2010-02-18 23:16:19 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by Pawel Jakub Dawidek under sponsorship from
|
|
|
|
* the FreeBSD Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/linker.h>
|
|
|
|
#include <sys/module.h>
|
2011-01-28 21:56:47 +00:00
|
|
|
#include <sys/stat.h>
|
2010-02-18 23:16:19 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <libutil.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
2011-03-21 08:31:35 +00:00
|
|
|
#include <time.h>
|
2010-02-18 23:16:19 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <activemap.h>
|
|
|
|
#include <pjdlog.h>
|
|
|
|
|
|
|
|
#include "control.h"
|
2010-08-30 23:26:10 +00:00
|
|
|
#include "event.h"
|
2010-02-18 23:16:19 +00:00
|
|
|
#include "hast.h"
|
|
|
|
#include "hast_proto.h"
|
|
|
|
#include "hastd.h"
|
2010-08-29 21:41:53 +00:00
|
|
|
#include "hooks.h"
|
2010-02-18 23:16:19 +00:00
|
|
|
#include "subr.h"
|
|
|
|
|
|
|
|
/* Path to configuration file. */
|
2010-08-05 19:16:31 +00:00
|
|
|
const char *cfgpath = HAST_CONFIG;
|
2010-02-18 23:16:19 +00:00
|
|
|
/* Hastd configuration. */
|
|
|
|
static struct hastd_config *cfg;
|
|
|
|
/* Was SIGINT or SIGTERM signal received? */
|
|
|
|
bool sigexit_received = false;
|
2012-03-29 20:11:16 +00:00
|
|
|
/* Path to pidfile. */
|
|
|
|
static const char *pidfile;
|
2013-02-17 21:12:34 +00:00
|
|
|
/* Pidfile handle. */
|
2010-02-18 23:16:19 +00:00
|
|
|
struct pidfh *pfh;
|
2012-01-10 22:24:57 +00:00
|
|
|
/* Do we run in foreground? */
|
|
|
|
static bool foreground;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2010-08-29 21:41:53 +00:00
|
|
|
/* How often check for hooks running for too long. */
|
2010-10-04 21:44:26 +00:00
|
|
|
#define REPORT_INTERVAL 5
|
2010-08-29 21:41:53 +00:00
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
errx(EX_USAGE, "[-dFh] [-c config] [-P pidfile]");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_gate_load(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (modfind("g_gate") == -1) {
|
|
|
|
/* Not present in kernel, try loading it. */
|
|
|
|
if (kldload("geom_gate") == -1 || modfind("g_gate") == -1) {
|
|
|
|
if (errno != EEXIST) {
|
|
|
|
pjdlog_exit(EX_OSERR,
|
|
|
|
"Unable to load geom_gate module");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-28 21:48:15 +00:00
|
|
|
void
|
|
|
|
descriptors_cleanup(struct hast_resource *res)
|
|
|
|
{
|
2012-01-22 11:20:42 +00:00
|
|
|
struct hast_resource *tres, *tmres;
|
2011-05-19 23:18:42 +00:00
|
|
|
struct hastd_listen *lst;
|
2011-01-28 21:48:15 +00:00
|
|
|
|
2012-01-22 11:20:42 +00:00
|
|
|
TAILQ_FOREACH_SAFE(tres, &cfg->hc_resources, hr_next, tmres) {
|
2011-01-28 21:48:15 +00:00
|
|
|
if (tres == res) {
|
|
|
|
PJDLOG_VERIFY(res->hr_role == HAST_ROLE_SECONDARY ||
|
|
|
|
(res->hr_remotein == NULL &&
|
|
|
|
res->hr_remoteout == NULL));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (tres->hr_remotein != NULL)
|
|
|
|
proto_close(tres->hr_remotein);
|
|
|
|
if (tres->hr_remoteout != NULL)
|
|
|
|
proto_close(tres->hr_remoteout);
|
2011-02-06 12:21:29 +00:00
|
|
|
if (tres->hr_ctrl != NULL)
|
|
|
|
proto_close(tres->hr_ctrl);
|
|
|
|
if (tres->hr_event != NULL)
|
|
|
|
proto_close(tres->hr_event);
|
|
|
|
if (tres->hr_conn != NULL)
|
|
|
|
proto_close(tres->hr_conn);
|
2012-01-22 11:20:42 +00:00
|
|
|
TAILQ_REMOVE(&cfg->hc_resources, tres, hr_next);
|
|
|
|
free(tres);
|
2011-01-28 21:48:15 +00:00
|
|
|
}
|
|
|
|
if (cfg->hc_controlin != NULL)
|
|
|
|
proto_close(cfg->hc_controlin);
|
|
|
|
proto_close(cfg->hc_controlconn);
|
2012-01-22 11:20:42 +00:00
|
|
|
while ((lst = TAILQ_FIRST(&cfg->hc_listen)) != NULL) {
|
|
|
|
TAILQ_REMOVE(&cfg->hc_listen, lst, hl_next);
|
2011-05-19 23:18:42 +00:00
|
|
|
if (lst->hl_conn != NULL)
|
|
|
|
proto_close(lst->hl_conn);
|
2012-01-22 11:20:42 +00:00
|
|
|
free(lst);
|
2011-05-19 23:18:42 +00:00
|
|
|
}
|
2011-01-28 21:48:15 +00:00
|
|
|
(void)pidfile_close(pfh);
|
|
|
|
hook_fini();
|
|
|
|
pjdlog_fini();
|
|
|
|
}
|
|
|
|
|
2011-01-28 21:56:47 +00:00
|
|
|
static const char *
|
|
|
|
dtype2str(mode_t mode)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (S_ISBLK(mode))
|
|
|
|
return ("block device");
|
2011-03-22 10:39:34 +00:00
|
|
|
else if (S_ISCHR(mode))
|
2011-01-28 21:56:47 +00:00
|
|
|
return ("character device");
|
2011-03-22 10:39:34 +00:00
|
|
|
else if (S_ISDIR(mode))
|
2011-01-28 21:56:47 +00:00
|
|
|
return ("directory");
|
|
|
|
else if (S_ISFIFO(mode))
|
|
|
|
return ("pipe or FIFO");
|
2011-03-22 10:39:34 +00:00
|
|
|
else if (S_ISLNK(mode))
|
2011-01-28 21:56:47 +00:00
|
|
|
return ("symbolic link");
|
2011-03-22 10:39:34 +00:00
|
|
|
else if (S_ISREG(mode))
|
2011-01-28 21:56:47 +00:00
|
|
|
return ("regular file");
|
|
|
|
else if (S_ISSOCK(mode))
|
|
|
|
return ("socket");
|
2011-03-22 10:39:34 +00:00
|
|
|
else if (S_ISWHT(mode))
|
2011-01-28 21:56:47 +00:00
|
|
|
return ("whiteout");
|
|
|
|
else
|
|
|
|
return ("unknown");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
descriptors_assert(const struct hast_resource *res, int pjdlogmode)
|
|
|
|
{
|
|
|
|
char msg[256];
|
|
|
|
struct stat sb;
|
|
|
|
long maxfd;
|
|
|
|
bool isopen;
|
|
|
|
mode_t mode;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point descriptor to syslog socket is closed, so if we want
|
|
|
|
* to log assertion message, we have to first store it in 'msg' local
|
|
|
|
* buffer and then open syslog socket and log it.
|
|
|
|
*/
|
|
|
|
msg[0] = '\0';
|
|
|
|
|
|
|
|
maxfd = sysconf(_SC_OPEN_MAX);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (maxfd == -1) {
|
2011-02-06 14:06:37 +00:00
|
|
|
pjdlog_init(pjdlogmode);
|
|
|
|
pjdlog_prefix_set("[%s] (%s) ", res->hr_name,
|
|
|
|
role2str(res->hr_role));
|
2011-01-28 21:56:47 +00:00
|
|
|
pjdlog_errno(LOG_WARNING, "sysconf(_SC_OPEN_MAX) failed");
|
2011-02-06 14:06:37 +00:00
|
|
|
pjdlog_fini();
|
2011-01-28 21:56:47 +00:00
|
|
|
maxfd = 16384;
|
|
|
|
}
|
|
|
|
for (fd = 0; fd <= maxfd; fd++) {
|
|
|
|
if (fstat(fd, &sb) == 0) {
|
|
|
|
isopen = true;
|
|
|
|
mode = sb.st_mode;
|
|
|
|
} else if (errno == EBADF) {
|
|
|
|
isopen = false;
|
|
|
|
mode = 0;
|
|
|
|
} else {
|
2011-02-06 14:09:19 +00:00
|
|
|
(void)snprintf(msg, sizeof(msg),
|
2011-01-28 21:56:47 +00:00
|
|
|
"Unable to fstat descriptor %d: %s", fd,
|
|
|
|
strerror(errno));
|
2011-02-06 14:07:58 +00:00
|
|
|
break;
|
2011-01-28 21:56:47 +00:00
|
|
|
}
|
|
|
|
if (fd == STDIN_FILENO || fd == STDOUT_FILENO ||
|
|
|
|
fd == STDERR_FILENO) {
|
|
|
|
if (!isopen) {
|
2011-02-06 14:09:19 +00:00
|
|
|
(void)snprintf(msg, sizeof(msg),
|
2011-01-28 21:56:47 +00:00
|
|
|
"Descriptor %d (%s) is closed, but should be open.",
|
|
|
|
fd, (fd == STDIN_FILENO ? "stdin" :
|
|
|
|
(fd == STDOUT_FILENO ? "stdout" : "stderr")));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (fd == proto_descriptor(res->hr_event)) {
|
|
|
|
if (!isopen) {
|
2011-02-06 14:09:19 +00:00
|
|
|
(void)snprintf(msg, sizeof(msg),
|
2011-01-28 21:56:47 +00:00
|
|
|
"Descriptor %d (event) is closed, but should be open.",
|
|
|
|
fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!S_ISSOCK(mode)) {
|
2011-02-06 14:09:19 +00:00
|
|
|
(void)snprintf(msg, sizeof(msg),
|
2011-01-28 21:56:47 +00:00
|
|
|
"Descriptor %d (event) is %s, but should be %s.",
|
|
|
|
fd, dtype2str(mode), dtype2str(S_IFSOCK));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (fd == proto_descriptor(res->hr_ctrl)) {
|
|
|
|
if (!isopen) {
|
2011-02-06 14:09:19 +00:00
|
|
|
(void)snprintf(msg, sizeof(msg),
|
2011-01-28 21:56:47 +00:00
|
|
|
"Descriptor %d (ctrl) is closed, but should be open.",
|
|
|
|
fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!S_ISSOCK(mode)) {
|
2011-02-06 14:09:19 +00:00
|
|
|
(void)snprintf(msg, sizeof(msg),
|
2011-01-28 21:56:47 +00:00
|
|
|
"Descriptor %d (ctrl) is %s, but should be %s.",
|
|
|
|
fd, dtype2str(mode), dtype2str(S_IFSOCK));
|
|
|
|
break;
|
|
|
|
}
|
2011-03-23 11:09:04 +00:00
|
|
|
} else if (res->hr_role == HAST_ROLE_PRIMARY &&
|
|
|
|
fd == proto_descriptor(res->hr_conn)) {
|
2011-02-03 11:39:49 +00:00
|
|
|
if (!isopen) {
|
2011-02-06 14:09:19 +00:00
|
|
|
(void)snprintf(msg, sizeof(msg),
|
2011-02-03 11:39:49 +00:00
|
|
|
"Descriptor %d (conn) is closed, but should be open.",
|
|
|
|
fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!S_ISSOCK(mode)) {
|
2011-02-06 14:09:19 +00:00
|
|
|
(void)snprintf(msg, sizeof(msg),
|
2011-02-03 11:39:49 +00:00
|
|
|
"Descriptor %d (conn) is %s, but should be %s.",
|
|
|
|
fd, dtype2str(mode), dtype2str(S_IFSOCK));
|
|
|
|
break;
|
|
|
|
}
|
2011-03-23 11:09:04 +00:00
|
|
|
} else if (res->hr_role == HAST_ROLE_SECONDARY &&
|
|
|
|
res->hr_conn != NULL &&
|
|
|
|
fd == proto_descriptor(res->hr_conn)) {
|
|
|
|
if (isopen) {
|
|
|
|
(void)snprintf(msg, sizeof(msg),
|
|
|
|
"Descriptor %d (conn) is open, but should be closed.",
|
|
|
|
fd);
|
|
|
|
break;
|
|
|
|
}
|
2011-01-28 21:56:47 +00:00
|
|
|
} else if (res->hr_role == HAST_ROLE_SECONDARY &&
|
|
|
|
fd == proto_descriptor(res->hr_remotein)) {
|
|
|
|
if (!isopen) {
|
2011-02-06 14:09:19 +00:00
|
|
|
(void)snprintf(msg, sizeof(msg),
|
2011-01-28 21:56:47 +00:00
|
|
|
"Descriptor %d (remote in) is closed, but should be open.",
|
|
|
|
fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!S_ISSOCK(mode)) {
|
2011-02-06 14:09:19 +00:00
|
|
|
(void)snprintf(msg, sizeof(msg),
|
2011-01-28 21:56:47 +00:00
|
|
|
"Descriptor %d (remote in) is %s, but should be %s.",
|
|
|
|
fd, dtype2str(mode), dtype2str(S_IFSOCK));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (res->hr_role == HAST_ROLE_SECONDARY &&
|
|
|
|
fd == proto_descriptor(res->hr_remoteout)) {
|
|
|
|
if (!isopen) {
|
2011-02-06 14:09:19 +00:00
|
|
|
(void)snprintf(msg, sizeof(msg),
|
2011-01-28 21:56:47 +00:00
|
|
|
"Descriptor %d (remote out) is closed, but should be open.",
|
|
|
|
fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!S_ISSOCK(mode)) {
|
2011-02-06 14:09:19 +00:00
|
|
|
(void)snprintf(msg, sizeof(msg),
|
2011-01-28 21:56:47 +00:00
|
|
|
"Descriptor %d (remote out) is %s, but should be %s.",
|
|
|
|
fd, dtype2str(mode), dtype2str(S_IFSOCK));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isopen) {
|
2011-02-06 14:09:19 +00:00
|
|
|
(void)snprintf(msg, sizeof(msg),
|
2011-01-28 21:56:47 +00:00
|
|
|
"Descriptor %d is open (%s), but should be closed.",
|
|
|
|
fd, dtype2str(mode));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (msg[0] != '\0') {
|
|
|
|
pjdlog_init(pjdlogmode);
|
|
|
|
pjdlog_prefix_set("[%s] (%s) ", res->hr_name,
|
|
|
|
role2str(res->hr_role));
|
|
|
|
PJDLOG_ABORT("%s", msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-29 15:42:24 +00:00
|
|
|
static void
|
|
|
|
child_exit_log(unsigned int pid, int status)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
|
|
|
|
pjdlog_debug(1, "Worker process exited gracefully (pid=%u).",
|
|
|
|
pid);
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
pjdlog_error("Worker process killed (pid=%u, signal=%d).",
|
|
|
|
pid, WTERMSIG(status));
|
|
|
|
} else {
|
|
|
|
pjdlog_error("Worker process exited ungracefully (pid=%u, exitcode=%d).",
|
|
|
|
pid, WIFEXITED(status) ? WEXITSTATUS(status) : -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
static void
|
|
|
|
child_exit(void)
|
|
|
|
{
|
|
|
|
struct hast_resource *res;
|
|
|
|
int status;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
while ((pid = wait3(&status, WNOHANG, NULL)) > 0) {
|
|
|
|
/* Find resource related to the process that just exited. */
|
|
|
|
TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
|
|
|
|
if (pid == res->hr_workerpid)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (res == NULL) {
|
|
|
|
/*
|
|
|
|
* This can happen when new connection arrives and we
|
2010-08-29 21:41:53 +00:00
|
|
|
* cancel child responsible for the old one or if this
|
|
|
|
* was hook which we executed.
|
2010-02-18 23:16:19 +00:00
|
|
|
*/
|
2010-08-29 21:41:53 +00:00
|
|
|
hook_check_one(pid, status);
|
2010-02-18 23:16:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pjdlog_prefix_set("[%s] (%s) ", res->hr_name,
|
|
|
|
role2str(res->hr_role));
|
2010-04-29 15:42:24 +00:00
|
|
|
child_exit_log(pid, status);
|
2010-09-22 18:57:06 +00:00
|
|
|
child_cleanup(res);
|
2010-02-18 23:16:19 +00:00
|
|
|
if (res->hr_role == HAST_ROLE_PRIMARY) {
|
2010-04-29 15:42:24 +00:00
|
|
|
/*
|
|
|
|
* Restart child process if it was killed by signal
|
|
|
|
* or exited because of temporary problem.
|
|
|
|
*/
|
|
|
|
if (WIFSIGNALED(status) ||
|
|
|
|
(WIFEXITED(status) &&
|
|
|
|
WEXITSTATUS(status) == EX_TEMPFAIL)) {
|
2010-04-28 22:41:06 +00:00
|
|
|
sleep(1);
|
|
|
|
pjdlog_info("Restarting worker process.");
|
|
|
|
hastd_primary(res);
|
|
|
|
} else {
|
|
|
|
res->hr_role = HAST_ROLE_INIT;
|
|
|
|
pjdlog_info("Changing resource role back to %s.",
|
|
|
|
role2str(res->hr_role));
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
pjdlog_prefix_set("%s", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-05 19:16:31 +00:00
|
|
|
static bool
|
|
|
|
resource_needs_restart(const struct hast_resource *res0,
|
|
|
|
const struct hast_resource *res1)
|
|
|
|
{
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(strcmp(res0->hr_name, res1->hr_name) == 0);
|
2010-08-05 19:16:31 +00:00
|
|
|
|
|
|
|
if (strcmp(res0->hr_provname, res1->hr_provname) != 0)
|
|
|
|
return (true);
|
|
|
|
if (strcmp(res0->hr_localpath, res1->hr_localpath) != 0)
|
|
|
|
return (true);
|
|
|
|
if (res0->hr_role == HAST_ROLE_INIT ||
|
|
|
|
res0->hr_role == HAST_ROLE_SECONDARY) {
|
|
|
|
if (strcmp(res0->hr_remoteaddr, res1->hr_remoteaddr) != 0)
|
|
|
|
return (true);
|
2011-03-21 08:54:59 +00:00
|
|
|
if (strcmp(res0->hr_sourceaddr, res1->hr_sourceaddr) != 0)
|
|
|
|
return (true);
|
2010-08-05 19:16:31 +00:00
|
|
|
if (res0->hr_replication != res1->hr_replication)
|
|
|
|
return (true);
|
2011-03-06 22:56:14 +00:00
|
|
|
if (res0->hr_checksum != res1->hr_checksum)
|
|
|
|
return (true);
|
2011-03-06 23:09:33 +00:00
|
|
|
if (res0->hr_compression != res1->hr_compression)
|
|
|
|
return (true);
|
2010-08-05 19:16:31 +00:00
|
|
|
if (res0->hr_timeout != res1->hr_timeout)
|
|
|
|
return (true);
|
2010-08-27 15:16:52 +00:00
|
|
|
if (strcmp(res0->hr_exec, res1->hr_exec) != 0)
|
|
|
|
return (true);
|
2011-09-28 13:08:51 +00:00
|
|
|
/*
|
|
|
|
* When metaflush has changed we don't really need restart,
|
|
|
|
* but it is just easier this way.
|
|
|
|
*/
|
|
|
|
if (res0->hr_metaflush != res1->hr_metaflush)
|
|
|
|
return (true);
|
2010-08-05 19:16:31 +00:00
|
|
|
}
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
resource_needs_reload(const struct hast_resource *res0,
|
|
|
|
const struct hast_resource *res1)
|
|
|
|
{
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(strcmp(res0->hr_name, res1->hr_name) == 0);
|
|
|
|
PJDLOG_ASSERT(strcmp(res0->hr_provname, res1->hr_provname) == 0);
|
|
|
|
PJDLOG_ASSERT(strcmp(res0->hr_localpath, res1->hr_localpath) == 0);
|
2010-08-05 19:16:31 +00:00
|
|
|
|
|
|
|
if (res0->hr_role != HAST_ROLE_PRIMARY)
|
|
|
|
return (false);
|
|
|
|
|
|
|
|
if (strcmp(res0->hr_remoteaddr, res1->hr_remoteaddr) != 0)
|
|
|
|
return (true);
|
2011-03-21 08:54:59 +00:00
|
|
|
if (strcmp(res0->hr_sourceaddr, res1->hr_sourceaddr) != 0)
|
|
|
|
return (true);
|
2010-08-05 19:16:31 +00:00
|
|
|
if (res0->hr_replication != res1->hr_replication)
|
|
|
|
return (true);
|
2011-03-06 22:56:14 +00:00
|
|
|
if (res0->hr_checksum != res1->hr_checksum)
|
|
|
|
return (true);
|
2011-03-06 23:09:33 +00:00
|
|
|
if (res0->hr_compression != res1->hr_compression)
|
|
|
|
return (true);
|
2010-08-05 19:16:31 +00:00
|
|
|
if (res0->hr_timeout != res1->hr_timeout)
|
|
|
|
return (true);
|
2010-08-27 15:16:52 +00:00
|
|
|
if (strcmp(res0->hr_exec, res1->hr_exec) != 0)
|
|
|
|
return (true);
|
2011-09-28 13:08:51 +00:00
|
|
|
if (res0->hr_metaflush != res1->hr_metaflush)
|
|
|
|
return (true);
|
2010-08-05 19:16:31 +00:00
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2011-01-24 15:04:15 +00:00
|
|
|
static void
|
|
|
|
resource_reload(const struct hast_resource *res)
|
|
|
|
{
|
|
|
|
struct nv *nvin, *nvout;
|
|
|
|
int error;
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
|
2011-01-24 15:04:15 +00:00
|
|
|
|
|
|
|
nvout = nv_alloc();
|
2011-04-26 19:38:30 +00:00
|
|
|
nv_add_uint8(nvout, CONTROL_RELOAD, "cmd");
|
2011-01-24 15:04:15 +00:00
|
|
|
nv_add_string(nvout, res->hr_remoteaddr, "remoteaddr");
|
2011-03-21 08:54:59 +00:00
|
|
|
nv_add_string(nvout, res->hr_sourceaddr, "sourceaddr");
|
2011-01-24 15:04:15 +00:00
|
|
|
nv_add_int32(nvout, (int32_t)res->hr_replication, "replication");
|
2011-03-06 22:56:14 +00:00
|
|
|
nv_add_int32(nvout, (int32_t)res->hr_checksum, "checksum");
|
2011-03-06 23:09:33 +00:00
|
|
|
nv_add_int32(nvout, (int32_t)res->hr_compression, "compression");
|
2011-01-24 15:04:15 +00:00
|
|
|
nv_add_int32(nvout, (int32_t)res->hr_timeout, "timeout");
|
|
|
|
nv_add_string(nvout, res->hr_exec, "exec");
|
2011-09-28 13:08:51 +00:00
|
|
|
nv_add_int32(nvout, (int32_t)res->hr_metaflush, "metaflush");
|
2011-01-24 15:04:15 +00:00
|
|
|
if (nv_error(nvout) != 0) {
|
|
|
|
nv_free(nvout);
|
|
|
|
pjdlog_error("Unable to allocate header for reload message.");
|
|
|
|
return;
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
if (hast_proto_send(res, res->hr_ctrl, nvout, NULL, 0) == -1) {
|
2011-01-24 15:04:15 +00:00
|
|
|
pjdlog_errno(LOG_ERR, "Unable to send reload message");
|
|
|
|
nv_free(nvout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nv_free(nvout);
|
|
|
|
|
|
|
|
/* Receive response. */
|
2012-01-10 22:39:07 +00:00
|
|
|
if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) == -1) {
|
2011-01-24 15:04:15 +00:00
|
|
|
pjdlog_errno(LOG_ERR, "Unable to receive reload reply");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
error = nv_get_int16(nvin, "error");
|
|
|
|
nv_free(nvin);
|
|
|
|
if (error != 0) {
|
|
|
|
pjdlog_common(LOG_ERR, 0, error, "Reload failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
static void
|
|
|
|
hastd_reload(void)
|
|
|
|
{
|
2010-08-05 19:16:31 +00:00
|
|
|
struct hastd_config *newcfg;
|
|
|
|
struct hast_resource *nres, *cres, *tres;
|
2011-05-19 23:18:42 +00:00
|
|
|
struct hastd_listen *nlst, *clst;
|
2011-10-17 12:22:09 +00:00
|
|
|
struct pidfh *newpfh;
|
2011-05-19 23:18:42 +00:00
|
|
|
unsigned int nlisten;
|
2010-08-05 19:16:31 +00:00
|
|
|
uint8_t role;
|
2011-10-17 12:22:09 +00:00
|
|
|
pid_t otherpid;
|
2010-08-05 19:16:31 +00:00
|
|
|
|
|
|
|
pjdlog_info("Reloading configuration...");
|
|
|
|
|
2011-10-17 12:22:09 +00:00
|
|
|
newpfh = NULL;
|
|
|
|
|
2010-08-05 19:16:31 +00:00
|
|
|
newcfg = yy_config_parse(cfgpath, false);
|
|
|
|
if (newcfg == NULL)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if control address has changed.
|
|
|
|
*/
|
|
|
|
if (strcmp(cfg->hc_controladdr, newcfg->hc_controladdr) != 0) {
|
|
|
|
if (proto_server(newcfg->hc_controladdr,
|
2012-01-10 22:39:07 +00:00
|
|
|
&newcfg->hc_controlconn) == -1) {
|
2010-08-05 19:16:31 +00:00
|
|
|
pjdlog_errno(LOG_ERR,
|
|
|
|
"Unable to listen on control address %s",
|
|
|
|
newcfg->hc_controladdr);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
2011-05-19 23:18:42 +00:00
|
|
|
* Check if any listen address has changed.
|
2010-08-05 19:16:31 +00:00
|
|
|
*/
|
2011-05-19 23:18:42 +00:00
|
|
|
nlisten = 0;
|
|
|
|
TAILQ_FOREACH(nlst, &newcfg->hc_listen, hl_next) {
|
|
|
|
TAILQ_FOREACH(clst, &cfg->hc_listen, hl_next) {
|
|
|
|
if (strcmp(nlst->hl_addr, clst->hl_addr) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (clst != NULL && clst->hl_conn != NULL) {
|
|
|
|
pjdlog_info("Keep listening on address %s.",
|
|
|
|
nlst->hl_addr);
|
|
|
|
nlst->hl_conn = clst->hl_conn;
|
|
|
|
nlisten++;
|
|
|
|
} else if (proto_server(nlst->hl_addr, &nlst->hl_conn) == 0) {
|
|
|
|
pjdlog_info("Listening on new address %s.",
|
|
|
|
nlst->hl_addr);
|
|
|
|
nlisten++;
|
|
|
|
} else {
|
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to listen on address %s", nlst->hl_addr);
|
2010-08-05 19:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-19 23:18:42 +00:00
|
|
|
if (nlisten == 0) {
|
|
|
|
pjdlog_error("No addresses to listen on.");
|
|
|
|
goto failed;
|
|
|
|
}
|
2011-10-17 12:22:09 +00:00
|
|
|
/*
|
|
|
|
* Check if pidfile's path has changed.
|
|
|
|
*/
|
2012-03-29 20:11:16 +00:00
|
|
|
if (!foreground && pidfile == NULL &&
|
|
|
|
strcmp(cfg->hc_pidfile, newcfg->hc_pidfile) != 0) {
|
2011-10-17 12:22:09 +00:00
|
|
|
newpfh = pidfile_open(newcfg->hc_pidfile, 0600, &otherpid);
|
|
|
|
if (newpfh == NULL) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Another hastd is already running, pidfile: %s, pid: %jd.",
|
|
|
|
newcfg->hc_pidfile, (intmax_t)otherpid);
|
|
|
|
} else {
|
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to open or create pidfile %s",
|
|
|
|
newcfg->hc_pidfile);
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
} else if (pidfile_write(newpfh) == -1) {
|
2011-10-17 12:22:09 +00:00
|
|
|
/* Write PID to a file. */
|
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to write PID to file %s",
|
|
|
|
newcfg->hc_pidfile);
|
|
|
|
} else {
|
|
|
|
pjdlog_debug(1, "PID stored in %s.",
|
|
|
|
newcfg->hc_pidfile);
|
|
|
|
}
|
|
|
|
}
|
2011-05-19 23:18:42 +00:00
|
|
|
|
|
|
|
/* No failures from now on. */
|
|
|
|
|
2010-08-05 19:16:31 +00:00
|
|
|
/*
|
2011-05-19 23:18:42 +00:00
|
|
|
* Switch to new control socket.
|
2010-08-05 19:16:31 +00:00
|
|
|
*/
|
|
|
|
if (newcfg->hc_controlconn != NULL) {
|
|
|
|
pjdlog_info("Control socket changed from %s to %s.",
|
|
|
|
cfg->hc_controladdr, newcfg->hc_controladdr);
|
|
|
|
proto_close(cfg->hc_controlconn);
|
|
|
|
cfg->hc_controlconn = newcfg->hc_controlconn;
|
|
|
|
newcfg->hc_controlconn = NULL;
|
|
|
|
strlcpy(cfg->hc_controladdr, newcfg->hc_controladdr,
|
|
|
|
sizeof(cfg->hc_controladdr));
|
|
|
|
}
|
2011-10-17 12:22:09 +00:00
|
|
|
/*
|
|
|
|
* Switch to new pidfile.
|
|
|
|
*/
|
2012-01-10 22:41:09 +00:00
|
|
|
if (newpfh != NULL) {
|
|
|
|
pjdlog_info("Pidfile changed from %s to %s.", cfg->hc_pidfile,
|
|
|
|
newcfg->hc_pidfile);
|
|
|
|
(void)pidfile_remove(pfh);
|
|
|
|
pfh = newpfh;
|
|
|
|
(void)strlcpy(cfg->hc_pidfile, newcfg->hc_pidfile,
|
|
|
|
sizeof(cfg->hc_pidfile));
|
|
|
|
}
|
2011-05-19 23:18:42 +00:00
|
|
|
/*
|
|
|
|
* Switch to new listen addresses. Close all that were removed.
|
|
|
|
*/
|
|
|
|
while ((clst = TAILQ_FIRST(&cfg->hc_listen)) != NULL) {
|
|
|
|
TAILQ_FOREACH(nlst, &newcfg->hc_listen, hl_next) {
|
|
|
|
if (strcmp(nlst->hl_addr, clst->hl_addr) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (nlst == NULL && clst->hl_conn != NULL) {
|
|
|
|
proto_close(clst->hl_conn);
|
|
|
|
pjdlog_info("No longer listening on address %s.",
|
|
|
|
clst->hl_addr);
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&cfg->hc_listen, clst, hl_next);
|
|
|
|
free(clst);
|
2010-08-05 19:16:31 +00:00
|
|
|
}
|
2011-05-19 23:18:42 +00:00
|
|
|
TAILQ_CONCAT(&cfg->hc_listen, &newcfg->hc_listen, hl_next);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2010-08-05 19:16:31 +00:00
|
|
|
/*
|
|
|
|
* Stop and remove resources that were removed from the configuration.
|
|
|
|
*/
|
|
|
|
TAILQ_FOREACH_SAFE(cres, &cfg->hc_resources, hr_next, tres) {
|
|
|
|
TAILQ_FOREACH(nres, &newcfg->hc_resources, hr_next) {
|
|
|
|
if (strcmp(cres->hr_name, nres->hr_name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (nres == NULL) {
|
|
|
|
control_set_role(cres, HAST_ROLE_INIT);
|
|
|
|
TAILQ_REMOVE(&cfg->hc_resources, cres, hr_next);
|
|
|
|
pjdlog_info("Resource %s removed.", cres->hr_name);
|
|
|
|
free(cres);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Move new resources to the current configuration.
|
|
|
|
*/
|
|
|
|
TAILQ_FOREACH_SAFE(nres, &newcfg->hc_resources, hr_next, tres) {
|
|
|
|
TAILQ_FOREACH(cres, &cfg->hc_resources, hr_next) {
|
|
|
|
if (strcmp(cres->hr_name, nres->hr_name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cres == NULL) {
|
|
|
|
TAILQ_REMOVE(&newcfg->hc_resources, nres, hr_next);
|
|
|
|
TAILQ_INSERT_TAIL(&cfg->hc_resources, nres, hr_next);
|
|
|
|
pjdlog_info("Resource %s added.", nres->hr_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Deal with modified resources.
|
|
|
|
* Depending on what has changed exactly we might want to perform
|
|
|
|
* different actions.
|
|
|
|
*
|
|
|
|
* We do full resource restart in the following situations:
|
|
|
|
* Resource role is INIT or SECONDARY.
|
|
|
|
* Resource role is PRIMARY and path to local component or provider
|
|
|
|
* name has changed.
|
|
|
|
* In case of PRIMARY, the worker process will be killed and restarted,
|
|
|
|
* which also means removing /dev/hast/<name> provider and
|
|
|
|
* recreating it.
|
|
|
|
*
|
|
|
|
* We do just reload (send SIGHUP to worker process) if we act as
|
2011-09-28 13:08:51 +00:00
|
|
|
* PRIMARY, but only if remote address, source address, replication
|
|
|
|
* mode, timeout, execution path or metaflush has changed.
|
|
|
|
* For those, there is no need to restart worker process.
|
2010-08-05 19:16:31 +00:00
|
|
|
* If PRIMARY receives SIGHUP, it will reconnect if remote address or
|
2011-09-28 13:08:51 +00:00
|
|
|
* source address has changed or it will set new timeout if only timeout
|
|
|
|
* has changed or it will update metaflush if only metaflush has
|
|
|
|
* changed.
|
2010-08-05 19:16:31 +00:00
|
|
|
*/
|
|
|
|
TAILQ_FOREACH_SAFE(nres, &newcfg->hc_resources, hr_next, tres) {
|
|
|
|
TAILQ_FOREACH(cres, &cfg->hc_resources, hr_next) {
|
|
|
|
if (strcmp(cres->hr_name, nres->hr_name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(cres != NULL);
|
2010-08-05 19:16:31 +00:00
|
|
|
if (resource_needs_restart(cres, nres)) {
|
|
|
|
pjdlog_info("Resource %s configuration was modified, restarting it.",
|
|
|
|
cres->hr_name);
|
|
|
|
role = cres->hr_role;
|
|
|
|
control_set_role(cres, HAST_ROLE_INIT);
|
|
|
|
TAILQ_REMOVE(&cfg->hc_resources, cres, hr_next);
|
|
|
|
free(cres);
|
|
|
|
TAILQ_REMOVE(&newcfg->hc_resources, nres, hr_next);
|
|
|
|
TAILQ_INSERT_TAIL(&cfg->hc_resources, nres, hr_next);
|
|
|
|
control_set_role(nres, role);
|
|
|
|
} else if (resource_needs_reload(cres, nres)) {
|
|
|
|
pjdlog_info("Resource %s configuration was modified, reloading it.",
|
|
|
|
cres->hr_name);
|
|
|
|
strlcpy(cres->hr_remoteaddr, nres->hr_remoteaddr,
|
|
|
|
sizeof(cres->hr_remoteaddr));
|
2011-03-21 08:54:59 +00:00
|
|
|
strlcpy(cres->hr_sourceaddr, nres->hr_sourceaddr,
|
|
|
|
sizeof(cres->hr_sourceaddr));
|
2010-08-05 19:16:31 +00:00
|
|
|
cres->hr_replication = nres->hr_replication;
|
2011-03-06 22:56:14 +00:00
|
|
|
cres->hr_checksum = nres->hr_checksum;
|
2011-03-06 23:09:33 +00:00
|
|
|
cres->hr_compression = nres->hr_compression;
|
2010-08-05 19:16:31 +00:00
|
|
|
cres->hr_timeout = nres->hr_timeout;
|
2011-01-22 22:31:55 +00:00
|
|
|
strlcpy(cres->hr_exec, nres->hr_exec,
|
|
|
|
sizeof(cres->hr_exec));
|
2011-09-28 13:08:51 +00:00
|
|
|
cres->hr_metaflush = nres->hr_metaflush;
|
2011-01-24 15:04:15 +00:00
|
|
|
if (cres->hr_workerpid != 0)
|
|
|
|
resource_reload(cres);
|
2010-08-05 19:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
yy_config_free(newcfg);
|
|
|
|
pjdlog_info("Configuration reloaded successfully.");
|
|
|
|
return;
|
|
|
|
failed:
|
|
|
|
if (newcfg != NULL) {
|
|
|
|
if (newcfg->hc_controlconn != NULL)
|
|
|
|
proto_close(newcfg->hc_controlconn);
|
2011-05-19 23:18:42 +00:00
|
|
|
while ((nlst = TAILQ_FIRST(&newcfg->hc_listen)) != NULL) {
|
|
|
|
if (nlst->hl_conn != NULL) {
|
|
|
|
TAILQ_FOREACH(clst, &cfg->hc_listen, hl_next) {
|
|
|
|
if (strcmp(nlst->hl_addr,
|
|
|
|
clst->hl_addr) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (clst == NULL || clst->hl_conn == NULL)
|
|
|
|
proto_close(nlst->hl_conn);
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&newcfg->hc_listen, nlst, hl_next);
|
|
|
|
free(nlst);
|
|
|
|
}
|
2010-08-05 19:16:31 +00:00
|
|
|
yy_config_free(newcfg);
|
|
|
|
}
|
2011-10-17 12:22:09 +00:00
|
|
|
if (newpfh != NULL)
|
|
|
|
(void)pidfile_remove(newpfh);
|
2010-08-05 19:16:31 +00:00
|
|
|
pjdlog_warning("Configuration not reloaded.");
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
2010-08-27 21:28:02 +00:00
|
|
|
static void
|
|
|
|
terminate_workers(void)
|
|
|
|
{
|
|
|
|
struct hast_resource *res;
|
|
|
|
|
|
|
|
pjdlog_info("Termination signal received, exiting.");
|
|
|
|
TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
|
|
|
|
if (res->hr_workerpid == 0)
|
|
|
|
continue;
|
|
|
|
pjdlog_info("Terminating worker process (resource=%s, role=%s, pid=%u).",
|
|
|
|
res->hr_name, role2str(res->hr_role), res->hr_workerpid);
|
|
|
|
if (kill(res->hr_workerpid, SIGTERM) == 0)
|
|
|
|
continue;
|
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to send signal to worker process (resource=%s, role=%s, pid=%u).",
|
|
|
|
res->hr_name, role2str(res->hr_role), res->hr_workerpid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
static void
|
2011-05-19 23:18:42 +00:00
|
|
|
listen_accept(struct hastd_listen *lst)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
|
|
|
struct hast_resource *res;
|
|
|
|
struct proto_conn *conn;
|
|
|
|
struct nv *nvin, *nvout, *nverr;
|
|
|
|
const char *resname;
|
|
|
|
const unsigned char *token;
|
|
|
|
char laddr[256], raddr[256];
|
2013-02-17 21:12:34 +00:00
|
|
|
uint8_t version;
|
2010-02-18 23:16:19 +00:00
|
|
|
size_t size;
|
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
|
2011-05-19 23:18:42 +00:00
|
|
|
proto_local_address(lst->hl_conn, laddr, sizeof(laddr));
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_debug(1, "Accepting connection to %s.", laddr);
|
|
|
|
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_accept(lst->hl_conn, &conn) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_ERR, "Unable to accept connection %s", laddr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
proto_local_address(conn, laddr, sizeof(laddr));
|
|
|
|
proto_remote_address(conn, raddr, sizeof(raddr));
|
2010-06-14 21:46:48 +00:00
|
|
|
pjdlog_info("Connection from %s to %s.", raddr, laddr);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2010-04-29 15:36:32 +00:00
|
|
|
/* Error in setting timeout is not critical, but why should it fail? */
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_timeout(conn, HAST_TIMEOUT) == -1)
|
2010-04-29 15:36:32 +00:00
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
nvin = nvout = nverr = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Before receiving any data see if remote host have access to any
|
|
|
|
* resource.
|
|
|
|
*/
|
|
|
|
TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
|
|
|
|
if (proto_address_match(conn, res->hr_remoteaddr))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (res == NULL) {
|
|
|
|
pjdlog_error("Client %s isn't known.", raddr);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
/* Ok, remote host can access at least one resource. */
|
|
|
|
|
2012-01-10 22:39:07 +00:00
|
|
|
if (hast_proto_recv_hdr(conn, &nvin) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_ERR, "Unable to receive header from %s",
|
|
|
|
raddr);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
|
|
|
|
resname = nv_get_string(nvin, "resource");
|
|
|
|
if (resname == NULL) {
|
|
|
|
pjdlog_error("No 'resource' field in the header received from %s.",
|
|
|
|
raddr);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
pjdlog_debug(2, "%s: resource=%s", raddr, resname);
|
2013-02-17 21:12:34 +00:00
|
|
|
version = nv_get_uint8(nvin, "version");
|
|
|
|
pjdlog_debug(2, "%s: version=%hhu", raddr, version);
|
|
|
|
if (version == 0) {
|
|
|
|
/*
|
|
|
|
* If no version is sent, it means this is protocol version 1.
|
|
|
|
*/
|
|
|
|
version = 1;
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
token = nv_get_uint8_array(nvin, &size, "token");
|
|
|
|
/*
|
2012-01-07 16:09:33 +00:00
|
|
|
* NULL token means that this is first connection.
|
2010-02-18 23:16:19 +00:00
|
|
|
*/
|
|
|
|
if (token != NULL && size != sizeof(res->hr_token)) {
|
|
|
|
pjdlog_error("Received token of invalid size from %s (expected %zu, got %zu).",
|
|
|
|
raddr, sizeof(res->hr_token), size);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* From now on we want to send errors to the remote node.
|
|
|
|
*/
|
|
|
|
nverr = nv_alloc();
|
|
|
|
|
|
|
|
/* Find resource related to this connection. */
|
|
|
|
TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
|
|
|
|
if (strcmp(resname, res->hr_name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Have we found the resource? */
|
|
|
|
if (res == NULL) {
|
|
|
|
pjdlog_error("No resource '%s' as requested by %s.",
|
|
|
|
resname, raddr);
|
|
|
|
nv_add_stringf(nverr, "errmsg", "Resource not configured.");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now that we know resource name setup log prefix. */
|
|
|
|
pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
|
|
|
|
|
|
|
|
/* Does the remote host have access to this resource? */
|
|
|
|
if (!proto_address_match(conn, res->hr_remoteaddr)) {
|
|
|
|
pjdlog_error("Client %s has no access to the resource.", raddr);
|
|
|
|
nv_add_stringf(nverr, "errmsg", "No access to the resource.");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
/* Is the resource marked as secondary? */
|
|
|
|
if (res->hr_role != HAST_ROLE_SECONDARY) {
|
2011-04-20 16:38:05 +00:00
|
|
|
pjdlog_warning("We act as %s for the resource and not as %s as requested by %s.",
|
2010-02-18 23:16:19 +00:00
|
|
|
role2str(res->hr_role), role2str(HAST_ROLE_SECONDARY),
|
|
|
|
raddr);
|
|
|
|
nv_add_stringf(nverr, "errmsg",
|
|
|
|
"Remote node acts as %s for the resource and not as %s.",
|
|
|
|
role2str(res->hr_role), role2str(HAST_ROLE_SECONDARY));
|
2011-04-20 18:43:28 +00:00
|
|
|
if (res->hr_role == HAST_ROLE_PRIMARY) {
|
|
|
|
/*
|
|
|
|
* If we act as primary request the other side to wait
|
2011-04-20 18:49:12 +00:00
|
|
|
* for us a bit, as we might be finishing cleanups.
|
2011-04-20 18:43:28 +00:00
|
|
|
*/
|
|
|
|
nv_add_uint8(nverr, 1, "wait");
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
/* Does token (if exists) match? */
|
|
|
|
if (token != NULL && memcmp(token, res->hr_token,
|
|
|
|
sizeof(res->hr_token)) != 0) {
|
|
|
|
pjdlog_error("Token received from %s doesn't match.", raddr);
|
2010-06-14 21:46:48 +00:00
|
|
|
nv_add_stringf(nverr, "errmsg", "Token doesn't match.");
|
2010-02-18 23:16:19 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If there is no token, but we have half-open connection
|
|
|
|
* (only remotein) or full connection (worker process is running)
|
|
|
|
* we have to cancel those and accept the new connection.
|
|
|
|
*/
|
|
|
|
if (token == NULL) {
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_remoteout == NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_debug(1, "Initial connection from %s.", raddr);
|
|
|
|
if (res->hr_workerpid != 0) {
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_remotein == NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_debug(1,
|
|
|
|
"Worker process exists (pid=%u), stopping it.",
|
|
|
|
(unsigned int)res->hr_workerpid);
|
|
|
|
/* Stop child process. */
|
2012-01-10 22:39:07 +00:00
|
|
|
if (kill(res->hr_workerpid, SIGINT) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_ERR,
|
|
|
|
"Unable to stop worker process (pid=%u)",
|
|
|
|
(unsigned int)res->hr_workerpid);
|
|
|
|
/*
|
|
|
|
* Other than logging the problem we
|
|
|
|
* ignore it - nothing smart to do.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
/* Wait for it to exit. */
|
|
|
|
else if ((pid = waitpid(res->hr_workerpid,
|
|
|
|
&status, 0)) != res->hr_workerpid) {
|
2010-04-29 15:42:24 +00:00
|
|
|
/* We can only log the problem. */
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_ERR,
|
|
|
|
"Waiting for worker process (pid=%u) failed",
|
|
|
|
(unsigned int)res->hr_workerpid);
|
|
|
|
} else {
|
2010-04-29 15:42:24 +00:00
|
|
|
child_exit_log(res->hr_workerpid, status);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
2010-09-22 18:57:06 +00:00
|
|
|
child_cleanup(res);
|
2010-02-18 23:16:19 +00:00
|
|
|
} else if (res->hr_remotein != NULL) {
|
|
|
|
char oaddr[256];
|
|
|
|
|
2010-10-17 15:47:27 +00:00
|
|
|
proto_remote_address(res->hr_remotein, oaddr,
|
|
|
|
sizeof(oaddr));
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_debug(1,
|
|
|
|
"Canceling half-open connection from %s on connection from %s.",
|
|
|
|
oaddr, raddr);
|
|
|
|
proto_close(res->hr_remotein);
|
|
|
|
res->hr_remotein = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks and cleanups are done.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (token == NULL) {
|
2013-12-10 20:09:49 +00:00
|
|
|
if (version > HAST_PROTO_VERSION) {
|
|
|
|
pjdlog_info("Remote protocol version %hhu is not supported, falling back to version %hhu.",
|
|
|
|
version, (unsigned char)HAST_PROTO_VERSION);
|
|
|
|
version = HAST_PROTO_VERSION;
|
|
|
|
}
|
|
|
|
pjdlog_debug(1, "Negotiated protocol version %hhu.", version);
|
2013-02-17 21:12:34 +00:00
|
|
|
res->hr_version = version;
|
2010-02-18 23:16:19 +00:00
|
|
|
arc4random_buf(res->hr_token, sizeof(res->hr_token));
|
|
|
|
nvout = nv_alloc();
|
2013-02-17 21:12:34 +00:00
|
|
|
nv_add_uint8(nvout, version, "version");
|
2010-02-18 23:16:19 +00:00
|
|
|
nv_add_uint8_array(nvout, res->hr_token,
|
|
|
|
sizeof(res->hr_token), "token");
|
|
|
|
if (nv_error(nvout) != 0) {
|
|
|
|
pjdlog_common(LOG_ERR, 0, nv_error(nvout),
|
|
|
|
"Unable to prepare return header for %s", raddr);
|
|
|
|
nv_add_stringf(nverr, "errmsg",
|
|
|
|
"Remote node was unable to prepare return header: %s.",
|
|
|
|
strerror(nv_error(nvout)));
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-02-17 21:12:34 +00:00
|
|
|
if (hast_proto_send(res, conn, nvout, NULL, 0) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
int error = errno;
|
|
|
|
|
|
|
|
pjdlog_errno(LOG_ERR, "Unable to send response to %s",
|
|
|
|
raddr);
|
|
|
|
nv_add_stringf(nverr, "errmsg",
|
|
|
|
"Remote node was unable to send response: %s.",
|
|
|
|
strerror(error));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
res->hr_remotein = conn;
|
|
|
|
pjdlog_debug(1, "Incoming connection from %s configured.",
|
|
|
|
raddr);
|
|
|
|
} else {
|
|
|
|
res->hr_remoteout = conn;
|
|
|
|
pjdlog_debug(1, "Outgoing connection to %s configured.", raddr);
|
|
|
|
hastd_secondary(res, nvin);
|
|
|
|
}
|
|
|
|
nv_free(nvin);
|
|
|
|
nv_free(nvout);
|
|
|
|
nv_free(nverr);
|
|
|
|
pjdlog_prefix_set("%s", "");
|
|
|
|
return;
|
|
|
|
fail:
|
|
|
|
if (nv_error(nverr) != 0) {
|
|
|
|
pjdlog_common(LOG_ERR, 0, nv_error(nverr),
|
|
|
|
"Unable to prepare error header for %s", raddr);
|
|
|
|
goto close;
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
if (hast_proto_send(NULL, conn, nverr, NULL, 0) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_ERR, "Unable to send error to %s", raddr);
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
close:
|
|
|
|
if (nvin != NULL)
|
|
|
|
nv_free(nvin);
|
|
|
|
if (nvout != NULL)
|
|
|
|
nv_free(nvout);
|
|
|
|
if (nverr != NULL)
|
|
|
|
nv_free(nverr);
|
|
|
|
proto_close(conn);
|
|
|
|
pjdlog_prefix_set("%s", "");
|
|
|
|
}
|
|
|
|
|
2011-02-03 11:39:49 +00:00
|
|
|
static void
|
|
|
|
connection_migrate(struct hast_resource *res)
|
|
|
|
{
|
|
|
|
struct proto_conn *conn;
|
|
|
|
int16_t val = 0;
|
|
|
|
|
2011-03-21 08:33:58 +00:00
|
|
|
pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
|
|
|
|
|
2011-03-23 11:09:04 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
|
|
|
|
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) {
|
2011-02-03 11:39:49 +00:00
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to receive connection command");
|
|
|
|
return;
|
|
|
|
}
|
2011-03-21 08:54:59 +00:00
|
|
|
if (proto_client(res->hr_sourceaddr[0] != '\0' ? res->hr_sourceaddr : NULL,
|
2012-01-10 22:39:07 +00:00
|
|
|
res->hr_remoteaddr, &conn) == -1) {
|
2011-02-03 11:39:49 +00:00
|
|
|
val = errno;
|
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to create outgoing connection to %s",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_connect(conn, -1) == -1) {
|
2011-02-03 11:39:49 +00:00
|
|
|
val = errno;
|
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
|
|
|
|
res->hr_remoteaddr);
|
|
|
|
proto_close(conn);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
val = 0;
|
|
|
|
out:
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) {
|
2011-02-03 11:39:49 +00:00
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to send reply to connection request");
|
|
|
|
}
|
2012-01-10 22:39:07 +00:00
|
|
|
if (val == 0 && proto_connection_send(res->hr_conn, conn) == -1)
|
2011-02-03 11:39:49 +00:00
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to send connection");
|
2011-03-21 08:33:58 +00:00
|
|
|
|
|
|
|
pjdlog_prefix_set("%s", "");
|
2011-02-03 11:39:49 +00:00
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
static void
|
2011-03-21 15:29:20 +00:00
|
|
|
check_signals(void)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2010-09-22 19:08:11 +00:00
|
|
|
struct timespec sigtimeout;
|
|
|
|
sigset_t mask;
|
2011-03-21 15:29:20 +00:00
|
|
|
int signo;
|
2010-08-29 21:41:53 +00:00
|
|
|
|
2010-09-22 19:08:11 +00:00
|
|
|
sigtimeout.tv_sec = 0;
|
|
|
|
sigtimeout.tv_nsec = 0;
|
|
|
|
|
|
|
|
PJDLOG_VERIFY(sigemptyset(&mask) == 0);
|
|
|
|
PJDLOG_VERIFY(sigaddset(&mask, SIGHUP) == 0);
|
|
|
|
PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
|
|
|
|
PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
|
|
|
|
PJDLOG_VERIFY(sigaddset(&mask, SIGCHLD) == 0);
|
2010-06-14 21:18:58 +00:00
|
|
|
|
2011-03-21 15:29:20 +00:00
|
|
|
while ((signo = sigtimedwait(&mask, NULL, &sigtimeout)) != -1) {
|
|
|
|
switch (signo) {
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
sigexit_received = true;
|
|
|
|
terminate_workers();
|
|
|
|
proto_close(cfg->hc_controlconn);
|
|
|
|
exit(EX_OK);
|
|
|
|
break;
|
|
|
|
case SIGCHLD:
|
|
|
|
child_exit();
|
|
|
|
break;
|
|
|
|
case SIGHUP:
|
|
|
|
hastd_reload();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PJDLOG_ABORT("Unexpected signal (%d).", signo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
main_loop(void)
|
|
|
|
{
|
|
|
|
struct hast_resource *res;
|
2011-05-19 23:18:42 +00:00
|
|
|
struct hastd_listen *lst;
|
2011-03-21 15:29:20 +00:00
|
|
|
struct timeval seltimeout;
|
|
|
|
int fd, maxfd, ret;
|
|
|
|
time_t lastcheck, now;
|
|
|
|
fd_set rfds;
|
|
|
|
|
2011-03-22 10:39:34 +00:00
|
|
|
lastcheck = time(NULL);
|
2011-03-21 15:29:20 +00:00
|
|
|
seltimeout.tv_sec = REPORT_INTERVAL;
|
|
|
|
seltimeout.tv_usec = 0;
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
for (;;) {
|
2011-03-21 15:29:20 +00:00
|
|
|
check_signals();
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2010-06-14 21:18:58 +00:00
|
|
|
/* Setup descriptors for select(2). */
|
2010-02-18 23:16:19 +00:00
|
|
|
FD_ZERO(&rfds);
|
2010-08-30 23:26:10 +00:00
|
|
|
maxfd = fd = proto_descriptor(cfg->hc_controlconn);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(fd >= 0);
|
2010-08-30 23:26:10 +00:00
|
|
|
FD_SET(fd, &rfds);
|
2011-05-19 23:18:42 +00:00
|
|
|
TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next) {
|
|
|
|
if (lst->hl_conn == NULL)
|
|
|
|
continue;
|
|
|
|
fd = proto_descriptor(lst->hl_conn);
|
|
|
|
PJDLOG_ASSERT(fd >= 0);
|
|
|
|
FD_SET(fd, &rfds);
|
2016-04-21 06:21:33 +00:00
|
|
|
maxfd = MAX(fd, maxfd);
|
2011-05-19 23:18:42 +00:00
|
|
|
}
|
2010-08-30 23:26:10 +00:00
|
|
|
TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
|
|
|
|
if (res->hr_event == NULL)
|
|
|
|
continue;
|
|
|
|
fd = proto_descriptor(res->hr_event);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(fd >= 0);
|
2010-08-30 23:26:10 +00:00
|
|
|
FD_SET(fd, &rfds);
|
2016-04-21 06:21:33 +00:00
|
|
|
maxfd = MAX(fd, maxfd);
|
2011-02-03 11:39:49 +00:00
|
|
|
if (res->hr_role == HAST_ROLE_PRIMARY) {
|
|
|
|
/* Only primary workers asks for connections. */
|
2011-03-23 11:09:04 +00:00
|
|
|
PJDLOG_ASSERT(res->hr_conn != NULL);
|
2011-02-03 11:39:49 +00:00
|
|
|
fd = proto_descriptor(res->hr_conn);
|
|
|
|
PJDLOG_ASSERT(fd >= 0);
|
|
|
|
FD_SET(fd, &rfds);
|
2016-04-21 06:21:33 +00:00
|
|
|
maxfd = MAX(fd, maxfd);
|
2011-03-23 11:09:04 +00:00
|
|
|
} else {
|
|
|
|
PJDLOG_ASSERT(res->hr_conn == NULL);
|
2011-02-03 11:39:49 +00:00
|
|
|
}
|
2010-08-30 23:26:10 +00:00
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(maxfd + 1 <= (int)FD_SETSIZE);
|
2010-09-22 19:08:11 +00:00
|
|
|
ret = select(maxfd + 1, &rfds, NULL, NULL, &seltimeout);
|
2011-03-21 08:31:35 +00:00
|
|
|
now = time(NULL);
|
|
|
|
if (lastcheck + REPORT_INTERVAL <= now) {
|
2010-10-04 21:43:06 +00:00
|
|
|
hook_check();
|
2011-03-21 08:31:35 +00:00
|
|
|
lastcheck = now;
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
|
|
|
/*
|
|
|
|
* select(2) timed out, so there should be no
|
|
|
|
* descriptors to check.
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
} else if (ret == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
KEEP_ERRNO((void)pidfile_remove(pfh));
|
|
|
|
pjdlog_exit(EX_OSERR, "select() failed");
|
|
|
|
}
|
|
|
|
|
2011-03-21 15:29:20 +00:00
|
|
|
/*
|
|
|
|
* Check for signals before we do anything to update our
|
|
|
|
* info about terminated workers in the meantime.
|
|
|
|
*/
|
|
|
|
check_signals();
|
|
|
|
|
2010-08-30 23:26:10 +00:00
|
|
|
if (FD_ISSET(proto_descriptor(cfg->hc_controlconn), &rfds))
|
2010-02-18 23:16:19 +00:00
|
|
|
control_handle(cfg);
|
2011-05-19 23:18:42 +00:00
|
|
|
TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next) {
|
|
|
|
if (lst->hl_conn == NULL)
|
|
|
|
continue;
|
|
|
|
if (FD_ISSET(proto_descriptor(lst->hl_conn), &rfds))
|
|
|
|
listen_accept(lst);
|
|
|
|
}
|
2010-08-30 23:26:10 +00:00
|
|
|
TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
|
|
|
|
if (res->hr_event == NULL)
|
|
|
|
continue;
|
|
|
|
if (FD_ISSET(proto_descriptor(res->hr_event), &rfds)) {
|
|
|
|
if (event_recv(res) == 0)
|
|
|
|
continue;
|
|
|
|
/* The worker process exited? */
|
|
|
|
proto_close(res->hr_event);
|
|
|
|
res->hr_event = NULL;
|
2011-03-23 11:09:04 +00:00
|
|
|
if (res->hr_conn != NULL) {
|
|
|
|
proto_close(res->hr_conn);
|
|
|
|
res->hr_conn = NULL;
|
|
|
|
}
|
2011-02-03 11:39:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-03-23 11:09:04 +00:00
|
|
|
if (res->hr_role == HAST_ROLE_PRIMARY) {
|
|
|
|
PJDLOG_ASSERT(res->hr_conn != NULL);
|
|
|
|
if (FD_ISSET(proto_descriptor(res->hr_conn),
|
|
|
|
&rfds)) {
|
|
|
|
connection_migrate(res);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PJDLOG_ASSERT(res->hr_conn == NULL);
|
2010-08-30 23:26:10 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-04 21:41:18 +00:00
|
|
|
static void
|
|
|
|
dummy_sighandler(int sig __unused)
|
|
|
|
{
|
|
|
|
/* Nothing to do. */
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2011-05-19 23:18:42 +00:00
|
|
|
struct hastd_listen *lst;
|
2010-02-18 23:16:19 +00:00
|
|
|
pid_t otherpid;
|
|
|
|
int debuglevel;
|
2010-09-22 19:08:11 +00:00
|
|
|
sigset_t mask;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
foreground = false;
|
|
|
|
debuglevel = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
ch = getopt(argc, argv, "c:dFhP:");
|
|
|
|
if (ch == -1)
|
|
|
|
break;
|
|
|
|
switch (ch) {
|
|
|
|
case 'c':
|
|
|
|
cfgpath = optarg;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
debuglevel++;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
foreground = true;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
pidfile = optarg;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2011-01-27 19:24:07 +00:00
|
|
|
pjdlog_init(PJDLOG_MODE_STD);
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_debug_set(debuglevel);
|
|
|
|
|
2010-10-24 15:38:58 +00:00
|
|
|
g_gate_load();
|
|
|
|
|
2011-10-17 09:54:07 +00:00
|
|
|
/*
|
|
|
|
* When path to the configuration file is relative, obtain full path,
|
|
|
|
* so we can always find the file, even after daemonizing and changing
|
|
|
|
* working directory to /.
|
|
|
|
*/
|
|
|
|
if (cfgpath[0] != '/') {
|
|
|
|
const char *newcfgpath;
|
|
|
|
|
|
|
|
newcfgpath = realpath(cfgpath, NULL);
|
|
|
|
if (newcfgpath == NULL) {
|
|
|
|
pjdlog_exit(EX_CONFIG,
|
|
|
|
"Unable to obtain full path of %s", cfgpath);
|
|
|
|
}
|
|
|
|
cfgpath = newcfgpath;
|
|
|
|
}
|
|
|
|
|
2010-08-05 19:08:54 +00:00
|
|
|
cfg = yy_config_parse(cfgpath, true);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(cfg != NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-10-17 12:22:09 +00:00
|
|
|
if (pidfile != NULL) {
|
|
|
|
if (strlcpy(cfg->hc_pidfile, pidfile,
|
|
|
|
sizeof(cfg->hc_pidfile)) >= sizeof(cfg->hc_pidfile)) {
|
|
|
|
pjdlog_exitx(EX_CONFIG, "Pidfile path is too long.");
|
|
|
|
}
|
|
|
|
}
|
2012-01-10 22:24:57 +00:00
|
|
|
|
2012-03-29 20:11:16 +00:00
|
|
|
if (pidfile != NULL || !foreground) {
|
2012-01-10 22:24:57 +00:00
|
|
|
pfh = pidfile_open(cfg->hc_pidfile, 0600, &otherpid);
|
|
|
|
if (pfh == NULL) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
pjdlog_exitx(EX_TEMPFAIL,
|
|
|
|
"Another hastd is already running, pidfile: %s, pid: %jd.",
|
|
|
|
cfg->hc_pidfile, (intmax_t)otherpid);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If we cannot create pidfile for other reasons,
|
|
|
|
* only warn.
|
|
|
|
*/
|
|
|
|
pjdlog_errno(LOG_WARNING,
|
|
|
|
"Unable to open or create pidfile %s",
|
|
|
|
cfg->hc_pidfile);
|
2011-10-17 12:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 14:35:29 +00:00
|
|
|
/*
|
|
|
|
* Restore default actions for interesting signals in case parent
|
|
|
|
* process (like init(8)) decided to ignore some of them (like SIGHUP).
|
|
|
|
*/
|
|
|
|
PJDLOG_VERIFY(signal(SIGHUP, SIG_DFL) != SIG_ERR);
|
|
|
|
PJDLOG_VERIFY(signal(SIGINT, SIG_DFL) != SIG_ERR);
|
|
|
|
PJDLOG_VERIFY(signal(SIGTERM, SIG_DFL) != SIG_ERR);
|
2010-10-04 21:41:18 +00:00
|
|
|
/*
|
|
|
|
* Because SIGCHLD is ignored by default, setup dummy handler for it,
|
|
|
|
* so we can mask it.
|
|
|
|
*/
|
|
|
|
PJDLOG_VERIFY(signal(SIGCHLD, dummy_sighandler) != SIG_ERR);
|
2011-01-12 14:35:29 +00:00
|
|
|
|
2010-09-22 19:08:11 +00:00
|
|
|
PJDLOG_VERIFY(sigemptyset(&mask) == 0);
|
|
|
|
PJDLOG_VERIFY(sigaddset(&mask, SIGHUP) == 0);
|
|
|
|
PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0);
|
|
|
|
PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0);
|
|
|
|
PJDLOG_VERIFY(sigaddset(&mask, SIGCHLD) == 0);
|
|
|
|
PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
/* Listen on control address. */
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_server(cfg->hc_controladdr, &cfg->hc_controlconn) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
KEEP_ERRNO((void)pidfile_remove(pfh));
|
|
|
|
pjdlog_exit(EX_OSERR, "Unable to listen on control address %s",
|
|
|
|
cfg->hc_controladdr);
|
|
|
|
}
|
|
|
|
/* Listen for remote connections. */
|
2011-05-19 23:18:42 +00:00
|
|
|
TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next) {
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_server(lst->hl_addr, &lst->hl_conn) == -1) {
|
2011-05-19 23:18:42 +00:00
|
|
|
KEEP_ERRNO((void)pidfile_remove(pfh));
|
|
|
|
pjdlog_exit(EX_OSERR, "Unable to listen on address %s",
|
|
|
|
lst->hl_addr);
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!foreground) {
|
2012-01-10 22:39:07 +00:00
|
|
|
if (daemon(0, 0) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
KEEP_ERRNO((void)pidfile_remove(pfh));
|
|
|
|
pjdlog_exit(EX_OSERR, "Unable to daemonize");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start logging to syslog. */
|
|
|
|
pjdlog_mode_set(PJDLOG_MODE_SYSLOG);
|
2012-03-29 20:11:16 +00:00
|
|
|
}
|
|
|
|
if (pidfile != NULL || !foreground) {
|
2010-02-18 23:16:19 +00:00
|
|
|
/* Write PID to a file. */
|
2012-01-10 22:39:07 +00:00
|
|
|
if (pidfile_write(pfh) == -1) {
|
2010-02-18 23:16:19 +00:00
|
|
|
pjdlog_errno(LOG_WARNING,
|
2011-10-17 12:22:09 +00:00
|
|
|
"Unable to write PID to a file %s",
|
|
|
|
cfg->hc_pidfile);
|
|
|
|
} else {
|
|
|
|
pjdlog_debug(1, "PID stored in %s.", cfg->hc_pidfile);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-19 23:18:42 +00:00
|
|
|
pjdlog_info("Started successfully, running protocol version %d.",
|
|
|
|
HAST_PROTO_VERSION);
|
|
|
|
|
|
|
|
pjdlog_debug(1, "Listening on control address %s.",
|
|
|
|
cfg->hc_controladdr);
|
|
|
|
TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next)
|
|
|
|
pjdlog_info("Listening on address %s.", lst->hl_addr);
|
|
|
|
|
2010-08-29 21:41:53 +00:00
|
|
|
hook_init();
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
main_loop();
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|