The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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  */
23 RCSID("$Id: ce65a28ed43999cd68ecf632d2fdf5846c5be42e $")
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 {
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  */
68 ssize_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 #ifdef __COVERITY__
79  /*
80  * Coverity thinks that total += wrote might underflow,
81  * which causes an issue at the return.
82  */
83  if (total + wrote < total) return -1;
84 #endif
85  total += wrote;
86  while (wrote > 0) {
87  /*
88  * An entire vector element was written
89  */
90  if (wrote >= (ssize_t)vector_p->iov_len) {
91  iovcnt--;
92  wrote -= vector_p->iov_len;
93  vector_p++;
94  continue;
95  }
96 
97  /*
98  * Partial vector element was written
99  */
100  vector_p->iov_len -= wrote;
101  vector_p->iov_base = ((char *)vector_p->iov_base) + wrote;
102  break;
103  }
104  continue;
105  } else if (wrote == 0) return total;
106 
107  switch (errno) {
108  /* Write operation would block, use select() to implement a timeout */
109 #if EWOULDBLOCK != EAGAIN
110  case EWOULDBLOCK:
111  case EAGAIN:
112 #else
113  case EAGAIN:
114 #endif
115  {
116  int ret;
117  fd_set write_set;
118 
119  FD_ZERO(&write_set);
120  FD_SET(fd, &write_set);
121 
122  /* Don't let signals mess up the select */
123  do {
124  ret = select(fd + 1, NULL, &write_set, NULL, &(fr_time_delta_to_timeval(timeout)));
125  } while ((ret == -1) && (errno == EINTR));
126 
127  /* Select returned 0 which means it reached the timeout */
128  if (ret == 0) {
129  fr_strerror_const("Write timed out");
130  return -1;
131  }
132 
133  /* Other select error */
134  if (ret < 0) {
135  fr_strerror_printf("Failed waiting on socket: %s", fr_syserror(errno));
136  return -1;
137  }
138 
139  /* select said a file descriptor was ready for writing */
140  if (!fr_cond_assert(FD_ISSET(fd, &write_set))) return -1;
141 
142  break;
143  }
144 
145  default:
146  return -1;
147  }
148  }
149 
150  return total;
151 }
#define RCSID(id)
Definition: build.h:481
#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
static fr_time_delta_t timeout
Definition: dhcpclient.c:54
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
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
ssize_t fr_slen_t
Definition: merged_model.c:35
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
return fr_dbuff_set(dbuff, &our_dbuff)
static size_t char ** out
Definition: value.h:997