chiark / gitweb /
logind: save object data only when started fully
[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/seats/", 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         if (!s->started)
90                 return 0;
91
92         r = safe_mkdir("/run/systemd/seats", 0755, 0, 0);
93         if (r < 0)
94                 goto finish;
95
96         r = fopen_temporary(s->state_file, &f, &temp_path);
97         if (r < 0)
98                 goto finish;
99
100         fchmod(fileno(f), 0644);
101
102         fprintf(f,
103                 "# This is private data. Do not parse.\n"
104                 "IS_VTCONSOLE=%i\n",
105                 seat_is_vtconsole(s));
106
107         if (s->active) {
108                 assert(s->active->user);
109
110                 fprintf(f,
111                         "ACTIVE=%s\n"
112                         "ACTIVE_UID=%lu\n",
113                         s->active->id,
114                         (unsigned long) s->active->user->uid);
115         }
116
117         if (s->sessions) {
118                 Session *i;
119
120                 fputs("OTHER=", f);
121                 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
122                         if (i == s->active)
123                                 continue;
124
125                         fprintf(f,
126                                 "%s%c",
127                                 i->id,
128                                 i->sessions_by_seat_next ? ' ' : '\n');
129                 }
130
131                 fputs("OTHER_UIDS=", f);
132                 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
133                         if (i == s->active)
134                                 continue;
135
136                         fprintf(f,
137                                 "%lu%c",
138                                 (unsigned long) i->user->uid,
139                                 i->sessions_by_seat_next ? ' ' : '\n');
140                 }
141         }
142
143         fflush(f);
144
145         if (ferror(f) || rename(temp_path, s->state_file) < 0) {
146                 r = -errno;
147                 unlink(s->state_file);
148                 unlink(temp_path);
149         }
150
151         fclose(f);
152         free(temp_path);
153
154 finish:
155         if (r < 0)
156                 log_error("Failed to save seat data for %s: %s", s->id, strerror(-r));
157
158         return r;
159 }
160
161 int seat_load(Seat *s) {
162         assert(s);
163
164         /* There isn't actually anything to read here ... */
165
166         return 0;
167 }
168
169 static int vt_allocate(int vtnr) {
170         int fd, r;
171         char *p;
172
173         assert(vtnr >= 1);
174
175         if (asprintf(&p, "/dev/tty%i", vtnr) < 0)
176                 return -ENOMEM;
177
178         fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
179         free(p);
180
181         r = fd < 0 ? -errno : 0;
182
183         if (fd >= 0)
184                 close_nointr_nofail(fd);
185
186         return r;
187 }
188
189 static int seat_preallocate_vts(Seat *s) {
190         int r = 0;
191         unsigned i;
192
193         assert(s);
194         assert(s->manager);
195
196         if (s->manager->n_autovts <= 0)
197                 return 0;
198
199         if (!seat_is_vtconsole(s))
200                 return 0;
201
202         for (i = 1; i < s->manager->n_autovts; i++) {
203                 int q;
204
205                 q = vt_allocate(i);
206                 if (q < 0) {
207                         log_error("Failed to preallocate VT %i: %s", i, strerror(-q));
208                         r = q;
209                 }
210         }
211
212         return r;
213 }
214
215 int seat_apply_acls(Seat *s, Session *old_active) {
216         int r;
217
218         assert(s);
219
220         r = devnode_acl_all(s->manager->udev,
221                             s->id,
222                             false,
223                             !!old_active, old_active ? old_active->user->uid : 0,
224                             !!s->active, s->active ? s->active->user->uid : 0);
225
226         if (r < 0)
227                 log_error("Failed to apply ACLs: %s", strerror(-r));
228
229         return r;
230 }
231
232 int seat_set_active(Seat *s, Session *session) {
233         Session *old_active;
234
235         assert(s);
236         assert(!session || session->seat == s);
237
238         if (session == s->active)
239                 return 0;
240
241         old_active = s->active;
242         s->active = session;
243
244         seat_apply_acls(s, old_active);
245
246         if (session && session->started)
247                 session_send_changed(session, "Active\0");
248
249         if (!session || session->started)
250                 seat_send_changed(s, "ActiveSession\0");
251
252         seat_save(s);
253
254         if (session)
255                 session_save(session);
256
257         if (old_active)
258                 session_save(old_active);
259
260         return 0;
261 }
262
263 int seat_active_vt_changed(Seat *s, int vtnr) {
264         Session *i, *new_active = NULL;
265         int r;
266
267         assert(s);
268         assert(vtnr >= 1);
269
270         if (!seat_is_vtconsole(s))
271                 return -EINVAL;
272
273         log_debug("VT changed to %i", vtnr);
274
275         LIST_FOREACH(sessions_by_seat, i, s->sessions)
276                 if (i->vtnr == vtnr) {
277                         new_active = i;
278                         break;
279                 }
280
281         r = seat_set_active(s, new_active);
282         manager_spawn_autovt(s->manager, vtnr);
283
284         return r;
285 }
286
287 int seat_read_active_vt(Seat *s) {
288         char t[64];
289         ssize_t k;
290         int r, vtnr;
291
292         assert(s);
293
294         if (!seat_is_vtconsole(s))
295                 return 0;
296
297         lseek(s->manager->console_active_fd, SEEK_SET, 0);
298
299         k = read(s->manager->console_active_fd, t, sizeof(t)-1);
300         if (k <= 0) {
301                 log_error("Failed to read current console: %s", k < 0 ? strerror(-errno) : "EOF");
302                 return k < 0 ? -errno : -EIO;
303         }
304
305         t[k] = 0;
306         truncate_nl(t);
307
308         if (!startswith(t, "tty")) {
309                 log_error("Hm, /sys/class/tty/tty0/active is badly formatted.");
310                 return -EIO;
311         }
312
313         r = safe_atoi(t+3, &vtnr);
314         if (r < 0) {
315                 log_error("Failed to parse VT number %s", t+3);
316                 return r;
317         }
318
319         if (vtnr <= 0) {
320                 log_error("VT number invalid: %s", t+3);
321                 return -EIO;
322         }
323
324         return seat_active_vt_changed(s, vtnr);
325 }
326
327 int seat_start(Seat *s) {
328         assert(s);
329
330         if (s->started)
331                 return 0;
332
333         log_info("New seat %s.", s->id);
334
335         /* Initialize VT magic stuff */
336         seat_preallocate_vts(s);
337
338         /* Read current VT */
339         seat_read_active_vt(s);
340
341         /* Save seat data */
342         seat_save(s);
343
344         s->started = true;
345
346         seat_send_signal(s, true);
347
348         return 0;
349 }
350
351 int seat_stop(Seat *s) {
352         int r = 0;
353
354         assert(s);
355
356         if (s->started)
357                 log_info("Removed seat %s.", s->id);
358
359         seat_stop_sessions(s);
360
361         unlink(s->state_file);
362         seat_add_to_gc_queue(s);
363
364         if (s->started)
365                 seat_send_signal(s, false);
366
367         s->started = false;
368
369         return r;
370 }
371
372 int seat_stop_sessions(Seat *s) {
373         Session *session;
374         int r = 0, k;
375
376         assert(s);
377
378         LIST_FOREACH(sessions_by_seat, session, s->sessions) {
379                 k = session_stop(session);
380                 if (k < 0)
381                         r = k;
382         }
383
384         return r;
385 }
386
387 int seat_attach_session(Seat *s, Session *session) {
388         assert(s);
389         assert(session);
390         assert(!session->seat);
391
392         if (!seat_is_vtconsole(s) && s->sessions)
393                 return -EEXIST;
394
395         session->seat = s;
396         LIST_PREPEND(Session, sessions_by_seat, s->sessions, session);
397
398         seat_send_changed(s, "Sessions\0");
399
400         if (!seat_is_vtconsole(s)) {
401                 assert(!s->active);
402                 seat_set_active(s, session);
403         }
404
405         return 0;
406 }
407
408 bool seat_is_vtconsole(Seat *s) {
409         assert(s);
410
411         return s->manager->vtconsole == s;
412 }
413
414 int seat_get_idle_hint(Seat *s, dual_timestamp *t) {
415         Session *session;
416         bool idle_hint = true;
417         dual_timestamp ts = { 0, 0 };
418
419         assert(s);
420
421         LIST_FOREACH(sessions_by_seat, session, s->sessions) {
422                 dual_timestamp k;
423                 int ih;
424
425                 ih = session_get_idle_hint(session, &k);
426                 if (ih < 0)
427                         return ih;
428
429                 if (!ih) {
430                         if (!idle_hint) {
431                                 if (k.monotonic < ts.monotonic)
432                                         ts = k;
433                         } else {
434                                 idle_hint = false;
435                                 ts = k;
436                         }
437                 } else if (idle_hint) {
438
439                         if (k.monotonic > ts.monotonic)
440                                 ts = k;
441                 }
442         }
443
444         if (t)
445                 *t = ts;
446
447         return idle_hint;
448 }
449
450 int seat_check_gc(Seat *s) {
451         assert(s);
452
453         if (seat_is_vtconsole(s))
454                 return 1;
455
456         return !!s->devices;
457 }
458
459 void seat_add_to_gc_queue(Seat *s) {
460         assert(s);
461
462         if (s->in_gc_queue)
463                 return;
464
465         LIST_PREPEND(Seat, gc_queue, s->manager->seat_gc_queue, s);
466         s->in_gc_queue = true;
467 }
468
469 static bool seat_name_valid_char(char c) {
470         return
471                 (c >= 'a' && c <= 'z') ||
472                 (c >= 'A' && c <= 'Z') ||
473                 (c >= '0' && c <= '9') ||
474                 c == '-' ||
475                 c == '_';
476 }
477
478 bool seat_name_is_valid(const char *name) {
479         const char *p;
480
481         assert(name);
482
483         if (!startswith(name, "seat"))
484                 return false;
485
486         if (!name[4])
487                 return false;
488
489         for (p = name; *p; p++)
490                 if (!seat_name_valid_char(*p))
491                         return false;
492
493         return true;
494 }