]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
20263082 | 2 | |
20263082 | 3 | #include <errno.h> |
20263082 | 4 | #include <fcntl.h> |
5eda94dd | 5 | #include <string.h> |
ca78ad1d | 6 | #include <sys/stat.h> |
07630cea | 7 | #include <unistd.h> |
20263082 | 8 | |
cc377381 | 9 | #include "sd-messages.h" |
07630cea | 10 | |
b5efdb8a | 11 | #include "alloc-util.h" |
3ffd4af2 | 12 | #include "fd-util.h" |
0d39fa9c | 13 | #include "fileio.h" |
f97b34a6 | 14 | #include "format-util.h" |
5eda94dd | 15 | #include "logind-acl.h" |
3ffd4af2 | 16 | #include "logind-seat.h" |
49e942b2 | 17 | #include "mkdir.h" |
6bedfcbb | 18 | #include "parse-util.h" |
d054f0a4 | 19 | #include "stdio-util.h" |
07630cea | 20 | #include "string-util.h" |
288a74cc | 21 | #include "terminal-util.h" |
e4de7287 | 22 | #include "tmpfile-util.h" |
07630cea | 23 | #include "util.h" |
20263082 | 24 | |
8c29a457 LP |
25 | int seat_new(Seat** ret, Manager *m, const char *id) { |
26 | _cleanup_(seat_freep) Seat *s = NULL; | |
27 | int r; | |
20263082 | 28 | |
8c29a457 | 29 | assert(ret); |
20263082 LP |
30 | assert(m); |
31 | assert(id); | |
32 | ||
8c29a457 LP |
33 | if (!seat_name_is_valid(id)) |
34 | return -EINVAL; | |
35 | ||
36 | s = new(Seat, 1); | |
20263082 | 37 | if (!s) |
8c29a457 LP |
38 | return -ENOMEM; |
39 | ||
40 | *s = (Seat) { | |
41 | .manager = m, | |
42 | }; | |
20263082 | 43 | |
98a28fef | 44 | s->state_file = strappend("/run/systemd/seats/", id); |
6b430fdb | 45 | if (!s->state_file) |
8c29a457 | 46 | return -ENOMEM; |
20263082 | 47 | |
2b6bf07d | 48 | s->id = basename(s->state_file); |
20263082 | 49 | |
8c29a457 LP |
50 | r = hashmap_put(m->seats, s->id, s); |
51 | if (r < 0) | |
52 | return r; | |
20263082 | 53 | |
8c29a457 LP |
54 | *ret = TAKE_PTR(s); |
55 | return 0; | |
20263082 LP |
56 | } |
57 | ||
8c29a457 LP |
58 | Seat* seat_free(Seat *s) { |
59 | if (!s) | |
60 | return NULL; | |
20263082 | 61 | |
14c3baca | 62 | if (s->in_gc_queue) |
71fda00f | 63 | LIST_REMOVE(gc_queue, s->manager->seat_gc_queue, s); |
14c3baca | 64 | |
20263082 LP |
65 | while (s->sessions) |
66 | session_free(s->sessions); | |
67 | ||
68 | assert(!s->active); | |
69 | ||
70 | while (s->devices) | |
71 | device_free(s->devices); | |
72 | ||
73 | hashmap_remove(s->manager->seats, s->id); | |
74 | ||
49e6fdbf | 75 | free(s->positions); |
d2f92cdf | 76 | free(s->state_file); |
8c29a457 LP |
77 | |
78 | return mfree(s); | |
20263082 LP |
79 | } |
80 | ||
81 | int seat_save(Seat *s) { | |
cc377381 LP |
82 | _cleanup_free_ char *temp_path = NULL; |
83 | _cleanup_fclose_ FILE *f = NULL; | |
20263082 LP |
84 | int r; |
85 | ||
86 | assert(s); | |
87 | ||
accaeded LP |
88 | if (!s->started) |
89 | return 0; | |
90 | ||
37c1d5e9 | 91 | r = mkdir_safe_label("/run/systemd/seats", 0755, 0, 0, MKDIR_WARN_MODE); |
20263082 | 92 | if (r < 0) |
dacd6cee | 93 | goto fail; |
14c3baca LP |
94 | |
95 | r = fopen_temporary(s->state_file, &f, &temp_path); | |
96 | if (r < 0) | |
dacd6cee | 97 | goto fail; |
20263082 | 98 | |
0d536673 | 99 | (void) fchmod(fileno(f), 0644); |
20263082 LP |
100 | |
101 | fprintf(f, | |
14c3baca | 102 | "# This is private data. Do not parse.\n" |
92432fcc | 103 | "IS_SEAT0=%i\n" |
f1a8e221 LP |
104 | "CAN_MULTI_SESSION=%i\n" |
105 | "CAN_TTY=%i\n" | |
106 | "CAN_GRAPHICAL=%i\n", | |
92432fcc | 107 | seat_is_seat0(s), |
f1a8e221 LP |
108 | seat_can_multi_session(s), |
109 | seat_can_tty(s), | |
110 | seat_can_graphical(s)); | |
20263082 LP |
111 | |
112 | if (s->active) { | |
113 | assert(s->active->user); | |
114 | ||
115 | fprintf(f, | |
116 | "ACTIVE=%s\n" | |
a08ac7e0 | 117 | "ACTIVE_UID="UID_FMT"\n", |
20263082 | 118 | s->active->id, |
a08ac7e0 | 119 | s->active->user->uid); |
20263082 LP |
120 | } |
121 | ||
122 | if (s->sessions) { | |
123 | Session *i; | |
20263082 | 124 | |
0d536673 | 125 | fputs("SESSIONS=", f); |
20263082 | 126 | LIST_FOREACH(sessions_by_seat, i, s->sessions) { |
14c3baca LP |
127 | fprintf(f, |
128 | "%s%c", | |
129 | i->id, | |
130 | i->sessions_by_seat_next ? ' ' : '\n'); | |
131 | } | |
20263082 | 132 | |
0d536673 | 133 | fputs("UIDS=", f); |
fde78a3a | 134 | LIST_FOREACH(sessions_by_seat, i, s->sessions) |
20263082 | 135 | fprintf(f, |
90b2de37 ZJS |
136 | UID_FMT"%c", |
137 | i->user->uid, | |
14c3baca | 138 | i->sessions_by_seat_next ? ' ' : '\n'); |
20263082 LP |
139 | } |
140 | ||
dacd6cee LP |
141 | r = fflush_and_check(f); |
142 | if (r < 0) | |
143 | goto fail; | |
14c3baca | 144 | |
dacd6cee | 145 | if (rename(temp_path, s->state_file) < 0) { |
20263082 | 146 | r = -errno; |
dacd6cee | 147 | goto fail; |
20263082 LP |
148 | } |
149 | ||
dacd6cee LP |
150 | return 0; |
151 | ||
152 | fail: | |
153 | (void) unlink(s->state_file); | |
154 | ||
155 | if (temp_path) | |
156 | (void) unlink(temp_path); | |
14c3baca | 157 | |
dacd6cee | 158 | return log_error_errno(r, "Failed to save seat data %s: %m", s->state_file); |
20263082 LP |
159 | } |
160 | ||
161 | int seat_load(Seat *s) { | |
162 | assert(s); | |
163 | ||
a185c5aa LP |
164 | /* There isn't actually anything to read here ... */ |
165 | ||
20263082 LP |
166 | return 0; |
167 | } | |
168 | ||
14cb109d YW |
169 | static int vt_allocate(unsigned vtnr) { |
170 | char p[sizeof("/dev/tty") + DECIMAL_STR_MAX(unsigned)]; | |
cc377381 | 171 | _cleanup_close_ int fd = -1; |
20263082 LP |
172 | |
173 | assert(vtnr >= 1); | |
174 | ||
d054f0a4 | 175 | xsprintf(p, "/dev/tty%u", vtnr); |
20263082 | 176 | fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC); |
cc377381 | 177 | if (fd < 0) |
cce08496 | 178 | return fd; |
20263082 | 179 | |
cc377381 | 180 | return 0; |
20263082 LP |
181 | } |
182 | ||
30ed21ce | 183 | int seat_preallocate_vts(Seat *s) { |
3f49d45a LP |
184 | int r = 0; |
185 | unsigned i; | |
20263082 LP |
186 | |
187 | assert(s); | |
188 | assert(s->manager); | |
189 | ||
30ed21ce LP |
190 | log_debug("Preallocating VTs..."); |
191 | ||
20263082 LP |
192 | if (s->manager->n_autovts <= 0) |
193 | return 0; | |
194 | ||
bf7825ae | 195 | if (!seat_has_vts(s)) |
20263082 LP |
196 | return 0; |
197 | ||
e7886786 | 198 | for (i = 1; i <= s->manager->n_autovts; i++) { |
20263082 LP |
199 | int q; |
200 | ||
201 | q = vt_allocate(i); | |
e555d126 LP |
202 | if (q < 0) |
203 | r = log_error_errno(q, "Failed to preallocate VT %u: %m", i); | |
20263082 LP |
204 | } |
205 | ||
206 | return r; | |
207 | } | |
208 | ||
5eda94dd LP |
209 | int seat_apply_acls(Seat *s, Session *old_active) { |
210 | int r; | |
20263082 | 211 | |
5eda94dd | 212 | assert(s); |
20263082 | 213 | |
4f209af7 | 214 | r = devnode_acl_all(s->id, |
5eda94dd LP |
215 | false, |
216 | !!old_active, old_active ? old_active->user->uid : 0, | |
217 | !!s->active, s->active ? s->active->user->uid : 0); | |
20263082 | 218 | |
5eda94dd | 219 | if (r < 0) |
e555d126 | 220 | return log_error_errno(r, "Failed to apply ACLs: %m"); |
20263082 | 221 | |
e555d126 | 222 | return 0; |
20263082 LP |
223 | } |
224 | ||
9418f147 LP |
225 | int seat_set_active(Seat *s, Session *session) { |
226 | Session *old_active; | |
227 | ||
228 | assert(s); | |
77527da0 | 229 | assert(!session || session->seat == s); |
9418f147 LP |
230 | |
231 | if (session == s->active) | |
232 | return 0; | |
233 | ||
234 | old_active = s->active; | |
235 | s->active = session; | |
236 | ||
dfd55270 | 237 | if (old_active) { |
118ecf32 | 238 | session_device_pause_all(old_active); |
cc377381 | 239 | session_send_changed(old_active, "Active", NULL); |
dfd55270 | 240 | } |
118ecf32 | 241 | |
75bbdf47 | 242 | (void) seat_apply_acls(s, old_active); |
9418f147 | 243 | |
118ecf32 | 244 | if (session && session->started) { |
cc377381 | 245 | session_send_changed(session, "Active", NULL); |
118ecf32 DH |
246 | session_device_resume_all(session); |
247 | } | |
9418f147 LP |
248 | |
249 | if (!session || session->started) | |
cc377381 | 250 | seat_send_changed(s, "ActiveSession", NULL); |
9418f147 | 251 | |
98a28fef LP |
252 | seat_save(s); |
253 | ||
7f7bb946 | 254 | if (session) { |
98a28fef | 255 | session_save(session); |
7f7bb946 LP |
256 | user_save(session->user); |
257 | } | |
98a28fef | 258 | |
7f7bb946 | 259 | if (old_active) { |
98a28fef | 260 | session_save(old_active); |
78ab361c CG |
261 | if (!session || session->user != old_active->user) |
262 | user_save(old_active->user); | |
7f7bb946 | 263 | } |
98a28fef | 264 | |
9418f147 LP |
265 | return 0; |
266 | } | |
267 | ||
14cb109d | 268 | int seat_switch_to(Seat *s, unsigned num) { |
49e6fdbf DH |
269 | /* Public session positions skip 0 (there is only F1-F12). Maybe it |
270 | * will get reassigned in the future, so return error for now. */ | |
55493982 | 271 | if (num == 0) |
49e6fdbf DH |
272 | return -EINVAL; |
273 | ||
15403427 DH |
274 | if (num >= s->position_count || !s->positions[num]) { |
275 | /* allow switching to unused VTs to trigger auto-activate */ | |
276 | if (seat_has_vts(s) && num < 64) | |
277 | return chvt(num); | |
278 | ||
49e6fdbf | 279 | return -EINVAL; |
15403427 | 280 | } |
49e6fdbf DH |
281 | |
282 | return session_activate(s->positions[num]); | |
283 | } | |
284 | ||
285 | int seat_switch_to_next(Seat *s) { | |
14cb109d | 286 | unsigned start, i; |
49e6fdbf | 287 | |
55493982 | 288 | if (s->position_count == 0) |
49e6fdbf DH |
289 | return -EINVAL; |
290 | ||
291 | start = 1; | |
e6494a07 DH |
292 | if (s->active && s->active->position > 0) |
293 | start = s->active->position; | |
49e6fdbf DH |
294 | |
295 | for (i = start + 1; i < s->position_count; ++i) | |
296 | if (s->positions[i]) | |
297 | return session_activate(s->positions[i]); | |
298 | ||
299 | for (i = 1; i < start; ++i) | |
300 | if (s->positions[i]) | |
301 | return session_activate(s->positions[i]); | |
302 | ||
303 | return -EINVAL; | |
304 | } | |
305 | ||
306 | int seat_switch_to_previous(Seat *s) { | |
14cb109d | 307 | unsigned start, i; |
49e6fdbf | 308 | |
55493982 | 309 | if (s->position_count == 0) |
49e6fdbf DH |
310 | return -EINVAL; |
311 | ||
312 | start = 1; | |
e6494a07 DH |
313 | if (s->active && s->active->position > 0) |
314 | start = s->active->position; | |
49e6fdbf DH |
315 | |
316 | for (i = start - 1; i > 0; --i) | |
317 | if (s->positions[i]) | |
318 | return session_activate(s->positions[i]); | |
319 | ||
320 | for (i = s->position_count - 1; i > start; --i) | |
321 | if (s->positions[i]) | |
322 | return session_activate(s->positions[i]); | |
323 | ||
324 | return -EINVAL; | |
325 | } | |
326 | ||
14cb109d | 327 | int seat_active_vt_changed(Seat *s, unsigned vtnr) { |
9418f147 LP |
328 | Session *i, *new_active = NULL; |
329 | int r; | |
20263082 LP |
330 | |
331 | assert(s); | |
332 | assert(vtnr >= 1); | |
20263082 | 333 | |
bf7825ae | 334 | if (!seat_has_vts(s)) |
14c3baca LP |
335 | return -EINVAL; |
336 | ||
92bd5ff3 | 337 | log_debug("VT changed to %u", vtnr); |
20263082 | 338 | |
be94d954 MP |
339 | /* we might have earlier closing sessions on the same VT, so try to |
340 | * find a running one first */ | |
20263082 | 341 | LIST_FOREACH(sessions_by_seat, i, s->sessions) |
be94d954 | 342 | if (i->vtnr == vtnr && !i->stopping) { |
14c3baca | 343 | new_active = i; |
20263082 LP |
344 | break; |
345 | } | |
346 | ||
be94d954 MP |
347 | if (!new_active) { |
348 | /* no running one? then we can't decide which one is the | |
349 | * active one, let the first one win */ | |
350 | LIST_FOREACH(sessions_by_seat, i, s->sessions) | |
351 | if (i->vtnr == vtnr) { | |
352 | new_active = i; | |
353 | break; | |
354 | } | |
355 | } | |
356 | ||
9418f147 | 357 | r = seat_set_active(s, new_active); |
5eda94dd | 358 | manager_spawn_autovt(s->manager, vtnr); |
20263082 | 359 | |
9418f147 | 360 | return r; |
20263082 LP |
361 | } |
362 | ||
14c3baca LP |
363 | int seat_read_active_vt(Seat *s) { |
364 | char t[64]; | |
365 | ssize_t k; | |
49679ff7 | 366 | int vtnr; |
14c3baca LP |
367 | |
368 | assert(s); | |
369 | ||
bf7825ae | 370 | if (!seat_has_vts(s)) |
14c3baca LP |
371 | return 0; |
372 | ||
e8f3e7a7 ZJS |
373 | if (lseek(s->manager->console_active_fd, SEEK_SET, 0) < 0) |
374 | return log_error_errno(errno, "lseek on console_active_fd failed: %m"); | |
14c3baca LP |
375 | |
376 | k = read(s->manager->console_active_fd, t, sizeof(t)-1); | |
377 | if (k <= 0) { | |
65641b3c | 378 | log_error("Failed to read current console: %s", k < 0 ? strerror(errno) : "EOF"); |
14c3baca LP |
379 | return k < 0 ? -errno : -EIO; |
380 | } | |
381 | ||
382 | t[k] = 0; | |
383 | truncate_nl(t); | |
384 | ||
49679ff7 FB |
385 | vtnr = vtnr_from_tty(t); |
386 | if (vtnr < 0) { | |
387 | log_error_errno(vtnr, "Hm, /sys/class/tty/tty0/active is badly formatted: %m"); | |
14c3baca LP |
388 | return -EIO; |
389 | } | |
390 | ||
391 | return seat_active_vt_changed(s, vtnr); | |
392 | } | |
393 | ||
394 | int seat_start(Seat *s) { | |
395 | assert(s); | |
396 | ||
3f49d45a LP |
397 | if (s->started) |
398 | return 0; | |
399 | ||
877d54e9 | 400 | log_struct(LOG_INFO, |
2b044526 | 401 | "MESSAGE_ID=" SD_MESSAGE_SEAT_START_STR, |
877d54e9 | 402 | "SEAT_ID=%s", s->id, |
a1230ff9 | 403 | LOG_MESSAGE("New seat %s.", s->id)); |
3f49d45a | 404 | |
14c3baca LP |
405 | /* Initialize VT magic stuff */ |
406 | seat_preallocate_vts(s); | |
407 | ||
408 | /* Read current VT */ | |
409 | seat_read_active_vt(s); | |
410 | ||
7f7bb946 LP |
411 | s->started = true; |
412 | ||
14c3baca LP |
413 | /* Save seat data */ |
414 | seat_save(s); | |
415 | ||
da119395 LP |
416 | seat_send_signal(s, true); |
417 | ||
14c3baca LP |
418 | return 0; |
419 | } | |
420 | ||
9bb69af4 | 421 | int seat_stop(Seat *s, bool force) { |
e6958b7e | 422 | int r; |
20263082 LP |
423 | |
424 | assert(s); | |
425 | ||
ed18b08b | 426 | if (s->started) |
877d54e9 | 427 | log_struct(LOG_INFO, |
2b044526 | 428 | "MESSAGE_ID=" SD_MESSAGE_SEAT_STOP_STR, |
877d54e9 | 429 | "SEAT_ID=%s", s->id, |
a1230ff9 | 430 | LOG_MESSAGE("Removed seat %s.", s->id)); |
da119395 | 431 | |
e6958b7e | 432 | r = seat_stop_sessions(s, force); |
a185c5aa | 433 | |
75bbdf47 | 434 | (void) unlink(s->state_file); |
a185c5aa LP |
435 | seat_add_to_gc_queue(s); |
436 | ||
ed18b08b LP |
437 | if (s->started) |
438 | seat_send_signal(s, false); | |
439 | ||
a185c5aa LP |
440 | s->started = false; |
441 | ||
442 | return r; | |
443 | } | |
444 | ||
9bb69af4 | 445 | int seat_stop_sessions(Seat *s, bool force) { |
a185c5aa LP |
446 | Session *session; |
447 | int r = 0, k; | |
448 | ||
449 | assert(s); | |
450 | ||
20263082 | 451 | LIST_FOREACH(sessions_by_seat, session, s->sessions) { |
9bb69af4 | 452 | k = session_stop(session, force); |
20263082 LP |
453 | if (k < 0) |
454 | r = k; | |
455 | } | |
456 | ||
a185c5aa LP |
457 | return r; |
458 | } | |
14c3baca | 459 | |
49e6fdbf | 460 | void seat_evict_position(Seat *s, Session *session) { |
3e6b205f | 461 | Session *iter; |
14cb109d | 462 | unsigned pos = session->position; |
49e6fdbf | 463 | |
e6494a07 | 464 | session->position = 0; |
49e6fdbf | 465 | |
55493982 | 466 | if (pos == 0) |
49e6fdbf DH |
467 | return; |
468 | ||
3e6b205f | 469 | if (pos < s->position_count && s->positions[pos] == session) { |
49e6fdbf | 470 | s->positions[pos] = NULL; |
3e6b205f DH |
471 | |
472 | /* There might be another session claiming the same | |
55493982 | 473 | * position (eg., during gdm->session transition), so let's look |
3e6b205f DH |
474 | * for it and set it on the free slot. */ |
475 | LIST_FOREACH(sessions_by_seat, iter, s->sessions) { | |
28103328 | 476 | if (iter->position == pos && session_get_state(iter) != SESSION_CLOSING) { |
3e6b205f DH |
477 | s->positions[pos] = iter; |
478 | break; | |
479 | } | |
480 | } | |
481 | } | |
49e6fdbf DH |
482 | } |
483 | ||
14cb109d | 484 | void seat_claim_position(Seat *s, Session *session, unsigned pos) { |
49e6fdbf DH |
485 | /* with VTs, the position is always the same as the VTnr */ |
486 | if (seat_has_vts(s)) | |
487 | pos = session->vtnr; | |
488 | ||
ca2d3784 | 489 | if (!GREEDY_REALLOC0(s->positions, s->position_count, pos + 1)) |
49e6fdbf DH |
490 | return; |
491 | ||
492 | seat_evict_position(s, session); | |
493 | ||
e6494a07 | 494 | session->position = pos; |
da770c38 | 495 | if (pos > 0) |
49e6fdbf DH |
496 | s->positions[pos] = session; |
497 | } | |
498 | ||
499 | static void seat_assign_position(Seat *s, Session *session) { | |
14cb109d | 500 | unsigned pos; |
49e6fdbf | 501 | |
e6494a07 | 502 | if (session->position > 0) |
49e6fdbf DH |
503 | return; |
504 | ||
505 | for (pos = 1; pos < s->position_count; ++pos) | |
506 | if (!s->positions[pos]) | |
507 | break; | |
508 | ||
509 | seat_claim_position(s, session, pos); | |
510 | } | |
511 | ||
a185c5aa LP |
512 | int seat_attach_session(Seat *s, Session *session) { |
513 | assert(s); | |
514 | assert(session); | |
515 | assert(!session->seat); | |
3f49d45a | 516 | |
c506027a DH |
517 | if (!seat_has_vts(s) != !session->vtnr) |
518 | return -EINVAL; | |
519 | ||
a185c5aa | 520 | session->seat = s; |
71fda00f | 521 | LIST_PREPEND(sessions_by_seat, s->sessions, session); |
49e6fdbf | 522 | seat_assign_position(s, session); |
a185c5aa | 523 | |
bf7825ae | 524 | /* On seats with VTs, the VT logic defines which session is active. On |
3fdb2494 DH |
525 | * seats without VTs, we automatically activate new sessions. */ |
526 | if (!seat_has_vts(s)) | |
9418f147 | 527 | seat_set_active(s, session); |
9418f147 | 528 | |
a185c5aa LP |
529 | return 0; |
530 | } | |
531 | ||
d7bd01b5 DH |
532 | void seat_complete_switch(Seat *s) { |
533 | Session *session; | |
534 | ||
535 | assert(s); | |
536 | ||
537 | /* if no session-switch is pending or if it got canceled, do nothing */ | |
538 | if (!s->pending_switch) | |
539 | return; | |
540 | ||
ae2a15bc | 541 | session = TAKE_PTR(s->pending_switch); |
d7bd01b5 DH |
542 | |
543 | seat_set_active(s, session); | |
544 | } | |
545 | ||
bf7825ae DH |
546 | bool seat_has_vts(Seat *s) { |
547 | assert(s); | |
548 | ||
549 | return seat_is_seat0(s) && s->manager->console_active_fd >= 0; | |
550 | } | |
551 | ||
92432fcc | 552 | bool seat_is_seat0(Seat *s) { |
a185c5aa LP |
553 | assert(s); |
554 | ||
92432fcc | 555 | return s->manager->seat0 == s; |
a185c5aa LP |
556 | } |
557 | ||
addedec4 LP |
558 | bool seat_can_multi_session(Seat *s) { |
559 | assert(s); | |
560 | ||
bf7825ae | 561 | return seat_has_vts(s); |
addedec4 LP |
562 | } |
563 | ||
f1a8e221 LP |
564 | bool seat_can_tty(Seat *s) { |
565 | assert(s); | |
566 | ||
bf7825ae | 567 | return seat_has_vts(s); |
f1a8e221 LP |
568 | } |
569 | ||
718d006a DH |
570 | bool seat_has_master_device(Seat *s) { |
571 | assert(s); | |
572 | ||
573 | /* device list is ordered by "master" flag */ | |
574 | return !!s->devices && s->devices->master; | |
575 | } | |
576 | ||
f1a8e221 LP |
577 | bool seat_can_graphical(Seat *s) { |
578 | assert(s); | |
579 | ||
718d006a | 580 | return seat_has_master_device(s); |
f1a8e221 LP |
581 | } |
582 | ||
a185c5aa LP |
583 | int seat_get_idle_hint(Seat *s, dual_timestamp *t) { |
584 | Session *session; | |
585 | bool idle_hint = true; | |
5cb14b37 | 586 | dual_timestamp ts = DUAL_TIMESTAMP_NULL; |
a185c5aa LP |
587 | |
588 | assert(s); | |
589 | ||
590 | LIST_FOREACH(sessions_by_seat, session, s->sessions) { | |
591 | dual_timestamp k; | |
592 | int ih; | |
593 | ||
594 | ih = session_get_idle_hint(session, &k); | |
595 | if (ih < 0) | |
596 | return ih; | |
597 | ||
598 | if (!ih) { | |
599 | if (!idle_hint) { | |
4a271908 | 600 | if (k.monotonic > ts.monotonic) |
a185c5aa LP |
601 | ts = k; |
602 | } else { | |
603 | idle_hint = false; | |
604 | ts = k; | |
605 | } | |
606 | } else if (idle_hint) { | |
607 | ||
608 | if (k.monotonic > ts.monotonic) | |
609 | ts = k; | |
610 | } | |
611 | } | |
612 | ||
613 | if (t) | |
614 | *t = ts; | |
615 | ||
616 | return idle_hint; | |
20263082 | 617 | } |
14c3baca | 618 | |
5c093a23 | 619 | bool seat_may_gc(Seat *s, bool drop_not_started) { |
14c3baca LP |
620 | assert(s); |
621 | ||
4a4b033f | 622 | if (drop_not_started && !s->started) |
5c093a23 | 623 | return true; |
932e3ee7 | 624 | |
92432fcc | 625 | if (seat_is_seat0(s)) |
5c093a23 | 626 | return false; |
14c3baca | 627 | |
5c093a23 | 628 | return !seat_has_master_device(s); |
14c3baca LP |
629 | } |
630 | ||
631 | void seat_add_to_gc_queue(Seat *s) { | |
632 | assert(s); | |
633 | ||
634 | if (s->in_gc_queue) | |
635 | return; | |
636 | ||
71fda00f | 637 | LIST_PREPEND(gc_queue, s->manager->seat_gc_queue, s); |
14c3baca LP |
638 | s->in_gc_queue = true; |
639 | } | |
3f49d45a LP |
640 | |
641 | static bool seat_name_valid_char(char c) { | |
642 | return | |
643 | (c >= 'a' && c <= 'z') || | |
644 | (c >= 'A' && c <= 'Z') || | |
645 | (c >= '0' && c <= '9') || | |
4c701096 | 646 | IN_SET(c, '-', '_'); |
3f49d45a LP |
647 | } |
648 | ||
649 | bool seat_name_is_valid(const char *name) { | |
650 | const char *p; | |
651 | ||
652 | assert(name); | |
653 | ||
654 | if (!startswith(name, "seat")) | |
655 | return false; | |
656 | ||
657 | if (!name[4]) | |
658 | return false; | |
659 | ||
660 | for (p = name; *p; p++) | |
661 | if (!seat_name_valid_char(*p)) | |
662 | return false; | |
663 | ||
1c9a2c10 LP |
664 | if (strlen(name) > 255) |
665 | return false; | |
666 | ||
3f49d45a LP |
667 | return true; |
668 | } |