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);
100 r = safe_mkdir("/run/systemd/users", 0755, 0, 0);
104 r = fopen_temporary(u->state_file, &f, &temp_path);
108 fchmod(fileno(f), 0644);
111 "# This is private data. Do not parse.\n"
115 user_state_to_string(user_get_state(u)));
139 if (ferror(f) || rename(temp_path, u->state_file) < 0) {
141 unlink(u->state_file);
150 log_error("Failed to save user data for %s: %s", u->name, strerror(-r));
155 int user_load(User *u) {
157 char *display = NULL;
162 r = parse_env_file(u->state_file, NEWLINE,
163 "CGROUP", &u->cgroup_path,
164 "RUNTIME", &u->runtime_path,
165 "SERVICE", &u->service,
174 log_error("Failed to read %s: %s", u->state_file, strerror(-r));
179 s = hashmap_get(u->manager->sessions, display);
183 if (s && s->display && display_is_local(s->display))
189 static int user_mkdir_runtime_path(User *u) {
195 r = safe_mkdir("/run/user", 0755, 0, 0);
197 log_error("Failed to create /run/user: %s", strerror(-r));
201 if (!u->runtime_path) {
202 p = strappend("/run/user/", u->name);
205 log_error("Out of memory");
211 r = safe_mkdir(p, 0700, u->uid, u->gid);
213 log_error("Failed to create runtime directory %s: %s", p, strerror(-r));
215 u->runtime_path = NULL;
223 static int user_create_cgroup(User *u) {
230 if (!u->cgroup_path) {
231 if (asprintf(&p, "%s/%s", u->manager->cgroup_path, u->name) < 0) {
232 log_error("Out of memory");
238 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p);
240 log_error("Failed to create cgroup "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
242 u->cgroup_path = NULL;
248 STRV_FOREACH(k, u->manager->controllers) {
249 r = cg_create(*k, p);
251 log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r));
257 static int user_start_service(User *u) {
263 int user_start(User *u) {
271 log_info("New user %s logged in.", u->name);
273 /* Make XDG_RUNTIME_DIR */
274 r = user_mkdir_runtime_path(u);
279 r = user_create_cgroup(u);
283 /* Spawn user systemd */
284 r = user_start_service(u);
288 dual_timestamp_get(&u->timestamp);
292 /* Save new user data */
295 user_send_signal(u, true);
300 static int user_stop_service(User *u) {
309 static int user_shall_kill(User *u) {
312 if (!u->manager->kill_user_processes)
315 if (strv_contains(u->manager->kill_exclude_users, u->name))
318 if (strv_isempty(u->manager->kill_only_users))
321 return strv_contains(u->manager->kill_only_users, u->name);
324 static int user_kill_cgroup(User *u) {
333 cg_trim(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
335 if (user_shall_kill(u)) {
337 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
339 log_error("Failed to kill user cgroup: %s", strerror(-r));
342 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
344 log_error("Failed to check user cgroup: %s", strerror(-r));
346 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path);
348 log_error("Failed to delete user cgroup: %s", strerror(-r));
353 STRV_FOREACH(k, u->manager->controllers)
354 cg_trim(*k, u->cgroup_path, true);
356 free(u->cgroup_path);
357 u->cgroup_path = NULL;
362 static int user_remove_runtime_path(User *u) {
367 if (!u->runtime_path)
370 r = rm_rf(u->runtime_path, false, true);
372 log_error("Failed to remove runtime directory %s: %s", u->runtime_path, strerror(-r));
374 free(u->runtime_path);
375 u->runtime_path = NULL;
380 int user_stop(User *u) {
386 log_info("User %s logged out.", u->name);
388 LIST_FOREACH(sessions_by_user, s, u->sessions) {
395 k = user_stop_service(u);
400 k = user_kill_cgroup(u);
404 /* Kill XDG_RUNTIME_DIR */
405 k = user_remove_runtime_path(u);
409 unlink(u->state_file);
410 user_add_to_gc_queue(u);
413 user_send_signal(u, false);
420 int user_get_idle_hint(User *u, dual_timestamp *t) {
422 bool idle_hint = true;
423 dual_timestamp ts = { 0, 0 };
427 LIST_FOREACH(sessions_by_user, s, u->sessions) {
431 ih = session_get_idle_hint(s, &k);
437 if (k.monotonic < ts.monotonic)
443 } else if (idle_hint) {
445 if (k.monotonic > ts.monotonic)
456 int user_check_gc(User *u) {
465 if (asprintf(&p, "/var/lib/systemd/linger/%s", u->name) < 0)
468 r = access(p, F_OK) >= 0;
474 if (u->cgroup_path) {
475 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
486 void user_add_to_gc_queue(User *u) {
492 LIST_PREPEND(User, gc_queue, u->manager->user_gc_queue, u);
493 u->in_gc_queue = true;
496 UserState user_get_state(User *u) {
502 return USER_LINGERING;
504 LIST_FOREACH(sessions_by_user, i, u->sessions)
505 if (session_is_active(i))
511 static const char* const user_state_table[_USER_STATE_MAX] = {
512 [USER_OFFLINE] = "offline",
513 [USER_LINGERING] = "lingering",
514 [USER_ONLINE] = "online",
515 [USER_ACTIVE] = "active"
518 DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);