chiark / gitweb /
95c66dd7125649cff8d54efbe6c475e65109bc9f
[elogind.git] / src / logind-seat.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2011 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <assert.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/ioctl.h>
27 #include <linux/vt.h>
28 #include <string.h>
29
30 #include "logind-seat.h"
31 #include "logind-acl.h"
32 #include "util.h"
33
34 Seat *seat_new(Manager *m, const char *id) {
35         Seat *s;
36
37         assert(m);
38         assert(id);
39
40         s = new0(Seat, 1);
41         if (!s)
42                 return NULL;
43
44         s->state_file = strappend("/run/systemd/seat/", id);
45         if (!s->state_file) {
46                 free(s);
47                 return NULL;
48         }
49
50         s->id = file_name_from_path(s->state_file);
51         s->manager = m;
52
53         if (hashmap_put(m->seats, s->id, s) < 0) {
54                 free(s->state_file);
55                 free(s);
56                 return NULL;
57         }
58
59         return s;
60 }
61
62 void seat_free(Seat *s) {
63         assert(s);
64
65         if (s->in_gc_queue)
66                 LIST_REMOVE(Seat, gc_queue, s->manager->seat_gc_queue, s);
67
68         while (s->sessions)
69                 session_free(s->sessions);
70
71         assert(!s->active);
72
73         while (s->devices)
74                 device_free(s->devices);
75
76         hashmap_remove(s->manager->seats, s->id);
77
78         free(s->state_file);
79         free(s);
80 }
81
82 int seat_save(Seat *s) {
83         int r;
84         FILE *f;
85         char *temp_path;
86
87         assert(s);
88
89         r = safe_mkdir("/run/systemd/seat", 0755, 0, 0);
90         if (r < 0)
91                 goto finish;
92
93         r = fopen_temporary(s->state_file, &f, &temp_path);
94         if (r < 0)
95                 goto finish;
96
97         fchmod(fileno(f), 0644);
98
99         fprintf(f,
100                 "# This is private data. Do not parse.\n"
101                 "IS_VTCONSOLE=%i\n",
102                 seat_is_vtconsole(s));
103
104         if (s->active) {
105                 assert(s->active->user);
106
107                 fprintf(f,
108                         "ACTIVE=%s\n"
109                         "ACTIVE_UID=%lu\n",
110                         s->active->id,
111                         (unsigned long) s->active->user->uid);
112         }
113
114         if (s->sessions) {
115                 Session *i;
116
117                 fputs("OTHER=", f);
118                 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
119                         if (i == s->active)
120                                 continue;
121
122                         fprintf(f,
123                                 "%s%c",
124                                 i->id,
125                                 i->sessions_by_seat_next ? ' ' : '\n');
126                 }
127
128                 fputs("OTHER_UIDS=", f);
129                 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
130                         if (i == s->active)
131                                 continue;
132
133                         fprintf(f,
134                                 "%lu%c",
135                                 (unsigned long) i->user->uid,
136                                 i->sessions_by_seat_next ? ' ' : '\n');
137                 }
138         }
139
140         fflush(f);
141
142         if (ferror(f) || rename(temp_path, s->state_file) < 0) {
143                 r = -errno;
144                 unlink(s->state_file);
145                 unlink(temp_path);
146         }
147
148         fclose(f);
149         free(temp_path);
150
151 finish:
152         if (r < 0)
153                 log_error("Failed to save seat data for %s: %s", s->id, strerror(-r));
154
155         return r;
156 }
157
158 int seat_load(Seat *s) {
159         assert(s);
160
161         /* There isn't actually anything to read here ... */
162
163         return 0;
164 }
165
166 static int vt_allocate(int vtnr) {
167         int fd, r;
168         char *p;
169
170         assert(vtnr >= 1);
171
172         if (asprintf(&p, "/dev/tty%i", vtnr) < 0)
173                 return -ENOMEM;
174
175         fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
176         free(p);
177
178         r = fd < 0 ? -errno : 0;
179
180         if (fd >= 0)
181                 close_nointr_nofail(fd);
182
183         return r;
184 }
185
186 static int seat_preallocate_vts(Seat *s) {
187         int r = 0;
188         unsigned i;
189
190         assert(s);
191         assert(s->manager);
192
193         if (s->manager->n_autovts <= 0)
194                 return 0;
195
196         if (!seat_is_vtconsole(s))
197                 return 0;
198
199         for (i = 1; i < s->manager->n_autovts; i++) {
200                 int q;
201
202                 q = vt_allocate(i);
203                 if (q < 0) {
204                         log_error("Failed to preallocate VT %i: %s", i, strerror(-q));
205                         r = q;
206                 }
207         }
208
209         return r;
210 }
211
212 int seat_apply_acls(Seat *s, Session *old_active) {
213         int r;
214
215         assert(s);
216
217         r = devnode_acl_all(s->manager->udev,
218                             s->id,
219                             false,
220                             !!old_active, old_active ? old_active->user->uid : 0,
221                             !!s->active, s->active ? s->active->user->uid : 0);
222
223         if (r < 0)
224                 log_error("Failed to apply ACLs: %s", strerror(-r));
225
226         return r;
227 }
228
229 int seat_active_vt_changed(Seat *s, int vtnr) {
230         Session *i, *new_active = NULL, *old_active;
231
232         assert(s);
233         assert(vtnr >= 1);
234
235         if (!seat_is_vtconsole(s))
236                 return -EINVAL;
237
238         log_debug("VT changed to %i", vtnr);
239
240         LIST_FOREACH(sessions_by_seat, i, s->sessions)
241                 if (i->vtnr == vtnr) {
242                         new_active = i;
243                         break;
244                 }
245
246         if (new_active == s->active)
247                 return 0;
248
249         old_active = s->active;
250         s->active = new_active;
251
252         seat_apply_acls(s, old_active);
253         manager_spawn_autovt(s->manager, vtnr);
254
255         return 0;
256 }
257
258 int seat_read_active_vt(Seat *s) {
259         char t[64];
260         ssize_t k;
261         int r, vtnr;
262
263         assert(s);
264
265         if (!seat_is_vtconsole(s))
266                 return 0;
267
268         lseek(s->manager->console_active_fd, SEEK_SET, 0);
269
270         k = read(s->manager->console_active_fd, t, sizeof(t)-1);
271         if (k <= 0) {
272                 log_error("Failed to read current console: %s", k < 0 ? strerror(-errno) : "EOF");
273                 return k < 0 ? -errno : -EIO;
274         }
275
276         t[k] = 0;
277         truncate_nl(t);
278
279         if (!startswith(t, "tty")) {
280                 log_error("Hm, /sys/class/tty/tty0/active is badly formatted.");
281                 return -EIO;
282         }
283
284         r = safe_atoi(t+3, &vtnr);
285         if (r < 0) {
286                 log_error("Failed to parse VT number %s", t+3);
287                 return r;
288         }
289
290         if (vtnr <= 0) {
291                 log_error("VT number invalid: %s", t+3);
292                 return -EIO;
293         }
294
295         return seat_active_vt_changed(s, vtnr);
296 }
297
298 int seat_start(Seat *s) {
299         assert(s);
300
301         if (s->started)
302                 return 0;
303
304         log_info("New seat %s.", s->id);
305
306         /* Initialize VT magic stuff */
307         seat_preallocate_vts(s);
308
309         /* Read current VT */
310         seat_read_active_vt(s);
311
312         /* Save seat data */
313         seat_save(s);
314
315         s->started = true;
316
317         seat_send_signal(s, true);
318
319         return 0;
320 }
321
322 int seat_stop(Seat *s) {
323         int r = 0;
324
325         assert(s);
326
327         if (!s->started)
328                 return 0;
329
330         log_info("Removed seat %s.", s->id);
331
332         seat_send_signal(s, false);
333
334         seat_stop_sessions(s);
335
336         unlink(s->state_file);
337         seat_add_to_gc_queue(s);
338
339         s->started = false;
340
341         return r;
342 }
343
344 int seat_stop_sessions(Seat *s) {
345         Session *session;
346         int r = 0, k;
347
348         assert(s);
349
350         LIST_FOREACH(sessions_by_seat, session, s->sessions) {
351                 k = session_stop(session);
352                 if (k < 0)
353                         r = k;
354         }
355
356         return r;
357 }
358
359 int seat_attach_session(Seat *s, Session *session) {
360         assert(s);
361         assert(session);
362         assert(!session->seat);
363
364         if (!seat_is_vtconsole(s)) {
365                 if (s->sessions)
366                         return -EEXIST;
367
368                 assert(!s->active);
369                 s->active = session;
370         }
371
372         session->seat = s;
373         LIST_PREPEND(Session, sessions_by_seat, s->sessions, session);
374
375         return 0;
376 }
377
378 bool seat_is_vtconsole(Seat *s) {
379         assert(s);
380
381         return s->manager->vtconsole == s;
382 }
383
384 int seat_get_idle_hint(Seat *s, dual_timestamp *t) {
385         Session *session;
386         bool idle_hint = true;
387         dual_timestamp ts = { 0, 0 };
388
389         assert(s);
390
391         LIST_FOREACH(sessions_by_seat, session, s->sessions) {
392                 dual_timestamp k;
393                 int ih;
394
395                 ih = session_get_idle_hint(session, &k);
396                 if (ih < 0)
397                         return ih;
398
399                 if (!ih) {
400                         if (!idle_hint) {
401                                 if (k.monotonic < ts.monotonic)
402                                         ts = k;
403                         } else {
404                                 idle_hint = false;
405                                 ts = k;
406                         }
407                 } else if (idle_hint) {
408
409                         if (k.monotonic > ts.monotonic)
410                                 ts = k;
411                 }
412         }
413
414         if (t)
415                 *t = ts;
416
417         return idle_hint;
418 }
419
420 int seat_check_gc(Seat *s) {
421         assert(s);
422
423         if (seat_is_vtconsole(s))
424                 return 1;
425
426         return !!s->devices;
427 }
428
429 void seat_add_to_gc_queue(Seat *s) {
430         assert(s);
431
432         if (s->in_gc_queue)
433                 return;
434
435         LIST_PREPEND(Seat, gc_queue, s->manager->seat_gc_queue, s);
436         s->in_gc_queue = true;
437 }
438
439 static bool seat_name_valid_char(char c) {
440         return
441                 (c >= 'a' && c <= 'z') ||
442                 (c >= 'A' && c <= 'Z') ||
443                 (c >= '0' && c <= '9') ||
444                 c == '-' ||
445                 c == '_';
446 }
447
448 bool seat_name_is_valid(const char *name) {
449         const char *p;
450
451         assert(name);
452
453         if (!startswith(name, "seat"))
454                 return false;
455
456         if (!name[4])
457                 return false;
458
459         for (p = name; *p; p++)
460                 if (!seat_name_valid_char(*p))
461                         return false;
462
463         return true;
464 }