Skip to content

libs/libc/unistd: Answer every POSIX name in sysconf() - #21674

Merged
acassis merged 4 commits into
apache:masterfrom
casaroli:i-sysconf-up
Nov 11, 2026
Merged

acassis merged 4 commits into
apache:masterfrom
casaroli:i-sysconf-up

Conversation

@casaroli

@casaroli casaroli commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

sysconf() knew 21 names. For all other names of <unistd.h>, also for the limits that <limits.h> defines (LINE_MAX, HOST_NAME_MAX, TIMER_MAX, PTHREAD_KEYS_MAX, ...), it returned -1 with errno ENOSYS. POSIX requires EINVAL for a name that is not valid, and -1 without a change of errno for a valid name that has no limit or an option that is not supported. Programs read -1 as "no limit" or "not supported" and check errno to see an error.

Commits 1 and 2 come first because sysconf(_SC_SIGQUEUE_MAX) now gives 32, and sigqueue() never failed with EAGAIN. LTP sigqueue/9-1 skipped itself while sysconf() gave -1. With the new value it queued 33 signals and printed Failed: sigqueue() queued SIGQUEUE_MAX+1 signals; CI shows this as a timeout, because the runner waits 40 s for PASS. Each queued signal takes an entry from the kernel heap, so a sender could queue signals without a limit to a task that blocks them or does not run. POSIX requires EAGAIN when {SIGQUEUE_MAX} signals are still pending at the receiver. With commit 1, sigqueue() and pthread_sigqueue() fail with EAGAIN when 32 signals are pending in the task group of the receiver, or queued there for a handler. nxsig_tcbdispatch() counts them in the critical section that queues the signal, so concurrent senders cannot pass the limit. A new argument of nxsig_dispatch(), group_signal() and nxsig_tcbdispatch() applies the limit; only sigqueue() and pthread_sigqueue() set it. kill(), raise(), nxsig_queue() (aio, drivers), timers and message queue notifications are not limited, but their signals count. Commit 2 corrects the error name (EGAIN) in Documentation/reference/user/07_signals.rst.

Commit 3 makes sysconf() answer every name of <unistd.h>:

  • A limit gives the value of its <limits.h> constant (or the POSIX minimum, for the bc/expr/regex limits that <limits.h> does not define).
  • An option gives the value of its <unistd.h> macro. For an option that <unistd.h> leaves undefined, sysconf() gives _POSIX_VERSION if the configuration has it, else -1: _SC_FSYNC (always), _SC_IPV6 (CONFIG_NET_IPv6), _SC_REGEXP (CONFIG_LIBC_REGEX), _SC_THREAD_PRIO_INHERIT (CONFIG_PRIORITY_INHERITANCE), _SC_THREAD_PRIO_PROTECT (CONFIG_PRIORITY_PROTECT). Documentation/standards/posix.rst already lists these options as supported with the same options.
  • The names of the C compilation environments (_SC_V6_*, _SC_XBS5_*) give 1 for the environment of the build (from the sizes of int, long, pointers and off_t), else -1.
  • _SC_PHYS_PAGES and _SC_AVPHYS_PAGES come from sysinfo(). _SC_GETGR_R_SIZE_MAX is the buffer that getgrgid_r() needs with CONFIG_LIBC_GROUP_FILE.
  • _SC_THREAD_THREADS_MAX is -1 (no limit). The value 255 came from the uint8_t member count of the task group, which ec08031 removed; the PID hash table grows when it is full (sched/task/task_setup.c).
  • The other names (tracing, batch, STREAMS, job control, posix_spawn() with process groups, ...) give -1 with errno unchanged. A name that is not valid gives -1 with EINVAL.
  • _SC_THREAD_KEYS_MAX is 0 in a configuration without pthreads, which has no CONFIG_TLS_NELEM.

Commit 4 adds the sysconf() names of POSIX.1-2024 that <unistd.h> did not define, with their answers: _SC_NSIG (NSIG), _SC_THREAD_ROBUST_PRIO_INHERIT and _SC_THREAD_ROBUST_PRIO_PROTECT (the priority option and robust mutexes, !CONFIG_PTHREAD_MUTEX_UNSAFE), _SC_V7_* and _SC_V8_* (the same as _SC_V6_*), _SC_DEVICE_CONTROL and _SC_XOPEN_UUCP (-1: no posix_devctl(), no UUCP utilities).

The code stays a switch: a table of the values is not smaller with -Os (cortex-m0: 624 bytes for a table, 452 for the switch; cortex-m4: 620 and 628).

Impact

