[lxc-devel] [lxc/master] [RFC]: lxccontainer: detect if we should send SIGRTMIN+3

brauner on Github lxc-bot at linuxcontainers.org
Mon Jul 18 20:47:47 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 479 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160718/ebb3e4a7/attachment.bin>
-------------- next part --------------
From 5defaf4373e60bc2ca0b1c1d812e3a4acceb7a26 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at mailbox.org>
Date: Mon, 18 Jul 2016 22:21:56 +0200
Subject: [PATCH] lxccontainer: detect if we should send SIGRTMIN+3

This is required by systemd to cleanly shutdown. Other init systems should not
have SIGRTMIN+3 in the blocked signals set.

Signed-off-by: Christian Brauner <cbrauner at suse.de>
---
 src/lxc/lxccontainer.c |  9 +++++++++
 src/lxc/utils.c        | 36 ++++++++++++++++++++++++++++++++++++
 src/lxc/utils.h        |  5 +++++
 3 files changed, 50 insertions(+)

diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index 4a4dc42..7571625 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -1606,6 +1606,15 @@ static bool do_lxcapi_shutdown(struct lxc_container *c, int timeout)
 		return true;
 	if (c->lxc_conf && c->lxc_conf->haltsignal)
 		haltsignal = c->lxc_conf->haltsignal;
+
+	/* Detect whether we should send SIGRTMIN + 3 (e.g. systemd). */
+	if (haltsignal == SIGPWR) {
+		if (check_sigblk(pid, (SIGRTMIN + 3))) {
+			haltsignal = (SIGRTMIN + 3);
+			INFO("Using SIGRTMIN+3 as halt signal.");
+		}
+	}
+
 	kill(pid, haltsignal);
 	retv = do_lxcapi_wait(c, "STOPPED", timeout);
 	return retv;
diff --git a/src/lxc/utils.c b/src/lxc/utils.c
index 5c35ae8..df6988e 100644
--- a/src/lxc/utils.c
+++ b/src/lxc/utils.c
@@ -1838,3 +1838,39 @@ int lxc_strmunmap(void *addr, size_t length)
 {
 	return munmap(addr, length + 1);
 }
+
+/* Check whether we need to send SIGRTMIN+3 to init. (e.g. used by systemd to
+ * cleanly shutdown container.
+ */
+bool check_sigblk(pid_t pid, int signal)
+{
+	bool bret = false;
+	char *line = NULL;
+	long unsigned int sigblk = 0;
+	size_t n = 0;
+	char status[MAXPATHLEN];
+	int ret;
+	FILE *f;
+
+	ret = snprintf(status, MAXPATHLEN, "/proc/%d/status", pid);
+	if (ret < 0 || ret >= MAXPATHLEN)
+		return bret;
+
+	f = fopen(status, "r");
+	if (!f)
+		return bret;
+
+	while (getline(&line, &n, f) != -1) {
+		if (!strncmp(line, "SigBlk:\t", 8))
+			if (sscanf(line + 8, "%lx", &sigblk) != 1)
+				goto out;
+	}
+
+	if (sigblk & signal)
+		bret = true;
+
+out:
+	free(line);
+	fclose(f);
+	return bret;
+}
diff --git a/src/lxc/utils.h b/src/lxc/utils.h
index 22161cb..ee4d0d6 100644
--- a/src/lxc/utils.h
+++ b/src/lxc/utils.h
@@ -296,4 +296,9 @@ int open_devnull(void);
 int set_stdfds(int fd);
 int null_stdfds(void);
 int lxc_count_file_lines(const char *fn);
+
+/* Check whether we need to send SIGRTMIN+3 to init. (e.g. used by systemd to
+ * cleanly shutdown container.
+ */
+bool check_sigblk(pid_t pid, int signal);
 #endif /* __LXC_UTILS_H */


More information about the lxc-devel mailing list