core: linux: Don't cache the RealtimeKit D-Bus connection If the D-Bus subsystem is shutdown and restarted mid-execution, the cached connection will be invalid. Fetch it each time that it is used to ensure that the connection is always from the current context.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
diff --git a/src/core/linux/SDL_threadprio.c b/src/core/linux/SDL_threadprio.c
index 73abbc8..865b89c 100644
--- a/src/core/linux/SDL_threadprio.c
+++ b/src/core/linux/SDL_threadprio.c
@@ -57,7 +57,7 @@
#define XDG_PORTAL_DBUS_PATH "/org/freedesktop/portal/desktop"
#define XDG_PORTAL_DBUS_INTERFACE "org.freedesktop.portal.Realtime"
-static DBusConnection *rtkit_dbus_conn;
+static SDL_bool rtkit_use_session_conn;
static const char *rtkit_dbus_node;
static const char *rtkit_dbus_path;
static const char *rtkit_dbus_interface;
@@ -87,37 +87,52 @@ set_rtkit_interface()
/* xdg-desktop-portal works in all instances, so check for it first. */
if (dbus && realtime_portal_supported(dbus->session_conn)) {
- rtkit_dbus_conn = dbus->session_conn;
+ rtkit_use_session_conn = SDL_TRUE;
rtkit_dbus_node = XDG_PORTAL_DBUS_NODE;
rtkit_dbus_path = XDG_PORTAL_DBUS_PATH;
rtkit_dbus_interface = XDG_PORTAL_DBUS_INTERFACE;
} else { /* Fall back to the standard rtkit interface in all other cases. */
- rtkit_dbus_conn = dbus ? dbus->system_conn : NULL;
+ rtkit_use_session_conn = SDL_FALSE;
rtkit_dbus_node = RTKIT_DBUS_NODE;
rtkit_dbus_path = RTKIT_DBUS_PATH;
rtkit_dbus_interface = RTKIT_DBUS_INTERFACE;
}
}
+static DBusConnection*
+get_rtkit_dbus_connection()
+{
+ SDL_DBusContext *dbus = SDL_DBus_GetContext();
+
+ if (dbus) {
+ return rtkit_use_session_conn ? dbus->session_conn : dbus->system_conn;
+ }
+
+ return NULL;
+}
+
static void
rtkit_initialize()
{
+ DBusConnection *dbus_conn;
+
set_rtkit_interface();
+ dbus_conn = get_rtkit_dbus_connection();
/* Try getting minimum nice level: this is often greater than PRIO_MIN (-20). */
- if (!rtkit_dbus_conn || !SDL_DBus_QueryPropertyOnConnection(rtkit_dbus_conn, rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "MinNiceLevel",
+ if (!dbus_conn || !SDL_DBus_QueryPropertyOnConnection(dbus_conn, rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "MinNiceLevel",
DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
rtkit_min_nice_level = -20;
}
/* Try getting maximum realtime priority: this can be less than the POSIX default (99). */
- if (!rtkit_dbus_conn || !SDL_DBus_QueryPropertyOnConnection(rtkit_dbus_conn, rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "MaxRealtimePriority",
+ if (!dbus_conn || !SDL_DBus_QueryPropertyOnConnection(dbus_conn, rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "MaxRealtimePriority",
DBUS_TYPE_INT32, &rtkit_max_realtime_priority)) {
rtkit_max_realtime_priority = 99;
}
/* Try getting maximum rttime allowed by rtkit: exceeding this value will result in SIGKILL */
- if (!rtkit_dbus_conn || !SDL_DBus_QueryPropertyOnConnection(rtkit_dbus_conn, rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "RTTimeUSecMax",
+ if (!dbus_conn || !SDL_DBus_QueryPropertyOnConnection(dbus_conn, rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "RTTimeUSecMax",
DBUS_TYPE_INT64, &rtkit_max_rttime_usec)) {
rtkit_max_rttime_usec = 200000;
}
@@ -190,16 +205,18 @@ rtkit_initialize_realtime_thread()
static SDL_bool
rtkit_setpriority_nice(pid_t thread, int nice_level)
{
+ DBusConnection *dbus_conn;
Uint64 pid = (Uint64)getpid();
Uint64 tid = (Uint64)thread;
Sint32 nice = (Sint32)nice_level;
pthread_once(&rtkit_initialize_once, rtkit_initialize);
+ dbus_conn = get_rtkit_dbus_connection();
if (nice < rtkit_min_nice_level)
nice = rtkit_min_nice_level;
- if (!rtkit_dbus_conn || !SDL_DBus_CallMethodOnConnection(rtkit_dbus_conn,
+ if (!dbus_conn || !SDL_DBus_CallMethodOnConnection(dbus_conn,
rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "MakeThreadHighPriorityWithPID",
DBUS_TYPE_UINT64, &pid, DBUS_TYPE_UINT64, &tid, DBUS_TYPE_INT32, &nice, DBUS_TYPE_INVALID,
DBUS_TYPE_INVALID)) {
@@ -211,11 +228,13 @@ rtkit_setpriority_nice(pid_t thread, int nice_level)
static SDL_bool
rtkit_setpriority_realtime(pid_t thread, int rt_priority)
{
+ DBusConnection *dbus_conn;
Uint64 pid = (Uint64)getpid();
Uint64 tid = (Uint64)thread;
Uint32 priority = (Uint32)rt_priority;
pthread_once(&rtkit_initialize_once, rtkit_initialize);
+ dbus_conn = get_rtkit_dbus_connection();
if (priority > rtkit_max_realtime_priority)
priority = rtkit_max_realtime_priority;
@@ -228,7 +247,7 @@ rtkit_setpriority_realtime(pid_t thread, int rt_priority)
// go through to determine whether it really needs to fail or not.
rtkit_initialize_realtime_thread();
- if (!rtkit_dbus_conn || !SDL_DBus_CallMethodOnConnection(rtkit_dbus_conn,
+ if (!dbus_conn || !SDL_DBus_CallMethodOnConnection(dbus_conn,
rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "MakeThreadRealtimeWithPID",
DBUS_TYPE_UINT64, &pid, DBUS_TYPE_UINT64, &tid, DBUS_TYPE_UINT32, &priority, DBUS_TYPE_INVALID,
DBUS_TYPE_INVALID)) {