Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/service: imply Type=exec if credentials are used #32612

Merged
merged 2 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions man/systemd.service.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@

<itemizedlist>
<listitem><para>If set to <option>simple</option> (the default if <varname>ExecStart=</varname>
is specified but neither <varname>Type=</varname> nor <varname>BusName=</varname> are), the
service manager will consider the unit started immediately after the main service process has
been forked off (i.e. immediately after <function>fork()</function>, and before various process
attributes have been configured and in particular before the new process has called
<function>execve()</function> to invoke the actual service binary). Typically,
is specified but neither <varname>Type=</varname> nor <varname>BusName=</varname> are, and
credentials are not used), the service manager will consider the unit started immediately after
the main service process has been forked off (i.e. immediately after <function>fork()</function>,
and before various process attributes have been configured and in particular before the new process
has called <function>execve()</function> to invoke the actual service binary). Typically,
<varname>Type=</varname><option>exec</option> is the better choice, see below.</para>

<para>It is expected that the process configured with <varname>ExecStart=</varname> is the main
Expand All @@ -188,7 +188,9 @@
Note that this means <command>systemctl start</command> command lines for <option>exec</option>
services will report failure when the service's binary cannot be invoked successfully (for
example because the selected <varname>User=</varname> doesn't exist, or the service binary is
missing).</para></listitem>
missing). This type is implied if credentials are used (refer to <varname>LoadCredential=</varname>
in <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
for details).</para></listitem>

<listitem><para>If set to <option>forking</option>, the manager will consider the unit started
immediately after the binary that forked off by the manager exits. <emphasis>The use of this type
Expand Down
6 changes: 6 additions & 0 deletions src/core/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "devnum-util.h"
#include "env-util.h"
#include "escape.h"
#include "exec-credential.h"
#include "exit-status.h"
#include "fd-util.h"
#include "fileio.h"
Expand Down Expand Up @@ -697,6 +698,9 @@ static int service_verify(Service *s) {
if (s->runtime_max_usec == USEC_INFINITY && s->runtime_rand_extra_usec != 0)
log_unit_warning(UNIT(s), "Service has RuntimeRandomizedExtraSec= setting, but no RuntimeMaxSec=. Ignoring.");

if (s->type == SERVICE_SIMPLE && s->exec_command[SERVICE_EXEC_START_POST] && exec_context_has_credentials(&s->exec_context))
log_unit_warning(UNIT(s), "Service uses a combination of Type=simple, ExecStartPost=, and credentials. This could lead to race conditions. Continuing.");

if (s->exit_type == SERVICE_EXIT_CGROUP && cg_unified() < CGROUP_UNIFIED_SYSTEMD)
log_unit_warning(UNIT(s), "Service has ExitType=cgroup set, but we are running with legacy cgroups v1, which might not work correctly. Continuing.");

Expand Down Expand Up @@ -825,6 +829,8 @@ static int service_add_extras(Service *s) {
/* Figure out a type automatically */
if (s->bus_name)
s->type = SERVICE_DBUS;
else if (exec_context_has_credentials(&s->exec_context))
s->type = SERVICE_EXEC;
else if (s->exec_command[SERVICE_EXEC_START])
s->type = SERVICE_SIMPLE;
else
Expand Down
4 changes: 2 additions & 2 deletions test/test-execute/exec-set-credential.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Description=Test for SetCredential=
[Service]
ExecStart=sh -x -c 'test "$$(cat %d/test-execute.set-credential)" = "hoge"'
ExecStartPost=sh -x -c 'test "$$(cat %d/test-execute.set-credential)" = "hoge"'
ExecStop=sh -x -c 'test "$$(cat %d/test-execute.set-credential)" = "hoge"'
ExecStopPost=sh -x -c 'test "$$(cat %d/test-execute.set-credential)" = "hoge"'
ExecStop=bash -x -c '[[ ! -v CREDENTIALS_DIRECTORY ]]'
ExecStopPost=bash -x -c '[[ ! -v CREDENTIALS_DIRECTORY ]]'
Type=oneshot
SetCredential=test-execute.set-credential:hoge