Created
September 10, 2016 09:19
-
-
Save alexellis/cce3b566311d736f63c85b9291571503 to your computer and use it in GitHub Desktop.
chroot in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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