logind.c 46.6 KB
Newer Older
1 2 3 4 5 6 7 8
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2011 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
9 10
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
11 12 13 14 15
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
  Lesser General Public License for more details.
17

18
  You should have received a copy of the GNU Lesser General Public License
19 20 21 22 23 24 25 26 27 28
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <errno.h>
#include <pwd.h>
#include <libudev.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/epoll.h>
29 30
#include <sys/ioctl.h>
#include <linux/vt.h>
31
#include <sys/timerfd.h>
32

33 34
#include <systemd/sd-daemon.h>

35 36 37
#include "logind.h"
#include "dbus-common.h"
#include "dbus-loop.h"
38 39
#include "strv.h"
#include "conf-parser.h"
40
#include "mkdir.h"
41 42 43 44 45 46 47 48 49 50

Manager *manager_new(void) {
        Manager *m;

        m = new0(Manager, 1);
        if (!m)
                return NULL;

        m->console_active_fd = -1;
        m->bus_fd = -1;
51 52
        m->udev_seat_fd = -1;
        m->udev_vcsa_fd = -1;
53
        m->udev_button_fd = -1;
54
        m->epoll_fd = -1;
55
        m->reserve_vt_fd = -1;
56

57
        m->n_autovts = 6;
58
        m->reserve_vt = 6;
59
        m->inhibit_delay_max = 5 * USEC_PER_SEC;
60
        m->handle_power_key = HANDLE_POWEROFF;
61 62
        m->handle_suspend_key = HANDLE_SUSPEND;
        m->handle_hibernate_key = HANDLE_HIBERNATE;
63 64
        m->handle_lid_switch = HANDLE_SUSPEND;
        m->lid_switch_ignore_inhibited = true;
65

66 67 68 69 70
        m->idle_action_fd = -1;
        m->idle_action_usec = 30 * USEC_PER_MINUTE;
        m->idle_action = HANDLE_IGNORE;
        m->idle_action_not_before_usec = now(CLOCK_MONOTONIC);

71 72 73 74
        m->devices = hashmap_new(string_hash_func, string_compare_func);
        m->seats = hashmap_new(string_hash_func, string_compare_func);
        m->sessions = hashmap_new(string_hash_func, string_compare_func);
        m->users = hashmap_new(trivial_hash_func, trivial_compare_func);
75
        m->inhibitors = hashmap_new(string_hash_func, string_compare_func);
76
        m->buttons = hashmap_new(string_hash_func, string_compare_func);
77

78 79 80
        m->user_cgroups = hashmap_new(string_hash_func, string_compare_func);
        m->session_cgroups = hashmap_new(string_hash_func, string_compare_func);

81 82
        m->session_fds = hashmap_new(trivial_hash_func, trivial_compare_func);
        m->inhibitor_fds = hashmap_new(trivial_hash_func, trivial_compare_func);
83
        m->button_fds = hashmap_new(trivial_hash_func, trivial_compare_func);
84

85
        if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons ||
86 87
            !m->user_cgroups || !m->session_cgroups ||
            !m->session_fds || !m->inhibitor_fds || !m->button_fds) {
88 89 90 91 92 93 94
                manager_free(m);
                return NULL;
        }

        m->reset_controllers = strv_new("cpu", NULL);
        m->kill_exclude_users = strv_new("root", NULL);
        if (!m->reset_controllers || !m->kill_exclude_users) {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
                manager_free(m);
                return NULL;
        }

        m->udev = udev_new();
        if (!m->udev) {
                manager_free(m);
                return NULL;
        }

        if (cg_get_user_path(&m->cgroup_path) < 0) {
                manager_free(m);
                return NULL;
        }

        return m;
}

