Commit graph

1574 commits

Author SHA1 Message Date
Pisit Sawangvonganan 557ad8e45c shell: devmem: address cmd_dump multiple call problem
Call `getopt_init()` to reset `state->optind` instead of directly setting
`optind = 1`. The `getopt()` function uses `getopt_state` from
`getopt_state_get` for state handling, so `getopt_init()` should be called
to correctly run the command again.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-05-15 10:13:23 +02:00
Alberto Escolar Piedras 7c8d538982 posix: env: Fix 2 build warnings
Fix 2 build warnings in posix/options/env:
The maximum length given to strncpy() matches the input
string length, which makes the call equivalent to strcpy().
As the destination buffer size has been ensured sufficient
(in the first case by chechking just before,
in the second case by allocating it big enough),
let's just use strcpy() instead.

lib/posix/options/env.c: In function 'getenv_r':
lib/posix/options/env.c:109:17: error: 'strncpy' specified bound
depends on the length of the source argument
[-Werror=stringop-truncation]
  109 |                 strncpy(buf, val, vsize);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~
lib/posix/options/env.c:104:25: note: length computed here
  104 |                 vsize = strlen(val) + 1;
      |                         ^~~~~~~~~~~
lib/posix/options/env.c: In function 'setenv':
lib/posix/options/env.c:191:17: error: 'strncpy' output truncated
before terminating nul copying as many bytes from a string as its
length [-Werror=stringop-truncation]
  191 |                 strncpy(environ[ret], name, nsize);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/posix/options/env.c:128:51: note: length computed here
  128 |         const size_t nsize = (name == NULL) ? 0 : strlen(name);
      |                                                   ^~~~~~~~~~~~

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-05-14 13:25:33 +02:00
David Schneider 012c9a2847 libc/picolibc: Support Picolibc for toolchains without bundled Picolibc
Allow Picolibc to get build from module, if the toolchain does not
include a bundled picolibc.
This should always be possible, except for native applications and
if libcpp is required.

Signed-off-by: David Schneider <david.schneider@chargepoint.com>
2024-05-09 10:28:35 +02:00
Chris Friedt b1e2e4ac2c posix: fs: create a separate Kconfig option for fsync
Create CONFIG_POSIX_FSYNC to match the spec.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-05-07 21:22:56 -04:00
Patryk Duda 60d03c53e1 libc: minimal: Add 'noreturn' attribute to abort() and exit()
This aligns abort() and exit() definitions with other libc.

Without 'noreturn' attribute, compilers have to assume that we will
return from these functions which can lead to surprising errors like
'error: non-void function does not return a value'.

Signed-off-by: Patryk Duda <patrykd@google.com>
2024-05-06 17:32:17 +01:00
frei tycho 32ba549536 lib: os: coding guidelines: add explicit cast to void
- added explicit cast to void when returned value is expectedly ignored

Signed-off-by: frei tycho <tfrei@baumer.com>
2024-05-03 07:52:42 -04:00
Yong Cong Sin 4363a787b1 posix: uname: increase version name length
to 70 to prevent overflowing the char array.

Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2024-04-29 17:40:16 +01:00
Patryk Duda ef258166ba posix: Introduce getentropy() function
The function writes random data to the provided buffer. Maximum
permitted buffer size is 256 bytes. On success the function returns 0,
otherwise it returns -1 and sets errno to appropriate value.

Signed-off-by: Patryk Duda <patrykd@google.com>
2024-04-29 11:46:06 +01:00
Chris Friedt cdb1193f3e posix: add stubs for asynchronous io
Add stubs for POSIX asynchronous io that return -1 and set
errno to ENOTSUP.

The functions and structures in aio.h are required by the
_POSIX_ASYNCHRONOUS_IO Option as detailed in Section E.1 of
IEEE-1003.1-2017.

The _POSIX_ASYNCHRONOUS_IO interface is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-04-29 11:02:11 +02:00
Alberto Escolar Piedras 60d5ce452e libC kconfig: Introduce a new option to avoid the native libC
Let's introduce a new option so components which cannot
be used with the native libC can select it.
And at this point let's already have POSIX_API select it
instead of being used in the libC choice default.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-04-24 09:53:36 +02:00
Alberto Escolar Piedras 84e7107b32 Revert "posix: use sys_sem instead of k_spinlock for pool synch"
This reverts commit 6e66aa1f7c.

