]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: unify logging of "Must be root" message
authorLennart Poettering <lennart@poettering.net>
Mon, 11 Dec 2017 22:00:57 +0000 (23:00 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 11 Dec 2017 22:19:45 +0000 (23:19 +0100)
Let's unify this in one call, generalizing must_be_root() from
bootctl.c.

src/basic/process-util.c
src/basic/process-util.h
src/boot/bootctl.c
src/nspawn/nspawn.c
src/systemctl/systemctl.c
src/udev/udevadm-control.c
src/udev/udevd.c

index b3d96cdb0f6a72767342742552b03a19a83a80b5..5f001494f039633ac8a59028c596635ed0c573d7 100644 (file)
@@ -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",
index d99fb3d6b84fbdf458953bb3e65bb78251593cf4..39c194df0d60bab7c70102a2881b89635056c562 100644 (file)
@@ -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);
index a5700992d4dd0d838dfce79d7be920c8f2778dc7..b685b3c791571046608ac095b42db540950142d0 100644 (file)
@@ -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;
index f217def92d53b7b03aabc929bd0a1c469b3890d4..51663e28dc9c21da1552a04ac160628a08142321 100644 (file)
@@ -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;
index 0a39a7e26c79c78cf77e9e6ac8a8a5b1e5fbaf01..4f92659fcd2e9dd69ec7cf3af11bd3790ee457e9 100644 (file)
@@ -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;
                 }
 
index ab9237ff7857010610a03e1179cfe50ef8abb2f3..d80d61583dd7fa4684eb0a35890c4a1f67552714 100644 (file)
@@ -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)
index 38ced53676394f9ba44ae1aca103ce3ca630a192..1644935ff9de00371efb1dc52d93b7d2251002ad 100644 (file)
@@ -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;