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 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/user", 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
));
175 s
= hashmap_get(u
->manager
->sessions
, display
);
178 if (s
&& s
->display
&& x11_display_is_local(s
->display
))
184 static int user_mkdir_runtime_path(User
*u
) {
190 r
= safe_mkdir("/run/user", 0755, 0, 0);
192 log_error("Failed to create /run/user: %s", strerror(-r
));
196 if (!u
->runtime_path
) {
197 p
= strappend("/run/user/", u
->name
);
200 log_error("Out of memory");
206 r
= safe_mkdir(p
, 0700, u
->uid
, u
->gid
);
208 log_error("Failed to create runtime directory %s: %s", p
, strerror(-r
));
210 u
->runtime_path
= NULL
;
218 static int user_create_cgroup(User
*u
) {
225 if (!u
->cgroup_path
) {
226 if (asprintf(&p
, "%s/%s", u
->manager
->cgroup_path
, u
->name
) < 0) {
227 log_error("Out of memory");
233 r
= cg_create(SYSTEMD_CGROUP_CONTROLLER
, p
);
236 u
->cgroup_path
= NULL
;
237 log_error("Failed to create cgroup "SYSTEMD_CGROUP_CONTROLLER
":%s: %s", p
, strerror(-r
));
243 STRV_FOREACH(k
, u
->manager
->controllers
) {
244 r
= cg_create(*k
, p
);
246 log_warning("Failed to create cgroup %s:%s: %s", *k
, p
, strerror(-r
));
252 static int user_start_service(User
*u
) {
258 int user_start(User
*u
) {
266 /* Make XDG_RUNTIME_DIR */
267 r
= user_mkdir_runtime_path(u
);
272 r
= user_create_cgroup(u
);
276 /* Spawn user systemd */
277 r
= user_start_service(u
);
281 /* Save new user data */
284 dual_timestamp_get(&u
->timestamp
);
288 user_send_signal(u
, true);
293 static int user_stop_service(User
*u
) {
302 static int user_shall_kill(User
*u
) {
305 return u
->manager
->kill_user_processes
;
308 static int user_kill_cgroup(User
*u
) {
317 cg_trim(SYSTEMD_CGROUP_CONTROLLER
, u
->cgroup_path
, false);
319 if (user_shall_kill(u
)) {
321 r
= cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER
, u
->cgroup_path
, true);
323 log_error("Failed to kill user cgroup: %s", strerror(-r
));
326 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, u
->cgroup_path
, true);
328 log_error("Failed to check user cgroup: %s", strerror(-r
));
330 r
= cg_delete(SYSTEMD_CGROUP_CONTROLLER
, u
->cgroup_path
);
332 log_error("Failed to delete user cgroup: %s", strerror(-r
));
337 STRV_FOREACH(k
, u
->manager
->controllers
)
338 cg_trim(*k
, u
->cgroup_path
, true);
340 free(u
->cgroup_path
);
341 u
->cgroup_path
= NULL
;
346 static int user_remove_runtime_path(User
*u
) {
351 if (!u
->runtime_path
)
354 r
= rm_rf(u
->runtime_path
, false, true);
356 log_error("Failed to remove runtime directory %s: %s", u
->runtime_path
, strerror(-r
));
358 free(u
->runtime_path
);
359 u
->runtime_path
= NULL
;
364 int user_stop(User
*u
) {
372 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
) {
378 user_send_signal(u
, false);
381 k
= user_stop_service(u
);
386 k
= user_kill_cgroup(u
);
390 /* Kill XDG_RUNTIME_DIR */
391 k
= user_remove_runtime_path(u
);
395 unlink(u
->state_file
);
396 user_add_to_gc_queue(u
);
403 int user_get_idle_hint(User
*u
, dual_timestamp
*t
) {
405 bool idle_hint
= true;
406 dual_timestamp ts
= { 0, 0 };
410 LIST_FOREACH(sessions_by_user
, s
, u
->sessions
) {
414 ih
= session_get_idle_hint(s
, &k
);
420 if (k
.monotonic
< ts
.monotonic
)
426 } else if (idle_hint
) {
428 if (k
.monotonic
> ts
.monotonic
)
439 int user_check_gc(User
*u
) {
448 if (asprintf(&p
, "/var/lib/systemd/linger/%s", u
->name
) < 0)
451 r
= access(p
, F_OK
) >= 0;
457 if (u
->cgroup_path
) {
458 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, u
->cgroup_path
, false);
469 void user_add_to_gc_queue(User
*u
) {
475 LIST_PREPEND(User
, gc_queue
, u
->manager
->user_gc_queue
, u
);
476 u
->in_gc_queue
= true;
479 UserState
user_get_state(User
*u
) {
485 return USER_LINGERING
;
487 LIST_FOREACH(sessions_by_user
, i
, u
->sessions
)
488 if (session_is_active(i
))
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"
501 DEFINE_STRING_TABLE_LOOKUP(user_state
, UserState
);