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"
22 #include "playerData.h"
24 #include "directory.h"
26 #include "signal_check.h"
32 #include <sys/types.h>
34 #include <sys/resource.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");
49 if (!isUpdatingDB()) {
51 playlistVersionChange();
53 if (cycle_log_files() < 0)
54 return COMMAND_RETURN_KILL
;
55 playerCycleLogFiles();
61 static void chldSigHandler(int signal
)
65 DEBUG("main process got SIGCHLD\n");
66 while (0 != (pid
= wait3(&status
, WNOHANG
, NULL
))) {
73 player_sigChldHandler(pid
, status
);
74 directory_sigChldHandler(pid
, status
);
78 void initSigHandlers(void)
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)
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)
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)
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)
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
) ;