chiark / gitweb /
logind: implement D-Bus properties
[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                 s->manager->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         return 0;
162 }
163
164 static int vt_allocate(int vtnr) {
165         int fd, r;
166         char *p;
167
168         assert(vtnr >= 1);
169
170         if (asprintf(&p, "/dev/tty%i", vtnr) < 0)
171                 return -ENOMEM;
172
173         fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
174         free(p);
175
176         r = fd < 0 ? -errno : 0;
177
178         if (fd >= 0)
179                 close_nointr_nofail(fd);
180
181         return r;
182 }
183
184 static int seat_preallocate_vts(Seat *s) {
185         int r = 0;
186         unsigned i;
187
188         assert(s);
189         assert(s->manager);
190
191         if (s->manager->n_autovts <= 0)
192                 return 0;
193
194         if (s->manager->vtconsole != s)
195                 return 0;
196
197         for (i = 1; i < s->manager->n_autovts; i++) {
198                 int q;
199
200                 q = vt_allocate(i);
201                 if (q < 0) {
202                         log_error("Failed to preallocate VT %i: %s", i, strerror(-q));
203                         r = q;
204                 }
205         }
206
207         return r;
208 }
209
210 int seat_apply_acls(Seat *s, Session *old_active) {
211         int r;
212
213         assert(s);
214
215         r = devnode_acl_all(s->manager->udev,
216                             s->id,
217                             false,
218                             !!old_active, old_active ? old_active->user->uid : 0,
219                             !!s->active, s->active ? s->active->user->uid : 0);
220
221         if (r < 0)
222                 log_error("Failed to apply ACLs: %s", strerror(-r));
223
224         return r;
225 }
226
227 int seat_active_vt_changed(Seat *s, int vtnr) {
228         Session *i, *new_active = NULL, *old_active;
229
230         assert(s);
231         assert(vtnr >= 1);
232
233         if (s->manager->vtconsole != s)
234                 return -EINVAL;
235
236         log_debug("VT changed to %i", vtnr);
237
238         LIST_FOREACH(sessions_by_seat, i, s->sessions)
239                 if (i->vtnr == vtnr) {
240                         new_active = i;
241                         break;
242                 }
243
244         if (new_active == s->active)
245                 return 0;
246
247         old_active = s->active;
248         s->active = new_active;
249
250         seat_apply_acls(s, old_active);
251         manager_spawn_autovt(s->manager, vtnr);
252
253         return 0;
254 }
255
256 int seat_read_active_vt(Seat *s) {
257         char t[64];
258         ssize_t k;
259         int r, vtnr;
260
261         assert(s);
262
263         if (s->manager->vtconsole != s)
264                 return 0;
265
266         lseek(s->manager->console_active_fd, SEEK_SET, 0);
267
268         k = read(s->manager->console_active_fd, t, sizeof(t)-1);
269         if (k <= 0) {
270                 log_error("Failed to read current console: %s", k < 0 ? strerror(-errno) : "EOF");
271                 return k < 0 ? -errno : -EIO;
272         }
273
274         t[k] = 0;
275         truncate_nl(t);
276
277         if (!startswith(t, "tty")) {
278                 log_error("Hm, /sys/class/tty/tty0/active is badly formatted.");
279                 return -EIO;
280         }
281
282         r = safe_atoi(t+3, &vtnr);
283         if (r < 0) {
284                 log_error("Failed to parse VT number %s", t+3);
285                 return r;
286         }
287
288         if (vtnr <= 0) {
289                 log_error("VT number invalid: %s", t+3);
290                 return -EIO;
291         }
292
293         return seat_active_vt_changed(s, vtnr);
294 }
295
296 int seat_start(Seat *s) {
297         assert(s);
298
299         if (s->started)
300                 return 0;
301
302         log_info("New seat %s.", s->id);
303
304         /* Initialize VT magic stuff */
305         seat_preallocate_vts(s);
306
307         /* Read current VT */
308         seat_read_active_vt(s);
309
310         /* Save seat data */
311         seat_save(s);
312
313         s->started = true;
314
315         return 0;
316 }
317
318 int seat_stop(Seat *s) {
319         Session *session;
320         int r = 0, k;
321
322         assert(s);
323
324         if (!s->started)
325                 return 0;
326
327         log_info("Removed seat %s.", s->id);
328
329         LIST_FOREACH(sessions_by_seat, session, s->sessions) {
330                 k = session_stop(session);
331                 if (k < 0)
332                         r = k;
333         }
334
335         unlink(s->state_file);
336         seat_add_to_gc_queue(s);
337
338         s->started = false;
339
340         return r;
341 }
342
343 int seat_check_gc(Seat *s) {
344         assert(s);
345
346         if (s->manager->vtconsole == s)
347                 return 1;
348
349         return !!s->devices;
350 }
351
352 void seat_add_to_gc_queue(Seat *s) {
353         assert(s);
354
355         if (s->in_gc_queue)
356                 return;
357
358         LIST_PREPEND(Seat, gc_queue, s->manager->seat_gc_queue, s);
359         s->in_gc_queue = true;
360 }
361
362 static bool seat_name_valid_char(char c) {
363         return
364                 (c >= 'a' && c <= 'z') ||
365                 (c >= 'A' && c <= 'Z') ||
366                 (c >= '0' && c <= '9') ||
367                 c == '-' ||
368                 c == '_';
369 }
370
371 bool seat_name_is_valid(const char *name) {
372         const char *p;
373
374         assert(name);
375
376         if (!startswith(name, "seat"))
377                 return false;
378
379         if (!name[4])
380                 return false;
381
382         for (p = name; *p; p++)
383                 if (!seat_name_valid_char(*p))
384                         return false;
385
386         return true;
387 }