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