chiark / gitweb /
Prep v231.2: Add mor debug messages to find out, why 'loginctl suspend' isn't working.
[elogind.git] / src / login / elogind-action.c
1 /***
2   This file is part of elogind.
3
4   Copyright 2017 Sven Eden
5
6   elogind 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   elogind 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 elogind; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20
21 #include "elogind-action.h"
22 #include "fd-util.h"
23 #include "process-util.h"
24 #include "sleep.h"
25 #include "sleep-config.h"
26
27
28 static int run_helper(const char *helper) {
29         int pid = fork();
30         if (pid < 0) {
31                 return log_error_errno(errno, "Failed to fork: %m");
32         }
33
34         if (pid == 0) {
35                 /* Child */
36
37                 close_all_fds(NULL, 0);
38
39                 execlp(helper, helper, NULL);
40                 log_error_errno(errno, "Failed to execute %s: %m", helper);
41                 _exit(EXIT_FAILURE);
42         }
43
44         return wait_for_terminate_and_warn(helper, pid, true);
45 }
46
47 int shutdown_or_sleep(Manager *m, HandleAction action) {
48
49         assert(m);
50
51         switch (action) {
52         case HANDLE_POWEROFF:
53                 return run_helper(HALT);
54         case HANDLE_REBOOT:
55                 return run_helper(REBOOT);
56         case HANDLE_HALT:
57                 return run_helper(HALT);
58         case HANDLE_KEXEC:
59                 return run_helper(KEXEC);
60         case HANDLE_SUSPEND:
61                 return do_sleep("suspend", m->suspend_mode, m->suspend_state);
62         case HANDLE_HIBERNATE:
63                 return do_sleep("hibernate", m->hibernate_mode, m->hibernate_state);
64         case HANDLE_HYBRID_SLEEP:
65                 return do_sleep("hybrid-sleep", m->hybrid_sleep_mode, m->hybrid_sleep_state);
66         default:
67                 return -EINVAL;
68         }
69 }