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

#ifndef WIN32

25
#include "Log.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "Main.hxx"
27
#include "event/Loop.hxx"
28
#include "GlobalEvents.hxx"
29
#include "mpd_error.h"
30 31

#include <glib.h>
32

33
#include <signal.h>
34
#include <errno.h>
35
#include <string.h>
36

37
static void exit_signal_handler(G_GNUC_UNUSED int signum)
Avuton Olrich's avatar
Avuton Olrich committed
38
{
39
	GlobalEvents::Emit(GlobalEvents::SHUTDOWN);
40
}
41

42 43
static void reload_signal_handler(G_GNUC_UNUSED int signum)
{
44
	GlobalEvents::Emit(GlobalEvents::RELOAD);
45 46
}

47 48 49 50
static void
x_sigaction(int signum, const struct sigaction *act)
{
	if (sigaction(signum, act, NULL) < 0)
51
		MPD_ERROR("sigaction() failed: %s", strerror(errno));
52 53
}

54 55
static void
handle_reload_event(void)
56
{
57
	g_debug("got SIGHUP, reopening log files");
58
	cycle_log_files();
59 60
}

61 62
#endif

Eric Wong's avatar
Eric Wong committed
63
void initSigHandlers(void)
Avuton Olrich's avatar
Avuton Olrich committed
64
{
65
#ifndef WIN32
Warren Dukes's avatar
Warren Dukes committed
66 67 68
	struct sigaction sa;

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

	sa.sa_handler = exit_signal_handler;
	x_sigaction(SIGINT, &sa);
	x_sigaction(SIGTERM, &sa);

77
	GlobalEvents::Register(GlobalEvents::RELOAD, handle_reload_event);
78 79
	sa.sa_handler = reload_signal_handler;
	x_sigaction(SIGHUP, &sa);
80
#endif
Warren Dukes's avatar
Warren Dukes committed
81
}