2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
5 Copyright (C) 2004-2014 Free Software Foundation Europe e.V.
7 The original author of Bacula is Kern Sibbald, with contributions
8 from many others, a complete list can be found in the file AUTHORS.
10 You may use this file and others of this release according to the
11 license defined in the LICENSE file, which includes the Affero General
12 Public License, v3.0 ("AGPLv3") and some additional permissions and
13 terms pursuant to its AGPLv3 Section 7.
15 This notice must be preserved when any source code is
16 conveyed and/or propagated.
18 Bacula(R) is a registered trademark of Kern Sibbald.
21 * Process and thread timer routines, built on top of watchdogs.
23 * Nic Bellamy <nic@bellamy.co.nz>, October 2004.
30 const int dbglvl = 900;
32 /* Forward referenced functions */
33 static void stop_btimer(btimer_t *wid);
34 static btimer_t *btimer_start_common(uint32_t wait);
36 /* Forward referenced callback functions */
37 static void callback_child_timer(watchdog_t *self);
38 static void callback_thread_timer(watchdog_t *self);
40 static void destructor_thread_timer(watchdog_t *self);
41 static void destructor_child_timer(watchdog_t *self);
45 * Start a timer on a child process of pid, kill it after wait seconds.
47 * Returns: btimer_t *(pointer to btimer_t struct) on success
50 btimer_t *start_child_timer(JCR *jcr, pid_t pid, uint32_t wait)
54 wid = btimer_start_common(wait);
58 wid->type = TYPE_CHILD;
63 wid->wd->callback = callback_child_timer;
64 wid->wd->one_shot = false;
65 wid->wd->interval = wait;
66 register_watchdog(wid->wd);
68 Dmsg3(dbglvl, "Start child timer %p, pid %d for %d secs.\n", wid, pid, wait);
75 void stop_child_timer(btimer_t *wid)
78 Dmsg0(dbglvl, "stop_child_timer called with NULL btimer_id\n");
81 Dmsg2(dbglvl, "Stop child timer %p pid %d\n", wid, wid->pid);
86 static void destructor_child_timer(watchdog_t *self)
88 btimer_t *wid = (btimer_t *)self->data;
94 static void callback_child_timer(watchdog_t *self)
96 btimer_t *wid = (btimer_t *)self->data;
99 /* First kill attempt; try killing it softly (kill -SONG) first */
102 Dmsg2(dbglvl, "watchdog %p term PID %d\n", self, wid->pid);
104 /* Kill -TERM the specified PID, and reschedule a -KILL for 5 seconds
105 * later. (Warning: this should let dvd-writepart enough time to term
106 * and kill growisofs, which takes 3 seconds, so the interval must not
107 * be less than 5 seconds)
109 kill(wid->pid, SIGTERM);
112 /* This is the second call - terminate with prejudice. */
113 Dmsg2(dbglvl, "watchdog %p kill PID %d\n", self, wid->pid);
115 kill(wid->pid, SIGKILL);
117 /* Setting one_shot to true before we leave ensures we don't get
120 self->one_shot = true;
125 * Start a timer on a thread. kill it after wait seconds.
127 * Returns: btimer_t *(pointer to btimer_t struct) on success
130 btimer_t *start_thread_timer(JCR *jcr, pthread_t tid, uint32_t wait)
133 wid = btimer_start_common(wait);
135 Dmsg1(dbglvl, "start_thread_timer return NULL from common. wait=%d.\n", wait);
138 wid->type = TYPE_PTHREAD;
142 wid->wd->callback = callback_thread_timer;
143 wid->wd->one_shot = true;
144 wid->wd->interval = wait;
145 register_watchdog(wid->wd);
147 Dmsg3(dbglvl, "Start thread timer %p tid %p for %d secs.\n", wid, tid, wait);
153 * Start a timer on a BSOCK. kill it after wait seconds.
155 * Returns: btimer_t *(pointer to btimer_t struct) on success
158 btimer_t *start_bsock_timer(BSOCK *bsock, uint32_t wait)
161 if (wait <= 0) { /* wait should be > 0 */
164 wid = btimer_start_common(wait);
168 wid->type = TYPE_BSOCK;
169 wid->tid = pthread_self();
171 wid->jcr = bsock->jcr();
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(dbglvl, "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(dbglvl, "Stop bsock timer %p tid=%p at %d.\n", wid, wid->tid, time(NULL));
201 void stop_thread_timer(btimer_t *wid)
204 Dmsg0(dbglvl, "stop_thread_timer called with NULL btimer_id\n");
207 Dmsg2(dbglvl, "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(dbglvl, "thread timer %p kill %s tid=%p at %d.\n", self,
225 wid->type == TYPE_BSOCK ? "bsock" : "thread", wid->tid, time(NULL));
227 Dmsg2(dbglvl, "killed jid=%u Job=%s\n", wid->jcr->JobId, wid->jcr->Job);
230 if (wid->type == TYPE_BSOCK && wid->bsock) {
231 wid->bsock->set_timed_out();
233 pthread_kill(wid->tid, TIMEOUT_SIGNAL);
236 static btimer_t *btimer_start_common(uint32_t wait)
238 btimer_t *wid = (btimer_t *)malloc(sizeof(btimer_t));
240 wid->wd = new_watchdog();
241 if (wid->wd == NULL) {
254 static void stop_btimer(btimer_t *wid)
257 Emsg0(M_ABORT, 0, _("stop_btimer called with NULL btimer_id\n"));
259 unregister_watchdog(wid->wd);