mergerfs/tools/preload.c

441 lines
9.4 KiB
C
Raw Normal View History

/*
ISC License
Copyright (c) 2025, Antonio SJ Musumeci <trapexit@spawn.link>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef __FreeBSD__
#define O_TMPFILE 0
#else
#define _GNU_SOURCE
//#define _POSIX_C_SOURCE 200809L
#endif
#include <assert.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef __FreeBSD__
#include <sys/extattr.h>
#else
#include <sys/xattr.h>
#endif
#include <unistd.h>
2026-11-25 12:59:47 -05:00
// Unary + applies the same integer promotion as a variadic argument.
typedef __typeof__(+(mode_t)0) OpenModeArg;
static
int
_open_needs_mode(const int flags_)
{
return ((flags_ & O_CREAT) || (O_TMPFILE && ((flags_ & O_TMPFILE) == O_TMPFILE)));
}
typedef char IOCTL_BUF[4096];
#define IOCTL_APP_TYPE 0xDF
#define IOCTL_FILE_INFO _IOWR(IOCTL_APP_TYPE,0,IOCTL_BUF)
#define LOAD_FUNC(func) \
do \
{ \
if(!_libc_##func) \
_libc_##func = (func##_func_t)dlsym(RTLD_NEXT,#func); \
assert(_libc_##func != NULL); \
} \
while(0)
typedef int (*open_func_t)(const char*, int, ...);
typedef int (*open64_func_t)(const char*, int, ...);
typedef int (*openat_func_t)(int, const char*, int, ...);
typedef int (*openat64_func_t)(int, const char*, int, ...);
typedef int (*creat_func_t)(const char*, mode_t);
typedef int (*creat64_func_t)(const char*, mode_t);
typedef FILE* (*fopen_func_t)(const char*, const char*);
typedef FILE* (*fopen64_func_t)(const char*, const char*);
static open_func_t _libc_open = NULL;
static open64_func_t _libc_open64 = NULL;
static openat_func_t _libc_openat = NULL;
static openat64_func_t _libc_openat64 = NULL;
static fopen_func_t _libc_fopen = NULL;
static fopen64_func_t _libc_fopen64 = NULL;
static creat_func_t _libc_creat = NULL;
static creat64_func_t _libc_creat64 = NULL;
static
int
get_underlying_filepath(int fd_,
char *filepath_,
size_t filepath_size_)
{
2026-11-25 12:59:47 -05:00
ssize_t rv;
2026-11-25 12:59:47 -05:00
#ifdef __FreeBSD__
rv = extattr_get_fd(fd_,EXTATTR_NAMESPACE_USER,"mergerfs.fullpath",filepath_,filepath_size_);
#else
rv = fgetxattr(fd_,"user.mergerfs.fullpath",filepath_,filepath_size_);
2026-11-25 12:59:47 -05:00
#endif
if((rv <= 0) || ((size_t)rv >= filepath_size_))
return -1;
2026-11-25 12:59:47 -05:00
// The virtual xattr contains pathname bytes, not a trailing NUL.
filepath_[rv] = '\0';
2026-11-25 12:59:47 -05:00
return rv;
}
#ifndef _FILE_OFFSET_BITS
int
open(const char *pathname_,
int flags_,
...)
{
int rv;
int fd;
mode_t mode;
struct stat st;
LOAD_FUNC(open);
mode = 0;
2026-11-25 12:59:47 -05:00
if(_open_needs_mode(flags_))
{
va_list args;
va_start(args,flags_);
2026-11-25 12:59:47 -05:00
mode = va_arg(args,OpenModeArg);
va_end(args);
}
fd = _libc_open(pathname_,flags_,mode);
if(fd == -1)
return -1;
2024-02-29 12:11:04 -06:00
if(flags_ & (O_DIRECTORY|O_PATH|O_TMPFILE))
return fd;
rv = fstat(fd,&st);
if(rv == -1)
return fd;
if((st.st_mode & S_IFMT) != S_IFREG)
return fd;
char real_pathname[PATH_MAX];
rv = get_underlying_filepath(fd,real_pathname,sizeof(real_pathname));
if(rv == -1)
return fd;
2026-11-25 12:59:47 -05:00
// Truncation already happened on the mergerfs open; do not repeat it.
flags_ &= ~(O_EXCL|O_CREAT|O_TRUNC);
rv = _libc_open(real_pathname,flags_,mode);
if(rv == -1)
return fd;
close(fd);
return rv;
}
#endif
int
open64(const char *pathname_,
int flags_,
...)
{
int rv;
int fd;
mode_t mode;
struct stat st;
LOAD_FUNC(open64);
mode = 0;
2026-11-25 12:59:47 -05:00
if(_open_needs_mode(flags_))
{
va_list args;
va_start(args,flags_);
2026-11-25 12:59:47 -05:00
mode = va_arg(args,OpenModeArg);
va_end(args);
}
fd = _libc_open64(pathname_,flags_,mode);
if(fd == -1)
return -1;
2024-02-29 12:11:04 -06:00
if(flags_ & (O_DIRECTORY|O_PATH|O_TMPFILE))
return fd;
rv = fstat(fd,&st);
if(rv == -1)
return fd;
if((st.st_mode & S_IFMT) != S_IFREG)
return fd;
char real_pathname[PATH_MAX];
rv = get_underlying_filepath(fd,real_pathname,sizeof(real_pathname));
if(rv == -1)
return fd;
2026-11-25 12:59:47 -05:00
// Truncation already happened on the mergerfs open; do not repeat it.
flags_ &= ~(O_EXCL|O_CREAT|O_TRUNC);
rv = _libc_open64(real_pathname,flags_,mode);
if(rv == -1)
return fd;
close(fd);
return rv;
}
#ifndef _FILE_OFFSET_BITS
int
openat(int dirfd_,
const char *pathname_,
int flags_,
...)
{
int rv;
int fd;
mode_t mode;
struct stat st;
LOAD_FUNC(openat);
mode = 0;
2026-11-25 12:59:47 -05:00
if(_open_needs_mode(flags_))
{
va_list args;
va_start(args,flags_);
2026-11-25 12:59:47 -05:00
mode = va_arg(args,OpenModeArg);
va_end(args);
}
fd = _libc_openat(dirfd_,pathname_,flags_,mode);
if(fd == -1)
return -1;
2024-02-29 12:11:04 -06:00
if(flags_ & (O_DIRECTORY|O_PATH|O_TMPFILE))
return fd;
rv = fstat(fd,&st);
if(rv == -1)
return fd;
if((st.st_mode & S_IFMT) != S_IFREG)
return fd;
char real_pathname[PATH_MAX];
rv = get_underlying_filepath(fd,real_pathname,sizeof(real_pathname));
if(rv == -1)
return fd;
2026-11-25 12:59:47 -05:00
// Truncation already happened on the mergerfs open; do not repeat it.
flags_ &= ~(O_EXCL|O_CREAT|O_TRUNC);
rv = _libc_openat(dirfd_,real_pathname,flags_,mode);
if(rv == -1)
return fd;
close(fd);
return rv;
}
#endif
int
openat64(int dirfd_,
const char *pathname_,
int flags_,
...)
{
int rv;
int fd;
mode_t mode;
struct stat st;
LOAD_FUNC(openat64);
mode = 0;
2026-11-25 12:59:47 -05:00
if(_open_needs_mode(flags_))
{
va_list args;
va_start(args,flags_);
2026-11-25 12:59:47 -05:00
mode = va_arg(args,OpenModeArg);
va_end(args);
}
fd = _libc_openat64(dirfd_,pathname_,flags_,mode);
if(fd == -1)
return -1;
2024-02-29 12:11:04 -06:00
if(flags_ & (O_DIRECTORY|O_PATH|O_TMPFILE))
return fd;
rv = fstat(fd,&st);
if(rv == -1)
return fd;
if((st.st_mode & S_IFMT) != S_IFREG)
return fd;
char real_pathname[PATH_MAX];
rv = get_underlying_filepath(fd,real_pathname,sizeof(real_pathname));
if(rv == -1)
return fd;
2026-11-25 12:59:47 -05:00
// Truncation already happened on the mergerfs open; do not repeat it.
flags_ &= ~(O_EXCL|O_CREAT|O_TRUNC);
rv = _libc_openat64(dirfd_,real_pathname,flags_,mode);
if(rv == -1)
return fd;
close(fd);
return rv;
}
2026-11-25 12:59:47 -05:00
// Replaying a creating or truncating mode could recreate a vanished
// name or truncate an existing file twice. Only redirect read modes.
static
FILE*
2026-11-25 12:59:47 -05:00
_fopen_try_backing_stream(FILE *f_,
const char *mode_,
fopen_func_t fopen_func_)
{
2026-11-25 12:59:47 -05:00
int fd;
int rv;
FILE *f2;
char real_pathname[PATH_MAX];
struct stat st;
2026-11-25 12:59:47 -05:00
if((mode_[0] != 'r') || (strchr(mode_,'x') != NULL))
return f_;
2026-11-25 12:59:47 -05:00
fd = fileno(f_);
if(fd == -1)
2026-11-25 12:59:47 -05:00
return f_;
rv = fstat(fd,&st);
if(rv == -1)
2026-11-25 12:59:47 -05:00
return f_;
if((st.st_mode & S_IFMT) != S_IFREG)
2026-11-25 12:59:47 -05:00
return f_;
rv = get_underlying_filepath(fd,real_pathname,sizeof(real_pathname));
if(rv == -1)
2026-11-25 12:59:47 -05:00
return f_;
2026-11-25 12:59:47 -05:00
f2 = fopen_func_(real_pathname,mode_);
if(f2 == NULL)
2026-11-25 12:59:47 -05:00
return f_;
2026-11-25 12:59:47 -05:00
fclose(f_);
return f2;
}
2026-11-25 12:59:47 -05:00
#ifndef _FILE_OFFSET_BITS
FILE*
fopen(const char *pathname_,
const char *mode_)
{
FILE *f;
LOAD_FUNC(fopen);
f = _libc_fopen(pathname_,mode_);
if(f == NULL)
return NULL;
return _fopen_try_backing_stream(f,mode_,_libc_fopen);
}
#endif
FILE*
fopen64(const char *pathname_,
const char *mode_)
{
FILE *f;
LOAD_FUNC(fopen64);
f = _libc_fopen64(pathname_,mode_);
if(f == NULL)
return NULL;
2026-11-25 12:59:47 -05:00
return _fopen_try_backing_stream(f,mode_,_libc_fopen64);
}
#ifndef _FILE_OFFSET_BITS
int
creat(const char *pathname_,
mode_t mode_)
{
int fd;
int rv;
LOAD_FUNC(creat);
2026-11-25 12:59:47 -05:00
LOAD_FUNC(open);
fd = _libc_creat(pathname_,mode_);
if(fd == -1)
return -1;
char real_pathname[PATH_MAX];
rv = get_underlying_filepath(fd,real_pathname,sizeof(real_pathname));
if(rv == -1)
return fd;
2026-11-25 12:59:47 -05:00
// The mergerfs creat already created and truncated the backing file.
rv = _libc_open(real_pathname,O_WRONLY);
if(rv == -1)
return fd;
close(fd);
return rv;
}
#endif
int
creat64(const char *pathname_,
mode_t mode_)
{
int fd;
int rv;
LOAD_FUNC(creat64);
2026-11-25 12:59:47 -05:00
LOAD_FUNC(open64);
fd = _libc_creat64(pathname_,mode_);
if(fd == -1)
return -1;
char real_pathname[PATH_MAX];
rv = get_underlying_filepath(fd,real_pathname,sizeof(real_pathname));
if(rv == -1)
return fd;
2026-11-25 12:59:47 -05:00
// The mergerfs creat already created and truncated the backing file.
rv = _libc_open64(real_pathname,O_WRONLY);
if(rv == -1)
return fd;
close(fd);
return rv;
}