void manager_free(Manager *m) {
        Session *session;
        User *u;
        Device *d;
        Seat *s;
118
        Inhibitor *i;
119
        Button *b;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

        assert(m);

        while ((session = hashmap_first(m->sessions)))
                session_free(session);

        while ((u = hashmap_first(m->users)))
                user_free(u);

        while ((d = hashmap_first(m->devices)))
                device_free(d);

        while ((s = hashmap_first(m->seats)))
                seat_free(s);

135 136 137
        while ((i = hashmap_first(m->inhibitors)))
                inhibitor_free(i);

138 139 140
        while ((b = hashmap_first(m->buttons)))
                button_free(b);

141 142
        hashmap_free(m->devices);
        hashmap_free(m->seats);
143 144 145
        hashmap_free(m->sessions);
        hashmap_free(m->users);
        hashmap_free(m->inhibitors);
146
        hashmap_free(m->buttons);
147

148 149 150
        hashmap_free(m->user_cgroups);
        hashmap_free(m->session_cgroups);

151 152
        hashmap_free(m->session_fds);
        hashmap_free(m->inhibitor_fds);
153
        hashmap_free(m->button_fds);
154 155 156 157

        if (m->console_active_fd >= 0)
                close_nointr_nofail(m->console_active_fd);

158 159 160 161
        if (m->udev_seat_monitor)
                udev_monitor_unref(m->udev_seat_monitor);
        if (m->udev_vcsa_monitor)
                udev_monitor_unref(m->udev_vcsa_monitor);
162 163
        if (m->udev_button_monitor)
                udev_monitor_unref(m->udev_button_monitor);
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

        if (m->udev)
                udev_unref(m->udev);

        if (m->bus) {
                dbus_connection_flush(m->bus);
                dbus_connection_close(m->bus);
                dbus_connection_unref(m->bus);
        }

        if (m->bus_fd >= 0)
                close_nointr_nofail(m->bus_fd);

        if (m->epoll_fd >= 0)
                close_nointr_nofail(m->epoll_fd);

180 181 182
        if (m->reserve_vt_fd >= 0)
                close_nointr_nofail(m->reserve_vt_fd);

183 184 185
        if (m->idle_action_fd >= 0)
                close_nointr_nofail(m->idle_action_fd);

186 187 188 189 190
        strv_free(m->controllers);
        strv_free(m->reset_controllers);
        strv_free(m->kill_only_users);
        strv_free(m->kill_exclude_users);

191 192
        free(m->action_job);

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
        free(m->cgroup_path);
        free(m);
}

int manager_add_device(Manager *m, const char *sysfs, Device **_device) {
        Device *d;

        assert(m);
        assert(sysfs);

        d = hashmap_get(m->devices, sysfs);
        if (d) {
                if (_device)
                        *_device = d;

                return 0;
        }

        d = device_new(m, sysfs);
        if (!d)
                return -ENOMEM;

        if (_device)
                *_device = d;

        return 0;
}

int manager_add_seat(Manager *m, const char *id, Seat **_seat) {
        Seat *s;

        assert(m);
        assert(id);

        s = hashmap_get(m->seats, id);
        if (s) {
                if (_seat)
                        *_seat = s;

                return 0;
        }

        s = seat_new(m, id);
        if (!s)
                return -ENOMEM;

        if (_seat)
                *_seat = s;

        return 0;
}

int manager_add_session(Manager *m, User *u, const char *id, Session **_session) {
        Session *s;

        assert(m);
        assert(id);

        s = hashmap_get(m->sessions, id);
        if (s) {
                if (_session)
                        *_session = s;

                return 0;
        }

        s = session_new(m, u, id);
        if (!s)
                return -ENOMEM;

        if (_session)
                *_session = s;

        return 0;
}

int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user) {
        User *u;

        assert(m);
        assert(name);

        u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
        if (u) {
                if (_user)
                        *_user = u;

                return 0;
        }

        u = user_new(m, uid, gid, name);
        if (!u)
                return -ENOMEM;

Lennart Poettering's avatar
Lennart Poettering committed
287 288
        if (_user)
                *_user = u;
289

Lennart Poettering's avatar
Lennart Poettering committed
290
        return 0;
291 292 293
}

int manager_add_user_by_name(Manager *m, const char *name, User **_user) {
294 295 296
        uid_t uid;
        gid_t gid;
        int r;
297 298 299 300

        assert(m);
        assert(name);

301
        r = get_user_creds(&name, &uid, &gid, NULL, NULL);
302 303
        if (r < 0)
                return r;
304

305
        return manager_add_user(m, uid, gid, name, _user);
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
}

int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) {
        struct passwd *p;

        assert(m);

        errno = 0;
        p = getpwuid(uid);
        if (!p)
                return errno ? -errno : -ENOENT;

        return manager_add_user(m, uid, p->pw_gid, p->pw_name, _user);
}

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor) {
        Inhibitor *i;

        assert(m);
        assert(id);

        i = hashmap_get(m->inhibitors, id);
        if (i) {
                if (_inhibitor)
                        *_inhibitor = i;

                return 0;
        }

        i = inhibitor_new(m, id);
        if (!i)
                return -ENOMEM;

        if (_inhibitor)
                *_inhibitor = i;

        return 0;
}

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
int manager_add_button(Manager *m, const char *name, Button **_button) {
        Button *b;

        assert(m);
        assert(name);

        b = hashmap_get(m->buttons, name);
        if (b) {
                if (_button)
                        *_button = b;

                return 0;
        }

        b = button_new(m, name);
        if (!b)
                return -ENOMEM;

        if (_button)
                *_button = b;

        return 0;
}

