chiark / gitweb /
Prep v229: Consolidate system sleep functions.
[elogind.git] / src / login / logind-action.c
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
20 #include <unistd.h>
21
22 #include "alloc-util.h"
23 #include "bus-error.h"
24 #include "bus-util.h"
25 #include "conf-parser.h"
26 #include "formats-util.h"
27 #include "logind-action.h"
28 #include "process-util.h"
29 //#include "sleep-config.h"
30 //#include "special.h"
31 #include "string-table.h"
32 #include "terminal-util.h"
33 #include "user-util.h"
34
35 // Additional includes needed by elogind
36 #include "fd-util.h"
37 #include "fileio.h"
38 #include "sd-messages.h"
39 #include "strv.h"
40
41
42 int manager_handle_action(
43                 Manager *m,
44                 InhibitWhat inhibit_key,
45                 HandleAction handle,
46                 bool ignore_inhibited,
47                 bool is_edge) {
48
49         static const char * const message_table[_HANDLE_ACTION_MAX] = {
50                 [HANDLE_POWEROFF] = "Powering Off...",
51                 [HANDLE_REBOOT] = "Rebooting...",
52                 [HANDLE_HALT] = "Halting...",
53                 [HANDLE_KEXEC] = "Rebooting via kexec...",
54                 [HANDLE_SUSPEND] = "Suspending...",
55                 [HANDLE_HIBERNATE] = "Hibernating...",
56                 [HANDLE_HYBRID_SLEEP] = "Hibernating and suspending..."
57         };
58
59 #if 0 /// elogind does this itself. No target table required
60         static const char * const target_table[_HANDLE_ACTION_MAX] = {
61                 [HANDLE_POWEROFF] = SPECIAL_POWEROFF_TARGET,
62                 [HANDLE_REBOOT] = SPECIAL_REBOOT_TARGET,
63                 [HANDLE_HALT] = SPECIAL_HALT_TARGET,
64                 [HANDLE_KEXEC] = SPECIAL_KEXEC_TARGET,
65                 [HANDLE_SUSPEND] = SPECIAL_SUSPEND_TARGET,
66                 [HANDLE_HIBERNATE] = SPECIAL_HIBERNATE_TARGET,
67                 [HANDLE_HYBRID_SLEEP] = SPECIAL_HYBRID_SLEEP_TARGET
68         };
69 #endif // 0
70
71         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
72         InhibitWhat inhibit_operation;
73         Inhibitor *offending = NULL;
74         bool supported;
75         int r;
76
77         assert(m);
78
79         /* If the key handling is turned off, don't do anything */
80         if (handle == HANDLE_IGNORE) {
81                 log_debug("Refusing operation, as it is turned off.");
82                 return 0;
83         }
84
85         if (inhibit_key == INHIBIT_HANDLE_LID_SWITCH) {
86                 /* If the last system suspend or startup is too close,
87                  * let's not suspend for now, to give USB docking
88                  * stations some time to settle so that we can
89                  * properly watch its displays. */
90                 if (m->lid_switch_ignore_event_source) {
91                         log_debug("Ignoring lid switch request, system startup or resume too close.");
92                         return 0;
93                 }
94         }
95
96         /* If the key handling is inhibited, don't do anything */
97         if (inhibit_key > 0) {
98                 if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0, NULL)) {
99                         log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_key));
100                         return 0;
101                 }
102         }
103
104         /* Locking is handled differently from the rest. */
105         if (handle == HANDLE_LOCK) {
106
107                 if (!is_edge)
108                         return 0;
109
110                 log_info("Locking sessions...");
111                 session_send_lock_all(m, true);
112                 return 1;
113         }
114
115         if (handle == HANDLE_SUSPEND)
116                 supported = can_sleep(m, "suspend") > 0;
117         else if (handle == HANDLE_HIBERNATE)
118                 supported = can_sleep(m, "hibernate") > 0;
119         else if (handle == HANDLE_HYBRID_SLEEP)
120                 supported = can_sleep(m, "hybrid-sleep") > 0;
121         else if (handle == HANDLE_KEXEC)
122                 supported = access(KEXEC, X_OK) >= 0;
123         else
124                 supported = true;
125
126         if (!supported) {
127                 log_warning("Requested operation not supported, ignoring.");
128                 return -EOPNOTSUPP;
129         }
130
131         if (m->action_what) {
132                 log_debug("Action already in progress, ignoring.");
133                 return -EALREADY;
134         }
135
136         inhibit_operation = handle == HANDLE_SUSPEND || handle == HANDLE_HIBERNATE || handle == HANDLE_HYBRID_SLEEP ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN;
137
138         /* If the actual operation is inhibited, warn and fail */
139         if (!ignore_inhibited &&
140             manager_is_inhibited(m, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0, &offending)) {
141                 _cleanup_free_ char *comm = NULL, *u = NULL;
142
143                 get_process_comm(offending->pid, &comm);
144                 u = uid_to_name(offending->uid);
145
146                 /* If this is just a recheck of the lid switch then don't warn about anything */
147                 if (!is_edge) {
148                         log_debug("Refusing operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.",
149                                   inhibit_what_to_string(inhibit_operation),
150                                   offending->uid, strna(u),
151                                   offending->pid, strna(comm));
152                         return 0;
153                 }
154
155                 log_error("Refusing operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.",
156                           inhibit_what_to_string(inhibit_operation),
157                           offending->uid, strna(u),
158                           offending->pid, strna(comm));
159
160                 return -EPERM;
161         }
162
163         log_info("%s", message_table[handle]);
164
165 #if 0 /// elogind uses its own variant, which can use the handle directly.
166         r = bus_manager_shutdown_or_sleep_now_or_later(m, target_table[handle], inhibit_operation, &error);
167 #else
168         r = bus_manager_shutdown_or_sleep_now_or_later(m, handle, inhibit_operation, &error);
169 #endif // 0
170         if (r < 0) {
171                 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
172                 return r;
173         }
174
175         return 1;
176 }
177
178 static int run_helper(const char *helper) {
179         int pid = fork();
180         if (pid < 0) {
181                 return log_error_errno(errno, "Failed to fork: %m");
182         }
183
184         if (pid == 0) {
185                 /* Child */
186
187                 close_all_fds(NULL, 0);
188
189                 execlp(helper, helper, NULL);
190                 log_error_errno(errno, "Failed to execute %s: %m", helper);
191                 _exit(EXIT_FAILURE);
192         }
193
194         return wait_for_terminate_and_warn(helper, pid, true);
195 }
196
197 int shutdown_or_sleep(Manager *m, HandleAction action) {
198
199         assert(m);
200
201         switch (action) {
202         case HANDLE_POWEROFF:
203                 return run_helper(HALT);
204         case HANDLE_REBOOT:
205                 return run_helper(REBOOT);
206         case HANDLE_HALT:
207                 return run_helper(HALT);
208         case HANDLE_KEXEC:
209                 return run_helper(KEXEC);
210         case HANDLE_SUSPEND:
211                 return do_sleep("suspend", m->suspend_mode, m->suspend_state);
212         case HANDLE_HIBERNATE:
213                 return do_sleep("hibernate", m->hibernate_mode, m->hibernate_state);
214         case HANDLE_HYBRID_SLEEP:
215                 return do_sleep("hybrid-sleep", m->hybrid_sleep_mode, m->hybrid_sleep_state);
216         default:
217                 return -EINVAL;
218         }
219 }
220
221 static const char* const handle_action_table[_HANDLE_ACTION_MAX] = {
222         [HANDLE_IGNORE] = "ignore",
223         [HANDLE_POWEROFF] = "poweroff",
224         [HANDLE_REBOOT] = "reboot",
225         [HANDLE_HALT] = "halt",
226         [HANDLE_KEXEC] = "kexec",
227         [HANDLE_SUSPEND] = "suspend",
228         [HANDLE_HIBERNATE] = "hibernate",
229         [HANDLE_HYBRID_SLEEP] = "hybrid-sleep",
230         [HANDLE_LOCK] = "lock"
231 };
232
233 DEFINE_STRING_TABLE_LOOKUP(handle_action, HandleAction);
234 DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_action, handle_action, HandleAction, "Failed to parse handle action setting");