Skip to content

Instantly share code, notes, and snippets.

@l-margiela
Created July 15, 2017 18:21
Show Gist options
  • Save l-margiela/3a2475b13146af4ee039782f7f502684 to your computer and use it in GitHub Desktop.
Save l-margiela/3a2475b13146af4ee039782f7f502684 to your computer and use it in GitHub Desktop.
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