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/Makefile.am |   1 +
 src/redshift.c  |  78 ++++-------------------------------------
 src/signals.c   | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/signals.h   |  38 ++++++++++++++++++++
 4 files changed, 152 insertions(+), 72 deletions(-)
 create mode 100644 src/signals.c
 create mode 100644 src/signals.h

diff --git a/src/Makefile.am b/src/Makefile.am
index c7a5444..318fc2c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -10,6 +10,7 @@ bin_PROGRAMS = redshift
 
 redshift_SOURCES = \
 	redshift.c redshift.h \
+	signals.c signals.h \
 	colorramp.c colorramp.h \
 	config-ini.c config-ini.h \
 	location-manual.c location-manual.h \
diff --git a/src/redshift.c b/src/redshift.c
index defd0a1..bf741bb 100644
--- a/src/redshift.c
+++ b/src/redshift.c
@@ -48,6 +48,7 @@
 #include "solar.h"
 #include "systemtime.h"
 #include "hooks.h"
+#include "signals.h"
 
 /* pause() is not defined on windows platform but is not needed either.
    Use a noop macro instead. */
@@ -319,32 +320,6 @@ static const char *period_names[] = {
 	N_("Transition")
 };
 
-#if defined(HAVE_SIGNAL_H) && !defined(__WIN32__)
-
-static volatile sig_atomic_t exiting = 0;
-static 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;
-}
-
-#else /* ! HAVE_SIGNAL_H || __WIN32__ */
-
-static int exiting = 0;
-static int disable = 0;
-
-#endif /* ! HAVE_SIGNAL_H || __WIN32__ */
-
 
 /* Determine which period we are currently in. */
 static period_t
@@ -817,52 +792,11 @@ run_continual_mode(const location_t *loc,
 	   will be exactly 6500K. */
 	double adjustment_alpha = 1.0;
 
-#if defined(HAVE_SIGNAL_H) && !defined(__WIN32__)
-	struct sigaction sigact;
-	sigset_t sigset;
-	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);
+	r = signals_install_handlers();
 	if (r < 0) {
-		perror("sigaction");
-		return -1;
+		return r;
 	}
 
-	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__ */
-
 	if (verbose) {
 		printf(_("Status: %s\n"), _("Enabled"));
 	}
@@ -1447,9 +1381,9 @@ main(int argc, char *argv[])
 		              stderr);
 		        exit(EXIT_FAILURE);
 		}
-	
+
 		provider->free(&location_state);
-	
+
 		if (verbose) {
 			print_location(&loc);
 
@@ -1470,7 +1404,7 @@ main(int argc, char *argv[])
 		                MIN_LAT, MAX_LAT);
 		        exit(EXIT_FAILURE);
 		}
-	
+
 		/* Longitude */
 		if (loc.lon < MIN_LON || loc.lon > MAX_LON) {
 		        /* TRANSLATORS: Append degree symbols if possible. */
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;
+}
diff --git a/src/signals.h b/src/signals.h
new file mode 100644
index 0000000..36cdaf1
--- /dev/null
+++ b/src/signals.h
@@ -0,0 +1,38 @@
+/* signals.h -- Signal processing header
+   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>
+*/
+#ifndef REDSHIFT_SIGNALS_H
+#define REDSHIFT_SIGNALS_H
+
+
+#if defined(HAVE_SIGNAL_H) && !defined(__WIN32__)
+
+extern volatile sig_atomic_t exiting;
+extern volatile sig_atomic_t disable;
+
+#else /* ! HAVE_SIGNAL_H || __WIN32__ */
+#  define exiting  0
+#  define disable  0
+#endif /* ! HAVE_SIGNAL_H || __WIN32__ */
+
+
+int signals_install_handlers(void);
+
+
+#endif /* REDSHIFT_SIGNALS_H */
-- 
cgit v1.2.3-70-g09d2