2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2012-12-23 22:32:48 +01:00
|
|
|
|
2012-12-23 22:12:01 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2013-11-05 01:10:21 +01:00
|
|
|
#include "bus-error.h"
|
2015-10-26 22:31:05 +01:00
|
|
|
#include "bus-util.h"
|
|
|
|
#include "conf-parser.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2015-10-26 22:31:05 +01:00
|
|
|
#include "logind-action.h"
|
2019-04-30 15:05:14 +02:00
|
|
|
#include "logind-dbus.h"
|
|
|
|
#include "logind-session-dbus.h"
|
2015-04-10 19:10:00 +02:00
|
|
|
#include "process-util.h"
|
2015-10-26 22:31:05 +01:00
|
|
|
#include "sleep-config.h"
|
|
|
|
#include "special.h"
|
|
|
|
#include "string-table.h"
|
2015-04-10 23:15:59 +02:00
|
|
|
#include "terminal-util.h"
|
2015-10-25 22:32:30 +01:00
|
|
|
#include "user-util.h"
|
2012-12-23 22:32:48 +01:00
|
|
|
|
2018-04-10 13:15:00 +02:00
|
|
|
const char* manager_target_for_action(HandleAction handle) {
|
|
|
|
static const char * const target_table[_HANDLE_ACTION_MAX] = {
|
|
|
|
[HANDLE_POWEROFF] = SPECIAL_POWEROFF_TARGET,
|
|
|
|
[HANDLE_REBOOT] = SPECIAL_REBOOT_TARGET,
|
|
|
|
[HANDLE_HALT] = SPECIAL_HALT_TARGET,
|
|
|
|
[HANDLE_KEXEC] = SPECIAL_KEXEC_TARGET,
|
|
|
|
[HANDLE_SUSPEND] = SPECIAL_SUSPEND_TARGET,
|
|
|
|
[HANDLE_HIBERNATE] = SPECIAL_HIBERNATE_TARGET,
|
|
|
|
[HANDLE_HYBRID_SLEEP] = SPECIAL_HYBRID_SLEEP_TARGET,
|
|
|
|
[HANDLE_SUSPEND_THEN_HIBERNATE] = SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET,
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(handle >= 0);
|
|
|
|
if (handle < (ssize_t) ELEMENTSOF(target_table))
|
|
|
|
return target_table[handle];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-23 22:32:48 +01:00
|
|
|
int manager_handle_action(
|
|
|
|
Manager *m,
|
|
|
|
InhibitWhat inhibit_key,
|
|
|
|
HandleAction handle,
|
|
|
|
bool ignore_inhibited,
|
|
|
|
bool is_edge) {
|
|
|
|
|
|
|
|
static const char * const message_table[_HANDLE_ACTION_MAX] = {
|
|
|
|
[HANDLE_POWEROFF] = "Powering Off...",
|
|
|
|
[HANDLE_REBOOT] = "Rebooting...",
|
|
|
|
[HANDLE_HALT] = "Halting...",
|
|
|
|
[HANDLE_KEXEC] = "Rebooting via kexec...",
|
|
|
|
[HANDLE_SUSPEND] = "Suspending...",
|
|
|
|
[HANDLE_HIBERNATE] = "Hibernating...",
|
2018-03-08 14:17:33 +01:00
|
|
|
[HANDLE_HYBRID_SLEEP] = "Hibernating and suspending...",
|
2018-03-28 18:00:06 +02:00
|
|
|
[HANDLE_SUSPEND_THEN_HIBERNATE] = "Suspending, then hibernating...",
|
2012-12-23 22:32:48 +01:00
|
|
|
};
|
|
|
|
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
2012-12-23 22:32:48 +01:00
|
|
|
InhibitWhat inhibit_operation;
|
2013-11-27 02:38:06 +01:00
|
|
|
Inhibitor *offending = NULL;
|
2013-01-24 04:56:44 +01:00
|
|
|
bool supported;
|
2018-04-10 13:15:00 +02:00
|
|
|
const char *target;
|
2013-11-05 01:10:21 +01:00
|
|
|
int r;
|
2012-12-23 22:32:48 +01:00
|
|
|
|
|
|
|
assert(m);
|
2019-08-03 22:43:34 +02:00
|
|
|
|
|
|
|
/* If the key handling is turned off, don't do anything */
|
|
|
|
if (handle == HANDLE_IGNORE) {
|
|
|
|
log_debug("Refusing operation, as it is turned off.");
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-23 22:32:48 +01:00
|
|
|
|
2014-02-24 16:22:23 +01:00
|
|
|
if (inhibit_key == INHIBIT_HANDLE_LID_SWITCH) {
|
2014-03-03 20:49:33 +01:00
|
|
|
/* If the last system suspend or startup is too close,
|
|
|
|
* let's not suspend for now, to give USB docking
|
|
|
|
* stations some time to settle so that we can
|
|
|
|
* properly watch its displays. */
|
|
|
|
if (m->lid_switch_ignore_event_source) {
|
|
|
|
log_debug("Ignoring lid switch request, system startup or resume too close.");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-24 16:22:23 +01:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
/* If the key handling is inhibited, don't do anything */
|
2016-08-08 10:07:38 +02:00
|
|
|
if (inhibit_key > 0) {
|
2013-11-27 02:38:06 +01:00
|
|
|
if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0, NULL)) {
|
2019-06-13 18:11:56 +02:00
|
|
|
log_debug("Refusing %s operation, %s is inhibited.",
|
|
|
|
handle_action_to_string(handle),
|
|
|
|
inhibit_what_to_string(inhibit_key));
|
2013-11-05 01:10:21 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Locking is handled differently from the rest. */
|
|
|
|
if (handle == HANDLE_LOCK) {
|
2014-02-21 21:10:00 +01:00
|
|
|
if (!is_edge)
|
|
|
|
return 0;
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
log_info("Locking sessions...");
|
|
|
|
session_send_lock_all(m, true);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-12-23 22:12:01 +01:00
|
|
|
if (handle == HANDLE_SUSPEND)
|
2013-05-04 18:31:28 +02:00
|
|
|
supported = can_sleep("suspend") > 0;
|
2012-12-23 22:12:01 +01:00
|
|
|
else if (handle == HANDLE_HIBERNATE)
|
2013-05-04 18:31:28 +02:00
|
|
|
supported = can_sleep("hibernate") > 0;
|
2012-12-23 22:12:01 +01:00
|
|
|
else if (handle == HANDLE_HYBRID_SLEEP)
|
2013-05-04 18:31:28 +02:00
|
|
|
supported = can_sleep("hybrid-sleep") > 0;
|
2018-03-28 18:00:06 +02:00
|
|
|
else if (handle == HANDLE_SUSPEND_THEN_HIBERNATE)
|
|
|
|
supported = can_sleep("suspend-then-hibernate") > 0;
|
2012-12-23 22:12:01 +01:00
|
|
|
else if (handle == HANDLE_KEXEC)
|
2013-07-16 05:04:52 +02:00
|
|
|
supported = access(KEXEC, X_OK) >= 0;
|
2013-01-24 04:56:44 +01:00
|
|
|
else
|
|
|
|
supported = true;
|
2012-12-23 22:12:01 +01:00
|
|
|
|
2018-11-16 21:44:36 +01:00
|
|
|
if (!supported && IN_SET(handle, HANDLE_HIBERNATE, HANDLE_HYBRID_SLEEP, HANDLE_SUSPEND_THEN_HIBERNATE)) {
|
|
|
|
supported = can_sleep("suspend") > 0;
|
|
|
|
if (supported) {
|
2019-06-13 18:11:56 +02:00
|
|
|
log_notice("Requested %s operation is not supported, using regular suspend instead.",
|
|
|
|
handle_action_to_string(handle));
|
2018-11-16 21:44:36 +01:00
|
|
|
handle = HANDLE_SUSPEND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 18:11:56 +02:00
|
|
|
if (!supported)
|
|
|
|
return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
|
|
|
|
"Requested %s operation not supported, ignoring.", handle_action_to_string(handle));
|
2012-12-23 22:12:01 +01:00
|
|
|
|
2019-06-13 18:11:56 +02:00
|
|
|
if (m->action_what > 0)
|
|
|
|
return log_debug_errno(SYNTHETIC_ERRNO(EALREADY),
|
|
|
|
"Action already in progress (%s), ignoring requested %s operation.",
|
|
|
|
inhibit_what_to_string(m->action_what),
|
|
|
|
handle_action_to_string(handle));
|
2012-12-23 22:32:48 +01:00
|
|
|
|
2018-04-10 13:15:00 +02:00
|
|
|
assert_se(target = manager_target_for_action(handle));
|
|
|
|
|
2018-03-08 14:17:33 +01:00
|
|
|
inhibit_operation = IN_SET(handle, HANDLE_SUSPEND, HANDLE_HIBERNATE,
|
|
|
|
HANDLE_HYBRID_SLEEP,
|
2018-03-28 18:00:06 +02:00
|
|
|
HANDLE_SUSPEND_THEN_HIBERNATE) ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN;
|
2012-12-23 22:32:48 +01:00
|
|
|
|
|
|
|
/* If the actual operation is inhibited, warn and fail */
|
|
|
|
if (!ignore_inhibited &&
|
2013-11-27 02:38:06 +01:00
|
|
|
manager_is_inhibited(m, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0, &offending)) {
|
|
|
|
_cleanup_free_ char *comm = NULL, *u = NULL;
|
|
|
|
|
2018-11-16 21:46:49 +01:00
|
|
|
(void) get_process_comm(offending->pid, &comm);
|
2013-11-27 02:38:06 +01:00
|
|
|
u = uid_to_name(offending->uid);
|
2012-12-23 22:32:48 +01:00
|
|
|
|
|
|
|
/* If this is just a recheck of the lid switch then don't warn about anything */
|
2019-06-13 18:11:56 +02:00
|
|
|
log_full(is_edge ? LOG_ERR : LOG_DEBUG,
|
|
|
|
"Refusing %s operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.",
|
|
|
|
handle_action_to_string(handle),
|
|
|
|
inhibit_what_to_string(inhibit_operation),
|
|
|
|
offending->uid, strna(u),
|
|
|
|
offending->pid, strna(comm));
|
|
|
|
|
|
|
|
return is_edge ? -EPERM : 0;
|
2012-12-23 22:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log_info("%s", message_table[handle]);
|
|
|
|
|
2018-04-10 13:15:00 +02:00
|
|
|
r = bus_manager_shutdown_or_sleep_now_or_later(m, target, inhibit_operation, &error);
|
2018-08-07 03:14:30 +02:00
|
|
|
if (r < 0)
|
2019-06-13 18:11:56 +02:00
|
|
|
return log_error_errno(r, "Failed to execute %s operation: %s",
|
|
|
|
handle_action_to_string(handle),
|
|
|
|
bus_error_message(&error, r));
|
2012-12-23 22:32:48 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* const handle_action_table[_HANDLE_ACTION_MAX] = {
|
|
|
|
[HANDLE_IGNORE] = "ignore",
|
|
|
|
[HANDLE_POWEROFF] = "poweroff",
|
|
|
|
[HANDLE_REBOOT] = "reboot",
|
|
|
|
[HANDLE_HALT] = "halt",
|
|
|
|
[HANDLE_KEXEC] = "kexec",
|
|
|
|
[HANDLE_SUSPEND] = "suspend",
|
|
|
|
[HANDLE_HIBERNATE] = "hibernate",
|
|
|
|
[HANDLE_HYBRID_SLEEP] = "hybrid-sleep",
|
2018-03-28 18:00:06 +02:00
|
|
|
[HANDLE_SUSPEND_THEN_HIBERNATE] = "suspend-then-hibernate",
|
2018-04-10 13:15:00 +02:00
|
|
|
[HANDLE_LOCK] = "lock",
|
2012-12-23 22:32:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_STRING_TABLE_LOOKUP(handle_action, HandleAction);
|
|
|
|
DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_action, handle_action, HandleAction, "Failed to parse handle action setting");
|