Created
February 25, 2015 03:04
-
-
Save gdestuynder/203aea49cf901c078cb2 to your computer and use it in GitHub Desktop.
ns_exec.c
This file contains hidden or 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
/* ns_exec.c | |
Copyright 2013, Michael Kerrisk | |
Licensed under GNU General Public License v2 or later | |
Join a namespace and execute a command in the namespace | |
*/ | |
#define _GNU_SOURCE | |
#include <fcntl.h> | |
#include <sched.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
/* 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) | |
int | |
main(int argc, char *argv[]) | |
{ | |
int fd; | |
if (argc < 3) { | |
fprintf(stderr, "%s /proc/PID/ns/FILE cmd [arg...]\n", argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
fd = open(argv[1], O_RDONLY); /* Get descriptor for namespace */ | |
if (fd == -1) | |
errExit("open"); | |
if (setns(fd, 0) == -1) /* Join that namespace */ | |
errExit("setns"); | |
execvp(argv[2], &argv[2]); /* Execute a command in namespace */ | |
errExit("execvp"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment