sig_handlers.c 4.05 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 "player.h"
22
#include "playerData.h"
Warren Dukes's avatar
Warren Dukes committed
23
#include "playlist.h"
24
#include "directory.h"
25 26 27
#include "command.h"
#include "signal_check.h"
#include "log.h"
28
#include "player.h"
29
#include "decode.h"
Warren Dukes's avatar
Warren Dukes committed
30 31

#include <signal.h>
32 33 34 35
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
36
#include <errno.h>
37 38
#include <unistd.h>

Eric Wong's avatar
Eric Wong committed
39
int handlePendingSignals(void)
Avuton Olrich's avatar
Avuton Olrich committed
40 41 42
{
	if (signal_is_pending(SIGINT) || signal_is_pending(SIGTERM)) {
		DEBUG("main process got SIGINT or SIGTERM, exiting\n");
43
		return COMMAND_RETURN_KILL;
Avuton Olrich's avatar
Avuton Olrich committed
44
	}
45

Avuton Olrich's avatar
Avuton Olrich committed
46 47
	if (signal_is_pending(SIGHUP)) {
		DEBUG("got SIGHUP, rereading DB\n");
48
		signal_clear(SIGHUP);
Avuton Olrich's avatar
Avuton Olrich committed
49 50 51 52
		if (!isUpdatingDB()) {
			readDirectoryDB();
			playlistVersionChange();
		}
Eric Wong's avatar
Eric Wong committed
53
		if (cycle_log_files() < 0)
Avuton Olrich's avatar
Avuton Olrich committed
54 55
			return COMMAND_RETURN_KILL;
		playerCycleLogFiles();
Warren Dukes's avatar
Warren Dukes committed
56 57
	}

58
	return 0;
59 60
}

61
static void chldSigHandler(int signal)
Avuton Olrich's avatar
Avuton Olrich committed
62
{
63
	int status;
64
	int pid;
65
	/* DEBUG("main process got SIGCHLD\n"); */
Avuton Olrich's avatar
Avuton Olrich committed
66 67 68 69 70 71
	while (0 != (pid = wait3(&status, WNOHANG, NULL))) {
		if (pid < 0) {
			if (errno == EINTR)
				continue;
			else
				break;
72
		}
Avuton Olrich's avatar
Avuton Olrich committed
73
		player_sigChldHandler(pid, status);
74
		directory_sigChldHandler(pid, status);
75 76 77
	}
}

Eric Wong's avatar
Eric Wong committed
78
void initSigHandlers(void)
Avuton Olrich's avatar
Avuton Olrich committed
79
{
Warren Dukes's avatar
Warren Dukes committed
80 81 82
	struct sigaction sa;

	sa.sa_flags = 0;
83
	sigemptyset(&sa.sa_mask);
Warren Dukes's avatar
Warren Dukes committed
84
	sa.sa_handler = SIG_IGN;
Avuton Olrich's avatar
Avuton Olrich committed
85
	while (sigaction(SIGPIPE, &sa, NULL) < 0 && errno == EINTR) ;
86
	sa.sa_handler = chldSigHandler;
Avuton Olrich's avatar
Avuton Olrich committed
87 88 89 90 91
	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
92 93
}

Eric Wong's avatar
Eric Wong committed
94
void finishSigHandlers(void)
Avuton Olrich's avatar
Avuton Olrich committed
95 96 97 98 99
{
	signal_unhandle(SIGINT);
	signal_unhandle(SIGUSR1);
	signal_unhandle(SIGTERM);
	signal_unhandle(SIGHUP);
Warren Dukes's avatar
Warren Dukes committed
100 101
}

Eric Wong's avatar
Eric Wong committed
102
void setSigHandlersForDecoder(void)
Avuton Olrich's avatar
Avuton Olrich committed
103
{
104 105 106 107 108
	struct sigaction sa;

	finishSigHandlers();

	sa.sa_flags = 0;
109
	sigemptyset(&sa.sa_mask);
110
	sa.sa_handler = SIG_IGN;
Avuton Olrich's avatar
Avuton Olrich committed
111 112
	while (sigaction(SIGHUP, &sa, NULL) < 0 && errno == EINTR) ;
	while (sigaction(SIGINT, &sa, NULL) < 0 && errno == EINTR) ;
113 114
	sa.sa_flags = SA_SIGINFO;
	sa.sa_sigaction = decodeSigHandler;
Avuton Olrich's avatar
Avuton Olrich committed
115 116
	while (sigaction(SIGCHLD, &sa, NULL) < 0 && errno == EINTR) ;
	while (sigaction(SIGTERM, &sa, NULL) < 0 && errno == EINTR) ;
117 118
}

Eric Wong's avatar
Eric Wong committed
119
void ignoreSignals(void)
Avuton Olrich's avatar
Avuton Olrich committed
120
{
121 122 123 124 125
	struct sigaction sa;

	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);
	sa.sa_handler = SIG_IGN;
126
	sa.sa_sigaction = NULL;
Avuton Olrich's avatar
Avuton Olrich committed
127 128 129 130 131 132
	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) ;
133 134
}

Eric Wong's avatar
Eric Wong committed
135
void blockSignals(void)
Avuton Olrich's avatar
Avuton Olrich committed
136
{
Warren Dukes's avatar
Warren Dukes committed
137 138 139
	sigset_t sset;

	sigemptyset(&sset);
Avuton Olrich's avatar
Avuton Olrich committed
140 141 142 143 144 145
	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
146 147
}

Eric Wong's avatar
Eric Wong committed
148
void unblockSignals(void)
Avuton Olrich's avatar
Avuton Olrich committed
149
{
Warren Dukes's avatar
Warren Dukes committed
150 151 152
	sigset_t sset;

	sigemptyset(&sset);
Avuton Olrich's avatar
Avuton Olrich committed
153 154 155 156 157 158
	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
159
}