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