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