From c9edda0fbf6908815eac60750319a18496a65fb1 Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Fri, 15 Mar 2024 03:59:15 -0400 Subject: [PATCH] posix: unistd: support for confstr() Support querying POSIX string configuration values (similar to sysconf()). confstr() is required by the POSIX_SINGLE_PROCESS Option Group as detailed in Section E.1 of IEEE-1003.1-2017 and has been part of the specification since POSIX-2. The POSIX_SINGLE_PROCESS Option Group is required for PSE51, PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory for any POSIX conforming system as per Section A.2.1.3 of IEEE-1003-1.2017. With this, we have complete support for the POSIX_SINGLE_PROCESS Option Group. Signed-off-by: Christopher Friedt --- include/zephyr/posix/sys/confstr.h | 51 ++++++++++++++++++++++++++++++ include/zephyr/posix/unistd.h | 4 +++ lib/posix/options/CMakeLists.txt | 1 + lib/posix/options/Kconfig | 1 + lib/posix/options/Kconfig.confstr | 9 ++++++ lib/posix/options/confstr.c | 21 ++++++++++++ 6 files changed, 87 insertions(+) create mode 100644 include/zephyr/posix/sys/confstr.h create mode 100644 lib/posix/options/Kconfig.confstr create mode 100644 lib/posix/options/confstr.c diff --git a/include/zephyr/posix/sys/confstr.h b/include/zephyr/posix/sys/confstr.h new file mode 100644 index 0000000000..55f1ab4a87 --- /dev/null +++ b/include/zephyr/posix/sys/confstr.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2024, Meta + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef ZEPHYR_INCLUDE_POSIX_SYS_CONFSTR_H_ +#define ZEPHYR_INCLUDE_POSIX_SYS_CONFSTR_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +enum { + _CS_PATH, + _CS_POSIX_V7_ILP32_OFF32_CFLAGS, + _CS_POSIX_V7_ILP32_OFF32_LDFLAGS, + _CS_POSIX_V7_ILP32_OFF32_LIBS, + _CS_POSIX_V7_ILP32_OFFBIG_CFLAGS, + _CS_POSIX_V7_ILP32_OFFBIG_LDFLAGS, + _CS_POSIX_V7_ILP32_OFFBIG_LIBS, + _CS_POSIX_V7_LP64_OFF64_CFLAGS, + _CS_POSIX_V7_LP64_OFF64_LDFLAGS, + _CS_POSIX_V7_LP64_OFF64_LIBS, + _CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS, + _CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS, + _CS_POSIX_V7_LPBIG_OFFBIG_LIBS, + _CS_POSIX_V7_THREADS_CFLAGS, + _CS_POSIX_V7_THREADS_LDFLAGS, + _CS_POSIX_V7_WIDTH_RESTRICTED_ENVS, + _CS_V7_ENV, + _CS_POSIX_V6_ILP32_OFF32_CFLAGS, + _CS_POSIX_V6_ILP32_OFF32_LDFLAGS, + _CS_POSIX_V6_ILP32_OFF32_LIBS, + _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS, + _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS, + _CS_POSIX_V6_ILP32_OFFBIG_LIBS, + _CS_POSIX_V6_LP64_OFF64_CFLAGS, + _CS_POSIX_V6_LP64_OFF64_LDFLAGS, + _CS_POSIX_V6_LP64_OFF64_LIBS, + _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS, + _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS, + _CS_POSIX_V6_LPBIG_OFFBIG_LIBS, + _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS, + _CS_V6_ENV, +}; + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_POSIX_SYS_CONFSTR_H_ */ diff --git a/include/zephyr/posix/unistd.h b/include/zephyr/posix/unistd.h index 05d2e4e058..0865a8e131 100644 --- a/include/zephyr/posix/unistd.h +++ b/include/zephyr/posix/unistd.h @@ -19,6 +19,7 @@ #ifdef CONFIG_POSIX_SYSCONF #include #endif +#include #include #include @@ -266,6 +267,9 @@ int usleep(useconds_t useconds); #ifdef CONFIG_POSIX_SYSCONF_IMPL_FULL long sysconf(int opt); #endif +#if _POSIX_C_SOURCE >= 2 +size_t confstr(int name, char *buf, size_t len); +#endif #ifdef __cplusplus } diff --git a/lib/posix/options/CMakeLists.txt b/lib/posix/options/CMakeLists.txt index 3907238cd5..eccaded728 100644 --- a/lib/posix/options/CMakeLists.txt +++ b/lib/posix/options/CMakeLists.txt @@ -40,6 +40,7 @@ zephyr_library_sources_ifdef(CONFIG_POSIX_API perror.c) zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK clock.c) zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK nanosleep.c) zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK sleep.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_FS fs.c) zephyr_library_sources_ifdef(CONFIG_POSIX_MQUEUE mqueue.c) diff --git a/lib/posix/options/Kconfig b/lib/posix/options/Kconfig index 06de40ab72..496a7697d1 100644 --- a/lib/posix/options/Kconfig +++ b/lib/posix/options/Kconfig @@ -27,6 +27,7 @@ endif # POSIX_CLOCK rsource "Kconfig.barrier" rsource "Kconfig.clock" rsource "Kconfig.cond" +rsource "Kconfig.confstr" rsource "Kconfig.env" rsource "Kconfig.eventfd" rsource "Kconfig.fdtable" diff --git a/lib/posix/options/Kconfig.confstr b/lib/posix/options/Kconfig.confstr new file mode 100644 index 0000000000..154f14f2d5 --- /dev/null +++ b/lib/posix/options/Kconfig.confstr @@ -0,0 +1,9 @@ +# Copyright (c) 2024, Meta +# +# SPDX-License-Identifier: Apache-2.0 + +config POSIX_CONFSTR + bool "Retrieve string system configuration" + default y if POSIX_API + help + This enables the POSIX confstr() function. diff --git a/lib/posix/options/confstr.c b/lib/posix/options/confstr.c new file mode 100644 index 0000000000..f319fe5540 --- /dev/null +++ b/lib/posix/options/confstr.c @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024, Meta + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +size_t confstr(int name, char *buf, size_t len) +{ + if (name < 0 || name > _CS_V6_ENV) { + errno = EINVAL; + return 0; + } + + if (buf != NULL && len > 0) { + buf[0] = '\0'; + } + + return 1; +}