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 /* Make XDG_RUNTIME_DIR */
269 r = user_mkdir_runtime_path(u);
274 r = user_create_cgroup(u);
278 /* Spawn user systemd */
279 r = user_start_service(u);
283 /* Save new user data */
286 dual_timestamp_get(&u->timestamp);
290 user_send_signal(u, true);
295 static int user_stop_service(User *u) {
304 static int user_shall_kill(User *u) {
307 return u->manager->kill_user_processes;
310 static int user_kill_cgroup(User *u) {
319 cg_trim(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
321 if (user_shall_kill(u)) {
323 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
325 log_error("Failed to kill user cgroup: %s", strerror(-r));
328 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
330 log_error("Failed to check user cgroup: %s", strerror(-r));
332 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path);
334 log_error("Failed to delete user cgroup: %s", strerror(-r));
339 STRV_FOREACH(k, u->manager->controllers)
340 cg_trim(*k, u->cgroup_path, true);
342 free(u->cgroup_path);
343 u->cgroup_path = NULL;
348 static int user_remove_runtime_path(User *u) {
353 if (!u->runtime_path)
356 r = rm_rf(u->runtime_path, false, true);
358 log_error("Failed to remove runtime directory %s: %s", u->runtime_path, strerror(-r));
360 free(u->runtime_path);
361 u->runtime_path = NULL;
366 int user_stop(User *u) {
374 LIST_FOREACH(sessions_by_user, s, u->sessions) {
380 user_send_signal(u, false);
383 k = user_stop_service(u);
388 k = user_kill_cgroup(u);
392 /* Kill XDG_RUNTIME_DIR */
393 k = user_remove_runtime_path(u);
397 unlink(u->state_file);
398 user_add_to_gc_queue(u);
405 int user_get_idle_hint(User *u, dual_timestamp *t) {
407 bool idle_hint = true;
408 dual_timestamp ts = { 0, 0 };
412 LIST_FOREACH(sessions_by_user, s, u->sessions) {
416 ih = session_get_idle_hint(s, &k);
422 if (k.monotonic < ts.monotonic)
428 } else if (idle_hint) {
430 if (k.monotonic > ts.monotonic)
441 int user_check_gc(User *u) {
450 if (asprintf(&p, "/var/lib/systemd/linger/%s", u->name) < 0)
453 r = access(p, F_OK) >= 0;
459 if (u->cgroup_path) {
460 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
471 void user_add_to_gc_queue(User *u) {
477 LIST_PREPEND(User, gc_queue, u->manager->user_gc_queue, u);
478 u->in_gc_queue = true;
481 UserState user_get_state(User *u) {
487 return USER_LINGERING;
489 LIST_FOREACH(sessions_by_user, i, u->sessions)
490 if (session_is_active(i))
496 static const char* const user_state_table[_USER_STATE_MAX] = {
497 [USER_OFFLINE] = "offline",
498 [USER_LINGERING] = "lingering",
499 [USER_ONLINE] = "online",
500 [USER_ACTIVE] = "active"
503 DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);