HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/login/logind-dbus.c
Git blob: 38cb6ce3dd8e7c377cf4004e698626c8561e1006
Lines 3001–4000 of 4,785
"Reboot parameter not supported in containers, refusing.");
r = bus_verify_polkit_async(
message,
"org.freedesktop.login1.set-reboot-parameter",
/* details= */ NULL,
&m->polkit_registry,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = update_reboot_parameter_and_warn(arg, false);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, NULL);
}
static int method_can_reboot_parameter(
sd_bus_message *message,
void *userdata,
sd_bus_error *error) {
_unused_ Manager *m = ASSERT_PTR(userdata);
int r;
assert(message);
r = detect_container();
if (r < 0)
return r;
if (r > 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
return sd_bus_reply_method_return(message, "s", "na");
return return_test_polkit(
message,
"org.freedesktop.login1.set-reboot-parameter",
/* details= */ NULL,
/* good_user= */ UID_INVALID,
error);
}
static int property_get_reboot_to_firmware_setup(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
int r;
assert(bus);
assert(reply);
assert(userdata);
r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
if (r == -ENXIO) {
/* EFI case: let's see what is currently configured in the EFI variables */
r = efi_get_reboot_to_firmware();
if (r < 0 && r != -EOPNOTSUPP)
log_warning_errno(r, "Failed to determine reboot-to-firmware-setup state: %m");
} else if (r < 0)
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
else if (r > 0) {
/* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
if (access("/run/systemd/reboot-to-firmware-setup", F_OK) < 0) {
if (errno != ENOENT)
log_warning_errno(errno, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
r = false;
} else
r = true;
}
return sd_bus_message_append(reply, "b", r > 0);
}
static int method_set_reboot_to_firmware_setup(
sd_bus_message *message,
void *userdata,
sd_bus_error *error) {
Manager *m = ASSERT_PTR(userdata);
bool use_efi;
int b, r;
assert(message);
r = sd_bus_message_read(message, "b", &b);
if (r < 0)
return r;
r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
if (r == -ENXIO) {
/* EFI case: let's see what the firmware supports */
r = efi_reboot_to_firmware_supported();
if (r == -EOPNOTSUPP)
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Firmware does not support boot into firmware.");
if (r < 0)
return r;
use_efi = true;
} else if (r <= 0) {
/* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
if (r < 0)
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Firmware does not support boot into firmware.");
} else
/* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
use_efi = false;
r = bus_verify_polkit_async(
message,
"org.freedesktop.login1.set-reboot-to-firmware-setup",
/* details= */ NULL,
&m->polkit_registry,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
if (use_efi) {
r = efi_set_reboot_to_firmware(b);
if (r < 0)
return r;
} else {
if (b) {
r = touch("/run/systemd/reboot-to-firmware-setup");
if (r < 0)
return r;
} else {
if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno != ENOENT)
return -errno;
}
}
return sd_bus_reply_method_return(message, NULL);
}
static int method_can_reboot_to_firmware_setup(
sd_bus_message *message,
void *userdata,
sd_bus_error *error) {
_unused_ Manager *m = ASSERT_PTR(userdata);
int r;
assert(message);
r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
if (r == -ENXIO) {
/* EFI case: let's see what the firmware supports */
r = efi_reboot_to_firmware_supported();
if (r < 0) {
if (r != -EOPNOTSUPP)
log_warning_errno(r, "Failed to determine whether reboot to firmware is supported: %m");
return sd_bus_reply_method_return(message, "s", "na");
}
} else if (r <= 0) {
/* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
if (r < 0)
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
return sd_bus_reply_method_return(message, "s", "na");
}
return return_test_polkit(
message,
"org.freedesktop.login1.set-reboot-to-firmware-setup",
/* details= */ NULL,
/* good_user= */ UID_INVALID,
error);
}
static int property_get_reboot_to_boot_loader_menu(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
uint64_t x = UINT64_MAX;
int r;
assert(bus);
assert(reply);
assert(userdata);
r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
if (r == -ENXIO) {
/* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distinguished:
*
* 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
* 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
* 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
*/
r = efi_loader_get_config_timeout_one_shot(&x);
if (r < 0) {
if (r != -ENOENT)
log_warning_errno(r, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m");
}
} else if (r < 0)
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
else if (r > 0) {
_cleanup_free_ char *v = NULL;
/* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
r = read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v);
if (r < 0) {
if (r != -ENOENT)
log_warning_errno(r, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
} else {
r = safe_atou64(v, &x);
if (r < 0)
log_warning_errno(r, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
}
}
return sd_bus_message_append(reply, "t", x);
}
static int method_set_reboot_to_boot_loader_menu(
sd_bus_message *message,
void *userdata,
sd_bus_error *error) {
Manager *m = ASSERT_PTR(userdata);
bool use_efi;
uint64_t x;
int r;
assert(message);
r = sd_bus_message_read(message, "t", &x);
if (r < 0)
return r;
r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
if (r == -ENXIO) {
uint64_t features;
/* EFI case: let's see if booting into boot loader menu is supported. */
r = efi_loader_get_features(&features);
if (r < 0)
log_warning_errno(r, "Failed to determine whether reboot to boot loader menu is supported: %m");
if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT))
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader does not support boot into boot loader menu.");
use_efi = true;
} else if (r <= 0) {
/* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
if (r < 0)
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader does not support boot into boot loader menu.");
} else
/* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
use_efi = false;
r = bus_verify_polkit_async(
message,
"org.freedesktop.login1.set-reboot-to-boot-loader-menu",
/* details= */ NULL,
&m->polkit_registry,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
if (use_efi) {
if (x == UINT64_MAX)
r = efi_set_variable(EFI_LOADER_VARIABLE_STR("LoaderConfigTimeoutOneShot"), NULL, 0);
else {
char buf[DECIMAL_STR_MAX(uint64_t) + 1];
xsprintf(buf, "%" PRIu64, DIV_ROUND_UP(x, USEC_PER_SEC)); /* second granularity */
r = efi_set_variable_string(EFI_LOADER_VARIABLE_STR("LoaderConfigTimeoutOneShot"), buf);
}
if (r < 0)
return r;
} else {
if (x == UINT64_MAX) {
if (unlink("/run/systemd/reboot-to-boot-loader-menu") < 0 && errno != ENOENT)
return -errno;
} else {
r = write_string_filef("/run/systemd/reboot-to-boot-loader-menu",
WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL,
"%" PRIu64, x); /* μs granularity */
if (r < 0)
return r;
}
}
return sd_bus_reply_method_return(message, NULL);
}
static int method_can_reboot_to_boot_loader_menu(
sd_bus_message *message,
void *userdata,
sd_bus_error *error) {
_unused_ Manager *m = ASSERT_PTR(userdata);
int r;
assert(message);
r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
if (r == -ENXIO) {
uint64_t features = 0;
/* EFI case, let's see if booting into boot loader menu is supported. */
r = efi_loader_get_features(&features);
if (r < 0)
log_warning_errno(r, "Failed to determine whether reboot to boot loader menu is supported: %m");
if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT))
return sd_bus_reply_method_return(message, "s", "na");
} else if (r <= 0) {
/* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
if (r < 0)
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
return sd_bus_reply_method_return(message, "s", "na");
}
return return_test_polkit(
message,
"org.freedesktop.login1.set-reboot-to-boot-loader-menu",
/* details= */ NULL,
/* good_user= */ UID_INVALID,
error);
}
static int property_get_reboot_to_boot_loader_entry(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
_cleanup_free_ char *v = NULL;
Manager *m = ASSERT_PTR(userdata);
const char *x = NULL;
int r;
assert(bus);
assert(reply);
r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
if (r == -ENXIO) {
/* EFI case: let's read the LoaderEntryOneShot variable */
r = efi_loader_update_entry_one_shot_cache(&m->efi_loader_entry_one_shot, &m->efi_loader_entry_one_shot_stat);
if (r < 0) {
if (r != -ENOENT)
log_warning_errno(r, "Failed to read LoaderEntryOneShot variable, ignoring: %m");
} else
x = m->efi_loader_entry_one_shot;
} else if (r < 0)
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
else if (r > 0) {
/* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
r = read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v);
if (r < 0) {
if (r != -ENOENT)
log_warning_errno(r, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m");
} else if (!efi_loader_entry_name_valid(v))
log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
else
x = v;
}
return sd_bus_message_append(reply, "s", x);
}
static int boot_loader_entry_exists(Manager *m, const char *id) {
_cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL;
int r;
assert(m);
assert(id);
r = boot_config_load_auto(&config, NULL, NULL);
if (r < 0 && r != -ENOKEY) /* don't complain if no GPT is found, hence skip ENOKEY */
return r;
r = manager_read_efi_boot_loader_entries(m);
if (r >= 0)
(void) boot_config_augment_from_loader(&config, m->efi_boot_loader_entries, /* auto_only= */ true);
return !!boot_config_find_entry(&config, id);
}
static int method_set_reboot_to_boot_loader_entry(
sd_bus_message *message,
void *userdata,
sd_bus_error *error) {
Manager *m = ASSERT_PTR(userdata);
bool use_efi;
const char *v;
int r;
assert(message);
r = sd_bus_message_read(message, "s", &v);
if (r < 0)
return r;
if (isempty(v))
v = NULL;
else if (efi_loader_entry_name_valid(v)) {
r = boot_loader_entry_exists(m, v);
if (r < 0)
return r;
if (r == 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader entry '%s' is not known.", v);
} else
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Boot loader entry name '%s' is not valid, refusing.", v);
r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
if (r == -ENXIO) {
uint64_t features;
/* EFI case: let's see if booting into boot loader entry is supported. */
r = efi_loader_get_features(&features);
if (r < 0)
log_warning_errno(r, "Failed to determine whether reboot into boot loader entry is supported: %m");
if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_ENTRY_ONESHOT))
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Loader does not support boot into boot loader entry.");
use_efi = true;
} else if (r <= 0) {
/* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
if (r < 0)
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Loader does not support boot into boot loader entry.");
} else
/* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
use_efi = false;
r = bus_verify_polkit_async(
message,
"org.freedesktop.login1.set-reboot-to-boot-loader-entry",
/* details= */ NULL,
&m->polkit_registry,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
if (use_efi) {
if (isempty(v))
/* Delete item */
r = efi_set_variable(EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"), NULL, 0);
else
r = efi_set_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"), v);
if (r < 0)
return r;
} else {
if (isempty(v)) {
if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno != ENOENT)
return -errno;
} else {
r = write_string_file("/run/systemd/reboot-to-boot-loader-entry", v, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
if (r < 0)
return r;
}
}
return sd_bus_reply_method_return(message, NULL);
}
static int method_can_reboot_to_boot_loader_entry(
sd_bus_message *message,
void *userdata,
sd_bus_error *error) {
_unused_ Manager *m = ASSERT_PTR(userdata);
int r;
assert(message);
r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
if (r == -ENXIO) {
uint64_t features = 0;
/* EFI case, let's see if booting into boot loader entry is supported. */
r = efi_loader_get_features(&features);
if (r < 0)
log_warning_errno(r, "Failed to determine whether reboot to boot loader entry is supported: %m");
if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_ENTRY_ONESHOT))
return sd_bus_reply_method_return(message, "s", "na");
} else if (r <= 0) {
/* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
if (r < 0)
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
return sd_bus_reply_method_return(message, "s", "na");
}
return return_test_polkit(
message,
"org.freedesktop.login1.set-reboot-to-boot-loader-entry",
/* details= */ NULL,
/* good_user= */ UID_INVALID,
error);
}
static int property_get_boot_loader_entries(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
_cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL;
Manager *m = ASSERT_PTR(userdata);
size_t i;
int r;
assert(bus);
assert(reply);
r = boot_config_load_auto(&config, NULL, NULL);
if (r < 0 && r != -ENOKEY) /* don't complain if there's no GPT found */
return r;
r = manager_read_efi_boot_loader_entries(m);
if (r >= 0)
(void) boot_config_augment_from_loader(&config, m->efi_boot_loader_entries, /* auto_only= */ true);
r = sd_bus_message_open_container(reply, 'a', "s");
if (r < 0)
return r;
for (i = 0; i < config.n_entries; i++) {
BootEntry *e = config.entries + i;
r = sd_bus_message_append(reply, "s", e->id);
if (r < 0)
return r;
}
return sd_bus_message_close_container(reply);
}
static int wall_message_validate(const char *wall_message, sd_bus_error *error) {
if (strlen(wall_message) > WALL_MESSAGE_MAX)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Wall message too long, maximum permitted length is %u characters.",
WALL_MESSAGE_MAX);
if (string_has_cc(wall_message, /* ok= */ "\n\t"))
return sd_bus_error_set(error,
SD_BUS_ERROR_INVALID_ARGS,
"Wall message contains control characters, refusing.");
return 0;
}
static int property_set_wall_message(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *value,
void *userdata,
sd_bus_error *error) {
char **p = ASSERT_PTR(userdata);
const char *s;
int r;
assert(value);
r = sd_bus_message_read(value, "s", &s);
if (r < 0)
return r;
r = wall_message_validate(s, error);
if (r < 0)
return r;
return free_and_strdup_warn(p, empty_to_null(s));
}
static int method_set_wall_message(
sd_bus_message *message,
void *userdata,
sd_bus_error *error) {
int r;
Manager *m = ASSERT_PTR(userdata);
char *wall_message;
int enable_wall_messages;
assert(message);
r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages);
if (r < 0)
return r;
r = wall_message_validate(wall_message, error);
if (r < 0)
return r;
/* Short-circuit the operation if the desired state is already in place, to
* avoid an unnecessary polkit permission check. */
if (streq_ptr(m->wall_message, empty_to_null(wall_message)) &&
m->wall_messages == enable_wall_messages)
goto done;
r = bus_verify_polkit_async(
message,
"org.freedesktop.login1.set-wall-message",
/* details= */ NULL,
&m->polkit_registry,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* Will call us back */
r = free_and_strdup(&m->wall_message, empty_to_null(wall_message));
if (r < 0)
return log_oom();
m->wall_messages = enable_wall_messages;
done:
return sd_bus_reply_method_return(message, NULL);
}
static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
const char *who, *why, *what, *mode;
_cleanup_free_ char *id = NULL;
_cleanup_close_ int fifo_fd = -EBADF;
Manager *m = ASSERT_PTR(userdata);
InhibitMode mm;
InhibitWhat w;
uid_t uid;
_unused_ bool error_or_denial = false;
int r;
assert(message);
r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
if (r < 0)
return r;
w = inhibit_what_from_string(what);
if (w <= 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Invalid what specification %s", what);
mm = inhibit_mode_from_string(mode);
if (mm < 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Invalid mode specification %s", mode);
/* Delay is only supported for shutdown/sleep */
if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS,
"Delay inhibitors only supported for shutdown and sleep");
/* Don't allow taking delay locks while we are already
* executing the operation. We shouldn't create the impression
* that the lock was successful if the machine is about to go
* down/suspend any moment. */
if (m->delayed_action && m->delayed_action->inhibit_what & w)
return sd_bus_error_set(error, BUS_ERROR_OPERATION_IN_PROGRESS,
"The operation inhibition has been requested for is already running");
BIT_FOREACH(i, w) {
const InhibitWhat v = 1U << i;
r = bus_verify_polkit_async(
message,
v == INHIBIT_SHUTDOWN ? (IN_SET(mm, INHIBIT_BLOCK, INHIBIT_BLOCK_WEAK) ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
v == INHIBIT_SLEEP ? (IN_SET(mm, INHIBIT_BLOCK, INHIBIT_BLOCK_WEAK) ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
v == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" :
v == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
v == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
v == INHIBIT_HANDLE_REBOOT_KEY ? "org.freedesktop.login1.inhibit-handle-reboot-key" :
v == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
"org.freedesktop.login1.inhibit-handle-lid-switch",
/* details= */ NULL,
&m->polkit_registry,
error);
if (r < 0) {
/* If we get -EBUSY, it means a polkit decision was made, but not for
* this action in particular. Assuming there are more actions requested,
* ignore that error and allow the decision to be revealed later. */
if ((~v & w) && r == -EBUSY)
error_or_denial = true;
else
return r;
}
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
}
/* If error_or_denial was set above, it means that a polkit denial or
* error was deferred for a future call to bus_verify_polkit_async()
* to catch. In any case, it also means that the payload guarded by
* these polkit calls should never be executed, and hence we should
* never reach this point. */
assert(!error_or_denial);
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID|SD_BUS_CREDS_PIDFD, &creds);
if (r < 0)
return r;
r = sd_bus_creds_get_euid(creds, &uid);
if (r < 0)
return r;
r = bus_creds_get_pidref(creds, &pidref);
if (r < 0)
return r;
if (hashmap_size(m->inhibitors) >= m->inhibitors_max)
return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED,
"Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.",
m->inhibitors_max);
do {
id = mfree(id);
if (asprintf(&id, "%" PRIu64, ++m->inhibit_counter) < 0)
return -ENOMEM;
} while (hashmap_contains(m->inhibitors, id));
_cleanup_(inhibitor_freep) Inhibitor *i = NULL;
r = manager_add_inhibitor(m, id, &i);
if (r < 0)
return r;
i->what = w;
i->mode = mm;
i->pid = TAKE_PIDREF(pidref);
i->uid = uid;
i->why = strdup(why);
i->who = strdup(who);
if (!i->why || !i->who)
return -ENOMEM;
fifo_fd = inhibitor_create_fifo(i);
if (fifo_fd < 0)
return fifo_fd;
r = inhibitor_start(i);
if (r < 0)
return r;
TAKE_PTR(i);
return sd_bus_reply_method_return(message, "h", fifo_fd);
}
static int bus_property_get_kill_exclude_users(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *reterr_error) {
Manager *m = ASSERT_PTR(userdata);
/* NULL represents the default, i.e. only root is excluded. */
return sd_bus_message_append_strv(reply, m->kill_exclude_users ?: STRV_MAKE("root"));
}
static const sd_bus_vtable manager_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", bus_property_get_bool, bus_property_set_bool, offsetof(Manager, wall_messages), 0),
SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, property_set_wall_message, offsetof(Manager, wall_message), 0),
SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KillExcludeUsers", "as", bus_property_get_kill_exclude_users, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KillUserProcesses", "b", bus_property_get_bool, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter, 0, 0),
SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, 0),
SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu, 0, 0),
SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry, 0, 0),
SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("BlockWeakInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL, offsetof(Manager, user_stop_delay), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("SleepOperation", "as", property_get_sleep_operations, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandlePowerKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_power_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandleRebootKey", "s", property_get_handle_action, offsetof(Manager, handle_reboot_key), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandleRebootKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_reboot_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandleSuspendKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandleHibernateKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HandleSecureAttentionKey", "s", property_get_handle_action, offsetof(Manager, handle_secure_attention_key), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
SD_BUS_PROPERTY("PreparingForShutdownWithMetadata", "a{sv}", property_get_preparing_shutdown_with_metadata, 0, 0),
SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("DesignatedMaintenanceTime", "s", property_get_maintenance_time, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power, 0, 0),
SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL, offsetof(Manager, runtime_dir_inodes), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size, offsetof(Manager, inhibitors), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size, offsetof(Manager, sessions), 0),
SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
SD_BUS_PROPERTY("StopIdleSessionUSec", "t", NULL, offsetof(Manager, stop_idle_session_usec), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_METHOD_WITH_ARGS("GetSession",
SD_BUS_ARGS("s", session_id),
SD_BUS_RESULT("o", object_path),
method_get_session,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("GetSessionByPID",
SD_BUS_ARGS("u", pid),
SD_BUS_RESULT("o", object_path),
method_get_session_by_pid,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("GetUser",
SD_BUS_ARGS("u", uid),
SD_BUS_RESULT("o", object_path),
method_get_user,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("GetUserByPID",
SD_BUS_ARGS("u", pid),
SD_BUS_RESULT("o", object_path),
method_get_user_by_pid,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("GetSeat",
SD_BUS_ARGS("s", seat_id),
SD_BUS_RESULT("o", object_path),
method_get_seat,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("ListSessions",
SD_BUS_NO_ARGS,
SD_BUS_RESULT("a(susso)", sessions),
method_list_sessions,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("ListSessionsEx",
SD_BUS_NO_ARGS,
SD_BUS_RESULT("a(sussussbto)", sessions),
method_list_sessions_ex,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("ListUsers",
SD_BUS_NO_ARGS,
SD_BUS_RESULT("a(uso)", users),
method_list_users,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("ListSeats",
SD_BUS_NO_ARGS,
SD_BUS_RESULT("a(so)", seats),
method_list_seats,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("ListInhibitors",
SD_BUS_NO_ARGS,
SD_BUS_RESULT("a(ssssuu)", inhibitors),
method_list_inhibitors,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("CreateSession",
SD_BUS_ARGS("u", uid,
"u", pid,
"s", service,
"s", type,
"s", class,
"s", desktop,
"s", seat_id,
"u", vtnr,
"s", tty,
"s", display,
"b", remote,
"s", remote_user,
"s", remote_host,
"a(sv)", properties),
SD_BUS_RESULT("s", session_id,
"o", object_path,
"s", runtime_path,
"h", fifo_fd,
"u", uid,
"s", seat_id,
"u", vtnr,
"b", existing),
method_create_session,
0),
SD_BUS_METHOD_WITH_ARGS("CreateSessionWithPIDFD",
SD_BUS_ARGS("u", uid,
"h", pidfd,
"s", service,
"s", type,
"s", class,
"s", desktop,
"s", seat_id,
"u", vtnr,
"s", tty,
"s", display,
"b", remote,
"s", remote_user,
"s", remote_host,
"t", flags,
"a(sv)", properties),
SD_BUS_RESULT("s", session_id,
"o", object_path,
"s", runtime_path,
"h", fifo_fd,
"u", uid,
"s", seat_id,
"u", vtnr,
"b", existing),
method_create_session_pidfd,
0),
SD_BUS_METHOD_WITH_ARGS("ReleaseSession",
SD_BUS_ARGS("s", session_id),
SD_BUS_NO_RESULT,
method_release_session,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("ActivateSession",
SD_BUS_ARGS("s", session_id),
SD_BUS_NO_RESULT,
method_activate_session,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("ActivateSessionOnSeat",
SD_BUS_ARGS("s", session_id, "s", seat_id),
SD_BUS_NO_RESULT,
method_activate_session_on_seat,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("LockSession",
SD_BUS_ARGS("s", session_id),
SD_BUS_NO_RESULT,
method_lock_session,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("UnlockSession",
SD_BUS_ARGS("s", session_id),
SD_BUS_NO_RESULT,
method_lock_session,
SD_BUS_VTABLE_UNPRIVILEGED),