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>
29 #include "logind-seat.h"
32 Seat *seat_new(Manager *m, const char *id) {
42 s->state_file = strappend("/run/systemd/seat/", id);
48 s->id = file_name_from_path(s->state_file);
50 if (hashmap_put(m->seats, s->id, s) < 0) {
61 void seat_free(Seat *s) {
65 session_free(s->sessions);
70 device_free(s->devices);
72 hashmap_remove(s->manager->seats, s->id);
78 int seat_save(Seat *s) {
84 r = safe_mkdir("/run/systemd/seat", 0755, 0, 0);
88 f = fopen(s->state_file, "we");
94 s->manager->vtconsole == s);
97 assert(s->active->user);
103 (unsigned long) s->active->user->uid);
108 fputs("OTHER_UIDS=", f);
110 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
118 i == s->sessions ? "" : " ",
119 (unsigned long) i->user->uid);
126 unlink(s->state_file);
133 int seat_load(Seat *s) {
139 static int vt_allocate(int vtnr) {
145 if (asprintf(&p, "/dev/tty%i", vtnr) < 0)
148 fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
151 r = fd < 0 ? -errno : 0;
154 close_nointr_nofail(fd);
159 int seat_preallocate_vts(Seat *s) {
165 if (s->manager->n_autovts <= 0)
168 if (s->manager->vtconsole != s)
171 for (i = 1; i < s->manager->n_autovts; i++) {
182 int seat_apply_acls(Seat *s) {
189 static int vt_is_busy(int vtnr) {
190 struct vt_stat vt_stat;
195 fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
199 if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0)
202 r = !!(vt_stat.v_state & (1 << vtnr));
204 close_nointr_nofail(fd);
209 void seat_active_vt_changed(Seat *s, int vtnr) {
214 assert(s->manager->vtconsole == s);
218 LIST_FOREACH(sessions_by_seat, i, s->sessions)
219 if (i->vtnr == vtnr) {
226 if (vt_is_busy(vtnr) == 0)
227 manager_spawn_autovt(s->manager, vtnr);
230 int seat_stop(Seat *s) {
236 LIST_FOREACH(sessions_by_seat, session, s->sessions) {
239 k = session_stop(session);