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