summaryrefslogtreecommitdiff
path: root/0028-use-supervisor-to-notify-sandbox-exit-event.patch
blob: 379eb907904ad287d6be470be6eea201d17639e0 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
From 835185f7c4739993c2ca26d737bb0a45277ad932 Mon Sep 17 00:00:00 2001
From: jikai <jikai11@huawei.com>
Date: Wed, 20 Mar 2024 15:48:42 +0800
Subject: [PATCH 28/43] use supervisor to notify sandbox exit event

Signed-off-by: jikai <jikai11@huawei.com>
---
 src/daemon/modules/api/container_api.h        |  2 +-
 .../modules/container/restore/restore.c       |  6 +++-
 .../modules/container/supervisor/supervisor.c | 15 +++++++-
 .../modules/service/service_container.c       |  9 +++--
 .../controller/shim/shim_controller.cc        | 35 ++-----------------
 src/daemon/sandbox/sandbox_ops.cc             | 23 ++++++++++++
 src/daemon/sandbox/sandbox_ops.h              |  2 ++
 7 files changed, 53 insertions(+), 39 deletions(-)

diff --git a/src/daemon/modules/api/container_api.h b/src/daemon/modules/api/container_api.h
index 4602d244..43d66d64 100644
--- a/src/daemon/modules/api/container_api.h
+++ b/src/daemon/modules/api/container_api.h
@@ -270,7 +270,7 @@ bool container_is_valid_state_string(const char *state);
 void container_update_health_monitor(const char *container_id);
 
 extern int container_supervisor_add_exit_monitor(int fd, const pid_ppid_info_t *pid_info, const char *name,
-                                                 const char *runtime);
+                                                 const char *runtime, bool sandbox_container);
 
 extern char *container_exit_fifo_create(const char *cont_state_path);
 
diff --git a/src/daemon/modules/container/restore/restore.c b/src/daemon/modules/container/restore/restore.c
index 2669ea22..76868e28 100644
--- a/src/daemon/modules/container/restore/restore.c
+++ b/src/daemon/modules/container/restore/restore.c
@@ -57,6 +57,7 @@ static int restore_supervisor(const container_t *cont)
     char *statepath = cont->state_path;
     char *runtime = cont->runtime;
     pid_ppid_info_t pid_info = { 0 };
