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-2003 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; /* this has granularity of SLEEP_TIME */
36 #define SLEEP_TIME 30 /* examine things every 30 seconds */
38 /* Forward referenced functions */
39 static void *btimer_thread(void *arg);
40 static void stop_btimer(btimer_id wid);
41 static btimer_id btimer_start_common(uint32_t wait);
44 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
45 static pthread_cond_t timer = PTHREAD_COND_INITIALIZER;
47 static btimer_t *timer_chain = NULL;
51 * Timeout signal comes here
53 void timeout_handler(int sig)
55 return; /* thus interrupting the function */
60 * Start watchdog thread
62 * Returns: 0 on success
65 int start_watchdog(void)
69 struct sigaction sigtimer;
71 sigtimer.sa_flags = 0;
72 sigtimer.sa_handler = timeout_handler;
73 sigfillset(&sigtimer.sa_mask);
74 sigaction(TIMEOUT_SIGNAL, &sigtimer, NULL);
75 watchdog_time = time(NULL);
77 if ((stat = pthread_create(&wdid, NULL, btimer_thread, (void *)NULL)) != 0) {
78 pthread_mutex_destroy(&mutex);
79 pthread_cond_destroy(&timer);
86 * Terminate the watchdog thread
88 * Returns: 0 on success
91 int stop_watchdog(void)
97 if ((stat = pthread_cond_signal(&timer)) != 0) {
107 * This is the actual watchdog thread.
109 static void *btimer_thread(void *arg)
115 Dmsg0(200, "Start watchdog thread\n");
116 pthread_detach(pthread_self());
119 time_t timer_start, now;
121 Dmsg0(200, "Top of watchdog loop\n");
123 watchdog_time = time(NULL); /* update timer */
125 /* Walk through all JCRs checking if any one is
126 * blocked for more than specified max time.
129 for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
130 free_locked_jcr(jcr);
131 if (jcr->JobId == 0) {
134 fd = jcr->store_bsock;
136 timer_start = fd->timer_start;
137 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
138 fd->timed_out = TRUE;
139 Jmsg(jcr, M_ERROR, 0, _(
140 "Watchdog sending kill after %d secs to thread stalled reading Storage daemon.\n"),
141 watchdog_time - timer_start);
142 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
145 fd = jcr->file_bsock;
147 timer_start = fd->timer_start;
148 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
149 fd->timed_out = TRUE;
150 Jmsg(jcr, M_ERROR, 0, _(
151 "Watchdog sending kill after %d secs to thread stalled reading File daemon.\n"),
152 watchdog_time - timer_start);
153 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
158 timer_start = fd->timer_start;
159 if (timer_start && (watchdog_time - timer_start) > fd->timeout) {
160 fd->timed_out = TRUE;
161 Jmsg(jcr, M_ERROR, 0, _(
162 "Watchdog sending kill after %d secs to thread stalled reading Director.\n"),
163 watchdog_time - timer_start);
164 pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
171 Dmsg0(200, "Watchdog sleep.\n");
172 bmicrosleep(SLEEP_TIME, 0);
176 * Now handle child and thread timers set by the code.
178 /* Walk child chain killing off any process overdue */
180 for (wid = timer_chain; wid; wid=wid->next) {
182 /* First ask him politely to go away */
183 if (!wid->killed && now > (wid->start_time + wid->wait)) {
184 // Dmsg1(000, "Watchdog sigterm pid=%d\n", wid->pid);
185 if (wid->type == TYPE_CHILD) {
186 kill(wid->pid, SIGTERM);
189 Dmsg1(200, "watchdog kill thread %d\n", wid->tid);
190 pthread_kill(wid->tid, TIMEOUT_SIGNAL);
194 /* If we asked a child to die, wait 3 seconds and slam him */
198 for (wid1 = timer_chain; wid1; wid1=wid1->next) {
199 if (wid->type == TYPE_CHILD &&
200 !wid1->killed && now > (wid1->start_time + wid1->wait)) {
201 kill(wid1->pid, SIGKILL);
202 // Dmsg1(000, "Watchdog killed pid=%d\n", wid->pid);
209 } /* end of big for loop */
211 Dmsg0(200, "End watchdog\n");
216 * Start a timer on a child process of pid, kill it after wait seconds.
217 * NOTE! Granularity is SLEEP_TIME (i.e. 30 seconds)
219 * Returns: btimer_id (pointer to btimer_t struct) on success
222 btimer_id start_child_timer(pid_t pid, uint32_t wait)
225 wid = btimer_start_common(wait);
227 wid->type = TYPE_CHILD;
228 Dmsg2(200, "Start child timer 0x%x for %d secs.\n", wid, wait);
233 * Start a timer on a thread. kill it after wait seconds.
234 * NOTE! Granularity is SLEEP_TIME (i.e. 30 seconds)
236 * Returns: btimer_id (pointer to btimer_t struct) on success
239 btimer_id start_thread_timer(pthread_t tid, uint32_t wait)
242 wid = btimer_start_common(wait);
244 wid->type = TYPE_PTHREAD;
245 Dmsg2(200, "Start thread timer 0x%x for %d secs.\n", wid, wait);
249 static btimer_id btimer_start_common(uint32_t wait)
251 btimer_id wid = (btimer_id)malloc(sizeof(btimer_t));
254 /* Chain it into timer_chain as the first item */
256 wid->next = timer_chain;
258 timer_chain->prev = wid;
261 wid->start_time = time(NULL);
271 void stop_child_timer(btimer_id wid)
273 Dmsg2(200, "Stop child timer 0x%x for %d secs.\n", wid, wid->wait);
280 void stop_thread_timer(btimer_id wid)
285 Dmsg2(200, "Stop thread timer 0x%x for %d secs.\n", wid, wid->wait);
293 static void stop_btimer(btimer_id wid)
296 Emsg0(M_ABORT, 0, _("NULL btimer_id.\n"));
299 /* Remove wid from timer_chain */
300 if (!wid->prev) { /* if no prev */
301 timer_chain = wid->next; /* set new head */
303 wid->prev->next = wid->next; /* update prev */
306 wid->next->prev = wid->prev; /* unlink it */