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 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
6cd525e8 | 19 | #include <assert.h> |
aeb16260 | 20 | #include <errno.h> |
636ae5db | 21 | #include <limits.h> |
aeb16260 DG |
22 | #include <unistd.h> |
23 | ||
33b14136 MD |
24 | #include "readwrite.h" |
25 | ||
26 | /* | |
27 | * lttng_read and lttng_write take care of EINTR and partial read/write. | |
28 | * Upon success, they return the "count" received as parameter. | |
29 | * They can return a negative value if an error occurs. | |
30 | * If a value lower than the requested "count" is returned, it means an | |
83f4233d | 31 | * error occurred. |
33b14136 MD |
32 | * The error can be checked by querying errno. |
33 | */ | |
73ec1cf9 | 34 | LTTNG_HIDDEN |
33b14136 MD |
35 | ssize_t lttng_read(int fd, void *buf, size_t count) |
36 | { | |
37 | size_t i = 0; | |
38 | ssize_t ret; | |
39 | ||
aeb16260 DG |
40 | assert(buf); |
41 | ||
636ae5db DG |
42 | /* |
43 | * Deny a read count that can be bigger then the returned value max size. | |
44 | * This makes the function to never return an overflow value. | |
45 | */ | |
46 | if (count > SSIZE_MAX) { | |
47 | return -EINVAL; | |
48 | } | |
49 | ||
33b14136 | 50 | do { |
6cd525e8 | 51 | ret = read(fd, buf + i, count - i); |
33b14136 MD |
52 | if (ret < 0) { |
53 | if (errno == EINTR) { | |
54 | continue; /* retry operation */ | |
55 | } else { | |
56 | goto error; | |
57 | } | |
58 | } | |
59 | i += ret; | |
60 | assert(i <= count); | |
61 | } while (count - i > 0 && ret > 0); | |
62 | return i; | |
63 | ||
64 | error: | |
65 | if (i == 0) { | |
66 | return -1; | |
67 | } else { | |
68 | return i; | |
69 | } | |
70 | } | |
71 | ||
73ec1cf9 | 72 | LTTNG_HIDDEN |
33b14136 MD |
73 | ssize_t lttng_write(int fd, const void *buf, size_t count) |
74 | { | |
75 | size_t i = 0; | |
76 | ssize_t ret; | |
77 | ||
aeb16260 DG |
78 | assert(buf); |
79 | ||
636ae5db DG |
80 | /* |
81 | * Deny a write count that can be bigger then the returned value max size. | |
82 | * This makes the function to never return an overflow value. | |
83 | */ | |
84 | if (count > SSIZE_MAX) { | |
85 | return -EINVAL; | |
86 | } | |
87 | ||
33b14136 | 88 | do { |
6cd525e8 | 89 | ret = write(fd, buf + i, count - i); |
33b14136 MD |
90 | if (ret < 0) { |
91 | if (errno == EINTR) { | |
92 | continue; /* retry operation */ | |
93 | } else { | |
94 | goto error; | |
95 | } | |
96 | } | |
97 | i += ret; | |
98 | assert(i <= count); | |
99 | } while (count - i > 0 && ret > 0); | |
100 | return i; | |
101 | ||
102 | error: | |
103 | if (i == 0) { | |
104 | return -1; | |
105 | } else { | |
106 | return i; | |
107 | } | |
108 | } |