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