369
int manager_process_seat_device(Manager *m, struct udev_device *d) {
370 371 372 373 374
        Device *device;
        int r;

        assert(m);

Lennart Poettering's avatar
Lennart Poettering committed
375
        if (streq_ptr(udev_device_get_action(d), "remove")) {
376

377
                device = hashmap_get(m->devices, udev_device_get_syspath(d));
Lennart Poettering's avatar
Lennart Poettering committed
378 379
                if (!device)
                        return 0;
380

Lennart Poettering's avatar
Lennart Poettering committed
381 382 383 384 385 386 387
                seat_add_to_gc_queue(device->seat);
                device_free(device);

        } else {
                const char *sn;
                Seat *seat;

388
                sn = udev_device_get_property_value(d, "ID_SEAT");
389
                if (isempty(sn))
Lennart Poettering's avatar
Lennart Poettering committed
390 391
                        sn = "seat0";

392 393 394 395
                if (!seat_name_is_valid(sn)) {
                        log_warning("Device with invalid seat name %s found, ignoring.", sn);
                        return 0;
                }
Lennart Poettering's avatar
Lennart Poettering committed
396

397
                r = manager_add_device(m, udev_device_get_syspath(d), &device);
Lennart Poettering's avatar
Lennart Poettering committed
398 399 400 401 402 403 404 405 406 407 408 409
                if (r < 0)
                        return r;

                r = manager_add_seat(m, sn, &seat);
                if (r < 0) {
                        if (!device->seat)
                                device_free(device);

                        return r;
                }

                device_attach(device, seat);
410
                seat_start(seat);
Lennart Poettering's avatar
Lennart Poettering committed
411
        }
412 413 414 415

        return 0;
}

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
int manager_process_button_device(Manager *m, struct udev_device *d) {
        Button *b;

        int r;

        assert(m);

        if (streq_ptr(udev_device_get_action(d), "remove")) {

                b = hashmap_get(m->buttons, udev_device_get_sysname(d));
                if (!b)
                        return 0;

                button_free(b);

        } else {
                const char *sn;

                r = manager_add_button(m, udev_device_get_sysname(d), &b);
                if (r < 0)
                        return r;

                sn = udev_device_get_property_value(d, "ID_SEAT");
                if (isempty(sn))
                        sn = "seat0";

                button_set_seat(b, sn);
                button_open(b);
        }

        return 0;
}

449 450 451
int manager_enumerate_devices(Manager *m) {
        struct udev_list_entry *item = NULL, *first = NULL;
        struct udev_enumerate *e;
Lennart Poettering's avatar
Lennart Poettering committed
452
        int r;
453 454 455 456 457 458 459

        assert(m);

        /* Loads devices from udev and creates seats for them as
         * necessary */

        e = udev_enumerate_new(m->udev);
Lennart Poettering's avatar
Lennart Poettering committed
460 461
        if (!e) {
                r = -ENOMEM;
462
                goto finish;
Lennart Poettering's avatar
Lennart Poettering committed
463
        }
464

465
        r = udev_enumerate_add_match_tag(e, "master-of-seat");
Lennart Poettering's avatar
Lennart Poettering committed
466
        if (r < 0)
467 468
                goto finish;

Lennart Poettering's avatar
Lennart Poettering committed
469 470
        r = udev_enumerate_scan_devices(e);
        if (r < 0)
471 472 473 474 475 476 477 478
                goto finish;

        first = udev_enumerate_get_list_entry(e);
        udev_list_entry_foreach(item, first) {
                struct udev_device *d;
                int k;

                d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
Lennart Poettering's avatar
Lennart Poettering committed
479 480
                if (!d) {
                        r = -ENOMEM;
481
                        goto finish;
Lennart Poettering's avatar
Lennart Poettering committed
482
                }
483

484
                k = manager_process_seat_device(m, d);
485 486 487 488 489 490 491 492 493 494 495 496 497
                udev_device_unref(d);

                if (k < 0)
                        r = k;
        }

finish:
        if (e)
                udev_enumerate_unref(e);

        return r;
}

