stand: add a mechanism to avoid env var propagation to kenv

Our only user of this at the moment is teken.{fg,bg}_color.  These are
special because teken is a library common to both the kernel and the
loader, and we want to avoid having special vars to control the loader
vs. the kernel.  Ideally, if a user wants a different set of console
colors, then they set the appropriate teken variable and it
Just Works(TM) everywhere.  We can't just avoid setting the env vars,
because we specifically want to install a hook to adjust how loader is
drawn.

This allows us to avoid breaking a kernel config(5) that has some
default teken colors set with our defaults.  That's a valid
configuration, even if it might seem weird that they don't want to set
colors in both loader and the kernel -- they may not anticipate spending
any time in loader, and thus prefer to just let it do its default
behavior.

NOKENV is expected to be unset if the value is overwritten, rather than
acting as a persistent marker that we do not want the value to persist
under any circumstance.  We can always add another flag bit later for
persistence if we find a use for that, but most variables are fine to
carry over.  This is mostly needed for environment variables that we
really just want to set a hook for.

Future work could expand this to break it out to the scripted
interfaces.  We have discussed some options like a new built-in command,
or adding a flag to the existing `set` command, but haven't really come
up with a concrete plan to avoid confusion.

Reviewed by:	imp
Differential Revision:	https://reviews.freebsd.org/D50888
This commit is contained in:
Kyle Evans
2025-08-21 22:48:14 -05:00
parent 95e6fd1fd8
commit 510e473ff3
4 changed files with 25 additions and 4 deletions
+8 -1
View File
@@ -242,6 +242,7 @@ gfx_fb_evalcolor(const char *envname, teken_color_t *cattr,
{ {
const char *ptr; const char *ptr;
char env[10]; char env[10];
int eflags = EV_VOLATILE | EV_NOKENV;
bool from_env = false; bool from_env = false;
ptr = getenv(envname); ptr = getenv(envname);
@@ -257,10 +258,16 @@ gfx_fb_evalcolor(const char *envname, teken_color_t *cattr,
if (unsetenv(envname) != 0) if (unsetenv(envname) != 0)
return (true); return (true);
from_env = true; from_env = true;
/*
* If we're carrying over an existing value, we *do* want that
* to propagate to the kenv.
*/
eflags &= ~EV_NOKENV;
} }
snprintf(env, sizeof(env), "%d", *cattr); snprintf(env, sizeof(env), "%d", *cattr);
env_setenv(envname, EV_VOLATILE, env, sethook, unsethook); env_setenv(envname, eflags, env, sethook, unsethook);
return (from_env); return (from_env);
} }
+2
View File
@@ -172,6 +172,8 @@ md_copyenv(vm_offset_t start)
/* Traverse the environment. */ /* Traverse the environment. */
for (ep = environ; ep != NULL; ep = ep->ev_next) { for (ep = environ; ep != NULL; ep = ep->ev_next) {
if ((ep->ev_flags & EV_NOKENV) != 0)
continue;
len = strlen(ep->ev_name); len = strlen(ep->ev_name);
if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len) if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
break; break;
+14 -3
View File
@@ -65,6 +65,17 @@ env_setenv(const char *name, int flags, const void *value,
struct env_var *ev, *curr, *last; struct env_var *ev, *curr, *last;
if ((ev = env_getenv(name)) != NULL) { if ((ev = env_getenv(name)) != NULL) {
/*
* If the new value doesn't have NOKENV set, we'll drop the flag
* if it's set on the entry so that the override propagates
* correctly. We do this *before* sending it to the hook in
* case the hook declines to operate on it (e.g., because the
* value matches what was already set) -- we would still want
* the explicitly set value to propagate.
*/
if (!(flags & EV_NOKENV))
ev->ev_flags &= ~EV_NOKENV;
/* /*
* If there's a set hook, let it do the work * If there's a set hook, let it do the work
* (unless we are working for one already). * (unless we are working for one already).
@@ -77,7 +88,6 @@ env_setenv(const char *name, int flags, const void *value,
free(ev->ev_value); free(ev->ev_value);
ev->ev_value = NULL; ev->ev_value = NULL;
ev->ev_flags &= ~EV_DYNAMIC; ev->ev_flags &= ~EV_DYNAMIC;
} else { } else {
/* /*
@@ -123,12 +133,13 @@ env_setenv(const char *name, int flags, const void *value,
/* If we have a new value, use it */ /* If we have a new value, use it */
if (flags & EV_VOLATILE) { if (flags & EV_VOLATILE) {
ev->ev_value = strdup(value); ev->ev_value = strdup(value);
ev->ev_flags |= EV_DYNAMIC; flags |= EV_DYNAMIC;
} else { } else {
ev->ev_value = (char *)value; ev->ev_value = (char *)value;
ev->ev_flags |= flags & EV_DYNAMIC;
} }
ev->ev_flags |= flags & (EV_DYNAMIC | EV_NOKENV);
return (0); return (0);
} }
+1
View File
@@ -352,6 +352,7 @@ extern int pager_file(const char *fname);
#define EV_DYNAMIC (1<<0) /* value was dynamically allocated, free if changed/unset */ #define EV_DYNAMIC (1<<0) /* value was dynamically allocated, free if changed/unset */
#define EV_VOLATILE (1<<1) /* value is volatile, make a copy of it */ #define EV_VOLATILE (1<<1) /* value is volatile, make a copy of it */
#define EV_NOHOOK (1<<2) /* don't call hook when setting */ #define EV_NOHOOK (1<<2) /* don't call hook when setting */
#define EV_NOKENV (1<<3) /* don't add to kenv (loader-only) */
struct env_var; struct env_var;
typedef char *(ev_format_t)(struct env_var *ev); typedef char *(ev_format_t)(struct env_var *ev);