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