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
|
From 734fca150e1c5da2814a55e0315bde8e828e6e8a Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Fri, 17 Feb 2023 16:07:53 +0800
Subject: [PATCH 14/53] add retry for read/write
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
src/cmd/isulad-shim/common.c | 6 +++---
src/cmd/isulad-shim/process.c | 2 +-
src/cmd/isulad/main.c | 4 ++--
src/daemon/common/selinux_label.c | 2 +-
src/daemon/entry/connect/grpc/grpc_containers_service.cc | 9 ++++++---
src/daemon/entry/cri/sysctl_tools.c | 2 +-
src/daemon/executor/container_cb/execution.c | 4 ++--
.../modules/container/container_gc/containers_gc.c | 3 ++-
src/daemon/modules/events_sender/event_sender.c | 2 +-
src/daemon/modules/image/oci/storage/storage.c | 4 +++-
src/daemon/modules/log/log_gather.c | 6 +++---
src/daemon/modules/plugin/plugin.c | 2 +-
src/daemon/modules/runtime/isula/isula_rt_ops.c | 4 ++--
src/daemon/modules/service/io_handler.c | 2 +-
src/daemon/modules/service/service_container.c | 2 +-
src/utils/cutils/utils.c | 2 +-
src/utils/cutils/utils_aes.c | 2 +-
src/utils/cutils/utils_file.c | 2 +-
src/utils/tar/util_archive.c | 4 ++--
src/utils/tar/util_gzip.c | 2 +-
20 files changed, 36 insertions(+), 30 deletions(-)
diff --git a/src/cmd/isulad-shim/common.c b/src/cmd/isulad-shim/common.c
index bb8464bb..0c345187 100644
--- a/src/cmd/isulad-shim/common.c
+++ b/src/cmd/isulad-shim/common.c
@@ -196,7 +196,7 @@ int generate_random_str(char *id, size_t len)
}
for (i = 0; i < len; i++) {
int nret;
- if (read(fd, &num, sizeof(int)) < 0) {
+ if (read_nointr(fd, &num, sizeof(int)) < 0) {
close(fd);
return SHIM_ERR;
}
@@ -232,8 +232,8 @@ void write_message(int fd, const char *level, const char *fmt, ...)
va_end(arg_list);
snprintf(msg, MAX_MESSAGE_LEN - 1, "{\"level\": \"%s\", \"msg\": \"%s\"}\n", level, buf);
- nwrite = write(fd, msg, strlen(msg));
- if (nwrite != strlen(msg)) {
+ nwrite = write_nointr_in_total(fd, msg, strlen(msg));
+ if (nwrite < 0 || (size_t)nwrite != strlen(msg)) {
return;
}
}
diff --git a/src/cmd/isulad-shim/process.c b/src/cmd/isulad-shim/process.c
index 8a0aa142..02ce3c85 100644
--- a/src/cmd/isulad-shim/process.c
+++ b/src/cmd/isulad-shim/process.c
@@ -283,7 +283,7 @@ static void *do_io_copy(void *data)
break;
}
- int r_count = read(ioc->fd_from, buf, DEFAULT_IO_COPY_BUF);
+ int r_count = util_read_nointr(ioc->fd_from, buf, DEFAULT_IO_COPY_BUF);
if (r_count == -1) {
if (errno == EAGAIN || errno == EINTR) {
continue;
diff --git a/src/cmd/isulad/main.c b/src/cmd/isulad/main.c
index b17657c5..a75fb189 100644
--- a/src/cmd/isulad/main.c
+++ b/src/cmd/isulad/main.c
@@ -482,8 +482,8 @@ int check_and_save_pid(const char *fn)
goto out;
}
- len = (int)write(fd, pidbuf, strlen(pidbuf));
- if (len < 0) {
+ len = util_write_nointr(fd, pidbuf, strlen(pidbuf));
+ if (len < 0 || len != strlen(pidbuf)) {
ERROR("Failed to write pid to file:%s: %s", fn, strerror(errno));
ret = -1;
}
diff --git a/src/daemon/common/selinux_label.c b/src/daemon/common/selinux_label.c
index 24294780..173f3acb 100644
--- a/src/daemon/common/selinux_label.c
+++ b/src/daemon/common/selinux_label.c
@@ -310,7 +310,7 @@ static int get_random_value(unsigned int range, unsigned int *val)
return -1;
}
- if (read(fd, &num, sizeof(int)) < 0) {
+ if (util_read_nointr(fd, &num, sizeof(int)) < 0) {
ERROR("Failed to read urandom value\n");
ret = -1;
goto out;
diff --git a/src/daemon/entry/connect/grpc/grpc_containers_service.cc b/src/daemon/entry/connect/grpc/grpc_containers_service.cc
index c0210ed9..eb79223b 100644
--- a/src/daemon/entry/connect/grpc/grpc_containers_service.cc
+++ b/src/daemon/entry/connect/grpc/grpc_containers_service.cc
@@ -292,7 +292,8 @@ Status ContainerServiceImpl::RemoteStart(ServerContext *context,
break;
}
const std::string &command = request.stdin();
- if (write(read_pipe_fd[1], (void *)(command.c_str()), command.length()) < 0) {
+ int nret = util_write_nointr_in_total(read_pipe_fd[1], command.c_str(), command.length());
+ if (nret < 0 || (size_t)nret != command.length()) {
ERROR("sub write over!");
break;
}
@@ -407,7 +408,8 @@ public:
}
for (int i = 0; i < request.cmd_size(); i++) {
std::string command = request.cmd(i);
- if (write(m_read_pipe_fd, (void *)(command.c_str()), command.length()) < 0) {
+ int nret = util_write_nointr_in_total(m_read_pipe_fd, command.c_str(), command.length());
+ if (nret < 0 || (size_t)nret != command.length()) {
ERROR("sub write over!");
return;
}
@@ -629,7 +631,8 @@ Status ContainerServiceImpl::Attach(ServerContext *context, ServerReaderWriter<A
break;
}
std::string command = request.stdin();
- if (write(pipefd[1], (void *)(command.c_str()), command.length()) < 0) {
+ int nret = util_write_nointr_in_total(pipefd[1], command.c_str(), command.length());
+ if (nret < 0 || (size_t)nret != command.length()) {
ERROR("sub write over!");
break;
}
diff --git a/src/daemon/entry/cri/sysctl_tools.c b/src/daemon/entry/cri/sysctl_tools.c
index 257ccf8f..3c558fa1 100644
--- a/src/daemon/entry/cri/sysctl_tools.c
+++ b/src/daemon/entry/cri/sysctl_tools.c
@@ -99,7 +99,7 @@ int set_sysctl(const char *sysctl, int new_value, char **err)
goto free_out;
}
rsize = util_write_nointr(fd, buff, strlen(buff));
- if (rsize <= 0) {
+ if (rsize < 0 || (size_t)rsize != strlen(buff)) {
if (asprintf(err, "Write new value failed: %s", strerror(errno)) < 0) {
*err = util_strdup_s("Out of memory");
}
diff --git a/src/daemon/executor/container_cb/execution.c b/src/daemon/executor/container_cb/execution.c
index ed70fc14..198052d3 100644
--- a/src/daemon/executor/container_cb/execution.c
+++ b/src/daemon/executor/container_cb/execution.c
@@ -348,7 +348,7 @@ static int maybe_create_cpu_realtime_file(int64_t value, const char *file, const
return -1;
}
nwrite = util_write_nointr(fd, buf, strlen(buf));
- if (nwrite < 0 || nwrite != strlen(buf)) {
+ if (nwrite < 0 || (size_t)nwrite != strlen(buf)) {
ERROR("Failed to write %s to %s: %s", buf, fpath, strerror(errno));
isulad_set_error_message("Failed to write '%s' to '%s': %s", buf, fpath, strerror(errno));
return -1;
@@ -451,7 +451,7 @@ static int container_start_prepare(container_t *cont, const container_start_requ
// init cgroup path for cpu_rt_runtime and cpu_rt_period
// we should do this in start container, not create container
- // because, in scenarios:
+ // because, in scenarios:
// 1. enable cpu-rt of isulad;
// 2. then run container with --cpu-rt-runtime
// 3. then reboot machine;
diff --git a/src/daemon/modules/container/container_gc/containers_gc.c b/src/daemon/modules/container/container_gc/containers_gc.c
index 8c858a96..9feb6d3c 100644
--- a/src/daemon/modules/container/container_gc/containers_gc.c
+++ b/src/daemon/modules/container/container_gc/containers_gc.c
@@ -88,7 +88,8 @@ static int save_gc_config(const char *json_gc_config)
goto out;
}
- if (write(fd, json_gc_config, strlen(json_gc_config)) == -1) {
+ nret = util_write_nointr(fd, json_gc_config, strlen(json_gc_config));
+ if (nret < 0 || (size_t)nret != strlen(json_gc_config)) {
ERROR("write %s failed: %s", filename, strerror(errno));
ret = -1;
}
diff --git a/src/daemon/modules/events_sender/event_sender.c b/src/daemon/modules/events_sender/event_sender.c
index 03dcbbf3..a3903f3e 100644
--- a/src/daemon/modules/events_sender/event_sender.c
+++ b/src/daemon/modules/events_sender/event_sender.c
@@ -58,7 +58,7 @@ static void isulad_monitor_fifo_send(const struct monitord_msg *msg)
do {
ret = util_write_nointr(fd, msg, sizeof(struct monitord_msg));
- if (ret != sizeof(struct monitord_msg)) {
+ if (ret < 0 || (size_t)ret != sizeof(struct monitord_msg)) {
util_usleep_nointerupt(1000);
}
} while (ret != sizeof(struct monitord_msg));
diff --git a/src/daemon/modules/image/oci/storage/storage.c b/src/daemon/modules/image/oci/storage/storage.c
index 829ea8d0..2f4bdf5f 100644
--- a/src/daemon/modules/image/oci/storage/storage.c
+++ b/src/daemon/modules/image/oci/storage/storage.c
@@ -1429,6 +1429,7 @@ static int do_add_checked_layer(const char *lid, int fd, map_t *checked_layers)
bool default_value = true;
char buf[PATH_MAX] = { 0 };
int ret = 0;
+ int nret;
if (strlen(lid) >= PATH_MAX - 1) {
ERROR("Invalid layer id: %s", lid);
@@ -1438,7 +1439,8 @@ static int do_add_checked_layer(const char *lid, int fd, map_t *checked_layers)
(void)memcpy(buf, lid, strlen(lid));
buf[strlen(lid)] = '\n';
// save checked layer ids into file
- if (util_write_nointr(fd, buf, strlen(lid) + 1) < 0) {
+ nret = util_write_nointr(fd, buf, strlen(lid) + 1);
+ if (nret < 0 || (size_t)nret != strlen(lid) + 1) {
ERROR("Write checked layer data failed: %s", strerror(errno));
ret = -1;
goto out;
diff --git a/src/daemon/modules/log/log_gather.c b/src/daemon/modules/log/log_gather.c
index 49facaa2..414c9ad1 100644
--- a/src/daemon/modules/log/log_gather.c
+++ b/src/daemon/modules/log/log_gather.c
@@ -183,9 +183,9 @@ static int write_into_file(const void *buf, size_t g_log_size)
return -1;
}
}
- ret = (int)write(g_log_fd, buf, g_log_size);
- if (ret <= 0) {
- return ret;
+ ret = util_write_nointr_in_total(g_log_fd, buf, g_log_size);
+ if (ret < 0 || (size_t)ret != g_log_size) {
+ return -1;
}
write_size += ret;
diff --git a/src/daemon/modules/plugin/plugin.c b/src/daemon/modules/plugin/plugin.c
index 53afeeaf..1c0af368 100644
--- a/src/daemon/modules/plugin/plugin.c
+++ b/src/daemon/modules/plugin/plugin.c
@@ -618,7 +618,7 @@ static int process_plugin_events(int inotify_fd, const char *plugin_dir)
struct inotify_event *plugin_event = NULL;
char buffer[8192 + 1] = { 0 };
int action = 0;
- events_length = read(inotify_fd, buffer, 8192);
+ events_length = util_read_nointr(inotify_fd, buffer, 8192);
if (events_length <= 0) {
ERROR("Failed to wait events");
diff --git a/src/daemon/modules/runtime/isula/isula_rt_ops.c b/src/daemon/modules/runtime/isula/isula_rt_ops.c
index 76e3bcb7..5463bb1b 100644
--- a/src/daemon/modules/runtime/isula/isula_rt_ops.c
+++ b/src/daemon/modules/runtime/isula/isula_rt_ops.c
@@ -1363,8 +1363,8 @@ int rt_isula_exec_resize(const char *id, const char *runtime, const rt_exec_resi
goto out;
}
- count = write(fd, data, RESIZE_DATA_SIZE);
- if (count <= 0) {
+ count = util_write_nointr(fd, data, strlen(data));
+ if (count < 0 || (size_t)count != strlen(data)) {
ERROR("write exec resize data error");
ret = -1;
goto out;
diff --git a/src/daemon/modules/service/io_handler.c b/src/daemon/modules/service/io_handler.c
index 893733bc..98c763a4 100644
--- a/src/daemon/modules/service/io_handler.c
+++ b/src/daemon/modules/service/io_handler.c
@@ -340,7 +340,7 @@ static ssize_t write_to_fd(void *context, const void *data, size_t len)
{
ssize_t ret;
ret = util_write_nointr(*(int *)context, data, len);
- if ((ret <= 0) || (ret != (ssize_t)len)) {
+ if (ret < 0 || (size_t)ret != len) {
ERROR("Failed to write: %s", strerror(errno));
return -1;
}
diff --git a/src/daemon/modules/service/service_container.c b/src/daemon/modules/service/service_container.c
index eeb035a0..cc777411 100644
--- a/src/daemon/modules/service/service_container.c
+++ b/src/daemon/modules/service/service_container.c
@@ -345,7 +345,7 @@ static int write_env_content(const char *env_path, const char **env, size_t env_
goto out;
}
nret = util_write_nointr(fd, env_content, strlen(env_content));
- if (nret < 0 || nret != len - 1) {
+ if (nret < 0 || (size_t)nret != strlen(env_content)) {
SYSERROR("Write env file failed");
free(env_content);
ret = -1;
diff --git a/src/utils/cutils/utils.c b/src/utils/cutils/utils.c
index de636bcb..f99b28e4 100644
--- a/src/utils/cutils/utils.c
+++ b/src/utils/cutils/utils.c
@@ -1251,7 +1251,7 @@ int util_generate_random_str(char *id, size_t len)
}
for (i = 0; i < len; i++) {
int nret;
- if (read(fd, &num, sizeof(int)) < 0) {
+ if (util_read_nointr(fd, &num, sizeof(int)) < 0) {
ERROR("Failed to read urandom value");
close(fd);
return -1;
diff --git a/src/utils/cutils/utils_aes.c b/src/utils/cutils/utils_aes.c
index 1e25ecd3..055a9538 100644
--- a/src/utils/cutils/utils_aes.c
+++ b/src/utils/cutils/utils_aes.c
@@ -77,7 +77,7 @@ int util_aes_key(const char *key_file, bool create, unsigned char *aeskey)
goto out;
}
- if (read(fd, aeskey, AES_256_CFB_KEY_LEN) != AES_256_CFB_KEY_LEN) {
+ if (util_read_nointr(fd, aeskey, AES_256_CFB_KEY_LEN) != AES_256_CFB_KEY_LEN) {
ERROR("read key file %s failed: %s", key_file, strerror(errno));
ret = -1;
goto out;
diff --git a/src/utils/cutils/utils_file.c b/src/utils/cutils/utils_file.c
index cdd712a7..34c5b060 100644
--- a/src/utils/cutils/utils_file.c
+++ b/src/utils/cutils/utils_file.c
@@ -998,7 +998,7 @@ int util_file2str(const char *filename, char *buf, size_t len)
if (fd == -1) {
return -1;
}
- num_read = (int)read(fd, buf, len - 1);
+ num_read = (int)util_read_nointr(fd, buf, len - 1);
if (num_read <= 0) {
num_read = -1;
} else {
diff --git a/src/utils/tar/util_archive.c b/src/utils/tar/util_archive.c
index 2d56d8a7..7ace2924 100644
--- a/src/utils/tar/util_archive.c
+++ b/src/utils/tar/util_archive.c
@@ -662,7 +662,7 @@ child_out:
if (ret != 0) {
ERROR("Wait archive_untar_handler failed with error:%s", strerror(errno));
fcntl(pipe_stderr[0], F_SETFL, O_NONBLOCK);
- if (read(pipe_stderr[0], errbuf, BUFSIZ) < 0) {
+ if (util_read_nointr(pipe_stderr[0], errbuf, BUFSIZ) < 0) {
ERROR("read error message from child failed");
}
}
@@ -1057,7 +1057,7 @@ child_out:
if (ret != 0) {
ERROR("tar failed");
fcntl(pipe_for_read[0], F_SETFL, O_NONBLOCK);
- if (read(pipe_for_read[0], errbuf, BUFSIZ) < 0) {
+ if (util_read_nointr(pipe_for_read[0], errbuf, BUFSIZ) < 0) {
ERROR("read error message from child failed");
}
}
diff --git a/src/utils/tar/util_gzip.c b/src/utils/tar/util_gzip.c
index 5c34d719..2f4750be 100644
--- a/src/utils/tar/util_gzip.c
+++ b/src/utils/tar/util_gzip.c
@@ -212,7 +212,7 @@ int gzip(const char *filename, size_t len)
return -1;
}
- size_read = read(pipefd[0], buffer, BUFSIZ);
+ size_read = util_read_nointr(pipefd[0], buffer, BUFSIZ);
close(pipefd[0]);
if (size_read) {
--
2.25.1
|