Blob Blame History Raw
From 84d11e6c6a3b827b2daa32e16303235ce33d49f5 Mon Sep 17 00:00:00 2001
From: NeilBrown <neilb@suse.de>
Date: Thu, 1 Aug 2013 11:16:14 +1000
Subject: [PATCH] Grow: exit background thread cleanly on SIGTERM.

If the mdadm thread that monitors a reshape gets SIGTERM it should
exit cleanly and clear the 'suspended' region of the array.
However it mustn't clear 'sync_max' as that would allow the
reshape to continue unmonitored.

If the thread ever does get killed, the array should really be
shutdown soon after if possible.

Signed-off-by: NeilBrown <neilb@suse.de>
---
 Grow.c        | 15 +++++++++++++--
 super-intel.c |  2 ++
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/Grow.c b/Grow.c
index ff4ed5d..ce80c52 100644
--- a/Grow.c
+++ b/Grow.c
@@ -24,6 +24,7 @@
 #include	"mdadm.h"
 #include	"dlink.h"
 #include	<sys/mman.h>
+#include	<signal.h>
 
 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
 #error no endian defined
@@ -729,7 +730,8 @@ void abort_reshape(struct mdinfo *sra)
 	sysfs_set_num(sra, NULL, "suspend_hi", 0);
 	sysfs_set_num(sra, NULL, "suspend_lo", 0);
 	sysfs_set_num(sra, NULL, "sync_min", 0);
-	sysfs_set_str(sra, NULL, "sync_max", "max");
+	// It isn't safe to reset sync_max as we aren't monitoring.
+	// Array really should be stopped at this point.
 }
 
 int remove_disks_for_takeover(struct supertype *st,
@@ -2726,6 +2728,12 @@ static int impose_level(int fd, int level, char *devname, int verbose)
 	return ret_val;
 }
 
+int sigterm = 0;
+static void catch_term(int sig)
+{
+	sigterm = 1;
+}
+
 static int reshape_array(char *container, int fd, char *devname,
 			 struct supertype *st, struct mdinfo *info,
 			 int force, struct mddev_dev *devlist,
@@ -3260,6 +3268,8 @@ started:
 		fd = -1;
 	mlockall(MCL_FUTURE);
 
+	signal(SIGTERM, catch_term);
+
 	if (st->ss->external) {
 		/* metadata handler takes it from here */
 		done = st->ss->manage_reshape(
@@ -4243,7 +4253,8 @@ int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
 				forget_backup(dests, destfd,
 					      destoffsets, 1);
 		}
-
+		if (sigterm)
+			rv = -2;
 		if (rv < 0) {
 			if (rv == -1)
 				done = 1;
diff --git a/super-intel.c b/super-intel.c
index 4df33f4..7b24068 100644
--- a/super-intel.c
+++ b/super-intel.c
@@ -10444,6 +10444,8 @@ static int imsm_manage_reshape(
 			dprintf("wait_for_reshape_imsm returned error!\n");
 			goto abort;
 		}
+		if (sigterm)
+			goto abort;
 
 		if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
 			/* ignore error == 2, this can mean end of reshape here
-- 
1.8.4.2