| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <pwd.h> |
| 4 |
#include <sys/stat.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "sd-bus.h" |
| 8 |
#include "sd-device.h" |
| 9 |
#include "sd-messages.h" |
| 10 |
|
| 11 |
#include "alloc-util.h" |
| 12 |
#include "audit-util.h" |
| 13 |
#include "bitfield.h" |
| 14 |
#include "bootspec.h" |
| 15 |
#include "bus-common-errors.h" |
| 16 |
#include "bus-error.h" |
| 17 |
#include "bus-get-properties.h" |
| 18 |
#include "bus-locator.h" |
| 19 |
#include "bus-object.h" |
| 20 |
#include "bus-polkit.h" |
| 21 |
#include "bus-unit-util.h" |
| 22 |
#include "bus-util.h" |
| 23 |
#include "cgroup-util.h" |
| 24 |
#include "device-util.h" |
| 25 |
#include "dirent-util.h" |
| 26 |
#include "efi-api.h" |
| 27 |
#include "efi-loader.h" |
| 28 |
#include "efivars.h" |
| 29 |
#include "env-file.h" |
| 30 |
#include "env-util.h" |
| 31 |
#include "errno-util.h" |
| 32 |
#include "escape.h" |
| 33 |
#include "event-util.h" |
| 34 |
#include "fd-util.h" |
| 35 |
#include "fileio.h" |
| 36 |
#include "format-util.h" |
| 37 |
#include "fs-util.h" |
| 38 |
#include "hashmap.h" |
| 39 |
#include "login-util.h" |
| 40 |
#include "logind-session.h" |
| 41 |
#include "logind.h" |
| 42 |
#include "logind-action.h" |
| 43 |
#include "logind-dbus.h" |
| 44 |
#include "logind-polkit.h" |
| 45 |
#include "logind-seat.h" |
| 46 |
#include "logind-seat-dbus.h" |
| 47 |
#include "logind-session-dbus.h" |
| 48 |
#include "logind-shutdown.h" |
| 49 |
#include "logind-user.h" |
| 50 |
#include "logind-user-dbus.h" |
| 51 |
#include "logind-utmp.h" |
| 52 |
#include "mkdir.h" |
| 53 |
#include "os-util.h" |
| 54 |
#include "parse-util.h" |
| 55 |
#include "path-util.h" |
| 56 |
#include "process-util.h" |
| 57 |
#include "reboot-util.h" |
| 58 |
#include "serialize.h" |
| 59 |
#include "signal-util.h" |
| 60 |
#include "sleep-config.h" |
| 61 |
#include "stdio-util.h" |
| 62 |
#include "string-util.h" |
| 63 |
#include "strv.h" |
| 64 |
#include "terminal-util.h" |
| 65 |
#include "tmpfile-util.h" |
| 66 |
#include "unit-def.h" |
| 67 |
#include "user-record.h" |
| 68 |
#include "user-util.h" |
| 69 |
#include "virt.h" |
| 70 |
#include "wall.h" |
| 71 |
|
| 72 |
/* As a random fun fact sysvinit had a 252 (256-(strlen(" \r\n")+1)) |
| 73 |
* character limit for the wall message. |
| 74 |
* https://git.savannah.nongnu.org/cgit/sysvinit.git/tree/src/shutdown.c#n72 |
| 75 |
* There is no real technical need for that but doesn't make sense |
| 76 |
* to store arbitrary amounts either. As we are not stingy here, we |
| 77 |
* allow 4k. |
| 78 |
*/ |
| 79 |
#define WALL_MESSAGE_MAX 4096U |
| 80 |
|
| 81 |
static int get_sender_session( |
| 82 |
Manager *m, |
| 83 |
sd_bus_message *message, |
| 84 |
bool consult_display, |
| 85 |
sd_bus_error *error, |
| 86 |
Session **ret) { |
| 87 |
|
| 88 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
| 89 |
Session *session = NULL; |
| 90 |
const char *name; |
| 91 |
int r; |
| 92 |
|
| 93 |
assert(m); |
| 94 |
assert(ret); |
| 95 |
|
| 96 |
/* Acquire the sender's session. This first checks if the sending process is inside a session itself, |
| 97 |
* and returns that. If not and 'consult_display' is true, this returns the display session of the |
| 98 |
* owning user of the caller. */ |
| 99 |
|
| 100 |
r = sd_bus_query_sender_creds(message, |
| 101 |
SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT| |
| 102 |
(consult_display ? SD_BUS_CREDS_OWNER_UID : 0), &creds); |
| 103 |
if (r < 0) |
| 104 |
return r; |
| 105 |
|
| 106 |
r = sd_bus_creds_get_session(creds, &name); |
| 107 |
if (r < 0) { |
| 108 |
if (r != -ENXIO) |
| 109 |
return r; |
| 110 |
|
| 111 |
if (consult_display) { |
| 112 |
uid_t uid; |
| 113 |
|
| 114 |
r = sd_bus_creds_get_owner_uid(creds, &uid); |
| 115 |
if (r < 0) { |
| 116 |
if (r != -ENXIO) |
| 117 |
return r; |
| 118 |
} else { |
| 119 |
User *user; |
| 120 |
|
| 121 |
user = hashmap_get(m->users, UID_TO_PTR(uid)); |
| 122 |
if (user) |
| 123 |
session = user->display; |
| 124 |
} |
| 125 |
} |
| 126 |
} else |
| 127 |
session = hashmap_get(m->sessions, name); |
| 128 |
|
| 129 |
if (!session) |
| 130 |
return sd_bus_error_set(error, BUS_ERROR_NO_SESSION_FOR_PID, |
| 131 |
consult_display ? |
| 132 |
"Caller does not belong to any known session and doesn't own any suitable session." : |
| 133 |
"Caller does not belong to any known session."); |
| 134 |
|
| 135 |
*ret = session; |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
int manager_get_session_from_creds( |
| 140 |
Manager *m, |
| 141 |
sd_bus_message *message, |
| 142 |
const char *name, |
| 143 |
sd_bus_error *error, |
| 144 |
Session **ret) { |
| 145 |
|
| 146 |
Session *session; |
| 147 |
|
| 148 |
assert(m); |
| 149 |
assert(ret); |
| 150 |
|
| 151 |
if (session_is_self(name)) /* the caller's own session */ |
| 152 |
return get_sender_session(m, message, false, error, ret); |
| 153 |
if (session_is_auto(name)) /* The caller's own session if they have one, otherwise their user's display session */ |
| 154 |
return get_sender_session(m, message, true, error, ret); |
| 155 |
|
| 156 |
session = hashmap_get(m->sessions, name); |
| 157 |
if (!session) |
| 158 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name); |
| 159 |
|
| 160 |
*ret = session; |
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
static int get_sender_user(Manager *m, sd_bus_message *message, sd_bus_error *error, User **ret) { |
| 165 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
| 166 |
uid_t uid; |
| 167 |
User *user; |
| 168 |
int r; |
| 169 |
|
| 170 |
assert(ret); |
| 171 |
|
| 172 |
/* Note that we get the owner UID of the session, not the actual client UID here! */ |
| 173 |
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds); |
| 174 |
if (r < 0) |
| 175 |
return r; |
| 176 |
|
| 177 |
r = sd_bus_creds_get_owner_uid(creds, &uid); |
| 178 |
if (r < 0) { |
| 179 |
if (r != -ENXIO) |
| 180 |
return r; |
| 181 |
|
| 182 |
user = NULL; |
| 183 |
} else |
| 184 |
user = hashmap_get(m->users, UID_TO_PTR(uid)); |
| 185 |
|
| 186 |
if (!user) |
| 187 |
return sd_bus_error_set(error, BUS_ERROR_NO_USER_FOR_PID, |
| 188 |
"Caller does not belong to any logged in or lingering user"); |
| 189 |
|
| 190 |
*ret = user; |
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret) { |
| 195 |
User *user; |
| 196 |
|
| 197 |
assert(m); |
| 198 |
assert(ret); |
| 199 |
|
| 200 |
if (!uid_is_valid(uid)) |
| 201 |
return get_sender_user(m, message, error, ret); |
| 202 |
|
| 203 |
user = hashmap_get(m->users, UID_TO_PTR(uid)); |
| 204 |
if (!user) |
| 205 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, |
| 206 |
"User ID "UID_FMT" is not logged in or lingering", uid); |
| 207 |
|
| 208 |
*ret = user; |
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
int manager_get_seat_from_creds( |
| 213 |
Manager *m, |
| 214 |
sd_bus_message *message, |
| 215 |
const char *name, |
| 216 |
sd_bus_error *error, |
| 217 |
Seat **ret) { |
| 218 |
|
| 219 |
Seat *seat; |
| 220 |
int r; |
| 221 |
|
| 222 |
assert(m); |
| 223 |
assert(ret); |
| 224 |
|
| 225 |
if (seat_is_self(name) || seat_is_auto(name)) { |
| 226 |
Session *session; |
| 227 |
|
| 228 |
/* Use these special seat names as session names */ |
| 229 |
r = manager_get_session_from_creds(m, message, name, error, &session); |
| 230 |
if (r < 0) |
| 231 |
return r; |
| 232 |
|
| 233 |
seat = session->seat; |
| 234 |
if (!seat) |
| 235 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "Session '%s' has no seat.", session->id); |
| 236 |
} else { |
| 237 |
seat = hashmap_get(m->seats, name); |
| 238 |
if (!seat) |
| 239 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name); |
| 240 |
} |
| 241 |
|
| 242 |
*ret = seat; |
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
static int return_test_polkit( |
| 247 |
sd_bus_message *message, |
| 248 |
const char *action, |
| 249 |
const char **details, |
| 250 |
uid_t good_user, |
| 251 |
sd_bus_error *e) { |
| 252 |
|
| 253 |
const char *result; |
| 254 |
bool challenge; |
| 255 |
int r; |
| 256 |
|
| 257 |
r = bus_test_polkit(message, action, details, good_user, &challenge, e); |
| 258 |
if (r < 0) |
| 259 |
return r; |
| 260 |
|
| 261 |
if (r > 0) |
| 262 |
result = "yes"; |
| 263 |
else if (challenge) |
| 264 |
result = "challenge"; |
| 265 |
else |
| 266 |
result = "no"; |
| 267 |
|
| 268 |
return sd_bus_reply_method_return(message, "s", result); |
| 269 |
} |
| 270 |
|
| 271 |
static int property_get_idle_hint( |
| 272 |
sd_bus *bus, |
| 273 |
const char *path, |
| 274 |
const char *interface, |
| 275 |
const char *property, |
| 276 |
sd_bus_message *reply, |
| 277 |
void *userdata, |
| 278 |
sd_bus_error *error) { |
| 279 |
|
| 280 |
Manager *m = ASSERT_PTR(userdata); |
| 281 |
|
| 282 |
assert(bus); |
| 283 |
assert(reply); |
| 284 |
|
| 285 |
return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, /* ret_timestamp= */ NULL)); |
| 286 |
} |
| 287 |
|
| 288 |
static int property_get_idle_since_hint( |
| 289 |
sd_bus *bus, |
| 290 |
const char *path, |
| 291 |
const char *interface, |
| 292 |
const char *property, |
| 293 |
sd_bus_message *reply, |
| 294 |
void *userdata, |
| 295 |
sd_bus_error *error) { |
| 296 |
|
| 297 |
Manager *m = ASSERT_PTR(userdata); |
| 298 |
dual_timestamp t; |
| 299 |
|
| 300 |
assert(bus); |
| 301 |
assert(reply); |
| 302 |
|
| 303 |
manager_get_idle_hint(m, &t); |
| 304 |
|
| 305 |
return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic); |
| 306 |
} |
| 307 |
|
| 308 |
static int property_get_inhibited( |
| 309 |
sd_bus *bus, |
| 310 |
const char *path, |
| 311 |
const char *interface, |
| 312 |
const char *property, |
| 313 |
sd_bus_message *reply, |
| 314 |
void *userdata, |
| 315 |
sd_bus_error *error) { |
| 316 |
|
| 317 |
Manager *m = ASSERT_PTR(userdata); |
| 318 |
InhibitWhat w; |
| 319 |
|
| 320 |
assert(bus); |
| 321 |
assert(reply); |
| 322 |
|
| 323 |
if (streq(property, "BlockInhibited")) |
| 324 |
w = manager_inhibit_what(m, INHIBIT_BLOCK); |
| 325 |
else if (streq(property, "BlockWeakInhibited")) |
| 326 |
w = manager_inhibit_what(m, INHIBIT_BLOCK_WEAK); |
| 327 |
else if (streq(property, "DelayInhibited")) |
| 328 |
w = manager_inhibit_what(m, INHIBIT_DELAY); |
| 329 |
else |
| 330 |
assert_not_reached(); |
| 331 |
|
| 332 |
return sd_bus_message_append(reply, "s", inhibit_what_to_string(w)); |
| 333 |
} |
| 334 |
|
| 335 |
static int property_get_preparing( |
| 336 |
sd_bus *bus, |
| 337 |
const char *path, |
| 338 |
const char *interface, |
| 339 |
const char *property, |
| 340 |
sd_bus_message *reply, |
| 341 |
void *userdata, |
| 342 |
sd_bus_error *error) { |
| 343 |
|
| 344 |
Manager *m = ASSERT_PTR(userdata); |
| 345 |
bool b = false; |
| 346 |
|
| 347 |
assert(bus); |
| 348 |
assert(reply); |
| 349 |
|
| 350 |
if (m->delayed_action) { |
| 351 |
if (streq(property, "PreparingForShutdown")) |
| 352 |
b = m->delayed_action->inhibit_what & INHIBIT_SHUTDOWN; |
| 353 |
else |
| 354 |
b = m->delayed_action->inhibit_what & INHIBIT_SLEEP; |
| 355 |
} |
| 356 |
|
| 357 |
return sd_bus_message_append(reply, "b", b); |
| 358 |
} |
| 359 |
|
| 360 |
static int property_get_preparing_shutdown_with_metadata( |
| 361 |
sd_bus *bus, |
| 362 |
const char *path, |
| 363 |
const char *interface, |
| 364 |
const char *property, |
| 365 |
sd_bus_message *reply, |
| 366 |
void *userdata, |
| 367 |
sd_bus_error *error) { |
| 368 |
|
| 369 |
Manager *m = ASSERT_PTR(userdata); |
| 370 |
|
| 371 |
assert(bus); |
| 372 |
assert(reply); |
| 373 |
|
| 374 |
if (!m->delayed_action || !(m->delayed_action->inhibit_what & INHIBIT_SHUTDOWN)) |
| 375 |
return sd_bus_message_append(reply, "a{sv}", 1, "preparing", "b", false); |
| 376 |
|
| 377 |
return sd_bus_message_append( |
| 378 |
reply, |
| 379 |
"a{sv}", |
| 380 |
2, |
| 381 |
"preparing", |
| 382 |
"b", |
| 383 |
true, |
| 384 |
"type", |
| 385 |
"s", |
| 386 |
handle_action_to_string(m->delayed_action->handle)); |
| 387 |
} |
| 388 |
|
| 389 |
static int property_get_sleep_operations( |
| 390 |
sd_bus *bus, |
| 391 |
const char *path, |
| 392 |
const char *interface, |
| 393 |
const char *property, |
| 394 |
sd_bus_message *reply, |
| 395 |
void *userdata, |
| 396 |
sd_bus_error *error) { |
| 397 |
|
| 398 |
Manager *m = ASSERT_PTR(userdata); |
| 399 |
_cleanup_strv_free_ char **actions = NULL; |
| 400 |
int r; |
| 401 |
|
| 402 |
assert(bus); |
| 403 |
assert(reply); |
| 404 |
|
| 405 |
r = handle_action_get_enabled_sleep_actions(m->handle_action_sleep_mask, &actions); |
| 406 |
if (r < 0) |
| 407 |
return r; |
| 408 |
|
| 409 |
return sd_bus_message_append_strv(reply, actions); |
| 410 |
} |
| 411 |
|
| 412 |
static int property_get_scheduled_shutdown( |
| 413 |
sd_bus *bus, |
| 414 |
const char *path, |
| 415 |
const char *interface, |
| 416 |
const char *property, |
| 417 |
sd_bus_message *reply, |
| 418 |
void *userdata, |
| 419 |
sd_bus_error *error) { |
| 420 |
|
| 421 |
Manager *m = ASSERT_PTR(userdata); |
| 422 |
int r; |
| 423 |
|
| 424 |
assert(bus); |
| 425 |
assert(reply); |
| 426 |
|
| 427 |
r = sd_bus_message_open_container(reply, 'r', "st"); |
| 428 |
if (r < 0) |
| 429 |
return r; |
| 430 |
|
| 431 |
if (m->delayed_action) { |
| 432 |
usec_t t = m->scheduled_shutdown_timeout; /* fall back to the schedule time on failure below */ |
| 433 |
|
| 434 |
if (m->inhibit_timeout_source) { |
| 435 |
r = sd_event_source_get_time(m->inhibit_timeout_source, &t); |
| 436 |
if (r < 0) |
| 437 |
log_debug_errno(r, "Failed to get time of inhibit timeout event source, ignoring: %m"); |
| 438 |
} |
| 439 |
|
| 440 |
r = sd_bus_message_append( |
| 441 |
reply, "st", |
| 442 |
handle_action_to_string(m->delayed_action->handle), t); |
| 443 |
} else |
| 444 |
r = sd_bus_message_append( |
| 445 |
reply, "st", |
| 446 |
handle_action_to_string(m->scheduled_shutdown_action), |
| 447 |
m->scheduled_shutdown_timeout); |
| 448 |
if (r < 0) |
| 449 |
return r; |
| 450 |
|
| 451 |
return sd_bus_message_close_container(reply); |
| 452 |
} |
| 453 |
|
| 454 |
static int property_get_maintenance_time( |
| 455 |
sd_bus *bus, |
| 456 |
const char *path, |
| 457 |
const char *interface, |
| 458 |
const char *property, |
| 459 |
sd_bus_message *reply, |
| 460 |
void *userdata, |
| 461 |
sd_bus_error *error) { |
| 462 |
|
| 463 |
Manager *m = ASSERT_PTR(userdata); |
| 464 |
_cleanup_free_ char *s = NULL; |
| 465 |
int r; |
| 466 |
|
| 467 |
assert(bus); |
| 468 |
assert(reply); |
| 469 |
|
| 470 |
if (m->maintenance_time) { |
| 471 |
r = calendar_spec_to_string(m->maintenance_time, &s); |
| 472 |
if (r < 0) |
| 473 |
return log_error_errno(r, "Failed to format calendar specification: %m"); |
| 474 |
} |
| 475 |
|
| 476 |
return sd_bus_message_append(reply, "s", s); |
| 477 |
} |
| 478 |
|
| 479 |
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction); |
| 480 |
static BUS_DEFINE_PROPERTY_GET(property_get_docked, "b", Manager, manager_is_docked_or_external_displays); |
| 481 |
static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed, "b", Manager, manager_is_lid_closed); |
| 482 |
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power, "b", manager_is_on_external_power()); |
| 483 |
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max, "t", CGROUP_LIMIT_MAX); |
| 484 |
static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size, "t", Hashmap *, (uint64_t) hashmap_size); |
| 485 |
|
| 486 |
static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 487 |
_cleanup_free_ char *p = NULL; |
| 488 |
Manager *m = ASSERT_PTR(userdata); |
| 489 |
const char *name; |
| 490 |
Session *session; |
| 491 |
int r; |
| 492 |
|
| 493 |
assert(message); |
| 494 |
|
| 495 |
r = sd_bus_message_read(message, "s", &name); |
| 496 |
if (r < 0) |
| 497 |
return r; |
| 498 |
|
| 499 |
r = manager_get_session_from_creds(m, message, name, error, &session); |
| 500 |
if (r < 0) |
| 501 |
return r; |
| 502 |
|
| 503 |
p = session_bus_path(session); |
| 504 |
if (!p) |
| 505 |
return -ENOMEM; |
| 506 |
|
| 507 |
return sd_bus_reply_method_return(message, "o", p); |
| 508 |
} |
| 509 |
|
| 510 |
/* Get login session of a process. This is not what you are looking for these days, |
| 511 |
* as apps may instead belong to a user service unit. This includes terminal |
| 512 |
* emulators and hence command-line apps. */ |
| 513 |
static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 514 |
Manager *m = ASSERT_PTR(userdata); |
| 515 |
_cleanup_free_ char *p = NULL; |
| 516 |
Session *session = NULL; |
| 517 |
pid_t pid; |
| 518 |
int r; |
| 519 |
|
| 520 |
assert(message); |
| 521 |
|
| 522 |
assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
| 523 |
|
| 524 |
r = sd_bus_message_read(message, "u", &pid); |
| 525 |
if (r < 0) |
| 526 |
return r; |
| 527 |
if (pid < 0) |
| 528 |
return -EINVAL; |
| 529 |
|
| 530 |
if (pid == 0) { |
| 531 |
r = manager_get_session_from_creds(m, message, NULL, error, &session); |
| 532 |
if (r < 0) |
| 533 |
return r; |
| 534 |
} else { |
| 535 |
r = manager_get_session_by_pidref(m, &PIDREF_MAKE_FROM_PID(pid), &session); |
| 536 |
if (r < 0) |
| 537 |
return r; |
| 538 |
|
| 539 |
if (!session) |
| 540 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, |
| 541 |
"PID "PID_FMT" does not belong to any known session", pid); |
| 542 |
} |
| 543 |
|
| 544 |
p = session_bus_path(session); |
| 545 |
if (!p) |
| 546 |
return -ENOMEM; |
| 547 |
|
| 548 |
return sd_bus_reply_method_return(message, "o", p); |
| 549 |
} |
| 550 |
|
| 551 |
static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 552 |
_cleanup_free_ char *p = NULL; |
| 553 |
Manager *m = ASSERT_PTR(userdata); |
| 554 |
uint32_t uid; |
| 555 |
User *user; |
| 556 |
int r; |
| 557 |
|
| 558 |
assert(message); |
| 559 |
|
| 560 |
r = sd_bus_message_read(message, "u", &uid); |
| 561 |
if (r < 0) |
| 562 |
return r; |
| 563 |
|
| 564 |
r = manager_get_user_from_creds(m, message, uid, error, &user); |
| 565 |
if (r < 0) |
| 566 |
return r; |
| 567 |
|
| 568 |
p = user_bus_path(user); |
| 569 |
if (!p) |
| 570 |
return -ENOMEM; |
| 571 |
|
| 572 |
return sd_bus_reply_method_return(message, "o", p); |
| 573 |
} |
| 574 |
|
| 575 |
static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 576 |
_cleanup_free_ char *p = NULL; |
| 577 |
Manager *m = ASSERT_PTR(userdata); |
| 578 |
User *user = NULL; |
| 579 |
pid_t pid; |
| 580 |
int r; |
| 581 |
|
| 582 |
assert(message); |
| 583 |
|
| 584 |
assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
| 585 |
|
| 586 |
r = sd_bus_message_read(message, "u", &pid); |
| 587 |
if (r < 0) |
| 588 |
return r; |
| 589 |
if (pid < 0) |
| 590 |
return -EINVAL; |
| 591 |
|
| 592 |
if (pid == 0) { |
| 593 |
r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user); |
| 594 |
if (r < 0) |
| 595 |
return r; |
| 596 |
} else { |
| 597 |
r = manager_get_user_by_pid(m, pid, &user); |
| 598 |
if (r < 0) |
| 599 |
return r; |
| 600 |
if (!user) |
| 601 |
return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, |
| 602 |
"PID "PID_FMT" does not belong to any logged in user or lingering user", |
| 603 |
pid); |
| 604 |
} |
| 605 |
|
| 606 |
p = user_bus_path(user); |
| 607 |
if (!p) |
| 608 |
return -ENOMEM; |
| 609 |
|
| 610 |
return sd_bus_reply_method_return(message, "o", p); |
| 611 |
} |
| 612 |
|
| 613 |
static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 614 |
_cleanup_free_ char *p = NULL; |
| 615 |
Manager *m = ASSERT_PTR(userdata); |
| 616 |
const char *name; |
| 617 |
Seat *seat; |
| 618 |
int r; |
| 619 |
|
| 620 |
assert(message); |
| 621 |
|
| 622 |
r = sd_bus_message_read(message, "s", &name); |
| 623 |
if (r < 0) |
| 624 |
return r; |
| 625 |
|
| 626 |
r = manager_get_seat_from_creds(m, message, name, error, &seat); |
| 627 |
if (r < 0) |
| 628 |
return r; |
| 629 |
|
| 630 |
p = seat_bus_path(seat); |
| 631 |
if (!p) |
| 632 |
return -ENOMEM; |
| 633 |
|
| 634 |
return sd_bus_reply_method_return(message, "o", p); |
| 635 |
} |
| 636 |
|
| 637 |
static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 638 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 639 |
Manager *m = ASSERT_PTR(userdata); |
| 640 |
Session *session; |
| 641 |
int r; |
| 642 |
|
| 643 |
assert(message); |
| 644 |
|
| 645 |
r = sd_bus_message_new_method_return(message, &reply); |
| 646 |
if (r < 0) |
| 647 |
return r; |
| 648 |
|
| 649 |
r = sd_bus_message_open_container(reply, 'a', "(susso)"); |
| 650 |
if (r < 0) |
| 651 |
return r; |
| 652 |
|
| 653 |
HASHMAP_FOREACH(session, m->sessions) { |
| 654 |
_cleanup_free_ char *p = NULL; |
| 655 |
|
| 656 |
p = session_bus_path(session); |
| 657 |
if (!p) |
| 658 |
return -ENOMEM; |
| 659 |
|
| 660 |
r = sd_bus_message_append(reply, "(susso)", |
| 661 |
session->id, |
| 662 |
(uint32_t) session->user->user_record->uid, |
| 663 |
session->user->user_record->user_name, |
| 664 |
session->seat ? session->seat->id : "", |
| 665 |
p); |
| 666 |
if (r < 0) |
| 667 |
return r; |
| 668 |
} |
| 669 |
|
| 670 |
r = sd_bus_message_close_container(reply); |
| 671 |
if (r < 0) |
| 672 |
return r; |
| 673 |
|
| 674 |
return sd_bus_message_send(reply); |
| 675 |
} |
| 676 |
|
| 677 |
static int method_list_sessions_ex(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 678 |
Manager *m = ASSERT_PTR(userdata); |
| 679 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 680 |
int r; |
| 681 |
|
| 682 |
assert(message); |
| 683 |
|
| 684 |
r = sd_bus_message_new_method_return(message, &reply); |
| 685 |
if (r < 0) |
| 686 |
return r; |
| 687 |
|
| 688 |
r = sd_bus_message_open_container(reply, 'a', "(sussussbto)"); |
| 689 |
if (r < 0) |
| 690 |
return r; |
| 691 |
|
| 692 |
Session *s; |
| 693 |
HASHMAP_FOREACH(s, m->sessions) { |
| 694 |
_cleanup_free_ char *path = NULL; |
| 695 |
dual_timestamp idle_ts; |
| 696 |
bool idle; |
| 697 |
|
| 698 |
assert(s->user); |
| 699 |
|
| 700 |
path = session_bus_path(s); |
| 701 |
if (!path) |
| 702 |
return -ENOMEM; |
| 703 |
|
| 704 |
idle = session_get_idle_hint(s, &idle_ts); |
| 705 |
|
| 706 |
r = sd_bus_message_append(reply, "(sussussbto)", |
| 707 |
s->id, |
| 708 |
(uint32_t) s->user->user_record->uid, |
| 709 |
s->user->user_record->user_name, |
| 710 |
s->seat ? s->seat->id : "", |
| 711 |
(uint32_t) s->leader.pid, |
| 712 |
session_class_to_string(s->class), |
| 713 |
s->tty, |
| 714 |
idle, |
| 715 |
idle_ts.monotonic, |
| 716 |
path); |
| 717 |
if (r < 0) |
| 718 |
return r; |
| 719 |
} |
| 720 |
|
| 721 |
r = sd_bus_message_close_container(reply); |
| 722 |
if (r < 0) |
| 723 |
return r; |
| 724 |
|
| 725 |
return sd_bus_message_send(reply); |
| 726 |
} |
| 727 |
|
| 728 |
static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 729 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 730 |
Manager *m = ASSERT_PTR(userdata); |
| 731 |
User *user; |
| 732 |
int r; |
| 733 |
|
| 734 |
assert(message); |
| 735 |
|
| 736 |
r = sd_bus_message_new_method_return(message, &reply); |
| 737 |
if (r < 0) |
| 738 |
return r; |
| 739 |
|
| 740 |
r = sd_bus_message_open_container(reply, 'a', "(uso)"); |
| 741 |
if (r < 0) |
| 742 |
return r; |
| 743 |
|
| 744 |
HASHMAP_FOREACH(user, m->users) { |
| 745 |
_cleanup_free_ char *p = NULL; |
| 746 |
|
| 747 |
p = user_bus_path(user); |
| 748 |
if (!p) |
| 749 |
return -ENOMEM; |
| 750 |
|
| 751 |
r = sd_bus_message_append(reply, "(uso)", |
| 752 |
(uint32_t) user->user_record->uid, |
| 753 |
user->user_record->user_name, |
| 754 |
p); |
| 755 |
if (r < 0) |
| 756 |
return r; |
| 757 |
} |
| 758 |
|
| 759 |
r = sd_bus_message_close_container(reply); |
| 760 |
if (r < 0) |
| 761 |
return r; |
| 762 |
|
| 763 |
return sd_bus_message_send(reply); |
| 764 |
} |
| 765 |
|
| 766 |
static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 767 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 768 |
Manager *m = ASSERT_PTR(userdata); |
| 769 |
Seat *seat; |
| 770 |
int r; |
| 771 |
|
| 772 |
assert(message); |
| 773 |
|
| 774 |
r = sd_bus_message_new_method_return(message, &reply); |
| 775 |
if (r < 0) |
| 776 |
return r; |
| 777 |
|
| 778 |
r = sd_bus_message_open_container(reply, 'a', "(so)"); |
| 779 |
if (r < 0) |
| 780 |
return r; |
| 781 |
|
| 782 |
HASHMAP_FOREACH(seat, m->seats) { |
| 783 |
_cleanup_free_ char *p = NULL; |
| 784 |
|
| 785 |
p = seat_bus_path(seat); |
| 786 |
if (!p) |
| 787 |
return -ENOMEM; |
| 788 |
|
| 789 |
r = sd_bus_message_append(reply, "(so)", seat->id, p); |
| 790 |
if (r < 0) |
| 791 |
return r; |
| 792 |
} |
| 793 |
|
| 794 |
r = sd_bus_message_close_container(reply); |
| 795 |
if (r < 0) |
| 796 |
return r; |
| 797 |
|
| 798 |
return sd_bus_message_send(reply); |
| 799 |
} |
| 800 |
|
| 801 |
static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 802 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 803 |
Manager *m = ASSERT_PTR(userdata); |
| 804 |
Inhibitor *inhibitor; |
| 805 |
int r; |
| 806 |
|
| 807 |
assert(message); |
| 808 |
|
| 809 |
r = sd_bus_message_new_method_return(message, &reply); |
| 810 |
if (r < 0) |
| 811 |
return r; |
| 812 |
|
| 813 |
r = sd_bus_message_open_container(reply, 'a', "(ssssuu)"); |
| 814 |
if (r < 0) |
| 815 |
return r; |
| 816 |
|
| 817 |
HASHMAP_FOREACH(inhibitor, m->inhibitors) { |
| 818 |
|
| 819 |
r = sd_bus_message_append(reply, "(ssssuu)", |
| 820 |
strempty(inhibit_what_to_string(inhibitor->what)), |
| 821 |
strempty(inhibitor->who), |
| 822 |
strempty(inhibitor->why), |
| 823 |
strempty(inhibit_mode_to_string(inhibitor->mode)), |
| 824 |
(uint32_t) inhibitor->uid, |
| 825 |
(uint32_t) inhibitor->pid.pid); |
| 826 |
if (r < 0) |
| 827 |
return r; |
| 828 |
} |
| 829 |
|
| 830 |
r = sd_bus_message_close_container(reply); |
| 831 |
if (r < 0) |
| 832 |
return r; |
| 833 |
|
| 834 |
return sd_bus_message_send(reply); |
| 835 |
} |
| 836 |
|
| 837 |
static int manager_choose_session_id( |
| 838 |
Manager *m, |
| 839 |
const PidRef *leader, |
| 840 |
char **ret_id) { |
| 841 |
|
| 842 |
int r; |
| 843 |
|
| 844 |
assert(m); |
| 845 |
assert(pidref_is_set(leader)); |
| 846 |
assert(ret_id); |
| 847 |
|
| 848 |
/* Try to keep our session IDs and the audit session IDs in sync */ |
| 849 |
_cleanup_free_ char *id = NULL; |
| 850 |
uint32_t audit_id = AUDIT_SESSION_INVALID; |
| 851 |
r = audit_session_from_pid(leader, &audit_id); |
| 852 |
if (r < 0) { |
| 853 |
if (r != -ENODATA) |
| 854 |
log_debug_errno(r, "Failed to read audit session ID of process " PID_FMT ", ignoring: %m", leader->pid); |
| 855 |
} else { |
| 856 |
if (asprintf(&id, "%"PRIu32, audit_id) < 0) |
| 857 |
return -ENOMEM; |
| 858 |
|
| 859 |
/* Wut? There's already a session by this name and we didn't find it above? Weird, then let's |
| 860 |
* not trust the audit data and let's better register a new ID */ |
| 861 |
if (hashmap_contains(m->sessions, id)) { |
| 862 |
log_warning("Existing logind session ID %s used by new audit session, ignoring.", id); |
| 863 |
id = mfree(id); |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
if (!id) |
| 868 |
do { |
| 869 |
id = mfree(id); |
| 870 |
|
| 871 |
if (asprintf(&id, "c%" PRIu64, ++m->session_counter) < 0) |
| 872 |
return -ENOMEM; |
| 873 |
|
| 874 |
} while (hashmap_contains(m->sessions, id)); |
| 875 |
|
| 876 |
/* The generated names should not clash with 'auto' or 'self' */ |
| 877 |
assert(!session_is_self(id)); |
| 878 |
assert(!session_is_auto(id)); |
| 879 |
|
| 880 |
*ret_id = TAKE_PTR(id); |
| 881 |
return 0; |
| 882 |
} |
| 883 |
|
| 884 |
int manager_create_session( |
| 885 |
Manager *m, |
| 886 |
uid_t uid, |
| 887 |
PidRef *leader, /* consumed */ |
| 888 |
const char *service, |
| 889 |
SessionType type, |
| 890 |
SessionClass class, |
| 891 |
const char *desktop, |
| 892 |
Seat *seat, |
| 893 |
unsigned vtnr, |
| 894 |
const char *tty, |
| 895 |
const char *display, |
| 896 |
bool remote, |
| 897 |
const char *remote_user, |
| 898 |
const char *remote_host, |
| 899 |
char * const *extra_device_access, |
| 900 |
Session **ret_session) { |
| 901 |
|
| 902 |
bool mangle_class = false; |
| 903 |
int r; |
| 904 |
|
| 905 |
assert(m); |
| 906 |
assert(uid_is_valid(uid)); |
| 907 |
assert(pidref_is_set(leader)); |
| 908 |
assert(ret_session); |
| 909 |
|
| 910 |
/* Returns: |
| 911 |
* -EBUSY → client is already in a session |
| 912 |
* -EADDRNOTAVAIL → VT is already taken |
| 913 |
* -EUSERS → limit of sessions reached |
| 914 |
*/ |
| 915 |
|
| 916 |
if (type == _SESSION_TYPE_INVALID) { |
| 917 |
if (!isempty(display)) |
| 918 |
type = SESSION_X11; |
| 919 |
else if (!isempty(tty)) |
| 920 |
type = SESSION_TTY; |
| 921 |
else |
| 922 |
type = SESSION_UNSPECIFIED; |
| 923 |
} |
| 924 |
|
| 925 |
if (class == _SESSION_CLASS_INVALID) { |
| 926 |
if (type == SESSION_UNSPECIFIED) |
| 927 |
class = SESSION_BACKGROUND; |
| 928 |
else |
| 929 |
class = SESSION_USER; |
| 930 |
|
| 931 |
/* If we determined the class automatically, then let's later potentially change it to early |
| 932 |
* or light flavours, once we learn the disposition of the user */ |
| 933 |
mangle_class = true; |
| 934 |
} |
| 935 |
|
| 936 |
/* Check if we are already in a logind session, and if so refuse. */ |
| 937 |
r = manager_get_session_by_pidref(m, leader, /* ret= */ NULL); |
| 938 |
if (r < 0) |
| 939 |
return log_debug_errno( |
| 940 |
r, |
| 941 |
"Failed to check if process " PID_FMT " is already in a session: %m", |
| 942 |
leader->pid); |
| 943 |
if (r > 0) |
| 944 |
return log_debug_errno(SYNTHETIC_ERRNO(EBUSY), "Client is already in a session."); |
| 945 |
|
| 946 |
/* Old gdm and lightdm start the user-session on the same VT as the greeter session. But they destroy |
| 947 |
* the greeter session after the user-session and want the user-session to take over the VT. We need |
| 948 |
* to support this for backwards-compatibility, so make sure we allow new sessions on a VT that a |
| 949 |
* greeter is running on. Furthermore, to allow re-logins, we have to allow a greeter to take over a |
| 950 |
* used VT for the exact same reasons. */ |
| 951 |
if (class != SESSION_GREETER && |
| 952 |
vtnr > 0 && |
| 953 |
vtnr < MALLOC_ELEMENTSOF(m->seat0->positions) && |
| 954 |
m->seat0->positions[vtnr] && |
| 955 |
m->seat0->positions[vtnr]->class != SESSION_GREETER) |
| 956 |
return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL), "VT already occupied by a session."); |
| 957 |
|
| 958 |
if (hashmap_size(m->sessions) >= m->sessions_max) |
| 959 |
return log_debug_errno(SYNTHETIC_ERRNO(EUSERS), "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", m->sessions_max); |
| 960 |
|
| 961 |
_cleanup_free_ char *id = NULL; |
| 962 |
r = manager_choose_session_id(m, leader, &id); |
| 963 |
if (r < 0) |
| 964 |
return r; |
| 965 |
|
| 966 |
/* If we are not watching utmp already, try again */ |
| 967 |
manager_reconnect_utmp(m); |
| 968 |
|
| 969 |
User *user = NULL; |
| 970 |
Session *session = NULL; |
| 971 |
|
| 972 |
r = manager_add_user_by_uid(m, uid, &user); |
| 973 |
if (r < 0) |
| 974 |
goto fail; |
| 975 |
|
| 976 |
/* If we picked the session class on our own, and the user is not a regular one, and the session is |
| 977 |
* not a graphical one then do not pull in session manager by default. For root make a special |
| 978 |
* exception: for TTY logins leave the service manager on, but relax the /run/nologin |
| 979 |
* restrictions. */ |
| 980 |
if (mangle_class && |
| 981 |
IN_SET(user_record_disposition(user->user_record), USER_INTRINSIC, USER_SYSTEM, USER_DYNAMIC)) { |
| 982 |
|
| 983 |
if (class == SESSION_USER) { |
| 984 |
if (user_record_is_root(user->user_record)) |
| 985 |
class = SESSION_USER_EARLY; |
| 986 |
else if (SESSION_TYPE_IS_GRAPHICAL(type)) |
| 987 |
class = SESSION_USER; |
| 988 |
else |
| 989 |
class = SESSION_USER_LIGHT; |
| 990 |
|
| 991 |
} else if (class == SESSION_BACKGROUND) |
| 992 |
class = SESSION_BACKGROUND_LIGHT; |
| 993 |
} |
| 994 |
|
| 995 |
r = manager_add_session(m, id, &session); |
| 996 |
if (r < 0) |
| 997 |
goto fail; |
| 998 |
|
| 999 |
session_set_user(session, user); |
| 1000 |
r = session_set_leader_consume(session, TAKE_PIDREF(*leader)); |
| 1001 |
if (r < 0) |
| 1002 |
goto fail; |
| 1003 |
|
| 1004 |
session->original_type = session->type = type; |
| 1005 |
session->remote = remote; |
| 1006 |
session->vtnr = vtnr; |
| 1007 |
session->class = class; |
| 1008 |
|
| 1009 |
/* Once the first session that is of a pinning class shows up we'll change the GC mode for the user |
| 1010 |
* from USER_GC_BY_ANY to USER_GC_BY_PIN, so that the user goes away once the last pinning session |
| 1011 |
* goes away. Background: we want that [email protected] – when started manually – remains around (which |
| 1012 |
* itself is a non-pinning session), but gets stopped when the last pinning session goes away. */ |
| 1013 |
|
| 1014 |
if (SESSION_CLASS_PIN_USER(class)) |
| 1015 |
user->gc_mode = USER_GC_BY_PIN; |
| 1016 |
|
| 1017 |
if (!isempty(tty)) { |
| 1018 |
r = strdup_to(&session->tty, tty); |
| 1019 |
if (r < 0) |
| 1020 |
goto fail; |
| 1021 |
|
| 1022 |
session->tty_validity = TTY_FROM_PAM; |
| 1023 |
} |
| 1024 |
|
| 1025 |
if (!isempty(display)) { |
| 1026 |
r = strdup_to(&session->display, display); |
| 1027 |
if (r < 0) |
| 1028 |
goto fail; |
| 1029 |
} |
| 1030 |
|
| 1031 |
if (!isempty(remote_user)) { |
| 1032 |
r = strdup_to(&session->remote_user, remote_user); |
| 1033 |
if (r < 0) |
| 1034 |
goto fail; |
| 1035 |
} |
| 1036 |
|
| 1037 |
if (!isempty(remote_host)) { |
| 1038 |
r = strdup_to(&session->remote_host, remote_host); |
| 1039 |
if (r < 0) |
| 1040 |
goto fail; |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (!isempty(service)) { |
| 1044 |
r = strdup_to(&session->service, service); |
| 1045 |
if (r < 0) |
| 1046 |
goto fail; |
| 1047 |
} |
| 1048 |
|
| 1049 |
if (!isempty(desktop)) { |
| 1050 |
r = strdup_to(&session->desktop, desktop); |
| 1051 |
if (r < 0) |
| 1052 |
goto fail; |
| 1053 |
} |
| 1054 |
|
| 1055 |
r = strv_copy_unless_empty(extra_device_access, &session->extra_device_access); |
| 1056 |
if (r < 0) |
| 1057 |
goto fail; |
| 1058 |
|
| 1059 |
if (seat) { |
| 1060 |
r = seat_attach_session(seat, session); |
| 1061 |
if (r < 0) |
| 1062 |
goto fail; |
| 1063 |
} |
| 1064 |
|
| 1065 |
*ret_session = session; |
| 1066 |
return 0; |
| 1067 |
|
| 1068 |
fail: |
| 1069 |
if (session) |
| 1070 |
session_add_to_gc_queue(session); |
| 1071 |
|
| 1072 |
if (user) |
| 1073 |
user_add_to_gc_queue(user); |
| 1074 |
|
| 1075 |
return r; |
| 1076 |
} |
| 1077 |
|
| 1078 |
static int manager_create_session_by_bus( |
| 1079 |
Manager *m, |
| 1080 |
sd_bus_message *message, |
| 1081 |
sd_bus_error *error, |
| 1082 |
uid_t uid, |
| 1083 |
pid_t leader_pid, |
| 1084 |
int leader_pidfd, |
| 1085 |
const char *service, |
| 1086 |
const char *type, |
| 1087 |
const char *class, |
| 1088 |
const char *desktop, |
| 1089 |
const char *cseat, |
| 1090 |
uint32_t vtnr, |
| 1091 |
const char *tty, |
| 1092 |
const char *display, |
| 1093 |
int remote, |
| 1094 |
const char *remote_user, |
| 1095 |
const char *remote_host, |
| 1096 |
uint64_t flags) { |
| 1097 |
|
| 1098 |
int r; |
| 1099 |
|
| 1100 |
assert(m); |
| 1101 |
assert(message); |
| 1102 |
|
| 1103 |
if (!uid_is_valid(uid)) |
| 1104 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UID"); |
| 1105 |
|
| 1106 |
if (flags != 0) |
| 1107 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Flags must be zero."); |
| 1108 |
|
| 1109 |
_cleanup_(pidref_done) PidRef leader = PIDREF_NULL; |
| 1110 |
if (leader_pidfd >= 0) |
| 1111 |
r = pidref_set_pidfd(&leader, leader_pidfd); |
| 1112 |
else if (leader_pid == 0) |
| 1113 |
r = bus_query_sender_pidref(message, &leader); |
| 1114 |
else { |
| 1115 |
if (leader_pid < 0) |
| 1116 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Leader PID is not valid"); |
| 1117 |
|
| 1118 |
r = pidref_set_pid(&leader, leader_pid); |
| 1119 |
} |
| 1120 |
if (r < 0) |
| 1121 |
return r; |
| 1122 |
|
| 1123 |
if (leader.pid == 1 || pidref_is_self(&leader)) |
| 1124 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID"); |
| 1125 |
|
| 1126 |
if (leader.fd < 0) |
| 1127 |
return sd_bus_error_set_errnof(error, EUNATCH, "Leader PIDFD not available"); |
| 1128 |
|
| 1129 |
SessionType t; |
| 1130 |
if (isempty(type)) |
| 1131 |
t = _SESSION_TYPE_INVALID; |
| 1132 |
else { |
| 1133 |
t = session_type_from_string(type); |
| 1134 |
if (t < 0) |
| 1135 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1136 |
"Invalid session type %s", type); |
| 1137 |
} |
| 1138 |
|
| 1139 |
SessionClass c; |
| 1140 |
if (isempty(class)) |
| 1141 |
c = _SESSION_CLASS_INVALID; |
| 1142 |
else { |
| 1143 |
c = session_class_from_string(class); |
| 1144 |
if (c < 0) |
| 1145 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1146 |
"Invalid session class %s", class); |
| 1147 |
if (c == SESSION_NONE) |
| 1148 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1149 |
"Refusing session class %s", class); |
| 1150 |
} |
| 1151 |
|
| 1152 |
if (isempty(desktop)) |
| 1153 |
desktop = NULL; |
| 1154 |
else { |
| 1155 |
if (!string_is_safe(desktop, STRING_ALLOW_GLOBS)) |
| 1156 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1157 |
"Invalid desktop string %s", desktop); |
| 1158 |
} |
| 1159 |
|
| 1160 |
Seat *seat = NULL; |
| 1161 |
if (isempty(cseat)) |
| 1162 |
seat = NULL; |
| 1163 |
else { |
| 1164 |
seat = hashmap_get(m->seats, cseat); |
| 1165 |
if (!seat) |
| 1166 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, |
| 1167 |
"No seat '%s' known", cseat); |
| 1168 |
} |
| 1169 |
|
| 1170 |
if (isempty(tty)) |
| 1171 |
tty = NULL; |
| 1172 |
else if (tty_is_vc(tty)) { |
| 1173 |
int v; |
| 1174 |
|
| 1175 |
if (!seat) |
| 1176 |
seat = m->seat0; |
| 1177 |
else if (seat != m->seat0) |
| 1178 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1179 |
"TTY %s is virtual console but seat %s is not seat0", tty, seat->id); |
| 1180 |
|
| 1181 |
v = vtnr_from_tty(tty); |
| 1182 |
if (v <= 0) |
| 1183 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1184 |
"Cannot determine VT number from virtual console TTY %s", tty); |
| 1185 |
|
| 1186 |
if (vtnr == 0) |
| 1187 |
vtnr = (uint32_t) v; |
| 1188 |
else if (vtnr != (uint32_t) v) |
| 1189 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1190 |
"Specified TTY and VT number do not match"); |
| 1191 |
|
| 1192 |
} else if (tty_is_console(tty)) { |
| 1193 |
|
| 1194 |
if (!seat) |
| 1195 |
seat = m->seat0; |
| 1196 |
else if (seat != m->seat0) |
| 1197 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1198 |
"Console TTY specified but seat is not seat0"); |
| 1199 |
|
| 1200 |
if (vtnr != 0) |
| 1201 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1202 |
"Console TTY specified but VT number is not 0"); |
| 1203 |
} |
| 1204 |
|
| 1205 |
if (seat) { |
| 1206 |
if (seat_has_vts(seat)) { |
| 1207 |
if (!vtnr_is_valid(vtnr)) |
| 1208 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1209 |
"VT number out of range"); |
| 1210 |
} else { |
| 1211 |
if (vtnr != 0) |
| 1212 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, |
| 1213 |
"Seat has no VTs but VT number not 0"); |
| 1214 |
} |
| 1215 |
} |
| 1216 |
|
| 1217 |
Session *session; |
| 1218 |
r = manager_create_session( |
| 1219 |
m, |
| 1220 |
uid, |
| 1221 |
&leader, |
| 1222 |
service, |
| 1223 |
t, |
| 1224 |
c, |
| 1225 |
desktop, |
| 1226 |
seat, |
| 1227 |
vtnr, |
| 1228 |
tty, |
| 1229 |
display, |
| 1230 |
remote, |
| 1231 |
remote_user, |
| 1232 |
remote_host, |
| 1233 |
/* extra_device_access= */ NULL, |
| 1234 |
&session); |
| 1235 |
if (r == -EBUSY) |
| 1236 |
return sd_bus_error_set(error, BUS_ERROR_SESSION_BUSY, "Already running in a session or user slice"); |
| 1237 |
if (r == -EADDRNOTAVAIL) |
| 1238 |
return sd_bus_error_set(error, BUS_ERROR_SESSION_BUSY, "Virtual terminal already occupied by a session"); |
| 1239 |
if (r == -EUSERS) |
| 1240 |
return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", m->sessions_max); |
| 1241 |
if (r < 0) |
| 1242 |
return r; |
| 1243 |
|
| 1244 |
r = sd_bus_message_enter_container(message, 'a', "(sv)"); |
| 1245 |
if (r < 0) |
| 1246 |
goto fail; |
| 1247 |
r = session_start(session, message, error); |
| 1248 |
if (r < 0) |
| 1249 |
goto fail; |
| 1250 |
r = sd_bus_message_exit_container(message); |
| 1251 |
if (r < 0) |
| 1252 |
goto fail; |
| 1253 |
|
| 1254 |
session->create_message = sd_bus_message_ref(message); |
| 1255 |
|
| 1256 |
/* Now call into session_send_create_reply(), which will reply to this method call for us. Or it |
| 1257 |
* won't – in case we just spawned a session scope and/or user service manager, and they aren't ready |
| 1258 |
* yet. We'll call session_create_reply() again once the session scope or the user service manager is |
| 1259 |
* ready, where the function will check again if a reply is then ready to be sent, and then do so if |
| 1260 |
* all is complete - or wait again. */ |
| 1261 |
r = session_send_create_reply(session, /* error= */ NULL); |
| 1262 |
if (r < 0) |
| 1263 |
goto fail; |
| 1264 |
|
| 1265 |
return 1; |
| 1266 |
|
| 1267 |
fail: |
| 1268 |
if (session) |
| 1269 |
session_add_to_gc_queue(session); |
| 1270 |
|
| 1271 |
return r; |
| 1272 |
} |
| 1273 |
|
| 1274 |
static int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1275 |
const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop; |
| 1276 |
pid_t leader_pid; |
| 1277 |
uint32_t vtnr; |
| 1278 |
uid_t uid; |
| 1279 |
int remote, r; |
| 1280 |
|
| 1281 |
assert(message); |
| 1282 |
|
| 1283 |
assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
| 1284 |
assert_cc(sizeof(uid_t) == sizeof(uint32_t)); |
| 1285 |
|
| 1286 |
r = sd_bus_message_read( |
| 1287 |
message, |
| 1288 |
"uusssssussbss", |
| 1289 |
&uid, |
| 1290 |
&leader_pid, |
| 1291 |
&service, |
| 1292 |
&type, |
| 1293 |
&class, |
| 1294 |
&desktop, |
| 1295 |
&cseat, |
| 1296 |
&vtnr, |
| 1297 |
&tty, |
| 1298 |
&display, |
| 1299 |
&remote, |
| 1300 |
&remote_user, |
| 1301 |
&remote_host); |
| 1302 |
if (r < 0) |
| 1303 |
return r; |
| 1304 |
|
| 1305 |
return manager_create_session_by_bus( |
| 1306 |
userdata, |
| 1307 |
message, |
| 1308 |
error, |
| 1309 |
uid, |
| 1310 |
leader_pid, |
| 1311 |
/* leader_pidfd= */ -EBADF, |
| 1312 |
service, |
| 1313 |
type, |
| 1314 |
class, |
| 1315 |
desktop, |
| 1316 |
cseat, |
| 1317 |
vtnr, |
| 1318 |
tty, |
| 1319 |
display, |
| 1320 |
remote, |
| 1321 |
remote_user, |
| 1322 |
remote_host, |
| 1323 |
/* flags= */ 0); |
| 1324 |
} |
| 1325 |
|
| 1326 |
static int method_create_session_pidfd(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1327 |
const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop; |
| 1328 |
uint64_t flags; |
| 1329 |
uint32_t vtnr; |
| 1330 |
uid_t uid; |
| 1331 |
int leader_fd = -EBADF, remote, r; |
| 1332 |
|
| 1333 |
r = sd_bus_message_read( |
| 1334 |
message, |
| 1335 |
"uhsssssussbsst", |
| 1336 |
&uid, |
| 1337 |
&leader_fd, |
| 1338 |
&service, |
| 1339 |
&type, |
| 1340 |
&class, |
| 1341 |
&desktop, |
| 1342 |
&cseat, |
| 1343 |
&vtnr, |
| 1344 |
&tty, |
| 1345 |
&display, |
| 1346 |
&remote, |
| 1347 |
&remote_user, |
| 1348 |
&remote_host, |
| 1349 |
&flags); |
| 1350 |
if (r < 0) |
| 1351 |
return r; |
| 1352 |
|
| 1353 |
return manager_create_session_by_bus( |
| 1354 |
userdata, |
| 1355 |
message, |
| 1356 |
error, |
| 1357 |
uid, |
| 1358 |
/* leader_pid= */ 0, |
| 1359 |
leader_fd, |
| 1360 |
service, |
| 1361 |
type, |
| 1362 |
class, |
| 1363 |
desktop, |
| 1364 |
cseat, |
| 1365 |
vtnr, |
| 1366 |
tty, |
| 1367 |
display, |
| 1368 |
remote, |
| 1369 |
remote_user, |
| 1370 |
remote_host, |
| 1371 |
flags); |
| 1372 |
} |
| 1373 |
|
| 1374 |
static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1375 |
Manager *m = ASSERT_PTR(userdata); |
| 1376 |
Session *session, *sender_session; |
| 1377 |
const char *name; |
| 1378 |
int r; |
| 1379 |
|
| 1380 |
assert(message); |
| 1381 |
|
| 1382 |
r = sd_bus_message_read(message, "s", &name); |
| 1383 |
if (r < 0) |
| 1384 |
return r; |
| 1385 |
|
| 1386 |
r = manager_get_session_from_creds(m, message, name, error, &session); |
| 1387 |
if (r < 0) |
| 1388 |
return r; |
| 1389 |
|
| 1390 |
r = get_sender_session(m, message, /* consult_display= */ false, error, &sender_session); |
| 1391 |
if (r < 0) |
| 1392 |
return r; |
| 1393 |
|
| 1394 |
if (session != sender_session) |
| 1395 |
return sd_bus_error_set(error, SD_BUS_ERROR_ACCESS_DENIED, |
| 1396 |
"Refused to release session, since it doesn't match the one of the client"); |
| 1397 |
|
| 1398 |
r = session_release(session); |
| 1399 |
if (r < 0) |
| 1400 |
return r; |
| 1401 |
|
| 1402 |
return sd_bus_reply_method_return(message, NULL); |
| 1403 |
} |
| 1404 |
|
| 1405 |
static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1406 |
Manager *m = ASSERT_PTR(userdata); |
| 1407 |
Session *session; |
| 1408 |
const char *name; |
| 1409 |
int r; |
| 1410 |
|
| 1411 |
assert(message); |
| 1412 |
|
| 1413 |
r = sd_bus_message_read(message, "s", &name); |
| 1414 |
if (r < 0) |
| 1415 |
return r; |
| 1416 |
|
| 1417 |
r = manager_get_session_from_creds(m, message, name, error, &session); |
| 1418 |
if (r < 0) |
| 1419 |
return r; |
| 1420 |
|
| 1421 |
/* PolicyKit is done by bus_session_method_activate() */ |
| 1422 |
|
| 1423 |
return bus_session_method_activate(message, session, error); |
| 1424 |
} |
| 1425 |
|
| 1426 |
static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1427 |
const char *session_name, *seat_name; |
| 1428 |
Manager *m = ASSERT_PTR(userdata); |
| 1429 |
Session *session; |
| 1430 |
Seat *seat; |
| 1431 |
int r; |
| 1432 |
|
| 1433 |
assert(message); |
| 1434 |
|
| 1435 |
/* Same as ActivateSession() but refuses to work if the seat doesn't match */ |
| 1436 |
|
| 1437 |
r = sd_bus_message_read(message, "ss", &session_name, &seat_name); |
| 1438 |
if (r < 0) |
| 1439 |
return r; |
| 1440 |
|
| 1441 |
r = manager_get_session_from_creds(m, message, session_name, error, &session); |
| 1442 |
if (r < 0) |
| 1443 |
return r; |
| 1444 |
|
| 1445 |
r = manager_get_seat_from_creds(m, message, seat_name, error, &seat); |
| 1446 |
if (r < 0) |
| 1447 |
return r; |
| 1448 |
|
| 1449 |
if (session->seat != seat) |
| 1450 |
return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, |
| 1451 |
"Session %s not on seat %s", session_name, seat_name); |
| 1452 |
|
| 1453 |
r = check_polkit_chvt(message, m, error); |
| 1454 |
if (r < 0) |
| 1455 |
return r; |
| 1456 |
if (r == 0) |
| 1457 |
return 1; /* Will call us back */ |
| 1458 |
|
| 1459 |
r = session_activate(session); |
| 1460 |
if (r < 0) |
| 1461 |
return r; |
| 1462 |
|
| 1463 |
return sd_bus_reply_method_return(message, NULL); |
| 1464 |
} |
| 1465 |
|
| 1466 |
static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1467 |
Manager *m = ASSERT_PTR(userdata); |
| 1468 |
Session *session; |
| 1469 |
const char *name; |
| 1470 |
int r; |
| 1471 |
|
| 1472 |
assert(message); |
| 1473 |
|
| 1474 |
r = sd_bus_message_read(message, "s", &name); |
| 1475 |
if (r < 0) |
| 1476 |
return r; |
| 1477 |
|
| 1478 |
r = manager_get_session_from_creds(m, message, name, error, &session); |
| 1479 |
if (r < 0) |
| 1480 |
return r; |
| 1481 |
|
| 1482 |
return bus_session_method_lock(message, session, error); |
| 1483 |
} |
| 1484 |
|
| 1485 |
static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1486 |
Manager *m = ASSERT_PTR(userdata); |
| 1487 |
int r; |
| 1488 |
|
| 1489 |
assert(message); |
| 1490 |
|
| 1491 |
r = bus_verify_polkit_async( |
| 1492 |
message, |
| 1493 |
"org.freedesktop.login1.lock-sessions", |
| 1494 |
/* details= */ NULL, |
| 1495 |
&m->polkit_registry, |
| 1496 |
error); |
| 1497 |
if (r < 0) |
| 1498 |
return r; |
| 1499 |
if (r == 0) |
| 1500 |
return 1; /* Will call us back */ |
| 1501 |
|
| 1502 |
r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions")); |
| 1503 |
if (r < 0) |
| 1504 |
return r; |
| 1505 |
|
| 1506 |
return sd_bus_reply_method_return(message, NULL); |
| 1507 |
} |
| 1508 |
|
| 1509 |
static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1510 |
const char *name; |
| 1511 |
Manager *m = ASSERT_PTR(userdata); |
| 1512 |
Session *session; |
| 1513 |
int r; |
| 1514 |
|
| 1515 |
assert(message); |
| 1516 |
|
| 1517 |
r = sd_bus_message_read(message, "s", &name); |
| 1518 |
if (r < 0) |
| 1519 |
return r; |
| 1520 |
|
| 1521 |
r = manager_get_session_from_creds(m, message, name, error, &session); |
| 1522 |
if (r < 0) |
| 1523 |
return r; |
| 1524 |
|
| 1525 |
return bus_session_method_kill(message, session, error); |
| 1526 |
} |
| 1527 |
|
| 1528 |
static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1529 |
Manager *m = ASSERT_PTR(userdata); |
| 1530 |
uint32_t uid; |
| 1531 |
User *user; |
| 1532 |
int r; |
| 1533 |
|
| 1534 |
assert(message); |
| 1535 |
|
| 1536 |
r = sd_bus_message_read(message, "u", &uid); |
| 1537 |
if (r < 0) |
| 1538 |
return r; |
| 1539 |
|
| 1540 |
r = manager_get_user_from_creds(m, message, uid, error, &user); |
| 1541 |
if (r < 0) |
| 1542 |
return r; |
| 1543 |
|
| 1544 |
return bus_user_method_kill(message, user, error); |
| 1545 |
} |
| 1546 |
|
| 1547 |
static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1548 |
Manager *m = ASSERT_PTR(userdata); |
| 1549 |
const char *name; |
| 1550 |
Session *session; |
| 1551 |
int r; |
| 1552 |
|
| 1553 |
assert(message); |
| 1554 |
|
| 1555 |
r = sd_bus_message_read(message, "s", &name); |
| 1556 |
if (r < 0) |
| 1557 |
return r; |
| 1558 |
|
| 1559 |
r = manager_get_session_from_creds(m, message, name, error, &session); |
| 1560 |
if (r < 0) |
| 1561 |
return r; |
| 1562 |
|
| 1563 |
return bus_session_method_terminate(message, session, error); |
| 1564 |
} |
| 1565 |
|
| 1566 |
static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1567 |
Manager *m = ASSERT_PTR(userdata); |
| 1568 |
uint32_t uid; |
| 1569 |
User *user; |
| 1570 |
int r; |
| 1571 |
|
| 1572 |
assert(message); |
| 1573 |
|
| 1574 |
r = sd_bus_message_read(message, "u", &uid); |
| 1575 |
if (r < 0) |
| 1576 |
return r; |
| 1577 |
|
| 1578 |
r = manager_get_user_from_creds(m, message, uid, error, &user); |
| 1579 |
if (r < 0) |
| 1580 |
return r; |
| 1581 |
|
| 1582 |
return bus_user_method_terminate(message, user, error); |
| 1583 |
} |
| 1584 |
|
| 1585 |
static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1586 |
Manager *m = ASSERT_PTR(userdata); |
| 1587 |
const char *name; |
| 1588 |
Seat *seat; |
| 1589 |
int r; |
| 1590 |
|
| 1591 |
assert(message); |
| 1592 |
|
| 1593 |
r = sd_bus_message_read(message, "s", &name); |
| 1594 |
if (r < 0) |
| 1595 |
return r; |
| 1596 |
|
| 1597 |
r = manager_get_seat_from_creds(m, message, name, error, &seat); |
| 1598 |
if (r < 0) |
| 1599 |
return r; |
| 1600 |
|
| 1601 |
return bus_seat_method_terminate(message, seat, error); |
| 1602 |
} |
| 1603 |
|
| 1604 |
static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1605 |
Manager *m = ASSERT_PTR(userdata); |
| 1606 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
| 1607 |
uint32_t uid, auth_uid; |
| 1608 |
int r, enable, interactive; |
| 1609 |
|
| 1610 |
assert(message); |
| 1611 |
|
| 1612 |
r = sd_bus_message_read(message, "ubb", &uid, &enable, &interactive); |
| 1613 |
if (r < 0) |
| 1614 |
return r; |
| 1615 |
|
| 1616 |
r = sd_bus_query_sender_creds(message, |
| 1617 |
SD_BUS_CREDS_EUID|SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, |
| 1618 |
&creds); |
| 1619 |
if (r < 0) |
| 1620 |
return r; |
| 1621 |
|
| 1622 |
if (!uid_is_valid(uid)) { |
| 1623 |
/* Note that we get the owner UID of the session or user unit, not the actual client UID here! */ |
| 1624 |
r = sd_bus_creds_get_owner_uid(creds, &uid); |
| 1625 |
if (r < 0) |
| 1626 |
return r; |
| 1627 |
} |
| 1628 |
|
| 1629 |
/* owner_uid is racy, so for authorization we must use euid */ |
| 1630 |
r = sd_bus_creds_get_euid(creds, &auth_uid); |
| 1631 |
if (r < 0) |
| 1632 |
return r; |
| 1633 |
|
| 1634 |
_cleanup_free_ struct passwd *pw = NULL; |
| 1635 |
|
| 1636 |
r = getpwuid_malloc(uid, &pw); |
| 1637 |
if (r < 0) |
| 1638 |
return r; |
| 1639 |
|
| 1640 |
r = bus_verify_polkit_async_full( |
| 1641 |
message, |
| 1642 |
uid == auth_uid ? "org.freedesktop.login1.set-self-linger" : |
| 1643 |
"org.freedesktop.login1.set-user-linger", |
| 1644 |
/* details= */ NULL, |
| 1645 |
/* good_user= */ UID_INVALID, |
| 1646 |
interactive ? POLKIT_ALLOW_INTERACTIVE : 0, |
| 1647 |
&m->polkit_registry, |
| 1648 |
error); |
| 1649 |
if (r < 0) |
| 1650 |
return r; |
| 1651 |
if (r == 0) |
| 1652 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 1653 |
|
| 1654 |
(void) mkdir_p_label("/var/lib/systemd", 0755); |
| 1655 |
r = mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE); |
| 1656 |
if (r < 0) |
| 1657 |
return r; |
| 1658 |
|
| 1659 |
_cleanup_free_ char *escaped = NULL; |
| 1660 |
const char *path; |
| 1661 |
User *u; |
| 1662 |
|
| 1663 |
escaped = cescape(pw->pw_name); |
| 1664 |
if (!escaped) |
| 1665 |
return -ENOMEM; |
| 1666 |
|
| 1667 |
path = strjoina("/var/lib/systemd/linger/", escaped); |
| 1668 |
|
| 1669 |
if (enable) { |
| 1670 |
r = touch(path); |
| 1671 |
if (r < 0) |
| 1672 |
return r; |
| 1673 |
|
| 1674 |
if (manager_add_user_by_uid(m, uid, &u) >= 0) { |
| 1675 |
(void) user_send_changed(u, "Linger"); |
| 1676 |
r = user_start(u); |
| 1677 |
if (r < 0) { |
| 1678 |
user_add_to_gc_queue(u); |
| 1679 |
return r; |
| 1680 |
} |
| 1681 |
} |
| 1682 |
|
| 1683 |
} else { |
| 1684 |
r = unlink(path); |
| 1685 |
if (r < 0 && errno != ENOENT) |
| 1686 |
return -errno; |
| 1687 |
|
| 1688 |
u = hashmap_get(m->users, UID_TO_PTR(uid)); |
| 1689 |
if (u) { |
| 1690 |
/* Make sure that disabling lingering will terminate the user tracking if no sessions pin it. */ |
| 1691 |
u->gc_mode = USER_GC_BY_PIN; |
| 1692 |
user_add_to_gc_queue(u); |
| 1693 |
(void) user_send_changed(u, "Linger"); |
| 1694 |
} |
| 1695 |
} |
| 1696 |
|
| 1697 |
return sd_bus_reply_method_return(message, NULL); |
| 1698 |
} |
| 1699 |
|
| 1700 |
static int trigger_device(Manager *m, sd_device *parent) { |
| 1701 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 1702 |
int r; |
| 1703 |
|
| 1704 |
assert(m); |
| 1705 |
|
| 1706 |
r = sd_device_enumerator_new(&e); |
| 1707 |
if (r < 0) |
| 1708 |
return r; |
| 1709 |
|
| 1710 |
r = sd_device_enumerator_allow_uninitialized(e); |
| 1711 |
if (r < 0) |
| 1712 |
return r; |
| 1713 |
|
| 1714 |
if (parent) { |
| 1715 |
r = sd_device_enumerator_add_match_parent(e, parent); |
| 1716 |
if (r < 0) |
| 1717 |
return r; |
| 1718 |
} |
| 1719 |
|
| 1720 |
FOREACH_DEVICE(e, d) { |
| 1721 |
r = sd_device_trigger(d, SD_DEVICE_CHANGE); |
| 1722 |
if (r < 0) |
| 1723 |
log_device_debug_errno(d, r, "Failed to trigger device, ignoring: %m"); |
| 1724 |
} |
| 1725 |
|
| 1726 |
return 0; |
| 1727 |
} |
| 1728 |
|
| 1729 |
static int attach_device(Manager *m, const char *seat, const char *sysfs, sd_bus_error *error) { |
| 1730 |
_cleanup_(sd_device_unrefp) sd_device *d = NULL; |
| 1731 |
_cleanup_free_ char *file = NULL; |
| 1732 |
const char *id_for_seat; |
| 1733 |
int r; |
| 1734 |
|
| 1735 |
assert(m); |
| 1736 |
assert(seat); |
| 1737 |
assert(sysfs); |
| 1738 |
|
| 1739 |
r = sd_device_new_from_syspath(&d, sysfs); |
| 1740 |
if (r < 0) |
| 1741 |
return sd_bus_error_set_errnof(error, r, "Failed to open device '%s': %m", sysfs); |
| 1742 |
|
| 1743 |
if (sd_device_has_current_tag(d, "seat") <= 0) |
| 1744 |
return sd_bus_error_set_errnof(error, ENODEV, "Device '%s' lacks 'seat' udev tag.", sysfs); |
| 1745 |
|
| 1746 |
if (sd_device_get_property_value(d, "ID_FOR_SEAT", &id_for_seat) < 0) |
| 1747 |
return sd_bus_error_set_errnof(error, ENODEV, "Device '%s' lacks 'ID_FOR_SEAT' udev property.", sysfs); |
| 1748 |
|
| 1749 |
if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) |
| 1750 |
return -ENOMEM; |
| 1751 |
|
| 1752 |
r = write_string_filef( |
| 1753 |
file, |
| 1754 |
WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MKDIR_0755|WRITE_STRING_FILE_LABEL, |
| 1755 |
"TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat); |
| 1756 |
if (r < 0) |
| 1757 |
return r; |
| 1758 |
|
| 1759 |
return trigger_device(m, d); |
| 1760 |
} |
| 1761 |
|
| 1762 |
static int flush_devices(Manager *m) { |
| 1763 |
_cleanup_closedir_ DIR *d = NULL; |
| 1764 |
|
| 1765 |
assert(m); |
| 1766 |
|
| 1767 |
d = opendir("/etc/udev/rules.d"); |
| 1768 |
if (!d) { |
| 1769 |
if (errno != ENOENT) |
| 1770 |
log_warning_errno(errno, "Failed to open %s: %m", "/etc/udev/rules.d"); |
| 1771 |
} else |
| 1772 |
FOREACH_DIRENT_ALL(de, d, break) { |
| 1773 |
if (!dirent_is_file(de)) |
| 1774 |
continue; |
| 1775 |
|
| 1776 |
if (!startswith(de->d_name, "72-seat-")) |
| 1777 |
continue; |
| 1778 |
|
| 1779 |
if (!endswith(de->d_name, ".rules")) |
| 1780 |
continue; |
| 1781 |
|
| 1782 |
if (unlinkat(dirfd(d), de->d_name, 0) < 0) |
| 1783 |
log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name); |
| 1784 |
} |
| 1785 |
|
| 1786 |
return trigger_device(m, NULL); |
| 1787 |
} |
| 1788 |
|
| 1789 |
static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1790 |
const char *sysfs, *seat; |
| 1791 |
Manager *m = ASSERT_PTR(userdata); |
| 1792 |
int interactive, r; |
| 1793 |
|
| 1794 |
assert(message); |
| 1795 |
|
| 1796 |
r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive); |
| 1797 |
if (r < 0) |
| 1798 |
return r; |
| 1799 |
|
| 1800 |
if (!path_is_normalized(sysfs)) |
| 1801 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not normalized", sysfs); |
| 1802 |
if (!path_startswith(sysfs, "/sys")) |
| 1803 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs); |
| 1804 |
|
| 1805 |
if (seat_is_self(seat) || seat_is_auto(seat)) { |
| 1806 |
Seat *found; |
| 1807 |
|
| 1808 |
r = manager_get_seat_from_creds(m, message, seat, error, &found); |
| 1809 |
if (r < 0) |
| 1810 |
return r; |
| 1811 |
|
| 1812 |
seat = found->id; |
| 1813 |
|
| 1814 |
} else if (!seat_name_is_valid(seat)) /* Note that a seat does not have to exist yet for this operation to succeed */ |
| 1815 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat name %s is not valid", seat); |
| 1816 |
|
| 1817 |
r = bus_verify_polkit_async_full( |
| 1818 |
message, |
| 1819 |
"org.freedesktop.login1.attach-device", |
| 1820 |
/* details= */ NULL, |
| 1821 |
/* good_user= */ UID_INVALID, |
| 1822 |
interactive ? POLKIT_ALLOW_INTERACTIVE : 0, |
| 1823 |
&m->polkit_registry, |
| 1824 |
error); |
| 1825 |
if (r < 0) |
| 1826 |
return r; |
| 1827 |
if (r == 0) |
| 1828 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 1829 |
|
| 1830 |
r = attach_device(m, seat, sysfs, error); |
| 1831 |
if (r < 0) |
| 1832 |
return r; |
| 1833 |
|
| 1834 |
return sd_bus_reply_method_return(message, NULL); |
| 1835 |
} |
| 1836 |
|
| 1837 |
static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1838 |
Manager *m = ASSERT_PTR(userdata); |
| 1839 |
int interactive, r; |
| 1840 |
|
| 1841 |
assert(message); |
| 1842 |
|
| 1843 |
r = sd_bus_message_read(message, "b", &interactive); |
| 1844 |
if (r < 0) |
| 1845 |
return r; |
| 1846 |
|
| 1847 |
r = bus_verify_polkit_async_full( |
| 1848 |
message, |
| 1849 |
"org.freedesktop.login1.flush-devices", |
| 1850 |
/* details= */ NULL, |
| 1851 |
/* good_user= */ UID_INVALID, |
| 1852 |
interactive ? POLKIT_ALLOW_INTERACTIVE : 0, |
| 1853 |
&m->polkit_registry, |
| 1854 |
error); |
| 1855 |
if (r < 0) |
| 1856 |
return r; |
| 1857 |
if (r == 0) |
| 1858 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 1859 |
|
| 1860 |
r = flush_devices(m); |
| 1861 |
if (r < 0) |
| 1862 |
return r; |
| 1863 |
|
| 1864 |
return sd_bus_reply_method_return(message, NULL); |
| 1865 |
} |
| 1866 |
|
| 1867 |
static int bus_manager_log_shutdown( |
| 1868 |
Manager *m, |
| 1869 |
const HandleActionData *a) { |
| 1870 |
assert(m); |
| 1871 |
assert(a); |
| 1872 |
|
| 1873 |
const char *message = a->message ?: "System is shutting down"; |
| 1874 |
const char *log_verb = a->log_verb ? strjoina("SHUTDOWN=", a->log_verb) : NULL; |
| 1875 |
|
| 1876 |
return log_struct(LOG_NOTICE, |
| 1877 |
LOG_ITEM("MESSAGE_ID=%s", a->message_id ?: SD_MESSAGE_SHUTDOWN_STR), |
| 1878 |
LOG_MESSAGE("%s%s%s%s.", |
| 1879 |
message, |
| 1880 |
m->wall_message ? " (" : "", |
| 1881 |
strempty(m->wall_message), |
| 1882 |
m->wall_message ? ")" : ""), |
| 1883 |
log_verb); |
| 1884 |
} |
| 1885 |
|
| 1886 |
static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) { |
| 1887 |
Manager *m = ASSERT_PTR(userdata); |
| 1888 |
|
| 1889 |
assert(e); |
| 1890 |
|
| 1891 |
m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source); |
| 1892 |
return 0; |
| 1893 |
} |
| 1894 |
|
| 1895 |
int manager_set_lid_switch_ignore(Manager *m, usec_t until) { |
| 1896 |
int r; |
| 1897 |
|
| 1898 |
assert(m); |
| 1899 |
|
| 1900 |
if (until <= now(CLOCK_MONOTONIC)) |
| 1901 |
return 0; |
| 1902 |
|
| 1903 |
/* We want to ignore the lid switch for a while after each |
| 1904 |
* suspend, and after boot-up. Hence let's install a timer for |
| 1905 |
* this. As long as the event source exists we ignore the lid |
| 1906 |
* switch. */ |
| 1907 |
|
| 1908 |
if (m->lid_switch_ignore_event_source) { |
| 1909 |
usec_t u; |
| 1910 |
|
| 1911 |
r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u); |
| 1912 |
if (r < 0) |
| 1913 |
return r; |
| 1914 |
|
| 1915 |
if (until <= u) |
| 1916 |
return 0; |
| 1917 |
|
| 1918 |
r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until); |
| 1919 |
} else |
| 1920 |
r = sd_event_add_time( |
| 1921 |
m->event, |
| 1922 |
&m->lid_switch_ignore_event_source, |
| 1923 |
CLOCK_MONOTONIC, |
| 1924 |
until, 0, |
| 1925 |
lid_switch_ignore_handler, m); |
| 1926 |
|
| 1927 |
return r; |
| 1928 |
} |
| 1929 |
|
| 1930 |
static int send_prepare_for(Manager *m, const HandleActionData *a, bool _active) { |
| 1931 |
int k = 0, r, active = _active; |
| 1932 |
|
| 1933 |
assert(m); |
| 1934 |
assert(a); |
| 1935 |
|
| 1936 |
/* Only sleep/shutdown actions emit a signal */ |
| 1937 |
if (a->inhibit_what < 0) |
| 1938 |
return 0; |
| 1939 |
|
| 1940 |
assert(IN_SET(a->inhibit_what, INHIBIT_SHUTDOWN, INHIBIT_SLEEP)); |
| 1941 |
|
| 1942 |
/* We need to send both old and new signal for backward compatibility. The newer one allows clients |
| 1943 |
* to know which type of reboot is going to happen, as they might be doing different actions (e.g.: |
| 1944 |
* on soft-reboot), and it is sent first, so that clients know that if they receive the old one |
| 1945 |
* first then they don't have to wait for the new one, as it means it's not supported. So, do not |
| 1946 |
* change the order here, as it is an API. */ |
| 1947 |
if (a->inhibit_what == INHIBIT_SHUTDOWN) { |
| 1948 |
k = sd_bus_emit_signal(m->bus, |
| 1949 |
"/org/freedesktop/login1", |
| 1950 |
"org.freedesktop.login1.Manager", |
| 1951 |
"PrepareForShutdownWithMetadata", |
| 1952 |
"ba{sv}", |
| 1953 |
active, |
| 1954 |
1, |
| 1955 |
"type", |
| 1956 |
"s", |
| 1957 |
handle_action_to_string(a->handle)); |
| 1958 |
if (k < 0) |
| 1959 |
log_debug_errno(k, "Failed to emit PrepareForShutdownWithMetadata(): %m"); |
| 1960 |
} |
| 1961 |
|
| 1962 |
r = sd_bus_emit_signal(m->bus, |
| 1963 |
"/org/freedesktop/login1", |
| 1964 |
"org.freedesktop.login1.Manager", |
| 1965 |
a->inhibit_what == INHIBIT_SHUTDOWN ? "PrepareForShutdown" : "PrepareForSleep", |
| 1966 |
"b", |
| 1967 |
active); |
| 1968 |
if (r < 0) |
| 1969 |
log_debug_errno(r, "Failed to emit PrepareForShutdown(): %m"); |
| 1970 |
|
| 1971 |
return RET_GATHER(k, r); |
| 1972 |
} |
| 1973 |
|
| 1974 |
static int strdup_job(sd_bus_message *reply, char **ret) { |
| 1975 |
const char *j; |
| 1976 |
char *job; |
| 1977 |
int r; |
| 1978 |
|
| 1979 |
assert(reply); |
| 1980 |
assert(ret); |
| 1981 |
|
| 1982 |
r = sd_bus_message_read_basic(reply, 'o', &j); |
| 1983 |
if (r < 0) |
| 1984 |
return r; |
| 1985 |
|
| 1986 |
job = strdup(j); |
| 1987 |
if (!job) |
| 1988 |
return -ENOMEM; |
| 1989 |
|
| 1990 |
*ret = job; |
| 1991 |
return 0; |
| 1992 |
} |
| 1993 |
|
| 1994 |
static int execute_shutdown_or_sleep( |
| 1995 |
Manager *m, |
| 1996 |
const HandleActionData *a, |
| 1997 |
sd_bus_error *error) { |
| 1998 |
|
| 1999 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 2000 |
int r; |
| 2001 |
|
| 2002 |
assert(m); |
| 2003 |
assert(!m->action_job); |
| 2004 |
assert(a); |
| 2005 |
|
| 2006 |
if (a->inhibit_what == INHIBIT_SHUTDOWN) |
| 2007 |
bus_manager_log_shutdown(m, a); |
| 2008 |
|
| 2009 |
r = bus_call_method( |
| 2010 |
m->bus, |
| 2011 |
bus_systemd_mgr, |
| 2012 |
"StartUnit", |
| 2013 |
error, |
| 2014 |
&reply, |
| 2015 |
"ss", a->target, "replace-irreversibly"); |
| 2016 |
if (r < 0) |
| 2017 |
goto fail; |
| 2018 |
|
| 2019 |
r = strdup_job(reply, &m->action_job); |
| 2020 |
if (r < 0) |
| 2021 |
goto fail; |
| 2022 |
|
| 2023 |
/* Save the action to prevent another request of shutdown or friends before the current action being |
| 2024 |
* finished. See method_do_shutdown_or_sleep(). This is also used in match_job_removed() to log what |
| 2025 |
* kind of action is finished. */ |
| 2026 |
m->delayed_action = a; |
| 2027 |
|
| 2028 |
/* Make sure the lid switch is ignored for a while. */ |
| 2029 |
manager_set_lid_switch_ignore(m, usec_add(now(CLOCK_MONOTONIC), m->holdoff_timeout_usec)); |
| 2030 |
|
| 2031 |
return 0; |
| 2032 |
|
| 2033 |
fail: |
| 2034 |
/* Tell people that they now may take a lock again. */ |
| 2035 |
(void) send_prepare_for(m, a, false); |
| 2036 |
|
| 2037 |
return r; |
| 2038 |
} |
| 2039 |
|
| 2040 |
int manager_dispatch_delayed(Manager *manager, bool timeout) { |
| 2041 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 2042 |
Inhibitor *offending = NULL; |
| 2043 |
int r; |
| 2044 |
|
| 2045 |
assert(manager); |
| 2046 |
|
| 2047 |
if (!manager->delayed_action || manager->action_job) |
| 2048 |
return 0; |
| 2049 |
|
| 2050 |
if (manager_is_inhibited( |
| 2051 |
manager, |
| 2052 |
manager->delayed_action->inhibit_what, |
| 2053 |
/* since= */ NULL, |
| 2054 |
MANAGER_IS_INHIBITED_CHECK_DELAY, |
| 2055 |
UID_INVALID, |
| 2056 |
&offending)) { |
| 2057 |
if (!timeout) |
| 2058 |
return 0; |
| 2059 |
|
| 2060 |
_cleanup_free_ char *comm = NULL, *u = NULL; |
| 2061 |
(void) pidref_get_comm(&offending->pid, &comm); |
| 2062 |
u = uid_to_name(offending->uid); |
| 2063 |
|
| 2064 |
log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.", |
| 2065 |
offending->uid, strna(u), |
| 2066 |
offending->pid.pid, strna(comm)); |
| 2067 |
} |
| 2068 |
|
| 2069 |
/* Actually do the operation */ |
| 2070 |
r = execute_shutdown_or_sleep(manager, manager->delayed_action, &error); |
| 2071 |
if (r < 0) { |
| 2072 |
log_warning("Error during inhibitor-delayed operation (already returned success to client): %s", |
| 2073 |
bus_error_message(&error, r)); |
| 2074 |
|
| 2075 |
manager->delayed_action = NULL; |
| 2076 |
} |
| 2077 |
|
| 2078 |
return 1; /* We did some work. */ |
| 2079 |
} |
| 2080 |
|
| 2081 |
static int manager_inhibit_timeout_handler( |
| 2082 |
sd_event_source *s, |
| 2083 |
uint64_t usec, |
| 2084 |
void *userdata) { |
| 2085 |
|
| 2086 |
Manager *manager = ASSERT_PTR(userdata); |
| 2087 |
|
| 2088 |
assert(manager->inhibit_timeout_source == s); |
| 2089 |
|
| 2090 |
return manager_dispatch_delayed(manager, true); |
| 2091 |
} |
| 2092 |
|
| 2093 |
static int delay_shutdown_or_sleep( |
| 2094 |
Manager *m, |
| 2095 |
const HandleActionData *a) { |
| 2096 |
|
| 2097 |
int r; |
| 2098 |
|
| 2099 |
assert(m); |
| 2100 |
assert(a); |
| 2101 |
|
| 2102 |
r = event_reset_time_relative( |
| 2103 |
m->event, &m->inhibit_timeout_source, |
| 2104 |
CLOCK_MONOTONIC, m->inhibit_delay_max, /* accuracy= */ 0, |
| 2105 |
manager_inhibit_timeout_handler, m, |
| 2106 |
/* priority= */ 0, "inhibit-timeout", /* force_reset= */ true); |
| 2107 |
if (r < 0) |
| 2108 |
return log_error_errno(r, "Failed to reset timer event source for inhibit timeout: %m"); |
| 2109 |
|
| 2110 |
m->delayed_action = a; |
| 2111 |
|
| 2112 |
return 0; |
| 2113 |
} |
| 2114 |
|
| 2115 |
static void cancel_delayed_action(Manager *m) { |
| 2116 |
assert(m); |
| 2117 |
|
| 2118 |
(void) sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_OFF); |
| 2119 |
|
| 2120 |
/* When m->action_job is NULL, the delayed action has not been triggered yet. Let's clear it to |
| 2121 |
* accept later shutdown and friends. |
| 2122 |
* |
| 2123 |
* When m->action_job is non-NULL, the delayed action has been already triggered, and now we are |
| 2124 |
* waiting for the job being finished. In match_job_removed(), the triggered action will be used. |
| 2125 |
* Hence, do not clear it. */ |
| 2126 |
if (!m->action_job) |
| 2127 |
m->delayed_action = NULL; |
| 2128 |
} |
| 2129 |
|
| 2130 |
int bus_manager_shutdown_or_sleep_now_or_later( |
| 2131 |
Manager *m, |
| 2132 |
const HandleActionData *a, |
| 2133 |
sd_bus_error *error) { |
| 2134 |
|
| 2135 |
_cleanup_free_ char *load_state = NULL; |
| 2136 |
bool delayed; |
| 2137 |
int r; |
| 2138 |
|
| 2139 |
assert(m); |
| 2140 |
assert(a); |
| 2141 |
assert(!m->action_job); |
| 2142 |
|
| 2143 |
r = unit_load_state(m->bus, a->target, &load_state); |
| 2144 |
if (r < 0) |
| 2145 |
return r; |
| 2146 |
|
| 2147 |
if (!streq(load_state, "loaded")) |
| 2148 |
return log_notice_errno(SYNTHETIC_ERRNO(EACCES), |
| 2149 |
"Unit %s is %s, refusing operation.", |
| 2150 |
a->target, load_state); |
| 2151 |
|
| 2152 |
/* Tell everybody to prepare for shutdown/sleep */ |
| 2153 |
(void) send_prepare_for(m, a, true); |
| 2154 |
|
| 2155 |
delayed = |
| 2156 |
m->inhibit_delay_max > 0 && |
| 2157 |
a->inhibit_what >= 0 && |
| 2158 |
manager_is_inhibited(m, a->inhibit_what, NULL, MANAGER_IS_INHIBITED_CHECK_DELAY, UID_INVALID, NULL); |
| 2159 |
|
| 2160 |
if (delayed) |
| 2161 |
/* Shutdown is delayed, keep in mind what we |
| 2162 |
* want to do, and start a timeout */ |
| 2163 |
r = delay_shutdown_or_sleep(m, a); |
| 2164 |
else |
| 2165 |
/* Shutdown is not delayed, execute it |
| 2166 |
* immediately */ |
| 2167 |
r = execute_shutdown_or_sleep(m, a, error); |
| 2168 |
|
| 2169 |
return r; |
| 2170 |
} |
| 2171 |
|
| 2172 |
static int setup_wall_message_timer(Manager *m, sd_bus_message* message) { |
| 2173 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
| 2174 |
int r; |
| 2175 |
|
| 2176 |
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds); |
| 2177 |
if (r >= 0) { |
| 2178 |
const char *tty = NULL; |
| 2179 |
|
| 2180 |
(void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid); |
| 2181 |
(void) sd_bus_creds_get_tty(creds, &tty); |
| 2182 |
|
| 2183 |
r = free_and_strdup(&m->scheduled_shutdown_tty, tty); |
| 2184 |
if (r < 0) |
| 2185 |
return log_oom(); |
| 2186 |
} |
| 2187 |
|
| 2188 |
r = manager_setup_wall_message_timer(m); |
| 2189 |
if (r < 0) |
| 2190 |
return r; |
| 2191 |
|
| 2192 |
return 0; |
| 2193 |
} |
| 2194 |
|
| 2195 |
static int method_do_shutdown_or_sleep( |
| 2196 |
Manager *m, |
| 2197 |
sd_bus_message *message, |
| 2198 |
HandleAction action, |
| 2199 |
bool with_flags, |
| 2200 |
sd_bus_error *error) { |
| 2201 |
|
| 2202 |
uint64_t flags; |
| 2203 |
int r; |
| 2204 |
|
| 2205 |
assert(m); |
| 2206 |
assert(message); |
| 2207 |
assert(HANDLE_ACTION_IS_SHUTDOWN(action) || HANDLE_ACTION_IS_SLEEP(action)); |
| 2208 |
|
| 2209 |
if (with_flags) { |
| 2210 |
/* New style method: with flags parameter (and interactive bool in the bus message header) */ |
| 2211 |
r = sd_bus_message_read(message, "t", &flags); |
| 2212 |
if (r < 0) |
| 2213 |
return r; |
| 2214 |
if ((flags & ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC) != 0) |
| 2215 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, |
| 2216 |
"Invalid flags parameter"); |
| 2217 |
|
| 2218 |
if (FLAGS_SET(flags, (SD_LOGIND_REBOOT_VIA_KEXEC|SD_LOGIND_SOFT_REBOOT))) |
| 2219 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, |
| 2220 |
"Both reboot via kexec and soft reboot selected, which is not supported"); |
| 2221 |
|
| 2222 |
if (action != HANDLE_REBOOT) { |
| 2223 |
if (FLAGS_SET(flags, SD_LOGIND_REBOOT_VIA_KEXEC)) |
| 2224 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, |
| 2225 |
"Reboot via kexec option is only applicable with reboot operations"); |
| 2226 |
if (flags & (SD_LOGIND_SOFT_REBOOT|SD_LOGIND_SOFT_REBOOT_IF_NEXTROOT_SET_UP)) |
| 2227 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, |
| 2228 |
"Soft reboot option is only applicable with reboot operations"); |
| 2229 |
} |
| 2230 |
} else { |
| 2231 |
/* Old style method: no flags parameter, but interactive bool passed as boolean in |
| 2232 |
* payload. Let's convert this argument to the new-style flags parameter for our internal |
| 2233 |
* use. */ |
| 2234 |
int interactive; |
| 2235 |
|
| 2236 |
r = sd_bus_message_read(message, "b", &interactive); |
| 2237 |
if (r < 0) |
| 2238 |
return r; |
| 2239 |
|
| 2240 |
flags = interactive ? SD_LOGIND_INTERACTIVE : 0; |
| 2241 |
} |
| 2242 |
|
| 2243 |
const HandleActionData *a = NULL; |
| 2244 |
|
| 2245 |
if (FLAGS_SET(flags, SD_LOGIND_SOFT_REBOOT) || |
| 2246 |
(FLAGS_SET(flags, SD_LOGIND_SOFT_REBOOT_IF_NEXTROOT_SET_UP) && path_is_os_tree("/run/nextroot") > 0)) |
| 2247 |
a = handle_action_lookup(HANDLE_SOFT_REBOOT); |
| 2248 |
else if (FLAGS_SET(flags, SD_LOGIND_REBOOT_VIA_KEXEC) && kexec_loaded()) |
| 2249 |
a = handle_action_lookup(HANDLE_KEXEC); |
| 2250 |
|
| 2251 |
if (action == HANDLE_SLEEP) { |
| 2252 |
HandleAction selected; |
| 2253 |
|
| 2254 |
selected = handle_action_sleep_select(m); |
| 2255 |
if (selected < 0) |
| 2256 |
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, |
| 2257 |
"None of the configured sleep operations are supported"); |
| 2258 |
|
| 2259 |
assert_se(a = handle_action_lookup(selected)); |
| 2260 |
|
| 2261 |
} else if (HANDLE_ACTION_IS_SLEEP(action)) { |
| 2262 |
SleepSupport support; |
| 2263 |
|
| 2264 |
assert_se(a = handle_action_lookup(action)); |
| 2265 |
|
| 2266 |
assert(a->sleep_operation >= 0); |
| 2267 |
assert(a->sleep_operation < _SLEEP_OPERATION_MAX); |
| 2268 |
|
| 2269 |
r = sleep_supported_full(a->sleep_operation, &support); |
| 2270 |
if (r < 0) |
| 2271 |
return r; |
| 2272 |
if (r == 0) |
| 2273 |
switch (support) { |
| 2274 |
|
| 2275 |
case SLEEP_DISABLED: |
| 2276 |
return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, |
| 2277 |
"Sleep verb '%s' is disabled by config", |
| 2278 |
sleep_operation_to_string(a->sleep_operation)); |
| 2279 |
|
| 2280 |
case SLEEP_NOT_CONFIGURED: |
| 2281 |
case SLEEP_STATE_OR_MODE_NOT_SUPPORTED: |
| 2282 |
case SLEEP_ALARM_NOT_SUPPORTED: |
| 2283 |
return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, |
| 2284 |
"Sleep verb '%s' is not configured or configuration is not supported by kernel", |
| 2285 |
sleep_operation_to_string(a->sleep_operation)); |
| 2286 |
|
| 2287 |
case SLEEP_RESUME_NOT_SUPPORTED: |
| 2288 |
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, |
| 2289 |
"Not running on EFI and resume= is not set, or noresume is set. No available method to resume from hibernation"); |
| 2290 |
|
| 2291 |
case SLEEP_RESUME_DEVICE_MISSING: |
| 2292 |
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, |
| 2293 |
"Specified resume device is missing or is not an active swap device"); |
| 2294 |
|
| 2295 |
case SLEEP_RESUME_MISCONFIGURED: |
| 2296 |
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, |
| 2297 |
"Invalid resume config: resume= is not populated yet resume_offset= is"); |
| 2298 |
|
| 2299 |
case SLEEP_NOT_ENOUGH_SWAP_SPACE: |
| 2300 |
return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, |
| 2301 |
"Not enough suitable swap space for hibernation available on compatible block devices and file systems"); |
| 2302 |
|
| 2303 |
default: |
| 2304 |
assert_not_reached(); |
| 2305 |
|
| 2306 |
} |
| 2307 |
} else if (!a) |
| 2308 |
assert_se(a = handle_action_lookup(action)); |
| 2309 |
|
| 2310 |
r = manager_verify_shutdown_creds(m, message, /* link= */ NULL, a, flags, error); |
| 2311 |
if (r != 0) |
| 2312 |
return r; |
| 2313 |
|
| 2314 |
{ |
| 2315 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 2316 |
|
| 2317 |
(void) bus_query_sender_pidref(message, &pidref); |
| 2318 |
log_shutdown_caller(&pidref, handle_action_to_string(a->handle)); |
| 2319 |
} |
| 2320 |
|
| 2321 |
if (m->delayed_action) |
| 2322 |
return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, |
| 2323 |
"Action %s already in progress, refusing requested %s operation.", |
| 2324 |
handle_action_to_string(m->delayed_action->handle), |
| 2325 |
handle_action_to_string(a->handle)); |
| 2326 |
|
| 2327 |
/* reset case we're shorting a scheduled shutdown */ |
| 2328 |
m->unlink_nologin = false; |
| 2329 |
manager_reset_scheduled_shutdown(m); |
| 2330 |
|
| 2331 |
m->scheduled_shutdown_timeout = 0; |
| 2332 |
m->scheduled_shutdown_action = action; |
| 2333 |
|
| 2334 |
(void) setup_wall_message_timer(m, message); |
| 2335 |
|
| 2336 |
r = bus_manager_shutdown_or_sleep_now_or_later(m, a, error); |
| 2337 |
if (r < 0) |
| 2338 |
return r; |
| 2339 |
|
| 2340 |
return sd_bus_reply_method_return(message, NULL); |
| 2341 |
} |
| 2342 |
|
| 2343 |
static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2344 |
Manager *m = userdata; |
| 2345 |
|
| 2346 |
return method_do_shutdown_or_sleep( |
| 2347 |
m, message, |
| 2348 |
HANDLE_POWEROFF, |
| 2349 |
sd_bus_message_is_method_call(message, NULL, "PowerOffWithFlags"), |
| 2350 |
error); |
| 2351 |
} |
| 2352 |
|
| 2353 |
static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2354 |
Manager *m = userdata; |
| 2355 |
|
| 2356 |
return method_do_shutdown_or_sleep( |
| 2357 |
m, message, |
| 2358 |
HANDLE_REBOOT, |
| 2359 |
sd_bus_message_is_method_call(message, NULL, "RebootWithFlags"), |
| 2360 |
error); |
| 2361 |
} |
| 2362 |
|
| 2363 |
static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2364 |
Manager *m = userdata; |
| 2365 |
|
| 2366 |
return method_do_shutdown_or_sleep( |
| 2367 |
m, message, |
| 2368 |
HANDLE_HALT, |
| 2369 |
sd_bus_message_is_method_call(message, NULL, "HaltWithFlags"), |
| 2370 |
error); |
| 2371 |
} |
| 2372 |
|
| 2373 |
static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2374 |
Manager *m = userdata; |
| 2375 |
|
| 2376 |
return method_do_shutdown_or_sleep( |
| 2377 |
m, message, |
| 2378 |
HANDLE_SUSPEND, |
| 2379 |
sd_bus_message_is_method_call(message, NULL, "SuspendWithFlags"), |
| 2380 |
error); |
| 2381 |
} |
| 2382 |
|
| 2383 |
static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2384 |
Manager *m = userdata; |
| 2385 |
|
| 2386 |
return method_do_shutdown_or_sleep( |
| 2387 |
m, message, |
| 2388 |
HANDLE_HIBERNATE, |
| 2389 |
sd_bus_message_is_method_call(message, NULL, "HibernateWithFlags"), |
| 2390 |
error); |
| 2391 |
} |
| 2392 |
|
| 2393 |
static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2394 |
Manager *m = userdata; |
| 2395 |
|
| 2396 |
return method_do_shutdown_or_sleep( |
| 2397 |
m, message, |
| 2398 |
HANDLE_HYBRID_SLEEP, |
| 2399 |
sd_bus_message_is_method_call(message, NULL, "HybridSleepWithFlags"), |
| 2400 |
error); |
| 2401 |
} |
| 2402 |
|
| 2403 |
static int method_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2404 |
Manager *m = userdata; |
| 2405 |
|
| 2406 |
return method_do_shutdown_or_sleep( |
| 2407 |
m, message, |
| 2408 |
HANDLE_SUSPEND_THEN_HIBERNATE, |
| 2409 |
sd_bus_message_is_method_call(message, NULL, "SuspendThenHibernateWithFlags"), |
| 2410 |
error); |
| 2411 |
} |
| 2412 |
|
| 2413 |
static int method_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2414 |
Manager *m = userdata; |
| 2415 |
|
| 2416 |
return method_do_shutdown_or_sleep( |
| 2417 |
m, message, |
| 2418 |
HANDLE_SLEEP, |
| 2419 |
/* with_flags= */ true, |
| 2420 |
error); |
| 2421 |
} |
| 2422 |
|
| 2423 |
static int nologin_timeout_handler( |
| 2424 |
sd_event_source *s, |
| 2425 |
uint64_t usec, |
| 2426 |
void *userdata) { |
| 2427 |
|
| 2428 |
Manager *m = ASSERT_PTR(userdata); |
| 2429 |
|
| 2430 |
log_info("Creating /run/nologin, blocking further logins..."); |
| 2431 |
|
| 2432 |
m->unlink_nologin = |
| 2433 |
create_shutdown_run_nologin_or_warn() >= 0; |
| 2434 |
|
| 2435 |
return 0; |
| 2436 |
} |
| 2437 |
|
| 2438 |
static usec_t nologin_timeout_usec(usec_t elapse) { |
| 2439 |
/* Issue /run/nologin five minutes before shutdown */ |
| 2440 |
return LESS_BY(elapse, 5 * USEC_PER_MINUTE); |
| 2441 |
} |
| 2442 |
|
| 2443 |
static int update_schedule_file(Manager *m) { |
| 2444 |
_cleanup_(unlink_and_freep) char *temp_path = NULL; |
| 2445 |
_cleanup_fclose_ FILE *f = NULL; |
| 2446 |
int r; |
| 2447 |
|
| 2448 |
assert(m); |
| 2449 |
assert(handle_action_valid(m->scheduled_shutdown_action)); |
| 2450 |
|
| 2451 |
r = mkdir_parents_label(SHUTDOWN_SCHEDULE_FILE, 0755); |
| 2452 |
if (r < 0) |
| 2453 |
return log_error_errno(r, "Failed to create shutdown subdirectory: %m"); |
| 2454 |
|
| 2455 |
r = fopen_temporary(SHUTDOWN_SCHEDULE_FILE, &f, &temp_path); |
| 2456 |
if (r < 0) |
| 2457 |
return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m"); |
| 2458 |
|
| 2459 |
(void) fchmod(fileno(f), 0644); |
| 2460 |
|
| 2461 |
serialize_usec(f, "USEC", m->scheduled_shutdown_timeout); |
| 2462 |
serialize_item_format(f, "WARN_WALL", "%s", one_zero(m->wall_messages)); |
| 2463 |
serialize_item_format(f, "MODE", "%s", handle_action_to_string(m->scheduled_shutdown_action)); |
| 2464 |
serialize_item_format(f, "UID", UID_FMT, m->scheduled_shutdown_uid); |
| 2465 |
|
| 2466 |
if (m->scheduled_shutdown_tty) |
| 2467 |
serialize_item_format(f, "TTY", "%s", m->scheduled_shutdown_tty); |
| 2468 |
|
| 2469 |
if (!isempty(m->wall_message)) { |
| 2470 |
r = serialize_item_escaped(f, "WALL_MESSAGE", m->wall_message); |
| 2471 |
if (r < 0) |
| 2472 |
goto fail; |
| 2473 |
} |
| 2474 |
|
| 2475 |
r = fflush_and_check(f); |
| 2476 |
if (r < 0) |
| 2477 |
goto fail; |
| 2478 |
|
| 2479 |
if (rename(temp_path, SHUTDOWN_SCHEDULE_FILE) < 0) { |
| 2480 |
r = -errno; |
| 2481 |
goto fail; |
| 2482 |
} |
| 2483 |
|
| 2484 |
temp_path = mfree(temp_path); |
| 2485 |
return 0; |
| 2486 |
|
| 2487 |
fail: |
| 2488 |
(void) unlink(SHUTDOWN_SCHEDULE_FILE); |
| 2489 |
|
| 2490 |
return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m"); |
| 2491 |
} |
| 2492 |
|
| 2493 |
static int manager_scheduled_shutdown_handler( |
| 2494 |
sd_event_source *s, |
| 2495 |
uint64_t usec, |
| 2496 |
void *userdata) { |
| 2497 |
|
| 2498 |
Manager *m = ASSERT_PTR(userdata); |
| 2499 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 2500 |
const HandleActionData *a; |
| 2501 |
int r; |
| 2502 |
|
| 2503 |
assert_se(a = handle_action_lookup(m->scheduled_shutdown_action)); |
| 2504 |
|
| 2505 |
/* Don't allow multiple jobs being executed at the same time */ |
| 2506 |
if (m->delayed_action) { |
| 2507 |
r = log_error_errno(SYNTHETIC_ERRNO(EALREADY), |
| 2508 |
"Scheduled shutdown to %s failed: shutdown or sleep operation already in progress.", |
| 2509 |
a->target); |
| 2510 |
goto error; |
| 2511 |
} |
| 2512 |
|
| 2513 |
if (m->shutdown_dry_run) { |
| 2514 |
/* We do not process delay inhibitors here. Otherwise, we |
| 2515 |
* would have to be considered "in progress" (like the check |
| 2516 |
* above) for some seconds after our admin has seen the final |
| 2517 |
* wall message. */ |
| 2518 |
|
| 2519 |
bus_manager_log_shutdown(m, a); |
| 2520 |
log_info("Running in dry run, suppressing action."); |
| 2521 |
manager_reset_scheduled_shutdown(m); |
| 2522 |
|
| 2523 |
return 0; |
| 2524 |
} |
| 2525 |
|
| 2526 |
r = bus_manager_shutdown_or_sleep_now_or_later(m, a, &error); |
| 2527 |
if (r < 0) { |
| 2528 |
log_error_errno(r, "Scheduled shutdown to %s failed: %m", a->target); |
| 2529 |
goto error; |
| 2530 |
} |
| 2531 |
|
| 2532 |
return 0; |
| 2533 |
|
| 2534 |
error: |
| 2535 |
manager_reset_scheduled_shutdown(m); |
| 2536 |
return r; |
| 2537 |
} |
| 2538 |
|
| 2539 |
static int manager_setup_shutdown_timers(Manager* m) { |
| 2540 |
int r; |
| 2541 |
|
| 2542 |
assert(m); |
| 2543 |
|
| 2544 |
r = event_reset_time(m->event, &m->scheduled_shutdown_timeout_source, |
| 2545 |
CLOCK_REALTIME, |
| 2546 |
m->scheduled_shutdown_timeout, 0, |
| 2547 |
manager_scheduled_shutdown_handler, m, |
| 2548 |
0, "scheduled-shutdown-timeout", true); |
| 2549 |
if (r < 0) |
| 2550 |
goto fail; |
| 2551 |
|
| 2552 |
r = event_reset_time(m->event, &m->nologin_timeout_source, |
| 2553 |
CLOCK_REALTIME, |
| 2554 |
nologin_timeout_usec(m->scheduled_shutdown_timeout), 0, |
| 2555 |
nologin_timeout_handler, m, |
| 2556 |
0, "nologin-timeout", true); |
| 2557 |
if (r < 0) |
| 2558 |
goto fail; |
| 2559 |
|
| 2560 |
return 0; |
| 2561 |
|
| 2562 |
fail: |
| 2563 |
m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source); |
| 2564 |
m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source); |
| 2565 |
|
| 2566 |
return r; |
| 2567 |
} |
| 2568 |
|
| 2569 |
void manager_load_scheduled_shutdown(Manager *m) { |
| 2570 |
_cleanup_fclose_ FILE *f = NULL; |
| 2571 |
_cleanup_free_ char *usec = NULL, |
| 2572 |
*warn_wall = NULL, |
| 2573 |
*mode = NULL, |
| 2574 |
*wall_message = NULL, |
| 2575 |
*uid = NULL, |
| 2576 |
*tty = NULL; |
| 2577 |
int r; |
| 2578 |
|
| 2579 |
assert(m); |
| 2580 |
|
| 2581 |
r = parse_env_file(f, SHUTDOWN_SCHEDULE_FILE, |
| 2582 |
"USEC", &usec, |
| 2583 |
"WARN_WALL", &warn_wall, |
| 2584 |
"MODE", &mode, |
| 2585 |
"WALL_MESSAGE", &wall_message, |
| 2586 |
"UID", &uid, |
| 2587 |
"TTY", &tty); |
| 2588 |
|
| 2589 |
/* reset will delete the file */ |
| 2590 |
manager_reset_scheduled_shutdown(m); |
| 2591 |
|
| 2592 |
if (r == -ENOENT) |
| 2593 |
return; |
| 2594 |
if (r < 0) |
| 2595 |
return (void) log_debug_errno(r, "Failed to parse " SHUTDOWN_SCHEDULE_FILE ": %m"); |
| 2596 |
|
| 2597 |
HandleAction handle = handle_action_from_string(mode); |
| 2598 |
if (handle < 0) |
| 2599 |
return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse scheduled shutdown type: %s", mode); |
| 2600 |
|
| 2601 |
if (!usec) |
| 2602 |
return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "USEC is required"); |
| 2603 |
if (deserialize_usec(usec, &m->scheduled_shutdown_timeout) < 0) |
| 2604 |
return; |
| 2605 |
|
| 2606 |
/* assign parsed type only after we know usec is also valid */ |
| 2607 |
m->scheduled_shutdown_action = handle; |
| 2608 |
|
| 2609 |
if (warn_wall) { |
| 2610 |
r = parse_boolean(warn_wall); |
| 2611 |
if (r < 0) |
| 2612 |
log_debug_errno(r, "Failed to parse enabling wall messages"); |
| 2613 |
else |
| 2614 |
m->wall_messages = r; |
| 2615 |
} |
| 2616 |
|
| 2617 |
if (wall_message) { |
| 2618 |
_cleanup_free_ char *unescaped = NULL; |
| 2619 |
r = cunescape(wall_message, 0, &unescaped); |
| 2620 |
if (r < 0) |
| 2621 |
log_debug_errno(r, "Failed to parse wall message: %s", wall_message); |
| 2622 |
else |
| 2623 |
free_and_replace(m->wall_message, unescaped); |
| 2624 |
} |
| 2625 |
|
| 2626 |
if (uid) { |
| 2627 |
r = parse_uid(uid, &m->scheduled_shutdown_uid); |
| 2628 |
if (r < 0) |
| 2629 |
log_debug_errno(r, "Failed to parse wall uid: %s", uid); |
| 2630 |
} |
| 2631 |
|
| 2632 |
free_and_replace(m->scheduled_shutdown_tty, tty); |
| 2633 |
|
| 2634 |
r = manager_setup_shutdown_timers(m); |
| 2635 |
if (r < 0) |
| 2636 |
return manager_reset_scheduled_shutdown(m); |
| 2637 |
|
| 2638 |
(void) manager_setup_wall_message_timer(m); |
| 2639 |
(void) update_schedule_file(m); |
| 2640 |
|
| 2641 |
return; |
| 2642 |
} |
| 2643 |
|
| 2644 |
static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2645 |
Manager *m = ASSERT_PTR(userdata); |
| 2646 |
HandleAction handle; |
| 2647 |
const HandleActionData *a; |
| 2648 |
uint64_t elapse; |
| 2649 |
char *type; |
| 2650 |
int r; |
| 2651 |
bool dry_run = false; |
| 2652 |
|
| 2653 |
assert(message); |
| 2654 |
|
| 2655 |
r = sd_bus_message_read(message, "st", &type, &elapse); |
| 2656 |
if (r < 0) |
| 2657 |
return r; |
| 2658 |
|
| 2659 |
if (startswith(type, "dry-")) { |
| 2660 |
type += 4; |
| 2661 |
dry_run = true; |
| 2662 |
} |
| 2663 |
|
| 2664 |
handle = handle_action_from_string(type); |
| 2665 |
if (!HANDLE_ACTION_IS_SHUTDOWN(handle)) |
| 2666 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type: %s", type); |
| 2667 |
|
| 2668 |
assert_se(a = handle_action_lookup(handle)); |
| 2669 |
assert(a->polkit_action); |
| 2670 |
|
| 2671 |
r = manager_verify_shutdown_creds(m, message, /* link= */ NULL, a, 0, error); |
| 2672 |
if (r != 0) |
| 2673 |
return r; |
| 2674 |
|
| 2675 |
if (elapse == USEC_INFINITY) { |
| 2676 |
if (m->maintenance_time) { |
| 2677 |
r = calendar_spec_next_usec(m->maintenance_time, now(CLOCK_REALTIME), &elapse); |
| 2678 |
if (r == -ENOENT) |
| 2679 |
return sd_bus_error_set(error, |
| 2680 |
BUS_ERROR_DESIGNATED_MAINTENANCE_TIME_NOT_SCHEDULED, |
| 2681 |
"No upcoming maintenance window scheduled"); |
| 2682 |
if (r < 0) |
| 2683 |
return sd_bus_error_set_errnof(error, r, |
| 2684 |
"Failed to determine next maintenance window: %m"); |
| 2685 |
|
| 2686 |
log_info("Scheduled %s at maintenance window %s", type, FORMAT_TIMESTAMP(elapse)); |
| 2687 |
} else |
| 2688 |
/* the good old shutdown command uses one minute by default */ |
| 2689 |
elapse = usec_add(now(CLOCK_REALTIME), USEC_PER_MINUTE); |
| 2690 |
} |
| 2691 |
|
| 2692 |
m->scheduled_shutdown_action = handle; |
| 2693 |
m->shutdown_dry_run = dry_run; |
| 2694 |
m->scheduled_shutdown_timeout = elapse; |
| 2695 |
|
| 2696 |
r = manager_setup_shutdown_timers(m); |
| 2697 |
if (r < 0) |
| 2698 |
return r; |
| 2699 |
|
| 2700 |
r = setup_wall_message_timer(m, message); |
| 2701 |
if (r >= 0) |
| 2702 |
r = update_schedule_file(m); |
| 2703 |
|
| 2704 |
if (r < 0) { |
| 2705 |
manager_reset_scheduled_shutdown(m); |
| 2706 |
return r; |
| 2707 |
} |
| 2708 |
|
| 2709 |
manager_send_changed(m, "ScheduledShutdown"); |
| 2710 |
|
| 2711 |
return sd_bus_reply_method_return(message, NULL); |
| 2712 |
} |
| 2713 |
|
| 2714 |
static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2715 |
Manager *m = ASSERT_PTR(userdata); |
| 2716 |
const HandleActionData *a; |
| 2717 |
bool cancelled; |
| 2718 |
int r; |
| 2719 |
|
| 2720 |
assert(message); |
| 2721 |
|
| 2722 |
cancelled = handle_action_valid(m->scheduled_shutdown_action) && m->scheduled_shutdown_action != HANDLE_IGNORE; |
| 2723 |
if (!cancelled) |
| 2724 |
return sd_bus_reply_method_return(message, "b", false); |
| 2725 |
|
| 2726 |
assert_se(a = handle_action_lookup(m->scheduled_shutdown_action)); |
| 2727 |
if (!a->polkit_action) |
| 2728 |
return sd_bus_error_set(error, SD_BUS_ERROR_AUTH_FAILED, "Unsupported shutdown type"); |
| 2729 |
|
| 2730 |
r = bus_verify_polkit_async( |
| 2731 |
message, |
| 2732 |
a->polkit_action, |
| 2733 |
/* details= */ NULL, |
| 2734 |
&m->polkit_registry, |
| 2735 |
error); |
| 2736 |
if (r < 0) |
| 2737 |
return r; |
| 2738 |
if (r == 0) |
| 2739 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 2740 |
|
| 2741 |
if (m->wall_messages) { |
| 2742 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
| 2743 |
const char *tty = NULL; |
| 2744 |
uid_t uid = 0; |
| 2745 |
|
| 2746 |
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds); |
| 2747 |
if (r >= 0) { |
| 2748 |
(void) sd_bus_creds_get_uid(creds, &uid); |
| 2749 |
(void) sd_bus_creds_get_tty(creds, &tty); |
| 2750 |
} |
| 2751 |
|
| 2752 |
_cleanup_free_ char *username = uid_to_name(uid); |
| 2753 |
|
| 2754 |
log_struct(LOG_INFO, |
| 2755 |
LOG_MESSAGE("System shutdown has been cancelled"), |
| 2756 |
LOG_ITEM("ACTION=%s", handle_action_to_string(a->handle)), |
| 2757 |
LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN_CANCELED_STR), |
| 2758 |
username ? "OPERATOR=%s" : NULL, username); |
| 2759 |
|
| 2760 |
(void) wall("System shutdown has been cancelled", |
| 2761 |
username, tty, logind_wall_tty_filter, m); |
| 2762 |
} |
| 2763 |
|
| 2764 |
cancel_delayed_action(m); |
| 2765 |
manager_reset_scheduled_shutdown(m); |
| 2766 |
|
| 2767 |
return sd_bus_reply_method_return(message, "b", true); |
| 2768 |
} |
| 2769 |
|
| 2770 |
static int method_can_shutdown_or_sleep( |
| 2771 |
Manager *m, |
| 2772 |
sd_bus_message *message, |
| 2773 |
HandleAction action, |
| 2774 |
sd_bus_error *error) { |
| 2775 |
|
| 2776 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
| 2777 |
bool multiple_sessions, challenge, blocked, check_unit_state = true; |
| 2778 |
const HandleActionData *a; |
| 2779 |
uid_t uid; |
| 2780 |
int r; |
| 2781 |
|
| 2782 |
assert(m); |
| 2783 |
assert(message); |
| 2784 |
assert(HANDLE_ACTION_IS_SHUTDOWN(action) || HANDLE_ACTION_IS_SLEEP(action)); |
| 2785 |
|
| 2786 |
if (action == HANDLE_SLEEP) { |
| 2787 |
HandleAction selected; |
| 2788 |
|
| 2789 |
selected = handle_action_sleep_select(m); |
| 2790 |
if (selected < 0) |
| 2791 |
return sd_bus_reply_method_return(message, "s", "na"); |
| 2792 |
|
| 2793 |
check_unit_state = false; /* Already handled by handle_action_sleep_select */ |
| 2794 |
|
| 2795 |
assert_se(a = handle_action_lookup(selected)); |
| 2796 |
|
| 2797 |
} else if (HANDLE_ACTION_IS_SLEEP(action)) { |
| 2798 |
SleepSupport support; |
| 2799 |
|
| 2800 |
assert_se(a = handle_action_lookup(action)); |
| 2801 |
|
| 2802 |
assert(a->sleep_operation >= 0); |
| 2803 |
assert(a->sleep_operation < _SLEEP_OPERATION_MAX); |
| 2804 |
|
| 2805 |
r = sleep_supported_full(a->sleep_operation, &support); |
| 2806 |
if (r < 0) |
| 2807 |
return r; |
| 2808 |
if (r == 0) |
| 2809 |
return sd_bus_reply_method_return(message, "s", support == SLEEP_DISABLED ? "no" : "na"); |
| 2810 |
} else |
| 2811 |
assert_se(a = handle_action_lookup(action)); |
| 2812 |
|
| 2813 |
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); |
| 2814 |
if (r < 0) |
| 2815 |
return r; |
| 2816 |
|
| 2817 |
r = sd_bus_creds_get_euid(creds, &uid); |
| 2818 |
if (r < 0) |
| 2819 |
return r; |
| 2820 |
|
| 2821 |
r = manager_have_multiple_sessions(m, uid); |
| 2822 |
if (r < 0) |
| 2823 |
return r; |
| 2824 |
|
| 2825 |
multiple_sessions = r > 0; |
| 2826 |
blocked = manager_is_inhibited(m, a->inhibit_what, NULL, /* flags= */ 0, uid, NULL); |
| 2827 |
|
| 2828 |
if (check_unit_state && a->target) { |
| 2829 |
_cleanup_free_ char *load_state = NULL; |
| 2830 |
|
| 2831 |
r = unit_load_state(m->bus, a->target, &load_state); |
| 2832 |
if (r < 0) |
| 2833 |
return r; |
| 2834 |
|
| 2835 |
if (!streq(load_state, "loaded")) |
| 2836 |
return sd_bus_reply_method_return(message, "s", "no"); |
| 2837 |
} |
| 2838 |
|
| 2839 |
const char *result; |
| 2840 |
r = bus_test_polkit( |
| 2841 |
message, |
| 2842 |
a->polkit_action, |
| 2843 |
/* details= */ NULL, |
| 2844 |
/* good_user= */ UID_INVALID, |
| 2845 |
&challenge, |
| 2846 |
error); |
| 2847 |
if (r < 0) |
| 2848 |
return r; |
| 2849 |
if (r > 0) |
| 2850 |
result = "yes"; |
| 2851 |
else if (challenge) |
| 2852 |
result = "challenge"; |
| 2853 |
else |
| 2854 |
result = "no"; |
| 2855 |
|
| 2856 |
if (multiple_sessions) { |
| 2857 |
r = bus_test_polkit( |
| 2858 |
message, |
| 2859 |
a->polkit_action_multiple_sessions, |
| 2860 |
/* details= */ NULL, |
| 2861 |
/* good_user= */ UID_INVALID, |
| 2862 |
&challenge, |
| 2863 |
error); |
| 2864 |
if (r < 0) |
| 2865 |
return r; |
| 2866 |
|
| 2867 |
if (r == 0) { |
| 2868 |
if (challenge) { |
| 2869 |
if (streq(result, "yes")) /* Avoid upgrading no -> challenge */ |
| 2870 |
result = "challenge"; |
| 2871 |
} else |
| 2872 |
result = "no"; |
| 2873 |
} |
| 2874 |
} |
| 2875 |
|
| 2876 |
if (blocked) { |
| 2877 |
r = bus_test_polkit( |
| 2878 |
message, |
| 2879 |
a->polkit_action_ignore_inhibit, |
| 2880 |
/* details= */ NULL, |
| 2881 |
/* good_user= */ UID_INVALID, |
| 2882 |
&challenge, |
| 2883 |
error); |
| 2884 |
if (r < 0) |
| 2885 |
return r; |
| 2886 |
|
| 2887 |
if (r == 0) { |
| 2888 |
if (challenge) { |
| 2889 |
if (streq(result, "yes")) |
| 2890 |
result = "inhibited"; |
| 2891 |
/* If result is already "challenge" or "no", the held inhibitor has no effect */ |
| 2892 |
} else { |
| 2893 |
if (streq(result, "yes")) |
| 2894 |
result = "inhibitor-blocked"; |
| 2895 |
else if (streq(result, "challenge")) |
| 2896 |
result = "challenge-inhibitor-blocked"; |
| 2897 |
/* If the result is already "no", the held inhibitor has no effect */ |
| 2898 |
} |
| 2899 |
} |
| 2900 |
} |
| 2901 |
|
| 2902 |
return sd_bus_reply_method_return(message, "s", result); |
| 2903 |
} |
| 2904 |
|
| 2905 |
static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2906 |
Manager *m = userdata; |
| 2907 |
|
| 2908 |
return method_can_shutdown_or_sleep(m, message, HANDLE_POWEROFF, error); |
| 2909 |
} |
| 2910 |
|
| 2911 |
static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2912 |
Manager *m = userdata; |
| 2913 |
|
| 2914 |
return method_can_shutdown_or_sleep(m, message, HANDLE_REBOOT, error); |
| 2915 |
} |
| 2916 |
|
| 2917 |
static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2918 |
Manager *m = userdata; |
| 2919 |
|
| 2920 |
return method_can_shutdown_or_sleep(m, message, HANDLE_HALT, error); |
| 2921 |
} |
| 2922 |
|
| 2923 |
static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2924 |
Manager *m = userdata; |
| 2925 |
|
| 2926 |
return method_can_shutdown_or_sleep(m, message, HANDLE_SUSPEND, error); |
| 2927 |
} |
| 2928 |
|
| 2929 |
static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2930 |
Manager *m = userdata; |
| 2931 |
|
| 2932 |
return method_can_shutdown_or_sleep(m, message, HANDLE_HIBERNATE, error); |
| 2933 |
} |
| 2934 |
|
| 2935 |
static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2936 |
Manager *m = userdata; |
| 2937 |
|
| 2938 |
return method_can_shutdown_or_sleep(m, message, HANDLE_HYBRID_SLEEP, error); |
| 2939 |
} |
| 2940 |
|
| 2941 |
static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2942 |
Manager *m = userdata; |
| 2943 |
|
| 2944 |
return method_can_shutdown_or_sleep(m, message, HANDLE_SUSPEND_THEN_HIBERNATE, error); |
| 2945 |
} |
| 2946 |
|
| 2947 |
static int method_can_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 2948 |
Manager *m = userdata; |
| 2949 |
|
| 2950 |
return method_can_shutdown_or_sleep(m, message, HANDLE_SLEEP, error); |
| 2951 |
} |
| 2952 |
|
| 2953 |
static int property_get_reboot_parameter( |
| 2954 |
sd_bus *bus, |
| 2955 |
const char *path, |
| 2956 |
const char *interface, |
| 2957 |
const char *property, |
| 2958 |
sd_bus_message *reply, |
| 2959 |
void *userdata, |
| 2960 |
sd_bus_error *error) { |
| 2961 |
_cleanup_free_ char *parameter = NULL; |
| 2962 |
int r; |
| 2963 |
|
| 2964 |
assert(bus); |
| 2965 |
assert(reply); |
| 2966 |
assert(userdata); |
| 2967 |
|
| 2968 |
r = read_reboot_parameter(¶meter); |
| 2969 |
if (r < 0) |
| 2970 |
return r; |
| 2971 |
|
| 2972 |
return sd_bus_message_append(reply, "s", parameter); |
| 2973 |
} |
| 2974 |
|
| 2975 |
static int method_set_reboot_parameter( |
| 2976 |
sd_bus_message *message, |
| 2977 |
void *userdata, |
| 2978 |
sd_bus_error *error) { |
| 2979 |
|
| 2980 |
Manager *m = ASSERT_PTR(userdata); |
| 2981 |
const char *arg; |
| 2982 |
int r; |
| 2983 |
|
| 2984 |
assert(message); |
| 2985 |
|
| 2986 |
r = sd_bus_message_read(message, "s", &arg); |
| 2987 |
if (r < 0) |
| 2988 |
return r; |
| 2989 |
|
| 2990 |
if (!reboot_parameter_is_valid(arg)) |
| 2991 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid reboot parameter '%s'.", arg); |
| 2992 |
|
| 2993 |
r = detect_container(); |
| 2994 |
if (r < 0) |
| 2995 |
return r; |
| 2996 |
if (r > 0) |
| 2997 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, |
| 2998 |
"Reboot parameter not supported in containers, refusing."); |
| 2999 |
|
| 3000 |
r = bus_verify_polkit_async( |
| 3001 |
message, |
| 3002 |
"org.freedesktop.login1.set-reboot-parameter", |
| 3003 |
/* details= */ NULL, |
| 3004 |
&m->polkit_registry, |
| 3005 |
error); |
| 3006 |
if (r < 0) |
| 3007 |
return r; |
| 3008 |
if (r == 0) |
| 3009 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 3010 |
|
| 3011 |
r = update_reboot_parameter_and_warn(arg, false); |
| 3012 |
if (r < 0) |
| 3013 |
return r; |
| 3014 |
|
| 3015 |
return sd_bus_reply_method_return(message, NULL); |
| 3016 |
} |
| 3017 |
|
| 3018 |
static int method_can_reboot_parameter( |
| 3019 |
sd_bus_message *message, |
| 3020 |
void *userdata, |
| 3021 |
sd_bus_error *error) { |
| 3022 |
|
| 3023 |
_unused_ Manager *m = ASSERT_PTR(userdata); |
| 3024 |
int r; |
| 3025 |
|
| 3026 |
assert(message); |
| 3027 |
|
| 3028 |
r = detect_container(); |
| 3029 |
if (r < 0) |
| 3030 |
return r; |
| 3031 |
if (r > 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */ |
| 3032 |
return sd_bus_reply_method_return(message, "s", "na"); |
| 3033 |
|
| 3034 |
return return_test_polkit( |
| 3035 |
message, |
| 3036 |
"org.freedesktop.login1.set-reboot-parameter", |
| 3037 |
/* details= */ NULL, |
| 3038 |
/* good_user= */ UID_INVALID, |
| 3039 |
error); |
| 3040 |
} |
| 3041 |
|
| 3042 |
static int property_get_reboot_to_firmware_setup( |
| 3043 |
sd_bus *bus, |
| 3044 |
const char *path, |
| 3045 |
const char *interface, |
| 3046 |
const char *property, |
| 3047 |
sd_bus_message *reply, |
| 3048 |
void *userdata, |
| 3049 |
sd_bus_error *error) { |
| 3050 |
int r; |
| 3051 |
|
| 3052 |
assert(bus); |
| 3053 |
assert(reply); |
| 3054 |
assert(userdata); |
| 3055 |
|
| 3056 |
r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP"); |
| 3057 |
if (r == -ENXIO) { |
| 3058 |
/* EFI case: let's see what is currently configured in the EFI variables */ |
| 3059 |
r = efi_get_reboot_to_firmware(); |
| 3060 |
if (r < 0 && r != -EOPNOTSUPP) |
| 3061 |
log_warning_errno(r, "Failed to determine reboot-to-firmware-setup state: %m"); |
| 3062 |
} else if (r < 0) |
| 3063 |
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m"); |
| 3064 |
else if (r > 0) { |
| 3065 |
/* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */ |
| 3066 |
if (access("/run/systemd/reboot-to-firmware-setup", F_OK) < 0) { |
| 3067 |
if (errno != ENOENT) |
| 3068 |
log_warning_errno(errno, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m"); |
| 3069 |
|
| 3070 |
r = false; |
| 3071 |
} else |
| 3072 |
r = true; |
| 3073 |
} |
| 3074 |
|
| 3075 |
return sd_bus_message_append(reply, "b", r > 0); |
| 3076 |
} |
| 3077 |
|
| 3078 |
static int method_set_reboot_to_firmware_setup( |
| 3079 |
sd_bus_message *message, |
| 3080 |
void *userdata, |
| 3081 |
sd_bus_error *error) { |
| 3082 |
|
| 3083 |
Manager *m = ASSERT_PTR(userdata); |
| 3084 |
bool use_efi; |
| 3085 |
int b, r; |
| 3086 |
|
| 3087 |
assert(message); |
| 3088 |
|
| 3089 |
r = sd_bus_message_read(message, "b", &b); |
| 3090 |
if (r < 0) |
| 3091 |
return r; |
| 3092 |
|
| 3093 |
r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP"); |
| 3094 |
if (r == -ENXIO) { |
| 3095 |
/* EFI case: let's see what the firmware supports */ |
| 3096 |
|
| 3097 |
r = efi_reboot_to_firmware_supported(); |
| 3098 |
if (r == -EOPNOTSUPP) |
| 3099 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Firmware does not support boot into firmware."); |
| 3100 |
if (r < 0) |
| 3101 |
return r; |
| 3102 |
|
| 3103 |
use_efi = true; |
| 3104 |
|
| 3105 |
} else if (r <= 0) { |
| 3106 |
/* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */ |
| 3107 |
|
| 3108 |
if (r < 0) |
| 3109 |
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m"); |
| 3110 |
|
| 3111 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Firmware does not support boot into firmware."); |
| 3112 |
} else |
| 3113 |
/* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */ |
| 3114 |
use_efi = false; |
| 3115 |
|
| 3116 |
r = bus_verify_polkit_async( |
| 3117 |
message, |
| 3118 |
"org.freedesktop.login1.set-reboot-to-firmware-setup", |
| 3119 |
/* details= */ NULL, |
| 3120 |
&m->polkit_registry, |
| 3121 |
error); |
| 3122 |
if (r < 0) |
| 3123 |
return r; |
| 3124 |
if (r == 0) |
| 3125 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 3126 |
|
| 3127 |
if (use_efi) { |
| 3128 |
r = efi_set_reboot_to_firmware(b); |
| 3129 |
if (r < 0) |
| 3130 |
return r; |
| 3131 |
} else { |
| 3132 |
if (b) { |
| 3133 |
r = touch("/run/systemd/reboot-to-firmware-setup"); |
| 3134 |
if (r < 0) |
| 3135 |
return r; |
| 3136 |
} else { |
| 3137 |
if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno != ENOENT) |
| 3138 |
return -errno; |
| 3139 |
} |
| 3140 |
} |
| 3141 |
|
| 3142 |
return sd_bus_reply_method_return(message, NULL); |
| 3143 |
} |
| 3144 |
|
| 3145 |
static int method_can_reboot_to_firmware_setup( |
| 3146 |
sd_bus_message *message, |
| 3147 |
void *userdata, |
| 3148 |
sd_bus_error *error) { |
| 3149 |
|
| 3150 |
_unused_ Manager *m = ASSERT_PTR(userdata); |
| 3151 |
int r; |
| 3152 |
|
| 3153 |
assert(message); |
| 3154 |
|
| 3155 |
r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP"); |
| 3156 |
if (r == -ENXIO) { |
| 3157 |
/* EFI case: let's see what the firmware supports */ |
| 3158 |
|
| 3159 |
r = efi_reboot_to_firmware_supported(); |
| 3160 |
if (r < 0) { |
| 3161 |
if (r != -EOPNOTSUPP) |
| 3162 |
log_warning_errno(r, "Failed to determine whether reboot to firmware is supported: %m"); |
| 3163 |
|
| 3164 |
return sd_bus_reply_method_return(message, "s", "na"); |
| 3165 |
} |
| 3166 |
|
| 3167 |
} else if (r <= 0) { |
| 3168 |
/* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */ |
| 3169 |
|
| 3170 |
if (r < 0) |
| 3171 |
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m"); |
| 3172 |
|
| 3173 |
return sd_bus_reply_method_return(message, "s", "na"); |
| 3174 |
} |
| 3175 |
|
| 3176 |
return return_test_polkit( |
| 3177 |
message, |
| 3178 |
"org.freedesktop.login1.set-reboot-to-firmware-setup", |
| 3179 |
/* details= */ NULL, |
| 3180 |
/* good_user= */ UID_INVALID, |
| 3181 |
error); |
| 3182 |
} |
| 3183 |
|
| 3184 |
static int property_get_reboot_to_boot_loader_menu( |
| 3185 |
sd_bus *bus, |
| 3186 |
const char *path, |
| 3187 |
const char *interface, |
| 3188 |
const char *property, |
| 3189 |
sd_bus_message *reply, |
| 3190 |
void *userdata, |
| 3191 |
sd_bus_error *error) { |
| 3192 |
|
| 3193 |
uint64_t x = UINT64_MAX; |
| 3194 |
int r; |
| 3195 |
|
| 3196 |
assert(bus); |
| 3197 |
assert(reply); |
| 3198 |
assert(userdata); |
| 3199 |
|
| 3200 |
r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU"); |
| 3201 |
if (r == -ENXIO) { |
| 3202 |
/* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distinguished: |
| 3203 |
* |
| 3204 |
* 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user) |
| 3205 |
* 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user) |
| 3206 |
* 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds |
| 3207 |
*/ |
| 3208 |
|
| 3209 |
r = efi_loader_get_config_timeout_one_shot(&x); |
| 3210 |
if (r < 0) { |
| 3211 |
if (r != -ENOENT) |
| 3212 |
log_warning_errno(r, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m"); |
| 3213 |
} |
| 3214 |
|
| 3215 |
} else if (r < 0) |
| 3216 |
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m"); |
| 3217 |
else if (r > 0) { |
| 3218 |
_cleanup_free_ char *v = NULL; |
| 3219 |
|
| 3220 |
/* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */ |
| 3221 |
|
| 3222 |
r = read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v); |
| 3223 |
if (r < 0) { |
| 3224 |
if (r != -ENOENT) |
| 3225 |
log_warning_errno(r, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m"); |
| 3226 |
} else { |
| 3227 |
r = safe_atou64(v, &x); |
| 3228 |
if (r < 0) |
| 3229 |
log_warning_errno(r, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m"); |
| 3230 |
} |
| 3231 |
} |
| 3232 |
|
| 3233 |
return sd_bus_message_append(reply, "t", x); |
| 3234 |
} |
| 3235 |
|
| 3236 |
static int method_set_reboot_to_boot_loader_menu( |
| 3237 |
sd_bus_message *message, |
| 3238 |
void *userdata, |
| 3239 |
sd_bus_error *error) { |
| 3240 |
|
| 3241 |
Manager *m = ASSERT_PTR(userdata); |
| 3242 |
bool use_efi; |
| 3243 |
uint64_t x; |
| 3244 |
int r; |
| 3245 |
|
| 3246 |
assert(message); |
| 3247 |
|
| 3248 |
r = sd_bus_message_read(message, "t", &x); |
| 3249 |
if (r < 0) |
| 3250 |
return r; |
| 3251 |
|
| 3252 |
r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU"); |
| 3253 |
if (r == -ENXIO) { |
| 3254 |
uint64_t features; |
| 3255 |
|
| 3256 |
/* EFI case: let's see if booting into boot loader menu is supported. */ |
| 3257 |
|
| 3258 |
r = efi_loader_get_features(&features); |
| 3259 |
if (r < 0) |
| 3260 |
log_warning_errno(r, "Failed to determine whether reboot to boot loader menu is supported: %m"); |
| 3261 |
if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT)) |
| 3262 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader does not support boot into boot loader menu."); |
| 3263 |
|
| 3264 |
use_efi = true; |
| 3265 |
|
| 3266 |
} else if (r <= 0) { |
| 3267 |
/* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */ |
| 3268 |
|
| 3269 |
if (r < 0) |
| 3270 |
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m"); |
| 3271 |
|
| 3272 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader does not support boot into boot loader menu."); |
| 3273 |
} else |
| 3274 |
/* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */ |
| 3275 |
use_efi = false; |
| 3276 |
|
| 3277 |
r = bus_verify_polkit_async( |
| 3278 |
message, |
| 3279 |
"org.freedesktop.login1.set-reboot-to-boot-loader-menu", |
| 3280 |
/* details= */ NULL, |
| 3281 |
&m->polkit_registry, |
| 3282 |
error); |
| 3283 |
if (r < 0) |
| 3284 |
return r; |
| 3285 |
if (r == 0) |
| 3286 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 3287 |
|
| 3288 |
if (use_efi) { |
| 3289 |
if (x == UINT64_MAX) |
| 3290 |
r = efi_set_variable(EFI_LOADER_VARIABLE_STR("LoaderConfigTimeoutOneShot"), NULL, 0); |
| 3291 |
else { |
| 3292 |
char buf[DECIMAL_STR_MAX(uint64_t) + 1]; |
| 3293 |
xsprintf(buf, "%" PRIu64, DIV_ROUND_UP(x, USEC_PER_SEC)); /* second granularity */ |
| 3294 |
|
| 3295 |
r = efi_set_variable_string(EFI_LOADER_VARIABLE_STR("LoaderConfigTimeoutOneShot"), buf); |
| 3296 |
} |
| 3297 |
if (r < 0) |
| 3298 |
return r; |
| 3299 |
} else { |
| 3300 |
if (x == UINT64_MAX) { |
| 3301 |
if (unlink("/run/systemd/reboot-to-boot-loader-menu") < 0 && errno != ENOENT) |
| 3302 |
return -errno; |
| 3303 |
} else { |
| 3304 |
r = write_string_filef("/run/systemd/reboot-to-boot-loader-menu", |
| 3305 |
WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL, |
| 3306 |
"%" PRIu64, x); /* μs granularity */ |
| 3307 |
if (r < 0) |
| 3308 |
return r; |
| 3309 |
} |
| 3310 |
} |
| 3311 |
|
| 3312 |
return sd_bus_reply_method_return(message, NULL); |
| 3313 |
} |
| 3314 |
|
| 3315 |
static int method_can_reboot_to_boot_loader_menu( |
| 3316 |
sd_bus_message *message, |
| 3317 |
void *userdata, |
| 3318 |
sd_bus_error *error) { |
| 3319 |
|
| 3320 |
_unused_ Manager *m = ASSERT_PTR(userdata); |
| 3321 |
int r; |
| 3322 |
|
| 3323 |
assert(message); |
| 3324 |
|
| 3325 |
r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU"); |
| 3326 |
if (r == -ENXIO) { |
| 3327 |
uint64_t features = 0; |
| 3328 |
|
| 3329 |
/* EFI case, let's see if booting into boot loader menu is supported. */ |
| 3330 |
|
| 3331 |
r = efi_loader_get_features(&features); |
| 3332 |
if (r < 0) |
| 3333 |
log_warning_errno(r, "Failed to determine whether reboot to boot loader menu is supported: %m"); |
| 3334 |
if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT)) |
| 3335 |
return sd_bus_reply_method_return(message, "s", "na"); |
| 3336 |
|
| 3337 |
} else if (r <= 0) { |
| 3338 |
/* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */ |
| 3339 |
|
| 3340 |
if (r < 0) |
| 3341 |
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m"); |
| 3342 |
|
| 3343 |
return sd_bus_reply_method_return(message, "s", "na"); |
| 3344 |
} |
| 3345 |
|
| 3346 |
return return_test_polkit( |
| 3347 |
message, |
| 3348 |
"org.freedesktop.login1.set-reboot-to-boot-loader-menu", |
| 3349 |
/* details= */ NULL, |
| 3350 |
/* good_user= */ UID_INVALID, |
| 3351 |
error); |
| 3352 |
} |
| 3353 |
|
| 3354 |
static int property_get_reboot_to_boot_loader_entry( |
| 3355 |
sd_bus *bus, |
| 3356 |
const char *path, |
| 3357 |
const char *interface, |
| 3358 |
const char *property, |
| 3359 |
sd_bus_message *reply, |
| 3360 |
void *userdata, |
| 3361 |
sd_bus_error *error) { |
| 3362 |
|
| 3363 |
_cleanup_free_ char *v = NULL; |
| 3364 |
Manager *m = ASSERT_PTR(userdata); |
| 3365 |
const char *x = NULL; |
| 3366 |
int r; |
| 3367 |
|
| 3368 |
assert(bus); |
| 3369 |
assert(reply); |
| 3370 |
|
| 3371 |
r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY"); |
| 3372 |
if (r == -ENXIO) { |
| 3373 |
/* EFI case: let's read the LoaderEntryOneShot variable */ |
| 3374 |
|
| 3375 |
r = efi_loader_update_entry_one_shot_cache(&m->efi_loader_entry_one_shot, &m->efi_loader_entry_one_shot_stat); |
| 3376 |
if (r < 0) { |
| 3377 |
if (r != -ENOENT) |
| 3378 |
log_warning_errno(r, "Failed to read LoaderEntryOneShot variable, ignoring: %m"); |
| 3379 |
} else |
| 3380 |
x = m->efi_loader_entry_one_shot; |
| 3381 |
|
| 3382 |
} else if (r < 0) |
| 3383 |
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m"); |
| 3384 |
else if (r > 0) { |
| 3385 |
|
| 3386 |
/* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */ |
| 3387 |
|
| 3388 |
r = read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v); |
| 3389 |
if (r < 0) { |
| 3390 |
if (r != -ENOENT) |
| 3391 |
log_warning_errno(r, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m"); |
| 3392 |
} else if (!efi_loader_entry_name_valid(v)) |
| 3393 |
log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring."); |
| 3394 |
else |
| 3395 |
x = v; |
| 3396 |
} |
| 3397 |
|
| 3398 |
return sd_bus_message_append(reply, "s", x); |
| 3399 |
} |
| 3400 |
|
| 3401 |
static int boot_loader_entry_exists(Manager *m, const char *id) { |
| 3402 |
_cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL; |
| 3403 |
int r; |
| 3404 |
|
| 3405 |
assert(m); |
| 3406 |
assert(id); |
| 3407 |
|
| 3408 |
r = boot_config_load_auto(&config, NULL, NULL); |
| 3409 |
if (r < 0 && r != -ENOKEY) /* don't complain if no GPT is found, hence skip ENOKEY */ |
| 3410 |
return r; |
| 3411 |
|
| 3412 |
r = manager_read_efi_boot_loader_entries(m); |
| 3413 |
if (r >= 0) |
| 3414 |
(void) boot_config_augment_from_loader(&config, m->efi_boot_loader_entries, /* auto_only= */ true); |
| 3415 |
|
| 3416 |
return !!boot_config_find_entry(&config, id); |
| 3417 |
} |
| 3418 |
|
| 3419 |
static int method_set_reboot_to_boot_loader_entry( |
| 3420 |
sd_bus_message *message, |
| 3421 |
void *userdata, |
| 3422 |
sd_bus_error *error) { |
| 3423 |
|
| 3424 |
Manager *m = ASSERT_PTR(userdata); |
| 3425 |
bool use_efi; |
| 3426 |
const char *v; |
| 3427 |
int r; |
| 3428 |
|
| 3429 |
assert(message); |
| 3430 |
|
| 3431 |
r = sd_bus_message_read(message, "s", &v); |
| 3432 |
if (r < 0) |
| 3433 |
return r; |
| 3434 |
|
| 3435 |
if (isempty(v)) |
| 3436 |
v = NULL; |
| 3437 |
else if (efi_loader_entry_name_valid(v)) { |
| 3438 |
r = boot_loader_entry_exists(m, v); |
| 3439 |
if (r < 0) |
| 3440 |
return r; |
| 3441 |
if (r == 0) |
| 3442 |
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader entry '%s' is not known.", v); |
| 3443 |
} else |
| 3444 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Boot loader entry name '%s' is not valid, refusing.", v); |
| 3445 |
|
| 3446 |
r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY"); |
| 3447 |
if (r == -ENXIO) { |
| 3448 |
uint64_t features; |
| 3449 |
|
| 3450 |
/* EFI case: let's see if booting into boot loader entry is supported. */ |
| 3451 |
|
| 3452 |
r = efi_loader_get_features(&features); |
| 3453 |
if (r < 0) |
| 3454 |
log_warning_errno(r, "Failed to determine whether reboot into boot loader entry is supported: %m"); |
| 3455 |
if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_ENTRY_ONESHOT)) |
| 3456 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Loader does not support boot into boot loader entry."); |
| 3457 |
|
| 3458 |
use_efi = true; |
| 3459 |
|
| 3460 |
} else if (r <= 0) { |
| 3461 |
/* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */ |
| 3462 |
|
| 3463 |
if (r < 0) |
| 3464 |
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m"); |
| 3465 |
|
| 3466 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Loader does not support boot into boot loader entry."); |
| 3467 |
} else |
| 3468 |
/* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */ |
| 3469 |
use_efi = false; |
| 3470 |
|
| 3471 |
r = bus_verify_polkit_async( |
| 3472 |
message, |
| 3473 |
"org.freedesktop.login1.set-reboot-to-boot-loader-entry", |
| 3474 |
/* details= */ NULL, |
| 3475 |
&m->polkit_registry, |
| 3476 |
error); |
| 3477 |
if (r < 0) |
| 3478 |
return r; |
| 3479 |
if (r == 0) |
| 3480 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 3481 |
|
| 3482 |
if (use_efi) { |
| 3483 |
if (isempty(v)) |
| 3484 |
/* Delete item */ |
| 3485 |
r = efi_set_variable(EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"), NULL, 0); |
| 3486 |
else |
| 3487 |
r = efi_set_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"), v); |
| 3488 |
if (r < 0) |
| 3489 |
return r; |
| 3490 |
} else { |
| 3491 |
if (isempty(v)) { |
| 3492 |
if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno != ENOENT) |
| 3493 |
return -errno; |
| 3494 |
} else { |
| 3495 |
r = write_string_file("/run/systemd/reboot-boot-to-loader-entry", v, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL); |
| 3496 |
if (r < 0) |
| 3497 |
return r; |
| 3498 |
} |
| 3499 |
} |
| 3500 |
|
| 3501 |
return sd_bus_reply_method_return(message, NULL); |
| 3502 |
} |
| 3503 |
|
| 3504 |
static int method_can_reboot_to_boot_loader_entry( |
| 3505 |
sd_bus_message *message, |
| 3506 |
void *userdata, |
| 3507 |
sd_bus_error *error) { |
| 3508 |
|
| 3509 |
_unused_ Manager *m = ASSERT_PTR(userdata); |
| 3510 |
int r; |
| 3511 |
|
| 3512 |
assert(message); |
| 3513 |
|
| 3514 |
r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY"); |
| 3515 |
if (r == -ENXIO) { |
| 3516 |
uint64_t features = 0; |
| 3517 |
|
| 3518 |
/* EFI case, let's see if booting into boot loader entry is supported. */ |
| 3519 |
|
| 3520 |
r = efi_loader_get_features(&features); |
| 3521 |
if (r < 0) |
| 3522 |
log_warning_errno(r, "Failed to determine whether reboot to boot loader entry is supported: %m"); |
| 3523 |
if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_ENTRY_ONESHOT)) |
| 3524 |
return sd_bus_reply_method_return(message, "s", "na"); |
| 3525 |
|
| 3526 |
} else if (r <= 0) { |
| 3527 |
/* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */ |
| 3528 |
|
| 3529 |
if (r < 0) |
| 3530 |
log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m"); |
| 3531 |
|
| 3532 |
return sd_bus_reply_method_return(message, "s", "na"); |
| 3533 |
} |
| 3534 |
|
| 3535 |
return return_test_polkit( |
| 3536 |
message, |
| 3537 |
"org.freedesktop.login1.set-reboot-to-boot-loader-entry", |
| 3538 |
/* details= */ NULL, |
| 3539 |
/* good_user= */ UID_INVALID, |
| 3540 |
error); |
| 3541 |
} |
| 3542 |
|
| 3543 |
static int property_get_boot_loader_entries( |
| 3544 |
sd_bus *bus, |
| 3545 |
const char *path, |
| 3546 |
const char *interface, |
| 3547 |
const char *property, |
| 3548 |
sd_bus_message *reply, |
| 3549 |
void *userdata, |
| 3550 |
sd_bus_error *error) { |
| 3551 |
|
| 3552 |
_cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL; |
| 3553 |
Manager *m = ASSERT_PTR(userdata); |
| 3554 |
size_t i; |
| 3555 |
int r; |
| 3556 |
|
| 3557 |
assert(bus); |
| 3558 |
assert(reply); |
| 3559 |
|
| 3560 |
r = boot_config_load_auto(&config, NULL, NULL); |
| 3561 |
if (r < 0 && r != -ENOKEY) /* don't complain if there's no GPT found */ |
| 3562 |
return r; |
| 3563 |
|
| 3564 |
r = manager_read_efi_boot_loader_entries(m); |
| 3565 |
if (r >= 0) |
| 3566 |
(void) boot_config_augment_from_loader(&config, m->efi_boot_loader_entries, /* auto_only= */ true); |
| 3567 |
|
| 3568 |
r = sd_bus_message_open_container(reply, 'a', "s"); |
| 3569 |
if (r < 0) |
| 3570 |
return r; |
| 3571 |
|
| 3572 |
for (i = 0; i < config.n_entries; i++) { |
| 3573 |
BootEntry *e = config.entries + i; |
| 3574 |
|
| 3575 |
r = sd_bus_message_append(reply, "s", e->id); |
| 3576 |
if (r < 0) |
| 3577 |
return r; |
| 3578 |
} |
| 3579 |
|
| 3580 |
return sd_bus_message_close_container(reply); |
| 3581 |
} |
| 3582 |
|
| 3583 |
static int wall_message_validate(const char *wall_message, sd_bus_error *error) { |
| 3584 |
if (strlen(wall_message) > WALL_MESSAGE_MAX) |
| 3585 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 3586 |
"Wall message too long, maximum permitted length is %u characters.", |
| 3587 |
WALL_MESSAGE_MAX); |
| 3588 |
|
| 3589 |
if (string_has_cc(wall_message, /* ok= */ "\n\t")) |
| 3590 |
return sd_bus_error_set(error, |
| 3591 |
SD_BUS_ERROR_INVALID_ARGS, |
| 3592 |
"Wall message contains control characters, refusing."); |
| 3593 |
|
| 3594 |
return 0; |
| 3595 |
} |
| 3596 |
|
| 3597 |
static int property_set_wall_message( |
| 3598 |
sd_bus *bus, |
| 3599 |
const char *path, |
| 3600 |
const char *interface, |
| 3601 |
const char *property, |
| 3602 |
sd_bus_message *value, |
| 3603 |
void *userdata, |
| 3604 |
sd_bus_error *error) { |
| 3605 |
|
| 3606 |
char **p = ASSERT_PTR(userdata); |
| 3607 |
const char *s; |
| 3608 |
int r; |
| 3609 |
|
| 3610 |
assert(value); |
| 3611 |
|
| 3612 |
r = sd_bus_message_read(value, "s", &s); |
| 3613 |
if (r < 0) |
| 3614 |
return r; |
| 3615 |
|
| 3616 |
r = wall_message_validate(s, error); |
| 3617 |
if (r < 0) |
| 3618 |
return r; |
| 3619 |
|
| 3620 |
return free_and_strdup_warn(p, empty_to_null(s)); |
| 3621 |
} |
| 3622 |
|
| 3623 |
static int method_set_wall_message( |
| 3624 |
sd_bus_message *message, |
| 3625 |
void *userdata, |
| 3626 |
sd_bus_error *error) { |
| 3627 |
|
| 3628 |
int r; |
| 3629 |
Manager *m = ASSERT_PTR(userdata); |
| 3630 |
char *wall_message; |
| 3631 |
int enable_wall_messages; |
| 3632 |
|
| 3633 |
assert(message); |
| 3634 |
|
| 3635 |
r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages); |
| 3636 |
if (r < 0) |
| 3637 |
return r; |
| 3638 |
|
| 3639 |
r = wall_message_validate(wall_message, error); |
| 3640 |
if (r < 0) |
| 3641 |
return r; |
| 3642 |
|
| 3643 |
/* Short-circuit the operation if the desired state is already in place, to |
| 3644 |
* avoid an unnecessary polkit permission check. */ |
| 3645 |
if (streq_ptr(m->wall_message, empty_to_null(wall_message)) && |
| 3646 |
m->wall_messages == enable_wall_messages) |
| 3647 |
goto done; |
| 3648 |
|
| 3649 |
r = bus_verify_polkit_async( |
| 3650 |
message, |
| 3651 |
"org.freedesktop.login1.set-wall-message", |
| 3652 |
/* details= */ NULL, |
| 3653 |
&m->polkit_registry, |
| 3654 |
error); |
| 3655 |
if (r < 0) |
| 3656 |
return r; |
| 3657 |
if (r == 0) |
| 3658 |
return 1; /* Will call us back */ |
| 3659 |
|
| 3660 |
r = free_and_strdup(&m->wall_message, empty_to_null(wall_message)); |
| 3661 |
if (r < 0) |
| 3662 |
return log_oom(); |
| 3663 |
|
| 3664 |
m->wall_messages = enable_wall_messages; |
| 3665 |
|
| 3666 |
done: |
| 3667 |
return sd_bus_reply_method_return(message, NULL); |
| 3668 |
} |
| 3669 |
|
| 3670 |
static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 3671 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
| 3672 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 3673 |
const char *who, *why, *what, *mode; |
| 3674 |
_cleanup_free_ char *id = NULL; |
| 3675 |
_cleanup_close_ int fifo_fd = -EBADF; |
| 3676 |
Manager *m = ASSERT_PTR(userdata); |
| 3677 |
InhibitMode mm; |
| 3678 |
InhibitWhat w; |
| 3679 |
uid_t uid; |
| 3680 |
_unused_ bool error_or_denial = false; |
| 3681 |
int r; |
| 3682 |
|
| 3683 |
assert(message); |
| 3684 |
|
| 3685 |
r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode); |
| 3686 |
if (r < 0) |
| 3687 |
return r; |
| 3688 |
|
| 3689 |
w = inhibit_what_from_string(what); |
| 3690 |
if (w <= 0) |
| 3691 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 3692 |
"Invalid what specification %s", what); |
| 3693 |
|
| 3694 |
mm = inhibit_mode_from_string(mode); |
| 3695 |
if (mm < 0) |
| 3696 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
| 3697 |
"Invalid mode specification %s", mode); |
| 3698 |
|
| 3699 |
/* Delay is only supported for shutdown/sleep */ |
| 3700 |
if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP))) |
| 3701 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, |
| 3702 |
"Delay inhibitors only supported for shutdown and sleep"); |
| 3703 |
|
| 3704 |
/* Don't allow taking delay locks while we are already |
| 3705 |
* executing the operation. We shouldn't create the impression |
| 3706 |
* that the lock was successful if the machine is about to go |
| 3707 |
* down/suspend any moment. */ |
| 3708 |
if (m->delayed_action && m->delayed_action->inhibit_what & w) |
| 3709 |
return sd_bus_error_set(error, BUS_ERROR_OPERATION_IN_PROGRESS, |
| 3710 |
"The operation inhibition has been requested for is already running"); |
| 3711 |
|
| 3712 |
BIT_FOREACH(i, w) { |
| 3713 |
const InhibitWhat v = 1U << i; |
| 3714 |
|
| 3715 |
r = bus_verify_polkit_async( |
| 3716 |
message, |
| 3717 |
v == INHIBIT_SHUTDOWN ? (IN_SET(mm, INHIBIT_BLOCK, INHIBIT_BLOCK_WEAK) ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") : |
| 3718 |
v == INHIBIT_SLEEP ? (IN_SET(mm, INHIBIT_BLOCK, INHIBIT_BLOCK_WEAK) ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") : |
| 3719 |
v == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" : |
| 3720 |
v == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" : |
| 3721 |
v == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" : |
| 3722 |
v == INHIBIT_HANDLE_REBOOT_KEY ? "org.freedesktop.login1.inhibit-handle-reboot-key" : |
| 3723 |
v == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" : |
| 3724 |
"org.freedesktop.login1.inhibit-handle-lid-switch", |
| 3725 |
/* details= */ NULL, |
| 3726 |
&m->polkit_registry, |
| 3727 |
error); |
| 3728 |
if (r < 0) { |
| 3729 |
/* If we get -EBUSY, it means a polkit decision was made, but not for |
| 3730 |
* this action in particular. Assuming there are more actions requested, |
| 3731 |
* ignore that error and allow the decision to be revealed later. */ |
| 3732 |
if ((~v & w) && r == -EBUSY) |
| 3733 |
error_or_denial = true; |
| 3734 |
else |
| 3735 |
return r; |
| 3736 |
} |
| 3737 |
if (r == 0) |
| 3738 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 3739 |
} |
| 3740 |
|
| 3741 |
/* If error_or_denial was set above, it means that a polkit denial or |
| 3742 |
* error was deferred for a future call to bus_verify_polkit_async() |
| 3743 |
* to catch. In any case, it also means that the payload guarded by |
| 3744 |
* these polkit calls should never be executed, and hence we should |
| 3745 |
* never reach this point. */ |
| 3746 |
assert(!error_or_denial); |
| 3747 |
|
| 3748 |
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID|SD_BUS_CREDS_PIDFD, &creds); |
| 3749 |
if (r < 0) |
| 3750 |
return r; |
| 3751 |
|
| 3752 |
r = sd_bus_creds_get_euid(creds, &uid); |
| 3753 |
if (r < 0) |
| 3754 |
return r; |
| 3755 |
|
| 3756 |
r = bus_creds_get_pidref(creds, &pidref); |
| 3757 |
if (r < 0) |
| 3758 |
return r; |
| 3759 |
|
| 3760 |
if (hashmap_size(m->inhibitors) >= m->inhibitors_max) |
| 3761 |
return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, |
| 3762 |
"Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", |
| 3763 |
m->inhibitors_max); |
| 3764 |
|
| 3765 |
do { |
| 3766 |
id = mfree(id); |
| 3767 |
|
| 3768 |
if (asprintf(&id, "%" PRIu64, ++m->inhibit_counter) < 0) |
| 3769 |
return -ENOMEM; |
| 3770 |
|
| 3771 |
} while (hashmap_contains(m->inhibitors, id)); |
| 3772 |
|
| 3773 |
_cleanup_(inhibitor_freep) Inhibitor *i = NULL; |
| 3774 |
r = manager_add_inhibitor(m, id, &i); |
| 3775 |
if (r < 0) |
| 3776 |
return r; |
| 3777 |
|
| 3778 |
i->what = w; |
| 3779 |
i->mode = mm; |
| 3780 |
i->pid = TAKE_PIDREF(pidref); |
| 3781 |
i->uid = uid; |
| 3782 |
i->why = strdup(why); |
| 3783 |
i->who = strdup(who); |
| 3784 |
|
| 3785 |
if (!i->why || !i->who) |
| 3786 |
return -ENOMEM; |
| 3787 |
|
| 3788 |
fifo_fd = inhibitor_create_fifo(i); |
| 3789 |
if (fifo_fd < 0) |
| 3790 |
return fifo_fd; |
| 3791 |
|
| 3792 |
r = inhibitor_start(i); |
| 3793 |
if (r < 0) |
| 3794 |
return r; |
| 3795 |
TAKE_PTR(i); |
| 3796 |
|
| 3797 |
return sd_bus_reply_method_return(message, "h", fifo_fd); |
| 3798 |
} |
| 3799 |
|
| 3800 |
static const sd_bus_vtable manager_vtable[] = { |
| 3801 |
SD_BUS_VTABLE_START(0), |
| 3802 |
|
| 3803 |
SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", bus_property_get_bool, bus_property_set_bool, offsetof(Manager, wall_messages), 0), |
| 3804 |
SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, property_set_wall_message, offsetof(Manager, wall_message), 0), |
| 3805 |
|
| 3806 |
SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3807 |
SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3808 |
SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3809 |
SD_BUS_PROPERTY("KillUserProcesses", "b", bus_property_get_bool, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3810 |
SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter, 0, 0), |
| 3811 |
SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, 0), |
| 3812 |
SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu, 0, 0), |
| 3813 |
SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry, 0, 0), |
| 3814 |
SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 3815 |
SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 3816 |
SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 3817 |
SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 3818 |
SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 3819 |
SD_BUS_PROPERTY("BlockWeakInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 3820 |
SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 3821 |
SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3822 |
SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL, offsetof(Manager, user_stop_delay), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3823 |
SD_BUS_PROPERTY("SleepOperation", "as", property_get_sleep_operations, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 3824 |
SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3825 |
SD_BUS_PROPERTY("HandlePowerKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_power_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3826 |
SD_BUS_PROPERTY("HandleRebootKey", "s", property_get_handle_action, offsetof(Manager, handle_reboot_key), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3827 |
SD_BUS_PROPERTY("HandleRebootKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_reboot_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3828 |
SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3829 |
SD_BUS_PROPERTY("HandleSuspendKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3830 |
SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3831 |
SD_BUS_PROPERTY("HandleHibernateKeyLongPress", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key_long_press), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3832 |
SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3833 |
SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3834 |
SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3835 |
SD_BUS_PROPERTY("HandleSecureAttentionKey", "s", property_get_handle_action, offsetof(Manager, handle_secure_attention_key), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3836 |
SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3837 |
SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3838 |
SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3839 |
SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0), |
| 3840 |
SD_BUS_PROPERTY("PreparingForShutdownWithMetadata", "a{sv}", property_get_preparing_shutdown_with_metadata, 0, 0), |
| 3841 |
SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0), |
| 3842 |
SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 3843 |
SD_BUS_PROPERTY("DesignatedMaintenanceTime", "s", property_get_maintenance_time, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 3844 |
SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0), |
| 3845 |
SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 3846 |
SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power, 0, 0), |
| 3847 |
SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3848 |
SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3849 |
SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL, offsetof(Manager, runtime_dir_inodes), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3850 |
SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3851 |
SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size, offsetof(Manager, inhibitors), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 3852 |
SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3853 |
SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size, offsetof(Manager, sessions), 0), |
| 3854 |
SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), |
| 3855 |
SD_BUS_PROPERTY("StopIdleSessionUSec", "t", NULL, offsetof(Manager, stop_idle_session_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
| 3856 |
|
| 3857 |
SD_BUS_METHOD_WITH_ARGS("GetSession", |
| 3858 |
SD_BUS_ARGS("s", session_id), |
| 3859 |
SD_BUS_RESULT("o", object_path), |
| 3860 |
method_get_session, |
| 3861 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3862 |
SD_BUS_METHOD_WITH_ARGS("GetSessionByPID", |
| 3863 |
SD_BUS_ARGS("u", pid), |
| 3864 |
SD_BUS_RESULT("o", object_path), |
| 3865 |
method_get_session_by_pid, |
| 3866 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3867 |
SD_BUS_METHOD_WITH_ARGS("GetUser", |
| 3868 |
SD_BUS_ARGS("u", uid), |
| 3869 |
SD_BUS_RESULT("o", object_path), |
| 3870 |
method_get_user, |
| 3871 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3872 |
SD_BUS_METHOD_WITH_ARGS("GetUserByPID", |
| 3873 |
SD_BUS_ARGS("u", pid), |
| 3874 |
SD_BUS_RESULT("o", object_path), |
| 3875 |
method_get_user_by_pid, |
| 3876 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3877 |
SD_BUS_METHOD_WITH_ARGS("GetSeat", |
| 3878 |
SD_BUS_ARGS("s", seat_id), |
| 3879 |
SD_BUS_RESULT("o", object_path), |
| 3880 |
method_get_seat, |
| 3881 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3882 |
SD_BUS_METHOD_WITH_ARGS("ListSessions", |
| 3883 |
SD_BUS_NO_ARGS, |
| 3884 |
SD_BUS_RESULT("a(susso)", sessions), |
| 3885 |
method_list_sessions, |
| 3886 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3887 |
SD_BUS_METHOD_WITH_ARGS("ListSessionsEx", |
| 3888 |
SD_BUS_NO_ARGS, |
| 3889 |
SD_BUS_RESULT("a(sussussbto)", sessions), |
| 3890 |
method_list_sessions_ex, |
| 3891 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3892 |
SD_BUS_METHOD_WITH_ARGS("ListUsers", |
| 3893 |
SD_BUS_NO_ARGS, |
| 3894 |
SD_BUS_RESULT("a(uso)", users), |
| 3895 |
method_list_users, |
| 3896 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3897 |
SD_BUS_METHOD_WITH_ARGS("ListSeats", |
| 3898 |
SD_BUS_NO_ARGS, |
| 3899 |
SD_BUS_RESULT("a(so)", seats), |
| 3900 |
method_list_seats, |
| 3901 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3902 |
SD_BUS_METHOD_WITH_ARGS("ListInhibitors", |
| 3903 |
SD_BUS_NO_ARGS, |
| 3904 |
SD_BUS_RESULT("a(ssssuu)", inhibitors), |
| 3905 |
method_list_inhibitors, |
| 3906 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3907 |
SD_BUS_METHOD_WITH_ARGS("CreateSession", |
| 3908 |
SD_BUS_ARGS("u", uid, |
| 3909 |
"u", pid, |
| 3910 |
"s", service, |
| 3911 |
"s", type, |
| 3912 |
"s", class, |
| 3913 |
"s", desktop, |
| 3914 |
"s", seat_id, |
| 3915 |
"u", vtnr, |
| 3916 |
"s", tty, |
| 3917 |
"s", display, |
| 3918 |
"b", remote, |
| 3919 |
"s", remote_user, |
| 3920 |
"s", remote_host, |
| 3921 |
"a(sv)", properties), |
| 3922 |
SD_BUS_RESULT("s", session_id, |
| 3923 |
"o", object_path, |
| 3924 |
"s", runtime_path, |
| 3925 |
"h", fifo_fd, |
| 3926 |
"u", uid, |
| 3927 |
"s", seat_id, |
| 3928 |
"u", vtnr, |
| 3929 |
"b", existing), |
| 3930 |
method_create_session, |
| 3931 |
0), |
| 3932 |
SD_BUS_METHOD_WITH_ARGS("CreateSessionWithPIDFD", |
| 3933 |
SD_BUS_ARGS("u", uid, |
| 3934 |
"h", pidfd, |
| 3935 |
"s", service, |
| 3936 |
"s", type, |
| 3937 |
"s", class, |
| 3938 |
"s", desktop, |
| 3939 |
"s", seat_id, |
| 3940 |
"u", vtnr, |
| 3941 |
"s", tty, |
| 3942 |
"s", display, |
| 3943 |
"b", remote, |
| 3944 |
"s", remote_user, |
| 3945 |
"s", remote_host, |
| 3946 |
"t", flags, |
| 3947 |
"a(sv)", properties), |
| 3948 |
SD_BUS_RESULT("s", session_id, |
| 3949 |
"o", object_path, |
| 3950 |
"s", runtime_path, |
| 3951 |
"h", fifo_fd, |
| 3952 |
"u", uid, |
| 3953 |
"s", seat_id, |
| 3954 |
"u", vtnr, |
| 3955 |
"b", existing), |
| 3956 |
method_create_session_pidfd, |
| 3957 |
0), |
| 3958 |
SD_BUS_METHOD_WITH_ARGS("ReleaseSession", |
| 3959 |
SD_BUS_ARGS("s", session_id), |
| 3960 |
SD_BUS_NO_RESULT, |
| 3961 |
method_release_session, |
| 3962 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3963 |
SD_BUS_METHOD_WITH_ARGS("ActivateSession", |
| 3964 |
SD_BUS_ARGS("s", session_id), |
| 3965 |
SD_BUS_NO_RESULT, |
| 3966 |
method_activate_session, |
| 3967 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3968 |
SD_BUS_METHOD_WITH_ARGS("ActivateSessionOnSeat", |
| 3969 |
SD_BUS_ARGS("s", session_id, "s", seat_id), |
| 3970 |
SD_BUS_NO_RESULT, |
| 3971 |
method_activate_session_on_seat, |
| 3972 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3973 |
SD_BUS_METHOD_WITH_ARGS("LockSession", |
| 3974 |
SD_BUS_ARGS("s", session_id), |
| 3975 |
SD_BUS_NO_RESULT, |
| 3976 |
method_lock_session, |
| 3977 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3978 |
SD_BUS_METHOD_WITH_ARGS("UnlockSession", |
| 3979 |
SD_BUS_ARGS("s", session_id), |
| 3980 |
SD_BUS_NO_RESULT, |
| 3981 |
method_lock_session, |
| 3982 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3983 |
SD_BUS_METHOD("LockSessions", |
| 3984 |
NULL, |
| 3985 |
NULL, |
| 3986 |
method_lock_sessions, |
| 3987 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3988 |
SD_BUS_METHOD("UnlockSessions", |
| 3989 |
NULL, |
| 3990 |
NULL, |
| 3991 |
method_lock_sessions, |
| 3992 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3993 |
SD_BUS_METHOD_WITH_ARGS("KillSession", |
| 3994 |
SD_BUS_ARGS("s", session_id, "s", whom, "i", signal_number), |
| 3995 |
SD_BUS_NO_RESULT, |
| 3996 |
method_kill_session, |
| 3997 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 3998 |
SD_BUS_METHOD_WITH_ARGS("KillUser", |
| 3999 |
SD_BUS_ARGS("u", uid, "i", signal_number), |
| 4000 |
SD_BUS_NO_RESULT, |
| 4001 |
method_kill_user, |
| 4002 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4003 |
SD_BUS_METHOD_WITH_ARGS("TerminateSession", |
| 4004 |
SD_BUS_ARGS("s", session_id), |
| 4005 |
SD_BUS_NO_RESULT, |
| 4006 |
method_terminate_session, |
| 4007 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4008 |
SD_BUS_METHOD_WITH_ARGS("TerminateUser", |
| 4009 |
SD_BUS_ARGS("u", uid), |
| 4010 |
SD_BUS_NO_RESULT, |
| 4011 |
method_terminate_user, |
| 4012 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4013 |
SD_BUS_METHOD_WITH_ARGS("TerminateSeat", |
| 4014 |
SD_BUS_ARGS("s", seat_id), |
| 4015 |
SD_BUS_NO_RESULT, |
| 4016 |
method_terminate_seat, |
| 4017 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4018 |
SD_BUS_METHOD_WITH_ARGS("SetUserLinger", |
| 4019 |
SD_BUS_ARGS("u", uid, "b", enable, "b", interactive), |
| 4020 |
SD_BUS_NO_RESULT, |
| 4021 |
method_set_user_linger, |
| 4022 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4023 |
SD_BUS_METHOD_WITH_ARGS("AttachDevice", |
| 4024 |
SD_BUS_ARGS("s", seat_id, "s", sysfs_path, "b", interactive), |
| 4025 |
SD_BUS_NO_RESULT, |
| 4026 |
method_attach_device, |
| 4027 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4028 |
SD_BUS_METHOD_WITH_ARGS("FlushDevices", |
| 4029 |
SD_BUS_ARGS("b", interactive), |
| 4030 |
SD_BUS_NO_RESULT, |
| 4031 |
method_flush_devices, |
| 4032 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4033 |
SD_BUS_METHOD_WITH_ARGS("PowerOff", |
| 4034 |
SD_BUS_ARGS("b", interactive), |
| 4035 |
SD_BUS_NO_RESULT, |
| 4036 |
method_poweroff, |
| 4037 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4038 |
SD_BUS_METHOD_WITH_ARGS("PowerOffWithFlags", |
| 4039 |
SD_BUS_ARGS("t", flags), |
| 4040 |
SD_BUS_NO_RESULT, |
| 4041 |
method_poweroff, |
| 4042 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4043 |
SD_BUS_METHOD_WITH_ARGS("Reboot", |
| 4044 |
SD_BUS_ARGS("b", interactive), |
| 4045 |
SD_BUS_NO_RESULT, |
| 4046 |
method_reboot, |
| 4047 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4048 |
SD_BUS_METHOD_WITH_ARGS("RebootWithFlags", |
| 4049 |
SD_BUS_ARGS("t", flags), |
| 4050 |
SD_BUS_NO_RESULT, |
| 4051 |
method_reboot, |
| 4052 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4053 |
SD_BUS_METHOD_WITH_ARGS("Halt", |
| 4054 |
SD_BUS_ARGS("b", interactive), |
| 4055 |
SD_BUS_NO_RESULT, |
| 4056 |
method_halt, |
| 4057 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4058 |
SD_BUS_METHOD_WITH_ARGS("HaltWithFlags", |
| 4059 |
SD_BUS_ARGS("t", flags), |
| 4060 |
SD_BUS_NO_RESULT, |
| 4061 |
method_halt, |
| 4062 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4063 |
SD_BUS_METHOD_WITH_ARGS("Suspend", |
| 4064 |
SD_BUS_ARGS("b", interactive), |
| 4065 |
SD_BUS_NO_RESULT, |
| 4066 |
method_suspend, |
| 4067 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4068 |
SD_BUS_METHOD_WITH_ARGS("SuspendWithFlags", |
| 4069 |
SD_BUS_ARGS("t", flags), |
| 4070 |
SD_BUS_NO_RESULT, |
| 4071 |
method_suspend, |
| 4072 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4073 |
SD_BUS_METHOD_WITH_ARGS("Hibernate", |
| 4074 |
SD_BUS_ARGS("b", interactive), |
| 4075 |
SD_BUS_NO_RESULT, |
| 4076 |
method_hibernate, |
| 4077 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4078 |
SD_BUS_METHOD_WITH_ARGS("HibernateWithFlags", |
| 4079 |
SD_BUS_ARGS("t", flags), |
| 4080 |
SD_BUS_NO_RESULT, |
| 4081 |
method_hibernate, |
| 4082 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4083 |
SD_BUS_METHOD_WITH_ARGS("HybridSleep", |
| 4084 |
SD_BUS_ARGS("b", interactive), |
| 4085 |
SD_BUS_NO_RESULT, |
| 4086 |
method_hybrid_sleep, |
| 4087 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4088 |
SD_BUS_METHOD_WITH_ARGS("HybridSleepWithFlags", |
| 4089 |
SD_BUS_ARGS("t", flags), |
| 4090 |
SD_BUS_NO_RESULT, |
| 4091 |
method_hybrid_sleep, |
| 4092 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4093 |
SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernate", |
| 4094 |
SD_BUS_ARGS("b", interactive), |
| 4095 |
SD_BUS_NO_RESULT, |
| 4096 |
method_suspend_then_hibernate, |
| 4097 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4098 |
SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernateWithFlags", |
| 4099 |
SD_BUS_ARGS("t", flags), |
| 4100 |
SD_BUS_NO_RESULT, |
| 4101 |
method_suspend_then_hibernate, |
| 4102 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4103 |
SD_BUS_METHOD_WITH_ARGS("Sleep", |
| 4104 |
SD_BUS_ARGS("t", flags), |
| 4105 |
SD_BUS_NO_RESULT, |
| 4106 |
method_sleep, |
| 4107 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4108 |
SD_BUS_METHOD_WITH_ARGS("CanPowerOff", |
| 4109 |
SD_BUS_NO_ARGS, |
| 4110 |
SD_BUS_RESULT("s", result), |
| 4111 |
method_can_poweroff, |
| 4112 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4113 |
SD_BUS_METHOD_WITH_ARGS("CanReboot", |
| 4114 |
SD_BUS_NO_ARGS, |
| 4115 |
SD_BUS_RESULT("s", result), |
| 4116 |
method_can_reboot, |
| 4117 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4118 |
SD_BUS_METHOD_WITH_ARGS("CanHalt", |
| 4119 |
SD_BUS_NO_ARGS, |
| 4120 |
SD_BUS_RESULT("s", result), |
| 4121 |
method_can_halt, |
| 4122 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4123 |
SD_BUS_METHOD_WITH_ARGS("CanSuspend", |
| 4124 |
SD_BUS_NO_ARGS, |
| 4125 |
SD_BUS_RESULT("s", result), |
| 4126 |
method_can_suspend, |
| 4127 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4128 |
SD_BUS_METHOD_WITH_ARGS("CanHibernate", |
| 4129 |
SD_BUS_NO_ARGS, |
| 4130 |
SD_BUS_RESULT("s", result), |
| 4131 |
method_can_hibernate, |
| 4132 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4133 |
SD_BUS_METHOD_WITH_ARGS("CanHybridSleep", |
| 4134 |
SD_BUS_NO_ARGS, |
| 4135 |
SD_BUS_RESULT("s", result), |
| 4136 |
method_can_hybrid_sleep, |
| 4137 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4138 |
SD_BUS_METHOD_WITH_ARGS("CanSuspendThenHibernate", |
| 4139 |
SD_BUS_NO_ARGS, |
| 4140 |
SD_BUS_RESULT("s", result), |
| 4141 |
method_can_suspend_then_hibernate, |
| 4142 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4143 |
SD_BUS_METHOD_WITH_ARGS("CanSleep", |
| 4144 |
SD_BUS_NO_ARGS, |
| 4145 |
SD_BUS_RESULT("s", result), |
| 4146 |
method_can_sleep, |
| 4147 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4148 |
SD_BUS_METHOD_WITH_ARGS("ScheduleShutdown", |
| 4149 |
SD_BUS_ARGS("s", type, "t", usec), |
| 4150 |
SD_BUS_NO_RESULT, |
| 4151 |
method_schedule_shutdown, |
| 4152 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4153 |
SD_BUS_METHOD_WITH_ARGS("CancelScheduledShutdown", |
| 4154 |
SD_BUS_NO_ARGS, |
| 4155 |
SD_BUS_RESULT("b", cancelled), |
| 4156 |
method_cancel_scheduled_shutdown, |
| 4157 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4158 |
SD_BUS_METHOD_WITH_ARGS("Inhibit", |
| 4159 |
SD_BUS_ARGS("s", what, "s", who, "s", why, "s", mode), |
| 4160 |
SD_BUS_RESULT("h", pipe_fd), |
| 4161 |
method_inhibit, |
| 4162 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4163 |
SD_BUS_METHOD_WITH_ARGS("CanRebootParameter", |
| 4164 |
SD_BUS_NO_ARGS, |
| 4165 |
SD_BUS_RESULT("s", result), |
| 4166 |
method_can_reboot_parameter, |
| 4167 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4168 |
SD_BUS_METHOD_WITH_ARGS("SetRebootParameter", |
| 4169 |
SD_BUS_ARGS("s", parameter), |
| 4170 |
SD_BUS_NO_RESULT, |
| 4171 |
method_set_reboot_parameter, |
| 4172 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4173 |
SD_BUS_METHOD_WITH_ARGS("CanRebootToFirmwareSetup", |
| 4174 |
SD_BUS_NO_ARGS, |
| 4175 |
SD_BUS_RESULT("s", result), |
| 4176 |
method_can_reboot_to_firmware_setup, |
| 4177 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4178 |
SD_BUS_METHOD_WITH_ARGS("SetRebootToFirmwareSetup", |
| 4179 |
SD_BUS_ARGS("b", enable), |
| 4180 |
SD_BUS_NO_RESULT, |
| 4181 |
method_set_reboot_to_firmware_setup, |
| 4182 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4183 |
SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderMenu", |
| 4184 |
SD_BUS_NO_ARGS, |
| 4185 |
SD_BUS_RESULT("s", result), |
| 4186 |
method_can_reboot_to_boot_loader_menu, |
| 4187 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4188 |
SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderMenu", |
| 4189 |
SD_BUS_ARGS("t", timeout), |
| 4190 |
SD_BUS_NO_RESULT, |
| 4191 |
method_set_reboot_to_boot_loader_menu, |
| 4192 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4193 |
SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderEntry", |
| 4194 |
SD_BUS_NO_ARGS, |
| 4195 |
SD_BUS_RESULT("s", result), |
| 4196 |
method_can_reboot_to_boot_loader_entry, |
| 4197 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4198 |
SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderEntry", |
| 4199 |
SD_BUS_ARGS("s", boot_loader_entry), |
| 4200 |
SD_BUS_NO_RESULT, |
| 4201 |
method_set_reboot_to_boot_loader_entry, |
| 4202 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4203 |
SD_BUS_METHOD_WITH_ARGS("SetWallMessage", |
| 4204 |
SD_BUS_ARGS("s", wall_message, "b", enable), |
| 4205 |
SD_BUS_NO_RESULT, |
| 4206 |
method_set_wall_message, |
| 4207 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 4208 |
|
| 4209 |
SD_BUS_SIGNAL_WITH_ARGS("SecureAttentionKey", |
| 4210 |
SD_BUS_ARGS("s", seat_id, "o", object_path), |
| 4211 |
0), |
| 4212 |
SD_BUS_SIGNAL_WITH_ARGS("SessionNew", |
| 4213 |
SD_BUS_ARGS("s", session_id, "o", object_path), |
| 4214 |
0), |
| 4215 |
SD_BUS_SIGNAL_WITH_ARGS("SessionRemoved", |
| 4216 |
SD_BUS_ARGS("s", session_id, "o", object_path), |
| 4217 |
0), |
| 4218 |
SD_BUS_SIGNAL_WITH_ARGS("UserNew", |
| 4219 |
SD_BUS_ARGS("u", uid, "o", object_path), |
| 4220 |
0), |
| 4221 |
SD_BUS_SIGNAL_WITH_ARGS("UserRemoved", |
| 4222 |
SD_BUS_ARGS("u", uid, "o", object_path), |
| 4223 |
0), |
| 4224 |
SD_BUS_SIGNAL_WITH_ARGS("SeatNew", |
| 4225 |
SD_BUS_ARGS("s", seat_id, "o", object_path), |
| 4226 |
0), |
| 4227 |
SD_BUS_SIGNAL_WITH_ARGS("SeatRemoved", |
| 4228 |
SD_BUS_ARGS("s", seat_id, "o", object_path), |
| 4229 |
0), |
| 4230 |
SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdown", |
| 4231 |
SD_BUS_ARGS("b", start), |
| 4232 |
0), |
| 4233 |
SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdownWithMetadata", |
| 4234 |
SD_BUS_ARGS("b", start, "a{sv}", metadata), |
| 4235 |
0), |
| 4236 |
SD_BUS_SIGNAL_WITH_ARGS("PrepareForSleep", |
| 4237 |
SD_BUS_ARGS("b", start), |
| 4238 |
0), |
| 4239 |
|
| 4240 |
SD_BUS_VTABLE_END |
| 4241 |
}; |
| 4242 |
|
| 4243 |
const BusObjectImplementation manager_object = { |
| 4244 |
"/org/freedesktop/login1", |
| 4245 |
"org.freedesktop.login1.Manager", |
| 4246 |
.vtables = BUS_VTABLES(manager_vtable), |
| 4247 |
.children = BUS_IMPLEMENTATIONS(&seat_object, |
| 4248 |
&session_object, |
| 4249 |
&user_object), |
| 4250 |
}; |
| 4251 |
|
| 4252 |
static void session_jobs_reply(Session *s, uint32_t jid, const char *unit, const char *result) { |
| 4253 |
assert(s); |
| 4254 |
assert(unit); |
| 4255 |
|
| 4256 |
if (!s->started) |
| 4257 |
return; |
| 4258 |
|
| 4259 |
if (result && !streq(result, "done")) { |
| 4260 |
_cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; |
| 4261 |
|
| 4262 |
sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, |
| 4263 |
"Job %u for unit '%s' failed with '%s'", jid, unit, result); |
| 4264 |
|
| 4265 |
(void) session_send_create_reply(s, &e); |
| 4266 |
(void) session_send_upgrade_reply(s, &e); |
| 4267 |
return; |
| 4268 |
} |
| 4269 |
|
| 4270 |
(void) session_send_create_reply(s, /* error= */ NULL); |
| 4271 |
(void) session_send_upgrade_reply(s, /* error= */ NULL); |
| 4272 |
} |
| 4273 |
|
| 4274 |
int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 4275 |
Manager *m = ASSERT_PTR(userdata); |
| 4276 |
const char *path, *result, *unit; |
| 4277 |
uint32_t id; |
| 4278 |
int r; |
| 4279 |
|
| 4280 |
assert(message); |
| 4281 |
|
| 4282 |
r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result); |
| 4283 |
if (r < 0) { |
| 4284 |
bus_log_parse_error(r); |
| 4285 |
return 0; |
| 4286 |
} |
| 4287 |
|
| 4288 |
if (m->action_job && streq(m->action_job, path)) { |
| 4289 |
assert(m->delayed_action); |
| 4290 |
log_info("Operation '%s' finished.", handle_action_to_string(m->delayed_action->handle)); |
| 4291 |
|
| 4292 |
/* Tell people that they now may take a lock again */ |
| 4293 |
(void) send_prepare_for(m, m->delayed_action, false); |
| 4294 |
|
| 4295 |
m->action_job = mfree(m->action_job); |
| 4296 |
m->delayed_action = NULL; |
| 4297 |
return 0; |
| 4298 |
} |
| 4299 |
|
| 4300 |
Session *session; |
| 4301 |
User *user; |
| 4302 |
|
| 4303 |
session = hashmap_get(m->session_units, unit); |
| 4304 |
if (session) { |
| 4305 |
if (streq_ptr(path, session->scope_job)) { |
| 4306 |
session->scope_job = mfree(session->scope_job); |
| 4307 |
session_jobs_reply(session, id, unit, result); |
| 4308 |
|
| 4309 |
session_save(session); |
| 4310 |
user_save(session->user); |
| 4311 |
} |
| 4312 |
|
| 4313 |
session_add_to_gc_queue(session); |
| 4314 |
} |
| 4315 |
|
| 4316 |
user = hashmap_get(m->user_units, unit); |
| 4317 |
if (user) { |
| 4318 |
/* If the user is stopping, we're tracking stop jobs here. So don't send reply. */ |
| 4319 |
if (!user->stopping) { |
| 4320 |
char **user_job; |
| 4321 |
FOREACH_ARGUMENT(user_job, &user->runtime_dir_job, &user->service_manager_job, &user->measure_job) |
| 4322 |
if (streq_ptr(path, *user_job)) { |
| 4323 |
*user_job = mfree(*user_job); |
| 4324 |
|
| 4325 |
LIST_FOREACH(sessions_by_user, s, user->sessions) |
| 4326 |
/* Don't propagate user service failures to the client */ |
| 4327 |
session_jobs_reply(s, id, unit, /* result= */ NULL); |
| 4328 |
|
| 4329 |
user_save(user); |
| 4330 |
break; |
| 4331 |
} |
| 4332 |
} |
| 4333 |
|
| 4334 |
user_add_to_gc_queue(user); |
| 4335 |
} |
| 4336 |
|
| 4337 |
return 0; |
| 4338 |
} |
| 4339 |
|
| 4340 |
int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 4341 |
const char *path, *unit; |
| 4342 |
Manager *m = ASSERT_PTR(userdata); |
| 4343 |
Session *session; |
| 4344 |
User *user; |
| 4345 |
int r; |
| 4346 |
|
| 4347 |
assert(message); |
| 4348 |
|
| 4349 |
r = sd_bus_message_read(message, "so", &unit, &path); |
| 4350 |
if (r < 0) { |
| 4351 |
bus_log_parse_error(r); |
| 4352 |
return 0; |
| 4353 |
} |
| 4354 |
|
| 4355 |
session = hashmap_get(m->session_units, unit); |
| 4356 |
if (session) |
| 4357 |
session_add_to_gc_queue(session); |
| 4358 |
|
| 4359 |
user = hashmap_get(m->user_units, unit); |
| 4360 |
if (user) |
| 4361 |
user_add_to_gc_queue(user); |
| 4362 |
|
| 4363 |
return 0; |
| 4364 |
} |
| 4365 |
|
| 4366 |
int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 4367 |
_cleanup_free_ char *unit = NULL; |
| 4368 |
Manager *m = ASSERT_PTR(userdata); |
| 4369 |
const char *path; |
| 4370 |
Session *session; |
| 4371 |
User *user; |
| 4372 |
int r; |
| 4373 |
|
| 4374 |
assert(message); |
| 4375 |
|
| 4376 |
path = sd_bus_message_get_path(message); |
| 4377 |
if (!path) |
| 4378 |
return 0; |
| 4379 |
|
| 4380 |
r = unit_name_from_dbus_path(path, &unit); |
| 4381 |
if (r == -EINVAL) /* not a unit */ |
| 4382 |
return 0; |
| 4383 |
if (r < 0) { |
| 4384 |
log_oom(); |
| 4385 |
return 0; |
| 4386 |
} |
| 4387 |
|
| 4388 |
session = hashmap_get(m->session_units, unit); |
| 4389 |
if (session) |
| 4390 |
session_add_to_gc_queue(session); |
| 4391 |
|
| 4392 |
user = hashmap_get(m->user_units, unit); |
| 4393 |
if (user) |
| 4394 |
user_add_to_gc_queue(user); |
| 4395 |
|
| 4396 |
return 0; |
| 4397 |
} |
| 4398 |
|
| 4399 |
int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 4400 |
Manager *m = ASSERT_PTR(userdata); |
| 4401 |
Session *session; |
| 4402 |
int b, r; |
| 4403 |
|
| 4404 |
assert(message); |
| 4405 |
|
| 4406 |
r = sd_bus_message_read(message, "b", &b); |
| 4407 |
if (r < 0) { |
| 4408 |
bus_log_parse_error(r); |
| 4409 |
return 0; |
| 4410 |
} |
| 4411 |
|
| 4412 |
if (b) |
| 4413 |
return 0; |
| 4414 |
|
| 4415 |
/* systemd finished reloading, let's recheck all our sessions */ |
| 4416 |
log_debug("System manager has been reloaded, rechecking sessions..."); |
| 4417 |
|
| 4418 |
HASHMAP_FOREACH(session, m->sessions) |
| 4419 |
session_add_to_gc_queue(session); |
| 4420 |
|
| 4421 |
return 0; |
| 4422 |
} |
| 4423 |
|
| 4424 |
int manager_send_changed_strv(Manager *manager, char **properties) { |
| 4425 |
assert(manager); |
| 4426 |
|
| 4427 |
return sd_bus_emit_properties_changed_strv( |
| 4428 |
manager->bus, |
| 4429 |
"/org/freedesktop/login1", |
| 4430 |
"org.freedesktop.login1.Manager", |
| 4431 |
properties); |
| 4432 |
} |
| 4433 |
|
| 4434 |
int manager_start_scope( |
| 4435 |
Manager *manager, |
| 4436 |
const char *scope, |
| 4437 |
const PidRef *pidref, |
| 4438 |
bool allow_pidfd, |
| 4439 |
const char *slice, |
| 4440 |
const char *description, |
| 4441 |
const char * const *requires, |
| 4442 |
const char * const *wants, |
| 4443 |
const char * const *extra_after, |
| 4444 |
const char *requires_mounts_for, |
| 4445 |
sd_bus_message *more_properties, |
| 4446 |
sd_bus_error *error, |
| 4447 |
char **ret_job) { |
| 4448 |
|
| 4449 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
| 4450 |
_cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; |
| 4451 |
int r; |
| 4452 |
|
| 4453 |
assert(manager); |
| 4454 |
assert(scope); |
| 4455 |
assert(pidref_is_set(pidref)); |
| 4456 |
assert(ret_job); |
| 4457 |
|
| 4458 |
r = bus_message_new_method_call(manager->bus, &m, bus_systemd_mgr, "StartTransientUnit"); |
| 4459 |
if (r < 0) |
| 4460 |
return r; |
| 4461 |
|
| 4462 |
r = sd_bus_message_append(m, "ss", scope, "fail"); |
| 4463 |
if (r < 0) |
| 4464 |
return r; |
| 4465 |
|
| 4466 |
r = sd_bus_message_open_container(m, 'a', "(sv)"); |
| 4467 |
if (r < 0) |
| 4468 |
return r; |
| 4469 |
|
| 4470 |
if (!isempty(slice)) { |
| 4471 |
r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice); |
| 4472 |
if (r < 0) |
| 4473 |
return r; |
| 4474 |
} |
| 4475 |
|
| 4476 |
if (!isempty(description)) { |
| 4477 |
r = sd_bus_message_append(m, "(sv)", "Description", "s", description); |
| 4478 |
if (r < 0) |
| 4479 |
return r; |
| 4480 |
} |
| 4481 |
|
| 4482 |
STRV_FOREACH(i, requires) { |
| 4483 |
r = sd_bus_message_append(m, "(sv)", "Requires", "as", 1, *i); |
| 4484 |
if (r < 0) |
| 4485 |
return r; |
| 4486 |
|
| 4487 |
r = sd_bus_message_append(m, "(sv)", "After", "as", 1, *i); |
| 4488 |
if (r < 0) |
| 4489 |
return r; |
| 4490 |
} |
| 4491 |
|
| 4492 |
STRV_FOREACH(i, wants) { |
| 4493 |
r = sd_bus_message_append(m, "(sv)", "Wants", "as", 1, *i); |
| 4494 |
if (r < 0) |
| 4495 |
return r; |
| 4496 |
|
| 4497 |
r = sd_bus_message_append(m, "(sv)", "After", "as", 1, *i); |
| 4498 |
if (r < 0) |
| 4499 |
return r; |
| 4500 |
} |
| 4501 |
|
| 4502 |
STRV_FOREACH(i, extra_after) { |
| 4503 |
r = sd_bus_message_append(m, "(sv)", "After", "as", 1, *i); |
| 4504 |
if (r < 0) |
| 4505 |
return r; |
| 4506 |
} |
| 4507 |
|
| 4508 |
if (!empty_or_root(requires_mounts_for)) { |
| 4509 |
r = sd_bus_message_append(m, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for); |
| 4510 |
if (r < 0) |
| 4511 |
return r; |
| 4512 |
} |
| 4513 |
|
| 4514 |
/* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore |
| 4515 |
* SIGTERM */ |
| 4516 |
r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true); |
| 4517 |
if (r < 0) |
| 4518 |
return r; |
| 4519 |
|
| 4520 |
r = bus_append_scope_pidref(m, pidref, allow_pidfd); |
| 4521 |
if (r < 0) |
| 4522 |
return r; |
| 4523 |
|
| 4524 |
/* For login session scopes, if a process is OOM killed by the kernel, *don't* terminate the rest of |
| 4525 |
the scope */ |
| 4526 |
r = sd_bus_message_append(m, "(sv)", "OOMPolicy", "s", "continue"); |
| 4527 |
if (r < 0) |
| 4528 |
return r; |
| 4529 |
|
| 4530 |
/* disable TasksMax= for the session scope, rely on the slice setting for it */ |
| 4531 |
r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", UINT64_MAX); |
| 4532 |
if (r < 0) |
| 4533 |
return bus_log_create_error(r); |
| 4534 |
|
| 4535 |
if (more_properties) { |
| 4536 |
/* If TasksMax also appears here, it will overwrite the default value set above */ |
| 4537 |
r = sd_bus_message_copy(m, more_properties, true); |
| 4538 |
if (r < 0) |
| 4539 |
return r; |
| 4540 |
} |
| 4541 |
|
| 4542 |
r = sd_bus_message_close_container(m); |
| 4543 |
if (r < 0) |
| 4544 |
return r; |
| 4545 |
|
| 4546 |
r = sd_bus_message_append(m, "a(sa(sv))", 0); |
| 4547 |
if (r < 0) |
| 4548 |
return r; |
| 4549 |
|
| 4550 |
r = sd_bus_call(manager->bus, m, 0, &e, &reply); |
| 4551 |
if (r < 0) { |
| 4552 |
/* If this failed with a property we couldn't write, this is quite likely because the server |
| 4553 |
* doesn't support PIDFDs yet, let's try without. */ |
| 4554 |
if (allow_pidfd && |
| 4555 |
sd_bus_error_has_names(&e, SD_BUS_ERROR_UNKNOWN_PROPERTY, SD_BUS_ERROR_PROPERTY_READ_ONLY)) |
| 4556 |
return manager_start_scope( |
| 4557 |
manager, |
| 4558 |
scope, |
| 4559 |
pidref, |
| 4560 |
/* allow_pidfd= */ false, |
| 4561 |
slice, |
| 4562 |
description, |
| 4563 |
requires, |
| 4564 |
wants, |
| 4565 |
extra_after, |
| 4566 |
requires_mounts_for, |
| 4567 |
more_properties, |
| 4568 |
error, |
| 4569 |
ret_job); |
| 4570 |
|
| 4571 |
return sd_bus_error_move(error, &e); |
| 4572 |
} |
| 4573 |
|
| 4574 |
return strdup_job(reply, ret_job); |
| 4575 |
} |
| 4576 |
|
| 4577 |
int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **ret_job) { |
| 4578 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 4579 |
int r; |
| 4580 |
|
| 4581 |
assert(manager); |
| 4582 |
assert(unit); |
| 4583 |
assert(ret_job); |
| 4584 |
|
| 4585 |
r = bus_call_method( |
| 4586 |
manager->bus, |
| 4587 |
bus_systemd_mgr, |
| 4588 |
"StartUnit", |
| 4589 |
error, |
| 4590 |
&reply, |
| 4591 |
"ss", unit, "replace"); |
| 4592 |
if (r < 0) |
| 4593 |
return r; |
| 4594 |
|
| 4595 |
return strdup_job(reply, ret_job); |
| 4596 |
} |
| 4597 |
|
| 4598 |
int manager_stop_unit( |
| 4599 |
Manager *manager, |
| 4600 |
const char *unit, |
| 4601 |
const char *job_mode, |
| 4602 |
sd_bus_error *ret_error, |
| 4603 |
char **ret_job) { |
| 4604 |
|
| 4605 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 4606 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 4607 |
int r; |
| 4608 |
|
| 4609 |
assert(manager); |
| 4610 |
assert(unit); |
| 4611 |
assert(ret_job); |
| 4612 |
|
| 4613 |
r = bus_call_method( |
| 4614 |
manager->bus, |
| 4615 |
bus_systemd_mgr, |
| 4616 |
"StopUnit", |
| 4617 |
&error, |
| 4618 |
&reply, |
| 4619 |
"ss", unit, job_mode ?: "fail"); |
| 4620 |
if (r < 0) { |
| 4621 |
if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT, BUS_ERROR_LOAD_FAILED)) { |
| 4622 |
*ret_job = NULL; |
| 4623 |
return 0; |
| 4624 |
} |
| 4625 |
|
| 4626 |
sd_bus_error_move(ret_error, &error); |
| 4627 |
return r; |
| 4628 |
} |
| 4629 |
|
| 4630 |
r = strdup_job(reply, ret_job); |
| 4631 |
if (r < 0) |
| 4632 |
return r; |
| 4633 |
|
| 4634 |
return 1; |
| 4635 |
} |
| 4636 |
|
| 4637 |
int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *ret_error) { |
| 4638 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 4639 |
_cleanup_free_ char *path = NULL; |
| 4640 |
int r; |
| 4641 |
|
| 4642 |
assert(manager); |
| 4643 |
assert(scope); |
| 4644 |
|
| 4645 |
path = unit_dbus_path_from_name(scope); |
| 4646 |
if (!path) |
| 4647 |
return -ENOMEM; |
| 4648 |
|
| 4649 |
r = sd_bus_call_method( |
| 4650 |
manager->bus, |
| 4651 |
"org.freedesktop.systemd1", |
| 4652 |
path, |
| 4653 |
"org.freedesktop.systemd1.Scope", |
| 4654 |
"Abandon", |
| 4655 |
&error, |
| 4656 |
NULL, |
| 4657 |
NULL); |
| 4658 |
if (r < 0) { |
| 4659 |
if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT, |
| 4660 |
BUS_ERROR_LOAD_FAILED, |
| 4661 |
BUS_ERROR_SCOPE_NOT_RUNNING)) |
| 4662 |
return 0; |
| 4663 |
|
| 4664 |
sd_bus_error_move(ret_error, &error); |
| 4665 |
return r; |
| 4666 |
} |
| 4667 |
|
| 4668 |
return 1; |
| 4669 |
} |
| 4670 |
|
| 4671 |
int manager_kill_unit(Manager *manager, const char *unit, KillWhom whom, int signo, sd_bus_error *error) { |
| 4672 |
assert(manager); |
| 4673 |
assert(unit); |
| 4674 |
assert(SIGNAL_VALID(signo)); |
| 4675 |
|
| 4676 |
return bus_call_method( |
| 4677 |
manager->bus, |
| 4678 |
bus_systemd_mgr, |
| 4679 |
"KillUnit", |
| 4680 |
error, |
| 4681 |
NULL, |
| 4682 |
"ssi", |
| 4683 |
unit, |
| 4684 |
whom == KILL_LEADER ? "main" : "all", |
| 4685 |
signo); |
| 4686 |
} |
| 4687 |
|
| 4688 |
int manager_unit_is_active(Manager *manager, const char *unit, sd_bus_error *ret_error) { |
| 4689 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 4690 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 4691 |
_cleanup_free_ char *path = NULL; |
| 4692 |
const char *state; |
| 4693 |
int r; |
| 4694 |
|
| 4695 |
assert(manager); |
| 4696 |
assert(unit); |
| 4697 |
|
| 4698 |
path = unit_dbus_path_from_name(unit); |
| 4699 |
if (!path) |
| 4700 |
return -ENOMEM; |
| 4701 |
|
| 4702 |
r = sd_bus_get_property( |
| 4703 |
manager->bus, |
| 4704 |
"org.freedesktop.systemd1", |
| 4705 |
path, |
| 4706 |
"org.freedesktop.systemd1.Unit", |
| 4707 |
"ActiveState", |
| 4708 |
&error, |
| 4709 |
&reply, |
| 4710 |
"s"); |
| 4711 |
if (r < 0) { |
| 4712 |
/* systemd might have dropped off momentarily, let's |
| 4713 |
* not make this an error */ |
| 4714 |
if (bus_error_is_connection(&error)) |
| 4715 |
return true; |
| 4716 |
|
| 4717 |
/* If the unit is already unloaded then it's not |
| 4718 |
* active */ |
| 4719 |
if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT, |
| 4720 |
BUS_ERROR_LOAD_FAILED)) |
| 4721 |
return false; |
| 4722 |
|
| 4723 |
sd_bus_error_move(ret_error, &error); |
| 4724 |
return r; |
| 4725 |
} |
| 4726 |
|
| 4727 |
r = sd_bus_message_read(reply, "s", &state); |
| 4728 |
if (r < 0) |
| 4729 |
return r; |
| 4730 |
|
| 4731 |
return !STR_IN_SET(state, "inactive", "failed"); |
| 4732 |
} |
| 4733 |
|
| 4734 |
int manager_job_is_active(Manager *manager, const char *path, sd_bus_error *ret_error) { |
| 4735 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 4736 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 4737 |
int r; |
| 4738 |
|
| 4739 |
assert(manager); |
| 4740 |
assert(path); |
| 4741 |
|
| 4742 |
r = sd_bus_get_property( |
| 4743 |
manager->bus, |
| 4744 |
"org.freedesktop.systemd1", |
| 4745 |
path, |
| 4746 |
"org.freedesktop.systemd1.Job", |
| 4747 |
"State", |
| 4748 |
&error, |
| 4749 |
&reply, |
| 4750 |
"s"); |
| 4751 |
if (r < 0) { |
| 4752 |
if (bus_error_is_connection(&error)) |
| 4753 |
return true; |
| 4754 |
|
| 4755 |
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) |
| 4756 |
return false; |
| 4757 |
|
| 4758 |
sd_bus_error_move(ret_error, &error); |
| 4759 |
return r; |
| 4760 |
} |
| 4761 |
|
| 4762 |
/* We don't actually care about the state really. The fact |
| 4763 |
* that we could read the job state is enough for us */ |
| 4764 |
|
| 4765 |
return true; |
| 4766 |
} |