Skip to content

Instantly share code, notes, and snippets.

@RoliSoft
Created September 1, 2016 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RoliSoft/84813cc353caec614dee8bf74c1b09ef to your computer and use it in GitHub Desktop.
Save RoliSoft/84813cc353caec614dee8bf74c1b09ef to your computer and use it in GitHub Desktop.
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