Commit graph

561 commits

Author SHA1 Message Date
Anas Nashif
303af992e5 style: fix 'if (' usage in cmake files
Replace with 'if(' and 'else(' per the cmake style guidelines.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-10-29 11:44:13 +02:00
Fabio Baltieri
700a1a5a28 lib, kernel: use single evaluation min/max/clamp
Replace all in-function instances of MIN/MAX/CLAMP with the single
evaluation version min/max/clamp.

There's probably no race conditions in these files, but the single
evaluation ones save a couple of instructions each so they should save
few code bytes and potentially perform better, so they should be
preferred in general.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2025-10-24 01:10:40 +03:00
Jukka Rissanen
5d4e4e5018 lib: posix: device_io: Add standard file descriptor sizes
Make sure we always allocate space for stdin, stdout and stderr file
descriptors if Posix device io option is enabled.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-10-21 17:22:03 +03:00
Benjamin Cabé
312688d7ea Revert "posix: xsi: streams: deprecate CONFIG_XOPEN_STREAMS"
This reverts commit b762a43738
which was merged without enough time in review.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-10-20 11:28:54 -04:00
Chris Friedt
b762a43738 posix: xsi: streams: deprecate CONFIG_XOPEN_STREAMS
CONFIG_XOPEN_STREAMS does not follow the pattern of other XSI Option
Groups, where the Option Group name is not the same as the feature
test macro that indicates it is supported by the implementation.

Deprecate CONFIG_XOPEN_STREAMS and rename it to CONFIG_XSI_STREAMS.

For more information, please see
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/\
V1_chap02.html#tag_02_01_05_09

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-10-19 20:53:48 -04:00
Chris Friedt
8f899db04d posix: sched: move sched_yield() to pthread.c
The `sched_yield()` function was originally included to facilitate the of
POSIX_REALTIME and POSIX_THREADS_EXT Option Groups in Issue 5.

It was then marked as part of the _POSIX_PROCESS_SCHEDULING Option in
Issue 6, but then was not clearly marked as part of the
POSIX_THREADS(_BASE) Option Group until Issue 7.

Moving it to `pthread.c` (and making it a function with regular
linkage rather than inline) ensures that it will be available with the
`POSIX_THREADS` Option Group.

For more information, please see `POSIX_THREADS_BASE` in
https://pubs.opengroup.org/onlinepubs/9699919799/xrat/\
V4_subprofiles.html and
https://pubs.opengroup.org/onlinepubs/9799919799/xrat/\
V4_subprofiles.html

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-10-15 11:46:00 -04:00
Chris Friedt
aaf391405c posix: options: add keep-sorted-start and -stop
Add zephyr-keep-sorted-start and zephyr-keep-sorted-stop
comments.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-10-15 12:08:00 +03:00
Chris Friedt
8a48177130 posix: profiles: make POSIX_AEP_CHOICE_ZEPHYR the default
Default POSIX_AEP_CHOICE to POSIX_AEP_CHOICE_ZEPHYR

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-10-15 12:08:00 +03:00
Chris Friedt
27bc1fed47 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 <cfriedt@tenstorrent.com>
2025-10-15 12:08:00 +03:00
Chris Friedt
c64974cc75 posix: c_lib_ext: apply clang-format to getopt sources
The getopt sources were not formatted according to our codying style
conventions, so apply formatting rules as a separate commit.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-10-15 12:08:00 +03:00
Chris Friedt
12fd61d6c7 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 <cfriedt@tenstorrent.com>
2025-10-15 12:08:00 +03:00
Chris Friedt
ed0ae68f77 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 <cfriedt@tenstorrent.com>
2025-10-15 12:08:00 +03:00
Chris Friedt
20cda02d11 posix: multi process: add support for times()
Add support for the `times()` function, which can be used to get the
number of ticks spent in "system" and "user" mode, and which returns
the "real time" that has expired, since an arbitrary point in the past.

The following invariant should always hold:

rtime <= stime + utime

The `times()` function measures time for the calling process and all
of it's child processes. In Zephyr, we don't support separate processes
(yet), so the time spent in child processes is zero.

Additionally, in Zephyr we do not differentiate between "system" time
(i.e. time spent executing kernel code) and "user" time (time spent
executing application code). We only have information on "total time"
(time spent executing code) and "execution time" (time spent executing
code plus idle time).

