/src/freeradius-server/src/lib/util/iovec.c
Line | Count | Source |
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 | | */ |
23 | | RCSID("$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 | | */ |
38 | | fr_slen_t fr_concatv(fr_dbuff_t *out, struct iovec vector[], int iovcnt) |
39 | 0 | { |
40 | 0 | int i; |
41 | |
|
42 | 0 | fr_dbuff_t our_out = FR_DBUFF(out); |
43 | |
|
44 | 0 | for (i = 0; i < iovcnt; i++) FR_DBUFF_IN_MEMCPY_RETURN(&our_out, |
45 | 0 | (uint8_t *)vector[i].iov_base, vector[i].iov_len); |
46 | | |
47 | 0 | return fr_dbuff_set(out, &our_out); |
48 | 0 | } |
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 | | */ |
68 | | ssize_t fr_writev(int fd, struct iovec vector[], int iovcnt, fr_time_delta_t timeout) |
69 | 0 | { |
70 | 0 | struct iovec *vector_p = vector; |
71 | 0 | ssize_t total = 0; |
72 | |
|
73 | 0 | while (iovcnt > 0) { |
74 | 0 | ssize_t wrote; |
75 | |
|
76 | 0 | wrote = writev(fd, vector_p, iovcnt); |
77 | 0 | if (wrote > 0) { |
78 | 0 | total += wrote; |
79 | 0 | while (wrote > 0) { |
80 | | /* |
81 | | * An entire vector element was written |
82 | | */ |
83 | 0 | if (wrote >= (ssize_t)vector_p->iov_len) { |
84 | 0 | iovcnt--; |
85 | 0 | wrote -= vector_p->iov_len; |
86 | 0 | vector_p++; |
87 | 0 | continue; |
88 | 0 | } |
89 | | |
90 | | /* |
91 | | * Partial vector element was written |
92 | | */ |
93 | 0 | vector_p->iov_len -= wrote; |
94 | 0 | vector_p->iov_base = ((char *)vector_p->iov_base) + wrote; |
95 | 0 | break; |
96 | 0 | } |
97 | 0 | continue; |
98 | 0 | } else if (wrote == 0) { |
99 | | /* coverity[return_overflow] */ |
100 | 0 | return total; |
101 | 0 | } |
102 | | |
103 | 0 | 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 | 0 | case EAGAIN: |
110 | 0 | #endif |
111 | 0 | { |
112 | 0 | int ret; |
113 | 0 | fd_set write_set; |
114 | |
|
115 | 0 | FD_ZERO(&write_set); |
116 | 0 | FD_SET(fd, &write_set); |
117 | | |
118 | | /* Don't let signals mess up the select */ |
119 | 0 | do { |
120 | 0 | ret = select(fd + 1, NULL, &write_set, NULL, &(fr_time_delta_to_timeval(timeout))); |
121 | 0 | } while ((ret == -1) && (errno == EINTR)); |
122 | | |
123 | | /* Select returned 0 which means it reached the timeout */ |
124 | 0 | if (ret == 0) { |
125 | 0 | fr_strerror_const("Write timed out"); |
126 | 0 | return -1; |
127 | 0 | } |
128 | | |
129 | | /* Other select error */ |
130 | 0 | if (ret < 0) { |
131 | 0 | fr_strerror_printf("Failed waiting on socket: %s", fr_syserror(errno)); |
132 | 0 | return -1; |
133 | 0 | } |
134 | | |
135 | | /* select said a file descriptor was ready for writing */ |
136 | 0 | if (!fr_cond_assert(FD_ISSET(fd, &write_set))) return -1; |
137 | | |
138 | 0 | break; |
139 | 0 | } |
140 | | |
141 | 0 | default: |
142 | 0 | return -1; |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | 0 | return total; |
147 | 0 | } |