Created
July 15, 2017 18:21
-
-
Save l-margiela/3a2475b13146af4ee039782f7f502684 to your computer and use it in GitHub Desktop.
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
int netns_switch(char *name) | |
{ | |
char net_path[PATH_MAX]; | |
int netns; | |
snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name); | |
netns = open(net_path, O_RDONLY | O_CLOEXEC); | |
if (netns < 0) { | |
fprintf(stderr, "Cannot open network namespace \"%s\": %s\n", | |
name, strerror(errno)); | |
return -1; | |
} | |
if (setns(netns, CLONE_NEWNET) < 0) { | |
fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n", | |
name, strerror(errno)); | |
close(netns); | |
return -1; | |
} | |
close(netns); | |
if (unshare(CLONE_NEWNS) < 0) { | |
fprintf(stderr, "unshare failed: %s\n", strerror(errno)); | |
return -1; | |
} | |
[…] | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment