OpenVPN 3 Core Library
Loading...
Searching...
No Matches
asiosignal.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// A simple class that allows an arbitrary set of posix signals to be
13// associated with an Asio handler.
14
15#ifndef OPENVPN_ASIO_ASIOSIGNAL_H
16#define OPENVPN_ASIO_ASIOSIGNAL_H
17
18#include <openvpn/io/io.hpp>
19
21#include <openvpn/common/rc.hpp>
22
23namespace openvpn {
24
25class ASIOSignals : public RC<thread_safe_refcount>
26{
27 public:
29
30 ASIOSignals(openvpn_io::io_context &io_context)
31 : halt(false), signals_(io_context)
32 {
33 }
34
35 enum
36 {
37 S_SIGINT = (1 << 0),
38 S_SIGTERM = (1 << 1),
39#ifndef OPENVPN_PLATFORM_WIN
40 S_SIGQUIT = (1 << 2),
41 S_SIGHUP = (1 << 3),
42 S_SIGUSR1 = (1 << 4),
43 S_SIGUSR2 = (1 << 5),
44#endif
45 };
46
47 template <typename SignalHandler>
48 void register_signals(SignalHandler stop_handler, unsigned int sigmask = (S_SIGINT | S_SIGTERM))
49 {
50 if (sigmask & S_SIGINT)
51 signals_.add(SIGINT);
52 if (sigmask & S_SIGTERM)
53 signals_.add(SIGTERM);
54#ifndef OPENVPN_PLATFORM_WIN
55 if (sigmask & S_SIGQUIT)
56 signals_.add(SIGQUIT);
57 if (sigmask & S_SIGHUP)
58 signals_.add(SIGHUP);
59 if (sigmask & S_SIGUSR1)
60 signals_.add(SIGUSR1);
61 if (sigmask & S_SIGUSR2)
62 signals_.add(SIGUSR2);
63#endif
64 signals_.async_wait(stop_handler);
65 }
66
67 template <typename SignalHandler>
68 void register_signals_all(SignalHandler stop_handler)
69 {
70 register_signals(stop_handler,
72 | S_SIGTERM
73#ifndef OPENVPN_PLATFORM_WIN
74 | S_SIGHUP
75 | S_SIGUSR1
76 | S_SIGUSR2
77#endif
78 );
79 }
80
81 void cancel()
82 {
83 if (!halt)
84 {
85 halt = true;
86 signals_.cancel();
87 }
88 }
89
90 private:
91 bool halt;
92 openvpn_io::signal_set signals_;
93};
94
95} // namespace openvpn
96
97#endif
void register_signals(SignalHandler stop_handler, unsigned int sigmask=(S_SIGINT|S_SIGTERM))
void register_signals_all(SignalHandler stop_handler)
ASIOSignals(openvpn_io::io_context &io_context)
RCPtr< ASIOSignals > Ptr
openvpn_io::signal_set signals_
The smart pointer class.
Definition rc.hpp:119
Reference count base class for objects tracked by RCPtr. Disallows copying and assignment.
Definition rc.hpp:912