monitor: fix file handle leak

In some cases passing file to monitor left file open.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
This commit is contained in:
Stephen Hemminger 2015-12-30 17:19:04 -08:00
parent b27f005b27
commit e49b51d663
3 changed files with 17 additions and 5 deletions

View File

@ -284,12 +284,16 @@ int do_ipmonitor(int argc, char **argv)
} }
if (file) { if (file) {
FILE *fp; FILE *fp;
int err;
fp = fopen(file, "r"); fp = fopen(file, "r");
if (fp == NULL) { if (fp == NULL) {
perror("Cannot fopen"); perror("Cannot fopen");
exit(-1); exit(-1);
} }
return rtnl_from_file(fp, accept_msg, stdout); err = rtnl_from_file(fp, accept_msg, stdout);
fclose(fp);
return err;
} }
if (rtnl_open(&rth, groups) < 0) if (rtnl_open(&rth, groups) < 0)

View File

@ -411,12 +411,16 @@ int do_xfrm_monitor(int argc, char **argv)
if (file) { if (file) {
FILE *fp; FILE *fp;
int err;
fp = fopen(file, "r"); fp = fopen(file, "r");
if (fp == NULL) { if (fp == NULL) {
perror("Cannot fopen"); perror("Cannot fopen");
exit(-1); exit(-1);
} }
return rtnl_from_file(fp, xfrm_accept_msg, (void*)stdout); err = rtnl_from_file(fp, xfrm_accept_msg, stdout);
fclose(fp);
return err;
} }
if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0) if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0)

View File

@ -91,13 +91,17 @@ int do_tcmonitor(int argc, char **argv)
} }
if (file) { if (file) {
FILE *fp; FILE *fp = fopen(file, "r");
fp = fopen(file, "r"); int ret;
if (fp == NULL) { if (fp == NULL) {
perror("Cannot fopen"); perror("Cannot fopen");
exit(-1); exit(-1);
} }
return rtnl_from_file(fp, accept_tcmsg, (void*)stdout);
ret = rtnl_from_file(fp, accept_tcmsg, stdout);
fclose(fp);
return ret;
} }
if (rtnl_open(&rth, groups) < 0) if (rtnl_open(&rth, groups) < 0)