HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/login/logind-dbus.c
Git blob: 38cb6ce3dd8e7c377cf4004e698626c8561e1006
Lines 2001–3000 of 4,785
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
int r;
assert(m);
assert(!m->action_job);
assert(a);
if (a->inhibit_what == INHIBIT_SHUTDOWN)
bus_manager_log_shutdown(m, a);
r = bus_call_method(
m->bus,
bus_systemd_mgr,
"StartUnit",
error,
&reply,
"ss", a->target, "replace-irreversibly");
if (r < 0)
goto fail;
r = strdup_job(reply, &m->action_job);
if (r < 0)
goto fail;
/* Save the action to prevent another request of shutdown or friends before the current action being
* finished. See method_do_shutdown_or_sleep(). This is also used in match_job_removed() to log what
* kind of action is finished. */
m->delayed_action = a;
/* Make sure the lid switch is ignored for a while. */
manager_set_lid_switch_ignore(m, usec_add(now(CLOCK_MONOTONIC), m->holdoff_timeout_usec));
return 0;
fail:
/* Tell people that they now may take a lock again. */
(void) send_prepare_for(m, a, false);
return r;
}
int manager_dispatch_delayed(Manager *manager, bool timeout) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
Inhibitor *offending = NULL;
int r;
assert(manager);
if (!manager->delayed_action || manager->action_job)
return 0;
if (manager_is_inhibited(
manager,
manager->delayed_action->inhibit_what,
/* since= */ NULL,
MANAGER_IS_INHIBITED_CHECK_DELAY,
UID_INVALID,
&offending)) {
if (!timeout)
return 0;
_cleanup_free_ char *comm = NULL, *u = NULL;
(void) pidref_get_comm(&offending->pid, &comm);
u = uid_to_name(offending->uid);
log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
offending->uid, strna(u),
offending->pid.pid, strna(comm));
}
/* Actually do the operation */
r = execute_shutdown_or_sleep(manager, manager->delayed_action, &error);
if (r < 0) {
log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
bus_error_message(&error, r));
manager->delayed_action = NULL;
}
return 1; /* We did some work. */
}
static int manager_inhibit_timeout_handler(
sd_event_source *s,
uint64_t usec,
void *userdata) {
Manager *manager = ASSERT_PTR(userdata);
assert(manager->inhibit_timeout_source == s);
return manager_dispatch_delayed(manager, true);
}
static int delay_shutdown_or_sleep(
Manager *m,
const HandleActionData *a) {
int r;
assert(m);
assert(a);
r = event_reset_time_relative(
m->event, &m->inhibit_timeout_source,
CLOCK_MONOTONIC, m->inhibit_delay_max, /* accuracy= */ 0,
manager_inhibit_timeout_handler, m,
/* priority= */ 0, "inhibit-timeout", /* force_reset= */ true);
if (r < 0)
return log_error_errno(r, "Failed to reset timer event source for inhibit timeout: %m");
m->delayed_action = a;
return 0;
}
static void cancel_delayed_action(Manager *m) {
assert(m);
(void) sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_OFF);
/* When m->action_job is NULL, the delayed action has not been triggered yet. Let's clear it to
* accept later shutdown and friends.
*
* When m->action_job is non-NULL, the delayed action has been already triggered, and now we are
* waiting for the job being finished. In match_job_removed(), the triggered action will be used.
* Hence, do not clear it. */
if (!m->action_job)
m->delayed_action = NULL;
}
int bus_manager_shutdown_or_sleep_now_or_later(
Manager *m,
const HandleActionData *a,
sd_bus_error *error) {
_cleanup_free_ char *load_state = NULL;
bool delayed;
int r;
assert(m);
assert(a);
assert(!m->action_job);
r = unit_load_state(m->bus, a->target, &load_state);
if (r < 0)
return r;
if (!streq(load_state, "loaded"))
return log_notice_errno(SYNTHETIC_ERRNO(EACCES),
"Unit %s is %s, refusing operation.",
a->target, load_state);
/* Tell everybody to prepare for shutdown/sleep */
(void) send_prepare_for(m, a, true);
delayed =
m->inhibit_delay_max > 0 &&
a->inhibit_what >= 0 &&
manager_is_inhibited(m, a->inhibit_what, NULL, MANAGER_IS_INHIBITED_CHECK_DELAY, UID_INVALID, NULL);
if (delayed)
/* Shutdown is delayed, keep in mind what we
* want to do, and start a timeout */
r = delay_shutdown_or_sleep(m, a);
else
/* Shutdown is not delayed, execute it
* immediately */
r = execute_shutdown_or_sleep(m, a, error);
return r;
}
static int setup_wall_message_timer(Manager *m, sd_bus_message* message) {
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
int r;
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
if (r >= 0) {
const char *tty = NULL;
(void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid);
(void) sd_bus_creds_get_tty(creds, &tty);
r = free_and_strdup(&m->scheduled_shutdown_tty, tty);
if (r < 0)
return log_oom();
}
r = manager_setup_wall_message_timer(m);
if (r < 0)
return r;
return 0;
}
static int method_do_shutdown_or_sleep(
Manager *m,
sd_bus_message *message,
HandleAction action,
bool with_flags,
sd_bus_error *error) {
uint64_t flags;
int r;
assert(m);
assert(message);
assert(HANDLE_ACTION_IS_SHUTDOWN(action) || HANDLE_ACTION_IS_SLEEP(action));
if (with_flags) {
/* New style method: with flags parameter (and interactive bool in the bus message header) */
r = sd_bus_message_read(message, "t", &flags);
if (r < 0)
return r;
if ((flags & ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC) != 0)
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS,
"Invalid flags parameter");
if (FLAGS_SET(flags, (SD_LOGIND_REBOOT_VIA_KEXEC|SD_LOGIND_SOFT_REBOOT)))
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS,
"Both reboot via kexec and soft reboot selected, which is not supported");
if (action != HANDLE_REBOOT) {
if (FLAGS_SET(flags, SD_LOGIND_REBOOT_VIA_KEXEC))
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS,
"Reboot via kexec option is only applicable with reboot operations");
if (flags & (SD_LOGIND_SOFT_REBOOT|SD_LOGIND_SOFT_REBOOT_IF_NEXTROOT_SET_UP))
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS,
"Soft reboot option is only applicable with reboot operations");
}
} else {
/* Old style method: no flags parameter, but interactive bool passed as boolean in
* payload. Let's convert this argument to the new-style flags parameter for our internal
* use. */
int interactive;
r = sd_bus_message_read(message, "b", &interactive);
if (r < 0)
return r;
flags = interactive ? SD_LOGIND_INTERACTIVE : 0;
}
const HandleActionData *a = NULL;
if (FLAGS_SET(flags, SD_LOGIND_SOFT_REBOOT) ||
(FLAGS_SET(flags, SD_LOGIND_SOFT_REBOOT_IF_NEXTROOT_SET_UP) && path_is_os_tree("/run/nextroot") > 0))
a = handle_action_lookup(HANDLE_SOFT_REBOOT);
else if (FLAGS_SET(flags, SD_LOGIND_REBOOT_VIA_KEXEC) && kexec_loaded())
a = handle_action_lookup(HANDLE_KEXEC);
if (action == HANDLE_SLEEP) {
HandleAction selected;
selected = handle_action_sleep_select(m);
if (selected < 0)
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
"None of the configured sleep operations are supported");
assert_se(a = handle_action_lookup(selected));
} else if (HANDLE_ACTION_IS_SLEEP(action)) {
SleepSupport support;
assert_se(a = handle_action_lookup(action));
assert(a->sleep_operation >= 0);
assert(a->sleep_operation < _SLEEP_OPERATION_MAX);
r = sleep_supported_full(a->sleep_operation, &support);
if (r < 0)
return r;
if (r == 0)
switch (support) {
case SLEEP_DISABLED:
return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
"Sleep verb '%s' is disabled by config",
sleep_operation_to_string(a->sleep_operation));
case SLEEP_NOT_CONFIGURED:
case SLEEP_STATE_OR_MODE_NOT_SUPPORTED:
case SLEEP_ALARM_NOT_SUPPORTED:
return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
"Sleep verb '%s' is not configured or configuration is not supported by kernel",
sleep_operation_to_string(a->sleep_operation));
case SLEEP_RESUME_NOT_SUPPORTED:
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
"Not running on EFI and resume= is not set, or noresume is set. No available method to resume from hibernation");
case SLEEP_RESUME_DEVICE_MISSING:
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
"Specified resume device is missing or is not an active swap device");
case SLEEP_RESUME_MISCONFIGURED:
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
"Invalid resume config: resume= is not populated yet resume_offset= is");
case SLEEP_NOT_ENOUGH_SWAP_SPACE:
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
"Not enough suitable swap space for hibernation available on compatible block devices and file systems");
default:
assert_not_reached();
}
} else if (!a)
assert_se(a = handle_action_lookup(action));
r = manager_verify_shutdown_creds(m, message, /* link= */ NULL, a, flags, error);
if (r != 0)
return r;
{
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
(void) bus_query_sender_pidref(message, &pidref);
log_shutdown_caller(&pidref, handle_action_to_string(a->handle));
}
if (m->delayed_action)
return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS,
"Action %s already in progress, refusing requested %s operation.",
handle_action_to_string(m->delayed_action->handle),
handle_action_to_string(a->handle));
r = bus_manager_shutdown_or_sleep_now_or_later(m, a, error);
if (r < 0)
return r;
/* Reset in case we're short-circuiting a scheduled shutdown. */
m->unlink_nologin = false;
manager_reset_scheduled_shutdown(m);
m->scheduled_shutdown_timeout = 0;
m->scheduled_shutdown_action = action;
(void) setup_wall_message_timer(m, message);
return sd_bus_reply_method_return(message, NULL);
}
static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
m, message,
HANDLE_POWEROFF,
sd_bus_message_is_method_call(message, NULL, "PowerOffWithFlags"),
error);
}
static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
m, message,
HANDLE_REBOOT,
sd_bus_message_is_method_call(message, NULL, "RebootWithFlags"),
error);
}
static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
m, message,
HANDLE_HALT,
sd_bus_message_is_method_call(message, NULL, "HaltWithFlags"),
error);
}
static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
m, message,
HANDLE_SUSPEND,
sd_bus_message_is_method_call(message, NULL, "SuspendWithFlags"),
error);
}
static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
m, message,
HANDLE_HIBERNATE,
sd_bus_message_is_method_call(message, NULL, "HibernateWithFlags"),
error);
}
static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
m, message,
HANDLE_HYBRID_SLEEP,
sd_bus_message_is_method_call(message, NULL, "HybridSleepWithFlags"),
error);
}
static int method_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
m, message,
HANDLE_SUSPEND_THEN_HIBERNATE,
sd_bus_message_is_method_call(message, NULL, "SuspendThenHibernateWithFlags"),
error);
}
static int method_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_do_shutdown_or_sleep(
m, message,
HANDLE_SLEEP,
/* with_flags= */ true,
error);
}
static int nologin_timeout_handler(
sd_event_source *s,
uint64_t usec,
void *userdata) {
Manager *m = ASSERT_PTR(userdata);
log_info("Creating /run/nologin, blocking further logins...");
m->unlink_nologin =
create_shutdown_run_nologin_or_warn() >= 0;
return 0;
}
static usec_t nologin_timeout_usec(usec_t elapse) {
/* Issue /run/nologin five minutes before shutdown */
return LESS_BY(elapse, 5 * USEC_PER_MINUTE);
}
static int update_schedule_file(Manager *m) {
_cleanup_(unlink_and_freep) char *temp_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
assert(m);
assert(handle_action_valid(m->scheduled_shutdown_action));
r = mkdir_parents_label(SHUTDOWN_SCHEDULE_FILE, 0755);
if (r < 0)
return log_error_errno(r, "Failed to create shutdown subdirectory: %m");
r = fopen_temporary(SHUTDOWN_SCHEDULE_FILE, &f, &temp_path);
if (r < 0)
return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m");
(void) fchmod(fileno(f), 0644);
serialize_usec(f, "USEC", m->scheduled_shutdown_timeout);
serialize_item_format(f, "WARN_WALL", "%s", one_zero(m->wall_messages));
serialize_item_format(f, "MODE", "%s", handle_action_to_string(m->scheduled_shutdown_action));
serialize_item_format(f, "UID", UID_FMT, m->scheduled_shutdown_uid);
if (m->scheduled_shutdown_tty)
serialize_item_format(f, "TTY", "%s", m->scheduled_shutdown_tty);
if (!isempty(m->wall_message)) {
r = serialize_item_escaped(f, "WALL_MESSAGE", m->wall_message);
if (r < 0)
goto fail;
}
r = fflush_and_check(f);
if (r < 0)
goto fail;
if (rename(temp_path, SHUTDOWN_SCHEDULE_FILE) < 0) {
r = -errno;
goto fail;
}
temp_path = mfree(temp_path);
return 0;
fail:
(void) unlink(SHUTDOWN_SCHEDULE_FILE);
return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
}
static int manager_scheduled_shutdown_handler(
sd_event_source *s,
uint64_t usec,
void *userdata) {
Manager *m = ASSERT_PTR(userdata);
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
const HandleActionData *a;
int r;
assert_se(a = handle_action_lookup(m->scheduled_shutdown_action));
/* Don't allow multiple jobs being executed at the same time */
if (m->delayed_action) {
r = log_error_errno(SYNTHETIC_ERRNO(EALREADY),
"Scheduled shutdown to %s failed: shutdown or sleep operation already in progress.",
a->target);
goto error;
}
if (m->shutdown_dry_run) {
/* We do not process delay inhibitors here. Otherwise, we
* would have to be considered "in progress" (like the check
* above) for some seconds after our admin has seen the final
* wall message. */
bus_manager_log_shutdown(m, a);
log_info("Running in dry run, suppressing action.");
manager_reset_scheduled_shutdown(m);
return 0;
}
r = bus_manager_shutdown_or_sleep_now_or_later(m, a, &error);
if (r < 0) {
log_error_errno(r, "Scheduled shutdown to %s failed: %m", a->target);
goto error;
}
return 0;
error:
manager_reset_scheduled_shutdown(m);
return r;
}
static int manager_setup_shutdown_timers(Manager* m) {
int r;
assert(m);
r = event_reset_time(m->event, &m->scheduled_shutdown_timeout_source,
CLOCK_REALTIME,
m->scheduled_shutdown_timeout, 0,
manager_scheduled_shutdown_handler, m,
0, "scheduled-shutdown-timeout", true);
if (r < 0)
goto fail;
r = event_reset_time(m->event, &m->nologin_timeout_source,
CLOCK_REALTIME,
nologin_timeout_usec(m->scheduled_shutdown_timeout), 0,
nologin_timeout_handler, m,
0, "nologin-timeout", true);
if (r < 0)
goto fail;
return 0;
fail:
m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source);
return r;
}
void manager_load_scheduled_shutdown(Manager *m) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *usec = NULL,
*warn_wall = NULL,
*mode = NULL,
*wall_message = NULL,
*uid = NULL,
*tty = NULL;
int r;
assert(m);
r = parse_env_file(f, SHUTDOWN_SCHEDULE_FILE,
"USEC", &usec,
"WARN_WALL", &warn_wall,
"MODE", &mode,
"WALL_MESSAGE", &wall_message,
"UID", &uid,
"TTY", &tty);
/* reset will delete the file */
manager_reset_scheduled_shutdown(m);
if (r == -ENOENT)
return;
if (r < 0)
return (void) log_debug_errno(r, "Failed to parse " SHUTDOWN_SCHEDULE_FILE ": %m");
HandleAction handle = handle_action_from_string(mode);
if (handle < 0)
return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse scheduled shutdown type: %s", mode);
if (!usec)
return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "USEC is required");
if (deserialize_usec(usec, &m->scheduled_shutdown_timeout) < 0)
return;
/* assign parsed type only after we know usec is also valid */
m->scheduled_shutdown_action = handle;
if (warn_wall) {
r = parse_boolean(warn_wall);
if (r < 0)
log_debug_errno(r, "Failed to parse enabling wall messages");
else
m->wall_messages = r;
}
if (wall_message) {
_cleanup_free_ char *unescaped = NULL;
r = cunescape(wall_message, 0, &unescaped);
if (r < 0)
log_debug_errno(r, "Failed to parse wall message: %s", wall_message);
else
free_and_replace(m->wall_message, unescaped);
}
if (uid) {
r = parse_uid(uid, &m->scheduled_shutdown_uid);
if (r < 0)
log_debug_errno(r, "Failed to parse wall uid: %s", uid);
}
free_and_replace(m->scheduled_shutdown_tty, tty);
r = manager_setup_shutdown_timers(m);
if (r < 0)
return manager_reset_scheduled_shutdown(m);
(void) manager_setup_wall_message_timer(m);
(void) update_schedule_file(m);
return;
}
static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = ASSERT_PTR(userdata);
HandleAction handle;
const HandleActionData *a;
uint64_t elapse;
char *type;
int r;
bool dry_run = false;
assert(message);
r = sd_bus_message_read(message, "st", &type, &elapse);
if (r < 0)
return r;
if (startswith(type, "dry-")) {
type += 4;
dry_run = true;
}
handle = handle_action_from_string(type);
if (!HANDLE_ACTION_IS_SHUTDOWN(handle))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type: %s", type);
assert_se(a = handle_action_lookup(handle));
assert(a->polkit_action);
r = manager_verify_shutdown_creds(m, message, /* link= */ NULL, a, 0, error);
if (r != 0)
return r;
if (elapse == USEC_INFINITY) {
if (m->maintenance_time) {
r = calendar_spec_next_usec(m->maintenance_time, now(CLOCK_REALTIME), &elapse);
if (r == -ENOENT)
return sd_bus_error_set(error,
BUS_ERROR_DESIGNATED_MAINTENANCE_TIME_NOT_SCHEDULED,
"No upcoming maintenance window scheduled");
if (r < 0)
return sd_bus_error_set_errnof(error, r,
"Failed to determine next maintenance window: %m");
log_info("Scheduled %s at maintenance window %s", type, FORMAT_TIMESTAMP(elapse));
} else
/* the good old shutdown command uses one minute by default */
elapse = usec_add(now(CLOCK_REALTIME), USEC_PER_MINUTE);
}
m->scheduled_shutdown_action = handle;
m->shutdown_dry_run = dry_run;
m->scheduled_shutdown_timeout = elapse;
r = manager_setup_shutdown_timers(m);
if (r < 0)
return r;
r = setup_wall_message_timer(m, message);
if (r >= 0)
r = update_schedule_file(m);
if (r < 0) {
manager_reset_scheduled_shutdown(m);
return r;
}
manager_send_changed(m, "ScheduledShutdown");
return sd_bus_reply_method_return(message, NULL);
}
static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = ASSERT_PTR(userdata);
const HandleActionData *a;
bool cancelled;
int r;
assert(message);
cancelled = handle_action_valid(m->scheduled_shutdown_action) && m->scheduled_shutdown_action != HANDLE_IGNORE;
if (!cancelled)
return sd_bus_reply_method_return(message, "b", false);
assert_se(a = handle_action_lookup(m->scheduled_shutdown_action));
if (!a->polkit_action)
return sd_bus_error_set(error, SD_BUS_ERROR_AUTH_FAILED, "Unsupported shutdown type");
r = bus_verify_polkit_async(
message,
a->polkit_action,
/* 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 (m->wall_messages) {
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
const char *tty = NULL;
uid_t uid = 0;
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
if (r >= 0) {
(void) sd_bus_creds_get_uid(creds, &uid);
(void) sd_bus_creds_get_tty(creds, &tty);
}
_cleanup_free_ char *username = uid_to_name(uid);
log_struct(LOG_INFO,
LOG_MESSAGE("System shutdown has been cancelled"),
LOG_ITEM("ACTION=%s", handle_action_to_string(a->handle)),
LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN_CANCELED_STR),
username ? "OPERATOR=%s" : NULL, username);
(void) wall("System shutdown has been cancelled",
username, tty, logind_wall_tty_filter, m);
}
cancel_delayed_action(m);
manager_reset_scheduled_shutdown(m);
return sd_bus_reply_method_return(message, "b", true);
}
static int method_can_shutdown_or_sleep(
Manager *m,
sd_bus_message *message,
HandleAction action,
sd_bus_error *error) {
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
bool multiple_sessions, challenge, blocked, check_unit_state = true;
const HandleActionData *a;
uid_t uid;
int r;
assert(m);
assert(message);
assert(HANDLE_ACTION_IS_SHUTDOWN(action) || HANDLE_ACTION_IS_SLEEP(action));
if (action == HANDLE_SLEEP) {
HandleAction selected;
selected = handle_action_sleep_select(m);
if (selected < 0)
return sd_bus_reply_method_return(message, "s", "na");
check_unit_state = false; /* Already handled by handle_action_sleep_select */
assert_se(a = handle_action_lookup(selected));
} else if (HANDLE_ACTION_IS_SLEEP(action)) {
SleepSupport support;
assert_se(a = handle_action_lookup(action));
assert(a->sleep_operation >= 0);
assert(a->sleep_operation < _SLEEP_OPERATION_MAX);
r = sleep_supported_full(a->sleep_operation, &support);
if (r < 0)
return r;
if (r == 0)
return sd_bus_reply_method_return(message, "s", support == SLEEP_DISABLED ? "no" : "na");
} else
assert_se(a = handle_action_lookup(action));
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
if (r < 0)
return r;
r = sd_bus_creds_get_euid(creds, &uid);
if (r < 0)
return r;
r = manager_have_multiple_sessions(m, uid);
if (r < 0)
return r;
multiple_sessions = r > 0;
blocked = manager_is_inhibited(m, a->inhibit_what, NULL, /* flags= */ 0, uid, NULL);
if (check_unit_state && a->target) {
_cleanup_free_ char *load_state = NULL;
r = unit_load_state(m->bus, a->target, &load_state);
if (r < 0)
return r;
if (!streq(load_state, "loaded"))
return sd_bus_reply_method_return(message, "s", "no");
}
const char *result;
r = bus_test_polkit(
message,
a->polkit_action,
/* details= */ NULL,
/* good_user= */ UID_INVALID,
&challenge,
error);
if (r < 0)
return r;
if (r > 0)
result = "yes";
else if (challenge)
result = "challenge";
else
result = "no";
if (multiple_sessions) {
r = bus_test_polkit(
message,
a->polkit_action_multiple_sessions,
/* details= */ NULL,
/* good_user= */ UID_INVALID,
&challenge,
error);
if (r < 0)
return r;
if (r == 0) {
if (challenge) {
if (streq(result, "yes")) /* Avoid upgrading no -> challenge */
result = "challenge";
} else
result = "no";
}
}
if (blocked) {
r = bus_test_polkit(
message,
a->polkit_action_ignore_inhibit,
/* details= */ NULL,
/* good_user= */ UID_INVALID,
&challenge,
error);
if (r < 0)
return r;
if (r == 0) {
if (challenge) {
if (streq(result, "yes"))
result = "inhibited";
/* If result is already "challenge" or "no", the held inhibitor has no effect */
} else {
if (streq(result, "yes"))
result = "inhibitor-blocked";
else if (streq(result, "challenge"))
result = "challenge-inhibitor-blocked";
/* If the result is already "no", the held inhibitor has no effect */
}
}
}
return sd_bus_reply_method_return(message, "s", result);
}
static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(m, message, HANDLE_POWEROFF, error);
}
static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(m, message, HANDLE_REBOOT, error);
}
static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(m, message, HANDLE_HALT, error);
}
static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(m, message, HANDLE_SUSPEND, error);
}
static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(m, message, HANDLE_HIBERNATE, error);
}
static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(m, message, HANDLE_HYBRID_SLEEP, error);
}
static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(m, message, HANDLE_SUSPEND_THEN_HIBERNATE, error);
}
static int method_can_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
return method_can_shutdown_or_sleep(m, message, HANDLE_SLEEP, error);
}
static int property_get_reboot_parameter(
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 *parameter = NULL;
int r;
assert(bus);
assert(reply);
assert(userdata);
r = read_reboot_parameter(¶meter);
if (r < 0)
return r;
return sd_bus_message_append(reply, "s", parameter);
}
static int method_set_reboot_parameter(
sd_bus_message *message,
void *userdata,
sd_bus_error *error) {
Manager *m = ASSERT_PTR(userdata);
const char *arg;
int r;
assert(message);
r = sd_bus_message_read(message, "s", &arg);
if (r < 0)
return r;
if (!reboot_parameter_is_valid(arg))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid reboot parameter '%s'.", arg);
r = detect_container();
if (r < 0)
return r;
if (r > 0)
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED,