diff options
author | Lennart Poettering <lennart@poettering.net> | 2017-12-15 16:23:09 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2017-12-15 20:52:28 +0100 |
commit | b0d7c9899391c3a4476703fd3b0f578b9706bf1e (patch) | |
tree | d36f8d43c2f4a10a1289cef4a95953baa93d12dd /src | |
parent | 97d1fb94ba3b557abf9acbcd47acb11dbeb1239d (diff) | |
download | systemd-b0d7c9899391c3a4476703fd3b0f578b9706bf1e.tar.gz systemd-b0d7c9899391c3a4476703fd3b0f578b9706bf1e.tar.bz2 systemd-b0d7c9899391c3a4476703fd3b0f578b9706bf1e.zip |
core: split out various startup safety checks from main() into its own function
No functional changes, just some refactoring to make main() more
digestable.
Diffstat (limited to 'src')
-rw-r--r-- | src/core/main.c | 63 |
1 files changed, 37 insertions, 26 deletions
diff --git a/src/core/main.c b/src/core/main.c index 6196a6ce6c..29220a7231 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -2091,6 +2091,38 @@ static int load_configuration(int argc, char **argv, const char **ret_error_mess return 0; } +static int safety_checks(void) { + + if (arg_action == ACTION_TEST && + geteuid() == 0) { + log_error("Don't run test mode as root."); + return -EPERM; + } + + if (!arg_system && + arg_action == ACTION_RUN && + sd_booted() <= 0) { + log_error("Trying to run as user instance, but the system has not been booted with systemd."); + return -EOPNOTSUPP; + } + + if (!arg_system && + arg_action == ACTION_RUN && + !getenv("XDG_RUNTIME_DIR")) { + log_error("Trying to run as user instance, but $XDG_RUNTIME_DIR is not set."); + return -EUNATCH; + } + + if (arg_system && + arg_action == ACTION_RUN && + running_in_chroot() > 0) { + log_error("Cannot be run in a chroot() environment."); + return -EOPNOTSUPP; + } + + return 0; +} + int main(int argc, char *argv[]) { Manager *m = NULL; int r, retval = EXIT_FAILURE; @@ -2269,30 +2301,15 @@ int main(int argc, char *argv[]) { if (r < 0) goto finish; - if (arg_action == ACTION_TEST && - geteuid() == 0) { - log_error("Don't run test mode as root."); - goto finish; - } - - if (!arg_system && - arg_action == ACTION_RUN && - sd_booted() <= 0) { - log_error("Trying to run as user instance, but the system has not been booted with systemd."); - goto finish; - } - - if (arg_system && - arg_action == ACTION_RUN && - running_in_chroot() > 0) { - log_error("Cannot be run in a chroot() environment."); + r = safety_checks(); + if (r < 0) goto finish; - } - if (IN_SET(arg_action, ACTION_TEST, ACTION_HELP)) { + if (IN_SET(arg_action, ACTION_TEST, ACTION_HELP)) pager_open(arg_no_pager, false); + + if (arg_action != ACTION_RUN) skip_setup = true; - } if (arg_action == ACTION_HELP) { retval = help(); @@ -2307,12 +2324,6 @@ int main(int argc, char *argv[]) { goto finish; } - if (!arg_system && - !getenv("XDG_RUNTIME_DIR")) { - log_error("Trying to run as user instance, but $XDG_RUNTIME_DIR is not set."); - goto finish; - } - assert_se(IN_SET(arg_action, ACTION_RUN, ACTION_TEST)); /* Close logging fds, in order not to confuse fdset below */ |