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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
From 13bc364cb5d8c03b701dde2b2811be84ee608e92 Mon Sep 17 00:00:00 2001
From: xuxuepeng <xuxuepeng1@huawei.com>
Date: Fri, 24 Nov 2023 14:18:32 +0800
Subject: [PATCH 41/64] Acquire system info in on demand
Signed-off-by: xuxuepeng <xuxuepeng1@huawei.com>
---
src/cmd/isulad/main.c | 2 --
src/daemon/common/sysinfo.c | 10 +-----
src/daemon/common/sysinfo.h | 6 ++++
.../executor/container_cb/execution_create.c | 32 +++++++++----------
.../executor/container_cb/execution_extend.c | 9 +++++-
src/daemon/modules/spec/verify.c | 32 ++++++++-----------
src/daemon/modules/spec/verify.h | 5 +--
test/mocks/sysinfo_mock.cc | 8 +++++
test/mocks/sysinfo_mock.h | 1 +
test/mocks/verify_mock.cc | 4 +--
test/mocks/verify_mock.h | 2 +-
11 files changed, 60 insertions(+), 51 deletions(-)
diff --git a/src/cmd/isulad/main.c b/src/cmd/isulad/main.c
index 5b971a72..95454e2a 100644
--- a/src/cmd/isulad/main.c
+++ b/src/cmd/isulad/main.c
@@ -1765,8 +1765,6 @@ int main(int argc, char **argv)
update_isulad_rlimits();
- (void)get_sys_info(true);
-
clock_gettime(CLOCK_MONOTONIC, &t_start);
if (pre_init_daemon(argc, argv) != 0) {
diff --git a/src/daemon/common/sysinfo.c b/src/daemon/common/sysinfo.c
index 39338925..28665834 100644
--- a/src/daemon/common/sysinfo.c
+++ b/src/daemon/common/sysinfo.c
@@ -19,7 +19,6 @@
#include <errno.h>
#include <sys/sysinfo.h>
-#include <isula_libutils/auto_cleanup.h>
#include <isula_libutils/log.h>
#include "err_msg.h"
@@ -30,8 +29,6 @@
#define etcOsRelease "/etc/os-release"
#define altOsRelease "/usr/lib/os-release"
-static sysinfo_t *g_sysinfo = NULL;
-
static char *get_pagesize(const char *pline)
{
size_t headlen;
@@ -382,10 +379,6 @@ sysinfo_t *get_sys_info(bool quiet)
sysinfo_t *sysinfo = NULL;
int ret = 0;
- if (g_sysinfo != NULL) {
- return g_sysinfo;
- }
-
sysinfo = util_common_calloc_s(sizeof(sysinfo_t));
if (sysinfo == NULL) {
ERROR("Out of memory");
@@ -413,7 +406,6 @@ sysinfo_t *get_sys_info(bool quiet)
if (ret != 0) {
goto out;
}
- g_sysinfo = sysinfo;
out:
if (ret != 0) {
free_sysinfo(sysinfo);
@@ -577,7 +569,7 @@ char *sysinfo_cgroup_controller_cpurt_mnt_path(void)
__isula_auto_free char *mnt = NULL;
__isula_auto_free char *root = NULL;
char fpath[PATH_MAX] = { 0 };
- sysinfo_t *sysinfo = NULL;
+ __isula_auto_sysinfo_t sysinfo_t *sysinfo = NULL;
sysinfo = get_sys_info(true);
if (sysinfo == NULL) {
diff --git a/src/daemon/common/sysinfo.h b/src/daemon/common/sysinfo.h
index 363576a9..cb44d1c5 100644
--- a/src/daemon/common/sysinfo.h
+++ b/src/daemon/common/sysinfo.h
@@ -21,6 +21,7 @@ extern "C" {
#include <stdbool.h>
#include <stdint.h>
+#include <isula_libutils/auto_cleanup.h>
#include "cgroup.h"
@@ -96,6 +97,11 @@ void free_mounts_info(mountinfo_t **minfos);
char *sysinfo_cgroup_controller_cpurt_mnt_path(void);
+// define auto free function callback for sysinfo_t
+define_auto_cleanup_callback(free_sysinfo, sysinfo_t)
+// define auto free macro for sysinfo_t
+#define __isula_auto_sysinfo_t auto_cleanup_tag(free_sysinfo)
+
#ifdef __cplusplus
}
#endif
diff --git a/src/daemon/executor/container_cb/execution_create.c b/src/daemon/executor/container_cb/execution_create.c
index 6b6c3b75..ca2a9163 100644
--- a/src/daemon/executor/container_cb/execution_create.c
+++ b/src/daemon/executor/container_cb/execution_create.c
@@ -145,7 +145,7 @@ static int merge_external_rootfs_to_host_config(host_config *host_spec, const ch
return 0;
}
-static host_config *get_host_spec(const container_create_request *request)
+static host_config *get_host_spec(const container_create_request *request, const sysinfo_t *sysinfo)
{
host_config *host_spec = NULL;
@@ -158,7 +158,7 @@ static host_config *get_host_spec(const container_create_request *request)
goto error_out;
}
- if (verify_host_config_settings(host_spec, false)) {
+ if (verify_host_config_settings(host_spec, sysinfo, false)) {
ERROR("Failed to verify host config settings");
goto error_out;
}
@@ -1109,17 +1109,9 @@ static int preparate_runtime_environment(const container_create_request *request
return 0;
}
-static int adapt_host_spec(host_config *host_spec)
+static int adapt_host_spec(host_config *host_spec, const sysinfo_t *sysinfo)
{
int ret = 0;
- sysinfo_t *sysinfo = NULL;
-
- sysinfo = get_sys_info(true);
- if (sysinfo == NULL) {
- ERROR("Can not get system info");
- ret = -1;
- goto out;
- }
if (host_spec->memory > 0 && host_spec->memory_swap == 0 && sysinfo->cgmeminfo.swap) {
if (host_spec->memory > (INT64_MAX / 2)) {
@@ -1136,14 +1128,14 @@ out:
}
static int get_basic_spec(const container_create_request *request, host_config **host_spec,
- container_config **container_spec)
+ container_config **container_spec, const sysinfo_t *sysinfo)
{
- *host_spec = get_host_spec(request);
+ *host_spec = get_host_spec(request, sysinfo);
if (*host_spec == NULL) {
return -1;
}
- if (adapt_host_spec(*host_spec) != 0) {
+ if (adapt_host_spec(*host_spec, sysinfo) != 0) {
return -1;
}
@@ -1393,6 +1385,7 @@ int container_create_cb(const container_create_request *request, container_creat
int ret = 0;
bool skip_id_name_manage = false;
bool skip_sandbox_key_manage = false;
+ __isula_auto_sysinfo_t sysinfo_t *sysinfo = NULL;
DAEMON_CLEAR_ERRMSG();
@@ -1413,7 +1406,14 @@ int container_create_cb(const container_create_request *request, container_creat
goto clean_nameindex;
}
- if (get_basic_spec(request, &host_spec, &container_spec) != 0) {
+ sysinfo = get_sys_info(true);
+ if (sysinfo == NULL) {
+ ERROR("Failed to get system info");
+ cc = ISULAD_ERR_EXEC;
+ goto clean_nameindex;
+ }
+
+ if (get_basic_spec(request, &host_spec, &container_spec, sysinfo) != 0) {
cc = ISULAD_ERR_INPUT;
goto clean_container_root_dir;
}
@@ -1540,7 +1540,7 @@ int container_create_cb(const container_create_request *request, container_creat
goto clean_netns;
}
- if (verify_container_settings(oci_spec) != 0) {
+ if (verify_container_settings(oci_spec, sysinfo) != 0) {
ERROR("Failed to verify container settings");
cc = ISULAD_ERR_EXEC;
goto umount_channel;
diff --git a/src/daemon/executor/container_cb/execution_extend.c b/src/daemon/executor/container_cb/execution_extend.c
index de017b4e..25ec5d3b 100644
--- a/src/daemon/executor/container_cb/execution_extend.c
+++ b/src/daemon/executor/container_cb/execution_extend.c
@@ -1110,8 +1110,15 @@ static int update_host_config_check(container_t *cont, host_config *hostconfig)
{
int ret = 0;
const char *id = cont->common_config->id;
+ __isula_auto_sysinfo_t sysinfo_t *sysinfo = NULL;
- ret = verify_host_config_settings(hostconfig, true);
+ sysinfo = get_sys_info(true);
+ if (sysinfo == NULL) {
+ ERROR("Failed to get system info for updating container %s", id);
+ return -1;
+ }
+
+ ret = verify_host_config_settings(hostconfig, sysinfo, true);
if (ret != 0) {
return -1;
}
diff --git a/src/daemon/modules/spec/verify.c b/src/daemon/modules/spec/verify.c
index 2a8b3259..b9e3c606 100644
--- a/src/daemon/modules/spec/verify.c
+++ b/src/daemon/modules/spec/verify.c
@@ -41,7 +41,6 @@
#include "constants.h"
#include "err_msg.h"
#include "isula_libutils/log.h"
-#include "sysinfo.h"
#include "selinux_label.h"
#include "image_api.h"
#include "utils.h"
@@ -1614,16 +1613,13 @@ out:
}
/* verify container settings */
-int verify_container_settings(const oci_runtime_spec *container)
+int verify_container_settings(const oci_runtime_spec *container, const sysinfo_t *sysinfo)
{
int ret = 0;
- sysinfo_t *sysinfo = NULL;
- sysinfo = get_sys_info(true);
- if (sysinfo == NULL) {
- ERROR("Can not get system info");
- ret = -1;
- goto out;
+ if (container == NULL || sysinfo == NULL) {
+ ERROR("Invalid input arguments for verifying container settings");
+ return -1;
}
if (!util_valid_host_name(container->hostname)) {
@@ -1987,16 +1983,9 @@ static int host_config_settings_restart_policy(const host_config *hostconfig)
return verify_restart_policy_name(rp, hostconfig);
}
-static int host_config_settings_with_sysinfo(host_config *hostconfig, bool update)
+static int host_config_settings_with_sysinfo(host_config *hostconfig, const sysinfo_t *sysinfo, bool update)
{
int ret = 0;
- sysinfo_t *sysinfo = NULL;
-
- sysinfo = get_sys_info(true);
- if (sysinfo == NULL) {
- ERROR("Can not get system info");
- return -1;
- }
ret = verify_host_config_hugetlbs(sysinfo, &(hostconfig->hugetlbs), &(hostconfig->hugetlbs_len));
if (ret != 0) {
@@ -2055,7 +2044,7 @@ out:
}
/* verify host config settings */
-int verify_host_config_settings(host_config *hostconfig, bool update)
+int verify_host_config_settings(host_config *hostconfig, const sysinfo_t *sysinfo, bool update)
{
int ret = 0;
#ifdef ENABLE_USERNS_REMAP
@@ -2066,6 +2055,13 @@ int verify_host_config_settings(host_config *hostconfig, bool update)
goto out;
}
+ if (sysinfo == NULL) {
+ ERROR("Invalid sysinfo for verifying host config settings");
+ isulad_set_error_message("Invalid sysinfo for verifying host config settings");
+ ret = -1;
+ goto out;
+ }
+
#ifdef ENABLE_USERNS_REMAP
if (userns_remap != NULL && hostconfig->user_remap != NULL) {
ERROR("invalid --user-remap command option, daemon already configed --userns-remap");
@@ -2081,7 +2077,7 @@ int verify_host_config_settings(host_config *hostconfig, bool update)
goto out;
}
- ret = host_config_settings_with_sysinfo(hostconfig, update);
+ ret = host_config_settings_with_sysinfo(hostconfig, sysinfo, update);
if (ret != 0) {
goto out;
}
diff --git a/src/daemon/modules/spec/verify.h b/src/daemon/modules/spec/verify.h
index 21e8fba8..0224f9fb 100644
--- a/src/daemon/modules/spec/verify.h
+++ b/src/daemon/modules/spec/verify.h
@@ -20,18 +20,19 @@
#include "isula_libutils/oci_runtime_spec.h"
#include "isula_libutils/host_config.h"
#include "isula_libutils/container_config.h"
+#include "sysinfo.h"
#ifdef __cplusplus
extern "C" {
#endif
-int verify_container_settings(const oci_runtime_spec *container);
+int verify_container_settings(const oci_runtime_spec *container, const sysinfo_t *sysinfo);
int verify_oci_hook(const oci_runtime_spec_hooks *h);
int verify_container_settings_start(const oci_runtime_spec *oci_spec);
-int verify_host_config_settings(host_config *hostconfig, bool update);
+int verify_host_config_settings(host_config *hostconfig, const sysinfo_t *sysinfo, bool update);
int verify_container_config(const container_config *container_spec, const char *runtime);
diff --git a/test/mocks/sysinfo_mock.cc b/test/mocks/sysinfo_mock.cc
index f9abc786..d8f33f84 100644
--- a/test/mocks/sysinfo_mock.cc
+++ b/test/mocks/sysinfo_mock.cc
@@ -63,6 +63,14 @@ char *validate_hugetlb(const char *pagesize, uint64_t limit)
return nullptr;
}
+sysinfo_t *get_sys_info(bool quiet)
+{
+ if (g_sysinfo_mock != nullptr) {
+ return g_sysinfo_mock->GetSysInfo(quiet);
+ }
+ return nullptr;
+}
+
void free_sysinfo(sysinfo_t *sysinfo)
{
if (g_sysinfo_mock != nullptr) {
diff --git a/test/mocks/sysinfo_mock.h b/test/mocks/sysinfo_mock.h
index 45208b0f..2b8e926d 100644
--- a/test/mocks/sysinfo_mock.h
+++ b/test/mocks/sysinfo_mock.h
@@ -26,6 +26,7 @@ public:
MOCK_METHOD1(FreeMountsInfo, void(mountinfo_t **minfos));
MOCK_METHOD0(GetDefaultHugePageSize, char *(void));
MOCK_METHOD2(ValidateHugetlb, char*(const char *pagesize, uint64_t limit));
+ MOCK_METHOD1(GetSysInfo, sysinfo_t *(bool quiet));
MOCK_METHOD1(FreeSysinfo, void(sysinfo_t *sysinfo));
};
diff --git a/test/mocks/verify_mock.cc b/test/mocks/verify_mock.cc
index 0e7e7461..4c481676 100644
--- a/test/mocks/verify_mock.cc
+++ b/test/mocks/verify_mock.cc
@@ -24,10 +24,10 @@ void MockVerify_SetMock(MockVerify *mock)
g_verify_mock = mock;
}
-int verify_host_config_settings(host_config *hostconfig, bool update)
+int verify_host_config_settings(host_config *hostconfig, const sysinfo_t *sysinfo, bool update)
{
if (g_verify_mock != nullptr) {
- return g_verify_mock->VerifyHostConfigSettings(hostconfig, update);
+ return g_verify_mock->VerifyHostConfigSettings(hostconfig, sysinfo, update);
}
return 0;
}
diff --git a/test/mocks/verify_mock.h b/test/mocks/verify_mock.h
index 7890159f..b9ad8627 100644
--- a/test/mocks/verify_mock.h
+++ b/test/mocks/verify_mock.h
@@ -21,7 +21,7 @@
class MockVerify {
public:
- MOCK_METHOD2(VerifyHostConfigSettings, int(host_config *hostconfig, bool update));
+ MOCK_METHOD3(VerifyHostConfigSettings, int(host_config *hostconfig, const sysinfo_t *sysinfo, bool update));
};
void MockVerify_SetMock(MockVerify* mock);
--
2.42.0
|