Skip to content

Commit 41de9cc

Browse files
committed
core: support specifier expansion in PassEnvironment=
I can't come up with any usecase for this, but let's add this here, to match what we support for Environment=. It's kind surprising if we support specifier expansion for some environment related settings, but not for others.
1 parent f7f3f5c commit 41de9cc

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/core/dbus-execute.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,13 +2000,17 @@ int bus_exec_context_set_transient_property(
20002000

20012001
} else if (streq(name, "PassEnvironment")) {
20022002

2003-
_cleanup_strv_free_ char **l = NULL;
2003+
_cleanup_strv_free_ char **l = NULL, **q = NULL;
20042004

20052005
r = sd_bus_message_read_strv(message, &l);
20062006
if (r < 0)
20072007
return r;
20082008

2009-
if (!strv_env_name_is_valid(l))
2009+
r = unit_full_printf_strv(u, l, &q);
2010+
if (r < 0)
2011+
return r;
2012+
2013+
if (!strv_env_name_is_valid(q))
20102014
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid PassEnvironment= block.");
20112015

20122016
if (mode != UNIT_CHECK) {
@@ -2016,11 +2020,12 @@ int bus_exec_context_set_transient_property(
20162020
} else {
20172021
_cleanup_free_ char *joined = NULL;
20182022

2019-
r = strv_extend_strv(&c->pass_environment, l, true);
2023+
r = strv_extend_strv(&c->pass_environment, q, true);
20202024
if (r < 0)
20212025
return r;
20222026

2023-
joined = strv_join_quoted(c->pass_environment);
2027+
/* We write just the new settings out to file, with unresolved specifiers. */
2028+
joined = strv_join_quoted(l);
20242029
if (!joined)
20252030
return -ENOMEM;
20262031

src/core/load-fragment.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,9 +2189,10 @@ int config_parse_pass_environ(
21892189
void *userdata) {
21902190

21912191
const char *whole_rvalue = rvalue;
2192-
char*** passenv = data;
21932192
_cleanup_strv_free_ char **n = NULL;
21942193
size_t nlen = 0, nbufsize = 0;
2194+
char*** passenv = data;
2195+
Unit *u = userdata;
21952196
int r;
21962197

21972198
assert(filename);
@@ -2206,7 +2207,7 @@ int config_parse_pass_environ(
22062207
}
22072208

22082209
for (;;) {
2209-
_cleanup_free_ char *word = NULL;
2210+
_cleanup_free_ char *word = NULL, *k = NULL;
22102211

22112212
r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES);
22122213
if (r == 0)
@@ -2219,17 +2220,30 @@ int config_parse_pass_environ(
22192220
break;
22202221
}
22212222

2222-
if (!env_name_is_valid(word)) {
2223-
log_syntax(unit, LOG_ERR, filename, line, EINVAL,
2224-
"Invalid environment name for %s, ignoring: %s", lvalue, word);
2223+
if (u) {
2224+
r = unit_full_printf(u, word, &k);
2225+
if (r < 0) {
2226+
log_syntax(unit, LOG_ERR, filename, line, r,
2227+
"Failed to resolve specifiers, ignoring: %s", word);
2228+
continue;
2229+
}
2230+
} else {
2231+
k = word;
2232+
word = NULL;
2233+
}
2234+
2235+
if (!env_name_is_valid(k)) {
2236+
log_syntax(unit, LOG_ERR, filename, line, 0,
2237+
"Invalid environment name for %s, ignoring: %s", lvalue, k);
22252238
continue;
22262239
}
22272240

22282241
if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
22292242
return log_oom();
2230-
n[nlen++] = word;
2243+
2244+
n[nlen++] = k;
22312245
n[nlen] = NULL;
2232-
word = NULL;
2246+
k = NULL;
22332247
}
22342248

22352249
if (n) {

0 commit comments

Comments
 (0)