-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal.c
66 lines (58 loc) · 1.57 KB
/
signal.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
#include <stdio.h> // FILE, fopen, fwrite, fclose, fprintf
#include <unistd.h> // unlink
#include <string.h> // strlen, memset, strerror
#include <stdlib.h> // exit
#include <signal.h> // struct sigaction, sigaction, sigemptyset, SA_*, SIG*
#include <errno.h> // errno
#include "sysmond.h"
// Handle SIGTERM and SIGUSR1 signals
void signalHandler( int sig )
{
FILE* file;
switch( sig )
{
case SIGTERM:
unlink( sysmond_args.pidFile ); // remove pidFile
exit( 0 );
break;
case SIGUSR1:
file = fopen( sysmond_args.dbgFile, "a" );
if ( file != NULL )
{
fwrite( data[ data_available ],
strlen( data[ data_available ] ),
1,
file );
fclose( file );
}
break;
}
}
void install_sighandler()
{
struct sigaction new;
int ret;
// Initialize struct sigaction new
memset( &new, 0, sizeof( struct sigaction ) );
new.sa_handler = signalHandler;
sigemptyset( &new.sa_mask );
new.sa_flags = SA_RESTART;
// Set up SIGTERM
ret = sigaction( SIGTERM, &new, NULL );
if ( ret == -1)
{
fprintf(stderr, "Could not set sighandler for SIGTERM: %s\n",
strerror( errno )
);
exit( EXIT_FAILURE );
}
// Set up SIGUSR1
ret = sigaction( SIGUSR1, &new, NULL );
if ( ret == -1 )
{
fprintf( stderr, "Could not set sighandler for SIGUSR1: %s\n",
strerror( errno )
);
exit( EXIT_FAILURE );
}
}