Deleted Added
full compact
hastd.c (211899) hastd.c (211977)
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 211899 2010-08-27 21:28:02Z pjd $");
32__FBSDID("$FreeBSD: head/sbin/hastd/hastd.c 211977 2010-08-29 21:41:53Z 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>

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

50
51#include <activemap.h>
52#include <pjdlog.h>
53
54#include "control.h"
55#include "hast.h"
56#include "hast_proto.h"
57#include "hastd.h"
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>

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

50
51#include <activemap.h>
52#include <pjdlog.h>
53
54#include "control.h"
55#include "hast.h"
56#include "hast_proto.h"
57#include "hastd.h"
58#include "hooks.h"
58#include "subr.h"
59
60/* Path to configuration file. */
61const char *cfgpath = HAST_CONFIG;
62/* Hastd configuration. */
63static struct hastd_config *cfg;
64/* Was SIGCHLD signal received? */
65bool sigchld_received = false;
66/* Was SIGHUP signal received? */
67bool sighup_received = false;
68/* Was SIGINT or SIGTERM signal received? */
69bool sigexit_received = false;
70/* PID file handle. */
71struct pidfh *pfh;
72
59#include "subr.h"
60
61/* Path to configuration file. */
62const char *cfgpath = HAST_CONFIG;
63/* Hastd configuration. */
64static struct hastd_config *cfg;
65/* Was SIGCHLD signal received? */
66bool sigchld_received = false;
67/* Was SIGHUP signal received? */
68bool sighup_received = false;
69/* Was SIGINT or SIGTERM signal received? */
70bool sigexit_received = false;
71/* PID file handle. */
72struct pidfh *pfh;
73
74/* How often check for hooks running for too long. */
75#define REPORT_INTERVAL 10
76
73static void
74usage(void)
75{
76
77 errx(EX_USAGE, "[-dFh] [-c config] [-P pidfile]");
78}
79
80static void

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

139 /* Find resource related to the process that just exited. */
140 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
141 if (pid == res->hr_workerpid)
142 break;
143 }
144 if (res == NULL) {
145 /*
146 * This can happen when new connection arrives and we
77static void
78usage(void)
79{
80
81 errx(EX_USAGE, "[-dFh] [-c config] [-P pidfile]");
82}
83
84static void

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

143 /* Find resource related to the process that just exited. */
144 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
145 if (pid == res->hr_workerpid)
146 break;
147 }
148 if (res == NULL) {
149 /*
150 * This can happen when new connection arrives and we
147 * cancel child responsible for the old one.
151 * cancel child responsible for the old one or if this
152 * was hook which we executed.
148 */
153 */
154 hook_check_one(pid, status);
149 continue;
150 }
151 pjdlog_prefix_set("[%s] (%s) ", res->hr_name,
152 role2str(res->hr_role));
153 child_exit_log(pid, status);
154 proto_close(res->hr_ctrl);
155 res->hr_workerpid = 0;
156 if (res->hr_role == HAST_ROLE_PRIMARY) {

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

615 pjdlog_prefix_set("%s", "");
616}
617
618static void
619main_loop(void)
620{
621 fd_set rfds, wfds;
622 int cfd, lfd, maxfd, ret;
155 continue;
156 }
157 pjdlog_prefix_set("[%s] (%s) ", res->hr_name,
158 role2str(res->hr_role));
159 child_exit_log(pid, status);
160 proto_close(res->hr_ctrl);
161 res->hr_workerpid = 0;
162 if (res->hr_role == HAST_ROLE_PRIMARY) {

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

621 pjdlog_prefix_set("%s", "");
622}
623
624static void
625main_loop(void)
626{
627 fd_set rfds, wfds;
628 int cfd, lfd, maxfd, ret;
629 struct timeval timeout;
623
630
631 timeout.tv_sec = REPORT_INTERVAL;
632 timeout.tv_usec = 0;
633
624 for (;;) {
625 if (sigexit_received) {
626 sigexit_received = false;
627 terminate_workers();
628 exit(EX_OK);
629 }
630 if (sigchld_received) {
631 sigchld_received = false;

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

643 /* Setup descriptors for select(2). */
644 FD_ZERO(&rfds);
645 FD_SET(cfd, &rfds);
646 FD_SET(lfd, &rfds);
647 FD_ZERO(&wfds);
648 FD_SET(cfd, &wfds);
649 FD_SET(lfd, &wfds);
650
634 for (;;) {
635 if (sigexit_received) {
636 sigexit_received = false;
637 terminate_workers();
638 exit(EX_OK);
639 }
640 if (sigchld_received) {
641 sigchld_received = false;

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

653 /* Setup descriptors for select(2). */
654 FD_ZERO(&rfds);
655 FD_SET(cfd, &rfds);
656 FD_SET(lfd, &rfds);
657 FD_ZERO(&wfds);
658 FD_SET(cfd, &wfds);
659 FD_SET(lfd, &wfds);
660
651 ret = select(maxfd + 1, &rfds, &wfds, NULL, NULL);
652 if (ret == -1) {
661 ret = select(maxfd + 1, &rfds, &wfds, NULL, &timeout);
662 if (ret == 0)
663 hook_check(false);
664 else if (ret == -1) {
653 if (errno == EINTR)
654 continue;
655 KEEP_ERRNO((void)pidfile_remove(pfh));
656 pjdlog_exit(EX_OSERR, "select() failed");
657 }
658
659 if (FD_ISSET(cfd, &rfds) || FD_ISSET(cfd, &wfds))
660 control_handle(cfg);

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

749
750 /* Write PID to a file. */
751 if (pidfile_write(pfh) < 0) {
752 pjdlog_errno(LOG_WARNING,
753 "Unable to write PID to a file");
754 }
755 }
756
665 if (errno == EINTR)
666 continue;
667 KEEP_ERRNO((void)pidfile_remove(pfh));
668 pjdlog_exit(EX_OSERR, "select() failed");
669 }
670
671 if (FD_ISSET(cfd, &rfds) || FD_ISSET(cfd, &wfds))
672 control_handle(cfg);

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

761
762 /* Write PID to a file. */
763 if (pidfile_write(pfh) < 0) {
764 pjdlog_errno(LOG_WARNING,
765 "Unable to write PID to a file");
766 }
767 }
768
769 hook_init();
770
757 main_loop();
758
759 exit(0);
760}
771 main_loop();
772
773 exit(0);
774}