[PATCH 01/10] globalvar: add scopes to snapshot and restore globalvars

Ahmad Fatoum a.fatoum at pengutronix.de
Thu Nov 12 00:41:14 PST 2026


On 10/30/26 11:53, Sascha Hauer wrote:
> Code that runs foreign code, like boot scripts, often wants to revert
> whatever that code changed in the global namespace afterwards. So far
> this is done by hand for each use case: either by saving and restoring a
> fixed list of variables, or by putting variables into a dedicated
> namespace which is cleared afterwards.
> 
> Add globalvar_scope_push() and globalvar_scope_pop() to do this
> generically. A scope records the current values of all globalvars whose
> name begins with one of the given prefixes. Popping it restores changed
> values, removes variables created in the meantime and creates removed
> variables again. Variables are only removed or recreated if they were
> created with globalvar_add_simple(), which is what the shell's global
> command and most dynamically created variables use. For all other
> globalvars, which are backed by a C variable, only the value is
> restored, through the regular setter so the C variable follows.
> 
> Scopes can be nested as long as they are popped in reverse order.

As mentioned before, I have misgivings about _global_ variables being scoped,
but we have effectively that already for some variables...

Some bike shedding: I think I like the term of "banked" global variables
more, but that's just a suggestion.

> 
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>

Reviewed-by: Ahmad Fatoum <a.fatoum at pengutronix.de>

> +static bool globalvar_scope_match(const struct globalvar_scope *scope,
> +				  const char *name)
> +{
> +	const char * const *prefix;
> +
> +	for (prefix = scope->prefixes; *prefix; prefix++)
> +		if (str_has_prefix(name, *prefix))
> +			return true;
I see that the prefixes have a trailing . to avoid partial matches, but
I think that's a bit of a surprising API compared to do boundary matching
(or matching to the end of string _or_ dot). Just a nit.


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list