mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-03 12:36:37 -05:00
Since each pad was described to Wine's udev backend with the identity its SDL backend sees, the udev backend left every pad to SDL. Where SDL does not open the pad, such as a session without proot, Windows games lost both the gamepad and the on-screen controls. The udev backend owns the pads again with the Steam Input identity it had before. SDL inside Wine's bus driver now reads the same identity for those pads and skips them as it skips any Steam Input pad, so each pad is still listed once and both triggers keep reading together.
745 lines
29 KiB
Python
745 lines
29 KiB
Python
from pathlib import Path
|
|
import os
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
|
|
|
|
ONCE = r"""
|
|
#define _GNU_SOURCE
|
|
#include <dlfcn.h>
|
|
#include <pthread.h>
|
|
#include <sys/syscall.h>
|
|
#include <unistd.h>
|
|
|
|
static pthread_once_t once = PTHREAD_ONCE_INIT;
|
|
static long (*next_syscall)(long, ...);
|
|
|
|
static void init(void) { next_syscall = (long (*)(long, ...))dlsym(RTLD_NEXT, "syscall"); }
|
|
|
|
ssize_t readlink(const char *path, char *buf, size_t size) {
|
|
pthread_once(&once, init);
|
|
return next_syscall(SYS_readlinkat, -100, path, buf, size);
|
|
}
|
|
|
|
__attribute__((constructor)) static void start(void) {
|
|
char link[4096];
|
|
readlink("/proc/self/exe", link, sizeof(link));
|
|
}
|
|
"""
|
|
|
|
FIRST = r"""
|
|
#define _GNU_SOURCE
|
|
#include <dlfcn.h>
|
|
#include <stddef.h>
|
|
|
|
int marker(void) { return 1; }
|
|
|
|
int next_marker(void) {
|
|
int (*next)(void) = (int (*)(void))dlsym(RTLD_NEXT, "marker");
|
|
return next != NULL ? next() : 0;
|
|
}
|
|
"""
|
|
|
|
SECOND = "int marker(void) { return 2; }\n"
|
|
|
|
CHAIN = r"""
|
|
#define _GNU_SOURCE
|
|
#include <dlfcn.h>
|
|
#include <stddef.h>
|
|
|
|
static int calls;
|
|
|
|
int chained(void) { return calls > 1; }
|
|
|
|
void *dlsym(void *handle, const char *name) {
|
|
static void *(*real)(void *, const char *);
|
|
if (real == NULL) real = (void *(*)(void *, const char *))dlvsym(RTLD_NEXT, "dlsym", "GLIBC_2.2.5");
|
|
if (real == NULL) real = (void *(*)(void *, const char *))dlvsym(RTLD_NEXT, "dlsym", "GLIBC_2.17");
|
|
if (real == NULL) real = (void *(*)(void *, const char *))dlvsym(RTLD_NEXT, "dlsym", "GLIBC_2.34");
|
|
calls++;
|
|
return real(handle, name);
|
|
}
|
|
"""
|
|
|
|
MARK = r"""
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
char *realpath(const char *path, char *resolved) {
|
|
(void)path;
|
|
if (resolved == NULL) return strdup("marked");
|
|
strcpy(resolved, "marked");
|
|
return resolved;
|
|
}
|
|
"""
|
|
|
|
LOCKED = r"""
|
|
#include <sys/syscall.h>
|
|
#include <unistd.h>
|
|
|
|
extern int busy;
|
|
|
|
__attribute__((constructor)) static void start(void) {
|
|
busy = 1;
|
|
syscall(SYS_getpid);
|
|
busy = 0;
|
|
}
|
|
"""
|
|
|
|
ARENA = r"""
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
static _Alignas(64) unsigned char arena[1 << 23];
|
|
static size_t used;
|
|
int busy;
|
|
static int busy_allocations;
|
|
|
|
void *memalign(size_t align, size_t size) {
|
|
uintptr_t start;
|
|
if (busy) busy_allocations++;
|
|
if (align < 16) align = 16;
|
|
start = ((uintptr_t)(arena + used) + 16 + align - 1) & ~(uintptr_t)(align - 1);
|
|
if (start + size > (uintptr_t)(arena + sizeof(arena))) return NULL;
|
|
((size_t *)start)[-1] = size;
|
|
used = start + size - (uintptr_t)arena;
|
|
return (void *)start;
|
|
}
|
|
|
|
void *malloc(size_t size) { return memalign(16, size); }
|
|
void *aligned_alloc(size_t align, size_t size) { return memalign(align, size); }
|
|
int posix_memalign(void **out, size_t align, size_t size) { return (*out = memalign(align, size)) ? 0 : 12; }
|
|
void free(void *p) { (void)p; }
|
|
|
|
void *calloc(size_t n, size_t size) {
|
|
void *p = malloc(n * size);
|
|
if (p) memset(p, 0, n * size);
|
|
return p;
|
|
}
|
|
|
|
void *realloc(void *p, size_t size) {
|
|
void *q = malloc(size);
|
|
if (p && q) {
|
|
size_t old = ((size_t *)p)[-1];
|
|
memcpy(q, p, old < size ? old : size);
|
|
}
|
|
return q;
|
|
}
|
|
|
|
int main(void) {
|
|
printf("allocations %d\n", busy_allocations);
|
|
return 0;
|
|
}
|
|
"""
|
|
|
|
UDEV = r"""
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
struct list { const char *name; struct list *next; };
|
|
struct enumerate { struct list head; struct list added; char name[16]; int fail; };
|
|
|
|
void *udev_enumerate_new(void *udev) {
|
|
static int count;
|
|
struct enumerate *enumerate = calloc(1, sizeof(*enumerate));
|
|
(void)udev;
|
|
snprintf(enumerate->name, sizeof(enumerate->name), "real%d", count++);
|
|
enumerate->head.name = enumerate->name;
|
|
enumerate->added.name = "added";
|
|
return enumerate;
|
|
}
|
|
void *udev_enumerate_unref(void *enumerate) { free(enumerate); return NULL; }
|
|
int udev_enumerate_add_match_subsystem(void *enumerate, const char *subsystem) {
|
|
if (strcmp(subsystem, "fail") == 0) ((struct enumerate *)enumerate)->fail = 1;
|
|
return 0;
|
|
}
|
|
int udev_enumerate_add_match_parent(void *enumerate, void *parent) {
|
|
if (parent == NULL) return -1;
|
|
((struct enumerate *)enumerate)->head.name = "parented";
|
|
return 0;
|
|
}
|
|
int udev_enumerate_add_syspath(void *enumerate, const char *syspath) {
|
|
(void)syspath;
|
|
((struct enumerate *)enumerate)->head.next = &((struct enumerate *)enumerate)->added;
|
|
return 0;
|
|
}
|
|
int udev_enumerate_scan_devices(void *enumerate) { return ((struct enumerate *)enumerate)->fail ? -1 : 0; }
|
|
void *udev_enumerate_get_list_entry(void *enumerate) { return &((struct enumerate *)enumerate)->head; }
|
|
void *udev_list_entry_get_next(void *entry) { return ((struct list *)entry)->next; }
|
|
void *udev_list_entry_get_by_name(void *entry, const char *name) {
|
|
return strcmp(((struct list *)entry)->name, name) == 0 ? entry : NULL;
|
|
}
|
|
const char *udev_list_entry_get_name(void *entry) { return ((struct list *)entry)->name; }
|
|
void *udev_device_new_from_syspath(void *udev, const char *syspath) { (void)udev; (void)syspath; return NULL; }
|
|
void *udev_device_new_from_subsystem_sysname(void *udev, const char *subsystem, const char *sysname) {
|
|
(void)udev; (void)subsystem; (void)sysname;
|
|
return NULL;
|
|
}
|
|
int real_device_calls;
|
|
void *udev_device_unref(void *device) { (void)device; real_device_calls++; return NULL; }
|
|
void *udev_device_ref(void *device) { (void)device; real_device_calls++; return NULL; }
|
|
void *udev_device_get_parent(void *device) { (void)device; real_device_calls++; return NULL; }
|
|
void *udev_device_get_udev(void *device) { (void)device; real_device_calls++; return NULL; }
|
|
const char *udev_device_get_syspath(void *device) { (void)device; real_device_calls++; return "real"; }
|
|
const char *udev_device_get_sysnum(void *device) { (void)device; real_device_calls++; return "real"; }
|
|
void *udev_device_new_from_devnum(void *udev, char type, unsigned long devnum) {
|
|
(void)udev; (void)type; (void)devnum;
|
|
return NULL;
|
|
}
|
|
"""
|
|
|
|
DEVICE_FUNCTIONS = (
|
|
"p:udev_device_ref", "p:udev_device_get_udev", "p:udev_device_get_parent",
|
|
"P:udev_device_get_parent_with_subsystem_devtype", "s:udev_device_get_devpath", "s:udev_device_get_subsystem",
|
|
"s:udev_device_get_devtype", "s:udev_device_get_syspath", "s:udev_device_get_sysname",
|
|
"s:udev_device_get_sysnum", "s:udev_device_get_devnode", "i:udev_device_get_is_initialized",
|
|
"p:udev_device_get_devlinks_list_entry", "p:udev_device_get_properties_list_entry",
|
|
"p:udev_device_get_tags_list_entry", "p:udev_device_get_current_tags_list_entry",
|
|
"p:udev_device_get_sysattr_list_entry", "S:udev_device_get_property_value", "s:udev_device_get_driver",
|
|
"d:udev_device_get_devnum", "s:udev_device_get_action", "u:udev_device_get_seqnum",
|
|
"u:udev_device_get_usec_since_initialized", "S:udev_device_get_sysattr_value",
|
|
"W:udev_device_set_sysattr_value", "I:udev_device_has_tag", "I:udev_device_has_current_tag",
|
|
"p:udev_device_unref",
|
|
)
|
|
SIGNATURES = {
|
|
"p": ("void *", ""), "s": ("const char *", ""), "i": ("int", ""), "d": ("dev_t", ""),
|
|
"u": ("unsigned long long", ""), "S": ("const char *", ", const char *a"), "I": ("int", ", const char *a"),
|
|
"P": ("void *", ", const char *a, const char *b"), "W": ("int", ", const char *a, const char *b"),
|
|
}
|
|
UDEV += "#include <sys/types.h>\n" + "".join(
|
|
f"{SIGNATURES[kind][0]} {name}(void *device{SIGNATURES[kind][1]}) {{ (void)device; real_device_calls++; return 0; }}\n"
|
|
for kind, _, name in (entry.partition(":") for entry in DEVICE_FUNCTIONS) if not re.search(rf"\b{name}\(", UDEV))
|
|
|
|
PROGRAM = r"""
|
|
#define _GNU_SOURCE
|
|
#include <dlfcn.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <link.h>
|
|
#include <stdint.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/sysmacros.h>
|
|
#include <unistd.h>
|
|
|
|
#define PAD "/sys/devices/virtual/input/droiddeck0"
|
|
#define PARENT0 "/sys/devices/droiddeck0/droiddeck-input0"
|
|
#define EVENT0 PARENT0 "/event0"
|
|
#define EVENT1 "/sys/devices/droiddeck1/droiddeck-input1/event1"
|
|
#define HANDLES 256
|
|
|
|
int next_marker(void);
|
|
|
|
static void *lib;
|
|
static void *(*new_device)(void *, const char *);
|
|
static void *(*new_sysname)(void *, const char *, const char *);
|
|
static void *(*ref)(void *);
|
|
static void *(*unref)(void *);
|
|
static void *(*get_parent)(void *);
|
|
static void *(*get_udev)(void *);
|
|
static const char *(*syspath)(void *);
|
|
static const char *(*sysname)(void *);
|
|
static const char *(*sysnum)(void *);
|
|
static const char *(*devnode)(void *);
|
|
static const char *(*property)(void *, const char *);
|
|
static void *(*properties)(void *);
|
|
static void *(*enumerate_new)(void *);
|
|
static int (*match)(void *, const char *);
|
|
static int (*match_parent)(void *, void *);
|
|
static int (*add_syspath)(void *, const char *);
|
|
static int (*scan)(void *);
|
|
static void *(*list)(void *);
|
|
static void *(*next)(void *);
|
|
static void *(*by_name)(void *, const char *);
|
|
static const char *(*name)(void *);
|
|
static const char *(*value)(void *);
|
|
static const char *(*sysattr)(void *, const char *);
|
|
|
|
static void *sym(const char *symbol) { return dlsym(lib, symbol); }
|
|
|
|
static const char *entry_name(void *entry) { return entry != NULL ? name(entry) : "none"; }
|
|
|
|
static void print_list(const char *label, void *entry) {
|
|
printf("%s", label);
|
|
for (; entry != NULL; entry = next(entry)) printf(" %s", name(entry));
|
|
printf("\n");
|
|
}
|
|
|
|
static void print_properties(const char *label, void *device) {
|
|
void *entry;
|
|
|
|
printf("%s", label);
|
|
for (entry = properties(device); entry != NULL; entry = next(entry)) printf(" %s=%s", name(entry), value(entry));
|
|
printf("\n");
|
|
}
|
|
|
|
static void *matched(void *udev, const char *subsystem) {
|
|
void *enumerate = enumerate_new(udev);
|
|
|
|
match(enumerate, subsystem);
|
|
return enumerate;
|
|
}
|
|
|
|
static void pad_file(const char *node, int present) {
|
|
char path[4096];
|
|
FILE *file;
|
|
|
|
snprintf(path, sizeof(path), "%s/%s", getenv("FAKE_EVDEV_DIR"), node);
|
|
if (!present) {
|
|
unlink(path);
|
|
} else if ((file = fopen(path, "w")) != NULL) {
|
|
fclose(file);
|
|
}
|
|
}
|
|
|
|
static void run_udev(void) {
|
|
int udev_a, udev_b, i, failures = 0;
|
|
void *device, *parent, *other, *entry, *held[HANDLES] = {0};
|
|
char resolved[4096];
|
|
const char *path;
|
|
|
|
printf("missing %d\n", sym("udev_no_such_symbol") == NULL);
|
|
printf("plain %d\n", dlsym(RTLD_DEFAULT, "printf") != NULL);
|
|
device = new_device(&udev_a, PAD);
|
|
printf("syspath %s sysnum %s devnode %s\n", syspath(device), sysnum(device), devnode(device));
|
|
parent = get_parent(device);
|
|
printf("nested %d\n", strncmp(syspath(device), syspath(parent), strlen(syspath(parent))) == 0 &&
|
|
syspath(device)[strlen(syspath(parent))] == '/');
|
|
other = new_device(&udev_a, EVENT0);
|
|
printf("canonical %s\n", other != NULL ? syspath(other) : "none");
|
|
if (other != NULL) unref(other);
|
|
printf("parent %s sysname %s stable %d\n", syspath(parent), sysname(parent), parent == get_parent(device));
|
|
print_properties("device properties", device);
|
|
print_properties("parent properties", parent);
|
|
entry = properties(device);
|
|
printf("joystick %s unknown %d\n", value(by_name(entry, "ID_INPUT_JOYSTICK")), by_name(entry, "NO_SUCH_KEY") == NULL);
|
|
ref(device);
|
|
unref(device);
|
|
printf("held %s\n", syspath(device));
|
|
ref(parent);
|
|
unref(device);
|
|
printf("retained %s\n", syspath(parent));
|
|
unref(parent);
|
|
for (i = 0; i < 1000; i++) {
|
|
device = new_device(&udev_a, PAD);
|
|
if (device == NULL || get_parent(device) == NULL || get_parent(device) != get_parent(device) ||
|
|
strcmp(syspath(device), EVENT0) != 0)
|
|
failures++;
|
|
if (device != NULL) unref(device);
|
|
}
|
|
printf("reuse failures %d\n", failures);
|
|
for (i = 0; i < HANDLES && (held[i] = new_device(&udev_a, PAD)) != NULL; i++) {
|
|
}
|
|
printf("full %d extra %d parent %d\n", i, new_device(&udev_a, PAD) == NULL, get_parent(held[0]) == NULL);
|
|
unref(held[1]);
|
|
parent = get_parent(held[0]);
|
|
printf("freed parent %s stable %d\n", parent != NULL ? syspath(parent) : "none", parent == get_parent(held[0]));
|
|
for (i = 0; i < HANDLES; i++)
|
|
if (i != 1 && held[i] != NULL) unref(held[i]);
|
|
path = realpath("/sys/class/input/droiddeck-input0", resolved);
|
|
printf("parent link %s\n", path != NULL ? path : "none");
|
|
path = realpath("/sys/class/input/input0", resolved);
|
|
printf("kernel link shadowed %d\n", path != NULL && strncmp(path, "/sys/devices/droiddeck", 22) == 0);
|
|
printf("kernel sysname shadowed %d\n", new_sysname(&udev_a, "input", "input0") != NULL);
|
|
other = new_sysname(&udev_a, "input", "droiddeck-input0");
|
|
printf("parent sysname %s\n", other != NULL ? syspath(other) : "none");
|
|
if (other != NULL) unref(other);
|
|
device = new_device(&udev_a, PAD);
|
|
other = new_device(&udev_b, PAD);
|
|
printf("contexts %d %d\n", get_udev(device) == (void *)&udev_a, get_udev(other) == (void *)&udev_b);
|
|
}
|
|
|
|
static void run_stable(void) {
|
|
int udev;
|
|
char copy[64];
|
|
void *device = new_device(&udev, PAD);
|
|
const char *before = devnode(device);
|
|
|
|
snprintf(copy, sizeof(copy), "%s", before);
|
|
pad_file("event0", 0);
|
|
printf("gone %d\n", new_device(&udev, PAD) == NULL);
|
|
printf("devnode %s same %d property %s\n", devnode(device), devnode(device) == before && strcmp(before, copy) == 0,
|
|
property(device, "DEVNAME"));
|
|
}
|
|
|
|
static void run_enumerate(void) {
|
|
int udev, i;
|
|
void *first, *entry, *found, *missing, *failed, *parented, *real_parent, *device, *parent, *other;
|
|
|
|
first = matched(&udev, "input");
|
|
scan(first);
|
|
entry = list(first);
|
|
print_list("scan", entry);
|
|
pad_file("event1", 1);
|
|
print_list("same scan", list(first));
|
|
entry = list(first);
|
|
found = by_name(entry, EVENT0);
|
|
missing = by_name(entry, EVENT1);
|
|
printf("by name %s missing %d\n", entry_name(found), missing == NULL);
|
|
for (i = 0; i < 20; i++) {
|
|
void *other = matched(&udev, "input");
|
|
scan(other);
|
|
list(other);
|
|
}
|
|
printf("kept tail %s\n", entry_name(next(entry)));
|
|
scan(first);
|
|
print_list("rescan", list(first));
|
|
printf("rescan by name %s\n", entry_name(by_name(list(first), EVENT1)));
|
|
add_syspath(first, "/sys/devices/added");
|
|
print_list("added", list(first));
|
|
failed = matched(&udev, "input");
|
|
match(failed, "fail");
|
|
printf("failed scan %d\n", scan(failed));
|
|
print_list("failed", list(failed));
|
|
device = new_device(&udev, PAD);
|
|
parent = get_parent(device);
|
|
parented = enumerate_new(&udev);
|
|
printf("synthetic parent %d\n", match_parent(parented, parent));
|
|
scan(parented);
|
|
print_list("parented", list(parented));
|
|
real_parent = enumerate_new(&udev);
|
|
printf("real parent %d\n", match_parent(real_parent, &udev));
|
|
scan(real_parent);
|
|
print_list("real parent list", list(real_parent));
|
|
other = matched(&udev, "input");
|
|
match_parent(other, &udev);
|
|
scan(other);
|
|
print_list("real parent suppressed", list(other));
|
|
other = matched(&udev, "input");
|
|
match_parent(other, parent);
|
|
match_parent(other, &udev);
|
|
scan(other);
|
|
print_list("real replaces synthetic", list(other));
|
|
other = matched(&udev, "input");
|
|
match_parent(other, &udev);
|
|
match_parent(other, parent);
|
|
scan(other);
|
|
print_list("synthetic replaces real", list(other));
|
|
other = matched(&udev, "input");
|
|
match_parent(other, parent);
|
|
printf("bad parent %d\n", match_parent(other, NULL));
|
|
scan(other);
|
|
print_list("bad parent kept", list(other));
|
|
other = enumerate_new(&udev);
|
|
match(other, "fail");
|
|
match_parent(other, parent);
|
|
printf("nothing added scan %d\n", scan(other));
|
|
}
|
|
|
|
static void run_every(int count, char **entries) {
|
|
void *(*from_devnum)(void *, char, dev_t) = sym("udev_device_new_from_devnum");
|
|
int *real_calls = dlsym(lib, "real_device_calls");
|
|
int udev;
|
|
void *pad = from_devnum(&udev, 'c', makedev(13, 64));
|
|
int i;
|
|
|
|
for (i = 0; pad != NULL && i < count; i++) {
|
|
const char *name = entries[i] + 2;
|
|
void *call = sym(name);
|
|
|
|
if (call == NULL) {
|
|
printf("missing %s\n", name);
|
|
continue;
|
|
}
|
|
switch (entries[i][0]) {
|
|
case 'p': ((void *(*)(void *))call)(pad); break;
|
|
case 's': ((const char *(*)(void *))call)(pad); break;
|
|
case 'i': ((int (*)(void *))call)(pad); break;
|
|
case 'd': ((dev_t (*)(void *))call)(pad); break;
|
|
case 'u': ((unsigned long long (*)(void *))call)(pad); break;
|
|
case 'S': ((const char *(*)(void *, const char *))call)(pad, "attr"); break;
|
|
case 'I': ((int (*)(void *, const char *))call)(pad, "attr"); break;
|
|
case 'P': ((void *(*)(void *, const char *, const char *))call)(pad, "input", NULL); break;
|
|
case 'W': ((int (*)(void *, const char *, const char *))call)(pad, "attr", "value"); break;
|
|
default: printf("unknown signature %s\n", entries[i]);
|
|
}
|
|
}
|
|
printf("pad %d real calls %d\n", pad != NULL, real_calls != NULL ? *real_calls : -1);
|
|
}
|
|
|
|
static void print_product(const char *label, void *device) {
|
|
const char *uevent = device != NULL ? sysattr(device, "uevent") : NULL;
|
|
|
|
printf("%s %.*s\n", label, uevent != NULL ? (int)strcspn(uevent, "\n") : 4, uevent != NULL ? uevent : "none");
|
|
}
|
|
|
|
static void run_identity(void) {
|
|
int udev;
|
|
void *device = new_device(&udev, PAD);
|
|
|
|
print_product("pad", device);
|
|
print_product("parent", device != NULL ? get_parent(device) : NULL);
|
|
}
|
|
|
|
static void run_probe(void) {
|
|
int udev;
|
|
|
|
printf("pad %d\n", new_device(&udev, PAD) != NULL);
|
|
}
|
|
|
|
static int loaded_span(struct dl_phdr_info *info, size_t size, void *data) {
|
|
uintptr_t *span = data;
|
|
int i;
|
|
|
|
(void)size;
|
|
if (info->dlpi_addr != span[0]) return 0;
|
|
for (i = 0; i < info->dlpi_phnum; i++)
|
|
if (info->dlpi_phdr[i].p_type == PT_LOAD && info->dlpi_phdr[i].p_vaddr + info->dlpi_phdr[i].p_memsz > span[1])
|
|
span[1] = info->dlpi_phdr[i].p_vaddr + info->dlpi_phdr[i].p_memsz;
|
|
return 1;
|
|
}
|
|
|
|
static void run_reload(const char *path) {
|
|
struct link_map *map = NULL;
|
|
uintptr_t span[2] = {0, 0};
|
|
void *first = enumerate_new(NULL);
|
|
void *second;
|
|
|
|
dlinfo(lib, RTLD_DI_LINKMAP, &map);
|
|
span[0] = map->l_addr;
|
|
dl_iterate_phdr(loaded_span, span);
|
|
dlclose(lib);
|
|
mmap((void *)span[0], span[1], PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0);
|
|
lib = dlopen(path, RTLD_NOW | RTLD_LOCAL);
|
|
enumerate_new = sym("udev_enumerate_new");
|
|
list = sym("udev_enumerate_get_list_entry");
|
|
second = enumerate_new(NULL);
|
|
printf("reload %s %s\n", entry_name(list(first)), entry_name(list(second)));
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
char link[4096];
|
|
|
|
if (strcmp(argv[1], "once") == 0) {
|
|
printf("readlink %d\n", readlink("/proc/self/exe", link, sizeof(link)) > 0);
|
|
return 0;
|
|
}
|
|
if (strcmp(argv[1], "next") == 0) {
|
|
int (*chained)(void) = (int (*)(void))dlsym(RTLD_DEFAULT, "chained");
|
|
printf("next %d\n", next_marker());
|
|
if (chained != NULL) printf("chained %d\n", chained());
|
|
return 0;
|
|
}
|
|
lib = dlopen(argv[2], RTLD_NOW | RTLD_LOCAL);
|
|
if (strcmp(argv[1], "realpath") == 0) {
|
|
printf("realpath %s\n", realpath("/no/such/inputudev/path", link));
|
|
return 0;
|
|
}
|
|
new_device = sym("udev_device_new_from_syspath");
|
|
new_sysname = sym("udev_device_new_from_subsystem_sysname");
|
|
ref = sym("udev_device_ref");
|
|
unref = sym("udev_device_unref");
|
|
get_parent = sym("udev_device_get_parent");
|
|
get_udev = sym("udev_device_get_udev");
|
|
syspath = sym("udev_device_get_syspath");
|
|
sysname = sym("udev_device_get_sysname");
|
|
sysnum = sym("udev_device_get_sysnum");
|
|
devnode = sym("udev_device_get_devnode");
|
|
property = sym("udev_device_get_property_value");
|
|
properties = sym("udev_device_get_properties_list_entry");
|
|
enumerate_new = sym("udev_enumerate_new");
|
|
match = sym("udev_enumerate_add_match_subsystem");
|
|
match_parent = sym("udev_enumerate_add_match_parent");
|
|
add_syspath = sym("udev_enumerate_add_syspath");
|
|
scan = sym("udev_enumerate_scan_devices");
|
|
list = sym("udev_enumerate_get_list_entry");
|
|
next = sym("udev_list_entry_get_next");
|
|
by_name = sym("udev_list_entry_get_by_name");
|
|
name = sym("udev_list_entry_get_name");
|
|
value = sym("udev_list_entry_get_value");
|
|
sysattr = sym("udev_device_get_sysattr_value");
|
|
if (strcmp(argv[1], "probe") == 0) run_probe();
|
|
else if (strcmp(argv[1], "identity") == 0) run_identity();
|
|
else if (strcmp(argv[1], "every") == 0) run_every(argc - 3, argv + 3);
|
|
else if (strcmp(argv[1], "stable") == 0) run_stable();
|
|
else if (strcmp(argv[1], "enumerate") == 0) run_enumerate();
|
|
else if (strcmp(argv[1], "reload") == 0) run_reload(argv[2]);
|
|
else run_udev();
|
|
fflush(stdout);
|
|
return 0;
|
|
}
|
|
"""
|
|
|
|
|
|
def compile_c(*args):
|
|
result = subprocess.run(["cc", *args], capture_output=True, text=True)
|
|
if result.returncode != 0:
|
|
raise AssertionError(f"cc {' '.join(args)} failed:\n{result.stderr}")
|
|
return result.stdout
|
|
|
|
|
|
@unittest.skipUnless(sys.platform == "linux" and shutil.which("cc"), "needs Linux and cc")
|
|
class InputUdevPreloadTest(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.dir = Path(tempfile.mkdtemp())
|
|
cls.addClassCleanup(shutil.rmtree, cls.dir, ignore_errors=True)
|
|
build = cls.dir / "build"
|
|
build.mkdir()
|
|
if "__GLIBC__" not in compile_c("-dM", "-E", "-x", "c", "-include", "limits.h", "/dev/null"):
|
|
raise unittest.SkipTest("needs a glibc toolchain")
|
|
sources = (("once.c", ONCE), ("first.c", FIRST), ("second.c", SECOND), ("chain.c", CHAIN), ("mark.c", MARK),
|
|
("udev.c", UDEV), ("program.c", PROGRAM), ("locked.c", LOCKED), ("arena.c", ARENA))
|
|
for name, text in sources:
|
|
(build / name).write_text(text)
|
|
cls.lib = cls.dir / "libddsession.so"
|
|
compile_c("-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-o", str(cls.lib),
|
|
*map(str, sorted(PRELOAD.glob("*.c"))), "-ldl")
|
|
for name in ("once", "first", "second", "chain", "mark"):
|
|
compile_c("-shared", "-fPIC", "-O0", "-pthread", "-o", str(cls.dir / f"lib{name}.so"),
|
|
str(build / f"{name}.c"), "-ldl")
|
|
cls.udev = cls.dir / "libudev.so.1"
|
|
compile_c("-shared", "-fPIC", "-O2", "-o", str(cls.udev), str(build / "udev.c"))
|
|
cls.program = cls.dir / "program"
|
|
compile_c("-O0", "-o", str(cls.program), str(build / "program.c"), str(cls.dir / "libfirst.so"), "-ldl")
|
|
compile_c("-shared", "-fPIC", "-O0", "-o", str(cls.dir / "liblocked.so"), str(build / "locked.c"))
|
|
cls.arena = cls.dir / "arena"
|
|
compile_c("-O0", "-rdynamic", "-o", str(cls.arena), str(build / "arena.c"), "-Wl,--no-as-needed",
|
|
str(cls.dir / "liblocked.so"),
|
|
f"-Wl,-rpath,{cls.dir}")
|
|
cls.pads = cls.pad_dir("pads", "event0")
|
|
|
|
@classmethod
|
|
def pad_dir(cls, name, *nodes):
|
|
pads = cls.dir / name
|
|
pads.mkdir()
|
|
for node in nodes:
|
|
(pads / node).write_text("")
|
|
return pads
|
|
|
|
def run_program(self, mode, *preloads, pads=None, slots="0=/pad", program=None, args=(), extra=None):
|
|
env = dict(os.environ, LD_PRELOAD=":".join(str(self.dir / name) for name in ("libddsession.so", *preloads)),
|
|
FAKE_EVDEV_MEMFD_PATHS=slots, FAKE_EVDEV_DIR=str(pads or self.pads))
|
|
for key in ("FAKE_EVDEV_UINPUT", "FAKE_EVDEV_STEAM_VIRTUAL", "SDL_GAMECONTROLLER_IGNORE_DEVICES",
|
|
"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT"):
|
|
env.pop(key, None)
|
|
env.update(extra or {})
|
|
result = subprocess.run([str(program or self.program), mode, str(self.udev), *args], env=env, capture_output=True, text=True,
|
|
timeout=20)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
return result.stdout.splitlines()
|
|
|
|
def test_a_lookup_from_another_preloads_initialiser_does_not_reenter_it(self):
|
|
self.assertEqual(self.run_program("once", "libonce.so"), ["readlink 1"])
|
|
|
|
def test_a_first_wrapped_call_inside_the_programs_allocator_does_not_allocate(self):
|
|
result = subprocess.run([str(self.arena)], env=dict(os.environ, LD_PRELOAD=str(self.lib)), capture_output=True,
|
|
text=True, timeout=20)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertEqual(result.stdout.splitlines(), ["allocations 0"])
|
|
|
|
def test_the_real_lookup_still_sees_the_original_caller(self):
|
|
self.assertEqual(self.run_program("next", "libfirst.so", "libsecond.so"), ["next 2"])
|
|
|
|
def test_another_preloads_dlsym_stays_in_the_chain(self):
|
|
self.assertEqual(self.run_program("next", "libfirst.so", "libchain.so", "libsecond.so"), ["next 2", "chained 1"])
|
|
|
|
def test_non_udev_lookups_skip_the_opened_libudev(self):
|
|
self.assertEqual(self.run_program("realpath", "libmark.so"), ["realpath marked"])
|
|
|
|
def test_a_program_resolving_udev_through_dlsym_gets_the_pads(self):
|
|
parent = "/sys/devices/droiddeck0/droiddeck-input0"
|
|
pad = f"{parent}/event0"
|
|
self.assertEqual(self.run_program("udev"), [
|
|
"missing 1",
|
|
"plain 1",
|
|
f"syspath {pad} sysnum 0 devnode /dev/input/event0",
|
|
"nested 1",
|
|
f"canonical {pad}",
|
|
f"parent {parent} sysname droiddeck-input0 stable 1",
|
|
f"device properties DEVNAME=/dev/input/event0 DEVPATH={pad[4:]} ID_INPUT=1 ID_INPUT_JOYSTICK=1",
|
|
f"parent properties DEVPATH={parent[4:]} ID_INPUT=1 ID_INPUT_JOYSTICK=1",
|
|
"joystick 1 unknown 1",
|
|
f"held {pad}",
|
|
f"retained {parent}",
|
|
"reuse failures 0",
|
|
"full 256 extra 1 parent 1",
|
|
f"freed parent {parent} stable 1",
|
|
f"parent link {parent}",
|
|
"kernel link shadowed 0",
|
|
"kernel sysname shadowed 0",
|
|
f"parent sysname {parent}",
|
|
"contexts 1 1",
|
|
])
|
|
|
|
def test_no_device_call_on_a_pad_reaches_the_real_libudev(self):
|
|
self.assertEqual(self.run_program("every", args=DEVICE_FUNCTIONS), ["pad 1 real calls 0"])
|
|
|
|
def test_a_handle_keeps_its_strings_after_the_pad_goes_away(self):
|
|
self.assertEqual(self.run_program("stable", pads=self.pad_dir("stable", "event0")), [
|
|
"gone 1",
|
|
"devnode /dev/input/event0 same 1 property /dev/input/event0",
|
|
])
|
|
|
|
def test_enumerations_keep_their_scan_and_their_state(self):
|
|
parent = "/sys/devices/droiddeck0/droiddeck-input0"
|
|
pad = f"{parent}/event0"
|
|
pad1 = "/sys/devices/droiddeck1/droiddeck-input1/event1"
|
|
self.assertEqual(self.run_program("enumerate", pads=self.pad_dir("enumerate", "event0"), slots="0=/pad;1=/pad"), [
|
|
f"scan {pad} real0",
|
|
f"same scan {pad} real0",
|
|
f"by name {pad} missing 1",
|
|
"kept tail real0",
|
|
f"rescan {pad} {pad1} real0",
|
|
f"rescan by name {pad1}",
|
|
f"added {pad} {pad1} real0 added",
|
|
"failed scan 0",
|
|
f"failed {pad} {pad1}",
|
|
"synthetic parent 0",
|
|
f"parented {parent} {pad}",
|
|
"real parent 0",
|
|
"real parent list parented",
|
|
"real parent suppressed parented",
|
|
"real replaces synthetic parented",
|
|
f"synthetic replaces real {parent} {pad}",
|
|
"bad parent -1",
|
|
f"bad parent kept {parent} {pad}",
|
|
"nothing added scan -1",
|
|
])
|
|
|
|
def identity(self, pads=None, **extra):
|
|
return self.run_program("identity", pads=pads, extra=extra)
|
|
|
|
def test_a_ring_pad_keeps_the_steam_input_identity_whatever_sdl_is_told(self):
|
|
steam = ["pad PRODUCT=6/28de/11ff/110", "parent PRODUCT=6/28de/11ff/110"]
|
|
for extra in ({}, {"FAKE_EVDEV_UINPUT": "1"}, {"FAKE_EVDEV_STEAM_VIRTUAL": "0"}, {"FAKE_EVDEV_STEAM_VIRTUAL": "1"},
|
|
{"SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x28de/0x1142"},
|
|
{"SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x28de/0x1142,0x045E/0x028E"},
|
|
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": "0x045e/0x028e"},
|
|
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": "0x28de/0x11ff"},
|
|
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": ""},
|
|
{"FAKE_EVDEV_UINPUT": "1", "SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x045e/0x028e"}):
|
|
with self.subTest(**extra):
|
|
self.assertEqual(self.identity(**extra), steam)
|
|
|
|
def test_a_pad_under_steam_inputs_own_virtual_pad_stays_an_xbox_pad(self):
|
|
pads = self.pad_dir("under-virtual", "event0", "event16")
|
|
(pads / ".uinput-event16.uevent").write_text('PRODUCT=6/28de/11ff/1\nNAME="Microsoft X-Box 360 pad 0"\n')
|
|
self.assertEqual(self.identity(pads, FAKE_EVDEV_UINPUT="1", SDL_GAMECONTROLLER_IGNORE_DEVICES="0x045e/0x028e"),
|
|
["pad PRODUCT=3/45e/28e/110", "parent PRODUCT=3/45e/28e/110"])
|
|
|
|
def test_a_program_that_closes_and_reopens_libudev_keeps_working(self):
|
|
self.assertEqual(self.run_program("reload"), ["reload real0 real1"])
|
|
|
|
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)):
|
|
with self.subTest(path=path):
|
|
client = self.dir / "clients" / path
|
|
client.parent.mkdir(parents=True, exist_ok=True)
|
|
shutil.copy(self.program, client)
|
|
self.assertEqual(self.run_program("probe", program=client), [f"pad {pad}"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|