Commit Graph

109 Commits

Author SHA1 Message Date
Mike Yuan
a2c8652a2a tree-wide: use strv_extend_strv_consume() where appropriate 2024-09-21 00:53:50 +02:00
Lennart Poettering
cc915eefaf env-util: suppress unnecessary setenv() in setenvf() 2024-07-19 11:41:42 +02:00
Zbigniew Jędrzejewski-Szmek
ff3f29537c various: move ptr indicator to return value 2024-06-19 16:37:12 +02:00
Yu Watanabe
e367b42664 tree-wide: replace strv_sort() + strv_uniq() -> strv_sort_uniq() 2024-06-18 00:37:50 +09:00
Mike Yuan
8b8edb512a env-util: remove unneeded DISABLE_WARNING_FORMAT_NONLITERAL
_printf_ is used, so this shouldn't emit a warning
in the first place.
2024-03-12 15:41:51 +08:00
Lennart Poettering
dbe253aa9d env-util: also rename getenv_uint64_secure() → secure_getenv_uint64()
As in the previous commit, let's not change the order of the words
compared to the underlying glibc API.
2024-02-28 15:38:00 +01:00
Lennart Poettering
efb9b3bab2 env-util: rename getenv_bool_secure() → secure_getenv_bool()
The glibc API is behind the wrapper is called "secure_getenv()", hence
our wrapper really should keep the order too, otherwise things are just
too confusing.
2024-02-28 15:38:00 +01:00
Mike Yuan
9128fd553c env-util: minor modernization 2024-02-15 19:23:52 +08:00
Adrian Vovk
83b4576195 env-util: Add helper to store current log level
This is useful after a fork but before an exec into a binary that uses
systemd's logging utilities. For example, this should be used in dbus
services that fork off worker processes: currently, the log level set by
the LogControl dbus API will be lost because of the exec, and the worker
process will not have the correct log level set.
2024-02-14 09:21:45 +01:00
Mike Yuan
70e80269aa env-util: drop _pure_ for strv_env_get_n
This function calls getenv() internally, making it
non-pure, as envvars can change between two calls
even if passed arguments are the same.
2024-01-30 03:30:02 +08:00
Mike Yuan
17ca151733 env-util: don't use strlen_ptr if known non-NULL 2024-01-30 03:29:53 +08:00
Rose
aa9ff6c28d tree-wide: replace string functions with fundamental functions 2024-01-11 13:36:25 +09:00
Lennart Poettering
b20e9dc515 env-util: add new setenvf() helper
And convert some pieces of code over.
2024-01-08 23:22:58 +01:00
Yu Watanabe
b3a9d980f3 tree-wide: drop space between variable and an increment/decrement 2023-12-25 01:56:40 +09:00
Lennart Poettering
20f8b345db env-util: add strv_env_assignf() helper 2023-12-21 19:07:29 +01:00
Luca Boccassi
58cb36e56b env-util: add helper to replace env block 2023-10-12 13:37:22 +01:00
Lennart Poettering
f331434d13 env-file: when resolving env vars in command lines, collect list of unset/invalid ones
When resolving environment variables we currently silently resolve unset
and invalid environment variables to empty strings. Let's do this
slightly less silently: log about unset and invalid env vars, but still
resolve them to an empty string.

Fixes: #27036
2023-06-27 13:49:46 +02:00
Lennart Poettering
7658139c01 env-util: make strv_env_get() a static inline wrapper around strv_env_get_n()
Let's make length = SIZE_MAX also mean "call strlen()".
2023-06-27 13:49:46 +02:00
Lennart Poettering
d5ce24c99e env-util: introduce proper flags type ReplaceEnvFlags for replace_env() 2023-06-27 13:49:46 +02:00
Lennart Poettering
10930fbb1c env-util: add explicit size check before strndupa() in strv_env_get_n()
Let's better be safe than sorry.
2023-06-27 13:49:46 +02:00
Lennart Poettering
2b07147e9c env-util: modernize env_name_is_valid_n() a bit
If the size is specified as SIZE_MAX, then imply strlen().
2023-06-27 13:49:46 +02:00
David Tardon
93eceb59e6 env-util: use more _cleanup_ in replace_env_argv() 2023-04-27 21:42:38 +02:00
David Tardon
cc09d8a5a1 env-util: use _cleanup_ in replace_env_argv() 2023-04-27 21:42:38 +02:00
David Tardon
5013d6def3 env-util: rename variable 2023-04-27 21:42:38 +02:00
David Tardon
480a7919bc env-util: use _cleanup_ in strv_env_delete() 2023-04-27 21:42:38 +02:00
Yu Watanabe
409759634e env-util: introduce strv_env_assign_many() 2023-03-28 02:51:54 +09:00
Johannes Schauer Marin Rodrigues
3fa8a1148a sysusers: make sp_lstchg shadow field reproducible
If the environment variable SOURCE_DATE_EPOCH is set, use its value
instead of the current time.
2022-09-02 07:17:25 +02:00
Lennart Poettering
ff25d3385d tree-wide: add global ascii_isdigit() + ascii_isalpha()
We now have a local implementation in string-util-fundamental.c, but
it's useful at a lot of other places, hence let's give it a more
expressive name and share it across the tree.

