From e0a617fcada616a8112f7d9df51ac10bd58130af Mon Sep 17 00:00:00 2001
From: Mattias Andrée <maandree@operamail.com>
Date: Thu, 7 May 2015 17:25:16 +0200
Subject: Move signal processing functions and sigaction calls to signals from
 redshift.c
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Mattias Andrée <maandree@operamail.com>
---
 src/signals.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 107 insertions(+)
 create mode 100644 src/signals.c

(limited to 'src/signals.c')

diff --git a/src/signals.c b/src/signals.c
new file mode 100644
index 0000000..3dac82c
--- /dev/null
+++ b/src/signals.c
@@ -0,0 +1,107 @@
+/* signals.c -- Signal processing source
+   This file is part of Redshift.
+
+   Redshift 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 3 of the License, or
+   (at your option) any later version.
+
+   Redshift 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 Redshift.  If not, see <http://www.gnu.org/licenses/>.
+
+   Copyright (c) 2009-2015  Jon Lund Steffensen <jonlst@gmail.com>
+   Copyright (c) 2015  Mattias Andrée <maandree@member.fsf.org>
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#if defined(HAVE_SIGNAL_H) && !defined(__WIN32__)
+# include <signal.h>
+#endif
+
+#include "signals.h"
+
+
+#if defined(HAVE_SIGNAL_H) && !defined(__WIN32__)
+
+volatile sig_atomic_t exiting = 0;
+volatile sig_atomic_t disable = 0;
+
+
+/* Signal handler for exit signals */
+static void
+sigexit(int signo)
+{
+	exiting = 1;
+}
+
+/* Signal handler for disable signal */
+static void
+sigdisable(int signo)
+{
+	disable = 1;
+}
+
+#endif /* ! HAVE_SIGNAL_H || __WIN32__ */
+
+
+int
+signals_install_handlers(void)
+{
+#if defined(HAVE_SIGNAL_H) && !defined(__WIN32__)
+	struct sigaction sigact;
+	sigset_t sigset;
+	int r;
+	sigemptyset(&sigset);
+
+	/* Install signal handler for INT and TERM signals */
+	sigact.sa_handler = sigexit;
+	sigact.sa_mask = sigset;
+	sigact.sa_flags = 0;
+
+	r = sigaction(SIGINT, &sigact, NULL);
+	if (r < 0) {
+		perror("sigaction");
+		return -1;
+	}
+
+	r = sigaction(SIGTERM, &sigact, NULL);
+	if (r < 0) {
+		perror("sigaction");
+		return -1;
+	}
+
+	/* Install signal handler for USR1 signal */
+	sigact.sa_handler = sigdisable;
+	sigact.sa_mask = sigset;
+	sigact.sa_flags = 0;
+
+	r = sigaction(SIGUSR1, &sigact, NULL);
+	if (r < 0) {
+		perror("sigaction");
+		return -1;
+	}
+
+	/* Ignore CHLD signal. This causes child processes
+	   (hooks) to be reaped automatically. */
+	sigact.sa_handler = SIG_IGN;
+	sigact.sa_mask = sigset;
+	sigact.sa_flags = 0;
+
+	r = sigaction(SIGCHLD, &sigact, NULL);
+	if (r < 0) {
+		perror("sigaction");
+		return -1;
+	}
+#endif /* HAVE_SIGNAL_H && ! __WIN32__ */
+
+	return 0;
+}
-- 
cgit v1.2.3-70-g09d2