And fixes an indentation issue in the original code,
which triggers checkpatch.

This PR:
https://github.com/zephyrproject-rtos/zephyr/pull/71718
introduced a regression breaking CI:
https://github.com/zephyrproject-rtos/zephyr/issues/71814
Let's just revert it until a better fix is done.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-04-23 11:51:44 -04:00
Chris Friedt 6e66aa1f7c posix: use sys_sem instead of k_spinlock for pool synch
Based on Andy's talk at eoss 2024, use the sys/sem.h api instead
of the spinlock.h api to synchronize pooled elements since it
has minimal overhead like semaphores but also works from
userspace.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-04-23 07:01:25 -04:00
Karthikeyan Krishnasamy cc06c2b21f lib: posix: fs: fix errno handling
ftruncate should return -1 on errors and it needs to
set appropriate error values in errno

Signed-off-by: Karthikeyan Krishnasamy <karthikeyan@linumiz.com>
2024-04-22 04:33:16 -07:00
Karthikeyan Krishnasamy cd1ed1cdbf lib: posix: fs: add ftruncate support
Add support for ftruncate in posix implementation

Signed-off-by: Karthikeyan Krishnasamy <karthikeyan@linumiz.com>
2024-04-15 13:23:37 -04:00
Karthikeyan Krishnasamy 65ad32b3ca lib: os: fdtable: add support for fsync
Add support for fsync in existing posix ioctl call

