core/execute: always set $USER and introduce SetLoginEnvironment=

Before this commit, $USER, $HOME, $LOGNAME and $SHELL are only
set when User= is set for the unit. For system service, this
results in different behaviors depending on whether User=root is set.

$USER always makes sense on its own, so let's set it unconditionally.
Ideally $HOME should be set too, but it causes trouble when e.g. getty
passes '-p' to login(1), which then doesn't override $HOME. $LOGNAME and
$SHELL are more like "login environments", and are generally not
suitable for system services. Therefore, a new option SetLoginEnvironment=
is also added to control the latter three variables.

Fixes #23438

Replaces #8227
This commit is contained in:
Mike Yuan
2023-10-10 00:00:26 +08:00
parent 1c9433559a
commit 854eca4a95
7 changed files with 88 additions and 24 deletions
+17 -4
View File
@@ -696,6 +696,19 @@
commands prefixed with <literal>+</literal>.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>SetLoginEnvironment=</varname></term>
<listitem><para>Takes a boolean parameter. If true, <varname>$HOME</varname>, <varname>$LOGNAME</varname>,
and <varname>$SHELL</varname> environment variables will be set for system services even if
<varname>User=</varname> is not set, i.e. when the default user <literal>root</literal> is used.
If false, the mentioned variables are not set by systemd, no matter whether <varname>User=</varname>
is set or not. This option normally has no effect on user services, since these variables are typically
inherited from user manager's own environment anyway.</para>
<xi:include href="version-info.xml" xpointer="v255"/></listitem>
</varlistentry>
<varlistentry>
<term><varname>PAMName=</varname></term>
@@ -3661,10 +3674,10 @@ StandardInputData=V2XigLJyZSBubyBzdHJhbmdlcnMgdG8gbG92ZQpZb3Uga25vdyB0aGUgcnVsZX
<term><varname>$HOME</varname></term>
<term><varname>$SHELL</varname></term>
<listitem><para>User name (twice), home directory, and the
login shell. The variables are set for the units that have
<varname>User=</varname> set, which includes user
<command>systemd</command> instances. See
<listitem><para>User name (twice), home directory, and the login shell. <varname>$USER</varname> is
set unconditionally, while <varname>$HOME</varname>, <varname>$LOGNAME</varname>, and <varname>$SHELL</varname>
are only set for the units that have <varname>User=</varname> set and <varname>SetLoginEnvironment=</varname>
unset or set to true. For user services, these variables are typically inherited from the user manager itself. See
<citerefentry project='die-net'><refentrytitle>passwd</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
</para>