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