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