2 Bacula® - The Network Backup Solution
4 Copyright (C) 2004-2011 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 three of the GNU Affero 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 Affero 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 Kern Sibbald.
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 if (wait <= 0) { /* wait should be > 0 */
172 wid = btimer_start_common(wait);
176 wid->type = TYPE_BSOCK;
177 wid->tid = pthread_self();
179 wid->jcr = bsock->jcr();
181 wid->wd->callback = callback_thread_timer;
182 wid->wd->one_shot = true;
183 wid->wd->interval = wait;
184 register_watchdog(wid->wd);
186 Dmsg4(dbglvl, "Start bsock timer %p tid=%p for %d secs at %d\n", wid,
187 wid->tid, wait, time(NULL));
195 void stop_bsock_timer(btimer_t *wid)
198 Dmsg0(900, "stop_bsock_timer called with NULL btimer_id\n");
201 Dmsg3(dbglvl, "Stop bsock timer %p tid=%p at %d.\n", wid, wid->tid, time(NULL));
209 void stop_thread_timer(btimer_t *wid)
212 Dmsg0(dbglvl, "stop_thread_timer called with NULL btimer_id\n");
215 Dmsg2(dbglvl, "Stop thread timer %p tid=%p.\n", wid, wid->tid);
220 static void destructor_thread_timer(watchdog_t *self)
222 btimer_t *wid = (btimer_t *)self->data;
228 static void callback_thread_timer(watchdog_t *self)
230 btimer_t *wid = (btimer_t *)self->data;
232 Dmsg4(dbglvl, "thread timer %p kill %s tid=%p at %d.\n", self,
233 wid->type == TYPE_BSOCK ? "bsock" : "thread", wid->tid, time(NULL));
235 Dmsg2(dbglvl, "killed jid=%u Job=%s\n", wid->jcr->JobId, wid->jcr->Job);
238 if (wid->type == TYPE_BSOCK && wid->bsock) {
239 wid->bsock->set_timed_out();
241 pthread_kill(wid->tid, TIMEOUT_SIGNAL);
244 static btimer_t *btimer_start_common(uint32_t wait)
246 btimer_t *wid = (btimer_t *)malloc(sizeof(btimer_t));
248 wid->wd = new_watchdog();
249 if (wid->wd == NULL) {
262 static void stop_btimer(btimer_t *wid)
265 Emsg0(M_ABORT, 0, _("stop_btimer called with NULL btimer_id\n"));
267 unregister_watchdog(wid->wd);