Commit | Line | Data |
---|---|---|
33b14136 MD |
1 | /* |
2 | * Copyright (C) 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
3 | * | |
4 | * This library is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
6 | * as published by the Free Software Foundation. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
11 | * for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public License | |
14 | * along with this library; if not, write to the Free Software Foundation, | |
15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
18 | #include <unistd.h> | |
19 | #include "readwrite.h" | |
20 | ||
21 | /* | |
22 | * lttng_read and lttng_write take care of EINTR and partial read/write. | |
23 | * Upon success, they return the "count" received as parameter. | |
24 | * They can return a negative value if an error occurs. | |
25 | * If a value lower than the requested "count" is returned, it means an | |
26 | * error occured. | |
27 | * The error can be checked by querying errno. | |
28 | */ | |
29 | ssize_t lttng_read(int fd, void *buf, size_t count) | |
30 | { | |
31 | size_t i = 0; | |
32 | ssize_t ret; | |
33 | ||
34 | do { | |
35 | ret = read(fd, &buf[i], count - i); | |
36 | if (ret < 0) { | |
37 | if (errno == EINTR) { | |
38 | continue; /* retry operation */ | |
39 | } else { | |
40 | goto error; | |
41 | } | |
42 | } | |
43 | i += ret; | |
44 | assert(i <= count); | |
45 | } while (count - i > 0 && ret > 0); | |
46 | return i; | |
47 | ||
48 | error: | |
49 | if (i == 0) { | |
50 | return -1; | |
51 | } else { | |
52 | return i; | |
53 | } | |
54 | } | |
55 | ||
56 | ssize_t lttng_write(int fd, const void *buf, size_t count) | |
57 | { | |
58 | size_t i = 0; | |
59 | ssize_t ret; | |
60 | ||
61 | do { | |
62 | ret = write(fd, &buf[i], count - i); | |
63 | if (ret < 0) { | |
64 | if (errno == EINTR) { | |
65 | continue; /* retry operation */ | |
66 | } else { | |
67 | goto error; | |
68 | } | |
69 | } | |
70 | i += ret; | |
71 | assert(i <= count); | |
72 | } while (count - i > 0 && ret > 0); | |
73 | return i; | |
74 | ||
75 | error: | |
76 | if (i == 0) { | |
77 | return -1; | |
78 | } else { | |
79 | return i; | |
80 | } | |
81 | } |