-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchdog.c
162 lines (124 loc) · 3.31 KB
/
watchdog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (C) 2016, CompuLab ltd.
* Author: Andrey Gelman <[email protected]>
* License: GNU GPLv2 or later, at your option
*/
/*
* Watchdog that sends the whole daemon down upon
* timeout detection.
* Resolution: seconds
* Signals employed: SIGUSR2
*/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include "watchdog.h"
#include "stats.h"
#include "common.h"
/* the earliest deadline at head */
static int time_compare(void *inlist, void *outlist)
{
WDeadline *a = (WDeadline *)inlist;
WDeadline *b = (WDeadline *)outlist;
return (int)(b->deadline - a->deadline);
}
static void watchdog_runner_cleanup(void *arg)
{
pthread_mutex_t *lock = (pthread_mutex_t *)arg;
pthread_mutex_unlock(lock);
}
static void *watchdog_runner(void *arg)
{
Watchdog *p = (Watchdog *)arg;
WDeadline *dl;
time_t deadline;
time_t now;
double delta;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
while ( 1 ) {
pthread_mutex_lock(&p->lock);
/* cleanup routine to be run upon thread cancellation */
pthread_cleanup_push(watchdog_runner_cleanup, &p->lock);
while (dlist_is_empty(p->activelist)) {
pthread_cond_wait(&p->list_not_empty, &p->lock);
}
dl = dlist_peek_front(p->activelist);
deadline = dl->deadline;
/* execute cleanup routine */
pthread_cleanup_pop(true);
now = time(NULL);
delta = difftime(now, deadline);
if (delta >= 0.0) {
/* timeout */
sloge("watchdog: timeout exceeded - shutdown daemon");
kill(0, SIGTERM);
break;
}
/* sleep is interruptible by (SIGUSR2) signal */
sleep((unsigned int)(-delta));
}
return NULL;
}
Watchdog *watchdog_create(void)
{
Watchdog *p;
p = (Watchdog *)malloc(sizeof(Watchdog));
pthread_mutex_init(&p->lock, NULL);
pthread_cond_init(&p->list_not_empty, NULL);
p->activelist = dlist_create(time_compare);
p->freelist = dlist_create(NULL);
pthread_create(&p->thread, NULL, watchdog_runner, p);
return p;
}
void watchdog_destroy(Watchdog *p)
{
WDeadline *dl;
pthread_cancel(p->thread);
pthread_join(p->thread, NULL);
pthread_mutex_destroy(&p->lock);
pthread_cond_destroy(&p->list_not_empty);
while ((dl = dlist_pop_front(p->activelist)) != NULL)
free(dl);
while ((dl = dlist_pop_front(p->freelist)) != NULL)
free(dl);
dlist_destroy(p->activelist);
dlist_destroy(p->freelist);
/* zero everything against evil eye */
memset(p, 0, sizeof(Watchdog));
free(p);
}
WDeadline *watchdog_submit_deadline(Watchdog *p, unsigned int delta)
{
WDeadline *dl;
time_t deadline;
deadline = time(NULL) + delta;
pthread_mutex_lock(&p->lock);
dl = dlist_pop_front(p->freelist);
if (dl == NULL) {
dl = (WDeadline *)calloc(1, sizeof(WDeadline));
if (dl == NULL)
goto submit_deadline_out;
stat_inc_watchdog_list_length();
}
dl->deadline = deadline;
dlist_push_ordered(p->activelist, dl);
pthread_cond_signal(&p->list_not_empty);
pthread_kill(p->thread, SIGUSR2);
submit_deadline_out:
pthread_mutex_unlock(&p->lock);
return dl;
}
void watchdog_clear_deadline(Watchdog *p, WDeadline *dl)
{
pthread_mutex_lock(&p->lock);
dlist_remove_node(p->activelist, dl);
dlist_push_back(p->freelist, dl);
pthread_mutex_unlock(&p->lock);
}