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);
52 if (hashmap_put(m->seats, s->id, s) < 0) {
63 void seat_free(Seat *s) {
67 session_free(s->sessions);
72 device_free(s->devices);
74 hashmap_remove(s->manager->seats, s->id);
80 int seat_save(Seat *s) {
86 r = safe_mkdir("/run/systemd/seat", 0755, 0, 0);
90 f = fopen(s->state_file, "we");
96 s->manager->vtconsole == s);
99 assert(s->active->user);
105 (unsigned long) s->active->user->uid);
110 fputs("OTHER_UIDS=", f);
112 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
120 i == s->sessions ? "" : " ",
121 (unsigned long) i->user->uid);
128 unlink(s->state_file);
135 int seat_load(Seat *s) {
141 static int vt_allocate(int vtnr) {
147 if (asprintf(&p, "/dev/tty%i", vtnr) < 0)
150 fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
153 r = fd < 0 ? -errno : 0;
156 close_nointr_nofail(fd);
161 int seat_preallocate_vts(Seat *s) {
167 if (s->manager->n_autovts <= 0)
170 if (s->manager->vtconsole != s)
173 for (i = 1; i < s->manager->n_autovts; i++) {
184 int seat_apply_acls(Seat *s, Session *old_active) {
189 r = devnode_acl_all(s->manager->udev,
192 !!old_active, old_active ? old_active->user->uid : 0,
193 !!s->active, s->active ? s->active->user->uid : 0);
196 log_error("Failed to apply ACLs: %s", strerror(-r));
201 int seat_active_vt_changed(Seat *s, int vtnr) {
207 assert(s->manager->vtconsole == s);
209 old_active = s->active;
212 LIST_FOREACH(sessions_by_seat, i, s->sessions)
213 if (i->vtnr == vtnr) {
218 if (old_active == s->active)
221 seat_apply_acls(s, old_active);
222 manager_spawn_autovt(s->manager, vtnr);
227 int seat_stop(Seat *s) {
233 LIST_FOREACH(sessions_by_seat, session, s->sessions) {
236 k = session_stop(session);