signal_check.c 1.55 KB
Newer Older
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * (c)2004 by mackstann
 * 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
 */

20
#include "signal_check.h"
21
#include "os_compat.h"
22

23
static volatile sig_atomic_t __caught_signals[NSIG];
24 25 26

static void __signal_handler(int sig)
{
Avuton Olrich's avatar
Avuton Olrich committed
27
	__caught_signals[sig] = 1;
28 29
}

Avuton Olrich's avatar
Avuton Olrich committed
30
static void __set_signal_handler(int sig, void (*handler) (int))
31
{
Avuton Olrich's avatar
Avuton Olrich committed
32 33 34 35 36
	struct sigaction act;
	act.sa_flags = 0;
	sigemptyset(&act.sa_mask);
	act.sa_handler = handler;
	while (sigaction(sig, &act, NULL) && errno == EINTR) ;
37 38 39 40
}

void signal_handle(int sig)
{
Avuton Olrich's avatar
Avuton Olrich committed
41
	__set_signal_handler(sig, __signal_handler);
42 43 44 45
}

void signal_unhandle(int sig)
{
Avuton Olrich's avatar
Avuton Olrich committed
46 47
	signal_clear(sig);
	__set_signal_handler(sig, SIG_DFL);
48 49 50 51
}

int signal_is_pending(int sig)
{
Avuton Olrich's avatar
Avuton Olrich committed
52
	return __caught_signals[sig];
53 54 55 56
}

void signal_clear(int sig)
{
Avuton Olrich's avatar
Avuton Olrich committed
57
	__caught_signals[sig] = 0;
58
}