]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
23406ce5 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2012 Lennart Poettering | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. | |
19 | ***/ | |
20 | ||
dc3a1b76 LP |
21 | #include <unistd.h> |
22 | ||
b5efdb8a | 23 | #include "alloc-util.h" |
cc377381 | 24 | #include "bus-error.h" |
8b43440b LP |
25 | #include "bus-util.h" |
26 | #include "conf-parser.h" | |
f97b34a6 | 27 | #include "format-util.h" |
8b43440b | 28 | #include "logind-action.h" |
0b452006 | 29 | #include "process-util.h" |
8b43440b LP |
30 | #include "sleep-config.h" |
31 | #include "special.h" | |
32 | #include "string-table.h" | |
288a74cc | 33 | #include "terminal-util.h" |
b1d4f8e1 | 34 | #include "user-util.h" |
23406ce5 LP |
35 | |
36 | int manager_handle_action( | |
37 | Manager *m, | |
38 | InhibitWhat inhibit_key, | |
39 | HandleAction handle, | |
40 | bool ignore_inhibited, | |
41 | bool is_edge) { | |
42 | ||
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 | }; | |
52 | ||
53 | static const char * const target_table[_HANDLE_ACTION_MAX] = { | |
54 | [HANDLE_POWEROFF] = SPECIAL_POWEROFF_TARGET, | |
55 | [HANDLE_REBOOT] = SPECIAL_REBOOT_TARGET, | |
56 | [HANDLE_HALT] = SPECIAL_HALT_TARGET, | |
57 | [HANDLE_KEXEC] = SPECIAL_KEXEC_TARGET, | |
58 | [HANDLE_SUSPEND] = SPECIAL_SUSPEND_TARGET, | |
59 | [HANDLE_HIBERNATE] = SPECIAL_HIBERNATE_TARGET, | |
60 | [HANDLE_HYBRID_SLEEP] = SPECIAL_HYBRID_SLEEP_TARGET | |
61 | }; | |
62 | ||
4afd3348 | 63 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
23406ce5 | 64 | InhibitWhat inhibit_operation; |
85a428c6 | 65 | Inhibitor *offending = NULL; |
af9792ac | 66 | bool supported; |
cc377381 | 67 | int r; |
23406ce5 LP |
68 | |
69 | assert(m); | |
70 | ||
71 | /* If the key handling is turned off, don't do anything */ | |
72 | if (handle == HANDLE_IGNORE) { | |
73 | log_debug("Refusing operation, as it is turned off."); | |
74 | return 0; | |
75 | } | |
76 | ||
2d62c530 | 77 | if (inhibit_key == INHIBIT_HANDLE_LID_SWITCH) { |
f9cd6be1 LP |
78 | /* If the last system suspend or startup is too close, |
79 | * let's not suspend for now, to give USB docking | |
80 | * stations some time to settle so that we can | |
81 | * properly watch its displays. */ | |
82 | if (m->lid_switch_ignore_event_source) { | |
83 | log_debug("Ignoring lid switch request, system startup or resume too close."); | |
84 | return 0; | |
85 | } | |
2d62c530 LP |
86 | } |
87 | ||
cc377381 | 88 | /* If the key handling is inhibited, don't do anything */ |
06a70b91 | 89 | if (inhibit_key > 0) { |
85a428c6 | 90 | if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0, NULL)) { |
cc377381 LP |
91 | log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_key)); |
92 | return 0; | |
93 | } | |
94 | } | |
95 | ||
96 | /* Locking is handled differently from the rest. */ | |
97 | if (handle == HANDLE_LOCK) { | |
ed4ba7e4 LP |
98 | |
99 | if (!is_edge) | |
100 | return 0; | |
101 | ||
cc377381 LP |
102 | log_info("Locking sessions..."); |
103 | session_send_lock_all(m, true); | |
104 | return 1; | |
105 | } | |
106 | ||
dc3a1b76 | 107 | if (handle == HANDLE_SUSPEND) |
19adb8a3 | 108 | supported = can_sleep("suspend") > 0; |
dc3a1b76 | 109 | else if (handle == HANDLE_HIBERNATE) |
19adb8a3 | 110 | supported = can_sleep("hibernate") > 0; |
dc3a1b76 | 111 | else if (handle == HANDLE_HYBRID_SLEEP) |
19adb8a3 | 112 | supported = can_sleep("hybrid-sleep") > 0; |
dc3a1b76 | 113 | else if (handle == HANDLE_KEXEC) |
78013564 | 114 | supported = access(KEXEC, X_OK) >= 0; |
af9792ac LP |
115 | else |
116 | supported = true; | |
dc3a1b76 LP |
117 | |
118 | if (!supported) { | |
119 | log_warning("Requested operation not supported, ignoring."); | |
15411c0c | 120 | return -EOPNOTSUPP; |
dc3a1b76 LP |
121 | } |
122 | ||
cc377381 LP |
123 | if (m->action_what) { |
124 | log_debug("Action already in progress, ignoring."); | |
125 | return -EALREADY; | |
23406ce5 LP |
126 | } |
127 | ||
a1a8e4f5 | 128 | inhibit_operation = IN_SET(handle, HANDLE_SUSPEND, HANDLE_HIBERNATE, HANDLE_HYBRID_SLEEP) ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN; |
23406ce5 LP |
129 | |
130 | /* If the actual operation is inhibited, warn and fail */ | |
131 | if (!ignore_inhibited && | |
85a428c6 LP |
132 | manager_is_inhibited(m, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0, &offending)) { |
133 | _cleanup_free_ char *comm = NULL, *u = NULL; | |
134 | ||
135 | get_process_comm(offending->pid, &comm); | |
136 | u = uid_to_name(offending->uid); | |
23406ce5 LP |
137 | |
138 | /* If this is just a recheck of the lid switch then don't warn about anything */ | |
139 | if (!is_edge) { | |
de0671ee | 140 | log_debug("Refusing operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.", |
85a428c6 | 141 | inhibit_what_to_string(inhibit_operation), |
de0671ee ZJS |
142 | offending->uid, strna(u), |
143 | offending->pid, strna(comm)); | |
23406ce5 LP |
144 | return 0; |
145 | } | |
146 | ||
de0671ee | 147 | log_error("Refusing operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.", |
85a428c6 | 148 | inhibit_what_to_string(inhibit_operation), |
de0671ee ZJS |
149 | offending->uid, strna(u), |
150 | offending->pid, strna(comm)); | |
85a428c6 | 151 | |
23406ce5 LP |
152 | return -EPERM; |
153 | } | |
154 | ||
155 | log_info("%s", message_table[handle]); | |
156 | ||
23406ce5 LP |
157 | r = bus_manager_shutdown_or_sleep_now_or_later(m, target_table[handle], inhibit_operation, &error); |
158 | if (r < 0) { | |
cc377381 | 159 | log_error("Failed to execute operation: %s", bus_error_message(&error, r)); |
23406ce5 LP |
160 | return r; |
161 | } | |
162 | ||
163 | return 1; | |
164 | } | |
165 | ||
166 | static const char* const handle_action_table[_HANDLE_ACTION_MAX] = { | |
167 | [HANDLE_IGNORE] = "ignore", | |
168 | [HANDLE_POWEROFF] = "poweroff", | |
169 | [HANDLE_REBOOT] = "reboot", | |
170 | [HANDLE_HALT] = "halt", | |
171 | [HANDLE_KEXEC] = "kexec", | |
172 | [HANDLE_SUSPEND] = "suspend", | |
173 | [HANDLE_HIBERNATE] = "hibernate", | |
174 | [HANDLE_HYBRID_SLEEP] = "hybrid-sleep", | |
175 | [HANDLE_LOCK] = "lock" | |
176 | }; | |
177 | ||
178 | DEFINE_STRING_TABLE_LOOKUP(handle_action, HandleAction); | |
179 | DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_action, handle_action, HandleAction, "Failed to parse handle action setting"); |