2 Bacula® - The Network Backup Solution
4 Copyright (C) 2004-2007 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of John Walker.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Process and thread timer routines, built on top of watchdogs.
31 * Nic Bellamy <nic@bellamy.co.nz>, October 2004.
38 /* Forward referenced functions */
39 static void stop_btimer(btimer_t *wid);
40 static btimer_t *btimer_start_common(uint32_t wait);
42 /* Forward referenced callback functions */
43 static void callback_child_timer(watchdog_t *self);
44 static void callback_thread_timer(watchdog_t *self);
46 static void destructor_thread_timer(watchdog_t *self);
47 static void destructor_child_timer(watchdog_t *self);
51 * Start a timer on a child process of pid, kill it after wait seconds.
53 * Returns: btimer_t *(pointer to btimer_t struct) on success
56 btimer_t *start_child_timer(JCR *jcr, pid_t pid, uint32_t wait)
60 wid = btimer_start_common(wait);
64 wid->type = TYPE_CHILD;
69 wid->wd->callback = callback_child_timer;
70 wid->wd->one_shot = false;
71 wid->wd->interval = wait;
72 register_watchdog(wid->wd);
74 Dmsg3(900, "Start child timer %p, pid %d for %d secs.\n", wid, pid, wait);
81 void stop_child_timer(btimer_t *wid)
84 Dmsg0(900, "stop_child_timer called with NULL btimer_id\n");
87 Dmsg2(900, "Stop child timer %p pid %d\n", wid, wid->pid);
92 static void destructor_child_timer(watchdog_t *self)
94 btimer_t *wid = (btimer_t *)self->data;
100 static void callback_child_timer(watchdog_t *self)
102 btimer_t *wid = (btimer_t *)self->data;
105 /* First kill attempt; try killing it softly (kill -SONG) first */
108 Dmsg2(050, "watchdog %p term PID %d\n", self, wid->pid);
110 /* Kill -TERM the specified PID, and reschedule a -KILL for 5 seconds
111 * later. (Warning: this should let dvd-writepart enough time to term
112 * and kill growisofs, which takes 3 seconds, so the interval must not
113 * be less than 5 seconds)
115 kill(wid->pid, SIGTERM);
118 /* This is the second call - terminate with prejudice. */
119 Dmsg2(050, "watchdog %p kill PID %d\n", self, wid->pid);
121 kill(wid->pid, SIGKILL);
123 /* Setting one_shot to true before we leave ensures we don't get
126 self->one_shot = true;
128 Jmsg(wid->jcr, M_INFO, 0, _("Child timer expired. Child process killed.\n"));
132 * Start a timer on a thread. kill it after wait seconds.
134 * Returns: btimer_t *(pointer to btimer_t struct) on success
137 btimer_t *start_thread_timer(JCR *jcr, pthread_t tid, uint32_t wait)
140 wid = btimer_start_common(wait);
142 Dmsg1(900, "start_thread_timer return NULL from common. wait=%d.\n", wait);
145 wid->type = TYPE_PTHREAD;
149 wid->wd->callback = callback_thread_timer;
150 wid->wd->one_shot = true;
151 wid->wd->interval = wait;
152 register_watchdog(wid->wd);
154 Dmsg3(900, "Start thread timer %p tid %p for %d secs.\n", wid, tid, wait);
160 * Start a timer on a BSOCK. kill it after wait seconds.
162 * Returns: btimer_t *(pointer to btimer_t struct) on success
165 btimer_t *start_bsock_timer(BSOCK *bsock, uint32_t wait)
168 wid = btimer_start_common(wait);
172 wid->type = TYPE_BSOCK;
173 wid->tid = pthread_self();
175 wid->jcr = bsock->jcr();
177 wid->wd->callback = callback_thread_timer;
178 wid->wd->one_shot = true;
179 wid->wd->interval = wait;
180 register_watchdog(wid->wd);
182 Dmsg4(950, "Start bsock timer %p tid=%p for %d secs at %d\n", wid,
183 wid->tid, wait, time(NULL));
191 void stop_bsock_timer(btimer_t *wid)
194 Dmsg0(900, "stop_bsock_timer called with NULL btimer_id\n");
197 Dmsg3(950, "Stop bsock timer %p tid=%p at %d.\n", wid, wid->tid, time(NULL));
205 void stop_thread_timer(btimer_t *wid)
208 Dmsg0(900, "stop_thread_timer called with NULL btimer_id\n");
211 Dmsg2(900, "Stop thread timer %p tid=%p.\n", wid, wid->tid);
216 static void destructor_thread_timer(watchdog_t *self)
218 btimer_t *wid = (btimer_t *)self->data;
224 static void callback_thread_timer(watchdog_t *self)
226 btimer_t *wid = (btimer_t *)self->data;
228 Dmsg4(50, "thread timer %p kill %s tid=%p at %d.\n", self,
229 wid->type == TYPE_BSOCK ? "bsock" : "thread", wid->tid, time(NULL));
231 if (wid->type == TYPE_BSOCK && wid->bsock) {
232 wid->bsock->set_timed_out();
234 Jmsg(wid->jcr, M_INFO, 0, _("Thread timer expired. Thread for JobId=%u interrupted.\n"),
235 get_jobid_from_tid(wid->tid));
236 pthread_kill(wid->tid, TIMEOUT_SIGNAL);
239 static btimer_t *btimer_start_common(uint32_t wait)
241 btimer_t *wid = (btimer_t *)malloc(sizeof(btimer_t));
243 wid->wd = new_watchdog();
244 if (wid->wd == NULL) {
257 static void stop_btimer(btimer_t *wid)
260 Emsg0(M_ABORT, 0, _("stop_btimer called with NULL btimer_id\n"));
262 unregister_watchdog(wid->wd);