1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-19 22:50:17 +03:00

Merge pull request #18375 from yuwata/cli-tools-also-read-kernel-command-line

tree-wide: make CLI tools also read kernel command line when run as service
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2021-02-08 11:45:42 +01:00 committed by GitHub
commit ad22e7cf5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
84 changed files with 210 additions and 106 deletions

View File

@ -3043,6 +3043,17 @@ StandardInputData=SWNrIHNpdHplIGRhIHVuJyBlc3NlIEtsb3BzLAp1ZmYgZWVtYWwga2xvcHAncy
</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>$SYSTEMD_EXEC_PID</varname></term>
<listitem><para>The PID of the unit process (e.g. process invoked by
<varname>ExecStart=</varname>). The child process can use this information to determine
whether the process is directly invoked by the service manager or indirectly as a child of
another process by comparing this value with the current PID (as similar to the scheme used in
<citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>
with <varname>$LISTEN_PID</varname> and <varname>$LISTEN_FDS</varname>).</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>$TERM</varname></term>

View File

@ -2446,7 +2446,7 @@ static int run(int argc, char *argv[]) {
setlocale(LC_ALL, "");
setlocale(LC_NUMERIC, "C"); /* we want to format/parse floats in C style */
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -380,7 +380,7 @@ static int run(int argc, char *argv[]) {
unsigned max_brightness, brightness;
int r;
log_setup_service();
log_setup();
if (strv_contains(strv_skip(argv, 1), "--help"))
return help();

View File

@ -12,6 +12,8 @@
#include "extract-word.h"
#include "macro.h"
#include "parse-util.h"
#include "process-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "utf8.h"
@ -749,3 +751,24 @@ int set_unset_env(const char *name, const char *value, bool overwrite) {
return -errno;
return 0;
}
int setenv_systemd_exec_pid(bool update_only) {
char str[DECIMAL_STR_MAX(pid_t)];
const char *e;
/* Update $SYSTEMD_EXEC_PID=pid except when '*' is set for the variable. */
e = secure_getenv("SYSTEMD_EXEC_PID");
if (!e && update_only)
return 0;
if (streq_ptr(e, "*"))
return 0;
xsprintf(str, PID_FMT, getpid_cached());
if (setenv("SYSTEMD_EXEC_PID", str, 1) < 0)
return -errno;
return 1;
}

View File

@ -55,3 +55,5 @@ int getenv_bool_secure(const char *p);
/* Like setenv, but calls unsetenv if value == NULL. */
int set_unset_env(const char *name, const char *value, bool overwrite);
int setenv_systemd_exec_pid(bool update_only);

View File

@ -1157,20 +1157,38 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
return 0;
}
void log_parse_environment(void) {
if (getpid_cached() == 1 || get_ctty_devnr(0, NULL) < 0)
/* Only try to read the command line in daemons. We assume that anything that has a
* controlling tty is user stuff. For PID1 we do a special check in case it hasn't
* closed the console yet. */
(void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
static bool should_parse_proc_cmdline(void) {
const char *e;
pid_t p;
log_parse_environment_cli();
/* PID1 always reads the kernel command line. */
if (getpid_cached() == 1)
return true;
/* If the process is directly executed by PID1 (e.g. ExecStart= or generator), systemd-importd,
* or systemd-homed, then $SYSTEMD_EXEC_PID= is set, and read the command line. */
e = getenv("SYSTEMD_EXEC_PID");
if (!e)
return false;
if (streq(e, "*"))
/* For testing. */
return true;
if (parse_pid(e, &p) < 0)
/* We know that systemd sets the variable correctly. Something else must have set it. */
log_debug("Failed to parse \"$SYSTEMD_EXEC_PID=%s\". Ignoring.", e);
return getpid_cached() == p;
}
void log_parse_environment_cli(void) {
void log_parse_environment(void) {
const char *e;
/* Do not call from library code. */
const char *e;
if (should_parse_proc_cmdline())
(void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
e = getenv("SYSTEMD_LOG_TARGET");
if (e && log_set_target_from_string(e) < 0)
@ -1460,22 +1478,10 @@ int log_dup_console(void) {
return 0;
}
void log_setup_service(void) {
/* Sets up logging the way it is most appropriate for running a program as a service. Note that using this
* doesn't make the binary unsuitable for invocation on the command line, as log output will still go to the
* terminal if invoked interactively. */
void log_setup(void) {
log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
(void) log_open();
}
void log_setup_cli(void) {
/* Sets up logging the way it is most appropriate for running a program as a CLI utility. */
log_set_target(LOG_TARGET_AUTO);
log_parse_environment_cli();
(void) log_open();
if (log_on_console() && show_color < 0)
log_show_color(true);
}

View File

@ -69,7 +69,6 @@ void log_close(void);
void log_forget_fds(void);
void log_parse_environment(void);
void log_parse_environment_cli(void);
int log_dispatch_internal(
int level,
@ -301,5 +300,4 @@ int log_syntax_invalid_utf8_internal(
#define DEBUG_LOGGING _unlikely_(log_get_max_level() >= LOG_DEBUG)
void log_setup_service(void);
void log_setup_cli(void);
void log_setup(void);

View File

@ -190,7 +190,7 @@ static int run(int argc, char *argv[]) {
if (r <= 0)
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
log_setup_service();
log_setup();
umask(0022);

View File

@ -2565,7 +2565,7 @@ static int busctl_main(int argc, char *argv[]) {
static int run(int argc, char *argv[]) {
int r;
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -163,7 +163,7 @@ static void show_cg_info(const char *controller, const char *path) {
static int run(int argc, char *argv[]) {
int r, output_flags;
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -22,7 +22,7 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
log_setup_service();
log_setup();
fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
if (fd < 0) {

View File

@ -915,7 +915,7 @@ static int run(int argc, char *argv[]) {
CGroupMask mask;
int r;
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -1794,7 +1794,7 @@ static int build_environment(
assert(p);
assert(ret);
#define N_ENV_VARS 16
#define N_ENV_VARS 17
our_env = new0(char*, N_ENV_VARS + _EXEC_DIRECTORY_TYPE_MAX);
if (!our_env)
return -ENOMEM;
@ -1950,6 +1950,11 @@ static int build_environment(
our_env[n_env++] = x;
}
if (asprintf(&x, "SYSTEMD_EXEC_PID=" PID_FMT, getpid_cached()) < 0)
return -ENOMEM;
our_env[n_env++] = x;
our_env[n_env++] = NULL;
assert(n_env <= N_ENV_VARS + _EXEC_DIRECTORY_TYPE_MAX);
#undef N_ENV_VARS

View File

@ -4108,7 +4108,8 @@ static int manager_run_environment_generators(Manager *m) {
RUN_WITH_UMASK(0022)
r = execute_directories((const char* const*) paths, DEFAULT_TIMEOUT_USEC, gather_environment,
args, NULL, m->transient_environment, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
args, NULL, m->transient_environment,
EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS | EXEC_DIR_SET_SYSTEMD_EXEC_PID);
return r;
}
@ -4143,7 +4144,8 @@ static int manager_run_generators(Manager *m) {
RUN_WITH_UMASK(0022)
(void) execute_directories((const char* const*) paths, DEFAULT_TIMEOUT_USEC, NULL, NULL,
(char**) argv, m->transient_environment, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
(char**) argv, m->transient_environment,
EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS | EXEC_DIR_SET_SYSTEMD_EXEC_PID);
r = 0;

View File

@ -861,7 +861,7 @@ static int process_socket(int fd) {
assert(fd >= 0);
log_setup_service();
log_setup();
log_debug("Processing coredump received on stdin...");

View File

@ -1108,7 +1108,7 @@ static int run(int argc, char *argv[]) {
int r, units_active;
setlocale(LC_ALL, "");
log_setup_cli();
log_setup();
/* The journal merging logic potentially needs a lot of fds. */
(void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);

View File

@ -1395,7 +1395,7 @@ static int run(int argc, char *argv[]) {
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"This program requires at least two arguments.");
log_setup_service();
log_setup();
cryptsetup_enable_logging(cd);

View File

@ -643,7 +643,7 @@ static int parse_argv(int argc, char *argv[]) {
static int run(int argc, char *argv[]) {
int r, k, n_found = 0;
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -127,7 +127,7 @@ static int run(int argc, char *argv[]) {
* to detect whether we are being run in a virtualized
* environment or not */
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -158,7 +158,7 @@ static int run(int argc, char *argv[]) {
char **i;
int r;
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -1271,7 +1271,7 @@ static int run(int argc, char *argv[]) {
if (r <= 0)
return r;
log_setup_service();
log_setup();
umask(0022);

View File

@ -256,7 +256,7 @@ static int run(int argc, char *argv[]) {
int r, exit_status;
pid_t pid;
log_setup_service();
log_setup();
if (argc > 2)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),

View File

@ -20,7 +20,7 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
log_setup_service();
log_setup();
umask(0022);

View File

@ -3346,7 +3346,7 @@ static int run(int argc, char *argv[]) {
int r;
log_setup_cli();
log_setup();
r = redirect_bus_mgr();
if (r < 0)

View File

@ -1038,6 +1038,10 @@ static int home_start_work(Home *h, const char *verb, UserRecord *hr, UserRecord
_exit(EXIT_FAILURE);
}
r = setenv_systemd_exec_pid(true);
if (r < 0)
log_warning_errno(r, "Failed to update $SYSTEMD_EXEC_PID, ignoring: %m");
r = rearrange_stdio(stdin_fd, stdout_fd, STDERR_FILENO);
if (r < 0) {
log_error_errno(r, "Failed to rearrange stdin/stdout/stderr: %m");

View File

@ -17,7 +17,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(notify_on_cleanup) const char *notify_stop = NULL;
int r;
log_setup_service();
log_setup();
r = service_parse_argv("systemd-homed.service",
"A service to create, remove, change or inspect home areas.",

View File

@ -1633,7 +1633,7 @@ static int run(int argc, char *argv[]) {
start = now(CLOCK_MONOTONIC);
log_setup_service();
log_setup();
umask(0022);

View File

@ -443,7 +443,7 @@ static int run(int argc, char *argv[]) {
int r;
setlocale(LC_ALL, "");
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -1064,7 +1064,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
log_setup_service();
log_setup();
r = service_parse_argv("systemd-hostnamed.service",
"Manage the system hostname and related metadata.",

View File

@ -249,7 +249,7 @@ static int id128_main(int argc, char *argv[]) {
static int run(int argc, char *argv[]) {
int r;
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -11,6 +11,7 @@
#include "bus-log-control-api.h"
#include "bus-polkit.h"
#include "def.h"
#include "env-util.h"
#include "fd-util.h"
#include "float.h"
#include "hostname-util.h"
@ -403,6 +404,10 @@ static int transfer_start(Transfer *t) {
_exit(EXIT_FAILURE);
}
r = setenv_systemd_exec_pid(true);
if (r < 0)
log_warning_errno(r, "Failed to update $SYSTEMD_EXEC_PID, ignoring: %m");
switch (t->type) {
case TRANSFER_IMPORT_TAR:
@ -1361,7 +1366,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(manager_unrefp) Manager *m = NULL;
int r;
log_setup_service();
log_setup();
r = service_parse_argv("systemd-importd.service",
"VM and container image import and export service.",

View File

@ -317,7 +317,7 @@ static int run(int argc, char *argv[]) {
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"This program does not take arguments.");
log_setup_service();
log_setup();
umask(0022);

View File

@ -995,7 +995,7 @@ static int run(int argc, char *argv[]) {
MHD_USE_THREAD_PER_CONNECTION;
int r, n;
log_setup_service();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -1121,7 +1121,7 @@ static int run(int argc, char **argv) {
int r;
log_show_color(true);
log_parse_environment_cli();
log_parse_environment();
/* The journal merging logic potentially needs a lot of fds. */
(void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);

View File

@ -820,7 +820,7 @@ static int run(int argc, char **argv) {
int r;
log_show_color(true);
log_parse_environment_cli();
log_parse_environment();
/* The journal merging logic potentially needs a lot of fds. */
(void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);

View File

@ -129,7 +129,7 @@ static int run(int argc, char *argv[]) {
_cleanup_close_ int outfd = -1, errfd = -1, saved_stderr = -1;
int r;
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -2145,7 +2145,7 @@ int main(int argc, char *argv[]) {
int n_shown = 0, r, poll_fd = -1;
setlocale(LC_ALL, "");
log_setup_cli();
log_setup();
/* Increase max number of open files if we can, we might needs this when browsing journal files, which might be
* split up into many files. */

View File

@ -506,7 +506,7 @@ static int run(int argc, char *argv[]) {
int r;
setlocale(LC_ALL, "");
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -830,7 +830,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
log_setup_service();
log_setup();
r = service_parse_argv("systemd-localed.service",
"Manage system locale settings and key mappings.",

View File

@ -1464,7 +1464,7 @@ static int run(int argc, char *argv[]) {
int r;
setlocale(LC_ALL, "");
log_setup_cli();
log_setup();
/* The journal merging logic potentially needs a lot of fds. */
(void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);

View File

@ -1160,7 +1160,7 @@ static int run(int argc, char *argv[]) {
int r;
log_set_facility(LOG_AUTH);
log_setup_service();
log_setup();
r = service_parse_argv("systemd-logind.service",
"Manager for user logins and devices and privileged operations.",

View File

@ -2876,7 +2876,7 @@ static int run(int argc, char *argv[]) {
int r;
setlocale(LC_ALL, "");
log_setup_cli();
log_setup();
/* The journal merging logic potentially needs a lot of fds. */
(void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);

View File

@ -324,7 +324,7 @@ static int run(int argc, char *argv[]) {
int r;
log_set_facility(LOG_AUTH);
log_setup_service();
log_setup();
r = service_parse_argv("systemd-machined.service",
"Manage registrations of local VMs and containers.",

View File

@ -165,7 +165,7 @@ static int run(int argc, char *argv[]) {
if (r <= 0)
return r;
log_setup_service();
log_setup();
umask(0022);

View File

@ -2822,7 +2822,7 @@ static void warn_networkd_missing(void) {
static int run(int argc, char* argv[]) {
int r;
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -22,7 +22,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(notify_on_cleanup) const char *notify_message = NULL;
int r;
log_setup_service();
log_setup();
umask(0022);

View File

@ -184,7 +184,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(notify_on_cleanup) const char *notify_message = NULL;
int r;
log_setup_service();
log_setup();
umask(0022);

View File

@ -122,7 +122,7 @@ static int run(int argc, char *argv[]) {
unsigned long long s = 0;
int r;
log_setup_service();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -201,7 +201,7 @@ static int run(int argc, char *argv[]) {
dev_t devno;
int r;
log_setup_service();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -24,7 +24,7 @@ static int run(int argc, char *argv[]) {
struct stat st;
int r;
log_setup_service();
log_setup();
if (argc != 3)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),

View File

@ -1113,7 +1113,7 @@ static int run(int argc, char *argv[]) {
int r;
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -135,7 +135,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(manager_unrefp) Manager *m = NULL;
int r;
log_setup_service();
log_setup();
umask(0022);

View File

@ -367,7 +367,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(pstore_entries_reset) PStoreList list = {};
int r;
log_setup_service();
log_setup();
if (argc == 3) {
arg_sourcedir = argv[1];

View File

@ -58,7 +58,7 @@ static void test_files(void) {
static int run(int argc, char *argv[]) {
int r;
log_setup_service();
log_setup();
if (argc > 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),

View File

@ -110,7 +110,7 @@ static int run(int argc, char *argv[]) {
ssize_t k;
int r;
log_setup_service();
log_setup();
if (argc != 2)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),

View File

@ -77,7 +77,7 @@ static int run(int argc, char *argv[]) {
struct mntent* me;
int r;
log_setup_service();
log_setup();
if (argc > 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),

View File

@ -39,7 +39,7 @@ static int run(int argc, char *argv[]) {
size_t length = 0;
int r;
log_setup_service();
log_setup();
if (argc != 3)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Wrong number of arguments.");

View File

@ -3296,7 +3296,7 @@ static int run(int argc, char **argv) {
int r;
setlocale(LC_ALL, "");
log_setup_cli();
log_setup();
if (streq(program_invocation_short_name, "resolvconf"))
r = resolvconf_parse_argv(argc, argv);

View File

@ -26,7 +26,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(notify_on_cleanup) const char *notify_stop = NULL;
int r;
log_setup_service();
log_setup();
r = service_parse_argv("systemd-resolved.service",
"Provide name resolution with caching using DNS, mDNS, LLMNR.",

View File

@ -276,7 +276,7 @@ static int run(int argc, char *argv[]) {
if (argc > 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program requires no arguments.");
log_setup_service();
log_setup();
umask(0022);

View File

@ -34,7 +34,7 @@
/* Put this test here for a lack of better place */
assert_cc(EAGAIN == EWOULDBLOCK);
static int do_spawn(const char *path, char *argv[], int stdout_fd, pid_t *pid) {
static int do_spawn(const char *path, char *argv[], int stdout_fd, pid_t *pid, bool set_systemd_exec_pid) {
pid_t _pid;
int r;
@ -57,6 +57,12 @@ static int do_spawn(const char *path, char *argv[], int stdout_fd, pid_t *pid) {
(void) rlimit_nofile_safe();
if (set_systemd_exec_pid) {
r = setenv_systemd_exec_pid(false);
if (r < 0)
log_warning_errno(r, "Failed to set $SYSTEMD_EXEC_PID, ignoring: %m");
}
if (!argv) {
_argv[0] = (char*) path;
_argv[1] = NULL;
@ -132,7 +138,7 @@ static int do_execute(
return log_error_errno(fd, "Failed to open serialization file: %m");
}
r = do_spawn(t, argv, fd, &pid);
r = do_spawn(t, argv, fd, &pid, FLAGS_SET(flags, EXEC_DIR_SET_SYSTEMD_EXEC_PID));
if (r <= 0)
continue;

View File

@ -15,9 +15,10 @@ enum {
};
typedef enum {
EXEC_DIR_NONE = 0, /* No execdir flags */
EXEC_DIR_PARALLEL = 1 << 0, /* Execute scripts in parallel, if possible */
EXEC_DIR_IGNORE_ERRORS = 1 << 1, /* Ignore non-zero exit status of scripts */
EXEC_DIR_NONE = 0, /* No execdir flags */
EXEC_DIR_PARALLEL = 1 << 0, /* Execute scripts in parallel, if possible */
EXEC_DIR_IGNORE_ERRORS = 1 << 1, /* Ignore non-zero exit status of scripts */
EXEC_DIR_SET_SYSTEMD_EXEC_PID = 1 << 2, /* Set $SYSTEMD_EXEC_PID environment variable */
} ExecDirFlags;
typedef enum ExecCommandFlags {

View File

@ -368,7 +368,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
int r;
log_setup_service();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -100,7 +100,7 @@ int main(int argc, char *argv[]) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
log_setup_service();
log_setup();
print_mode(argc > 1 ? argv[1] : "");

View File

@ -386,7 +386,7 @@ static int run(int argc, char *argv[]) {
if (r <= 0)
return r;
log_setup_service();
log_setup();
umask(0022);

View File

@ -1027,7 +1027,7 @@ static int sysext_main(int argc, char *argv[]) {
static int run(int argc, char *argv[]) {
int r;
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -1085,7 +1085,7 @@ static int run(int argc, char *argv[]) {
int r;
setlocale(LC_ALL, "");
log_parse_environment_cli();
log_parse_environment();
log_open();
/* The journal merging logic potentially needs a lot of fds. */

View File

@ -1928,7 +1928,7 @@ static int run(int argc, char *argv[]) {
if (r <= 0)
return r;
log_setup_service();
log_setup();
if (arg_cat_config)
return cat_config();

View File

@ -84,7 +84,7 @@ static int parse_argv(int argc, char *argv[]) {
static int run(int argc, char **argv) {
int r;
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -4,9 +4,12 @@
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "parse-util.h"
#include "process-util.h"
#include "serialize.h"
#include "string-util.h"
#include "strv.h"
#include "tests.h"
#include "util.h"
static void test_strv_env_delete(void) {
@ -334,7 +337,44 @@ static void test_env_assignment_is_valid(void) {
assert_se(!env_assignment_is_valid("głąb=printf \"\x1b]0;<mock-chroot>\x07<mock-chroot>\""));
}
static void test_setenv_systemd_exec_pid(void) {
_cleanup_free_ char *saved = NULL;
const char *e;
pid_t p;
log_info("/* %s */", __func__);
e = getenv("SYSTEMD_EXEC_PID");
if (e)
assert_se(saved = strdup(e));
assert_se(unsetenv("SYSTEMD_EXEC_PID") >= 0);
assert_se(setenv_systemd_exec_pid(true) == 0);
assert_se(!getenv("SYSTEMD_EXEC_PID"));
assert_se(setenv("SYSTEMD_EXEC_PID", "*", 1) >= 0);
assert_se(setenv_systemd_exec_pid(true) == 0);
assert_se(e = getenv("SYSTEMD_EXEC_PID"));
assert_se(streq(e, "*"));
assert_se(setenv("SYSTEMD_EXEC_PID", "123abc", 1) >= 0);
assert_se(setenv_systemd_exec_pid(true) == 1);
assert_se(e = getenv("SYSTEMD_EXEC_PID"));
assert_se(parse_pid(e, &p) >= 0);
assert_se(p == getpid_cached());
assert_se(unsetenv("SYSTEMD_EXEC_PID") >= 0);
assert_se(setenv_systemd_exec_pid(false) == 1);
assert_se(e = getenv("SYSTEMD_EXEC_PID"));
assert_se(parse_pid(e, &p) >= 0);
assert_se(p == getpid_cached());
assert_se(set_unset_env("SYSTEMD_EXEC_PID", saved, 1) >= 0);
}
int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG);
test_strv_env_delete();
test_strv_env_get();
test_strv_env_unset();
@ -350,6 +390,7 @@ int main(int argc, char *argv[]) {
test_env_name_is_valid();
test_env_value_is_valid();
test_env_assignment_is_valid();
test_setenv_systemd_exec_pid();
return 0;
}

View File

@ -1051,7 +1051,7 @@ static int run(int argc, char *argv[]) {
int r;
setlocale(LC_ALL, "");
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -1109,7 +1109,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
log_setup_service();
log_setup();
r = service_parse_argv("systemd-timedated.service",
"Manage the system clock and timezone and NTP enablement.",

View File

@ -97,7 +97,7 @@ static int run(int argc, char *argv[]) {
int r;
log_set_facility(LOG_CRON);
log_setup_service();
log_setup();
umask(0022);

View File

@ -3363,7 +3363,7 @@ static int run(int argc, char *argv[]) {
if (r <= 0)
return r;
log_setup_service();
log_setup();
/* Descending down file system trees might take a lot of fds */
(void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);

View File

@ -684,7 +684,7 @@ static int ask_on_consoles(char *argv[]) {
static int run(int argc, char *argv[]) {
int r;
log_setup_service();
log_setup();
umask(0022);

View File

@ -41,7 +41,7 @@ int main(int argc, char *argv[]) {
struct stat st;
int r, q = 0;
log_setup_service();
log_setup();
if (stat("/usr", &st) < 0) {
log_error_errno(errno, "Failed to stat /usr: %m");

View File

@ -224,7 +224,7 @@ static int run(int argc, char *argv[]) {
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"This program requires one argument.");
log_setup_service();
log_setup();
umask(0022);

View File

@ -21,7 +21,7 @@ static int run(int argc, char *argv[]) {
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"This program requires one argument.");
log_setup_service();
log_setup();
umask(0022);

View File

@ -763,7 +763,7 @@ static int run(int argc, char *argv[]) {
int r;
log_setup_cli();
log_setup();
r = parse_argv(argc, argv);
if (r <= 0)

View File

@ -24,7 +24,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(notify_on_cleanup) const char *notify_stop = NULL;
int r;
log_setup_service();
log_setup();
umask(0022);

View File

@ -662,7 +662,7 @@ static int run(int argc, char *argv[]) {
unsigned n_iterations = 0;
int m, listen_fd, r;
log_setup_service();
log_setup();
m = sd_listen_fds(false);
if (m < 0)

View File

@ -430,7 +430,7 @@ int main(int argc, char **argv) {
unsigned idx = 0;
int r;
log_setup_service();
log_setup();
umask(0022);

View File

@ -136,7 +136,7 @@ static int run(int argc, char *argv[]) {
if (argc < 3)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program requires at least two arguments.");
log_setup_service();
log_setup();
umask(0022);

View File

@ -119,7 +119,7 @@ static int run(int argc, char *argv[]) {
dev_t devt;
int r;
log_setup_service();
log_setup();
if (argc > 3)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),