2 * Bacula File Daemon heartbeat routines
3 * Listens for heartbeats coming from the SD
4 * If configured, sends heartbeats to Dir
6 * Kern Sibbald, May MMIII
12 Copyright (C) 2003-2005 Kern Sibbald
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License
16 version 2 as ammended with additional clauses defined in the
17 file LICENSE in the main source directory.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 the file LICENSE for additional details.
29 #define WAIT_INTERVAL 10
31 extern "C" void *sd_heartbeat_thread(void *arg);
32 extern "C" void *dir_heartbeat_thread(void *arg);
33 extern bool no_signals;
36 * Listen on the SD socket for heartbeat signals.
37 * Send heartbeats to the Director every HB_TIME
40 extern "C" void *sd_heartbeat_thread(void *arg)
43 JCR *jcr = (JCR *)arg;
45 time_t last_heartbeat = time(NULL);
49 pthread_detach(pthread_self());
51 /* Get our own local copy */
52 sd = dup_bsock(jcr->store_bsock);
53 if ((oflags = fcntl(sd->fd, F_GETFL, 0)) != -1) {
54 fcntl(sd->fd, F_SETFL, oflags|O_NONBLOCK);
56 dir = dup_bsock(jcr->dir_bsock);
59 jcr->hb_dir_bsock = dir;
61 /* Hang reading the socket to the SD, and every time we get
62 * a heartbeat or we get a wait timeout (1 minute), we
63 * check to see if we need to send a heartbeat to the
66 for ( ; !is_bnet_stop(sd); ) {
67 n = bnet_wait_data_intr(sd, WAIT_INTERVAL);
68 if (me->heartbeat_interval) {
70 if (now-last_heartbeat >= me->heartbeat_interval) {
71 bnet_sig(dir, BNET_HEARTBEAT);
75 if (is_bnet_stop(sd)) {
78 if (n == 1) { /* input waiting */
79 bnet_recv(sd); /* read it -- probably heartbeat from sd */
80 if (sd->msglen <= 0) {
81 Dmsg1(100, "Got BNET_SIG %d from SD\n", sd->msglen);
83 Dmsg2(100, "Got %d bytes from SD. MSG=%s\n", sd->msglen, sd->msg);
90 jcr->hb_dir_bsock = NULL;
94 /* Startup the heartbeat thread -- see above */
95 void start_heartbeat_monitor(JCR *jcr)
98 * If no signals are set, do not start the heartbeat because
99 * it gives a constant stream of TIMEOUT_SIGNAL signals that
100 * make debugging impossible.
103 jcr->hb_bsock = NULL;
104 jcr->hb_dir_bsock = NULL;
105 pthread_create(&jcr->heartbeat_id, NULL, sd_heartbeat_thread, (void *)jcr);
109 /* Terminate the heartbeat thread. Used for both SD and DIR */
110 void stop_heartbeat_monitor(JCR *jcr)
116 /* Wait max 10 secs for heartbeat thread to start */
117 while (jcr->hb_bsock == NULL && cnt++ < 200) {
118 bmicrosleep(0, 50000); /* wait for start */
120 if (!jcr->hb_bsock) {
124 jcr->hb_bsock->timed_out = 1; /* set timed_out to terminate read */
125 jcr->hb_bsock->terminated = 1; /* set to terminate read */
127 if (jcr->hb_dir_bsock) {
128 jcr->hb_dir_bsock->timed_out = 1; /* set timed_out to terminate read */
129 jcr->hb_dir_bsock->terminated = 1; /* set to terminate read */
131 pthread_kill(jcr->heartbeat_id, TIMEOUT_SIGNAL); /* make heartbeat thread go away */
132 bmicrosleep(0, 50000);
134 /* Wait max 100 secs for heartbeat thread to stop */
135 while (jcr->hb_bsock && cnt++ < 200) {
136 pthread_kill(jcr->heartbeat_id, TIMEOUT_SIGNAL); /* make heartbeat thread go away */
137 bmicrosleep(0, 500000);
144 * Thread for sending heartbeats to the Director when there
145 * is no SD monitoring needed -- e.g. restore and verify Vol
146 * both do their own read() on the SD socket.
148 extern "C" void *dir_heartbeat_thread(void *arg)
150 JCR *jcr = (JCR *)arg;
152 time_t last_heartbeat = time(NULL);
154 pthread_detach(pthread_self());
156 /* Get our own local copy */
157 dir = dup_bsock(jcr->dir_bsock);
161 for ( ; !is_bnet_stop(dir); ) {
165 next = now - last_heartbeat;
166 if (next >= me->heartbeat_interval) {
167 bnet_sig(dir, BNET_HEARTBEAT);
168 last_heartbeat = now;
170 bmicrosleep(next, 0);
173 jcr->hb_bsock = NULL;
178 * Same as above but we don't listen to the SD
180 void start_dir_heartbeat(JCR *jcr)
182 if (me->heartbeat_interval) {
183 pthread_create(&jcr->heartbeat_id, NULL, dir_heartbeat_thread, (void *)jcr);
187 void stop_dir_heartbeat(JCR *jcr)
189 if (me->heartbeat_interval) {
190 stop_heartbeat_monitor(jcr);