namespaces/unshare.cThis is namespaces/unshare.c, an example to accompany the book, The Linux Programming Interface. This file is not printed in the book; it demonstrates Linux features that are not described in the book (typically features that have appeared since the book was published). The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3. In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions.
|
/* unshare.c A simple implementation of the unshare(1) command: unshare namespaces and execute a command. See https://lwn.net/Articles/531381/ */ #define _GNU_SOURCE #include <sched.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/wait.h> #ifndef CLONE_NEWCGROUP /* Added in Linux 4.6 */ #define CLONE_NEWCGROUP 0x02000000 #endif #ifndef CLONE_NEWTIME /* Added in Linux 5.6 */ #define CLONE_NEWTIME 0x00000080 #endif /* A simple error-handling function: print an error message based on the value in 'errno' and terminate the calling process */ #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ } while (0)
static void usage(char *pname) { fprintf(stderr, "Usage: %s [options] cmd [arg...]\n", pname); fprintf(stderr, "Options can be:\n"); fprintf(stderr, " -f fork() before executing cmd " "(useful when unsharing PID namespace)\n"); fprintf(stderr, " -C unshare cgroup namespace\n"); fprintf(stderr, " -i unshare IPC namespace\n"); fprintf(stderr, " -m unshare mount namespace\n"); fprintf(stderr, " -n unshare network namespace\n"); fprintf(stderr, " -p unshare PID namespace\n"); fprintf(stderr, " -T unshare time namespace\n"); fprintf(stderr, " -u unshare UTS namespace\n"); fprintf(stderr, " -U unshare user namespace\n"); exit(EXIT_FAILURE); }
int main(int argc, char *argv[]) { int flags = 0; int do_fork = 0; int opt; while ((opt = getopt(argc, argv, "+CfimnpTuU")) != -1) { switch (opt) { case 'f': do_fork = 1; break; case 'C': flags |= CLONE_NEWCGROUP; break; case 'i': flags |= CLONE_NEWIPC; break; case 'm': flags |= CLONE_NEWNS; break; case 'n': flags |= CLONE_NEWNET; break; case 'p': flags |= CLONE_NEWPID; break; case 'T': flags |= CLONE_NEWTIME; break; case 'u': flags |= CLONE_NEWUTS; break; case 'U': flags |= CLONE_NEWUSER; break; default: usage(argv[0]); } } if (optind >= argc) usage(argv[0]); if (unshare(flags) == -1) errExit("unshare"); /* If we are unsharing the PID namespace, then the caller is *not* moved into the new namespace. Instead, only the children are moved into the namespace. Therefore, we support an option that causes the program to call fork() before executing the specified program, in order to create a new child that will be created in a new PID namespace. */ if (do_fork) { if (fork()) { wait(NULL); /* Parent waits for child to complete */ exit(EXIT_SUCCESS); } /* Child falls through to execute command */ } execvp(argv[optind], &argv[optind]); errExit("execvp"); }
Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.