ip: netns: fix missing netns close on some error paths
In functions netns_pids() and netns_identify_pid(), the netns file is
not closed on some error paths.
Fix this using a conditional close and a single return point on both
functions.
Fixes: 44b563269e ("ip-nexthop: support flush by id")
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
This commit is contained in:
parent
b5a6ed9cc9
commit
38ef5bb7b4
38
ip/ipnetns.c
38
ip/ipnetns.c
|
|
@ -579,18 +579,18 @@ static int netns_pids(int argc, char **argv)
|
||||||
{
|
{
|
||||||
const char *name;
|
const char *name;
|
||||||
char net_path[PATH_MAX];
|
char net_path[PATH_MAX];
|
||||||
int netns;
|
int netns = -1, ret = -1;
|
||||||
struct stat netst;
|
struct stat netst;
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent *entry;
|
struct dirent *entry;
|
||||||
|
|
||||||
if (argc < 1) {
|
if (argc < 1) {
|
||||||
fprintf(stderr, "No netns name specified\n");
|
fprintf(stderr, "No netns name specified\n");
|
||||||
return -1;
|
goto out;
|
||||||
}
|
}
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
fprintf(stderr, "extra arguments specified\n");
|
fprintf(stderr, "extra arguments specified\n");
|
||||||
return -1;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
name = argv[0];
|
name = argv[0];
|
||||||
|
|
@ -599,18 +599,18 @@ static int netns_pids(int argc, char **argv)
|
||||||
if (netns < 0) {
|
if (netns < 0) {
|
||||||
fprintf(stderr, "Cannot open network namespace: %s\n",
|
fprintf(stderr, "Cannot open network namespace: %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return -1;
|
goto out;
|
||||||
}
|
}
|
||||||
if (fstat(netns, &netst) < 0) {
|
if (fstat(netns, &netst) < 0) {
|
||||||
fprintf(stderr, "Stat of netns failed: %s\n",
|
fprintf(stderr, "Stat of netns failed: %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return -1;
|
goto out;
|
||||||
}
|
}
|
||||||
dir = opendir("/proc/");
|
dir = opendir("/proc/");
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
fprintf(stderr, "Open of /proc failed: %s\n",
|
fprintf(stderr, "Open of /proc failed: %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return -1;
|
goto out;
|
||||||
}
|
}
|
||||||
while ((entry = readdir(dir))) {
|
while ((entry = readdir(dir))) {
|
||||||
char pid_net_path[PATH_MAX];
|
char pid_net_path[PATH_MAX];
|
||||||
|
|
@ -627,15 +627,19 @@ static int netns_pids(int argc, char **argv)
|
||||||
printf("%s\n", entry->d_name);
|
printf("%s\n", entry->d_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ret = 0;
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
return 0;
|
out:
|
||||||
|
if (netns >= 0)
|
||||||
|
close(netns);
|
||||||
|
return ret;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int netns_identify_pid(const char *pidstr, char *name, int len)
|
int netns_identify_pid(const char *pidstr, char *name, int len)
|
||||||
{
|
{
|
||||||
char net_path[PATH_MAX];
|
char net_path[PATH_MAX];
|
||||||
int netns;
|
int netns = -1, ret = -1;
|
||||||
struct stat netst;
|
struct stat netst;
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent *entry;
|
struct dirent *entry;
|
||||||
|
|
@ -647,22 +651,24 @@ int netns_identify_pid(const char *pidstr, char *name, int len)
|
||||||
if (netns < 0) {
|
if (netns < 0) {
|
||||||
fprintf(stderr, "Cannot open network namespace: %s\n",
|
fprintf(stderr, "Cannot open network namespace: %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return -1;
|
goto out;
|
||||||
}
|
}
|
||||||
if (fstat(netns, &netst) < 0) {
|
if (fstat(netns, &netst) < 0) {
|
||||||
fprintf(stderr, "Stat of netns failed: %s\n",
|
fprintf(stderr, "Stat of netns failed: %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return -1;
|
goto out;
|
||||||
}
|
}
|
||||||
dir = opendir(NETNS_RUN_DIR);
|
dir = opendir(NETNS_RUN_DIR);
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
/* Succeed treat a missing directory as an empty directory */
|
/* Succeed treat a missing directory as an empty directory */
|
||||||
if (errno == ENOENT)
|
if (errno == ENOENT) {
|
||||||
return 0;
|
ret = 0;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
fprintf(stderr, "Failed to open directory %s:%s\n",
|
fprintf(stderr, "Failed to open directory %s:%s\n",
|
||||||
NETNS_RUN_DIR, strerror(errno));
|
NETNS_RUN_DIR, strerror(errno));
|
||||||
return -1;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((entry = readdir(dir))) {
|
while ((entry = readdir(dir))) {
|
||||||
|
|
@ -685,8 +691,12 @@ int netns_identify_pid(const char *pidstr, char *name, int len)
|
||||||
strlcpy(name, entry->d_name, len);
|
strlcpy(name, entry->d_name, len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ret = 0;
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
return 0;
|
out:
|
||||||
|
if (netns >= 0)
|
||||||
|
close(netns);
|
||||||
|
return ret;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue