From fba868fa710a216adade59c2683d8bee20c7da4b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 11 Dec 2017 23:00:57 +0100 Subject: [PATCH] tree-wide: unify logging of "Must be root" message Let's unify this in one call, generalizing must_be_root() from bootctl.c. --- src/basic/process-util.c | 9 +++++++++ src/basic/process-util.h | 2 ++ src/boot/bootctl.c | 9 --------- src/nspawn/nspawn.c | 7 +++---- src/systemctl/systemctl.c | 16 ++++++++-------- src/udev/udevadm-control.c | 4 +--- src/udev/udevd.c | 5 ++--- 7 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/basic/process-util.c b/src/basic/process-util.c index b3d96cdb0f..5f001494f0 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -1053,6 +1053,15 @@ pid_t getpid_cached(void) { } } +int must_be_root(void) { + + if (geteuid() == 0) + return 0; + + log_error("Need to be root."); + return -EPERM; +} + static const char *const ioprio_class_table[] = { [IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_RT] = "realtime", diff --git a/src/basic/process-util.h b/src/basic/process-util.h index d99fb3d6b8..39c194df0d 100644 --- a/src/basic/process-util.h +++ b/src/basic/process-util.h @@ -138,3 +138,5 @@ static inline bool pid_is_valid(pid_t p) { int ioprio_parse_priority(const char *s, int *ret); pid_t getpid_cached(void); + +int must_be_root(void); diff --git a/src/boot/bootctl.c b/src/boot/bootctl.c index a5700992d4..b685b3c791 100644 --- a/src/boot/bootctl.c +++ b/src/boot/bootctl.c @@ -925,15 +925,6 @@ static void read_loader_efi_var(const char *name, char **var) { log_warning_errno(r, "Failed to read EFI variable %s: %m", name); } -static int must_be_root(void) { - - if (geteuid() == 0) - return 0; - - log_error("Need to be root."); - return -EPERM; -} - static int verb_status(int argc, char *argv[], void *userdata) { sd_id128_t uuid = SD_ID128_NULL; diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index f217def92d..51663e28dc 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -3783,11 +3783,10 @@ int main(int argc, char *argv[]) { if (r <= 0) goto finish; - if (geteuid() != 0) { - log_error("Need to be root."); - r = -EPERM; + r = must_be_root(); + if (r < 0) goto finish; - } + r = determine_names(); if (r < 0) goto finish; diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 0a39a7e26c..4f92659fcd 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -2074,10 +2074,9 @@ static int list_machines(int argc, char *argv[], void *userdata) { sd_bus *bus; int r; - if (geteuid() != 0) { - log_error("Must be root."); - return -EPERM; - } + r = must_be_root(); + if (r < 0) + return r; r = acquire_bus(BUS_MANAGER, &bus); if (r < 0) @@ -3603,9 +3602,10 @@ static int start_special(int argc, char *argv[], void *userdata) { if (r < 0) return r; - if (arg_force >= 2 && geteuid() != 0) { - log_error("Must be root."); - return -EPERM; + if (arg_force >= 2) { + r = must_be_root(); + if (r < 0) + return r; } r = prepare_firmware_setup(); @@ -8638,7 +8638,7 @@ static int halt_main(void) { if (geteuid() != 0) { if (arg_dry_run || arg_force > 0) { - log_error("Must be root."); + (void) must_be_root(); return -EPERM; } diff --git a/src/udev/udevadm-control.c b/src/udev/udevadm-control.c index ab9237ff78..d80d61583d 100644 --- a/src/udev/udevadm-control.c +++ b/src/udev/udevadm-control.c @@ -63,10 +63,8 @@ static int adm_control(struct udev *udev, int argc, char *argv[]) { {} }; - if (getuid() != 0) { - log_error("root privileges required"); + if (must_be_root() < 0) return 1; - } uctrl = udev_ctrl_new(udev); if (uctrl == NULL) diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 38ced53676..1644935ff9 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -1670,10 +1670,9 @@ int main(int argc, char *argv[]) { log_set_max_level(LOG_DEBUG); } - if (getuid() != 0) { - r = log_error_errno(EPERM, "root privileges required"); + r = must_be_root(); + if (r < 0) goto exit; - } if (arg_children_max == 0) { cpu_set_t cpu_set;