]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/btimers.c
Change copyright as per agreement with FSFE
[bacula/bacula] / bacula / src / lib / btimers.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 Kern Sibbald
5
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.
8
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.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  * Process and thread timer routines, built on top of watchdogs.
21  *
22  *    Nic Bellamy <nic@bellamy.co.nz>, October 2004.
23  *
24 */
25
26 #include "bacula.h"
27 #include "jcr.h"
28
29 const int dbglvl = 900;
30
31 /* Forward referenced functions */
32 static void stop_btimer(btimer_t *wid);
33 static btimer_t *btimer_start_common(uint32_t wait);
34
35 /* Forward referenced callback functions */
36 static void callback_child_timer(watchdog_t *self);
37 static void callback_thread_timer(watchdog_t *self);
38 #ifdef xxx
39 static void destructor_thread_timer(watchdog_t *self);
40 static void destructor_child_timer(watchdog_t *self);
41 #endif
42
43 /*
44  * Start a timer on a child process of pid, kill it after wait seconds.
45  *
46  *  Returns: btimer_t *(pointer to btimer_t struct) on success
47  *           NULL on failure
48  */
49 btimer_t *start_child_timer(JCR *jcr, pid_t pid, uint32_t wait)
50 {
51    btimer_t *wid;
52
53    wid = btimer_start_common(wait);
54    if (wid == NULL) {
55       return NULL;
56    }
57    wid->type = TYPE_CHILD;
58    wid->pid = pid;
59    wid->killed = false;
60    wid->jcr = jcr;
61
62    wid->wd->callback = callback_child_timer;
63    wid->wd->one_shot = false;
64    wid->wd->interval = wait;
65    register_watchdog(wid->wd);
66
67    Dmsg3(dbglvl, "Start child timer %p, pid %d for %d secs.\n", wid, pid, wait);
68    return wid;
69 }
70
71 /*
72  * Stop child timer
73  */
74 void stop_child_timer(btimer_t *wid)
75 {
76    if (wid == NULL) {
77       Dmsg0(dbglvl, "stop_child_timer called with NULL btimer_id\n");
78       return;
79    }
80    Dmsg2(dbglvl, "Stop child timer %p pid %d\n", wid, wid->pid);
81    stop_btimer(wid);
82 }
83
84 #ifdef xxx
85 static void destructor_child_timer(watchdog_t *self)
86 {
87    btimer_t *wid = (btimer_t *)self->data;
88    free(wid->wd);
89    free(wid);
90 }
91 #endif
92
93 static void callback_child_timer(watchdog_t *self)
94 {
95    btimer_t *wid = (btimer_t *)self->data;
96
97    if (!wid->killed) {
98       /* First kill attempt; try killing it softly (kill -SONG) first */
99       wid->killed = true;
100
101       Dmsg2(dbglvl, "watchdog %p term PID %d\n", self, wid->pid);
102
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)
107        */
108       kill(wid->pid, SIGTERM);
109       self->interval = 5;
110    } else {
111       /* This is the second call - terminate with prejudice. */
112       Dmsg2(dbglvl, "watchdog %p kill PID %d\n", self, wid->pid);
113
114       kill(wid->pid, SIGKILL);
115
116       /* Setting one_shot to true before we leave ensures we don't get
117        * rescheduled.
118        */
119       self->one_shot = true;
120    }
121 }
122
123 /*
124  * Start a timer on a thread. kill it after wait seconds.
125  *
126  *  Returns: btimer_t *(pointer to btimer_t struct) on success
127  *           NULL on failure
128  */
129 btimer_t *start_thread_timer(JCR *jcr, pthread_t tid, uint32_t wait)
130 {
131    btimer_t *wid;
132    wid = btimer_start_common(wait);
133    if (wid == NULL) {
134       Dmsg1(dbglvl, "start_thread_timer return NULL from common. wait=%d.\n", wait);
135       return NULL;
136    }
137    wid->type = TYPE_PTHREAD;
138    wid->tid = tid;
139    wid->jcr = jcr;
140
141    wid->wd->callback = callback_thread_timer;
142    wid->wd->one_shot = true;
143    wid->wd->interval = wait;
144    register_watchdog(wid->wd);
145
146    Dmsg3(dbglvl, "Start thread timer %p tid %p for %d secs.\n", wid, tid, wait);
147
148    return wid;
149 }
150
151 /*
152  * Start a timer on a BSOCK. kill it after wait seconds.
153  *
154  *  Returns: btimer_t *(pointer to btimer_t struct) on success
155  *           NULL on failure
156  */
157 btimer_t *start_bsock_timer(BSOCK *bsock, uint32_t wait)
158 {
159    btimer_t *wid;
160    if (wait <= 0) {                 /* wait should be > 0 */
161       return NULL;
162    }
163    wid = btimer_start_common(wait);
164    if (wid == NULL) {
165       return NULL;
166    }
167    wid->type = TYPE_BSOCK;
168    wid->tid = pthread_self();
169    wid->bsock = bsock;
170    wid->jcr = bsock->jcr();
171
172    wid->wd->callback = callback_thread_timer;
173    wid->wd->one_shot = true;
174    wid->wd->interval = wait;
175    register_watchdog(wid->wd);
176
177    Dmsg4(dbglvl, "Start bsock timer %p tid=%p for %d secs at %d\n", wid,
178          wid->tid, wait, time(NULL));
179
180    return wid;
181 }
182
183 /*
184  * Stop bsock timer
185  */
186 void stop_bsock_timer(btimer_t *wid)
187 {
188    if (wid == NULL) {
189       Dmsg0(900, "stop_bsock_timer called with NULL btimer_id\n");
190       return;
191    }
192    Dmsg3(dbglvl, "Stop bsock timer %p tid=%p at %d.\n", wid, wid->tid, time(NULL));
193    stop_btimer(wid);
194 }
195
196
197 /*
198  * Stop thread timer
199  */
200 void stop_thread_timer(btimer_t *wid)
201 {
202    if (wid == NULL) {
203       Dmsg0(dbglvl, "stop_thread_timer called with NULL btimer_id\n");
204       return;
205    }
206    Dmsg2(dbglvl, "Stop thread timer %p tid=%p.\n", wid, wid->tid);
207    stop_btimer(wid);
208 }
209
210 #ifdef xxx
211 static void destructor_thread_timer(watchdog_t *self)
212 {
213    btimer_t *wid = (btimer_t *)self->data;
214    free(wid->wd);
215    free(wid);
216 }
217 #endif
218
219 static void callback_thread_timer(watchdog_t *self)
220 {
221    btimer_t *wid = (btimer_t *)self->data;
222
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));
225    if (wid->jcr) {
226       Dmsg2(dbglvl, "killed jid=%u Job=%s\n", wid->jcr->JobId, wid->jcr->Job);
227    }
228
229    if (wid->type == TYPE_BSOCK && wid->bsock) {
230       wid->bsock->set_timed_out();
231    }
232    pthread_kill(wid->tid, TIMEOUT_SIGNAL);
233 }
234
235 static btimer_t *btimer_start_common(uint32_t wait)
236 {
237    btimer_t *wid = (btimer_t *)malloc(sizeof(btimer_t));
238
239    wid->wd = new_watchdog();
240    if (wid->wd == NULL) {
241       free(wid);
242       return NULL;
243    }
244    wid->wd->data = wid;
245    wid->killed = FALSE;
246
247    return wid;
248 }
249
250 /*
251  * Stop btimer
252  */
253 static void stop_btimer(btimer_t *wid)
254 {
255    if (wid == NULL) {
256       Emsg0(M_ABORT, 0, _("stop_btimer called with NULL btimer_id\n"));
257    }
258    unregister_watchdog(wid->wd);
259    free(wid->wd);
260    free(wid);
261 }