sigqueue() and pthread_sigqueue() fail with EAGAIN when 32 signals are pending or queued for a handler in the receiving task group, in every build mode; before, they queued without a limit. The other senders (kill(), raise(), nxsig_queue(), timers, message queues, aio) do not change.

sysconf() gives other results in every build mode. The values below are from qemu-armv8a:knsh (master 03aea78, then this PR):

  • Same as before (19 names): _SC_AIO_LISTIO_MAX, _SC_AIO_MAX, _SC_ARG_MAX, _SC_ATEXIT_MAX, _SC_CLK_TCK, _SC_CLOCK_SELECTION, _SC_CPUTIME, _SC_GETPW_R_SIZE_MAX, _SC_IOV_MAX, _SC_MONOTONIC_CLOCK, _SC_NGROUPS_MAX, _SC_NPROCESSORS_CONF, _SC_NPROCESSORS_ONLN, _SC_OPEN_MAX, _SC_PAGESIZE, _SC_PRIORITIZED_IO, _SC_REALTIME_SIGNALS, _SC_THREAD_CPUTIME, _SC_THREAD_STACK_MIN.
  • A name that is not valid: -1 with EINVAL (was ENOSYS).
  • Limits that now give a value (was -1 with ENOSYS): _SC_AIO_PRIO_DELTA_MAX 0, _SC_BC_BASE_MAX 99, _SC_BC_DIM_MAX 2048, _SC_BC_SCALE_MAX 99, _SC_BC_STRING_MAX 1000, _SC_CHILD_MAX 25, _SC_COLL_WEIGHTS_MAX 2, _SC_DELAYTIMER_MAX 32, _SC_EXPR_NEST_MAX 32, _SC_HOST_NAME_MAX 32, _SC_LINE_MAX 80, _SC_LOGIN_NAME_MAX 33, _SC_MQ_OPEN_MAX 8, _SC_MQ_PRIO_MAX 255, _SC_RE_DUP_MAX 255, _SC_RTSIG_MAX 32, _SC_SEM_NSEMS_MAX and _SC_SEM_VALUE_MAX INT_MAX, _SC_SIGQUEUE_MAX 32, _SC_STREAM_MAX 256, _SC_SYMLOOP_MAX 8, _SC_THREAD_DESTRUCTOR_ITERATIONS 4, _SC_THREAD_KEYS_MAX 0 (CONFIG_TLS_NELEM), _SC_TIMER_MAX 32, _SC_TTY_NAME_MAX 32, _SC_TZNAME_MAX 6, _SC_PHYS_PAGES 2354, _SC_AVPHYS_PAGES 2274.
  • Options that now give a value (was -1 with ENOSYS): 201712 for _SC_BARRIERS, _SC_FSYNC, _SC_READER_WRITER_LOCKS, _SC_SHARED_MEMORY_OBJECTS, _SC_THREADS, _SC_THREAD_ATTR_STACKADDR, _SC_THREAD_ATTR_STACKSIZE, _SC_THREAD_PRIORITY_SCHEDULING, _SC_THREAD_PROCESS_SHARED, _SC_VERSION; 1 for _SC_MESSAGE_PASSING, _SC_PRIORITY_SCHEDULING, _SC_SYNCHRONIZED_IO, _SC_TIMEOUTS, _SC_TIMERS, _SC_XOPEN_UNIX; 700 for _SC_XOPEN_VERSION. With the configuration options on, also _SC_THREAD_PRIO_INHERIT, _SC_THREAD_PRIO_PROTECT, _SC_REGEXP, _SC_IPV6 and one compilation environment name.
  • 61 names now give -1 with errno unchanged (was -1 with ENOSYS): the options of this configuration that are off (_SC_ASYNCHRONOUS_IO, _SC_MAPPED_FILES, _SC_SEMAPHORES, _SC_SPIN_LOCKS, _SC_SPORADIC_SERVER, ...), the options that NuttX does not have, _SC_GETGR_R_SIZE_MAX without CONFIG_LIBC_GROUP_FILE, and the compilation environments that the build does not use.
  • _SC_THREAD_THREADS_MAX: -1 with errno unchanged (was 255).
  • New names: _SC_NSIG 64; _SC_DEVICE_CONTROL, _SC_THREAD_ROBUST_PRIO_INHERIT, _SC_THREAD_ROBUST_PRIO_PROTECT, _SC_V7_*, _SC_V8_*, _SC_XOPEN_UUCP.

