|
Zbigniew Jędrzejewski-Szmek |
436654 |
From 75aad3b101548151905d528269ffd2a388955193 Mon Sep 17 00:00:00 2001
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
From: Philip Withnall <philip.withnall@collabora.co.uk>
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
Date: Fri, 29 May 2015 10:49:21 +0100
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
Subject: [PATCH] logind: Fix user_elect_display() to be more stable
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
MIME-Version: 1.0
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
Content-Type: text/plain; charset=UTF-8
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
Content-Transfer-Encoding: 8bit
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
The previous implementation of user_elect_display() could easily end up
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
overwriting the user’s valid graphical session with a new TTY session.
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
For example, consider the situation where there is one session:
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
c1, type = SESSION_X11, !stopping, class = SESSION_USER
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
it is initially elected as the user’s display (i.e. u->display = c1).
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
If another session is started, on a different VT, the sessions_by_user
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
list becomes:
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
c1, type = SESSION_X11, !stopping, class = SESSION_USER
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
c2, type = SESSION_TTY, !stopping, class = SESSION_USER
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
In the previous code, graphical = c1 and text = c2, as expected.
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
However, neither graphical nor text fulfil the conditions for setting
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
u->display = graphical (because neither is better than u->display), so
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
the code falls through to check the text variable. The conditions for
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
this match, as u->display->type != SESSION_TTY (it’s actually
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
SESSION_X11). Hence u->display is set to c2, which is incorrect, because
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
session c1 is still valid.
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
Refactor user_elect_display() to use a more explicit filter and
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
pre-order comparison over the sessions. This can be demonstrated to be
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
stable and only ever ‘upgrade’ the session to a more graphical one.
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
https://bugs.freedesktop.org/show_bug.cgi?id=90769
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
(cherry picked from commit 7ffeb45cc63e1326690fd9461b7a4719a3d4f85c)
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
---
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
src/login/logind-user.c | 90 +++++++++++++++++++++++++++++--------------------
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
1 file changed, 54 insertions(+), 36 deletions(-)
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
diff --git a/src/login/logind-user.c b/src/login/logind-user.c
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
index 71bff96728..2f62e34f63 100644
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
--- a/src/login/logind-user.c
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+++ b/src/login/logind-user.c
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
@@ -738,54 +738,72 @@ int user_kill(User *u, int signo) {
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
return manager_kill_unit(u->manager, u->slice, KILL_ALL, signo, NULL);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+static bool
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+elect_display_filter(Session *s) {
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ /* Return true if the session is a candidate for the user’s ‘primary
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ * session’ or ‘display’. */
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ assert(s);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ return (s->class == SESSION_USER && !s->stopping);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+}
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+static int
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+elect_display_compare(Session *s1, Session *s2) {
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ /* Indexed by SessionType. Lower numbers mean more preferred. */
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ const int type_ranks[_SESSION_TYPE_MAX] = {
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ [SESSION_UNSPECIFIED] = 0,
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ [SESSION_TTY] = -2,
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ [SESSION_X11] = -3,
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ [SESSION_WAYLAND] = -3,
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ [SESSION_MIR] = -3,
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ [SESSION_WEB] = -1,
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ };
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ /* Calculate the partial order relationship between s1 and s2,
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ * returning < 0 if s1 is preferred as the user’s ‘primary session’,
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ * 0 if s1 and s2 are equally preferred or incomparable, or > 0 if s2
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ * is preferred.
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ *
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ * s1 or s2 may be NULL. */
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ if ((s1 == NULL) != (s2 == NULL))
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ return (s1 == NULL) - (s2 == NULL);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ if (s1->stopping != s2->stopping)
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ return s1->stopping - s2->stopping;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ if ((s1->class != SESSION_USER) != (s2->class != SESSION_USER))
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ return (s1->class != SESSION_USER) - (s2->class != SESSION_USER);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ if ((s1->type == _SESSION_TYPE_INVALID) != (s2->type == _SESSION_TYPE_INVALID))
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ return (s1->type == _SESSION_TYPE_INVALID) - (s2->type == _SESSION_TYPE_INVALID);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ if (s1->type != s2->type)
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ return type_ranks[s1->type] - type_ranks[s2->type];
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ return 0;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+}
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
void user_elect_display(User *u) {
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- Session *graphical = NULL, *text = NULL, *other = NULL, *s;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ Session *s;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
assert(u);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
/* This elects a primary session for each user, which we call
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
* the "display". We try to keep the assignment stable, but we
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
* "upgrade" to better choices. */
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ log_debug("Electing new display for user %s", u->name);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
LIST_FOREACH(sessions_by_user, s, u->sessions) {
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- if (s->class != SESSION_USER)
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- continue;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- if (s->stopping)
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ if (!elect_display_filter(s)) {
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ log_debug("Ignoring session %s", s->id);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
continue;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ }
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- if (SESSION_TYPE_IS_GRAPHICAL(s->type))
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- graphical = s;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- else if (s->type == SESSION_TTY)
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- text = s;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- else
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- other = s;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- }
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- if (graphical &&
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- (!u->display ||
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- u->display->class != SESSION_USER ||
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- u->display->stopping ||
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- !SESSION_TYPE_IS_GRAPHICAL(u->display->type))) {
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- u->display = graphical;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- return;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- }
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- if (text &&
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- (!u->display ||
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- u->display->class != SESSION_USER ||
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- u->display->stopping ||
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- u->display->type != SESSION_TTY)) {
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- u->display = text;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- return;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ if (elect_display_compare(s, u->display) < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ log_debug("Choosing session %s in preference to %s", s->id, u->display ? u->display->id : "-");
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ u->display = s;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ }
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- if (other &&
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- (!u->display ||
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- u->display->class != SESSION_USER ||
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- u->display->stopping))
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- u->display = other;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
static const char* const user_state_table[_USER_STATE_MAX] = {
|