chiark / gitweb /
logind: implement GC
[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         if (s->state_file) {
79                 unlink(s->state_file);
80                 free(s->state_file);
81         }
82
83         free(s);
84 }
85
86 int seat_save(Seat *s) {
87         int r;
88         FILE *f;
89         char *temp_path;
90
91         assert(s);
92
93         r = safe_mkdir("/run/systemd/seat", 0755, 0, 0);
94         if (r < 0)
95                 goto finish;
96
97         r = fopen_temporary(s->state_file, &f, &temp_path);
98         if (r < 0)
99                 goto finish;
100
101         fchmod(fileno(f), 0644);
102
103         fprintf(f,
104                 "# This is private data. Do not parse.\n"
105                 "IS_VTCONSOLE=%i\n",
106                 s->manager->vtconsole == s);
107
108         if (s->active) {
109                 assert(s->active->user);
110
111                 fprintf(f,
112                         "ACTIVE=%s\n"
113                         "ACTIVE_UID=%lu\n",
114                         s->active->id,
115                         (unsigned long) s->active->user->uid);
116         }
117
118         if (s->sessions) {
119                 Session *i;
120
121                 fputs("OTHER=", f);
122                 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
123                         if (i == s->active)
124                                 continue;
125
126                         fprintf(f,
127                                 "%s%c",
128                                 i->id,
129                                 i->sessions_by_seat_next ? ' ' : '\n');
130                 }
131
132                 fputs("OTHER_UIDS=", f);
133                 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
134                         if (i == s->active)
135                                 continue;
136
137                         fprintf(f,
138                                 "%lu%c",
139                                 (unsigned long) i->user->uid,
140                                 i->sessions_by_seat_next ? ' ' : '\n');
141                 }
142         }
143
144         fflush(f);
145
146         if (ferror(f) || rename(temp_path, s->state_file) < 0) {
147                 r = -errno;
148                 unlink(s->state_file);
149                 unlink(temp_path);
150         }
151
152         fclose(f);
153         free(temp_path);
154
155 finish:
156         if (r < 0)
157                 log_error("Failed to save seat data for %s: %s", s->id, strerror(-r));
158
159         return r;
160 }
161
162 int seat_load(Seat *s) {
163         assert(s);
164
165         return 0;
166 }
167
168 static int vt_allocate(int vtnr) {
169         int fd, r;
170         char *p;
171
172         assert(vtnr >= 1);
173
174         if (asprintf(&p, "/dev/tty%i", vtnr) < 0)
175                 return -ENOMEM;
176
177         fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
178         free(p);
179
180         r = fd < 0 ? -errno : 0;
181
182         if (fd >= 0)
183                 close_nointr_nofail(fd);
184
185         return r;
186 }
187
188 static int seat_preallocate_vts(Seat *s) {
189         int i, r = 0;
190
191         assert(s);
192         assert(s->manager);
193
194         if (s->manager->n_autovts <= 0)
195                 return 0;
196
197         if (s->manager->vtconsole != s)
198                 return 0;
199
200         for (i = 1; i < s->manager->n_autovts; i++) {
201                 int q;
202
203                 q = vt_allocate(i);
204                 if (q < 0) {
205                         log_error("Failed to preallocate VT %i: %s", i, strerror(-q));
206                         r = q;
207                 }
208         }
209
210         return r;
211 }
212
213 int seat_apply_acls(Seat *s, Session *old_active) {
214         int r;
215
216         assert(s);
217
218         r = devnode_acl_all(s->manager->udev,
219                             s->id,
220                             false,
221                             !!old_active, old_active ? old_active->user->uid : 0,
222                             !!s->active, s->active ? s->active->user->uid : 0);
223
224         if (r < 0)
225                 log_error("Failed to apply ACLs: %s", strerror(-r));
226
227         return r;
228 }
229
230 int seat_active_vt_changed(Seat *s, int vtnr) {
231         Session *i, *new_active = NULL, *old_active;
232
233         assert(s);
234         assert(vtnr >= 1);
235
236         if (s->manager->vtconsole != s)
237                 return -EINVAL;
238
239         log_debug("VT changed to %i", vtnr);
240
241         LIST_FOREACH(sessions_by_seat, i, s->sessions)
242                 if (i->vtnr == vtnr) {
243                         new_active = i;
244                         break;
245                 }
246
247         if (new_active == s->active)
248                 return 0;
249
250         old_active = s->active;
251         s->active = new_active;
252
253         seat_apply_acls(s, old_active);
254         manager_spawn_autovt(s->manager, vtnr);
255
256         return 0;
257 }
258
259 int seat_read_active_vt(Seat *s) {
260         char t[64];
261         ssize_t k;
262         int r, vtnr;
263
264         assert(s);
265
266         if (s->manager->vtconsole != s)
267                 return 0;
268
269         lseek(s->manager->console_active_fd, SEEK_SET, 0);
270
271         k = read(s->manager->console_active_fd, t, sizeof(t)-1);
272         if (k <= 0) {
273                 log_error("Failed to read current console: %s", k < 0 ? strerror(-errno) : "EOF");
274                 return k < 0 ? -errno : -EIO;
275         }
276
277         t[k] = 0;
278         truncate_nl(t);
279
280         if (!startswith(t, "tty")) {
281                 log_error("Hm, /sys/class/tty/tty0/active is badly formatted.");
282                 return -EIO;
283         }
284
285         r = safe_atoi(t+3, &vtnr);
286         if (r < 0) {
287                 log_error("Failed to parse VT number %s", t+3);
288                 return r;
289         }
290
291         if (vtnr <= 0) {
292                 log_error("VT number invalid: %s", t+3);
293                 return -EIO;
294         }
295
296         return seat_active_vt_changed(s, vtnr);
297 }
298
299 int seat_start(Seat *s) {
300         assert(s);
301
302         /* Initialize VT magic stuff */
303         seat_preallocate_vts(s);
304
305         /* Read current VT */
306         seat_read_active_vt(s);
307
308         /* Save seat data */
309         seat_save(s);
310
311         return 0;
312 }
313
314 int seat_stop(Seat *s) {
315         Session *session;
316         int r = 0, k;
317
318         assert(s);
319
320         LIST_FOREACH(sessions_by_seat, session, s->sessions) {
321                 k = session_stop(session);
322                 if (k < 0)
323                         r = k;
324         }
325
326         seat_save(s);
327         seat_add_to_gc_queue(s);
328
329         return r;
330 }
331
332 int seat_check_gc(Seat *s) {
333         assert(s);
334
335         if (s->manager->vtconsole == s)
336                 return 1;
337
338         return !!s->devices;
339 }
340
341 void seat_add_to_gc_queue(Seat *s) {
342         assert(s);
343
344         if (s->in_gc_queue)
345                 return;
346
347         LIST_PREPEND(Seat, gc_queue, s->manager->seat_gc_queue, s);
348         s->in_gc_queue = true;
349 }