SignalHandlers.cxx 1.89 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 25

#ifndef WIN32

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

32
#include <signal.h>
33

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

36 37
static void
HandleShutdownSignal()
38
{
39
	SignalMonitorGetEventLoop().Break();
40 41
}

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

49 50
static void
handle_reload_event(void)
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, HandleShutdownSignal);
	SignalMonitorRegister(SIGTERM, HandleShutdownSignal);
73

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

void
SignalHandlersFinish()
{
	SignalMonitorFinish();
}