Blame SOURCES/0005-Replace-error-prone-signal-with-sigaction.patch

47b414
From 83a379cfbd283b387919fe05d44eb4c49e155ad6 Mon Sep 17 00:00:00 2001
47b414
From: Lukasz Florczak <lukasz.florczak@linux.intel.com>
47b414
Date: Mon, 21 Feb 2022 13:05:20 +0100
47b414
Subject: [PATCH 05/12] Replace error prone signal() with sigaction()
47b414
47b414
Up to this date signal() was used which implementation could vary [1].
47b414
Sigaction() call is preferred. This commit introduces replacement
47b414
from signal() to sigaction() by the use of signal_s() wrapper.
47b414
Also remove redundant signal.h header includes.
47b414
47b414
[1] https://man7.org/linux/man-pages/man2/signal.2.html
47b414
47b414
Signed-off-by: Lukasz Florczak <lukasz.florczak@linux.intel.com>
47b414
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
47b414
---
47b414
 Grow.c       |  4 ++--
47b414
 Monitor.c    |  5 +++--
47b414
 managemon.c  |  1 -
47b414
 mdadm.h      | 22 ++++++++++++++++++++++
47b414
 mdmon.c      |  1 -
47b414
 monitor.c    |  1 -
47b414
 probe_roms.c |  6 +++---
47b414
 raid6check.c | 25 +++++++++++++++----------
47b414
 util.c       |  1 -
47b414
 9 files changed, 45 insertions(+), 21 deletions(-)
47b414
47b414
diff --git a/Grow.c b/Grow.c
47b414
index aa72490b..18c5719b 100644
47b414
--- a/Grow.c
47b414
+++ b/Grow.c
47b414
@@ -26,7 +26,6 @@
47b414
 #include	<sys/mman.h>
47b414
 #include	<stddef.h>
47b414
 #include	<stdint.h>
47b414
-#include	<signal.h>
47b414
 #include	<sys/wait.h>
47b414
 
47b414
 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
47b414
@@ -3566,7 +3565,8 @@ started:
47b414
 		fd = -1;
47b414
 	mlockall(MCL_FUTURE);
47b414
 
47b414
-	signal(SIGTERM, catch_term);
47b414
+	if (signal_s(SIGTERM, catch_term) == SIG_ERR)
47b414
+		goto release;
47b414
 
