SignalMonitor.cxx 4.46 KB
Newer Older
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
/*
 * Copyright (C) 2003-2013 The Music Player Daemon Project
 * 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 "config.h"
#include "SignalMonitor.hxx"

#ifndef WIN32

#include "WakeFD.hxx"
#include "SocketMonitor.hxx"
#include "util/Manual.hxx"
#include "system/FatalError.hxx"

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

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

40 41
#include <algorithm>

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

46 47
#include <signal.h>

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

public:
	SignalMonitor(EventLoop &_loop)
		:SocketMonitor(_loop) {
58
#ifndef USE_SIGNALFD
59 60
		SocketMonitor::Open(fd.Get());
		SocketMonitor::ScheduleRead();
61
#endif
62 63 64 65
	}

	~SignalMonitor() {
		/* prevent the descriptor to be closed twice */
66 67 68 69
#ifdef USE_SIGNALFD
		if (SocketMonitor::IsDefined())
#endif
			SocketMonitor::Steal();
70 71
	}

72 73
	using SocketMonitor::GetEventLoop;

74 75 76 77 78 79 80 81 82 83 84 85
#ifdef USE_SIGNALFD
	void Update(sigset_t &mask) {
		const bool was_open = SocketMonitor::IsDefined();

		fd.Create(mask);

		if (!was_open) {
			SocketMonitor::Open(fd.Get());
			SocketMonitor::ScheduleRead();
		}
	}
#else
86 87 88
	void WakeUp() {
		fd.Write();
	}
89
#endif
90 91 92 93 94 95 96 97 98

private:
	virtual bool OnSocketReady(unsigned flags) override;
};

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

static SignalHandler signal_handlers[MAX_SIGNAL];
99 100 101 102

#ifdef USE_SIGNALFD
static sigset_t signal_mask;
#else
103
static std::atomic_bool signal_pending[MAX_SIGNAL];
104
#endif
105 106 107

static Manual<SignalMonitor> monitor;

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#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
at_fork_child()
{
	sigprocmask(SIG_UNBLOCK, &signal_mask, nullptr);
}

#else

123 124 125 126 127 128 129 130
static void
SignalCallback(int signo)
{
	assert(signal_handlers[signo] != nullptr);

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

132
#endif
133 134 135 136

void
SignalMonitorInit(EventLoop &loop)
{
137 138
#ifdef USE_SIGNALFD
	sigemptyset(&signal_mask);
139 140

	pthread_atfork(nullptr, nullptr, at_fork_child);
141 142
#endif

143 144 145
	monitor.Construct(loop);
}

146 147
#ifndef USE_SIGNALFD

148 149 150 151 152 153 154
static void
x_sigaction(int signum, const struct sigaction &act)
{
	if (sigaction(signum, &act, nullptr) < 0)
		FatalSystemError("sigaction() failed");
}

155 156
#endif

157 158 159
void
SignalMonitorFinish()
{
160 161 162
#ifdef USE_SIGNALFD
	std::fill_n(signal_handlers, MAX_SIGNAL, nullptr);
#else
163 164 165 166 167 168 169 170 171 172 173 174 175
	struct sigaction sa;
	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);
	sa.sa_handler = SIG_DFL;

	for (unsigned i = 0; i < MAX_SIGNAL; ++i) {
		if (signal_handlers[i] != nullptr) {
			x_sigaction(i, sa);
			signal_handlers[i] = nullptr;
		}
	}

	std::fill_n(signal_pending, MAX_SIGNAL, false);
176
#endif
177 178 179 180

	monitor.Destruct();
}

181 182 183 184 185 186
EventLoop &
SignalMonitorGetEventLoop()
{
	return monitor->GetEventLoop();
}

187 188 189 190
void
SignalMonitorRegister(int signo, SignalHandler handler)
{
	assert(signal_handlers[signo] == nullptr);
191
#ifndef USE_SIGNALFD
192
	assert(!signal_pending[signo]);
193
#endif
194 195 196

	signal_handlers[signo] = handler;

197 198 199 200 201 202 203 204
#ifdef USE_SIGNALFD
	sigaddset(&signal_mask, signo);

	if (sigprocmask(SIG_BLOCK, &signal_mask, nullptr) < 0)
		FatalSystemError("sigprocmask() failed");

	monitor->Update(signal_mask);
#else
205 206 207 208 209
	struct sigaction sa;
	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);
	sa.sa_handler = SignalCallback;
	x_sigaction(signo, sa);
210
#endif
211 212 213 214 215
}

bool
SignalMonitor::OnSocketReady(unsigned)
{
216 217 218 219 220 221 222 223 224
#ifdef USE_SIGNALFD
	int signo;
	while ((signo = fd.Read()) >= 0) {
		assert(unsigned(signo) < MAX_SIGNAL);
		assert(signal_handlers[signo] != nullptr);

		signal_handlers[signo]();
	}
#else
225 226 227 228 229
	fd.Read();

	for (unsigned i = 0; i < MAX_SIGNAL; ++i)
		if (signal_pending[i].exchange(false))
			signal_handlers[i]();
230
#endif
231 232 233 234 235

	return true;
}

#endif