chiark / gitweb /
logind: don't hit assert when we try to free NULL manager object
[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 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 #if 0 /// elogind does this itself. No target table required
59         static const char * const target_table[_HANDLE_ACTION_MAX] = {
60                 [HANDLE_POWEROFF] = SPECIAL_POWEROFF_TARGET,
61                 [HANDLE_REBOOT] = SPECIAL_REBOOT_TARGET,
62                 [HANDLE_HALT] = SPECIAL_HALT_TARGET,
63                 [HANDLE_KEXEC] = SPECIAL_KEXEC_TARGET,
64                 [HANDLE_SUSPEND] = SPECIAL_SUSPEND_TARGET,
65                 [HANDLE_HIBERNATE] = SPECIAL_HIBERNATE_TARGET,
66                 [HANDLE_HYBRID_SLEEP] = SPECIAL_HYBRID_SLEEP_TARGET
67         };
68 #endif // 0
69
70         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
71         InhibitWhat inhibit_operation;
72         Inhibitor *offending = NULL;
73         bool supported;
74         int r;
75
76         assert(m);
77
78         /* If the key handling is turned off, don't do anything */
79         if (handle == HANDLE_IGNORE) {
80                 log_debug("Refusing operation, as it is turned off.");
81                 return 0;
82         }
83
84         if (inhibit_key == INHIBIT_HANDLE_LID_SWITCH) {
85                 /* If the last system suspend or startup is too close,
86                  * let's not suspend for now, to give USB docking
87                  * stations some time to settle so that we can
88                  * properly watch its displays. */
89                 if (m->lid_switch_ignore_event_source) {
90                         log_debug("Ignoring lid switch request, system startup or resume too close.");
91                         return 0;
92                 }
93         }
94
95         /* If the key handling is inhibited, don't do anything */
96         if (inhibit_key > 0) {
97                 if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0, NULL)) {
98                         log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_key));
99                         return 0;
100                 }
101         }
102
103         /* Locking is handled differently from the rest. */
104         if (handle == HANDLE_LOCK) {
105
106                 if (!is_edge)
107                         return 0;
108
109                 log_info("Locking sessions...");
110                 session_send_lock_all(m, true);
111                 return 1;
112         }
113
114 #if 0 /// elogind needs its own can_sleep() variant.
115         if (handle == HANDLE_SUSPEND)
116                 supported = can_sleep("suspend") > 0;
117         else if (handle == HANDLE_HIBERNATE)
118                 supported = can_sleep("hibernate") > 0;
119         else if (handle == HANDLE_HYBRID_SLEEP)
120                 supported = can_sleep("hybrid-sleep") > 0;
121 #else
122         if (handle == HANDLE_SUSPEND)
123                 supported = can_sleep(m, "suspend") > 0;
124         else if (handle == HANDLE_HIBERNATE)
125                 supported = can_sleep(m, "hibernate") > 0;
126         else if (handle == HANDLE_HYBRID_SLEEP)
127                 supported = can_sleep(m, "hybrid-sleep") > 0;
128 #endif // 0
129         else if (handle == HANDLE_KEXEC)
130                 supported = access(KEXEC, X_OK) >= 0;
131         else
132                 supported = true;
133
134         if (!supported) {
135                 log_warning("Requested operation not supported, ignoring.");
136                 return -EOPNOTSUPP;
137         }
138
139         if (m->action_what) {
140                 log_debug("Action already in progress, ignoring.");
141                 return -EALREADY;
142         }
143
144         inhibit_operation = IN_SET(handle, HANDLE_SUSPEND, HANDLE_HIBERNATE, HANDLE_HYBRID_SLEEP) ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN;
145
146         /* If the actual operation is inhibited, warn and fail */
147         if (!ignore_inhibited &&
148             manager_is_inhibited(m, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0, &offending)) {
149                 _cleanup_free_ char *comm = NULL, *u = NULL;
150
151                 get_process_comm(offending->pid, &comm);
152                 u = uid_to_name(offending->uid);
153
154                 /* If this is just a recheck of the lid switch then don't warn about anything */
155                 if (!is_edge) {
156                         log_debug("Refusing operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.",
157                                   inhibit_what_to_string(inhibit_operation),
158                                   offending->uid, strna(u),
159                                   offending->pid, strna(comm));
160                         return 0;
161                 }
162
163                 log_error("Refusing operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.",
164                           inhibit_what_to_string(inhibit_operation),
165                           offending->uid, strna(u),
166                           offending->pid, strna(comm));
167
168                 return -EPERM;
169         }
170
171         log_info("%s", message_table[handle]);
172
173 #if 0 /// elogind uses its own variant, which can use the handle directly.
174         r = bus_manager_shutdown_or_sleep_now_or_later(m, target_table[handle], inhibit_operation, &error);
175 #else
176         r = bus_manager_shutdown_or_sleep_now_or_later(m, handle, inhibit_operation, &error);
177 #endif // 0
178         if (r < 0) {
179                 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
180                 return r;
181         }
182
183         return 1;
184 }
185
186 static const char* const handle_action_table[_HANDLE_ACTION_MAX] = {
187         [HANDLE_IGNORE] = "ignore",
188         [HANDLE_POWEROFF] = "poweroff",
189         [HANDLE_REBOOT] = "reboot",
190         [HANDLE_HALT] = "halt",
191         [HANDLE_KEXEC] = "kexec",
192         [HANDLE_SUSPEND] = "suspend",
193         [HANDLE_HIBERNATE] = "hibernate",
194         [HANDLE_HYBRID_SLEEP] = "hybrid-sleep",
195         [HANDLE_LOCK] = "lock"
196 };
197
198 DEFINE_STRING_TABLE_LOOKUP(handle_action, HandleAction);
199 DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_action, handle_action, HandleAction, "Failed to parse handle action setting");