diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-06-20 03:45:08 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-06-20 03:49:59 +0200 |
commit | 9444b1f20e311f073864d81e913bd4f32fe95cfd (patch) | |
tree | 7b4752c690729df0acec75c9eb2382026bbf6899 /src/login/logind-session.c | |
parent | 77f40f165cc60a1d6b8a3503e4b7e46814d5935e (diff) |
logind: add infrastructure to keep track of machines, and move to slices
- This changes all logind cgroup objects to use slice objects rather
than fixed croup locations.
- logind can now collect minimal information about running
VMs/containers. As fixed cgroup locations can no longer be used we
need an entity that keeps track of machine cgroups in whatever slice
they might be located. Since logind already keeps track of users,
sessions and seats this is a trivial addition.
- nspawn will now register with logind and pass various bits of metadata
along. A new option "--slice=" has been added to place the container
in a specific slice.
- loginctl gained commands to list, introspect and terminate machines.
- user.slice and machine.slice will now be pulled in by logind.service,
since only logind.service requires this slice.
Diffstat (limited to 'src/login/logind-session.c')
-rw-r--r-- | src/login/logind-session.c | 165 |
1 files changed, 119 insertions, 46 deletions
diff --git a/src/login/logind-session.c b/src/login/logind-session.c index 4fd3985811..aba517d1f7 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -35,7 +35,7 @@ #include "logind-session.h" #include "fileio.h" -Session* session_new(Manager *m, User *u, const char *id) { +Session* session_new(Manager *m, const char *id) { Session *s; assert(m); @@ -61,9 +61,6 @@ Session* session_new(Manager *m, User *u, const char *id) { s->manager = m; s->fifo_fd = -1; - s->user = u; - - LIST_PREPEND(Session, sessions_by_user, u->sessions, s); return s; } @@ -99,6 +96,7 @@ void session_free(Session *s) { free(s->remote_host); free(s->remote_user); free(s->service); + free(s->slice); hashmap_remove(s->manager->sessions, s->id); session_remove_fifo(s); @@ -107,6 +105,14 @@ void session_free(Session *s) { free(s); } +void session_set_user(Session *s, User *u) { + assert(s); + assert(!s->user); + + s->user = u; + LIST_PREPEND(Session, sessions_by_user, u->sessions, s); +} + int session_save(Session *s) { _cleanup_fclose_ FILE *f = NULL; _cleanup_free_ char *temp_path = NULL; @@ -114,6 +120,9 @@ int session_save(Session *s) { assert(s); + if (!s->user) + return -ESTALE; + if (!s->started) return 0; @@ -174,6 +183,9 @@ int session_save(Session *s) { if (s->service) fprintf(f, "SERVICE=%s\n", s->service); + if (s->seat) + fprintf(f, "SLICE=%s\n", s->slice); + if (s->seat && seat_can_multi_session(s->seat)) fprintf(f, "VTNR=%i\n", s->vtnr); @@ -183,6 +195,13 @@ int session_save(Session *s) { if (s->audit_id > 0) fprintf(f, "AUDIT=%"PRIu32"\n", s->audit_id); + if (dual_timestamp_is_set(&s->timestamp)) + fprintf(f, + "REALTIME=%llu\n" + "MONOTONIC=%llu\n", + (unsigned long long) s->timestamp.realtime, + (unsigned long long) s->timestamp.monotonic); + fflush(f); if (ferror(f) || rename(temp_path, s->state_file) < 0) { @@ -199,14 +218,17 @@ finish: } int session_load(Session *s) { - char *remote = NULL, + _cleanup_free_ char *remote = NULL, *kill_processes = NULL, *seat = NULL, *vtnr = NULL, *leader = NULL, *audit_id = NULL, *type = NULL, - *class = NULL; + *class = NULL, + *uid = NULL, + *realtime = NULL, + *monotonic = NULL; int k, r; @@ -223,14 +245,44 @@ int session_load(Session *s) { "REMOTE_HOST", &s->remote_host, "REMOTE_USER", &s->remote_user, "SERVICE", &s->service, + "SLICE", &s->slice, "VTNR", &vtnr, "LEADER", &leader, "TYPE", &type, "CLASS", &class, + "UID", &uid, + "REALTIME", &realtime, + "MONOTONIC", &monotonic, NULL); - if (r < 0) - goto finish; + if (r < 0) { + log_error("Failed to read %s: %s", s->state_file, strerror(-r)); + return r; + } + + if (!s->user) { + uid_t u; + User *user; + + if (!uid) { + log_error("UID not specified for session %s", s->id); + return -ENOENT; + } + + r = parse_uid(uid, &u); + if (r < 0) { + log_error("Failed to parse UID value %s for session %s.", uid, s->id); + return r; + } + + user = hashmap_get(s->manager->users, ULONG_TO_PTR((unsigned long) u)); + if (!user) { + log_error("User of session %s not known.", s->id); + return -ENOENT; + } + + session_set_user(s, user); + } if (remote) { k = parse_boolean(remote); @@ -295,14 +347,17 @@ int session_load(Session *s) { close_nointr_nofail(fd); } -finish: - free(remote); - free(kill_processes); - free(seat); - free(vtnr); - free(leader); - free(audit_id); - free(class); + if (realtime) { + unsigned long long l; + if (sscanf(realtime, "%llu", &l) > 0) + s->timestamp.realtime = l; + } + + if (monotonic) { + unsigned long long l; + if (sscanf(monotonic, "%llu", &l) > 0) + s->timestamp.monotonic = l; + } return r; } @@ -311,6 +366,7 @@ int session_activate(Session *s) { int r; assert(s); + assert(s->user); if (s->vtnr < 0) return -ENOTSUP; @@ -407,17 +463,19 @@ static int session_create_one_group(Session *s, const char *controller, const ch int r; assert(s); + assert(s->user); assert(path); - if (s->leader > 0) { + if (s->leader > 0) r = cg_create_and_attach(controller, path, s->leader); - if (r < 0) - r = cg_create(controller, path, NULL); - } else - r = cg_create(controller, path, NULL); + else + r = -EINVAL; - if (r < 0) - return r; + if (r < 0) { + r = cg_create(controller, path, NULL); + if (r < 0) + return r; + } r = cg_set_task_access(controller, path, 0644, s->user->uid, s->user->gid, -1); if (r >= 0) @@ -428,7 +486,6 @@ static int session_create_one_group(Session *s, const char *controller, const ch static int session_create_cgroup(Session *s) { char **k; - char *p; int r; assert(s); @@ -446,30 +503,41 @@ static int session_create_cgroup(Session *s) { if (!escaped) return log_oom(); - p = strjoin(s->user->cgroup_path, "/", escaped, NULL); - if (!p) + if (s->slice) { + _cleanup_free_ char *slice = NULL; + + r = cg_slice_to_path(s->slice, &slice); + if (r < 0) + return r; + + s->cgroup_path = strjoin(s->manager->cgroup_root, "/", slice, "/", escaped, NULL); + } else + s->cgroup_path = strjoin(s->user->cgroup_path, "/", escaped, NULL); + + if (!s->cgroup_path) + return log_oom(); + } + + if (!s->slice) { + s->slice = strdup(s->user->slice); + if (!s->slice) return log_oom(); - } else - p = s->cgroup_path; + } - r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, p); + r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path); if (r < 0) { - log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r)); - free(p); - s->cgroup_path = NULL; + log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", s->cgroup_path, strerror(-r)); return r; } - s->cgroup_path = p; - STRV_FOREACH(k, s->controllers) { if (strv_contains(s->reset_controllers, *k)) continue; - r = session_create_one_group(s, *k, p); + r = session_create_one_group(s, *k, s->cgroup_path); if (r < 0) - log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r)); + log_warning("Failed to create %s:%s: %s", *k, s->cgroup_path, strerror(-r)); } STRV_FOREACH(k, s->manager->controllers) { @@ -479,9 +547,9 @@ static int session_create_cgroup(Session *s) { strv_contains(s->controllers, *k)) continue; - r = session_create_one_group(s, *k, p); + r = session_create_one_group(s, *k, s->cgroup_path); if (r < 0) - log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r)); + log_warning("Failed to create %s:%s: %s", *k, s->cgroup_path, strerror(-r)); } if (s->leader > 0) { @@ -502,7 +570,6 @@ static int session_create_cgroup(Session *s) { r = cg_attach(*k, "/", s->leader); if (r < 0) log_warning("Failed to reset controller %s: %s", *k, strerror(-r)); - } } @@ -517,7 +584,9 @@ int session_start(Session *s) { int r; assert(s); - assert(s->user); + + if (!s->user) + return -ESTALE; if (s->started) return 0; @@ -542,7 +611,8 @@ int session_start(Session *s) { /* Create X11 symlink */ session_link_x11_socket(s); - dual_timestamp_get(&s->timestamp); + if (!dual_timestamp_is_set(&s->timestamp)) + dual_timestamp_get(&s->timestamp); if (s->seat) seat_read_active_vt(s->seat); @@ -667,6 +737,9 @@ int session_stop(Session *s) { assert(s); + if (!s->user) + return -ESTALE; + if (s->started) log_struct(s->type == SESSION_TTY || s->type == SESSION_X11 ? LOG_INFO : LOG_DEBUG, MESSAGE_ID(SD_MESSAGE_SESSION_STOP), @@ -932,6 +1005,9 @@ int session_check_gc(Session *s, bool drop_not_started) { if (drop_not_started && !s->started) return 0; + if (!s->user) + return 0; + if (s->fifo_fd >= 0) { r = pipe_eof(s->fifo_fd); @@ -978,8 +1054,8 @@ SessionState session_get_state(Session *s) { } int session_kill(Session *s, KillWho who, int signo) { + _cleanup_set_free_ Set *pid_set = NULL; int r = 0; - Set *pid_set = NULL; assert(s); @@ -998,7 +1074,7 @@ int session_kill(Session *s, KillWho who, int signo) { pid_set = set_new(trivial_hash_func, trivial_compare_func); if (!pid_set) - return -ENOMEM; + return log_oom(); if (s->leader > 0) { q = set_put(pid_set, LONG_TO_PTR(s->leader)); @@ -1012,9 +1088,6 @@ int session_kill(Session *s, KillWho who, int signo) { r = q; } - if (pid_set) - set_free(pid_set); - return r; } |