From: Lennart Poettering Date: Tue, 24 May 2011 22:55:58 +0000 (+0200) Subject: logind: implement GC X-Git-Tag: v30~180 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=14c3baca3eb8b32fc266e46127851585bee5aff2;p=systemd logind: implement GC --- diff --git a/src/logind-seat.c b/src/logind-seat.c index 743ded66..24a5d8f2 100644 --- a/src/logind-seat.c +++ b/src/logind-seat.c @@ -48,21 +48,23 @@ Seat *seat_new(Manager *m, const char *id) { } s->id = file_name_from_path(s->state_file); + s->manager = m; if (hashmap_put(m->seats, s->id, s) < 0) { - free(s->id); + free(s->state_file); free(s); return NULL; } - s->manager = m; - return s; } void seat_free(Seat *s) { assert(s); + if (s->in_gc_queue) + LIST_REMOVE(Seat, gc_queue, s->manager->seat_gc_queue, s); + while (s->sessions) session_free(s->sessions); @@ -73,25 +75,33 @@ void seat_free(Seat *s) { hashmap_remove(s->manager->seats, s->id); - free(s->state_file); + if (s->state_file) { + unlink(s->state_file); + free(s->state_file); + } + free(s); } int seat_save(Seat *s) { - FILE *f; int r; + FILE *f; + char *temp_path; assert(s); r = safe_mkdir("/run/systemd/seat", 0755, 0, 0); if (r < 0) - return r; + goto finish; + + r = fopen_temporary(s->state_file, &f, &temp_path); + if (r < 0) + goto finish; - f = fopen(s->state_file, "we"); - if (!f) - return -errno; + fchmod(fileno(f), 0644); fprintf(f, + "# This is private data. Do not parse.\n" "IS_VTCONSOLE=%i\n", s->manager->vtconsole == s); @@ -107,28 +117,45 @@ int seat_save(Seat *s) { if (s->sessions) { Session *i; - fputs("OTHER_UIDS=", f); + fputs("OTHER=", f); LIST_FOREACH(sessions_by_seat, i, s->sessions) { - assert(i->user); + if (i == s->active) + continue; + + fprintf(f, + "%s%c", + i->id, + i->sessions_by_seat_next ? ' ' : '\n'); + } + fputs("OTHER_UIDS=", f); + LIST_FOREACH(sessions_by_seat, i, s->sessions) { if (i == s->active) continue; fprintf(f, - "%s%lu", - i == s->sessions ? "" : " ", - (unsigned long) i->user->uid); + "%lu%c", + (unsigned long) i->user->uid, + i->sessions_by_seat_next ? ' ' : '\n'); } } fflush(f); - if (ferror(f)) { + + if (ferror(f) || rename(temp_path, s->state_file) < 0) { r = -errno; unlink(s->state_file); + unlink(temp_path); } fclose(f); + free(temp_path); + +finish: + if (r < 0) + log_error("Failed to save seat data for %s: %s", s->id, strerror(-r)); + return r; } @@ -158,7 +185,7 @@ static int vt_allocate(int vtnr) { return r; } -int seat_preallocate_vts(Seat *s) { +static int seat_preallocate_vts(Seat *s) { int i, r = 0; assert(s); @@ -174,8 +201,10 @@ int seat_preallocate_vts(Seat *s) { int q; q = vt_allocate(i); - if (r >= 0 && q < 0) + if (q < 0) { + log_error("Failed to preallocate VT %i: %s", i, strerror(-q)); r = q; + } } return r; @@ -199,44 +228,122 @@ int seat_apply_acls(Seat *s, Session *old_active) { } int seat_active_vt_changed(Seat *s, int vtnr) { - Session *i; - Session *old_active; + Session *i, *new_active = NULL, *old_active; assert(s); assert(vtnr >= 1); - assert(s->manager->vtconsole == s); - old_active = s->active; - s->active = NULL; + if (s->manager->vtconsole != s) + return -EINVAL; + + log_debug("VT changed to %i", vtnr); LIST_FOREACH(sessions_by_seat, i, s->sessions) if (i->vtnr == vtnr) { - s->active = i; + new_active = i; break; } - if (old_active == s->active) + if (new_active == s->active) return 0; + old_active = s->active; + s->active = new_active; + seat_apply_acls(s, old_active); manager_spawn_autovt(s->manager, vtnr); return 0; } +int seat_read_active_vt(Seat *s) { + char t[64]; + ssize_t k; + int r, vtnr; + + assert(s); + + if (s->manager->vtconsole != s) + return 0; + + lseek(s->manager->console_active_fd, SEEK_SET, 0); + + k = read(s->manager->console_active_fd, t, sizeof(t)-1); + if (k <= 0) { + log_error("Failed to read current console: %s", k < 0 ? strerror(-errno) : "EOF"); + return k < 0 ? -errno : -EIO; + } + + t[k] = 0; + truncate_nl(t); + + if (!startswith(t, "tty")) { + log_error("Hm, /sys/class/tty/tty0/active is badly formatted."); + return -EIO; + } + + r = safe_atoi(t+3, &vtnr); + if (r < 0) { + log_error("Failed to parse VT number %s", t+3); + return r; + } + + if (vtnr <= 0) { + log_error("VT number invalid: %s", t+3); + return -EIO; + } + + return seat_active_vt_changed(s, vtnr); +} + +int seat_start(Seat *s) { + assert(s); + + /* Initialize VT magic stuff */ + seat_preallocate_vts(s); + + /* Read current VT */ + seat_read_active_vt(s); + + /* Save seat data */ + seat_save(s); + + return 0; +} + int seat_stop(Seat *s) { Session *session; - int r = 0; + int r = 0, k; assert(s); LIST_FOREACH(sessions_by_seat, session, s->sessions) { - int k; - k = session_stop(session); if (k < 0) r = k; } + seat_save(s); + seat_add_to_gc_queue(s); + return r; } + +int seat_check_gc(Seat *s) { + assert(s); + + if (s->manager->vtconsole == s) + return 1; + + return !!s->devices; +} + +void seat_add_to_gc_queue(Seat *s) { + assert(s); + + if (s->in_gc_queue) + return; + + LIST_PREPEND(Seat, gc_queue, s->manager->seat_gc_queue, s); + s->in_gc_queue = true; +} diff --git a/src/logind-seat.h b/src/logind-seat.h index 4b6b3401..b1a8d619 100644 --- a/src/logind-seat.h +++ b/src/logind-seat.h @@ -40,15 +40,26 @@ struct Seat { Session *active; LIST_HEAD(Session, sessions); + + bool in_gc_queue:1; + + LIST_FIELDS(Seat, gc_queue); }; Seat *seat_new(Manager *m, const char *id); void seat_free(Seat *s); -int seat_preallocate_vts(Seat *s); -int seat_active_vt_changed(Seat *s, int vtnr); -int seat_apply_acls(Seat *s, Session *old_active); -int seat_stop(Seat *s); + int seat_save(Seat *s); int seat_load(Seat *s); +int seat_apply_acls(Seat *s, Session *old_active); +int seat_active_vt_changed(Seat *s, int vtnr); +int seat_read_active_vt(Seat *s); + +int seat_start(Seat *s); +int seat_stop(Seat *s); + +int seat_check_gc(Seat *s); +void seat_add_to_gc_queue(Seat *s); + #endif diff --git a/src/logind-session.c b/src/logind-session.c index c10f5e62..566323d1 100644 --- a/src/logind-session.c +++ b/src/logind-session.c @@ -34,7 +34,7 @@ Session* session_new(Manager *m, User *u, const char *id) { assert(m); assert(id); - s = new(Session, 1); + s = new0(Session, 1); if (!s) return NULL; @@ -56,7 +56,7 @@ Session* session_new(Manager *m, User *u, const char *id) { s->pipe_fd = -1; s->user = u; - dual_timestamp_get(&s->timestamp); + LIST_PREPEND(Session, sessions_by_user, u->sessions, s); return s; } @@ -64,6 +64,9 @@ Session* session_new(Manager *m, User *u, const char *id) { void session_free(Session *s) { assert(s); + if (s->in_gc_queue) + LIST_REMOVE(Session, gc_queue, s->manager->session_gc_queue, s); + if (s->user) { LIST_REMOVE(Session, sessions_by_user, s->user->sessions, s); @@ -83,26 +86,33 @@ void session_free(Session *s) { hashmap_remove(s->manager->sessions, s->id); - free(s->state_file); + if (s->state_file) { + unlink(s->state_file); + free(s->state_file); + } + free(s); } int session_save(Session *s) { FILE *f; int r = 0; + char *temp_path; assert(s); r = safe_mkdir("/run/systemd/session", 0755, 0, 0); if (r < 0) - return r; + goto finish; - f = fopen(s->state_file, "we"); - if (!f) - return -errno; + r = fopen_temporary(s->state_file, &f, &temp_path); + if (r < 0) + goto finish; assert(s->user); + fchmod(fileno(f), 0644); + fprintf(f, "# This is private data. Do not parse.\n" "UID=%lu\n" @@ -157,12 +167,20 @@ int session_save(Session *s) { (unsigned long long) s->audit_id); fflush(f); - if (ferror(f)) { + + if (ferror(f) || rename(temp_path, s->state_file) < 0) { r = -errno; unlink(s->state_file); + unlink(temp_path); } fclose(f); + free(temp_path); + +finish: + if (r < 0) + log_error("Failed to save session data for %s: %s", s->id, strerror(-r)); + return r; } @@ -196,10 +214,7 @@ int session_activate(Session *s) { old_active = s->seat->active; s->seat->active = s; - seat_apply_acls(s->seat, old_active); - manager_spawn_autovt(s->manager, s->vtnr); - - return 0; + return seat_apply_acls(s->seat, old_active); } bool x11_display_is_local(const char *display) { @@ -333,11 +348,6 @@ int session_start(Session *s) { assert(s); assert(s->user); - /* Create user first */ - r = user_start(s->user); - if (r < 0) - return r; - /* Create cgroup */ r = session_create_cgroup(s); if (r < 0) @@ -345,6 +355,12 @@ int session_start(Session *s) { /* Create X11 symlink */ session_link_x11_socket(s); + + /* Save session data */ + session_save(s); + + dual_timestamp_get(&s->timestamp); + return 0; } @@ -429,6 +445,8 @@ int session_stop(Session *s) { /* Remove X11 symlink */ session_unlink_x11_socket(s); + session_save(s); + return r; } @@ -452,7 +470,7 @@ int session_check_gc(Session *s) { if (r < 0) return r; - if (r <= 0) + if (r == 0) return 1; } @@ -469,6 +487,16 @@ int session_check_gc(Session *s) { return 0; } +void session_add_to_gc_queue(Session *s) { + assert(s); + + if (s->in_gc_queue) + return; + + LIST_PREPEND(Session, gc_queue, s->manager->session_gc_queue, s); + s->in_gc_queue = true; +} + static const char* const session_type_table[_SESSION_TYPE_MAX] = { [SESSION_TERMINAL] = "terminal", [SESSION_X11] = "x11" diff --git a/src/logind-session.h b/src/logind-session.h index b3f0b5fa..4c6e768f 100644 --- a/src/logind-session.h +++ b/src/logind-session.h @@ -66,17 +66,21 @@ struct Session { char *cgroup_path; char **controllers, **reset_controllers; - bool kill_processes; + bool kill_processes:1; + bool in_gc_queue:1; LIST_FIELDS(Session, sessions_by_user); LIST_FIELDS(Session, sessions_by_seat); + + LIST_FIELDS(Session, gc_queue); }; Session *session_new(Manager *m, User *u, const char *id); void session_free(Session *s); +int session_check_gc(Session *s); +void session_add_to_gc_queue(Session *s); int session_activate(Session *s); bool session_is_active(Session *s); -int session_check_gc(Session *s); int session_start(Session *s); int session_stop(Session *s); int session_save(Session *s); diff --git a/src/logind-user.c b/src/logind-user.c index e0a38425..19db746d 100644 --- a/src/logind-user.c +++ b/src/logind-user.c @@ -35,7 +35,7 @@ User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) { assert(m); assert(name); - u = new(User, 1); + u = new0(User, 1); if (!u) return NULL; @@ -68,6 +68,9 @@ User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) { void user_free(User *u) { assert(u); + if (u->in_gc_queue) + LIST_REMOVE(User, gc_queue, u->manager->user_gc_queue, u); + while (u->sessions) session_free(u->sessions); @@ -79,26 +82,35 @@ void user_free(User *u) { hashmap_remove(u->manager->users, ULONG_TO_PTR((unsigned long) u->uid)); free(u->name); - free(u->state_file); + + if (u->state_file) { + unlink(u->state_file); + free(u->state_file); + } + free(u); } int user_save(User *u) { FILE *f; int r; + char *temp_path; assert(u); assert(u->state_file); r = safe_mkdir("/run/systemd/user", 0755, 0, 0); if (r < 0) - return r; + goto finish; - f = fopen(u->state_file, "we"); - if (!f) - return -errno; + r = fopen_temporary(u->state_file, &f, &temp_path); + if (r < 0) + goto finish; + + fchmod(fileno(f), 0644); fprintf(f, + "# This is private data. Do not parse.\n" "NAME=%s\n" "STATE=%s\n", u->name, @@ -125,12 +137,20 @@ int user_save(User *u) { u->display->id); fflush(f); - if (ferror(f)) { + + if (ferror(f) || rename(temp_path, u->state_file) < 0) { r = -errno; unlink(u->state_file); + unlink(temp_path); } fclose(f); + free(temp_path); + +finish: + if (r < 0) + log_error("Failed to save user data for %s: %s", u->name, strerror(-r)); + return r; } @@ -260,6 +280,9 @@ int user_start(User *u) { if (r < 0) return r; + /* Save new user data */ + user_save(u); + dual_timestamp_get(&u->timestamp); return 0; @@ -395,6 +418,16 @@ int user_check_gc(User *u) { return 0; } +void user_add_to_gc_queue(User *u) { + assert(u); + + if (u->in_gc_queue) + return; + + LIST_PREPEND(User, gc_queue, u->manager->user_gc_queue, u); + u->in_gc_queue = true; +} + UserState user_get_state(User *u) { Session *i; diff --git a/src/logind-user.h b/src/logind-user.h index 04392e01..fc480594 100644 --- a/src/logind-user.h +++ b/src/logind-user.h @@ -54,14 +54,18 @@ struct User { dual_timestamp timestamp; + bool in_gc_queue:1; + LIST_HEAD(Session, sessions); + LIST_FIELDS(User, gc_queue); }; User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name); void user_free(User *u); +int user_check_gc(User *u); +void user_add_to_gc_queue(User *u); int user_start(User *u); int user_stop(User *u); -int user_check_gc(User *u); UserState user_get_state(User *u); int user_save(User *u); int user_load(User *u); diff --git a/src/logind.c b/src/logind.c index 7e9b706d..5d5181a1 100644 --- a/src/logind.c +++ b/src/logind.c @@ -200,7 +200,6 @@ int manager_add_session(Manager *m, User *u, const char *id, Session **_session) int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user) { User *u; - int r; assert(m); assert(name); @@ -217,16 +216,10 @@ int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User ** if (!u) return -ENOMEM; - r = user_start(u); - if (r < 0) { - user_stop(u); - user_free(u); - } else { - if (_user) - *_user = u; - } + if (_user) + *_user = u; - return r; + return 0; } int manager_add_user_by_name(Manager *m, const char *name, User **_user) { @@ -257,33 +250,53 @@ int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) { } int manager_process_device(Manager *m, struct udev_device *d) { - const char *sn; - Seat *seat; Device *device; int r; assert(m); - sn = udev_device_get_property_value(d, "SEAT"); - if (!sn) - sn = "seat0"; + if (streq_ptr(udev_device_get_action(d), "remove")) { - r = manager_add_device(m, udev_device_get_syspath(d), &device); - if (r < 0) - return r; + device = hashmap_get(m->devices, udev_device_get_syspath(d)); + if (!device) + return 0; - r = manager_add_seat(m, sn, &seat); - if (r < 0) - return r; + seat_add_to_gc_queue(device->seat); + device_free(device); + + } else { + const char *sn; + Seat *seat; + + sn = udev_device_get_property_value(d, "SEAT"); + if (!sn) + sn = "seat0"; + + if (!startswith(sn, "seat")) + return -EINVAL; + + r = manager_add_device(m, udev_device_get_syspath(d), &device); + 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); + } - device_attach(device, seat); return 0; } int manager_enumerate_devices(Manager *m) { struct udev_list_entry *item = NULL, *first = NULL; struct udev_enumerate *e; - int r = -ENOMEM; + int r; assert(m); @@ -291,16 +304,21 @@ int manager_enumerate_devices(Manager *m) { * necessary */ e = udev_enumerate_new(m->udev); - if (!e) + if (!e) { + r = -ENOMEM; goto finish; + } - if (udev_enumerate_add_match_subsystem(e, "graphics") < 0) + r = udev_enumerate_add_match_subsystem(e, "graphics"); + if (r < 0) goto finish; - if (udev_enumerate_add_match_tag(e, "seat") < 0) + r = udev_enumerate_add_match_tag(e, "seat"); + if (r < 0) goto finish; - if (udev_enumerate_scan_devices(e) < 0) + r = udev_enumerate_scan_devices(e); + if (r < 0) goto finish; first = udev_enumerate_get_list_entry(e); @@ -309,8 +327,10 @@ int manager_enumerate_devices(Manager *m) { int k; d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item)); - if (!d) + if (!d) { + r = -ENOMEM; goto finish; + } k = manager_process_device(m, d); udev_device_unref(d); @@ -353,6 +373,9 @@ int manager_enumerate_seats(Manager *m) { if (!dirent_is_file(de)) continue; + if (!startswith(de->d_name, "seat")) + continue; + s = hashmap_get(m->seats, de->d_name); if (!s) { unlinkat(dirfd(d), de->d_name, 0); @@ -385,24 +408,61 @@ static int manager_enumerate_users_from_cgroup(Manager *m) { while ((r = cg_read_subgroup(d, &name)) > 0) { User *user; + int k; + + 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; + } - r = manager_add_user_by_name(m, name, &user); free(name); + } + + closedir(d); + + return r; +} - if (r < 0) - break; - if (user->cgroup_path) +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)) continue; - if (asprintf(&user->cgroup_path, "%s/%s", m->cgroup_path, name) < 0) { - r = -ENOMEM; - break; + 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; } } - if (d) - closedir(d); + closedir(d); return r; } @@ -410,12 +470,19 @@ static int manager_enumerate_users_from_cgroup(Manager *m) { int manager_enumerate_users(Manager *m) { DIR *d; struct dirent *de; - int r; + int r, k; assert(m); + /* First, enumerate user cgroups */ r = manager_enumerate_users_from_cgroup(m); + /* 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 */ d = opendir("/run/systemd/users"); if (!d) { if (errno == ENOENT) @@ -428,7 +495,6 @@ int manager_enumerate_users(Manager *m) { while ((de = readdir(d))) { unsigned long ul; User *u; - int k; if (!dirent_is_file(de)) continue; @@ -465,6 +531,9 @@ static int manager_enumerate_sessions_from_cgroup(Manager *m) { char *name; int k; + if (!u->cgroup_path) + continue; + k = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, &d); if (k < 0) { if (k == -ENOENT) @@ -479,18 +548,21 @@ static int manager_enumerate_sessions_from_cgroup(Manager *m) { Session *session; k = manager_add_session(m, u, name, &session); - free(name); - - if (k < 0) + if (k < 0) { + free(name); break; + } - if (session->cgroup_path) - continue; + session_add_to_gc_queue(session); - if (asprintf(&session->cgroup_path, "%s/%s", u->cgroup_path, name) < 0) { - k = -ENOMEM; - break; - } + if (!session->cgroup_path) + if (asprintf(&session->cgroup_path, "%s/%s", u->cgroup_path, name) < 0) { + k = -ENOMEM; + free(name); + break; + } + + free(name); } closedir(d); @@ -509,8 +581,10 @@ int manager_enumerate_sessions(Manager *m) { assert(m); + /* First enumerate session cgroups */ r = manager_enumerate_sessions_from_cgroup(m); + /* Second, read in session data stored on disk */ d = opendir("/run/systemd/sessions"); if (!d) { if (errno == ENOENT) @@ -543,38 +617,6 @@ int manager_enumerate_sessions(Manager *m) { return r; } -int manager_start_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)) - continue; - - 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; - } - } - - closedir(d); - - return r; -} - int manager_dispatch_udev(Manager *m) { struct udev_device *d; int r; @@ -592,39 +634,10 @@ int manager_dispatch_udev(Manager *m) { } int manager_dispatch_console(Manager *m) { - char t[64]; - ssize_t k; - int r, vtnr; - assert(m); - lseek(m->console_active_fd, SEEK_SET, 0); - - k = read(m->console_active_fd, t, sizeof(t)-1); - if (k <= 0) { - log_error("Failed to read current console: %s", k < 0 ? strerror(-errno) : "EOF"); - return k < 0 ? -errno : -EIO; - } - - t[k] = 0; - if (!startswith(t, "tty")) { - log_error("Hm, /sys/class/tty/tty0/active is badly formatted."); - return -EIO; - } - - r = safe_atoi(t+3, &vtnr); - if (r < 0) { - log_error("Failed to parse VT number %s", t+3); - return r; - } - - if (vtnr <= 0) { - log_error("VT number invalid: %s", t+3); - return -EIO; - } - if (m->vtconsole) - seat_active_vt_changed(m->vtconsole, vtnr); + seat_read_active_vt(m->vtconsole); return 0; } @@ -766,7 +779,7 @@ static int manager_connect_console(Manager *m) { } zero(ev); - ev.events = EPOLLIN; + ev.events = 0; ev.data.u32 = FD_CONSOLE; if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->console_active_fd, &ev) < 0) @@ -777,6 +790,7 @@ static int manager_connect_console(Manager *m) { static int manager_connect_udev(Manager *m) { struct epoll_event ev; + int r; assert(m); assert(!m->udev_monitor); @@ -785,13 +799,17 @@ static int manager_connect_udev(Manager *m) { if (!m->udev_monitor) return -ENOMEM; - udev_monitor_filter_add_match_tag(m->udev_monitor, "seat"); + r = udev_monitor_filter_add_match_tag(m->udev_monitor, "seat"); + if (r < 0) + return r; - if (udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "graphics", NULL) < 0) - return -ENOMEM; + r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "graphics", NULL); + if (r < 0) + return r; - if (udev_monitor_enable_receiving(m->udev_monitor) < 0) - return -ENOMEM; + r = udev_monitor_enable_receiving(m->udev_monitor); + if (r < 0) + return r; m->udev_fd = udev_monitor_get_fd(m->udev_monitor); @@ -805,8 +823,50 @@ static int manager_connect_udev(Manager *m) { return 0; } +void manager_gc(Manager *m) { + Seat *seat; + Session *session; + User *user; + + assert(m); + + while ((seat = m->seat_gc_queue)) { + LIST_REMOVE(Seat, gc_queue, m->seat_gc_queue, seat); + seat->in_gc_queue = false; + + if (seat_check_gc(seat) == 0) { + seat_stop(seat); + seat_free(seat); + } + } + + while ((session = m->session_gc_queue)) { + LIST_REMOVE(Session, gc_queue, m->session_gc_queue, session); + session->in_gc_queue = false; + + if (session_check_gc(session) == 0) { + session_stop(session); + session_free(session); + } + } + + while ((user = m->user_gc_queue)) { + LIST_REMOVE(User, gc_queue, m->user_gc_queue, user); + user->in_gc_queue = false; + + if (user_check_gc(user) == 0) { + user_stop(user); + user_free(user); + } + } +} + int manager_startup(Manager *m) { int r; + Seat *seat; + Session *session; + User *user; + Iterator i; assert(m); assert(m->epoll_fd <= 0); @@ -830,14 +890,29 @@ int manager_startup(Manager *m) { if (r < 0) return r; + /* Instantiate magic seat 0 */ + r = manager_add_seat(m, "seat0", &m->vtconsole); + if (r < 0) + return r; + /* Deserialize state */ manager_enumerate_devices(m); manager_enumerate_seats(m); manager_enumerate_users(m); manager_enumerate_sessions(m); - /* Spawn lingering users */ - manager_start_linger_users(m); + /* Get rid of objects that are no longer used */ + manager_gc(m); + + /* And start everything */ + HASHMAP_FOREACH(seat, m->seats, i) + seat_start(seat); + + HASHMAP_FOREACH(user, m->users, i) + user_start(user); + + HASHMAP_FOREACH(session, m->sessions, i) + session_start(session); return 0; } @@ -849,6 +924,8 @@ int manager_run(Manager *m) { struct epoll_event event; int n; + manager_gc(m); + if (dbus_connection_dispatch(m->bus) != DBUS_DISPATCH_COMPLETE) continue; diff --git a/src/logind.h b/src/logind.h index be5dab74..7a312162 100644 --- a/src/logind.h +++ b/src/logind.h @@ -59,6 +59,10 @@ struct Manager { Hashmap *sessions; Hashmap *users; + LIST_HEAD(Seat, seat_gc_queue); + LIST_HEAD(Session, session_gc_queue); + LIST_HEAD(User, user_gc_queue); + struct udev *udev; struct udev_monitor *udev_monitor; @@ -81,25 +85,29 @@ struct Manager { Manager *manager_new(void); void manager_free(Manager *m); + int manager_add_device(Manager *m, const char *sysfs, Device **_device); int manager_add_seat(Manager *m, const char *id, Seat **_seat); int manager_add_session(Manager *m, User *u, const char *id, Session **_session); int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user); int manager_add_user_by_name(Manager *m, const char *name, User **_user); int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user); + int manager_process_device(Manager *m, struct udev_device *d); int manager_dispatch_udev(Manager *m); int manager_dispatch_console(Manager *m); + int manager_enumerate_devices(Manager *m); int manager_enumerate_seats(Manager *m); int manager_enumerate_sessions(Manager *m); int manager_enumerate_users(Manager *m); -int manager_start_one_linger_user(Manager *m, const char *user); -int manager_start_linger_users(Manager *m); + int manager_startup(Manager *m); int manager_run(Manager *m); int manager_spawn_autovt(Manager *m, int vtnr); +void manager_gc(Manager *m); + bool x11_display_is_local(const char *display); #endif diff --git a/src/org.freedesktop.login1.conf b/src/org.freedesktop.login1.conf new file mode 100644 index 00000000..20008ea4 --- /dev/null +++ b/src/org.freedesktop.login1.conf @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + +