logind-user.c revision b9460fdc8b57102b665a662b82efcced4b1af72b
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2011 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart PoetteringUser* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) {
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen if (asprintf(&u->state_file, "/run/systemd/users/"UID_FMT, uid) < 0)
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen if (hashmap_put(m->users, UID_TO_PTR(uid), u) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering LIST_REMOVE(gc_queue, u->manager->user_gc_queue, u);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering hashmap_remove(u->manager->user_units, u->slice);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering hashmap_remove(u->manager->user_units, u->service);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering hashmap_remove(u->manager->users, UID_TO_PTR(u->uid));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = mkdir_safe_label("/run/systemd/users", 0755, 0, 0);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = fopen_temporary(u->state_file, &f, &temp_path);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "# This is private data. Do not parse.\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering user_state_to_string(user_get_state(u)));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fprintf(f, "RUNTIME=%s\n", u->runtime_path);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fprintf(f, "SERVICE_JOB=%s\n", u->service_job);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fprintf(f, "SLICE_JOB=%s\n", u->slice_job);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fprintf(f, "DISPLAY=%s\n", u->display->id);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (dual_timestamp_is_set(&u->timestamp))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering LIST_FOREACH(sessions_by_user, i, u->sessions) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering LIST_FOREACH(sessions_by_user, i, u->sessions) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering LIST_FOREACH(sessions_by_user, i, u->sessions) {
23432a1c249b9c513e438bffe9778a7ce2ee74feZbigniew Jędrzejewski-Szmek LIST_FOREACH(sessions_by_user, i, u->sessions) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (session_get_state(i) == SESSION_CLOSING)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_FOREACH(sessions_by_user, i, u->sessions) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering LIST_FOREACH(sessions_by_user, i, u->sessions) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (session_get_state(i) == SESSION_CLOSING || !i->seat)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (ferror(f) || rename(temp_path, u->state_file) < 0) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek log_error_errno(r, "Failed to save user data %s: %m", u->state_file);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek _cleanup_free_ char *display = NULL, *realtime = NULL, *monotonic = NULL;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = parse_env_file(u->state_file, NEWLINE,
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek log_error_errno(r, "Failed to read %s: %m", u->state_file);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering s = hashmap_get(u->manager->sessions, display);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (s && s->display && display_is_local(s->display))
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen unsigned long long l;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek unsigned long long l;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (sscanf(monotonic, "%llu", &l) > 0)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = mkdir_safe_label("/run/user", 0755, 0, 0);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return log_error_errno(r, "Failed to create /run/user: %m");
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (asprintf(&p, "/run/user/" UID_FMT, u->uid) < 0)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (path_is_mount_point(p, 0) <= 0) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = asprintf(&t, "mode=0700,smackfsroot=*,uid=" UID_FMT ",gid=" GID_FMT ",size=%zu", u->uid, u->gid, u->manager->runtime_dir_size);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = asprintf(&t, "mode=0700,uid=" UID_FMT ",gid=" GID_FMT ",size=%zu", u->uid, u->gid, u->manager->runtime_dir_size);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = mount("tmpfs", p, "tmpfs", MS_NODEV|MS_NOSUID, t);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = log_error_errno(errno, "Failed to mount per-user tmpfs directory %s: %m", p);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering /* Lacking permissions, maybe
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering * CAP_SYS_ADMIN-less container? In this case,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering * just use a normal directory. */
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = chmod_and_chown(p, 0700, u->uid, u->gid);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering log_error_errno(r, "Failed to change runtime directory ownership and mode: %m");
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering /* Try to clean up, but ignore errors */
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering char lu[DECIMAL_STR_MAX(uid_t) + 1], *slice;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = slice_build_subslice(SPECIAL_USER_SLICE, lu, &slice);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = manager_start_unit(u->manager, slice, &error, &job);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering log_error("Failed to start user slice: %s", bus_error_message(&error, r));
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering hashmap_put(u->manager->user_units, u->slice, u);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering char lu[DECIMAL_STR_MAX(uid_t) + 1], *service;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = unit_name_build("user", lu, ".service", &service);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return log_error_errno(r, "Failed to build service name: %m");
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = manager_start_unit(u->manager, service, &error, &job);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering log_error("Failed to start user service: %s", bus_error_message(&error, r));
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek hashmap_put(u->manager->user_units, u->service, u);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering log_debug("New user %s logged in.", u->name);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering /* Make XDG_RUNTIME_DIR */
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering /* Create cgroup */
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek /* Spawn user systemd */
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!dual_timestamp_is_set(&u->timestamp))
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek dual_timestamp_get(&u->timestamp);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek /* Save new user data */
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = manager_stop_unit(u->manager, u->slice, &error, &job);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek log_error("Failed to stop user slice: %s", bus_error_message(&error, r));
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen r = manager_stop_unit(u->manager, u->service, &error, &job);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_error("Failed to stop user service: %s", bus_error_message(&error, r));
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_error_errno(r, "Failed to remove runtime directory %s: %m", u->runtime_path);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek /* Ignore cases where the directory isn't mounted, as that's
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek * quite possible, if we lacked the permissions to mount
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = umount2(u->runtime_path, MNT_DETACH);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0 && errno != EINVAL && errno != ENOENT)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_error_errno(errno, "Failed to unmount user runtime directory %s: %m", u->runtime_path);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = rm_rf(u->runtime_path, REMOVE_ROOT);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_error_errno(r, "Failed to remove runtime directory %s: %m", u->runtime_path);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekint user_stop(User *u, bool force) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek /* Stop jobs have already been queued */
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen LIST_FOREACH(sessions_by_user, s, u->sessions) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen /* Kill systemd */
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen /* Kill cgroup */
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen int r = 0, k;
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering LIST_FOREACH(sessions_by_user, s, u->sessions) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen /* Kill XDG_RUNTIME_DIR */
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen /* Clean SysV + POSIX IPC objects */
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringint user_get_idle_hint(User *u, dual_timestamp *t) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering LIST_FOREACH(sessions_by_user, s, u->sessions) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering p = strjoina("/var/lib/systemd/linger/", cc);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringbool user_check_gc(User *u, bool drop_not_started) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (u->slice_job && manager_job_is_active(u->manager, u->slice_job))
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek if (u->service_job && manager_job_is_active(u->manager, u->service_job))
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmekvoid user_add_to_gc_queue(User *u) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering LIST_PREPEND(gc_queue, u->manager->user_gc_queue, u);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering LIST_FOREACH(sessions_by_user, i, u->sessions) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return all_closing ? USER_CLOSING : USER_ONLINE;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmekint user_kill(User *u, int signo) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return manager_kill_unit(u->manager, u->slice, KILL_ALL, signo, NULL);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic bool elect_display_filter(Session *s) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek /* Return true if the session is a candidate for the user’s ‘primary
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek * session’ or ‘display’. */
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return (s->class == SESSION_USER && !s->stopping);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic int elect_display_compare(Session *s1, Session *s2) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek /* Indexed by SessionType. Lower numbers mean more preferred. */
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek const int type_ranks[_SESSION_TYPE_MAX] = {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen /* Calculate the partial order relationship between s1 and s2,
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen * returning < 0 if s1 is preferred as the user’s ‘primary session’,
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen * 0 if s1 and s2 are equally preferred or incomparable, or > 0 if s2
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen * is preferred.
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen * s1 or s2 may be NULL. */
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if ((s1 == NULL) != (s2 == NULL))
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if ((s1->class != SESSION_USER) != (s2->class != SESSION_USER))
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return (s1->class != SESSION_USER) - (s2->class != SESSION_USER);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if ((s1->type == _SESSION_TYPE_INVALID) != (s2->type == _SESSION_TYPE_INVALID))
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return (s1->type == _SESSION_TYPE_INVALID) - (s2->type == _SESSION_TYPE_INVALID);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return type_ranks[s1->type] - type_ranks[s2->type];
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmekvoid user_elect_display(User *u) {
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek /* This elects a primary session for each user, which we call
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek * the "display". We try to keep the assignment stable, but we
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek * "upgrade" to better choices. */
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek log_debug("Electing new display for user %s", u->name);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek LIST_FOREACH(sessions_by_user, s, u->sessions) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (elect_display_compare(s, u->display) < 0) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek log_debug("Choosing session %s in preference to %s", s->id, u->display ? u->display->id : "-");
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersenstatic const char* const user_state_table[_USER_STATE_MAX] = {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom GundersenDEFINE_STRING_TABLE_LOOKUP(user_state, UserState);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen const char *e;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen unsigned long ul;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (errno != 0 || f != e) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_syntax(unit, LOG_ERR, filename, line, errno ? errno : EINVAL, "Failed to parse percentage value, ignoring: %s", rvalue);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_syntax(unit, LOG_ERR, filename, line, errno ? errno : EINVAL, "Percentage value out of range, ignoring: %s", rvalue);
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering *sz = PAGE_ALIGN((size_t) ((physical_memory() * (uint64_t) ul) / (uint64_t) 100));
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen log_syntax(unit, LOG_ERR, filename, line, r < 0 ? -r : ERANGE, "Failed to parse size value, ignoring: %s", rvalue);