#include "bus-error.h"
#include "logind-session.h"
+#define RELEASE_USEC (20*USEC_PER_SEC)
+
+static void session_remove_fifo(Session *s);
+
static unsigned long devt_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) {
uint64_t u = *(const dev_t*)p;
if (s->in_gc_queue)
LIST_REMOVE(gc_queue, s->manager->session_gc_queue, s);
+ s->timer_event_source = sd_event_source_unref(s->timer_event_source);
+
session_remove_fifo(s);
session_drop_controller(s);
hashmap_remove(s->manager->sessions, s->id);
+ s->vt_source = sd_event_source_unref(s->vt_source);
+
free(s->state_file);
free(s);
}
fprintf(f,
"# This is private data. Do not parse.\n"
- "UID=%lu\n"
+ "UID="UID_FMT"\n"
"USER=%s\n"
"ACTIVE=%i\n"
"STATE=%s\n"
"REMOTE=%i\n",
- (unsigned long) s->user->uid,
+ s->user->uid,
s->user->name,
session_is_active(s),
session_state_to_string(session_get_state(s)),
fprintf(f, "POS=%u\n", s->pos);
if (s->leader > 0)
- fprintf(f, "LEADER=%lu\n", (unsigned long) s->leader);
+ fprintf(f, "LEADER="PID_FMT"\n", s->leader);
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);
+ "REALTIME="USEC_FMT"\n"
+ "MONOTONIC="USEC_FMT"\n",
+ s->timestamp.realtime,
+ s->timestamp.monotonic);
if (s->controller)
fprintf(f, "CONTROLLER=%s\n", s->controller);
finish:
if (r < 0)
- log_error("Failed to save session data for %s: %s", s->id, strerror(-r));
+ log_error("Failed to save session data %s: %s", s->state_file, strerror(-r));
return r;
}
if (!s->scope) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_free_ char *description = NULL;
- const char *kill_mode;
char *scope, *job;
description = strjoin("Session ", s->id, " of user ", s->user->name, NULL);
if (!scope)
return log_oom();
- kill_mode = manager_shall_kill(s->manager, s->user->name) ? "control-group" : "none";
-
- r = manager_start_scope(s->manager, scope, s->leader, s->user->slice, description, "systemd-user-sessions.service", kill_mode, &error, &job);
+ r = manager_start_scope(s->manager, scope, s->leader, s->user->slice, description, "systemd-logind.service", &error, &job);
if (r < 0) {
log_error("Failed to start session scope %s: %s %s",
scope, bus_error_message(&error, r), error.name);
s->started = true;
- /* Save session data */
+ /* Save data */
session_save(s);
user_save(s->user);
+ if (s->seat)
+ seat_save(s->seat);
+ /* Send signals */
session_send_signal(s, true);
-
+ user_send_changed(s->user, "Sessions", NULL);
if (s->seat) {
- seat_save(s->seat);
-
if (s->seat->active == s)
seat_send_changed(s->seat, "Sessions", "ActiveSession", NULL);
else
seat_send_changed(s->seat, "Sessions", NULL);
}
- user_send_changed(s->user, "Sessions", NULL);
-
return 0;
}
if (!s->scope)
return 0;
- r = manager_stop_unit(s->manager, s->scope, &error, &job);
- if (r < 0) {
- log_error("Failed to stop session scope: %s", bus_error_message(&error, r));
- return r;
- }
+ if (manager_shall_kill(s->manager, s->user->name)) {
+ r = manager_stop_unit(s->manager, s->scope, &error, &job);
+ if (r < 0) {
+ log_error("Failed to stop session scope: %s", bus_error_message(&error, r));
+ return r;
+ }
- free(s->scope_job);
- s->scope_job = job;
+ free(s->scope_job);
+ s->scope_job = job;
+ } else {
+ r = manager_abandon_scope(s->manager, s->scope, &error);
+ if (r < 0) {
+ log_error("Failed to abandon session scope: %s", bus_error_message(&error, r));
+ return r;
+ }
+ }
return 0;
}
if (!s->user)
return -ESTALE;
+ s->timer_event_source = sd_event_source_unref(s->timer_event_source);
+
+ /* We are going down, don't care about FIFOs anymore */
+ session_remove_fifo(s);
+
/* Kill cgroup */
r = session_stop_scope(s);
+ s->stopping = true;
+
session_save(s);
user_save(s->user);
"MESSAGE=Removed session %s.", s->id,
NULL);
+ s->timer_event_source = sd_event_source_unref(s->timer_event_source);
+
/* Kill session devices */
while ((sd = hashmap_first(s->devices)))
session_device_free(sd);
if (s->seat->active == s)
seat_set_active(s->seat, NULL);
- seat_send_changed(s->seat, "Sessions", NULL);
seat_save(s->seat);
+ seat_send_changed(s->seat, "Sessions", NULL);
}
- user_send_changed(s->user, "Sessions", NULL);
user_save(s->user);
+ user_send_changed(s->user, "Sessions", NULL);
return r;
}
+static int release_timeout_callback(sd_event_source *es, uint64_t usec, void *userdata) {
+ Session *s = userdata;
+
+ assert(es);
+ assert(s);
+
+ session_stop(s);
+ return 0;
+}
+
+void session_release(Session *s) {
+ assert(s);
+
+ if (!s->started || s->stopping)
+ return;
+
+ if (!s->timer_event_source)
+ sd_event_add_monotonic(s->manager->event, now(CLOCK_MONOTONIC) + RELEASE_USEC, 0, release_timeout_callback, s, &s->timer_event_source);
+}
+
bool session_is_active(Session *s) {
assert(s);
return r;
}
-void session_remove_fifo(Session *s) {
+static void session_remove_fifo(Session *s) {
assert(s);
if (s->fifo_event_source)
}
bool session_check_gc(Session *s, bool drop_not_started) {
- int r;
-
assert(s);
if (drop_not_started && !s->started)
return false;
if (s->fifo_fd >= 0) {
- r = pipe_eof(s->fifo_fd);
- if (r < 0)
- return true;
-
- if (r == 0)
+ if (pipe_eof(s->fifo_fd) <= 0)
return true;
}
SessionState session_get_state(Session *s) {
assert(s);
+ if (s->stopping || s->timer_event_source)
+ return SESSION_CLOSING;
+
if (s->scope_job)
return SESSION_OPENING;
- if (s->fifo_fd < 0)
- return SESSION_CLOSING;
-
if (session_is_active(s))
return SESSION_ACTIVE;
}
static int session_open_vt(Session *s) {
- char path[128];
+ char path[sizeof("/dev/tty") + DECIMAL_STR_MAX(s->vtnr)];
if (!s->vtnr)
return -1;
if (vt < 0)
return;
- sd_event_source_unref(s->vt_source);
- s->vt_source = NULL;
+ s->vt_source = sd_event_source_unref(s->vt_source);
ioctl(vt, KDSETMODE, KD_TEXT);