Add $SYSTEMD_INVOKED_AS

This allows multi-call binaries to be easily invoked with a different
name. After installation, the name is set by creating a symlink. But in
build directories, we don't create the symlinks. (There are also other
ways to achieve the same thing, e.g. zsh supports $ARGV0, and exec -a
can be used, but those are either non-portable or are more complicated
to use.)  The primary use-case for me is to test --help output for
multicall binaries.

Also reorder the help for env vars to group the more generic ones near
the top.

This was initially proposed in https://github.com/systemd/systemd/pull/24054,
but there were some comments about the implementation. I had a branch
with the patch, but I don't think I ever actually submitted it as a
pull request.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-05-25 12:37:36 +02:00
parent 4d18eac5a9
commit 58e7959988
4 changed files with 38 additions and 11 deletions
+7 -5
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) {