1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
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.
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.
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/>.
26 #include "logind-user.h"
28 #include "cgroup-util.h"
32 User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) {
42 u->name = strdup(name);
48 if (asprintf(&u->state_file, "/run/systemd/users/%lu", (unsigned long) uid) < 0) {
54 if (hashmap_put(m->users, ULONG_TO_PTR((unsigned long) uid), u) < 0) {
68 void user_free(User *u) {
72 LIST_REMOVE(User, gc_queue, u->manager->user_gc_queue, u);
75 session_free(u->sessions);
80 free(u->runtime_path);
82 hashmap_remove(u->manager->users, ULONG_TO_PTR((unsigned long) u->uid));
89 int user_save(User *u) {
95 assert(u->state_file);
97 r = safe_mkdir("/run/systemd/users", 0755, 0, 0);
101 r = fopen_temporary(u->state_file, &f, &temp_path);
105 fchmod(fileno(f), 0644);
108 "# This is private data. Do not parse.\n"
112 user_state_to_string(user_get_state(u)));
136 if (ferror(f) || rename(temp_path, u->state_file) < 0) {
138 unlink(u->state_file);
147 log_error("Failed to save user data for %s: %s", u->name, strerror(-r));
152 int user_load(User *u) {
154 char *display = NULL;
159 r = parse_env_file(u->state_file, NEWLINE,
160 "CGROUP", &u->cgroup_path,
161 "RUNTIME", &u->runtime_path,
162 "SERVICE", &u->service,
171 log_error("Failed to read %s: %s", u->state_file, strerror(-r));
176 s = hashmap_get(u->manager->sessions, display);
180 if (s && s->display && x11_display_is_local(s->display))
186 static int user_mkdir_runtime_path(User *u) {
192 r = safe_mkdir("/run/user", 0755, 0, 0);
194 log_error("Failed to create /run/user: %s", strerror(-r));
198 if (!u->runtime_path) {
199 p = strappend("/run/user/", u->name);
202 log_error("Out of memory");
208 r = safe_mkdir(p, 0700, u->uid, u->gid);
210 log_error("Failed to create runtime directory %s: %s", p, strerror(-r));
212 u->runtime_path = NULL;
220 static int user_create_cgroup(User *u) {
227 if (!u->cgroup_path) {
228 if (asprintf(&p, "%s/%s", u->manager->cgroup_path, u->name) < 0) {
229 log_error("Out of memory");
235 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p);
238 u->cgroup_path = NULL;
239 log_error("Failed to create cgroup "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
245 STRV_FOREACH(k, u->manager->controllers) {
246 r = cg_create(*k, p);
248 log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r));
254 static int user_start_service(User *u) {
260 int user_start(User *u) {
268 log_info("New user %s logged in.", u->name);
270 /* Make XDG_RUNTIME_DIR */
271 r = user_mkdir_runtime_path(u);
276 r = user_create_cgroup(u);
280 /* Spawn user systemd */
281 r = user_start_service(u);
285 /* Save new user data */
288 dual_timestamp_get(&u->timestamp);
292 user_send_signal(u, true);
297 static int user_stop_service(User *u) {
306 static int user_shall_kill(User *u) {
309 if (!u->manager->kill_user_processes)
312 if (strv_contains(u->manager->kill_exclude_users, u->name))
315 if (strv_isempty(u->manager->kill_only_users))
318 return strv_contains(u->manager->kill_only_users, u->name);
321 static int user_kill_cgroup(User *u) {
330 cg_trim(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
332 if (user_shall_kill(u)) {
334 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
336 log_error("Failed to kill user cgroup: %s", strerror(-r));
339 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
341 log_error("Failed to check user cgroup: %s", strerror(-r));
343 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path);
345 log_error("Failed to delete user cgroup: %s", strerror(-r));
350 STRV_FOREACH(k, u->manager->controllers)
351 cg_trim(*k, u->cgroup_path, true);
353 free(u->cgroup_path);
354 u->cgroup_path = NULL;
359 static int user_remove_runtime_path(User *u) {
364 if (!u->runtime_path)
367 r = rm_rf(u->runtime_path, false, true);
369 log_error("Failed to remove runtime directory %s: %s", u->runtime_path, strerror(-r));
371 free(u->runtime_path);
372 u->runtime_path = NULL;
377 int user_stop(User *u) {
383 log_info("User %s logged out.", u->name);
385 LIST_FOREACH(sessions_by_user, s, u->sessions) {
392 k = user_stop_service(u);
397 k = user_kill_cgroup(u);
401 /* Kill XDG_RUNTIME_DIR */
402 k = user_remove_runtime_path(u);
406 unlink(u->state_file);
407 user_add_to_gc_queue(u);
410 user_send_signal(u, false);
417 int user_get_idle_hint(User *u, dual_timestamp *t) {
419 bool idle_hint = true;
420 dual_timestamp ts = { 0, 0 };
424 LIST_FOREACH(sessions_by_user, s, u->sessions) {
428 ih = session_get_idle_hint(s, &k);
434 if (k.monotonic < ts.monotonic)
440 } else if (idle_hint) {
442 if (k.monotonic > ts.monotonic)
453 int user_check_gc(User *u) {
462 if (asprintf(&p, "/var/lib/systemd/linger/%s", u->name) < 0)
465 r = access(p, F_OK) >= 0;
471 if (u->cgroup_path) {
472 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
483 void user_add_to_gc_queue(User *u) {
489 LIST_PREPEND(User, gc_queue, u->manager->user_gc_queue, u);
490 u->in_gc_queue = true;
493 UserState user_get_state(User *u) {
499 return USER_LINGERING;
501 LIST_FOREACH(sessions_by_user, i, u->sessions)
502 if (session_is_active(i))
508 static const char* const user_state_table[_USER_STATE_MAX] = {
509 [USER_OFFLINE] = "offline",
510 [USER_LINGERING] = "lingering",
511 [USER_ONLINE] = "online",
512 [USER_ACTIVE] = "active"
515 DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);