]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/watchdog.c
Update ReleaseNotes
[bacula/bacula] / bacula / src / lib / watchdog.c
1 /*
2  * Bacula thread watchdog routine. General routine that 
3  *  allows setting a watchdog timer with a callback that is
4  *  called when the timer goes off.
5  *
6  *  Kern Sibbald, January MMII
7  *
8  */
9 /*
10    Bacula® - The Network Backup Solution
11
12    Copyright (C) 2002-2006 Free Software Foundation Europe e.V.
13
14    The main author of Bacula is Kern Sibbald, with contributions from
15    many others, a complete list can be found in the file AUTHORS.
16    This program is Free Software; you can redistribute it and/or
17    modify it under the terms of version two of the GNU General Public
18    License as published by the Free Software Foundation plus additions
19    that are listed in the file LICENSE.
20
21    This program is distributed in the hope that it will be useful, but
22    WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24    General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program; if not, write to the Free Software
28    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29    02110-1301, USA.
30
31    Bacula® is a registered trademark of John Walker.
32    The licensor of Bacula is the Free Software Foundation Europe
33    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
34    Switzerland, email:ftf@fsfeurope.org.
35 */
36
37 #include "bacula.h"
38 #include "jcr.h"
39
40 /* Exported globals */
41 time_t watchdog_time = 0;             /* this has granularity of SLEEP_TIME */
42 time_t watchdog_sleep_time = 60;      /* examine things every 60 seconds */
43 void *start_sbrk;   
44
45 /* Locals */
46 static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
47 static pthread_cond_t timer = PTHREAD_COND_INITIALIZER;
48
49 /* Forward referenced functions */
50 extern "C" void *watchdog_thread(void *arg);
51
52 static void wd_lock();
53 static void wd_unlock();
54
55 /* Static globals */
56 static bool quit = false;;
57 static bool wd_is_init = false;
58 static brwlock_t lock;                /* watchdog lock */
59
60 static pthread_t wd_tid;
61 static dlist *wd_queue;
62 static dlist *wd_inactive;
63
64 /*
65  * Start watchdog thread
66  *
67  *  Returns: 0 on success
68  *           errno on failure
69  */
70 int start_watchdog(void)
71 {
72    int stat;
73    watchdog_t *dummy = NULL;
74    int errstat;
75
76    if (wd_is_init) {
77       return 0;
78    }
79    start_sbrk = sbrk(0);
80    Dmsg0(800, "Initialising NicB-hacked watchdog thread\n");
81    watchdog_time = time(NULL);
82
83    if ((errstat=rwl_init(&lock)) != 0) {
84       Emsg1(M_ABORT, 0, _("Unable to initialize watchdog lock. ERR=%s\n"),
85             strerror(errstat));
86    }
87    wd_queue = New(dlist(dummy, &dummy->link));
88    wd_inactive = New(dlist(dummy, &dummy->link));
89    wd_is_init = true;
90
91    if ((stat = pthread_create(&wd_tid, NULL, watchdog_thread, NULL)) != 0) {
92       return stat;
93    }
94    return 0;
95 }
96
97 /*
98  * Wake watchdog timer thread so that it walks the
99  *  queue and adjusts its wait time (or exits).
100  */
101 static void ping_watchdog()
102 {
103    P(timer_mutex);
104    pthread_cond_signal(&timer);
105    V(timer_mutex);
106 }
107
108 /*
109  * Terminate the watchdog thread
110  *
111  * Returns: 0 on success
112  *          errno on failure
113  */
114 int stop_watchdog(void)
115 {
116    int stat;
117    watchdog_t *p;
118
119    if (!wd_is_init) {
120       return 0;
121    }
122
123    quit = true;                       /* notify watchdog thread to stop */
124    wd_is_init = false;
125
126    ping_watchdog();
127    stat = pthread_join(wd_tid, NULL);
128
129    while (!wd_queue->empty()) {
130       void *item = wd_queue->first();
131       wd_queue->remove(item);
132       p = (watchdog_t *)item;
133       if (p->destructor != NULL) {
134          p->destructor(p);
135       }
136       free(p);
137    }
138    delete wd_queue;
139    wd_queue = NULL;
140
141    while (!wd_inactive->empty()) {
142       void *item = wd_inactive->first();
143       wd_inactive->remove(item);
144       p = (watchdog_t *)item;
145       if (p->destructor != NULL) {
146          p->destructor(p);
147       }
148       free(p);
149    }
150    delete wd_inactive;
151    wd_inactive = NULL;
152    rwl_destroy(&lock);
153
154    return stat;
155 }
156
157 watchdog_t *new_watchdog(void)
158 {
159    watchdog_t *wd = (watchdog_t *)malloc(sizeof(watchdog_t));
160
161    if (!wd_is_init) {
162       start_watchdog();
163    }
164
165    if (wd == NULL) {
166       return NULL;
167    }
168    wd->one_shot = true;
169    wd->interval = 0;
170    wd->callback = NULL;
171    wd->destructor = NULL;
172    wd->data = NULL;
173
174    return wd;
175 }
176
177 bool register_watchdog(watchdog_t *wd)
178 {
179    if (!wd_is_init) {
180       Emsg0(M_ABORT, 0, _("BUG! register_watchdog called before start_watchdog\n"));
181    }
182    if (wd->callback == NULL) {
183       Emsg1(M_ABORT, 0, _("BUG! Watchdog %p has NULL callback\n"), wd);
184    }
185    if (wd->interval == 0) {
186       Emsg1(M_ABORT, 0, _("BUG! Watchdog %p has zero interval\n"), wd);
187    }
188
189    wd_lock();
190    wd->next_fire = watchdog_time + wd->interval;
191    wd_queue->append(wd);
192    Dmsg3(800, "Registered watchdog %p, interval %d%s\n",
193          wd, wd->interval, wd->one_shot ? " one shot" : "");
194    wd_unlock();
195    ping_watchdog();
196
197    return false;
198 }
199
200 bool unregister_watchdog(watchdog_t *wd)
201 {
202    watchdog_t *p;
203    bool ok = false;
204
205    if (!wd_is_init) {
206       Emsg0(M_ABORT, 0, _("BUG! unregister_watchdog_unlocked called before start_watchdog\n"));
207    }
208
209    wd_lock();
210    foreach_dlist(p, wd_queue) {
211       if (wd == p) {
212          wd_queue->remove(wd);
213          Dmsg1(800, "Unregistered watchdog %p\n", wd);
214          ok = true;
215          goto get_out;
216       }
217    }
218
219    foreach_dlist(p, wd_inactive) {
220       if (wd == p) {
221          wd_inactive->remove(wd);
222          Dmsg1(800, "Unregistered inactive watchdog %p\n", wd);
223          ok = true;
224          goto get_out;
225       }
226    }
227
228    Dmsg1(800, "Failed to unregister watchdog %p\n", wd);
229
230 get_out:
231    wd_unlock();
232    ping_watchdog();
233    return ok;
234 }
235
236 /*
237  * This is the thread that walks the watchdog queue
238  *  and when a queue item fires, the callback is
239  *  invoked.  If it is a one shot, the queue item
240  *  is moved to the inactive queue.
241  */
242 extern "C" void *watchdog_thread(void *arg)
243 {
244    struct timespec timeout;
245    struct timeval tv;
246    struct timezone tz;
247    time_t next_time;
248
249    Dmsg0(800, "NicB-reworked watchdog thread entered\n");
250
251    while (!quit) {
252       watchdog_t *p;
253
254       /*
255        *
256        *  NOTE. lock_jcr_chain removed, but the message below
257        *   was left until we are sure there are no deadlocks.
258        *  
259        * We lock the jcr chain here because a good number of the
260        *   callback routines lock the jcr chain. We need to lock
261        *   it here *before* the watchdog lock because the SD message
262        *   thread first locks the jcr chain, then when closing the
263        *   job locks the watchdog chain. If the two threads do not
264        *   lock in the same order, we get a deadlock -- each holds
265        *   the other's needed lock.
266        */
267       wd_lock();
268
269 walk_list:
270       watchdog_time = time(NULL);
271       next_time = watchdog_time + watchdog_sleep_time;
272       foreach_dlist(p, wd_queue) {
273          if (p->next_fire <= watchdog_time) {
274             /* Run the callback */
275             Dmsg2(3400, "Watchdog callback p=0x%p fire=%d\n", p, p->next_fire);
276             p->callback(p);
277
278             /* Reschedule (or move to inactive list if it's a one-shot timer) */
279             if (p->one_shot) {
280                wd_queue->remove(p);
281                wd_inactive->append(p);
282                goto walk_list;
283             } else {
284                p->next_fire = watchdog_time + p->interval;
285             }
286          }
287          if (p->next_fire <= next_time) {
288             next_time = p->next_fire;
289          }
290       }
291       wd_unlock();
292
293       /*
294        * Wait sleep time or until someone wakes us
295        */
296       gettimeofday(&tv, &tz);
297       timeout.tv_nsec = tv.tv_usec * 1000;
298       timeout.tv_sec = tv.tv_sec + next_time - time(NULL);
299       while (timeout.tv_nsec >= 1000000000) {
300          timeout.tv_nsec -= 1000000000;
301          timeout.tv_sec++;
302       }
303
304       Dmsg1(1900, "pthread_cond_timedwait %d\n", timeout.tv_sec - tv.tv_sec);
305       /* Note, this unlocks mutex during the sleep */
306       P(timer_mutex);
307       pthread_cond_timedwait(&timer, &timer_mutex, &timeout);
308       V(timer_mutex);
309    }
310
311    Dmsg0(800, "NicB-reworked watchdog thread exited\n");
312    return NULL;
313 }
314
315 /*
316  * Watchdog lock, this can be called multiple times by the same
317  *   thread without blocking, but must be unlocked the number of
318  *   times it was locked.
319  */
320 static void wd_lock()
321 {
322    int errstat;
323    if ((errstat=rwl_writelock(&lock)) != 0) {
324       Emsg1(M_ABORT, 0, _("rwl_writelock failure. ERR=%s\n"),
325            strerror(errstat));
326    }
327 }
328
329 /*
330  * Unlock the watchdog. This can be called multiple times by the
331  *   same thread up to the number of times that thread called
332  *   wd_ lock()/
333  */
334 static void wd_unlock()
335 {
336    int errstat;
337    if ((errstat=rwl_writeunlock(&lock)) != 0) {
338       Emsg1(M_ABORT, 0, _("rwl_writeunlock failure. ERR=%s\n"),
339            strerror(errstat));
340    }
341 }