]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/heartbeat.c
Update copyright
[bacula/bacula] / bacula / src / filed / heartbeat.c
1 /*
2  *  Bacula File Daemon heartbeat routines
3  *    Listens for heartbeats coming from the SD
4  *    If configured, sends heartbeats to Dir
5  *
6  *    Kern Sibbald, May MMIII
7  *
8  *   Version $Id$
9  *
10  */
11 /*
12    Bacula® - The Network Backup Solution
13
14    Copyright (C) 2003-2006 Free Software Foundation Europe e.V.
15
16    The main author of Bacula is Kern Sibbald, with contributions from
17    many others, a complete list can be found in the file AUTHORS.
18    This program is Free Software; you can redistribute it and/or
19    modify it under the terms of version two of the GNU General Public
20    License as published by the Free Software Foundation plus additions
21    that are listed in the file LICENSE.
22
23    This program is distributed in the hope that it will be useful, but
24    WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26    General Public License for more details.
27
28    You should have received a copy of the GNU General Public License
29    along with this program; if not, write to the Free Software
30    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31    02110-1301, USA.
32
33    Bacula® is a registered trademark of John Walker.
34    The licensor of Bacula is the Free Software Foundation Europe
35    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
36    Switzerland, email:ftf@fsfeurope.org.
37 */
38
39 #include "bacula.h"
40 #include "filed.h"
41
42 #define WAIT_INTERVAL 5
43
44 extern "C" void *sd_heartbeat_thread(void *arg);
45 extern "C" void *dir_heartbeat_thread(void *arg);
46 extern bool no_signals;
47
48 /*
49  * Listen on the SD socket for heartbeat signals.
50  * Send heartbeats to the Director every HB_TIME
51  *   seconds.
52  */
53 extern "C" void *sd_heartbeat_thread(void *arg)
54 {
55    int32_t n;
56    JCR *jcr = (JCR *)arg;
57    BSOCK *sd, *dir;
58    time_t last_heartbeat = time(NULL);
59    time_t now;
60
61    pthread_detach(pthread_self());
62
63    /* Get our own local copy */
64    sd = dup_bsock(jcr->store_bsock);
65    dir = dup_bsock(jcr->dir_bsock);
66
67    jcr->hb_bsock = sd;
68    jcr->hb_dir_bsock = dir;
69
70    /* Hang reading the socket to the SD, and every time we get
71     *   a heartbeat or we get a wait timeout (1 minute), we
72     *   check to see if we need to send a heartbeat to the
73     *   Director.
74     */
75    for ( ; !is_bnet_stop(sd); ) {
76       n = bnet_wait_data_intr(sd, WAIT_INTERVAL);
77       if (me->heartbeat_interval) {
78          now = time(NULL);
79          if (now-last_heartbeat >= me->heartbeat_interval) {
80             bnet_sig(dir, BNET_HEARTBEAT);
81             last_heartbeat = now;
82          }
83       }
84       if (n < 0 || is_bnet_stop(sd)) {
85          break;
86       }
87       if (n == 1) {                   /* input waiting */
88          bnet_recv(sd);               /* read it -- probably heartbeat from sd */
89          if (sd->msglen <= 0) {
90             Dmsg1(100, "Got BNET_SIG %d from SD\n", sd->msglen);
91          } else {
92             Dmsg2(100, "Got %d bytes from SD. MSG=%s\n", sd->msglen, sd->msg);
93          }
94       }
95       Dmsg2(100, "wait_intr=%d stop=%d\n", n, is_bnet_stop(sd));
96    }
97    bnet_close(sd);
98    bnet_close(dir);
99    jcr->hb_bsock = NULL;
100    jcr->hb_dir_bsock = NULL;
101    return NULL;
102 }
103
104 /* Startup the heartbeat thread -- see above */
105 void start_heartbeat_monitor(JCR *jcr)
106 {
107    /*
108     * If no signals are set, do not start the heartbeat because
109     * it gives a constant stream of TIMEOUT_SIGNAL signals that
110     * make debugging impossible.
111     */
112    if (!no_signals) {
113       jcr->hb_bsock = NULL;
114       jcr->hb_dir_bsock = NULL;
115       pthread_create(&jcr->heartbeat_id, NULL, sd_heartbeat_thread, (void *)jcr);
116    }
117 }
118
119 /* Terminate the heartbeat thread. Used for both SD and DIR */
120 void stop_heartbeat_monitor(JCR *jcr)
121 {
122    int cnt = 0;
123    if (no_signals) {
124       return;
125    }
126    /* Wait max 10 secs for heartbeat thread to start */
127    while (jcr->hb_bsock == NULL && cnt++ < 200) {
128       bmicrosleep(0, 50000);         /* wait for start */
129    }
130    if (!jcr->hb_bsock) {
131    }
132
133    if (jcr->hb_bsock) {
134       jcr->hb_bsock->timed_out = 1;   /* set timed_out to terminate read */
135       jcr->hb_bsock->terminated = 1;  /* set to terminate read */
136    }
137    if (jcr->hb_dir_bsock) {
138       jcr->hb_dir_bsock->timed_out = 1;   /* set timed_out to terminate read */
139       jcr->hb_dir_bsock->terminated = 1;  /* set to terminate read */
140    }
141    pthread_kill(jcr->heartbeat_id, TIMEOUT_SIGNAL);  /* make heartbeat thread go away */
142    bmicrosleep(0, 50000);
143    cnt = 0;
144    /* Wait max 100 secs for heartbeat thread to stop */
145    while (jcr->hb_bsock && cnt++ < 200) {
146       pthread_kill(jcr->heartbeat_id, TIMEOUT_SIGNAL);  /* make heartbeat thread go away */
147       bmicrosleep(0, 500000);
148    }
149 }
150
151 /*
152  * Thread for sending heartbeats to the Director when there
153  *   is no SD monitoring needed -- e.g. restore and verify Vol
154  *   both do their own read() on the SD socket.
155  */
156 extern "C" void *dir_heartbeat_thread(void *arg)
157 {
158    JCR *jcr = (JCR *)arg;
159    BSOCK *dir;
160    time_t last_heartbeat = time(NULL);
161
162    pthread_detach(pthread_self());
163
164    /* Get our own local copy */
165    dir = dup_bsock(jcr->dir_bsock);
166
167    jcr->hb_bsock = dir;
168
169    for ( ; !is_bnet_stop(dir); ) {
170       time_t now, next;
171
172       now = time(NULL);
173       next = now - last_heartbeat;
174       if (next >= me->heartbeat_interval) {
175          bnet_sig(dir, BNET_HEARTBEAT);
176          last_heartbeat = now;
177       }
178       bmicrosleep(next, 0);
179    }
180    bnet_close(dir);
181    jcr->hb_bsock = NULL;
182    return NULL;
183 }
184
185 /*
186  * Same as above but we don't listen to the SD
187  */
188 void start_dir_heartbeat(JCR *jcr)
189 {
190    if (me->heartbeat_interval) {
191       pthread_create(&jcr->heartbeat_id, NULL, dir_heartbeat_thread, (void *)jcr);
192    }
193 }
194
195 void stop_dir_heartbeat(JCR *jcr)
196 {
197    if (me->heartbeat_interval) {
198       stop_heartbeat_monitor(jcr);
199    }
200 }