Skip to content

Commit 305ec62

Browse files
cfriedtnashif
authored andcommitted
posix: device_io: implement pselect()
Implement pselect() as it's required by POSIX_DEVICE_IO Signed-off-by: Chris Friedt <[email protected]>
1 parent 49ac191 commit 305ec62

File tree

6 files changed

+28
-10
lines changed

6 files changed

+28
-10
lines changed

include/zephyr/net/socket_select.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ typedef struct zvfs_fd_set zsock_fd_set;
5151
static inline int zsock_select(int nfds, zsock_fd_set *readfds, zsock_fd_set *writefds,
5252
zsock_fd_set *exceptfds, struct zsock_timeval *timeout)
5353
{
54-
struct timeval;
54+
struct timespec to = {
55+
.tv_sec = (timeout == NULL) ? 0 : timeout->tv_sec,
56+
.tv_nsec = (long)((timeout == NULL) ? 0 : timeout->tv_usec * NSEC_PER_USEC)};
5557

56-
return zvfs_select(nfds, readfds, writefds, exceptfds, (struct timeval *)timeout);
58+
return zvfs_select(nfds, (struct zvfs_fd_set *)readfds, (struct zvfs_fd_set *)writefds,
59+
(struct zvfs_fd_set *)exceptfds, (timeout == NULL) ? NULL : &to, NULL);
5760
}
5861

5962
/** Number of file descriptors which can be added to zsock_fd_set */

include/zephyr/posix/sys/select.h

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ extern "C" {
2020

2121
struct timeval;
2222

23+
int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
24+
const struct timespec *timeout, const void *sigmask);
2325
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);
2426
void FD_CLR(int fd, fd_set *fdset);
2527
int FD_ISSET(int fd, fd_set *fdset);

include/zephyr/sys/fdtable.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ struct timespec;
223223
__syscall int zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds,
224224
struct zvfs_fd_set *ZRESTRICT writefds,
225225
struct zvfs_fd_set *ZRESTRICT errorfds,
226-
const struct timeval *ZRESTRICT timeout);
226+
const struct timespec *ZRESTRICT timeout, const void *ZRESTRICT sigmask);
227227

228228
/**
229229
* Request codes for fd_op_vtable.ioctl().

lib/os/zvfs/zvfs_select.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void ZVFS_FD_SET(int fd, struct zvfs_fd_set *set)
7777
int z_impl_zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds,
7878
struct zvfs_fd_set *ZRESTRICT writefds,
7979
struct zvfs_fd_set *ZRESTRICT exceptfds,
80-
const struct timeval *ZRESTRICT timeout)
80+
const struct timespec *ZRESTRICT timeout, const void *ZRESTRICT sigmask)
8181
{
8282
struct zvfs_pollfd pfds[CONFIG_ZVFS_POLL_MAX];
8383
k_timeout_t poll_timeout;
@@ -142,7 +142,8 @@ int z_impl_zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds,
142142
if (timeout == NULL) {
143143
poll_timeout = K_FOREVER;
144144
} else {
145-
poll_timeout = K_USEC(timeout->tv_sec * USEC_PER_SEC + timeout->tv_usec);
145+
poll_timeout =
146+
K_USEC(timeout->tv_sec * USEC_PER_SEC + timeout->tv_nsec / NSEC_PER_USEC);
146147
}
147148

148149
res = zvfs_poll_internal(pfds, num_pfds, poll_timeout);
@@ -220,10 +221,11 @@ int z_impl_zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds,
220221
static int z_vrfy_zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds,
221222
struct zvfs_fd_set *ZRESTRICT writefds,
222223
struct zvfs_fd_set *ZRESTRICT exceptfds,
223-
const struct timeval *ZRESTRICT timeout)
224+
const struct timespec *ZRESTRICT timeout,
225+
const void *ZRESTRICT sigmask)
224226
{
225227
struct zvfs_fd_set *readfds_copy = NULL, *writefds_copy = NULL, *exceptfds_copy = NULL;
226-
struct timeval *to = NULL;
228+
struct timespec *to = NULL;
227229
int ret = -1;
228230

229231
if (readfds) {
@@ -261,7 +263,7 @@ static int z_vrfy_zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds,
261263
}
262264
}
263265

264-
ret = z_impl_zvfs_select(nfds, readfds_copy, writefds_copy, exceptfds_copy, to);
266+
ret = z_impl_zvfs_select(nfds, readfds_copy, writefds_copy, exceptfds_copy, to, sigmask);
265267

266268
if (ret >= 0) {
267269
if (readfds_copy) {

lib/posix/options/device_io.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ ssize_t pread(int fd, void *buf, size_t count, off_t offset)
7171
return zvfs_read(fd, buf, count, (size_t *)&off);
7272
}
7373

74+
int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
75+
const struct timespec *timeout, const void *sigmask)
76+
{
77+
return zvfs_select(nfds, readfds, writefds, exceptfds, timeout, sigmask);
78+
}
79+
7480
ssize_t pwrite(int fd, void *buf, size_t count, off_t offset)
7581
{
7682
size_t off = (size_t)offset;
@@ -93,7 +99,12 @@ FUNC_ALIAS(read, _read, ssize_t);
9399

94100
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
95101
{
96-
return zvfs_select(nfds, readfds, writefds, exceptfds, timeout);
102+
struct timespec to = {
103+
.tv_sec = (timeout == NULL) ? 0 : timeout->tv_sec,
104+
.tv_nsec = (long)((timeout == NULL) ? 0 : timeout->tv_usec * NSEC_PER_USEC)};
105+
106+
return zvfs_select(nfds, readfds, writefds, exceptfds, (timeout == NULL) ? NULL : &to,
107+
NULL);
97108
}
98109

99110
ssize_t write(int fd, const void *buf, size_t sz)

tests/posix/headers/src/sys_select_h.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ZTEST(posix_headers, test_sys_select_h)
3030
FD_SET(0, &fds);
3131
FD_ZERO(&fds);
3232

33-
/* zassert_not_null(pselect); */ /* not implemented */
33+
zassert_not_null(pselect);
3434
zassert_not_null(select);
3535
}
3636
}

0 commit comments

Comments
 (0)