Add $SYSTEMD_INVOKED_AS (#42286)

This commit is contained in:
Yu Watanabe
2026-05-26 02:25:08 +09:00
committed by GitHub
5 changed files with 50 additions and 27 deletions

View File

@@ -35,11 +35,15 @@ All tools:
no-ops. If that's what's explicitly desired, you might consider setting
`$SYSTEMD_OFFLINE=1`.
* `$SYSTEMD_IN_INITRD` — takes a boolean. If set, overrides initrd detection.
This is useful for debugging and testing initrd-only programs in the main
system.
* `$SYSTEMD_FIRST_BOOT=0|1` — if set, assume "first boot" condition to be false
or true, instead of checking the flag file created by PID 1.
* `$SD_EVENT_PROFILE_DELAYS=1` — if set, the sd-event event loop implementation
will print latency information at runtime.
* `$SYSTEMD_INVOKED_AS=name` — override argv[0] for detection of a multicall
binary. E.g. `SYSTEMD_INVOKED_AS=systemd-udevd build/udevadm`.
* `$SYSTEMD_PROC_CMDLINE` — if set, the contents are used as the kernel command
line instead of the actual one in `/proc/cmdline`. This is useful for
@@ -76,9 +80,8 @@ All tools:
(relevant in particular for the system manager and `systemd-hostnamed`).
Must be a valid hostname (either a single label or a FQDN).
* `$SYSTEMD_IN_INITRD` — takes a boolean. If set, overrides initrd detection.
This is useful for debugging and testing initrd-only programs in the main
system.
* `$SD_EVENT_PROFILE_DELAYS=1` — if set, the sd-event event loop implementation
will print latency information at runtime.
* `$SYSTEMD_BUS_TIMEOUT=SECS` — specifies the maximum time to wait for method call
completion. If no time unit is specified, assumes seconds. The usual other units

View File

@@ -29,13 +29,15 @@ void save_argc_argv(int argc, char **argv) {
}
bool invoked_as(char *argv[], const char *token) {
if (!argv || isempty(argv[0]))
const char *progname = secure_getenv("SYSTEMD_INVOKED_AS");
if (!progname && argv)
progname = argv[0];
if (isempty(progname) || isempty(token))
return false;
if (isempty(token))
return false;
return strstr(last_path_component(argv[0]), token);
return strstr(last_path_component(progname), token);
}
bool invoked_by_systemd(void) {

View File

@@ -1,8 +1,11 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <stdlib.h>
#include "alloc-util.h"
#include "ansi-color.h"
#include "help-util.h"
#include "path-util.h"
#include "pretty-print.h"
/* These are helpers for putting together --help texts in a uniform way with a common output style. Each
@@ -25,12 +28,16 @@
* options.[ch] APIs. */
void help_cmdline(const char *arguments) {
const char *progname =
last_path_component(secure_getenv("SYSTEMD_INVOKED_AS"))
?: program_invocation_short_name;
assert(arguments);
printf("%s>%s %s %s\n",
ansi_grey(),
ansi_normal(),
program_invocation_short_name,
progname,
arguments);
}

View File

@@ -1125,6 +1125,21 @@ TEST(getenv_for_pid) {
}
}
TEST(invoked_as) {
ASSERT_FALSE(invoked_as(NULL, "foobar"));
ASSERT_FALSE(invoked_as(NULL, "barbar"));
ASSERT_EQ(setenv("SYSTEMD_INVOKED_AS", "/usr/bin/foobar", 1), 0);
ASSERT_TRUE(invoked_as(NULL, "foobar"));
ASSERT_FALSE(invoked_as(NULL, "barbar"));
ASSERT_TRUE(invoked_as(NULL, "foo"));
ASSERT_TRUE(invoked_as(STRV_MAKE("barbar", "barbar", "y"), "foobar"));
ASSERT_FALSE(invoked_as(STRV_MAKE("barbar", "barbar", "y"), "barbar"));
ASSERT_EQ(unsetenv("SYSTEMD_INVOKED_AS"), 0);
}
static int intro(void) {
log_show_color(true);
return EXIT_SUCCESS;

View File

@@ -248,19 +248,14 @@ manual_testcase_01_resolvectl() {
resolvectl dns hoge.foo 10.0.1.3 10.0.1.4
assert_in '10.0.1.1 10.0.1.2' "$(resolvectl dns hoge)"
assert_in '10.0.1.3 10.0.1.4' "$(resolvectl dns hoge.foo)"
if ! RESOLVCONF=$(command -v resolvconf 2>/dev/null); then
TMPDIR=$(mktemp -d -p /tmp resolvconf-tests.XXXXXX)
RESOLVCONF="$TMPDIR"/resolvconf
ln -s "$(command -v resolvectl 2>/dev/null)" "$RESOLVCONF"
fi
# DNS servers
echo nameserver 10.0.2.1 10.0.2.2 | "$RESOLVCONF" -a hoge
echo nameserver 10.0.2.3 10.0.2.4 | "$RESOLVCONF" -a hoge.foo
echo nameserver 10.0.2.1 10.0.2.2 | SYSTEMD_INVOKED_AS=resolvconf resolvectl -a hoge
echo nameserver 10.0.2.3 10.0.2.4 | SYSTEMD_INVOKED_AS=resolvconf resolvectl -a hoge.foo
assert_in '10.0.2.1 10.0.2.2' "$(resolvectl dns hoge)"
assert_in '10.0.2.3 10.0.2.4' "$(resolvectl dns hoge.foo)"
echo nameserver 10.0.3.1 10.0.3.2 | "$RESOLVCONF" -a hoge.inet.ipsec.192.168.35
echo nameserver 10.0.3.3 10.0.3.4 | "$RESOLVCONF" -a hoge.foo.dhcp
echo nameserver 10.0.3.1 10.0.3.2 | SYSTEMD_INVOKED_AS=resolvconf resolvectl -a hoge.inet.ipsec.192.168.35
echo nameserver 10.0.3.3 10.0.3.4 | SYSTEMD_INVOKED_AS=resolvconf resolvectl -a hoge.foo.dhcp
assert_in '10.0.3.1 10.0.3.2' "$(resolvectl dns hoge)"
assert_in '10.0.3.3 10.0.3.4' "$(resolvectl dns hoge.foo)"
@@ -268,36 +263,37 @@ manual_testcase_01_resolvectl() {
# without domain/search clears existing domain
resolvectl domain hoge test-domain.example.com
assert_in 'test-domain.example.com' "$(resolvectl domain hoge)"
echo nameserver 10.0.2.1 10.0.2.2 | "$RESOLVCONF" -a hoge
echo nameserver 10.0.2.1 10.0.2.2 | SYSTEMD_INVOKED_AS=resolvconf resolvectl -a hoge
assert_not_in 'test-domain.example.com' "$(resolvectl domain hoge)"
# cannot set domain without DNS servers
(! echo domain test-domain.example.com | "$RESOLVCONF" -a hoge)
(! echo domain test-domain.example.com | SYSTEMD_INVOKED_AS=resolvconf resolvectl -a hoge)
# can set domain with DNS server(s)
echo -e "nameserver 10.0.2.1 10.0.2.2\ndomain test-domain1.example.com test-domain2.example.com\nsearch test-search-domain.example.com" | "$RESOLVCONF" -a hoge
echo -e "nameserver 10.0.2.1 10.0.2.2\ndomain test-domain1.example.com test-domain2.example.com\nsearch test-search-domain.example.com" | \
SYSTEMD_INVOKED_AS=resolvconf resolvectl -a hoge
assert_in 'test-domain1.example.com' "$(resolvectl domain hoge)"
assert_in 'test-domain2.example.com' "$(resolvectl domain hoge)"
assert_in 'test-search-domain.example.com' "$(resolvectl domain hoge)"
# Tests for 'resolvconf -x'
echo nameserver 10.0.2.1 | "$RESOLVCONF" -x -a hoge
echo nameserver 10.0.2.1 | SYSTEMD_INVOKED_AS=resolvconf resolvectl -x -a hoge
assert_in '~.' "$(resolvectl domain hoge)"
resolvectl domain hoge "hoge.example.com"
assert_in 'hoge.example.com' "$(resolvectl domain hoge)"
assert_not_in '~.' "$(resolvectl domain hoge)"
echo -e "nameserver 10.0.2.1\ndomain test-domain.example.com" | "$RESOLVCONF" -x -a hoge
echo -e "nameserver 10.0.2.1\ndomain test-domain.example.com" | SYSTEMD_INVOKED_AS=resolvconf resolvectl -x -a hoge
assert_in 'test-domain.example.com' "$(resolvectl domain hoge)"
assert_in '~.' "$(resolvectl domain hoge)"
# Tests for 'resolvconf -p'
resolvectl default-route hoge yes
assert_in 'yes' "$(resolvectl default-route hoge)"
echo nameserver 10.0.3.3 10.0.3.4 | "$RESOLVCONF" -p -a hoge
echo nameserver 10.0.3.3 10.0.3.4 | SYSTEMD_INVOKED_AS=resolvconf resolvectl -p -a hoge
assert_in 'no' "$(resolvectl default-route hoge)"
# Tests for 'resolvconf -d'
resolvectl dns hoge 10.0.3.1 10.0.3.2
resolvectl domain hoge test-domain.example.com
"$RESOLVCONF" -d hoge
SYSTEMD_INVOKED_AS=resolvconf resolvectl -d hoge
assert_not_in '10.0.3.1' "$(resolvectl dns hoge)"
assert_not_in '10.0.3.2' "$(resolvectl dns hoge)"
assert_not_in 'test-domain.example.com' "$(resolvectl domain hoge)"