1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
24 #include "bus-error.h"
26 #include "conf-parser.h"
27 #include "format-util.h"
28 #include "logind-action.h"
29 #include "process-util.h"
30 #include "sleep-config.h"
32 #include "string-table.h"
33 #include "terminal-util.h"
34 #include "user-util.h"
36 int manager_handle_action(
38 InhibitWhat inhibit_key
,
40 bool ignore_inhibited
,
43 static const char * const message_table
[_HANDLE_ACTION_MAX
] = {
44 [HANDLE_POWEROFF
] = "Powering Off...",
45 [HANDLE_REBOOT
] = "Rebooting...",
46 [HANDLE_HALT
] = "Halting...",
47 [HANDLE_KEXEC
] = "Rebooting via kexec...",
48 [HANDLE_SUSPEND
] = "Suspending...",
49 [HANDLE_HIBERNATE
] = "Hibernating...",
50 [HANDLE_HYBRID_SLEEP
] = "Hibernating and suspending...",
51 [HANDLE_SUSPEND_TO_HIBERNATE
] = "Suspending to hibernate...",
54 static const char * const target_table
[_HANDLE_ACTION_MAX
] = {
55 [HANDLE_POWEROFF
] = SPECIAL_POWEROFF_TARGET
,
56 [HANDLE_REBOOT
] = SPECIAL_REBOOT_TARGET
,
57 [HANDLE_HALT
] = SPECIAL_HALT_TARGET
,
58 [HANDLE_KEXEC
] = SPECIAL_KEXEC_TARGET
,
59 [HANDLE_SUSPEND
] = SPECIAL_SUSPEND_TARGET
,
60 [HANDLE_HIBERNATE
] = SPECIAL_HIBERNATE_TARGET
,
61 [HANDLE_HYBRID_SLEEP
] = SPECIAL_HYBRID_SLEEP_TARGET
,
62 [HANDLE_SUSPEND_TO_HIBERNATE
] = SPECIAL_SUSPEND_TO_HIBERNATE_TARGET
,
65 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
66 InhibitWhat inhibit_operation
;
67 Inhibitor
*offending
= NULL
;
73 /* If the key handling is turned off, don't do anything */
74 if (handle
== HANDLE_IGNORE
) {
75 log_debug("Refusing operation, as it is turned off.");
79 if (inhibit_key
== INHIBIT_HANDLE_LID_SWITCH
) {
80 /* If the last system suspend or startup is too close,
81 * let's not suspend for now, to give USB docking
82 * stations some time to settle so that we can
83 * properly watch its displays. */
84 if (m
->lid_switch_ignore_event_source
) {
85 log_debug("Ignoring lid switch request, system startup or resume too close.");
90 /* If the key handling is inhibited, don't do anything */
91 if (inhibit_key
> 0) {
92 if (manager_is_inhibited(m
, inhibit_key
, INHIBIT_BLOCK
, NULL
, true, false, 0, NULL
)) {
93 log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_key
));
98 /* Locking is handled differently from the rest. */
99 if (handle
== HANDLE_LOCK
) {
104 log_info("Locking sessions...");
105 session_send_lock_all(m
, true);
109 if (handle
== HANDLE_SUSPEND
)
110 supported
= can_sleep("suspend") > 0;
111 else if (handle
== HANDLE_HIBERNATE
)
112 supported
= can_sleep("hibernate") > 0;
113 else if (handle
== HANDLE_HYBRID_SLEEP
)
114 supported
= can_sleep("hybrid-sleep") > 0;
115 else if (handle
== HANDLE_SUSPEND_TO_HIBERNATE
)
116 supported
= can_sleep("suspend-to-hibernate") > 0;
117 else if (handle
== HANDLE_KEXEC
)
118 supported
= access(KEXEC
, X_OK
) >= 0;
123 log_warning("Requested operation not supported, ignoring.");
127 if (m
->action_what
) {
128 log_debug("Action already in progress, ignoring.");
132 inhibit_operation
= IN_SET(handle
, HANDLE_SUSPEND
, HANDLE_HIBERNATE
,
134 HANDLE_SUSPEND_TO_HIBERNATE
) ? INHIBIT_SLEEP
: INHIBIT_SHUTDOWN
;
136 /* If the actual operation is inhibited, warn and fail */
137 if (!ignore_inhibited
&&
138 manager_is_inhibited(m
, inhibit_operation
, INHIBIT_BLOCK
, NULL
, false, false, 0, &offending
)) {
139 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
141 get_process_comm(offending
->pid
, &comm
);
142 u
= uid_to_name(offending
->uid
);
144 /* If this is just a recheck of the lid switch then don't warn about anything */
146 log_debug("Refusing operation, %s is inhibited by UID "UID_FMT
"/%s, PID "PID_FMT
"/%s.",
147 inhibit_what_to_string(inhibit_operation
),
148 offending
->uid
, strna(u
),
149 offending
->pid
, strna(comm
));
153 log_error("Refusing operation, %s is inhibited by UID "UID_FMT
"/%s, PID "PID_FMT
"/%s.",
154 inhibit_what_to_string(inhibit_operation
),
155 offending
->uid
, strna(u
),
156 offending
->pid
, strna(comm
));
161 log_info("%s", message_table
[handle
]);
163 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, target_table
[handle
], inhibit_operation
, &error
);
165 log_error("Failed to execute operation: %s", bus_error_message(&error
, r
));
172 static const char* const handle_action_table
[_HANDLE_ACTION_MAX
] = {
173 [HANDLE_IGNORE
] = "ignore",
174 [HANDLE_POWEROFF
] = "poweroff",
175 [HANDLE_REBOOT
] = "reboot",
176 [HANDLE_HALT
] = "halt",
177 [HANDLE_KEXEC
] = "kexec",
178 [HANDLE_SUSPEND
] = "suspend",
179 [HANDLE_HIBERNATE
] = "hibernate",
180 [HANDLE_HYBRID_SLEEP
] = "hybrid-sleep",
181 [HANDLE_SUSPEND_TO_HIBERNATE
] = "suspend-to-hibernate",
182 [HANDLE_LOCK
] = "lock"
185 DEFINE_STRING_TABLE_LOOKUP(handle_action
, HandleAction
);
186 DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_action
, handle_action
, HandleAction
, "Failed to parse handle action setting");