Files
DroidDeck/tools/linuxfs/preload/lock.c
T
MaxsTechReview a20528c62b fix(preload): 32-bit x86 programs get correct large-file stat, open, write and lock results (#2592)
The session preload is also built for i386 and preloaded into every 32-bit
x86 program under FEX. There it aliased fstat64, ftruncate64, pwrite64,
fallocate64, posix_fallocate64, open64, openat64 and fcntl64 to their 32-bit
hooks, which on i386 take a different struct layout or argument size. fstat64
returned a size with garbage high bits, so libdbus rejected a valid 33-byte
/etc/machine-id as too large and aborted the program. The aliases now exist
only where off_t is off64_t; on i386 the 64-bit calls reach their own glibc
functions, keeping the open and lock hooks.
2026-11-15 17:20:59 -05:00

116 lines
2.7 KiB
C

/*
* Ported from WinNative (maxjivi05, feature/wayland-gamescope, 2026-09-18/19), GPL-3.0.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stddef.h>
#include <sys/file.h>
#include <unistd.h>
static int unsupported(int error) {
return error == ENOSYS || error == ENOTSUP || error == EOPNOTSUPP;
}
int flock(int fd, int operation) {
static int (*real_flock)(int, int);
int result;
if (real_flock == NULL) real_flock = dlsym(RTLD_NEXT, "flock");
if (real_flock == NULL) return 0;
result = real_flock(fd, operation);
if (result < 0 && unsupported(errno)) return 0;
return result;
}
static int is_lock_command(int cmd) {
switch (cmd) {
case F_SETLK:
case F_SETLKW:
#if defined(F_SETLK64) && F_SETLK64 != F_SETLK
case F_SETLK64:
case F_SETLKW64:
#endif
#ifdef F_OFD_SETLK
case F_OFD_SETLK:
#endif
#ifdef F_OFD_SETLKW
case F_OFD_SETLKW:
#endif
return 1;
default:
return 0;
}
}
static int is_lock_query(int cmd) {
switch (cmd) {
case F_GETLK:
#if defined(F_GETLK64) && F_GETLK64 != F_GETLK
case F_GETLK64:
#endif
#ifdef F_OFD_GETLK
case F_OFD_GETLK:
#endif
return 1;
default:
return 0;
}
}
int bl_nl_before_dup(int fd, void **state) __attribute__((visibility("hidden")));
void bl_nl_after_dup(void *state, int fd) __attribute__((visibility("hidden")));
static int fcntl_with(int (**real_fcntl)(int, int, ...), const char *name, int fd, int cmd, void *arg) {
int result;
if (*real_fcntl == NULL) *real_fcntl = dlsym(RTLD_NEXT, name);
if (*real_fcntl == NULL) {
errno = ENOSYS;
return -1;
}
void *nl_state = NULL;
int duplicate = cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC;
if (duplicate && bl_nl_before_dup(fd, &nl_state) != 0) return -1;
result = (*real_fcntl)(fd, cmd, arg);
if (duplicate) bl_nl_after_dup(nl_state, result);
if (result >= 0 || !unsupported(errno)) return result;
if (is_lock_command(cmd)) return 0;
if (is_lock_query(cmd) && arg != NULL) {
((struct flock *)arg)->l_type = F_UNLCK;
return 0;
}
return result;
}
int fcntl(int fd, int cmd, ...) {
static int (*real_fcntl)(int, int, ...);
va_list ap;
void *arg;
va_start(ap, cmd);
arg = va_arg(ap, void *);
va_end(ap);
return fcntl_with(&real_fcntl, "fcntl", fd, cmd, arg);
}
#if defined(__GLIBC__) && defined(F_SETLK64)
#ifdef __OFF_T_MATCHES_OFF64_T
int fcntl64(int fd, int cmd, ...) __attribute__((alias("fcntl")));
#else
int fcntl64(int fd, int cmd, ...) {
static int (*real_fcntl64)(int, int, ...);
va_list ap;
void *arg;
va_start(ap, cmd);
arg = va_arg(ap, void *);
va_end(ap);
return fcntl_with(&real_fcntl64, "fcntl64", fd, cmd, arg);
}
#endif
#endif