2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2016 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * Process and thread timer routines, built on top of watchdogs.
22 * Nic Bellamy <nic@bellamy.co.nz>, October 2004.
29 const int dbglvl = 900;
31 /* Forward referenced functions */
32 static void stop_btimer(btimer_t *wid);
33 static btimer_t *btimer_start_common(uint32_t wait);
35 /* Forward referenced callback functions */
36 static void callback_child_timer(watchdog_t *self);
37 static void callback_thread_timer(watchdog_t *self);
39 static void destructor_thread_timer(watchdog_t *self);
40 static void destructor_child_timer(watchdog_t *self);
44 * Start a timer on a child process of pid, kill it after wait seconds.
46 * Returns: btimer_t *(pointer to btimer_t struct) on success
49 btimer_t *start_child_timer(JCR *jcr, pid_t pid, uint32_t wait)
53 wid = btimer_start_common(wait);
57 wid->type = TYPE_CHILD;
62 wid->wd->callback = callback_child_timer;
63 wid->wd->one_shot = false;
64 wid->wd->interval = wait;
65 register_watchdog(wid->wd);
67 Dmsg3(dbglvl, "Start child timer %p, pid %d for %d secs.\n", wid, pid, wait);
74 void stop_child_timer(btimer_t *wid)
77 Dmsg0(dbglvl, "stop_child_timer called with NULL btimer_id\n");
80 Dmsg2(dbglvl, "Stop child timer %p pid %d\n", wid, wid->pid);
85 static void destructor_child_timer(watchdog_t *self)
87 btimer_t *wid = (btimer_t *)self->data;
93 static void callback_child_timer(watchdog_t *self)
95 btimer_t *wid = (btimer_t *)self->data;
98 /* First kill attempt; try killing it softly (kill -SONG) first */
101 Dmsg2(dbglvl, "watchdog %p term PID %d\n", self, wid->pid);
103 /* Kill -TERM the specified PID, and reschedule a -KILL for 5 seconds
104 * later. (Warning: this should let dvd-writepart enough time to term
105 * and kill growisofs, which takes 3 seconds, so the interval must not
106 * be less than 5 seconds)
108 kill(wid->pid, SIGTERM);
111 /* This is the second call - terminate with prejudice. */
112 Dmsg2(dbglvl, "watchdog %p kill PID %d\n", self, wid->pid);
114 kill(wid->pid, SIGKILL);
116 /* Setting one_shot to true before we leave ensures we don't get
119 self->one_shot = true;
124 * Start a timer on a thread. kill it after wait seconds.
126 * Returns: btimer_t *(pointer to btimer_t struct) on success
129 btimer_t *start_thread_timer(JCR *jcr, pthread_t tid, uint32_t wait)
132 wid = btimer_start_common(wait);
134 Dmsg1(dbglvl, "start_thread_timer return NULL from common. wait=%d.\n", wait);
137 wid->type = TYPE_PTHREAD;
141 wid->wd->callback = callback_thread_timer;
142 wid->wd->one_shot = true;
143 wid->wd->interval = wait;
144 register_watchdog(wid->wd);
146 Dmsg3(dbglvl, "Start thread timer %p tid %p for %d secs.\n", wid, tid, wait);
152 * Start a timer on a BSOCK. kill it after wait seconds.
154 * Returns: btimer_t *(pointer to btimer_t struct) on success
157 btimer_t *start_bsock_timer(BSOCK *bsock, uint32_t wait)
160 if (wait <= 0) { /* wait should be > 0 */
163 wid = btimer_start_common(wait);
167 wid->type = TYPE_BSOCK;
168 wid->tid = pthread_self();
170 wid->jcr = bsock->jcr();
172 wid->wd->callback = callback_thread_timer;
173 wid->wd->one_shot = true;
174 wid->wd->interval = wait;
175 register_watchdog(wid->wd);
177 Dmsg4(dbglvl, "Start bsock timer %p tid=%p for %d secs at %d\n", wid,
178 wid->tid, wait, time(NULL));
186 void stop_bsock_timer(btimer_t *wid)
189 Dmsg0(900, "stop_bsock_timer called with NULL btimer_id\n");
192 Dmsg3(dbglvl, "Stop bsock timer %p tid=%p at %d.\n", wid, wid->tid, time(NULL));
200 void stop_thread_timer(btimer_t *wid)
203 Dmsg0(dbglvl, "stop_thread_timer called with NULL btimer_id\n");
206 Dmsg2(dbglvl, "Stop thread timer %p tid=%p.\n", wid, wid->tid);
211 static void destructor_thread_timer(watchdog_t *self)
213 btimer_t *wid = (btimer_t *)self->data;
219 static void callback_thread_timer(watchdog_t *self)
221 btimer_t *wid = (btimer_t *)self->data;
223 Dmsg4(dbglvl, "thread timer %p kill %s tid=%p at %d.\n", self,
224 wid->type == TYPE_BSOCK ? "bsock" : "thread", wid->tid, time(NULL));
226 Dmsg2(dbglvl, "killed jid=%u Job=%s\n", wid->jcr->JobId, wid->jcr->Job);
229 if (wid->type == TYPE_BSOCK && wid->bsock) {
230 wid->bsock->set_timed_out();
232 pthread_kill(wid->tid, TIMEOUT_SIGNAL);
235 static btimer_t *btimer_start_common(uint32_t wait)
237 btimer_t *wid = (btimer_t *)malloc(sizeof(btimer_t));
239 wid->wd = new_watchdog();
240 if (wid->wd == NULL) {
253 static void stop_btimer(btimer_t *wid)
256 Emsg0(M_ABORT, 0, _("stop_btimer called with NULL btimer_id\n"));
258 unregister_watchdog(wid->wd);