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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2011 New Dream Network
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include "common/safe_io.h"
#include "include/compat.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/socket.h>
ssize_t safe_read(int fd, void *buf, size_t count)
{
size_t cnt = 0;
while (cnt < count) {
ssize_t r = read(fd, buf, count - cnt);
if (r <= 0) {
if (r == 0) {
// EOF
return cnt;
}
if (errno == EINTR)
continue;
return -errno;
}
cnt += r;
buf = (char *)buf + r;
}
return cnt;
}
#ifdef _WIN32
// "read" doesn't work with Windows sockets.
ssize_t safe_recv(int fd, void *buf, size_t count)
{
size_t cnt = 0;
while (cnt < count) {
ssize_t r = recv(fd, (SOCKOPT_VAL_TYPE)buf, count - cnt, 0);
if (r <= 0) {
if (r == 0) {
// EOF
return cnt;
}
int err = ceph_sock_errno();
if (err == EAGAIN || err == EINTR) {
continue;
}
return -err;
}
cnt += r;
buf = (char *)buf + r;
}
return cnt;
}
#else
ssize_t safe_recv(int fd, void *buf, size_t count)
{
// We'll use "safe_read" so that this can work with any type of
// file descriptor.
return safe_read(fd, buf, count);
}
#endif /* _WIN32 */
ssize_t safe_read_exact(int fd, void *buf, size_t count)
{
ssize_t ret = safe_read(fd, buf, count);
if (ret < 0)
return ret;
if ((size_t)ret != count)
return -EDOM;
return 0;
}
ssize_t safe_recv_exact(int fd, void *buf, size_t count)
{
ssize_t ret = safe_recv(fd, buf, count);
if (ret < 0)
return ret;
if ((size_t)ret != count)
return -EDOM;
return 0;
}
ssize_t safe_write(int fd, const void *buf, size_t count)
{
while (count > 0) {
ssize_t r = write(fd, buf, count);
if (r < 0) {
if (errno == EINTR)
continue;
return -errno;
}
count -= r;
buf = (char *)buf + r;
}
return 0;
}
#ifdef _WIN32
ssize_t safe_send(int fd, const void *buf, size_t count)
{
while (count > 0) {
ssize_t r = send(fd, (SOCKOPT_VAL_TYPE)buf, count, 0);
if (r < 0) {
int err = ceph_sock_errno();
if (err == EINTR || err == EAGAIN) {
continue;
}
return -err;
}
count -= r;
buf = (char *)buf + r;
}
return 0;
}
#else
ssize_t safe_send(int fd, const void *buf, size_t count)
{
return safe_write(fd, buf, count);
}
#endif /* _WIN32 */
ssize_t safe_pread(int fd, void *buf, size_t count, off_t offset)
{
size_t cnt = 0;
char *b = (char*)buf;
while (cnt < count) {
ssize_t r = pread(fd, b + cnt, count - cnt, offset + cnt);
if (r <= 0) {
if (r == 0) {
// EOF
return cnt;
}
if (errno == EINTR)
continue;
return -errno;
}
cnt += r;
}
return cnt;
}
ssize_t safe_pread_exact(int fd, void *buf, size_t count, off_t offset)
{
ssize_t ret = safe_pread(fd, buf, count, offset);
if (ret < 0)
return ret;
if ((size_t)ret != count)
return -EDOM;
return 0;
}
ssize_t safe_pwrite(int fd, const void *buf, size_t count, off_t offset)
{
while (count > 0) {
ssize_t r = pwrite(fd, buf, count, offset);
if (r < 0) {
if (errno == EINTR)
continue;
return -errno;
}
count -= r;
buf = (char *)buf + r;
offset += r;
}
return 0;
}
#ifdef CEPH_HAVE_SPLICE
ssize_t safe_splice(int fd_in, off_t *off_in, int fd_out, off_t *off_out,
size_t len, unsigned int flags)
{
size_t cnt = 0;
while (cnt < len) {
ssize_t r = splice(fd_in, off_in, fd_out, off_out, len - cnt, flags);
if (r <= 0) {
if (r == 0) {
// EOF
return cnt;
}
if (errno == EINTR)
continue;
if (errno == EAGAIN)
break;
return -errno;
}
cnt += r;
}
return cnt;
}
ssize_t safe_splice_exact(int fd_in, off_t *off_in, int fd_out,
off_t *off_out, size_t len, unsigned int flags)
{
ssize_t ret = safe_splice(fd_in, off_in, fd_out, off_out, len, flags);
if (ret < 0)
return ret;
if ((size_t)ret != len)
return -EDOM;
return 0;
}
#endif
int safe_write_file(const char *base, const char *file,
const char *val, size_t vallen,
unsigned mode)
{
int ret;
char fn[PATH_MAX];
char tmp[PATH_MAX];
int fd;
// does the file already have correct content?
char oldval[80];
ret = safe_read_file(base, file, oldval, sizeof(oldval));
if (ret == (int)vallen && memcmp(oldval, val, vallen) == 0)
return 0; // yes.
snprintf(fn, sizeof(fn), "%s/%s", base, file);
snprintf(tmp, sizeof(tmp), "%s/%s.tmp", base, file);
fd = open(tmp, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, mode);
if (fd < 0) {
ret = errno;
return -ret;
}
ret = safe_write(fd, val, vallen);
if (ret) {
VOID_TEMP_FAILURE_RETRY(close(fd));
return ret;
}
ret = fsync(fd);
if (ret < 0) ret = -errno;
VOID_TEMP_FAILURE_RETRY(close(fd));
if (ret < 0) {
unlink(tmp);
return ret;
}
ret = rename(tmp, fn);
if (ret < 0) {
ret = -errno;
unlink(tmp);
return ret;
}
fd = open(base, O_RDONLY|O_BINARY);
if (fd < 0) {
ret = -errno;
return ret;
}
ret = fsync(fd);
if (ret < 0) ret = -errno;
VOID_TEMP_FAILURE_RETRY(close(fd));
return ret;
}
int safe_read_file(const char *base, const char *file,
char *val, size_t vallen)
{
char fn[PATH_MAX];
int fd, len;
snprintf(fn, sizeof(fn), "%s/%s", base, file);
fd = open(fn, O_RDONLY|O_BINARY);
if (fd < 0) {
return -errno;
}
len = safe_read(fd, val, vallen);
if (len < 0) {
VOID_TEMP_FAILURE_RETRY(close(fd));
return len;
}
// close sometimes returns errors, but only after write()
VOID_TEMP_FAILURE_RETRY(close(fd));
return len;
}
|