498 499 500 501 502 503 504 505 506
int manager_enumerate_buttons(Manager *m) {
        struct udev_list_entry *item = NULL, *first = NULL;
        struct udev_enumerate *e;
        int r;

        assert(m);

        /* Loads buttons from udev */

507
        if (m->handle_power_key == HANDLE_IGNORE &&
508 509
            m->handle_suspend_key == HANDLE_IGNORE &&
            m->handle_hibernate_key == HANDLE_IGNORE &&
510
            m->handle_lid_switch == HANDLE_IGNORE)
511 512
                return 0;

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
        e = udev_enumerate_new(m->udev);
        if (!e) {
                r = -ENOMEM;
                goto finish;
        }

        r = udev_enumerate_add_match_subsystem(e, "input");
        if (r < 0)
                goto finish;

        r = udev_enumerate_add_match_tag(e, "power-switch");
        if (r < 0)
                goto finish;

        r = udev_enumerate_scan_devices(e);
        if (r < 0)
                goto finish;

        first = udev_enumerate_get_list_entry(e);
        udev_list_entry_foreach(item, first) {
                struct udev_device *d;
                int k;

                d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
                if (!d) {
                        r = -ENOMEM;
                        goto finish;
                }

                k = manager_process_button_device(m, d);
                udev_device_unref(d);

                if (k < 0)
                        r = k;
        }

finish:
        if (e)
                udev_enumerate_unref(e);

        return r;
}

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
int manager_enumerate_seats(Manager *m) {
        DIR *d;
        struct dirent *de;
        int r = 0;

        assert(m);

        /* This loads data about seats stored on disk, but does not
         * actually create any seats. Removes data of seats that no
         * longer exist. */

        d = opendir("/run/systemd/seats");
        if (!d) {
                if (errno == ENOENT)
                        return 0;

                log_error("Failed to open /run/systemd/seats: %m");
                return -errno;
        }

        while ((de = readdir(d))) {
                Seat *s;
                int k;

                if (!dirent_is_file(de))
                        continue;

                s = hashmap_get(m->seats, de->d_name);
                if (!s) {
                        unlinkat(dirfd(d), de->d_name, 0);
                        continue;
                }

                k = seat_load(s);
                if (k < 0)
                        r = k;
        }

        closedir(d);

        return r;
}

static int manager_enumerate_users_from_cgroup(Manager *m) {
600
        int r = 0, k;
601 602 603 604 605 606 607 608 609 610 611 612
        char *name;
        DIR *d;

        r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_path, &d);
        if (r < 0) {
                if (r == -ENOENT)
                        return 0;

                log_error("Failed to open %s: %s", m->cgroup_path, strerror(-r));
                return r;
        }

613
        while ((k = cg_read_subgroup(d, &name)) > 0) {
614
                User *user;
Lennart Poettering's avatar
Lennart Poettering committed
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630

                k = manager_add_user_by_name(m, name, &user);
                if (k < 0) {
                        free(name);
                        r = k;
                        continue;
                }

                user_add_to_gc_queue(user);

                if (!user->cgroup_path)
                        if (asprintf(&user->cgroup_path, "%s/%s", m->cgroup_path, name) < 0) {
                                r = -ENOMEM;
                                free(name);
                                break;
                        }
631 632

                free(name);
Lennart Poettering's avatar
Lennart Poettering committed
633 634
        }

635 636 637
        if (r >= 0 && k < 0)
                r = k;

Lennart Poettering's avatar
Lennart Poettering committed
638 639 640 641
        closedir(d);

        return r;
}
642

Lennart Poettering's avatar
Lennart Poettering committed
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
static int manager_enumerate_linger_users(Manager *m) {
        DIR *d;
        struct dirent *de;
        int r = 0;

        d = opendir("/var/lib/systemd/linger");
        if (!d) {
                if (errno == ENOENT)
                        return 0;

                log_error("Failed to open /var/lib/systemd/linger/: %m");
                return -errno;
        }

        while ((de = readdir(d))) {
                int k;

                if (!dirent_is_file(de))
661 662
                        continue;

Lennart Poettering's avatar
Lennart Poettering committed
663 664 665 666
                k = manager_add_user_by_name(m, de->d_name, NULL);
                if (k < 0) {
                        log_notice("Couldn't add lingering user %s: %s", de->d_name, strerror(-k));
                        r = k;
667 668 669
                }
        }

Lennart Poettering's avatar
Lennart Poettering committed
670
        closedir(d);
671 672 673 674 675 676 677

        return r;
}

int manager_enumerate_users(Manager *m) {
        DIR *d;
        struct dirent *de;
Lennart Poettering's avatar
Lennart Poettering committed
678
        int r, k;
679 680 681

        assert(m);

Lennart Poettering's avatar
Lennart Poettering committed
682
        /* First, enumerate user cgroups */
683 684
        r = manager_enumerate_users_from_cgroup(m);

Lennart Poettering's avatar
Lennart Poettering committed
685 686 687 688 689 690
        /* Second, add lingering users on top */
        k = manager_enumerate_linger_users(m);
        if (k < 0)
                r = k;

        /* Third, read in user data stored on disk */
691 692 693 694 695 696 697 698 699 700
        d = opendir("/run/systemd/users");
        if (!d) {
                if (errno == ENOENT)
                        return 0;

                log_error("Failed to open /run/systemd/users: %m");
                return -errno;
        }

        while ((de = readdir(d))) {
701
                uid_t uid;
702 703 704 705 706
                User *u;

                if (!dirent_is_file(de))
                        continue;

707
                k = parse_uid(de->d_name, &uid);
708 709 710 711 712
                if (k < 0) {
                        log_error("Failed to parse file name %s: %s", de->d_name, strerror(-k));
                        continue;
                }

713
                u = hashmap_get(m->users, ULONG_TO_PTR(uid));
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
                if (!u) {
                        unlinkat(dirfd(d), de->d_name, 0);
                        continue;
                }

                k = user_load(u);
                if (k < 0)
                        r = k;
        }

        closedir(d);

        return r;
}