Signed-off-by: Karthikeyan Krishnasamy <karthikeyan@linumiz.com>
2024-04-15 13:23:37 -04:00
Gaetan Perrot 51439f012a posix: Implement isastream
`isastream()` is required as part of _XOPEN_STREAMS Option Group.

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
2024-04-13 20:23:34 -04:00
Celina Sophie Kalus 5bd86eaddb posix: eventfd: Fix unsetting internal flags in ioctl
Commit e6eb0a705b ("posix: eventfd: revise locking, signaling, and
allocation") introduced a regression where the internal flags of an
event file descriptor would be erased when calling the F_SETFL ioctl
operation.

This includes the flag EFD_IN_USE_INTERNAL which determines whether
this file descriptor has been opened, thus effectively closing the
eventfd whenever one tries to change a flag.

Signed-off-by: Celina Sophie Kalus <hello@celinakalus.de>
2024-04-13 06:00:08 -04:00
Fin Maaß 1ab6f82c7e lib: smf: check for NULL
don't execute share_paren() if the target is NULL
in smf_execute_ancestor_exit_actions()

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
2024-04-10 07:40:21 -04:00
Yong Cong Sin cd2c7874b9 posix: uname: use BUILD_VERSION only if not empty
The `BUILD_VERSION` can be defined but empty when built
without git, causing version to be missing from the uname
version string:

```
*** Booting Zephyr OS build 3.5.0 ***

Printing everything in utsname...
sysname[7]: Zephyr
nodename[7]: zephyr
release[13]: 3.5.0
version[61]:  Apr  1 2024 23:48:30
machine[8]: riscv64

uart:~$
```

Let's check if it is empty before using it, so that
`KERNEL_VERSION_STRING`, which is generated independently
with cmake can be used as a fallback:

```
*** Booting Zephyr OS build 3.5.0 ***

Printing everything in utsname...
sysname[7]: Zephyr
nodename[7]: zephyr
release[13]: 3.5.0
version[61]: 3.5.0 Apr  1 2024 23:53:48
machine[8]: riscv64

uart:~$
```

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-04-04 23:47:33 +02:00
Gaetan Perrot 228d932967 posix: implement pthread_setschedprio
Implement posix pthread_setschedprio()

Signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
2024-04-04 10:25:53 -04:00
Chris Friedt 5ab984a834 posix: convert static inline functions to library functions
With CONFIG_NET_POSIX_NAMES being deprecated, convert static
inlines in headers to prototypes, and move implementations
to lib/posix/options/net.c .

Since select and poll should technically also operate on
non-socket file descriptors, these may be relocated in the
future.

Below are some reasons for this change.
1. So posix calls are regular symbols and not directly inlined
   as syscalls. This is also the most portable linkage type.
2. Many posix calls are cancellation points and additional
   checks are necessary within the implementation.
3. The more we add to an inline call, the less sense it makes
   for that call to be inline.
4. When the implementation of a posix function changes, the
   interface for it should still remain consistent.

Note: the same principles have been applied in Zephyr's
posix implementation already, e.g. clock_gettime(), so this
is nothing new.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-04-02 21:01:33 -04:00
Gaetan Perrot d61a7b2777 posix: Implement set and get inheritsched APIs for pthread attr
Implement `pthread_attr_setinheritsched()` and
`pthread_attr_getinheritsched()`are required
as part of _POSIX_THREAD_PRIORITY_SCHEDULING Option Group.

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
2024-04-01 11:04:35 +01:00
Chris Friedt 494493a3b8 posix: net: add a trivial implementation of sockatmark()
Add a trivial implementation of sockatmark().

Signed-off-by: Chris Friedt <chrisfriedt@gmail.com>
2024-03-27 07:36:18 -04:00
Chris Friedt 62031f5daa posix: net: add trivial netdb function implementations
Add trivial function implementations for those declared in
netdb.h .

Signed-off-by: Chris Friedt <chrisfriedt@gmail.com>
2024-03-27 07:36:18 -04:00
Chris Friedt a00eeb638a posix: net: implement if_nameindex() et al
Implement if_indextoname(), if_freenameindex(), if_nameindex(),
and if_nametoindex() by wrapping around networking subystem
calls.

Signed-off-by: Chris Friedt <chrisfriedt@gmail.com>
2024-03-27 07:36:18 -04:00
Chris Friedt 7a397a08b7 posix: net: implement inet_ntoa()
Add an implementation of inet_ntoa().

Signed-off-by: Chris Friedt <chrisfriedt@gmail.com>
2024-03-27 07:36:18 -04:00
Chris Friedt 4edb9017c2 posix: net: add kconfig option and inet_addr()
Add a Kconfig option for POSIX_NETWORKING and implement
inet_addr().

Signed-off-by: Chris Friedt <chrisfriedt@gmail.com>
2024-03-27 07:36:18 -04:00
Christopher Friedt 1847280fb4 posix: rwlock: implement pthread_rwlockattr_setpshared
Implement pthread_rwlockattr_setpshared() and
pthread_rwlockattr_getpshared().

Both functions are required by the _POSIX_READER_WRITER_LOCKS
Option as detailed in Section E.1 of IEEE-1003.1-2017.

The _POSIX_READER_WRITER_LOCKS Option is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-22 16:19:57 -04:00
Christopher Friedt 61c4139925 posix: rwlock: pthread_rwlockattr_init / destroy in lib
Move the implementation of the following functions into
the posix library rather than having themn as static
inline functions.

* pthread_rwlockattr_init()
* pthread_rwlockattr_destroy()

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-22 16:19:57 -04:00
Christopher Friedt d36dd8097e posix: clock: implement clock_getres()
clock_getres() is required by the POSIX_TIMERS Option Group
as detailed in Section E.1 of IEEE-1003.1-2017.

The POSIX_TIMERS Option Group is required for PSE51, PSE52,
PSE53, and PSE54 conformance, and is otherwise mandatory for
any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

With this, we have complete support for the POSIX_TIMERS
Option Group.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-22 16:19:27 -04:00
Christopher Friedt c9edda0fbf posix: unistd: support for confstr()
Support querying POSIX string configuration values (similar to
sysconf()).

confstr() is required by the POSIX_SINGLE_PROCESS Option
Group as detailed in Section E.1 of IEEE-1003.1-2017 and has
been part of the specification since POSIX-2.

The POSIX_SINGLE_PROCESS Option Group is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

With this, we have complete support for the POSIX_SINGLE_PROCESS
Option Group.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-22 08:57:44 +01:00
Gaetan Perrot 669edc0d5e posix: Implement set and get scope APIs for pthread attr
Implement `pthread_attr_setscope()` and `pthread_attr_getscope()`
are required as part of _POSIX_THREAD_PRIORITY_SCHEDULING Option Group.

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
2024-03-18 10:15:53 +01:00
Matthias Alleman b0f9f16f14 lib: posix: shell: Fix compiler warning of lib__posix__shell
This PR fixes the following compiler warning:
No SOURCES given to Zephyr library: lib__posix__shell

Signed-off-by: Matthias Alleman <matthias.alleman@basalte.be>
2024-03-15 06:30:31 -04:00
Christopher Friedt cdc36d090c posix: sysconf: add a "small" implementation option
Previously, sysconf() was only available as a macro (i.e. the
"extra-small" option).

This has the advantage of being compile-time constant, and
optimized for both space and speed. One disadvantage is that
querying an `_SC_` value that was invalid or unsupported
would result in a compile error.

Provide a "small" implementation of sysconf() (via Kconfig
choice) as a normal addressable function.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-12 17:57:11 +00:00
Gaetan Perrot 6badcad883 posix: Implement getmsg and getpmsg
`getmsg()` and `getpmsg()` are required
as part of _XOPEN_STREAMS Option Group.

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
2024-03-11 14:25:26 +01:00
Jukka Rissanen 71fd9b2e07 posix: syslog: Fix uninitialized variable error
Seen this in compiler

lib/posix/options/syslog.c: In function 'setlogmask':
lib/posix/options/syslog.c:66:16: error: 'oldpri' may be used uninitialized
   66 |         return oldpri;
      |                ^~~~~~
lib/posix/options/syslog.c:59:13: note: 'oldpri' was declared here
   59 |         int oldpri;
      |             ^~~~~~
lib/posix/options/syslog.c: In function 'vsyslog':
lib/posix/options/syslog.c:83:33: error: 'mask' may be used uninitialized
   83 |         if ((BIT(level) & mask) == 0) {
      |             ~~~~~~~~~~~~~~~~~~~~^~~~
lib/posix/options/syslog.c:71:17: note: 'mask' was declared here
   71 |         uint8_t mask;
      |                 ^~~~

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-03-10 07:44:03 -04:00
Christopher Friedt 529023e6f8 posix: manually define _POSIX_C_SOURCE in lib/posix
This PR adds declarations for the application conformance
feature test macro _POSIX_C_SOURCE.

It needs to be defined to value greater than or equal to 200112L
by the appplication.

However, Zephyr currently does not have a simple and consistent
means of specifying this value for POSIX samples, tests,
applications, and other libraries.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-08 04:28:47 -05:00
Christopher Friedt 447fc24352 posix: shell: add an environment variable shell command
This service is used to get, set, and unset system
environment variables.

Note: shell parameter expansion is not supported
at this time.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-08 04:28:47 -05:00
Christopher Friedt 0f23818153 posix: env: support for environ, getenv(), setenv(), unsetenv()
Support getting and setting POSIX environment variables.

Additionally, the thread-safe BSD variant getenv_r() is
provided.

environ, getenv(), setenv(), and unsetenv() are required by
the POSIX_SINGLE_PROCESS Option Group as detailed in
Section E.1 of IEEE-1003.1-2017.

The POSIX_SINGLE_PROCESS Option Group is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-08 04:28:47 -05:00
Florian La Roche 0d5a670f4f lib/os/printk.c: use ARG_UNUSED() for char_out()
Use ARG_UNUSED() within the function char_out()
to handel unused param.

Signed-off-by: Florian La Roche <Florian.LaRoche@gmail.com>
2024-03-06 12:11:26 +01:00
Glenn Andrews 0569809c80 Lib: SMF: Add initial transition and smf_set_handled()
Brings SMF framework closer into alignment with accepted Hierarchical State
Machine operation by:
1. Allowing 'programming by difference' by having some child states handle
   events and prevent propagation up to the parent run actions while others
   propagate events up to a common handler in a parent state.
2. Optionally allow initial transitions within a parent state to determine
   the most nested child state to transition to.
3. Adding a test case for `CONFIG_SMF_INITIAL_TRANSITION` and
   `smf_set_handled()`
4. Updating documentation for the new API (and fixing some references)

There was discussion in https://github.com/zephyrproject-rtos/zephyr/issues/55344
about not making the initial transition a Kconfig option, but I'm not sure
of any way else of doing it without permanently adding a pointer to each
`smf_state` entry, which is a problem for resource-constrained devices.

This does not fix https://github.com/zephyrproject-rtos/zephyr/issues/66341
but documentation has been updated to warn users of the issue.

Signed-off-by: Glenn Andrews <glenn.andrews.42@gmail.com>
2024-03-04 22:16:40 +01:00
Radoslaw Koppel 118f1592ff buildsystem: Enable LTO also for the application
It turns out that currently LTO is enabled only for the kernel.
This commit updates it to enable it for the whole application
and adds additional LTO exclusions required for the standard
C libraries to build and link properly.

Signed-off-by: Radosław Koppel <radoslaw.koppel@nordicsemi.no>
2024-03-02 15:39:36 +01:00
Christopher Friedt 3593720080 posix: syslog: add support for syslog
This is just a simple wrapper around Zephyr's native log api.

Note: the standard LOG_ERR syslog priority does conflict with
Zephyr's LOG_ERR() macro. This will need to be worked-around on
a case-by-case basis.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-02-29 02:45:01 -05:00
Robert Lubos 0966be01fc libc: Move gmtime_r into common
gmtime_r() has been in the minimal libc for years, however it was not
added to expcetions due to an overlook. In order to do this however, it
has to be moved first to the common libc area, so that it's available
to any libc that may not implement it.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-02-28 00:54:42 +09:00
Christopher Friedt ec27e8875d posix: mutexattr: improvements for pthread_mutexattr_t
Previously, pthread_mutexattr_gettype() and
pthread_mutexattr_settype() were non-conformant and also
less safe, as they would not check whether a pthread_mutexattr_t
had been initialized prior to manipulating them. Furthermore,
they would potentially dereference NULL pointers.

Additionally, move the pthread_mutexattr_init() and
pthread_mutexattr_destroy() functions to the library, and add
some level of checking to them so that they are more than simply
static inline / no-op calls.

Lastly, reduce the size of struct pthread_mutexattr to only
what is necessary (one byte should suffice).

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-02-27 06:11:03 -05:00
Christopher Friedt 1a3bb3fd5c posix: mqmeue: do not return NULL after pthread_exit()
pthread_exit() does not return and therefore it does not make
sense to return NULL after it in mq_notify_thread(), and that
would constitute dead code.

Rather than explicitly exiting the thread, simply return
gracefully from the thread function, and allow the pthread to
terminate in the usual way.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-02-26 08:12:29 -05:00
Gaetan Perrot cd060f6cd3 posix: Implement fdetach and fattach
`fdetach()` and `fattach()` are required
as part of _XOPEN_STREAMS Option Group.

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
2024-02-26 11:36:04 +00:00
Christopher Friedt f3709e20b8 posix: convert all error logs to debug logs
In Zephyr, things are often optimized for size first. That's how
we fit into such tight parking spaces.

This change gives more control to the user about whether the
POSIX API does any logging at all, simultaneously shrinking binary
size while improving speed.

No bytes / cycles left behind!

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-02-24 10:34:05 -05:00
Najumon B.A 89745fe472 lib: acpi: update shell and test app with the modifed resource struct
update shell and test app with the modifed resource struct such as
acpi_irq_resource and acpi_mmio_resource

Signed-off-by: Najumon B.A <najumon.ba@intel.com>
2024-02-15 12:28:55 +01:00
Najumon B.A 2139479722 lib: acpi: fix for build error when acpi not enabled
fix for acpi.h header file generates invalid C-code when
 CONFIG_ACPI=n

Signed-off-by: Najumon B.A <najumon.ba@intel.com>
2024-02-15 12:28:55 +01:00