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