]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
3f49d45a | 2 | |
a185c5aa | 3 | #include <errno.h> |
ca78ad1d | 4 | #include <sys/stat.h> |
98a28fef | 5 | #include <unistd.h> |
a185c5aa | 6 | |
4f209af7 | 7 | #include "sd-device.h" |
cc377381 | 8 | #include "sd-messages.h" |
4f5dd394 | 9 | |
b5efdb8a | 10 | #include "alloc-util.h" |
430f0182 | 11 | #include "audit-util.h" |
31b221cf | 12 | #include "bootspec.h" |
96aad8d1 | 13 | #include "bus-common-errors.h" |
4f5dd394 | 14 | #include "bus-error.h" |
40af3d02 | 15 | #include "bus-get-properties.h" |
9b71e4ab | 16 | #include "bus-locator.h" |
269e4d2d | 17 | #include "bus-polkit.h" |
c8c8ee85 | 18 | #include "bus-unit-util.h" |
4f5dd394 | 19 | #include "bus-util.h" |
75100aeb | 20 | #include "cgroup-util.h" |
8437c059 | 21 | #include "device-util.h" |
a0956174 | 22 | #include "dirent-util.h" |
7be4b236 | 23 | #include "efi-api.h" |
0bb2f0f1 | 24 | #include "efi-loader.h" |
269e4d2d | 25 | #include "efivars.h" |
aa6123e8 | 26 | #include "env-file.h" |
e667266a | 27 | #include "env-util.h" |
4f5dd394 | 28 | #include "escape.h" |
aa6123e8 | 29 | #include "event-util.h" |
3ffd4af2 | 30 | #include "fd-util.h" |
4f5dd394 | 31 | #include "fileio-label.h" |
ee228be1 | 32 | #include "fileio.h" |
f97b34a6 | 33 | #include "format-util.h" |
f4f15635 | 34 | #include "fs-util.h" |
5ed73478 | 35 | #include "logind-action.h" |
6ecda0fb | 36 | #include "logind-dbus.h" |
7820a56c | 37 | #include "logind-polkit.h" |
6ecda0fb LP |
38 | #include "logind-seat-dbus.h" |
39 | #include "logind-session-dbus.h" | |
40 | #include "logind-user-dbus.h" | |
4f5dd394 | 41 | #include "logind.h" |
36dd5ffd | 42 | #include "missing_capability.h" |
35cd0ba5 | 43 | #include "mkdir-label.h" |
31b221cf | 44 | #include "parse-util.h" |
4f5dd394 | 45 | #include "path-util.h" |
0b452006 | 46 | #include "process-util.h" |
428b296a | 47 | #include "reboot-util.h" |
4f5dd394 | 48 | #include "selinux-util.h" |
54d7fcc6 | 49 | #include "sleep-config.h" |
4f5dd394 | 50 | #include "special.h" |
aa6123e8 | 51 | #include "serialize.h" |
31b221cf | 52 | #include "stdio-util.h" |
4f5dd394 | 53 | #include "strv.h" |
288a74cc | 54 | #include "terminal-util.h" |
e4de7287 | 55 | #include "tmpfile-util.h" |
4f5dd394 | 56 | #include "unit-name.h" |
b1d4f8e1 | 57 | #include "user-util.h" |
e2fa5721 | 58 | #include "utmp-wtmp.h" |
428b296a | 59 | #include "virt.h" |
53c0397b | 60 | #include "wall.h" |
3f49d45a | 61 | |
1831759a LN |
62 | /* As a random fun fact sysvinit had a 252 (256-(strlen(" \r\n")+1)) |
63 | * character limit for the wall message. | |
64 | * https://git.savannah.nongnu.org/cgit/sysvinit.git/tree/src/shutdown.c#n72 | |
65 | * There is no real technical need for that but doesn't make sense | |
66 | * to store arbitrary amounts either. As we are not stingy here, we | |
67 | * allow 4k. | |
68 | */ | |
63b98386 | 69 | #define WALL_MESSAGE_MAX 4096U |
1831759a | 70 | |
aa6123e8 LN |
71 | #define SHUTDOWN_SCHEDULE_FILE "/run/systemd/shutdown/scheduled" |
72 | ||
73 | static int update_schedule_file(Manager *m); | |
5ed73478 | 74 | static void reset_scheduled_shutdown(Manager *m); |
aa6123e8 | 75 | static int manager_setup_shutdown_timers(Manager* m); |
5ed73478 | 76 | |
3b92c086 LP |
77 | static int get_sender_session( |
78 | Manager *m, | |
79 | sd_bus_message *message, | |
80 | bool consult_display, | |
81 | sd_bus_error *error, | |
82 | Session **ret) { | |
aeb07570 | 83 | |
4afd3348 | 84 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
3b92c086 | 85 | Session *session = NULL; |
aeb07570 | 86 | const char *name; |
309a29df LP |
87 | int r; |
88 | ||
3b92c086 LP |
89 | /* Acquire the sender's session. This first checks if the sending process is inside a session itself, |
90 | * and returns that. If not and 'consult_display' is true, this returns the display session of the | |
91 | * owning user of the caller. */ | |
92 | ||
93 | r = sd_bus_query_sender_creds(message, | |
94 | SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT| | |
95 | (consult_display ? SD_BUS_CREDS_OWNER_UID : 0), &creds); | |
aeb07570 AJ |
96 | if (r < 0) |
97 | return r; | |
98 | ||
99 | r = sd_bus_creds_get_session(creds, &name); | |
3b92c086 LP |
100 | if (r < 0) { |
101 | if (r != -ENXIO) | |
102 | return r; | |
103 | ||
104 | if (consult_display) { | |
105 | uid_t uid; | |
106 | ||
107 | r = sd_bus_creds_get_owner_uid(creds, &uid); | |
108 | if (r < 0) { | |
109 | if (r != -ENXIO) | |
110 | return r; | |
111 | } else { | |
112 | User *user; | |
113 | ||
114 | user = hashmap_get(m->users, UID_TO_PTR(uid)); | |
115 | if (user) | |
116 | session = user->display; | |
117 | } | |
118 | } | |
119 | } else | |
120 | session = hashmap_get(m->sessions, name); | |
aeb07570 | 121 | |
aeb07570 | 122 | if (!session) |
3b92c086 LP |
123 | return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, |
124 | consult_display ? | |
125 | "Caller does not belong to any known session and doesn't own any suitable session." : | |
126 | "Caller does not belong to any known session."); | |
aeb07570 AJ |
127 | |
128 | *ret = session; | |
129 | return 0; | |
aeb07570 AJ |
130 | } |
131 | ||
3b92c086 LP |
132 | int manager_get_session_from_creds( |
133 | Manager *m, | |
134 | sd_bus_message *message, | |
135 | const char *name, | |
136 | sd_bus_error *error, | |
137 | Session **ret) { | |
138 | ||
aeb07570 AJ |
139 | Session *session; |
140 | ||
309a29df | 141 | assert(m); |
309a29df LP |
142 | assert(ret); |
143 | ||
3b92c086 LP |
144 | if (SEAT_IS_SELF(name)) /* the caller's own session */ |
145 | return get_sender_session(m, message, false, error, ret); | |
146 | if (SEAT_IS_AUTO(name)) /* The caller's own session if they have one, otherwise their user's display session */ | |
147 | return get_sender_session(m, message, true, error, ret); | |
309a29df LP |
148 | |
149 | session = hashmap_get(m->sessions, name); | |
150 | if (!session) | |
151 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name); | |
152 | ||
153 | *ret = session; | |
154 | return 0; | |
155 | } | |
156 | ||
aeb07570 | 157 | static int get_sender_user(Manager *m, sd_bus_message *message, sd_bus_error *error, User **ret) { |
aeb07570 AJ |
158 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
159 | uid_t uid; | |
309a29df LP |
160 | User *user; |
161 | int r; | |
162 | ||
aeb07570 AJ |
163 | /* Note that we get the owner UID of the session, not the actual client UID here! */ |
164 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds); | |
165 | if (r < 0) | |
166 | return r; | |
167 | ||
168 | r = sd_bus_creds_get_owner_uid(creds, &uid); | |
3b92c086 LP |
169 | if (r < 0) { |
170 | if (r != -ENXIO) | |
171 | return r; | |
172 | ||
173 | user = NULL; | |
174 | } else | |
175 | user = hashmap_get(m->users, UID_TO_PTR(uid)); | |
aeb07570 | 176 | |
aeb07570 | 177 | if (!user) |
3b92c086 LP |
178 | return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, |
179 | "Caller does not belong to any logged in or lingering user"); | |
aeb07570 AJ |
180 | |
181 | *ret = user; | |
182 | return 0; | |
aeb07570 AJ |
183 | } |
184 | ||
185 | int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret) { | |
186 | User *user; | |
187 | ||
309a29df | 188 | assert(m); |
309a29df LP |
189 | assert(ret); |
190 | ||
aeb07570 AJ |
191 | if (!uid_is_valid(uid)) |
192 | return get_sender_user(m, message, error, ret); | |
309a29df | 193 | |
8cb4ab00 | 194 | user = hashmap_get(m->users, UID_TO_PTR(uid)); |
309a29df | 195 | if (!user) |
124d7cb2 ZJS |
196 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, |
197 | "User ID "UID_FMT" is not logged in or lingering", uid); | |
309a29df LP |
198 | |
199 | *ret = user; | |
200 | return 0; | |
201 | } | |
202 | ||
3b92c086 LP |
203 | int manager_get_seat_from_creds( |
204 | Manager *m, | |
205 | sd_bus_message *message, | |
206 | const char *name, | |
207 | sd_bus_error *error, | |
208 | Seat **ret) { | |
209 | ||
309a29df LP |
210 | Seat *seat; |
211 | int r; | |
212 | ||
213 | assert(m); | |
309a29df LP |
214 | assert(ret); |
215 | ||
3b92c086 | 216 | if (SEAT_IS_SELF(name) || SEAT_IS_AUTO(name)) { |
309a29df LP |
217 | Session *session; |
218 | ||
3b92c086 LP |
219 | /* Use these special seat names as session names */ |
220 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
309a29df LP |
221 | if (r < 0) |
222 | return r; | |
223 | ||
224 | seat = session->seat; | |
309a29df | 225 | if (!seat) |
3b92c086 | 226 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "Session '%s' has no seat.", session->id); |
309a29df LP |
227 | } else { |
228 | seat = hashmap_get(m->seats, name); | |
229 | if (!seat) | |
230 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name); | |
231 | } | |
232 | ||
233 | *ret = seat; | |
234 | return 0; | |
235 | } | |
236 | ||
ce7f1070 VJ |
237 | static int return_test_polkit( |
238 | sd_bus_message *message, | |
239 | int capability, | |
240 | const char *action, | |
241 | const char **details, | |
242 | uid_t good_user, | |
243 | sd_bus_error *e) { | |
244 | ||
245 | const char *result; | |
246 | bool challenge; | |
247 | int r; | |
248 | ||
249 | r = bus_test_polkit(message, capability, action, details, good_user, &challenge, e); | |
250 | if (r < 0) | |
251 | return r; | |
252 | ||
253 | if (r > 0) | |
254 | result = "yes"; | |
255 | else if (challenge) | |
256 | result = "challenge"; | |
257 | else | |
258 | result = "no"; | |
259 | ||
260 | return sd_bus_reply_method_return(message, "s", result); | |
261 | } | |
262 | ||
cc377381 LP |
263 | static int property_get_idle_hint( |
264 | sd_bus *bus, | |
265 | const char *path, | |
266 | const char *interface, | |
267 | const char *property, | |
268 | sd_bus_message *reply, | |
ebcf1f97 LP |
269 | void *userdata, |
270 | sd_bus_error *error) { | |
a185c5aa | 271 | |
99534007 | 272 | Manager *m = ASSERT_PTR(userdata); |
a185c5aa | 273 | |
cc377381 LP |
274 | assert(bus); |
275 | assert(reply); | |
cc377381 LP |
276 | |
277 | return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0); | |
a185c5aa LP |
278 | } |
279 | ||
cc377381 LP |
280 | static int property_get_idle_since_hint( |
281 | sd_bus *bus, | |
282 | const char *path, | |
283 | const char *interface, | |
284 | const char *property, | |
285 | sd_bus_message *reply, | |
ebcf1f97 LP |
286 | void *userdata, |
287 | sd_bus_error *error) { | |
cc377381 | 288 | |
99534007 | 289 | Manager *m = ASSERT_PTR(userdata); |
5cb14b37 | 290 | dual_timestamp t = DUAL_TIMESTAMP_NULL; |
a185c5aa | 291 | |
cc377381 LP |
292 | assert(bus); |
293 | assert(reply); | |
a185c5aa LP |
294 | |
295 | manager_get_idle_hint(m, &t); | |
a185c5aa | 296 | |
cc377381 | 297 | return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic); |
a185c5aa LP |
298 | } |
299 | ||
cc377381 LP |
300 | static int property_get_inhibited( |
301 | sd_bus *bus, | |
302 | const char *path, | |
303 | const char *interface, | |
304 | const char *property, | |
305 | sd_bus_message *reply, | |
ebcf1f97 LP |
306 | void *userdata, |
307 | sd_bus_error *error) { | |
cc377381 | 308 | |
99534007 | 309 | Manager *m = ASSERT_PTR(userdata); |
f8e2fb7b | 310 | InhibitWhat w; |
f8e2fb7b | 311 | |
cc377381 LP |
312 | assert(bus); |
313 | assert(reply); | |
f8e2fb7b | 314 | |
cc377381 | 315 | w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY); |
f8e2fb7b | 316 | |
cc377381 | 317 | return sd_bus_message_append(reply, "s", inhibit_what_to_string(w)); |
f8e2fb7b LP |
318 | } |
319 | ||
cc377381 LP |
320 | static int property_get_preparing( |
321 | sd_bus *bus, | |
322 | const char *path, | |
323 | const char *interface, | |
324 | const char *property, | |
325 | sd_bus_message *reply, | |
ebcf1f97 LP |
326 | void *userdata, |
327 | sd_bus_error *error) { | |
cc377381 | 328 | |
99534007 | 329 | Manager *m = ASSERT_PTR(userdata); |
5ed73478 | 330 | bool b = false; |
5e4a79da | 331 | |
cc377381 LP |
332 | assert(bus); |
333 | assert(reply); | |
5e4a79da | 334 | |
5ed73478 LN |
335 | if (m->delayed_action) { |
336 | if (streq(property, "PreparingForShutdown")) | |
337 | b = m->delayed_action->inhibit_what & INHIBIT_SHUTDOWN; | |
338 | else | |
339 | b = m->delayed_action->inhibit_what & INHIBIT_SLEEP; | |
340 | } | |
5e4a79da | 341 | |
cc377381 | 342 | return sd_bus_message_append(reply, "b", b); |
5e4a79da LP |
343 | } |
344 | ||
8aaa023a DM |
345 | static int property_get_scheduled_shutdown( |
346 | sd_bus *bus, | |
347 | const char *path, | |
348 | const char *interface, | |
349 | const char *property, | |
350 | sd_bus_message *reply, | |
351 | void *userdata, | |
352 | sd_bus_error *error) { | |
353 | ||
99534007 | 354 | Manager *m = ASSERT_PTR(userdata); |
8aaa023a DM |
355 | int r; |
356 | ||
357 | assert(bus); | |
358 | assert(reply); | |
8aaa023a DM |
359 | |
360 | r = sd_bus_message_open_container(reply, 'r', "st"); | |
361 | if (r < 0) | |
362 | return r; | |
363 | ||
5ed73478 | 364 | r = sd_bus_message_append(reply, "st", |
a3ddcc11 | 365 | m->scheduled_shutdown_action ? handle_action_to_string(m->scheduled_shutdown_action->handle) : NULL, |
5ed73478 | 366 | m->scheduled_shutdown_timeout); |
8aaa023a DM |
367 | if (r < 0) |
368 | return r; | |
369 | ||
370 | return sd_bus_message_close_container(reply); | |
371 | } | |
372 | ||
cc377381 | 373 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction); |
01adcd69 | 374 | static BUS_DEFINE_PROPERTY_GET(property_get_docked, "b", Manager, manager_is_docked_or_external_displays); |
9b9c23da | 375 | static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed, "b", Manager, manager_is_lid_closed); |
63168cb5 | 376 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power, "b", manager_is_on_external_power()); |
01adcd69 YW |
377 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max, "t", CGROUP_LIMIT_MAX); |
378 | static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size, "t", Hashmap *, (uint64_t) hashmap_size); | |
28414939 | 379 | |
19070062 | 380 | static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 381 | _cleanup_free_ char *p = NULL; |
99534007 | 382 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
383 | const char *name; |
384 | Session *session; | |
385 | int r; | |
386 | ||
cc377381 | 387 | assert(message); |
cc377381 LP |
388 | |
389 | r = sd_bus_message_read(message, "s", &name); | |
390 | if (r < 0) | |
ebcf1f97 | 391 | return r; |
cc377381 | 392 | |
309a29df LP |
393 | r = manager_get_session_from_creds(m, message, name, error, &session); |
394 | if (r < 0) | |
395 | return r; | |
cc377381 LP |
396 | |
397 | p = session_bus_path(session); | |
398 | if (!p) | |
ebcf1f97 | 399 | return -ENOMEM; |
cc377381 | 400 | |
df2d202e | 401 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 LP |
402 | } |
403 | ||
7b33c622 AJ |
404 | /* Get login session of a process. This is not what you are looking for these days, |
405 | * as apps may instead belong to a user service unit. This includes terminal | |
406 | * emulators and hence command-line apps. */ | |
19070062 | 407 | static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 408 | _cleanup_free_ char *p = NULL; |
954449b8 | 409 | Session *session = NULL; |
99534007 | 410 | Manager *m = ASSERT_PTR(userdata); |
4e724d9c | 411 | pid_t pid; |
cc377381 LP |
412 | int r; |
413 | ||
cc377381 | 414 | assert(message); |
cc377381 | 415 | |
4e724d9c LP |
416 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
417 | ||
cc377381 LP |
418 | r = sd_bus_message_read(message, "u", &pid); |
419 | if (r < 0) | |
ebcf1f97 | 420 | return r; |
07b38ba5 | 421 | if (pid < 0) |
06820eaf | 422 | return -EINVAL; |
cc377381 | 423 | |
06820eaf | 424 | if (pid == 0) { |
309a29df | 425 | r = manager_get_session_from_creds(m, message, NULL, error, &session); |
5b12334d LP |
426 | if (r < 0) |
427 | return r; | |
309a29df LP |
428 | } else { |
429 | r = manager_get_session_by_pid(m, pid, &session); | |
4e724d9c | 430 | if (r < 0) |
ebcf1f97 | 431 | return r; |
4e724d9c | 432 | |
309a29df | 433 | if (!session) |
124d7cb2 ZJS |
434 | return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, |
435 | "PID "PID_FMT" does not belong to any known session", pid); | |
309a29df | 436 | } |
cc377381 LP |
437 | |
438 | p = session_bus_path(session); | |
439 | if (!p) | |
ebcf1f97 | 440 | return -ENOMEM; |
cc377381 | 441 | |
df2d202e | 442 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 LP |
443 | } |
444 | ||
19070062 | 445 | static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 446 | _cleanup_free_ char *p = NULL; |
99534007 | 447 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
448 | uint32_t uid; |
449 | User *user; | |
450 | int r; | |
451 | ||
cc377381 | 452 | assert(message); |
cc377381 LP |
453 | |
454 | r = sd_bus_message_read(message, "u", &uid); | |
455 | if (r < 0) | |
ebcf1f97 | 456 | return r; |
cc377381 | 457 | |
309a29df LP |
458 | r = manager_get_user_from_creds(m, message, uid, error, &user); |
459 | if (r < 0) | |
460 | return r; | |
cc377381 LP |
461 | |
462 | p = user_bus_path(user); | |
463 | if (!p) | |
ebcf1f97 | 464 | return -ENOMEM; |
cc377381 | 465 | |
df2d202e | 466 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 LP |
467 | } |
468 | ||
19070062 | 469 | static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 470 | _cleanup_free_ char *p = NULL; |
99534007 | 471 | Manager *m = ASSERT_PTR(userdata); |
954449b8 | 472 | User *user = NULL; |
4e724d9c | 473 | pid_t pid; |
fb6becb4 | 474 | int r; |
98a28fef | 475 | |
cc377381 | 476 | assert(message); |
cc377381 | 477 | |
4e724d9c LP |
478 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
479 | ||
cc377381 LP |
480 | r = sd_bus_message_read(message, "u", &pid); |
481 | if (r < 0) | |
ebcf1f97 | 482 | return r; |
07b38ba5 | 483 | if (pid < 0) |
06820eaf | 484 | return -EINVAL; |
cc377381 | 485 | |
06820eaf | 486 | if (pid == 0) { |
309a29df | 487 | r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user); |
5b12334d LP |
488 | if (r < 0) |
489 | return r; | |
309a29df LP |
490 | } else { |
491 | r = manager_get_user_by_pid(m, pid, &user); | |
4e724d9c | 492 | if (r < 0) |
ebcf1f97 | 493 | return r; |
309a29df | 494 | if (!user) |
095b8833 AJ |
495 | return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, |
496 | "PID "PID_FMT" does not belong to any logged in user or lingering user", | |
497 | pid); | |
4e724d9c LP |
498 | } |
499 | ||
cc377381 LP |
500 | p = user_bus_path(user); |
501 | if (!p) | |
ebcf1f97 | 502 | return -ENOMEM; |
cc377381 | 503 | |
df2d202e | 504 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 LP |
505 | } |
506 | ||
19070062 | 507 | static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 508 | _cleanup_free_ char *p = NULL; |
99534007 | 509 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
510 | const char *name; |
511 | Seat *seat; | |
512 | int r; | |
513 | ||
98a28fef | 514 | assert(message); |
98a28fef | 515 | |
cc377381 LP |
516 | r = sd_bus_message_read(message, "s", &name); |
517 | if (r < 0) | |
ebcf1f97 | 518 | return r; |
98a28fef | 519 | |
309a29df LP |
520 | r = manager_get_seat_from_creds(m, message, name, error, &seat); |
521 | if (r < 0) | |
522 | return r; | |
98a28fef | 523 | |
cc377381 LP |
524 | p = seat_bus_path(seat); |
525 | if (!p) | |
ebcf1f97 | 526 | return -ENOMEM; |
98a28fef | 527 | |
df2d202e | 528 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 | 529 | } |
98a28fef | 530 | |
19070062 | 531 | static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 532 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
99534007 | 533 | Manager *m = ASSERT_PTR(userdata); |
cc377381 | 534 | Session *session; |
cc377381 LP |
535 | int r; |
536 | ||
cc377381 | 537 | assert(message); |
98a28fef | 538 | |
df2d202e | 539 | r = sd_bus_message_new_method_return(message, &reply); |
cc377381 | 540 | if (r < 0) |
ebcf1f97 | 541 | return r; |
98a28fef | 542 | |
cc377381 LP |
543 | r = sd_bus_message_open_container(reply, 'a', "(susso)"); |
544 | if (r < 0) | |
ebcf1f97 | 545 | return r; |
cc377381 | 546 | |
90e74a66 | 547 | HASHMAP_FOREACH(session, m->sessions) { |
cc377381 LP |
548 | _cleanup_free_ char *p = NULL; |
549 | ||
550 | p = session_bus_path(session); | |
551 | if (!p) | |
ebcf1f97 | 552 | return -ENOMEM; |
cc377381 LP |
553 | |
554 | r = sd_bus_message_append(reply, "(susso)", | |
555 | session->id, | |
22c902fa LP |
556 | (uint32_t) session->user->user_record->uid, |
557 | session->user->user_record->user_name, | |
cc377381 LP |
558 | session->seat ? session->seat->id : "", |
559 | p); | |
560 | if (r < 0) | |
ebcf1f97 | 561 | return r; |
cc377381 LP |
562 | } |
563 | ||
564 | r = sd_bus_message_close_container(reply); | |
565 | if (r < 0) | |
ebcf1f97 | 566 | return r; |
cc377381 | 567 | |
9030ca46 | 568 | return sd_bus_send(NULL, reply, NULL); |
cc377381 LP |
569 | } |
570 | ||
19070062 | 571 | static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 572 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
99534007 | 573 | Manager *m = ASSERT_PTR(userdata); |
cc377381 | 574 | User *user; |
cc377381 LP |
575 | int r; |
576 | ||
cc377381 | 577 | assert(message); |
cc377381 | 578 | |
df2d202e | 579 | r = sd_bus_message_new_method_return(message, &reply); |
cc377381 | 580 | if (r < 0) |
ebcf1f97 | 581 | return r; |
cc377381 LP |
582 | |
583 | r = sd_bus_message_open_container(reply, 'a', "(uso)"); | |
584 | if (r < 0) | |
ebcf1f97 | 585 | return r; |
cc377381 | 586 | |
90e74a66 | 587 | HASHMAP_FOREACH(user, m->users) { |
cc377381 LP |
588 | _cleanup_free_ char *p = NULL; |
589 | ||
590 | p = user_bus_path(user); | |
591 | if (!p) | |
ebcf1f97 | 592 | return -ENOMEM; |
cc377381 LP |
593 | |
594 | r = sd_bus_message_append(reply, "(uso)", | |
22c902fa LP |
595 | (uint32_t) user->user_record->uid, |
596 | user->user_record->user_name, | |
cc377381 LP |
597 | p); |
598 | if (r < 0) | |
ebcf1f97 | 599 | return r; |
cc377381 LP |
600 | } |
601 | ||
602 | r = sd_bus_message_close_container(reply); | |
603 | if (r < 0) | |
ebcf1f97 | 604 | return r; |
cc377381 | 605 | |
9030ca46 | 606 | return sd_bus_send(NULL, reply, NULL); |
cc377381 LP |
607 | } |
608 | ||
19070062 | 609 | static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 610 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
99534007 | 611 | Manager *m = ASSERT_PTR(userdata); |
cc377381 | 612 | Seat *seat; |
cc377381 LP |
613 | int r; |
614 | ||
cc377381 | 615 | assert(message); |
cc377381 | 616 | |
df2d202e | 617 | r = sd_bus_message_new_method_return(message, &reply); |
cc377381 | 618 | if (r < 0) |
ebcf1f97 | 619 | return r; |
cc377381 LP |
620 | |
621 | r = sd_bus_message_open_container(reply, 'a', "(so)"); | |
622 | if (r < 0) | |
ebcf1f97 | 623 | return r; |
cc377381 | 624 | |
90e74a66 | 625 | HASHMAP_FOREACH(seat, m->seats) { |
cc377381 LP |
626 | _cleanup_free_ char *p = NULL; |
627 | ||
628 | p = seat_bus_path(seat); | |
629 | if (!p) | |
ebcf1f97 | 630 | return -ENOMEM; |
cc377381 | 631 | |
b8358bce | 632 | r = sd_bus_message_append(reply, "(so)", seat->id, p); |
cc377381 | 633 | if (r < 0) |
ebcf1f97 | 634 | return r; |
cc377381 LP |
635 | } |
636 | ||
637 | r = sd_bus_message_close_container(reply); | |
638 | if (r < 0) | |
ebcf1f97 | 639 | return r; |
cc377381 | 640 | |
9030ca46 | 641 | return sd_bus_send(NULL, reply, NULL); |
cc377381 LP |
642 | } |
643 | ||
19070062 | 644 | static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 645 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
99534007 | 646 | Manager *m = ASSERT_PTR(userdata); |
cc377381 | 647 | Inhibitor *inhibitor; |
cc377381 LP |
648 | int r; |
649 | ||
19070062 | 650 | assert(message); |
19070062 | 651 | |
df2d202e | 652 | r = sd_bus_message_new_method_return(message, &reply); |
cc377381 | 653 | if (r < 0) |
ebcf1f97 | 654 | return r; |
cc377381 LP |
655 | |
656 | r = sd_bus_message_open_container(reply, 'a', "(ssssuu)"); | |
657 | if (r < 0) | |
ebcf1f97 | 658 | return r; |
cc377381 | 659 | |
90e74a66 | 660 | HASHMAP_FOREACH(inhibitor, m->inhibitors) { |
cc377381 | 661 | |
dbfa3fbb | 662 | r = sd_bus_message_append(reply, "(ssssuu)", |
cc377381 LP |
663 | strempty(inhibit_what_to_string(inhibitor->what)), |
664 | strempty(inhibitor->who), | |
665 | strempty(inhibitor->why), | |
666 | strempty(inhibit_mode_to_string(inhibitor->mode)), | |
667 | (uint32_t) inhibitor->uid, | |
89bad70f | 668 | (uint32_t) inhibitor->pid.pid); |
cc377381 | 669 | if (r < 0) |
ebcf1f97 | 670 | return r; |
cc377381 LP |
671 | } |
672 | ||
673 | r = sd_bus_message_close_container(reply); | |
674 | if (r < 0) | |
ebcf1f97 | 675 | return r; |
cc377381 | 676 | |
9030ca46 | 677 | return sd_bus_send(NULL, reply, NULL); |
cc377381 LP |
678 | } |
679 | ||
19070062 | 680 | static int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
a4cd87e9 | 681 | const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop; |
c7543606 | 682 | _cleanup_free_ char *id = NULL; |
cc377381 | 683 | Session *session = NULL; |
bc2b6332 | 684 | uint32_t audit_id = 0; |
99534007 | 685 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
686 | User *user = NULL; |
687 | Seat *seat = NULL; | |
06820eaf LP |
688 | pid_t leader; |
689 | uid_t uid; | |
cc377381 LP |
690 | int remote; |
691 | uint32_t vtnr = 0; | |
692 | SessionType t; | |
693 | SessionClass c; | |
694 | int r; | |
695 | ||
cc377381 | 696 | assert(message); |
cc377381 | 697 | |
06820eaf LP |
698 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
699 | assert_cc(sizeof(uid_t) == sizeof(uint32_t)); | |
700 | ||
124d7cb2 ZJS |
701 | r = sd_bus_message_read(message, "uusssssussbss", |
702 | &uid, &leader, &service, &type, &class, &desktop, &cseat, | |
703 | &vtnr, &tty, &display, &remote, &remote_user, &remote_host); | |
cc377381 | 704 | if (r < 0) |
ebcf1f97 | 705 | return r; |
cc377381 | 706 | |
06820eaf | 707 | if (!uid_is_valid(uid)) |
1b09b81c | 708 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UID"); |
bc2b6332 | 709 | if (leader < 0 || leader == 1 || leader == getpid_cached()) |
1b09b81c | 710 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID"); |
98a28fef | 711 | |
e2acb67b LP |
712 | if (isempty(type)) |
713 | t = _SESSION_TYPE_INVALID; | |
714 | else { | |
715 | t = session_type_from_string(type); | |
716 | if (t < 0) | |
124d7cb2 ZJS |
717 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
718 | "Invalid session type %s", type); | |
e2acb67b | 719 | } |
98a28fef | 720 | |
55efac6c | 721 | if (isempty(class)) |
e2acb67b LP |
722 | c = _SESSION_CLASS_INVALID; |
723 | else { | |
55efac6c | 724 | c = session_class_from_string(class); |
e2acb67b | 725 | if (c < 0) |
124d7cb2 ZJS |
726 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
727 | "Invalid session class %s", class); | |
e2acb67b | 728 | } |
55efac6c | 729 | |
a4cd87e9 LP |
730 | if (isempty(desktop)) |
731 | desktop = NULL; | |
732 | else { | |
733 | if (!string_is_safe(desktop)) | |
124d7cb2 ZJS |
734 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
735 | "Invalid desktop string %s", desktop); | |
a4cd87e9 LP |
736 | } |
737 | ||
954449b8 LP |
738 | if (isempty(cseat)) |
739 | seat = NULL; | |
98a28fef | 740 | else { |
954449b8 LP |
741 | seat = hashmap_get(m->seats, cseat); |
742 | if (!seat) | |
124d7cb2 ZJS |
743 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, |
744 | "No seat '%s' known", cseat); | |
98a28fef LP |
745 | } |
746 | ||
98a28fef | 747 | if (tty_is_vc(tty)) { |
4d6d6518 | 748 | int v; |
98a28fef | 749 | |
954449b8 | 750 | if (!seat) |
92432fcc DH |
751 | seat = m->seat0; |
752 | else if (seat != m->seat0) | |
124d7cb2 ZJS |
753 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
754 | "TTY %s is virtual console but seat %s is not seat0", tty, seat->id); | |
98a28fef | 755 | |
4d6d6518 | 756 | v = vtnr_from_tty(tty); |
4d6d6518 | 757 | if (v <= 0) |
124d7cb2 ZJS |
758 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
759 | "Cannot determine VT number from virtual console TTY %s", tty); | |
98a28fef | 760 | |
bc2b6332 | 761 | if (vtnr == 0) |
4d6d6518 LP |
762 | vtnr = (uint32_t) v; |
763 | else if (vtnr != (uint32_t) v) | |
124d7cb2 ZJS |
764 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
765 | "Specified TTY and VT number do not match"); | |
cc377381 | 766 | |
d1122ad5 LP |
767 | } else if (tty_is_console(tty)) { |
768 | ||
954449b8 | 769 | if (!seat) |
92432fcc DH |
770 | seat = m->seat0; |
771 | else if (seat != m->seat0) | |
124d7cb2 ZJS |
772 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
773 | "Console TTY specified but seat is not seat0"); | |
d1122ad5 LP |
774 | |
775 | if (vtnr != 0) | |
124d7cb2 ZJS |
776 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
777 | "Console TTY specified but VT number is not 0"); | |
978cf3c7 | 778 | } |
98a28fef | 779 | |
954449b8 | 780 | if (seat) { |
bf7825ae | 781 | if (seat_has_vts(seat)) { |
bc2b6332 | 782 | if (vtnr <= 0 || vtnr > 63) |
124d7cb2 ZJS |
783 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
784 | "VT number out of range"); | |
4d6d6518 | 785 | } else { |
d1122ad5 | 786 | if (vtnr != 0) |
124d7cb2 ZJS |
787 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
788 | "Seat has no VTs but VT number not 0"); | |
4d6d6518 LP |
789 | } |
790 | } | |
791 | ||
e2acb67b LP |
792 | if (t == _SESSION_TYPE_INVALID) { |
793 | if (!isempty(display)) | |
794 | t = SESSION_X11; | |
795 | else if (!isempty(tty)) | |
796 | t = SESSION_TTY; | |
797 | else | |
798 | t = SESSION_UNSPECIFIED; | |
799 | } | |
800 | ||
801 | if (c == _SESSION_CLASS_INVALID) { | |
a4cd87e9 | 802 | if (t == SESSION_UNSPECIFIED) |
e2acb67b | 803 | c = SESSION_BACKGROUND; |
a4cd87e9 LP |
804 | else |
805 | c = SESSION_USER; | |
e2acb67b LP |
806 | } |
807 | ||
06820eaf | 808 | if (leader == 0) { |
4afd3348 | 809 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
5b12334d LP |
810 | |
811 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); | |
812 | if (r < 0) | |
813 | return r; | |
814 | ||
5b12334d | 815 | r = sd_bus_creds_get_pid(creds, (pid_t*) &leader); |
cc377381 | 816 | if (r < 0) |
ebcf1f97 | 817 | return r; |
9444b1f2 LP |
818 | } |
819 | ||
124d7cb2 ZJS |
820 | /* Check if we are already in a logind session. Or if we are in user@.service |
821 | * which is a special PAM session that avoids creating a logind session. */ | |
bc2b6332 | 822 | r = manager_get_user_by_pid(m, leader, NULL); |
e8a3144e AJ |
823 | if (r < 0) |
824 | return r; | |
bc2b6332 | 825 | if (r > 0) |
124d7cb2 ZJS |
826 | return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, |
827 | "Already running in a session or user slice"); | |
b80120c4 DH |
828 | |
829 | /* | |
830 | * Old gdm and lightdm start the user-session on the same VT as | |
831 | * the greeter session. But they destroy the greeter session | |
832 | * after the user-session and want the user-session to take | |
833 | * over the VT. We need to support this for | |
834 | * backwards-compatibility, so make sure we allow new sessions | |
cc85d562 DH |
835 | * on a VT that a greeter is running on. Furthermore, to allow |
836 | * re-logins, we have to allow a greeter to take over a used VT for | |
837 | * the exact same reasons. | |
b80120c4 | 838 | */ |
cc85d562 DH |
839 | if (c != SESSION_GREETER && |
840 | vtnr > 0 && | |
319a4f4b | 841 | vtnr < MALLOC_ELEMENTSOF(m->seat0->positions) && |
b80120c4 DH |
842 | m->seat0->positions[vtnr] && |
843 | m->seat0->positions[vtnr]->class != SESSION_GREETER) | |
1b09b81c | 844 | return sd_bus_error_set(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session"); |
21c390cc | 845 | |
183e0738 | 846 | if (hashmap_size(m->sessions) >= m->sessions_max) |
124d7cb2 ZJS |
847 | return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, |
848 | "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", | |
849 | m->sessions_max); | |
183e0738 | 850 | |
3a87a86e LP |
851 | (void) audit_session_from_pid(leader, &audit_id); |
852 | if (audit_session_is_valid(audit_id)) { | |
954449b8 | 853 | /* Keep our session IDs and the audit session IDs in sync */ |
21c390cc | 854 | |
de0671ee | 855 | if (asprintf(&id, "%"PRIu32, audit_id) < 0) |
ebcf1f97 | 856 | return -ENOMEM; |
21c390cc | 857 | |
b0bd159f LP |
858 | /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's |
859 | * not trust the audit data and let's better register a new ID */ | |
860 | if (hashmap_contains(m->sessions, id)) { | |
bc2b6332 | 861 | log_warning("Existing logind session ID %s used by new audit session, ignoring.", id); |
3a87a86e | 862 | audit_id = AUDIT_SESSION_INVALID; |
97b11eed | 863 | id = mfree(id); |
07714753 | 864 | } |
954449b8 | 865 | } |
07714753 | 866 | |
954449b8 | 867 | if (!id) { |
07714753 | 868 | do { |
97b11eed | 869 | id = mfree(id); |
07714753 | 870 | |
97352764 | 871 | if (asprintf(&id, "c%" PRIu64, ++m->session_counter) < 0) |
ebcf1f97 | 872 | return -ENOMEM; |
07714753 | 873 | |
b0bd159f | 874 | } while (hashmap_contains(m->sessions, id)); |
98a28fef LP |
875 | } |
876 | ||
3b92c086 LP |
877 | /* The generated names should not clash with 'auto' or 'self' */ |
878 | assert(!SESSION_IS_SELF(id)); | |
879 | assert(!SESSION_IS_AUTO(id)); | |
880 | ||
5238e957 | 881 | /* If we are not watching utmp already, try again */ |
3d0ef5c7 LP |
882 | manager_reconnect_utmp(m); |
883 | ||
954449b8 | 884 | r = manager_add_user_by_uid(m, uid, &user); |
ebcf1f97 | 885 | if (r < 0) |
954449b8 LP |
886 | goto fail; |
887 | ||
9444b1f2 | 888 | r = manager_add_session(m, id, &session); |
ebcf1f97 | 889 | if (r < 0) |
98a28fef LP |
890 | goto fail; |
891 | ||
9444b1f2 | 892 | session_set_user(session, user); |
fe3ab845 YW |
893 | r = session_set_leader(session, leader); |
894 | if (r < 0) | |
895 | goto fail; | |
9444b1f2 | 896 | |
db72aea4 | 897 | session->original_type = session->type = t; |
55efac6c | 898 | session->class = c; |
98a28fef | 899 | session->remote = remote; |
98a28fef LP |
900 | session->vtnr = vtnr; |
901 | ||
98a28fef LP |
902 | if (!isempty(tty)) { |
903 | session->tty = strdup(tty); | |
904 | if (!session->tty) { | |
ebcf1f97 | 905 | r = -ENOMEM; |
98a28fef LP |
906 | goto fail; |
907 | } | |
3d0ef5c7 LP |
908 | |
909 | session->tty_validity = TTY_FROM_PAM; | |
98a28fef LP |
910 | } |
911 | ||
912 | if (!isempty(display)) { | |
913 | session->display = strdup(display); | |
914 | if (!session->display) { | |
ebcf1f97 | 915 | r = -ENOMEM; |
98a28fef LP |
916 | goto fail; |
917 | } | |
918 | } | |
919 | ||
920 | if (!isempty(remote_user)) { | |
921 | session->remote_user = strdup(remote_user); | |
922 | if (!session->remote_user) { | |
ebcf1f97 | 923 | r = -ENOMEM; |
98a28fef LP |
924 | goto fail; |
925 | } | |
926 | } | |
927 | ||
928 | if (!isempty(remote_host)) { | |
929 | session->remote_host = strdup(remote_host); | |
930 | if (!session->remote_host) { | |
ebcf1f97 | 931 | r = -ENOMEM; |
98a28fef LP |
932 | goto fail; |
933 | } | |
934 | } | |
935 | ||
936 | if (!isempty(service)) { | |
937 | session->service = strdup(service); | |
938 | if (!session->service) { | |
ebcf1f97 | 939 | r = -ENOMEM; |
98a28fef LP |
940 | goto fail; |
941 | } | |
942 | } | |
943 | ||
a4cd87e9 LP |
944 | if (!isempty(desktop)) { |
945 | session->desktop = strdup(desktop); | |
946 | if (!session->desktop) { | |
947 | r = -ENOMEM; | |
948 | goto fail; | |
949 | } | |
950 | } | |
951 | ||
954449b8 LP |
952 | if (seat) { |
953 | r = seat_attach_session(seat, session); | |
ebcf1f97 | 954 | if (r < 0) |
98a28fef LP |
955 | goto fail; |
956 | } | |
957 | ||
22f93314 JS |
958 | r = sd_bus_message_enter_container(message, 'a', "(sv)"); |
959 | if (r < 0) | |
d88ffeee | 960 | goto fail; |
22f93314 | 961 | |
25a1ab4e | 962 | r = session_start(session, message, error); |
22f93314 JS |
963 | if (r < 0) |
964 | goto fail; | |
965 | ||
966 | r = sd_bus_message_exit_container(message); | |
ebcf1f97 | 967 | if (r < 0) |
98a28fef LP |
968 | goto fail; |
969 | ||
cc377381 | 970 | session->create_message = sd_bus_message_ref(message); |
98a28fef | 971 | |
bc2b6332 | 972 | /* Now, let's wait until the slice unit and stuff got created. We send the reply back from |
f7340ab2 | 973 | * session_send_create_reply(). */ |
cba38758 | 974 | |
cc377381 | 975 | return 1; |
98a28fef LP |
976 | |
977 | fail: | |
98a28fef LP |
978 | if (session) |
979 | session_add_to_gc_queue(session); | |
980 | ||
981 | if (user) | |
982 | user_add_to_gc_queue(user); | |
983 | ||
98a28fef LP |
984 | return r; |
985 | } | |
986 | ||
19070062 | 987 | static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 988 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
989 | Session *session; |
990 | const char *name; | |
991 | int r; | |
314b4b0a | 992 | |
cc377381 | 993 | assert(message); |
cc377381 LP |
994 | |
995 | r = sd_bus_message_read(message, "s", &name); | |
996 | if (r < 0) | |
ebcf1f97 | 997 | return r; |
cc377381 | 998 | |
309a29df LP |
999 | r = manager_get_session_from_creds(m, message, name, error, &session); |
1000 | if (r < 0) | |
1001 | return r; | |
cc377381 | 1002 | |
ad8780c9 ZJS |
1003 | r = session_release(session); |
1004 | if (r < 0) | |
1005 | return r; | |
cc377381 | 1006 | |
df2d202e | 1007 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
1008 | } |
1009 | ||
19070062 | 1010 | static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1011 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
1012 | Session *session; |
1013 | const char *name; | |
1014 | int r; | |
f8e2fb7b | 1015 | |
cc377381 | 1016 | assert(message); |
cc377381 LP |
1017 | |
1018 | r = sd_bus_message_read(message, "s", &name); | |
1019 | if (r < 0) | |
ebcf1f97 | 1020 | return r; |
cc377381 | 1021 | |
309a29df LP |
1022 | r = manager_get_session_from_creds(m, message, name, error, &session); |
1023 | if (r < 0) | |
1024 | return r; | |
cc377381 | 1025 | |
4acf0cfd LP |
1026 | /* PolicyKit is done by bus_session_method_activate() */ |
1027 | ||
19070062 | 1028 | return bus_session_method_activate(message, session, error); |
cc377381 LP |
1029 | } |
1030 | ||
19070062 | 1031 | static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1032 | const char *session_name, *seat_name; |
99534007 | 1033 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
1034 | Session *session; |
1035 | Seat *seat; | |
1036 | int r; | |
1037 | ||
f8e2fb7b | 1038 | assert(message); |
f8e2fb7b | 1039 | |
3b92c086 | 1040 | /* Same as ActivateSession() but refuses to work if the seat doesn't match */ |
f8e2fb7b | 1041 | |
cc377381 LP |
1042 | r = sd_bus_message_read(message, "ss", &session_name, &seat_name); |
1043 | if (r < 0) | |
ebcf1f97 | 1044 | return r; |
eecd1362 | 1045 | |
309a29df LP |
1046 | r = manager_get_session_from_creds(m, message, session_name, error, &session); |
1047 | if (r < 0) | |
1048 | return r; | |
beaafb2e | 1049 | |
309a29df LP |
1050 | r = manager_get_seat_from_creds(m, message, seat_name, error, &seat); |
1051 | if (r < 0) | |
1052 | return r; | |
314b4b0a | 1053 | |
cc377381 | 1054 | if (session->seat != seat) |
124d7cb2 ZJS |
1055 | return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, |
1056 | "Session %s not on seat %s", session_name, seat_name); | |
cc377381 | 1057 | |
7820a56c | 1058 | r = check_polkit_chvt(message, m, error); |
4acf0cfd LP |
1059 | if (r < 0) |
1060 | return r; | |
1061 | if (r == 0) | |
1062 | return 1; /* Will call us back */ | |
1063 | ||
cc377381 | 1064 | r = session_activate(session); |
f8e2fb7b | 1065 | if (r < 0) |
ebcf1f97 | 1066 | return r; |
f8e2fb7b | 1067 | |
df2d202e | 1068 | return sd_bus_reply_method_return(message, NULL); |
cc377381 | 1069 | } |
f8e2fb7b | 1070 | |
19070062 | 1071 | static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1072 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
1073 | Session *session; |
1074 | const char *name; | |
1075 | int r; | |
f8e2fb7b | 1076 | |
cc377381 | 1077 | assert(message); |
f8e2fb7b | 1078 | |
cc377381 LP |
1079 | r = sd_bus_message_read(message, "s", &name); |
1080 | if (r < 0) | |
ebcf1f97 | 1081 | return r; |
f8e2fb7b | 1082 | |
309a29df LP |
1083 | r = manager_get_session_from_creds(m, message, name, error, &session); |
1084 | if (r < 0) | |
1085 | return r; | |
f8e2fb7b | 1086 | |
19070062 | 1087 | return bus_session_method_lock(message, session, error); |
cc377381 | 1088 | } |
f8e2fb7b | 1089 | |
19070062 | 1090 | static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1091 | Manager *m = ASSERT_PTR(userdata); |
cc377381 | 1092 | int r; |
f8e2fb7b | 1093 | |
cc377381 | 1094 | assert(message); |
f8e2fb7b | 1095 | |
c529695e LP |
1096 | r = bus_verify_polkit_async( |
1097 | message, | |
1098 | CAP_SYS_ADMIN, | |
1099 | "org.freedesktop.login1.lock-sessions", | |
403ed0e5 | 1100 | NULL, |
c529695e LP |
1101 | false, |
1102 | UID_INVALID, | |
1103 | &m->polkit_registry, | |
1104 | error); | |
1105 | if (r < 0) | |
1106 | return r; | |
1107 | if (r == 0) | |
1108 | return 1; /* Will call us back */ | |
1109 | ||
cc377381 LP |
1110 | r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions")); |
1111 | if (r < 0) | |
ebcf1f97 | 1112 | return r; |
f8e2fb7b | 1113 | |
df2d202e | 1114 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
1115 | } |
1116 | ||
19070062 | 1117 | static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c529695e | 1118 | const char *name; |
99534007 | 1119 | Manager *m = ASSERT_PTR(userdata); |
cc377381 | 1120 | Session *session; |
cc377381 LP |
1121 | int r; |
1122 | ||
cc377381 | 1123 | assert(message); |
cc377381 | 1124 | |
c529695e | 1125 | r = sd_bus_message_read(message, "s", &name); |
cc377381 | 1126 | if (r < 0) |
ebcf1f97 | 1127 | return r; |
cc377381 | 1128 | |
309a29df LP |
1129 | r = manager_get_session_from_creds(m, message, name, error, &session); |
1130 | if (r < 0) | |
1131 | return r; | |
f8e2fb7b | 1132 | |
19070062 | 1133 | return bus_session_method_kill(message, session, error); |
cc377381 | 1134 | } |
f8e2fb7b | 1135 | |
19070062 | 1136 | static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1137 | Manager *m = ASSERT_PTR(userdata); |
cc377381 | 1138 | uint32_t uid; |
cc377381 LP |
1139 | User *user; |
1140 | int r; | |
f8e2fb7b | 1141 | |
cc377381 | 1142 | assert(message); |
cc377381 | 1143 | |
c529695e | 1144 | r = sd_bus_message_read(message, "u", &uid); |
cc377381 | 1145 | if (r < 0) |
ebcf1f97 | 1146 | return r; |
cc377381 | 1147 | |
309a29df LP |
1148 | r = manager_get_user_from_creds(m, message, uid, error, &user); |
1149 | if (r < 0) | |
1150 | return r; | |
cc377381 | 1151 | |
19070062 | 1152 | return bus_user_method_kill(message, user, error); |
cc377381 LP |
1153 | } |
1154 | ||
19070062 | 1155 | static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1156 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
1157 | const char *name; |
1158 | Session *session; | |
1159 | int r; | |
1160 | ||
cc377381 | 1161 | assert(message); |
cc377381 LP |
1162 | |
1163 | r = sd_bus_message_read(message, "s", &name); | |
1164 | if (r < 0) | |
ebcf1f97 | 1165 | return r; |
cc377381 | 1166 | |
309a29df LP |
1167 | r = manager_get_session_from_creds(m, message, name, error, &session); |
1168 | if (r < 0) | |
1169 | return r; | |
cc377381 | 1170 | |
19070062 | 1171 | return bus_session_method_terminate(message, session, error); |
cc377381 LP |
1172 | } |
1173 | ||
19070062 | 1174 | static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1175 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
1176 | uint32_t uid; |
1177 | User *user; | |
1178 | int r; | |
1179 | ||
cc377381 | 1180 | assert(message); |
cc377381 LP |
1181 | |
1182 | r = sd_bus_message_read(message, "u", &uid); | |
1183 | if (r < 0) | |
ebcf1f97 | 1184 | return r; |
cc377381 | 1185 | |
309a29df LP |
1186 | r = manager_get_user_from_creds(m, message, uid, error, &user); |
1187 | if (r < 0) | |
1188 | return r; | |
cc377381 | 1189 | |
19070062 | 1190 | return bus_user_method_terminate(message, user, error); |
cc377381 LP |
1191 | } |
1192 | ||
19070062 | 1193 | static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1194 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
1195 | const char *name; |
1196 | Seat *seat; | |
1197 | int r; | |
1198 | ||
cc377381 | 1199 | assert(message); |
cc377381 LP |
1200 | |
1201 | r = sd_bus_message_read(message, "s", &name); | |
1202 | if (r < 0) | |
ebcf1f97 | 1203 | return r; |
cc377381 | 1204 | |
309a29df LP |
1205 | r = manager_get_seat_from_creds(m, message, name, error, &seat); |
1206 | if (r < 0) | |
1207 | return r; | |
cc377381 | 1208 | |
19070062 | 1209 | return bus_seat_method_terminate(message, seat, error); |
cc377381 LP |
1210 | } |
1211 | ||
19070062 | 1212 | static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
34160d91 | 1213 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
cc377381 | 1214 | _cleanup_free_ char *cc = NULL; |
99534007 | 1215 | Manager *m = ASSERT_PTR(userdata); |
152199f2 | 1216 | int r, b, interactive; |
cc377381 LP |
1217 | struct passwd *pw; |
1218 | const char *path; | |
34160d91 | 1219 | uint32_t uid, auth_uid; |
cc377381 | 1220 | |
cc377381 | 1221 | assert(message); |
cc377381 LP |
1222 | |
1223 | r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive); | |
1224 | if (r < 0) | |
ebcf1f97 | 1225 | return r; |
cc377381 | 1226 | |
34160d91 AJ |
1227 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID | |
1228 | SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds); | |
1229 | if (r < 0) | |
1230 | return r; | |
309a29df | 1231 | |
34160d91 AJ |
1232 | if (!uid_is_valid(uid)) { |
1233 | /* Note that we get the owner UID of the session or user unit, | |
1234 | * not the actual client UID here! */ | |
309a29df LP |
1235 | r = sd_bus_creds_get_owner_uid(creds, &uid); |
1236 | if (r < 0) | |
1237 | return r; | |
34160d91 | 1238 | } |
06820eaf | 1239 | |
34160d91 AJ |
1240 | /* owner_uid is racy, so for authorization we must use euid */ |
1241 | r = sd_bus_creds_get_euid(creds, &auth_uid); | |
1242 | if (r < 0) | |
1243 | return r; | |
309a29df | 1244 | |
cc377381 LP |
1245 | errno = 0; |
1246 | pw = getpwuid(uid); | |
1247 | if (!pw) | |
66855de7 | 1248 | return errno_or_else(ENOENT); |
cc377381 | 1249 | |
f3885791 LP |
1250 | r = bus_verify_polkit_async( |
1251 | message, | |
1252 | CAP_SYS_ADMIN, | |
34160d91 AJ |
1253 | uid == auth_uid ? "org.freedesktop.login1.set-self-linger" : |
1254 | "org.freedesktop.login1.set-user-linger", | |
403ed0e5 | 1255 | NULL, |
f3885791 | 1256 | interactive, |
c529695e | 1257 | UID_INVALID, |
f3885791 LP |
1258 | &m->polkit_registry, |
1259 | error); | |
cc377381 | 1260 | if (r < 0) |
ebcf1f97 | 1261 | return r; |
cc377381 LP |
1262 | if (r == 0) |
1263 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1264 | ||
6e5dcce4 | 1265 | (void) mkdir_p_label("/var/lib/systemd", 0755); |
37c1d5e9 | 1266 | r = mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE); |
cc377381 | 1267 | if (r < 0) |
ebcf1f97 | 1268 | return r; |
cc377381 LP |
1269 | |
1270 | cc = cescape(pw->pw_name); | |
1271 | if (!cc) | |
ebcf1f97 | 1272 | return -ENOMEM; |
cc377381 | 1273 | |
63c372cb | 1274 | path = strjoina("/var/lib/systemd/linger/", cc); |
cc377381 LP |
1275 | if (b) { |
1276 | User *u; | |
1277 | ||
1278 | r = touch(path); | |
1279 | if (r < 0) | |
ebcf1f97 | 1280 | return r; |
cc377381 LP |
1281 | |
1282 | if (manager_add_user_by_uid(m, uid, &u) >= 0) | |
513cf7da | 1283 | user_start(u); |
cc377381 LP |
1284 | |
1285 | } else { | |
1286 | User *u; | |
1287 | ||
1288 | r = unlink(path); | |
1289 | if (r < 0 && errno != ENOENT) | |
ebcf1f97 | 1290 | return -errno; |
cc377381 | 1291 | |
8cb4ab00 | 1292 | u = hashmap_get(m->users, UID_TO_PTR(uid)); |
cc377381 LP |
1293 | if (u) |
1294 | user_add_to_gc_queue(u); | |
1295 | } | |
1296 | ||
df2d202e | 1297 | return sd_bus_reply_method_return(message, NULL); |
f8e2fb7b LP |
1298 | } |
1299 | ||
a1af8372 | 1300 | static int trigger_device(Manager *m, sd_device *parent) { |
4f209af7 | 1301 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
b668e064 LP |
1302 | int r; |
1303 | ||
1304 | assert(m); | |
1305 | ||
4f209af7 YW |
1306 | r = sd_device_enumerator_new(&e); |
1307 | if (r < 0) | |
1308 | return r; | |
1309 | ||
1310 | r = sd_device_enumerator_allow_uninitialized(e); | |
1311 | if (r < 0) | |
1312 | return r; | |
b668e064 | 1313 | |
a1af8372 DDM |
1314 | if (parent) { |
1315 | r = sd_device_enumerator_add_match_parent(e, parent); | |
06acf2d4 LP |
1316 | if (r < 0) |
1317 | return r; | |
2eb916cd LP |
1318 | } |
1319 | ||
8437c059 | 1320 | FOREACH_DEVICE(e, d) { |
d8021ea1 | 1321 | r = sd_device_trigger(d, SD_DEVICE_CHANGE); |
4f209af7 | 1322 | if (r < 0) |
d8021ea1 | 1323 | log_device_debug_errno(d, r, "Failed to trigger device, ignoring: %m"); |
b668e064 LP |
1324 | } |
1325 | ||
06acf2d4 | 1326 | return 0; |
b668e064 LP |
1327 | } |
1328 | ||
08237f06 | 1329 | static int attach_device(Manager *m, const char *seat, const char *sysfs, sd_bus_error *error) { |
4f209af7 | 1330 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
7fd1b19b | 1331 | _cleanup_free_ char *rule = NULL, *file = NULL; |
c28fa3d3 | 1332 | const char *id_for_seat; |
47a26690 LP |
1333 | int r; |
1334 | ||
1335 | assert(m); | |
1336 | assert(seat); | |
1337 | assert(sysfs); | |
1338 | ||
4f209af7 YW |
1339 | r = sd_device_new_from_syspath(&d, sysfs); |
1340 | if (r < 0) | |
08237f06 | 1341 | return sd_bus_error_set_errnof(error, r, "Failed to open device '%s': %m", sysfs); |
47a26690 | 1342 | |
643bb924 | 1343 | if (sd_device_has_current_tag(d, "seat") <= 0) |
08237f06 | 1344 | return sd_bus_error_set_errnof(error, ENODEV, "Device '%s' lacks 'seat' udev tag.", sysfs); |
47a26690 | 1345 | |
4f209af7 | 1346 | if (sd_device_get_property_value(d, "ID_FOR_SEAT", &id_for_seat) < 0) |
08237f06 | 1347 | return sd_bus_error_set_errnof(error, ENODEV, "Device '%s' lacks 'ID_FOR_SEAT' udev property.", sysfs); |
47a26690 | 1348 | |
06acf2d4 LP |
1349 | if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) |
1350 | return -ENOMEM; | |
47a26690 | 1351 | |
06acf2d4 LP |
1352 | if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) |
1353 | return -ENOMEM; | |
47a26690 | 1354 | |
4f209af7 | 1355 | (void) mkdir_p_label("/etc/udev/rules.d", 0755); |
574d5f2d | 1356 | r = write_string_file_atomic_label(file, rule); |
a0a0c7f1 | 1357 | if (r < 0) |
06acf2d4 | 1358 | return r; |
47a26690 | 1359 | |
06acf2d4 | 1360 | return trigger_device(m, d); |
47a26690 LP |
1361 | } |
1362 | ||
b668e064 | 1363 | static int flush_devices(Manager *m) { |
c2b2df60 | 1364 | _cleanup_closedir_ DIR *d = NULL; |
b668e064 LP |
1365 | |
1366 | assert(m); | |
1367 | ||
1368 | d = opendir("/etc/udev/rules.d"); | |
1369 | if (!d) { | |
1370 | if (errno != ENOENT) | |
56f64d95 | 1371 | log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m"); |
c7f0d9e5 | 1372 | } else |
8fb3f009 | 1373 | FOREACH_DIRENT_ALL(de, d, break) { |
b668e064 LP |
1374 | if (!dirent_is_file(de)) |
1375 | continue; | |
1376 | ||
1377 | if (!startswith(de->d_name, "72-seat-")) | |
1378 | continue; | |
1379 | ||
1380 | if (!endswith(de->d_name, ".rules")) | |
1381 | continue; | |
1382 | ||
1383 | if (unlinkat(dirfd(d), de->d_name, 0) < 0) | |
56f64d95 | 1384 | log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name); |
b668e064 | 1385 | } |
b668e064 LP |
1386 | |
1387 | return trigger_device(m, NULL); | |
1388 | } | |
1389 | ||
19070062 | 1390 | static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1391 | const char *sysfs, *seat; |
99534007 | 1392 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
1393 | int interactive, r; |
1394 | ||
cc377381 | 1395 | assert(message); |
cc377381 LP |
1396 | |
1397 | r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive); | |
1398 | if (r < 0) | |
ebcf1f97 | 1399 | return r; |
cc377381 | 1400 | |
bda06130 LP |
1401 | if (!path_is_normalized(sysfs)) |
1402 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not normalized", sysfs); | |
cc377381 | 1403 | if (!path_startswith(sysfs, "/sys")) |
ebcf1f97 | 1404 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs); |
cc377381 | 1405 | |
ed179418 LP |
1406 | if (SEAT_IS_SELF(seat) || SEAT_IS_AUTO(seat)) { |
1407 | Seat *found; | |
1408 | ||
1409 | r = manager_get_seat_from_creds(m, message, seat, error, &found); | |
1410 | if (r < 0) | |
1411 | return r; | |
1412 | ||
1413 | seat = found->id; | |
1414 | ||
1415 | } else if (!seat_name_is_valid(seat)) /* Note that a seat does not have to exist yet for this operation to succeed */ | |
1416 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat name %s is not valid", seat); | |
cc377381 | 1417 | |
f3885791 LP |
1418 | r = bus_verify_polkit_async( |
1419 | message, | |
1420 | CAP_SYS_ADMIN, | |
1421 | "org.freedesktop.login1.attach-device", | |
403ed0e5 | 1422 | NULL, |
f3885791 | 1423 | interactive, |
c529695e | 1424 | UID_INVALID, |
f3885791 LP |
1425 | &m->polkit_registry, |
1426 | error); | |
cc377381 | 1427 | if (r < 0) |
ebcf1f97 | 1428 | return r; |
cc377381 LP |
1429 | if (r == 0) |
1430 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1431 | ||
08237f06 | 1432 | r = attach_device(m, seat, sysfs, error); |
cc377381 | 1433 | if (r < 0) |
ebcf1f97 | 1434 | return r; |
cc377381 | 1435 | |
df2d202e | 1436 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
1437 | } |
1438 | ||
19070062 | 1439 | static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1440 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
1441 | int interactive, r; |
1442 | ||
cc377381 | 1443 | assert(message); |
cc377381 LP |
1444 | |
1445 | r = sd_bus_message_read(message, "b", &interactive); | |
1446 | if (r < 0) | |
ebcf1f97 | 1447 | return r; |
cc377381 | 1448 | |
f3885791 LP |
1449 | r = bus_verify_polkit_async( |
1450 | message, | |
1451 | CAP_SYS_ADMIN, | |
1452 | "org.freedesktop.login1.flush-devices", | |
403ed0e5 | 1453 | NULL, |
f3885791 | 1454 | interactive, |
c529695e | 1455 | UID_INVALID, |
f3885791 LP |
1456 | &m->polkit_registry, |
1457 | error); | |
cc377381 | 1458 | if (r < 0) |
ebcf1f97 | 1459 | return r; |
cc377381 LP |
1460 | if (r == 0) |
1461 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1462 | ||
1463 | r = flush_devices(m); | |
1464 | if (r < 0) | |
ebcf1f97 | 1465 | return r; |
cc377381 | 1466 | |
df2d202e | 1467 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
1468 | } |
1469 | ||
89f13440 | 1470 | static int have_multiple_sessions( |
89f13440 | 1471 | Manager *m, |
409133be | 1472 | uid_t uid) { |
89f13440 | 1473 | |
2154761f | 1474 | Session *session; |
89f13440 LP |
1475 | |
1476 | assert(m); | |
1477 | ||
1ca04b87 LP |
1478 | /* Check for other users' sessions. Greeter sessions do not |
1479 | * count, and non-login sessions do not count either. */ | |
90e74a66 | 1480 | HASHMAP_FOREACH(session, m->sessions) |
1ca04b87 | 1481 | if (session->class == SESSION_USER && |
22c902fa | 1482 | session->user->user_record->uid != uid) |
2154761f | 1483 | return true; |
89f13440 LP |
1484 | |
1485 | return false; | |
1486 | } | |
1487 | ||
836fb00f YA |
1488 | static int bus_manager_log_shutdown( |
1489 | Manager *m, | |
b0a3a5e5 | 1490 | const HandleActionData *a) { |
5ed73478 LN |
1491 | assert(m); |
1492 | assert(a); | |
836fb00f | 1493 | |
0cb09bcb | 1494 | const char *message = a->message ?: "System is shutting down"; |
153d1579 | 1495 | const char *log_verb = a->log_verb ? strjoina("SHUTDOWN=", a->log_verb) : NULL; |
836fb00f | 1496 | |
5ed73478 | 1497 | return log_struct(LOG_NOTICE, |
0cb09bcb ZJS |
1498 | "MESSAGE_ID=%s", a->message_id ?: SD_MESSAGE_SHUTDOWN_STR, |
1499 | LOG_MESSAGE("%s%s%s%s.", | |
1500 | message, | |
1501 | m->wall_message ? " (" : "", | |
1502 | strempty(m->wall_message), | |
1503 | m->wall_message ? ")" : ""), | |
153d1579 | 1504 | log_verb); |
314b4b0a LP |
1505 | } |
1506 | ||
b5d3e168 | 1507 | static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) { |
99534007 | 1508 | Manager *m = ASSERT_PTR(userdata); |
b5d3e168 KS |
1509 | |
1510 | assert(e); | |
b5d3e168 KS |
1511 | |
1512 | m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source); | |
1513 | return 0; | |
1514 | } | |
1515 | ||
1516 | int manager_set_lid_switch_ignore(Manager *m, usec_t until) { | |
1517 | int r; | |
1518 | ||
1519 | assert(m); | |
1520 | ||
1521 | if (until <= now(CLOCK_MONOTONIC)) | |
1522 | return 0; | |
1523 | ||
1524 | /* We want to ignore the lid switch for a while after each | |
1525 | * suspend, and after boot-up. Hence let's install a timer for | |
1526 | * this. As long as the event source exists we ignore the lid | |
1527 | * switch. */ | |
1528 | ||
1529 | if (m->lid_switch_ignore_event_source) { | |
1530 | usec_t u; | |
1531 | ||
1532 | r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u); | |
1533 | if (r < 0) | |
1534 | return r; | |
1535 | ||
1536 | if (until <= u) | |
1537 | return 0; | |
1538 | ||
1539 | r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until); | |
1540 | } else | |
6a0f1f6d LP |
1541 | r = sd_event_add_time( |
1542 | m->event, | |
1543 | &m->lid_switch_ignore_event_source, | |
1544 | CLOCK_MONOTONIC, | |
1545 | until, 0, | |
1546 | lid_switch_ignore_handler, m); | |
b5d3e168 KS |
1547 | |
1548 | return r; | |
1549 | } | |
1550 | ||
e4aab5cf LB |
1551 | static int send_prepare_for(Manager *m, const HandleActionData *a, bool _active) { |
1552 | int k = 0, r, active = _active; | |
6d7f7fd4 AJ |
1553 | |
1554 | assert(m); | |
e4aab5cf LB |
1555 | assert(a); |
1556 | assert(IN_SET(a->inhibit_what, INHIBIT_SHUTDOWN, INHIBIT_SLEEP)); | |
1557 | ||
1558 | /* We need to send both old and new signal for backward compatibility. The newer one allows clients | |
1559 | * to know which type of reboot is going to happen, as they might be doing different actions (e.g.: | |
1560 | * on soft-reboot), and it is sent first, so that clients know that if they receive the old one | |
1561 | * first then they don't have to wait for the new one, as it means it's not supported. So, do not | |
1562 | * change the order here, as it is an API. */ | |
1563 | if (a->inhibit_what == INHIBIT_SHUTDOWN) { | |
1564 | k = sd_bus_emit_signal(m->bus, | |
1565 | "/org/freedesktop/login1", | |
1566 | "org.freedesktop.login1.Manager", | |
1567 | "PrepareForShutdownWithMetadata", | |
1568 | "ba{sv}", | |
1569 | active, | |
1570 | 1, | |
1571 | "type", | |
1572 | "s", | |
1573 | handle_action_to_string(a->handle)); | |
1574 | if (k < 0) | |
1575 | log_debug_errno(k, "Failed to emit PrepareForShutdownWithMetadata(): %m"); | |
1576 | } | |
1577 | ||
1578 | r = sd_bus_emit_signal(m->bus, | |
1579 | "/org/freedesktop/login1", | |
1580 | "org.freedesktop.login1.Manager", | |
1581 | a->inhibit_what == INHIBIT_SHUTDOWN ? "PrepareForShutdown" : "PrepareForSleep", | |
1582 | "b", | |
1583 | active); | |
1584 | if (r < 0) | |
1585 | log_debug_errno(r, "Failed to emit PrepareForShutdown(): %m"); | |
1586 | ||
1587 | return RET_GATHER(k, r); | |
6d7f7fd4 AJ |
1588 | } |
1589 | ||
314b4b0a LP |
1590 | static int execute_shutdown_or_sleep( |
1591 | Manager *m, | |
b0a3a5e5 | 1592 | const HandleActionData *a, |
cc377381 | 1593 | sd_bus_error *error) { |
314b4b0a | 1594 | |
4afd3348 | 1595 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
cc377381 | 1596 | const char *p; |
cc377381 | 1597 | int r; |
eecd1362 | 1598 | |
af9792ac | 1599 | assert(m); |
5ed73478 | 1600 | assert(a); |
eecd1362 | 1601 | |
5ed73478 LN |
1602 | if (a->inhibit_what == INHIBIT_SHUTDOWN) |
1603 | bus_manager_log_shutdown(m, a); | |
314b4b0a | 1604 | |
5d990cc5 | 1605 | r = bus_call_method( |
df75a1a8 | 1606 | m->bus, |
5d990cc5 | 1607 | bus_systemd_mgr, |
df75a1a8 AJ |
1608 | "StartUnit", |
1609 | error, | |
1610 | &reply, | |
5ed73478 | 1611 | "ss", a->target, "replace-irreversibly"); |
df75a1a8 | 1612 | if (r < 0) |
6d7f7fd4 | 1613 | goto error; |
af9792ac | 1614 | |
df75a1a8 AJ |
1615 | r = sd_bus_message_read(reply, "o", &p); |
1616 | if (r < 0) | |
6d7f7fd4 | 1617 | goto error; |
af9792ac | 1618 | |
6b9f8b71 YW |
1619 | r = free_and_strdup(&m->action_job, p); |
1620 | if (r < 0) | |
6d7f7fd4 | 1621 | goto error; |
af9792ac | 1622 | |
5ed73478 | 1623 | m->delayed_action = a; |
af9792ac | 1624 | |
f9cd6be1 | 1625 | /* Make sure the lid switch is ignored for a while */ |
496db330 | 1626 | manager_set_lid_switch_ignore(m, usec_add(now(CLOCK_MONOTONIC), m->holdoff_timeout_usec)); |
f9cd6be1 | 1627 | |
af9792ac | 1628 | return 0; |
6d7f7fd4 AJ |
1629 | |
1630 | error: | |
1631 | /* Tell people that they now may take a lock again */ | |
e4aab5cf | 1632 | (void) send_prepare_for(m, a, false); |
6d7f7fd4 AJ |
1633 | |
1634 | return r; | |
eecd1362 LP |
1635 | } |
1636 | ||
418b22b8 | 1637 | int manager_dispatch_delayed(Manager *manager, bool timeout) { |
4afd3348 | 1638 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
c0f32805 | 1639 | Inhibitor *offending = NULL; |
c0f32805 DM |
1640 | int r; |
1641 | ||
1642 | assert(manager); | |
c0f32805 | 1643 | |
5ed73478 | 1644 | if (!manager->delayed_action || manager->action_job) |
c0f32805 DM |
1645 | return 0; |
1646 | ||
5ed73478 | 1647 | if (manager_is_inhibited(manager, manager->delayed_action->inhibit_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) { |
c0f32805 DM |
1648 | _cleanup_free_ char *comm = NULL, *u = NULL; |
1649 | ||
418b22b8 DM |
1650 | if (!timeout) |
1651 | return 0; | |
1652 | ||
89bad70f | 1653 | (void) get_process_comm(offending->pid.pid, &comm); |
c0f32805 DM |
1654 | u = uid_to_name(offending->uid); |
1655 | ||
1656 | log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.", | |
1657 | offending->uid, strna(u), | |
89bad70f | 1658 | offending->pid.pid, strna(comm)); |
c0f32805 DM |
1659 | } |
1660 | ||
1661 | /* Actually do the operation */ | |
5ed73478 | 1662 | r = execute_shutdown_or_sleep(manager, manager->delayed_action, &error); |
c0f32805 | 1663 | if (r < 0) { |
6d7f7fd4 AJ |
1664 | log_warning("Error during inhibitor-delayed operation (already returned success to client): %s", |
1665 | bus_error_message(&error, r)); | |
c0f32805 | 1666 | |
5ed73478 | 1667 | manager->delayed_action = NULL; |
c0f32805 DM |
1668 | } |
1669 | ||
8207b832 | 1670 | return 1; /* We did some work. */ |
418b22b8 DM |
1671 | } |
1672 | ||
1673 | static int manager_inhibit_timeout_handler( | |
1674 | sd_event_source *s, | |
1675 | uint64_t usec, | |
1676 | void *userdata) { | |
1677 | ||
99534007 | 1678 | Manager *manager = ASSERT_PTR(userdata); |
418b22b8 | 1679 | |
418b22b8 DM |
1680 | assert(manager->inhibit_timeout_source == s); |
1681 | ||
8207b832 | 1682 | return manager_dispatch_delayed(manager, true); |
c0f32805 DM |
1683 | } |
1684 | ||
314b4b0a LP |
1685 | static int delay_shutdown_or_sleep( |
1686 | Manager *m, | |
b0a3a5e5 | 1687 | const HandleActionData *a) { |
eecd1362 | 1688 | |
c0f32805 | 1689 | int r; |
c0f32805 | 1690 | |
eecd1362 | 1691 | assert(m); |
5ed73478 | 1692 | assert(a); |
eecd1362 | 1693 | |
c0f32805 | 1694 | if (m->inhibit_timeout_source) { |
39cf0351 | 1695 | r = sd_event_source_set_time_relative(m->inhibit_timeout_source, m->inhibit_delay_max); |
c0f32805 | 1696 | if (r < 0) |
39cf0351 | 1697 | return log_error_errno(r, "sd_event_source_set_time_relative() failed: %m"); |
c0f32805 DM |
1698 | |
1699 | r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT); | |
1700 | if (r < 0) | |
c2a23db0 | 1701 | return log_error_errno(r, "sd_event_source_set_enabled() failed: %m"); |
c0f32805 | 1702 | } else { |
39cf0351 LP |
1703 | r = sd_event_add_time_relative( |
1704 | m->event, | |
1705 | &m->inhibit_timeout_source, | |
1706 | CLOCK_MONOTONIC, m->inhibit_delay_max, 0, | |
1707 | manager_inhibit_timeout_handler, m); | |
c0f32805 DM |
1708 | if (r < 0) |
1709 | return r; | |
1710 | } | |
1711 | ||
5ed73478 | 1712 | m->delayed_action = a; |
d889a206 LP |
1713 | |
1714 | return 0; | |
1715 | } | |
1716 | ||
069cfc85 LP |
1717 | int bus_manager_shutdown_or_sleep_now_or_later( |
1718 | Manager *m, | |
b0a3a5e5 | 1719 | const HandleActionData *a, |
cc377381 | 1720 | sd_bus_error *error) { |
069cfc85 | 1721 | |
c8c8ee85 | 1722 | _cleanup_free_ char *load_state = NULL; |
069cfc85 LP |
1723 | bool delayed; |
1724 | int r; | |
1725 | ||
1726 | assert(m); | |
5ed73478 | 1727 | assert(a); |
af9792ac | 1728 | assert(!m->action_job); |
069cfc85 | 1729 | |
5ed73478 | 1730 | r = unit_load_state(m->bus, a->target, &load_state); |
c8c8ee85 ZJS |
1731 | if (r < 0) |
1732 | return r; | |
1733 | ||
baaa35ad ZJS |
1734 | if (!streq(load_state, "loaded")) |
1735 | return log_notice_errno(SYNTHETIC_ERRNO(EACCES), | |
1736 | "Unit %s is %s, refusing operation.", | |
5ed73478 | 1737 | a->target, load_state); |
c8c8ee85 | 1738 | |
314b4b0a | 1739 | /* Tell everybody to prepare for shutdown/sleep */ |
e4aab5cf | 1740 | (void) send_prepare_for(m, a, true); |
314b4b0a | 1741 | |
069cfc85 LP |
1742 | delayed = |
1743 | m->inhibit_delay_max > 0 && | |
5ed73478 | 1744 | manager_is_inhibited(m, a->inhibit_what, INHIBIT_DELAY, NULL, false, false, 0, NULL); |
069cfc85 LP |
1745 | |
1746 | if (delayed) | |
1747 | /* Shutdown is delayed, keep in mind what we | |
1748 | * want to do, and start a timeout */ | |
5ed73478 | 1749 | r = delay_shutdown_or_sleep(m, a); |
314b4b0a | 1750 | else |
069cfc85 LP |
1751 | /* Shutdown is not delayed, execute it |
1752 | * immediately */ | |
5ed73478 | 1753 | r = execute_shutdown_or_sleep(m, a, error); |
069cfc85 LP |
1754 | |
1755 | return r; | |
1756 | } | |
1757 | ||
b7aa9589 | 1758 | static int verify_shutdown_creds( |
d889a206 | 1759 | Manager *m, |
cc377381 | 1760 | sd_bus_message *message, |
b0a3a5e5 | 1761 | const HandleActionData *a, |
8885fed4 | 1762 | uint64_t flags, |
ebcf1f97 | 1763 | sd_bus_error *error) { |
d889a206 | 1764 | |
4afd3348 | 1765 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
8885fed4 | 1766 | bool multiple_sessions, blocked, interactive; |
cc377381 | 1767 | uid_t uid; |
b7aa9589 | 1768 | int r; |
d889a206 LP |
1769 | |
1770 | assert(m); | |
5ed73478 | 1771 | assert(a); |
d889a206 | 1772 | assert(message); |
6524990f | 1773 | |
05bae4a6 | 1774 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); |
5b12334d LP |
1775 | if (r < 0) |
1776 | return r; | |
1777 | ||
05bae4a6 | 1778 | r = sd_bus_creds_get_euid(creds, &uid); |
cc377381 | 1779 | if (r < 0) |
ebcf1f97 | 1780 | return r; |
409133be | 1781 | |
cc377381 | 1782 | r = have_multiple_sessions(m, uid); |
d889a206 | 1783 | if (r < 0) |
ebcf1f97 | 1784 | return r; |
d889a206 LP |
1785 | |
1786 | multiple_sessions = r > 0; | |
5ed73478 | 1787 | blocked = manager_is_inhibited(m, a->inhibit_what, INHIBIT_BLOCK, NULL, false, true, uid, NULL); |
8885fed4 | 1788 | interactive = flags & SD_LOGIND_INTERACTIVE; |
d889a206 | 1789 | |
878e32b6 | 1790 | if (multiple_sessions) { |
5ed73478 LN |
1791 | r = bus_verify_polkit_async( |
1792 | message, | |
1793 | CAP_SYS_BOOT, | |
1794 | a->polkit_action_multiple_sessions, | |
1795 | NULL, | |
1796 | interactive, | |
1797 | UID_INVALID, | |
1798 | &m->polkit_registry, | |
1799 | error); | |
d889a206 | 1800 | if (r < 0) |
ebcf1f97 | 1801 | return r; |
055d4066 ZJS |
1802 | if (r == 0) |
1803 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
d889a206 LP |
1804 | } |
1805 | ||
8885fed4 DR |
1806 | if (blocked) { |
1807 | /* We don't check polkit for root here, because you can't be more privileged than root */ | |
1808 | if (uid == 0 && (flags & SD_LOGIND_ROOT_CHECK_INHIBITORS)) | |
1809 | return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, | |
1810 | "Access denied to root due to active block inhibitor"); | |
1811 | ||
5ed73478 LN |
1812 | r = bus_verify_polkit_async(message, |
1813 | CAP_SYS_BOOT, | |
1814 | a->polkit_action_ignore_inhibit, | |
1815 | NULL, | |
1816 | interactive, | |
1817 | UID_INVALID, | |
1818 | &m->polkit_registry, | |
1819 | error); | |
878e32b6 FB |
1820 | if (r < 0) |
1821 | return r; | |
1822 | if (r == 0) | |
1823 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
d889a206 LP |
1824 | } |
1825 | ||
878e32b6 | 1826 | if (!multiple_sessions && !blocked) { |
5ed73478 LN |
1827 | r = bus_verify_polkit_async(message, |
1828 | CAP_SYS_BOOT, | |
1829 | a->polkit_action, | |
1830 | NULL, | |
1831 | interactive, | |
1832 | UID_INVALID, | |
1833 | &m->polkit_registry, | |
1834 | error); | |
d889a206 | 1835 | if (r < 0) |
ebcf1f97 | 1836 | return r; |
055d4066 ZJS |
1837 | if (r == 0) |
1838 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
d889a206 LP |
1839 | } |
1840 | ||
b7aa9589 DM |
1841 | return 0; |
1842 | } | |
1843 | ||
90b1ec03 LN |
1844 | static int setup_wall_message_timer(Manager *m, sd_bus_message* message) { |
1845 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
1846 | int r; | |
1847 | ||
1848 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds); | |
1849 | if (r >= 0) { | |
1850 | const char *tty = NULL; | |
1851 | ||
1852 | (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid); | |
1853 | (void) sd_bus_creds_get_tty(creds, &tty); | |
1854 | ||
1855 | r = free_and_strdup(&m->scheduled_shutdown_tty, tty); | |
1856 | if (r < 0) | |
1857 | return log_oom(); | |
1858 | } | |
1859 | ||
1860 | r = manager_setup_wall_message_timer(m); | |
1861 | if (r < 0) | |
1862 | return r; | |
1863 | ||
1864 | return 0; | |
1865 | } | |
1866 | ||
b7aa9589 DM |
1867 | static int method_do_shutdown_or_sleep( |
1868 | Manager *m, | |
1869 | sd_bus_message *message, | |
b0a3a5e5 | 1870 | const HandleActionData *a, |
8885fed4 | 1871 | bool with_flags, |
b7aa9589 DM |
1872 | sd_bus_error *error) { |
1873 | ||
3ecafb1f LP |
1874 | uint64_t flags; |
1875 | int r; | |
b7aa9589 DM |
1876 | |
1877 | assert(m); | |
1878 | assert(message); | |
5ed73478 | 1879 | assert(a); |
b7aa9589 | 1880 | |
3ecafb1f LP |
1881 | if (with_flags) { |
1882 | /* New style method: with flags parameter (and interactive bool in the bus message header) */ | |
8885fed4 | 1883 | r = sd_bus_message_read(message, "t", &flags); |
3ecafb1f LP |
1884 | if (r < 0) |
1885 | return r; | |
1886 | if ((flags & ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC) != 0) | |
b2800613 ZJS |
1887 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, |
1888 | "Invalid flags parameter"); | |
9edf5af5 LP |
1889 | |
1890 | if (FLAGS_SET(flags, (SD_LOGIND_REBOOT_VIA_KEXEC|SD_LOGIND_SOFT_REBOOT))) | |
b2800613 | 1891 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, |
9edf5af5 LP |
1892 | "Both reboot via kexec and soft reboot selected, which is not supported"); |
1893 | ||
1894 | if (a->handle != HANDLE_REBOOT) { | |
1895 | if (flags & SD_LOGIND_REBOOT_VIA_KEXEC) | |
1896 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, | |
1897 | "Reboot via kexec option is only applicable with reboot operations"); | |
1898 | if (flags & SD_LOGIND_SOFT_REBOOT) | |
1899 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, | |
1900 | "Soft reboot option is only applicable with reboot operations"); | |
1901 | } | |
3ecafb1f LP |
1902 | } else { |
1903 | /* Old style method: no flags parameter, but interactive bool passed as boolean in | |
1904 | * payload. Let's convert this argument to the new-style flags parameter for our internal | |
1905 | * use. */ | |
1906 | int interactive; | |
b7aa9589 | 1907 | |
3ecafb1f LP |
1908 | r = sd_bus_message_read(message, "b", &interactive); |
1909 | if (r < 0) | |
1910 | return r; | |
8885fed4 | 1911 | |
3ecafb1f LP |
1912 | flags = interactive ? SD_LOGIND_INTERACTIVE : 0; |
1913 | } | |
8885fed4 | 1914 | |
1d8a16f1 | 1915 | if ((flags & SD_LOGIND_REBOOT_VIA_KEXEC) && kexec_loaded()) |
48bccaa9 | 1916 | a = handle_action_lookup(HANDLE_KEXEC); |
9edf5af5 LP |
1917 | else if ((flags & SD_LOGIND_SOFT_REBOOT)) |
1918 | a = handle_action_lookup(HANDLE_SOFT_REBOOT); | |
0d96caa5 | 1919 | |
b7aa9589 | 1920 | /* Don't allow multiple jobs being executed at the same time */ |
5ed73478 | 1921 | if (m->delayed_action) |
124d7cb2 ZJS |
1922 | return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, |
1923 | "There's already a shutdown or sleep operation in progress"); | |
b7aa9589 | 1924 | |
5ed73478 LN |
1925 | if (a->sleep_operation >= 0) { |
1926 | r = can_sleep(a->sleep_operation); | |
b71c9758 | 1927 | if (r == -ENOSPC) |
8340b762 | 1928 | return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, |
43cb0f3e | 1929 | "Not enough suitable swap space for hibernation available on compatible block devices and file systems"); |
b71c9758 | 1930 | if (r == 0) |
8340b762 | 1931 | return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, |
b2800613 ZJS |
1932 | "Sleep verb \"%s\" not supported", |
1933 | sleep_operation_to_string(a->sleep_operation)); | |
b7aa9589 DM |
1934 | if (r < 0) |
1935 | return r; | |
b7aa9589 DM |
1936 | } |
1937 | ||
5ed73478 | 1938 | r = verify_shutdown_creds(m, message, a, flags, error); |
b7aa9589 DM |
1939 | if (r != 0) |
1940 | return r; | |
1941 | ||
5ed73478 LN |
1942 | /* reset case we're shorting a scheduled shutdown */ |
1943 | m->unlink_nologin = false; | |
1944 | reset_scheduled_shutdown(m); | |
1945 | ||
1946 | m->scheduled_shutdown_timeout = 0; | |
a3ddcc11 | 1947 | m->scheduled_shutdown_action = a; |
5ed73478 | 1948 | |
90b1ec03 LN |
1949 | (void) setup_wall_message_timer(m, message); |
1950 | ||
5ed73478 | 1951 | r = bus_manager_shutdown_or_sleep_now_or_later(m, a, error); |
d889a206 | 1952 | if (r < 0) |
ebcf1f97 | 1953 | return r; |
d889a206 | 1954 | |
df2d202e | 1955 | return sd_bus_reply_method_return(message, NULL); |
eecd1362 LP |
1956 | } |
1957 | ||
19070062 | 1958 | static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
3f49d45a LP |
1959 | Manager *m = userdata; |
1960 | ||
cc377381 LP |
1961 | return method_do_shutdown_or_sleep( |
1962 | m, message, | |
48bccaa9 | 1963 | handle_action_lookup(HANDLE_POWEROFF), |
8885fed4 | 1964 | sd_bus_message_is_method_call(message, NULL, "PowerOffWithFlags"), |
ebcf1f97 | 1965 | error); |
cc377381 | 1966 | } |
88e3dc90 | 1967 | |
19070062 | 1968 | static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1969 | Manager *m = userdata; |
88e3dc90 | 1970 | |
cc377381 LP |
1971 | return method_do_shutdown_or_sleep( |
1972 | m, message, | |
48bccaa9 | 1973 | handle_action_lookup(HANDLE_REBOOT), |
8885fed4 | 1974 | sd_bus_message_is_method_call(message, NULL, "RebootWithFlags"), |
ebcf1f97 | 1975 | error); |
cc377381 | 1976 | } |
88e3dc90 | 1977 | |
36b69c31 LP |
1978 | static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1979 | Manager *m = userdata; | |
1980 | ||
1981 | return method_do_shutdown_or_sleep( | |
1982 | m, message, | |
48bccaa9 | 1983 | handle_action_lookup(HANDLE_HALT), |
8885fed4 | 1984 | sd_bus_message_is_method_call(message, NULL, "HaltWithFlags"), |
36b69c31 LP |
1985 | error); |
1986 | } | |
1987 | ||
19070062 | 1988 | static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1989 | Manager *m = userdata; |
88e3dc90 | 1990 | |
cc377381 LP |
1991 | return method_do_shutdown_or_sleep( |
1992 | m, message, | |
48bccaa9 | 1993 | handle_action_lookup(HANDLE_SUSPEND), |
8885fed4 | 1994 | sd_bus_message_is_method_call(message, NULL, "SuspendWithFlags"), |
ebcf1f97 | 1995 | error); |
cc377381 | 1996 | } |
88e3dc90 | 1997 | |
15e99a43 LP |
1998 | static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1999 | Manager *m = userdata; | |
2000 | ||
2001 | return method_do_shutdown_or_sleep( | |
2002 | m, message, | |
48bccaa9 | 2003 | handle_action_lookup(HANDLE_HIBERNATE), |
8885fed4 | 2004 | sd_bus_message_is_method_call(message, NULL, "HibernateWithFlags"), |
15e99a43 LP |
2005 | error); |
2006 | } | |
2007 | ||
2008 | static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2009 | Manager *m = userdata; | |
2010 | ||
2011 | return method_do_shutdown_or_sleep( | |
2012 | m, message, | |
48bccaa9 | 2013 | handle_action_lookup(HANDLE_HYBRID_SLEEP), |
8885fed4 | 2014 | sd_bus_message_is_method_call(message, NULL, "HybridSleepWithFlags"), |
15e99a43 LP |
2015 | error); |
2016 | } | |
2017 | ||
e68c79db | 2018 | static int method_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c58493c0 ML |
2019 | Manager *m = userdata; |
2020 | ||
2021 | return method_do_shutdown_or_sleep( | |
2022 | m, message, | |
48bccaa9 | 2023 | handle_action_lookup(HANDLE_SUSPEND_THEN_HIBERNATE), |
8885fed4 | 2024 | sd_bus_message_is_method_call(message, NULL, "SuspendThenHibernateWithFlags"), |
c58493c0 ML |
2025 | error); |
2026 | } | |
2027 | ||
867c37f6 DM |
2028 | static int nologin_timeout_handler( |
2029 | sd_event_source *s, | |
2030 | uint64_t usec, | |
2031 | void *userdata) { | |
2032 | ||
2033 | Manager *m = userdata; | |
867c37f6 DM |
2034 | |
2035 | log_info("Creating /run/nologin, blocking further logins..."); | |
2036 | ||
6e11e7e6 LP |
2037 | m->unlink_nologin = |
2038 | create_shutdown_run_nologin_or_warn() >= 0; | |
867c37f6 DM |
2039 | |
2040 | return 0; | |
2041 | } | |
2042 | ||
cbb6c000 LP |
2043 | static usec_t nologin_timeout_usec(usec_t elapse) { |
2044 | /* Issue /run/nologin five minutes before shutdown */ | |
2045 | return LESS_BY(elapse, 5 * USEC_PER_MINUTE); | |
2046 | } | |
2047 | ||
aa6123e8 LN |
2048 | void manager_load_scheduled_shutdown(Manager *m) { |
2049 | _cleanup_fclose_ FILE *f = NULL; | |
2050 | _cleanup_free_ char *usec = NULL, | |
2051 | *warn_wall = NULL, | |
2052 | *mode = NULL, | |
2053 | *wall_message = NULL, | |
2054 | *uid = NULL, | |
2055 | *tty = NULL; | |
2056 | int r; | |
2057 | ||
2058 | assert(m); | |
2059 | ||
2060 | r = parse_env_file(f, SHUTDOWN_SCHEDULE_FILE, | |
2061 | "USEC", &usec, | |
2062 | "WARN_WALL", &warn_wall, | |
2063 | "MODE", &mode, | |
2064 | "WALL_MESSAGE", &wall_message, | |
2065 | "UID", &uid, | |
2066 | "TTY", &tty); | |
2067 | ||
2068 | /* reset will delete the file */ | |
2069 | reset_scheduled_shutdown(m); | |
2070 | ||
2071 | if (r == -ENOENT) | |
2072 | return; | |
2073 | if (r < 0) | |
2074 | return (void) log_debug_errno(r, "Failed to parse " SHUTDOWN_SCHEDULE_FILE ": %m"); | |
2075 | ||
2076 | HandleAction handle = handle_action_from_string(mode); | |
2077 | if (handle < 0) | |
2078 | return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse scheduled shutdown type: %s", mode); | |
2079 | ||
2080 | if (!usec) | |
2081 | return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "USEC is required"); | |
2082 | if (deserialize_usec(usec, &m->scheduled_shutdown_timeout) < 0) | |
2083 | return; | |
2084 | ||
2085 | /* assign parsed type only after we know usec is also valid */ | |
a3ddcc11 | 2086 | m->scheduled_shutdown_action = handle_action_lookup(handle); |
aa6123e8 LN |
2087 | |
2088 | if (warn_wall) { | |
2089 | r = parse_boolean(warn_wall); | |
2090 | if (r < 0) | |
2091 | log_debug_errno(r, "Failed to parse enabling wall messages"); | |
2092 | else | |
2093 | m->enable_wall_messages = r; | |
2094 | } | |
2095 | ||
2096 | if (wall_message) { | |
2097 | _cleanup_free_ char *unescaped = NULL; | |
2098 | r = cunescape(wall_message, 0, &unescaped); | |
2099 | if (r < 0) | |
2100 | log_debug_errno(r, "Failed to parse wall message: %s", wall_message); | |
2101 | else | |
2102 | free_and_replace(m->wall_message, unescaped); | |
2103 | } | |
2104 | ||
2105 | if (uid) { | |
2106 | r = parse_uid(uid, &m->scheduled_shutdown_uid); | |
2107 | if (r < 0) | |
2108 | log_debug_errno(r, "Failed to parse wall uid: %s", uid); | |
2109 | } | |
2110 | ||
2111 | free_and_replace(m->scheduled_shutdown_tty, tty); | |
2112 | ||
2113 | r = manager_setup_shutdown_timers(m); | |
2114 | if (r < 0) | |
2115 | return reset_scheduled_shutdown(m); | |
2116 | ||
2117 | (void) manager_setup_wall_message_timer(m); | |
2118 | (void) update_schedule_file(m); | |
2119 | ||
2120 | return; | |
2121 | } | |
2122 | ||
867c37f6 | 2123 | static int update_schedule_file(Manager *m) { |
70f1280c | 2124 | _cleanup_(unlink_and_freep) char *temp_path = NULL; |
867c37f6 | 2125 | _cleanup_fclose_ FILE *f = NULL; |
91b3e7fb | 2126 | int r; |
867c37f6 DM |
2127 | |
2128 | assert(m); | |
a3ddcc11 | 2129 | assert(m->scheduled_shutdown_action); |
867c37f6 | 2130 | |
aa6123e8 | 2131 | r = mkdir_parents_label(SHUTDOWN_SCHEDULE_FILE, 0755); |
867c37f6 DM |
2132 | if (r < 0) |
2133 | return log_error_errno(r, "Failed to create shutdown subdirectory: %m"); | |
2134 | ||
aa6123e8 | 2135 | r = fopen_temporary(SHUTDOWN_SCHEDULE_FILE, &f, &temp_path); |
867c37f6 DM |
2136 | if (r < 0) |
2137 | return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m"); | |
2138 | ||
2139 | (void) fchmod(fileno(f), 0644); | |
2140 | ||
aa6123e8 LN |
2141 | serialize_usec(f, "USEC", m->scheduled_shutdown_timeout); |
2142 | serialize_item_format(f, "WARN_WALL", "%s", one_zero(m->enable_wall_messages)); | |
a3ddcc11 | 2143 | serialize_item_format(f, "MODE", "%s", handle_action_to_string(m->scheduled_shutdown_action->handle)); |
aa6123e8 | 2144 | serialize_item_format(f, "UID", UID_FMT, m->scheduled_shutdown_uid); |
867c37f6 | 2145 | |
aa6123e8 LN |
2146 | if (m->scheduled_shutdown_tty) |
2147 | serialize_item_format(f, "TTY", "%s", m->scheduled_shutdown_tty); | |
91b3e7fb | 2148 | |
aa6123e8 LN |
2149 | if (!isempty(m->wall_message)) { |
2150 | r = serialize_item_escaped(f, "WALL_MESSAGE", m->wall_message); | |
2151 | if (r < 0) | |
91b3e7fb | 2152 | goto fail; |
91b3e7fb | 2153 | } |
867c37f6 | 2154 | |
dacd6cee LP |
2155 | r = fflush_and_check(f); |
2156 | if (r < 0) | |
2157 | goto fail; | |
867c37f6 | 2158 | |
aa6123e8 | 2159 | if (rename(temp_path, SHUTDOWN_SCHEDULE_FILE) < 0) { |
867c37f6 | 2160 | r = -errno; |
dacd6cee | 2161 | goto fail; |
867c37f6 DM |
2162 | } |
2163 | ||
70f1280c | 2164 | temp_path = mfree(temp_path); |
dacd6cee LP |
2165 | return 0; |
2166 | ||
2167 | fail: | |
aa6123e8 | 2168 | (void) unlink(SHUTDOWN_SCHEDULE_FILE); |
dacd6cee LP |
2169 | |
2170 | return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m"); | |
867c37f6 DM |
2171 | } |
2172 | ||
df75a1a8 | 2173 | static void reset_scheduled_shutdown(Manager *m) { |
36b69c31 LP |
2174 | assert(m); |
2175 | ||
df75a1a8 AJ |
2176 | m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source); |
2177 | m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source); | |
2178 | m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source); | |
36b69c31 | 2179 | |
a3ddcc11 | 2180 | m->scheduled_shutdown_action = NULL; |
aa6123e8 LN |
2181 | m->scheduled_shutdown_timeout = USEC_INFINITY; |
2182 | m->scheduled_shutdown_uid = UID_INVALID; | |
ee3d33c4 LN |
2183 | m->scheduled_shutdown_tty = mfree(m->scheduled_shutdown_tty); |
2184 | m->wall_message = mfree(m->wall_message); | |
df75a1a8 AJ |
2185 | m->shutdown_dry_run = false; |
2186 | ||
2187 | if (m->unlink_nologin) { | |
af229d7a | 2188 | (void) unlink_or_warn("/run/nologin"); |
df75a1a8 AJ |
2189 | m->unlink_nologin = false; |
2190 | } | |
36b69c31 | 2191 | |
aa6123e8 | 2192 | (void) unlink(SHUTDOWN_SCHEDULE_FILE); |
df75a1a8 AJ |
2193 | } |
2194 | ||
8aaa023a DM |
2195 | static int manager_scheduled_shutdown_handler( |
2196 | sd_event_source *s, | |
2197 | uint64_t usec, | |
2198 | void *userdata) { | |
2199 | ||
b0a3a5e5 | 2200 | const HandleActionData *a = NULL; |
4afd3348 | 2201 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
99534007 | 2202 | Manager *m = ASSERT_PTR(userdata); |
8aaa023a DM |
2203 | int r; |
2204 | ||
a3ddcc11 | 2205 | a = m->scheduled_shutdown_action; |
5ed73478 | 2206 | assert(a); |
8aaa023a | 2207 | |
b498d6ea | 2208 | /* Don't allow multiple jobs being executed at the same time */ |
5ed73478 | 2209 | if (m->delayed_action) { |
6d7f7fd4 | 2210 | r = -EALREADY; |
5ed73478 | 2211 | log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", a->target); |
6d7f7fd4 | 2212 | goto error; |
b498d6ea AJ |
2213 | } |
2214 | ||
df75a1a8 AJ |
2215 | if (m->shutdown_dry_run) { |
2216 | /* We do not process delay inhibitors here. Otherwise, we | |
2217 | * would have to be considered "in progress" (like the check | |
2218 | * above) for some seconds after our admin has seen the final | |
2219 | * wall message. */ | |
2220 | ||
5ed73478 | 2221 | bus_manager_log_shutdown(m, a); |
df75a1a8 AJ |
2222 | log_info("Running in dry run, suppressing action."); |
2223 | reset_scheduled_shutdown(m); | |
2224 | ||
2225 | return 0; | |
2226 | } | |
2227 | ||
a3ddcc11 | 2228 | r = bus_manager_shutdown_or_sleep_now_or_later(m, m->scheduled_shutdown_action, &error); |
6d7f7fd4 | 2229 | if (r < 0) { |
5ed73478 | 2230 | log_error_errno(r, "Scheduled shutdown to %s failed: %m", a->target); |
6d7f7fd4 AJ |
2231 | goto error; |
2232 | } | |
8aaa023a DM |
2233 | |
2234 | return 0; | |
6d7f7fd4 AJ |
2235 | |
2236 | error: | |
2237 | reset_scheduled_shutdown(m); | |
2238 | return r; | |
8aaa023a DM |
2239 | } |
2240 | ||
19070062 | 2241 | static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 2242 | Manager *m = ASSERT_PTR(userdata); |
5ed73478 | 2243 | HandleAction handle; |
b0a3a5e5 | 2244 | const HandleActionData *a; |
8aaa023a DM |
2245 | uint64_t elapse; |
2246 | char *type; | |
2247 | int r; | |
d13f5e16 | 2248 | bool dry_run = false; |
8aaa023a | 2249 | |
8aaa023a DM |
2250 | assert(message); |
2251 | ||
2252 | r = sd_bus_message_read(message, "st", &type, &elapse); | |
2253 | if (r < 0) | |
2254 | return r; | |
2255 | ||
1389f4b9 DM |
2256 | if (startswith(type, "dry-")) { |
2257 | type += 4; | |
d13f5e16 | 2258 | dry_run = true; |
1389f4b9 DM |
2259 | } |
2260 | ||
5ed73478 | 2261 | handle = handle_action_from_string(type); |
cb88da82 LP |
2262 | if (!HANDLE_ACTION_IS_SHUTDOWN(handle)) |
2263 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type: %s", type); | |
8aaa023a | 2264 | |
48bccaa9 | 2265 | a = handle_action_lookup(handle); |
5ed73478 LN |
2266 | assert(a); |
2267 | assert(a->polkit_action); | |
2268 | ||
2269 | r = verify_shutdown_creds(m, message, a, 0, error); | |
8aaa023a DM |
2270 | if (r != 0) |
2271 | return r; | |
2272 | ||
a3ddcc11 | 2273 | m->scheduled_shutdown_action = a; |
d13f5e16 | 2274 | m->shutdown_dry_run = dry_run; |
8aaa023a DM |
2275 | m->scheduled_shutdown_timeout = elapse; |
2276 | ||
aa6123e8 LN |
2277 | r = manager_setup_shutdown_timers(m); |
2278 | if (r < 0) | |
2279 | return r; | |
2280 | ||
90b1ec03 | 2281 | r = setup_wall_message_timer(m, message); |
aa6123e8 LN |
2282 | if (r >= 0) |
2283 | r = update_schedule_file(m); | |
2284 | ||
90b1ec03 | 2285 | if (r < 0) { |
aa6123e8 | 2286 | reset_scheduled_shutdown(m); |
e2fa5721 | 2287 | return r; |
90b1ec03 | 2288 | } |
e2fa5721 | 2289 | |
aa6123e8 LN |
2290 | return sd_bus_reply_method_return(message, NULL); |
2291 | } | |
2292 | ||
2293 | static int manager_setup_shutdown_timers(Manager* m) { | |
2294 | int r; | |
2295 | ||
2296 | r = event_reset_time(m->event, &m->scheduled_shutdown_timeout_source, | |
2297 | CLOCK_REALTIME, | |
2298 | m->scheduled_shutdown_timeout, 0, | |
2299 | manager_scheduled_shutdown_handler, m, | |
2300 | 0, "scheduled-shutdown-timeout", true); | |
f8169e62 | 2301 | if (r < 0) |
aa6123e8 | 2302 | goto fail; |
867c37f6 | 2303 | |
aa6123e8 LN |
2304 | r = event_reset_time(m->event, &m->nologin_timeout_source, |
2305 | CLOCK_REALTIME, | |
2306 | nologin_timeout_usec(m->scheduled_shutdown_timeout), 0, | |
2307 | nologin_timeout_handler, m, | |
2308 | 0, "nologin-timeout", true); | |
2309 | if (r < 0) | |
2310 | goto fail; | |
2311 | ||
2312 | return 0; | |
2313 | ||
2314 | fail: | |
2315 | m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source); | |
2316 | m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source); | |
2317 | ||
2318 | return r; | |
8aaa023a DM |
2319 | } |
2320 | ||
19070062 | 2321 | static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 2322 | Manager *m = ASSERT_PTR(userdata); |
b0a3a5e5 | 2323 | const HandleActionData *a; |
8aaa023a | 2324 | bool cancelled; |
ec14fba9 | 2325 | int r; |
8aaa023a | 2326 | |
8aaa023a DM |
2327 | assert(message); |
2328 | ||
a3ddcc11 LP |
2329 | cancelled = m->scheduled_shutdown_action |
2330 | && !IN_SET(m->scheduled_shutdown_action->handle, HANDLE_IGNORE, _HANDLE_ACTION_INVALID); | |
ec14fba9 | 2331 | if (!cancelled) |
d2fc0ecf | 2332 | return sd_bus_reply_method_return(message, "b", false); |
ec14fba9 | 2333 | |
a3ddcc11 | 2334 | a = m->scheduled_shutdown_action; |
5ed73478 LN |
2335 | if (!a->polkit_action) |
2336 | return sd_bus_error_set(error, SD_BUS_ERROR_AUTH_FAILED, "Unsupported shutdown type"); | |
ec14fba9 LN |
2337 | |
2338 | r = bus_verify_polkit_async( | |
2339 | message, | |
2340 | CAP_SYS_BOOT, | |
5ed73478 | 2341 | a->polkit_action, |
ec14fba9 LN |
2342 | NULL, |
2343 | false, | |
2344 | UID_INVALID, | |
2345 | &m->polkit_registry, | |
2346 | error); | |
2347 | if (r < 0) | |
2348 | return r; | |
2349 | if (r == 0) | |
2350 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2351 | ||
ec14fba9 | 2352 | if (m->enable_wall_messages) { |
4afd3348 | 2353 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
e2fa5721 DM |
2354 | const char *tty = NULL; |
2355 | uid_t uid = 0; | |
e2fa5721 DM |
2356 | |
2357 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds); | |
2358 | if (r >= 0) { | |
2359 | (void) sd_bus_creds_get_uid(creds, &uid); | |
2360 | (void) sd_bus_creds_get_tty(creds, &tty); | |
2361 | } | |
2362 | ||
90750669 ZJS |
2363 | _cleanup_free_ char *username = uid_to_name(uid); |
2364 | ||
2365 | log_struct(LOG_INFO, | |
2366 | LOG_MESSAGE("System shutdown has been cancelled"), | |
2367 | "ACTION=%s", handle_action_to_string(a->handle), | |
87305b0f | 2368 | "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_CANCELED_STR, |
90750669 ZJS |
2369 | username ? "OPERATOR=%s" : NULL, username); |
2370 | ||
53c0397b TK |
2371 | (void) wall("System shutdown has been cancelled", |
2372 | username, tty, logind_wall_tty_filter, m); | |
e2fa5721 DM |
2373 | } |
2374 | ||
ea74f39c CG |
2375 | reset_scheduled_shutdown(m); |
2376 | ||
d2fc0ecf | 2377 | return sd_bus_reply_method_return(message, "b", true); |
8aaa023a DM |
2378 | } |
2379 | ||
cc377381 LP |
2380 | static int method_can_shutdown_or_sleep( |
2381 | Manager *m, | |
2382 | sd_bus_message *message, | |
b0a3a5e5 | 2383 | const HandleActionData *a, |
ebcf1f97 | 2384 | sd_bus_error *error) { |
de07ab16 | 2385 | |
4afd3348 | 2386 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
cc377381 LP |
2387 | bool multiple_sessions, challenge, blocked; |
2388 | const char *result = NULL; | |
2389 | uid_t uid; | |
2390 | int r; | |
de07ab16 | 2391 | |
cc377381 LP |
2392 | assert(m); |
2393 | assert(message); | |
5ed73478 LN |
2394 | assert(a); |
2395 | ||
2396 | if (a->sleep_operation >= 0) { | |
2397 | r = can_sleep(a->sleep_operation); | |
8340b762 | 2398 | if (IN_SET(r, 0, -ENOSPC)) |
b71c9758 | 2399 | return sd_bus_reply_method_return(message, "s", "na"); |
de07ab16 | 2400 | if (r < 0) |
ebcf1f97 | 2401 | return r; |
cc377381 | 2402 | } |
de07ab16 | 2403 | |
05bae4a6 | 2404 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); |
5b12334d LP |
2405 | if (r < 0) |
2406 | return r; | |
2407 | ||
05bae4a6 | 2408 | r = sd_bus_creds_get_euid(creds, &uid); |
cc377381 | 2409 | if (r < 0) |
ebcf1f97 | 2410 | return r; |
de07ab16 | 2411 | |
cc377381 LP |
2412 | r = have_multiple_sessions(m, uid); |
2413 | if (r < 0) | |
ebcf1f97 | 2414 | return r; |
de07ab16 | 2415 | |
cc377381 | 2416 | multiple_sessions = r > 0; |
5ed73478 | 2417 | blocked = manager_is_inhibited(m, a->inhibit_what, INHIBIT_BLOCK, NULL, false, true, uid, NULL); |
de07ab16 | 2418 | |
5ed73478 | 2419 | HandleAction handle = handle_action_from_string(sleep_operation_to_string(a->sleep_operation)); |
c8c8ee85 ZJS |
2420 | if (handle >= 0) { |
2421 | const char *target; | |
2422 | ||
48bccaa9 | 2423 | target = handle_action_lookup(handle)->target; |
c8c8ee85 ZJS |
2424 | if (target) { |
2425 | _cleanup_free_ char *load_state = NULL; | |
2426 | ||
2427 | r = unit_load_state(m->bus, target, &load_state); | |
2428 | if (r < 0) | |
2429 | return r; | |
2430 | ||
2431 | if (!streq(load_state, "loaded")) { | |
2432 | result = "no"; | |
2433 | goto finish; | |
2434 | } | |
2435 | } | |
2436 | } | |
2437 | ||
cc377381 | 2438 | if (multiple_sessions) { |
5ed73478 | 2439 | r = bus_test_polkit(message, CAP_SYS_BOOT, a->polkit_action_multiple_sessions, NULL, UID_INVALID, &challenge, error); |
de07ab16 | 2440 | if (r < 0) |
ebcf1f97 | 2441 | return r; |
bef422ae | 2442 | |
cc377381 LP |
2443 | if (r > 0) |
2444 | result = "yes"; | |
2445 | else if (challenge) | |
2446 | result = "challenge"; | |
2447 | else | |
2448 | result = "no"; | |
2449 | } | |
bef422ae | 2450 | |
cc377381 | 2451 | if (blocked) { |
5ed73478 | 2452 | r = bus_test_polkit(message, CAP_SYS_BOOT, a->polkit_action_ignore_inhibit, NULL, UID_INVALID, &challenge, error); |
bef422ae | 2453 | if (r < 0) |
ebcf1f97 | 2454 | return r; |
bef422ae | 2455 | |
0c093a62 HT |
2456 | if (r > 0) { |
2457 | if (!result) | |
2458 | result = "yes"; | |
2459 | } else if (challenge) { | |
2460 | if (!result || streq(result, "yes")) | |
2461 | result = "challenge"; | |
2462 | } else | |
cc377381 LP |
2463 | result = "no"; |
2464 | } | |
bef422ae | 2465 | |
cc377381 LP |
2466 | if (!multiple_sessions && !blocked) { |
2467 | /* If neither inhibit nor multiple sessions | |
2468 | * apply then just check the normal policy */ | |
bef422ae | 2469 | |
5ed73478 | 2470 | r = bus_test_polkit(message, CAP_SYS_BOOT, a->polkit_action, NULL, UID_INVALID, &challenge, error); |
bef422ae | 2471 | if (r < 0) |
ebcf1f97 | 2472 | return r; |
bef422ae | 2473 | |
cc377381 LP |
2474 | if (r > 0) |
2475 | result = "yes"; | |
2476 | else if (challenge) | |
2477 | result = "challenge"; | |
2478 | else | |
2479 | result = "no"; | |
2480 | } | |
bef422ae | 2481 | |
c8c8ee85 | 2482 | finish: |
df2d202e | 2483 | return sd_bus_reply_method_return(message, "s", result); |
cc377381 | 2484 | } |
bef422ae | 2485 | |
19070062 | 2486 | static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 2487 | Manager *m = userdata; |
bef422ae | 2488 | |
cc377381 | 2489 | return method_can_shutdown_or_sleep( |
48bccaa9 | 2490 | m, message, handle_action_lookup(HANDLE_POWEROFF), |
ebcf1f97 | 2491 | error); |
cc377381 | 2492 | } |
bef422ae | 2493 | |
19070062 | 2494 | static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 2495 | Manager *m = userdata; |
bef422ae | 2496 | |
cc377381 | 2497 | return method_can_shutdown_or_sleep( |
48bccaa9 | 2498 | m, message, handle_action_lookup(HANDLE_REBOOT), |
ebcf1f97 | 2499 | error); |
cc377381 | 2500 | } |
bef422ae | 2501 | |
36b69c31 LP |
2502 | static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2503 | Manager *m = userdata; | |
2504 | ||
2505 | return method_can_shutdown_or_sleep( | |
48bccaa9 | 2506 | m, message, handle_action_lookup(HANDLE_HALT), |
36b69c31 LP |
2507 | error); |
2508 | } | |
2509 | ||
19070062 | 2510 | static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 2511 | Manager *m = userdata; |
7f7bb946 | 2512 | |
cc377381 | 2513 | return method_can_shutdown_or_sleep( |
48bccaa9 | 2514 | m, message, handle_action_lookup(HANDLE_SUSPEND), |
ebcf1f97 | 2515 | error); |
cc377381 | 2516 | } |
7f7bb946 | 2517 | |
19070062 | 2518 | static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 2519 | Manager *m = userdata; |
02b16a19 | 2520 | |
cc377381 | 2521 | return method_can_shutdown_or_sleep( |
48bccaa9 | 2522 | m, message, handle_action_lookup(HANDLE_HIBERNATE), |
ebcf1f97 | 2523 | error); |
cc377381 | 2524 | } |
7f7bb946 | 2525 | |
19070062 | 2526 | static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 2527 | Manager *m = userdata; |
7f7bb946 | 2528 | |
cc377381 | 2529 | return method_can_shutdown_or_sleep( |
48bccaa9 | 2530 | m, message, handle_action_lookup(HANDLE_HYBRID_SLEEP), |
ebcf1f97 | 2531 | error); |
cc377381 | 2532 | } |
38f3fc7d | 2533 | |
e68c79db | 2534 | static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c58493c0 ML |
2535 | Manager *m = userdata; |
2536 | ||
2537 | return method_can_shutdown_or_sleep( | |
48bccaa9 | 2538 | m, message, handle_action_lookup(HANDLE_SUSPEND_THEN_HIBERNATE), |
c58493c0 ML |
2539 | error); |
2540 | } | |
2541 | ||
428b296a VJ |
2542 | static int property_get_reboot_parameter( |
2543 | sd_bus *bus, | |
2544 | const char *path, | |
2545 | const char *interface, | |
2546 | const char *property, | |
2547 | sd_bus_message *reply, | |
2548 | void *userdata, | |
2549 | sd_bus_error *error) { | |
2550 | _cleanup_free_ char *parameter = NULL; | |
2551 | int r; | |
2552 | ||
2553 | assert(bus); | |
2554 | assert(reply); | |
2555 | assert(userdata); | |
2556 | ||
2557 | r = read_reboot_parameter(¶meter); | |
2558 | if (r < 0) | |
2559 | return r; | |
2560 | ||
2561 | return sd_bus_message_append(reply, "s", parameter); | |
2562 | } | |
2563 | ||
2564 | static int method_set_reboot_parameter( | |
2565 | sd_bus_message *message, | |
2566 | void *userdata, | |
2567 | sd_bus_error *error) { | |
2568 | ||
99534007 | 2569 | Manager *m = ASSERT_PTR(userdata); |
428b296a VJ |
2570 | const char *arg; |
2571 | int r; | |
2572 | ||
2573 | assert(message); | |
428b296a VJ |
2574 | |
2575 | r = sd_bus_message_read(message, "s", &arg); | |
2576 | if (r < 0) | |
2577 | return r; | |
2578 | ||
2579 | r = detect_container(); | |
2580 | if (r < 0) | |
2581 | return r; | |
2582 | if (r > 0) | |
2583 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, | |
2584 | "Reboot parameter not supported in containers, refusing."); | |
2585 | ||
2586 | r = bus_verify_polkit_async(message, | |
2587 | CAP_SYS_ADMIN, | |
2588 | "org.freedesktop.login1.set-reboot-parameter", | |
2589 | NULL, | |
2590 | false, | |
2591 | UID_INVALID, | |
2592 | &m->polkit_registry, | |
2593 | error); | |
2594 | if (r < 0) | |
2595 | return r; | |
2596 | if (r == 0) | |
2597 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2598 | ||
2599 | r = update_reboot_parameter_and_warn(arg, false); | |
2600 | if (r < 0) | |
2601 | return r; | |
2602 | ||
2603 | return sd_bus_reply_method_return(message, NULL); | |
2604 | } | |
2605 | ||
2606 | static int method_can_reboot_parameter( | |
2607 | sd_bus_message *message, | |
2608 | void *userdata, | |
2609 | sd_bus_error *error) { | |
2610 | ||
99534007 | 2611 | _unused_ Manager *m = ASSERT_PTR(userdata); |
428b296a VJ |
2612 | int r; |
2613 | ||
2614 | assert(message); | |
428b296a VJ |
2615 | |
2616 | r = detect_container(); | |
2617 | if (r < 0) | |
2618 | return r; | |
2619 | if (r > 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */ | |
2620 | return sd_bus_reply_method_return(message, "s", "na"); | |
2621 | ||
2622 | return return_test_polkit( | |
2623 | message, | |
2624 | CAP_SYS_ADMIN, | |
2625 | "org.freedesktop.login1.set-reboot-parameter", | |
2626 | NULL, | |
2627 | UID_INVALID, | |
2628 | error); | |
2629 | } | |
2630 | ||
5bdf2243 JJ |
2631 | static int property_get_reboot_to_firmware_setup( |
2632 | sd_bus *bus, | |
2633 | const char *path, | |
2634 | const char *interface, | |
2635 | const char *property, | |
2636 | sd_bus_message *reply, | |
2637 | void *userdata, | |
2638 | sd_bus_error *error) { | |
2639 | int r; | |
2640 | ||
2641 | assert(bus); | |
2642 | assert(reply); | |
2643 | assert(userdata); | |
2644 | ||
e667266a LP |
2645 | r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP"); |
2646 | if (r == -ENXIO) { | |
2647 | /* EFI case: let's see what is currently configured in the EFI variables */ | |
2648 | r = efi_get_reboot_to_firmware(); | |
2649 | if (r < 0 && r != -EOPNOTSUPP) | |
2650 | log_warning_errno(r, "Failed to determine reboot-to-firmware-setup state: %m"); | |
2651 | } else if (r < 0) | |
2652 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m"); | |
2653 | else if (r > 0) { | |
2654 | /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */ | |
2655 | if (access("/run/systemd/reboot-to-firmware-setup", F_OK) < 0) { | |
2656 | if (errno != ENOENT) | |
2657 | log_warning_errno(errno, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m"); | |
2658 | ||
2659 | r = false; | |
2660 | } else | |
2661 | r = true; | |
2662 | } | |
5bdf2243 JJ |
2663 | |
2664 | return sd_bus_message_append(reply, "b", r > 0); | |
2665 | } | |
2666 | ||
2667 | static int method_set_reboot_to_firmware_setup( | |
5bdf2243 JJ |
2668 | sd_bus_message *message, |
2669 | void *userdata, | |
2670 | sd_bus_error *error) { | |
2671 | ||
99534007 | 2672 | Manager *m = ASSERT_PTR(userdata); |
e667266a LP |
2673 | bool use_efi; |
2674 | int b, r; | |
5bdf2243 | 2675 | |
5bdf2243 | 2676 | assert(message); |
5bdf2243 | 2677 | |
889f25b2 | 2678 | r = sd_bus_message_read(message, "b", &b); |
5bdf2243 JJ |
2679 | if (r < 0) |
2680 | return r; | |
2681 | ||
e667266a LP |
2682 | r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP"); |
2683 | if (r == -ENXIO) { | |
2684 | /* EFI case: let's see what the firmware supports */ | |
2685 | ||
2686 | r = efi_reboot_to_firmware_supported(); | |
2687 | if (r == -EOPNOTSUPP) | |
1b09b81c | 2688 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Firmware does not support boot into firmware."); |
e667266a LP |
2689 | if (r < 0) |
2690 | return r; | |
2691 | ||
2692 | use_efi = true; | |
2693 | ||
2694 | } else if (r <= 0) { | |
2695 | /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */ | |
2696 | ||
2697 | if (r < 0) | |
2698 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m"); | |
2699 | ||
1b09b81c | 2700 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Firmware does not support boot into firmware."); |
e667266a LP |
2701 | } else |
2702 | /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */ | |
2703 | use_efi = false; | |
2704 | ||
5bdf2243 JJ |
2705 | r = bus_verify_polkit_async(message, |
2706 | CAP_SYS_ADMIN, | |
2707 | "org.freedesktop.login1.set-reboot-to-firmware-setup", | |
403ed0e5 | 2708 | NULL, |
889f25b2 | 2709 | false, |
5bdf2243 JJ |
2710 | UID_INVALID, |
2711 | &m->polkit_registry, | |
2712 | error); | |
2713 | if (r < 0) | |
2714 | return r; | |
2715 | if (r == 0) | |
2716 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2717 | ||
e667266a LP |
2718 | if (use_efi) { |
2719 | r = efi_set_reboot_to_firmware(b); | |
2720 | if (r < 0) | |
2721 | return r; | |
2722 | } else { | |
2723 | if (b) { | |
2724 | r = touch("/run/systemd/reboot-to-firmware-setup"); | |
2725 | if (r < 0) | |
2726 | return r; | |
2727 | } else { | |
2728 | if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno != ENOENT) | |
2729 | return -errno; | |
2730 | } | |
2731 | } | |
5bdf2243 JJ |
2732 | |
2733 | return sd_bus_reply_method_return(message, NULL); | |
2734 | } | |
2735 | ||
2736 | static int method_can_reboot_to_firmware_setup( | |
5bdf2243 JJ |
2737 | sd_bus_message *message, |
2738 | void *userdata, | |
2739 | sd_bus_error *error) { | |
2740 | ||
99534007 | 2741 | _unused_ Manager *m = ASSERT_PTR(userdata); |
e667266a | 2742 | int r; |
5bdf2243 | 2743 | |
5bdf2243 | 2744 | assert(message); |
5bdf2243 | 2745 | |
e667266a LP |
2746 | r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP"); |
2747 | if (r == -ENXIO) { | |
2748 | /* EFI case: let's see what the firmware supports */ | |
2749 | ||
2750 | r = efi_reboot_to_firmware_supported(); | |
2751 | if (r < 0) { | |
2752 | if (r != -EOPNOTSUPP) | |
2753 | log_warning_errno(r, "Failed to determine whether reboot to firmware is supported: %m"); | |
2754 | ||
350f9518 | 2755 | return sd_bus_reply_method_return(message, "s", "na"); |
e667266a | 2756 | } |
6f302ce6 | 2757 | |
e667266a LP |
2758 | } else if (r <= 0) { |
2759 | /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */ | |
2760 | ||
2761 | if (r < 0) | |
2762 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m"); | |
2763 | ||
350f9518 | 2764 | return sd_bus_reply_method_return(message, "s", "na"); |
6f302ce6 | 2765 | } |
5bdf2243 | 2766 | |
350f9518 LP |
2767 | return return_test_polkit( |
2768 | message, | |
2769 | CAP_SYS_ADMIN, | |
2770 | "org.freedesktop.login1.set-reboot-to-firmware-setup", | |
2771 | NULL, | |
2772 | UID_INVALID, | |
2773 | error); | |
5bdf2243 JJ |
2774 | } |
2775 | ||
31b221cf LP |
2776 | static int property_get_reboot_to_boot_loader_menu( |
2777 | sd_bus *bus, | |
2778 | const char *path, | |
2779 | const char *interface, | |
2780 | const char *property, | |
2781 | sd_bus_message *reply, | |
2782 | void *userdata, | |
2783 | sd_bus_error *error) { | |
2784 | ||
2785 | uint64_t x = UINT64_MAX; | |
2786 | int r; | |
2787 | ||
2788 | assert(bus); | |
2789 | assert(reply); | |
2790 | assert(userdata); | |
2791 | ||
2792 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU"); | |
2793 | if (r == -ENXIO) { | |
513631a7 | 2794 | /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distinguished: |
31b221cf LP |
2795 | * |
2796 | * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user) | |
2797 | * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user) | |
2798 | * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds | |
2799 | */ | |
2800 | ||
e8df4eee | 2801 | r = efi_loader_get_config_timeout_one_shot(&x); |
31b221cf LP |
2802 | if (r < 0) { |
2803 | if (r != -ENOENT) | |
e8df4eee | 2804 | log_warning_errno(r, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m"); |
31b221cf LP |
2805 | } |
2806 | ||
2807 | } else if (r < 0) | |
2808 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m"); | |
2809 | else if (r > 0) { | |
2810 | _cleanup_free_ char *v = NULL; | |
2811 | ||
2812 | /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */ | |
2813 | ||
2814 | r = read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v); | |
2815 | if (r < 0) { | |
2816 | if (r != -ENOENT) | |
2817 | log_warning_errno(r, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m"); | |
2818 | } else { | |
2819 | r = safe_atou64(v, &x); | |
2820 | if (r < 0) | |
2821 | log_warning_errno(r, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m"); | |
2822 | } | |
2823 | } | |
2824 | ||
2825 | return sd_bus_message_append(reply, "t", x); | |
2826 | } | |
2827 | ||
2828 | static int method_set_reboot_to_boot_loader_menu( | |
2829 | sd_bus_message *message, | |
2830 | void *userdata, | |
2831 | sd_bus_error *error) { | |
2832 | ||
99534007 | 2833 | Manager *m = ASSERT_PTR(userdata); |
31b221cf LP |
2834 | bool use_efi; |
2835 | uint64_t x; | |
2836 | int r; | |
2837 | ||
2838 | assert(message); | |
31b221cf LP |
2839 | |
2840 | r = sd_bus_message_read(message, "t", &x); | |
2841 | if (r < 0) | |
2842 | return r; | |
2843 | ||
2844 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU"); | |
2845 | if (r == -ENXIO) { | |
2846 | uint64_t features; | |
2847 | ||
2848 | /* EFI case: let's see if booting into boot loader menu is supported. */ | |
2849 | ||
2850 | r = efi_loader_get_features(&features); | |
2851 | if (r < 0) | |
2852 | log_warning_errno(r, "Failed to determine whether reboot to boot loader menu is supported: %m"); | |
2853 | if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT)) | |
1b09b81c | 2854 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader does not support boot into boot loader menu."); |
31b221cf LP |
2855 | |
2856 | use_efi = true; | |
2857 | ||
2858 | } else if (r <= 0) { | |
2859 | /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */ | |
2860 | ||
2861 | if (r < 0) | |
2862 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m"); | |
2863 | ||
1b09b81c | 2864 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader does not support boot into boot loader menu."); |
31b221cf LP |
2865 | } else |
2866 | /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */ | |
2867 | use_efi = false; | |
2868 | ||
2869 | r = bus_verify_polkit_async(message, | |
2870 | CAP_SYS_ADMIN, | |
2871 | "org.freedesktop.login1.set-reboot-to-boot-loader-menu", | |
2872 | NULL, | |
2873 | false, | |
2874 | UID_INVALID, | |
2875 | &m->polkit_registry, | |
2876 | error); | |
2877 | if (r < 0) | |
2878 | return r; | |
2879 | if (r == 0) | |
2880 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2881 | ||
2882 | if (use_efi) { | |
2883 | if (x == UINT64_MAX) | |
e6f055cb | 2884 | r = efi_set_variable(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot), NULL, 0); |
31b221cf LP |
2885 | else { |
2886 | char buf[DECIMAL_STR_MAX(uint64_t) + 1]; | |
2887 | xsprintf(buf, "%" PRIu64, DIV_ROUND_UP(x, USEC_PER_SEC)); /* second granularity */ | |
2888 | ||
e6f055cb | 2889 | r = efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot), buf); |
31b221cf LP |
2890 | } |
2891 | if (r < 0) | |
2892 | return r; | |
2893 | } else { | |
2894 | if (x == UINT64_MAX) { | |
3a978b30 | 2895 | if (unlink("/run/systemd/reboot-to-boot-loader-menu") < 0 && errno != ENOENT) |
31b221cf LP |
2896 | return -errno; |
2897 | } else { | |
2898 | char buf[DECIMAL_STR_MAX(uint64_t) + 1]; | |
2899 | ||
e503019b | 2900 | xsprintf(buf, "%" PRIu64, x); /* μs granularity */ |
31b221cf | 2901 | |
3a978b30 | 2902 | r = write_string_file_atomic_label("/run/systemd/reboot-to-boot-loader-menu", buf); |
31b221cf LP |
2903 | if (r < 0) |
2904 | return r; | |
2905 | } | |
2906 | } | |
2907 | ||
2908 | return sd_bus_reply_method_return(message, NULL); | |
2909 | } | |
2910 | ||
2911 | static int method_can_reboot_to_boot_loader_menu( | |
2912 | sd_bus_message *message, | |
2913 | void *userdata, | |
2914 | sd_bus_error *error) { | |
2915 | ||
99534007 | 2916 | _unused_ Manager *m = ASSERT_PTR(userdata); |
31b221cf LP |
2917 | int r; |
2918 | ||
31b221cf | 2919 | assert(message); |
31b221cf LP |
2920 | |
2921 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU"); | |
2922 | if (r == -ENXIO) { | |
2923 | uint64_t features = 0; | |
2924 | ||
2925 | /* EFI case, let's see if booting into boot loader menu is supported. */ | |
2926 | ||
2927 | r = efi_loader_get_features(&features); | |
2928 | if (r < 0) | |
2929 | log_warning_errno(r, "Failed to determine whether reboot to boot loader menu is supported: %m"); | |
2930 | if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT)) | |
350f9518 | 2931 | return sd_bus_reply_method_return(message, "s", "na"); |
31b221cf LP |
2932 | |
2933 | } else if (r <= 0) { | |
2934 | /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */ | |
2935 | ||
2936 | if (r < 0) | |
2937 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m"); | |
2938 | ||
350f9518 | 2939 | return sd_bus_reply_method_return(message, "s", "na"); |
31b221cf LP |
2940 | } |
2941 | ||
350f9518 LP |
2942 | return return_test_polkit( |
2943 | message, | |
2944 | CAP_SYS_ADMIN, | |
2945 | "org.freedesktop.login1.set-reboot-to-boot-loader-menu", | |
2946 | NULL, | |
2947 | UID_INVALID, | |
2948 | error); | |
31b221cf LP |
2949 | } |
2950 | ||
2951 | static int property_get_reboot_to_boot_loader_entry( | |
2952 | sd_bus *bus, | |
2953 | const char *path, | |
2954 | const char *interface, | |
2955 | const char *property, | |
2956 | sd_bus_message *reply, | |
2957 | void *userdata, | |
2958 | sd_bus_error *error) { | |
2959 | ||
2960 | _cleanup_free_ char *v = NULL; | |
99534007 | 2961 | Manager *m = ASSERT_PTR(userdata); |
af2697e8 | 2962 | const char *x = NULL; |
31b221cf LP |
2963 | int r; |
2964 | ||
2965 | assert(bus); | |
2966 | assert(reply); | |
31b221cf LP |
2967 | |
2968 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY"); | |
2969 | if (r == -ENXIO) { | |
2970 | /* EFI case: let's read the LoaderEntryOneShot variable */ | |
2971 | ||
af2697e8 | 2972 | r = efi_loader_update_entry_one_shot_cache(&m->efi_loader_entry_one_shot, &m->efi_loader_entry_one_shot_stat); |
31b221cf LP |
2973 | if (r < 0) { |
2974 | if (r != -ENOENT) | |
af2697e8 LP |
2975 | log_warning_errno(r, "Failed to read LoaderEntryOneShot variable, ignoring: %m"); |
2976 | } else | |
2977 | x = m->efi_loader_entry_one_shot; | |
2978 | ||
31b221cf LP |
2979 | } else if (r < 0) |
2980 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m"); | |
2981 | else if (r > 0) { | |
2982 | ||
2983 | /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */ | |
2984 | ||
2985 | r = read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v); | |
2986 | if (r < 0) { | |
2987 | if (r != -ENOENT) | |
af2697e8 LP |
2988 | log_warning_errno(r, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m"); |
2989 | } else if (!efi_loader_entry_name_valid(v)) | |
31b221cf | 2990 | log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring."); |
af2697e8 LP |
2991 | else |
2992 | x = v; | |
31b221cf LP |
2993 | } |
2994 | ||
af2697e8 | 2995 | return sd_bus_message_append(reply, "s", x); |
31b221cf LP |
2996 | } |
2997 | ||
d4bd786d | 2998 | static int boot_loader_entry_exists(Manager *m, const char *id) { |
f7a7a5e2 | 2999 | _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL; |
31b221cf LP |
3000 | int r; |
3001 | ||
d4bd786d | 3002 | assert(m); |
31b221cf LP |
3003 | assert(id); |
3004 | ||
af9ae750 | 3005 | r = boot_config_load_auto(&config, NULL, NULL); |
cc5957dc | 3006 | if (r < 0 && r != -ENOKEY) /* don't complain if no GPT is found, hence skip ENOKEY */ |
31b221cf LP |
3007 | return r; |
3008 | ||
d4bd786d LP |
3009 | r = manager_read_efi_boot_loader_entries(m); |
3010 | if (r >= 0) | |
af9ae750 | 3011 | (void) boot_config_augment_from_loader(&config, m->efi_boot_loader_entries, /* auto_only= */ true); |
31b221cf | 3012 | |
bb682057 | 3013 | return !!boot_config_find_entry(&config, id); |
31b221cf LP |
3014 | } |
3015 | ||
3016 | static int method_set_reboot_to_boot_loader_entry( | |
3017 | sd_bus_message *message, | |
3018 | void *userdata, | |
3019 | sd_bus_error *error) { | |
3020 | ||
99534007 | 3021 | Manager *m = ASSERT_PTR(userdata); |
31b221cf LP |
3022 | bool use_efi; |
3023 | const char *v; | |
3024 | int r; | |
3025 | ||
3026 | assert(message); | |
31b221cf LP |
3027 | |
3028 | r = sd_bus_message_read(message, "s", &v); | |
3029 | if (r < 0) | |
3030 | return r; | |
3031 | ||
3032 | if (isempty(v)) | |
3033 | v = NULL; | |
3034 | else if (efi_loader_entry_name_valid(v)) { | |
d4bd786d | 3035 | r = boot_loader_entry_exists(m, v); |
31b221cf LP |
3036 | if (r < 0) |
3037 | return r; | |
3038 | if (r == 0) | |
3039 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader entry '%s' is not known.", v); | |
3040 | } else | |
3041 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Boot loader entry name '%s' is not valid, refusing.", v); | |
3042 | ||
3043 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY"); | |
3044 | if (r == -ENXIO) { | |
3045 | uint64_t features; | |
3046 | ||
3047 | /* EFI case: let's see if booting into boot loader entry is supported. */ | |
3048 | ||
3049 | r = efi_loader_get_features(&features); | |
3050 | if (r < 0) | |
3051 | log_warning_errno(r, "Failed to determine whether reboot into boot loader entry is supported: %m"); | |
3052 | if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_ENTRY_ONESHOT)) | |
1b09b81c | 3053 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Loader does not support boot into boot loader entry."); |
31b221cf LP |
3054 | |
3055 | use_efi = true; | |
3056 | ||
3057 | } else if (r <= 0) { | |
3058 | /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */ | |
3059 | ||
3060 | if (r < 0) | |
3061 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m"); | |
3062 | ||
1b09b81c | 3063 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Loader does not support boot into boot loader entry."); |
31b221cf LP |
3064 | } else |
3065 | /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */ | |
3066 | use_efi = false; | |
3067 | ||
3068 | r = bus_verify_polkit_async(message, | |
3069 | CAP_SYS_ADMIN, | |
3070 | "org.freedesktop.login1.set-reboot-to-boot-loader-entry", | |
3071 | NULL, | |
3072 | false, | |
3073 | UID_INVALID, | |
3074 | &m->polkit_registry, | |
3075 | error); | |
3076 | if (r < 0) | |
3077 | return r; | |
3078 | if (r == 0) | |
3079 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
3080 | ||
3081 | if (use_efi) { | |
3082 | if (isempty(v)) | |
3083 | /* Delete item */ | |
e6f055cb | 3084 | r = efi_set_variable(EFI_LOADER_VARIABLE(LoaderEntryOneShot), NULL, 0); |
31b221cf | 3085 | else |
e6f055cb | 3086 | r = efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot), v); |
31b221cf LP |
3087 | if (r < 0) |
3088 | return r; | |
3089 | } else { | |
3090 | if (isempty(v)) { | |
3091 | if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno != ENOENT) | |
3092 | return -errno; | |
3093 | } else { | |
3094 | r = write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v); | |
3095 | if (r < 0) | |
3096 | return r; | |
3097 | } | |
3098 | } | |
3099 | ||
3100 | return sd_bus_reply_method_return(message, NULL); | |
3101 | } | |
3102 | ||
3103 | static int method_can_reboot_to_boot_loader_entry( | |
3104 | sd_bus_message *message, | |
3105 | void *userdata, | |
3106 | sd_bus_error *error) { | |
3107 | ||
99534007 | 3108 | _unused_ Manager *m = ASSERT_PTR(userdata); |
31b221cf LP |
3109 | int r; |
3110 | ||
3111 | assert(message); | |
31b221cf LP |
3112 | |
3113 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY"); | |
3114 | if (r == -ENXIO) { | |
3115 | uint64_t features = 0; | |
3116 | ||
3117 | /* EFI case, let's see if booting into boot loader entry is supported. */ | |
3118 | ||
3119 | r = efi_loader_get_features(&features); | |
3120 | if (r < 0) | |
3121 | log_warning_errno(r, "Failed to determine whether reboot to boot loader entry is supported: %m"); | |
3122 | if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_ENTRY_ONESHOT)) | |
350f9518 | 3123 | return sd_bus_reply_method_return(message, "s", "na"); |
31b221cf LP |
3124 | |
3125 | } else if (r <= 0) { | |
3126 | /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */ | |
3127 | ||
3128 | if (r < 0) | |
3129 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m"); | |
3130 | ||
350f9518 | 3131 | return sd_bus_reply_method_return(message, "s", "na"); |
31b221cf LP |
3132 | } |
3133 | ||
350f9518 LP |
3134 | return return_test_polkit( |
3135 | message, | |
3136 | CAP_SYS_ADMIN, | |
3137 | "org.freedesktop.login1.set-reboot-to-boot-loader-entry", | |
3138 | NULL, | |
3139 | UID_INVALID, | |
3140 | error); | |
31b221cf LP |
3141 | } |
3142 | ||
3143 | static int property_get_boot_loader_entries( | |
3144 | sd_bus *bus, | |
3145 | const char *path, | |
3146 | const char *interface, | |
3147 | const char *property, | |
3148 | sd_bus_message *reply, | |
3149 | void *userdata, | |
3150 | sd_bus_error *error) { | |
3151 | ||
f7a7a5e2 | 3152 | _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL; |
99534007 | 3153 | Manager *m = ASSERT_PTR(userdata); |
31b221cf LP |
3154 | size_t i; |
3155 | int r; | |
3156 | ||
3157 | assert(bus); | |
3158 | assert(reply); | |
31b221cf | 3159 | |
af9ae750 | 3160 | r = boot_config_load_auto(&config, NULL, NULL); |
cc5957dc | 3161 | if (r < 0 && r != -ENOKEY) /* don't complain if there's no GPT found */ |
31b221cf LP |
3162 | return r; |
3163 | ||
d4bd786d LP |
3164 | r = manager_read_efi_boot_loader_entries(m); |
3165 | if (r >= 0) | |
af9ae750 | 3166 | (void) boot_config_augment_from_loader(&config, m->efi_boot_loader_entries, /* auto_only= */ true); |
31b221cf LP |
3167 | |
3168 | r = sd_bus_message_open_container(reply, 'a', "s"); | |
3169 | if (r < 0) | |
3170 | return r; | |
3171 | ||
3172 | for (i = 0; i < config.n_entries; i++) { | |
3173 | BootEntry *e = config.entries + i; | |
3174 | ||
3175 | r = sd_bus_message_append(reply, "s", e->id); | |
3176 | if (r < 0) | |
3177 | return r; | |
3178 | } | |
3179 | ||
3180 | return sd_bus_message_close_container(reply); | |
3181 | } | |
3182 | ||
9ef15026 JS |
3183 | static int method_set_wall_message( |
3184 | sd_bus_message *message, | |
3185 | void *userdata, | |
3186 | sd_bus_error *error) { | |
3187 | ||
3188 | int r; | |
99534007 | 3189 | Manager *m = ASSERT_PTR(userdata); |
9ef15026 | 3190 | char *wall_message; |
81bb3ca1 | 3191 | int enable_wall_messages; |
9ef15026 JS |
3192 | |
3193 | assert(message); | |
9ef15026 JS |
3194 | |
3195 | r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages); | |
3196 | if (r < 0) | |
3197 | return r; | |
3198 | ||
1831759a LN |
3199 | if (strlen(wall_message) > WALL_MESSAGE_MAX) |
3200 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
3201 | "Wall message too long, maximum permitted length is %u characters.", | |
3202 | WALL_MESSAGE_MAX); | |
5ed73478 | 3203 | |
5644d47b ZJS |
3204 | /* Short-circuit the operation if the desired state is already in place, to |
3205 | * avoid an unnecessary polkit permission check. */ | |
3206 | if (streq_ptr(m->wall_message, empty_to_null(wall_message)) && | |
3207 | m->enable_wall_messages == enable_wall_messages) | |
3208 | goto done; | |
3209 | ||
9ef15026 JS |
3210 | r = bus_verify_polkit_async(message, |
3211 | CAP_SYS_ADMIN, | |
3212 | "org.freedesktop.login1.set-wall-message", | |
403ed0e5 | 3213 | NULL, |
9ef15026 JS |
3214 | false, |
3215 | UID_INVALID, | |
3216 | &m->polkit_registry, | |
3217 | error); | |
9ef15026 JS |
3218 | if (r < 0) |
3219 | return r; | |
3220 | if (r == 0) | |
3221 | return 1; /* Will call us back */ | |
3222 | ||
28a9ec44 ZJS |
3223 | r = free_and_strdup(&m->wall_message, empty_to_null(wall_message)); |
3224 | if (r < 0) | |
3225 | return log_oom(); | |
5744f59a | 3226 | |
9ef15026 JS |
3227 | m->enable_wall_messages = enable_wall_messages; |
3228 | ||
5644d47b | 3229 | done: |
9ef15026 JS |
3230 | return sd_bus_reply_method_return(message, NULL); |
3231 | } | |
3232 | ||
19070062 | 3233 | static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 3234 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
89bad70f | 3235 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
cc377381 LP |
3236 | const char *who, *why, *what, *mode; |
3237 | _cleanup_free_ char *id = NULL; | |
254d1313 | 3238 | _cleanup_close_ int fifo_fd = -EBADF; |
99534007 | 3239 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
3240 | InhibitMode mm; |
3241 | InhibitWhat w; | |
3242 | pid_t pid; | |
3243 | uid_t uid; | |
3244 | int r; | |
7f7bb946 | 3245 | |
cc377381 | 3246 | assert(message); |
38f3fc7d | 3247 | |
cc377381 LP |
3248 | r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode); |
3249 | if (r < 0) | |
ebcf1f97 | 3250 | return r; |
38f3fc7d | 3251 | |
cc377381 LP |
3252 | w = inhibit_what_from_string(what); |
3253 | if (w <= 0) | |
124d7cb2 ZJS |
3254 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
3255 | "Invalid what specification %s", what); | |
38f3fc7d | 3256 | |
cc377381 LP |
3257 | mm = inhibit_mode_from_string(mode); |
3258 | if (mm < 0) | |
124d7cb2 ZJS |
3259 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
3260 | "Invalid mode specification %s", mode); | |
7f7bb946 | 3261 | |
cc377381 LP |
3262 | /* Delay is only supported for shutdown/sleep */ |
3263 | if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP))) | |
124d7cb2 ZJS |
3264 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
3265 | "Delay inhibitors only supported for shutdown and sleep"); | |
38f3fc7d | 3266 | |
cc377381 LP |
3267 | /* Don't allow taking delay locks while we are already |
3268 | * executing the operation. We shouldn't create the impression | |
3269 | * that the lock was successful if the machine is about to go | |
3270 | * down/suspend any moment. */ | |
5ed73478 | 3271 | if (m->delayed_action && m->delayed_action->inhibit_what & w) |
124d7cb2 ZJS |
3272 | return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, |
3273 | "The operation inhibition has been requested for is already running"); | |
cc377381 | 3274 | |
c529695e LP |
3275 | r = bus_verify_polkit_async( |
3276 | message, | |
3277 | CAP_SYS_BOOT, | |
3278 | w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") : | |
3279 | w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") : | |
3280 | w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" : | |
3281 | w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" : | |
3282 | w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" : | |
adbb2b6a | 3283 | w == INHIBIT_HANDLE_REBOOT_KEY ? "org.freedesktop.login1.inhibit-handle-reboot-key" : |
c529695e LP |
3284 | w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" : |
3285 | "org.freedesktop.login1.inhibit-handle-lid-switch", | |
403ed0e5 | 3286 | NULL, |
c529695e LP |
3287 | false, |
3288 | UID_INVALID, | |
3289 | &m->polkit_registry, | |
3290 | error); | |
cc377381 | 3291 | if (r < 0) |
ebcf1f97 | 3292 | return r; |
cc377381 LP |
3293 | if (r == 0) |
3294 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
7f7bb946 | 3295 | |
05bae4a6 | 3296 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds); |
5b12334d LP |
3297 | if (r < 0) |
3298 | return r; | |
3299 | ||
05bae4a6 | 3300 | r = sd_bus_creds_get_euid(creds, &uid); |
cc377381 | 3301 | if (r < 0) |
ebcf1f97 | 3302 | return r; |
7f7bb946 | 3303 | |
5b12334d | 3304 | r = sd_bus_creds_get_pid(creds, &pid); |
cc377381 | 3305 | if (r < 0) |
ebcf1f97 | 3306 | return r; |
47a26690 | 3307 | |
89bad70f LP |
3308 | r = pidref_set_pid(&pidref, pid); |
3309 | if (r < 0) | |
3310 | return sd_bus_error_set_errnof(error, r, "Failed pin source process "PID_FMT": %m", pid); | |
3311 | ||
c5a11ae2 | 3312 | if (hashmap_size(m->inhibitors) >= m->inhibitors_max) |
124d7cb2 ZJS |
3313 | return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, |
3314 | "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", | |
3315 | m->inhibitors_max); | |
c5a11ae2 | 3316 | |
cc377381 | 3317 | do { |
97b11eed | 3318 | id = mfree(id); |
47a26690 | 3319 | |
97352764 | 3320 | if (asprintf(&id, "%" PRIu64, ++m->inhibit_counter) < 0) |
ebcf1f97 | 3321 | return -ENOMEM; |
47a26690 | 3322 | |
cc377381 | 3323 | } while (hashmap_get(m->inhibitors, id)); |
47a26690 | 3324 | |
0a9bf7fa | 3325 | _cleanup_(inhibitor_freep) Inhibitor *i = NULL; |
cc377381 LP |
3326 | r = manager_add_inhibitor(m, id, &i); |
3327 | if (r < 0) | |
ebcf1f97 | 3328 | return r; |
47a26690 | 3329 | |
cc377381 LP |
3330 | i->what = w; |
3331 | i->mode = mm; | |
89bad70f | 3332 | i->pid = TAKE_PIDREF(pidref); |
cc377381 LP |
3333 | i->uid = uid; |
3334 | i->why = strdup(why); | |
3335 | i->who = strdup(who); | |
7f7bb946 | 3336 | |
0a9bf7fa ZJS |
3337 | if (!i->why || !i->who) |
3338 | return -ENOMEM; | |
b668e064 | 3339 | |
cc377381 | 3340 | fifo_fd = inhibitor_create_fifo(i); |
0a9bf7fa ZJS |
3341 | if (fifo_fd < 0) |
3342 | return fifo_fd; | |
b668e064 | 3343 | |
2b695039 LP |
3344 | r = inhibitor_start(i); |
3345 | if (r < 0) | |
0a9bf7fa | 3346 | return r; |
43e94f86 | 3347 | TAKE_PTR(i); |
b668e064 | 3348 | |
df2d202e | 3349 | return sd_bus_reply_method_return(message, "h", fifo_fd); |
cc377381 | 3350 | } |
3f49d45a | 3351 | |
c2b178d3 | 3352 | static const sd_bus_vtable manager_vtable[] = { |
cc377381 LP |
3353 | SD_BUS_VTABLE_START(0), |
3354 | ||
81bb3ca1 | 3355 | SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", bus_property_get_bool, bus_property_set_bool, offsetof(Manager, enable_wall_messages), 0), |
e2fa5721 DM |
3356 | SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0), |
3357 | ||
556089dc LP |
3358 | SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST), |
3359 | SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST), | |
3360 | SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST), | |
0db7e17c | 3361 | SD_BUS_PROPERTY("KillUserProcesses", "b", bus_property_get_bool, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST), |
428b296a | 3362 | SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter, 0, 0), |
c30e0d7b | 3363 | SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, 0), |
31b221cf LP |
3364 | SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu, 0, 0), |
3365 | SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry, 0, 0), | |
3366 | SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
cc377381 LP |
3367 | SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
3368 | SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
3369 | SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
3370 | SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
3371 | SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
556089dc | 3372 | SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST), |
9afe9efb | 3373 | SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL, offsetof(Manager, user_stop_delay), SD_BUS_VTABLE_PROPERTY_CONST), |
556089dc | 3374 | SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST), |
c4db78fc LP |
3375 | SD_BUS_PROPERTY("HandlePowerKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_power_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST), |
3376 | SD_BUS_PROPERTY("HandleRebootKey", "s", property_get_handle_action, offsetof(Manager, handle_reboot_key), SD_BUS_VTABLE_PROPERTY_CONST), | |
3377 | SD_BUS_PROPERTY("HandleRebootKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_reboot_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST), | |
556089dc | 3378 | SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST), |
c4db78fc | 3379 | SD_BUS_PROPERTY("HandleSuspendKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST), |
556089dc | 3380 | SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST), |
c4db78fc | 3381 | SD_BUS_PROPERTY("HandleHibernateKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST), |
556089dc | 3382 | SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST), |
e25937a3 | 3383 | SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST), |
3c56cab4 | 3384 | SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST), |
9d10cbee | 3385 | SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
556089dc LP |
3386 | SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST), |
3387 | SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST), | |
cc377381 LP |
3388 | SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0), |
3389 | SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0), | |
8aaa023a | 3390 | SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0), |
14856079 | 3391 | SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0), |
9b9c23da | 3392 | SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed, 0, 0), |
4e96eb68 | 3393 | SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power, 0, 0), |
6d97d3c6 | 3394 | SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST), |
a7b46b7d | 3395 | SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST), |
cc1c85fb | 3396 | SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL, offsetof(Manager, runtime_dir_inodes), SD_BUS_VTABLE_PROPERTY_CONST), |
c5a11ae2 | 3397 | SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST), |
01adcd69 | 3398 | SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size, offsetof(Manager, inhibitors), 0), |
183e0738 | 3399 | SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST), |
01adcd69 | 3400 | SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size, offsetof(Manager, sessions), 0), |
28414939 | 3401 | SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), |
82325af3 | 3402 | SD_BUS_PROPERTY("StopIdleSessionUSec", "t", NULL, offsetof(Manager, stop_idle_session_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
cc377381 | 3403 | |
a6293b05 NK |
3404 | SD_BUS_METHOD_WITH_ARGS("GetSession", |
3405 | SD_BUS_ARGS("s", session_id), | |
3406 | SD_BUS_RESULT("o", object_path), | |
3407 | method_get_session, | |
3408 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3409 | SD_BUS_METHOD_WITH_ARGS("GetSessionByPID", | |
3410 | SD_BUS_ARGS("u", pid), | |
3411 | SD_BUS_RESULT("o", object_path), | |
3412 | method_get_session_by_pid, | |
3413 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3414 | SD_BUS_METHOD_WITH_ARGS("GetUser", | |
3415 | SD_BUS_ARGS("u", uid), | |
3416 | SD_BUS_RESULT("o", object_path), | |
3417 | method_get_user, | |
3418 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3419 | SD_BUS_METHOD_WITH_ARGS("GetUserByPID", | |
3420 | SD_BUS_ARGS("u", pid), | |
3421 | SD_BUS_RESULT("o", object_path), | |
3422 | method_get_user_by_pid, | |
3423 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3424 | SD_BUS_METHOD_WITH_ARGS("GetSeat", | |
3425 | SD_BUS_ARGS("s", seat_id), | |
3426 | SD_BUS_RESULT("o", object_path), | |
3427 | method_get_seat, | |
3428 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3429 | SD_BUS_METHOD_WITH_ARGS("ListSessions", | |
3430 | SD_BUS_NO_ARGS, | |
3431 | SD_BUS_RESULT("a(susso)", sessions), | |
3432 | method_list_sessions, | |
3433 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3434 | SD_BUS_METHOD_WITH_ARGS("ListUsers", | |
3435 | SD_BUS_NO_ARGS, | |
3436 | SD_BUS_RESULT("a(uso)", users), | |
3437 | method_list_users, | |
3438 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3439 | SD_BUS_METHOD_WITH_ARGS("ListSeats", | |
3440 | SD_BUS_NO_ARGS, | |
3441 | SD_BUS_RESULT("a(so)", seats), | |
3442 | method_list_seats, | |
3443 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3444 | SD_BUS_METHOD_WITH_ARGS("ListInhibitors", | |
3445 | SD_BUS_NO_ARGS, | |
3446 | SD_BUS_RESULT("a(ssssuu)", inhibitors), | |
3447 | method_list_inhibitors, | |
3448 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3449 | SD_BUS_METHOD_WITH_ARGS("CreateSession", | |
3450 | SD_BUS_ARGS("u", uid, | |
3451 | "u", pid, | |
3452 | "s", service, | |
3453 | "s", type, | |
3454 | "s", class, | |
3455 | "s", desktop, | |
3456 | "s", seat_id, | |
3457 | "u", vtnr, | |
3458 | "s", tty, | |
3459 | "s", display, | |
3460 | "b", remote, | |
3461 | "s", remote_user, | |
3462 | "s", remote_host, | |
3463 | "a(sv)", properties), | |
3464 | SD_BUS_RESULT("s", session_id, | |
3465 | "o", object_path, | |
3466 | "s", runtime_path, | |
3467 | "h", fifo_fd, | |
3468 | "u", uid, | |
3469 | "s", seat_id, | |
3470 | "u", vtnr, | |
3471 | "b", existing), | |
3472 | method_create_session, | |
3473 | 0), | |
3474 | SD_BUS_METHOD_WITH_ARGS("ReleaseSession", | |
3475 | SD_BUS_ARGS("s", session_id), | |
3476 | SD_BUS_NO_RESULT, | |
3477 | method_release_session, | |
3478 | 0), | |
3479 | SD_BUS_METHOD_WITH_ARGS("ActivateSession", | |
3480 | SD_BUS_ARGS("s", session_id), | |
3481 | SD_BUS_NO_RESULT, | |
3482 | method_activate_session, | |
3483 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3484 | SD_BUS_METHOD_WITH_ARGS("ActivateSessionOnSeat", | |
3485 | SD_BUS_ARGS("s", session_id, "s", seat_id), | |
3486 | SD_BUS_NO_RESULT, | |
3487 | method_activate_session_on_seat, | |
3488 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3489 | SD_BUS_METHOD_WITH_ARGS("LockSession", | |
3490 | SD_BUS_ARGS("s", session_id), | |
3491 | SD_BUS_NO_RESULT, | |
3492 | method_lock_session, | |
3493 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3494 | SD_BUS_METHOD_WITH_ARGS("UnlockSession", | |
3495 | SD_BUS_ARGS("s", session_id), | |
3496 | SD_BUS_NO_RESULT, | |
3497 | method_lock_session, | |
3498 | SD_BUS_VTABLE_UNPRIVILEGED), | |
4cb5fd0d ZJS |
3499 | SD_BUS_METHOD("LockSessions", |
3500 | NULL, | |
3501 | NULL, | |
3502 | method_lock_sessions, | |
3503 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3504 | SD_BUS_METHOD("UnlockSessions", | |
3505 | NULL, | |
3506 | NULL, | |
3507 | method_lock_sessions, | |
3508 | SD_BUS_VTABLE_UNPRIVILEGED), | |
a6293b05 NK |
3509 | SD_BUS_METHOD_WITH_ARGS("KillSession", |
3510 | SD_BUS_ARGS("s", session_id, "s", who, "i", signal_number), | |
3511 | SD_BUS_NO_RESULT, | |
3512 | method_kill_session, | |
3513 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3514 | SD_BUS_METHOD_WITH_ARGS("KillUser", | |
3515 | SD_BUS_ARGS("u", uid, "i", signal_number), | |
3516 | SD_BUS_NO_RESULT, | |
3517 | method_kill_user, | |
3518 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3519 | SD_BUS_METHOD_WITH_ARGS("TerminateSession", | |
3520 | SD_BUS_ARGS("s", session_id), | |
3521 | SD_BUS_NO_RESULT, | |
3522 | method_terminate_session, | |
3523 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3524 | SD_BUS_METHOD_WITH_ARGS("TerminateUser", | |
3525 | SD_BUS_ARGS("u", uid), | |
3526 | SD_BUS_NO_RESULT, | |
3527 | method_terminate_user, | |
3528 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3529 | SD_BUS_METHOD_WITH_ARGS("TerminateSeat", | |
3530 | SD_BUS_ARGS("s", seat_id), | |
3531 | SD_BUS_NO_RESULT, | |
3532 | method_terminate_seat, | |
3533 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3534 | SD_BUS_METHOD_WITH_ARGS("SetUserLinger", | |
3535 | SD_BUS_ARGS("u", uid, "b", enable, "b", interactive), | |
3536 | SD_BUS_NO_RESULT, | |
3537 | method_set_user_linger, | |
3538 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3539 | SD_BUS_METHOD_WITH_ARGS("AttachDevice", | |
3540 | SD_BUS_ARGS("s", seat_id, "s", sysfs_path, "b", interactive), | |
3541 | SD_BUS_NO_RESULT, | |
3542 | method_attach_device, | |
3543 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3544 | SD_BUS_METHOD_WITH_ARGS("FlushDevices", | |
3545 | SD_BUS_ARGS("b", interactive), | |
3546 | SD_BUS_NO_RESULT, | |
3547 | method_flush_devices, | |
3548 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3549 | SD_BUS_METHOD_WITH_ARGS("PowerOff", | |
3550 | SD_BUS_ARGS("b", interactive), | |
3551 | SD_BUS_NO_RESULT, | |
3552 | method_poweroff, | |
3553 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3554 | SD_BUS_METHOD_WITH_ARGS("PowerOffWithFlags", | |
3555 | SD_BUS_ARGS("t", flags), | |
3556 | SD_BUS_NO_RESULT, | |
3557 | method_poweroff, | |
3558 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3559 | SD_BUS_METHOD_WITH_ARGS("Reboot", | |
3560 | SD_BUS_ARGS("b", interactive), | |
3561 | SD_BUS_NO_RESULT, | |
3562 | method_reboot, | |
3563 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3564 | SD_BUS_METHOD_WITH_ARGS("RebootWithFlags", | |
3565 | SD_BUS_ARGS("t", flags), | |
3566 | SD_BUS_NO_RESULT, | |
3567 | method_reboot, | |
3568 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3569 | SD_BUS_METHOD_WITH_ARGS("Halt", | |
3570 | SD_BUS_ARGS("b", interactive), | |
3571 | SD_BUS_NO_RESULT, | |
3572 | method_halt, | |
3573 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3574 | SD_BUS_METHOD_WITH_ARGS("HaltWithFlags", | |
3575 | SD_BUS_ARGS("t", flags), | |
3576 | SD_BUS_NO_RESULT, | |
3577 | method_halt, | |
3578 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3579 | SD_BUS_METHOD_WITH_ARGS("Suspend", | |
3580 | SD_BUS_ARGS("b", interactive), | |
3581 | SD_BUS_NO_RESULT, | |
3582 | method_suspend, | |
3583 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3584 | SD_BUS_METHOD_WITH_ARGS("SuspendWithFlags", | |
3585 | SD_BUS_ARGS("t", flags), | |
3586 | SD_BUS_NO_RESULT, | |
3587 | method_suspend, | |
3588 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3589 | SD_BUS_METHOD_WITH_ARGS("Hibernate", | |
3590 | SD_BUS_ARGS("b", interactive), | |
3591 | SD_BUS_NO_RESULT, | |
3592 | method_hibernate, | |
3593 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3594 | SD_BUS_METHOD_WITH_ARGS("HibernateWithFlags", | |
3595 | SD_BUS_ARGS("t", flags), | |
3596 | SD_BUS_NO_RESULT, | |
3597 | method_hibernate, | |
3598 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3599 | SD_BUS_METHOD_WITH_ARGS("HybridSleep", | |
3600 | SD_BUS_ARGS("b", interactive), | |
3601 | SD_BUS_NO_RESULT, | |
3602 | method_hybrid_sleep, | |
3603 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3604 | SD_BUS_METHOD_WITH_ARGS("HybridSleepWithFlags", | |
3605 | SD_BUS_ARGS("t", flags), | |
3606 | SD_BUS_NO_RESULT, | |
3607 | method_hybrid_sleep, | |
3608 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3609 | SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernate", | |
3610 | SD_BUS_ARGS("b", interactive), | |
3611 | SD_BUS_NO_RESULT, | |
3612 | method_suspend_then_hibernate, | |
3613 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3614 | SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernateWithFlags", | |
3615 | SD_BUS_ARGS("t", flags), | |
3616 | SD_BUS_NO_RESULT, | |
3617 | method_suspend_then_hibernate, | |
3618 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3619 | SD_BUS_METHOD_WITH_ARGS("CanPowerOff", | |
3620 | SD_BUS_NO_ARGS, | |
3621 | SD_BUS_RESULT("s", result), | |
3622 | method_can_poweroff, | |
3623 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3624 | SD_BUS_METHOD_WITH_ARGS("CanReboot", | |
3625 | SD_BUS_NO_ARGS, | |
3626 | SD_BUS_RESULT("s", result), | |
3627 | method_can_reboot, | |
3628 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3629 | SD_BUS_METHOD_WITH_ARGS("CanHalt", | |
3630 | SD_BUS_NO_ARGS, | |
3631 | SD_BUS_RESULT("s", result), | |
3632 | method_can_halt, | |
3633 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3634 | SD_BUS_METHOD_WITH_ARGS("CanSuspend", | |
3635 | SD_BUS_NO_ARGS, | |
3636 | SD_BUS_RESULT("s", result), | |
3637 | method_can_suspend, | |
3638 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3639 | SD_BUS_METHOD_WITH_ARGS("CanHibernate", | |
3640 | SD_BUS_NO_ARGS, | |
3641 | SD_BUS_RESULT("s", result), | |
3642 | method_can_hibernate, | |
3643 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3644 | SD_BUS_METHOD_WITH_ARGS("CanHybridSleep", | |
3645 | SD_BUS_NO_ARGS, | |
3646 | SD_BUS_RESULT("s", result), | |
3647 | method_can_hybrid_sleep, | |
3648 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3649 | SD_BUS_METHOD_WITH_ARGS("CanSuspendThenHibernate", | |
3650 | SD_BUS_NO_ARGS, | |
3651 | SD_BUS_RESULT("s", result), | |
3652 | method_can_suspend_then_hibernate, | |
3653 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3654 | SD_BUS_METHOD_WITH_ARGS("ScheduleShutdown", | |
3655 | SD_BUS_ARGS("s", type, "t", usec), | |
3656 | SD_BUS_NO_RESULT, | |
3657 | method_schedule_shutdown, | |
3658 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3659 | SD_BUS_METHOD_WITH_ARGS("CancelScheduledShutdown", | |
3660 | SD_BUS_NO_ARGS, | |
3661 | SD_BUS_RESULT("b", cancelled), | |
3662 | method_cancel_scheduled_shutdown, | |
3663 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3664 | SD_BUS_METHOD_WITH_ARGS("Inhibit", | |
3665 | SD_BUS_ARGS("s", what, "s", who, "s", why, "s", mode), | |
3666 | SD_BUS_RESULT("h", pipe_fd), | |
3667 | method_inhibit, | |
3668 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3669 | SD_BUS_METHOD_WITH_ARGS("CanRebootParameter", | |
3670 | SD_BUS_NO_ARGS, | |
3671 | SD_BUS_RESULT("s", result), | |
3672 | method_can_reboot_parameter, | |
3673 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3674 | SD_BUS_METHOD_WITH_ARGS("SetRebootParameter", | |
3675 | SD_BUS_ARGS("s", parameter), | |
3676 | SD_BUS_NO_RESULT, | |
3677 | method_set_reboot_parameter, | |
3678 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3679 | SD_BUS_METHOD_WITH_ARGS("CanRebootToFirmwareSetup", | |
3680 | SD_BUS_NO_ARGS, | |
3681 | SD_BUS_RESULT("s", result), | |
3682 | method_can_reboot_to_firmware_setup, | |
3683 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3684 | SD_BUS_METHOD_WITH_ARGS("SetRebootToFirmwareSetup", | |
3685 | SD_BUS_ARGS("b", enable), | |
3686 | SD_BUS_NO_RESULT, | |
3687 | method_set_reboot_to_firmware_setup, | |
3688 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3689 | SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderMenu", | |
3690 | SD_BUS_NO_ARGS, | |
3691 | SD_BUS_RESULT("s", result), | |
3692 | method_can_reboot_to_boot_loader_menu, | |
3693 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3694 | SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderMenu", | |
3695 | SD_BUS_ARGS("t", timeout), | |
3696 | SD_BUS_NO_RESULT, | |
3697 | method_set_reboot_to_boot_loader_menu, | |
3698 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3699 | SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderEntry", | |
3700 | SD_BUS_NO_ARGS, | |
3701 | SD_BUS_RESULT("s", result), | |
3702 | method_can_reboot_to_boot_loader_entry, | |
3703 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3704 | SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderEntry", | |
3705 | SD_BUS_ARGS("s", boot_loader_entry), | |
3706 | SD_BUS_NO_RESULT, | |
3707 | method_set_reboot_to_boot_loader_entry, | |
3708 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3709 | SD_BUS_METHOD_WITH_ARGS("SetWallMessage", | |
3710 | SD_BUS_ARGS("s", wall_message, "b", enable), | |
3711 | SD_BUS_NO_RESULT, | |
3712 | method_set_wall_message, | |
3713 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3714 | ||
3715 | SD_BUS_SIGNAL_WITH_ARGS("SessionNew", | |
3716 | SD_BUS_ARGS("s", session_id, "o", object_path), | |
3717 | 0), | |
3718 | SD_BUS_SIGNAL_WITH_ARGS("SessionRemoved", | |
3719 | SD_BUS_ARGS("s", session_id, "o", object_path), | |
3720 | 0), | |
3721 | SD_BUS_SIGNAL_WITH_ARGS("UserNew", | |
3722 | SD_BUS_ARGS("u", uid, "o", object_path), | |
3723 | 0), | |
3724 | SD_BUS_SIGNAL_WITH_ARGS("UserRemoved", | |
3725 | SD_BUS_ARGS("u", uid, "o", object_path), | |
3726 | 0), | |
3727 | SD_BUS_SIGNAL_WITH_ARGS("SeatNew", | |
3728 | SD_BUS_ARGS("s", seat_id, "o", object_path), | |
3729 | 0), | |
3730 | SD_BUS_SIGNAL_WITH_ARGS("SeatRemoved", | |
3731 | SD_BUS_ARGS("s", seat_id, "o", object_path), | |
3732 | 0), | |
3733 | SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdown", | |
3734 | SD_BUS_ARGS("b", start), | |
3735 | 0), | |
e4aab5cf LB |
3736 | SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdownWithMetadata", |
3737 | SD_BUS_ARGS("b", start, "a{sv}", metadata), | |
3738 | 0), | |
a6293b05 NK |
3739 | SD_BUS_SIGNAL_WITH_ARGS("PrepareForSleep", |
3740 | SD_BUS_ARGS("b", start), | |
3741 | 0), | |
cc377381 LP |
3742 | |
3743 | SD_BUS_VTABLE_END | |
3744 | }; | |
3f49d45a | 3745 | |
c2b178d3 ZJS |
3746 | const BusObjectImplementation manager_object = { |
3747 | "/org/freedesktop/login1", | |
3748 | "org.freedesktop.login1.Manager", | |
3749 | .vtables = BUS_VTABLES(manager_vtable), | |
3750 | .children = BUS_IMPLEMENTATIONS(&seat_object, | |
3751 | &session_object, | |
3752 | &user_object), | |
3753 | }; | |
3754 | ||
b39648ed | 3755 | static int session_jobs_reply(Session *s, uint32_t jid, const char *unit, const char *result) { |
99e7e392 DH |
3756 | assert(s); |
3757 | assert(unit); | |
3758 | ||
3759 | if (!s->started) | |
25a1ab4e | 3760 | return 0; |
99e7e392 | 3761 | |
25a1ab4e | 3762 | if (result && !streq(result, "done")) { |
4afd3348 | 3763 | _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; |
99e7e392 | 3764 | |
124d7cb2 | 3765 | sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, |
b39648ed | 3766 | "Job %u for unit '%s' failed with '%s'", jid, unit, result); |
25a1ab4e | 3767 | return session_send_create_reply(s, &e); |
99e7e392 DH |
3768 | } |
3769 | ||
25a1ab4e | 3770 | return session_send_create_reply(s, NULL); |
99e7e392 DH |
3771 | } |
3772 | ||
19070062 | 3773 | int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 3774 | const char *path, *result, *unit; |
99534007 | 3775 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
3776 | Session *session; |
3777 | uint32_t id; | |
3778 | User *user; | |
3779 | int r; | |
3f49d45a | 3780 | |
cc377381 | 3781 | assert(message); |
3f49d45a | 3782 | |
cc377381 LP |
3783 | r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result); |
3784 | if (r < 0) { | |
ebcf1f97 | 3785 | bus_log_parse_error(r); |
65d73cf0 | 3786 | return 0; |
cc377381 | 3787 | } |
3f49d45a | 3788 | |
cc377381 | 3789 | if (m->action_job && streq(m->action_job, path)) { |
5ed73478 LN |
3790 | assert(m->delayed_action); |
3791 | log_info("Operation '%s' finished.", inhibit_what_to_string(m->delayed_action->inhibit_what)); | |
3f49d45a | 3792 | |
cc377381 | 3793 | /* Tell people that they now may take a lock again */ |
e4aab5cf | 3794 | (void) send_prepare_for(m, m->delayed_action, false); |
3f49d45a | 3795 | |
491ac9f2 | 3796 | m->action_job = mfree(m->action_job); |
5ed73478 | 3797 | m->delayed_action = NULL; |
cc377381 LP |
3798 | return 0; |
3799 | } | |
3f49d45a | 3800 | |
cc377381 | 3801 | session = hashmap_get(m->session_units, unit); |
25a1ab4e LP |
3802 | if (session) { |
3803 | if (streq_ptr(path, session->scope_job)) { | |
3804 | session->scope_job = mfree(session->scope_job); | |
b39648ed | 3805 | (void) session_jobs_reply(session, id, unit, result); |
25a1ab4e LP |
3806 | |
3807 | session_save(session); | |
3808 | user_save(session->user); | |
3809 | } | |
3f49d45a | 3810 | |
cc377381 LP |
3811 | session_add_to_gc_queue(session); |
3812 | } | |
3f49d45a | 3813 | |
cc377381 | 3814 | user = hashmap_get(m->user_units, unit); |
25a1ab4e LP |
3815 | if (user) { |
3816 | if (streq_ptr(path, user->service_job)) { | |
491ac9f2 | 3817 | user->service_job = mfree(user->service_job); |
3f49d45a | 3818 | |
03677889 YW |
3819 | LIST_FOREACH(sessions_by_user, s, user->sessions) |
3820 | (void) session_jobs_reply(s, id, unit, NULL /* don't propagate user service failures to the client */); | |
3f49d45a | 3821 | |
25a1ab4e LP |
3822 | user_save(user); |
3823 | } | |
dd9b67aa | 3824 | |
cc377381 | 3825 | user_add_to_gc_queue(user); |
3f49d45a LP |
3826 | } |
3827 | ||
cc377381 | 3828 | return 0; |
3f49d45a LP |
3829 | } |
3830 | ||
19070062 | 3831 | int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 3832 | const char *path, *unit; |
99534007 | 3833 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
3834 | Session *session; |
3835 | User *user; | |
3836 | int r; | |
1713813d | 3837 | |
1713813d LP |
3838 | assert(message); |
3839 | ||
cc377381 LP |
3840 | r = sd_bus_message_read(message, "so", &unit, &path); |
3841 | if (r < 0) { | |
ebcf1f97 | 3842 | bus_log_parse_error(r); |
65d73cf0 | 3843 | return 0; |
cc377381 | 3844 | } |
fb6becb4 | 3845 | |
cc377381 LP |
3846 | session = hashmap_get(m->session_units, unit); |
3847 | if (session) | |
3848 | session_add_to_gc_queue(session); | |
fb6becb4 | 3849 | |
cc377381 LP |
3850 | user = hashmap_get(m->user_units, unit); |
3851 | if (user) | |
3852 | user_add_to_gc_queue(user); | |
fb6becb4 | 3853 | |
cc377381 LP |
3854 | return 0; |
3855 | } | |
fb6becb4 | 3856 | |
19070062 | 3857 | int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 3858 | _cleanup_free_ char *unit = NULL; |
99534007 | 3859 | Manager *m = ASSERT_PTR(userdata); |
cc377381 LP |
3860 | const char *path; |
3861 | Session *session; | |
3862 | User *user; | |
ebcf1f97 | 3863 | int r; |
fb6becb4 | 3864 | |
cc377381 | 3865 | assert(message); |
fb6becb4 | 3866 | |
cc377381 LP |
3867 | path = sd_bus_message_get_path(message); |
3868 | if (!path) | |
3869 | return 0; | |
fb6becb4 | 3870 | |
ebcf1f97 | 3871 | r = unit_name_from_dbus_path(path, &unit); |
e5f5b5b9 LP |
3872 | if (r == -EINVAL) /* not a unit */ |
3873 | return 0; | |
65d73cf0 LP |
3874 | if (r < 0) { |
3875 | log_oom(); | |
3876 | return 0; | |
3877 | } | |
fb6becb4 | 3878 | |
cc377381 LP |
3879 | session = hashmap_get(m->session_units, unit); |
3880 | if (session) | |
3881 | session_add_to_gc_queue(session); | |
fb6becb4 | 3882 | |
cc377381 LP |
3883 | user = hashmap_get(m->user_units, unit); |
3884 | if (user) | |
3885 | user_add_to_gc_queue(user); | |
fb6becb4 | 3886 | |
cc377381 LP |
3887 | return 0; |
3888 | } | |
6fa48533 | 3889 | |
19070062 | 3890 | int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 3891 | Manager *m = ASSERT_PTR(userdata); |
cc377381 | 3892 | Session *session; |
cc377381 | 3893 | int b, r; |
943aca8e | 3894 | |
19070062 | 3895 | assert(message); |
943aca8e | 3896 | |
cc377381 LP |
3897 | r = sd_bus_message_read(message, "b", &b); |
3898 | if (r < 0) { | |
ebcf1f97 | 3899 | bus_log_parse_error(r); |
65d73cf0 | 3900 | return 0; |
cc377381 | 3901 | } |
943aca8e | 3902 | |
cc377381 LP |
3903 | if (b) |
3904 | return 0; | |
943aca8e | 3905 | |
cc377381 LP |
3906 | /* systemd finished reloading, let's recheck all our sessions */ |
3907 | log_debug("System manager has been reloaded, rechecking sessions..."); | |
6797c324 | 3908 | |
90e74a66 | 3909 | HASHMAP_FOREACH(session, m->sessions) |
cc377381 | 3910 | session_add_to_gc_queue(session); |
6797c324 | 3911 | |
cc377381 LP |
3912 | return 0; |
3913 | } | |
943aca8e | 3914 | |
cc377381 LP |
3915 | int manager_send_changed(Manager *manager, const char *property, ...) { |
3916 | char **l; | |
9418f147 LP |
3917 | |
3918 | assert(manager); | |
3919 | ||
cc377381 | 3920 | l = strv_from_stdarg_alloca(property); |
9418f147 | 3921 | |
cc377381 LP |
3922 | return sd_bus_emit_properties_changed_strv( |
3923 | manager->bus, | |
3924 | "/org/freedesktop/login1", | |
3925 | "org.freedesktop.login1.Manager", | |
3926 | l); | |
9418f147 | 3927 | } |
eecd1362 | 3928 | |
2adae5ac ZJS |
3929 | static int strdup_job(sd_bus_message *reply, char **job) { |
3930 | const char *j; | |
3931 | char *copy; | |
3932 | int r; | |
3933 | ||
3934 | r = sd_bus_message_read(reply, "o", &j); | |
3935 | if (r < 0) | |
3936 | return r; | |
3937 | ||
3938 | copy = strdup(j); | |
3939 | if (!copy) | |
3940 | return -ENOMEM; | |
3941 | ||
3942 | *job = copy; | |
3943 | return 1; | |
3944 | } | |
3945 | ||
fb6becb4 LP |
3946 | int manager_start_scope( |
3947 | Manager *manager, | |
3948 | const char *scope, | |
3949 | pid_t pid, | |
3950 | const char *slice, | |
3951 | const char *description, | |
25a1ab4e LP |
3952 | char **wants, |
3953 | char **after, | |
d5ac9d06 | 3954 | const char *requires_mounts_for, |
22f93314 | 3955 | sd_bus_message *more_properties, |
cc377381 | 3956 | sd_bus_error *error, |
fb6becb4 LP |
3957 | char **job) { |
3958 | ||
4afd3348 | 3959 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
cc377381 | 3960 | int r; |
fb6becb4 LP |
3961 | |
3962 | assert(manager); | |
3963 | assert(scope); | |
3964 | assert(pid > 1); | |
2adae5ac | 3965 | assert(job); |
fb6becb4 | 3966 | |
5d990cc5 | 3967 | r = bus_message_new_method_call(manager->bus, &m, bus_systemd_mgr, "StartTransientUnit"); |
cc377381 LP |
3968 | if (r < 0) |
3969 | return r; | |
fb6becb4 | 3970 | |
cc377381 LP |
3971 | r = sd_bus_message_append(m, "ss", strempty(scope), "fail"); |
3972 | if (r < 0) | |
3973 | return r; | |
fb6becb4 | 3974 | |
cc377381 LP |
3975 | r = sd_bus_message_open_container(m, 'a', "(sv)"); |
3976 | if (r < 0) | |
3977 | return r; | |
fb6becb4 LP |
3978 | |
3979 | if (!isempty(slice)) { | |
cc377381 LP |
3980 | r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice); |
3981 | if (r < 0) | |
3982 | return r; | |
fb6becb4 LP |
3983 | } |
3984 | ||
3985 | if (!isempty(description)) { | |
cc377381 LP |
3986 | r = sd_bus_message_append(m, "(sv)", "Description", "s", description); |
3987 | if (r < 0) | |
3988 | return r; | |
fb6becb4 LP |
3989 | } |
3990 | ||
25a1ab4e LP |
3991 | STRV_FOREACH(i, wants) { |
3992 | r = sd_bus_message_append(m, "(sv)", "Wants", "as", 1, *i); | |
cc377381 LP |
3993 | if (r < 0) |
3994 | return r; | |
7fb3ee51 LP |
3995 | } |
3996 | ||
25a1ab4e LP |
3997 | STRV_FOREACH(i, after) { |
3998 | r = sd_bus_message_append(m, "(sv)", "After", "as", 1, *i); | |
ba4c5d93 LP |
3999 | if (r < 0) |
4000 | return r; | |
4001 | } | |
4002 | ||
d5ac9d06 LP |
4003 | if (!empty_or_root(requires_mounts_for)) { |
4004 | r = sd_bus_message_append(m, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for); | |
4005 | if (r < 0) | |
4006 | return r; | |
4007 | } | |
4008 | ||
e743bca2 LP |
4009 | /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore |
4010 | * SIGTERM */ | |
cc377381 LP |
4011 | r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true); |
4012 | if (r < 0) | |
4013 | return r; | |
4014 | ||
4015 | r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid); | |
4016 | if (r < 0) | |
4017 | return r; | |
4018 | ||
98b6c94b ML |
4019 | /* For login session scopes, if a process is OOM killed by the kernel, *don't* terminate the rest of |
4020 | the scope */ | |
4021 | r = sd_bus_message_append(m, "(sv)", "OOMPolicy", "s", "continue"); | |
4022 | if (r < 0) | |
4023 | return r; | |
4024 | ||
22f93314 | 4025 | /* disable TasksMax= for the session scope, rely on the slice setting for it */ |
f5fbe71d | 4026 | r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", UINT64_MAX); |
90558f31 | 4027 | if (r < 0) |
22f93314 JS |
4028 | return bus_log_create_error(r); |
4029 | ||
4030 | if (more_properties) { | |
4031 | /* If TasksMax also appears here, it will overwrite the default value set above */ | |
4032 | r = sd_bus_message_copy(m, more_properties, true); | |
4033 | if (r < 0) | |
4034 | return r; | |
4035 | } | |
90558f31 | 4036 | |
cc377381 LP |
4037 | r = sd_bus_message_close_container(m); |
4038 | if (r < 0) | |
4039 | return r; | |
86b8d289 LP |
4040 | |
4041 | r = sd_bus_message_append(m, "a(sa(sv))", 0); | |
4042 | if (r < 0) | |
4043 | return r; | |
cc377381 | 4044 | |
c49b30a2 | 4045 | r = sd_bus_call(manager->bus, m, 0, error, &reply); |
cc377381 LP |
4046 | if (r < 0) |
4047 | return r; | |
fb6becb4 | 4048 | |
2adae5ac | 4049 | return strdup_job(reply, job); |
fb6becb4 LP |
4050 | } |
4051 | ||
cc377381 | 4052 | int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) { |
4afd3348 | 4053 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
fb6becb4 LP |
4054 | int r; |
4055 | ||
4056 | assert(manager); | |
4057 | assert(unit); | |
2adae5ac | 4058 | assert(job); |
fb6becb4 | 4059 | |
5d990cc5 | 4060 | r = bus_call_method( |
fb6becb4 | 4061 | manager->bus, |
5d990cc5 | 4062 | bus_systemd_mgr, |
fb6becb4 | 4063 | "StartUnit", |
fb6becb4 | 4064 | error, |
cc377381 | 4065 | &reply, |
79ee4ad1 | 4066 | "ss", unit, "replace"); |
cc377381 | 4067 | if (r < 0) |
fb6becb4 | 4068 | return r; |
fb6becb4 | 4069 | |
2adae5ac | 4070 | return strdup_job(reply, job); |
fb6becb4 LP |
4071 | } |
4072 | ||
1a42ce09 | 4073 | int manager_stop_unit(Manager *manager, const char *unit, const char *job_mode, sd_bus_error *error, char **ret_job) { |
4afd3348 | 4074 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
fb6becb4 LP |
4075 | int r; |
4076 | ||
4077 | assert(manager); | |
4078 | assert(unit); | |
1a42ce09 | 4079 | assert(ret_job); |
fb6becb4 | 4080 | |
5d990cc5 | 4081 | r = bus_call_method( |
fb6becb4 | 4082 | manager->bus, |
5d990cc5 | 4083 | bus_systemd_mgr, |
fb6becb4 | 4084 | "StopUnit", |
fb6becb4 | 4085 | error, |
cc377381 | 4086 | &reply, |
1a42ce09 | 4087 | "ss", unit, job_mode ?: "fail"); |
fb6becb4 | 4088 | if (r < 0) { |
955a6329 ZJS |
4089 | if (sd_bus_error_has_names(error, BUS_ERROR_NO_SUCH_UNIT, |
4090 | BUS_ERROR_LOAD_FAILED)) { | |
6797c324 | 4091 | |
1a42ce09 | 4092 | *ret_job = NULL; |
cc377381 | 4093 | sd_bus_error_free(error); |
6797c324 LP |
4094 | return 0; |
4095 | } | |
4096 | ||
fb6becb4 LP |
4097 | return r; |
4098 | } | |
4099 | ||
1a42ce09 | 4100 | return strdup_job(reply, ret_job); |
fb6becb4 LP |
4101 | } |
4102 | ||
ea3a7cf6 LP |
4103 | int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *ret_error) { |
4104 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
5f41d1f1 LP |
4105 | _cleanup_free_ char *path = NULL; |
4106 | int r; | |
4107 | ||
4108 | assert(manager); | |
4109 | assert(scope); | |
4110 | ||
4111 | path = unit_dbus_path_from_name(scope); | |
4112 | if (!path) | |
4113 | return -ENOMEM; | |
4114 | ||
4115 | r = sd_bus_call_method( | |
4116 | manager->bus, | |
4117 | "org.freedesktop.systemd1", | |
4118 | path, | |
4119 | "org.freedesktop.systemd1.Scope", | |
4120 | "Abandon", | |
ea3a7cf6 | 4121 | &error, |
5f41d1f1 LP |
4122 | NULL, |
4123 | NULL); | |
4124 | if (r < 0) { | |
955a6329 ZJS |
4125 | if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT, |
4126 | BUS_ERROR_LOAD_FAILED, | |
4127 | BUS_ERROR_SCOPE_NOT_RUNNING)) | |
5f41d1f1 | 4128 | return 0; |
5f41d1f1 | 4129 | |
ea3a7cf6 | 4130 | sd_bus_error_move(ret_error, &error); |
5f41d1f1 LP |
4131 | return r; |
4132 | } | |
4133 | ||
4134 | return 1; | |
4135 | } | |
4136 | ||
cc377381 | 4137 | int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) { |
fb6becb4 LP |
4138 | assert(manager); |
4139 | assert(unit); | |
4140 | ||
5d990cc5 | 4141 | return bus_call_method( |
fb6becb4 | 4142 | manager->bus, |
5d990cc5 | 4143 | bus_systemd_mgr, |
fb6becb4 | 4144 | "KillUnit", |
fb6becb4 | 4145 | error, |
cc377381 LP |
4146 | NULL, |
4147 | "ssi", unit, who == KILL_LEADER ? "main" : "all", signo); | |
fb6becb4 LP |
4148 | } |
4149 | ||
bd26aee1 | 4150 | int manager_unit_is_active(Manager *manager, const char *unit, sd_bus_error *ret_error) { |
4afd3348 LP |
4151 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4152 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
fb6becb4 | 4153 | _cleanup_free_ char *path = NULL; |
fb6becb4 | 4154 | const char *state; |
fb6becb4 LP |
4155 | int r; |
4156 | ||
4157 | assert(manager); | |
4158 | assert(unit); | |
4159 | ||
fb6becb4 LP |
4160 | path = unit_dbus_path_from_name(unit); |
4161 | if (!path) | |
4162 | return -ENOMEM; | |
4163 | ||
cc377381 | 4164 | r = sd_bus_get_property( |
fb6becb4 LP |
4165 | manager->bus, |
4166 | "org.freedesktop.systemd1", | |
4167 | path, | |
cc377381 LP |
4168 | "org.freedesktop.systemd1.Unit", |
4169 | "ActiveState", | |
fb6becb4 | 4170 | &error, |
cc377381 LP |
4171 | &reply, |
4172 | "s"); | |
fb6becb4 | 4173 | if (r < 0) { |
ebcf6976 | 4174 | /* systemd might have dropped off momentarily, let's |
cc377381 | 4175 | * not make this an error */ |
955a6329 ZJS |
4176 | if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY, |
4177 | SD_BUS_ERROR_DISCONNECTED)) | |
6797c324 | 4178 | return true; |
6797c324 | 4179 | |
cc377381 LP |
4180 | /* If the unit is already unloaded then it's not |
4181 | * active */ | |
955a6329 ZJS |
4182 | if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT, |
4183 | BUS_ERROR_LOAD_FAILED)) | |
6797c324 | 4184 | return false; |
6797c324 | 4185 | |
bd26aee1 | 4186 | sd_bus_error_move(ret_error, &error); |
fb6becb4 LP |
4187 | return r; |
4188 | } | |
4189 | ||
cc377381 LP |
4190 | r = sd_bus_message_read(reply, "s", &state); |
4191 | if (r < 0) | |
bd26aee1 | 4192 | return r; |
fb6becb4 | 4193 | |
bd26aee1 | 4194 | return !STR_IN_SET(state, "inactive", "failed"); |
cc377381 LP |
4195 | } |
4196 | ||
bd26aee1 | 4197 | int manager_job_is_active(Manager *manager, const char *path, sd_bus_error *ret_error) { |
4afd3348 LP |
4198 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4199 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
cc377381 LP |
4200 | int r; |
4201 | ||
4202 | assert(manager); | |
4203 | assert(path); | |
4204 | ||
4205 | r = sd_bus_get_property( | |
4206 | manager->bus, | |
4207 | "org.freedesktop.systemd1", | |
4208 | path, | |
4209 | "org.freedesktop.systemd1.Job", | |
4210 | "State", | |
4211 | &error, | |
4212 | &reply, | |
4213 | "s"); | |
4214 | if (r < 0) { | |
955a6329 ZJS |
4215 | if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY, |
4216 | SD_BUS_ERROR_DISCONNECTED)) | |
cc377381 LP |
4217 | return true; |
4218 | ||
4219 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) | |
4220 | return false; | |
4221 | ||
bd26aee1 | 4222 | sd_bus_error_move(ret_error, &error); |
cc377381 | 4223 | return r; |
fb6becb4 LP |
4224 | } |
4225 | ||
cc377381 LP |
4226 | /* We don't actually care about the state really. The fact |
4227 | * that we could read the job state is enough for us */ | |
fb6becb4 | 4228 | |
cc377381 | 4229 | return true; |
fb6becb4 | 4230 | } |