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