Skip to content

Instantly share code, notes, and snippets.

@alexellis
Created September 10, 2016 09:19
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 alexellis/cce3b566311d736f63c85b9291571503 to your computer and use it in GitHub Desktop.
Save alexellis/cce3b566311d736f63c85b9291571503 to your computer and use it in GitHub Desktop.
chroot in C
#include <stdio.h>
#include <unistd.h>
int enter_chroot(const char * root) {
/* chroot */
chdir(root);
if (chroot(root) != 0) {
perror("chroot");
return 1;
}
}
int read_print(const char* path) {
FILE *f;
f = fopen(path, "r");
if (f == NULL) {
perror(path);
return 1;
} else {
char buffer[50];
while (fgets(buffer, sizeof(buffer), f)) {
printf("%s", buffer);
}
}
return 0;
}
int main(void) {
read_print("/etc/hostname");
if(enter_chroot("/tmp")) {
return 1;
}
int val = read_print("/etc/hostname");
return val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment