Skip to content

Commit 821c142

Browse files
cfriedtMaureenHelm
authored andcommitted
posix: sysconf: declare sysconf macro values more reliably
The `sysconf()` implementation in Zephyr can be macro-based or library-based. Both approaches use the same set of runtime-invariant definitions in `sys/sysconf.h`. These were previously fine, as long as POSIX limits were guaranteed to be defined. However, some C libraries omit definitions for runtime-invariants values and instead force the application to query values via `sysconf()`. The specification formally supports that approach [1]. Normally, definitions are allowed to do so "on specific implementations where the corresponding value is equal to or greater than the stated minimum, but is unspecified." In practice, that is not always the case, but we need to be able to compile `sysconf()` anyway. Notable constants that are missing in Picolibc or Newlib include: - AIO_LISTIO_MAX - AIO_MAX - AIO_PRIO_DELTA_MAX - CHILD_MAX - DELAYTIMER_MAX - HOST_NAME_MAX - IOV_MAX - LOGIN_NAME_MAX - MQ_OPEN_MAX - MQ_PRIO_MAX - OPEN_MAX - PAGESIZE - PAGE_SIZE - PTHREAD_DESTRUCTOR_ITERATIONS - PTHREAD_KEYS_MAX - PTHREAD_THREADS_MAX - RTSIG_MAX - SEM_NSEMS_MAX - SEM_VALUE_MAX - SIGQUEUE_MAX - SS_REPL_MAX - STREAM_MAX - SYMLOOP_MAX - TIMER_MAX - TTY_NAME_MAX For greater portability, define those values using 1. a Kconfig-derived definition, 2. a symbolic minimum value (required by the spec), or 3. a numeric minimum value (if the required symbolic minimum is missing). [1] https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/limits.h.html Signed-off-by: Chris Friedt <[email protected]>
1 parent ef7c4dc commit 821c142

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

include/zephyr/posix/sys/sysconf.h

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef ZEPHYR_INCLUDE_POSIX_SYS_SYSCONF_H_
88
#define ZEPHYR_INCLUDE_POSIX_SYS_SYSCONF_H_
99

10+
#include <limits.h>
11+
1012
#include <zephyr/sys/util_macro.h>
1113

