summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Kareh <[email protected]>2019-01-29 15:19:14 -0500
committerVictor Kareh <[email protected]>2019-02-04 07:52:05 -0500
commit37d538eba3a6b36a6d739de4b7567a97203ada3a (patch)
treea55f46441b3e3394a9378f9d7b02ac3075b31813
parent409d9fcc735f6dfd6ada9d90360e045130379259 (diff)
downloadmate-session-manager-37d538eba3a6b36a6d739de4b7567a97203ada3a.tar.bz2
mate-session-manager-37d538eba3a6b36a6d739de4b7567a97203ada3a.tar.xz
systemd: add api for detecting if this is the last session for a user
https://bugzilla.gnome.org/show_bug.cgi?id=764029 Adapted from https://gitlab.gnome.org/GNOME/gnome-session/commit/f708bbbf
-rw-r--r--mate-session/gsm-systemd.c65
-rw-r--r--mate-session/gsm-systemd.h2
2 files changed, 67 insertions, 0 deletions
diff --git a/mate-session/gsm-systemd.c b/mate-session/gsm-systemd.c
index 5eafaed..da3f23b 100644
--- a/mate-session/gsm-systemd.c
+++ b/mate-session/gsm-systemd.c
@@ -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)
{
diff --git a/mate-session/gsm-systemd.h b/mate-session/gsm-systemd.h
index 5d6b654..c00ec03 100644
--- a/mate-session/gsm-systemd.h
+++ b/mate-session/gsm-systemd.h
@@ -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);