Small lib to mock chroot() for pacman in Arch Linux running under WSL.

September 1, 2016 ยท View on GitHub

// 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);

}