static int manager_enumerate_sessions_from_cgroup(Manager *m) {
        User *u;
        Iterator i;
        int r = 0;

        HASHMAP_FOREACH(u, m->users, i) {
                DIR *d;
                char *name;
                int k;

Lennart Poettering's avatar
Lennart Poettering committed
739 740 741
                if (!u->cgroup_path)
                        continue;

742 743 744 745 746 747 748 749 750 751 752 753 754
                k = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, &d);
                if (k < 0) {
                        if (k == -ENOENT)
                                continue;

                        log_error("Failed to open %s: %s", u->cgroup_path, strerror(-k));
                        r = k;
                        continue;
                }

                while ((k = cg_read_subgroup(d, &name)) > 0) {
                        Session *session;

755 756 757
                        if (streq(name, "shared"))
                                continue;

758
                        k = manager_add_session(m, u, name, &session);
Lennart Poettering's avatar
Lennart Poettering committed
759 760
                        if (k < 0) {
                                free(name);
761
                                break;
Lennart Poettering's avatar
Lennart Poettering committed
762
                        }
763

Lennart Poettering's avatar
Lennart Poettering committed
764
                        session_add_to_gc_queue(session);
765

Lennart Poettering's avatar
Lennart Poettering committed
766 767 768 769 770 771 772 773
                        if (!session->cgroup_path)
                                if (asprintf(&session->cgroup_path, "%s/%s", u->cgroup_path, name) < 0) {
                                        k = -ENOMEM;
                                        free(name);
                                        break;
                                }

                        free(name);
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
                }

                closedir(d);

                if (k < 0)
                        r = k;
        }

        return r;
}

int manager_enumerate_sessions(Manager *m) {
        DIR *d;
        struct dirent *de;
        int r = 0;

        assert(m);

Lennart Poettering's avatar
Lennart Poettering committed
792
        /* First enumerate session cgroups */
793 794
        r = manager_enumerate_sessions_from_cgroup(m);

Lennart Poettering's avatar
Lennart Poettering committed
795
        /* Second, read in session data stored on disk */
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
        d = opendir("/run/systemd/sessions");
        if (!d) {
                if (errno == ENOENT)
                        return 0;

                log_error("Failed to open /run/systemd/sessions: %m");
                return -errno;
        }

        while ((de = readdir(d))) {
                struct Session *s;
                int k;

                if (!dirent_is_file(de))
                        continue;

                s = hashmap_get(m->sessions, de->d_name);
                if (!s) {
                        unlinkat(dirfd(d), de->d_name, 0);
                        continue;
                }

                k = session_load(s);
                if (k < 0)
                        r = k;
        }

        closedir(d);

        return r;
}

828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
int manager_enumerate_inhibitors(Manager *m) {
        DIR *d;
        struct dirent *de;
        int r = 0;

        assert(m);

        d = opendir("/run/systemd/inhibit");
        if (!d) {
                if (errno == ENOENT)
                        return 0;

                log_error("Failed to open /run/systemd/inhibit: %m");
                return -errno;
        }

        while ((de = readdir(d))) {
                int k;
                Inhibitor *i;

                if (!dirent_is_file(de))
                        continue;

                k = manager_add_inhibitor(m, de->d_name, &i);
                if (k < 0) {
                        log_notice("Couldn't add inhibitor %s: %s", de->d_name, strerror(-k));
                        r = k;
                        continue;
                }

                k = inhibitor_load(i);
                if (k < 0)
                        r = k;
        }

        closedir(d);

        return r;
}

868
int manager_dispatch_seat_udev(Manager *m) {
869 870 871 872 873
        struct udev_device *d;
        int r;

        assert(m);

874
        d = udev_monitor_receive_device(m->udev_seat_monitor);
875 876 877
        if (!d)
                return -ENOMEM;

878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
        r = manager_process_seat_device(m, d);
        udev_device_unref(d);

        return r;
}

int manager_dispatch_vcsa_udev(Manager *m) {
        struct udev_device *d;
        int r = 0;
        const char *name;

        assert(m);

        d = udev_monitor_receive_device(m->udev_vcsa_monitor);
        if (!d)
                return -ENOMEM;

        name = udev_device_get_sysname(d);

        /* Whenever a VCSA device is removed try to reallocate our
         * VTs, to make sure our auto VTs never go away. */

        if (name && startswith(name, "vcsa") && streq_ptr(udev_device_get_action(d), "remove"))
                r = seat_preallocate_vts(m->vtconsole);

903 904 905 906 907
        udev_device_unref(d);

        return r;
}

