2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 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)
79 Dmsg2(dbglvl, "Stop child timer %p pid %d\n", wid, wid->pid);
84 static void destructor_child_timer(watchdog_t *self)
86 btimer_t *wid = (btimer_t *)self->data;
92 static void callback_child_timer(watchdog_t *self)
94 btimer_t *wid = (btimer_t *)self->data;
97 /* First kill attempt; try killing it softly (kill -SONG) first */
100 Dmsg2(dbglvl, "watchdog %p term PID %d\n", self, wid->pid);
102 /* Kill -TERM the specified PID, and reschedule a -KILL for 5 seconds
103 * later. (Warning: this should let dvd-writepart enough time to term
104 * and kill growisofs, which takes 3 seconds, so the interval must not
105 * be less than 5 seconds)
107 kill(wid->pid, SIGTERM);
110 /* This is the second call - terminate with prejudice. */
111 Dmsg2(dbglvl, "watchdog %p kill PID %d\n", self, wid->pid);
113 kill(wid->pid, SIGKILL);
115 /* Setting one_shot to true before we leave ensures we don't get
118 self->one_shot = true;
123 * Start a timer on a thread. kill it after wait seconds.
125 * Returns: btimer_t *(pointer to btimer_t struct) on success
128 btimer_t *start_thread_timer(JCR *jcr, pthread_t tid, uint32_t wait)
131 wid = btimer_start_common(wait);
133 Dmsg1(dbglvl, "start_thread_timer return NULL from common. wait=%d.\n", wait);
136 wid->type = TYPE_PTHREAD;
140 wid->wd->callback = callback_thread_timer;
141 wid->wd->one_shot = true;
142 wid->wd->interval = wait;
143 register_watchdog(wid->wd);
145 Dmsg3(dbglvl, "Start thread timer %p tid %p for %d secs.\n", wid, tid, wait);
151 * Start a timer on a BSOCK. kill it after wait seconds.
153 * Returns: btimer_t *(pointer to btimer_t struct) on success
156 btimer_t *start_bsock_timer(BSOCK *bsock, uint32_t wait)
159 if (wait <= 0) { /* wait should be > 0 */
162 wid = btimer_start_common(wait);
166 wid->type = TYPE_BSOCK;
167 wid->tid = pthread_self();
169 wid->jcr = bsock->jcr();
171 wid->wd->callback = callback_thread_timer;
172 wid->wd->one_shot = true;
173 wid->wd->interval = wait;
174 register_watchdog(wid->wd);
176 Dmsg4(dbglvl, "Start bsock timer %p tid=%p for %d secs at %d\n", wid,
177 wid->tid, wait, time(NULL));
185 void stop_bsock_timer(btimer_t *wid)
190 Dmsg3(dbglvl, "Stop bsock timer %p tid=%p at %d.\n", wid, wid->tid, time(NULL));
198 void stop_thread_timer(btimer_t *wid)
203 Dmsg2(dbglvl, "Stop thread timer %p tid=%p.\n", wid, wid->tid);
208 static void destructor_thread_timer(watchdog_t *self)
210 btimer_t *wid = (btimer_t *)self->data;
216 static void callback_thread_timer(watchdog_t *self)
218 btimer_t *wid = (btimer_t *)self->data;
220 Dmsg4(dbglvl, "thread timer %p kill %s tid=%p at %d.\n", self,
221 wid->type == TYPE_BSOCK ? "bsock" : "thread", wid->tid, time(NULL));
223 Dmsg2(dbglvl, "killed jid=%u Job=%s\n", wid->jcr->JobId, wid->jcr->Job);
226 if (wid->type == TYPE_BSOCK && wid->bsock) {
227 wid->bsock->set_timed_out();
229 pthread_kill(wid->tid, TIMEOUT_SIGNAL);
232 static btimer_t *btimer_start_common(uint32_t wait)
234 btimer_t *wid = (btimer_t *)malloc(sizeof(btimer_t));
236 wid->wd = new_watchdog();
237 if (wid->wd == NULL) {
250 static void stop_btimer(btimer_t *wid)
253 Emsg0(M_ABORT, 0, _("stop_btimer called with NULL btimer_id\n"));
255 unregister_watchdog(wid->wd);