sig_handlers.c 3.34 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 31
#include <sys/signal.h>
#include <sys/wait.h>
#include <errno.h>

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

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

50
	return 0;
51 52
}

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

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

	sa.sa_flags = 0;
73
	sigemptyset(&sa.sa_mask);
Warren Dukes's avatar
Warren Dukes committed
74
	sa.sa_handler = SIG_IGN;
Avuton Olrich's avatar
Avuton Olrich committed
75
	while (sigaction(SIGPIPE, &sa, NULL) < 0 && errno == EINTR) ;
76
	sa.sa_handler = chldSigHandler;
Avuton Olrich's avatar
Avuton Olrich committed
77 78 79 80 81
	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
82 83
}

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

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

	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);
	sa.sa_handler = SIG_IGN;
99
	sa.sa_sigaction = NULL;
Avuton Olrich's avatar
Avuton Olrich committed
100 101 102 103 104 105
	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) ;
106 107
}

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

	sigemptyset(&sset);
Avuton Olrich's avatar
Avuton Olrich committed
113 114 115 116 117 118
	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
119 120
}

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

	sigemptyset(&sset);
Avuton Olrich's avatar
Avuton Olrich committed
126 127 128 129 130 131
	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
132
}