Skip to content
Permalink
Browse files

systemd: add api for detecting if this is the last session for a user

  • Loading branch information...
vkareh committed Jan 29, 2019
1 parent 409d9fc commit 37d538eba3a6b36a6d739de4b7567a97203ada3a
Showing with 67 additions and 0 deletions.
  1. +65 −0 mate-session/gsm-systemd.c
  2. +2 −0 mate-session/gsm-systemd.h
@@ -421,6 +421,71 @@ emit_stop_complete (GsmSystemd *manager,
}
}

gboolean
gsm_systemd_is_last_session_for_user (GsmSystemd *manager)
{
char **sessions = NULL;
char *session = NULL;
gboolean is_last_session;
int ret, i;

ret = sd_pid_get_session (getpid (), &session);

if (ret != 0) {
return FALSE;
}

ret = sd_uid_get_sessions (getuid (), FALSE, &sessions);

if (ret <= 0) {
free (session);
return FALSE;
}

is_last_session = TRUE;
for (i = 0; sessions[i]; i++) {
char *state = NULL;
char *type = NULL;

if (g_strcmp0 (sessions[i], session) == 0)
continue;

ret = sd_session_get_state (sessions[i], &state);

if (ret != 0)
continue;

if (g_strcmp0 (state, "closing") == 0) {
free (state);
continue;
}
free (state);

ret = sd_session_get_type (sessions[i], &type);

if (ret != 0)
continue;

if (g_strcmp0 (type, "x11") != 0 &&
g_strcmp0 (type, "wayland") != 0) {
free (type);
continue;
}
free (type);

is_last_session = FALSE;

break;
}

for (i = 0; sessions[i]; i++)
free (sessions[i]);
free (sessions);
free (session);

return is_last_session;
}

void
gsm_systemd_attempt_restart (GsmSystemd *manager)
{
@@ -76,6 +76,8 @@ gboolean gsm_systemd_can_hibernate (GsmSystemd *manager);

gboolean gsm_systemd_can_suspend (GsmSystemd *manager);

gboolean gsm_systemd_is_last_session_for_user (GsmSystemd *manager);

void gsm_systemd_attempt_stop (GsmSystemd *manager);

void gsm_systemd_attempt_restart (GsmSystemd *manager);

0 comments on commit 37d538e

Please sign in to comment.
You can’t perform that action at this time.