Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <sched.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/mount.h>
- #include <sys/stat.h>
- #include <sys/sysinfo.h>
- #define DIR1 "/tmp/dir1_%d"
- #define DIR2 "/tmp/dir2_%d"
- int main(void)
- {
- /* unshare -rm: new mount + user namespace */
- if (getuid() != 0 && unshare(CLONE_NEWNS | CLONE_NEWUSER) == -1) {
- perror("unshare");
- return 1;
- }
- int nprocs = get_nprocs();
- int id = 0;
- for (int i = 1; i < nprocs; i++) {
- pid_t pid = fork();
- if (pid == -1) {
- perror("fork");
- return 1;
- }
- if (pid == 0) {
- id = i;
- break;
- }
- }
- char dir1[32], dir2[32];
- snprintf(dir1, sizeof(dir1), DIR1, id);
- snprintf(dir2, sizeof(dir2), DIR2, id);
- mkdir(dir1, 0755);
- mkdir(dir2, 0755);
- while (1) {
- if (mount(dir1, dir2, NULL, MS_BIND, NULL) == -1) {
- fprintf(stderr, "mount --bind %s %s: %s\n", dir1, dir2, strerror(errno));
- return 1;
- }
- if (umount(dir2) == -1) {
- fprintf(stderr, "umount %s: %s\n", dir2, strerror(errno));
- return 1;
- }
- }
- return 0;
- }
Advertisement