Skip to content

Commit

Permalink
posix: fd_mgmt: add fd_mgmt option group
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Friedt <[email protected]>
  • Loading branch information
Chris Friedt committed May 22, 2024
1 parent 7f91781 commit 51a4ef8
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/libc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ config NEWLIB_LIBC
depends on !NATIVE_APPLICATION
depends on NEWLIB_LIBC_SUPPORTED
select NEED_LIBC_MEM_PARTITION
imply POSIX_FD_MGMT_ALIAS_LSEEK
help
Build with newlib library. The newlib library is expected to be
part of the SDK in this case.
Expand Down
15 changes: 2 additions & 13 deletions lib/os/fdtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ int fsync(int fd)
FUNC_ALIAS(fsync, _fsync, int);
#endif /* CONFIG_POSIX_FSYNC */

off_t lseek(int fd, off_t offset, int whence)
off_t zvfs_lseek(int fd, off_t offset, int whence)
{
if (_check_fd(fd) < 0) {
return -1;
Expand All @@ -375,7 +375,6 @@ off_t lseek(int fd, off_t offset, int whence)
return z_fdtable_call_ioctl(fdtable[fd].vtable, fdtable[fd].obj, ZFD_IOCTL_LSEEK,
offset, whence);
}
FUNC_ALIAS(lseek, _lseek, off_t);

int ioctl(int fd, unsigned long request, ...)
{
Expand All @@ -393,26 +392,16 @@ int ioctl(int fd, unsigned long request, ...)
return res;
}

int fcntl(int fd, int cmd, ...)
int zvfs_fcntl(int fd, int cmd, va_list args)
{
va_list args;
int res;

if (_check_fd(fd) < 0) {
return -1;
}

/* Handle fdtable commands. */
if (cmd == F_DUPFD) {
/* Not implemented so far. */
errno = EINVAL;
return -1;
}

/* The rest of commands are per-fd, handled by ioctl vmethod. */
va_start(args, cmd);
res = fdtable[fd].vtable->ioctl(fdtable[fd].obj, cmd, args);
va_end(args);

return res;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/posix/options/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ zephyr_library_sources_ifdef(CONFIG_POSIX_API perror.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_ASYNCHRONOUS_IO aio.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_CONFSTR confstr.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_ENV env.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_FD_MGMT
fd_mgmt.c
)
zephyr_library_sources_ifdef(CONFIG_POSIX_FS fs.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_MQUEUE mqueue.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_MULTI_PROCESS
Expand Down
1 change: 1 addition & 0 deletions lib/posix/options/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ rsource "Kconfig.confstr"
rsource "Kconfig.env"
rsource "Kconfig.eventfd"
rsource "Kconfig.fdtable"
rsource "Kconfig.fd_mgmt"
rsource "Kconfig.fnmatch"
rsource "Kconfig.fs"
rsource "Kconfig.getentropy"
Expand Down
36 changes: 36 additions & 0 deletions lib/posix/options/Kconfig.fd_mgmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) 2024 Tenstorrent AI ULC
#
# SPDX-License-Identifier: Apache-2.0

menuconfig POSIX_FD_MGMT
bool "POSIX file descriptor management"
default y if POSIX_API
help
Select 'y' here and Zephyr will provide implementations for the POSIX_FD_MGMT Option Group.
This includes support for dup(), dup2(), fcntl(), fseeko(), ftello(), ftruncate(),
and lseek().

For more information, please see
https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_subprofiles.html

if POSIX_FD_MGMT

# These options are intended to be used for compatibility with external POSIX
# implementations such as those in Newlib or Picolibc.

config POSIX_FD_MGMT_ALIAS_FCNTL
bool
help
Select 'y' here and Zephyr will provide an alias for fcntl() as _fcntl().

config POSIX_FD_MGMT_ALIAS_FTRUNCATE
bool
help
Select 'y' here and Zephyr will provide an alias for ftruncate() as _ftruncate().

config POSIX_FD_MGMT_ALIAS_LSEEK
bool
help
Select 'y' here and Zephyr will provide an alias for lseek() as _lseek().

endif # POSIX_FD_MGMT
50 changes: 50 additions & 0 deletions lib/posix/options/fd_mgmt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2024, Tenstorrent AI ULC
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>

#include <zephyr/posix/unistd.h>
#include <zephyr/posix/sys/select.h>
#include <zephyr/posix/sys/socket.h>
#include <zephyr/sys/fdtable.h>

/* prototypes for external, not-yet-public, functions in fdtable.c or fs.c */
int zvfs_fcntl(int fd, int cmd, va_list arg);
int zvfs_ftruncate(int fd, off_t length);
off_t zvfs_lseek(int fd, off_t offset, int whence);

int fcntl(int fd, int cmd, ...)
{
int ret;
va_list args;

va_start(args, cmd);
ret = zvfs_fcntl(fd, cmd, args);
va_end(args);

return ret;
}
#ifdef CONFIG_POSIX_FD_MGMT_ALIAS_FCNTL
FUNC_ALIAS(fcntl, _fcntl, int);
#endif /* CONFIG_POSIX_FD_MGMT_ALIAS_FCNTL */

int ftruncate(int fd, off_t length)
{
return zvfs_ftruncate(fd, length);
}
#ifdef CONFIG_POSIX_FD_MGMT_ALIAS_FTRUNCATE
FUNC_ALIAS(ftruncate, _ftruncate, int);
#endif /* CONFIG_POSIX_FD_MGMT_ALIAS_FTRUNCATE */

off_t lseek(int fd, off_t offset, int whence)
{
return zvfs_lseek(fd, offset, whence);
}
#ifdef CONFIG_POSIX_FD_MGMT_ALIAS_LSEEK
FUNC_ALIAS(lseek, _lseek, off_t);
#endif /* CONFIG_POSIX_FD_MGMT_ALIAS_LSEEK */
2 changes: 1 addition & 1 deletion lib/posix/options/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ int mkdir(const char *path, mode_t mode)
* @brief Truncate file to specified length.
*
*/
int ftruncate(int fd, off_t length)
int zvfs_ftruncate(int fd, off_t length)
{
int rc;
struct posix_fs_desc *ptr = NULL;
Expand Down

0 comments on commit 51a4ef8

Please sign in to comment.