Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posix: implement FILE_LOCKING, SYSTEM_DATABASE, SYSTEM_DATABASE_R #83368

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 55 additions & 19 deletions doc/services/portability/posix/option_groups/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,19 @@ Enable this option group with :kconfig:option:`CONFIG_POSIX_FD_MGMT`.
POSIX_FILE_LOCKING
++++++++++++++++++

Enable this option group with :kconfig:option:`CONFIG_POSIX_FILE_LOCKING`.

.. csv-table:: POSIX_FILE_LOCKING
:header: API, Supported
:widths: 50,10

flockfile(),
ftrylockfile(),
funlockfile(),
getc_unlocked(),
getchar_unlocked(),
putc_unlocked(),
putchar_unlocked(),
flockfile(), yes
ftrylockfile(), yes
funlockfile(), yes
getc_unlocked(), yes
getchar_unlocked(), yes
putc_unlocked(), yes
putchar_unlocked(), yes

.. _posix_option_group_file_system:

Expand Down Expand Up @@ -539,6 +541,38 @@ Enable this option group with :kconfig:option:`CONFIG_POSIX_SPIN_LOCKS`.
pthread_spin_trylock(),yes
pthread_spin_unlock(),yes

.. _posix_option_group_system_database_r:

POSIX_SYSTEM_DATABASE_R
+++++++++++++++++++++++

Enable this option group with :kconfig:option:`CONFIG_POSIX_SYSTEM_DATABASE_R`.

.. csv-table:: POSIX_SYSTEM_DATABASE_R
:header: API, Supported
:widths: 50,10

getgrgid_r(),yes
getgrnam_r(),yes
getpwnam_r(),yes
getpwuid_r(),yes

.. _posix_option_group_system_database:

POSIX_SYSTEM_DATABASE
+++++++++++++++++++++

Enable this option group with :kconfig:option:`CONFIG_POSIX_SYSTEM_DATABASE`.

.. csv-table:: POSIX_SYSTEM_DATABASE
:header: API, Supported
:widths: 50,10

getgrgid(),yes
getgrnam(),yes
getpwnam(),yes
getpwuid(),yes

.. _posix_option_group_threads_base:

