SignalMonitor.cxx 4.18 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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"

23
#ifndef _WIN32
24 25 26 27 28

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

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 40
#include <algorithm>

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

45
#include <assert.h>
46
#include <signal.h>
47

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
	using SocketMonitor::GetEventLoop;

66 67 68 69 70 71 72 73 74 75 76 77
#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
78 79 80
	void WakeUp() {
		fd.Write();
	}
81
#endif
82 83 84 85 86 87 88 89 90

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];
91 92 93 94

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

static Manual<SignalMonitor> monitor;

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#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

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

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

124
#endif
125 126 127 128

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

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

135 136 137
	monitor.Construct(loop);
}

138 139
#ifndef USE_SIGNALFD

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

147 148
#endif

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

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

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

	monitor.Destruct();
}

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

	signal_handlers[signo] = handler;

183 184 185 186 187 188 189 190
#ifdef USE_SIGNALFD
	sigaddset(&signal_mask, signo);

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

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

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

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

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

	return true;
}

#endif