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

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