2 * Process and thread timer routines, built on top of watchdogs.
4 * Nic Bellamy <nic@bellamy.co.nz>, October 2004.
8 Bacula® - The Network Backup Solution
10 Copyright (C) 2004-2006 Free Software Foundation Europe e.V.
12 The main author of Bacula is Kern Sibbald, with contributions from
13 many others, a complete list can be found in the file AUTHORS.
14 This program is Free Software; you can redistribute it and/or
15 modify it under the terms of version two of the GNU General Public
16 License as published by the Free Software Foundation plus additions
17 that are listed in the file LICENSE.
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 Bacula® is a registered trademark of John Walker.
30 The licensor of Bacula is the Free Software Foundation Europe
31 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
32 Switzerland, email:ftf@fsfeurope.org.
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(pid_t pid, uint32_t wait)
60 wid = btimer_start_common(wait);
64 wid->type = TYPE_CHILD;
68 wid->wd->callback = callback_child_timer;
69 wid->wd->one_shot = false;
70 wid->wd->interval = wait;
71 register_watchdog(wid->wd);
73 Dmsg3(900, "Start child timer %p, pid %d for %d secs.\n", wid, pid, wait);
80 void stop_child_timer(btimer_t *wid)
83 Dmsg0(900, "stop_child_timer called with NULL btimer_id\n");
86 Dmsg2(900, "Stop child timer %p pid %d\n", wid, wid->pid);
91 static void destructor_child_timer(watchdog_t *self)
93 btimer_t *wid = (btimer_t *)self->data;
99 static void callback_child_timer(watchdog_t *self)
101 btimer_t *wid = (btimer_t *)self->data;
104 /* First kill attempt; try killing it softly (kill -SONG) first */
107 Dmsg2(050, "watchdog %p term PID %d\n", self, wid->pid);
109 /* Kill -TERM the specified PID, and reschedule a -KILL for 5 seconds
110 * later. (Warning: this should let dvd-writepart enough time to term
111 * and kill growisofs, which takes 3 seconds, so the interval must not
112 * be less than 5 seconds)
114 kill(wid->pid, SIGTERM);
117 /* This is the second call - terminate with prejudice. */
118 Dmsg2(050, "watchdog %p kill PID %d\n", self, wid->pid);
120 kill(wid->pid, SIGKILL);
122 /* Setting one_shot to true before we leave ensures we don't get
125 self->one_shot = true;
130 * Start a timer on a thread. kill it after wait seconds.
132 * Returns: btimer_t *(pointer to btimer_t struct) on success
135 btimer_t *start_thread_timer(pthread_t tid, uint32_t wait)
138 wid = btimer_start_common(wait);
140 Dmsg1(900, "start_thread_timer return NULL from common. wait=%d.\n", wait);
143 wid->type = TYPE_PTHREAD;
146 wid->wd->callback = callback_thread_timer;
147 wid->wd->one_shot = true;
148 wid->wd->interval = wait;
149 register_watchdog(wid->wd);
151 Dmsg3(900, "Start thread timer %p tid %p for %d secs.\n", wid, tid, wait);
157 * Start a timer on a BSOCK. kill it after wait seconds.
159 * Returns: btimer_t *(pointer to btimer_t struct) on success
162 btimer_t *start_bsock_timer(BSOCK *bsock, uint32_t wait)
165 wid = btimer_start_common(wait);
169 wid->type = TYPE_BSOCK;
170 wid->tid = pthread_self();
173 wid->wd->callback = callback_thread_timer;
174 wid->wd->one_shot = true;
175 wid->wd->interval = wait;
176 register_watchdog(wid->wd);
178 Dmsg4(950, "Start bsock timer %p tid=%p for %d secs at %d\n", wid,
179 wid->tid, wait, time(NULL));
187 void stop_bsock_timer(btimer_t *wid)
190 Dmsg0(900, "stop_bsock_timer called with NULL btimer_id\n");
193 Dmsg3(950, "Stop bsock timer %p tid=%p at %d.\n", wid, wid->tid, time(NULL));
201 void stop_thread_timer(btimer_t *wid)
204 Dmsg0(900, "stop_thread_timer called with NULL btimer_id\n");
207 Dmsg2(900, "Stop thread timer %p tid=%p.\n", wid, wid->tid);
212 static void destructor_thread_timer(watchdog_t *self)
214 btimer_t *wid = (btimer_t *)self->data;
220 static void callback_thread_timer(watchdog_t *self)
222 btimer_t *wid = (btimer_t *)self->data;
224 Dmsg4(50, "thread timer %p kill %s tid=%p at %d.\n", self,
225 wid->type == TYPE_BSOCK ? "bsock" : "thread", wid->tid, time(NULL));
227 if (wid->type == TYPE_BSOCK && wid->bsock) {
228 wid->bsock->timed_out = true;
230 pthread_kill(wid->tid, TIMEOUT_SIGNAL);
233 static btimer_t *btimer_start_common(uint32_t wait)
235 btimer_t *wid = (btimer_t *)malloc(sizeof(btimer_t));
237 wid->wd = new_watchdog();
238 if (wid->wd == NULL) {
251 static void stop_btimer(btimer_t *wid)
254 Emsg0(M_ABORT, 0, _("stop_btimer called with NULL btimer_id\n"));
256 unregister_watchdog(wid->wd);