summaryrefslogtreecommitdiff
path: root/0165-bugfix-do-purge-container-when-do_start_container-fa.patch
blob: f216b21c2b95dcc996d958e075dc244b783cebc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
From a6b8a2c04df21d940ce0d22128b776c00d460bba Mon Sep 17 00:00:00 2001
From: liuxu <liuxu156@huawei.com>
Date: Fri, 6 Dec 2024 10:39:22 +0800
Subject: [PATCH 07/19] bugfix:do purge container when do_start_container
 failed

Signed-off-by: liuxu <liuxu156@huawei.com>
---
 .../modules/container/restore/restore.c       |  1 +
 .../modules/container/supervisor/supervisor.c |  4 +--
 .../modules/service/service_container.c       | 25 ++++++++++++++++---
 3 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/src/daemon/modules/container/restore/restore.c b/src/daemon/modules/container/restore/restore.c
index 52f68d21..44ed14df 100644
--- a/src/daemon/modules/container/restore/restore.c
+++ b/src/daemon/modules/container/restore/restore.c
@@ -95,6 +95,7 @@ static int restore_supervisor(const container_t *cont)
 
     if (container_supervisor_add_exit_monitor(exit_fifo_fd, exit_fifo, &pid_info, cont)) {
         ERROR("Failed to add exit monitor to supervisor");
+        close(exit_fifo_fd);
         ret = -1;
         goto out;
     }
diff --git a/src/daemon/modules/container/supervisor/supervisor.c b/src/daemon/modules/container/supervisor/supervisor.c
index 294783eb..f77f58d7 100644
--- a/src/daemon/modules/container/supervisor/supervisor.c
+++ b/src/daemon/modules/container/supervisor/supervisor.c
@@ -328,7 +328,6 @@ int container_supervisor_add_exit_monitor(int fd, const char *exit_fifo, const p
 
     if (pid_info == NULL || cont == NULL || cont->common_config == NULL) {
         ERROR("Invalid input arguments");
-        close(fd);
         return -1;
     }
 
@@ -336,7 +335,6 @@ int container_supervisor_add_exit_monitor(int fd, const char *exit_fifo, const p
     cgroup_path = merge_container_cgroups_path(cont->common_config->id, cont->hostconfig);
     if (cgroup_path == NULL) {
         ERROR("Failed to get cgroup path");
-        close(fd);
         return -1;
     }
 #endif
@@ -344,7 +342,6 @@ int container_supervisor_add_exit_monitor(int fd, const char *exit_fifo, const p
     data = util_common_calloc_s(sizeof(struct supervisor_handler_data));
     if (data == NULL) {
         ERROR("Memory out");
-        close(fd);
         return -1;
     }
 
@@ -385,6 +382,7 @@ int container_supervisor_add_exit_monitor(int fd, const char *exit_fifo, const p
     goto out;
 
 err:
+    data->fd = -1;
     supervisor_handler_data_free(data);
 #ifdef ENABLE_OOM_MONITOR
     common_free_cgroup_oom_handler_info(oom_handler_info);
diff --git a/src/daemon/modules/service/service_container.c b/src/daemon/modules/service/service_container.c
index 8e80e936..250e8299 100644
--- a/src/daemon/modules/service/service_container.c
+++ b/src/daemon/modules/service/service_container.c
@@ -283,7 +283,6 @@ static int do_post_start_on_success(container_t *cont, int exit_fifo_fd,
 {
     int ret = 0;
 
-    // exit_fifo_fd was closed in container_supervisor_add_exit_monitor
     if (container_supervisor_add_exit_monitor(exit_fifo_fd, exit_fifo, pid_info, cont)) {
         ERROR("Failed to add exit monitor to supervisor");
         ret = -1;
@@ -936,7 +935,11 @@ static int do_start_container(container_t *cont, const char *console_fifos[], bo
 
     if (runtime_create(id, runtime, &create_params) != 0) {
         ret = -1;
+#ifdef ENABLE_CRI_API_V1
+        goto clean_prepare_container;
+#else
         goto close_exit_fd;
+#endif
     }
 
     start_params.rootpath = cont->root_path;
@@ -959,19 +962,33 @@ static int do_start_container(container_t *cont, const char *console_fifos[], bo
         if (do_post_start_on_success(cont, exit_fifo_fd, exit_fifo, pid_info) != 0) {
             ERROR("Failed to do post start on runtime start success");
             ret = -1;
-            goto clean_resources;
+#ifdef ENABLE_CRI_API_V1
+            goto clean_prepare_container;
+#else
+            goto close_exit_fd;
+#endif
         }
     } else {
         // wait monitor cleanup cgroup and processes finished
         wait_exit_fifo(id, exit_fifo_fd);
+#ifdef ENABLE_CRI_API_V1
+        goto clean_prepare_container;
+#else
         goto close_exit_fd;
+#endif
     }
     goto out;
 
+#ifdef ENABLE_CRI_API_V1
+clean_prepare_container:
+    if (cont->common_config->sandbox_info != NULL &&
+        sandbox_purge_container(cont->common_config) != 0) {
+        ERROR("Failed to remove container %s from sandbox", id);
+    }
+#endif
+
 close_exit_fd:
     close(exit_fifo_fd);
-
-clean_resources:
     clean_resources_on_failure(cont, engine_log_path, loglevel);
 
 out:
-- 
2.23.0