Skip to contentSkip to conversation

mdadm: Unify forks behaviour

PATCH Applied Mariusz Tkaczyk 2 participants 2 messages Nov 4, 2020 – Nov 25, 2020 MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mdadm: Unify forks behaviour

If mdadm is run by udev or systemd, it gets a pipe as each stream. Forks in the background may run after an event or service has been processed when udev is detached from pipe. As a result process fails quietly if any message is written.
To prevent from it, each fork has to close all parent streams. Leave stderr and stdout opened only for debug purposes.
Unify it across all forks. Introduce other descriptors detection by scanning /proc/self/fd directory. Add generic method for managing systemd services.

---
 Grow.c        |  52 +++------------------
 Incremental.c |   1 +
 Monitor.c     |   5 +-
 mdadm.h       |  10 ++++
 mdmon.c       |   9 +---
 util.c        | 124 ++++++++++++++++++++++++++++++++------------------
 6 files changed, 100 insertions(+), 101 deletions(-)
Grow.c +7 -45
index 57db7d45..6b8321c5 100644
@@ -2982,47 +2982,6 @@ static void catch_term(int sig)
 	sigterm = 1;
 }
 
-static int continue_via_systemd(char *devnm)
-{
-	int skipped, i, pid, status;
-	char pathbuf[1024];
-	/* In a systemd/udev world, it is best to get systemd to
-	 * run "mdadm --grow --continue" rather than running in the
-	 * background.
-	 */
-	switch(fork()) {
-	case  0:
-		/* FIXME yuk. CLOSE_EXEC?? */
-		skipped = 0;
-		for (i = 3; skipped < 20; i++)
-			if (close(i) < 0)
-				skipped++;
-			else
-				skipped = 0;
-
-		/* Don't want to see error messages from
-		 * systemctl.  If the service doesn't exist,
-		 * we fork ourselves.
-		 */
-		close(2);
-		open("/dev/null", O_WRONLY);
-		snprintf(pathbuf, sizeof(pathbuf),
-			 "mdadm-grow-continue@%s.service", devnm);
-		status = execl("/usr/bin/systemctl", "systemctl", "restart",
-			       pathbuf, NULL);
-		status = execl("/bin/systemctl", "systemctl", "restart",
-			       pathbuf, NULL);
-		exit(1);
-	case -1: /* Just do it ourselves. */
-		break;
-	default: /* parent - good */
-		pid = wait(&status);
-		if (pid >= 0 && status == 0)
-			return 1;
-	}
-	return 0;
-}
-
 static int reshape_array(char *container, int fd, char *devname,
 			 struct supertype *st, struct mdinfo *info,
 			 int force, struct mddev_dev *devlist,
@@ -3401,6 +3360,7 @@ static int reshape_array(char *container, int fd, char *devname,
 		default: /* parent */
 			return 0;
 		case 0:
+			manage_fork_fds(0);
 			map_fork();
 			break;
 		}
@@ -3509,8 +3469,9 @@ started:
 		return 1;
 	}
 
-	if (!forked && !check_env("MDADM_NO_SYSTEMCTL"))
-		if (continue_via_systemd(container ?: sra->sys_name)) {
+	if (!forked)
+		if (continue_via_systemd(container ?: sra->sys_name,
+					 GROW_SERVICE)) {
 			free(fdlist);
 			free(offsets);
 			sysfs_free(sra);
@@ -3704,8 +3665,8 @@ int reshape_container(char *container, char *devname,
 	 */
 	ping_monitor(container);
 
-	if (!forked && !freeze_reshape && !check_env("MDADM_NO_SYSTEMCTL"))
-		if (continue_via_systemd(container))
+	if (!forked && !freeze_reshape)
+		if (continue_via_systemd(container, GROW_SERVICE))
 			return 0;
 
 	switch (forked ? 0 : fork()) {
@@ -3718,6 +3679,7 @@ int reshape_container(char *container, char *devname,
 			printf("%s: multi-array reshape continues in background\n", Name);
 		return 0;
 	case 0: /* child */
+		manage_fork_fds(0);
 		map_fork();
 		break;
 	}
Incremental.c +1 -0
index 98dbcd92..ad9ec1cc 100644
@@ -1679,6 +1679,7 @@ static void run_udisks(char *arg1, char *arg2)
 	int pid = fork();
 	int status;
 	if (pid == 0) {
+		manage_fork_fds(1);
 		execl("/usr/bin/udisks", "udisks", arg1, arg2, NULL);
 		execl("/bin/udisks", "udisks", arg1, arg2, NULL);
 		exit(1);
Monitor.c +1 -4
index 2d6b3b90..66527c65 100644
@@ -291,10 +291,7 @@ static int make_daemon(char *pidfile)
 		perror("daemonise");
 		return 1;
 	}
-	close(0);
-	open("/dev/null", O_RDWR);
-	dup2(0, 1);
-	dup2(0, 2);
+	manage_fork_fds(0);
 	setsid();
 	return -1;
 }
mdadm.h +10 -0
index 399478b8..9b3ad1dc 100644
@@ -129,6 +129,14 @@ struct dlm_lksb {
 #define FAILED_SLOTS_DIR "/run/mdadm/failed-slots"
 #endif /* FAILED_SLOTS */
 
+#ifndef MDMON_SERVICE
+#define MDMON_SERVICE "mdmon"
+#endif /* MDMON_SERVICE */
+
+#ifndef GROW_SERVICE
+#define GROW_SERVICE "mdadm-grow-continue"
+#endif /* GROW_SERVICE */
+
 #include	"md_u.h"
 #include	"md_p.h"
 #include	"bitmap.h"
@@ -1497,6 +1505,8 @@ extern int is_standard(char *dev, int *nump);
 extern int same_dev(char *one, char *two);
 extern int compare_paths (char* path1,char* path2);
 extern void enable_fds(int devices);
+extern void manage_fork_fds(int close_all);
+extern int continue_via_systemd(char *devnm, char *service_name);
 
 extern int parse_auto(char *str, char *msg, int config);
 extern struct mddev_ident *conf_get_ident(char *dev);
mdmon.c +1 -8
index ff985d29..c71e62c6 100644
@@ -546,14 +546,7 @@ static int mdmon(char *devnm, int must_fork, int takeover)
 	}
 
 	setsid();
-	close(0);
-	open("/dev/null", O_RDWR);
-	close(1);
-	ignore = dup(0);
-#ifndef DEBUG
-	close(2);
-	ignore = dup(0);
-#endif
+	manage_fork_fds(0);
 
 	/* This silliness is to stop the compiler complaining
 	 * that we ignore 'ignore'
util.c +80 -44

151 lines not on this page · raw message

Signature · 1 line
2.25.0
Applied
#1 lore raw
Quoted 11 lines · Mariusz Tkaczyk

If mdadm is run by udev or systemd, it gets a pipe as each stream. Forks in the background may run after an event or service has been processed when udev is detached from pipe. As a result process fails quietly if any message is written.
To prevent from it, each fork has to close all parent streams. Leave stderr and stdout opened only for debug purposes.
Unify it across all forks. Introduce other descriptors detection by scanning /proc/self/fd directory. Add generic method for managing systemd services.

Quoted 8 lines
---
 Grow.c        |  52 +++------------------
 Incremental.c |   1 +
 Monitor.c     |   5 +-
 mdadm.h       |  10 ++++
 mdmon.c       |   9 +---
 util.c        | 124 ++++++++++++++++++++++++++++++++------------------
 6 files changed, 100 insertions(+), 101 deletions(-)

Applied!

Thanks,
Jes