908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
int manager_dispatch_button_udev(Manager *m) {
        struct udev_device *d;
        int r;

        assert(m);

        d = udev_monitor_receive_device(m->udev_button_monitor);
        if (!d)
                return -ENOMEM;

        r = manager_process_button_device(m, d);
        udev_device_unref(d);

        return r;
}

924 925 926 927
int manager_dispatch_console(Manager *m) {
        assert(m);

        if (m->vtconsole)
Lennart Poettering's avatar
Lennart Poettering committed
928
                seat_read_active_vt(m->vtconsole);
929 930 931 932

        return 0;
}

933 934 935 936 937 938
static int vt_is_busy(int vtnr) {
        struct vt_stat vt_stat;
        int r = 0, fd;

        assert(vtnr >= 1);

939 940 941 942 943 944 945
        /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
         * we'd open the latter we'd open the foreground tty which
         * hence would be unconditionally busy. By opening /dev/tty1
         * we avoid this. Since tty1 is special and needs to be an
         * explicitly loaded getty or DM this is safe. */

        fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC);
946 947 948 949 950 951 952 953 954 955 956 957 958
        if (fd < 0)
                return -errno;

        if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0)
                r = -errno;
        else
                r = !!(vt_stat.v_state & (1 << vtnr));

        close_nointr_nofail(fd);

        return r;
}

959
int manager_spawn_autovt(Manager *m, int vtnr) {
960
        int r;
961 962
        char *name = NULL;
        const char *mode = "fail";
963

964
        assert(m);
965
        assert(vtnr >= 1);
966

967 968
        if ((unsigned) vtnr > m->n_autovts &&
            (unsigned) vtnr != m->reserve_vt)
969 970
                return 0;

971 972 973 974 975 976 977 978 979 980 981
        if ((unsigned) vtnr != m->reserve_vt) {
                /* If this is the reserved TTY, we'll start the getty
                 * on it in any case, but otherwise only if it is not
                 * busy. */

                r = vt_is_busy(vtnr);
                if (r < 0)
                        return r;
                else if (r > 0)
                        return -EBUSY;
        }
982

983
        if (asprintf(&name, "autovt@tty%i.service", vtnr) < 0) {
984 985 986 987
                log_error("Could not allocate service name.");
                r = -ENOMEM;
                goto finish;
        }
988

989 990 991 992 993 994 995 996 997 998 999
        r = bus_method_call_with_reply (
                        m->bus,
                        "org.freedesktop.systemd1",
                        "/org/freedesktop/systemd1",
                        "org.freedesktop.systemd1.Manager",
                        "StartUnit",
                        NULL,
                        NULL,
                        DBUS_TYPE_STRING, &name,
                        DBUS_TYPE_STRING, &mode,
                        DBUS_TYPE_INVALID);
1000 1001 1002 1003 1004

finish:
        free(name);

        return r;
1005 1006
}

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
static int manager_reserve_vt(Manager *m) {
        _cleanup_free_ char *p = NULL;

        assert(m);

        if (m->reserve_vt <= 0)
                return 0;

        if (asprintf(&p, "/dev/tty%u", m->reserve_vt) < 0)
                return log_oom();

        m->reserve_vt_fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
        if (m->reserve_vt_fd < 0) {
1020 1021 1022 1023

                /* Don't complain on VT-less systems */
                if (errno != ENOENT)
                        log_warning("Failed to pin reserved VT: %m");
1024 1025 1026 1027 1028 1029
                return -errno;
        }

        return 0;
}

1030
int manager_get_session_by_cgroup(Manager *m, const char *cgroup, Session **session) {
1031
        Session *s;
1032 1033 1034 1035
        char *p;

        assert(m);
        assert(cgroup);
1036
        assert(session);
1037

1038
        s = hashmap_get(m->session_cgroups, cgroup);
1039 1040 1041 1042 1043
        if (s) {
                *session = s;
                return 1;
        }

1044
        p = strdupa(cgroup);
1045 1046 1047

        for (;;) {
                char *e;
1048

1049 1050
                e = strrchr(p, '/');
                if (!e || e == p) {
1051 1052 1053
                        *session = NULL;
                        return 0;
                }
1054

1055 1056
                *e = 0;

1057
                s = hashmap_get(m->session_cgroups, p);
1058 1059 1060 1061
                if (s) {
                        *session = s;
                        return 1;
                }
1062
        }
1063 1064
}

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
int manager_get_user_by_cgroup(Manager *m, const char *cgroup, User **user) {
        User *u;
        char *p;

        assert(m);
        assert(cgroup);
        assert(user);

        u = hashmap_get(m->user_cgroups, cgroup);
        if (u) {
                *user = u;
                return 1;
        }

1079
        p = strdupa(cgroup);
1080 1081
        if (!p)
                return log_oom();
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

        for (;;) {
                char *e;

                e = strrchr(p, '/');
                if (!e || e == p) {
                        *user = NULL;
                        return 0;
                }

                *e = 0;

                u = hashmap_get(m->user_cgroups, p);
                if (u) {
                        *user = u;
                        return 1;
                }
        }
}