Follow-up for: 8d9156660d
2022-07-05 14:25:07 +02:00
Yu Watanabe
de010b0b2e strv: make iterator in STRV_FOREACH() declaread in the loop
This also avoids multiple evaluations in STRV_FOREACH_BACKWARDS()
2022-03-19 08:33:33 +09:00
Lennart Poettering
e99ca14741 env-util: replace unsetenv_erase() by new getenv_steal_erase() helper
The new helper combines a bunch of steps every invocation of
unsetenv_erase() did so far: getenv() + strdup() + unsetenv_erase().
Let's unify this into one helper that is harder to use incorrectly. It's
in inspired by TAKE_PTR() in a way: get the env var out and invalidate
where it was before.
2022-02-20 12:38:06 +09:00
Lennart Poettering
7c248223eb tree-wide: use new RET_NERRNO() helper at various places 2021-11-16 08:04:09 +01:00
Lennart Poettering
2f82562bad alloc-util: add strdupa_safe() + strndupa_safe() and use it everywhere
Let's define two helpers strdupa_safe() + strndupa_safe() which do the
same as their non-safe counterparts, except that they abort if called
with allocations larger than ALLOCA_MAX.

This should ensure that all our alloca() based allocations are subject
to this limit.

afaics glibc offers three alloca() based APIs: alloca() itself,
strndupa() + strdupa(). With this we have now replacements for all of
them, that take the limit into account.
2021-10-14 15:57:52 +02:00
Andrew Soutar
5ef97a7122 basic/env-util: correctly parse extended vars after non-extended vars (#20941) 2021-10-06 11:55:27 +09:00
Lennart Poettering
7a6abbe937 env-util: add unsetenv_erase() helper
Let's unify how we remove secrets from the env block.
2021-08-17 13:17:44 +02:00
Zbigniew Jędrzejewski-Szmek
a14af47e64 basic/env-util: add a mode where we pull in the variable value from environment 2021-08-11 09:16:05 +02:00
Zbigniew Jędrzejewski-Szmek
4ab3d29ff0 Add implicit sentinel to strv_env_merge()
Just to make it a tiny bit nicer to use.
2021-08-11 09:11:42 +02:00
Yu Watanabe
2b070200f1 env-util: use strextend() 2021-05-20 18:18:56 +09:00
Zbigniew Jędrzejewski-Szmek
1dbd0bdb3a basic/env-util: silence two gcc warnings 2021-03-31 18:24:54 +02:00
Lennart Poettering
33d31c0e60 env-util: fix parameter handling of parse_env_extension_hierarchies() + getenv_path_list()
Our coding style dictates we should not clobber return parameters on
failure, and always initialize them on success. Do so here.

This changes getenv_path_list() to return ENXIO if the env var is not
set, which is similar to how we handle this in getenv_bool().

This drops debug logging from parse_env_extension_hierarchies(), since
it's done anyway in getenv_path_list()

Follow-up for: #18018
2021-02-25 13:24:53 +01:00
Zbigniew Jędrzejewski-Szmek
fa256f43e7 basic/env-util: add putenv_dup() 2021-02-22 20:10:55 +01:00
Luca Boccassi
93547f2812 env-util: refactor parsing helper for SYSTEMD_SYSEXT_HIERARCHIES out of sysext 2021-02-17 21:45:31 +00:00
Luca Boccassi
42e6a77bc5 env-util: add strv_env_pairs_get helper 2021-02-17 21:24:23 +00:00
Zbigniew Jędrzejewski-Szmek
13734c75b5 Refactor strv_env_replace() into strv_env_replace_consume()
All callers of strv_env_replace() would free the argument on error.
So let's follow the same pattern as with strv_consume (and similar
naming) and unconditionally "use up" the argument.
2021-02-16 16:10:14 +01:00
Zbigniew Jędrzejewski-Szmek
99bfce1080 basic/env-util: drop now-unused strv_env_set() 2021-02-16 16:10:14 +01:00
Zbigniew Jędrzejewski-Szmek
f08231fe07 basic/env-util: add strv_env_assign() helper 2021-02-16 16:10:14 +01:00
Zbigniew Jędrzejewski-Szmek
aaf057c4bb basic/env-util: add variant of strv_env_replace() that does strdup internally 2021-02-15 20:43:41 +01:00
Yu Watanabe
cfd1c6e262 env-util: introduce env_update_systemd_exec_pid()
Will be used in later commits.
2021-02-01 01:13:44 +09:00
Zbigniew Jędrzejewski-Szmek
30927a2484 Allow control characters in environment variable values
So far, we would allow certain control characters (NL since
b4346b9a77, TAB since 6294aa76d8), but not others. Having
other control characters in environment variable *value* is expected and widely
used, for various prompts like $LESS, $LESS_TERMCAP_*, and other similar
variables. The typical environment exported by bash already contains a dozen or
so such variables, so programs need to handle them.

We handle then correctly too, for example in 'systemctl show-environment',
since 804ee07c13. But we would still disallow setting such variables
by the user, in unit file Environment= and in set-environment/import-environment
operations. This is unexpected and confusing and doesn't help with anything
because such variables are present in the environment through other means.

When printing such variables, 'show-environment' escapes all special
characters, so variables with control characters are plainly visible.
In other uses, e.g. 'cat -v' can be used in similar fashion. This would already
need to be done to suppress color codes starting with \[.

Note that we still forbid invalid utf-8 with this patch. (Control characters
are valid, since they are valid 7-bit ascii.) I'm not sure if we should do
that, but since people haven't been actually asking for invalid utf-8, and only
for control characters, and invalid utf-8 causes other issues, I think it's OK
to leave this unchanged.

Fixes #4446, https://gitlab.gnome.org/GNOME/gnome-session/-/issues/45.
2021-01-19 14:18:34 +01:00
Zbigniew Jędrzejewski-Szmek
5b935a388c basic/env-util: modernization 2021-01-15 10:33:29 +01:00
Zbigniew Jędrzejewski-Szmek
063f9f0da9 basic/env-util: add little helper to call setenv or unsetenv 2020-11-10 15:48:14 +01:00