Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | /*
* Copyright (c) 2018 Linaro Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/syscall_handler.h>
#include <zephyr/sys/math_extras.h>
#include <zephyr/net/socket.h>
#include "sockets_internal.h"
/* Get size, in elements, of an array within a struct. */
#define STRUCT_MEMBER_ARRAY_SIZE(type, field) ARRAY_SIZE(((type *)0)->field)
/* Returns results in word_idx and bit_mask "output" params */
#define FD_SET_CALC_OFFSETS(set, word_idx, bit_mask) { \
unsigned int b_idx = fd % (sizeof(set->bitset[0]) * 8); \
word_idx = fd / (sizeof(set->bitset[0]) * 8); \
bit_mask = 1 << b_idx; \
}
void ZSOCK_FD_ZERO(zsock_fd_set *set)
{
int i;
for (i = 0; i < ARRAY_SIZE(set->bitset); i++) {
set->bitset[i] = 0U;
}
}
int ZSOCK_FD_ISSET(int fd, zsock_fd_set *set)
{
uint32_t word_idx, bit_mask;
if (fd < 0 || fd >= ZSOCK_FD_SETSIZE) {
return 0;
}
FD_SET_CALC_OFFSETS(set, word_idx, bit_mask);
return (set->bitset[word_idx] & bit_mask) != 0U;
}
void ZSOCK_FD_CLR(int fd, zsock_fd_set *set)
{
uint32_t word_idx, bit_mask;
if (fd < 0 || fd >= ZSOCK_FD_SETSIZE) {
return;
}
FD_SET_CALC_OFFSETS(set, word_idx, bit_mask);
set->bitset[word_idx] &= ~bit_mask;
}
void ZSOCK_FD_SET(int fd, zsock_fd_set *set)
{
uint32_t word_idx, bit_mask;
if (fd < 0 || fd >= ZSOCK_FD_SETSIZE) {
return;
}
FD_SET_CALC_OFFSETS(set, word_idx, bit_mask);
set->bitset[word_idx] |= bit_mask;
}
int z_impl_zsock_select(int nfds, zsock_fd_set *readfds, zsock_fd_set *writefds,
zsock_fd_set *exceptfds, struct zsock_timeval *timeout)
{
struct zsock_pollfd pfds[CONFIG_NET_SOCKETS_POLL_MAX];
k_timeout_t poll_timeout;
int i, res;
int num_pfds = 0;
int num_selects = 0;
int fd_no = 0;
for (i = 0; i < STRUCT_MEMBER_ARRAY_SIZE(zsock_fd_set, bitset); i++) {
uint32_t bit_mask = 1U;
uint32_t read_mask = 0U, write_mask = 0U, except_mask = 0U;
uint32_t ored_mask;
if (readfds != NULL) {
read_mask = readfds->bitset[i];
}
if (writefds != NULL) {
write_mask = writefds->bitset[i];
}
if (exceptfds != NULL) {
except_mask = exceptfds->bitset[i];
}
ored_mask = read_mask | write_mask | except_mask;
if (ored_mask == 0U) {
fd_no += sizeof(ored_mask) * 8;
continue;
}
do {
if (ored_mask & bit_mask) {
int events = 0;
if (num_pfds >= ARRAY_SIZE(pfds)) {
errno = ENOMEM;
return -1;
}
if (read_mask & bit_mask) {
events |= ZSOCK_POLLIN;
}
if (write_mask & bit_mask) {
events |= ZSOCK_POLLOUT;
}
if (except_mask & bit_mask) {
events |= ZSOCK_POLLPRI;
}
pfds[num_pfds].fd = fd_no;
pfds[num_pfds++].events = events;
}
bit_mask <<= 1;
fd_no++;
} while (bit_mask != 0U);
}
if (timeout == NULL) {
poll_timeout = K_FOREVER;
} else {
poll_timeout =
K_USEC(timeout->tv_sec * 1000000UL + timeout->tv_usec);
}
res = zsock_poll_internal(pfds, num_pfds, poll_timeout);
if (res == -1) {
return -1;
}
if (readfds != NULL) {
ZSOCK_FD_ZERO(readfds);
}
if (writefds != NULL) {
ZSOCK_FD_ZERO(writefds);
}
if (exceptfds != NULL) {
ZSOCK_FD_ZERO(exceptfds);
}
for (i = 0; i < num_pfds && res > 0; i++) {
short revents = pfds[i].revents;
int fd = pfds[i].fd;
if (revents == 0) {
continue;
}
/* POSIX: "EBADF: One or more of the file descriptor sets
* specified a file descriptor that is not a valid open
* file descriptor."
* So, unlike poll(), a single invalid fd aborts the entire
* select().
*/
if (revents & ZSOCK_POLLNVAL) {
errno = EBADF;
return -1;
}
if (revents & ZSOCK_POLLIN) {
if (readfds != NULL) {
ZSOCK_FD_SET(fd, readfds);
num_selects++;
}
}
if (revents & ZSOCK_POLLOUT) {
if (writefds != NULL) {
ZSOCK_FD_SET(fd, writefds);
num_selects++;
}
}
/* It's unclear if HUP/ERR belong here. At least not ignore
* them. Zephyr doesn't use HUP and barely use ERR so far.
*/
if (revents & (ZSOCK_POLLPRI | ZSOCK_POLLHUP | ZSOCK_POLLERR)) {
if (exceptfds != NULL) {
ZSOCK_FD_SET(fd, exceptfds);
num_selects++;
}
}
res--;
}
return num_selects;
}
#ifdef CONFIG_USERSPACE
static int z_vrfy_zsock_select(int nfds, zsock_fd_set *readfds,
zsock_fd_set *writefds,
zsock_fd_set *exceptfds,
struct zsock_timeval *timeout)
{
zsock_fd_set *readfds_copy = NULL, *writefds_copy = NULL,
*exceptfds_copy = NULL;
struct zsock_timeval *timeval = NULL;
int ret = -1;
if (readfds) {
readfds_copy = z_user_alloc_from_copy((void *)readfds,
sizeof(zsock_fd_set));
if (!readfds_copy) {
errno = ENOMEM;
goto out;
}
}
if (writefds) {
writefds_copy = z_user_alloc_from_copy((void *)writefds,
sizeof(zsock_fd_set));
if (!writefds_copy) {
errno = ENOMEM;
goto out;
}
}
if (exceptfds) {
exceptfds_copy = z_user_alloc_from_copy((void *)exceptfds,
sizeof(zsock_fd_set));
if (!exceptfds_copy) {
errno = ENOMEM;
goto out;
}
}
if (timeout) {
timeval = z_user_alloc_from_copy((void *)timeout,
sizeof(struct zsock_timeval));
if (!timeval) {
errno = ENOMEM;
goto out;
}
}
ret = z_impl_zsock_select(nfds, readfds_copy, writefds_copy,
exceptfds_copy, timeval);
if (ret >= 0) {
if (readfds_copy) {
z_user_to_copy((void *)readfds, readfds_copy,
sizeof(zsock_fd_set));
}
if (writefds_copy) {
z_user_to_copy((void *)writefds, writefds_copy,
sizeof(zsock_fd_set));
}
if (exceptfds_copy) {
z_user_to_copy((void *)exceptfds, exceptfds_copy,
sizeof(zsock_fd_set));
}
}
out:
k_free(timeval);
k_free(readfds_copy);
k_free(writefds_copy);
k_free(exceptfds_copy);
return ret;
}
#include <syscalls/zsock_select_mrsh.c>
#endif
|