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
|
//------------------------------------------------------------------------------
// Author: Pavel Karneliuk
// Description: Handling signals and map them to exceptions.
// Copyright (c) 2013 EPAM Systems
//------------------------------------------------------------------------------
/*
This file is part of Nfstrace.
Nfstrace is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 2 of the License.
Nfstrace is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Nfstrace. If not, see <http://www.gnu.org/licenses/>.
*/
//------------------------------------------------------------------------------
#include <cerrno>
#include <functional> // std::ref
#include <system_error>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <string.h> // for strsignal()
#include <sys/wait.h>
#include "controller/signal_handler.h"
//------------------------------------------------------------------------------
namespace NST
{
namespace controller
{
SignalHandler::Signal::Signal(int sig)
: std::runtime_error{::strsignal(sig)}
, signal_number{sig}
{
}
// synchronously wait signals and pass them to RunningStatus as an exception
static void handle_signals(const sigset_t waitmask,
std::atomic_flag& running,
RunningStatus& status)
{
while(running.test_and_set())
{
int signo {0};
const int err {::sigwait(&waitmask, &signo)};
if(err != 0)
{
status.push(std::system_error{err, std::system_category(),
"error in SignalHandler sigwait"});
return;
}
if(signo == SIGCHLD)
{
// wait childern(compression in dumping mode may call fork())
const pid_t pid {::wait(nullptr)};
if(pid == -1 && errno != ECHILD)
{
status.push(std::system_error{errno, std::system_category(),
"error in SignalHandler wait"});
}
}
else if(signo == SIGINT)
{
status.push(ProcessingDone{"Interrupted by user."});
}
else
{
status.push(SignalHandler::Signal{signo});
}
}
}
static void dummy(int) {}
SignalHandler::SignalHandler(RunningStatus& s)
: handler{}
, running{ATOMIC_FLAG_INIT} // false
{
// set dummy handler for SIGCHLD to prevent ignoring it
// in ::sigwait() on FreeBSD by default
struct sigaction chld;
memset(&chld, 0, sizeof(chld));
chld.sa_handler = dummy;
if(::sigaction(SIGCHLD, &chld, nullptr) != 0)
{
throw std::system_error(errno, std::system_category(),
"error in SignalHandler sigaction");
}
sigset_t mask;
::sigemptyset(&mask);
::sigaddset(&mask, SIGINT); // correct exit from program by Ctrl-C
::sigaddset(&mask, SIGCHLD); // stop sigwait-thread and wait children
::sigaddset(&mask, SIGHUP); // signal for losing terminal
const int err = ::pthread_sigmask(SIG_BLOCK, &mask, nullptr);
if(err != 0)
{
throw std::system_error(err, std::system_category(),
"error in SignalHandler pthread_sigmask");
}
running.test_and_set();
handler = std::thread{handle_signals, mask, std::ref(running), std::ref(s)};
}
SignalHandler::~SignalHandler()
{
running.clear();
// send signal to stop handler thread execution via unblock sigwait()
::pthread_kill(handler.native_handle(), SIGCHLD);
handler.join();
}
} // namespace controller
} // namespace NST
//------------------------------------------------------------------------------
|