1214
#ifdef __cplusplus
@@ -244,7 +246,8 @@ enum {
244246
COND_CODE_1(_POSIX2_C_DEV > 0, (_POSIX2_C_DEV), (-1))
245247
#define __z_posix_sysconf_SC_2_CHAR_TERM (-1L)
246248
#define __z_posix_sysconf_SC_COLL_WEIGHTS_MAX _POSIX2_COLL_WEIGHTS_MAX
247-
#define __z_posix_sysconf_SC_DELAYTIMER_MAX _POSIX_DELAYTIMER_MAX
249+
#define __z_posix_sysconf_SC_DELAYTIMER_MAX \
250+
COND_CODE_1(CONFIG_POSIX_TIMERS, (CONFIG_POSIX_DELAYTIMER_MAX), (0))
248251
#define __z_posix_sysconf_SC_EXPR_NEST_MAX _POSIX2_EXPR_NEST_MAX
249252
#define __z_posix_sysconf_SC_2_FORT_DEV (-1L)
250253
#define __z_posix_sysconf_SC_2_FORT_RUN (-1L)
@@ -273,34 +276,42 @@ enum {
273276
#define __z_posix_sysconf_SC_CLK_TCK (100L)
274277
#define __z_posix_sysconf_SC_GETGR_R_SIZE_MAX (0L)
275278
#define __z_posix_sysconf_SC_GETPW_R_SIZE_MAX (0L)
276-
#define __z_posix_sysconf_SC_AIO_LISTIO_MAX AIO_LISTIO_MAX
277-
#define __z_posix_sysconf_SC_AIO_MAX AIO_MAX
278-
#define __z_posix_sysconf_SC_AIO_PRIO_DELTA_MAX AIO_PRIO_DELTA_MAX
279-
#define __z_posix_sysconf_SC_ARG_MAX ARG_MAX
280-
#define __z_posix_sysconf_SC_ATEXIT_MAX ATEXIT_MAX
281-
#define __z_posix_sysconf_SC_CHILD_MAX CHILD_MAX
282-
#define __z_posix_sysconf_SC_HOST_NAME_MAX HOST_NAME_MAX
283-
#define __z_posix_sysconf_SC_IOV_MAX IOV_MAX
284-
#define __z_posix_sysconf_SC_LOGIN_NAME_MAX LOGIN_NAME_MAX
279+
#define __z_posix_sysconf_SC_AIO_LISTIO_MAX _POSIX_AIO_LISTIO_MAX
280+
#define __z_posix_sysconf_SC_AIO_MAX _POSIX_AIO_MAX
281+
#define __z_posix_sysconf_SC_AIO_PRIO_DELTA_MAX 0
282+
#define __z_posix_sysconf_SC_ARG_MAX _POSIX_ARG_MAX
283+
#define __z_posix_sysconf_SC_ATEXIT_MAX 32
284+
#define __z_posix_sysconf_SC_CHILD_MAX _POSIX_CHILD_MAX
285+
#define __z_posix_sysconf_SC_HOST_NAME_MAX \
286+
COND_CODE_1(CONFIG_POSIX_NETWORKING, (CONFIG_POSIX_HOST_NAME_MAX), (0))
287+
#define __z_posix_sysconf_SC_IOV_MAX 16 /* _XOPEN_IOV_MAX */
288+
#define __z_posix_sysconf_SC_LOGIN_NAME_MAX _POSIX_LOGIN_NAME_MAX
285289
#define __z_posix_sysconf_SC_NGROUPS_MAX _POSIX_NGROUPS_MAX
286-
#define __z_posix_sysconf_SC_MQ_OPEN_MAX MQ_OPEN_MAX
287-
#define __z_posix_sysconf_SC_MQ_PRIO_MAX MQ_PRIO_MAX
288-
#define __z_posix_sysconf_SC_OPEN_MAX CONFIG_ZVFS_OPEN_MAX
289-
#define __z_posix_sysconf_SC_PAGE_SIZE PAGE_SIZE
290-
#define __z_posix_sysconf_SC_PAGESIZE PAGESIZE
291-
#define __z_posix_sysconf_SC_THREAD_DESTRUCTOR_ITERATIONS PTHREAD_DESTRUCTOR_ITERATIONS
292-
#define __z_posix_sysconf_SC_THREAD_KEYS_MAX PTHREAD_KEYS_MAX
293-
#define __z_posix_sysconf_SC_THREAD_STACK_MIN PTHREAD_STACK_MIN
294-
#define __z_posix_sysconf_SC_THREAD_THREADS_MAX PTHREAD_THREADS_MAX
295-
#define __z_posix_sysconf_SC_RTSIG_MAX RTSIG_MAX
296-
#define __z_posix_sysconf_SC_SEM_NSEMS_MAX SEM_NSEMS_MAX
297-
#define __z_posix_sysconf_SC_SEM_VALUE_MAX SEM_VALUE_MAX
298-
#define __z_posix_sysconf_SC_SIGQUEUE_MAX SIGQUEUE_MAX
299-
#define __z_posix_sysconf_SC_STREAM_MAX STREAM_MAX
300-
#define __z_posix_sysconf_SC_SYMLOOP_MAX SYMLOOP_MAX
301-
#define __z_posix_sysconf_SC_TIMER_MAX TIMER_MAX
302-
#define __z_posix_sysconf_SC_TTY_NAME_MAX TTY_NAME_MAX
303-
#define __z_posix_sysconf_SC_TZNAME_MAX TZNAME_MAX
290+
#define __z_posix_sysconf_SC_MQ_OPEN_MAX \
291+
COND_CODE_1(CONFIG_POSIX_MESSAGE_PASSING, (CONFIG_POSIX_MQ_OPEN_MAX), (0))
292+
#define __z_posix_sysconf_SC_MQ_PRIO_MAX _POSIX_MQ_PRIO_MAX
293+
#define __z_posix_sysconf_SC_OPEN_MAX CONFIG_POSIX_OPEN_MAX
294+
#define __z_posix_sysconf_SC_PAGE_SIZE CONFIG_POSIX_PAGE_SIZE
295+
#define __z_posix_sysconf_SC_PAGESIZE CONFIG_POSIX_PAGE_SIZE
296+
#define __z_posix_sysconf_SC_THREAD_DESTRUCTOR_ITERATIONS _POSIX_THREAD_DESTRUCTOR_ITERATIONS
297+
#define __z_posix_sysconf_SC_THREAD_KEYS_MAX \
298+
COND_CODE_1(CONFIG_POSIX_THREADS, (CONFIG_POSIX_THREAD_KEYS_MAX), (0))
299+
#define __z_posix_sysconf_SC_THREAD_STACK_MIN 0
300+
#define __z_posix_sysconf_SC_THREAD_THREADS_MAX \
301+
COND_CODE_1(CONFIG_POSIX_THREADS, (CONFIG_POSIX_THREAD_THREADS_MAX), (0))
302+
#define __z_posix_sysconf_SC_RTSIG_MAX \
303+
COND_CODE_1(CONFIG_POSIX_REALTIME_SIGNALS, (CONFIG_POSIX_RTSIG_MAX), (0))
304+
#define __z_posix_sysconf_SC_SEM_NSEMS_MAX \
305+
COND_CODE_1(CONFIG_POSIX_SEMAPHORES, (CONFIG_POSIX_SEM_NSEMS_MAX), (0))
306+
#define __z_posix_sysconf_SC_SEM_VALUE_MAX \
307+
COND_CODE_1(CONFIG_POSIX_SEMAPHORES, (CONFIG_POSIX_SEM_VALUE_MAX), (0))
308+
#define __z_posix_sysconf_SC_SIGQUEUE_MAX _POSIX_SIGQUEUE_MAX
309+
#define __z_posix_sysconf_SC_STREAM_MAX _POSIX_STREAM_MAX
310+
#define __z_posix_sysconf_SC_SYMLOOP_MAX _POSIX_SYMLOOP_MAX
311+
#define __z_posix_sysconf_SC_TIMER_MAX \
312+
COND_CODE_1(CONFIG_POSIX_TIMERS, (CONFIG_POSIX_TIMER_MAX), (0))
313+
#define __z_posix_sysconf_SC_TTY_NAME_MAX _POSIX_TTY_NAME_MAX
314+
#define __z_posix_sysconf_SC_TZNAME_MAX _POSIX_TZNAME_MAX
304315

305316
#ifdef CONFIG_POSIX_SYSCONF_IMPL_MACRO
306317
#define sysconf(x) (long)CONCAT(__z_posix_sysconf, x)

0 commit comments

Comments
 (0)