2 * Bacula thread watchdog routine. General routine that monitors
3 * the daemon and signals a thread if it is blocked on a BSOCK
4 * too long. This prevents catastropic long waits -- generally
5 * due to Windows "hanging" the app.
7 * Kern Sibbald, January MMII
11 Copyright (C) 2000-2004 Kern Sibbald and John Walker
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 /* Exported globals */
34 time_t watchdog_time = 0; /* this has granularity of SLEEP_TIME */
36 #define SLEEP_TIME 1 /* examine things every second */
38 /* Forward referenced functions */
39 static void *watchdog_thread(void *arg);
40 static void wd_lock();
41 static void wd_unlock();
44 static bool quit = false;;
45 static bool wd_is_init = false;
46 static brwlock_t lock; /* watchdog lock */
48 static pthread_t wd_tid;
49 static dlist *wd_queue;
50 static dlist *wd_inactive;
53 * Start watchdog thread
55 * Returns: 0 on success
58 int start_watchdog(void)
61 watchdog_t *dummy = NULL;
67 Dmsg0(200, "Initialising NicB-hacked watchdog thread\n");
68 watchdog_time = time(NULL);
70 if ((errstat=rwl_init(&lock)) != 0) {
71 Emsg1(M_ABORT, 0, _("Unable to initialize watchdog lock. ERR=%s\n"),
74 wd_queue = new dlist(wd_queue, &dummy->link);
75 wd_inactive = new dlist(wd_inactive, &dummy->link);
77 if ((stat = pthread_create(&wd_tid, NULL, watchdog_thread, NULL)) != 0) {
85 * Terminate the watchdog thread
87 * Returns: 0 on success
90 int stop_watchdog(void)
99 quit = true; /* notify watchdog thread to stop */
102 stat = pthread_join(wd_tid, NULL);
104 foreach_dlist(p, wd_queue) {
105 if (p->destructor != NULL) {
113 foreach_dlist(p, wd_inactive) {
114 if (p->destructor != NULL) {
127 watchdog_t *new_watchdog(void)
129 watchdog_t *wd = (watchdog_t *)malloc(sizeof(watchdog_t));
141 wd->destructor = NULL;
147 bool register_watchdog(watchdog_t *wd)
150 Emsg0(M_ABORT, 0, "BUG! register_watchdog called before start_watchdog\n");
152 if (wd->callback == NULL) {
153 Emsg1(M_ABORT, 0, "BUG! Watchdog %p has NULL callback\n", wd);
155 if (wd->interval == 0) {
156 Emsg1(M_ABORT, 0, "BUG! Watchdog %p has zero interval\n", wd);
160 wd->next_fire = watchdog_time + wd->interval;
161 wd_queue->append(wd);
162 Dmsg3(200, "Registered watchdog %p, interval %d%s\n",
163 wd, wd->interval, wd->one_shot ? " one shot" : "");
169 bool unregister_watchdog_unlocked(watchdog_t *wd)
174 Emsg0(M_ABORT, 0, "BUG! unregister_watchdog_unlocked called before start_watchdog\n");
177 foreach_dlist(p, wd_queue) {
179 wd_queue->remove(wd);
180 Dmsg1(200, "Unregistered watchdog %p\n", wd);
185 foreach_dlist(p, wd_inactive) {
187 wd_inactive->remove(wd);
188 Dmsg1(200, "Unregistered inactive watchdog %p\n", wd);
193 Dmsg1(200, "Failed to unregister watchdog %p\n", wd);
197 bool unregister_watchdog(watchdog_t *wd)
202 Emsg0(M_ABORT, 0, "BUG! unregister_watchdog called before start_watchdog\n");
206 ret = unregister_watchdog_unlocked(wd);
212 static void *watchdog_thread(void *arg)
214 Dmsg0(200, "NicB-reworked watchdog thread entered\n");
220 * We lock the jcr chain here because a good number of the
221 * callback routines lock the jcr chain. We need to lock
222 * it here *before* the watchdog lock because the SD message
223 * thread first locks the jcr chain, then when closing the
224 * job locks the watchdog chain. If the two thread do not
225 * lock in the same order, we get a deadlock -- each holds
226 * the other's needed lock.
230 watchdog_time = time(NULL);
232 foreach_dlist(p, wd_queue) {
233 if (p->next_fire < watchdog_time) {
234 /* Run the callback */
237 /* Reschedule (or move to inactive list if it's a one-shot timer) */
240 wd_inactive->append(p);
242 p->next_fire = watchdog_time + p->interval;
248 bmicrosleep(SLEEP_TIME, 0);
251 Dmsg0(200, "NicB-reworked watchdog thread exited\n");
256 * Watchdog lock, this can be called multiple times by the same
257 * thread without blocking, but must be unlocked the number of
258 * times it was locked.
260 static void wd_lock()
263 if ((errstat=rwl_writelock(&lock)) != 0) {
264 Emsg1(M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
270 * Unlock the watchdog. This can be called multiple times by the
271 * same thread up to the number of times that thread called
274 static void wd_unlock()
277 if ((errstat=rwl_writeunlock(&lock)) != 0) {
278 Emsg1(M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",