]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/logind-seat.c
udev: move man pages to udev section
[thirdparty/systemd.git] / src / login / logind-seat.c
CommitLineData
20263082
LP
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>
5eda94dd 28#include <string.h>
20263082 29
90821c93 30#include "logind-seat.h"
5eda94dd 31#include "logind-acl.h"
20263082
LP
32#include "util.h"
33
34Seat *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
98a28fef 44 s->state_file = strappend("/run/systemd/seats/", id);
20263082
LP
45 if (!s->state_file) {
46 free(s);
47 return NULL;
48 }
49
50 s->id = file_name_from_path(s->state_file);
14c3baca 51 s->manager = m;
20263082
LP
52
53 if (hashmap_put(m->seats, s->id, s) < 0) {
14c3baca 54 free(s->state_file);
20263082
LP
55 free(s);
56 return NULL;
57 }
58
20263082
LP
59 return s;
60}
61
62void seat_free(Seat *s) {
63 assert(s);
64
14c3baca
LP
65 if (s->in_gc_queue)
66 LIST_REMOVE(Seat, gc_queue, s->manager->seat_gc_queue, s);
67
20263082
LP
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
d2f92cdf 78 free(s->state_file);
20263082
LP
79 free(s);
80}
81
82int seat_save(Seat *s) {
20263082 83 int r;
14c3baca
LP
84 FILE *f;
85 char *temp_path;
20263082
LP
86
87 assert(s);
88
accaeded
LP
89 if (!s->started)
90 return 0;
91
98a28fef 92 r = safe_mkdir("/run/systemd/seats", 0755, 0, 0);
20263082 93 if (r < 0)
14c3baca
LP
94 goto finish;
95
96 r = fopen_temporary(s->state_file, &f, &temp_path);
97 if (r < 0)
98 goto finish;
20263082 99
14c3baca 100 fchmod(fileno(f), 0644);
20263082
LP
101
102 fprintf(f,
14c3baca 103 "# This is private data. Do not parse.\n"
addedec4
LP
104 "IS_VTCONSOLE=%i\n"
105 "CAN_MULTI_SESSION=%i\n",
106 seat_is_vtconsole(s),
107 seat_can_multi_session(s));
20263082
LP
108
109 if (s->active) {
110 assert(s->active->user);
111
112 fprintf(f,
113 "ACTIVE=%s\n"
114 "ACTIVE_UID=%lu\n",
115 s->active->id,
116 (unsigned long) s->active->user->uid);
117 }
118
119 if (s->sessions) {
120 Session *i;
20263082 121
fde78a3a 122 fputs("SESSIONS=", f);
20263082 123 LIST_FOREACH(sessions_by_seat, i, s->sessions) {
14c3baca
LP
124 fprintf(f,
125 "%s%c",
126 i->id,
127 i->sessions_by_seat_next ? ' ' : '\n');
128 }
20263082 129
fde78a3a
LP
130 fputs("UIDS=", f);
131 LIST_FOREACH(sessions_by_seat, i, s->sessions)
20263082 132 fprintf(f,
14c3baca
LP
133 "%lu%c",
134 (unsigned long) i->user->uid,
135 i->sessions_by_seat_next ? ' ' : '\n');
20263082
LP
136 }
137
138 fflush(f);
14c3baca
LP
139
140 if (ferror(f) || rename(temp_path, s->state_file) < 0) {
20263082
LP
141 r = -errno;
142 unlink(s->state_file);
14c3baca 143 unlink(temp_path);
20263082
LP
144 }
145
146 fclose(f);
14c3baca
LP
147 free(temp_path);
148
149finish:
150 if (r < 0)
151 log_error("Failed to save seat data for %s: %s", s->id, strerror(-r));
152
20263082
LP
153 return r;
154}
155
156int seat_load(Seat *s) {
157 assert(s);
158
a185c5aa
LP
159 /* There isn't actually anything to read here ... */
160
20263082
LP
161 return 0;
162}
163
164static 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
30ed21ce 184int seat_preallocate_vts(Seat *s) {
3f49d45a
LP
185 int r = 0;
186 unsigned i;
20263082
LP
187
188 assert(s);
189 assert(s->manager);
190
30ed21ce
LP
191 log_debug("Preallocating VTs...");
192
20263082
LP
193 if (s->manager->n_autovts <= 0)
194 return 0;
195
addedec4 196 if (!seat_can_multi_session(s))
20263082
LP
197 return 0;
198
e7886786 199 for (i = 1; i <= s->manager->n_autovts; i++) {
20263082
LP
200 int q;
201
202 q = vt_allocate(i);
14c3baca
LP
203 if (q < 0) {
204 log_error("Failed to preallocate VT %i: %s", i, strerror(-q));
20263082 205 r = q;
14c3baca 206 }
20263082
LP
207 }
208
209 return r;
210}
211
5eda94dd
LP
212int seat_apply_acls(Seat *s, Session *old_active) {
213 int r;
20263082 214
5eda94dd 215 assert(s);
20263082 216
5eda94dd
LP
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);
20263082 222
5eda94dd
LP
223 if (r < 0)
224 log_error("Failed to apply ACLs: %s", strerror(-r));
20263082
LP
225
226 return r;
227}
228
9418f147
LP
229int seat_set_active(Seat *s, Session *session) {
230 Session *old_active;
231
232 assert(s);
77527da0 233 assert(!session || session->seat == s);
9418f147
LP
234
235 if (session == s->active)
236 return 0;
237
238 old_active = s->active;
239 s->active = session;
240
241 seat_apply_acls(s, old_active);
242
243 if (session && session->started)
244 session_send_changed(session, "Active\0");
245
246 if (!session || session->started)
247 seat_send_changed(s, "ActiveSession\0");
248
98a28fef
LP
249 seat_save(s);
250
7f7bb946 251 if (session) {
98a28fef 252 session_save(session);
7f7bb946
LP
253 user_save(session->user);
254 }
98a28fef 255
7f7bb946 256 if (old_active) {
98a28fef 257 session_save(old_active);
7f7bb946
LP
258 user_save(old_active->user);
259 }
98a28fef 260
9418f147
LP
261 return 0;
262}
263
5eda94dd 264int seat_active_vt_changed(Seat *s, int vtnr) {
9418f147
LP
265 Session *i, *new_active = NULL;
266 int r;
20263082
LP
267
268 assert(s);
269 assert(vtnr >= 1);
20263082 270
addedec4 271 if (!seat_can_multi_session(s))
14c3baca
LP
272 return -EINVAL;
273
274 log_debug("VT changed to %i", vtnr);
20263082
LP
275
276 LIST_FOREACH(sessions_by_seat, i, s->sessions)
277 if (i->vtnr == vtnr) {
14c3baca 278 new_active = i;
20263082
LP
279 break;
280 }
281
9418f147 282 r = seat_set_active(s, new_active);
5eda94dd 283 manager_spawn_autovt(s->manager, vtnr);
20263082 284
9418f147 285 return r;
20263082
LP
286}
287
14c3baca
LP
288int seat_read_active_vt(Seat *s) {
289 char t[64];
290 ssize_t k;
291 int r, vtnr;
292
293 assert(s);
294
addedec4 295 if (!seat_can_multi_session(s))
14c3baca
LP
296 return 0;
297
298 lseek(s->manager->console_active_fd, SEEK_SET, 0);
299
300 k = read(s->manager->console_active_fd, t, sizeof(t)-1);
301 if (k <= 0) {
302 log_error("Failed to read current console: %s", k < 0 ? strerror(-errno) : "EOF");
303 return k < 0 ? -errno : -EIO;
304 }
305
306 t[k] = 0;
307 truncate_nl(t);
308
309 if (!startswith(t, "tty")) {
310 log_error("Hm, /sys/class/tty/tty0/active is badly formatted.");
311 return -EIO;
312 }
313
314 r = safe_atoi(t+3, &vtnr);
315 if (r < 0) {
316 log_error("Failed to parse VT number %s", t+3);
317 return r;
318 }
319
320 if (vtnr <= 0) {
321 log_error("VT number invalid: %s", t+3);
322 return -EIO;
323 }
324
325 return seat_active_vt_changed(s, vtnr);
326}
327
328int seat_start(Seat *s) {
329 assert(s);
330
3f49d45a
LP
331 if (s->started)
332 return 0;
333
334 log_info("New seat %s.", s->id);
335
14c3baca
LP
336 /* Initialize VT magic stuff */
337 seat_preallocate_vts(s);
338
339 /* Read current VT */
340 seat_read_active_vt(s);
341
7f7bb946
LP
342 s->started = true;
343
14c3baca
LP
344 /* Save seat data */
345 seat_save(s);
346
da119395
LP
347 seat_send_signal(s, true);
348
14c3baca
LP
349 return 0;
350}
351
20263082 352int seat_stop(Seat *s) {
a185c5aa 353 int r = 0;
20263082
LP
354
355 assert(s);
356
ed18b08b
LP
357 if (s->started)
358 log_info("Removed seat %s.", s->id);
da119395 359
a185c5aa
LP
360 seat_stop_sessions(s);
361
362 unlink(s->state_file);
363 seat_add_to_gc_queue(s);
364
ed18b08b
LP
365 if (s->started)
366 seat_send_signal(s, false);
367
a185c5aa
LP
368 s->started = false;
369
370 return r;
371}
372
373int seat_stop_sessions(Seat *s) {
374 Session *session;
375 int r = 0, k;
376
377 assert(s);
378
20263082 379 LIST_FOREACH(sessions_by_seat, session, s->sessions) {
20263082
LP
380 k = session_stop(session);
381 if (k < 0)
382 r = k;
383 }
384
a185c5aa
LP
385 return r;
386}
14c3baca 387
a185c5aa
LP
388int seat_attach_session(Seat *s, Session *session) {
389 assert(s);
390 assert(session);
391 assert(!session->seat);
3f49d45a 392
a185c5aa
LP
393 session->seat = s;
394 LIST_PREPEND(Session, sessions_by_seat, s->sessions, session);
395
9418f147
LP
396 seat_send_changed(s, "Sessions\0");
397
abc5bbc6
LP
398 /* Note that even if a seat is not multi-session capable it
399 * still might have multiple sessions on it since old, dead
400 * sessions might continue to be tracked until all their
401 * processes are gone. The most recently added session
402 * (i.e. the first in s->sessions) is the one that matters. */
403
404 if (!seat_can_multi_session(s))
9418f147 405 seat_set_active(s, session);
9418f147 406
a185c5aa
LP
407 return 0;
408}
409
410bool seat_is_vtconsole(Seat *s) {
411 assert(s);
412
413 return s->manager->vtconsole == s;
414}
415
addedec4
LP
416bool seat_can_multi_session(Seat *s) {
417 assert(s);
418
419 if (!seat_is_vtconsole(s))
420 return false;
421
422 /* If we can't watch which VT is in the foreground, we don't
423 * support VT switching */
424
425 return s->manager->console_active_fd >= 0;
426}
427
a185c5aa
LP
428int seat_get_idle_hint(Seat *s, dual_timestamp *t) {
429 Session *session;
430 bool idle_hint = true;
431 dual_timestamp ts = { 0, 0 };
432
433 assert(s);
434
435 LIST_FOREACH(sessions_by_seat, session, s->sessions) {
436 dual_timestamp k;
437 int ih;
438
439 ih = session_get_idle_hint(session, &k);
440 if (ih < 0)
441 return ih;
442
443 if (!ih) {
444 if (!idle_hint) {
445 if (k.monotonic < ts.monotonic)
446 ts = k;
447 } else {
448 idle_hint = false;
449 ts = k;
450 }
451 } else if (idle_hint) {
452
453 if (k.monotonic > ts.monotonic)
454 ts = k;
455 }
456 }
457
458 if (t)
459 *t = ts;
460
461 return idle_hint;
20263082 462}
14c3baca 463
4a4b033f 464int seat_check_gc(Seat *s, bool drop_not_started) {
14c3baca
LP
465 assert(s);
466
4a4b033f 467 if (drop_not_started && !s->started)
932e3ee7
LP
468 return 0;
469
a185c5aa 470 if (seat_is_vtconsole(s))
14c3baca
LP
471 return 1;
472
473 return !!s->devices;
474}
475
476void seat_add_to_gc_queue(Seat *s) {
477 assert(s);
478
479 if (s->in_gc_queue)
480 return;
481
482 LIST_PREPEND(Seat, gc_queue, s->manager->seat_gc_queue, s);
483 s->in_gc_queue = true;
484}
3f49d45a
LP
485
486static bool seat_name_valid_char(char c) {
487 return
488 (c >= 'a' && c <= 'z') ||
489 (c >= 'A' && c <= 'Z') ||
490 (c >= '0' && c <= '9') ||
491 c == '-' ||
492 c == '_';
493}
494
495bool seat_name_is_valid(const char *name) {
496 const char *p;
497
498 assert(name);
499
500 if (!startswith(name, "seat"))
501 return false;
502
503 if (!name[4])
504 return false;
505
506 for (p = name; *p; p++)
507 if (!seat_name_valid_char(*p))
508 return false;
509
1c9a2c10
LP
510 if (strlen(name) > 255)
511 return false;
512
3f49d45a
LP
513 return true;
514}