1102
int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) {
1103
        _cleanup_free_ char *p = NULL;
1104 1105 1106 1107 1108
        int r;

        assert(m);
        assert(pid >= 1);
        assert(session);
1109

1110
        r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &p);
1111 1112 1113
        if (r < 0)
                return r;

1114
        return manager_get_session_by_cgroup(m, p, session);
1115 1116 1117 1118
}

void manager_cgroup_notify_empty(Manager *m, const char *cgroup) {
        Session *s;
1119
        User *u;
1120 1121 1122
        int r;

        r = manager_get_session_by_cgroup(m, cgroup, &s);
1123 1124
        if (r > 0)
                session_add_to_gc_queue(s);
1125

1126 1127 1128
        r = manager_get_user_by_cgroup(m, cgroup, &u);
        if (r > 0)
                user_add_to_gc_queue(u);
1129 1130
}

1131
static void manager_dispatch_other(Manager *m, int fd) {
1132
        Session *s;
1133
        Inhibitor *i;
1134
        Button *b;
1135 1136 1137 1138

        assert_se(m);
        assert_se(fd >= 0);

1139 1140 1141 1142 1143 1144 1145
        s = hashmap_get(m->session_fds, INT_TO_PTR(fd + 1));
        if (s) {
                assert(s->fifo_fd == fd);
                session_remove_fifo(s);
                session_stop(s);
                return;
        }
1146

1147 1148 1149 1150 1151 1152 1153 1154
        i = hashmap_get(m->inhibitor_fds, INT_TO_PTR(fd + 1));
        if (i) {
                assert(i->fifo_fd == fd);
                inhibitor_stop(i);
                inhibitor_free(i);
                return;
        }

1155 1156 1157 1158 1159 1160 1161 1162
        b = hashmap_get(m->button_fds, INT_TO_PTR(fd + 1));
        if (b) {
                assert(b->fd == fd);
                button_process(b);
                return;
        }

        assert_not_reached("Got event for unknown fd");
1163 1164
}

1165 1166 1167
static int manager_connect_bus(Manager *m) {
        DBusError error;
        int r;
1168 1169 1170 1171
        struct epoll_event ev = {
                .events = EPOLLIN,
                .data.u32 = FD_BUS,
        };
1172 1173 1174 1175 1176 1177 1178 1179 1180

        assert(m);
        assert(!m->bus);
        assert(m->bus_fd < 0);

        dbus_error_init(&error);

        m->bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
        if (!m->bus) {
1181
                log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
1182 1183 1184 1185
                r = -ECONNREFUSED;
                goto fail;
        }

1186 1187 1188 1189
        if (!dbus_connection_register_object_path(m->bus, "/org/freedesktop/login1", &bus_manager_vtable, m) ||
            !dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/seat", &bus_seat_vtable, m) ||
            !dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/session", &bus_session_vtable, m) ||
            !dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/user", &bus_user_vtable, m) ||
1190
            !dbus_connection_add_filter(m->bus, bus_message_filter, m, NULL)) {
1191
                r = log_oom();
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
                goto fail;
        }

        dbus_bus_add_match(m->bus,
                           "type='signal',"
                           "interface='org.freedesktop.systemd1.Agent',"
                           "member='Released',"
                           "path='/org/freedesktop/systemd1/agent'",
                           &error);

        if (dbus_error_is_set(&error)) {
1203
                log_error("Failed to register match: %s", bus_error_message(&error));
1204 1205 1206 1207
                r = -EIO;
                goto fail;
        }

1208 1209 1210 1211 1212 1213 1214 1215 1216
        r = dbus_bus_request_name(m->bus, "org.freedesktop.login1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
        if (dbus_error_is_set(&error)) {
                log_error("Failed to register name on bus: %s", bus_error_message(&error));
                r = -EIO;
                goto fail;
        }

        if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)  {
                log_error("Failed to acquire name.");
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
                r = -EEXIST;
                goto fail;
        }

        m->bus_fd = bus_loop_open(m->bus);
        if (m->bus_fd < 0) {
                r = m->bus_fd;
                goto fail;
        }

        if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->bus_fd, &ev) < 0)
                goto fail;

        return 0;

