SignalHandlers.cxx 1.94 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "SignalHandlers.hxx"
21
#include "event/SignalMonitor.hxx"
22

23
#ifndef _WIN32
24

25
#include "Log.hxx"
26
#include "LogInit.hxx"
27
#include "event/Loop.hxx"
28
#include "system/Error.hxx"
29
#include "util/Domain.hxx"
30

31
#include <signal.h>
32

33 34
static constexpr Domain signal_handlers_domain("signal_handlers");

35
static void
36
HandleShutdownSignal(void *ctx) noexcept
37
{
38 39
	auto &loop = *(EventLoop *)ctx;
	loop.Break();
40 41
}

42 43 44
static void
x_sigaction(int signum, const struct sigaction *act)
{
45
	if (sigaction(signum, act, nullptr) < 0)
46
		throw MakeErrno("sigaction() failed");
47 48
}

49
static void
50
handle_reload_event(void *) noexcept
51
{
52
	LogDebug(signal_handlers_domain, "got SIGHUP, reopening log files");
53
	cycle_log_files();
54 55
}

56 57
#endif

58 59
void
SignalHandlersInit(EventLoop &loop)
Avuton Olrich's avatar
Avuton Olrich committed
60
{
61 62
	SignalMonitorInit(loop);

63
#ifndef _WIN32
Warren Dukes's avatar
Warren Dukes committed
64 65 66
	struct sigaction sa;

	sa.sa_flags = 0;
67
	sigemptyset(&sa.sa_mask);
Warren Dukes's avatar
Warren Dukes committed
68
	sa.sa_handler = SIG_IGN;
69
	x_sigaction(SIGPIPE, &sa);
70

71 72
	SignalMonitorRegister(SIGINT, {&loop, HandleShutdownSignal});
	SignalMonitorRegister(SIGTERM, {&loop, HandleShutdownSignal});
73

74
	SignalMonitorRegister(SIGHUP, {nullptr, handle_reload_event});
75
#endif
Warren Dukes's avatar
Warren Dukes committed
76
}
77 78

void
79
SignalHandlersFinish() noexcept
80 81 82
{
	SignalMonitorFinish();
}