hastd.c (213008) | hastd.c (213009) |
---|---|
1/*- 2 * Copyright (c) 2009-2010 The FreeBSD Foundation 3 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org> 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 Pawel Jakub Dawidek <pjd@FreeBSD.org> 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 213008 2010-09-22 19:05:54Z pjd $"); | 32__FBSDID("$FreeBSD: head/sbin/hastd/hastd.c 213009 2010-09-22 19:08:11Z pjd $"); |
33 34#include <sys/param.h> 35#include <sys/linker.h> 36#include <sys/module.h> 37#include <sys/wait.h> 38 39#include <assert.h> 40#include <err.h> --- 17 unchanged lines hidden (view full) --- 58#include "hastd.h" 59#include "hooks.h" 60#include "subr.h" 61 62/* Path to configuration file. */ 63const char *cfgpath = HAST_CONFIG; 64/* Hastd configuration. */ 65static struct hastd_config *cfg; | 33 34#include <sys/param.h> 35#include <sys/linker.h> 36#include <sys/module.h> 37#include <sys/wait.h> 38 39#include <assert.h> 40#include <err.h> --- 17 unchanged lines hidden (view full) --- 58#include "hastd.h" 59#include "hooks.h" 60#include "subr.h" 61 62/* Path to configuration file. */ 63const char *cfgpath = HAST_CONFIG; 64/* Hastd configuration. */ 65static struct hastd_config *cfg; |
66/* Was SIGCHLD signal received? */ 67bool sigchld_received = false; 68/* Was SIGHUP signal received? */ 69bool sighup_received = false; | |
70/* Was SIGINT or SIGTERM signal received? */ 71bool sigexit_received = false; 72/* PID file handle. */ 73struct pidfh *pfh; 74 75/* How often check for hooks running for too long. */ 76#define REPORT_INTERVAL 10 77 78static void 79usage(void) 80{ 81 82 errx(EX_USAGE, "[-dFh] [-c config] [-P pidfile]"); 83} 84 85static void | 66/* Was SIGINT or SIGTERM signal received? */ 67bool sigexit_received = false; 68/* PID file handle. */ 69struct pidfh *pfh; 70 71/* How often check for hooks running for too long. */ 72#define REPORT_INTERVAL 10 73 74static void 75usage(void) 76{ 77 78 errx(EX_USAGE, "[-dFh] [-c config] [-P pidfile]"); 79} 80 81static void |
86sighandler(int sig) 87{ 88 89 switch (sig) { 90 case SIGINT: 91 case SIGTERM: 92 sigexit_received = true; 93 break; 94 case SIGCHLD: 95 sigchld_received = true; 96 break; 97 case SIGHUP: 98 sighup_received = true; 99 break; 100 default: 101 assert(!"invalid condition"); 102 } 103} 104 105static void | |
106g_gate_load(void) 107{ 108 109 if (modfind("g_gate") == -1) { 110 /* Not present in kernel, try loading it. */ 111 if (kldload("geom_gate") == -1 || modfind("g_gate") == -1) { 112 if (errno != EEXIST) { 113 pjdlog_exit(EX_OSERR, --- 506 unchanged lines hidden (view full) --- 620 proto_close(conn); 621 pjdlog_prefix_set("%s", ""); 622} 623 624static void 625main_loop(void) 626{ 627 struct hast_resource *res; | 82g_gate_load(void) 83{ 84 85 if (modfind("g_gate") == -1) { 86 /* Not present in kernel, try loading it. */ 87 if (kldload("geom_gate") == -1 || modfind("g_gate") == -1) { 88 if (errno != EEXIST) { 89 pjdlog_exit(EX_OSERR, --- 506 unchanged lines hidden (view full) --- 596 proto_close(conn); 597 pjdlog_prefix_set("%s", ""); 598} 599 600static void 601main_loop(void) 602{ 603 struct hast_resource *res; |
628 struct timeval timeout; 629 int fd, maxfd, ret; | 604 struct timeval seltimeout; 605 struct timespec sigtimeout; 606 int fd, maxfd, ret, signo; 607 sigset_t mask; |
630 fd_set rfds; 631 | 608 fd_set rfds; 609 |
632 timeout.tv_sec = REPORT_INTERVAL; 633 timeout.tv_usec = 0; | 610 seltimeout.tv_sec = REPORT_INTERVAL; 611 seltimeout.tv_usec = 0; 612 sigtimeout.tv_sec = 0; 613 sigtimeout.tv_nsec = 0; |
634 | 614 |
615 PJDLOG_VERIFY(sigemptyset(&mask) == 0); 616 PJDLOG_VERIFY(sigaddset(&mask, SIGHUP) == 0); 617 PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0); 618 PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0); 619 PJDLOG_VERIFY(sigaddset(&mask, SIGCHLD) == 0); 620 |
|
635 for (;;) { | 621 for (;;) { |
636 if (sigexit_received) { 637 sigexit_received = false; 638 terminate_workers(); 639 exit(EX_OK); | 622 while ((signo = sigtimedwait(&mask, NULL, &sigtimeout)) != -1) { 623 switch (signo) { 624 case SIGINT: 625 case SIGTERM: 626 sigexit_received = true; 627 terminate_workers(); 628 exit(EX_OK); 629 break; 630 case SIGCHLD: 631 child_exit(); 632 break; 633 case SIGHUP: 634 hastd_reload(); 635 break; 636 default: 637 assert(!"invalid condition"); 638 } |
640 } | 639 } |
641 if (sigchld_received) { 642 sigchld_received = false; 643 child_exit(); 644 } 645 if (sighup_received) { 646 sighup_received = false; 647 hastd_reload(); 648 } | |
649 650 /* Setup descriptors for select(2). */ 651 FD_ZERO(&rfds); 652 maxfd = fd = proto_descriptor(cfg->hc_controlconn); 653 assert(fd >= 0); 654 FD_SET(fd, &rfds); 655 fd = proto_descriptor(cfg->hc_listenconn); 656 assert(fd >= 0); --- 4 unchanged lines hidden (view full) --- 661 continue; 662 fd = proto_descriptor(res->hr_event); 663 assert(fd >= 0); 664 FD_SET(fd, &rfds); 665 maxfd = fd > maxfd ? fd : maxfd; 666 } 667 668 assert(maxfd + 1 <= (int)FD_SETSIZE); | 640 641 /* Setup descriptors for select(2). */ 642 FD_ZERO(&rfds); 643 maxfd = fd = proto_descriptor(cfg->hc_controlconn); 644 assert(fd >= 0); 645 FD_SET(fd, &rfds); 646 fd = proto_descriptor(cfg->hc_listenconn); 647 assert(fd >= 0); --- 4 unchanged lines hidden (view full) --- 652 continue; 653 fd = proto_descriptor(res->hr_event); 654 assert(fd >= 0); 655 FD_SET(fd, &rfds); 656 maxfd = fd > maxfd ? fd : maxfd; 657 } 658 659 assert(maxfd + 1 <= (int)FD_SETSIZE); |
669 ret = select(maxfd + 1, &rfds, NULL, NULL, &timeout); | 660 ret = select(maxfd + 1, &rfds, NULL, NULL, &seltimeout); |
670 if (ret == 0) 671 hook_check(false); 672 else if (ret == -1) { 673 if (errno == EINTR) 674 continue; 675 KEEP_ERRNO((void)pidfile_remove(pfh)); 676 pjdlog_exit(EX_OSERR, "select() failed"); 677 } --- 18 unchanged lines hidden (view full) --- 696 697int 698main(int argc, char *argv[]) 699{ 700 const char *pidfile; 701 pid_t otherpid; 702 bool foreground; 703 int debuglevel; | 661 if (ret == 0) 662 hook_check(false); 663 else if (ret == -1) { 664 if (errno == EINTR) 665 continue; 666 KEEP_ERRNO((void)pidfile_remove(pfh)); 667 pjdlog_exit(EX_OSERR, "select() failed"); 668 } --- 18 unchanged lines hidden (view full) --- 687 688int 689main(int argc, char *argv[]) 690{ 691 const char *pidfile; 692 pid_t otherpid; 693 bool foreground; 694 int debuglevel; |
695 sigset_t mask; |
|
704 705 g_gate_load(); 706 707 foreground = false; 708 debuglevel = 0; 709 pidfile = HASTD_PIDFILE; 710 711 for (;;) { --- 34 unchanged lines hidden (view full) --- 746 } 747 /* If we cannot create pidfile from other reasons, only warn. */ 748 pjdlog_errno(LOG_WARNING, "Unable to open or create pidfile"); 749 } 750 751 cfg = yy_config_parse(cfgpath, true); 752 assert(cfg != NULL); 753 | 696 697 g_gate_load(); 698 699 foreground = false; 700 debuglevel = 0; 701 pidfile = HASTD_PIDFILE; 702 703 for (;;) { --- 34 unchanged lines hidden (view full) --- 738 } 739 /* If we cannot create pidfile from other reasons, only warn. */ 740 pjdlog_errno(LOG_WARNING, "Unable to open or create pidfile"); 741 } 742 743 cfg = yy_config_parse(cfgpath, true); 744 assert(cfg != NULL); 745 |
754 signal(SIGINT, sighandler); 755 signal(SIGTERM, sighandler); 756 signal(SIGHUP, sighandler); 757 signal(SIGCHLD, sighandler); | 746 PJDLOG_VERIFY(sigemptyset(&mask) == 0); 747 PJDLOG_VERIFY(sigaddset(&mask, SIGHUP) == 0); 748 PJDLOG_VERIFY(sigaddset(&mask, SIGINT) == 0); 749 PJDLOG_VERIFY(sigaddset(&mask, SIGTERM) == 0); 750 PJDLOG_VERIFY(sigaddset(&mask, SIGCHLD) == 0); 751 PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0); |
758 759 /* Listen on control address. */ 760 if (proto_server(cfg->hc_controladdr, &cfg->hc_controlconn) < 0) { 761 KEEP_ERRNO((void)pidfile_remove(pfh)); 762 pjdlog_exit(EX_OSERR, "Unable to listen on control address %s", 763 cfg->hc_controladdr); 764 } 765 /* Listen for remote connections. */ --- 28 unchanged lines hidden --- | 752 753 /* Listen on control address. */ 754 if (proto_server(cfg->hc_controladdr, &cfg->hc_controlconn) < 0) { 755 KEEP_ERRNO((void)pidfile_remove(pfh)); 756 pjdlog_exit(EX_OSERR, "Unable to listen on control address %s", 757 cfg->hc_controladdr); 758 } 759 /* Listen for remote connections. */ --- 28 unchanged lines hidden --- |