tree-wide: unify logging of "Must be root" message

Let's unify this in one call, generalizing must_be_root() from
bootctl.c.
This commit is contained in:
Lennart Poettering 2017-12-11 23:00:57 +01:00
parent 4fe2ba0e25
commit fba868fa71
7 changed files with 25 additions and 27 deletions

View 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",

View 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);

View 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;

View 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;

View 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;
}

View 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)

View 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;