For now, only report utime, since it is not clear how to obtain other
values.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-10-14 17:27:08 -04:00
Chris Friedt
c8e22ec596 tests: posix: signals: adjust expectations for real-time signals
1. Fix an off-by-one error in `signo_fits()` (a utility function inside
   signal.c

2. Adjust expectations for C libraries that do not provide a sigset_t
  large enough to support `SIGRTMIN` or `SIGRAMAX`.

This fixes an issue in CI that appeared when run under ARC.

```shell
west build -p auto -b qemu_arc/qemu_arc_hs -t run tests/posix/signals
```

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-10-06 20:15:10 -04:00
Chris Friedt
5cbb2a421d posix: switch to using posix_time.h and posix_signal.h
To avoid conflicts between the C library's `time.h` and signal.h use an
"override" header (when necessary) for C libraries that do not themselves
provide POSIX definitions in `time.h` or `signal.h`.

V2

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-10-01 14:36:55 +03:00
Fabio Baltieri
9384cc5b96 Revert "posix: switch to using posix_time.h and posix_signal.h"
This reverts commit 1eb5c97bd6.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2025-09-25 14:15:57 -04:00
Chris Friedt
1eb5c97bd6 posix: switch to using posix_time.h and posix_signal.h
To avoid conflicts between the C library's `time.h` and signal.h use an
"override" header (when necessary) for C libraries that do not themselves
provide POSIX definitions in `time.h` or `signal.h`.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-09-25 12:01:23 +01:00
Chris Friedt
f071df29a7 posix: net: move gethostname() from static inline to normal function
Convert `gethostname()` from a static inline wrapper to a normal function
with regular linkage.

This change was prompted mainly as housekeeping for #95811, but a
secondary reason was to make this a linkable symbol so that the API works
at a binary level as well (which could be provided by the toolchain as
well).

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-09-23 09:39:24 +01:00
Romain Pelletant
732823e0c6 posix: options: replace kconfig for rw lock
- Update Kconfig from CONFIG_POSIX_READER_WRITER_LOCKS to POSIX_RW_LOCKS

Signed-off-by: Romain Pelletant <romainp@kickmaker.net>
2025-09-19 08:27:49 -04:00
Chris Friedt
86eca47529 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
ifdef CONFIG_POSIX_THREAD_SAFE_FUNCTIONS
..
endif /* CONFIG_POSIX_THREAD_SAFE_FUNCTIONS */
```

Which is not at all accurate, since that subprofiling option group is
`(CONFIG_)POSIX_SYSTEM_DATABASE_R` (which is a PR that is in-draft).

Remove the guards, since they are invalid anyway, and at most an
application will link properly. We don't really need the guards anyway,
since conditional compilation will be used to link the files into the
build.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-09-09 17:27:28 +02:00
Chris Friedt
ab8b55ac99 posix: move posix limit definitions to posix_limits.h
Rather than keeping limits defined in a common way, controllable with one
Kconfig, move posix definitions out of the standard common libc header
`<limits.h>` into `<zephyr/posix/posix_limits.h>`, not controlled with
Kconfig. Additionally, include `<zephyr/posix/posix_limits.h>` wherever
an in-tree C library includes a custom `<limits.h>`.

Note: the only reason any of the `<limits.h>` files exist, is to define
the POSIX limit PATH_MAX without declaring `_POSIX_C_SOURCE`, which would
be necessary according to the standard, and each of the custom `<limits.h>`
files is nearly identical in this regard.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-08-29 18:21:10 +02:00
Chris Friedt
c4533d1b69 posix: move limit definitions to common libc limits.h
In order to reduce conflicts with limits defined in external C libraries,
move Zephyr's POSIX limits from posix_features.h to limits.h in the
common C library sources.

In order to give the implementation better control over where POSIX
limit definitions originate, use `#include_next <limits.h>` to pull in
the corresponding header of the C library, and provide a non-user
configurable option `CONFIG_TC_PROVIDES_POSIX_LIMIT_DEFS` that may be set
in order to ensure that the C library version of POSIX limits are used.

Note: this may mean that runtime invariant values are inconsistent with
parts that are actually in use within Zephyr, so applications are
encouraged to use `sysconf()` with external C libraries that implement
parts of the POSIX standard to query system configuration values at
runtime.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-08-29 18:21:10 +02:00
Maksymilian Goryszewski
ba30c7b4e8 lib: posix: fs: Basic fstat() using seek and tell
Implement limited posix fstat()

Signed-off-by: Maksymilian Goryszewski <mgoryszewski@internships.antmicro.com>
2025-08-27 08:27:14 +02:00
Chris Friedt
82460c8a1d posix: signals: use Kconfig options instead of _NSIG
Use CONFIG_POSIX_RTSIG_MAX instead of _NSIG, since RTSIG_MAX isn't always
going to be defined.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-08-06 12:13:53 +03:00
Chris Friedt
a6f2bb0c4c posix: do not rely on PAGE_SIZE being defined in limits.h
Since an implementation may omit definitions of runtime invariant values
in limits.h if the corresponding value is equal to or greater than the
minimum, is unspecified, and must be queried at runtime [1], do not rely
on PAGE_SIZE being declared in limits.h .

[1]
https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/limits.h.html

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-08-06 12:13:42 +03:00
Mohamed Moawad
4b90816982 posix: Map CLOCK_REALTIME and CLOCK_MONOTONIC to Zephyr clock IDs
Some toolchains may define CLOCK_REALTIME and
CLOCK_MONOTONIC in their libc headers
with values that differ from Zephyr's internal
SYS_CLOCK_REALTIME and SYS_CLOCK_MONOTONIC.

To ensure consistent behavior across all boards and
toolchains, Introduce a helper function to map
CLOCK_REALTIME and CLOCK_MONOTONIC to Zephyr's internal
clock IDs (SYS_CLOCK_REALTIME and SYS_CLOCK_MONOTONIC).

This prevents mismatched clock IDs being passed to
the kernel, avoiding invalid clockid errors when using
functions like clock_gettime().

Signed-off-by: Mohamed Moawad <moawad@synopsys.com>
2025-07-19 13:43:21 -04:00
Alberto Escolar Piedras
e5db1f7f4a Kconfig: Remove NATIVE_APPLICATION option
CONFIG_NATIVE_APPLICATION, which represents the way in which
native_posix and its kin were built, was deprecated in 4.1 after the
introduction of native_sim and CONFIG_NATIVE_LIBRARY.

We remove support for it now for 4.3.

Let's remove the kconfig option and all dependencies in the kconfig
tree.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2025-07-19 09:38:15 +02:00
Guennadi Liakhovetski
05616c51b6 posix: fix posix clock undefined type
When built with the minimal libc while using POSIX clocks, Zephyr
compilation breaks with "error: unknown type name 'clockid_t'." Use
int instead of clockid_t to eliminate the dependency.

Suggested-by: Chris Friedt <cfriedt@tenstorrent.com>
Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2025-07-09 09:43:52 -05:00
Chris Friedt
c3368f6491 posix: env: use sizeof void ptr instead of char ptr ptr
Coverity regularly raised a CID with every release that it found
`sizeof(char **)` confusing and somehow nonportable.

With every release, it needed to be marked as a false positive.

Change `sizeof(char **)` to `sizeof(void *)` to silence the
CID warnings.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-06-26 10:50:56 -10:00
Minh Tang
8ce9e11242 lib: posix: Update attribute packed for posix_thread_attr
Add attributed packed for struct posix_thread_attr when arch is RX

Signed-off-by: Minh Tang <minh.tang.ue@bp.renesas.com>
2025-06-26 14:07:03 +02:00
Chris Friedt
8743da2f32 posix: use sys_clock implementation
Use the newly added sys_clock API in lib/os for

* clock_gettime()
* clock_settime()
* clock_nanosleep() and nanosleep()
* gettimeofday()

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-06-24 14:18:52 +02:00
Chris Friedt
282b47cf37 posix: cond + mutex: log messages generate warnings for 32-bit timeouts
Previously, log messages would generate warnings (escalated to errors
in CI) when CONFIG_TIMEOUT_64BIT=n.

For example,

```
west build -p auto -b qemu_cortex_m0 -t run tests/posix/timers/ \
  -- -DCONFIG_TIMEOUT_64BIT=n
..
warning: format '%llx' expects argument of type \
  'long long unsigned int', but argument 3 has type 'k_ticks_t' \
  {aka 'unsigned int'}
```

Use portable print specifiers and cast the argument to int64_t.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-06-24 14:18:52 +02:00
Keith Packard
34a03fb436 posix: Use 'char' for posix_condattr bitfields
Ensure that posix_condattr is smaller than pthread_condattr by keeping it
under one byte.

Signed-off-by: Keith Packard <keithp@keithp.com>
2025-06-17 16:06:21 +02:00
Jordan Yates
b64fb93d93 lib: posix: dedicated symbol for POSIX system headers
Add a dedicated symbol for including the POSIX system headers path
directly into the include path, enabling (for example)
`#include <time.h>` instead of `#include <zephyr/posix/time.h>`.

Signed-off-by: Jordan Yates <jordan@embeint.com>
2025-06-11 08:19:54 +02:00
Nimish Tambe
936d0278bd posix: fnmatch: fix bugs in fnmatch(), see issue #55186
all tests in tests/posix/c_lib_ext/src/fnmatch.c seem to pass
except one (which is still commented)

Signed-off-by: Nimish Tambe <nimisht@gmail.com>
2025-06-09 17:05:40 -05:00
Anas Nashif
7bbb834deb Revert "posix: rwlock: Refactor locking using k_timepoint_t"
This reverts commit 833eaccfd1.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-05-30 08:27:19 -04:00
Anas Nashif
0a6e579987 Revert "posix: net: move static inline gethostname() to lib definition"
This reverts commit e60070d91b.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-05-30 08:27:19 -04:00
Anas Nashif
a649b31cfc Revert "posix: device_io: mark pwrite() buf as const"
This reverts commit c14f97f194.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-05-30 08:27:19 -04:00
Anas Nashif
da48a04572 Revert "posix: sysconf: match _SC* defines with newlib/picolib"
This reverts commit c5f1c43b8c.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-05-30 08:27:19 -04:00
Nicholas Lowell
c5f1c43b8c posix: sysconf: match _SC* defines with newlib/picolib
It's possible for newlib/picolib libc libraries to
internally call sysconf() which would execute zephyr's
implementation.  However, if the _SC* defines do not have
matching values, then the incorrect switch case executes.

This issue arises when using newlib/picolib libc that includes
sysconf implementation for ARM.  With current defaults, the
zephyr sysconf() overrides the original libc sysconf() so
we must ensure proper operation.

We will switch to the #define list just like newlib/picolib.
We can't currently use their unistd.h directly due to a domino
of declaration conflicts.
For the "small" macro implementation, we have to drop using
CONCAT to prevent pre-expansion of the new #defines

Signed-off-by: Nicholas Lowell <Nicholas.Lowell@lexmark.com>
2025-05-30 10:26:22 +02:00
Nicholas Lowell
c14f97f194 posix: device_io: mark pwrite() buf as const
match standard by having input void *buf parameter
for pwrite() marked as const, and avoid any potential
declaration conflicts

Signed-off-by: Nicholas Lowell <Nicholas.Lowell@lexmark.com>
2025-05-30 10:26:22 +02:00
Nicholas Lowell
e60070d91b posix: net: move static inline gethostname() to lib definition
static inline gethostname() in unistd.h can cause declaration collisions.
we should just move it to a normal function definition like the
rest of the network functions.

Signed-off-by: Nicholas Lowell <Nicholas.Lowell@lexmark.com>
2025-05-30 10:26:22 +02:00
Cla Mattia Galliard
833eaccfd1 posix: rwlock: Refactor locking using k_timepoint_t
Make use of k_timepoint_t ind the calculation of the remaining time for the
rwlock.

Signed-off-by: Cla Mattia Galliard <clamattia@gmail.com>
2025-05-30 10:26:02 +02:00
Chris Friedt
8a5c744213 posix: timers: use newly added timespec util functions
Use the newly added timespec util functions to manipulate and
compare timespec structures with overflow detection.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-22 23:57:17 +02:00
Benjamin Cabé
a3f658454a posix: clock: fix maybe-uninitialized warning in z_clock_gettime
Compiler gets confused and thinks base may be used uninitialized. This
shouldn't be possible, but to make the warning go away, initialize it.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-05-14 15:18:57 +02:00
Chris Friedt
ae4a1dbf2c posix: implement the POSIX_CLOCK_SELECTION Option Group
Implement the POSIX_CLOCK_SELECTION Option Group.

This was mostly already done, but compiled / linked in the wrong places.

E.g. pthread_condattr_getclock() and pthread_condattr_setclock() were
in pthread.c and part of POSIX_THREADS_BASE. clock_nanosleep() was in
clock.c and part of POSIX_TIMERS.

This change builds them as part of clock_selection.c with
CONFIG_POSIX_CLOCK_SELECTION.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-13 22:23:21 -04:00
Chris Friedt
cfae041834 posix: implement the XSI_SINGLE_PROCESS Option Group
gettimeofday() was already implemented, but incorrectly lumped into
POSIX_TIMERS.

putenv() is really just a wrapper around setenv().

The only one left to implement was gethostid() which was relatively
trivial.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-13 22:23:21 -04:00
Chris Friedt
0b869efe6a posix: clock: move most implementations to clock_common.c
Move most implementations to clock_common.c in preparation for
moving gettimeofday() and clock_nanosleep() to different compilation
units.

We also take this as an opportunity to switch from using k_spinlock
to sys_sem.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-13 22:23:21 -04:00
Chris Friedt
73ee23e3ad posix: clock: create z_clock_gettime().. convenience functions.
To facilitate moving gettimeofday() and clock_nanosleep() to separate
compilation units, make z_clock_nanosleep(), z_clock_gettime(),
and z_clock_settime() convenience functions.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-13 22:23:21 -04:00
Chris Friedt
5c46fcb1e9 posix: env: move most implementations to env-common.c
Move most implementations to env-common.c in preparation for adding
putenv.c .

We also take this as an opportunity to switch from using k_spinlock
to sys_sem.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-13 22:23:21 -04:00