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

posix: env: support for environ, getenv(), setenv(), unsetenv() #66762

Merged
merged 6 commits into from
Mar 8, 2024

Conversation

cfriedt
Copy link
Member

@cfriedt cfriedt commented Dec 21, 2023

Note: Coding Guidelines failures are false positives

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.

This lets us do fun things like what is shown below in the Zephyr shell:

uart:~$ posix env 
  set    get    unset
uart:~$ posix env get 
uart:~$ posix env set FOO=BAR
uart:~$ posix env get
FOO=BAR
uart:~$ posix env get FOO
BAR
uart:~$ posix env unset FOO
uart:~$ posix env set " 42 BCD" "No Problem!"
uart:~$ posix env get
uart:~$ posix env set "_BcD" "No Problem!"
uart:~$ posix env get
_BcD=No Problem!
uart:~$ posix env unset _BcD

App and library code can then query values from the environment, as a relatively simple and portable way of steering the behaviour of code at runtime.

Some notes:

  • "poorly conditioned" variable names are dropped silently in the shell, which matches bash (or at least zsh)
  • the Zephyr shell does not perform shell variable expansion at this time; in other words echo "$VALUE" will not work as one might expect

Possible future improvements:

  • Heh... half joking, but adding shell variable expansion (or even an actual POSIX shell)
  • this implementation is optimized for size (a speed optimization could maybe use hash tables)
  • another future optimization for safety would be to have a malloc-free implementation
  • a possibly nice feature for the shell would be to enable tab completions of names with env get <tab>
  • another future improvement would be to use Kconfig / iterable sections to optionally enable some default environment variables to be populated (e.g. HOME, PWD, ...).
  • another future improvement might be to add hooks for "dynamic" environment variables like $RANDOM.

Fixes #66861
Fixes #66862
Fixes #66863
Fixes #66864

@cfriedt cfriedt force-pushed the setenv branch 2 times, most recently from 51df4f7 to 64111f4 Compare December 21, 2023 16:47
@cfriedt cfriedt marked this pull request as ready for review December 21, 2023 16:53
@zephyrbot zephyrbot added area: Shell Shell subsystem area: C Library C Standard Library area: POSIX POSIX API Library labels Dec 21, 2023
@cfriedt cfriedt marked this pull request as draft December 21, 2023 16:55
@cfriedt cfriedt force-pushed the setenv branch 2 times, most recently from 7e0f165 to 02aaed3 Compare December 21, 2023 18:21
@cfriedt cfriedt requested a review from aescolar December 21, 2023 18:21
@cfriedt

This comment was marked as resolved.

@cfriedt
Copy link
Member Author

cfriedt commented Dec 21, 2023

Screenshot 2023-12-21 at 1 41 35 PM

@cfriedt cfriedt marked this pull request as ready for review December 21, 2023 18:49
@zephyrbot zephyrbot added the area: native port Host native arch port (native_sim) label Dec 21, 2023
@aescolar

This comment was marked as off-topic.

arch/posix/CMakeLists.txt Outdated Show resolved Hide resolved
tests/posix/env/testcase.yaml Outdated Show resolved Hide resolved
@aescolar

This comment was marked as off-topic.

@cfriedt cfriedt marked this pull request as ready for review January 31, 2024 05:48
@cfriedt cfriedt requested a review from ycsin January 31, 2024 05:48
ycsin
ycsin previously approved these changes Jan 31, 2024
Copy link
Member

@ycsin ycsin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cfriedt
Copy link
Member Author

cfriedt commented Feb 2, 2024

LGTM

Thanks - I'll rebase to fix merge conflicts 😄

@cfriedt
Copy link
Member Author

cfriedt commented Feb 3, 2024

@ycsin - I also created a sample application (borrowed from your uname sample)

ycsin
ycsin previously approved these changes Feb 5, 2024
lib/posix/options/env.c Outdated Show resolved Hide resolved
tests/posix/env/src/env.c Outdated Show resolved Hide resolved
cfriedt added 6 commits March 7, 2024 10:42
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>
Add tests for environ, getenv(), setenv(), unsetenv(), and
getenv_r().

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
Mark environ, getenv(), setevn(), and unsetenv as supported.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
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>
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>
Add a sample application to demonstrate some basic
C and shell interfaces for manipulating environment
variables.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
@cfriedt
Copy link
Member Author

cfriedt commented Mar 7, 2024

  • mitigate possible memory leak by only assigning non-NULL return value to environ following realloc()
  • change _m_home and other macros to e.g. M_HOME in test suite

@cfriedt cfriedt merged commit 6ef6cf5 into zephyrproject-rtos:main Mar 8, 2024
25 of 26 checks passed
@cfriedt cfriedt deleted the setenv branch March 11, 2024 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: C Library C Standard Library area: POSIX POSIX API Library area: Samples Samples area: Shell Shell subsystem area: Tests Issues related to a particular existing or missing test
Projects
None yet
7 participants