1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/ioctl.h>
30 #include "logind-seat.h"
31 #include "logind-acl.h"
34 Seat *seat_new(Manager *m, const char *id) {
44 s->state_file = strappend("/run/systemd/seat/", id);
50 s->id = file_name_from_path(s->state_file);
53 if (hashmap_put(m->seats, s->id, s) < 0) {
62 void seat_free(Seat *s) {
66 LIST_REMOVE(Seat, gc_queue, s->manager->seat_gc_queue, s);
69 session_free(s->sessions);
74 device_free(s->devices);
76 hashmap_remove(s->manager->seats, s->id);
79 unlink(s->state_file);
86 int seat_save(Seat *s) {
93 r = safe_mkdir("/run/systemd/seat", 0755, 0, 0);
97 r = fopen_temporary(s->state_file, &f, &temp_path);
101 fchmod(fileno(f), 0644);
104 "# This is private data. Do not parse.\n"
106 s->manager->vtconsole == s);
109 assert(s->active->user);
115 (unsigned long) s->active->user->uid);
122 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
129 i->sessions_by_seat_next ? ' ' : '\n');
132 fputs("OTHER_UIDS=", f);
133 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
139 (unsigned long) i->user->uid,
140 i->sessions_by_seat_next ? ' ' : '\n');
146 if (ferror(f) || rename(temp_path, s->state_file) < 0) {
148 unlink(s->state_file);
157 log_error("Failed to save seat data for %s: %s", s->id, strerror(-r));
162 int seat_load(Seat *s) {
168 static int vt_allocate(int vtnr) {
174 if (asprintf(&p, "/dev/tty%i", vtnr) < 0)
177 fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
180 r = fd < 0 ? -errno : 0;
183 close_nointr_nofail(fd);
188 static int seat_preallocate_vts(Seat *s) {
194 if (s->manager->n_autovts <= 0)
197 if (s->manager->vtconsole != s)
200 for (i = 1; i < s->manager->n_autovts; i++) {
205 log_error("Failed to preallocate VT %i: %s", i, strerror(-q));
213 int seat_apply_acls(Seat *s, Session *old_active) {
218 r = devnode_acl_all(s->manager->udev,
221 !!old_active, old_active ? old_active->user->uid : 0,
222 !!s->active, s->active ? s->active->user->uid : 0);
225 log_error("Failed to apply ACLs: %s", strerror(-r));
230 int seat_active_vt_changed(Seat *s, int vtnr) {
231 Session *i, *new_active = NULL, *old_active;
236 if (s->manager->vtconsole != s)
239 log_debug("VT changed to %i", vtnr);
241 LIST_FOREACH(sessions_by_seat, i, s->sessions)
242 if (i->vtnr == vtnr) {
247 if (new_active == s->active)
250 old_active = s->active;
251 s->active = new_active;
253 seat_apply_acls(s, old_active);
254 manager_spawn_autovt(s->manager, vtnr);
259 int seat_read_active_vt(Seat *s) {
266 if (s->manager->vtconsole != s)
269 lseek(s->manager->console_active_fd, SEEK_SET, 0);
271 k = read(s->manager->console_active_fd, t, sizeof(t)-1);
273 log_error("Failed to read current console: %s", k < 0 ? strerror(-errno) : "EOF");
274 return k < 0 ? -errno : -EIO;
280 if (!startswith(t, "tty")) {
281 log_error("Hm, /sys/class/tty/tty0/active is badly formatted.");
285 r = safe_atoi(t+3, &vtnr);
287 log_error("Failed to parse VT number %s", t+3);
292 log_error("VT number invalid: %s", t+3);
296 return seat_active_vt_changed(s, vtnr);
299 int seat_start(Seat *s) {
302 /* Initialize VT magic stuff */
303 seat_preallocate_vts(s);
305 /* Read current VT */
306 seat_read_active_vt(s);
314 int seat_stop(Seat *s) {
320 LIST_FOREACH(sessions_by_seat, session, s->sessions) {
321 k = session_stop(session);
327 seat_add_to_gc_queue(s);
332 int seat_check_gc(Seat *s) {
335 if (s->manager->vtconsole == s)
341 void seat_add_to_gc_queue(Seat *s) {
347 LIST_PREPEND(Seat, gc_queue, s->manager->seat_gc_queue, s);
348 s->in_gc_queue = true;