-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
94 lines (80 loc) · 2.15 KB
/
main.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
#include <atomic>
#include <chrono>
#include <fstream>
#include <iostream>
#include <signal.h>
#include <sys/types.h>
#include <thread>
#include <unistd.h>
using namespace std;
std::atomic<bool> flagRunning{true};
fstream fnLog;
static void sigCallbackHandler(int signum)
{
switch(signum) {
case SIGINT:
{
cout << "\n*******************\n* SIGINT received *\n*******************\n";
cout << "Ctrl+C disabled";
flagRunning=true;
break;
}
case SIGQUIT:
{
cout << "\n*******************\n* SIGQUIT received *\n*******************\n";
flagRunning=false;
break;
}
case SIGTERM:
{
cout << "\n*******************\n* SIGTERM received *\n*******************\n";
flagRunning=false;
break;
}
case SIGHUP:
{
cout << "\n*******************\n* SIGHUP received *\n*******************\n";
cout << "Ow, warning the terminal died but I'm alive";
flagRunning=true;
break;
}
case SIGUSR1:
{
cout << "\n*******************\n* SIGUSR1 received *\n*******************\n";
cout << "OK, check update";
flagRunning=true;
break;
}
}
}
int main(void)
{
struct sigaction action;
cout << "PID: " << getpid() << endl;
// Redirect cout to file /tmp/output.log
fnLog.open("/tmp/output.log", ios::out);
streambuf* stream_buffer_file = fnLog.rdbuf();
cout.rdbuf(stream_buffer_file);
// Configuring signal Unix
action.sa_handler = sigCallbackHandler;
action.sa_flags = 0;
sigemptyset (&action.sa_mask);
// SIGINT - Ctrl+C
sigaction (SIGINT, &action, NULL);
// SIGTERM - Finish Application immediately
sigaction (SIGTERM, &action, NULL);
// SIGQUIT - Close Application
sigaction (SIGQUIT, &action, NULL);
// SIGHUP - Terminal disconnect
sigaction (SIGHUP, &action, NULL);
// SIGUSR1 - Custom Signal
sigaction (SIGUSR1, &action, NULL);
cout << "Go Loop\n" << flush;
while(flagRunning) {
cout << "." << flush;
this_thread::sleep_for(chrono::milliseconds(500));
}
cout << "\nMuhhh, by..." << endl;
fnLog.close();
return EXIT_SUCCESS;
}