1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"
22
23#ifdef SDL_PLATFORM_LINUX
24
25#ifndef SDL_THREADS_DISABLED
26#include <sys/time.h>
27#include <sys/resource.h>
28#include <pthread.h>
29#include <sched.h>
30#include <unistd.h>
31
32// RLIMIT_RTTIME requires kernel >= 2.6.25 and is in glibc >= 2.14
33#ifndef RLIMIT_RTTIME
34#define RLIMIT_RTTIME 15
35#endif
36// SCHED_RESET_ON_FORK is in kernel >= 2.6.32.
37#ifndef SCHED_RESET_ON_FORK
38#define SCHED_RESET_ON_FORK 0x40000000
39#endif
40
41#include "SDL_dbus.h"
42
43#ifdef SDL_USE_LIBDBUS
44
45// d-bus queries to org.freedesktop.RealtimeKit1.
46#define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
47#define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
48#define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
49
50// d-bus queries to the XDG portal interface to RealtimeKit1
51#define XDG_PORTAL_DBUS_NODE "org.freedesktop.portal.Desktop"
52#define XDG_PORTAL_DBUS_PATH "/org/freedesktop/portal/desktop"
53#define XDG_PORTAL_DBUS_INTERFACE "org.freedesktop.portal.Realtime"
54
55static bool rtkit_use_session_conn;
56static const char *rtkit_dbus_node;
57static const char *rtkit_dbus_path;
58static const char *rtkit_dbus_interface;
59
60static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
61static Sint32 rtkit_min_nice_level = -20;
62static Sint32 rtkit_max_realtime_priority = 99;
63static Sint64 rtkit_max_rttime_usec = 200000;
64
65/*
66 * Checking that the RTTimeUSecMax property exists and is an int64 confirms that:
67 * - The desktop portal exists and supports the realtime interface.
68 * - The realtime interface is new enough to have the required bug fixes applied.
69 */
70static bool realtime_portal_supported(DBusConnection *conn)
71{
72 Sint64 res;
73 return SDL_DBus_QueryPropertyOnConnection(conn, XDG_PORTAL_DBUS_NODE, XDG_PORTAL_DBUS_PATH, XDG_PORTAL_DBUS_INTERFACE,
74 "RTTimeUSecMax", DBUS_TYPE_INT64, &res);
75}
76
77static void set_rtkit_interface(void)
78{
79 SDL_DBusContext *dbus = SDL_DBus_GetContext();
80
81 // xdg-desktop-portal works in all instances, so check for it first.
82 if (dbus && realtime_portal_supported(dbus->session_conn)) {
83 rtkit_use_session_conn = true;
84 rtkit_dbus_node = XDG_PORTAL_DBUS_NODE;
85 rtkit_dbus_path = XDG_PORTAL_DBUS_PATH;
86 rtkit_dbus_interface = XDG_PORTAL_DBUS_INTERFACE;
87 } else { // Fall back to the standard rtkit interface in all other cases.
88 rtkit_use_session_conn = false;
89 rtkit_dbus_node = RTKIT_DBUS_NODE;
90 rtkit_dbus_path = RTKIT_DBUS_PATH;
91 rtkit_dbus_interface = RTKIT_DBUS_INTERFACE;
92 }
93}
94
95static DBusConnection *get_rtkit_dbus_connection(void)
96{
97 SDL_DBusContext *dbus = SDL_DBus_GetContext();
98
99 if (dbus) {
100 return rtkit_use_session_conn ? dbus->session_conn : dbus->system_conn;
101 }
102
103 return NULL;
104}
105
106static void rtkit_initialize(void)
107{
108 DBusConnection *dbus_conn;
109
110 set_rtkit_interface();
111 dbus_conn = get_rtkit_dbus_connection();
112
113 // Try getting minimum nice level: this is often greater than PRIO_MIN (-20).
114 if (!dbus_conn || !SDL_DBus_QueryPropertyOnConnection(dbus_conn, rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "MinNiceLevel",
115 DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
116 rtkit_min_nice_level = -20;
117 }
118
119 // Try getting maximum realtime priority: this can be less than the POSIX default (99).
120 if (!dbus_conn || !SDL_DBus_QueryPropertyOnConnection(dbus_conn, rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "MaxRealtimePriority",
121 DBUS_TYPE_INT32, &rtkit_max_realtime_priority)) {
122 rtkit_max_realtime_priority = 99;
123 }
124
125 // Try getting maximum rttime allowed by rtkit: exceeding this value will result in SIGKILL
126 if (!dbus_conn || !SDL_DBus_QueryPropertyOnConnection(dbus_conn, rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "RTTimeUSecMax",
127 DBUS_TYPE_INT64, &rtkit_max_rttime_usec)) {
128 rtkit_max_rttime_usec = 200000;
129 }
130}
131
132static bool rtkit_initialize_realtime_thread(void)
133{
134 // Following is an excerpt from rtkit README that outlines the requirements
135 // a thread must meet before making rtkit requests:
136 //
137 // * Only clients with RLIMIT_RTTIME set will get RT scheduling
138 //
139 // * RT scheduling will only be handed out to processes with
140 // SCHED_RESET_ON_FORK set to guarantee that the scheduling
141 // settings cannot 'leak' to child processes, thus making sure
142 // that 'RT fork bombs' cannot be used to bypass RLIMIT_RTTIME
143 // and take the system down.
144 //
145 // * Limits are enforced on all user controllable resources, only
146 // a maximum number of users, processes, threads can request RT
147 // scheduling at the same time.
148 //
149 // * Only a limited number of threads may be made RT in a
150 // specific time frame.
151 //
152 // * Client authorization is verified with PolicyKit
153
154 int err;
155 struct rlimit rlimit;
156 int nLimit = RLIMIT_RTTIME;
157 pid_t nPid = 0; // self
158 int nSchedPolicy = sched_getscheduler(nPid) | SCHED_RESET_ON_FORK;
159 struct sched_param schedParam;
160
161 SDL_zero(schedParam);
162
163 // Requirement #1: Set RLIMIT_RTTIME
164 err = getrlimit(nLimit, &rlimit);
165 if (err) {
166 return false;
167 }
168
169 // Current rtkit allows a max of 200ms right now
170 rlimit.rlim_max = rtkit_max_rttime_usec;
171 rlimit.rlim_cur = rlimit.rlim_max / 2;
172 err = setrlimit(nLimit, &rlimit);
173 if (err) {
174 return false;
175 }
176
177 // Requirement #2: Add SCHED_RESET_ON_FORK to the scheduler policy
178 err = sched_getparam(nPid, &schedParam);
179 if (err) {
180 return false;
181 }
182
183 err = sched_setscheduler(nPid, nSchedPolicy, &schedParam);
184 if (err) {
185 return false;
186 }
187
188 return true;
189}
190
191static bool rtkit_setpriority_nice(pid_t thread, int nice_level)
192{
193 DBusConnection *dbus_conn;
194 Uint64 pid = (Uint64)getpid();
195 Uint64 tid = (Uint64)thread;
196 Sint32 nice = (Sint32)nice_level;
197
198 pthread_once(&rtkit_initialize_once, rtkit_initialize);
199 dbus_conn = get_rtkit_dbus_connection();
200
201 if (nice < rtkit_min_nice_level) {
202 nice = rtkit_min_nice_level;
203 }
204
205 if (!dbus_conn || !SDL_DBus_CallMethodOnConnection(dbus_conn,
206 rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "MakeThreadHighPriorityWithPID",
207 DBUS_TYPE_UINT64, &pid, DBUS_TYPE_UINT64, &tid, DBUS_TYPE_INT32, &nice, DBUS_TYPE_INVALID,
208 DBUS_TYPE_INVALID)) {
209 return false;
210 }
211 return true;
212}
213
214static bool rtkit_setpriority_realtime(pid_t thread, int rt_priority)
215{
216 DBusConnection *dbus_conn;
217 Uint64 pid = (Uint64)getpid();
218 Uint64 tid = (Uint64)thread;
219 Uint32 priority = (Uint32)rt_priority;
220
221 pthread_once(&rtkit_initialize_once, rtkit_initialize);
222 dbus_conn = get_rtkit_dbus_connection();
223
224 if (priority > rtkit_max_realtime_priority) {
225 priority = rtkit_max_realtime_priority;
226 }
227
228 // We always perform the thread state changes necessary for rtkit.
229 // This wastes some system calls if the state is already set but
230 // typically code sets a thread priority and leaves it so it's
231 // not expected that this wasted effort will be an issue.
232 // We also do not quit if this fails, we let the rtkit request
233 // go through to determine whether it really needs to fail or not.
234 rtkit_initialize_realtime_thread();
235
236 if (!dbus_conn || !SDL_DBus_CallMethodOnConnection(dbus_conn,
237 rtkit_dbus_node, rtkit_dbus_path, rtkit_dbus_interface, "MakeThreadRealtimeWithPID",
238 DBUS_TYPE_UINT64, &pid, DBUS_TYPE_UINT64, &tid, DBUS_TYPE_UINT32, &priority, DBUS_TYPE_INVALID,
239 DBUS_TYPE_INVALID)) {
240 return false;
241 }
242 return true;
243}
244#else
245
246#define rtkit_max_realtime_priority 99
247
248#endif // dbus
249#endif // threads
250
251// this is a public symbol, so it has to exist even if threads are disabled.
252bool SDL_SetLinuxThreadPriority(Sint64 threadID, int priority)
253{
254#ifdef SDL_THREADS_DISABLED
255 return SDL_Unsupported();
256#else
257 if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) == 0) {
258 return true;
259 }
260
261#ifdef SDL_USE_LIBDBUS
262 /* Note that this fails you most likely:
263 * Have your process's scheduler incorrectly configured.
264 See the requirements at:
265 http://git.0pointer.net/rtkit.git/tree/README#n16
266 * Encountered dbus/polkit security restrictions. Note
267 that the RealtimeKit1 dbus endpoint is inaccessible
268 over ssh connections for most common distro configs.
269 You might want to check your local config for details:
270 /usr/share/polkit-1/actions/org.freedesktop.RealtimeKit1.policy
271
272 README and sample code at: http://git.0pointer.net/rtkit.git
273 */
274 if (rtkit_setpriority_nice((pid_t)threadID, priority)) {
275 return true;
276 }
277#endif
278
279 return SDL_SetError("setpriority() failed");
280#endif
281}
282
283// this is a public symbol, so it has to exist even if threads are disabled.
284bool SDL_SetLinuxThreadPriorityAndPolicy(Sint64 threadID, int sdlPriority, int schedPolicy)
285{
286#ifdef SDL_THREADS_DISABLED
287 return SDL_Unsupported();
288#else
289 int osPriority;
290
291 if (schedPolicy == SCHED_RR || schedPolicy == SCHED_FIFO) {
292 if (sdlPriority == SDL_THREAD_PRIORITY_LOW) {
293 osPriority = 1;
294 } else if (sdlPriority == SDL_THREAD_PRIORITY_HIGH) {
295 osPriority = rtkit_max_realtime_priority * 3 / 4;
296 } else if (sdlPriority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
297 osPriority = rtkit_max_realtime_priority;
298 } else {
299 osPriority = rtkit_max_realtime_priority / 2;
300 }
301 } else {
302 if (sdlPriority == SDL_THREAD_PRIORITY_LOW) {
303 osPriority = 19;
304 } else if (sdlPriority == SDL_THREAD_PRIORITY_HIGH) {
305 osPriority = -10;
306 } else if (sdlPriority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
307 osPriority = -20;
308 } else {
309 osPriority = 0;
310 }
311
312 if (setpriority(PRIO_PROCESS, (id_t)threadID, osPriority) == 0) {
313 return true;
314 }
315 }
316
317#ifdef SDL_USE_LIBDBUS
318 /* Note that this fails you most likely:
319 * Have your process's scheduler incorrectly configured.
320 See the requirements at:
321 http://git.0pointer.net/rtkit.git/tree/README#n16
322 * Encountered dbus/polkit security restrictions. Note
323 that the RealtimeKit1 dbus endpoint is inaccessible
324 over ssh connections for most common distro configs.
325 You might want to check your local config for details:
326 /usr/share/polkit-1/actions/org.freedesktop.RealtimeKit1.policy
327
328 README and sample code at: http://git.0pointer.net/rtkit.git
329 */
330 if (schedPolicy == SCHED_RR || schedPolicy == SCHED_FIFO) {
331 if (rtkit_setpriority_realtime((pid_t)threadID, osPriority)) {
332 return true;
333 }
334 } else {
335 if (rtkit_setpriority_nice((pid_t)threadID, osPriority)) {
336 return true;
337 }
338 }
339#endif
340
341 return SDL_SetError("setpriority() failed");
342#endif
343}
344
345#endif // SDL_PLATFORM_LINUX
346