Repository navigation
Conversation
POSIX specifies "extern char **environ;", and programs assign it, for example "environ = some_array;" before they run another program. NuttX defines environ as a macro for get_environ_ptr(), so those programs do not compile. In a flat or protected build all tasks share the C library data, so the macro stays. In the user space of a kernel build every process has its own copy, and environ is now a real variable: - libc defines it, and crt0 sets it to the environment and registers its address in the new ar_environ field of the process's reserved data area. - The kernel keeps owning the environment. getenv(), setenv(), unsetenv(), clearenv() and get_environ_ptr() first take over an array that the program assigned to environ (env_sync_in()), copying it before the old environment is freed because its strings often point into the old one. After a change they point environ at the new environment (env_sync_out()). exec and posix_spawn see the program's array through the same path. A program that did not register environ (built before this change) is not affected: its reserved area holds NULL. Changing one entry of the kernel's array in place (environ[i] = "X=1") is still not supported. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
…lds. In flat and protected builds environ is a macro for get_environ_ptr(), a function call, so programs that assign environ do not compile there. All tasks of those builds share the C library data, so environ cannot be a single variable. With the new option CONFIG_LIBC_ENVIRON_ASSIGNABLE (default n) it names a per-task-group location instead: "#define environ (*get_environ_location())", which returns the address of the new ta_environ field of the group's task_info_s. That is an lvalue, so "environ = array;" compiles. As with errno, a program's own "extern char **environ;" compiles with a -Wstrict-prototypes warning. The kernel uses the same env_sync_in() and env_sync_out() as in kernel builds; only the location differs. The kernel can reach any group's task_info_s in these builds, so env_dup() also publishes the environment of a new group. The option adds about 330 bytes (mps2-an500:nsh). Without it nothing changes. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
casaroli
requested review from
Donny9,
GUIDINGLI,
anchao,
jerpelea,
masayuki2009,
pussuw,
simbit18,
xiaoxiang781216 and
yf13
as code owners
October 9, 2026 15:16
| * | ||
| ****************************************************************************/ | ||
|
|
||
| static FAR char ***env_user_environ(FAR struct task_group_s *group) |
Contributor
There was a problem hiding this comment.
we have a better fix which moves all environ staff from kernel to libc, and will upstream the related change by tomorrow.
Contributor
Author
There was a problem hiding this comment.
OK, I will close this PR when your change lands; please link it here. I can test it with our program that assigns environ, calls setenv(), passes environ to posix_spawn() and calls clearenv().
|
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
POSIX specifies
extern char **environ;, and programs assign it, for exampleenviron = some_array;before they run another program. NuttX definesenvironas a macro forget_environ_ptr(), so those programs do not compile:error: lvalue required as left operand of assignment.Commit 1, kernel builds: in the user space of a kernel build every process has its own copy of the C library data, so
environis now a real variable.ar_environfield of the process's reserved data area.getenv(),setenv(),unsetenv(),clearenv()andget_environ_ptr()first take over an array that the program assigned toenviron(env_sync_in()), and copy it before the old environment is freed, because its strings often point into the old one. After a change they pointenvironat the new environment (env_sync_out()).execandposix_spawnsee the program's array through the same path.NULL.Commit 2, flat and protected builds: all tasks share the C library data, so
environcannot be one variable. The new optionCONFIG_LIBC_ENVIRON_ASSIGNABLE(default n) makesenvironname a per-task-group location:#define environ (*get_environ_location()), the newta_environfield of the group'stask_info_s. That is an lvalue, soenviron = array;compiles. The kernel uses the sameenv_sync_in()andenv_sync_out().Impact
Kernel builds:
environis a variable in user space; the kernel code above runs on each environment call. Flat and protected builds: no change unlessCONFIG_LIBC_ENVIRON_ASSIGNABLE=y.Size (text + data), flat builds with the option off: no change on
lm3s6965-ek:qemu-flat(262000) andmps2-an500:nsh(365031). With the option on: +360 bytes onmps2-an500:nsh, +188 bytes onlm3s6965-ek:qemu-protected.Not supported: changing one entry of the kernel's array in place (
environ[i] = "X=1"). With the option, a program's ownextern char **environ;compiles with a-Wstrict-prototypeswarning, as forerrno.Testing
QEMU 11. Test programs built outside the tree against the export package for
knsh, and built into the image in place ofexamples/hellofornsh.qemu-armv8a:knsh: a program assignsenviron, callssetenv(), runs itself as a child withposix_spawnp(..., environ), then callsclearenv().Before:
After:
qemu-armv8a:nsh(flat) withCONFIG_LIBC_ENVIRON_ASSIGNABLE=y: the same steps, with a child task fromtask_create(). Without the option the program does not compile (lvalue error), as before.Kernel builds with
-Wno-cpp -Werror, each followed bymake exportand the appsmake import:qemu-armv8a:knsh,imx93-evk:knsh,qemu-armv7a:knsh,sabre-6quad:netknsh,sama5d4-ek:knsh,rv-virt:knsh,rv-virt:knsh64,rv-virt:ksmp64,rv-virt:knsh_paging,rv-virt:knetnsh64,rv-virt:kfb64,icicle:knsh,canmv230:knsh,esp32s3-devkit:kernel_n8r2.Flat and protected builds with
-Wno-cpp -Werror, option off and on:lm3s6965-ek:qemu-flat(on: overflows flash by 24 bytes; the board has no room left),mps2-an500:nsh,lm3s6965-ek:qemu-protected.tools/checkpatch.sh -c -u -m -gpasses.