Update to 6762
[qball-mpd.git] / src / sig_handlers.c
blobfc29d2522080a1629cbfeda827e6b8fa21b9a1d6
1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 * (c) 2004 Nick Welch (mack@incise.org)
4 * This project's homepage is: http://www.musicpd.org
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "sig_handlers.h"
21 #include "player.h"
22 #include "playerData.h"
23 #include "playlist.h"
24 #include "directory.h"
25 #include "command.h"
26 #include "signal_check.h"
27 #include "log.h"
28 #include "player.h"
29 #include "decode.h"
31 #include <signal.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <sys/resource.h>
35 #include <sys/wait.h>
36 #include <errno.h>
37 #include <unistd.h>
39 int handlePendingSignals(void)
41 if (signal_is_pending(SIGINT) || signal_is_pending(SIGTERM)) {
42 DEBUG("main process got SIGINT or SIGTERM, exiting\n");
43 return COMMAND_RETURN_KILL;
46 if (signal_is_pending(SIGHUP)) {
47 DEBUG("got SIGHUP, rereading DB\n");
48 signal_clear(SIGHUP);
49 if (!isUpdatingDB()) {
50 readDirectoryDB();
51 playlistVersionChange();
53 if (cycle_log_files() < 0)
54 return COMMAND_RETURN_KILL;
55 playerCycleLogFiles();
58 return 0;
61 static void chldSigHandler(int signal)
63 int status;
64 int pid;
65 DEBUG("main process got SIGCHLD\n");
66 while (0 != (pid = wait3(&status, WNOHANG, NULL))) {
67 if (pid < 0) {
68 if (errno == EINTR)
69 continue;
70 else
71 break;
73 player_sigChldHandler(pid, status);
74 directory_sigChldHandler(pid, status);
78 void initSigHandlers(void)
80 struct sigaction sa;
82 sa.sa_flags = 0;
83 sigemptyset(&sa.sa_mask);
84 sa.sa_handler = SIG_IGN;
85 while (sigaction(SIGPIPE, &sa, NULL) < 0 && errno == EINTR) ;
86 sa.sa_handler = chldSigHandler;
87 while (sigaction(SIGCHLD, &sa, NULL) < 0 && errno == EINTR) ;
88 signal_handle(SIGUSR1);
89 signal_handle(SIGINT);
90 signal_handle(SIGTERM);
91 signal_handle(SIGHUP);
94 void finishSigHandlers(void)
96 signal_unhandle(SIGINT);
97 signal_unhandle(SIGUSR1);
98 signal_unhandle(SIGTERM);
99 signal_unhandle(SIGHUP);
102 void setSigHandlersForDecoder(void)
104 struct sigaction sa;
106 finishSigHandlers();
108 sa.sa_flags = 0;
109 sigemptyset(&sa.sa_mask);
110 sa.sa_handler = SIG_IGN;
111 while (sigaction(SIGHUP, &sa, NULL) < 0 && errno == EINTR) ;
112 while (sigaction(SIGINT, &sa, NULL) < 0 && errno == EINTR) ;
113 sa.sa_flags = SA_SIGINFO;
114 sa.sa_sigaction = decodeSigHandler;
115 while (sigaction(SIGCHLD, &sa, NULL) < 0 && errno == EINTR) ;
116 while (sigaction(SIGTERM, &sa, NULL) < 0 && errno == EINTR) ;
119 void ignoreSignals(void)
121 struct sigaction sa;
123 sa.sa_flags = 0;
124 sigemptyset(&sa.sa_mask);
125 sa.sa_handler = SIG_IGN;
126 sa.sa_sigaction = NULL;
127 while (sigaction(SIGPIPE, &sa, NULL) < 0 && errno == EINTR) ;
128 while (sigaction(SIGCHLD, &sa, NULL) < 0 && errno == EINTR) ;
129 while (sigaction(SIGUSR1, &sa, NULL) < 0 && errno == EINTR) ;
130 while (sigaction(SIGINT, &sa, NULL) < 0 && errno == EINTR) ;
131 while (sigaction(SIGTERM, &sa, NULL) < 0 && errno == EINTR) ;
132 while (sigaction(SIGHUP, &sa, NULL) < 0 && errno == EINTR) ;
135 void blockSignals(void)
137 sigset_t sset;
139 sigemptyset(&sset);
140 sigaddset(&sset, SIGCHLD);
141 sigaddset(&sset, SIGUSR1);
142 sigaddset(&sset, SIGHUP);
143 sigaddset(&sset, SIGINT);
144 sigaddset(&sset, SIGTERM);
145 while (sigprocmask(SIG_BLOCK, &sset, NULL) < 0 && errno == EINTR) ;
148 void unblockSignals(void)
150 sigset_t sset;
152 sigemptyset(&sset);
153 sigaddset(&sset, SIGCHLD);
154 sigaddset(&sset, SIGUSR1);
155 sigaddset(&sset, SIGHUP);
156 sigaddset(&sset, SIGINT);
157 sigaddset(&sset, SIGTERM);
158 while (sigprocmask(SIG_UNBLOCK, &sset, NULL) < 0 && errno == EINTR) ;