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