From: Lennart Poettering Date: Mon, 11 Dec 2017 22:00:57 +0000 (+0100) Subject: tree-wide: unify logging of "Must be root" message X-Git-Tag: v236~24^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fba868fa710a216adade59c2683d8bee20c7da4b;p=thirdparty%2Fsystemd.git tree-wide: unify logging of "Must be root" message Let's unify this in one call, generalizing must_be_root() from bootctl.c. --- diff --git a/src/basic/process-util.c b/src/basic/process-util.c index b3d96cdb0f6..5f001494f03 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 d99fb3d6b84..39c194df0d6 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 a5700992d4d..b685b3c7915 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 f217def92d5..51663e28dc9 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 0a39a7e26c7..4f92659fcd2 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 ab9237ff785..d80d61583dd 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 38ced536763..1644935ff9d 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;