Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Small lib to mock chroot() for pacman in Arch Linux running under WSL.
// gcc libmockchroot.c -shared -fPIC -ldl -o /lib64/libmockchroot.so
// echo "/lib64/libmockchroot.so" >> /etc/ld.so.preload
#define _GNU_SOURCE
#include <unistd.h>
#include <libgen.h>
#include <dlfcn.h>
#include <linux/limits.h>
#include <string.h>
int chroot(const char *path)
{
// return 0 for pacman
char dest[PATH_MAX];
if (readlink("/proc/self/exe", dest, PATH_MAX) != -1) {
char* file = basename(dest);
if (strcmp(file, "pacman") == 0) {
return 0;
}
}
// passthrough for others,
// currently results in -1 and errno=38
int (*_chroot)(const char*);
_chroot = dlsym(RTLD_NEXT, "chroot");
return (*_chroot)(path);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.