From e86abfe020343614a766b3203664e21a634b723f Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Sat, 30 Aug 2025 15:54:08 -0400 Subject: [PATCH 01/11] posix: system_database_r: remove invalid compilation filters pwd.c and grp.c both had incorrect preprocessor guards around the stubs that they were implementing. Functions were surrounded by ```cpp .. ``` Which is not at all accurate, since that subprofiling option group is `(CONFIG_)POSIX_SYSTEM_DATABASE_R`.D Remove the guards, since they are invalid anyway, and at most an application will link properly. Signed-off-by: Chris Friedt --- lib/posix/options/grp.c | 4 ---- lib/posix/options/pwd.c | 4 ---- 2 files changed, 8 deletions(-) diff --git a/lib/posix/options/grp.c b/lib/posix/options/grp.c index 32c840e10164f..af395c88fa00c 100644 --- a/lib/posix/options/grp.c +++ b/lib/posix/options/grp.c @@ -9,8 +9,6 @@ #include #include -#ifdef CONFIG_POSIX_THREAD_SAFE_FUNCTIONS - int getgrnam_r(const char *name, struct group *grp, char *buffer, size_t bufsize, struct group **result) { @@ -33,5 +31,3 @@ int getgrgid_r(gid_t gid, struct group *grp, char *buffer, size_t bufsize, struc return ENOSYS; } - -#endif /* CONFIG_POSIX_THREAD_SAFE_FUNCTIONS */ diff --git a/lib/posix/options/pwd.c b/lib/posix/options/pwd.c index f806767f3df90..5cafbb0a3a94a 100644 --- a/lib/posix/options/pwd.c +++ b/lib/posix/options/pwd.c @@ -9,8 +9,6 @@ #include #include -#ifdef CONFIG_POSIX_THREAD_SAFE_FUNCTIONS - int getpwnam_r(const char *nam, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result) { @@ -33,5 +31,3 @@ int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, stru return ENOSYS; } - -#endif /* CONFIG_POSIX_THREAD_SAFE_FUNCTIONS */ From cf6f41dd48518afaa74ca76c7aeb3b974006ed11 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Sat, 30 Aug 2025 06:59:07 -0400 Subject: [PATCH 02/11] posix: limits: de-duplicate limit logic Rather than duplicating limit logic in several places, de-duplicate it and centralize preprocessor checks in posix_limits.h . Signed-off-by: Chris Friedt --- include/zephyr/posix/posix_limits.h | 4 ++++ lib/libc/arcmwdt/include/limits.h | 17 ++--------------- lib/libc/armstdc/include/limits.h | 15 +-------------- lib/libc/iar/include/limits.h | 15 +-------------- 4 files changed, 8 insertions(+), 43 deletions(-) diff --git a/include/zephyr/posix/posix_limits.h b/include/zephyr/posix/posix_limits.h index 4441f9497cf35..9c225b4b4d4e0 100644 --- a/include/zephyr/posix/posix_limits.h +++ b/include/zephyr/posix/posix_limits.h @@ -7,6 +7,8 @@ #ifndef ZEPHYR_INCLUDE_ZEPHYR_POSIX_POSIX_LIMITS_H_ #define ZEPHYR_INCLUDE_ZEPHYR_POSIX_POSIX_LIMITS_H_ +#if defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__) + /* * clang-format and checkpatch disagree on formatting here, so rely on checkpatch and disable * clang-format since checkpatch cannot be selectively disabled. @@ -122,4 +124,6 @@ /* clang-format on */ +#endif + #endif /* ZEPHYR_INCLUDE_ZEPHYR_POSIX_POSIX_LIMITS_H_ */ diff --git a/lib/libc/arcmwdt/include/limits.h b/lib/libc/arcmwdt/include/limits.h index 96982ad6e4146..2e9a4d3fb4c68 100644 --- a/lib/libc/arcmwdt/include/limits.h +++ b/lib/libc/arcmwdt/include/limits.h @@ -8,23 +8,10 @@ #define LIB_LIBC_ARCMWDT_INCLUDE_LIMITS_H_ #include_next - -#ifdef __cplusplus -extern "C" { -#endif - -#if defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__) - #include -#else - +#ifndef PATH_MAX #define PATH_MAX 256 - -#endif - -#ifdef __cplusplus -} #endif -#endif /* ZEPHYR_LIB_LIBC_ARMSTDC_INCLUDE_LIMITS_H_ */ +#endif /* LIB_LIBC_ARCMWDT_INCLUDE_LIMITS_H_ */ diff --git a/lib/libc/armstdc/include/limits.h b/lib/libc/armstdc/include/limits.h index 13206aa68b566..54dabd8fdaa4a 100644 --- a/lib/libc/armstdc/include/limits.h +++ b/lib/libc/armstdc/include/limits.h @@ -8,23 +8,10 @@ #define ZEPHYR_LIB_LIBC_ARMSTDC_INCLUDE_LIMITS_H_ #include_next - -#ifdef __cplusplus -extern "C" { -#endif - -#if defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__) - #include -#else - +#ifndef PATH_MAX #define PATH_MAX 256 - -#endif - -#ifdef __cplusplus -} #endif #endif /* ZEPHYR_LIB_LIBC_ARMSTDC_INCLUDE_LIMITS_H_ */ diff --git a/lib/libc/iar/include/limits.h b/lib/libc/iar/include/limits.h index 6db549c355e92..010f0509649e7 100644 --- a/lib/libc/iar/include/limits.h +++ b/lib/libc/iar/include/limits.h @@ -8,23 +8,10 @@ #define ZEPHYR_LIB_LIBC_IAR_INCLUDE_LIMITS_H_ #include_next - -#ifdef __cplusplus -extern "C" { -#endif - -#if defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__) - #include -#else - +#ifndef PATH_MAX #define PATH_MAX 256 - -#endif - -#ifdef __cplusplus -} #endif #endif /* ZEPHYR_LIB_LIBC_IAR_INCLUDE_LIMITS_H_ */ From bbde7f2b21ef7fe3d11252329fcdb1c8df1f0fb1 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Sat, 30 Aug 2025 07:46:18 -0400 Subject: [PATCH 03/11] libc: newlib: limits.h: include posix limits from zephyr Newlib does not seem to define a number of mandatory POSIX limits (e.g. minimum values). Include Zephyr's POSIX definitions in an override of Newlib's limits.h (when the appropriate application conformance macro is provided). Signed-off-by: Chris Friedt --- lib/libc/newlib/include/limits.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 lib/libc/newlib/include/limits.h diff --git a/lib/libc/newlib/include/limits.h b/lib/libc/newlib/include/limits.h new file mode 100644 index 0000000000000..81f9bbd406611 --- /dev/null +++ b/lib/libc/newlib/include/limits.h @@ -0,0 +1,13 @@ +/* + * Copyright The Zephyr Project Contributors + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_LIB_LIBC_NEWLIB_INCLUDE_LIMITS_H_ +#define ZEPHYR_LIB_LIBC_NEWLIB_INCLUDE_LIMITS_H_ + +#include_next +#include + +#endif /* ZEPHYR_LIB_LIBC_NEWLIB_INCLUDE_LIMITS_H_ */ From 90015c729f6c0a3a512d49aa655360d89e41e910 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Fri, 22 Aug 2025 10:32:08 -0400 Subject: [PATCH 04/11] tests: posix: common: use realtime clock instead of monotonic Use the realtime clock, since that is always guaranteed to be available with _POSIX_TIMERS. Signed-off-by: Chris Friedt --- tests/posix/common/src/mutex.c | 3 ++- tests/posix/common/src/pthread.c | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/posix/common/src/mutex.c b/tests/posix/common/src/mutex.c index 40987d50297a0..20b992620eb8a 100644 --- a/tests/posix/common/src/mutex.c +++ b/tests/posix/common/src/mutex.c @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -182,7 +183,7 @@ static void *test_mutex_timedlock_fn(void *arg) struct timespec time_point; pthread_mutex_t *mtx = (pthread_mutex_t *)arg; - zassume_ok(clock_gettime(CLOCK_MONOTONIC, &time_point)); + zassume_ok(clock_gettime(CLOCK_REALTIME, &time_point)); timespec_add_ms(&time_point, TIMEDLOCK_TIMEOUT_MS); return INT_TO_POINTER(pthread_mutex_timedlock(mtx, &time_point)); diff --git a/tests/posix/common/src/pthread.c b/tests/posix/common/src/pthread.c index 035730ee4dc73..96a16f7ddb33b 100644 --- a/tests/posix/common/src/pthread.c +++ b/tests/posix/common/src/pthread.c @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -409,8 +410,8 @@ ZTEST(pthread, test_pthread_timedjoin) }; /* setup timespecs when the thread is still running and when it is done */ - clock_gettime(CLOCK_MONOTONIC, ¬_done); - clock_gettime(CLOCK_MONOTONIC, &done); + clock_gettime(CLOCK_REALTIME, ¬_done); + clock_gettime(CLOCK_REALTIME, &done); not_done.tv_nsec += sleep_duration_ms / 2 * NSEC_PER_MSEC; done.tv_nsec += sleep_duration_ms * 1.5 * NSEC_PER_MSEC; while (not_done.tv_nsec >= NSEC_PER_SEC) { From 92b2019d322c241ed22f0739fc65034dd08f633f Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Fri, 22 Aug 2025 10:33:59 -0400 Subject: [PATCH 05/11] posix: options: define _POSIX_TIMERS when configured Define _POSIX_TIMERS=200809L when so configured. This change is required to ensure that Newlib provides the necessary POSIX timer API definitions in toolchain headers. Signed-off-by: Chris Friedt --- include/zephyr/posix/posix_features.h | 4 ---- lib/posix/options/CMakeLists.txt | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/zephyr/posix/posix_features.h b/include/zephyr/posix/posix_features.h index a78b4c2a8d941..9328704351a50 100644 --- a/include/zephyr/posix/posix_features.h +++ b/include/zephyr/posix/posix_features.h @@ -177,10 +177,6 @@ #define _POSIX_TIMEOUTS _POSIX_VERSION #endif -#ifdef CONFIG_POSIX_TIMERS -#define _POSIX_TIMERS _POSIX_VERSION -#endif - /* #define _POSIX_TRACE (-1L) */ /* #define _POSIX_TRACE_EVENT_FILTER (-1L) */ /* #define _POSIX_TRACE_INHERIT (-1L) */ diff --git a/lib/posix/options/CMakeLists.txt b/lib/posix/options/CMakeLists.txt index 5ef2032e5a91c..df8919d097428 100644 --- a/lib/posix/options/CMakeLists.txt +++ b/lib/posix/options/CMakeLists.txt @@ -1,5 +1,6 @@ # SPDX-License-Identifier: Apache-2.0 +set(POSIX_VERSION 200809L) set(GEN_DIR ${ZEPHYR_BINARY_DIR}/include/generated) zephyr_syscall_header_ifdef(CONFIG_POSIX_CLOCK_SELECTION posix_clock.h) @@ -125,6 +126,9 @@ if (NOT CONFIG_TC_PROVIDES_POSIX_TIMERS) timespec_to_timeout.c ) endif() +if (CONFIG_POSIX_TIMERS) + zephyr_compile_definitions(-D_POSIX_TIMERS=${POSIX_VERSION}) +endif() if (NOT CONFIG_TC_PROVIDES_POSIX_READER_WRITER_LOCKS) # Note: the Option is _POSIX_READER_WRITER_LOCKS, while the Option Group is POSIX_RW_LOCKS. From 921328e6fe5a456db283eefb70b4a88df3cec307 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Fri, 22 Aug 2025 12:31:15 -0400 Subject: [PATCH 06/11] posix: time.h: move to posix_time.h To avoid conflicts between the C library's time.h and posix time.h, use an "override" header when necessary for C libraries that do not themselves provide POSIX definitions in time.h . This will allow non-POSIX C libraries to take advantage of Zephyr's implementation. --- include/zephyr/posix/mqueue.h | 3 +- include/zephyr/posix/posix_features.h | 12 -- include/zephyr/posix/posix_time.h | 195 ++++++++++++++++++++++++++ include/zephyr/posix/pthread.h | 2 +- include/zephyr/posix/semaphore.h | 3 +- include/zephyr/posix/signal.h | 2 + include/zephyr/posix/time.h | 114 --------------- include/zephyr/posix/unistd.h | 2 + lib/libc/arcmwdt/include/time.h | 18 +-- lib/libc/armstdc/include/time.h | 13 ++ lib/libc/iar/include/time.h | 23 +-- lib/libc/minimal/include/time.h | 4 +- lib/libc/picolibc/CMakeLists.txt | 1 + lib/libc/picolibc/include/time.h | 33 +++++ lib/posix/options/CMakeLists.txt | 11 ++ lib/posix/options/Kconfig.pthread | 4 + lib/posix/options/clock.c | 1 - lib/posix/options/clock_selection.c | 1 - lib/posix/options/timer.c | 2 +- tests/lib/c_lib/thrd/src/thrd.h | 2 +- tests/posix/common/src/clock.c | 1 + tests/posix/semaphores/src/main.c | 1 + tests/posix/timers/src/timer.c | 1 + 23 files changed, 279 insertions(+), 170 deletions(-) create mode 100644 include/zephyr/posix/posix_time.h delete mode 100644 include/zephyr/posix/time.h create mode 100644 lib/libc/armstdc/include/time.h create mode 100644 lib/libc/picolibc/include/time.h diff --git a/include/zephyr/posix/mqueue.h b/include/zephyr/posix/mqueue.h index 40db073337490..d3fb90ca72110 100644 --- a/include/zephyr/posix/mqueue.h +++ b/include/zephyr/posix/mqueue.h @@ -7,8 +7,9 @@ #ifndef ZEPHYR_INCLUDE_POSIX_MESSAGE_PASSING_H_ #define ZEPHYR_INCLUDE_POSIX_MESSAGE_PASSING_H_ +#include + #include -#include #include #include #include diff --git a/include/zephyr/posix/posix_features.h b/include/zephyr/posix/posix_features.h index 9328704351a50..935e3ce2fe1ea 100644 --- a/include/zephyr/posix/posix_features.h +++ b/include/zephyr/posix/posix_features.h @@ -56,10 +56,6 @@ #define _POSIX_CLOCK_SELECTION _POSIX_VERSION #endif -#ifdef CONFIG_POSIX_CPUTIME -#define _POSIX_CPUTIME _POSIX_VERSION -#endif - #ifdef CONFIG_POSIX_FSYNC #define _POSIX_FSYNC _POSIX_VERSION #endif @@ -90,10 +86,6 @@ #define _POSIX_MESSAGE_PASSING _POSIX_VERSION #endif -#ifdef CONFIG_POSIX_MONOTONIC_CLOCK -#define _POSIX_MONOTONIC_CLOCK _POSIX_VERSION -#endif - /* #define _POSIX_PRIORITIZED_IO (-1L) */ #ifdef CONFIG_POSIX_PRIORITY_SCHEDULING @@ -161,10 +153,6 @@ /* #define _POSIX_THREAD_ROBUST_PRIO_INHERIT (-1L) */ /* #define _POSIX_THREAD_ROBUST_PRIO_PROTECT (-1L) */ -#ifdef CONFIG_POSIX_THREAD_SAFE_FUNCTIONS -#define _POSIX_THREAD_SAFE_FUNCTIONS _POSIX_VERSION -#endif - /* #define _POSIX_THREAD_SPORADIC_SERVER (-1L) */ #ifdef CONFIG_POSIX_THREADS diff --git a/include/zephyr/posix/posix_time.h b/include/zephyr/posix/posix_time.h new file mode 100644 index 0000000000000..0b9a8c840cc0c --- /dev/null +++ b/include/zephyr/posix/posix_time.h @@ -0,0 +1,195 @@ +/* + * Copyright The Zephyr Project Contributors + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_INCLUDE_ZEPHYR_POSIX_POSIX_TIME_H_ +#define ZEPHYR_INCLUDE_ZEPHYR_POSIX_POSIX_TIME_H_ + +#if defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__) + +#include + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* clock_t must be defined in the libc time.h */ +/* size_t must be defined in the libc stddef.h */ +/* time_t must be defined in the libc time.h */ + +#if !defined(_CLOCKID_T_DECLARED) && !defined(__clockid_t_defined) +typedef unsigned long clockid_t; +#define _CLOCKID_T_DECLARED +#define __clockid_t_defined +#endif + +#if !defined(_TIMER_T_DECLARED) && !defined(__timer_t_defined) +typedef unsigned long timer_t; +#define _TIMER_T_DECLARED +#define __timer_t_defined +#endif + +#if !defined(_LOCALE_T_DECLARED) && !defined(__locale_t_defined) +#ifdef CONFIG_NEWLIB_LIBC +struct __locale_t; +typedef struct __locale_t *locale_t; +#else +typedef void *locale_t; +#endif +#define _LOCALE_T_DECLARED +#define __locale_t_defined +#endif + +#if !defined(_PID_T_DECLARED) && !defined(__pid_t_defined) +typedef int pid_t; +#define _PID_T_DECLARED +#define __pid_t_defined +#endif + +#if defined(_POSIX_REALTIME_SIGNALS) +struct sigevent; +#endif + +/* struct tm must be defined in the libc time.h */ + +#if __STDC_VERSION__ >= 201112L +/* struct timespec must be defined in the libc time.h */ +#else +#if !defined(_TIMESPEC_DECLARED) && !defined(__timespec_defined) +typedef struct { + time_t tv_sec; + long tv_nsec; +} timespec_t; +#define _TIMESPEC_DECLARED +#define __timespec_defined +#endif +#endif + +#if !defined(_ITIMERSPEC_DECLARED) && !defined(__itimerspec_defined) +struct itimerspec { + struct timespec it_interval; + struct timespec it_value; +}; +#define _ITIMERSPEC_DECLARED +#define __itimerspec_defined +#endif + +/* NULL must be defined in the libc stddef.h */ + +#ifndef CLOCK_REALTIME +#define CLOCK_REALTIME ((clockid_t)SYS_CLOCK_REALTIME) +#endif + +#ifndef CLOCKS_PER_SEC +#if defined(_XOPEN_SOURCE) +#define CLOCKS_PER_SEC 1000000 +#else +#define CLOCKS_PER_SEC CONFIG_TICKS_PER_SECOND +#endif +#endif + +#if defined(_POSIX_CPUTIME) || defined(__DOXYGEN__) +#ifndef CLOCK_PROCESS_CPUTIME_ID +#define CLOCK_PROCESS_CPUTIME_ID ((clockid_t)2) +#endif +#endif + +#if defined(_POSIX_THREAD_CPUTIME) || defined(__DOXYGEN__) +#ifndef CLOCK_THREAD_CPUTIME_ID +#define CLOCK_THREAD_CPUTIME_ID ((clockid_t)3) +#endif +#endif + +#if defined(_POSIX_MONOTONIC_CLOCK) || defined(__DOXYGEN__) +#ifndef CLOCK_MONOTONIC +#define CLOCK_MONOTONIC ((clockid_t)SYS_CLOCK_MONOTONIC) +#endif +#endif + +#ifndef TIMER_ABSTIME +#define TIMER_ABSTIME SYS_TIMER_ABSTIME +#endif + +/* asctime() must be declared in the libc time.h */ +#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(__DOXYGEN__) +char *asctime_r(const struct tm *ZRESTRICT tm, char *ZRESTRICT buf); +#endif +/* clock() must be declared in the libc time.h */ +#if defined(_POSIX_CPUTIME) || defined(__DOXYGEN__) +int clock_getcpuclockid(pid_t pid, clockid_t *clock_id); +#endif +#if defined(_POSIX_TIMERS) || defined(__DOXYGEN__) +int clock_getres(clockid_t clock_id, struct timespec *ts); +int clock_gettime(clockid_t clock_id, struct timespec *ts); +#endif +#if defined(_POSIX_CLOCK_SELECTION) || defined(__DOXYGEN__) +int clock_nanosleep(clockid_t clock_id, int flags, + const struct timespec *rqtp, struct timespec *rmtp); +#endif +#if defined(_POSIX_TIMERS) || defined(__DOXYGEN__) +int clock_settime(clockid_t clock_id, const struct timespec *ts); +#endif +/* ctime() must be declared in the libc time.h */ +#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(__DOXYGEN__) +char *ctime_r(const time_t *clock, char *buf); +#endif +/* difftime() must be declared in the libc time.h */ +#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__) +struct tm *getdate(const char *string); +#endif +/* gmtime() must be declared in the libc time.h */ +#if __STDC_VERSION__ >= 202311L +/* gmtime_r() must be declared in the libc time.h */ +#else +#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(__DOXYGEN__) +struct tm *gmtime_r(const time_t *ZRESTRICT timer, struct tm *ZRESTRICT result); +#endif +#endif +/* localtime() must be declared in the libc time.h */ +#if __STDC_VERSION__ >= 202311L +/* localtime_r() must be declared in the libc time.h */ +#else +#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(__DOXYGEN__) +struct tm *localtime_r(const time_t *ZRESTRICT timer, struct tm *ZRESTRICT result); +#endif +#endif +/* mktime() must be declared in the libc time.h */ +#if defined(_POSIX_TIMERS) || defined(__DOXYGEN__) +int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); +#endif +/* strftime() must be declared in the libc time.h */ +size_t strftime_l(char *ZRESTRICT s, size_t maxsize, + const char *ZRESTRICT format, const struct tm *ZRESTRICT timeptr, + locale_t locale); +#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__) +char *strptime(const char *ZRESTRICT s, const char *ZRESTRICT format, struct tm *ZRESTRICT tm); +#endif +/* time() must be declared in the libc time.h */ +#if defined(_POSIX_TIMERS) || defined(__DOXYGEN__) +int timer_create(clockid_t clockId, struct sigevent *ZRESTRICT evp, timer_t *ZRESTRICT timerid); +int timer_delete(timer_t timerid); +int timer_getoverrun(timer_t timerid); +int timer_gettime(timer_t timerid, struct itimerspec *its); +int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, + struct itimerspec *ovalue); +#endif + +#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__) +extern int daylight; +extern long timezone; +#endif + +extern char *tzname[]; + +#ifdef __cplusplus +} +#endif + +#endif /* defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__) */ + +#endif /* ZEPHYR_INCLUDE_ZEPHYR_POSIX_POSIX_TIME_H_ */ diff --git a/include/zephyr/posix/pthread.h b/include/zephyr/posix/pthread.h index 685a8d9a9316e..8b9b03e490c54 100644 --- a/include/zephyr/posix/pthread.h +++ b/include/zephyr/posix/pthread.h @@ -9,9 +9,9 @@ #include #include +#include #include -#include #include #include diff --git a/include/zephyr/posix/semaphore.h b/include/zephyr/posix/semaphore.h index e3f6014cfec45..943df122cff59 100644 --- a/include/zephyr/posix/semaphore.h +++ b/include/zephyr/posix/semaphore.h @@ -6,7 +6,8 @@ #ifndef ZEPHYR_INCLUDE_POSIX_SEMAPHORE_H_ #define ZEPHYR_INCLUDE_POSIX_SEMAPHORE_H_ -#include +#include + #include #ifdef __cplusplus diff --git a/include/zephyr/posix/signal.h b/include/zephyr/posix/signal.h index 7513bfd104753..45dfb0caee4f2 100644 --- a/include/zephyr/posix/signal.h +++ b/include/zephyr/posix/signal.h @@ -6,6 +6,8 @@ #ifndef ZEPHYR_INCLUDE_POSIX_SIGNAL_H_ #define ZEPHYR_INCLUDE_POSIX_SIGNAL_H_ +#include + /* include posix_types.h before posix_features.h (here) to avoid build errors against newlib */ #include #include "posix_features.h" diff --git a/include/zephyr/posix/time.h b/include/zephyr/posix/time.h deleted file mode 100644 index df2980601722e..0000000000000 --- a/include/zephyr/posix/time.h +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2018 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ -#ifndef ZEPHYR_INCLUDE_POSIX_TIME_H_ -#define ZEPHYR_INCLUDE_POSIX_TIME_H_ - -/* Read standard header. This may find since they - * refer to the same file when include/posix is in the search path. - */ -#include - -#ifdef CONFIG_NEWLIB_LIBC -/* Kludge to support outdated newlib version as used in SDK 0.10 for Xtensa */ -#include - -#ifdef __NEWLIB__ -/* Newever Newlib 3.x+ */ -#include -#else /* __NEWLIB__ */ -/* Workaround for older Newlib 2.x, as used by Xtensa. It lacks sys/_timeval.h, - * so mimic it here. - */ -#include -#ifndef __timespec_defined - -#ifdef __cplusplus -extern "C" { -#endif - -struct timespec { - time_t tv_sec; - long tv_nsec; -}; - -struct itimerspec { - struct timespec it_interval; /* Timer interval */ - struct timespec it_value; /* Timer expiration */ -}; - -#ifdef __cplusplus -} -#endif - -#endif /* __timespec_defined */ -#endif /* __NEWLIB__ */ - -#else /* CONFIG_NEWLIB_LIBC */ -/* Not Newlib */ -# if defined(CONFIG_ARCH_POSIX) && defined(CONFIG_EXTERNAL_LIBC) -# include -# include -# else -# include -# endif -#endif /* CONFIG_NEWLIB_LIBC */ - -#include -#include -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef CLOCK_REALTIME -#define CLOCK_REALTIME SYS_CLOCK_REALTIME -#endif - -#ifndef CLOCK_PROCESS_CPUTIME_ID -#define CLOCK_PROCESS_CPUTIME_ID 2 -#endif - -#ifndef CLOCK_THREAD_CPUTIME_ID -#define CLOCK_THREAD_CPUTIME_ID 3 -#endif - -#ifndef CLOCK_MONOTONIC -#define CLOCK_MONOTONIC SYS_CLOCK_MONOTONIC -#endif - -#ifndef TIMER_ABSTIME -#define TIMER_ABSTIME SYS_TIMER_ABSTIME -#endif - -int clock_gettime(clockid_t clock_id, struct timespec *ts); -int clock_getres(clockid_t clock_id, struct timespec *ts); -int clock_settime(clockid_t clock_id, const struct timespec *ts); -int clock_getcpuclockid(pid_t pid, clockid_t *clock_id); -/* Timer APIs */ -int timer_create(clockid_t clockId, struct sigevent *evp, timer_t *timerid); -int timer_delete(timer_t timerid); -struct itimerspec; -int timer_gettime(timer_t timerid, struct itimerspec *its); -int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, - struct itimerspec *ovalue); -int timer_getoverrun(timer_t timerid); -int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); -int clock_nanosleep(clockid_t clock_id, int flags, - const struct timespec *rqtp, struct timespec *rmtp); - -#ifdef __cplusplus -} -#endif - -#else /* ZEPHYR_INCLUDE_POSIX_TIME_H_ */ -/* Read the toolchain header when finds itself on the - * first attempt. - */ -#include_next -#endif /* ZEPHYR_INCLUDE_POSIX_TIME_H_ */ diff --git a/include/zephyr/posix/unistd.h b/include/zephyr/posix/unistd.h index 10d44bfd4a8d2..89f698159e1b4 100644 --- a/include/zephyr/posix/unistd.h +++ b/include/zephyr/posix/unistd.h @@ -6,6 +6,8 @@ #ifndef ZEPHYR_INCLUDE_POSIX_UNISTD_H_ #define ZEPHYR_INCLUDE_POSIX_UNISTD_H_ +#include + #include #ifdef CONFIG_POSIX_API diff --git a/lib/libc/arcmwdt/include/time.h b/lib/libc/arcmwdt/include/time.h index 245bd3a77d236..1d9ed0a7a9a05 100644 --- a/lib/libc/arcmwdt/include/time.h +++ b/lib/libc/arcmwdt/include/time.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Synopsys + * Copyright The Zephyr Project Contributors * * SPDX-License-Identifier: Apache-2.0 */ @@ -8,18 +8,6 @@ #define LIB_LIBC_ARCMWDT_INCLUDE_TIME_H_ #include_next +#include -#ifdef __cplusplus -extern "C" { -#endif - -extern char *asctime_r(const struct tm *tp, char *buf); -extern char *ctime_r(const time_t *clock, char *buf); -extern struct tm *gmtime_r(const time_t *timep, struct tm *result); -extern struct tm *localtime_r(const time_t *timer, struct tm *result); - -#ifdef __cplusplus -} -#endif - -#endif /* LIB_LIBC_ARCMWDT_INCLUDE_TIME_H_ */ +#endif /* LIB_LIBC_ARCMWDT_INCLUDE_TIME_H_ */ diff --git a/lib/libc/armstdc/include/time.h b/lib/libc/armstdc/include/time.h new file mode 100644 index 0000000000000..1432916b21188 --- /dev/null +++ b/lib/libc/armstdc/include/time.h @@ -0,0 +1,13 @@ +/* + * Copyright The Zephyr Project Contributors + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_LIB_LIBC_ARMSTDC_INCLUDE_TIME_H_ +#define ZEPHYR_LIB_LIBC_ARMSTDC_INCLUDE_TIME_H_ + +#include_next +#include + +#endif /* ZEPHYR_LIB_LIBC_ARMSTDC_INCLUDE_TIME_H_ */ diff --git a/lib/libc/iar/include/time.h b/lib/libc/iar/include/time.h index b095987857418..2d4d2d9007578 100644 --- a/lib/libc/iar/include/time.h +++ b/lib/libc/iar/include/time.h @@ -1,31 +1,14 @@ /* - * Copyright (c) 2025 IAR Systems AB + * Copyright The Zephyr Project Contributors * * SPDX-License-Identifier: Apache-2.0 */ -/** - * @file - * @brief Declares additional time related functions based on POSIX - */ - #ifndef ZEPHYR_LIB_LIBC_IAR_INCLUDE_TIME_H_ #define ZEPHYR_LIB_LIBC_IAR_INCLUDE_TIME_H_ #include #include_next +#include -#ifdef __cplusplus -extern "C" { -#endif - -char *asctime_r(const struct tm *ZRESTRICT tp, char *ZRESTRICT buf); -char *ctime_r(const time_t *clock, char *buf); -struct tm *gmtime_r(const time_t *ZRESTRICT timep, struct tm *ZRESTRICT result); -struct tm *localtime_r(const time_t *ZRESTRICT timer, struct tm *ZRESTRICT result); - -#ifdef __cplusplus -} -#endif - -#endif /* ZEPHYR_LIB_LIBC_IAR_INCLUDE_TIME_H_ */ +#endif /* ZEPHYR_LIB_LIBC_IAR_INCLUDE_TIME_H_ */ diff --git a/lib/libc/minimal/include/time.h b/lib/libc/minimal/include/time.h index 62ec45b03a171..3e795f0db4817 100644 --- a/lib/libc/minimal/include/time.h +++ b/lib/libc/minimal/include/time.h @@ -58,12 +58,12 @@ struct tm *gmtime_r(const time_t *ZRESTRICT timep, char *asctime(const struct tm *timeptr); struct tm *localtime(const time_t *timer); char *ctime(const time_t *clock); -char *asctime_r(const struct tm *ZRESTRICT tp, char *ZRESTRICT buf); -char *ctime_r(const time_t *clock, char *buf); struct tm *localtime_r(const time_t *ZRESTRICT timer, struct tm *ZRESTRICT result); time_t time(time_t *tloc); +#include + #ifdef __cplusplus } #endif diff --git a/lib/libc/picolibc/CMakeLists.txt b/lib/libc/picolibc/CMakeLists.txt index 752379dd76d44..280a16a89454e 100644 --- a/lib/libc/picolibc/CMakeLists.txt +++ b/lib/libc/picolibc/CMakeLists.txt @@ -10,6 +10,7 @@ zephyr_library_sources( locks.c stdio.c ) +zephyr_include_directories(include) zephyr_library_compile_options($) diff --git a/lib/libc/picolibc/include/time.h b/lib/libc/picolibc/include/time.h new file mode 100644 index 0000000000000..47113c10d81af --- /dev/null +++ b/lib/libc/picolibc/include/time.h @@ -0,0 +1,33 @@ +/* + * Copyright The Zephyr Project Contributors + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_LIB_LIBC_ARMSTDC_INCLUDE_TIME_H_ +#define ZEPHYR_LIB_LIBC_ARMSTDC_INCLUDE_TIME_H_ + + +#if defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__) + +/* temporary workaround for https://github.com/picolibc/picolibc/pull/1079 */ +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _PID_T_DECLARED +typedef __pid_t pid_t; +#define _PID_T_DECLARED +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__) */ + +#include_next + +#endif /* ZEPHYR_LIB_LIBC_ARMSTDC_INCLUDE_TIME_H_ */ diff --git a/lib/posix/options/CMakeLists.txt b/lib/posix/options/CMakeLists.txt index df8919d097428..1143a2dc986e1 100644 --- a/lib/posix/options/CMakeLists.txt +++ b/lib/posix/options/CMakeLists.txt @@ -3,6 +3,8 @@ set(POSIX_VERSION 200809L) set(GEN_DIR ${ZEPHYR_BINARY_DIR}/include/generated) +zephyr_compile_definitions(-D_POSIX_C_SOURCE=${POSIX_VERSION}) + zephyr_syscall_header_ifdef(CONFIG_POSIX_CLOCK_SELECTION posix_clock.h) zephyr_syscall_header_ifdef(CONFIG_POSIX_TIMERS posix_clock.h) zephyr_syscall_header_ifdef(CONFIG_XSI_SINGLE_PROCESS posix_clock.h) @@ -81,6 +83,9 @@ endif() if (NOT CONFIG_TC_PROVIDES_POSIX_FILE_SYSTEM_R) zephyr_library_sources_ifdef(CONFIG_POSIX_FILE_SYSTEM_R file_system_r.c) endif() +if (CONFIG_POSIX_FILE_SYSTEM_R) + zephyr_compile_definitions(-D_POSIX_THREAD_SAFE_FUNCTIONS=${POSIX_VERSION}) +endif() if (NOT CONFIG_TC_PROVIDES_POSIX_MEMORY_PROTECTION) zephyr_library_sources_ifdef(CONFIG_POSIX_MEMORY_PROTECTION mprotect.c) @@ -128,6 +133,12 @@ if (NOT CONFIG_TC_PROVIDES_POSIX_TIMERS) endif() if (CONFIG_POSIX_TIMERS) zephyr_compile_definitions(-D_POSIX_TIMERS=${POSIX_VERSION}) + # FIXME: Until we have CONFIG_XSI_ADVANCED_REALTIME define _POSIX_CPUTIME and + # _POSIX_MONOTONIC_CLOCK with _POSIX_TIMERS. + # For more information on the Advanced Realtime Option Group, please see + # https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap02.html + zephyr_compile_definitions(-D_POSIX_CPUTIME=${POSIX_VERSION}) + zephyr_compile_definitions(-D_POSIX_MONOTONIC_CLOCK=${POSIX_VERSION}) endif() if (NOT CONFIG_TC_PROVIDES_POSIX_READER_WRITER_LOCKS) diff --git a/lib/posix/options/Kconfig.pthread b/lib/posix/options/Kconfig.pthread index 438506fe2182f..37aab14d472bd 100644 --- a/lib/posix/options/Kconfig.pthread +++ b/lib/posix/options/Kconfig.pthread @@ -157,6 +157,10 @@ config POSIX_THREAD_PRIO_PROTECT For more information, please see https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_getprotocol.html +# FIXME: this is an option, not an option group, and probably should be removed. +# Furthermore, it should also depend on POSIX_FILE_LOCKING and POSIX_SYSTEM_DATABASE_R rather +# than select any other options. The feature test macro _POSIX_THREAD_SAFE_FUNCTIONS should be +# enabled when dependencies are satisfied. config POSIX_THREAD_SAFE_FUNCTIONS bool "POSIX thread-safe functions" select POSIX_FILE_SYSTEM_R diff --git a/lib/posix/options/clock.c b/lib/posix/options/clock.c index 466974705ac3a..faa85cae775fc 100644 --- a/lib/posix/options/clock.c +++ b/lib/posix/options/clock.c @@ -10,7 +10,6 @@ #include #include -#include #include #include diff --git a/lib/posix/options/clock_selection.c b/lib/posix/options/clock_selection.c index 5717221b596ec..67e5a89eaccfe 100644 --- a/lib/posix/options/clock_selection.c +++ b/lib/posix/options/clock_selection.c @@ -12,7 +12,6 @@ #include #include -#include #include #include diff --git a/lib/posix/options/timer.c b/lib/posix/options/timer.c index 257fdce59ef36..ae2a263ff64b1 100644 --- a/lib/posix/options/timer.c +++ b/lib/posix/options/timer.c @@ -11,12 +11,12 @@ #include "posix_clock.h" #include +#include #include #include #include #include -#include #define ACTIVE 1 #define NOT_ACTIVE 0 diff --git a/tests/lib/c_lib/thrd/src/thrd.h b/tests/lib/c_lib/thrd/src/thrd.h index 103b30a4f5f63..35d71eb07ab86 100644 --- a/tests/lib/c_lib/thrd/src/thrd.h +++ b/tests/lib/c_lib/thrd/src/thrd.h @@ -8,8 +8,8 @@ #define TESTS_LIB_CLIB_SRC_TEST_THRD_H_ #include +#include -#include #include #include diff --git a/tests/posix/common/src/clock.c b/tests/posix/common/src/clock.c index a1b1336d0ce27..2467ac4df4bb9 100644 --- a/tests/posix/common/src/clock.c +++ b/tests/posix/common/src/clock.c @@ -9,6 +9,7 @@ #include #include +#include #include #include diff --git a/tests/posix/semaphores/src/main.c b/tests/posix/semaphores/src/main.c index 4ea118af926ce..c8b566349a644 100644 --- a/tests/posix/semaphores/src/main.c +++ b/tests/posix/semaphores/src/main.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include diff --git a/tests/posix/timers/src/timer.c b/tests/posix/timers/src/timer.c index 1d4936d6a4be6..49e896f991e43 100644 --- a/tests/posix/timers/src/timer.c +++ b/tests/posix/timers/src/timer.c @@ -9,6 +9,7 @@ #include #include +#include #define SECS_TO_SLEEP 2 #define DURATION_SECS 1 From 055eae806ebe69b26590d89089b2fed7ae5b9704 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Sat, 12 Apr 2025 10:55:36 -0400 Subject: [PATCH 07/11] eventfd: bring config to top-level of posix dir, since it is not posix The eventfd configuration does not need to be so deeply nested within POSIX since it does not depend on POSIX completely. Signed-off-by: Chris Friedt --- lib/posix/CMakeLists.txt | 1 + lib/posix/Kconfig | 5 ++++- lib/posix/eventfd/CMakeLists.txt | 4 ++++ lib/posix/{options/Kconfig.compat => eventfd/Kconfig} | 4 ---- lib/posix/{options => eventfd}/eventfd.c | 0 lib/posix/options/CMakeLists.txt | 3 +-- lib/posix/options/Kconfig | 2 -- 7 files changed, 10 insertions(+), 9 deletions(-) create mode 100644 lib/posix/eventfd/CMakeLists.txt rename lib/posix/{options/Kconfig.compat => eventfd/Kconfig} (87%) rename lib/posix/{options => eventfd}/eventfd.c (100%) diff --git a/lib/posix/CMakeLists.txt b/lib/posix/CMakeLists.txt index d62108b66f007..3db3541a54cb8 100644 --- a/lib/posix/CMakeLists.txt +++ b/lib/posix/CMakeLists.txt @@ -1,4 +1,5 @@ # SPDX-License-Identifier: Apache-2.0 add_subdirectory(options) +add_subdirectory_ifdef(CONFIG_EVENTFD eventfd) add_subdirectory_ifdef(CONFIG_POSIX_SHELL shell) diff --git a/lib/posix/Kconfig b/lib/posix/Kconfig index 534baf08cd240..1cc15de555e41 100644 --- a/lib/posix/Kconfig +++ b/lib/posix/Kconfig @@ -7,4 +7,7 @@ menu "POSIX API Support" rsource "options/Kconfig" rsource "shell/Kconfig" -endmenu # "POSIX API Support" +endmenu + +# Eventfd Support (not officially POSIX) +rsource "eventfd/Kconfig" diff --git a/lib/posix/eventfd/CMakeLists.txt b/lib/posix/eventfd/CMakeLists.txt new file mode 100644 index 0000000000000..2736572dd0061 --- /dev/null +++ b/lib/posix/eventfd/CMakeLists.txt @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: Apache-2.0 + +zephyr_library() +zephyr_library_sources(eventfd.c) diff --git a/lib/posix/options/Kconfig.compat b/lib/posix/eventfd/Kconfig similarity index 87% rename from lib/posix/options/Kconfig.compat rename to lib/posix/eventfd/Kconfig index e15cc869a7be6..019daef4a3524 100644 --- a/lib/posix/options/Kconfig.compat +++ b/lib/posix/eventfd/Kconfig @@ -3,8 +3,6 @@ # # SPDX-License-Identifier: Apache-2.0 -menu "Miscellaneous POSIX-related options" - config EVENTFD bool "Support for eventfd" select ZVFS @@ -13,5 +11,3 @@ config EVENTFD Enable support for event file descriptors, eventfd. An eventfd can be used as an event wait/notify mechanism together with POSIX calls like read, write and poll. - -endmenu diff --git a/lib/posix/options/eventfd.c b/lib/posix/eventfd/eventfd.c similarity index 100% rename from lib/posix/options/eventfd.c rename to lib/posix/eventfd/eventfd.c diff --git a/lib/posix/options/CMakeLists.txt b/lib/posix/options/CMakeLists.txt index 1143a2dc986e1..5d63fcb1bb908 100644 --- a/lib/posix/options/CMakeLists.txt +++ b/lib/posix/options/CMakeLists.txt @@ -30,7 +30,7 @@ if (NOT CONFIG_TC_PROVIDES_POSIX_SIGNALS) endif() if(CONFIG_POSIX_API OR CONFIG_POSIX_THREADS OR CONFIG_POSIX_TIMERS OR - CONFIG_POSIX_MESSAGE_PASSING OR CONFIG_POSIX_FILE_SYSTEM OR CONFIG_EVENTFD OR + CONFIG_POSIX_MESSAGE_PASSING OR CONFIG_POSIX_FILE_SYSTEM OR CONFIG_POSIX_C_LIB_EXT OR CONFIG_POSIX_SINGLE_PROCESS) # This is a temporary workaround so that Newlib declares the appropriate # types for us. POSIX features to be formalized as part of #51211 @@ -39,7 +39,6 @@ if(CONFIG_POSIX_API OR CONFIG_POSIX_THREADS OR CONFIG_POSIX_TIMERS OR endif() zephyr_library() -zephyr_library_sources_ifdef(CONFIG_EVENTFD eventfd.c) if (NOT CONFIG_TC_PROVIDES_POSIX_ASYNCHRONOUS_IO) zephyr_library_sources_ifdef(CONFIG_POSIX_ASYNCHRONOUS_IO aio.c) diff --git a/lib/posix/options/Kconfig b/lib/posix/options/Kconfig index 6c2b129170e30..cc077cf9abcde 100644 --- a/lib/posix/options/Kconfig +++ b/lib/posix/options/Kconfig @@ -40,8 +40,6 @@ rsource "Kconfig.xsi_threads_ext" endmenu # "X/Open system interfaces" -rsource "Kconfig.compat" - rsource "Kconfig.toolchain" endmenu # "POSIX Options" From 8fca66b1528d86bab284b5e7505e215a75f3b8e0 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Sat, 12 Apr 2025 07:54:36 -0400 Subject: [PATCH 08/11] posix: separate option groups into c library ext and system interfaces Separate the POSIX implementation into two categories: - Extensions to ISO C - System Interfaces The first category include standalone functions that generally do not require OS support or depend on any other features within the POSIX specification. The Option Groups that comprise this category include - POSIX_C_LIB_EXT: e.g. strnlen(), fnmatch() - POSIX_C_LANG_SUPPORT_R: e.g. gmtime_r(), strtok_r() The second category includes the majority of other POSIX Option Groups that do require OS support. The latter group may also be categorized generally as being NATIVE_LIBC_INCOMPATIBLE, although that might eventually become more granular. Signed-off-by: Chris Friedt --- lib/posix/CMakeLists.txt | 4 ++- lib/posix/Kconfig | 32 +++++++++++++++++++ lib/posix/{options => }/Kconfig.profile | 26 ++++++++------- lib/posix/{options => }/Kconfig.toolchain | 0 lib/posix/c_lang_support_r/CMakeLists.txt | 8 +++++ .../Kconfig} | 0 lib/posix/c_lib_ext/CMakeLists.txt | 17 ++++++++++ .../Kconfig.c_lib_ext => c_lib_ext/Kconfig} | 0 lib/posix/{options => c_lib_ext}/fnmatch.c | 0 lib/posix/{options => c_lib_ext}/getentropy.c | 0 .../{options => c_lib_ext}/getopt/README | 0 .../{options => c_lib_ext}/getopt/getopt.c | 0 .../{options => c_lib_ext}/getopt/getopt.h | 0 .../getopt/getopt_common.c | 0 .../getopt/getopt_common.h | 0 .../getopt/getopt_long.c | 0 lib/posix/options/CMakeLists.txt | 20 ++---------- lib/posix/options/Kconfig | 10 ------ tests/posix/single_process/prj.conf | 1 + 19 files changed, 77 insertions(+), 41 deletions(-) rename lib/posix/{options => }/Kconfig.profile (93%) rename lib/posix/{options => }/Kconfig.toolchain (100%) create mode 100644 lib/posix/c_lang_support_r/CMakeLists.txt rename lib/posix/{options/Kconfig.c_lang_r => c_lang_support_r/Kconfig} (100%) create mode 100644 lib/posix/c_lib_ext/CMakeLists.txt rename lib/posix/{options/Kconfig.c_lib_ext => c_lib_ext/Kconfig} (100%) rename lib/posix/{options => c_lib_ext}/fnmatch.c (100%) rename lib/posix/{options => c_lib_ext}/getentropy.c (100%) rename lib/posix/{options => c_lib_ext}/getopt/README (100%) rename lib/posix/{options => c_lib_ext}/getopt/getopt.c (100%) rename lib/posix/{options => c_lib_ext}/getopt/getopt.h (100%) rename lib/posix/{options => c_lib_ext}/getopt/getopt_common.c (100%) rename lib/posix/{options => c_lib_ext}/getopt/getopt_common.h (100%) rename lib/posix/{options => c_lib_ext}/getopt/getopt_long.c (100%) diff --git a/lib/posix/CMakeLists.txt b/lib/posix/CMakeLists.txt index 3db3541a54cb8..49d260f0a9bd3 100644 --- a/lib/posix/CMakeLists.txt +++ b/lib/posix/CMakeLists.txt @@ -1,5 +1,7 @@ # SPDX-License-Identifier: Apache-2.0 -add_subdirectory(options) add_subdirectory_ifdef(CONFIG_EVENTFD eventfd) +add_subdirectory_ifdef(CONFIG_POSIX_C_LANG_SUPPORT_R c_lang_support_r) +add_subdirectory_ifdef(CONFIG_POSIX_C_LIB_EXT c_lib_ext) add_subdirectory_ifdef(CONFIG_POSIX_SHELL shell) +add_subdirectory_ifdef(CONFIG_POSIX_SYSTEM_INTERFACES options) diff --git a/lib/posix/Kconfig b/lib/posix/Kconfig index 1cc15de555e41..4b93901ae17a5 100644 --- a/lib/posix/Kconfig +++ b/lib/posix/Kconfig @@ -1,11 +1,43 @@ # Copyright (c) 2024 Meta +# Copyright (c) 2025 Tenstorrent AI ULC # # SPDX-License-Identifier: Apache-2.0 menu "POSIX API Support" +# POSIX Subprofile Definitions +rsource "Kconfig.profile" + +# Toolchain hooks for external implementations +rsource "Kconfig.toolchain" + +# POSIX C Library Extensions +# This menu is for POSIX Option Groups that do not require OS support. +menu "POSIX C Library Extensions" +rsource "c_lang_support_r/Kconfig" +rsource "c_lib_ext/Kconfig" +endmenu + +# POSIX System Interfaces +menuconfig POSIX_SYSTEM_INTERFACES + bool "POSIX System Interfaces" + depends on !NATIVE_APPLICATION + select NATIVE_LIBC_INCOMPATIBLE + help + Select 'y' here to support POSIX System Interfaces within Zephyr. + + Options in this menu are organized by POSIX subprofiling Option Group. + + For more information, see + https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_subprofiles.html + + +if POSIX_SYSTEM_INTERFACES rsource "options/Kconfig" + +# POSIX Shell utilities rsource "shell/Kconfig" +endif endmenu diff --git a/lib/posix/options/Kconfig.profile b/lib/posix/Kconfig.profile similarity index 93% rename from lib/posix/options/Kconfig.profile rename to lib/posix/Kconfig.profile index 99dc78c20ea24..3a8f4064ae727 100644 --- a/lib/posix/options/Kconfig.profile +++ b/lib/posix/Kconfig.profile @@ -2,7 +2,7 @@ # # SPDX-License-Identifier: Apache-2.0 -config POSIX_SYSTEM_HEADERS +config POSIX_SYSTEM_INTERFACES bool select NATIVE_LIBC_INCOMPATIBLE help @@ -10,8 +10,7 @@ config POSIX_SYSTEM_HEADERS config POSIX_API bool "POSIX APIs" - select NATIVE_LIBC_INCOMPATIBLE - select POSIX_SYSTEM_HEADERS + select POSIX_SYSTEM_INTERFACES select POSIX_BASE_DEFINITIONS # clock_gettime(), pthread_create(), sem_get(), etc select POSIX_AEP_REALTIME_MINIMAL # CLOCK_MONOTONIC, pthread_attr_setstack(), etc select POSIX_NETWORKING if NETWORKING # inet_ntoa(), socket(), etc @@ -38,13 +37,13 @@ choice POSIX_AEP_CHOICE https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_subprofiles.html config POSIX_AEP_CHOICE_NONE - bool "No pre-defined POSIX subprofile" + bool "No POSIX subprofile" help - No pre-defined POSIX profile is selected. + No POSIX subprofile is selected. config POSIX_AEP_CHOICE_BASE - bool "Base definitions (system interfaces)" - select NATIVE_LIBC_INCOMPATIBLE + bool "Minimal POSIX System Profile" + select POSIX_SYSTEM_INTERFACES select POSIX_BASE_DEFINITIONS help Only enable the base definitions required for all POSIX systems. @@ -54,7 +53,7 @@ config POSIX_AEP_CHOICE_BASE config POSIX_AEP_CHOICE_PSE51 bool "Minimal Realtime System Profile (PSE51)" - select NATIVE_LIBC_INCOMPATIBLE + select POSIX_SYSTEM_INTERFACES select POSIX_BASE_DEFINITIONS select POSIX_AEP_REALTIME_MINIMAL help @@ -67,7 +66,7 @@ config POSIX_AEP_CHOICE_PSE51 config POSIX_AEP_CHOICE_PSE52 bool "Realtime Controller System Profile (PSE52)" - select NATIVE_LIBC_INCOMPATIBLE + select POSIX_SYSTEM_INTERFACES select POSIX_BASE_DEFINITIONS select POSIX_AEP_REALTIME_MINIMAL select POSIX_AEP_REALTIME_CONTROLLER @@ -81,7 +80,7 @@ config POSIX_AEP_CHOICE_PSE52 config POSIX_AEP_CHOICE_PSE53 bool "Dedicated Realtime System Profile (PSE53)" - select NATIVE_LIBC_INCOMPATIBLE + select POSIX_SYSTEM_INTERFACES select POSIX_BASE_DEFINITIONS select POSIX_AEP_REALTIME_MINIMAL select POSIX_AEP_REALTIME_CONTROLLER @@ -98,10 +97,11 @@ config POSIX_AEP_CHOICE_PSE53 endchoice # POSIX_AEP_CHOICE -# Base Definitions (System Interfaces) +if POSIX_SYSTEM_INTERFACES + +# Mandatory POSIX System Interfaces (base profile) config POSIX_BASE_DEFINITIONS bool - select POSIX_SYSTEM_HEADERS select POSIX_ASYNCHRONOUS_IO select POSIX_BARRIERS select POSIX_CLOCK_SELECTION @@ -187,3 +187,5 @@ config POSIX_AEP_REALTIME_DEDICATED For more information, please see https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_subprofiles.html + +endif # POSIX_SYSTEM_INTERFACE diff --git a/lib/posix/options/Kconfig.toolchain b/lib/posix/Kconfig.toolchain similarity index 100% rename from lib/posix/options/Kconfig.toolchain rename to lib/posix/Kconfig.toolchain diff --git a/lib/posix/c_lang_support_r/CMakeLists.txt b/lib/posix/c_lang_support_r/CMakeLists.txt new file mode 100644 index 0000000000000..db1e5c8d3a917 --- /dev/null +++ b/lib/posix/c_lang_support_r/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright The Zephyr Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +set(POSIX_VERSION 200809L) + +if (CONFIG_POSIX_C_LANG_SUPPORT_R) + zephyr_compile_definitions(-D_POSIX_THREAD_SAFE_FUNCTIONS=${POSIX_VERSION}) +endif() diff --git a/lib/posix/options/Kconfig.c_lang_r b/lib/posix/c_lang_support_r/Kconfig similarity index 100% rename from lib/posix/options/Kconfig.c_lang_r rename to lib/posix/c_lang_support_r/Kconfig diff --git a/lib/posix/c_lib_ext/CMakeLists.txt b/lib/posix/c_lib_ext/CMakeLists.txt new file mode 100644 index 0000000000000..a4c3e09d839f2 --- /dev/null +++ b/lib/posix/c_lib_ext/CMakeLists.txt @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: Apache-2.0 + +zephyr_include_directories(getopt) + +if (CONFIG_TC_PROVIDES_POSIX_C_LIB_EXT) + return() +endif() + +zephyr_library() +zephyr_library_sources( + fnmatch.c + getentropy.c + getopt/getopt.c + getopt/getopt_common.c +) + +zephyr_library_sources_ifdef(CONFIG_GETOPT_LONG getopt/getopt_long.c) diff --git a/lib/posix/options/Kconfig.c_lib_ext b/lib/posix/c_lib_ext/Kconfig similarity index 100% rename from lib/posix/options/Kconfig.c_lib_ext rename to lib/posix/c_lib_ext/Kconfig diff --git a/lib/posix/options/fnmatch.c b/lib/posix/c_lib_ext/fnmatch.c similarity index 100% rename from lib/posix/options/fnmatch.c rename to lib/posix/c_lib_ext/fnmatch.c diff --git a/lib/posix/options/getentropy.c b/lib/posix/c_lib_ext/getentropy.c similarity index 100% rename from lib/posix/options/getentropy.c rename to lib/posix/c_lib_ext/getentropy.c diff --git a/lib/posix/options/getopt/README b/lib/posix/c_lib_ext/getopt/README similarity index 100% rename from lib/posix/options/getopt/README rename to lib/posix/c_lib_ext/getopt/README diff --git a/lib/posix/options/getopt/getopt.c b/lib/posix/c_lib_ext/getopt/getopt.c similarity index 100% rename from lib/posix/options/getopt/getopt.c rename to lib/posix/c_lib_ext/getopt/getopt.c diff --git a/lib/posix/options/getopt/getopt.h b/lib/posix/c_lib_ext/getopt/getopt.h similarity index 100% rename from lib/posix/options/getopt/getopt.h rename to lib/posix/c_lib_ext/getopt/getopt.h diff --git a/lib/posix/options/getopt/getopt_common.c b/lib/posix/c_lib_ext/getopt/getopt_common.c similarity index 100% rename from lib/posix/options/getopt/getopt_common.c rename to lib/posix/c_lib_ext/getopt/getopt_common.c diff --git a/lib/posix/options/getopt/getopt_common.h b/lib/posix/c_lib_ext/getopt/getopt_common.h similarity index 100% rename from lib/posix/options/getopt/getopt_common.h rename to lib/posix/c_lib_ext/getopt/getopt_common.h diff --git a/lib/posix/options/getopt/getopt_long.c b/lib/posix/c_lib_ext/getopt/getopt_long.c similarity index 100% rename from lib/posix/options/getopt/getopt_long.c rename to lib/posix/c_lib_ext/getopt/getopt_long.c diff --git a/lib/posix/options/CMakeLists.txt b/lib/posix/options/CMakeLists.txt index 5d63fcb1bb908..ac11617c28b84 100644 --- a/lib/posix/options/CMakeLists.txt +++ b/lib/posix/options/CMakeLists.txt @@ -9,7 +9,7 @@ zephyr_syscall_header_ifdef(CONFIG_POSIX_CLOCK_SELECTION posix_clock.h) zephyr_syscall_header_ifdef(CONFIG_POSIX_TIMERS posix_clock.h) zephyr_syscall_header_ifdef(CONFIG_XSI_SINGLE_PROCESS posix_clock.h) -if(CONFIG_POSIX_SYSTEM_HEADERS) +if(CONFIG_POSIX_SYSTEM_INTERFACES) zephyr_include_directories(${ZEPHYR_BASE}/include/zephyr/posix) endif() @@ -31,7 +31,7 @@ endif() if(CONFIG_POSIX_API OR CONFIG_POSIX_THREADS OR CONFIG_POSIX_TIMERS OR CONFIG_POSIX_MESSAGE_PASSING OR CONFIG_POSIX_FILE_SYSTEM OR - CONFIG_POSIX_C_LIB_EXT OR CONFIG_POSIX_SINGLE_PROCESS) + CONFIG_POSIX_SINGLE_PROCESS) # This is a temporary workaround so that Newlib declares the appropriate # types for us. POSIX features to be formalized as part of #51211 zephyr_compile_options($<$:-D_POSIX_THREADS>) @@ -52,15 +52,6 @@ if (NOT CONFIG_TC_PROVIDES_POSIX_CLOCK_SELECTION) zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK_SELECTION clock_selection.c) endif() -if (NOT CONFIG_TC_PROVIDES_POSIX_C_LIB_EXT) - zephyr_library_sources_ifdef(CONFIG_POSIX_C_LIB_EXT - fnmatch.c - getentropy.c - getopt/getopt.c - getopt/getopt_common.c - ) -endif() - if (NOT CONFIG_TC_PROVIDES_POSIX_DEVICE_IO) zephyr_library_sources_ifdef(CONFIG_POSIX_DEVICE_IO # perror should be moved to the common libc @@ -185,13 +176,6 @@ if (NOT CONFIG_TC_PROVIDES_XSI_SYSTEM_LOGGING) zephyr_library_sources_ifdef(CONFIG_XSI_SYSTEM_LOGGING syslog.c) endif() -zephyr_library_sources_ifdef(CONFIG_GETOPT_LONG - getopt/getopt_long.c -) -zephyr_include_directories_ifdef(CONFIG_POSIX_C_LIB_EXT - getopt/ -) - zephyr_library_include_directories( ${ZEPHYR_BASE}/kernel/include ${ARCH_DIR}/${ARCH}/include diff --git a/lib/posix/options/Kconfig b/lib/posix/options/Kconfig index cc077cf9abcde..d635e07b23dba 100644 --- a/lib/posix/options/Kconfig +++ b/lib/posix/options/Kconfig @@ -4,14 +4,8 @@ # # SPDX-License-Identifier: Apache-2.0 -menu "POSIX Options" - -rsource "Kconfig.profile" - rsource "Kconfig.aio" rsource "Kconfig.barrier" -rsource "Kconfig.c_lang_r" -rsource "Kconfig.c_lib_ext" rsource "Kconfig.device_io" rsource "Kconfig.fd_mgmt" rsource "Kconfig.file_system_r" @@ -39,7 +33,3 @@ rsource "Kconfig.xsi_system_logging" rsource "Kconfig.xsi_threads_ext" endmenu # "X/Open system interfaces" - -rsource "Kconfig.toolchain" - -endmenu # "POSIX Options" diff --git a/tests/posix/single_process/prj.conf b/tests/posix/single_process/prj.conf index 7422432b9d0b8..ceec42c12c048 100644 --- a/tests/posix/single_process/prj.conf +++ b/tests/posix/single_process/prj.conf @@ -1,4 +1,5 @@ CONFIG_ZTEST=y +CONFIG_POSIX_SYSTEM_INTERFACES=y CONFIG_POSIX_SINGLE_PROCESS=y # Let's explicitly choose PICOLIBC, so it is used if supported even if it would not have been the From f854cd833377765e4bf469d6193e15830509fa37 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Sat, 12 Apr 2025 06:31:16 -0400 Subject: [PATCH 09/11] posix: profiles: add custom Zephyr POSIX subprofile Add a custom Zephyr POSIX subprofile specifically for enabling the default features that Zephyr requires as per the coding guidelines. Signed-off-by: Chris Friedt --- lib/posix/Kconfig.profile | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/posix/Kconfig.profile b/lib/posix/Kconfig.profile index 3a8f4064ae727..8a996efaaa40d 100644 --- a/lib/posix/Kconfig.profile +++ b/lib/posix/Kconfig.profile @@ -41,6 +41,28 @@ config POSIX_AEP_CHOICE_NONE help No POSIX subprofile is selected. +config POSIX_AEP_CHOICE_ZEPHYR + bool "Minimal Zephyr System Profile" + select POSIX_C_LIB_EXT + select POSIX_C_LANG_SUPPORT_R + help + Zephyr expects certain POSIX functions to be available throughout the build environment, + such as gmtime_r(), strnlen(), strtok_r(), and possibly others. + + These functions are divided into two standalone Option Groups that may be enabled + independently of the remainder of the POSIX API implementation; namely POSIX_C_LIB_EXT and + POSIX_C_LANG_SUPPORT_R. If not referenced by the Zephyr kernel or application, there are no + resource implications for enabling these option groups. + + Unlike pre-defined, standard POSIX subprofiles, this subprofile is custom to Zephyr and + therefore does not need to include the base definitions or system interfaces that would + otherwise be required for a conformant POSIX system or subprofile. This system profile + does not itself meet the requirements for POSIX implementation conformance. + + For more information, see + https://docs.zephyrproject.org/latest/contribute/coding_guidelines/index.html + https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_subprofiles.html + config POSIX_AEP_CHOICE_BASE bool "Minimal POSIX System Profile" select POSIX_SYSTEM_INTERFACES From 85be7a894ba9423a96cf46f920acfdac8a389bed Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Sat, 12 Apr 2025 08:13:52 -0400 Subject: [PATCH 10/11] posix: profiles: make POSIX_AEP_CHOICE_ZEPHYR the default Default POSIX_AEP_CHOICE to POSIX_AEP_CHOICE_ZEPHYR Signed-off-by: Chris Friedt --- lib/posix/Kconfig.profile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/posix/Kconfig.profile b/lib/posix/Kconfig.profile index 8a996efaaa40d..eed95528f5e6a 100644 --- a/lib/posix/Kconfig.profile +++ b/lib/posix/Kconfig.profile @@ -26,7 +26,7 @@ config POSIX_API choice POSIX_AEP_CHOICE prompt "POSIX Subprofile" - default POSIX_AEP_CHOICE_NONE + default POSIX_AEP_CHOICE_ZEPHYR help This choice is intended to help users select the correct POSIX profile for their application. Choices are based on IEEE 1003.13-2003 (now inactive / reserved) and From 8c52fbeda5030454dfa7fd72542065c92d64f27b Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Sat, 12 Apr 2025 08:55:16 -0400 Subject: [PATCH 11/11] posix: options: add keep-sorted-start and -stop Add zephyr-keep-sorted-start and zephyr-keep-sorted-stop comments. Signed-off-by: Chris Friedt --- lib/posix/CMakeLists.txt | 2 ++ lib/posix/options/Kconfig | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/lib/posix/CMakeLists.txt b/lib/posix/CMakeLists.txt index 49d260f0a9bd3..9a40cad191b26 100644 --- a/lib/posix/CMakeLists.txt +++ b/lib/posix/CMakeLists.txt @@ -1,7 +1,9 @@ # SPDX-License-Identifier: Apache-2.0 +# zephyr-keep-sorted-start add_subdirectory_ifdef(CONFIG_EVENTFD eventfd) add_subdirectory_ifdef(CONFIG_POSIX_C_LANG_SUPPORT_R c_lang_support_r) add_subdirectory_ifdef(CONFIG_POSIX_C_LIB_EXT c_lib_ext) add_subdirectory_ifdef(CONFIG_POSIX_SHELL shell) add_subdirectory_ifdef(CONFIG_POSIX_SYSTEM_INTERFACES options) +# zephyr-keep-sorted-stop diff --git a/lib/posix/options/Kconfig b/lib/posix/options/Kconfig index d635e07b23dba..3f072a293f6be 100644 --- a/lib/posix/options/Kconfig +++ b/lib/posix/options/Kconfig @@ -4,6 +4,7 @@ # # SPDX-License-Identifier: Apache-2.0 +# zephyr-keep-sorted-start rsource "Kconfig.aio" rsource "Kconfig.barrier" rsource "Kconfig.device_io" @@ -23,13 +24,16 @@ rsource "Kconfig.signal" rsource "Kconfig.spinlock" rsource "Kconfig.sync_io" rsource "Kconfig.timer" +# zephyr-keep-sorted-stop menu "X/Open system interfaces" +# zephyr-keep-sorted-start rsource "Kconfig.xsi_realtime" rsource "Kconfig.xsi_single_process" rsource "Kconfig.xsi_streams" rsource "Kconfig.xsi_system_logging" rsource "Kconfig.xsi_threads_ext" +# zephyr-keep-sorted-stop endmenu # "X/Open system interfaces"