sig_handlers.c 1.96 KB
Newer Older
Warren Dukes's avatar
Warren Dukes committed
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
Warren Dukes's avatar
Warren Dukes committed
3
 * (c) 2004 Nick Welch (mack@incise.org)
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "sig_handlers.h"
21 22 23

#ifndef WIN32

24
#include "log.h"
25
#include "main.h"
26
#include "event_pipe.h"
27 28

#include <glib.h>
29

30 31
#include <sys/signal.h>
#include <errno.h>
32
#include <string.h>
33

34
static void exit_signal_handler(G_GNUC_UNUSED int signum)
Avuton Olrich's avatar
Avuton Olrich committed
35
{
36 37
	g_main_loop_quit(main_loop);
}
38

39 40 41 42 43
static void reload_signal_handler(G_GNUC_UNUSED int signum)
{
	event_pipe_emit_fast(PIPE_EVENT_RELOAD);
}

44 45 46 47 48 49 50
static void
x_sigaction(int signum, const struct sigaction *act)
{
	if (sigaction(signum, act, NULL) < 0)
		g_error("sigaction() failed: %s", strerror(errno));
}

51 52
static void
handle_reload_event(void)
53
{
54 55
	DEBUG("got SIGHUP, rereading DB\n");
	cycle_log_files();
56 57
}

58 59
#endif

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

	sa.sa_flags = 0;
66
	sigemptyset(&sa.sa_mask);
Warren Dukes's avatar
Warren Dukes committed
67
	sa.sa_handler = SIG_IGN;
Avuton Olrich's avatar
Avuton Olrich committed
68
	while (sigaction(SIGPIPE, &sa, NULL) < 0 && errno == EINTR) ;
69 70 71 72 73

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

74 75 76
	event_pipe_register(PIPE_EVENT_RELOAD, handle_reload_event);
	sa.sa_handler = reload_signal_handler;
	x_sigaction(SIGHUP, &sa);
77
#endif
Warren Dukes's avatar
Warren Dukes committed
78
}