Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: posix: add include/posix to search path and drop posix header prefix #43987

Closed
wants to merge 1 commit into from

Conversation

cfriedt
Copy link
Member

@cfriedt cfriedt commented Mar 19, 2022

It is a pain point to prefix standard POSIX APIs (e.g. #include <zephyr/posix/unistd.h>). This is true, in particular, when integrating third-party code into an external module. However, using the POSIX API should also not be a default in Zephyr.

With this, if users choose to include standard POSIX headers via Kconfig, then those headers will be part of the default search path. The headers themselves do not move. POSIX headers will not appear in the default search path unless so configured.

Additional changes:

  • removed posix/time.h to mitigate need for #include_next workaround
  • gathered NSEC_PER_MSEC and USEC_PER_MSEC defs intosys_clock.h
  • restructured some syscalls (unistd, ioctl, clock)
  • sprinkled #include <unistd.h> where close() is used or use zsock_close() instead
  • removed a fair amount of duplicate code
  • additional improvements planned to improve code sharing / coexistence with net API
  • suggested minor changes to picolibc lib: libc: picolibc: time.h definitions and declarations #46910

Fixes #43998

@github-actions github-actions bot added area: API Changes to public APIs area: C Library C Standard Library area: Networking area: POSIX POSIX API Library labels Mar 19, 2022
@cfriedt cfriedt force-pushed the posix-includes branch 6 times, most recently from 797aa26 to 82c6301 Compare March 19, 2022 03:38
@cfriedt
Copy link
Member Author

cfriedt commented Mar 19, 2022

Required module changes

Unrelated failures
Pre-existing failures in main

Tests of primary concern

  • twister -G -T tests/posix -T tests/net
  • twister -b -T samples/posix -T samples/net
  • twister -p native_posix_64 -T tests/net -T samples/net

Compliance and Coding Style test failures are false positives.

@cfriedt cfriedt force-pushed the posix-includes branch 8 times, most recently from 07b56bf to 1596433 Compare March 20, 2022 20:04
@cfriedt cfriedt marked this pull request as ready for review March 20, 2022 21:08
@cfriedt
Copy link
Member Author

cfriedt commented Jul 5, 2022

@keith-packard - can you be a bit more specific?
The C library uses POSIX functions for various operations, in particular, picolibc uses:

More or less what I anticipated as well, because newlib had similar overlap. The hazards look entirely manageable though, @keith-packard. I would say the one that could potentially be (hand-wavy) is open, but mainly because of fcntl.h.

Probably should not be used in the Zephyr (yet?)
isatty

Probably should not be used in Zephyr
getpid
kill

If there is any discrepancy, we will likely use the picolibc definitions
lseek
lseek64
fstat
open

Likely fine (Note: we use libc's <time.h>):
read
unlink
write
gettimeofday
close
_exit

@keith-packard
Copy link
Collaborator

More or less what I anticipated as well, because newlib had similar overlap. The hazards look entirely manageable though, @keith-packard. I would say the one that could potentially be (hand-wavy) is open, but mainly because of fcntl.h.

Zephyr currently has conflicting definitions for many of these APIs as it defines them as aliases for static inline functions. That means code including picolibc headers will not get the correct definitions, they'll get external function references, which may (or may not) work, but will certainly be less efficient.

I think what would be best would be for the standard posix headers to define the standard posix APIs generating the right code to operate in Zephyr. That means inline code for syscalls, for instance.

Given that the Zephyr SDK is already using a new GNU triplet including '-zephyr-', it's reasonable to expect the libraries in that SDK to be zephyr-specific, so if the C library is built as part of that toolchain, it can (and should) understand the Zephyr syscall interface, defining the functions correctly for use in Zephyr. Similarly, a C library built as part of Zephyr should use the Zephyr syscall interface.

Including hacks in Zephyr to work around libraries not designed for it is reasonable where you can't fix the library. In this case, the upstream projects would all be happy to accept Zephyr OS support.

@cfriedt cfriedt force-pushed the posix-includes branch 2 times, most recently from 7d9c35e to 36889dd Compare July 6, 2022 00:29
@cfriedt
Copy link
Member Author

cfriedt commented Jul 6, 2022

More or less what I anticipated as well, because newlib had similar overlap. The hazards look entirely manageable though, @keith-packard. I would say the one that could potentially be (hand-wavy) is open, but mainly because of fcntl.h.

Zephyr currently has conflicting definitions for many of these APIs as it defines them as aliases for static inline functions.

This change fixes that.

I think what would be best would be for the standard posix headers to define the standard posix APIs generating the right code to operate in Zephyr. That means inline code for syscalls, for instance.

This change fixes that too.

Given that the Zephyr SDK is already using a new GNU triplet including '-zephyr-', it's reasonable to expect the libraries in that SDK to be zephyr-specific, so if the C library is built as part of that toolchain, it can (and should) understand the Zephyr syscall interface, defining the functions correctly for use in Zephyr. Similarly, a C library built as part of Zephyr should use the Zephyr syscall interface.

That is a reasonable long-term goal. Some work depends on this being resolved in the short-term.

Including hacks in Zephyr to work around libraries not designed for it is reasonable where you can't fix the library. In this case, the upstream projects would all be happy to accept Zephyr OS support.

Upstreamers welcome :-)

@MaureenHelm MaureenHelm removed the dev-review To be discussed in dev-review meeting label Jul 6, 2022
@cfriedt cfriedt force-pushed the posix-includes branch 2 times, most recently from 13c5de0 to caaeca8 Compare July 14, 2022 00:46
cfriedt added a commit to cfriedt/thrift that referenced this pull request Aug 11, 2022
This is a temporary workaround until the mountain of
tech-debt is flattened w.r.t the following:

zephyrproject-rtos/zephyr#43987
zephyrproject-rtos/gsoc-2022-thrift#62
zephyrproject-rtos/zephyr#43998
zephyrproject-rtos/zephyr#46910
zephyrproject-rtos/zephyr#45100

but also around the mountain of tech-debt as a result of
poor arch/posix software architecture, CONFIG_ARCH_POSIX
and CONFIG_POSIX_API incompatibility, and the resultant
spillover of moving POSIX definitions into the network
subsystem as a result.

Fixes zephyrproject-rtos/gsoc-2022-thrift#129

Signed-off-by: Christopher Friedt <[email protected]>
cfriedt added a commit to zephyrproject-rtos/gsoc-2022-thrift that referenced this pull request Aug 11, 2022
This is a temporary workaround until the mountain of
tech-debt is flattened w.r.t the following:

zephyrproject-rtos/zephyr#43987
#62
zephyrproject-rtos/zephyr#43998
zephyrproject-rtos/zephyr#46910
zephyrproject-rtos/zephyr#45100

but also around the mountain of tech-debt as a result of
poor arch/posix software architecture, CONFIG_ARCH_POSIX
and CONFIG_POSIX_API incompatibility, and the resultant
spillover of moving POSIX definitions into the network
subsystem as a result.

Fixes #129

Signed-off-by: Christopher Friedt <[email protected]>
cfriedt added a commit to zephyrproject-rtos/gsoc-2022-thrift that referenced this pull request Aug 11, 2022
This is a temporary workaround until the mountain of
tech-debt is flattened w.r.t the following:

zephyrproject-rtos/zephyr#43987
#62
zephyrproject-rtos/zephyr#43998
zephyrproject-rtos/zephyr#46910
zephyrproject-rtos/zephyr#45100

but also around the mountain of tech-debt as a result of
poor arch/posix software architecture, CONFIG_ARCH_POSIX
and CONFIG_POSIX_API incompatibility, and the resultant
spillover of moving POSIX definitions into the network
subsystem as a result.

Fixes #129

Signed-off-by: Christopher Friedt <[email protected]>
cfriedt added a commit to zephyrproject-rtos/gsoc-2022-thrift that referenced this pull request Aug 11, 2022
This is a temporary workaround until the mountain of
tech-debt is flattened w.r.t the following:

zephyrproject-rtos/zephyr#43987
#62
zephyrproject-rtos/zephyr#43998
zephyrproject-rtos/zephyr#46910
zephyrproject-rtos/zephyr#45100

but also around the mountain of tech-debt as a result of
poor arch/posix software architecture, CONFIG_ARCH_POSIX
and CONFIG_POSIX_API incompatibility, and the resultant
spillover of moving POSIX definitions into the network
subsystem as a result.

Fixes #129

Signed-off-by: Christopher Friedt <[email protected]>
@cfriedt
Copy link
Member Author

cfriedt commented Aug 11, 2022

Currently doing a rework of this to separate it into a series of several easily-reviewable commits.

cfriedt added a commit to zephyrproject-rtos/gsoc-2022-thrift that referenced this pull request Aug 11, 2022
This is a temporary workaround until the mountain of
tech-debt is flattened w.r.t the following:

zephyrproject-rtos/zephyr#43987
#62
zephyrproject-rtos/zephyr#43998
zephyrproject-rtos/zephyr#46910
zephyrproject-rtos/zephyr#45100

but also around the mountain of tech-debt as a result of
poor arch/posix software architecture, CONFIG_ARCH_POSIX
and CONFIG_POSIX_API incompatibility, and the resultant
spillover of moving POSIX definitions into the network
subsystem as a result.

Fixes #129

Signed-off-by: Christopher Friedt <[email protected]>
…efix

It is a pain point to prefix standard POSIX APIs (e.g.
`#include <posix/unistd.h>`). This is true, in particular,
when integrating third-party code into an external module.

With this, if users choose to include standard POSIX headers via Kconfig,
then those headers will be part of the default search path. The headers
themselves do not move. POSIX headers will not appear in the default
search path unless so configured.

Additional changes:
* removed `posix/time.h` to circumvent `#include_next` workaround
* gathered `NSEC_PER_MSEC` and `USEC_PER_MSEC` defs into `sys_clock.h`
* restructured some syscalls (unistd, ioctl, clock_getttime)
* sprinkled `#include <unistd.h>` where `close()` is used
* removed a fair amount of duplicate code

Fixes zephyrproject-rtos#43998

Signed-off-by: Christopher Friedt <[email protected]>
cfriedt added a commit to cfriedt/thrift that referenced this pull request Oct 1, 2022
This is a temporary workaround until the mountain of
tech-debt is flattened w.r.t the following:

zephyrproject-rtos/zephyr#43987
zephyrproject-rtos/gsoc-2022-thrift#62
zephyrproject-rtos/zephyr#43998
zephyrproject-rtos/zephyr#46910
zephyrproject-rtos/zephyr#45100

but also around the mountain of tech-debt as a result of
poor arch/posix software architecture, CONFIG_ARCH_POSIX
and CONFIG_POSIX_API incompatibility, and the resultant
spillover of moving POSIX definitions into the network
subsystem as a result.

Fixes zephyrproject-rtos/gsoc-2022-thrift#129

Signed-off-by: Christopher Friedt <[email protected]>
cfriedt added a commit to cfriedt/thrift that referenced this pull request Oct 1, 2022
This is a temporary workaround until the mountain of
tech-debt is flattened w.r.t the following:

zephyrproject-rtos/zephyr#43987
zephyrproject-rtos/gsoc-2022-thrift#62
zephyrproject-rtos/zephyr#43998
zephyrproject-rtos/zephyr#46910
zephyrproject-rtos/zephyr#45100

but also around the mountain of tech-debt as a result of
poor arch/posix software architecture, CONFIG_ARCH_POSIX
and CONFIG_POSIX_API incompatibility, and the resultant
spillover of moving POSIX definitions into the network
subsystem as a result.

Fixes zephyrproject-rtos/gsoc-2022-thrift#129

Signed-off-by: Christopher Friedt <[email protected]>
cfriedt added a commit to cfriedt/thrift that referenced this pull request Oct 25, 2022
This is a temporary workaround until the mountain of
tech-debt is flattened w.r.t the following:

zephyrproject-rtos/zephyr#43987
zephyrproject-rtos/gsoc-2022-thrift#62
zephyrproject-rtos/zephyr#43998
zephyrproject-rtos/zephyr#46910
zephyrproject-rtos/zephyr#45100

but also around the mountain of tech-debt as a result of
poor arch/posix software architecture, CONFIG_ARCH_POSIX
and CONFIG_POSIX_API incompatibility, and the resultant
spillover of moving POSIX definitions into the network
subsystem as a result.

Fixes zephyrproject-rtos/gsoc-2022-thrift#129

Signed-off-by: Christopher Friedt <[email protected]>
cfriedt added a commit to cfriedt/thrift that referenced this pull request Oct 27, 2022
This is a temporary workaround until the mountain of
tech-debt is flattened w.r.t the following:

zephyrproject-rtos/zephyr#43987
zephyrproject-rtos/gsoc-2022-thrift#62
zephyrproject-rtos/zephyr#43998
zephyrproject-rtos/zephyr#46910
zephyrproject-rtos/zephyr#45100

but also around the mountain of tech-debt as a result of
poor arch/posix software architecture, CONFIG_ARCH_POSIX
and CONFIG_POSIX_API incompatibility, and the resultant
spillover of moving POSIX definitions into the network
subsystem as a result.

Fixes zephyrproject-rtos/gsoc-2022-thrift#129

Signed-off-by: Christopher Friedt <[email protected]>
@github-actions
Copy link

github-actions bot commented Nov 2, 2022

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label Nov 2, 2022
@github-actions github-actions bot closed this Nov 17, 2022
@cfriedt cfriedt deleted the posix-includes branch June 3, 2023 01:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: C Library C Standard Library area: native port Host native arch port (native_sim) area: Networking area: POSIX POSIX API Library Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

posix: add include/posix to search path based on Kconfig
7 participants