/
watchdog.cpp
306 lines (275 loc) · 6.79 KB
/
watchdog.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Program watchdog.cpp
*
* This program defines a watchdog program that
* expects to read a control file of the form:
*
* command line (program to start & monitor)
* port number (decimal or 0xHEX)
* [1 or more] of
* name timeout(ms)
*
* Change History :
*
* $Log: watchdog.cpp,v $
* Revision 1.1 2008-06-06 21:48:10 ericn
* -import
*
*
* Copyright Boundary Devices, Inc. 2008
*/
#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/fcntl.h>
#include <sys/poll.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/time.h>
inline long long timeValToMs( struct timeval const &tv )
{
return ((long long)tv.tv_sec*1000)+((long long)tv.tv_usec / 1000 );
}
inline long long tickMs()
{
struct timeval now ; gettimeofday( &now, 0 );
return timeValToMs( now );
}
typedef struct _watchpoint_t {
char const *name ;
unsigned timeout ;
long long lastAlive ;
unsigned long minInterval ;
unsigned long maxInterval ;
unsigned long count ;
struct _watchpoint_t *next ;
} watchPoint_t ;
// a.la. fgets, but skipping blank lines and comments
static char *getLine( char *inBuf, unsigned inSize, FILE *fIn ){
char *out ;
do {
out = fgets( inBuf, inSize, fIn );
if( !out )
break;
else if( ( '#' == *out ) || ( '\n' == *out ) )
continue;
else {
out[strlen(out)-1] = '\0' ; // strip newline
break;
}
} while( 1 );
return out ;
}
static void parse( char *buf, char **args )
{
while( *buf ){
/*
* Strip whitespace. Use nulls, so
* that the previous argument is terminated
* automatically.
*/
while ((*buf == ' ') || (*buf == '\t'))
*buf++ = '\0' ;
/*
* Save the argument.
*/
*args++ = buf;
/*
* Skip over the argument.
*/
while (*buf && (*buf != ' ') && (*buf != '\t'))
buf++;
}
*buf = 0 ; // terminating null
*args = NULL;
}
static void showArgs( char **args )
{
while( *args ){
printf( "--> %s\n", *args++ );
}
}
static int volatile childPid_ = -1 ;
static void childHandler
( int sig,
struct siginfo *info,
void *context )
{
if( SIGCHLD == sig )
{
int exitCode ;
struct rusage usage ;
int pid ;
while( 0 < ( pid = wait3( &exitCode, WNOHANG, &usage ) ) ){
if( pid == childPid_ ){
printf( "%lld:child %d died\n", tickMs(), pid );
childPid_ = -1 ;
}
}
}
}
static void startChild( char **args, watchPoint_t *wp )
{
pid_t pid = fork();
if( 0 == pid ){
// child
printf( "child process %d\n", getpid() );
execvp( args[0], args );
exit(-1);
}
else {
printf( "parent of %d\n", pid );
childPid_ = pid ;
}
sleep( 2 );
int rval = kill(pid,SIGCONT);
printf( "SIGCONT: %d\n", rval );
if( 0 == rval ){
long long now = tickMs();
for( ; wp ; wp = wp->next ){
wp->lastAlive = now ;
wp->minInterval = 0xFFFFFFFF ;
wp->maxInterval = 0 ;
}
}
}
static unsigned numWP = 0 ;
static watchPoint_t *firstWP = 0 ;
static watchPoint_t **watchpoints = 0 ;
static void dumpStats( void ){
}
int main( int argc, char const * const argv[] )
{
if( 2 != argc ){
fprintf( stderr, "Usage: %s /path/to/controlFile\n", argv[0] );
return -1 ;
}
FILE *fIn = fopen( argv[1], "rt" );
if( !fIn ){
perror( argv[1] );
return -1 ;
}
char cmdLine[512];
if( !getLine( cmdLine, sizeof(cmdLine)-1, fIn ) ){
perror( "cmdLine" );
return -1 ;
}
while( getLine( inBuf,sizeof(inBuf)-1, fIn ) ){
char name[80];
unsigned timeout ;
int numRead ;
if( 2 == (numRead = sscanf( inBuf, "%s %u", &name, &timeout ) )){
watchPoint_t *newOne = new watchPoint_t ;
memset( newOne, 0, sizeof(*newOne) );
newOne->name = strdup( name );
newOne->timeout = timeout ;
newOne->next = firstWP ;
firstWP = newOne ;
++numWP ;
}
} // until EOF
if( 0 == numWP ){
fprintf( stderr, "no watchpoints defined\n" );
return -1 ;
}
fclose( fIn );
int const sockFd = socket( AF_INET, SOCK_DGRAM, 0 );
if( 0 > sockFd ){
perror( "socket" );
return -1 ;
}
int const doit = 1;
int rval = setsockopt(sockFd, SOL_SOCKET, SO_REUSEADDR, (void *)&doit, sizeof(doit));
if( 0 != rval ){
perror( "setsockopt" );
return -1 ;
}
int flags = flags = fcntl( sockFd, F_GETFL, 0 );
rval = fcntl( sockFd, F_SETFL, flags | O_NONBLOCK );
if( 0 != rval ){
perror( "O_NONBLOCK" );
return -1 ;
}
watchpoints = new watchPoint_t *[numWP];
printf( "command line: %s\n", cmdLine );
printf( "port %u (0x%x)\n", port, port );
unsigned idx = 0 ;
for( watchPoint_t *next = firstWP ; next ; next = next->next ){
unsigned wdNum = numWP-1-idx ;
idx++ ;
printf( "%u:%s: %u\n", wdNum, next->name, next->timeout );
watchpoints[wdNum] = next ;
}
char *args[80];
parse( cmdLine, args );
showArgs( args );
struct sigaction sa;
/* Initialize the sa structure */
sa.sa_handler = (__sighandler_t)childHandler ;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO ; // pass info
// int result =
sigaction( SIGCHLD, &sa, 0 );
do {
// start program
startChild( args, firstWP );
// now monitor program
pollfd rdPoll ;
rdPoll.fd = sockFd ;
while( 0 < childPid_ ){
rdPoll.events = POLLIN | POLLERR ;
int const numEvents = poll( &rdPoll, 1, 1000 );
long long const now = tickMs();
if( 1 == numEvents )
{
sockaddr_in remote ;
socklen_t remSize = sizeof(remote);
unsigned char in ;
int numRead ;
do {
numRead = recvfrom( sockFd, &in, 1, 0, (struct sockaddr *)&remote, &remSize );
if( 1 == numRead ){
if( in < numWP ){
watchPoint_t &wp = *watchpoints[in];
unsigned long elapsed = now-wp.lastAlive ;
wp.lastAlive = now ;
wp.count++ ;
if( elapsed < wp.minInterval )
wp.minInterval = elapsed ;
if( elapsed > wp.maxInterval )
wp.maxInterval = elapsed ;
}
else
fprintf( stderr, "Invalid watchpoint %u, max %u\n", in, numRead );
}
} while( 1 == numRead );
}
bool killit = false ;
for( unsigned i = 0 ; i < numWP ; i++ ){
watchPoint_t &wp = *watchpoints[i];
unsigned long elapsed = now-wp.lastAlive ;
if( elapsed > wp.timeout ){
printf( "event %u expired: %u elapsed, max %u\n", i, elapsed, wp.timeout );
killit = true ;
}
}
if( killit ){
pid_t pid = childPid_ ;
printf( "killing child process %d\n", pid );
if( 0 < pid ){
kill( pid, SIGKILL );
}
while( 0 > childPid_ ){
printf( "waiting for child\n" );
sleep(1);
}
}
} // while child is stilll alive
printf( "%lld:child died. restarting\n", tickMs() );
} while( 1 );
return 0 ;
}