File: signal.h

package info (click to toggle)
anbox 0.0~git20190124-1
  • links: PTS, VCS
  • area: contrib
  • in suites: buster
  • size: 22,000 kB
  • sloc: cpp: 76,190; ansic: 7,434; sh: 1,350; xml: 818; java: 780; python: 390; makefile: 35; lisp: 7
file content (117 lines) | stat: -rw-r--r-- 2,967 bytes parent folder | download | duplicates (7)
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
/*
 * Copyright © 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */

#ifndef CORE_POSIX_SIGNAL_H_
#define CORE_POSIX_SIGNAL_H_

#include <core/posix/visibility.h>

#include <core/signal.h>

#include <signal.h>

#include <initializer_list>
#include <memory>

namespace core
{
namespace posix
{
/**
 * @brief The Signal enum collects the most common POSIX signals.
 */
enum class Signal
{
    unknown = 0,
    sig_hup = SIGHUP,
    sig_int = SIGINT,
    sig_quit = SIGQUIT,
    sig_ill = SIGILL,
    sig_abrt = SIGABRT,
    sig_fpe = SIGFPE,
    sig_kill = SIGKILL,
    sig_segv = SIGSEGV,
    sig_pipe = SIGPIPE,
    sig_alrm = SIGALRM,
    sig_term = SIGTERM,
    sig_usr1 = SIGUSR1,
    sig_usr2 = SIGUSR2,
    sig_chld = SIGCHLD,
    sig_cont = SIGCONT,
    sig_stop = SIGSTOP,
    sig_tstp = SIGTSTP,
    sig_ttin = SIGTTIN,
    sig_ttou = SIGTTOU
};

/**
 * @brief The SignalTrap class encapsulates functionality to trap and handle signals.
 */
class CORE_POSIX_DLL_PUBLIC SignalTrap
{
public:
    SignalTrap(const SignalTrap&) = delete;
    virtual ~SignalTrap() = default;

    SignalTrap& operator=(const SignalTrap&) = delete;
    bool operator==(const SignalTrap&) const = delete;

    /**
     * @brief Returns true if the given signal is trapped by this instance.
     */
    virtual bool has(Signal signal) = 0;

    /**
     * @brief Starts observation of incoming signals, relaying them via
     * signal_raised(). The call blocks until stop is called.
     */
    virtual void run() = 0;

    /**
     * @brief Stops execution of the signal trap.
     */
    virtual void stop() = 0;

    /**
     * @brief Emitted whenever a trapped signal is raised by the operating system.
     */
    virtual core::Signal<Signal>& signal_raised() = 0;

protected:
    SignalTrap() = default;
};

/**
  * @brief Traps the specified signals for the entire process.
  */
CORE_POSIX_DLL_PUBLIC
std::shared_ptr<SignalTrap> trap_signals_for_process(
        std::initializer_list<core::posix::Signal> blocked_signals);

/**
  * @brief Traps the specified signals for the current thread, and inherits
  * the respective signal mask to all child-threads.
  */
CORE_POSIX_DLL_PUBLIC
std::shared_ptr<SignalTrap> trap_signals_for_all_subsequent_threads(
        std::initializer_list<core::posix::Signal> blocked_signals);

}
}

#endif