I know that use the command “chroot” in linux need some files or directories such as usr, bin and so on. But when I use the function chroot() in C, do I need these files?
Here is my code, which “hw.out” is a binary file which just print “Hello, world”. I compiled it and run it as root, but it was failed to print “Hello, world”. What else should I do? Thank you!
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int result = chroot(".");
if(result == 0)
printf("Chroot Succese.\n");
char *arrays[]={"./hw.out",NULL};
execvp("./hw.out", arrays);
return 0;
}
execvpis most likely failing, probably withENOENT: no such file or directory, ifhw.outis a dynamically linked executable.For that to work, all the libraries required by
hw.outneed to be findable in thechrooted environment.Try linking
hw.outstatically, and it should work. (And add error checking afterexecvpto see whaterrnois set to after the call if it returns.)