]>
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 <string.h> | |
23 | #include <unistd.h> | |
24 | #include <errno.h> | |
25 | ||
26 | #include "logind.h" | |
27 | #include "util.h" | |
28 | #include "cgroup-util.h" | |
29 | #include "hashmap.h" | |
30 | #include "strv.h" | |
31 | ||
32 | User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) { | |
33 | User *u; | |
34 | ||
35 | assert(m); | |
36 | assert(name); | |
37 | ||
38 | u = new(User, 1); | |
39 | if (!u) | |
40 | return NULL; | |
41 | ||
42 | u->name = strdup(name); | |
43 | if (!u->name) { | |
44 | free(u); | |
45 | return NULL; | |
46 | } | |
47 | ||
48 | if (asprintf(&u->state_file, "/run/systemd/user/%lu", (unsigned long) uid) < 0) { | |
49 | free(u->name); | |
50 | free(u); | |
51 | return NULL; | |
52 | } | |
53 | ||
54 | if (hashmap_put(m->users, ULONG_TO_PTR((unsigned long) uid), u) < 0) { | |
55 | free(u->state_file); | |
56 | free(u->name); | |
57 | free(u); | |
58 | return NULL; | |
59 | } | |
60 | ||
61 | u->manager = m; | |
62 | u->uid = uid; | |
63 | u->gid = gid; | |
64 | ||
65 | return u; | |
66 | } | |
67 | ||
68 | void user_free(User *u) { | |
69 | assert(u); | |
70 | ||
71 | while (u->sessions) | |
72 | session_free(u->sessions); | |
73 | ||
74 | free(u->cgroup_path); | |
75 | ||
76 | free(u->service); | |
77 | free(u->runtime_path); | |
78 | ||
79 | hashmap_remove(u->manager->users, ULONG_TO_PTR((unsigned long) u->uid)); | |
80 | ||
81 | free(u->name); | |
82 | free(u->state_file); | |
83 | free(u); | |
84 | } | |
85 | ||
86 | int user_save(User *u) { | |
87 | FILE *f; | |
88 | int r; | |
89 | ||
90 | assert(u); | |
91 | assert(u->state_file); | |
92 | ||
93 | r = safe_mkdir("/run/systemd/user", 0755, 0, 0); | |
94 | if (r < 0) | |
95 | return r; | |
96 | ||
97 | f = fopen(u->state_file, "we"); | |
98 | if (!f) | |
99 | return -errno; | |
100 | ||
101 | fprintf(f, | |
102 | "NAME=%s\n" | |
103 | "STATE=%s\n", | |
104 | u->name, | |
105 | user_state_to_string(user_get_state(u))); | |
106 | ||
107 | if (u->cgroup_path) | |
108 | fprintf(f, | |
109 | "CGROUP=%s\n", | |
110 | u->cgroup_path); | |
111 | ||
112 | if (u->runtime_path) | |
113 | fprintf(f, | |
114 | "RUNTIME=%s\n", | |
115 | u->runtime_path); | |
116 | ||
117 | if (u->service) | |
118 | fprintf(f, | |
119 | "SERVICE=%s\n", | |
120 | u->service); | |
121 | ||
122 | if (u->display) | |
123 | fprintf(f, | |
124 | "DISPLAY=%s\n", | |
125 | u->display->id); | |
126 | ||
127 | fflush(f); | |
128 | if (ferror(f)) { | |
129 | r = -errno; | |
130 | unlink(u->state_file); | |
131 | } | |
132 | ||
133 | fclose(f); | |
134 | return r; | |
135 | } | |
136 | ||
137 | int user_load(User *u) { | |
138 | int r; | |
139 | char *display = NULL; | |
140 | Session *s; | |
141 | ||
142 | assert(u); | |
143 | ||
144 | r = parse_env_file(u->state_file, "r", | |
145 | "CGROUP", &u->cgroup_path, | |
146 | "RUNTIME", &u->runtime_path, | |
147 | "SERVICE", &u->service, | |
148 | "DISPLAY", &display, | |
149 | NULL); | |
150 | if (r < 0) { | |
151 | free(display); | |
152 | ||
153 | if (r == -ENOENT) | |
154 | return 0; | |
155 | ||
156 | log_error("Failed to read %s: %s", u->state_file, strerror(-r)); | |
157 | return r; | |
158 | } | |
159 | ||
160 | s = hashmap_get(u->manager->sessions, display); | |
161 | free(display); | |
162 | ||
163 | if (s && s->display && x11_display_is_local(s->display)) | |
164 | u->display = s; | |
165 | ||
166 | return r; | |
167 | } | |
168 | ||
169 | static int user_mkdir_runtime_path(User *u) { | |
170 | char *p; | |
171 | int r; | |
172 | ||
173 | assert(u); | |
174 | ||
175 | r = safe_mkdir("/run/user", 0755, 0, 0); | |
176 | if (r < 0) { | |
177 | log_error("Failed to create /run/user: %s", strerror(-r)); | |
178 | return r; | |
179 | } | |
180 | ||
181 | if (!u->runtime_path) { | |
182 | p = strappend("/run/user/", u->name); | |
183 | ||
184 | if (!p) { | |
185 | log_error("Out of memory"); | |
186 | return -ENOMEM; | |
187 | } | |
188 | } else | |
189 | p = u->runtime_path; | |
190 | ||
191 | r = safe_mkdir(p, 0700, u->uid, u->gid); | |
192 | if (r < 0) { | |
193 | log_error("Failed to create runtime directory %s: %s", p, strerror(-r)); | |
194 | free(p); | |
195 | u->runtime_path = NULL; | |
196 | return r; | |
197 | } | |
198 | ||
199 | u->runtime_path = p; | |
200 | return 0; | |
201 | } | |
202 | ||
203 | static int user_create_cgroup(User *u) { | |
204 | char **k; | |
205 | char *p; | |
206 | int r; | |
207 | ||
208 | assert(u); | |
209 | ||
210 | if (!u->cgroup_path) { | |
211 | if (asprintf(&p, "%s/%s", u->manager->cgroup_path, u->name) < 0) { | |
212 | log_error("Out of memory"); | |
213 | return -ENOMEM; | |
214 | } | |
215 | } else | |
216 | p = u->cgroup_path; | |
217 | ||
218 | r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p); | |
219 | if (r < 0) { | |
220 | free(p); | |
221 | u->cgroup_path = NULL; | |
222 | log_error("Failed to create cgroup "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r)); | |
223 | return r; | |
224 | } | |
225 | ||
226 | u->cgroup_path = p; | |
227 | ||
228 | STRV_FOREACH(k, u->manager->controllers) { | |
229 | r = cg_create(*k, p); | |
230 | if (r < 0) | |
231 | log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r)); | |
232 | } | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | static int user_start_service(User *u) { | |
238 | assert(u); | |
239 | ||
240 | return 0; | |
241 | } | |
242 | ||
243 | int user_start(User *u) { | |
244 | int r; | |
245 | ||
246 | assert(u); | |
247 | ||
248 | /* Make XDG_RUNTIME_DIR */ | |
249 | r = user_mkdir_runtime_path(u); | |
250 | if (r < 0) | |
251 | return r; | |
252 | ||
253 | /* Create cgroup */ | |
254 | r = user_create_cgroup(u); | |
255 | if (r < 0) | |
256 | return r; | |
257 | ||
258 | /* Spawn user systemd */ | |
259 | r = user_start_service(u); | |
260 | if (r < 0) | |
261 | return r; | |
262 | ||
263 | dual_timestamp_get(&u->timestamp); | |
264 | ||
265 | return 0; | |
266 | } | |
267 | ||
268 | static int user_stop_service(User *u) { | |
269 | assert(u); | |
270 | ||
271 | if (!u->service) | |
272 | return 0; | |
273 | ||
274 | return 0; | |
275 | } | |
276 | ||
277 | static int user_shall_kill(User *u) { | |
278 | assert(u); | |
279 | ||
280 | return u->manager->kill_user_processes; | |
281 | } | |
282 | ||
283 | static int user_kill_cgroup(User *u) { | |
284 | int r; | |
285 | char **k; | |
286 | ||
287 | assert(u); | |
288 | ||
289 | if (!u->cgroup_path) | |
290 | return 0; | |
291 | ||
292 | cg_trim(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false); | |
293 | ||
294 | if (user_shall_kill(u)) { | |
295 | ||
296 | r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true); | |
297 | if (r < 0) | |
298 | log_error("Failed to kill user cgroup: %s", strerror(-r)); | |
299 | } else { | |
300 | ||
301 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true); | |
302 | if (r < 0) | |
303 | log_error("Failed to check user cgroup: %s", strerror(-r)); | |
304 | else if (r > 0) { | |
305 | r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path); | |
306 | if (r < 0) | |
307 | log_error("Failed to delete user cgroup: %s", strerror(-r)); | |
308 | } else | |
309 | r = -EBUSY; | |
310 | } | |
311 | ||
312 | STRV_FOREACH(k, u->manager->controllers) | |
313 | cg_trim(*k, u->cgroup_path, true); | |
314 | ||
315 | free(u->cgroup_path); | |
316 | u->cgroup_path = NULL; | |
317 | ||
318 | return r; | |
319 | } | |
320 | ||
321 | static int user_remove_runtime_path(User *u) { | |
322 | int r; | |
323 | ||
324 | assert(u); | |
325 | ||
326 | if (!u->runtime_path) | |
327 | return 0; | |
328 | ||
329 | r = rm_rf(u->runtime_path, false, true); | |
330 | if (r < 0) | |
331 | log_error("Failed to remove runtime directory %s: %s", u->runtime_path, strerror(-r)); | |
332 | ||
333 | free(u->runtime_path); | |
334 | u->runtime_path = NULL; | |
335 | ||
336 | return r; | |
337 | } | |
338 | ||
339 | int user_stop(User *u) { | |
340 | Session *s; | |
341 | int r = 0, k; | |
342 | assert(u); | |
343 | ||
344 | LIST_FOREACH(sessions_by_user, s, u->sessions) { | |
345 | k = session_stop(s); | |
346 | if (k < 0) | |
347 | r = k; | |
348 | } | |
349 | ||
350 | /* Kill systemd */ | |
351 | k = user_stop_service(u); | |
352 | if (k < 0) | |
353 | r = k; | |
354 | ||
355 | /* Kill cgroup */ | |
356 | k = user_kill_cgroup(u); | |
357 | if (k < 0) | |
358 | r = k; | |
359 | ||
360 | /* Kill XDG_RUNTIME_DIR */ | |
361 | k = user_remove_runtime_path(u); | |
362 | if (k < 0) | |
363 | r = k; | |
364 | ||
365 | return r; | |
366 | } | |
367 | ||
368 | int user_check_gc(User *u) { | |
369 | int r; | |
370 | char *p; | |
371 | ||
372 | assert(u); | |
373 | ||
374 | if (u->sessions) | |
375 | return 1; | |
376 | ||
377 | if (asprintf(&p, "/var/lib/systemd/linger/%s", u->name) < 0) | |
378 | return -ENOMEM; | |
379 | ||
380 | r = access(p, F_OK) >= 0; | |
381 | free(p); | |
382 | ||
383 | if (r > 0) | |
384 | return 1; | |
385 | ||
386 | if (u->cgroup_path) { | |
387 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false); | |
388 | if (r < 0) | |
389 | return r; | |
390 | ||
391 | if (r <= 0) | |
392 | return 1; | |
393 | } | |
394 | ||
395 | return 0; | |
396 | } | |
397 | ||
398 | UserState user_get_state(User *u) { | |
399 | Session *i; | |
400 | ||
401 | assert(u); | |
402 | ||
403 | if (!u->sessions) | |
404 | return USER_LINGERING; | |
405 | ||
406 | LIST_FOREACH(sessions_by_user, i, u->sessions) | |
407 | if (session_is_active(i)) | |
408 | return USER_ACTIVE; | |
409 | ||
410 | return USER_ONLINE; | |
411 | } | |
412 | ||
413 | static const char* const user_state_table[_USER_STATE_MAX] = { | |
414 | [USER_OFFLINE] = "offline", | |
415 | [USER_LINGERING] = "lingering", | |
416 | [USER_ONLINE] = "online", | |
417 | [USER_ACTIVE] = "active" | |
418 | }; | |
419 | ||
420 | DEFINE_STRING_TABLE_LOOKUP(user_state, UserState); |