From 9a7563b710ac1e3a668c3ea19c4802fc0a84b7bf Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Fri, 15 Mar 2024 03:59:15 -0400 Subject: [PATCH 1/4] 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 000000000000..55f1ab4a873e --- /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 05d2e4e0588a..0865a8e131cc 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 3907238cd52a..eccaded72854 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 06de40ab7279..496a7697d1a5 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 000000000000..154f14f2d525 --- /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 000000000000..f319fe5540eb --- /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; +} From a04f4bccea70ad485d39ba7972f15ea842dfc9ae Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Fri, 15 Mar 2024 03:59:45 -0400 Subject: [PATCH 2/4] tests: posix: common: add a test for confstr() Add a test for confstr(), which is required by the POSIX_SINGLE_PROCESS Option Group, as per IEEE 1003.1-2017. Signed-off-by: Christopher Friedt --- tests/posix/common/CMakeLists.txt | 2 + tests/posix/common/src/confstr.c | 67 +++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tests/posix/common/src/confstr.c diff --git a/tests/posix/common/CMakeLists.txt b/tests/posix/common/CMakeLists.txt index 1a4aefa91631..aa31ed450430 100644 --- a/tests/posix/common/CMakeLists.txt +++ b/tests/posix/common/CMakeLists.txt @@ -8,3 +8,5 @@ FILE(GLOB app_sources src/*.c) zephyr_include_directories(${ZEPHYR_BASE}/lib/posix) target_sources(app PRIVATE ${app_sources}) + +target_compile_options(app PRIVATE -U_POSIX_C_SOURCE -D_POSIX_C_SOURCE=200809L) diff --git a/tests/posix/common/src/confstr.c b/tests/posix/common/src/confstr.c new file mode 100644 index 000000000000..c1926faf60ba --- /dev/null +++ b/tests/posix/common/src/confstr.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024, Meta + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include +#include + +ZTEST(confstr, test_confstr) +{ + char buf[1]; + + /* degenerate cases */ + { + struct arg { + int name; + char *buf; + size_t len; + }; + + const struct arg arg1s[] = { + {-1, NULL, 0}, + {-1, NULL, sizeof(buf)}, + {-1, buf, 0}, + {-1, buf, sizeof(buf)}, + }; + + const struct arg arg2s[] = { + {_CS_PATH, NULL, 0}, + {_CS_PATH, buf, 0}, + }; + + const struct arg arg3s[] = { + {_CS_PATH, NULL, sizeof(buf)}, + }; + + ARRAY_FOR_EACH_PTR(arg1s, arg) { + errno = 0; + zassert_equal(0, confstr(arg->name, arg->buf, arg->len)); + zassert_equal(errno, EINVAL); + } + + ARRAY_FOR_EACH_PTR(arg2s, arg) { + errno = 0; + buf[0] = 0xff; + zassert_true(confstr(arg->name, arg->buf, arg->len) > 0); + zassert_equal(errno, 0); + zassert_equal((uint8_t)buf[0], 0xff); + } + + ARRAY_FOR_EACH_PTR(arg3s, arg) { + errno = 0; + zassert_true(confstr(arg->name, arg->buf, arg->len) > 0); + zassert_equal(errno, 0); + } + } + + buf[0] = 0xff; + zassert_true(confstr(_CS_PATH, buf, sizeof(buf) > 0)); + zassert_equal(buf[0], '\0'); +} + +ZTEST_SUITE(confstr, NULL, NULL, NULL, NULL, NULL); From 05ac7acb381afdbaca5741f83b5309c928e64e24 Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Tue, 19 Mar 2024 05:47:08 -0400 Subject: [PATCH 3/4] doc: posix: mark confstr() as supported Mark confstr() as supported in POSIX Option Groups. Signed-off-by: Christopher Friedt --- doc/services/portability/posix/option_groups/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/services/portability/posix/option_groups/index.rst b/doc/services/portability/posix/option_groups/index.rst index 229ae6b5ec3f..50f97c5fb25a 100644 --- a/doc/services/portability/posix/option_groups/index.rst +++ b/doc/services/portability/posix/option_groups/index.rst @@ -163,7 +163,7 @@ process applications. :header: API, Supported :widths: 50,10 - confstr(), + confstr(),yes environ,yes errno,yes getenv(),yes From 7e6a2555d7fc278d99ffc2a6d03787d3ab01e81c Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Tue, 19 Mar 2024 06:33:12 -0400 Subject: [PATCH 4/4] doc: posix: aep: mark POSIX_SINGLE_PROCESS as supported Mark the POSIX_SINGLE_PROCESS Option Group as supported in PSE51, PSE52, and PSE53 Application Environment Profiles. Signed-off-by: Christopher Friedt --- doc/services/portability/posix/aep/index.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/services/portability/posix/aep/index.rst b/doc/services/portability/posix/aep/index.rst index 5e28fa10397d..9b3ee4b8acd2 100644 --- a/doc/services/portability/posix/aep/index.rst +++ b/doc/services/portability/posix/aep/index.rst @@ -30,7 +30,7 @@ Minimal Realtime System Profile (PSE51) POSIX_DEVICE_IO,, :ref:`†` POSIX_FILE_LOCKING,, POSIX_SIGNALS,, :ref:`†` - POSIX_SINGLE_PROCESS,, :ref:`†` + POSIX_SINGLE_PROCESS, yes, POSIX_THREADS_BASE, yes, :ref:`†` XSI_THREADS_EXT, yes, :ref:`†` @@ -82,7 +82,7 @@ Realtime Controller System Profile (PSE52) POSIX_FILE_LOCKING,, POSIX_FILE_SYSTEM,, POSIX_SIGNALS,, :ref:`†` - POSIX_SINGLE_PROCESS,, :ref:`†` + POSIX_SINGLE_PROCESS, yes, POSIX_THREADS_BASE, yes, :ref:`†` XSI_THREADS_EXT, yes, :ref:`†` @@ -143,7 +143,7 @@ Dedicated Realtime System Profile (PSE53) POSIX_PIPE,, :ref:`†` POSIX_SIGNALS,, :ref:`†` POSIX_SIGNAL_JUMP,, :ref:`†` - POSIX_SINGLE_PROCESS,, :ref:`†` + POSIX_SINGLE_PROCESS, yes, POSIX_THREADS_BASE, yes, :ref:`†` XSI_THREADS_EXT, yes, :ref:`†`