]>
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> | |
28 | ||
90821c93 | 29 | #include "logind-seat.h" |
20263082 LP |
30 | #include "util.h" |
31 | ||
32 | Seat *seat_new(Manager *m, const char *id) { | |
33 | Seat *s; | |
34 | ||
35 | assert(m); | |
36 | assert(id); | |
37 | ||
38 | s = new0(Seat, 1); | |
39 | if (!s) | |
40 | return NULL; | |
41 | ||
42 | s->state_file = strappend("/run/systemd/seat/", id); | |
43 | if (!s->state_file) { | |
44 | free(s); | |
45 | return NULL; | |
46 | } | |
47 | ||
48 | s->id = file_name_from_path(s->state_file); | |
49 | ||
50 | if (hashmap_put(m->seats, s->id, s) < 0) { | |
51 | free(s->id); | |
52 | free(s); | |
53 | return NULL; | |
54 | } | |
55 | ||
56 | s->manager = m; | |
57 | ||
58 | return s; | |
59 | } | |
60 | ||
61 | void seat_free(Seat *s) { | |
62 | assert(s); | |
63 | ||
64 | while (s->sessions) | |
65 | session_free(s->sessions); | |
66 | ||
67 | assert(!s->active); | |
68 | ||
69 | while (s->devices) | |
70 | device_free(s->devices); | |
71 | ||
72 | hashmap_remove(s->manager->seats, s->id); | |
73 | ||
74 | free(s->state_file); | |
75 | free(s); | |
76 | } | |
77 | ||
78 | int seat_save(Seat *s) { | |
79 | FILE *f; | |
80 | int r; | |
81 | ||
82 | assert(s); | |
83 | ||
84 | r = safe_mkdir("/run/systemd/seat", 0755, 0, 0); | |
85 | if (r < 0) | |
86 | return r; | |
87 | ||
88 | f = fopen(s->state_file, "we"); | |
89 | if (!f) | |
90 | return -errno; | |
91 | ||
92 | fprintf(f, | |
93 | "IS_VTCONSOLE=%i\n", | |
94 | s->manager->vtconsole == s); | |
95 | ||
96 | if (s->active) { | |
97 | assert(s->active->user); | |
98 | ||
99 | fprintf(f, | |
100 | "ACTIVE=%s\n" | |
101 | "ACTIVE_UID=%lu\n", | |
102 | s->active->id, | |
103 | (unsigned long) s->active->user->uid); | |
104 | } | |
105 | ||
106 | if (s->sessions) { | |
107 | Session *i; | |
108 | fputs("OTHER_UIDS=", f); | |
109 | ||
110 | LIST_FOREACH(sessions_by_seat, i, s->sessions) { | |
111 | assert(i->user); | |
112 | ||
113 | if (i == s->active) | |
114 | continue; | |
115 | ||
116 | fprintf(f, | |
117 | "%s%lu", | |
118 | i == s->sessions ? "" : " ", | |
119 | (unsigned long) i->user->uid); | |
120 | } | |
121 | } | |
122 | ||
123 | fflush(f); | |
124 | if (ferror(f)) { | |
125 | r = -errno; | |
126 | unlink(s->state_file); | |
127 | } | |
128 | ||
129 | fclose(f); | |
130 | return r; | |
131 | } | |
132 | ||
133 | int seat_load(Seat *s) { | |
134 | assert(s); | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
139 | static int vt_allocate(int vtnr) { | |
140 | int fd, r; | |
141 | char *p; | |
142 | ||
143 | assert(vtnr >= 1); | |
144 | ||
145 | if (asprintf(&p, "/dev/tty%i", vtnr) < 0) | |
146 | return -ENOMEM; | |
147 | ||
148 | fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC); | |
149 | free(p); | |
150 | ||
151 | r = fd < 0 ? -errno : 0; | |
152 | ||
153 | if (fd >= 0) | |
154 | close_nointr_nofail(fd); | |
155 | ||
156 | return r; | |
157 | } | |
158 | ||
159 | int seat_preallocate_vts(Seat *s) { | |
160 | int i, r = 0; | |
161 | ||
162 | assert(s); | |
163 | assert(s->manager); | |
164 | ||
165 | if (s->manager->n_autovts <= 0) | |
166 | return 0; | |
167 | ||
168 | if (s->manager->vtconsole != s) | |
169 | return 0; | |
170 | ||
171 | for (i = 1; i < s->manager->n_autovts; i++) { | |
172 | int q; | |
173 | ||
174 | q = vt_allocate(i); | |
175 | if (r >= 0 && q < 0) | |
176 | r = q; | |
177 | } | |
178 | ||
179 | return r; | |
180 | } | |
181 | ||
182 | int seat_apply_acls(Seat *s) { | |
183 | assert(s); | |
184 | ||
185 | ||
186 | return 0; | |
187 | } | |
188 | ||
189 | static int vt_is_busy(int vtnr) { | |
190 | struct vt_stat vt_stat; | |
191 | int r = 0, fd; | |
192 | ||
193 | assert(vtnr >= 1); | |
194 | ||
195 | fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC); | |
196 | if (fd < 0) | |
197 | return -errno; | |
198 | ||
199 | if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0) | |
200 | r = -errno; | |
201 | else | |
202 | r = !!(vt_stat.v_state & (1 << vtnr)); | |
203 | ||
204 | close_nointr_nofail(fd); | |
205 | ||
206 | return r; | |
207 | } | |
208 | ||
209 | void seat_active_vt_changed(Seat *s, int vtnr) { | |
210 | Session *i; | |
211 | ||
212 | assert(s); | |
213 | assert(vtnr >= 1); | |
214 | assert(s->manager->vtconsole == s); | |
215 | ||
216 | s->active = NULL; | |
217 | ||
218 | LIST_FOREACH(sessions_by_seat, i, s->sessions) | |
219 | if (i->vtnr == vtnr) { | |
220 | s->active = i; | |
221 | break; | |
222 | } | |
223 | ||
224 | seat_apply_acls(s); | |
225 | ||
226 | if (vt_is_busy(vtnr) == 0) | |
227 | manager_spawn_autovt(s->manager, vtnr); | |
228 | } | |
229 | ||
230 | int seat_stop(Seat *s) { | |
231 | Session *session; | |
232 | int r = 0; | |
233 | ||
234 | assert(s); | |
235 | ||
236 | LIST_FOREACH(sessions_by_seat, session, s->sessions) { | |
237 | int k; | |
238 | ||
239 | k = session_stop(session); | |
240 | if (k < 0) | |
241 | r = k; | |
242 | } | |
243 | ||
244 | return r; | |
245 | } |