#define _GNU_SOURCE #include #include #include #include #include #include #include void slurp(const char *fn) { char buf[8192]; ssize_t r; int fd; printf("%s [pid: %d]\n", fn, getpid()); fflush(stdout); fd = open(fn, O_RDONLY); if (fd < 0) { perror("open"); exit(1); } r = read(fd, buf, sizeof(buf)-1); if (r < 0) { perror("read"); exit(1); } buf[r] = 0; puts(buf); fflush(stdout); if (close(fd) < 0) { perror("close"); exit(1); } } void newnet(const char *ns) { int fd; fd = open(ns, O_RDONLY); if (fd < 0) { perror("open"); exit(1); } if (setns(fd, CLONE_NEWNET) < 0) { perror("setns"); exit(1); } if (close(fd) < 0) { perror("close"); exit(1); } } int main(int argc, char **argv) { const char *ns = "/run/netns/test"; const char *fn = "/proc/net/sctp/snmp"; int d = 1; // Optional args: /run/netns/... /proc/net/... n if (argc >= 2) ns = argv[1]; if (argc >= 3) fn = argv[2]; if (argc >= 4 && argv[3][0] == 'n') d = 0; if (d) slurp(fn); printf("setns(%s) ...\n", ns); fflush(stdout); newnet(ns); slurp(fn); }