File: signals.c

package info (click to toggle)
arpalert 2.0.3-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,332 kB
  • ctags: 415
  • sloc: ansic: 4,185; sh: 447; makefile: 153; perl: 28
file content (121 lines) | stat: -rw-r--r-- 2,265 bytes parent folder | download
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
/*
 * Copyright (c) 2005-2010 Thierry FOURNIER
 * $Id: arpalert.c 421 2006-11-04 10:56:25Z thierry $
 *
 */

#include "config.h"

#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "log.h"
#include "data.h"
#include "maclist.h"
#include "alerte.h"
#include "sens.h"
#include "loadmodule.h"
#include "macname.h"

extern int errno;

void die(int);
void dumpmaclist(int);

// set flag signals
int sigchld = 0;
int sighup = 0;
int sigstop = 0;

void setsigchld(int signal){
	sigchld++;
	#ifdef DEBUG
	logmsg(LOG_DEBUG, "[%s %d %s] SIGCHLD",
	       __FILE__, __LINE__, __FUNCTION__);
	#endif
}
void setsighup(int signal){
	sighup++;
	#ifdef DEBUG
	logmsg(LOG_DEBUG, "[%s %d %s] SIGHUP",
	       __FILE__, __LINE__, __FUNCTION__);
	#endif
}
void setsigstop(int signal){
	sigstop++;
	#ifdef DEBUG
	logmsg(LOG_DEBUG, "[%s %d %s] SIGSTOP",
	       __FILE__, __LINE__, __FUNCTION__);
	#endif
}

// setup signals
void (*setsignal (int signal, void (*function)(int)))(int) {    
	struct sigaction old, new;

	memset(&new, 0, sizeof(struct sigaction));
	new.sa_handler = function;
	new.sa_flags = SA_RESTART;
	sigemptyset(&(new.sa_mask));
	if (sigaction(signal, &new, &old)){ 
		logmsg(LOG_ERR, "[%s %i] sigaction[%d]: %s",
		       __FILE__, __LINE__, errno, strerror(errno));
		exit(1);
	}
	return(old.sa_handler);
}

// init signals
void signals_init(void){
	(void)setsignal(SIGINT,  setsigstop);
	(void)setsignal(SIGTERM, setsigstop);
	(void)setsignal(SIGQUIT, setsigstop);
	(void)setsignal(SIGABRT, setsigstop);
	(void)setsignal(SIGCHLD, setsigchld); 
	(void)setsignal(SIGHUP,  setsighup); 
}

// signals computing
void signals_func(void){

	// SIGCHLD
	if(sigchld > 0){
		sigchld--;
		alerte_kill_pid();
	}
	// SIGHUP
	if(sighup > 0){
		sighup--;
		maclist_reload();
		sens_reload();
		macname_reload();
	}
	// SIGQUIT
	if(sigstop > 0){
		sigstop--;
		#ifdef DEBUG
		logmsg(LOG_DEBUG, "[%s %i %s] quit arpalert",
		       __FILE__, __LINE__, __FUNCTION__);
		#endif
		data_dump();
		module_unload();
		exit(0);
	}
}

// get next signal
void *signals_next(struct timeval *tv){
	if(sigchld != 0 ||
	   sighup != 0 ||
	   sigstop != 0){
		tv->tv_sec = 0;
		tv->tv_usec = 0;
		return signals_func;
	}

	tv->tv_sec = -1;
	return NULL;
}