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);
42 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
43 static pthread_cond_t timer = PTHREAD_COND_INITIALIZER;
45 static bool wd_is_init = false;
47 /* Forward referenced callback functions */
48 static pthread_t wd_tid;
51 static dlist *wd_queue;
52 static dlist *wd_inactive;
55 * Start watchdog thread
57 * Returns: 0 on success
60 int start_watchdog(void)
63 watchdog_t *dummy = NULL;
68 Dmsg0(200, "Initialising NicB-hacked watchdog thread\n");
69 watchdog_time = time(NULL);
72 wd_queue = new dlist(wd_queue, &dummy->link);
73 wd_inactive = new dlist(wd_inactive, &dummy->link);
75 if ((stat = pthread_create(&wd_tid, NULL, watchdog_thread, NULL)) != 0) {
83 * Terminate the watchdog thread
85 * Returns: 0 on success
88 int stop_watchdog(void)
97 Dmsg0(200, "Sending stop signal to NicB-hacked watchdog thread\n");
100 stat = pthread_cond_signal(&timer);
105 stat = pthread_join(wd_tid, NULL);
107 foreach_dlist(p, wd_queue) {
108 if (p->destructor != NULL) {
116 foreach_dlist(p, wd_inactive) {
117 if (p->destructor != NULL) {
128 watchdog_t *new_watchdog(void)
130 watchdog_t *wd = (watchdog_t *)malloc(sizeof(watchdog_t));
135 Emsg0(M_ABORT, 0, "BUG! new_watchdog called before start_watchdog\n");
145 wd->destructor = NULL;
151 bool register_watchdog(watchdog_t *wd)
154 Emsg0(M_ABORT, 0, "BUG! register_watchdog called before start_watchdog\n");
156 if (wd->callback == NULL) {
157 Emsg1(M_ABORT, 0, "BUG! Watchdog %p has NULL callback\n", wd);
159 if (wd->interval == 0) {
160 Emsg1(M_ABORT, 0, "BUG! Watchdog %p has zero interval\n", wd);
164 wd->next_fire = watchdog_time + wd->interval;
165 wd_queue->append(wd);
166 Dmsg3(200, "Registered watchdog %p, interval %d%s\n",
167 wd, wd->interval, wd->one_shot ? " one shot" : "");
173 bool unregister_watchdog_unlocked(watchdog_t *wd)
178 Emsg0(M_ABORT, 0, "BUG! unregister_watchdog_unlocked called before start_watchdog\n");
181 foreach_dlist(p, wd_queue) {
183 wd_queue->remove(wd);
184 Dmsg1(200, "Unregistered watchdog %p\n", wd);
189 foreach_dlist(p, wd_inactive) {
191 wd_inactive->remove(wd);
192 Dmsg1(200, "Unregistered inactive watchdog %p\n", wd);
197 Dmsg1(200, "Failed to unregister watchdog %p\n", wd);
201 bool unregister_watchdog(watchdog_t *wd)
206 Emsg0(M_ABORT, 0, "BUG! unregister_watchdog called before start_watchdog\n");
210 ret = unregister_watchdog_unlocked(wd);
216 static void *watchdog_thread(void *arg)
218 Dmsg0(200, "NicB-reworked watchdog thread entered\n");
229 watchdog_time = time(NULL);
231 foreach_dlist(p, wd_queue) {
232 if (p->next_fire < watchdog_time) {
233 /* Run the callback */
236 /* Reschedule (or move to inactive list if it's a one-shot timer) */
239 wd_inactive->append(p);
241 p->next_fire = watchdog_time + p->interval;
246 bmicrosleep(SLEEP_TIME, 0);
249 Dmsg0(200, "NicB-reworked watchdog thread exited\n");