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) {
89 r = safe_mkdir("/run/systemd/seats", 0755, 0, 0);
93 r = fopen_temporary(s->state_file, &f, &temp_path);
97 fchmod(fileno(f), 0644);
100 "# This is private data. Do not parse.\n"
102 seat_is_vtconsole(s));
105 assert(s->active->user);
111 (unsigned long) s->active->user->uid);
118 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
125 i->sessions_by_seat_next ? ' ' : '\n');
128 fputs("OTHER_UIDS=", f);
129 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
135 (unsigned long) i->user->uid,
136 i->sessions_by_seat_next ? ' ' : '\n');
142 if (ferror(f) || rename(temp_path, s->state_file) < 0) {
144 unlink(s->state_file);
153 log_error("Failed to save seat data for %s: %s", s->id, strerror(-r));
158 int seat_load(Seat *s) {
161 /* There isn't actually anything to read here ... */
166 static int vt_allocate(int vtnr) {
172 if (asprintf(&p, "/dev/tty%i", vtnr) < 0)
175 fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
178 r = fd < 0 ? -errno : 0;
181 close_nointr_nofail(fd);
186 static int seat_preallocate_vts(Seat *s) {
193 if (s->manager->n_autovts <= 0)
196 if (!seat_is_vtconsole(s))
199 for (i = 1; i < s->manager->n_autovts; i++) {
204 log_error("Failed to preallocate VT %i: %s", i, strerror(-q));
212 int seat_apply_acls(Seat *s, Session *old_active) {
217 r = devnode_acl_all(s->manager->udev,
220 !!old_active, old_active ? old_active->user->uid : 0,
221 !!s->active, s->active ? s->active->user->uid : 0);
224 log_error("Failed to apply ACLs: %s", strerror(-r));
229 int seat_set_active(Seat *s, Session *session) {
233 assert(!session || session->seat == s);
235 if (session == s->active)
238 old_active = s->active;
241 seat_apply_acls(s, old_active);
243 if (session && session->started)
244 session_send_changed(session, "Active\0");
246 if (!session || session->started)
247 seat_send_changed(s, "ActiveSession\0");
252 session_save(session);
255 session_save(old_active);
260 int seat_active_vt_changed(Seat *s, int vtnr) {
261 Session *i, *new_active = NULL;
267 if (!seat_is_vtconsole(s))
270 log_debug("VT changed to %i", vtnr);
272 LIST_FOREACH(sessions_by_seat, i, s->sessions)
273 if (i->vtnr == vtnr) {
278 r = seat_set_active(s, new_active);
279 manager_spawn_autovt(s->manager, vtnr);
284 int seat_read_active_vt(Seat *s) {
291 if (!seat_is_vtconsole(s))
294 lseek(s->manager->console_active_fd, SEEK_SET, 0);
296 k = read(s->manager->console_active_fd, t, sizeof(t)-1);
298 log_error("Failed to read current console: %s", k < 0 ? strerror(-errno) : "EOF");
299 return k < 0 ? -errno : -EIO;
305 if (!startswith(t, "tty")) {
306 log_error("Hm, /sys/class/tty/tty0/active is badly formatted.");
310 r = safe_atoi(t+3, &vtnr);
312 log_error("Failed to parse VT number %s", t+3);
317 log_error("VT number invalid: %s", t+3);
321 return seat_active_vt_changed(s, vtnr);
324 int seat_start(Seat *s) {
330 log_info("New seat %s.", s->id);
332 /* Initialize VT magic stuff */
333 seat_preallocate_vts(s);
335 /* Read current VT */
336 seat_read_active_vt(s);
343 seat_send_signal(s, true);
348 int seat_stop(Seat *s) {
354 log_info("Removed seat %s.", s->id);
356 seat_stop_sessions(s);
358 unlink(s->state_file);
359 seat_add_to_gc_queue(s);
362 seat_send_signal(s, false);
369 int seat_stop_sessions(Seat *s) {
375 LIST_FOREACH(sessions_by_seat, session, s->sessions) {
376 k = session_stop(session);
384 int seat_attach_session(Seat *s, Session *session) {
387 assert(!session->seat);
389 if (!seat_is_vtconsole(s) && s->sessions)
393 LIST_PREPEND(Session, sessions_by_seat, s->sessions, session);
395 seat_send_changed(s, "Sessions\0");
397 if (!seat_is_vtconsole(s)) {
399 seat_set_active(s, session);
405 bool seat_is_vtconsole(Seat *s) {
408 return s->manager->vtconsole == s;
411 int seat_get_idle_hint(Seat *s, dual_timestamp *t) {
413 bool idle_hint = true;
414 dual_timestamp ts = { 0, 0 };
418 LIST_FOREACH(sessions_by_seat, session, s->sessions) {
422 ih = session_get_idle_hint(session, &k);
428 if (k.monotonic < ts.monotonic)
434 } else if (idle_hint) {
436 if (k.monotonic > ts.monotonic)
447 int seat_check_gc(Seat *s) {
450 if (seat_is_vtconsole(s))
456 void seat_add_to_gc_queue(Seat *s) {
462 LIST_PREPEND(Seat, gc_queue, s->manager->seat_gc_queue, s);
463 s->in_gc_queue = true;
466 static bool seat_name_valid_char(char c) {
468 (c >= 'a' && c <= 'z') ||
469 (c >= 'A' && c <= 'Z') ||
470 (c >= '0' && c <= '9') ||
475 bool seat_name_is_valid(const char *name) {
480 if (!startswith(name, "seat"))
486 for (p = name; *p; p++)
487 if (!seat_name_valid_char(*p))