aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Rouven Czerwinski <rouven@czerwinskis.de>2022-05-13 20:30:19 +0200
committerLibravatar Kenny Levinsen <kl@kl.wtf>2022-05-18 11:20:17 +0200
commita3a82efbf6b5b3af840c70038b1b599ba29003ac (patch)
treefe28dbc19271b9dce54f7433a86bab0073fe8ca9
parentserver: request xdg-shell v2 (diff)
downloadsway-a3a82efbf6b5b3af840c70038b1b599ba29003ac.tar.gz
sway-a3a82efbf6b5b3af840c70038b1b599ba29003ac.tar.zst
sway-a3a82efbf6b5b3af840c70038b1b599ba29003ac.zip
realtime: request SCHED_RR using CAP_SYS_NICE
Try to gain SCHED_RR (round-robin) realtime scheduling privileges before starting the server. This requires CAP_SYS_NICE on Linux systems. We additionally register a pthread_atfork callback which resets the scheduling class back to SCHED_OTHER (the Linux system default). Due to CAP_SYS_NICE, setting RLIMIT_RTPRIO has no effect on the process as documented within man 7 sched (from Linux): Privileged (CAP_SYS_NICE) threads ignore the RLIMIT_RTPRIO limit; as with older kernels, they can make arbitrary changes to scheduling policy and priority. See getrlimit(2) for further information on RLIMIT_RTPRIO Note that this requires the sway distribution packagers to set the CAP_SYS_NICE capability on the sway binary. Supersedes #6992
-rw-r--r--include/sway/server.h2
-rw-r--r--sway/main.c2
-rw-r--r--sway/meson.build1
-rw-r--r--sway/realtime.c40
4 files changed, 45 insertions, 0 deletions
diff --git a/include/sway/server.h b/include/sway/server.h
index d8ccd64f..3d59ca56 100644
--- a/include/sway/server.h
+++ b/include/sway/server.h
@@ -173,4 +173,6 @@ void handle_pointer_constraint(struct wl_listener *listener, void *data);
173void xdg_activation_v1_handle_request_activate(struct wl_listener *listener, 173void xdg_activation_v1_handle_request_activate(struct wl_listener *listener,
174 void *data); 174 void *data);
175 175
176void set_rr_scheduling(void);
177
176#endif 178#endif
diff --git a/sway/main.c b/sway/main.c
index 78974e22..a0033c45 100644
--- a/sway/main.c
+++ b/sway/main.c
@@ -413,6 +413,8 @@ int main(int argc, char **argv) {
413 goto shutdown; 413 goto shutdown;
414 } 414 }
415 415
416 set_rr_scheduling();
417
416 if (!server_start(&server)) { 418 if (!server_start(&server)) {
417 sway_terminate(EXIT_FAILURE); 419 sway_terminate(EXIT_FAILURE);
418 goto shutdown; 420 goto shutdown;
diff --git a/sway/meson.build b/sway/meson.build
index 0ad783e3..6762ef2d 100644
--- a/sway/meson.build
+++ b/sway/meson.build
@@ -7,6 +7,7 @@ sway_sources = files(
7 'ipc-server.c', 7 'ipc-server.c',
8 'lock.c', 8 'lock.c',
9 'main.c', 9 'main.c',
10 'realtime.c',
10 'server.c', 11 'server.c',
11 'swaynag.c', 12 'swaynag.c',
12 'xdg_activation_v1.c', 13 'xdg_activation_v1.c',
diff --git a/sway/realtime.c b/sway/realtime.c
new file mode 100644
index 00000000..11154af0
--- /dev/null
+++ b/sway/realtime.c
@@ -0,0 +1,40 @@
1#include <sys/resource.h>
2#include <sched.h>
3#include <unistd.h>
4#include <pthread.h>
5#include "sway/server.h"
6#include "log.h"
7
8static void child_fork_callback(void) {
9 struct sched_param param;
10
11 param.sched_priority = 0;
12
13 int ret = pthread_setschedparam(pthread_self(), SCHED_OTHER, &param);
14 if (ret != 0) {
15 sway_log(SWAY_ERROR, "Failed to reset scheduler policy on fork");
16 }
17}
18
19void set_rr_scheduling(void) {
20 int prio = sched_get_priority_min(SCHED_RR);
21 int old_policy;
22 int ret;
23 struct sched_param param;
24
25 ret = pthread_getschedparam(pthread_self(), &old_policy, &param);
26 if (ret != 0) {
27 sway_log(SWAY_DEBUG, "Failed to get old scheduling priority");
28 return;
29 }
30
31 param.sched_priority = prio;
32
33 ret = pthread_setschedparam(pthread_self(), SCHED_RR, &param);
34 if (ret != 0) {
35 sway_log(SWAY_INFO, "Failed to set scheduling priority to %d", prio);
36 return;
37 }
38
39 pthread_atfork(NULL, NULL, child_fork_callback);
40}