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