From 2e2f66f4d2dc0eec584cb701649e20d9cf6eebc5 Mon Sep 17 00:00:00 2001
From: LiFeng <lifeng68@huawei.com>
Date: Tue, 10 Dec 2019 05:40:20 -0500
Subject: [PATCH 129/131] cgfsng: add retry for enter cgroup

Signed-off-by: LiFeng <lifeng68@huawei.com>
---
 src/lxc/cgroups/cgfsng.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c
index c96285e1..50bdc771 100644
--- a/src/lxc/cgroups/cgfsng.c
+++ b/src/lxc/cgroups/cgfsng.c
@@ -1300,11 +1300,23 @@ __cgfsng_ops static bool cgfsng_payload_enter(struct cgroup_ops *ops, pid_t pid)
 	for (int i = 0; ops->hierarchies[i]; i++) {
 		int ret;
 		char *fullpath;
+		int retry_count = 0;
+		int max_retry = 10;
 
 		fullpath = must_make_path(ops->hierarchies[i]->container_full_path,
 		                          "cgroup.procs", NULL);
+retry:
 		ret = lxc_write_to_file(fullpath, pidstr, len, false, 0666);
 		if (ret != 0) {
+			if (errno == ENOENT) {
+				if (retry_count < max_retry) {
+					SYSERROR("Failed to enter cgroup \"%s\" with retry count:%d", fullpath, retry_count);
+					(void)mkdir_eexist_on_last(ops->hierarchies[i]->container_full_path, 0755);
+					usleep(100 * 1000); /* 100 millisecond */
+					retry_count++;
+					goto retry;
+				}
+			}
 			SYSERROR("Failed to enter cgroup \"%s\"", fullpath);
 			free(fullpath);
 			return false;
@@ -2201,6 +2213,8 @@ static int cg_legacy_set_data(struct cgroup_ops *ops, const char *filename,
 	struct hierarchy *h;
 	int ret = 0;
 	char *controller = NULL;
+	int retry_count = 0;
+	int max_retry = 10;
 
 	len = strlen(filename);
 	controller = alloca(len + 1);
@@ -2228,8 +2242,19 @@ static int cg_legacy_set_data(struct cgroup_ops *ops, const char *filename,
 	}
 
 	fullpath = must_make_path(h->container_full_path, filename, NULL);
+
+retry:
 	ret = lxc_write_to_file(fullpath, value, strlen(value), false, 0666);
 	if (ret) {
+		if (errno == ENOENT) {
+			if (retry_count < max_retry) {
+				SYSERROR("setting cgroup config for ready process caused \"failed to write %s to %s\".", value, fullpath);
+				(void)mkdir_eexist_on_last(h->container_full_path, 0755);
+				usleep(100 * 1000); /* 100 millisecond */
+				retry_count++;
+				goto retry;
+			}
+		}
 		lxc_write_error_message(ops->errfd,
 		                        "%s:%d: setting cgroup config for ready process caused \"failed to write %s to %s: %s\".",
 		                        __FILE__, __LINE__, value, fullpath, strerror(errno));
-- 
2.23.0

