SignalMonitor.cxx 4.27 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * http://www.musicpd.org
 *
 * This program 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "SignalMonitor.hxx"
21
#include "config.h"
22

23
#ifndef _WIN32
24 25 26

#include "SocketMonitor.hxx"
#include "util/Manual.hxx"
27
#include "system/Error.hxx"
28

29 30 31 32 33 34 35
#ifdef USE_SIGNALFD
#include "system/SignalFD.hxx"
#else
#include "WakeFD.hxx"
#endif

#ifndef USE_SIGNALFD
36
#include <atomic>
37 38
#endif

39
#include <algorithm>
40
#include <cassert>
41
#include <csignal>
42

43 44 45 46
#ifdef USE_SIGNALFD
#include <pthread.h>
#endif

47
class SignalMonitor final : private SocketMonitor {
48 49 50
#ifdef USE_SIGNALFD
	SignalFD fd;
#else
51
	WakeFD fd;
52
#endif
53 54

public:
Max Kellermann's avatar
Max Kellermann committed
55
	explicit SignalMonitor(EventLoop &_loop)
56
		:SocketMonitor(_loop) {
57
#ifndef USE_SIGNALFD
58
		SocketMonitor::Open(SocketDescriptor(fd.Get()));
59
		SocketMonitor::ScheduleRead();
60
#endif
61 62
	}

63 64
	using SocketMonitor::GetEventLoop;

65
#ifdef USE_SIGNALFD
66
	void Update(sigset_t &mask) noexcept {
67 68 69 70 71
		const bool was_open = SocketMonitor::IsDefined();

		fd.Create(mask);

		if (!was_open) {
72
			SocketMonitor::Open(SocketDescriptor(fd.Get()));
73 74 75 76
			SocketMonitor::ScheduleRead();
		}
	}
#else
77
	void WakeUp() noexcept {
78 79
		fd.Write();
	}
80
#endif
81 82

private:
83
	bool OnSocketReady(unsigned flags) noexcept override;
84 85 86 87 88 89
};

/* this should be enough - is it? */
static constexpr unsigned MAX_SIGNAL = 64;

static SignalHandler signal_handlers[MAX_SIGNAL];
90 91 92 93

#ifdef USE_SIGNALFD
static sigset_t signal_mask;
#else
94
static std::atomic_bool signal_pending[MAX_SIGNAL];
95
#endif
96 97 98

static Manual<SignalMonitor> monitor;

99 100 101 102 103 104 105 106
#ifdef USE_SIGNALFD

/**
 * This is a pthread_atfork() callback that unblocks the signals that
 * were blocked for our signalfd().  Without this, our child processes
 * would inherit the blocked signals.
 */
static void
107
at_fork_child() noexcept
108 109 110 111 112 113
{
	sigprocmask(SIG_UNBLOCK, &signal_mask, nullptr);
}

#else

114
static void
115
SignalCallback(int signo) noexcept
116
{
117
	assert(signal_handlers[signo]);
118 119 120 121

	if (!signal_pending[signo].exchange(true))
		monitor->WakeUp();
}
122

123
#endif
124 125 126 127

void
SignalMonitorInit(EventLoop &loop)
{
128 129
#ifdef USE_SIGNALFD
	sigemptyset(&signal_mask);
130 131

	pthread_atfork(nullptr, nullptr, at_fork_child);
132 133
#endif

134 135 136
	monitor.Construct(loop);
}

137 138
#ifndef USE_SIGNALFD

139 140 141 142
static void
x_sigaction(int signum, const struct sigaction &act)
{
	if (sigaction(signum, &act, nullptr) < 0)
143
		throw MakeErrno("sigaction() failed");
144 145
}

146 147
#endif

148
void
149
SignalMonitorFinish() noexcept
150
{
151 152 153
#ifdef USE_SIGNALFD
	std::fill_n(signal_handlers, MAX_SIGNAL, nullptr);
#else
154 155 156 157 158 159
	struct sigaction sa;
	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);
	sa.sa_handler = SIG_DFL;

	for (unsigned i = 0; i < MAX_SIGNAL; ++i) {
160
		if (signal_handlers[i]) {
161
			sigaction(i, &sa, nullptr);
162 163 164 165 166
			signal_handlers[i] = nullptr;
		}
	}

	std::fill_n(signal_pending, MAX_SIGNAL, false);
167
#endif
168 169 170 171 172 173 174

	monitor.Destruct();
}

void
SignalMonitorRegister(int signo, SignalHandler handler)
{
175
	assert(!signal_handlers[signo]);
176
#ifndef USE_SIGNALFD
177
	assert(!signal_pending[signo]);
178
#endif
179 180 181

	signal_handlers[signo] = handler;

182 183 184 185
#ifdef USE_SIGNALFD
	sigaddset(&signal_mask, signo);

	if (sigprocmask(SIG_BLOCK, &signal_mask, nullptr) < 0)
186
		throw MakeErrno("sigprocmask() failed");
187 188 189

	monitor->Update(signal_mask);
#else
190 191 192 193 194
	struct sigaction sa;
	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);
	sa.sa_handler = SignalCallback;
	x_sigaction(signo, sa);
195
#endif
196 197 198
}

bool
199
SignalMonitor::OnSocketReady(unsigned) noexcept
200
{
201 202 203 204
#ifdef USE_SIGNALFD
	int signo;
	while ((signo = fd.Read()) >= 0) {
		assert(unsigned(signo) < MAX_SIGNAL);
205
		assert(signal_handlers[signo]);
206 207 208 209

		signal_handlers[signo]();
	}
#else
210 211 212 213 214
	fd.Read();

	for (unsigned i = 0; i < MAX_SIGNAL; ++i)
		if (signal_pending[i].exchange(false))
			signal_handlers[i]();
215
#endif
216 217 218 219 220

	return true;
}

#endif