2 * Process and thread timer routines, built on top of watchdogs.
4 * Nic Bellamy <nic@bellamy.co.nz>, October 2004.
8 Copyright (C) 2004-2005 Kern Sibbald
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 version 2 as ammended with additional clauses defined in the
13 file LICENSE in the main source directory.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 the file LICENSE for additional details.
25 /* Forward referenced functions */
26 static void stop_btimer(btimer_t *wid);
27 static btimer_t *btimer_start_common(uint32_t wait);
29 /* Forward referenced callback functions */
30 static void callback_child_timer(watchdog_t *self);
31 static void callback_thread_timer(watchdog_t *self);
33 static void destructor_thread_timer(watchdog_t *self);
34 static void destructor_child_timer(watchdog_t *self);
38 * Start a timer on a child process of pid, kill it after wait seconds.
40 * Returns: btimer_t *(pointer to btimer_t struct) on success
43 btimer_t *start_child_timer(pid_t pid, uint32_t wait)
47 wid = btimer_start_common(wait);
51 wid->type = TYPE_CHILD;
55 wid->wd->callback = callback_child_timer;
56 wid->wd->one_shot = false;
57 wid->wd->interval = wait;
58 register_watchdog(wid->wd);
60 Dmsg3(900, "Start child timer %p, pid %d for %d secs.\n", wid, pid, wait);
67 void stop_child_timer(btimer_t *wid)
70 Dmsg0(900, "stop_child_timer called with NULL btimer_id\n");
73 Dmsg2(900, "Stop child timer %p pid %d\n", wid, wid->pid);
78 static void destructor_child_timer(watchdog_t *self)
80 btimer_t *wid = (btimer_t *)self->data;
86 static void callback_child_timer(watchdog_t *self)
88 btimer_t *wid = (btimer_t *)self->data;
91 /* First kill attempt; try killing it softly (kill -SONG) first */
94 Dmsg2(050, "watchdog %p term PID %d\n", self, wid->pid);
96 /* Kill -TERM the specified PID, and reschedule a -KILL for 3 seconds
99 kill(wid->pid, SIGTERM);
102 /* This is the second call - terminate with prejudice. */
103 Dmsg2(050, "watchdog %p kill PID %d\n", self, wid->pid);
105 kill(wid->pid, SIGKILL);
107 /* Setting one_shot to true before we leave ensures we don't get
110 self->one_shot = true;
115 * Start a timer on a thread. kill it after wait seconds.
117 * Returns: btimer_t *(pointer to btimer_t struct) on success
120 btimer_t *start_thread_timer(pthread_t tid, uint32_t wait)
123 wid = btimer_start_common(wait);
125 Dmsg1(900, "start_thread_timer return NULL from common. wait=%d.\n", wait);
128 wid->type = TYPE_PTHREAD;
131 wid->wd->callback = callback_thread_timer;
132 wid->wd->one_shot = true;
133 wid->wd->interval = wait;
134 register_watchdog(wid->wd);
136 Dmsg3(900, "Start thread timer %p tid %p for %d secs.\n", wid, tid, wait);
142 * Start a timer on a BSOCK. kill it after wait seconds.
144 * Returns: btimer_t *(pointer to btimer_t struct) on success
147 btimer_t *start_bsock_timer(BSOCK *bsock, uint32_t wait)
150 wid = btimer_start_common(wait);
154 wid->type = TYPE_BSOCK;
155 wid->tid = pthread_self();
158 wid->wd->callback = callback_thread_timer;
159 wid->wd->one_shot = true;
160 wid->wd->interval = wait;
161 register_watchdog(wid->wd);
163 Dmsg4(950, "Start bsock timer %p tid=%p for %d secs at %d\n", wid,
164 wid->tid, wait, time(NULL));
172 void stop_bsock_timer(btimer_t *wid)
175 Dmsg0(900, "stop_bsock_timer called with NULL btimer_id\n");
178 Dmsg3(950, "Stop bsock timer %p tid=%p at %d.\n", wid, wid->tid, time(NULL));
186 void stop_thread_timer(btimer_t *wid)
189 Dmsg0(900, "stop_thread_timer called with NULL btimer_id\n");
192 Dmsg2(900, "Stop thread timer %p tid=%p.\n", wid, wid->tid);
197 static void destructor_thread_timer(watchdog_t *self)
199 btimer_t *wid = (btimer_t *)self->data;
205 static void callback_thread_timer(watchdog_t *self)
207 btimer_t *wid = (btimer_t *)self->data;
209 Dmsg4(50, "thread timer %p kill %s tid=%p at %d.\n", self,
210 wid->type == TYPE_BSOCK ? "bsock" : "thread", wid->tid, time(NULL));
212 if (wid->type == TYPE_BSOCK && wid->bsock) {
213 wid->bsock->timed_out = true;
215 pthread_kill(wid->tid, TIMEOUT_SIGNAL);
218 static btimer_t *btimer_start_common(uint32_t wait)
220 btimer_t *wid = (btimer_t *)malloc(sizeof(btimer_t));
222 wid->wd = new_watchdog();
223 if (wid->wd == NULL) {
236 static void stop_btimer(btimer_t *wid)
239 Emsg0(M_ABORT, 0, "stop_btimer called with NULL btimer_id\n");
241 unregister_watchdog(wid->wd);