Skip to content

Instantly share code, notes, and snippets.

@joxer
Created January 23, 2019 17:56
Show Gist options
  • Save joxer/1cd0a4495af4e3abd9564401c23bb2d8 to your computer and use it in GitHub Desktop.
Save joxer/1cd0a4495af4e3abd9564401c23bb2d8 to your computer and use it in GitHub Desktop.
exit the chroot
//create first a directory called chroot!
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
void printDirInformation(DIR* dir) {
char* name;
struct dirent* current_dir;
do {
current_dir = readdir(dir);
if(current_dir != NULL) {
name = current_dir->d_name;
printf("%s\n", name);
}
} while(current_dir != NULL );
}
int main() {
int fd;
fd = open(".", O_RDONLY);
chdir("chroot");
int ret = chroot(".");
if(ret == -1) {
printf("cannot enter in chroot\n");
exit(1);
}
printf("open first chroot\n");
DIR *dir = opendir(".");
if(dir != NULL)
printDirInformation(dir);
printf("open second chroot\n");
fchdir(fd);
chroot(".");
dir = opendir(".");
if(dir != NULL)
printDirInformation(dir);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment