The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
iovec.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/** Functions for a basic binary heaps
18 *
19 * @file src/lib/util/iovec.c
20 *
21 * @copyright 2023 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
22 */
23RCSID("$Id: acbbb1cdc606ba41c6dd8e6f7922a991d2034526 $")
24
25#include <freeradius-devel/util/iovec.h>
26#include <freeradius-devel/util/strerror.h>
27#include <freeradius-devel/util/syserror.h>
28
29/** Concatenate an iovec into a dbuff
30 *
31 * @param[out] out dbuff to write to.
32 * @param[in] vector to concatenate.
33 * @param[in] iovcnt length of vector array.
34 * @return
35 * - >= 0 on success.
36 * - <0 on failure.
37 */
38fr_slen_t fr_concatv(fr_dbuff_t *out, struct iovec vector[], int iovcnt)
39{
40 int i;
41
42 fr_dbuff_t our_out = FR_DBUFF(out);
43
44 for (i = 0; i < iovcnt; i++) FR_DBUFF_IN_MEMCPY_RETURN(&our_out,
45 (uint8_t *)vector[i].iov_base, vector[i].iov_len);
46
47 return fr_dbuff_set(out, &our_out);
48}
49
50/** Write out a vector to a file descriptor
51 *
52 * Wraps writev, calling it as necessary. If timeout is not NULL,
53 * timeout is applied to each call that returns EAGAIN or EWOULDBLOCK
54 *
55 * @note Should only be used on nonblocking file descriptors.
56 * @note Socket should likely be closed on timeout.
57 * @note iovec may be modified in such a way that it's not reusable.
58 * @note Leaves errno set to the last error that occurred.
59 *
60 * @param fd to write to.
61 * @param vector to write.
62 * @param iovcnt number of elements in iovec.
63 * @param timeout how long to wait for fd to become writable before timing out.
64 * @return
65 * - Number of bytes written.
66 * - -1 on failure.
67 */
68ssize_t fr_writev(int fd, struct iovec vector[], int iovcnt, fr_time_delta_t timeout)
69{
70 struct iovec *vector_p = vector;
71 ssize_t total = 0;
72
73 while (iovcnt > 0) {
74 ssize_t wrote;
75
76 wrote = writev(fd, vector_p, iovcnt);
77 if (wrote > 0) {
78 total += wrote;
79 while (wrote > 0) {
80 /*
81 * An entire vector element was written
82 */
83 if (wrote >= (ssize_t)vector_p->iov_len) {
84 iovcnt--;
85 wrote -= vector_p->iov_len;
86 vector_p++;
87 continue;
88 }
89
90 /*
91 * Partial vector element was written
92 */
93 vector_p->iov_len -= wrote;
94 vector_p->iov_base = ((char *)vector_p->iov_base) + wrote;
95 break;
96 }
97 continue;
98 } else if (wrote == 0) {
99 /* coverity[return_overflow] */
100 return total;
101 }
102
103 switch (errno) {
104 /* Write operation would block, use select() to implement a timeout */
105#if EWOULDBLOCK != EAGAIN
106 case EWOULDBLOCK:
107 case EAGAIN:
108#else
109 case EAGAIN:
110#endif
111 {
112 int ret;
113 fd_set write_set;
114
115 FD_ZERO(&write_set);
116 FD_SET(fd, &write_set);
117
118 /* Don't let signals mess up the select */
119 do {
120 ret = select(fd + 1, NULL, &write_set, NULL, &(fr_time_delta_to_timeval(timeout)));
121 } while ((ret == -1) && (errno == EINTR));
122
123 /* Select returned 0 which means it reached the timeout */
124 if (ret == 0) {
125 fr_strerror_const("Write timed out");
126 return -1;
127 }
128
129 /* Other select error */
130 if (ret < 0) {
131 fr_strerror_printf("Failed waiting on socket: %s", fr_syserror(errno));
132 return -1;
133 }
134
135 /* select said a file descriptor was ready for writing */
136 if (!fr_cond_assert(FD_ISSET(fd, &write_set))) return -1;
137
138 break;
139 }
140
141 default:
142 return -1;
143 }
144 }
145
146 return total;
147}
#define RCSID(id)
Definition build.h:483
#define fr_dbuff_set(_dst, _src)
Set the 'current' position in a dbuff or marker using another dbuff or marker, a char pointer,...
Definition dbuff.h:1004
#define FR_DBUFF_IN_MEMCPY_RETURN(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1382
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:222
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:139
ssize_t fr_writev(int fd, struct iovec vector[], int iovcnt, fr_time_delta_t timeout)
Write out a vector to a file descriptor.
Definition iovec.c:68
fr_slen_t fr_concatv(fr_dbuff_t *out, struct iovec vector[], int iovcnt)
Concatenate an iovec into a dbuff.
Definition iovec.c:38
long int ssize_t
unsigned char uint8_t
ssize_t fr_slen_t
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_time_delta_to_timeval(_delta)
Convert a delta to a timeval.
Definition time.h:656
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
static size_t char ** out
Definition value.h:997