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 const int dbglvl = 900;
40 /* Forward referenced functions */
41 static void stop_btimer(btimer_t *wid);
42 static btimer_t *btimer_start_common(uint32_t wait);
44 /* Forward referenced callback functions */
45 static void callback_child_timer(watchdog_t *self);
46 static void callback_thread_timer(watchdog_t *self);
48 static void destructor_thread_timer(watchdog_t *self);
49 static void destructor_child_timer(watchdog_t *self);
53 * Start a timer on a child process of pid, kill it after wait seconds.
55 * Returns: btimer_t *(pointer to btimer_t struct) on success
58 btimer_t *start_child_timer(JCR *jcr, pid_t pid, uint32_t wait)
62 wid = btimer_start_common(wait);
66 wid->type = TYPE_CHILD;
71 wid->wd->callback = callback_child_timer;
72 wid->wd->one_shot = false;
73 wid->wd->interval = wait;
74 register_watchdog(wid->wd);
76 Dmsg3(dbglvl, "Start child timer %p, pid %d for %d secs.\n", wid, pid, wait);
83 void stop_child_timer(btimer_t *wid)
86 Dmsg0(dbglvl, "stop_child_timer called with NULL btimer_id\n");
89 Dmsg2(dbglvl, "Stop child timer %p pid %d\n", wid, wid->pid);
94 static void destructor_child_timer(watchdog_t *self)
96 btimer_t *wid = (btimer_t *)self->data;
102 static void callback_child_timer(watchdog_t *self)
104 btimer_t *wid = (btimer_t *)self->data;
107 /* First kill attempt; try killing it softly (kill -SONG) first */
110 Dmsg2(dbglvl, "watchdog %p term PID %d\n", self, wid->pid);
112 /* Kill -TERM the specified PID, and reschedule a -KILL for 5 seconds
113 * later. (Warning: this should let dvd-writepart enough time to term
114 * and kill growisofs, which takes 3 seconds, so the interval must not
115 * be less than 5 seconds)
117 kill(wid->pid, SIGTERM);
120 /* This is the second call - terminate with prejudice. */
121 Dmsg2(dbglvl, "watchdog %p kill PID %d\n", self, wid->pid);
123 kill(wid->pid, SIGKILL);
125 /* Setting one_shot to true before we leave ensures we don't get
128 self->one_shot = true;
133 * Start a timer on a thread. kill it after wait seconds.
135 * Returns: btimer_t *(pointer to btimer_t struct) on success
138 btimer_t *start_thread_timer(JCR *jcr, pthread_t tid, uint32_t wait)
141 wid = btimer_start_common(wait);
143 Dmsg1(dbglvl, "start_thread_timer return NULL from common. wait=%d.\n", wait);
146 wid->type = TYPE_PTHREAD;
150 wid->wd->callback = callback_thread_timer;
151 wid->wd->one_shot = true;
152 wid->wd->interval = wait;
153 register_watchdog(wid->wd);
155 Dmsg3(dbglvl, "Start thread timer %p tid %p for %d secs.\n", wid, tid, wait);
161 * Start a timer on a BSOCK. kill it after wait seconds.
163 * Returns: btimer_t *(pointer to btimer_t struct) on success
166 btimer_t *start_bsock_timer(BSOCK *bsock, uint32_t wait)
169 wid = btimer_start_common(wait);
173 wid->type = TYPE_BSOCK;
174 wid->tid = pthread_self();
176 wid->jcr = bsock->jcr();
178 wid->wd->callback = callback_thread_timer;
179 wid->wd->one_shot = true;
180 wid->wd->interval = wait;
181 register_watchdog(wid->wd);
183 Dmsg4(dbglvl, "Start bsock timer %p tid=%p for %d secs at %d\n", wid,
184 wid->tid, wait, time(NULL));
192 void stop_bsock_timer(btimer_t *wid)
195 Dmsg0(900, "stop_bsock_timer called with NULL btimer_id\n");
198 Dmsg3(dbglvl, "Stop bsock timer %p tid=%p at %d.\n", wid, wid->tid, time(NULL));
206 void stop_thread_timer(btimer_t *wid)
209 Dmsg0(dbglvl, "stop_thread_timer called with NULL btimer_id\n");
212 Dmsg2(dbglvl, "Stop thread timer %p tid=%p.\n", wid, wid->tid);
217 static void destructor_thread_timer(watchdog_t *self)
219 btimer_t *wid = (btimer_t *)self->data;
225 static void callback_thread_timer(watchdog_t *self)
227 btimer_t *wid = (btimer_t *)self->data;
229 Dmsg4(dbglvl, "thread timer %p kill %s tid=%p at %d.\n", self,
230 wid->type == TYPE_BSOCK ? "bsock" : "thread", wid->tid, time(NULL));
232 Dmsg2(dbglvl, "killed jid=%u Job=%s\n", wid->jcr->JobId, wid->jcr->Job);
235 if (wid->type == TYPE_BSOCK && wid->bsock) {
236 wid->bsock->set_timed_out();
238 pthread_kill(wid->tid, TIMEOUT_SIGNAL);
241 static btimer_t *btimer_start_common(uint32_t wait)
243 btimer_t *wid = (btimer_t *)malloc(sizeof(btimer_t));
245 wid->wd = new_watchdog();
246 if (wid->wd == NULL) {
259 static void stop_btimer(btimer_t *wid)
262 Emsg0(M_ABORT, 0, _("stop_btimer called with NULL btimer_id\n"));
264 unregister_watchdog(wid->wd);