+    bool sandbox_container = false;
 
     nret = snprintf(container_state, sizeof(container_state), "%s/%s", statepath, id);
     if (nret < 0 || (size_t)nret >= sizeof(container_state)) {
@@ -90,8 +91,11 @@ static int restore_supervisor(const container_t *cont)
     pid_info.ppid = cont->state->state->p_pid;
     pid_info.start_time = cont->state->state->start_time;
     pid_info.pstart_time = cont->state->state->p_start_time;
+#ifdef ENABLE_CRI_API_V1
+    sandbox_container = is_sandbox_container(cont->common_config->sandbox_info);
+#endif
 
-    if (container_supervisor_add_exit_monitor(exit_fifo_fd, &pid_info, id, runtime)) {
+    if (container_supervisor_add_exit_monitor(exit_fifo_fd, &pid_info, id, runtime, sandbox_container)) {
         ERROR("Failed to add exit monitor to supervisor");
         ret = -1;
         goto out;
diff --git a/src/daemon/modules/container/supervisor/supervisor.c b/src/daemon/modules/container/supervisor/supervisor.c
index 1f9a043c..63289283 100644
--- a/src/daemon/modules/container/supervisor/supervisor.c
+++ b/src/daemon/modules/container/supervisor/supervisor.c
@@ -38,6 +38,9 @@
 #include "container_api.h"
 #include "event_type.h"
 #include "utils_file.h"
+#ifdef ENABLE_CRI_API_V1
+#include "sandbox_ops.h"
+#endif
 
 pthread_mutex_t g_supervisor_lock = PTHREAD_MUTEX_INITIALIZER;
 struct epoll_descr g_supervisor_descr;
@@ -47,6 +50,7 @@ struct supervisor_handler_data {
     int exit_code;
     char *name;
     char *runtime;
+    bool is_sandbox_container;
     pid_ppid_info_t pid_info;
 };
 
@@ -211,6 +215,14 @@ retry:
 
     (void)isulad_monitor_send_container_event(name, STOPPED, (int)pid, data->exit_code, NULL, NULL);
 
+#ifdef ENABLE_CRI_API_V1
+    if (data->is_sandbox_container) {
+        if (sandbox_on_sandbox_exit(name, data->exit_code) < 0) {
+            ERROR("Failed to handle sandbox %s exit", name);
+        }
+    }
+#endif
+
     supervisor_handler_data_free(data);
 
     DAEMON_CLEAR_ERRMSG();
@@ -259,7 +271,7 @@ static int supervisor_exit_cb(int fd, uint32_t events, void *cbdata, struct epol
 
 /* supervisor add exit monitor */
 int container_supervisor_add_exit_monitor(int fd, const pid_ppid_info_t *pid_info, const char *name,
-                                          const char *runtime)
+                                          const char *runtime, bool sandbox_container)
 {
     int ret = 0;
     struct supervisor_handler_data *data = NULL;
@@ -285,6 +297,7 @@ int container_supervisor_add_exit_monitor(int fd, const pid_ppid_info_t *pid_inf
     data->fd = fd;
     data->name = util_strdup_s(name);
     data->runtime = util_strdup_s(runtime);
+    data->is_sandbox_container = sandbox_container;
     data->pid_info.pid = pid_info->pid;
     data->pid_info.start_time = pid_info->start_time;
     data->pid_info.ppid = pid_info->ppid;
diff --git a/src/daemon/modules/service/service_container.c b/src/daemon/modules/service/service_container.c
index a3606a82..7b34cc7f 100644
--- a/src/daemon/modules/service/service_container.c
+++ b/src/daemon/modules/service/service_container.c
@@ -275,13 +275,14 @@ static void clean_resources_on_failure(const container_t *cont, const char *engi
     return;
 }
 
-static int do_post_start_on_success(const char *id, const char *runtime, const char *pidfile, int exit_fifo_fd,
+static int do_post_start_on_success(const char *id, const char *runtime, bool sandbox_container,
+                                    const char *pidfile, int exit_fifo_fd,
                                     const pid_ppid_info_t *pid_info)
 {
     int ret = 0;
 
     // exit_fifo_fd was closed in container_supervisor_add_exit_monitor
-    if (container_supervisor_add_exit_monitor(exit_fifo_fd, pid_info, id, runtime)) {
+    if (container_supervisor_add_exit_monitor(exit_fifo_fd, pid_info, id, runtime, sandbox_container)) {
         ERROR("Failed to add exit monitor to supervisor");
         ret = -1;
     }
@@ -749,6 +750,7 @@ static int do_start_container(container_t *cont, const char *console_fifos[], bo
     oci_runtime_spec *oci_spec = NULL;
     rt_create_params_t create_params = { 0 };
     rt_start_params_t start_params = { 0 };
+    bool sandbox_container;
 
     nret = snprintf(bundle, sizeof(bundle), "%s/%s", cont->root_path, id);
     if (nret < 0 || (size_t)nret >= sizeof(bundle)) {
@@ -897,6 +899,7 @@ static int do_start_container(container_t *cont, const char *console_fifos[], bo
     if (cont->common_config->sandbox_info != NULL) {
         create_params.task_addr = cont->common_config->sandbox_info->task_address;
     }
+    sandbox_container = is_sandbox_container(cont->common_config->sandbox_info);
 #endif
 
     if (runtime_create(id, runtime, &create_params) != 0) {
@@ -921,7 +924,7 @@ static int do_start_container(container_t *cont, const char *console_fifos[], bo
 
     ret = runtime_start(id, runtime, &start_params, pid_info);
     if (ret == 0) {
-        if (do_post_start_on_success(id, runtime, pidfile, exit_fifo_fd, pid_info) != 0) {
+        if (do_post_start_on_success(id, runtime, sandbox_container, pidfile, exit_fifo_fd, pid_info) != 0) {
             ERROR("Failed to do post start on runtime start success");
             ret = -1;
             goto clean_resources;
diff --git a/src/daemon/sandbox/controller/shim/shim_controller.cc b/src/daemon/sandbox/controller/shim/shim_controller.cc
index 39fcf8ea..593fade9 100644
--- a/src/daemon/sandbox/controller/shim/shim_controller.cc
+++ b/src/daemon/sandbox/controller/shim/shim_controller.cc
@@ -397,39 +397,8 @@ bool ShimController::Stop(const std::string &sandboxId, uint32_t timeoutSecs, Er
 
 bool ShimController::Wait(std::shared_ptr<SandboxStatusCallback> cb, const std::string &sandboxId, Errors &error)
 {
-    std::thread([this, cb, sandboxId]() {
-        if (m_cb == nullptr || m_cb->container.wait == nullptr) {
-            ERROR("Unimplemented callback");
-            return;
-        }
-
-        auto requestWrapper = makeUniquePtrCStructWrapper<container_wait_request>(free_container_wait_request);
-        if (requestWrapper == nullptr) {
-            ERROR("Out of memory");
-            return;
-        }
-        auto request = requestWrapper->get();
-        request->id = isula_strdup_s(sandboxId.c_str());
-        request->condition = WAIT_CONDITION_STOPPED;
-        container_wait_response *response { nullptr };
-
-        int ret = m_cb->container.wait(request, &response);
-        auto responseWrapper = makeUniquePtrCStructWrapper<container_wait_response>(response, free_container_wait_response);
-
-        if (ret != 0) {
-            std::string msg = (response != nullptr && response->errmsg != nullptr) ? response->errmsg : "internal";
-            ERROR("Failed to wait sandbox %s: %s", sandboxId.c_str(), msg.c_str());
-            return;
-        }
-
-        ControllerExitInfo info;
-        auto currentTime = std::chrono::high_resolution_clock::now();
-        auto duration = currentTime.time_since_epoch();
-        info.exitedAt = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count();
-        info.exitStatus = response->exit_code;
-        cb->OnSandboxExit(info);
-    }).detach();
-
+    // ShimController will use sandbox_on_exit callback of supervisor in lower container level
+    // to notify the sandbox exit event
     return true;
 }
 
diff --git a/src/daemon/sandbox/sandbox_ops.cc b/src/daemon/sandbox/sandbox_ops.cc
index 005063c0..b7fb40bf 100644
--- a/src/daemon/sandbox/sandbox_ops.cc
+++ b/src/daemon/sandbox/sandbox_ops.cc
@@ -18,6 +18,7 @@
 #include <isula_libutils/log.h>
 
 #include "controller_manager.h"
+#include "sandbox_manager.h"
 #include "namespace.h"
 #include "utils.h"
 
@@ -175,3 +176,25 @@ int sandbox_purge_exec(const container_config_v2_common_config *config, const ch
 {
     return do_sandbox_purge(config, exec_id);
 }
+
+int sandbox_on_sandbox_exit(const char *sandbox_id, int exit_code)
+{
+    if (nullptr == sandbox_id) {
+        ERROR("Invalid parameter: sandbox_id");
+        return -1;
+    }
+
+    auto sandbox = sandbox::SandboxManager::GetInstance()->GetSandbox(sandbox_id);
+    if (nullptr == sandbox) {
+        ERROR("Sandbox %s not found", sandbox_id);
+        return -1;
+    }
+
+    sandbox::ControllerExitInfo info;
+    auto currentTime = std::chrono::high_resolution_clock::now();
+    auto duration = currentTime.time_since_epoch();
+    info.exitedAt = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count();
+    info.exitStatus = exit_code;
+    sandbox->OnSandboxExit(info);
+    return 0;
+}
diff --git a/src/daemon/sandbox/sandbox_ops.h b/src/daemon/sandbox/sandbox_ops.h
index bef884fb..8189efd6 100644
--- a/src/daemon/sandbox/sandbox_ops.h
+++ b/src/daemon/sandbox/sandbox_ops.h
@@ -36,6 +36,8 @@ int sandbox_purge_container(const container_config_v2_common_config *config);
 
 int sandbox_purge_exec(const container_config_v2_common_config *config, const char *exec_id);
 
+int sandbox_on_sandbox_exit(const char *sandbox_id, int exit_code);
+
 #ifdef __cplusplus
 }
 #endif
-- 
2.34.1