mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 05:46:03 +00:00
revert(input): restore pre-overhaul controls (#2958)
This commit is contained in:
1 parent
33761c30ab
commit
9925873764
170 files changed
+3980
-4927
No files matched your search
@@ -572,15 +572,6 @@ const char *udev_device_get_devtype(void *device) {
|
||||
return real_get(device);
|
||||
}
|
||||
|
||||
const char *udev_device_get_driver(void *device) {
|
||||
static const char *(*real_get)(void *);
|
||||
|
||||
if (device_pad(device) != NULL) return NULL;
|
||||
if (real_get == NULL) real_get = next_symbol("udev_device_get_driver");
|
||||
if (real_get == NULL) return NULL;
|
||||
return real_get(device);
|
||||
}
|
||||
|
||||
const char *udev_device_get_action(void *device) {
|
||||
static const char *(*real_get)(void *);
|
||||
struct pad *pad = device_pad(device);
|
||||
@@ -1019,7 +1010,6 @@ static const struct udev_symbol udev_symbols[] = {
|
||||
{"udev_device_get_sysnum", (void *)udev_device_get_sysnum},
|
||||
{"udev_device_get_subsystem", (void *)udev_device_get_subsystem},
|
||||
{"udev_device_get_devtype", (void *)udev_device_get_devtype},
|
||||
{"udev_device_get_driver", (void *)udev_device_get_driver},
|
||||
{"udev_device_get_action", (void *)udev_device_get_action},
|
||||
{"udev_device_get_devnum", (void *)udev_device_get_devnum},
|
||||
{"udev_device_get_sysattr_value", (void *)udev_device_get_sysattr_value},
|
||||
@@ -1083,7 +1073,7 @@ static int steam_runtime_path(const char *path) {
|
||||
const char *name = strrchr(path, '/');
|
||||
size_t i;
|
||||
|
||||
if (name == NULL || (strcmp(name + 1, "steam") != 0 && strcmp(name + 1, "streaming_client") != 0)) return 0;
|
||||
if (name == NULL || strcmp(name + 1, "steam") != 0) return 0;
|
||||
for (i = 0; i < sizeof(runtimes) / sizeof(runtimes[0]); i++) {
|
||||
size_t length = strlen(runtimes[i]);
|
||||
|
||||
@@ -1094,8 +1084,8 @@ static int steam_runtime_path(const char *path) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* The Steam client and its Remote Play client (streaming_client) read the pads straight from /dev/input
|
||||
* and hidraw themselves, so they keep the real libudev. */
|
||||
/* The Steam client reads the pads straight from /dev/input and hands whatever the udev enumeration
|
||||
* gives it back to the real libudev, which cannot read these synthetic devices. */
|
||||
static int steam_client_process(void) {
|
||||
static _Atomic int cached = -1;
|
||||
char path[PATH_MAX];
|
||||
|
||||
@@ -111,9 +111,6 @@ typedef int (*accept4_fn)(int, struct sockaddr *, socklen_t *, int);
|
||||
static unsigned guarded_port(void);
|
||||
|
||||
__attribute__((visibility("hidden"))) int dd_udevmon_stand_in(int fd);
|
||||
__attribute__((visibility("hidden"))) int dd_udevmon_bind(int fd, const struct sockaddr *addr, socklen_t len,
|
||||
int (*real_bind)(int, const struct sockaddr *, socklen_t),
|
||||
int *rc);
|
||||
__attribute__((visibility("hidden"))) int dd_nl_bind(int fd, const struct sockaddr *addr,
|
||||
socklen_t len, int *rc);
|
||||
__attribute__((visibility("hidden"))) int dd_nl_connect(int fd, const struct sockaddr *addr,
|
||||
@@ -124,7 +121,6 @@ int bind(int fd, const struct sockaddr *addr, socklen_t len) {
|
||||
int nl_rc;
|
||||
if (!real) real = (bind_fn)dlsym(RTLD_NEXT, "bind");
|
||||
if (dd_nl_bind(fd, addr, len, &nl_rc)) return nl_rc;
|
||||
if (dd_udevmon_bind(fd, addr, len, real, &nl_rc)) return nl_rc;
|
||||
if (dd_udevmon_stand_in(fd)) {
|
||||
if (addr == NULL || len < sizeof(struct sockaddr_nl) || addr->sa_family != AF_NETLINK) {
|
||||
errno = EINVAL;
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
#include <sys/stat.h>
|
||||
#include <endian.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct stand_in {
|
||||
@@ -147,61 +146,6 @@ __attribute__((visibility("hidden"))) int dd_udevmon_stand_in(int fd) {
|
||||
return is_stand_in(fd);
|
||||
}
|
||||
|
||||
static int subscribed(int fd) {
|
||||
#ifdef UDEVMON_TEST
|
||||
if (getenv("DD_UDEVMON_REFUSE") != NULL) return 0;
|
||||
#endif
|
||||
struct sockaddr_nl nl;
|
||||
socklen_t len = sizeof(nl);
|
||||
memset(&nl, 0, sizeof(nl));
|
||||
return getsockname(fd, (struct sockaddr *)&nl, &len) == 0 && nl.nl_groups != 0;
|
||||
}
|
||||
|
||||
/* Android lets an app open a uevent socket but not join its multicast groups, and libudev joins none
|
||||
when no udev daemon runs, so a monitor bound there hears nothing. Such a socket is replaced, under
|
||||
the same descriptor, by a stand-in. */
|
||||
__attribute__((visibility("hidden"))) int dd_udevmon_bind(int fd, const struct sockaddr *addr, socklen_t len,
|
||||
int (*real_bind)(int, const struct sockaddr *, socklen_t),
|
||||
int *rc) {
|
||||
int protocol = -1;
|
||||
socklen_t protocol_len = sizeof(protocol);
|
||||
if (addr == NULL || len < sizeof(struct sockaddr_nl) || addr->sa_family != AF_NETLINK) return 0;
|
||||
if (is_stand_in(fd) || is_wine_process()) return 0;
|
||||
if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &protocol_len) != 0 || protocol != NETLINK_KOBJECT_UEVENT)
|
||||
return 0;
|
||||
*rc = real_bind(fd, addr, len);
|
||||
int refused = errno;
|
||||
if (*rc == 0 && subscribed(fd)) return 1;
|
||||
int status = fcntl(fd, F_GETFL);
|
||||
int descriptor = fcntl(fd, F_GETFD);
|
||||
int sv[2];
|
||||
if (status < 0 || descriptor < 0 || socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, sv) != 0) {
|
||||
errno = refused;
|
||||
return 1;
|
||||
}
|
||||
pthread_mutex_lock(&lock);
|
||||
size_t slot = sizeof(stand_ins) / sizeof(stand_ins[0]);
|
||||
for (size_t i = 0; i < sizeof(stand_ins) / sizeof(stand_ins[0]); i++) {
|
||||
if (stand_ins[i].fd == -1) { slot = i; break; }
|
||||
}
|
||||
if (slot == sizeof(stand_ins) / sizeof(stand_ins[0]) ||
|
||||
dup3(sv[0], fd, (descriptor & FD_CLOEXEC) ? O_CLOEXEC : 0) < 0) {
|
||||
pthread_mutex_unlock(&lock);
|
||||
close(sv[0]);
|
||||
close(sv[1]);
|
||||
errno = refused;
|
||||
return 1;
|
||||
}
|
||||
close(sv[0]);
|
||||
if (status & O_NONBLOCK) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
|
||||
stand_ins[slot].fd = fd;
|
||||
stand_ins[slot].peer = sv[1];
|
||||
stand_ins[slot].inode = socket_inode(fd);
|
||||
pthread_mutex_unlock(&lock);
|
||||
*rc = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int getsockname(int fd, struct sockaddr *addr, socklen_t *len) {
|
||||
static int (*real_getsockname)(int, struct sockaddr *, socklen_t *);
|
||||
struct sockaddr_nl nl;
|
||||
|
||||
@@ -19,8 +19,7 @@ class CdpGuardTest(unittest.TestCase):
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", str(SOURCE), str(SOURCE.parent / "binfmt.c"), str(SOURCE.parent / "webhelper.c"),
|
||||
str(Path(__file__).with_name("netlink_stubs.c")), "-x", "c", "-", "-ldl",
|
||||
"-o", str(cls.library)],
|
||||
input=b"int dd_udevmon_stand_in(int fd) { return 0; }\n"
|
||||
b"int dd_udevmon_bind(int fd, const void *addr, unsigned len, void *real, int *rc) { return 0; }", check=True, capture_output=True)
|
||||
input=b"int dd_udevmon_stand_in(int fd) { return 0; }", check=True, capture_output=True)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
|
||||
@@ -78,7 +78,6 @@ char *realpath(const char *path, char *resolved) {
|
||||
"""
|
||||
|
||||
UDEV = r"""
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -128,16 +127,6 @@ void *udev_device_get_parent(void *device) { (void)device; return NULL; }
|
||||
void *udev_device_get_udev(void *device) { (void)device; return NULL; }
|
||||
const char *udev_device_get_syspath(void *device) { (void)device; return "real"; }
|
||||
const char *udev_device_get_sysnum(void *device) { (void)device; return "real"; }
|
||||
|
||||
/* libudev reads the driver through the sd_device pointer it keeps after the udev context. */
|
||||
struct device { void *udev; const char *const *driver; };
|
||||
static const char *const driver_name = "real-driver";
|
||||
static struct device real_device = { NULL, &driver_name };
|
||||
void *udev_device_new_from_devnum(void *udev, char type, dev_t devnum) {
|
||||
(void)udev; (void)type; (void)devnum;
|
||||
return &real_device;
|
||||
}
|
||||
const char *udev_device_get_driver(void *device) { return ((struct device *)device)->driver[0]; }
|
||||
"""
|
||||
|
||||
PROGRAM = r"""
|
||||
@@ -146,7 +135,6 @@ PROGRAM = r"""
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define PAD "/sys/devices/virtual/input/droiddeck0"
|
||||
@@ -358,19 +346,6 @@ static void run_probe(void) {
|
||||
printf("pad %d\n", new_device(&udev, PAD) != NULL);
|
||||
}
|
||||
|
||||
static void run_driver(void) {
|
||||
int udev;
|
||||
void *(*from_devnum)(void *, char, dev_t) = sym("udev_device_new_from_devnum");
|
||||
const char *(*driver)(void *) = sym("udev_device_get_driver");
|
||||
void *pad = from_devnum(&udev, 'c', makedev(13, 64));
|
||||
const char *name = driver(pad);
|
||||
|
||||
printf("pad %s driver %s\n", syspath(pad), name != NULL ? name : "none");
|
||||
unref(pad);
|
||||
name = driver(from_devnum(&udev, 'c', makedev(13, 0)));
|
||||
printf("other driver %s\n", name != NULL ? name : "none");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
char link[4096];
|
||||
|
||||
@@ -412,7 +387,6 @@ int main(int argc, char **argv) {
|
||||
name = sym("udev_list_entry_get_name");
|
||||
value = sym("udev_list_entry_get_value");
|
||||
if (strcmp(argv[1], "probe") == 0) run_probe();
|
||||
else if (strcmp(argv[1], "driver") == 0) run_driver();
|
||||
else if (strcmp(argv[1], "stable") == 0) run_stable();
|
||||
else if (strcmp(argv[1], "enumerate") == 0) run_enumerate();
|
||||
else run_udev();
|
||||
@@ -541,15 +515,9 @@ class InputUdevPreloadTest(unittest.TestCase):
|
||||
"nothing added scan -1",
|
||||
])
|
||||
|
||||
def test_a_pad_has_no_driver_and_other_devices_ask_libudev(self):
|
||||
pad = "/sys/devices/droiddeck0/droiddeck-input0/event0"
|
||||
self.assertEqual(self.run_program("driver"), [f"pad {pad} driver none", "other driver real-driver"])
|
||||
|
||||
def test_only_the_steam_client_and_its_remote_play_client_get_the_real_libudev(self):
|
||||
for path, pad in (("steamrt64/steam", 0), ("steamrtarm64/steam", 0), ("steamrt64/streaming_client", 0),
|
||||
("steamrtarm64/streaming_client", 0), ("steamrt64/steam-probe", 1),
|
||||
("steamrt64/streaming_client2", 1), ("xsteamrt64/steam", 1), ("steamrt64/xsteam", 1),
|
||||
("steamrt/steam", 1), ("steamrt/streaming_client", 1)):
|
||||
def test_only_the_steam_client_runtime_path_gets_the_real_libudev(self):
|
||||
for path, pad in (("steamrt64/steam", 0), ("steamrtarm64/steam", 0), ("steamrt64/steam-probe", 1),
|
||||
("xsteamrt64/steam", 1), ("steamrt64/xsteam", 1), ("steamrt/steam", 1)):
|
||||
with self.subTest(path=path):
|
||||
client = self.dir / "clients" / path
|
||||
client.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
|
||||
|
||||
PROGRAM = r"""
|
||||
#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <poll.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
unsigned groups = argc > 1 ? (unsigned)atoi(argv[1]) : 2;
|
||||
int fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
|
||||
if (fd < 0) { printf("socket failed\n"); return 2; }
|
||||
struct sockaddr_nl nl = {.nl_family = AF_NETLINK, .nl_groups = groups};
|
||||
printf("bind %d\n", bind(fd, (struct sockaddr *)&nl, sizeof(nl)));
|
||||
printf("cloexec %d nonblock %d\n", (fcntl(fd, F_GETFD) & FD_CLOEXEC) != 0, (fcntl(fd, F_GETFL) & O_NONBLOCK) != 0);
|
||||
int (*inject)(const char *, const char *, const char *, const char *, unsigned, unsigned) =
|
||||
(int (*)(const char *, const char *, const char *, const char *, unsigned, unsigned))dlsym(RTLD_DEFAULT, "dd_udevmon_inject");
|
||||
int sent = inject("add", "/devices/droiddeck/usb4/4-1:1.3/0003:054C:09CC.0005/hidraw/hidraw20", "hidraw",
|
||||
"/dev/hidraw20", 245, 20);
|
||||
printf("sent %d\n", sent);
|
||||
struct pollfd p = {.fd = fd, .events = POLLIN};
|
||||
if (sent > 0 && poll(&p, 1, 1000) == 1) {
|
||||
char buf[2048];
|
||||
struct sockaddr_nl from;
|
||||
struct iovec iov = {.iov_base = buf, .iov_len = sizeof(buf)};
|
||||
struct msghdr msg = {.msg_name = &from, .msg_namelen = sizeof(from), .msg_iov = &iov, .msg_iovlen = 1};
|
||||
ssize_t n = recvmsg(fd, &msg, 0);
|
||||
int found = 0;
|
||||
for (ssize_t i = 0; n > 0 && i < n; i += (ssize_t)strlen(buf + i) + 1)
|
||||
if (!strcmp(buf + i, "ACTION=add") || !strcmp(buf + i, "DEVNAME=/dev/hidraw20")) found++;
|
||||
printf("received %s fields %d\n", n > 0 && !memcmp(buf, "libudev", 8) ? "libudev" : "nothing", found);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
@unittest.skipUnless(shutil.which("gcc") and os.uname().machine == "x86_64", "needs gcc on an x86-64 host")
|
||||
class UdevMonitorPreloadTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.tmp = Path(tempfile.mkdtemp())
|
||||
cls.library = cls.tmp / "libudevmon.so"
|
||||
subprocess.run(["gcc", "-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror", "-DUDEVMON_TEST", "-DBINFMT_TEST",
|
||||
'-DBINFMT_HANDLER="/bin/false"', "-o", str(cls.library), "-pthread"]
|
||||
+ [str(PRELOAD / name) for name in ("binfmt.c", "net.c", "udevmon.c", "pathcache.c", "perms.c", "webhelper.c")]
|
||||
+ [str(Path(__file__).with_name("netlink_stubs.c")), "-ldl"], check=True)
|
||||
source = cls.tmp / "program.c"
|
||||
source.write_text(PROGRAM.replace("#include <unistd.h>", "#include <unistd.h>\n#include <stdlib.h>"))
|
||||
cls.program = cls.tmp / "program"
|
||||
subprocess.run(["gcc", "-O2", "-o", str(cls.program), str(source), "-ldl"], check=True)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
shutil.rmtree(cls.tmp, True)
|
||||
|
||||
def run_program(self, *args, refuse=False):
|
||||
env = dict(os.environ, LD_PRELOAD=str(self.library))
|
||||
env.pop("DD_UDEVMON_REFUSE", None)
|
||||
if refuse:
|
||||
env["DD_UDEVMON_REFUSE"] = "1"
|
||||
return subprocess.run([str(self.program), *args], env=env, capture_output=True, text=True, check=True).stdout.splitlines()
|
||||
|
||||
def test_a_monitor_the_system_will_not_subscribe_hears_the_sessions_events(self):
|
||||
out = self.run_program(refuse=True)
|
||||
self.assertEqual(["bind 0", "cloexec 1 nonblock 1", "sent 1", "received libudev fields 2"], out)
|
||||
|
||||
def test_a_monitor_the_system_subscribes_stays_on_the_real_socket(self):
|
||||
self.assertEqual(["bind 0", "cloexec 1 nonblock 1", "sent 0"], self.run_program())
|
||||
|
||||
def test_a_monitor_bound_without_groups_hears_the_sessions_events(self):
|
||||
out = self.run_program("0")
|
||||
self.assertEqual(["bind 0", "cloexec 1 nonblock 1", "sent 1", "received libudev fields 2"], out)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user