sig_handlers.c 3.36 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 21
 * 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"
#include "playlist.h"
22
#include "database.h"
23
#include "update.h"
24 25 26
#include "command.h"
#include "signal_check.h"
#include "log.h"
27

28 29 30
#include <sys/signal.h>
#include <sys/wait.h>
#include <errno.h>
31
#include <glib.h>
32

Eric Wong's avatar
Eric Wong committed
33
int handlePendingSignals(void)
Avuton Olrich's avatar
Avuton Olrich committed
34 35 36
{
	if (signal_is_pending(SIGINT) || signal_is_pending(SIGTERM)) {
		DEBUG("main process got SIGINT or SIGTERM, exiting\n");
37
		return COMMAND_RETURN_KILL;
Avuton Olrich's avatar
Avuton Olrich committed
38
	}
39

Avuton Olrich's avatar
Avuton Olrich committed
40 41
	if (signal_is_pending(SIGHUP)) {
		DEBUG("got SIGHUP, rereading DB\n");
42
		signal_clear(SIGHUP);
Avuton Olrich's avatar
Avuton Olrich committed
43
		if (!isUpdatingDB()) {
44
			db_load();
Avuton Olrich's avatar
Avuton Olrich committed
45 46
			playlistVersionChange();
		}
Eric Wong's avatar
Eric Wong committed
47
		if (cycle_log_files() < 0)
Avuton Olrich's avatar
Avuton Olrich committed
48
			return COMMAND_RETURN_KILL;
Warren Dukes's avatar
Warren Dukes committed
49 50
	}

51
	return 0;
52 53
}

54
static void chldSigHandler(G_GNUC_UNUSED int sig)
Avuton Olrich's avatar
Avuton Olrich committed
55
{
56
	int status;
57
	int pid;
58
	/* DEBUG("main process got SIGCHLD\n"); */
Avuton Olrich's avatar
Avuton Olrich committed
59 60 61 62 63 64
	while (0 != (pid = wait3(&status, WNOHANG, NULL))) {
		if (pid < 0) {
			if (errno == EINTR)
				continue;
			else
				break;
65
		}
66 67 68
	}
}

Eric Wong's avatar
Eric Wong committed
69
void initSigHandlers(void)
Avuton Olrich's avatar
Avuton Olrich committed
70
{
Warren Dukes's avatar
Warren Dukes committed
71 72 73
	struct sigaction sa;

	sa.sa_flags = 0;
74
	sigemptyset(&sa.sa_mask);
Warren Dukes's avatar
Warren Dukes committed
75
	sa.sa_handler = SIG_IGN;
Avuton Olrich's avatar
Avuton Olrich committed
76
	while (sigaction(SIGPIPE, &sa, NULL) < 0 && errno == EINTR) ;
77
	sa.sa_handler = chldSigHandler;
Avuton Olrich's avatar
Avuton Olrich committed
78 79 80 81 82
	while (sigaction(SIGCHLD, &sa, NULL) < 0 && errno == EINTR) ;
	signal_handle(SIGUSR1);
	signal_handle(SIGINT);
	signal_handle(SIGTERM);
	signal_handle(SIGHUP);
Warren Dukes's avatar
Warren Dukes committed
83 84
}

Eric Wong's avatar
Eric Wong committed
85
void finishSigHandlers(void)
Avuton Olrich's avatar
Avuton Olrich committed
86 87 88 89 90
{
	signal_unhandle(SIGINT);
	signal_unhandle(SIGUSR1);
	signal_unhandle(SIGTERM);
	signal_unhandle(SIGHUP);
Warren Dukes's avatar
Warren Dukes committed
91 92
}

Eric Wong's avatar
Eric Wong committed
93
void ignoreSignals(void)
Avuton Olrich's avatar
Avuton Olrich committed
94
{
95 96 97 98 99
	struct sigaction sa;

	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);
	sa.sa_handler = SIG_IGN;
100
	sa.sa_sigaction = NULL;
Avuton Olrich's avatar
Avuton Olrich committed
101 102 103 104 105 106
	while (sigaction(SIGPIPE, &sa, NULL) < 0 && errno == EINTR) ;
	while (sigaction(SIGCHLD, &sa, NULL) < 0 && errno == EINTR) ;
	while (sigaction(SIGUSR1, &sa, NULL) < 0 && errno == EINTR) ;
	while (sigaction(SIGINT, &sa, NULL) < 0 && errno == EINTR) ;
	while (sigaction(SIGTERM, &sa, NULL) < 0 && errno == EINTR) ;
	while (sigaction(SIGHUP, &sa, NULL) < 0 && errno == EINTR) ;
107 108
}

Eric Wong's avatar
Eric Wong committed
109
void blockSignals(void)
Avuton Olrich's avatar
Avuton Olrich committed
110
{
Warren Dukes's avatar
Warren Dukes committed
111 112 113
	sigset_t sset;

	sigemptyset(&sset);
Avuton Olrich's avatar
Avuton Olrich committed
114 115 116 117 118 119
	sigaddset(&sset, SIGCHLD);
	sigaddset(&sset, SIGUSR1);
	sigaddset(&sset, SIGHUP);
	sigaddset(&sset, SIGINT);
	sigaddset(&sset, SIGTERM);
	while (sigprocmask(SIG_BLOCK, &sset, NULL) < 0 && errno == EINTR) ;
Warren Dukes's avatar
Warren Dukes committed
120 121
}

Eric Wong's avatar
Eric Wong committed
122
void unblockSignals(void)
Avuton Olrich's avatar
Avuton Olrich committed
123
{
Warren Dukes's avatar
Warren Dukes committed
124 125 126
	sigset_t sset;

	sigemptyset(&sset);
Avuton Olrich's avatar
Avuton Olrich committed
127 128 129 130 131 132
	sigaddset(&sset, SIGCHLD);
	sigaddset(&sset, SIGUSR1);
	sigaddset(&sset, SIGHUP);
	sigaddset(&sset, SIGINT);
	sigaddset(&sset, SIGTERM);
	while (sigprocmask(SIG_UNBLOCK, &sset, NULL) < 0 && errno == EINTR) ;
Warren Dukes's avatar
Warren Dukes committed
133
}