Add $SYSTEMD_INVOKED_AS (#42286)
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user