47b414
 	if (st->ss->external) {
47b414
 		/* metadata handler takes it from here */
47b414
diff --git a/Monitor.c b/Monitor.c
47b414
index 30c031a2..c0ab5412 100644
47b414
--- a/Monitor.c
47b414
+++ b/Monitor.c
47b414
@@ -26,7 +26,6 @@
47b414
 #include	"md_p.h"
47b414
 #include	"md_u.h"
47b414
 #include	<sys/wait.h>
47b414
-#include	<signal.h>
47b414
 #include	<limits.h>
47b414
 #include	<syslog.h>
47b414
 #ifndef NO_LIBUDEV
47b414
@@ -435,8 +434,10 @@ static void alert(char *event, char *dev, char *disc, struct alert_info *info)
47b414
 		if (mp) {
47b414
 			FILE *mdstat;
47b414
 			char hname[256];
47b414
+
47b414
 			gethostname(hname, sizeof(hname));
47b414
-			signal(SIGPIPE, SIG_IGN);
47b414
+			signal_s(SIGPIPE, SIG_IGN);
47b414
+
47b414
 			if (info->mailfrom)
47b414
 				fprintf(mp, "From: %s\n", info->mailfrom);
47b414
 			else
47b414
diff --git a/managemon.c b/managemon.c
47b414
index bb7334cf..0e9bdf00 100644
47b414
--- a/managemon.c
47b414
+++ b/managemon.c
47b414
@@ -106,7 +106,6 @@
47b414
 #include	"mdmon.h"
47b414
 #include	<sys/syscall.h>
47b414
 #include	<sys/socket.h>
47b414
-#include	<signal.h>
47b414
 
47b414
 static void close_aa(struct active_array *aa)
47b414
 {
47b414
diff --git a/mdadm.h b/mdadm.h
47b414
index c7268a71..26e7e5cd 100644
47b414
--- a/mdadm.h
47b414
+++ b/mdadm.h
47b414
@@ -46,6 +46,7 @@ extern __off64_t lseek64 __P ((int __fd, __off64_t __offset, int __whence));
47b414
 #include	<string.h>
47b414
 #include	<syslog.h>
47b414
 #include	<stdbool.h>
47b414
+#include	<signal.h>
47b414
 /* Newer glibc requires sys/sysmacros.h directly for makedev() */
47b414
 #include	<sys/sysmacros.h>
47b414
 #ifdef __dietlibc__
47b414
@@ -1729,6 +1730,27 @@ static inline char *to_subarray(struct mdstat_ent *ent, char *container)
47b414
 	return &ent->metadata_version[10+strlen(container)+1];
47b414
 }
47b414
 
47b414
+/**
47b414
+ * signal_s() - Wrapper for sigaction() with signal()-like interface.
47b414
+ * @sig: The signal to set the signal handler to.
47b414
+ * @handler: The signal handler.
47b414
+ *
47b414
+ * Return: previous handler or SIG_ERR on failure.
47b414
+ */
47b414
+static inline sighandler_t signal_s(int sig, sighandler_t handler)
47b414
+{
47b414
+	struct sigaction new_act;
47b414
+	struct sigaction old_act;
47b414
+
47b414
+	new_act.sa_handler = handler;
47b414
+	new_act.sa_flags = 0;
47b414
+
47b414
+	if (sigaction(sig, &new_act, &old_act) == 0)
47b414
+		return old_act.sa_handler;
47b414
+
47b414
+	return SIG_ERR;
47b414
+}
47b414
+
47b414
 #ifdef DEBUG
47b414
 #define dprintf(fmt, arg...) \
47b414
 	fprintf(stderr, "%s: %s: "fmt, Name, __func__, ##arg)
47b414
diff --git a/mdmon.c b/mdmon.c
47b414
index c71e62c6..5570574b 100644
47b414
--- a/mdmon.c
47b414
+++ b/mdmon.c
47b414
@@ -56,7 +56,6 @@
47b414
 #include	<errno.h>
47b414
 #include	<string.h>
47b414
 #include	<fcntl.h>
47b414
-#include	<signal.h>
47b414
 #include	<dirent.h>
47b414
 #ifdef USE_PTHREADS
47b414
 #include	<pthread.h>
47b414
diff --git a/monitor.c b/monitor.c
47b414
index e0d3be67..b877e595 100644
47b414
--- a/monitor.c
47b414
+++ b/monitor.c
47b414
@@ -22,7 +22,6 @@
47b414
 #include "mdmon.h"
47b414
 #include <sys/syscall.h>
47b414
 #include <sys/select.h>
47b414
-#include <signal.h>
47b414
 
47b414
 static char *array_states[] = {
47b414
 	"clear", "inactive", "suspended", "readonly", "read-auto",
47b414
diff --git a/probe_roms.c b/probe_roms.c
47b414
index 7ea04c7a..94c80c2c 100644
47b414
--- a/probe_roms.c
47b414
+++ b/probe_roms.c
47b414
@@ -22,7 +22,6 @@
47b414
 #include "probe_roms.h"
47b414
 #include "mdadm.h"
47b414
 #include <unistd.h>
47b414
-#include <signal.h>
47b414
 #include <fcntl.h>
47b414
 #include <sys/mman.h>
47b414
 #include <sys/stat.h>
47b414
@@ -69,7 +68,8 @@ static int probe_address16(const __u16 *ptr, __u16 *val)
47b414
 
47b414
 void probe_roms_exit(void)
47b414
 {
47b414
-	signal(SIGBUS, SIG_DFL);
47b414
+	signal_s(SIGBUS, SIG_DFL);
47b414
+
47b414
 	if (rom_fd >= 0) {
47b414
 		close(rom_fd);
47b414
 		rom_fd = -1;
47b414
@@ -98,7 +98,7 @@ int probe_roms_init(unsigned long align)
47b414
 	if (roms_init())
47b414
 		return -1;
47b414
 
47b414
-	if (signal(SIGBUS, sigbus) == SIG_ERR)
47b414
+	if (signal_s(SIGBUS, sigbus) == SIG_ERR)
47b414
 		rc = -1;
47b414
 	if (rc == 0) {
47b414
 		fd = open("/dev/mem", O_RDONLY);
47b414
diff --git a/raid6check.c b/raid6check.c
47b414
index a8e6005b..99477761 100644
47b414
--- a/raid6check.c
47b414
+++ b/raid6check.c
47b414
@@ -24,7 +24,6 @@
47b414
 
47b414
 #include "mdadm.h"
47b414
 #include <stdint.h>
47b414
-#include <signal.h>
47b414
 #include <sys/mman.h>
47b414
 
47b414
 #define CHECK_PAGE_BITS (12)
47b414
@@ -130,30 +129,36 @@ void raid6_stats(int *disk, int *results, int raid_disks, int chunk_size)
47b414
 }
47b414
 
47b414
 int lock_stripe(struct mdinfo *info, unsigned long long start,
47b414
-		int chunk_size, int data_disks, sighandler_t *sig) {
47b414
+		int chunk_size, int data_disks, sighandler_t *sig)
47b414
+{
47b414
 	int rv;
47b414
+
47b414
+	sig[0] = signal_s(SIGTERM, SIG_IGN);
47b414
+	sig[1] = signal_s(SIGINT, SIG_IGN);
47b414
+	sig[2] = signal_s(SIGQUIT, SIG_IGN);
47b414
+
47b414
+	if (sig[0] == SIG_ERR || sig[1] == SIG_ERR || sig[2] == SIG_ERR)
47b414
+		return 1;
47b414
+
47b414
 	if(mlockall(MCL_CURRENT | MCL_FUTURE) != 0) {
47b414
 		return 2;
47b414
 	}
47b414
 
47b414
-	sig[0] = signal(SIGTERM, SIG_IGN);
47b414
-	sig[1] = signal(SIGINT, SIG_IGN);
47b414
-	sig[2] = signal(SIGQUIT, SIG_IGN);
47b414
-
47b414
 	rv = sysfs_set_num(info, NULL, "suspend_lo", start * chunk_size * data_disks);
47b414
 	rv |= sysfs_set_num(info, NULL, "suspend_hi", (start + 1) * chunk_size * data_disks);
47b414
 	return rv * 256;
47b414
 }
47b414
 
47b414
-int unlock_all_stripes(struct mdinfo *info, sighandler_t *sig) {
47b414
+int unlock_all_stripes(struct mdinfo *info, sighandler_t *sig)
47b414
+{
47b414
 	int rv;
47b414
 	rv = sysfs_set_num(info, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
47b414
 	rv |= sysfs_set_num(info, NULL, "suspend_hi", 0);
47b414
 	rv |= sysfs_set_num(info, NULL, "suspend_lo", 0);
47b414
 
47b414
-	signal(SIGQUIT, sig[2]);
47b414
-	signal(SIGINT, sig[1]);
47b414
-	signal(SIGTERM, sig[0]);
47b414
+	signal_s(SIGQUIT, sig[2]);
47b414
+	signal_s(SIGINT, sig[1]);
47b414
+	signal_s(SIGTERM, sig[0]);
47b414
 
47b414
 	if(munlockall() != 0)
47b414
 		return 3;
47b414
diff --git a/util.c b/util.c
47b414
index 3d05d074..cc94f96e 100644
47b414
--- a/util.c
47b414
+++ b/util.c
47b414
@@ -35,7 +35,6 @@
47b414
 #include	<poll.h>
47b414
 #include	<ctype.h>
47b414
 #include	<dirent.h>
47b414
-#include	<signal.h>
47b414
 #include	<dlfcn.h>
47b414
 
47b414
 
47b414
-- 
47b414
2.31.1
47b414