sessiond: defer tracer notification jobs to the action executor
[lttng-tools.git] / src / common / readwrite.c
CommitLineData
33b14136 1/*
ab5be9fa 2 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
33b14136 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
33b14136 5 *
33b14136
MD
6 */
7
6c1c0768 8#define _LGPL_SOURCE
6cd525e8 9#include <assert.h>
636ae5db 10#include <limits.h>
aeb16260
DG
11#include <unistd.h>
12
edf4b93e
MJ
13#include <common/compat/errno.h>
14
33b14136
MD
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
83f4233d 22 * error occurred.
33b14136
MD
23 * The error can be checked by querying errno.
24 */
73ec1cf9 25LTTNG_HIDDEN
33b14136
MD
26ssize_t lttng_read(int fd, void *buf, size_t count)
27{
28 size_t i = 0;
29 ssize_t ret;
30
aeb16260
DG
31 assert(buf);
32
636ae5db
DG
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
33b14136 41 do {
6cd525e8 42 ret = read(fd, buf + i, count - i);
33b14136
MD
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
55error:
56 if (i == 0) {
57 return -1;
58 } else {
59 return i;
60 }
61}
62
73ec1cf9 63LTTNG_HIDDEN
33b14136
MD
64ssize_t lttng_write(int fd, const void *buf, size_t count)
65{
66 size_t i = 0;
67 ssize_t ret;
68
aeb16260
DG
69 assert(buf);
70
636ae5db
DG
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
33b14136 79 do {
6cd525e8 80 ret = write(fd, buf + i, count - i);
33b14136
MD
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
93error:
94 if (i == 0) {
95 return -1;
96 } else {
97 return i;
98 }
99}
This page took 0.054202 seconds and 4 git commands to generate.