lib/namespace: fix fd leakage in non-error case
My previous patch5950ba9("lib/namespace: don't leak fd in error case") was a step in the wrong direction. Instead of closing the opened file descriptor in error case only, follow a better approach here and close the fd as soon as it is not used anymore. This way the inelegant goto statements can be dropped, and the fd leak in non-error case is fixed as well. Fixes:5950ba9("lib/namespace: don't leak fd in error case") Signed-off-by: Phil Sutter <phil@nwl.cc>
This commit is contained in:
parent
6843d36e3d
commit
9e5ba07f49
|
|
@ -58,35 +58,34 @@ int netns_switch(char *name)
|
||||||
if (setns(netns, CLONE_NEWNET) < 0) {
|
if (setns(netns, CLONE_NEWNET) < 0) {
|
||||||
fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n",
|
fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n",
|
||||||
name, strerror(errno));
|
name, strerror(errno));
|
||||||
goto fail_close;
|
close(netns);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
close(netns);
|
||||||
|
|
||||||
if (unshare(CLONE_NEWNS) < 0) {
|
if (unshare(CLONE_NEWNS) < 0) {
|
||||||
fprintf(stderr, "unshare failed: %s\n", strerror(errno));
|
fprintf(stderr, "unshare failed: %s\n", strerror(errno));
|
||||||
goto fail_close;
|
return -1;
|
||||||
}
|
}
|
||||||
/* Don't let any mounts propagate back to the parent */
|
/* Don't let any mounts propagate back to the parent */
|
||||||
if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) {
|
if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) {
|
||||||
fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n",
|
fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
goto fail_close;
|
return -1;
|
||||||
}
|
}
|
||||||
/* Mount a version of /sys that describes the network namespace */
|
/* Mount a version of /sys that describes the network namespace */
|
||||||
if (umount2("/sys", MNT_DETACH) < 0) {
|
if (umount2("/sys", MNT_DETACH) < 0) {
|
||||||
fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno));
|
fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno));
|
||||||
goto fail_close;
|
return -1;
|
||||||
}
|
}
|
||||||
if (mount(name, "/sys", "sysfs", 0, NULL) < 0) {
|
if (mount(name, "/sys", "sysfs", 0, NULL) < 0) {
|
||||||
fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno));
|
fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno));
|
||||||
goto fail_close;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Setup bind mounts for config files in /etc */
|
/* Setup bind mounts for config files in /etc */
|
||||||
bind_etc(name);
|
bind_etc(name);
|
||||||
return 0;
|
return 0;
|
||||||
fail_close:
|
|
||||||
close(netns);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int netns_get_fd(const char *name)
|
int netns_get_fd(const char *name)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue