SignalHandlers.cxx 1.93 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "config.h"
21
#include "SignalHandlers.hxx"
22
#include "event/SignalMonitor.hxx"
23

24
#ifndef _WIN32
25

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

32
#include <signal.h>
33

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

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

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

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

57 58
#endif

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

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

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

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

75
	SignalMonitorRegister(SIGHUP, {nullptr, handle_reload_event});
76
#endif
Warren Dukes's avatar
Warren Dukes committed
77
}
78 79 80 81 82 83

void
SignalHandlersFinish()
{
	SignalMonitorFinish();
}