Deleted Added
full compact
hastd.c (226461) hastd.c (226463)
1/*-
2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * Copyright (c) 2010-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
4 * All rights reserved.
5 *
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
8 *

--- 15 unchanged lines hidden (view full) ---

24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * Copyright (c) 2010-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
4 * All rights reserved.
5 *
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
8 *

--- 15 unchanged lines hidden (view full) ---

24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sbin/hastd/hastd.c 226461 2011-10-17 09:54:07Z pjd $");
32__FBSDID("$FreeBSD: head/sbin/hastd/hastd.c 226463 2011-10-17 12:22:09Z pjd $");
33
34#include <sys/param.h>
35#include <sys/linker.h>
36#include <sys/module.h>
37#include <sys/stat.h>
38#include <sys/wait.h>
39
40#include <err.h>

--- 430 unchanged lines hidden (view full) ---

471}
472
473static void
474hastd_reload(void)
475{
476 struct hastd_config *newcfg;
477 struct hast_resource *nres, *cres, *tres;
478 struct hastd_listen *nlst, *clst;
33
34#include <sys/param.h>
35#include <sys/linker.h>
36#include <sys/module.h>
37#include <sys/stat.h>
38#include <sys/wait.h>
39
40#include <err.h>

--- 430 unchanged lines hidden (view full) ---

471}
472
473static void
474hastd_reload(void)
475{
476 struct hastd_config *newcfg;
477 struct hast_resource *nres, *cres, *tres;
478 struct hastd_listen *nlst, *clst;
479 struct pidfh *newpfh;
479 unsigned int nlisten;
480 uint8_t role;
480 unsigned int nlisten;
481 uint8_t role;
482 pid_t otherpid;
481
482 pjdlog_info("Reloading configuration...");
483
483
484 pjdlog_info("Reloading configuration...");
485
486 newpfh = NULL;
487
484 newcfg = yy_config_parse(cfgpath, false);
485 if (newcfg == NULL)
486 goto failed;
487
488 /*
489 * Check if control address has changed.
490 */
491 if (strcmp(cfg->hc_controladdr, newcfg->hc_controladdr) != 0) {

--- 27 unchanged lines hidden (view full) ---

519 pjdlog_errno(LOG_WARNING,
520 "Unable to listen on address %s", nlst->hl_addr);
521 }
522 }
523 if (nlisten == 0) {
524 pjdlog_error("No addresses to listen on.");
525 goto failed;
526 }
488 newcfg = yy_config_parse(cfgpath, false);
489 if (newcfg == NULL)
490 goto failed;
491
492 /*
493 * Check if control address has changed.
494 */
495 if (strcmp(cfg->hc_controladdr, newcfg->hc_controladdr) != 0) {

--- 27 unchanged lines hidden (view full) ---

523 pjdlog_errno(LOG_WARNING,
524 "Unable to listen on address %s", nlst->hl_addr);
525 }
526 }
527 if (nlisten == 0) {
528 pjdlog_error("No addresses to listen on.");
529 goto failed;
530 }
531 /*
532 * Check if pidfile's path has changed.
533 */
534 if (strcmp(cfg->hc_pidfile, newcfg->hc_pidfile) != 0) {
535 newpfh = pidfile_open(newcfg->hc_pidfile, 0600, &otherpid);
536 if (newpfh == NULL) {
537 if (errno == EEXIST) {
538 pjdlog_errno(LOG_WARNING,
539 "Another hastd is already running, pidfile: %s, pid: %jd.",
540 newcfg->hc_pidfile, (intmax_t)otherpid);
541 } else {
542 pjdlog_errno(LOG_WARNING,
543 "Unable to open or create pidfile %s",
544 newcfg->hc_pidfile);
545 }
546 } else if (pidfile_write(newpfh) < 0) {
547 /* Write PID to a file. */
548 pjdlog_errno(LOG_WARNING,
549 "Unable to write PID to file %s",
550 newcfg->hc_pidfile);
551 } else {
552 pjdlog_debug(1, "PID stored in %s.",
553 newcfg->hc_pidfile);
554 }
555 }
527
528 /* No failures from now on. */
529
530 /*
531 * Switch to new control socket.
532 */
533 if (newcfg->hc_controlconn != NULL) {
534 pjdlog_info("Control socket changed from %s to %s.",
535 cfg->hc_controladdr, newcfg->hc_controladdr);
536 proto_close(cfg->hc_controlconn);
537 cfg->hc_controlconn = newcfg->hc_controlconn;
538 newcfg->hc_controlconn = NULL;
539 strlcpy(cfg->hc_controladdr, newcfg->hc_controladdr,
540 sizeof(cfg->hc_controladdr));
541 }
542 /*
556
557 /* No failures from now on. */
558
559 /*
560 * Switch to new control socket.
561 */
562 if (newcfg->hc_controlconn != NULL) {
563 pjdlog_info("Control socket changed from %s to %s.",
564 cfg->hc_controladdr, newcfg->hc_controladdr);
565 proto_close(cfg->hc_controlconn);
566 cfg->hc_controlconn = newcfg->hc_controlconn;
567 newcfg->hc_controlconn = NULL;
568 strlcpy(cfg->hc_controladdr, newcfg->hc_controladdr,
569 sizeof(cfg->hc_controladdr));
570 }
571 /*
572 * Switch to new pidfile.
573 */
574 (void)pidfile_remove(pfh);
575 pfh = newpfh;
576 (void)strlcpy(cfg->hc_pidfile, newcfg->hc_pidfile,
577 sizeof(cfg->hc_pidfile));
578 /*
543 * Switch to new listen addresses. Close all that were removed.
544 */
545 while ((clst = TAILQ_FIRST(&cfg->hc_listen)) != NULL) {
546 TAILQ_FOREACH(nlst, &newcfg->hc_listen, hl_next) {
547 if (strcmp(nlst->hl_addr, clst->hl_addr) == 0)
548 break;
549 }
550 if (nlst == NULL && clst->hl_conn != NULL) {

--- 110 unchanged lines hidden (view full) ---

661 if (clst == NULL || clst->hl_conn == NULL)
662 proto_close(nlst->hl_conn);
663 }
664 TAILQ_REMOVE(&newcfg->hc_listen, nlst, hl_next);
665 free(nlst);
666 }
667 yy_config_free(newcfg);
668 }
579 * Switch to new listen addresses. Close all that were removed.
580 */
581 while ((clst = TAILQ_FIRST(&cfg->hc_listen)) != NULL) {
582 TAILQ_FOREACH(nlst, &newcfg->hc_listen, hl_next) {
583 if (strcmp(nlst->hl_addr, clst->hl_addr) == 0)
584 break;
585 }
586 if (nlst == NULL && clst->hl_conn != NULL) {

--- 110 unchanged lines hidden (view full) ---

697 if (clst == NULL || clst->hl_conn == NULL)
698 proto_close(nlst->hl_conn);
699 }
700 TAILQ_REMOVE(&newcfg->hc_listen, nlst, hl_next);
701 free(nlst);
702 }
703 yy_config_free(newcfg);
704 }
705 if (newpfh != NULL)
706 (void)pidfile_remove(newpfh);
669 pjdlog_warning("Configuration not reloaded.");
670}
671
672static void
673terminate_workers(void)
674{
675 struct hast_resource *res;
676

--- 441 unchanged lines hidden (view full) ---

1118 const char *pidfile;
1119 pid_t otherpid;
1120 bool foreground;
1121 int debuglevel;
1122 sigset_t mask;
1123
1124 foreground = false;
1125 debuglevel = 0;
707 pjdlog_warning("Configuration not reloaded.");
708}
709
710static void
711terminate_workers(void)
712{
713 struct hast_resource *res;
714

--- 441 unchanged lines hidden (view full) ---

1156 const char *pidfile;
1157 pid_t otherpid;
1158 bool foreground;
1159 int debuglevel;
1160 sigset_t mask;
1161
1162 foreground = false;
1163 debuglevel = 0;
1126 pidfile = HASTD_PIDFILE;
1164 pidfile = NULL;
1127
1128 for (;;) {
1129 int ch;
1130
1131 ch = getopt(argc, argv, "c:dFhP:");
1132 if (ch == -1)
1133 break;
1134 switch (ch) {

--- 17 unchanged lines hidden (view full) ---

1152 argc -= optind;
1153 argv += optind;
1154
1155 pjdlog_init(PJDLOG_MODE_STD);
1156 pjdlog_debug_set(debuglevel);
1157
1158 g_gate_load();
1159
1165
1166 for (;;) {
1167 int ch;
1168
1169 ch = getopt(argc, argv, "c:dFhP:");
1170 if (ch == -1)
1171 break;
1172 switch (ch) {

--- 17 unchanged lines hidden (view full) ---

1190 argc -= optind;
1191 argv += optind;
1192
1193 pjdlog_init(PJDLOG_MODE_STD);
1194 pjdlog_debug_set(debuglevel);
1195
1196 g_gate_load();
1197
1160 pfh = pidfile_open(pidfile, 0600, &otherpid);
1161 if (pfh == NULL) {
1162 if (errno == EEXIST) {
1163 pjdlog_exitx(EX_TEMPFAIL,
1164 "Another hastd is already running, pid: %jd.",
1165 (intmax_t)otherpid);
1166 }
1167 /* If we cannot create pidfile from other reasons, only warn. */
1168 pjdlog_errno(LOG_WARNING, "Unable to open or create pidfile");
1169 }
1170
1171 /*
1172 * When path to the configuration file is relative, obtain full path,
1173 * so we can always find the file, even after daemonizing and changing
1174 * working directory to /.
1175 */
1176 if (cfgpath[0] != '/') {
1177 const char *newcfgpath;
1178
1179 newcfgpath = realpath(cfgpath, NULL);
1180 if (newcfgpath == NULL) {
1181 pjdlog_exit(EX_CONFIG,
1182 "Unable to obtain full path of %s", cfgpath);
1183 }
1184 cfgpath = newcfgpath;
1185 }
1186
1187 cfg = yy_config_parse(cfgpath, true);
1188 PJDLOG_ASSERT(cfg != NULL);
1189
1198 /*
1199 * When path to the configuration file is relative, obtain full path,
1200 * so we can always find the file, even after daemonizing and changing
1201 * working directory to /.
1202 */
1203 if (cfgpath[0] != '/') {
1204 const char *newcfgpath;
1205
1206 newcfgpath = realpath(cfgpath, NULL);
1207 if (newcfgpath == NULL) {
1208 pjdlog_exit(EX_CONFIG,
1209 "Unable to obtain full path of %s", cfgpath);
1210 }
1211 cfgpath = newcfgpath;
1212 }
1213
1214 cfg = yy_config_parse(cfgpath, true);
1215 PJDLOG_ASSERT(cfg != NULL);
1216
1217 if (pidfile != NULL) {
1218 if (strlcpy(cfg->hc_pidfile, pidfile,
1219 sizeof(cfg->hc_pidfile)) >= sizeof(cfg->hc_pidfile)) {
1220 pjdlog_exitx(EX_CONFIG, "Pidfile path is too long.");
1221 }
1222 }
1223 pfh = pidfile_open(cfg->hc_pidfile, 0600, &otherpid);
1224 if (pfh == NULL) {
1225 if (errno == EEXIST) {
1226 pjdlog_exitx(EX_TEMPFAIL,
1227 "Another hastd is already running, pidfile: %s, pid: %jd.",
1228 cfg->hc_pidfile, (intmax_t)otherpid);
1229 }
1230 /* If we cannot create pidfile for other reasons, only warn. */
1231 pjdlog_errno(LOG_WARNING, "Unable to open or create pidfile %s",
1232 cfg->hc_pidfile);
1233 }
1234
1190 /*
1191 * Restore default actions for interesting signals in case parent
1192 * process (like init(8)) decided to ignore some of them (like SIGHUP).
1193 */
1194 PJDLOG_VERIFY(signal(SIGHUP, SIG_DFL) != SIG_ERR);
1195 PJDLOG_VERIFY(signal(SIGINT, SIG_DFL) != SIG_ERR);
1196 PJDLOG_VERIFY(signal(SIGTERM, SIG_DFL) != SIG_ERR);
1197 /*

--- 31 unchanged lines hidden (view full) ---

1229 }
1230
1231 /* Start logging to syslog. */
1232 pjdlog_mode_set(PJDLOG_MODE_SYSLOG);
1233
1234 /* Write PID to a file. */
1235 if (pidfile_write(pfh) < 0) {
1236 pjdlog_errno(LOG_WARNING,
1235 /*
1236 * Restore default actions for interesting signals in case parent
1237 * process (like init(8)) decided to ignore some of them (like SIGHUP).
1238 */
1239 PJDLOG_VERIFY(signal(SIGHUP, SIG_DFL) != SIG_ERR);
1240 PJDLOG_VERIFY(signal(SIGINT, SIG_DFL) != SIG_ERR);
1241 PJDLOG_VERIFY(signal(SIGTERM, SIG_DFL) != SIG_ERR);
1242 /*

--- 31 unchanged lines hidden (view full) ---

1274 }
1275
1276 /* Start logging to syslog. */
1277 pjdlog_mode_set(PJDLOG_MODE_SYSLOG);
1278
1279 /* Write PID to a file. */
1280 if (pidfile_write(pfh) < 0) {
1281 pjdlog_errno(LOG_WARNING,
1237 "Unable to write PID to a file");
1282 "Unable to write PID to a file %s",
1283 cfg->hc_pidfile);
1284 } else {
1285 pjdlog_debug(1, "PID stored in %s.", cfg->hc_pidfile);
1238 }
1239 }
1240
1241 pjdlog_info("Started successfully, running protocol version %d.",
1242 HAST_PROTO_VERSION);
1243
1244 pjdlog_debug(1, "Listening on control address %s.",
1245 cfg->hc_controladdr);
1246 TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next)
1247 pjdlog_info("Listening on address %s.", lst->hl_addr);
1248
1249 hook_init();
1250
1251 main_loop();
1252
1253 exit(0);
1254}
1286 }
1287 }
1288
1289 pjdlog_info("Started successfully, running protocol version %d.",
1290 HAST_PROTO_VERSION);
1291
1292 pjdlog_debug(1, "Listening on control address %s.",
1293 cfg->hc_controladdr);
1294 TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next)
1295 pjdlog_info("Listening on address %s.", lst->hl_addr);
1296
1297 hook_init();
1298
1299 main_loop();
1300
1301 exit(0);
1302}