]>
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 | ||
90821c93 | 26 | #include "logind-user.h" |
20263082 LP |
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 | ||
14c3baca | 38 | u = new0(User, 1); |
20263082 LP |
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 | ||
14c3baca LP |
71 | if (u->in_gc_queue) |
72 | LIST_REMOVE(User, gc_queue, u->manager->user_gc_queue, u); | |
73 | ||
20263082 LP |
74 | while (u->sessions) |
75 | session_free(u->sessions); | |
76 | ||
77 | free(u->cgroup_path); | |
78 | ||
79 | free(u->service); | |
80 | free(u->runtime_path); | |
81 | ||
82 | hashmap_remove(u->manager->users, ULONG_TO_PTR((unsigned long) u->uid)); | |
83 | ||
84 | free(u->name); | |
d2f92cdf | 85 | free(u->state_file); |
20263082 LP |
86 | free(u); |
87 | } | |
88 | ||
89 | int user_save(User *u) { | |
90 | FILE *f; | |
91 | int r; | |
14c3baca | 92 | char *temp_path; |
20263082 LP |
93 | |
94 | assert(u); | |
95 | assert(u->state_file); | |
96 | ||
97 | r = safe_mkdir("/run/systemd/user", 0755, 0, 0); | |
98 | if (r < 0) | |
14c3baca | 99 | goto finish; |
20263082 | 100 | |
14c3baca LP |
101 | r = fopen_temporary(u->state_file, &f, &temp_path); |
102 | if (r < 0) | |
103 | goto finish; | |
104 | ||
105 | fchmod(fileno(f), 0644); | |
20263082 LP |
106 | |
107 | fprintf(f, | |
14c3baca | 108 | "# This is private data. Do not parse.\n" |
20263082 LP |
109 | "NAME=%s\n" |
110 | "STATE=%s\n", | |
111 | u->name, | |
112 | user_state_to_string(user_get_state(u))); | |
113 | ||
114 | if (u->cgroup_path) | |
115 | fprintf(f, | |
116 | "CGROUP=%s\n", | |
117 | u->cgroup_path); | |
118 | ||
119 | if (u->runtime_path) | |
120 | fprintf(f, | |
121 | "RUNTIME=%s\n", | |
122 | u->runtime_path); | |
123 | ||
124 | if (u->service) | |
125 | fprintf(f, | |
126 | "SERVICE=%s\n", | |
127 | u->service); | |
128 | ||
129 | if (u->display) | |
130 | fprintf(f, | |
131 | "DISPLAY=%s\n", | |
132 | u->display->id); | |
133 | ||
134 | fflush(f); | |
14c3baca LP |
135 | |
136 | if (ferror(f) || rename(temp_path, u->state_file) < 0) { | |
20263082 LP |
137 | r = -errno; |
138 | unlink(u->state_file); | |
14c3baca | 139 | unlink(temp_path); |
20263082 LP |
140 | } |
141 | ||
142 | fclose(f); | |
14c3baca LP |
143 | free(temp_path); |
144 | ||
145 | finish: | |
146 | if (r < 0) | |
147 | log_error("Failed to save user data for %s: %s", u->name, strerror(-r)); | |
148 | ||
20263082 LP |
149 | return r; |
150 | } | |
151 | ||
152 | int user_load(User *u) { | |
153 | int r; | |
154 | char *display = NULL; | |
155 | Session *s; | |
156 | ||
157 | assert(u); | |
158 | ||
a185c5aa | 159 | r = parse_env_file(u->state_file, NEWLINE, |
20263082 LP |
160 | "CGROUP", &u->cgroup_path, |
161 | "RUNTIME", &u->runtime_path, | |
162 | "SERVICE", &u->service, | |
163 | "DISPLAY", &display, | |
164 | NULL); | |
165 | if (r < 0) { | |
166 | free(display); | |
167 | ||
168 | if (r == -ENOENT) | |
169 | return 0; | |
170 | ||
171 | log_error("Failed to read %s: %s", u->state_file, strerror(-r)); | |
172 | return r; | |
173 | } | |
174 | ||
175 | s = hashmap_get(u->manager->sessions, display); | |
176 | free(display); | |
177 | ||
178 | if (s && s->display && x11_display_is_local(s->display)) | |
179 | u->display = s; | |
180 | ||
181 | return r; | |
182 | } | |
183 | ||
184 | static int user_mkdir_runtime_path(User *u) { | |
185 | char *p; | |
186 | int r; | |
187 | ||
188 | assert(u); | |
189 | ||
190 | r = safe_mkdir("/run/user", 0755, 0, 0); | |
191 | if (r < 0) { | |
192 | log_error("Failed to create /run/user: %s", strerror(-r)); | |
193 | return r; | |
194 | } | |
195 | ||
196 | if (!u->runtime_path) { | |
197 | p = strappend("/run/user/", u->name); | |
198 | ||
199 | if (!p) { | |
200 | log_error("Out of memory"); | |
201 | return -ENOMEM; | |
202 | } | |
203 | } else | |
204 | p = u->runtime_path; | |
205 | ||
206 | r = safe_mkdir(p, 0700, u->uid, u->gid); | |
207 | if (r < 0) { | |
208 | log_error("Failed to create runtime directory %s: %s", p, strerror(-r)); | |
209 | free(p); | |
210 | u->runtime_path = NULL; | |
211 | return r; | |
212 | } | |
213 | ||
214 | u->runtime_path = p; | |
215 | return 0; | |
216 | } | |
217 | ||
218 | static int user_create_cgroup(User *u) { | |
219 | char **k; | |
220 | char *p; | |
221 | int r; | |
222 | ||
223 | assert(u); | |
224 | ||
225 | if (!u->cgroup_path) { | |
226 | if (asprintf(&p, "%s/%s", u->manager->cgroup_path, u->name) < 0) { | |
227 | log_error("Out of memory"); | |
228 | return -ENOMEM; | |
229 | } | |
230 | } else | |
231 | p = u->cgroup_path; | |
232 | ||
233 | r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p); | |
234 | if (r < 0) { | |
235 | free(p); | |
236 | u->cgroup_path = NULL; | |
237 | log_error("Failed to create cgroup "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r)); | |
238 | return r; | |
239 | } | |
240 | ||
241 | u->cgroup_path = p; | |
242 | ||
243 | STRV_FOREACH(k, u->manager->controllers) { | |
244 | r = cg_create(*k, p); | |
245 | if (r < 0) | |
246 | log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r)); | |
247 | } | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | static int user_start_service(User *u) { | |
253 | assert(u); | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
258 | int user_start(User *u) { | |
259 | int r; | |
260 | ||
261 | assert(u); | |
262 | ||
9418f147 LP |
263 | if (u->started) |
264 | return 0; | |
265 | ||
20263082 LP |
266 | /* Make XDG_RUNTIME_DIR */ |
267 | r = user_mkdir_runtime_path(u); | |
268 | if (r < 0) | |
269 | return r; | |
270 | ||
271 | /* Create cgroup */ | |
272 | r = user_create_cgroup(u); | |
273 | if (r < 0) | |
274 | return r; | |
275 | ||
276 | /* Spawn user systemd */ | |
277 | r = user_start_service(u); | |
278 | if (r < 0) | |
279 | return r; | |
280 | ||
14c3baca LP |
281 | /* Save new user data */ |
282 | user_save(u); | |
283 | ||
20263082 LP |
284 | dual_timestamp_get(&u->timestamp); |
285 | ||
9418f147 LP |
286 | u->started = true; |
287 | ||
da119395 LP |
288 | user_send_signal(u, true); |
289 | ||
20263082 LP |
290 | return 0; |
291 | } | |
292 | ||
293 | static int user_stop_service(User *u) { | |
294 | assert(u); | |
295 | ||
296 | if (!u->service) | |
297 | return 0; | |
298 | ||
299 | return 0; | |
300 | } | |
301 | ||
302 | static int user_shall_kill(User *u) { | |
303 | assert(u); | |
304 | ||
305 | return u->manager->kill_user_processes; | |
306 | } | |
307 | ||
308 | static int user_kill_cgroup(User *u) { | |
309 | int r; | |
310 | char **k; | |
311 | ||
312 | assert(u); | |
313 | ||
314 | if (!u->cgroup_path) | |
315 | return 0; | |
316 | ||
317 | cg_trim(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false); | |
318 | ||
319 | if (user_shall_kill(u)) { | |
320 | ||
321 | r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true); | |
322 | if (r < 0) | |
323 | log_error("Failed to kill user cgroup: %s", strerror(-r)); | |
324 | } else { | |
325 | ||
326 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true); | |
327 | if (r < 0) | |
328 | log_error("Failed to check user cgroup: %s", strerror(-r)); | |
329 | else if (r > 0) { | |
330 | r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path); | |
331 | if (r < 0) | |
332 | log_error("Failed to delete user cgroup: %s", strerror(-r)); | |
333 | } else | |
334 | r = -EBUSY; | |
335 | } | |
336 | ||
337 | STRV_FOREACH(k, u->manager->controllers) | |
338 | cg_trim(*k, u->cgroup_path, true); | |
339 | ||
340 | free(u->cgroup_path); | |
341 | u->cgroup_path = NULL; | |
342 | ||
343 | return r; | |
344 | } | |
345 | ||
346 | static int user_remove_runtime_path(User *u) { | |
347 | int r; | |
348 | ||
349 | assert(u); | |
350 | ||
351 | if (!u->runtime_path) | |
352 | return 0; | |
353 | ||
354 | r = rm_rf(u->runtime_path, false, true); | |
355 | if (r < 0) | |
356 | log_error("Failed to remove runtime directory %s: %s", u->runtime_path, strerror(-r)); | |
357 | ||
358 | free(u->runtime_path); | |
359 | u->runtime_path = NULL; | |
360 | ||
361 | return r; | |
362 | } | |
363 | ||
364 | int user_stop(User *u) { | |
365 | Session *s; | |
366 | int r = 0, k; | |
367 | assert(u); | |
368 | ||
9418f147 LP |
369 | if (!u->started) |
370 | return 0; | |
371 | ||
20263082 LP |
372 | LIST_FOREACH(sessions_by_user, s, u->sessions) { |
373 | k = session_stop(s); | |
374 | if (k < 0) | |
375 | r = k; | |
376 | } | |
377 | ||
da119395 LP |
378 | user_send_signal(u, false); |
379 | ||
20263082 LP |
380 | /* Kill systemd */ |
381 | k = user_stop_service(u); | |
382 | if (k < 0) | |
383 | r = k; | |
384 | ||
385 | /* Kill cgroup */ | |
386 | k = user_kill_cgroup(u); | |
387 | if (k < 0) | |
388 | r = k; | |
389 | ||
390 | /* Kill XDG_RUNTIME_DIR */ | |
391 | k = user_remove_runtime_path(u); | |
392 | if (k < 0) | |
393 | r = k; | |
394 | ||
d2f92cdf LP |
395 | unlink(u->state_file); |
396 | user_add_to_gc_queue(u); | |
397 | ||
9418f147 LP |
398 | u->started = false; |
399 | ||
20263082 LP |
400 | return r; |
401 | } | |
402 | ||
a185c5aa LP |
403 | int user_get_idle_hint(User *u, dual_timestamp *t) { |
404 | Session *s; | |
405 | bool idle_hint = true; | |
406 | dual_timestamp ts = { 0, 0 }; | |
407 | ||
408 | assert(u); | |
409 | ||
410 | LIST_FOREACH(sessions_by_user, s, u->sessions) { | |
411 | dual_timestamp k; | |
412 | int ih; | |
413 | ||
414 | ih = session_get_idle_hint(s, &k); | |
415 | if (ih < 0) | |
416 | return ih; | |
417 | ||
418 | if (!ih) { | |
419 | if (!idle_hint) { | |
420 | if (k.monotonic < ts.monotonic) | |
421 | ts = k; | |
422 | } else { | |
423 | idle_hint = false; | |
424 | ts = k; | |
425 | } | |
426 | } else if (idle_hint) { | |
427 | ||
428 | if (k.monotonic > ts.monotonic) | |
429 | ts = k; | |
430 | } | |
431 | } | |
432 | ||
433 | if (t) | |
434 | *t = ts; | |
435 | ||
436 | return idle_hint; | |
437 | } | |
438 | ||
20263082 LP |
439 | int user_check_gc(User *u) { |
440 | int r; | |
441 | char *p; | |
442 | ||
443 | assert(u); | |
444 | ||
445 | if (u->sessions) | |
446 | return 1; | |
447 | ||
448 | if (asprintf(&p, "/var/lib/systemd/linger/%s", u->name) < 0) | |
449 | return -ENOMEM; | |
450 | ||
451 | r = access(p, F_OK) >= 0; | |
452 | free(p); | |
453 | ||
454 | if (r > 0) | |
455 | return 1; | |
456 | ||
457 | if (u->cgroup_path) { | |
458 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false); | |
459 | if (r < 0) | |
460 | return r; | |
461 | ||
462 | if (r <= 0) | |
463 | return 1; | |
464 | } | |
465 | ||
466 | return 0; | |
467 | } | |
468 | ||
14c3baca LP |
469 | void user_add_to_gc_queue(User *u) { |
470 | assert(u); | |
471 | ||
472 | if (u->in_gc_queue) | |
473 | return; | |
474 | ||
475 | LIST_PREPEND(User, gc_queue, u->manager->user_gc_queue, u); | |
476 | u->in_gc_queue = true; | |
477 | } | |
478 | ||
20263082 LP |
479 | UserState user_get_state(User *u) { |
480 | Session *i; | |
481 | ||
482 | assert(u); | |
483 | ||
484 | if (!u->sessions) | |
485 | return USER_LINGERING; | |
486 | ||
487 | LIST_FOREACH(sessions_by_user, i, u->sessions) | |
488 | if (session_is_active(i)) | |
489 | return USER_ACTIVE; | |
490 | ||
491 | return USER_ONLINE; | |
492 | } | |
493 | ||
494 | static const char* const user_state_table[_USER_STATE_MAX] = { | |
495 | [USER_OFFLINE] = "offline", | |
496 | [USER_LINGERING] = "lingering", | |
497 | [USER_ONLINE] = "online", | |
498 | [USER_ACTIVE] = "active" | |
499 | }; | |
500 | ||
501 | DEFINE_STRING_TABLE_LOOKUP(user_state, UserState); |