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: 8bc22cce061d6fb8d2d880b2a58bbccb78ee7ec2 $")
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  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) return total;
99 
100  switch (errno) {
101  /* Write operation would block, use select() to implement a timeout */
102 #if EWOULDBLOCK != EAGAIN
103  case EWOULDBLOCK:
104  case EAGAIN:
105 #else
106  case EAGAIN:
107 #endif
108  {
109  int ret;
110  fd_set write_set;
111 
112  FD_ZERO(&write_set);
113  FD_SET(fd, &write_set);
114 
115  /* Don't let signals mess up the select */
116  do {
117  ret = select(fd + 1, NULL, &write_set, NULL, &(fr_time_delta_to_timeval(timeout)));
118  } while ((ret == -1) && (errno == EINTR));
119 
120  /* Select returned 0 which means it reached the timeout */
121  if (ret == 0) {
122  fr_strerror_const("Write timed out");
123  return -1;
124  }
125 
126  /* Other select error */
127  if (ret < 0) {
128  fr_strerror_printf("Failed waiting on socket: %s", fr_syserror(errno));
129  return -1;
130  }
131 
132  /* select said a file descriptor was ready for writing */
133  if (!fr_cond_assert(FD_ISSET(fd, &write_set))) return -1;
134 
135  break;
136  }
137 
138  default:
139  return -1;
140  }
141  }
142 
143  return total;
144 }
#define RCSID(id)
Definition: build.h:444
#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:1377
#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:137
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:654
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:984