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
|
From 23945e20c418595a7a4037e9258f23aa7bed6b48 Mon Sep 17 00:00:00 2001
From: jake <jikai11@huawei.com>
Date: Mon, 13 Nov 2023 08:15:12 +0000
Subject: [PATCH 07/14] !2244 Save task address of shim v2 * Save task address
of shim v2
---
.../v1/v1_cri_container_manager_service.cc | 6 ++
.../v1alpha/cri_container_manager_service.cc | 5 ++
src/daemon/modules/runtime/shim/shim_rt_ops.c | 86 ++++++++++++++-----
3 files changed, 74 insertions(+), 23 deletions(-)
diff --git a/src/daemon/entry/cri/v1/v1_cri_container_manager_service.cc b/src/daemon/entry/cri/v1/v1_cri_container_manager_service.cc
index 1f20d2d2..f635df2b 100644
--- a/src/daemon/entry/cri/v1/v1_cri_container_manager_service.cc
+++ b/src/daemon/entry/cri/v1/v1_cri_container_manager_service.cc
@@ -1013,6 +1013,12 @@ auto ContainerManagerService::ContainerStats(const std::string &containerID, Err
if (error.NotEmpty()) {
goto cleanup;
}
+ if (contStatsVec.size() == 0) {
+ ERROR("Failed to get container stats");
+ error.SetError("Failed to get container stats");
+ goto cleanup;
+ }
+
contStats = std::move(contStatsVec[0]);
cleanup:
diff --git a/src/daemon/entry/cri/v1alpha/cri_container_manager_service.cc b/src/daemon/entry/cri/v1alpha/cri_container_manager_service.cc
index 6f8ca114..9da25768 100644
--- a/src/daemon/entry/cri/v1alpha/cri_container_manager_service.cc
+++ b/src/daemon/entry/cri/v1alpha/cri_container_manager_service.cc
@@ -1019,6 +1019,11 @@ auto ContainerManagerService::ContainerStats(const std::string &containerID, Err
if (error.NotEmpty()) {
goto cleanup;
}
+ if (contStatsVec.size() == 0) {
+ ERROR("Failed to get container stats");
+ error.SetError("Failed to get container stats");
+ goto cleanup;
+ }
contStats = std::move(contStatsVec[0]);
cleanup:
diff --git a/src/daemon/modules/runtime/shim/shim_rt_ops.c b/src/daemon/modules/runtime/shim/shim_rt_ops.c
index d348dfe1..550b17f3 100644
--- a/src/daemon/modules/runtime/shim/shim_rt_ops.c
+++ b/src/daemon/modules/runtime/shim/shim_rt_ops.c
@@ -16,13 +16,17 @@
#define _GNU_SOURCE
#include "shim_rt_ops.h"
+
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <limits.h>
-#include "isula_libutils/log.h"
-#include "isula_libutils/shim_client_process_state.h"
+
+#include <isula_libutils/auto_cleanup.h>
+#include <isula_libutils/log.h>
+#include <isula_libutils/shim_client_process_state.h>
+
#include "utils.h"
#include "utils_string.h"
#include "constants.h"
@@ -318,16 +322,46 @@ bool rt_shim_detect(const char *runtime)
return false;
}
+static int save_shim_v2_address(const char *bundle, const char *addr)
+{
+ int nret;
+ char filename[PATH_MAX] = { 0 };
+
+ if (bundle == NULL) {
+ ERROR("Invalid input params");
+ return -1;
+ }
+
+ if (addr == NULL || strlen(addr) == 0) {
+ ERROR("Invalid shim v2 addr");
+ return -1;
+ }
+
+ nret = snprintf(filename, sizeof(filename), "%s/%s", bundle, "address");
+ if (nret < 0 || (size_t)nret >= sizeof(filename)) {
+ ERROR("Failed to print string");
+ return -1;
+ }
+
+ nret = util_atomic_write_file(filename, addr, strlen(addr), CONFIG_FILE_MODE, false);
+ if (nret != 0) {
+ ERROR("Failed to write file %s", filename);
+ return -1;
+ }
+
+ return 0;
+}
+
int rt_shim_create(const char *id, const char *runtime, const rt_create_params_t *params)
{
int ret = 0;
int pid = 0;
int fd = -1;
const char *task_address = NULL;
- char addr[PATH_MAX] = {0};
- char *exit_fifo_path = NULL;
- char *state_path = NULL;
- char *log_path = NULL;
+ char response[PATH_MAX] = {0};
+ __isula_auto_free char *exit_fifo_path = NULL;
+ __isula_auto_free char *state_path = NULL;
+ __isula_auto_free char *log_path = NULL;
if (id == NULL || runtime == NULL || params == NULL) {
ERROR("Invalid input params");
@@ -337,29 +371,25 @@ int rt_shim_create(const char *id, const char *runtime, const rt_create_params_t
exit_fifo_path = util_path_dir(params->exit_fifo);
if (exit_fifo_path == NULL) {
ERROR("%s: failed to get exit fifo dir from %s", id, params->exit_fifo);
- ret = -1;
- goto out;
+ return -1;
}
state_path = util_path_dir(exit_fifo_path);
if (state_path == NULL) {
ERROR("%s:failed to get state dir from %s", id, exit_fifo_path);
- ret = -1;
- goto out;
+ return -1;
}
log_path = util_string_append(SHIM_V2_LOG, params->bundle);
if (log_path == NULL) {
ERROR("Fail to append log path");
- ret = -1;
- goto out;
+ return -1;
}
fd = util_open(log_path, O_RDWR | O_CREAT | O_TRUNC, DEFAULT_SECURE_FILE_MODE);
if (fd < 0) {
ERROR("Failed to create log file for shim v2: %s", log_path);
- ret = -1;
- goto out;
+ return -1;
}
close(fd);
@@ -367,13 +397,13 @@ int rt_shim_create(const char *id, const char *runtime, const rt_create_params_t
* If task address is not set, create a new shim-v2 and get the address.
* If task address is set, use it directly.
*/
- if (params->task_addr == NULL) {
- if (shim_bin_v2_create(runtime, id, params->bundle, NULL, addr, state_path) != 0) {
+ if (params->task_addr == NULL || strlen(params->task_addr) == 0) {
+ if (shim_bin_v2_create(runtime, id, params->bundle, NULL, response, state_path) != 0) {
ERROR("%s: failed to create v2 shim", id);
- ret = -1;
- goto out;
+ return -1;
}
- task_address = addr;
+
+ task_address = response;
} else {
task_address = params->task_addr;
}
@@ -392,10 +422,20 @@ int rt_shim_create(const char *id, const char *runtime, const rt_create_params_t
goto out;
}
+ if (save_shim_v2_address(params->bundle, task_address) != 0) {
+ ERROR("%s: failed to save shim v2 address", id);
+ ret = -1;
+ goto out;
+ }
+
+ return 0;
+
out:
- free(log_path);
- free(exit_fifo_path);
- free(state_path);
+ if (ret != 0) {
+ if (shim_v2_kill(id, NULL, SIGKILL, false) != 0) {
+ ERROR("%s: kill shim v2 failed", id);
+ }
+ }
return ret;
}
@@ -614,7 +654,7 @@ int rt_shim_status(const char *id, const char *runtime, const rt_status_params_t
return -1;
}
- if (params->task_address != NULL) {
+ if (params->task_address != NULL && strlen(params->task_address) != 0) {
if (strlen(params->task_address) >= PATH_MAX) {
ERROR("Invalid task address");
return -1;
--
2.42.0
|