2 Bacula® - The Network Backup Solution
4 Copyright (C) 2004-2014 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from many
7 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 Bacula® is a registered trademark of Kern Sibbald.
17 * Process and thread timer routines, built on top of watchdogs.
19 * Nic Bellamy <nic@bellamy.co.nz>, October 2004.
26 const int dbglvl = 900;
28 /* Forward referenced functions */
29 static void stop_btimer(btimer_t *wid);
30 static btimer_t *btimer_start_common(uint32_t wait);
32 /* Forward referenced callback functions */
33 static void callback_child_timer(watchdog_t *self);
34 static void callback_thread_timer(watchdog_t *self);
36 static void destructor_thread_timer(watchdog_t *self);
37 static void destructor_child_timer(watchdog_t *self);
41 * Start a timer on a child process of pid, kill it after wait seconds.
43 * Returns: btimer_t *(pointer to btimer_t struct) on success
46 btimer_t *start_child_timer(JCR *jcr, pid_t pid, uint32_t wait)
50 wid = btimer_start_common(wait);
54 wid->type = TYPE_CHILD;
59 wid->wd->callback = callback_child_timer;
60 wid->wd->one_shot = false;
61 wid->wd->interval = wait;
62 register_watchdog(wid->wd);
64 Dmsg3(dbglvl, "Start child timer %p, pid %d for %d secs.\n", wid, pid, wait);
71 void stop_child_timer(btimer_t *wid)
74 Dmsg0(dbglvl, "stop_child_timer called with NULL btimer_id\n");
77 Dmsg2(dbglvl, "Stop child timer %p pid %d\n", wid, wid->pid);
82 static void destructor_child_timer(watchdog_t *self)
84 btimer_t *wid = (btimer_t *)self->data;
90 static void callback_child_timer(watchdog_t *self)
92 btimer_t *wid = (btimer_t *)self->data;
95 /* First kill attempt; try killing it softly (kill -SONG) first */
98 Dmsg2(dbglvl, "watchdog %p term PID %d\n", self, wid->pid);
100 /* Kill -TERM the specified PID, and reschedule a -KILL for 5 seconds
101 * later. (Warning: this should let dvd-writepart enough time to term
102 * and kill growisofs, which takes 3 seconds, so the interval must not
103 * be less than 5 seconds)
105 kill(wid->pid, SIGTERM);
108 /* This is the second call - terminate with prejudice. */
109 Dmsg2(dbglvl, "watchdog %p kill PID %d\n", self, wid->pid);
111 kill(wid->pid, SIGKILL);
113 /* Setting one_shot to true before we leave ensures we don't get
116 self->one_shot = true;
121 * Start a timer on a thread. kill it after wait seconds.
123 * Returns: btimer_t *(pointer to btimer_t struct) on success
126 btimer_t *start_thread_timer(JCR *jcr, pthread_t tid, uint32_t wait)
129 wid = btimer_start_common(wait);
131 Dmsg1(dbglvl, "start_thread_timer return NULL from common. wait=%d.\n", wait);
134 wid->type = TYPE_PTHREAD;
138 wid->wd->callback = callback_thread_timer;
139 wid->wd->one_shot = true;
140 wid->wd->interval = wait;
141 register_watchdog(wid->wd);
143 Dmsg3(dbglvl, "Start thread timer %p tid %p for %d secs.\n", wid, tid, wait);
149 * Start a timer on a BSOCK. kill it after wait seconds.
151 * Returns: btimer_t *(pointer to btimer_t struct) on success
154 btimer_t *start_bsock_timer(BSOCK *bsock, uint32_t wait)
157 if (wait <= 0) { /* wait should be > 0 */
160 wid = btimer_start_common(wait);
164 wid->type = TYPE_BSOCK;
165 wid->tid = pthread_self();
167 wid->jcr = bsock->jcr();
169 wid->wd->callback = callback_thread_timer;
170 wid->wd->one_shot = true;
171 wid->wd->interval = wait;
172 register_watchdog(wid->wd);
174 Dmsg4(dbglvl, "Start bsock timer %p tid=%p for %d secs at %d\n", wid,
175 wid->tid, wait, time(NULL));
183 void stop_bsock_timer(btimer_t *wid)
186 Dmsg0(900, "stop_bsock_timer called with NULL btimer_id\n");
189 Dmsg3(dbglvl, "Stop bsock timer %p tid=%p at %d.\n", wid, wid->tid, time(NULL));
197 void stop_thread_timer(btimer_t *wid)
200 Dmsg0(dbglvl, "stop_thread_timer called with NULL btimer_id\n");
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);