mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
fix(nativeguest): do not apply a realtime limit of zero (#3704)
Flatpak emulators that use SDL (Flycast, Rosalie's Mupen GUI, xemu) were killed with status 137 a second or two after a game started. Without a RealtimeKit, xdg-desktop-portal reports RTTimeUSecMax 0, and SDL sets that as RLIMIT_RTTIME before asking for a realtime thread. The kernel checks the limit on a thread that is realtime or has a CPU-time timer armed, and a limit of 0 is reached at once: SIGKILL at the hard limit. Fixes #3672
This commit is contained in:
1 parent
fe6f8638c6
commit
86b8ae3c99
2 files changed
+90
-1
No files matched your search
@@ -235,8 +235,16 @@ static int sandbox_only_limit(int resource, const struct rlimit *rlim) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The kernel checks RLIMIT_RTTIME on a thread that is realtime or has a CPU-time timer armed, and a
|
||||
// limit of 0 is reached at once: SIGXCPU at the soft limit, SIGKILL at the hard one. SDL sets the
|
||||
// RTTimeUSecMax it reads from org.freedesktop.portal.Realtime before it asks for a realtime thread,
|
||||
// and xdg-desktop-portal reports 0 when it reaches no RealtimeKit, as in a session with no system bus.
|
||||
static int fatal_limit(int resource, const struct rlimit *rlim) {
|
||||
return rlim && resource == RLIMIT_RTTIME && rlim->rlim_cur == 0;
|
||||
}
|
||||
|
||||
int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlim) {
|
||||
if (sandbox_only_limit((int)resource, rlim)) return 0;
|
||||
if (sandbox_only_limit((int)resource, rlim) || fatal_limit((int)resource, rlim)) return 0;
|
||||
static int (*orig_setrlimit)(__rlimit_resource_t, const struct rlimit *) = NULL;
|
||||
#if defined(__USE_FILE_OFFSET64) && !defined(__LP64__)
|
||||
// With 64-bit file offsets on a 32-bit system this function is setrlimit64
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from tools.tests.test_usr2_preload import runner
|
||||
|
||||
HOOK_BUILD = Path(__file__).resolve().parents[1] / "nativeguest/build.sh"
|
||||
|
||||
PROBE = r"""
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
static void show(const char *name, int ret) {
|
||||
struct rlimit now;
|
||||
if (getrlimit(RLIMIT_RTTIME, &now) != 0) exit(2);
|
||||
printf("%s\t%d\t%lld\t%lld\n", name, ret, (long long)now.rlim_cur, (long long)now.rlim_max);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct rlimit zero = {0, 0}, soft_zero = {0, 200000}, usable = {100000, 200000};
|
||||
show("start", 0);
|
||||
show("zero", setrlimit(RLIMIT_RTTIME, &zero));
|
||||
show("soft_zero", setrlimit(RLIMIT_RTTIME, &soft_zero));
|
||||
show("usable", setrlimit(RLIMIT_RTTIME, &usable));
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
class NativeHookRttimeLimitTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.cc, cls.prefix = runner()
|
||||
if cls.cc is None:
|
||||
raise unittest.SkipTest("no aarch64 compiler and runner")
|
||||
cls.tmp = tempfile.TemporaryDirectory(dir="/tmp")
|
||||
tmp = Path(cls.tmp.name)
|
||||
cls.hook = tmp / "hook.so"
|
||||
subprocess.run(["bash", str(HOOK_BUILD), str(cls.hook), cls.cc], check=True, capture_output=True)
|
||||
(tmp / "probe.c").write_text(PROBE)
|
||||
cls.probe = tmp / "probe"
|
||||
subprocess.run([cls.cc, "-O1", "-o", str(cls.probe), str(tmp / "probe.c")], check=True)
|
||||
cls.root = tmp / "root"
|
||||
cls.root.mkdir()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.tmp.cleanup()
|
||||
|
||||
def limits(self):
|
||||
hook_env = {"DDNATV_ROOT": str(self.root), "LD_PRELOAD": str(self.hook)}
|
||||
env = {k: v for k, v in os.environ.items() if k not in ("LD_PRELOAD", "DDNATV_ROOT")}
|
||||
if self.prefix:
|
||||
cmd = list(self.prefix) + [arg for key, value in hook_env.items() for arg in ("-E", f"{key}={value}")]
|
||||
else:
|
||||
cmd = []
|
||||
env.update(hook_env)
|
||||
out = subprocess.run(cmd + [str(self.probe)], env=env, capture_output=True, text=True, timeout=120)
|
||||
self.assertEqual(0, out.returncode, out.stdout + out.stderr)
|
||||
return {name: (int(ret), int(cur), int(hard))
|
||||
for name, ret, cur, hard in (line.split("\t") for line in out.stdout.splitlines())}
|
||||
|
||||
def test_a_realtime_limit_of_zero_is_reported_set_and_not_applied(self):
|
||||
limits = self.limits()
|
||||
self.assertEqual(0, limits["zero"][0])
|
||||
self.assertEqual(limits["start"][1:], limits["zero"][1:])
|
||||
|
||||
def test_a_soft_realtime_limit_of_zero_is_not_applied(self):
|
||||
limits = self.limits()
|
||||
self.assertEqual(0, limits["soft_zero"][0])
|
||||
self.assertEqual(limits["start"][1:], limits["soft_zero"][1:])
|
||||
|
||||
def test_a_usable_realtime_limit_is_applied(self):
|
||||
self.assertEqual((0, 100000, 200000), self.limits()["usable"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user