In nuttx and nuttx-apps, sysconf() is called only for _SC_PAGESIZE (fs/mmap/fs_mmisc.c, lib_valloc.c, getpagesize(), lely-canopen), _SC_OPEN_MAX (getdtablesize(), nsh_envcmds.c) and _SC_NPROCESSORS_ONLN (libuv). These values do not change.

Size: sysconf() is linked only when a program calls it. lm3s6965-ek:qemu-protected (-Os, armv7-m): sysconf 164 -> 676 bytes, text 127652 -> 128164. nucleo-l432kc:nsh, rv-virt:nsh and qemu-armv8a:nsh do not link it (text unchanged).

Testing

Test: apache/nuttx-apps#4022 (testing/libc/sysconf): every name gives a value or -1 with errno unchanged; -1, INT_MIN and INT_MAX give -1 with EINVAL; the limits and options give the value of their constant; _SC_OPEN_MAX is the RLIMIT_NOFILE soft limit; the page, processor and compilation environment names give sane values. QEMU with HVF, 1 CPU:

Configuration master 03aea78 this PR
qemu-armv8a:knsh (kernel) 126 names, 144 errors 139 names, 0 errors
qemu-armv8a:nsh (flat) 126 names, 143 errors 139 names, 0 errors
qemu-armv8a:knsh and :nsh with PRIORITY_INHERITANCE, PRIORITY_PROTECT, FS_LARGEFILE, TLS_NELEM=8, LIBC_REGEX 139 names, 0 errors; _SC_THREAD_PRIO_INHERIT, _SC_THREAD_PRIO_PROTECT, both _SC_THREAD_ROBUST_PRIO_* and _SC_REGEXP 201712, _SC_THREAD_KEYS_MAX 8, _SC_V6_LP64_OFF64 1 (knsh)
mps2-an500:knsh (protected, armv7-m, QEMU TCG) 139 names, 0 errors
ostest, qemu-armv8a:knsh and qemu-armv8a:nsh status 0 status 0

Signals (commits 1 and 2), master 17637de and this PR:

Test master this PR
LTP sigqueue_9_1, sim:citest (Linux, CI image) PASS (skips: sysconf() gives -1) PASS (the 33rd sigqueue() gives EAGAIN); without commit 1 it fails
463 LTP signal cases (sigqueue, sigwait*, sigtimedwait, kill, raise, sigaction, sigprocmask, ...), sim:citest 381 pass the same results
Test program: 32 sigqueue() pass, the 33rd and pthread_sigqueue() give EAGAIN; kill() is not limited; sigwaitinfo(), delivery and thread exit free the count; a timer, a message queue and an aio notification (with the SIGPOLL of nxsig_queue()) still come at the limit; 4 threads that send at the same time queue exactly 32 fails (no limit) pass: sim:nsh (with aio), sim:smp (4 CPUs), qemu-armv8a:knsh
ostest, qemu-armv8a:knsh and qemu-armv8a:nsh (HVF, 1 CPU) status 0

Signal builds with EXTRAFLAGS=-Werror, each OK: qemu-armv8a:nsh, :knsh, :nsh_smp, mps2-an500:knsh (protected), rv-virt:nsh, sim:nsh, qemu-armv8a:nsh with CONFIG_ENABLE_PARTIAL_SIGNALS, esp32s3-devkit:knsh (the two changed esp32s3 files); qemu-armv8a:nsh with CONFIG_DISABLE_PTHREAD (no group members) compiles.

Builds, each OK: rv-virt:nsh (flat, rv32) and lm3s6965-ek:qemu-protected (protected, armv7-m) with EXTRAFLAGS=-Werror; ea3131:pgnsh (arm926, CONFIG_DISABLE_PTHREAD: no CONFIG_TLS_NELEM); qemu-armv8a:nsh with CMake (Ninja) and the test, no compiler warning in lib_sysconf.c or the test.

Compared with glibc 2.43 (Fedora, x86_64): an invalid name gives -1 with EINVAL on both; _SC_PAGESIZE 4096, _SC_IOV_MAX 1024, _SC_RTSIG_MAX 32, _SC_THREAD_DESTRUCTOR_ITERATIONS 4, _SC_SEM_VALUE_MAX INT_MAX and _SC_THREAD_THREADS_MAX -1 are the same; _SC_OPEN_MAX is the RLIMIT_NOFILE soft limit on both. The values that come from the configuration differ: _SC_CLK_TCK (NuttX CONFIG_USEC_PER_TICK, 1000 here; Linux 100), _SC_HOST_NAME_MAX (CONFIG_HOST_NAME_MAX 32; Linux 64), _SC_NPROCESSORS_* (CONFIG_SMP_NCPUS).

