This repository has been archived on 2022-05-27. You can view files and clone it, but cannot push or open issues or pull requests.
ocaml-cgroups2/examples/unshare_mount_passwd.c
2022-05-23 00:28:16 +01:00

43 lines
714 B
C

#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define BUF_SIZE 1024
void print_file(int fd);
int main() {
int fd;
if ((fd = open("/etc/passwd", O_RDONLY)) < 0)
perror("open");
print_file(fd);
if (close(fd))
perror("close");
if (unshare(CLONE_NEWNS))
perror("unshare");
printf("----- unshared -----\n");
if ((fd = open("/etc/passwd", O_RDONLY)) < 0)
perror("open");
print_file(fd);
if (close(fd))
perror("close");
}
void print_file(int fd) {
char buf[BUF_SIZE];
int bytes_read;
while ((bytes_read = read(fd, buf, BUF_SIZE)) > 0)
if (write(1, buf, bytes_read) < 0)
perror("write");
if (bytes_read == -1)
perror("read");
}