forked from aws/aws-fpga
-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.c
359 lines (289 loc) · 6.2 KB
/
io.c
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
/*
* Copyright 2015-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may
* not use this file except in compliance with the License. A copy of the
* License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
/** @file
* Wrappers for I/O functions
*/
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/uio.h>
#include <unistd.h>
#include <utils/io.h>
#include <utils/log.h>
int
epoll_init(int *epoll_fd)
{
int rc;
/*
* Create epoll file descriptor.
*
* @note The size field of epoll_create is ignored since Linux 2.6.8.
* It is initialized to 73 because that is the best number
* (there is only one correct answer) according to Dr. Sheldon Cooper.
*/
rc = epoll_create(73);
fail_on(rc == -1, err, "Cannot create epoll");
*epoll_fd = rc;
return 0;
err:
return -1;
}
int
epoll_add(int epoll_fd, struct epoll_cb *cb)
{
int rc;
struct epoll_event ev;
assert(cb && cb->fn);
memset(&ev, 0, sizeof(ev));
ev.events = EPOLLIN;
ev.data.ptr = cb;
rc = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, cb->fd, &ev);
if (rc)
log_error("Cannot add fd %d to epoll fd %d", cb->fd, epoll_fd);
return rc;
}
int
epoll_remove(int epoll_fd, struct epoll_cb *cb)
{
fail_on(cb->fd < 0, err, "Invalid fd");
return epoll_ctl(epoll_fd, EPOLL_CTL_DEL, cb->fd, NULL);
err:
return -1;
}
int
sread_loop(int fd, void *buffer, size_t size, size_t *rsize)
{
ssize_t cur = 0;
uint8_t *buf = buffer;
for (size_t received = 0; received < size; received += cur) {
do {
cur = read(fd, buf + received, size - received);
} while (cur == -1 && errno == EINTR);
if (cur < 0) {
if (rsize)
*rsize = received;
return -1;
}
if (cur == 0) {
if (rsize)
*rsize = received;
return 1;
}
}
if (rsize)
*rsize = size;
return 0;
}
int
write_loop(int fd, const void *buffer, size_t size)
{
ssize_t cur = 0;
const uint8_t *buf = buffer;
/* Catch non-initialized variables */
assert(fd);
for (size_t sent = 0; sent < size; sent += cur) {
do {
cur = write(fd, buf + sent, size - sent);
} while (cur == -1 && errno == EINTR);
if (cur < 0)
return -1;
if (cur == 0)
return 1;
}
return 0;
}
int
readv_loop(int fd, struct iovec *iov, int iov_len)
{
ssize_t rc;
size_t size;
retry:
size = 0;
for (int i = 0; i < iov_len; i++)
size += iov[i].iov_len;
do {
rc = readv(fd, iov, iov_len);
} while (rc == -1 && errno == EINTR);
if (rc < 0)
return -1;
if ((size_t)rc == size)
return 0;
while (1) {
if ((size_t)rc < iov->iov_len) {
iov->iov_base = (uint8_t *)iov->iov_base + rc;
iov->iov_len -= rc;
break;
}
rc -= iov->iov_len;
iov++;
iov_len--;
}
goto retry;
}
int
writev_loop(int fd, struct iovec *iov, int iov_len)
{
ssize_t rc;
size_t size;
/* Catch non-initialized variables */
assert(fd);
retry:
size = 0;
for (int i = 0; i < iov_len; i++)
size += iov[i].iov_len;
do {
rc = writev(fd, iov, iov_len);
} while (rc == -1 && errno == EINTR);
if (rc < 0)
return -1;
if ((size_t)rc == size)
return 0;
while (1) {
if ((size_t)rc < iov->iov_len) {
iov->iov_base = (uint8_t *)iov->iov_base + rc;
iov->iov_len -= rc;
break;
}
rc -= iov->iov_len;
iov++;
iov_len--;
}
goto retry;
}
int
string_to_int64(int64_t *num, const char *str)
{
char *end;
int64_t val;
/* NULL pointer or empty string? */
if (!str || !*str)
return -1;
errno = 0;
val = strtoll(str, &end, 0); /* Convert string to 64-bit decimal */
/* Didn't consume whole string? */
if (*end)
return -1;
/* Overflow, underflow, etc? */
if (errno)
return -1;
*num = val;
return 0;
}
int
string_to_uint64(uint64_t *num, const char *str)
{
char *end;
uint64_t val;
/* NULL pointer or empty string? */
if (!str || !*str)
return -1;
errno = 0;
val = strtoull(str, &end, 0); /* Convert string to 64-bit decimal */
/* Didn't consume whole string? */
if (*end)
return -1;
/* Overflow, underflow, etc? */
if (errno)
return -1;
*num = val;
return 0;
}
int
string_to_int(int *num, const char *str)
{
char *end;
long int val;
/* NULL pointer or empty string? */
if (!str || !*str)
return -1;
errno = 0;
val = strtol(str, &end, 0); /* Convert string to long int */
/* Didn't consume whole string? */
if (*end)
return -1;
/* Overflow, underflow, etc? */
if (errno)
return -1;
if (val > INT_MAX || val < INT_MIN)
return -1;
*num = (int)val;
return 0;
}
int
string_to_uint(unsigned int *num, const char *str)
{
char *end;
long int val;
/* NULL pointer or empty string? */
if (!str || !*str)
return -1;
errno = 0;
val = strtol(str, &end, 0); /* Convert string to long int */
/* Didn't consume whole string? */
if (*end)
return -1;
/* Overflow, underflow, etc? */
if (errno)
return -1;
if (val > UINT_MAX || val < 0)
return -1;
*num = (unsigned int)val;
return 0;
}
/**
* Counts the number of directory entries.
*
* @param[in] directory_path The path to a directory.
*
* @returns The number of directory entries in a given path.
*/
static int number_of_directory_entries(const char *directory_path)
{
int n = 0;
DIR *dd;
dd = opendir(directory_path);
fail_on(!dd, err, "Cannot open %s", directory_path);
errno = 0;
while (readdir(dd))
n++;
closedir(dd);
fail_on(errno, err, "Errno is set after readdir loop");
return n;
err:
return -1;
}
int number_of_open_files(pid_t pid)
{
int rc, n;
char path[sizeof("/proc/-2147483648/fd")];
rc = snprintf(path, sizeof(path), "/proc/%d/fd", pid);
fail_on((size_t)rc >= sizeof(path), err, "Cannot make PID path");
n = number_of_directory_entries(path);
fail_on(n < 0, err,
"Cannot get number of directory entries in %s", path);
/* Account for ".", ".." */
n -= 2;
/* Acount for opening "/proc/self/fd" if we're checking ourself. */
if (getpid() == pid)
n--;
return n;
err:
return -1;
}