chiark / gitweb /
shared: add process-util.[ch]
[elogind.git] / src / login / logind-action.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2012 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <errno.h>
25
26 #include "sd-messages.h"
27 #include "log.h"
28 #include "util.h"
29 #include "strv.h"
30 #include "fileio.h"
31 #include "build.h"
32 #include "def.h"
33 #include "conf-parser.h"
34 #include "sleep-config.h"
35 #include "bus-error.h"
36 #include "bus-util.h"
37 #include "logind-action.h"
38 #include "formats-util.h"
39 #include "process-util.h"
40
41 int manager_handle_action(
42                 Manager *m,
43                 InhibitWhat inhibit_key,
44                 HandleAction handle,
45                 bool ignore_inhibited,
46                 bool is_edge) {
47
48         static const char * const message_table[_HANDLE_ACTION_MAX] = {
49                 [HANDLE_POWEROFF] = "Powering Off...",
50                 [HANDLE_REBOOT] = "Rebooting...",
51                 [HANDLE_HALT] = "Halting...",
52                 [HANDLE_KEXEC] = "Rebooting via kexec...",
53                 [HANDLE_SUSPEND] = "Suspending...",
54                 [HANDLE_HIBERNATE] = "Hibernating...",
55                 [HANDLE_HYBRID_SLEEP] = "Hibernating and suspending..."
56         };
57
58         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
59         InhibitWhat inhibit_operation;
60         Inhibitor *offending = NULL;
61         bool supported;
62         int r;
63
64         assert(m);
65
66         /* If the key handling is turned off, don't do anything */
67         if (handle == HANDLE_IGNORE) {
68                 log_debug("Refusing operation, as it is turned off.");
69                 return 0;
70         }
71
72         if (inhibit_key == INHIBIT_HANDLE_LID_SWITCH) {
73                 /* If the last system suspend or startup is too close,
74                  * let's not suspend for now, to give USB docking
75                  * stations some time to settle so that we can
76                  * properly watch its displays. */
77                 if (m->lid_switch_ignore_event_source) {
78                         log_debug("Ignoring lid switch request, system startup or resume too close.");
79                         return 0;
80                 }
81         }
82
83         /* If the key handling is inhibited, don't do anything */
84         if (inhibit_key > 0) {
85                 if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0, NULL)) {
86                         log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_key));
87                         return 0;
88                 }
89         }
90
91         /* Locking is handled differently from the rest. */
92         if (handle == HANDLE_LOCK) {
93
94                 if (!is_edge)
95                         return 0;
96
97                 log_info("Locking sessions...");
98                 session_send_lock_all(m, true);
99                 return 1;
100         }
101
102         if (handle == HANDLE_SUSPEND)
103                 supported = can_sleep("suspend") > 0;
104         else if (handle == HANDLE_HIBERNATE)
105                 supported = can_sleep("hibernate") > 0;
106         else if (handle == HANDLE_HYBRID_SLEEP)
107                 supported = can_sleep("hybrid-sleep") > 0;
108         else if (handle == HANDLE_KEXEC)
109                 supported = access(KEXEC, X_OK) >= 0;
110         else
111                 supported = true;
112
113         if (!supported) {
114                 log_warning("Requested operation not supported, ignoring.");
115                 return -EOPNOTSUPP;
116         }
117
118         if (m->action_what) {
119                 log_debug("Action already in progress, ignoring.");
120                 return -EALREADY;
121         }
122
123         inhibit_operation = handle == HANDLE_SUSPEND || handle == HANDLE_HIBERNATE || handle == HANDLE_HYBRID_SLEEP ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN;
124
125         /* If the actual operation is inhibited, warn and fail */
126         if (!ignore_inhibited &&
127             manager_is_inhibited(m, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0, &offending)) {
128                 _cleanup_free_ char *comm = NULL, *u = NULL;
129
130                 get_process_comm(offending->pid, &comm);
131                 u = uid_to_name(offending->uid);
132
133                 /* If this is just a recheck of the lid switch then don't warn about anything */
134                 if (!is_edge) {
135                         log_debug("Refusing operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.",
136                                   inhibit_what_to_string(inhibit_operation),
137                                   offending->uid, strna(u),
138                                   offending->pid, strna(comm));
139                         return 0;
140                 }
141
142                 log_error("Refusing operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.",
143                           inhibit_what_to_string(inhibit_operation),
144                           offending->uid, strna(u),
145                           offending->pid, strna(comm));
146
147                 warn_melody();
148                 return -EPERM;
149         }
150
151         log_info("%s", message_table[handle]);
152
153         r = bus_manager_shutdown_or_sleep_now_or_later(m, handle, inhibit_operation, &error);
154         if (r < 0) {
155                 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
156                 return r;
157         }
158
159         return 1;
160 }
161
162 static int run_helper(const char *helper) {
163         int pid = fork();
164         if (pid < 0) {
165                 return log_error_errno(errno, "Failed to fork: %m");
166         }
167
168         if (pid == 0) {
169                 /* Child */
170
171                 close_all_fds(NULL, 0);
172
173                 execlp(helper, helper, NULL);
174                 log_error_errno(errno, "Failed to execute %s: %m", helper);
175                 _exit(EXIT_FAILURE);
176         }
177
178         return wait_for_terminate_and_warn(helper, pid, true);
179 }
180
181 static int write_mode(char **modes) {
182         int r = 0;
183         char **mode;
184
185         STRV_FOREACH(mode, modes) {
186                 int k;
187
188                 k = write_string_file("/sys/power/disk", *mode);
189                 if (k == 0)
190                         return 0;
191
192                 log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m",
193                                 *mode);
194                 if (r == 0)
195                         r = k;
196         }
197
198         if (r < 0)
199                 log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");
200
201         return r;
202 }
203
204 static int write_state(FILE **f, char **states) {
205         char **state;
206         int r = 0;
207
208         STRV_FOREACH(state, states) {
209                 int k;
210
211                 k = write_string_stream(*f, *state);
212                 if (k == 0)
213                         return 0;
214                 log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m",
215                                 *state);
216                 if (r == 0)
217                         r = k;
218
219                 fclose(*f);
220                 *f = fopen("/sys/power/state", "we");
221                 if (!*f)
222                         return log_error_errno(errno, "Failed to open /sys/power/state: %m");
223         }
224
225         return r;
226 }
227
228 static int do_sleep(const char *arg_verb, const char **modes, const char **states) {
229         char *arguments[] = {
230                 NULL,
231                 (char*) "pre",
232                 (char*) arg_verb,
233                 NULL
234         };
235         static const char* const dirs[] = { SYSTEM_SLEEP_PATH, NULL};
236         int r;
237         _cleanup_fclose_ FILE *f = NULL;
238
239         /* This file is opened first, so that if we hit an error,
240          * we can abort before modifying any state. */
241         f = fopen("/sys/power/state", "we");
242         if (!f)
243                 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
244
245         /* Configure the hibernation mode */
246         r = write_mode(modes);
247         if (r < 0)
248                 return r;
249
250         execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments);
251
252         log_struct(LOG_INFO,
253                    LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START),
254                    LOG_MESSAGE("Suspending system..."),
255                    "SLEEP=%s", arg_verb,
256                    NULL);
257
258         r = write_state(&f, states);
259         if (r < 0)
260                 return r;
261
262         log_struct(LOG_INFO,
263                    LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_STOP),
264                    LOG_MESSAGE("System resumed."),
265                    "SLEEP=%s", arg_verb,
266                    NULL);
267
268         arguments[1] = (char*) "post";
269         execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments);
270
271         return r;
272 }
273
274 int shutdown_or_sleep(Manager *m, HandleAction action) {
275         switch (action) {
276         case HANDLE_POWEROFF:
277                 return run_helper(HALT);
278         case HANDLE_REBOOT:
279                 return run_helper(REBOOT);
280         case HANDLE_HALT:
281                 return run_helper(HALT);
282         case HANDLE_KEXEC:
283                 return run_helper(KEXEC);
284         case HANDLE_SUSPEND:
285                 return do_sleep("suspend", m->suspend_mode, m->suspend_state);
286         case HANDLE_HIBERNATE:
287                 return do_sleep("hibernate", m->hibernate_mode, m->hibernate_state);
288         case HANDLE_HYBRID_SLEEP:
289                 return do_sleep("hybrid-sleep", m->hybrid_sleep_mode, m->hybrid_sleep_state);
290         default:
291                 return -EINVAL;
292         }
293 }
294
295 static const char* const handle_action_table[_HANDLE_ACTION_MAX] = {
296         [HANDLE_IGNORE] = "ignore",
297         [HANDLE_POWEROFF] = "poweroff",
298         [HANDLE_REBOOT] = "reboot",
299         [HANDLE_HALT] = "halt",
300         [HANDLE_KEXEC] = "kexec",
301         [HANDLE_SUSPEND] = "suspend",
302         [HANDLE_HIBERNATE] = "hibernate",
303         [HANDLE_HYBRID_SLEEP] = "hybrid-sleep",
304         [HANDLE_LOCK] = "lock"
305 };
306
307 DEFINE_STRING_TABLE_LOOKUP(handle_action, HandleAction);
308 DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_action, handle_action, HandleAction, "Failed to parse handle action setting");