Use lttng_read/lttng_write wrappers
[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 <unistd.h>
19 #include <errno.h>
20 #include <assert.h>
21 #include "readwrite.h"
22
23 /*
24 * lttng_read and lttng_write take care of EINTR and partial read/write.
25 * Upon success, they return the "count" received as parameter.
26 * They can return a negative value if an error occurs.
27 * If a value lower than the requested "count" is returned, it means an
28 * error occured.
29 * The error can be checked by querying errno.
30 */
31 ssize_t lttng_read(int fd, void *buf, size_t count)
32 {
33 size_t i = 0;
34 ssize_t ret;
35
36 do {
37 ret = read(fd, buf + i, count - i);
38 if (ret < 0) {
39 if (errno == EINTR) {
40 continue; /* retry operation */
41 } else {
42 goto error;
43 }
44 }
45 i += ret;
46 assert(i <= count);
47 } while (count - i > 0 && ret > 0);
48 return i;
49
50 error:
51 if (i == 0) {
52 return -1;
53 } else {
54 return i;
55 }
56 }
57
58 ssize_t lttng_write(int fd, const void *buf, size_t count)
59 {
60 size_t i = 0;
61 ssize_t ret;
62
63 do {
64 ret = write(fd, buf + i, count - i);
65 if (ret < 0) {
66 if (errno == EINTR) {
67 continue; /* retry operation */
68 } else {
69 goto error;
70 }
71 }
72 i += ret;
73 assert(i <= count);
74 } while (count - i > 0 && ret > 0);
75 return i;
76
77 error:
78 if (i == 0) {
79 return -1;
80 } else {
81 return i;
82 }
83 }
This page took 0.029971 seconds and 4 git commands to generate.