Clean-up: sessiond: make some accesses to conditions const
[lttng-tools.git] / src / common / readwrite.c
1 /*
2 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <assert.h>
10 #include <limits.h>
11 #include <unistd.h>
12
13 #include <common/compat/errno.h>
14
15 #include "readwrite.h"
16
17 /*
18 * lttng_read and lttng_write take care of EINTR and partial read/write.
19 * Upon success, they return the "count" received as parameter.
20 * They can return a negative value if an error occurs.
21 * If a value lower than the requested "count" is returned, it means an
22 * error occurred.
23 * The error can be checked by querying errno.
24 */
25 LTTNG_HIDDEN
26 ssize_t lttng_read(int fd, void *buf, size_t count)
27 {
28 size_t i = 0;
29 ssize_t ret;
30
31 assert(buf);
32
33 /*
34 * Deny a read count that can be bigger then the returned value max size.
35 * This makes the function to never return an overflow value.
36 */
37 if (count > SSIZE_MAX) {
38 return -EINVAL;
39 }
40
41 do {
42 ret = read(fd, buf + i, count - i);
43 if (ret < 0) {
44 if (errno == EINTR) {
45 continue; /* retry operation */
46 } else {
47 goto error;
48 }
49 }
50 i += ret;
51 assert(i <= count);
52 } while (count - i > 0 && ret > 0);
53 return i;
54
55 error:
56 if (i == 0) {
57 return -1;
58 } else {
59 return i;
60 }
61 }
62
63 LTTNG_HIDDEN
64 ssize_t lttng_write(int fd, const void *buf, size_t count)
65 {
66 size_t i = 0;
67 ssize_t ret;
68
69 assert(buf);
70
71 /*
72 * Deny a write count that can be bigger then the returned value max size.
73 * This makes the function to never return an overflow value.
74 */
75 if (count > SSIZE_MAX) {
76 return -EINVAL;
77 }
78
79 do {
80 ret = write(fd, buf + i, count - i);
81 if (ret < 0) {
82 if (errno == EINTR) {
83 continue; /* retry operation */
84 } else {
85 goto error;
86 }
87 }
88 i += ret;
89 assert(i <= count);
90 } while (count - i > 0 && ret > 0);
91 return i;
92
93 error:
94 if (i == 0) {
95 return -1;
96 } else {
97 return i;
98 }
99 }
This page took 0.030567 seconds and 4 git commands to generate.