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 amended 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 5 seconds
97 * later. (Warning: this should let dvd-writepart enough time to term
98 * and kill growisofs, which takes 3 seconds, so the interval must not
99 * be less than 5 seconds)
101 kill(wid->pid, SIGTERM);
104 /* This is the second call - terminate with prejudice. */
105 Dmsg2(050, "watchdog %p kill PID %d\n", self, wid->pid);
107 kill(wid->pid, SIGKILL);
109 /* Setting one_shot to true before we leave ensures we don't get
112 self->one_shot = true;
117 * Start a timer on a thread. kill it after wait seconds.
119 * Returns: btimer_t *(pointer to btimer_t struct) on success
122 btimer_t *start_thread_timer(pthread_t tid, uint32_t wait)
125 wid = btimer_start_common(wait);
127 Dmsg1(900, "start_thread_timer return NULL from common. wait=%d.\n", wait);
130 wid->type = TYPE_PTHREAD;
133 wid->wd->callback = callback_thread_timer;
134 wid->wd->one_shot = true;
135 wid->wd->interval = wait;
136 register_watchdog(wid->wd);
138 Dmsg3(900, "Start thread timer %p tid %p for %d secs.\n", wid, tid, wait);
144 * Start a timer on a BSOCK. kill it after wait seconds.
146 * Returns: btimer_t *(pointer to btimer_t struct) on success
149 btimer_t *start_bsock_timer(BSOCK *bsock, uint32_t wait)
152 wid = btimer_start_common(wait);
156 wid->type = TYPE_BSOCK;
157 wid->tid = pthread_self();
160 wid->wd->callback = callback_thread_timer;
161 wid->wd->one_shot = true;
162 wid->wd->interval = wait;
163 register_watchdog(wid->wd);
165 Dmsg4(950, "Start bsock timer %p tid=%p for %d secs at %d\n", wid,
166 wid->tid, wait, time(NULL));
174 void stop_bsock_timer(btimer_t *wid)
177 Dmsg0(900, "stop_bsock_timer called with NULL btimer_id\n");
180 Dmsg3(950, "Stop bsock timer %p tid=%p at %d.\n", wid, wid->tid, time(NULL));
188 void stop_thread_timer(btimer_t *wid)
191 Dmsg0(900, "stop_thread_timer called with NULL btimer_id\n");
194 Dmsg2(900, "Stop thread timer %p tid=%p.\n", wid, wid->tid);
199 static void destructor_thread_timer(watchdog_t *self)
201 btimer_t *wid = (btimer_t *)self->data;
207 static void callback_thread_timer(watchdog_t *self)
209 btimer_t *wid = (btimer_t *)self->data;
211 Dmsg4(50, "thread timer %p kill %s tid=%p at %d.\n", self,
212 wid->type == TYPE_BSOCK ? "bsock" : "thread", wid->tid, time(NULL));
214 if (wid->type == TYPE_BSOCK && wid->bsock) {
215 wid->bsock->timed_out = true;
217 pthread_kill(wid->tid, TIMEOUT_SIGNAL);
220 static btimer_t *btimer_start_common(uint32_t wait)
222 btimer_t *wid = (btimer_t *)malloc(sizeof(btimer_t));
224 wid->wd = new_watchdog();
225 if (wid->wd == NULL) {
238 static void stop_btimer(btimer_t *wid)
241 Emsg0(M_ABORT, 0, _("stop_btimer called with NULL btimer_id\n"));
243 unregister_watchdog(wid->wd);