hastd.c revision 219351
11573Srgrimes/*- 21573Srgrimes * Copyright (c) 2009-2010 The FreeBSD Foundation 31573Srgrimes * Copyright (c) 2010-2011 Pawel Jakub Dawidek <pawel@dawidek.net> 41573Srgrimes * All rights reserved. 51573Srgrimes * 61573Srgrimes * This software was developed by Pawel Jakub Dawidek under sponsorship from 71573Srgrimes * the FreeBSD Foundation. 81573Srgrimes * 91573Srgrimes * Redistribution and use in source and binary forms, with or without 101573Srgrimes * modification, are permitted provided that the following conditions 111573Srgrimes * are met: 121573Srgrimes * 1. Redistributions of source code must retain the above copyright 131573Srgrimes * notice, this list of conditions and the following disclaimer. 141573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 151573Srgrimes * notice, this list of conditions and the following disclaimer in the 161573Srgrimes * documentation and/or other materials provided with the distribution. 171573Srgrimes * 181573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 191573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 201573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 211573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 221573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 231573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 241573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 251573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 261573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 271573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 281573Srgrimes * SUCH DAMAGE. 291573Srgrimes */ 301573Srgrimes 311573Srgrimes#include <sys/cdefs.h> 321573Srgrimes__FBSDID("$FreeBSD: head/sbin/hastd/hastd.c 219351 2011-03-06 22:56:14Z pjd $"); 331573Srgrimes 34176449Sdas#include <sys/param.h> 3592887Sobrien#include <sys/linker.h> 3692887Sobrien#include <sys/module.h> 371573Srgrimes#include <sys/stat.h> 381573Srgrimes#include <sys/wait.h> 391573Srgrimes 401573Srgrimes#include <err.h> 411573Srgrimes#include <errno.h> 421573Srgrimes#include <libutil.h> 431573Srgrimes#include <signal.h> 441573Srgrimes#include <stdbool.h> 451573Srgrimes#include <stdio.h> 461573Srgrimes#include <stdlib.h> 471573Srgrimes#include <string.h> 481573Srgrimes#include <sysexits.h> 491573Srgrimes#include <unistd.h> 501573Srgrimes 511573Srgrimes#include <activemap.h> 521573Srgrimes#include <pjdlog.h> 531573Srgrimes 541573Srgrimes#include "control.h" 551573Srgrimes#include "event.h" 561573Srgrimes#include "hast.h" 571573Srgrimes#include "hast_proto.h" 581573Srgrimes#include "hastd.h" 591573Srgrimes#include "hooks.h" 601573Srgrimes#include "subr.h" 611573Srgrimes 621573Srgrimes/* Path to configuration file. */ 631573Srgrimesconst char *cfgpath = HAST_CONFIG; 641573Srgrimes/* Hastd configuration. */ 651573Srgrimesstatic struct hastd_config *cfg; 661573Srgrimes/* Was SIGINT or SIGTERM signal received? */ 671573Srgrimesbool sigexit_received = false; 681573Srgrimes/* PID file handle. */ 691573Srgrimesstruct pidfh *pfh; 701573Srgrimes 711573Srgrimes/* How often check for hooks running for too long. */ 721573Srgrimes#define REPORT_INTERVAL 5 731573Srgrimes 741573Srgrimesstatic void 751573Srgrimesusage(void) 761573Srgrimes{ 771573Srgrimes 781573Srgrimes errx(EX_USAGE, "[-dFh] [-c config] [-P pidfile]"); 791573Srgrimes} 801573Srgrimes 811573Srgrimesstatic void 821573Srgrimesg_gate_load(void) 831573Srgrimes{ 841573Srgrimes 851573Srgrimes if (modfind("g_gate") == -1) { 861573Srgrimes /* Not present in kernel, try loading it. */ 871573Srgrimes if (kldload("geom_gate") == -1 || modfind("g_gate") == -1) { 881573Srgrimes if (errno != EEXIST) { 891573Srgrimes pjdlog_exit(EX_OSERR, 901573Srgrimes "Unable to load geom_gate module"); 911573Srgrimes } 921573Srgrimes } 931573Srgrimes } 941573Srgrimes} 951573Srgrimes 961573Srgrimesvoid 971573Srgrimesdescriptors_cleanup(struct hast_resource *res) 981573Srgrimes{ 991573Srgrimes struct hast_resource *tres; 1001573Srgrimes 1011573Srgrimes TAILQ_FOREACH(tres, &cfg->hc_resources, hr_next) { 1021573Srgrimes if (tres == res) { 1031573Srgrimes PJDLOG_VERIFY(res->hr_role == HAST_ROLE_SECONDARY || 1041573Srgrimes (res->hr_remotein == NULL && 1051573Srgrimes res->hr_remoteout == NULL)); 1061573Srgrimes continue; 1071573Srgrimes } 1081573Srgrimes if (tres->hr_remotein != NULL) 1091573Srgrimes proto_close(tres->hr_remotein); 1101573Srgrimes if (tres->hr_remoteout != NULL) 1111573Srgrimes proto_close(tres->hr_remoteout); 1121573Srgrimes if (tres->hr_ctrl != NULL) 1131573Srgrimes proto_close(tres->hr_ctrl); 1141573Srgrimes if (tres->hr_event != NULL) 1151573Srgrimes proto_close(tres->hr_event); 1161573Srgrimes if (tres->hr_conn != NULL) 1171573Srgrimes proto_close(tres->hr_conn); 1181573Srgrimes } 1191573Srgrimes if (cfg->hc_controlin != NULL) 1201573Srgrimes proto_close(cfg->hc_controlin); 1211573Srgrimes proto_close(cfg->hc_controlconn); 1221573Srgrimes proto_close(cfg->hc_listenconn); 1231573Srgrimes (void)pidfile_close(pfh); 1241573Srgrimes hook_fini(); 1251573Srgrimes pjdlog_fini(); 1261573Srgrimes} 1271573Srgrimes 1281573Srgrimesstatic const char * 1291573Srgrimesdtype2str(mode_t mode) 1301573Srgrimes{ 1311573Srgrimes 1321573Srgrimes if (S_ISBLK(mode)) 1331573Srgrimes return ("block device"); 1341573Srgrimes else if (S_ISCHR(mode)) 1351573Srgrimes return ("character device"); 1361573Srgrimes else if (S_ISDIR(mode)) 1371573Srgrimes return ("directory"); 1381573Srgrimes else if (S_ISFIFO(mode)) 1391573Srgrimes return ("pipe or FIFO"); 1401573Srgrimes else if (S_ISLNK(mode)) 1411573Srgrimes return ("symbolic link"); 1421573Srgrimes else if (S_ISREG(mode)) 1431573Srgrimes return ("regular file"); 1441573Srgrimes else if (S_ISSOCK(mode)) 1451573Srgrimes return ("socket"); 1461573Srgrimes else if (S_ISWHT(mode)) 1471573Srgrimes return ("whiteout"); 1481573Srgrimes else 1491573Srgrimes return ("unknown"); 1501573Srgrimes} 1511573Srgrimes 1521573Srgrimesvoid 1531573Srgrimesdescriptors_assert(const struct hast_resource *res, int pjdlogmode) 1541573Srgrimes{ 1551573Srgrimes char msg[256]; 1561573Srgrimes struct stat sb; 1571573Srgrimes long maxfd; 1581573Srgrimes bool isopen; 1591573Srgrimes mode_t mode; 1601573Srgrimes int fd; 1611573Srgrimes 1621573Srgrimes /* 1631573Srgrimes * At this point descriptor to syslog socket is closed, so if we want 1641573Srgrimes * to log assertion message, we have to first store it in 'msg' local 1651573Srgrimes * buffer and then open syslog socket and log it. 1661573Srgrimes */ 1671573Srgrimes msg[0] = '\0'; 1681573Srgrimes 1691573Srgrimes maxfd = sysconf(_SC_OPEN_MAX); 1701573Srgrimes if (maxfd < 0) { 1711573Srgrimes pjdlog_init(pjdlogmode); 1721573Srgrimes pjdlog_prefix_set("[%s] (%s) ", res->hr_name, 1731573Srgrimes role2str(res->hr_role)); 1741573Srgrimes pjdlog_errno(LOG_WARNING, "sysconf(_SC_OPEN_MAX) failed"); 1751573Srgrimes pjdlog_fini(); 1761573Srgrimes maxfd = 16384; 1771573Srgrimes } 1781573Srgrimes for (fd = 0; fd <= maxfd; fd++) { 1791573Srgrimes if (fstat(fd, &sb) == 0) { 1801573Srgrimes isopen = true; 1811573Srgrimes mode = sb.st_mode; 1821573Srgrimes } else if (errno == EBADF) { 1831573Srgrimes isopen = false; 1841573Srgrimes mode = 0; 1851573Srgrimes } else { 1861573Srgrimes (void)snprintf(msg, sizeof(msg), 1871573Srgrimes "Unable to fstat descriptor %d: %s", fd, 1881573Srgrimes strerror(errno)); 1891573Srgrimes break; 1901573Srgrimes } 1911573Srgrimes if (fd == STDIN_FILENO || fd == STDOUT_FILENO || 1921573Srgrimes fd == STDERR_FILENO) { 1931573Srgrimes if (!isopen) { 1941573Srgrimes (void)snprintf(msg, sizeof(msg), 1951573Srgrimes "Descriptor %d (%s) is closed, but should be open.", 1961573Srgrimes fd, (fd == STDIN_FILENO ? "stdin" : 1971573Srgrimes (fd == STDOUT_FILENO ? "stdout" : "stderr"))); 1981573Srgrimes break; 1991573Srgrimes } 2001573Srgrimes } else if (fd == proto_descriptor(res->hr_event)) { 2011573Srgrimes if (!isopen) { 2021573Srgrimes (void)snprintf(msg, sizeof(msg), 2031573Srgrimes "Descriptor %d (event) is closed, but should be open.", 2041573Srgrimes fd); 2051573Srgrimes break; 2061573Srgrimes } 2071573Srgrimes if (!S_ISSOCK(mode)) { 2081573Srgrimes (void)snprintf(msg, sizeof(msg), 2091573Srgrimes "Descriptor %d (event) is %s, but should be %s.", 2101573Srgrimes fd, dtype2str(mode), dtype2str(S_IFSOCK)); 2111573Srgrimes break; 2121573Srgrimes } 2131573Srgrimes } else if (fd == proto_descriptor(res->hr_ctrl)) { 2141573Srgrimes if (!isopen) { 2151573Srgrimes (void)snprintf(msg, sizeof(msg), 2161573Srgrimes "Descriptor %d (ctrl) is closed, but should be open.", 2171573Srgrimes fd); 2181573Srgrimes break; 2191573Srgrimes } 2201573Srgrimes if (!S_ISSOCK(mode)) { 2211573Srgrimes (void)snprintf(msg, sizeof(msg), 2221573Srgrimes "Descriptor %d (ctrl) is %s, but should be %s.", 2231573Srgrimes fd, dtype2str(mode), dtype2str(S_IFSOCK)); 2241573Srgrimes break; 2251573Srgrimes } 2261573Srgrimes } else if (fd == proto_descriptor(res->hr_conn)) { 2271573Srgrimes if (!isopen) { 2281573Srgrimes (void)snprintf(msg, sizeof(msg), 2291573Srgrimes "Descriptor %d (conn) is closed, but should be open.", 2301573Srgrimes fd); 2311573Srgrimes break; 2321573Srgrimes } 2331573Srgrimes if (!S_ISSOCK(mode)) { 2341573Srgrimes (void)snprintf(msg, sizeof(msg), 2351573Srgrimes "Descriptor %d (conn) is %s, but should be %s.", 2361573Srgrimes fd, dtype2str(mode), dtype2str(S_IFSOCK)); 2371573Srgrimes break; 2381573Srgrimes } 2391573Srgrimes } else if (res->hr_role == HAST_ROLE_SECONDARY && 2401573Srgrimes fd == proto_descriptor(res->hr_remotein)) { 2411573Srgrimes if (!isopen) { 2421573Srgrimes (void)snprintf(msg, sizeof(msg), 2431573Srgrimes "Descriptor %d (remote in) is closed, but should be open.", 2441573Srgrimes fd); 2451573Srgrimes break; 2461573Srgrimes } 2471573Srgrimes if (!S_ISSOCK(mode)) { 2481573Srgrimes (void)snprintf(msg, sizeof(msg), 2491573Srgrimes "Descriptor %d (remote in) is %s, but should be %s.", 2501573Srgrimes fd, dtype2str(mode), dtype2str(S_IFSOCK)); 2511573Srgrimes break; 2521573Srgrimes } 2531573Srgrimes } else if (res->hr_role == HAST_ROLE_SECONDARY && 2541573Srgrimes fd == proto_descriptor(res->hr_remoteout)) { 2551573Srgrimes if (!isopen) { 2561573Srgrimes (void)snprintf(msg, sizeof(msg), 2571573Srgrimes "Descriptor %d (remote out) is closed, but should be open.", 2581573Srgrimes fd); 2591573Srgrimes break; 2601573Srgrimes } 2611573Srgrimes if (!S_ISSOCK(mode)) { 2621573Srgrimes (void)snprintf(msg, sizeof(msg), 2631573Srgrimes "Descriptor %d (remote out) is %s, but should be %s.", 2641573Srgrimes fd, dtype2str(mode), dtype2str(S_IFSOCK)); 2651573Srgrimes break; 2661573Srgrimes } 2671573Srgrimes } else { 2681573Srgrimes if (isopen) { 2691573Srgrimes (void)snprintf(msg, sizeof(msg), 2701573Srgrimes "Descriptor %d is open (%s), but should be closed.", 2711573Srgrimes fd, dtype2str(mode)); 2721573Srgrimes break; 2731573Srgrimes } 2741573Srgrimes } 2751573Srgrimes } 2761573Srgrimes if (msg[0] != '\0') { 2771573Srgrimes pjdlog_init(pjdlogmode); 2781573Srgrimes pjdlog_prefix_set("[%s] (%s) ", res->hr_name, 2791573Srgrimes role2str(res->hr_role)); 2801573Srgrimes PJDLOG_ABORT("%s", msg); 2811573Srgrimes } 2821573Srgrimes} 2831573Srgrimes 2841573Srgrimesstatic void 2851573Srgrimeschild_exit_log(unsigned int pid, int status) 2861573Srgrimes{ 2871573Srgrimes 2881573Srgrimes if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 2891573Srgrimes pjdlog_debug(1, "Worker process exited gracefully (pid=%u).", 2901573Srgrimes pid); 2911573Srgrimes } else if (WIFSIGNALED(status)) { 2921573Srgrimes pjdlog_error("Worker process killed (pid=%u, signal=%d).", 2931573Srgrimes pid, WTERMSIG(status)); 2941573Srgrimes } else { 2951573Srgrimes pjdlog_error("Worker process exited ungracefully (pid=%u, exitcode=%d).", 2961573Srgrimes pid, WIFEXITED(status) ? WEXITSTATUS(status) : -1); 2971573Srgrimes } 2981573Srgrimes} 2991573Srgrimes 3001573Srgrimesstatic void 3011573Srgrimeschild_exit(void) 3021573Srgrimes{ 3031573Srgrimes struct hast_resource *res; 3041573Srgrimes int status; 3051573Srgrimes pid_t pid; 3061573Srgrimes 3071573Srgrimes while ((pid = wait3(&status, WNOHANG, NULL)) > 0) { 3081573Srgrimes /* Find resource related to the process that just exited. */ 3091573Srgrimes TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) { 3101573Srgrimes if (pid == res->hr_workerpid) 3111573Srgrimes break; 3121573Srgrimes } 3131573Srgrimes if (res == NULL) { 3141573Srgrimes /* 3151573Srgrimes * This can happen when new connection arrives and we 3161573Srgrimes * cancel child responsible for the old one or if this 3171573Srgrimes * was hook which we executed. 3181573Srgrimes */ 3191573Srgrimes hook_check_one(pid, status); 3201573Srgrimes continue; 3211573Srgrimes } 3221573Srgrimes pjdlog_prefix_set("[%s] (%s) ", res->hr_name, 3231573Srgrimes role2str(res->hr_role)); 3241573Srgrimes child_exit_log(pid, status); 3251573Srgrimes child_cleanup(res); 3261573Srgrimes if (res->hr_role == HAST_ROLE_PRIMARY) { 3271573Srgrimes /* 3281573Srgrimes * Restart child process if it was killed by signal 3291573Srgrimes * or exited because of temporary problem. 3301573Srgrimes */ 3311573Srgrimes if (WIFSIGNALED(status) || 3321573Srgrimes (WIFEXITED(status) && 3331573Srgrimes WEXITSTATUS(status) == EX_TEMPFAIL)) { 3341573Srgrimes sleep(1); 3351573Srgrimes pjdlog_info("Restarting worker process."); 3361573Srgrimes hastd_primary(res); 3371573Srgrimes } else { 3381573Srgrimes res->hr_role = HAST_ROLE_INIT; 3391573Srgrimes pjdlog_info("Changing resource role back to %s.", 3401573Srgrimes role2str(res->hr_role)); 3411573Srgrimes } 3421573Srgrimes } 3431573Srgrimes pjdlog_prefix_set("%s", ""); 3441573Srgrimes } 3451573Srgrimes} 3461573Srgrimes 3471573Srgrimesstatic bool 3481573Srgrimesresource_needs_restart(const struct hast_resource *res0, 3491573Srgrimes const struct hast_resource *res1) 3501573Srgrimes{ 3511573Srgrimes 3521573Srgrimes PJDLOG_ASSERT(strcmp(res0->hr_name, res1->hr_name) == 0); 3531573Srgrimes 3541573Srgrimes if (strcmp(res0->hr_provname, res1->hr_provname) != 0) 3551573Srgrimes return (true); 3561573Srgrimes if (strcmp(res0->hr_localpath, res1->hr_localpath) != 0) 3571573Srgrimes return (true); 35893211Sbde if (res0->hr_role == HAST_ROLE_INIT || 3591573Srgrimes res0->hr_role == HAST_ROLE_SECONDARY) { 3601573Srgrimes if (strcmp(res0->hr_remoteaddr, res1->hr_remoteaddr) != 0) 3611573Srgrimes return (true); 3621573Srgrimes if (res0->hr_replication != res1->hr_replication) 3631573Srgrimes return (true); 3641573Srgrimes if (res0->hr_checksum != res1->hr_checksum) 3651573Srgrimes return (true); 3661573Srgrimes if (res0->hr_timeout != res1->hr_timeout) 3671573Srgrimes return (true); 3681573Srgrimes if (strcmp(res0->hr_exec, res1->hr_exec) != 0) 3691573Srgrimes return (true); 3701573Srgrimes } 3711573Srgrimes return (false); 372138924Sdas} 3731573Srgrimes 374138924Sdasstatic bool 3751573Srgrimesresource_needs_reload(const struct hast_resource *res0, 3761573Srgrimes const struct hast_resource *res1) 377138924Sdas{ 3788870Srgrimes 3791573Srgrimes PJDLOG_ASSERT(strcmp(res0->hr_name, res1->hr_name) == 0); 3801573Srgrimes PJDLOG_ASSERT(strcmp(res0->hr_provname, res1->hr_provname) == 0); 3811573Srgrimes PJDLOG_ASSERT(strcmp(res0->hr_localpath, res1->hr_localpath) == 0); 3821573Srgrimes 3831573Srgrimes if (res0->hr_role != HAST_ROLE_PRIMARY) 384138924Sdas return (false); 3851573Srgrimes 3861573Srgrimes if (strcmp(res0->hr_remoteaddr, res1->hr_remoteaddr) != 0) 3871573Srgrimes return (true); 3881573Srgrimes if (res0->hr_replication != res1->hr_replication) 3891573Srgrimes return (true); 3901573Srgrimes if (res0->hr_checksum != res1->hr_checksum) 3911573Srgrimes return (true); 3921573Srgrimes if (res0->hr_timeout != res1->hr_timeout) 3931573Srgrimes return (true); 3941573Srgrimes if (strcmp(res0->hr_exec, res1->hr_exec) != 0) 3951573Srgrimes return (true); 3961573Srgrimes return (false); 3971573Srgrimes} 3981573Srgrimes 3991573Srgrimesstatic void 4001573Srgrimesresource_reload(const struct hast_resource *res) 4011573Srgrimes{ 4021573Srgrimes struct nv *nvin, *nvout; 4031573Srgrimes int error; 4041573Srgrimes 4051573Srgrimes PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY); 4061573Srgrimes 4071573Srgrimes nvout = nv_alloc(); 4081573Srgrimes nv_add_uint8(nvout, HASTCTL_RELOAD, "cmd"); 4091573Srgrimes nv_add_string(nvout, res->hr_remoteaddr, "remoteaddr"); 4101573Srgrimes nv_add_int32(nvout, (int32_t)res->hr_replication, "replication"); 4111573Srgrimes nv_add_int32(nvout, (int32_t)res->hr_checksum, "checksum"); 4121573Srgrimes nv_add_int32(nvout, (int32_t)res->hr_timeout, "timeout"); 4131573Srgrimes nv_add_string(nvout, res->hr_exec, "exec"); 4141573Srgrimes if (nv_error(nvout) != 0) { 4151573Srgrimes nv_free(nvout); 4161573Srgrimes pjdlog_error("Unable to allocate header for reload message."); 4171573Srgrimes return; 4181573Srgrimes } 4191573Srgrimes if (hast_proto_send(res, res->hr_ctrl, nvout, NULL, 0) < 0) { 4201573Srgrimes pjdlog_errno(LOG_ERR, "Unable to send reload message"); 4211573Srgrimes nv_free(nvout); 42293211Sbde return; 4231573Srgrimes } 4241573Srgrimes nv_free(nvout); 4251573Srgrimes 426108533Sschweikh /* Receive response. */ 4271573Srgrimes if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) < 0) { 4281573Srgrimes pjdlog_errno(LOG_ERR, "Unable to receive reload reply"); 4291573Srgrimes return; 4301573Srgrimes } 4311573Srgrimes error = nv_get_int16(nvin, "error"); 4321573Srgrimes nv_free(nvin); 4331573Srgrimes if (error != 0) { 4341573Srgrimes pjdlog_common(LOG_ERR, 0, error, "Reload failed"); 4351573Srgrimes return; 436150318Sbde } 4371573Srgrimes} 4381573Srgrimes 4391573Srgrimesstatic void 4401573Srgrimeshastd_reload(void) 4411573Srgrimes{ 4421573Srgrimes struct hastd_config *newcfg; 4431573Srgrimes struct hast_resource *nres, *cres, *tres; 4441573Srgrimes uint8_t role; 445138924Sdas 4461573Srgrimes pjdlog_info("Reloading configuration..."); 4471573Srgrimes 4481573Srgrimes newcfg = yy_config_parse(cfgpath, false); 4491573Srgrimes if (newcfg == NULL) 4501573Srgrimes goto failed; 4511573Srgrimes 4521573Srgrimes /* 4531573Srgrimes * Check if control address has changed. 4541573Srgrimes */ 4551573Srgrimes if (strcmp(cfg->hc_controladdr, newcfg->hc_controladdr) != 0) { 4561573Srgrimes if (proto_server(newcfg->hc_controladdr, 4571573Srgrimes &newcfg->hc_controlconn) < 0) { 4581573Srgrimes pjdlog_errno(LOG_ERR, 4591573Srgrimes "Unable to listen on control address %s", 4601573Srgrimes newcfg->hc_controladdr); 4611573Srgrimes goto failed; 4621573Srgrimes } 4631573Srgrimes } 4641573Srgrimes /* 4651573Srgrimes * Check if listen address has changed. 4661573Srgrimes */ 4671573Srgrimes if (strcmp(cfg->hc_listenaddr, newcfg->hc_listenaddr) != 0) { 4681573Srgrimes if (proto_server(newcfg->hc_listenaddr, 4691573Srgrimes &newcfg->hc_listenconn) < 0) { 4701573Srgrimes pjdlog_errno(LOG_ERR, "Unable to listen on address %s", 4711573Srgrimes newcfg->hc_listenaddr); 472 goto failed; 473 } 474 } 475 /* 476 * Only when both control and listen sockets are successfully 477 * initialized switch them to new configuration. 478 */ 479 if (newcfg->hc_controlconn != NULL) { 480 pjdlog_info("Control socket changed from %s to %s.", 481 cfg->hc_controladdr, newcfg->hc_controladdr); 482 proto_close(cfg->hc_controlconn); 483 cfg->hc_controlconn = newcfg->hc_controlconn; 484 newcfg->hc_controlconn = NULL; 485 strlcpy(cfg->hc_controladdr, newcfg->hc_controladdr, 486 sizeof(cfg->hc_controladdr)); 487 } 488 if (newcfg->hc_listenconn != NULL) { 489 pjdlog_info("Listen socket changed from %s to %s.", 490 cfg->hc_listenaddr, newcfg->hc_listenaddr); 491 proto_close(cfg->hc_listenconn); 492 cfg->hc_listenconn = newcfg->hc_listenconn; 493 newcfg->hc_listenconn = NULL; 494 strlcpy(cfg->hc_listenaddr, newcfg->hc_listenaddr, 495 sizeof(cfg->hc_listenaddr)); 496 } 497 498 /* 499 * Stop and remove resources that were removed from the configuration. 500 */ 501 TAILQ_FOREACH_SAFE(cres, &cfg->hc_resources, hr_next, tres) { 502 TAILQ_FOREACH(nres, &newcfg->hc_resources, hr_next) { 503 if (strcmp(cres->hr_name, nres->hr_name) == 0) 504 break; 505 } 506 if (nres == NULL) { 507 control_set_role(cres, HAST_ROLE_INIT); 508 TAILQ_REMOVE(&cfg->hc_resources, cres, hr_next); 509 pjdlog_info("Resource %s removed.", cres->hr_name); 510 free(cres); 511 } 512 } 513 /* 514 * Move new resources to the current configuration. 515 */ 516 TAILQ_FOREACH_SAFE(nres, &newcfg->hc_resources, hr_next, tres) { 517 TAILQ_FOREACH(cres, &cfg->hc_resources, hr_next) { 518 if (strcmp(cres->hr_name, nres->hr_name) == 0) 519 break; 520 } 521 if (cres == NULL) { 522 TAILQ_REMOVE(&newcfg->hc_resources, nres, hr_next); 523 TAILQ_INSERT_TAIL(&cfg->hc_resources, nres, hr_next); 524 pjdlog_info("Resource %s added.", nres->hr_name); 525 } 526 } 527 /* 528 * Deal with modified resources. 529 * Depending on what has changed exactly we might want to perform 530 * different actions. 531 * 532 * We do full resource restart in the following situations: 533 * Resource role is INIT or SECONDARY. 534 * Resource role is PRIMARY and path to local component or provider 535 * name has changed. 536 * In case of PRIMARY, the worker process will be killed and restarted, 537 * which also means removing /dev/hast/<name> provider and 538 * recreating it. 539 * 540 * We do just reload (send SIGHUP to worker process) if we act as 541 * PRIMARY, but only if remote address, replication mode, timeout or 542 * execution path has changed. For those, there is no need to restart 543 * worker process. 544 * If PRIMARY receives SIGHUP, it will reconnect if remote address or 545 * replication mode has changed or simply set new timeout if only 546 * timeout has changed. 547 */ 548 TAILQ_FOREACH_SAFE(nres, &newcfg->hc_resources, hr_next, tres) { 549 TAILQ_FOREACH(cres, &cfg->hc_resources, hr_next) { 550 if (strcmp(cres->hr_name, nres->hr_name) == 0) 551 break; 552 } 553 PJDLOG_ASSERT(cres != NULL); 554 if (resource_needs_restart(cres, nres)) { 555 pjdlog_info("Resource %s configuration was modified, restarting it.", 556 cres->hr_name); 557 role = cres->hr_role; 558 control_set_role(cres, HAST_ROLE_INIT); 559 TAILQ_REMOVE(&cfg->hc_resources, cres, hr_next); 560 free(cres); 561 TAILQ_REMOVE(&newcfg->hc_resources, nres, hr_next); 562 TAILQ_INSERT_TAIL(&cfg->hc_resources, nres, hr_next); 563 control_set_role(nres, role); 564 } else if (resource_needs_reload(cres, nres)) { 565 pjdlog_info("Resource %s configuration was modified, reloading it.", 566 cres->hr_name); 567 strlcpy(cres->hr_remoteaddr, nres->hr_remoteaddr, 568 sizeof(cres->hr_remoteaddr)); 569 cres->hr_replication = nres->hr_replication; 570 cres->hr_checksum = nres->hr_checksum; 571 cres->hr_timeout = nres->hr_timeout; 572 strlcpy(cres->hr_exec, nres->hr_exec, 573 sizeof(cres->hr_exec)); 574 if (cres->hr_workerpid != 0) 575 resource_reload(cres); 576 } 577 } 578 579 yy_config_free(newcfg); 580 pjdlog_info("Configuration reloaded successfully."); 581 return; 582failed: 583 if (newcfg != NULL) { 584 if (newcfg->hc_controlconn != NULL) 585 proto_close(newcfg->hc_controlconn); 586 if (newcfg->hc_listenconn != NULL) 587 proto_close(newcfg->hc_listenconn); 588 yy_config_free(newcfg); 589 } 590 pjdlog_warning("Configuration not reloaded."); 591} 592 593static void 594terminate_workers(void) 595{ 596 struct hast_resource *res; 597 598 pjdlog_info("Termination signal received, exiting."); 599 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) { 600 if (res->hr_workerpid == 0) 601 continue; 602 pjdlog_info("Terminating worker process (resource=%s, role=%s, pid=%u).", 603 res->hr_name, role2str(res->hr_role), res->hr_workerpid); 604 if (kill(res->hr_workerpid, SIGTERM) == 0) 605 continue; 606 pjdlog_errno(LOG_WARNING, 607 "Unable to send signal to worker process (resource=%s, role=%s, pid=%u).", 608 res->hr_name, role2str(res->hr_role), res->hr_workerpid); 609 } 610} 611 612static void 613listen_accept(void) 614{ 615 struct hast_resource *res; 616 struct proto_conn *conn; 617 struct nv *nvin, *nvout, *nverr; 618 const char *resname; 619 const unsigned char *token; 620 char laddr[256], raddr[256]; 621 size_t size; 622 pid_t pid; 623 int status; 624 625 proto_local_address(cfg->hc_listenconn, laddr, sizeof(laddr)); 626 pjdlog_debug(1, "Accepting connection to %s.", laddr); 627 628 if (proto_accept(cfg->hc_listenconn, &conn) < 0) { 629 pjdlog_errno(LOG_ERR, "Unable to accept connection %s", laddr); 630 return; 631 } 632 633 proto_local_address(conn, laddr, sizeof(laddr)); 634 proto_remote_address(conn, raddr, sizeof(raddr)); 635 pjdlog_info("Connection from %s to %s.", raddr, laddr); 636 637 /* Error in setting timeout is not critical, but why should it fail? */ 638 if (proto_timeout(conn, HAST_TIMEOUT) < 0) 639 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout"); 640 641 nvin = nvout = nverr = NULL; 642 643 /* 644 * Before receiving any data see if remote host have access to any 645 * resource. 646 */ 647 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) { 648 if (proto_address_match(conn, res->hr_remoteaddr)) 649 break; 650 } 651 if (res == NULL) { 652 pjdlog_error("Client %s isn't known.", raddr); 653 goto close; 654 } 655 /* Ok, remote host can access at least one resource. */ 656 657 if (hast_proto_recv_hdr(conn, &nvin) < 0) { 658 pjdlog_errno(LOG_ERR, "Unable to receive header from %s", 659 raddr); 660 goto close; 661 } 662 663 resname = nv_get_string(nvin, "resource"); 664 if (resname == NULL) { 665 pjdlog_error("No 'resource' field in the header received from %s.", 666 raddr); 667 goto close; 668 } 669 pjdlog_debug(2, "%s: resource=%s", raddr, resname); 670 token = nv_get_uint8_array(nvin, &size, "token"); 671 /* 672 * NULL token means that this is first conection. 673 */ 674 if (token != NULL && size != sizeof(res->hr_token)) { 675 pjdlog_error("Received token of invalid size from %s (expected %zu, got %zu).", 676 raddr, sizeof(res->hr_token), size); 677 goto close; 678 } 679 680 /* 681 * From now on we want to send errors to the remote node. 682 */ 683 nverr = nv_alloc(); 684 685 /* Find resource related to this connection. */ 686 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) { 687 if (strcmp(resname, res->hr_name) == 0) 688 break; 689 } 690 /* Have we found the resource? */ 691 if (res == NULL) { 692 pjdlog_error("No resource '%s' as requested by %s.", 693 resname, raddr); 694 nv_add_stringf(nverr, "errmsg", "Resource not configured."); 695 goto fail; 696 } 697 698 /* Now that we know resource name setup log prefix. */ 699 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role)); 700 701 /* Does the remote host have access to this resource? */ 702 if (!proto_address_match(conn, res->hr_remoteaddr)) { 703 pjdlog_error("Client %s has no access to the resource.", raddr); 704 nv_add_stringf(nverr, "errmsg", "No access to the resource."); 705 goto fail; 706 } 707 /* Is the resource marked as secondary? */ 708 if (res->hr_role != HAST_ROLE_SECONDARY) { 709 pjdlog_error("We act as %s for the resource and not as %s as requested by %s.", 710 role2str(res->hr_role), role2str(HAST_ROLE_SECONDARY), 711 raddr); 712 nv_add_stringf(nverr, "errmsg", 713 "Remote node acts as %s for the resource and not as %s.", 714 role2str(res->hr_role), role2str(HAST_ROLE_SECONDARY)); 715 goto fail; 716 } 717 /* Does token (if exists) match? */ 718 if (token != NULL && memcmp(token, res->hr_token, 719 sizeof(res->hr_token)) != 0) { 720 pjdlog_error("Token received from %s doesn't match.", raddr); 721 nv_add_stringf(nverr, "errmsg", "Token doesn't match."); 722 goto fail; 723 } 724 /* 725 * If there is no token, but we have half-open connection 726 * (only remotein) or full connection (worker process is running) 727 * we have to cancel those and accept the new connection. 728 */ 729 if (token == NULL) { 730 PJDLOG_ASSERT(res->hr_remoteout == NULL); 731 pjdlog_debug(1, "Initial connection from %s.", raddr); 732 if (res->hr_workerpid != 0) { 733 PJDLOG_ASSERT(res->hr_remotein == NULL); 734 pjdlog_debug(1, 735 "Worker process exists (pid=%u), stopping it.", 736 (unsigned int)res->hr_workerpid); 737 /* Stop child process. */ 738 if (kill(res->hr_workerpid, SIGINT) < 0) { 739 pjdlog_errno(LOG_ERR, 740 "Unable to stop worker process (pid=%u)", 741 (unsigned int)res->hr_workerpid); 742 /* 743 * Other than logging the problem we 744 * ignore it - nothing smart to do. 745 */ 746 } 747 /* Wait for it to exit. */ 748 else if ((pid = waitpid(res->hr_workerpid, 749 &status, 0)) != res->hr_workerpid) { 750 /* We can only log the problem. */ 751 pjdlog_errno(LOG_ERR, 752 "Waiting for worker process (pid=%u) failed", 753 (unsigned int)res->hr_workerpid); 754 } else { 755 child_exit_log(res->hr_workerpid, status); 756 } 757 child_cleanup(res); 758 } else if (res->hr_remotein != NULL) { 759 char oaddr[256]; 760 761 proto_remote_address(res->hr_remotein, oaddr, 762 sizeof(oaddr)); 763 pjdlog_debug(1, 764 "Canceling half-open connection from %s on connection from %s.", 765 oaddr, raddr); 766 proto_close(res->hr_remotein); 767 res->hr_remotein = NULL; 768 } 769 } 770 771 /* 772 * Checks and cleanups are done. 773 */ 774 775 if (token == NULL) { 776 arc4random_buf(res->hr_token, sizeof(res->hr_token)); 777 nvout = nv_alloc(); 778 nv_add_uint8_array(nvout, res->hr_token, 779 sizeof(res->hr_token), "token"); 780 if (nv_error(nvout) != 0) { 781 pjdlog_common(LOG_ERR, 0, nv_error(nvout), 782 "Unable to prepare return header for %s", raddr); 783 nv_add_stringf(nverr, "errmsg", 784 "Remote node was unable to prepare return header: %s.", 785 strerror(nv_error(nvout))); 786 goto fail; 787 } 788 if (hast_proto_send(NULL, conn, nvout, NULL, 0) < 0) { 789 int error = errno; 790 791 pjdlog_errno(LOG_ERR, "Unable to send response to %s", 792 raddr); 793 nv_add_stringf(nverr, "errmsg", 794 "Remote node was unable to send response: %s.", 795 strerror(error)); 796 goto fail; 797 } 798 res->hr_remotein = conn; 799 pjdlog_debug(1, "Incoming connection from %s configured.", 800 raddr); 801 } else { 802 res->hr_remoteout = conn; 803 pjdlog_debug(1, "Outgoing connection to %s configured.", raddr); 804 hastd_secondary(res, nvin); 805 } 806 nv_free(nvin); 807 nv_free(nvout); 808 nv_free(nverr); 809 pjdlog_prefix_set("%s", ""); 810 return; 811fail: 812 if (nv_error(nverr) != 0) { 813 pjdlog_common(LOG_ERR, 0, nv_error(nverr), 814 "Unable to prepare error header for %s", raddr); 815 goto close; 816 } 817 if (hast_proto_send(NULL, conn, nverr, NULL, 0) < 0) { 818 pjdlog_errno(LOG_ERR, "Unable to send error to %s", raddr); 819 goto close; 820 } 821close: 822 if (nvin != NULL) 823 nv_free(nvin); 824 if (nvout != NULL) 825 nv_free(nvout); 826 if (nverr != NULL) 827 nv_free(nverr); 828 proto_close(conn); 829 pjdlog_prefix_set("%s", ""); 830} 831 832static void 833connection_migrate(struct hast_resource *res) 834{ 835 struct proto_conn *conn; 836 int16_t val = 0; 837 838 if (proto_recv(res->hr_conn, &val, sizeof(val)) < 0) { 839 pjdlog_errno(LOG_WARNING, 840 "Unable to receive connection command"); 841 return; 842 } 843 if (proto_client(res->hr_remoteaddr, &conn) < 0) { 844 val = errno; 845 pjdlog_errno(LOG_WARNING, 846 "Unable to create outgoing connection to %s", 847 res->hr_remoteaddr); 848 goto out; 849 } 850 if (proto_connect(conn, -1) < 0) { 851 val = errno; 852 pjdlog_errno(LOG_WARNING, "Unable to connect to %s", 853 res->hr_remoteaddr); 854 proto_close(conn); 855 goto out; 856 } 857 val = 0; 858out: 859 if (proto_send(res->hr_conn, &val, sizeof(val)) < 0) { 860 pjdlog_errno(LOG_WARNING, 861 "Unable to send reply to connection request"); 862 } 863 if (val == 0 && proto_connection_send(res->hr_conn, conn) < 0) 864 pjdlog_errno(LOG_WARNING, "Unable to send connection"); 865} 866 867static void 868main_loop(void) 869{ 870 struct hast_resource *res; 871 struct timeval seltimeout; 872 struct timespec sigtimeout; 873 int fd, maxfd, ret, signo; 874 sigset_t mask; 875 fd_set rfds; 876 877 seltimeout.tv_sec = REPORT_INTERVAL; 878 seltimeout.tv_usec = 0; 879 sigtimeout.tv_sec = 0; 880 sigtimeout.tv_nsec = 0; 881 882 PJDLOG_VERIFY(sigemptyset(&mask) == 0); 883 PJDLOG_VERIFY(sigaddset(&mask, SIGHUP) == 0); 884 PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0); 885 PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0); 886 PJDLOG_VERIFY(sigaddset(&mask, SIGCHLD) == 0); 887 888 pjdlog_info("Started successfully, running protocol version %d.", 889 HAST_PROTO_VERSION); 890 891 for (;;) { 892 while ((signo = sigtimedwait(&mask, NULL, &sigtimeout)) != -1) { 893 switch (signo) { 894 case SIGINT: 895 case SIGTERM: 896 sigexit_received = true; 897 terminate_workers(); 898 proto_close(cfg->hc_controlconn); 899 exit(EX_OK); 900 break; 901 case SIGCHLD: 902 child_exit(); 903 break; 904 case SIGHUP: 905 hastd_reload(); 906 break; 907 default: 908 PJDLOG_ABORT("Unexpected signal (%d).", signo); 909 } 910 } 911 912 /* Setup descriptors for select(2). */ 913 FD_ZERO(&rfds); 914 maxfd = fd = proto_descriptor(cfg->hc_controlconn); 915 PJDLOG_ASSERT(fd >= 0); 916 FD_SET(fd, &rfds); 917 fd = proto_descriptor(cfg->hc_listenconn); 918 PJDLOG_ASSERT(fd >= 0); 919 FD_SET(fd, &rfds); 920 maxfd = fd > maxfd ? fd : maxfd; 921 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) { 922 if (res->hr_event == NULL) 923 continue; 924 PJDLOG_ASSERT(res->hr_conn != NULL); 925 fd = proto_descriptor(res->hr_event); 926 PJDLOG_ASSERT(fd >= 0); 927 FD_SET(fd, &rfds); 928 maxfd = fd > maxfd ? fd : maxfd; 929 if (res->hr_role == HAST_ROLE_PRIMARY) { 930 /* Only primary workers asks for connections. */ 931 fd = proto_descriptor(res->hr_conn); 932 PJDLOG_ASSERT(fd >= 0); 933 FD_SET(fd, &rfds); 934 maxfd = fd > maxfd ? fd : maxfd; 935 } 936 } 937 938 PJDLOG_ASSERT(maxfd + 1 <= (int)FD_SETSIZE); 939 ret = select(maxfd + 1, &rfds, NULL, NULL, &seltimeout); 940 if (ret == 0) 941 hook_check(); 942 else if (ret == -1) { 943 if (errno == EINTR) 944 continue; 945 KEEP_ERRNO((void)pidfile_remove(pfh)); 946 pjdlog_exit(EX_OSERR, "select() failed"); 947 } 948 949 if (FD_ISSET(proto_descriptor(cfg->hc_controlconn), &rfds)) 950 control_handle(cfg); 951 if (FD_ISSET(proto_descriptor(cfg->hc_listenconn), &rfds)) 952 listen_accept(); 953 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) { 954 if (res->hr_event == NULL) 955 continue; 956 PJDLOG_ASSERT(res->hr_conn != NULL); 957 if (FD_ISSET(proto_descriptor(res->hr_event), &rfds)) { 958 if (event_recv(res) == 0) 959 continue; 960 /* The worker process exited? */ 961 proto_close(res->hr_event); 962 res->hr_event = NULL; 963 proto_close(res->hr_conn); 964 res->hr_conn = NULL; 965 continue; 966 } 967 if (res->hr_role == HAST_ROLE_PRIMARY && 968 FD_ISSET(proto_descriptor(res->hr_conn), &rfds)) { 969 connection_migrate(res); 970 } 971 } 972 } 973} 974 975static void 976dummy_sighandler(int sig __unused) 977{ 978 /* Nothing to do. */ 979} 980 981int 982main(int argc, char *argv[]) 983{ 984 const char *pidfile; 985 pid_t otherpid; 986 bool foreground; 987 int debuglevel; 988 sigset_t mask; 989 990 foreground = false; 991 debuglevel = 0; 992 pidfile = HASTD_PIDFILE; 993 994 for (;;) { 995 int ch; 996 997 ch = getopt(argc, argv, "c:dFhP:"); 998 if (ch == -1) 999 break; 1000 switch (ch) { 1001 case 'c': 1002 cfgpath = optarg; 1003 break; 1004 case 'd': 1005 debuglevel++; 1006 break; 1007 case 'F': 1008 foreground = true; 1009 break; 1010 case 'P': 1011 pidfile = optarg; 1012 break; 1013 case 'h': 1014 default: 1015 usage(); 1016 } 1017 } 1018 argc -= optind; 1019 argv += optind; 1020 1021 pjdlog_init(PJDLOG_MODE_STD); 1022 pjdlog_debug_set(debuglevel); 1023 1024 g_gate_load(); 1025 1026 pfh = pidfile_open(pidfile, 0600, &otherpid); 1027 if (pfh == NULL) { 1028 if (errno == EEXIST) { 1029 pjdlog_exitx(EX_TEMPFAIL, 1030 "Another hastd is already running, pid: %jd.", 1031 (intmax_t)otherpid); 1032 } 1033 /* If we cannot create pidfile from other reasons, only warn. */ 1034 pjdlog_errno(LOG_WARNING, "Unable to open or create pidfile"); 1035 } 1036 1037 cfg = yy_config_parse(cfgpath, true); 1038 PJDLOG_ASSERT(cfg != NULL); 1039 1040 /* 1041 * Restore default actions for interesting signals in case parent 1042 * process (like init(8)) decided to ignore some of them (like SIGHUP). 1043 */ 1044 PJDLOG_VERIFY(signal(SIGHUP, SIG_DFL) != SIG_ERR); 1045 PJDLOG_VERIFY(signal(SIGINT, SIG_DFL) != SIG_ERR); 1046 PJDLOG_VERIFY(signal(SIGTERM, SIG_DFL) != SIG_ERR); 1047 /* 1048 * Because SIGCHLD is ignored by default, setup dummy handler for it, 1049 * so we can mask it. 1050 */ 1051 PJDLOG_VERIFY(signal(SIGCHLD, dummy_sighandler) != SIG_ERR); 1052 1053 PJDLOG_VERIFY(sigemptyset(&mask) == 0); 1054 PJDLOG_VERIFY(sigaddset(&mask, SIGHUP) == 0); 1055 PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0); 1056 PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0); 1057 PJDLOG_VERIFY(sigaddset(&mask, SIGCHLD) == 0); 1058 PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0); 1059 1060 /* Listen on control address. */ 1061 if (proto_server(cfg->hc_controladdr, &cfg->hc_controlconn) < 0) { 1062 KEEP_ERRNO((void)pidfile_remove(pfh)); 1063 pjdlog_exit(EX_OSERR, "Unable to listen on control address %s", 1064 cfg->hc_controladdr); 1065 } 1066 /* Listen for remote connections. */ 1067 if (proto_server(cfg->hc_listenaddr, &cfg->hc_listenconn) < 0) { 1068 KEEP_ERRNO((void)pidfile_remove(pfh)); 1069 pjdlog_exit(EX_OSERR, "Unable to listen on address %s", 1070 cfg->hc_listenaddr); 1071 } 1072 1073 if (!foreground) { 1074 if (daemon(0, 0) < 0) { 1075 KEEP_ERRNO((void)pidfile_remove(pfh)); 1076 pjdlog_exit(EX_OSERR, "Unable to daemonize"); 1077 } 1078 1079 /* Start logging to syslog. */ 1080 pjdlog_mode_set(PJDLOG_MODE_SYSLOG); 1081 1082 /* Write PID to a file. */ 1083 if (pidfile_write(pfh) < 0) { 1084 pjdlog_errno(LOG_WARNING, 1085 "Unable to write PID to a file"); 1086 } 1087 } 1088 1089 hook_init(); 1090 1091 main_loop(); 1092 1093 exit(0); 1094} 1095