Zephyr API Documentation 4.3.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
83 union {
84 ssize_t (*read)(void *obj, void *buf, size_t sz);
85 ssize_t (*read_offs)(void *obj, void *buf, size_t sz, size_t offset);
86 };
87 union {
88 ssize_t (*write)(void *obj, const void *buf, size_t sz);
89 ssize_t (*write_offs)(void *obj, const void *buf, size_t sz, size_t offset);
90 };
91 union {
92 int (*close)(void *obj);
93 int (*close2)(void *obj, int fd);
94 };
95 int (*ioctl)(void *obj, unsigned int request, va_list args);
96};
97
109
126void zvfs_finalize_typed_fd(int fd, void *obj, const struct fd_op_vtable *vtable, uint32_t mode);
127
138static inline void zvfs_finalize_fd(int fd, void *obj, const struct fd_op_vtable *vtable)
139{
141}
142
154int zvfs_alloc_fd(void *obj, const struct fd_op_vtable *vtable);
155
164void zvfs_free_fd(int fd);
165
182void *zvfs_get_fd_obj(int fd, const struct fd_op_vtable *vtable, int err);
183
196void *zvfs_get_fd_obj_and_vtable(int fd, const struct fd_op_vtable **vtable,
197 struct k_mutex **lock);
198
214bool zvfs_get_obj_lock_and_cond(void *obj, const struct fd_op_vtable *vtable, struct k_mutex **lock,
215 struct k_condvar **cond);
216
229static inline int zvfs_fdtable_call_ioctl(const struct fd_op_vtable *vtable, void *obj,
230 unsigned long request, ...)
231{
232 va_list args;
233 int res;
234
235 va_start(args, request);
236 res = vtable->ioctl(obj, request, args);
237 va_end(args);
238
239 return res;
240}
241
250
252 int fd;
253 short events;
254 short revents;
255};
256
257__syscall int zvfs_poll(struct zvfs_pollfd *fds, int nfds, int poll_timeout);
258
260 uint32_t bitset[DIV_ROUND_UP(ZVFS_OPEN_SIZE, 32)];
261};
262
264#define ZVFS_FD_SETSIZE (sizeof(((struct zvfs_fd_set *)0)->bitset) * 8)
265
266void ZVFS_FD_CLR(int fd, struct zvfs_fd_set *fdset);
267int ZVFS_FD_ISSET(int fd, struct zvfs_fd_set *fdset);
268void ZVFS_FD_SET(int fd, struct zvfs_fd_set *fdset);
269void ZVFS_FD_ZERO(struct zvfs_fd_set *fdset);
270
271__syscall int zvfs_select(int nfds, struct zvfs_fd_set *ZRESTRICT readfds,
272 struct zvfs_fd_set *ZRESTRICT writefds,
273 struct zvfs_fd_set *ZRESTRICT errorfds,
274 const struct timespec *ZRESTRICT timeout, const void *ZRESTRICT sigmask);
275
284enum {
285 /* Codes below 0x100 are reserved for fcntl() codes. */
295
296 /* Codes above 0x5400 and below 0x5500 are reserved for termios, FIO, etc */
299};
300
310int zvfs_open(const char *name, int flags, struct fd_op_vtable *vtable);
311
320int zvfs_fstat(int fd, struct zvfs_stat *buf);
321
329int zvfs_close(int fd);
330
342ssize_t zvfs_read(int fd, void *buf, size_t sz, const size_t *from_offset);
343
355ssize_t zvfs_write(int fd, const void *buf, size_t sz, const size_t *from_offset);
356
357#ifdef CONFIG_ZVFS_DEFAULT_FILE_VMETHODS
358int zvfs_ioctl_vmeth(void *obj, unsigned int request, va_list args);
359int zvfs_close_vmeth(void *obj);
360ssize_t zvfs_write_vmeth(void *obj, const void *buffer, size_t count);
361ssize_t zvfs_read_vmeth(void *obj, void *buffer, size_t count);
362#endif
363
371int zvfs_unlink(const char *path);
372
381int zvfs_rename(const char *old, const char *newp);
382
383#ifdef __cplusplus
384}
385#endif
386
387#include <zephyr/syscalls/fdtable.h>
388
389#endif /* ZEPHYR_INCLUDE_SYS_FDTABLE_H_ */
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:138
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:229
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.
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_SET_LOCK
Definition fdtable.h:291
@ ZFD_IOCTL_FIONREAD
Definition fdtable.h:297
@ ZFD_IOCTL_POLL_PREPARE
Definition fdtable.h:288
@ ZFD_IOCTL_FSYNC
Definition fdtable.h:286
@ ZFD_IOCTL_LSEEK
Definition fdtable.h:287
@ ZFD_IOCTL_FIONBIO
Definition fdtable.h:298
@ ZFD_IOCTL_POLL_OFFLOAD
Definition fdtable.h:290
@ ZFD_IOCTL_POLL_UPDATE
Definition fdtable.h:289
@ ZFD_IOCTL_TRUNCATE
Definition fdtable.h:293
@ ZFD_IOCTL_STAT
Definition fdtable.h:292
@ ZFD_IOCTL_MMAP
Definition fdtable.h:294
void ZVFS_FD_SET(int fd, struct zvfs_fd_set *fdset)
#define DIV_ROUND_UP(n, d)
Divide and round up.
Definition util.h:348
#define ZRESTRICT
Definition common.h:37
Public kernel APIs.
__SIZE_TYPE__ ssize_t
Definition types.h:28
__INTPTR_TYPE__ off_t
Definition types.h:36
flags
Definition parser.h:97
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
File descriptor virtual method table.
Definition fdtable.h:82
int(* close2)(void *obj, int fd)
Definition fdtable.h:93
ssize_t(* write_offs)(void *obj, const void *buf, size_t sz, size_t offset)
Definition fdtable.h:89
ssize_t(* read_offs)(void *obj, void *buf, size_t sz, size_t offset)
Definition fdtable.h:85
int(* close)(void *obj)
Definition fdtable.h:92
ssize_t(* read)(void *obj, void *buf, size_t sz)
Definition fdtable.h:84
ssize_t(* write)(void *obj, const void *buf, size_t sz)
Definition fdtable.h:88
int(* ioctl)(void *obj, unsigned int request, va_list args)
Definition fdtable.h:95
Definition kernel.h:3346
Mutex Structure.
Definition kernel.h:3234
Definition posix_signal.h:53
Definition fdtable.h:259
uint32_t bitset[DIV_ROUND_UP(ZVFS_OPEN_SIZE, 32)]
Definition fdtable.h:260
Definition fdtable.h:251
short events
Definition fdtable.h:253
short revents
Definition fdtable.h:254
int fd
Definition fdtable.h:252
Structure for result of the ZFD_IOCTL_STAT ioctl call.
Definition fdtable.h:246
uint32_t mode
Definition fdtable.h:248
off_t size
Definition fdtable.h:247
Misc utilities.