This repository was archived by the owner on Mar 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
134 lines (116 loc) · 2.42 KB
/
main.cpp
File metadata and controls
134 lines (116 loc) · 2.42 KB
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
/* main.cpp
* The beginning, and the end, but not quite the middle
* Copyright (c) 2002 Alien Internet Services
*/
#include "config.h"
#include <unistd.h>
#include <iostream.h>
#include <stdio.h>
#include <signal.h>
#include "daemon.h"
#include "sender.h"
#ifdef WITH_SNMP
# include "snmp.h"
#endif
/* sigHandler - Handler signals
* Original 11/08/2001 simonb
* 19/02/2002 simonb - Re-adapted
*/
void sigHandler(int sig)
{
#ifdef DEBUG
cout << "Caught signal " << sig << " (" << sys_siglist[sig] << ")" << endl;
#endif
switch (sig) {
// Rehash... well.. checkpoint
case SIGHUP:
Daemon::checkpoint();
break;
// Die violently
case SIGILL:
case SIGTRAP:
#ifdef SIGEMT
case SIGEMT:
#endif
#ifdef SIGBUS
case SIGBUS:
#endif
#ifdef SIGSEGV
case SIGSEGV:
#endif
#ifdef SIGSYS
case SIGSYS:
#endif
#ifdef SIGURG
case SIGURG:
#endif
case SIGFPE:
#ifdef DEBUG
cout << "Down I go :(" << endl;
#endif
exit(1); // This could be a little nicer..
break;
// Die gracefully
case SIGINT:
case SIGQUIT:
case SIGTERM:
case SIGABRT:
#ifdef SIGXCPU
case SIGXCPU: // should this be in the die violently section?
#endif
#ifdef SIGXFSZ
case SIGXFSZ: // should this be in the die violently section?
#endif
Daemon::shutdown(sys_siglist[sig]);
break;
// Everything else we ignore.
// default:
// ?!
}
// Reset the signal for future handling. Some os's do not need this??
signal(sig, sigHandler);
}
/* main
* Original 18/02/2002 simonb
*/
int main(void)
{
// Initialise our 'bits' :)
Daemon::init();
Sender::init();
#ifdef WITH_SNMP
SNMP::init();
#endif
// Set up the signal handler happily
for (register unsigned int i = NSIG; i--;) {
signal(i, sigHandler);
}
#ifndef DEBUG
switch (fork()) {
case -1:
cout << "Could not run in the background. Exiting..." << endl;
perror("fork");
exit(1);
case 0:
break;
default:
cout << "Running in the background..." << endl;
exit(0);
}
#else
cout << "Running in the foreground (debugging)..." << endl;
#endif
// Shoot!
Daemon::run();
// De-init stuff
Daemon::deinit();
#ifdef WITH_SNMP
SNMP::deinit();
#endif
// Clean up the signals (to be friendly)
for (register unsigned int i = NSIG; i--;) {
signal(i, SIG_DFL);
}
// Done. Return happily
return 0;
}