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