Blob Blame History Raw
From a9c15847579ce3ac210b2dcc41e2af6c5f8bd65c Mon Sep 17 00:00:00 2001
From: NeilBrown <neilb@suse.de>
Date: Thu, 1 Aug 2013 15:59:24 +1000
Subject: [PATCH] mdmon: don't lie to systemd.

Now that mdmon responds fairly well to SIGTERM, stop lying to
systemd about being started on the initrd.

Note that if mdmon is rerun (--takeover) for some reason, and systemd
chooses to kill processes before remounting / readonly, then the
unmount will hang.

If systemd ever lets us tell it that we don't want to be killed until
root is readonly, then we should do that.

Signed-off-by: NeilBrown <neilb@suse.de>
---
 mdadm.h |  2 ++
 mdmon.c | 12 ++++++++----
 util.c  | 11 +++++++++++
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/mdadm.h b/mdadm.h
index c5d9c30..43f3a57 100644
--- a/mdadm.h
+++ b/mdadm.h
@@ -1390,6 +1390,8 @@ extern void fmt_devname(char *name, int num);
 extern int stat2devnum(struct stat *st);
 extern int fd2devnum(int fd);
 
+extern int in_initrd(void);
+
 static inline int dev2major(int d)
 {
 	if (d >= 0)
diff --git a/mdmon.c b/mdmon.c
index 13f9510..f0b0623 100644
--- a/mdmon.c
+++ b/mdmon.c
@@ -298,10 +298,14 @@ int main(int argc, char *argv[])
 		{NULL, 0, NULL, 0}
 	};
 
-	/*
-	 * Always change process name to @dmon to avoid systemd killing it
-	 */
-	argv[0][0] = '@';
+	if (in_initrd()) {
+		/*
+		 * set first char of argv[0] to @. This is used by
+		 * systemd to signal that the task was launched from
+		 * initrd/initramfs and should be preserved during shutdown
+		 */
+		argv[0][0] = '@';
+	}
 
 	while ((opt = getopt_long(argc, argv, "thaF", options, NULL)) != -1) {
 		switch (opt) {
diff --git a/util.c b/util.c
index 3965f43..aa2c8be 100644
--- a/util.c
+++ b/util.c
@@ -29,6 +29,8 @@
 #include	<sys/utsname.h>
 #include	<sys/wait.h>
 #include	<sys/un.h>
+#include	<sys/vfs.h>
+#include	<linux/magic.h>
 #include	<ctype.h>
 #include	<dirent.h>
 #include	<signal.h>
@@ -1937,3 +1939,12 @@ void enable_fds(int devices)
 	}
 	return disks;
 }
+
+int in_initrd(void)
+{
+	/* This is based on similar function in systemd. */
+	struct statfs s;
+	return  statfs("/", &s) >= 0 &&
+		(s.f_type == TMPFS_MAGIC ||
+		 s.f_type == RAMFS_MAGIC);
+}
-- 
1.8.4.2