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