From 8f85eda569912dce488d2e951f853958ba800ada Mon Sep 17 00:00:00 2001
From: LiFeng <lifeng68@huawei.com>
Date: Wed, 27 Nov 2019 22:33:46 -0500
Subject: [PATCH 124/131] lxc: close maincmd fd before destroy cgroup

Signed-off-by: LiFeng <lifeng68@huawei.com>
---
 src/lxc/cgroups/cgfsng.c |  6 ++++--
 src/lxc/start.c          | 33 ++++++++++++++++++---------------
 2 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c
index 87b49b0f..c96285e1 100644
--- a/src/lxc/cgroups/cgfsng.c
+++ b/src/lxc/cgroups/cgfsng.c
@@ -2013,8 +2013,10 @@ __cgfsng_ops static bool cgfsng_attach(struct cgroup_ops *ops, const char *name,
 
 		path = lxc_cmd_get_cgroup_path(name, lxcpath, h->controllers[0]);
 		/* not running */
-		if (path == NULL)
-                        return false;
+		if (path == NULL) {
+			ERROR("Failed to attach %d to cgroup of %s", (int)pid, name);
+			return false;
+		}
 
 		fullpath = build_full_cgpath_from_monitorpath(h, path, "cgroup.procs");
 		free(path);
diff --git a/src/lxc/start.c b/src/lxc/start.c
index 7246283e..63be4e3e 100644
--- a/src/lxc/start.c
+++ b/src/lxc/start.c
@@ -1185,19 +1185,6 @@ void lxc_fini(const char *name, struct lxc_handler *handler)
 	while (namespace_count--)
 		free(namespaces[namespace_count]);
 
-retry:
-	if (!cgroup_ops->destroy(cgroup_ops, handler)) {
-                TRACE("Trying to kill all subprocess");
-                signal_all_processes(handler);
-                TRACE("Finished kill all subprocess");
-		if (retry_count < max_retry) {
-			usleep(100 * 1000); /* 100 millisecond */
-			retry_count++;
-			goto retry;
-		}
-		SYSERROR("Failed to destroy cgroup path for container: \"%s\"", handler->name);
-	}
-
 	if (handler->conf->reboot == REBOOT_NONE) {
 		/* For all new state clients simply close the command socket.
 		 * This will inform all state clients that the container is
@@ -1208,13 +1195,27 @@ retry:
 		lxc_abstract_unix_close(handler->conf->maincmd_fd);
 		handler->conf->maincmd_fd = -1;
 		TRACE("Closed command socket");
+	}
 
+retry:
+	if (!cgroup_ops->destroy(cgroup_ops, handler)) {
+	        TRACE("Trying to kill all subprocess");
+	        signal_all_processes(handler);
+	        TRACE("Finished kill all subprocess");
+	        if (retry_count < max_retry) {
+			usleep(100 * 1000); /* 100 millisecond */
+			retry_count++;
+			goto retry;
+		}
+		SYSERROR("Failed to destroy cgroup path for container: \"%s\"", handler->name);
+	}
+
+	if (handler->conf->reboot == REBOOT_NONE) {
 		/* This function will try to connect to the legacy lxc-monitord
 		 * state server and only exists for backwards compatibility.
 		 */
 		lxc_monitor_send_state(name, STOPPED, handler->lxcpath);
 
-
 		/* isuald: write exit code to exit fifo */
 		if (handler->conf->exit_fd >= 0) {
 			ret = write(handler->conf->exit_fd, &handler->exit_code, sizeof(int));
@@ -2848,7 +2849,6 @@ int do_lxcapi_clean_resource(char *name, char *lxcpath, struct lxc_conf *conf, p
 		ret = -1;
 		goto out;
 	}
-	signal_all_processes(handler);
 
 	if (run_oci_hooks(handler->name, "oci-poststop", handler->conf, handler->lxcpath)) {
 		ERROR("Failed to run lxc.hook.post-stop for container \"%s\".", handler->name);
@@ -2857,6 +2857,9 @@ int do_lxcapi_clean_resource(char *name, char *lxcpath, struct lxc_conf *conf, p
 
 retry:
 	if (!handler->cgroup_ops->destroy(handler->cgroup_ops, handler)) {
+		TRACE("Trying to kill all subprocess");
+		signal_all_processes(handler);
+		TRACE("Finished kill all subprocess");
 		if (retry_count < max_retry) {
 			usleep(100 * 1000); /* 100 millisecond */
 			retry_count++;
-- 
2.23.0