fail:
        dbus_error_free(&error);

        return r;
}

static int manager_connect_console(Manager *m) {
1239 1240 1241 1242
        struct epoll_event ev = {
                .events = 0,
                .data.u32 = FD_CONSOLE,
        };
1243 1244 1245 1246

        assert(m);
        assert(m->console_active_fd < 0);

1247 1248 1249 1250 1251 1252 1253 1254
        /* On certain architectures (S390 and Xen, and containers),
           /dev/tty0 does not exist, so don't fail if we can't open
           it. */
        if (access("/dev/tty0", F_OK) < 0) {
                m->console_active_fd = -1;
                return 0;
        }

1255 1256
        m->console_active_fd = open("/sys/class/tty/tty0/active", O_RDONLY|O_NOCTTY|O_CLOEXEC);
        if (m->console_active_fd < 0) {
1257 1258 1259 1260 1261 1262

                /* On some systems the device node /dev/tty0 may exist
                 * even though /sys/class/tty/tty0 does not. */
                if (errno == ENOENT)
                        return 0;

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
                log_error("Failed to open /sys/class/tty/tty0/active: %m");
                return -errno;
        }

        if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->console_active_fd, &ev) < 0)
                return -errno;

        return 0;
}

static int manager_connect_udev(Manager *m) {
Lennart Poettering's avatar
Lennart Poettering committed
1274
        int r;
1275 1276 1277 1278
        struct epoll_event ev = {
                .events = EPOLLIN,
                .data.u32 = FD_SEAT_UDEV,
        };
1279 1280

        assert(m);
1281 1282
        assert(!m->udev_seat_monitor);
        assert(!m->udev_vcsa_monitor);
1283
        assert(!m->udev_button_monitor);
1284

1285 1286
        m->udev_seat_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
        if (!m->udev_seat_monitor)
1287 1288
                return -ENOMEM;

1289
        r = udev_monitor_filter_add_match_tag(m->udev_seat_monitor, "master-of-seat");
Lennart Poettering's avatar
Lennart Poettering committed
1290 1291
        if (r < 0)
                return r;
1292

1293
        r = udev_monitor_enable_receiving(m->udev_seat_monitor);
Lennart Poettering's avatar
Lennart Poettering committed
1294 1295
        if (r < 0)
                return r;
1296

1297
        m->udev_seat_fd = udev_monitor_get_fd(m->udev_seat_monitor);
1298

1299 1300 1301
        if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->udev_seat_fd, &ev) < 0)
                return -errno;

1302
        /* Don't watch keys if nobody cares */
1303
        if (m->handle_power_key != HANDLE_IGNORE ||
1304 1305
            m->handle_suspend_key != HANDLE_IGNORE ||
            m->handle_hibernate_key != HANDLE_IGNORE ||
1306
            m->handle_lid_switch != HANDLE_IGNORE) {
1307

1308 1309 1310
                m->udev_button_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
                if (!m->udev_button_monitor)
                        return -ENOMEM;
1311

1312 1313 1314
                r = udev_monitor_filter_add_match_tag(m->udev_button_monitor, "power-switch");
                if (r < 0)
                        return r;
1315

1316 1317 1318
                r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_button_monitor, "input", NULL);
                if (r < 0)
                        return r;
1319

1320 1321 1322
                r = udev_monitor_enable_receiving(m->udev_button_monitor);
                if (r < 0)
                        return r;
1323

1324
                m->udev_button_fd = udev_monitor_get_fd(m->udev_button_monitor);
1325

1326 1327 1328 1329 1330 1331
                zero(ev);
                ev.events = EPOLLIN;
                ev.data.u32 = FD_BUTTON_UDEV;
                if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->udev_button_fd, &ev) < 0)
                        return -errno;
        }
1332

1333
        /* Don't bother watching VCSA devices, if nobody cares */
1334
        if (m->n_autovts > 0 && m->console_active_fd >= 0) {
1335

1336 1337 1338
                m->udev_vcsa_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
                if (!m->udev_vcsa_monitor)
                        return -ENOMEM;
1339

1340 1341 1342
                r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_vcsa_monitor, "vc", NULL);
                if (r < 0)
                        return r;
1343

1344 1345 1346
                r = udev_monitor_enable_receiving(m->udev_vcsa_monitor);
                if (r < 0)
                        return r;
1347

1348
                m->udev_vcsa_fd = udev_monitor_get_fd(m->udev_vcsa_monitor);
1349

1350 1351 1352 1353 1354 1355
                zero(ev);
                ev.events = EPOLLIN;
                ev.data.u32 = FD_VCSA_UDEV;
                if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->udev_vcsa_fd, &ev) < 0)