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/user/%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 session_free(u->sessions);
77 free(u->runtime_path);
79 hashmap_remove(u->manager->users, ULONG_TO_PTR((unsigned long) u->uid));
86 int user_save(User *u) {
91 assert(u->state_file);
93 r = safe_mkdir("/run/systemd/user", 0755, 0, 0);
97 f = fopen(u->state_file, "we");
105 user_state_to_string(user_get_state(u)));
130 unlink(u->state_file);
137 int user_load(User *u) {
139 char *display = NULL;
144 r = parse_env_file(u->state_file, "r",
145 "CGROUP", &u->cgroup_path,
146 "RUNTIME", &u->runtime_path,
147 "SERVICE", &u->service,
156 log_error("Failed to read %s: %s", u->state_file, strerror(-r));
160 s = hashmap_get(u->manager->sessions, display);
163 if (s && s->display && x11_display_is_local(s->display))
169 static int user_mkdir_runtime_path(User *u) {
175 r = safe_mkdir("/run/user", 0755, 0, 0);
177 log_error("Failed to create /run/user: %s", strerror(-r));
181 if (!u->runtime_path) {
182 p = strappend("/run/user/", u->name);
185 log_error("Out of memory");
191 r = safe_mkdir(p, 0700, u->uid, u->gid);
193 log_error("Failed to create runtime directory %s: %s", p, strerror(-r));
195 u->runtime_path = NULL;
203 static int user_create_cgroup(User *u) {
210 if (!u->cgroup_path) {
211 if (asprintf(&p, "%s/%s", u->manager->cgroup_path, u->name) < 0) {
212 log_error("Out of memory");
218 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p);
221 u->cgroup_path = NULL;
222 log_error("Failed to create cgroup "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
228 STRV_FOREACH(k, u->manager->controllers) {
229 r = cg_create(*k, p);
231 log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r));
237 static int user_start_service(User *u) {
243 int user_start(User *u) {
248 /* Make XDG_RUNTIME_DIR */
249 r = user_mkdir_runtime_path(u);
254 r = user_create_cgroup(u);
258 /* Spawn user systemd */
259 r = user_start_service(u);
263 dual_timestamp_get(&u->timestamp);
268 static int user_stop_service(User *u) {
277 static int user_shall_kill(User *u) {
280 return u->manager->kill_user_processes;
283 static int user_kill_cgroup(User *u) {
292 cg_trim(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
294 if (user_shall_kill(u)) {
296 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
298 log_error("Failed to kill user cgroup: %s", strerror(-r));
301 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true);
303 log_error("Failed to check user cgroup: %s", strerror(-r));
305 r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path);
307 log_error("Failed to delete user cgroup: %s", strerror(-r));
312 STRV_FOREACH(k, u->manager->controllers)
313 cg_trim(*k, u->cgroup_path, true);
315 free(u->cgroup_path);
316 u->cgroup_path = NULL;
321 static int user_remove_runtime_path(User *u) {
326 if (!u->runtime_path)
329 r = rm_rf(u->runtime_path, false, true);
331 log_error("Failed to remove runtime directory %s: %s", u->runtime_path, strerror(-r));
333 free(u->runtime_path);
334 u->runtime_path = NULL;
339 int user_stop(User *u) {
344 LIST_FOREACH(sessions_by_user, s, u->sessions) {
351 k = user_stop_service(u);
356 k = user_kill_cgroup(u);
360 /* Kill XDG_RUNTIME_DIR */
361 k = user_remove_runtime_path(u);
368 int user_check_gc(User *u) {
377 if (asprintf(&p, "/var/lib/systemd/linger/%s", u->name) < 0)
380 r = access(p, F_OK) >= 0;
386 if (u->cgroup_path) {
387 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false);
398 UserState user_get_state(User *u) {
404 return USER_LINGERING;
406 LIST_FOREACH(sessions_by_user, i, u->sessions)
407 if (session_is_active(i))
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"
420 DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);