Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
fdtable.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Linaro Limited
3 * Copyright (c) 2025 Antmicro
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7#ifndef ZEPHYR_INCLUDE_SYS_FDTABLE_H_
8#define ZEPHYR_INCLUDE_SYS_FDTABLE_H_
9
10#include <stdarg.h>
11#include <time.h>
12
13/* FIXME: For native_posix ssize_t, off_t. */
14#include <sys/types.h>
15#include <zephyr/kernel.h>
16#include <zephyr/sys/util.h>
17
18#ifdef CONFIG_PICOLIBC
19#define ZVFS_O_APPEND 0x0400
20#define ZVFS_O_CREAT 0x0040
21#define ZVFS_O_TRUNC 0x0200
22#else
23#define ZVFS_O_APPEND 0x0008
24#define ZVFS_O_CREAT 0x0200
25#define ZVFS_O_TRUNC 0x0400
26#endif
27
28#define ZVFS_O_RDONLY 00
29#define ZVFS_O_WRONLY 01
30#define ZVFS_O_RDWR 02
31
32#define ZVFS_O_EXCL 0x0800
33#define ZVFS_O_NONBLOCK 0x4000
34
35#define ZVFS_F_DUPFD 0
36#define ZVFS_F_GETFL 3
37#define ZVFS_F_SETFL 4
38
39/* File mode bits */
40#define ZVFS_MODE_IFMT 0170000
41#define ZVFS_MODE_UNSPEC 0000000
42#define ZVFS_MODE_IFIFO 0010000
43#define ZVFS_MODE_IFCHR 0020000
44#define ZVFS_MODE_IMSGQ 0030000
45#define ZVFS_MODE_IFDIR 0040000
46#define ZVFS_MODE_IFSEM 0050000
47#define ZVFS_MODE_IFBLK 0060000
48#define ZVFS_MODE_IFSHM 0070000
49#define ZVFS_MODE_IFREG 0100000
50#define ZVFS_MODE_IFLNK 0120000
51#define ZVFS_MODE_IFSOCK 0140000
52
53#define ZVFS_POLLIN BIT(0)
54#define ZVFS_POLLPRI BIT(1)
55#define ZVFS_POLLOUT BIT(2)
56#define ZVFS_POLLERR BIT(3)
57#define ZVFS_POLLHUP BIT(4)
58#define ZVFS_POLLNVAL BIT(5)
59
60/* FIXME: use k_off_t and k_ssize_t to avoid the POSIX->Zephyr->POSIX dependency cycle */
61#ifdef CONFIG_NEWLIB_LIBC
62#ifndef _OFF_T_DECLARED
63typedef __off_t off_t;
64#define _OFF_T_DECLARED
65#endif
66#ifndef _SSIZE_T_DECLARED
67typedef _ssize_t ssize_t;
68#define _SSIZE_T_DECLARED
69#endif
70#endif
71
72#include <zephyr/fs/fs.h>
73
74#ifdef __cplusplus
75extern "C" {
76#endif
77
78struct zvfs_pollfd;
79
85 union {
86 ssize_t (*read)(void *obj, void *buf, size_t sz);
87 ssize_t (*read_offs)(void *obj, void *buf, size_t sz, size_t offset);
88 };
89 union {
90 ssize_t (*write)(void *obj, const void *buf, size_t sz);
91 ssize_t (*write_offs)(void *obj, const void *buf, size_t sz, size_t offset);
92 };
93 union {
94 int (*close)(void *obj);
95 int (*close2)(void *obj, int fd);
96 };
97 int (*ioctl)(void *obj, unsigned int request, va_list args);
99 int (*poll_prepare)(void *obj, struct zvfs_pollfd *pfd, struct k_poll_event **pev,
100 struct k_poll_event *pev_end);
102 int (*poll_update)(void *obj, struct zvfs_pollfd *pfd, struct k_poll_event **pev);
104 int (*poll_offload)(void *obj, struct zvfs_pollfd *fds, int nfds, int timeout);
105};
106
108
109/* Called by zvfs_close() for every ZVFS_MODE_IFSOCK descriptor, implemented by the socket layer. */
110#if defined(CONFIG_NET_SOCKETS)
111void zvfs_socket_close_hook(int fd);
112#else
113static inline void zvfs_socket_close_hook(int fd)
114{
115 ARG_UNUSED(fd);
116}
117#endif /* CONFIG_NET_SOCKETS */
118
120
132
149void zvfs_finalize_typed_fd(int fd, void *obj, const struct fd_op_vtable *vtable, uint32_t mode);
150
161static inline void zvfs_finalize_fd(int fd, void *obj, const struct fd_op_vtable *vtable)
162{
164}
165
177int zvfs_alloc_fd(void *obj, const struct fd_op_vtable *vtable);
178
187void zvfs_free_fd(int fd);
188
205void *zvfs_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err);
206
219void *zvfs_get_fd_obj_and_vtable(int fd, const struct fd_op_vtable **vtable,
220 struct k_mutex **lock);
221
237bool zvfs_get_obj_lock_and_cond(void *obj, const struct fd_op_vtable *vtable, struct k_mutex **lock,
238 struct k_condvar **cond);
239
252static inline int zvfs_fdtable_call_ioctl(const struct fd_op_vtable *vtable, void *obj,
253 unsigned long request, ...)
254{
255 va_list args;
256 int res;
257
258 va_start(args, request);
259 res = vtable->ioctl(obj, request, args);
260 va_end(args);
261
262 return res;
263}
264
273
275 int fd;
276 short events;
277 short revents;
278};
279
280__syscall int zvfs_poll(struct zvfs_pollfd *fds, int nfds, int poll_timeout);
281
283#ifdef ZVFS_OPEN_SIZE
284 uint32_t bitset[DIV_ROUND_UP(ZVFS_OPEN_SIZE, 32)];
285#else
287#endif
288};
289
291#define ZVFS_FD_SETSIZE (sizeof(((struct zvfs_fd_set *)0)->bitset) * 8)
292
293void ZVFS_FD_CLR(int fd, struct zvfs_fd_set *fdset);
294int ZVFS_FD_ISSET(int fd, struct zvfs_fd_set *fdset);
295void ZVFS_FD_SET(int fd, struct zvfs_fd_set *fdset);
296void ZVFS_FD_ZERO(struct zvfs_fd_set *fdset);
297
298__syscall int zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds,
299 struct zvfs_fd_set *ZRESTRICT writefds,
300 struct zvfs_fd_set *ZRESTRICT errorfds,
301 const struct timespec *ZRESTRICT timeout, const void *ZRESTRICT sigmask);
302
311enum {
312 /* Codes below 0x100 are reserved for fcntl() codes. */
322
323 /* Codes above 0x5400 and below 0x5500 are reserved for termios, FIO, etc */
330};
331
349static inline int zvfs_fdtable_call_poll_prepare(const struct fd_op_vtable *vtable, void *obj,
350 struct zvfs_pollfd *pfd, struct k_poll_event **pev,
351 struct k_poll_event *pev_end)
352{
353 int ret;
354
355 if (vtable->poll_prepare != NULL) {
356 return vtable->poll_prepare(obj, pfd, pev, pev_end);
357 }
358
359 TOOLCHAIN_DISABLE_WARNING(TOOLCHAIN_WARNING_DEPRECATED_DECLARATIONS)
360 ret = zvfs_fdtable_call_ioctl(vtable, obj, ZFD_IOCTL_POLL_PREPARE, pfd, pev, pev_end);
361 TOOLCHAIN_ENABLE_WARNING(TOOLCHAIN_WARNING_DEPRECATED_DECLARATIONS)
362
363 return ret;
364}
365
381static inline int zvfs_fdtable_call_poll_update(const struct fd_op_vtable *vtable, void *obj,
382 struct zvfs_pollfd *pfd, struct k_poll_event **pev)
383{
384 int ret;
385
386 if (vtable->poll_update != NULL) {
387 return vtable->poll_update(obj, pfd, pev);
388 }
389
390 TOOLCHAIN_DISABLE_WARNING(TOOLCHAIN_WARNING_DEPRECATED_DECLARATIONS)
391 ret = zvfs_fdtable_call_ioctl(vtable, obj, ZFD_IOCTL_POLL_UPDATE, pfd, pev);
392 TOOLCHAIN_ENABLE_WARNING(TOOLCHAIN_WARNING_DEPRECATED_DECLARATIONS)
393
394 return ret;
395}
396
410static inline int zvfs_fdtable_call_poll_offload(const struct fd_op_vtable *vtable, void *obj,
411 struct zvfs_pollfd *fds, int nfds, int timeout)
412{
413 int ret;
414
415 if (vtable->poll_offload != NULL) {
416 return vtable->poll_offload(obj, fds, nfds, timeout);
417 }
418
419 TOOLCHAIN_DISABLE_WARNING(TOOLCHAIN_WARNING_DEPRECATED_DECLARATIONS)
420 ret = zvfs_fdtable_call_ioctl(vtable, obj, ZFD_IOCTL_POLL_OFFLOAD, fds, nfds, timeout);
421 TOOLCHAIN_ENABLE_WARNING(TOOLCHAIN_WARNING_DEPRECATED_DECLARATIONS)
422
423 return ret;
424}
425
435int zvfs_open(const char *name, int flags, struct fd_op_vtable *vtable);
436
445int zvfs_fstat(int fd, struct zvfs_stat *buf);
446
454int zvfs_close(int fd);
455
467ssize_t zvfs_read(int fd, void *buf, size_t sz, const size_t *from_offset);
468
480ssize_t zvfs_write(int fd, const void *buf, size_t sz, const size_t *from_offset);
481
482#ifdef CONFIG_ZVFS_DEFAULT_FILE_VMETHODS
483int zvfs_ioctl_vmeth(void *obj, unsigned int request, va_list args);
484int zvfs_close_vmeth(void *obj);
485ssize_t zvfs_write_vmeth(void *obj, const void *buffer, size_t count);
486ssize_t zvfs_read_vmeth(void *obj, void *buffer, size_t count);
487#endif
488
496int zvfs_unlink(const char *path);
497
506int zvfs_rename(const char *old, const char *newp);
507
508#ifdef __cplusplus
509}
510#endif
511
512#include <zephyr/syscalls/fdtable.h>
513
514#endif /* ZEPHYR_INCLUDE_SYS_FDTABLE_H_ */
static int zvfs_fdtable_call_poll_update(const struct fd_op_vtable *vtable, void *obj, struct zvfs_pollfd *pfd, struct k_poll_event **pev)
Call the poll update vmethod on an object.
Definition fdtable.h:381
int zvfs_alloc_fd(void *obj, const struct fd_op_vtable *vtable)
Allocate file descriptor for underlying I/O object.
void ZVFS_FD_CLR(int fd, struct zvfs_fd_set *fdset)
int zvfs_reserve_fd(void)
Reserve file descriptor.
void * zvfs_get_fd_obj_and_vtable(int fd, const struct fd_op_vtable **vtable, struct k_mutex **lock)
Get underlying object pointer and vtable pointer from file descriptor.
static void zvfs_finalize_fd(int fd, void *obj, const struct fd_op_vtable *vtable)
Finalize creation of file descriptor.
Definition fdtable.h:161
int zvfs_poll(struct zvfs_pollfd *fds, int nfds, int poll_timeout)
ssize_t zvfs_write(int fd, const void *buf, size_t sz, const size_t *from_offset)
Write bytes to file.
int zvfs_close(int fd)
Close file.
int zvfs_fstat(int fd, struct zvfs_stat *buf)
Get information about file.
int zvfs_open(const char *name, int flags, struct fd_op_vtable *vtable)
Open a file with a given name.
void zvfs_finalize_typed_fd(int fd, void *obj, const struct fd_op_vtable *vtable, uint32_t mode)
Finalize creation of file descriptor, with type.
#define ZVFS_MODE_UNSPEC
Definition fdtable.h:41
void ZVFS_FD_ZERO(struct zvfs_fd_set *fdset)
bool zvfs_get_obj_lock_and_cond(void *obj, const struct fd_op_vtable *vtable, struct k_mutex **lock, struct k_condvar **cond)
Get the mutex and condition variable associated with the given object and vtable.
void zvfs_free_fd(int fd)
Release reserved file descriptor.
int zvfs_unlink(const char *path)
Delete file or directory.
int zvfs_rename(const char *old, const char *newp)
Rename file or directory, moving it if necessary.
static int zvfs_fdtable_call_ioctl(const struct fd_op_vtable *vtable, void *obj, unsigned long request,...)
Call ioctl vmethod on an object using varargs.
Definition fdtable.h:252
int ZVFS_FD_ISSET(int fd, struct zvfs_fd_set *fdset)
void * zvfs_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err)
Get underlying object pointer from file descriptor.
static int zvfs_fdtable_call_poll_offload(const struct fd_op_vtable *vtable, void *obj, struct zvfs_pollfd *fds, int nfds, int timeout)
Call the poll offload vmethod on an object.
Definition fdtable.h:410
ssize_t zvfs_read(int fd, void *buf, size_t sz, const size_t *from_offset)
Read bytes from file.
int zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds, struct zvfs_fd_set *ZRESTRICT writefds, struct zvfs_fd_set *ZRESTRICT errorfds, const struct timespec *ZRESTRICT timeout, const void *ZRESTRICT sigmask)
@ ZFD_IOCTL_FIONWRITE
Get the number of bytes queued for TCP TX which have not yet been acknowledged.
Definition fdtable.h:329
@ ZFD_IOCTL_SET_LOCK
Definition fdtable.h:318
@ ZFD_IOCTL_FIONREAD
Get the number of bytes available to read.
Definition fdtable.h:325
@ ZFD_IOCTL_POLL_PREPARE
Definition fdtable.h:315
@ ZFD_IOCTL_FSYNC
Definition fdtable.h:313
@ ZFD_IOCTL_LSEEK
Definition fdtable.h:314
@ ZFD_IOCTL_FIONBIO
Set non-blocking mode.
Definition fdtable.h:327
@ ZFD_IOCTL_POLL_OFFLOAD
Definition fdtable.h:317
@ ZFD_IOCTL_POLL_UPDATE
Definition fdtable.h:316
@ ZFD_IOCTL_TRUNCATE
Definition fdtable.h:320
@ ZFD_IOCTL_STAT
Definition fdtable.h:319
@ ZFD_IOCTL_MMAP
Definition fdtable.h:321
static int zvfs_fdtable_call_poll_prepare(const struct fd_op_vtable *vtable, void *obj, struct zvfs_pollfd *pfd, struct k_poll_event **pev, struct k_poll_event *pev_end)
Call the poll prepare vmethod on an object.
Definition fdtable.h:349
void ZVFS_FD_SET(int fd, struct zvfs_fd_set *fdset)
Public API for the file system subsystem.
#define DIV_ROUND_UP(n, d)
Divide and round up.
Definition util.h:336
#define TOOLCHAIN_DISABLE_WARNING(warning)
Disable the specified compiler warning for all compilers.
Definition toolchain.h:318
#define TOOLCHAIN_ENABLE_WARNING(warning)
Re-enable the specified compiler warning for all compilers.
Definition toolchain.h:330
Public kernel APIs.
__SIZE_TYPE__ ssize_t
Definition types.h:28
__INTPTR_TYPE__ off_t
Definition types.h:36
flags
Definition parser.h:86
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
File descriptor virtual method table.
Definition fdtable.h:84
int(* poll_update)(void *obj, struct zvfs_pollfd *pfd, struct k_poll_event **pev)
Update poll results for an object.
Definition fdtable.h:102
int(* close2)(void *obj, int fd)
Definition fdtable.h:95
ssize_t(* write_offs)(void *obj, const void *buf, size_t sz, size_t offset)
Definition fdtable.h:91
int(* poll_prepare)(void *obj, struct zvfs_pollfd *pfd, struct k_poll_event **pev, struct k_poll_event *pev_end)
Prepare poll events for an object.
Definition fdtable.h:99
ssize_t(* read_offs)(void *obj, void *buf, size_t sz, size_t offset)
Definition fdtable.h:87
int(* close)(void *obj)
Definition fdtable.h:94
ssize_t(* read)(void *obj, void *buf, size_t sz)
Definition fdtable.h:86
int(* poll_offload)(void *obj, struct zvfs_pollfd *fds, int nfds, int timeout)
Poll using an offloaded implementation.
Definition fdtable.h:104
ssize_t(* write)(void *obj, const void *buf, size_t sz)
Definition fdtable.h:90
int(* ioctl)(void *obj, unsigned int request, va_list args)
Definition fdtable.h:97
Kernel condition variable structure.
Definition kernel.h:3897
Kernel mutex structure.
Definition kernel.h:3764
Poll Event.
Definition kernel.h:7108
Definition posix_signal.h:53
Definition fdtable.h:282
uint32_t bitset[1]
Definition fdtable.h:286
Definition fdtable.h:274
short events
Definition fdtable.h:276
short revents
Definition fdtable.h:277
int fd
Definition fdtable.h:275
Structure for result of the ZFD_IOCTL_STAT ioctl call.
Definition fdtable.h:269
uint32_t mode
Definition fdtable.h:271
off_t size
Definition fdtable.h:270
Misc utilities.