tools/checkpatch.sh -c -u -m -g origin/master..HEAD passes.

@github-actions github-actions Bot added Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Nov 11, 2026
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

esp32-devkitc

  • ROM: .flash.text +88 B (+0.1%, 130,304 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +88 B (+0.1%, 96,240 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +64 B (+0.1%, 86,532 B / 4,194,304 B, total: 2% used)

qemu-armv8a

  • Code: .text.group_signal +12 B, .text.group_signal_handler +8 B, .text.nxsig_dispatch +20 B, .text.nxsig_dispatch_unmasked +4 B, .text.nxsig_kill +4 B, .text.nxsig_notification_source +4 B, .text.nxsig_tcbdispatch +144 B, .text.nxsig_tgkill +4 B, .text.nxtask_exithook +4 B, .text.pthread_sigqueue +4 B, .text.sigqueue +4 B (+0.1%, 364,348 B)

qemu-intel64

  • Code: .text +153 B (+0.0%, 8,696,641 B)

rx65n-rsk2mb

  • ROM: .text +96 B (+0.1%, 92,156 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +480 B (+0.1%, 391,616 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Comment thread sched/signal/sig_queue.c Resolved
sigqueue() never failed with EAGAIN.  Each queued signal takes an entry
from the kernel heap, so a sender could queue signals without a limit to
a task that blocks them, or that does not run.  POSIX requires EAGAIN
when {SIGQUEUE_MAX} signals that the process queued are still pending at
the receiver.

sigqueue() and pthread_sigqueue() now fail with EAGAIN when SIGQUEUE_MAX
(32) signals are pending in the task group of the receiver, or queued
there for a signal handler.  nxsig_tcbdispatch() counts them in the
critical section that queues the signal, so senders on other CPUs cannot
pass the limit.  The count reads the lists, so no counter must follow
the paths that deliver or discard a signal.

A new argument of nxsig_dispatch(), group_signal() and
nxsig_tcbdispatch() applies the limit; only sigqueue() and
pthread_sigqueue() set it.  kill(), raise(), nxsig_queue() (aio,
drivers), the timers and the message queue notifications are not
limited, but their signals count.

Impact: sigqueue() and pthread_sigqueue() of more than 32 signals to a
receiver that does not take them now fail with EAGAIN, as POSIX
requires.  The other senders do not change.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
Give the correct name of the error and say when it occurs.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
sysconf() knew 21 names.  For all other names of <unistd.h>, also for
the limits that <limits.h> defines (LINE_MAX, HOST_NAME_MAX, TIMER_MAX,
PTHREAD_KEYS_MAX, ...), it returned -1 with errno ENOSYS.  POSIX
requires EINVAL for a name that is not valid, and -1 without a change of
errno for a valid name that has no limit or an option that is not
supported.  Programs read -1 as "no limit" and check errno.

Return the value of the <limits.h> or <unistd.h> constant for each
limit and option.  For an option that <unistd.h> leaves undefined,
return _POSIX_VERSION if the configuration has it (fsync(), IPv6,
regcomp(), the priority inheritance and protection of mutexes), else -1.
The names of the C compilation environments give 1 for the environment
of the build.  _SC_THREAD_THREADS_MAX is -1: the number of threads has
no limit (the uint8_t member count of the task group is gone).
_SC_PHYS_PAGES and _SC_AVPHYS_PAGES come from sysinfo().
_SC_GETGR_R_SIZE_MAX is the buffer that getgrgid_r() needs.  Return -1
with errno unchanged for the other names, and -1 with EINVAL for a name
that is not valid.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
POSIX.1-2024 lists sysconf() names that <unistd.h> did not define:
_SC_DEVICE_CONTROL, _SC_NSIG, _SC_THREAD_ROBUST_PRIO_INHERIT,
_SC_THREAD_ROBUST_PRIO_PROTECT, _SC_V7_* and _SC_V8_* (the C
compilation environments), and _SC_XOPEN_UUCP.  Define them and answer
them in sysconf(): _SC_NSIG is NSIG; the robust mutex names follow the
priority options and CONFIG_PTHREAD_MUTEX_UNSAFE; the environment names
give the same values as the _SC_V6_* names.  NuttX has no
posix_devctl() and no UUCP utilities: -1.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Arch: xtensa Issues related to the Xtensa architecture Size: L The size of the change in this PR is large and removed Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Nov 11, 2026
@acassis
acassis merged commit 0d2b21b into apache:master Nov 11, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: xtensa Issues related to the Xtensa architecture Area: Documentation Improvements or additions to documentation Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants