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/seats/", 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);
82 int seat_save(Seat *s) {
92 r = safe_mkdir("/run/systemd/seats", 0755, 0, 0);
96 r = fopen_temporary(s->state_file, &f, &temp_path);
100 fchmod(fileno(f), 0644);
103 "# This is private data. Do not parse.\n"
105 seat_is_vtconsole(s));
108 assert(s->active->user);
114 (unsigned long) s->active->user->uid);
121 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
128 i->sessions_by_seat_next ? ' ' : '\n');
131 fputs("OTHER_UIDS=", f);
132 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
138 (unsigned long) i->user->uid,
139 i->sessions_by_seat_next ? ' ' : '\n');
145 if (ferror(f) || rename(temp_path, s->state_file) < 0) {
147 unlink(s->state_file);
156 log_error("Failed to save seat data for %s: %s", s->id, strerror(-r));
161 int seat_load(Seat *s) {
164 /* There isn't actually anything to read here ... */
169 static int vt_allocate(int vtnr) {
175 if (asprintf(&p, "/dev/tty%i", vtnr) < 0)
178 fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
181 r = fd < 0 ? -errno : 0;
184 close_nointr_nofail(fd);
189 int seat_preallocate_vts(Seat *s) {
196 log_debug("Preallocating VTs...");
198 if (s->manager->n_autovts <= 0)
201 if (!seat_is_vtconsole(s))
204 for (i = 1; i <= s->manager->n_autovts; i++) {
209 log_error("Failed to preallocate VT %i: %s", i, strerror(-q));
217 int seat_apply_acls(Seat *s, Session *old_active) {
222 r = devnode_acl_all(s->manager->udev,
225 !!old_active, old_active ? old_active->user->uid : 0,
226 !!s->active, s->active ? s->active->user->uid : 0);
229 log_error("Failed to apply ACLs: %s", strerror(-r));
234 int seat_set_active(Seat *s, Session *session) {
238 assert(!session || session->seat == s);
240 if (session == s->active)
243 old_active = s->active;
246 seat_apply_acls(s, old_active);
248 if (session && session->started)
249 session_send_changed(session, "Active\0");
251 if (!session || session->started)
252 seat_send_changed(s, "ActiveSession\0");
257 session_save(session);
258 user_save(session->user);
262 session_save(old_active);
263 user_save(old_active->user);
269 int seat_active_vt_changed(Seat *s, int vtnr) {
270 Session *i, *new_active = NULL;
276 if (!seat_is_vtconsole(s))
279 log_debug("VT changed to %i", vtnr);
281 LIST_FOREACH(sessions_by_seat, i, s->sessions)
282 if (i->vtnr == vtnr) {
287 r = seat_set_active(s, new_active);
288 manager_spawn_autovt(s->manager, vtnr);
293 int seat_read_active_vt(Seat *s) {
300 if (!seat_is_vtconsole(s))
303 lseek(s->manager->console_active_fd, SEEK_SET, 0);
305 k = read(s->manager->console_active_fd, t, sizeof(t)-1);
307 log_error("Failed to read current console: %s", k < 0 ? strerror(-errno) : "EOF");
308 return k < 0 ? -errno : -EIO;
314 if (!startswith(t, "tty")) {
315 log_error("Hm, /sys/class/tty/tty0/active is badly formatted.");
319 r = safe_atoi(t+3, &vtnr);
321 log_error("Failed to parse VT number %s", t+3);
326 log_error("VT number invalid: %s", t+3);
330 return seat_active_vt_changed(s, vtnr);
333 int seat_start(Seat *s) {
339 log_info("New seat %s.", s->id);
341 /* Initialize VT magic stuff */
342 seat_preallocate_vts(s);
344 /* Read current VT */
345 seat_read_active_vt(s);
352 seat_send_signal(s, true);
357 int seat_stop(Seat *s) {
363 log_info("Removed seat %s.", s->id);
365 seat_stop_sessions(s);
367 unlink(s->state_file);
368 seat_add_to_gc_queue(s);
371 seat_send_signal(s, false);
378 int seat_stop_sessions(Seat *s) {
384 LIST_FOREACH(sessions_by_seat, session, s->sessions) {
385 k = session_stop(session);
393 int seat_attach_session(Seat *s, Session *session) {
396 assert(!session->seat);
398 if (!seat_is_vtconsole(s) && s->sessions)
402 LIST_PREPEND(Session, sessions_by_seat, s->sessions, session);
404 seat_send_changed(s, "Sessions\0");
406 if (!seat_is_vtconsole(s)) {
408 seat_set_active(s, session);
414 bool seat_is_vtconsole(Seat *s) {
417 return s->manager->vtconsole == s;
420 int seat_get_idle_hint(Seat *s, dual_timestamp *t) {
422 bool idle_hint = true;
423 dual_timestamp ts = { 0, 0 };
427 LIST_FOREACH(sessions_by_seat, session, s->sessions) {
431 ih = session_get_idle_hint(session, &k);
437 if (k.monotonic < ts.monotonic)
443 } else if (idle_hint) {
445 if (k.monotonic > ts.monotonic)
456 int seat_check_gc(Seat *s) {
459 if (seat_is_vtconsole(s))
465 void seat_add_to_gc_queue(Seat *s) {
471 LIST_PREPEND(Seat, gc_queue, s->manager->seat_gc_queue, s);
472 s->in_gc_queue = true;
475 static bool seat_name_valid_char(char c) {
477 (c >= 'a' && c <= 'z') ||
478 (c >= 'A' && c <= 'Z') ||
479 (c >= '0' && c <= '9') ||
484 bool seat_name_is_valid(const char *name) {
489 if (!startswith(name, "seat"))
495 for (p = name; *p; p++)
496 if (!seat_name_valid_char(*p))