POSIX_THREADS_BASE
Expand Down Expand Up @@ -1001,27 +1035,29 @@ Enable this option with :kconfig:option:`CONFIG_POSIX_THREAD_PRIORITY_SCHEDULING
_POSIX_THREAD_SAFE_FUNCTIONS
++++++++++++++++++++++++++++

Enable this option with :kconfig:option:`CONFIG_POSIX_THREAD_SAFE_FUNCTIONS`.
Enable this option with :kconfig:option:`CONFIG_POSIX_C_LANG_SUPPORT_R`,
:kconfig:option:`CONFIG_POSIX_FILE_LOCKING`, :kconfig:option:`CONFIG_POSIX_FILE_SYSTEM_R`,
and :kconfig:option:`CONFIG_POSIX_SYSTEM_DATABASE_R`.

.. csv-table:: _POSIX_THREAD_SAFE_FUNCTIONS
:header: API, Supported
:widths: 50,10

asctime_r(), yes
ctime_r(), yes (UTC timezone only)
flockfile(),
ftrylockfile(),
funlockfile(),
getc_unlocked(),
getchar_unlocked(),
getgrgid_r(),yes :ref:`†<posix_undefined_behaviour>`
getgrnam_r(),yes :ref:`†<posix_undefined_behaviour>`
getpwnam_r(),yes :ref:`†<posix_undefined_behaviour>`
getpwuid_r(),yes :ref:`†<posix_undefined_behaviour>`
flockfile(), yes
ftrylockfile(), yes
funlockfile(), yes
getc_unlocked(), yes
getchar_unlocked(), yes
getgrgid_r(),yes
getgrnam_r(),yes
getpwnam_r(),yes
getpwuid_r(),yes
gmtime_r(), yes
localtime_r(), yes (UTC timezone only)
putc_unlocked(),
putchar_unlocked(),
putc_unlocked(), yes
putchar_unlocked(), yes
rand_r(), yes
readdir_r(), yes
strerror_r(), yes
Expand Down
12 changes: 11 additions & 1 deletion include/zephyr/posix/grp.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2024 Meta Platforms
* Copyright (c) 2024 Tenstorrent AI ULC
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -24,9 +25,18 @@ struct group {
char **gr_mem;
};

#if defined(_XOPEN_SOURCE)
void endgrent(void);
struct group *getgrent(void);
#endif
struct group *getgrgid(gid_t gid);
int getgrgid_r(gid_t gid, struct group *grp, char *buffer, size_t bufsize, struct group **result);
struct group *getgrnam(const char *name);
int getgrnam_r(const char *name, struct group *grp, char *buffer, size_t bufsize,
struct group **result);
int getgrgid_r(gid_t gid, struct group *grp, char *buffer, size_t bufsize, struct group **result);
#if defined(_XOPEN_SOURCE)
void setgrent(void);
#endif

#ifdef __cplusplus
}
Expand Down
3 changes: 2 additions & 1 deletion include/zephyr/posix/posix_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@
/* #define _POSIX_THREAD_ROBUST_PRIO_INHERIT (-1L) */
/* #define _POSIX_THREAD_ROBUST_PRIO_PROTECT (-1L) */

#ifdef CONFIG_POSIX_THREAD_SAFE_FUNCTIONS
#if defined(CONFIG_POSIX_C_LANG_SUPPORT_R) && defined(CONFIG_POSIX_FILE_LOCKING) && \
defined(CONFIG_POSIX_FILE_SYSTEM_R) && defined(CONFIG_POSIX_SYSTEM_DATABASE_R)
#define _POSIX_THREAD_SAFE_FUNCTIONS _POSIX_VERSION
#endif

Expand Down
12 changes: 11 additions & 1 deletion include/zephyr/posix/pwd.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2024 Meta Platforms
* Copyright (c) 2024 Tenstorrent AI ULC
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -25,9 +26,18 @@ struct passwd {
char *pw_shell;
};

int getpwnam_r(const char *nam, struct passwd *pwd, char *buffer, size_t bufsize,
#if defined(_XOPEN_SOURCE)
void endpwent(void);
struct passwd *getpwent(void);
#endif
struct passwd *getpwnam(const char *name);
int getpwnam_r(const char *name, struct passwd *pwd, char *buffer, size_t bufsize,
struct passwd **result);
struct passwd *getpwuid(uid_t uid);
int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result);
#if defined(_XOPEN_SOURCE)
void setpwent(void);
#endif

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions include/zephyr/posix/sys/sysconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ enum {
#define __z_posix_sysconf_SC_XOPEN_UUCP (-1L)
#define __z_posix_sysconf_SC_XOPEN_VERSION _XOPEN_VERSION
#define __z_posix_sysconf_SC_CLK_TCK (100L)
#define __z_posix_sysconf_SC_GETGR_R_SIZE_MAX (0L)
#define __z_posix_sysconf_SC_GETPW_R_SIZE_MAX (0L)
#define __z_posix_sysconf_SC_GETGR_R_SIZE_MAX CONFIG_POSIX_GETGR_R_SIZE_MAX
#define __z_posix_sysconf_SC_GETPW_R_SIZE_MAX CONFIG_POSIX_GETPW_R_SIZE_MAX
#define __z_posix_sysconf_SC_AIO_LISTIO_MAX AIO_LISTIO_MAX
#define __z_posix_sysconf_SC_AIO_MAX AIO_MAX
#define __z_posix_sysconf_SC_AIO_PRIO_DELTA_MAX AIO_PRIO_DELTA_MAX
Expand Down
1 change: 1 addition & 0 deletions lib/libc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ config PICOLIBC
select LIBC_ERRNO if THREAD_LOCAL_STORAGE
select NEED_LIBC_MEM_PARTITION
select TC_PROVIDES_POSIX_C_LANG_SUPPORT_R
select TC_PROVIDES_POSIX_FILE_LOCKING
imply COMMON_LIBC_MALLOC
depends on PICOLIBC_SUPPORTED
help
Expand Down
5 changes: 4 additions & 1 deletion lib/libc/newlib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# SPDX-License-Identifier: Apache-2.0

zephyr_library()
zephyr_library_sources(libc-hooks.c)
zephyr_library_sources(
libc-hooks.c
z_libc.c
)

# Do not allow LTO when compiling libc-hooks.c file
set_source_files_properties(libc-hooks.c PROPERTIES COMPILE_OPTIONS $<TARGET_PROPERTY:compiler,prohibit_lto>)
Expand Down
82 changes: 82 additions & 0 deletions lib/libc/newlib/z_libc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2024 Tenstorrent AI ULC
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#include <zephyr/sys/fdtable.h>

extern void *_read_r;
extern void *_write_r;
extern void *_lseek_r;
extern void *_close_r;

static int z_libc_sflags(const char *mode)
{
int ret = 0;

switch (mode[0]) {
case 'r':
ret = ZVFS_O_RDONLY;
break;

case 'w':
ret = ZVFS_O_WRONLY | ZVFS_O_CREAT | ZVFS_O_TRUNC;
break;

case 'a':
ret = ZVFS_O_WRONLY | ZVFS_O_CREAT | ZVFS_O_APPEND;
break;
default:
return 0;
}

while (*++mode) {
switch (*mode) {
case '+':
ret |= ZVFS_O_RDWR;
break;
case 'x':
ret |= ZVFS_O_EXCL;
break;
default:
break;
}
}

return ret;
}

FILE *z_libc_file_alloc(int fd, const char *mode)
{
FILE *fp;

fp = calloc(1, sizeof(*fp));
if (fp == NULL) {
return NULL;
}

fp->_flags = z_libc_sflags(mode);
fp->_file = fd;
fp->_cookie = (void *)fp;

*((void **)fp->_read) = _read_r;
*((void **)fp->_write) = _write_r;
*((void **)fp->_seek) = _lseek_r;
*((void **)fp->_close) = _close_r;

return fp;
}

int z_libc_file_get_fd(FILE *fp)
{
if (fp == NULL) {
return -EINVAL;
}

return fp->_file;
}
1 change: 1 addition & 0 deletions lib/libc/picolibc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ zephyr_library_sources(
exit.c
locks.c
stdio.c
z_libc.c
)

zephyr_library_compile_options($<TARGET_PROPERTY:compiler,prohibit_lto>)
Expand Down
60 changes: 60 additions & 0 deletions lib/libc/picolibc/z_libc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2024 Tenstorrent AI ULC
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "stdio-bufio.h"

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#include <zephyr/sys/fdtable.h>

#define FDEV_SETUP_ZVFS(fd, buf, size, rwflags, bflags) \
FDEV_SETUP_BUFIO(fd, buf, size, zvfs_read_wrap, zvfs_write_wrap, zvfs_lseek, zvfs_close, \
rwflags, bflags)

int __posix_sflags(const char *mode, int *optr);

/* FIXME: do not use ssize_t or off_t */
ssize_t zvfs_read(int fd, void *buf, size_t sz, const size_t *from_offset);
ssize_t zvfs_write(int fd, const void *buf, size_t sz, const size_t *from_offset);
off_t zvfs_lseek(int fd, off_t offset, int whence);
int zvfs_close(int fd);

static ssize_t zvfs_read_wrap(int fd, void *buf, size_t sz)
{
return zvfs_read(fd, buf, sz, NULL);
}

static ssize_t zvfs_write_wrap(int fd, const void *buf, size_t sz)
{
return zvfs_write(fd, buf, sz, NULL);
}

FILE *z_libc_file_alloc(int fd, const char *mode)
{
struct __file_bufio *bf;
int __maybe_unused unused;

bf = calloc(1, sizeof(struct __file_bufio) + BUFSIZ);
if (bf == NULL) {
return NULL;
}

*bf = (struct __file_bufio)FDEV_SETUP_ZVFS(fd, (char *)(bf + 1), BUFSIZ,
__posix_sflags(mode, &unused), __BFALL);

__bufio_lock_init(&(bf->xfile.cfile.file));

return &(bf->xfile.cfile.file);
}

int z_libc_file_get_fd(const FILE *fp)
{
const struct __file_bufio *const bf = (const struct __file_bufio *)fp;

return POINTER_TO_INT(bf->ptr);
}
Loading
Loading