Commit b8b435e3 by Wolfgang Bumiller Committed by Stéphane Graber

netlink_open: close socket on error

All uses of netlink_open() assume that on error the nl_handler doesn't need to be closed, but some error cases happen after the socket was opened successfully and used to simply return -errno. Signed-off-by: 's avatarWolfgang Bumiller <w.bumiller@proxmox.com>
parent 3b860b72
...@@ -265,6 +265,7 @@ extern int netlink_open(struct nl_handler *handler, int protocol) ...@@ -265,6 +265,7 @@ extern int netlink_open(struct nl_handler *handler, int protocol)
socklen_t socklen; socklen_t socklen;
int sndbuf = 32768; int sndbuf = 32768;
int rcvbuf = 32768; int rcvbuf = 32768;
int err;
memset(handler, 0, sizeof(*handler)); memset(handler, 0, sizeof(*handler));
...@@ -274,11 +275,11 @@ extern int netlink_open(struct nl_handler *handler, int protocol) ...@@ -274,11 +275,11 @@ extern int netlink_open(struct nl_handler *handler, int protocol)
if (setsockopt(handler->fd, SOL_SOCKET, SO_SNDBUF, if (setsockopt(handler->fd, SOL_SOCKET, SO_SNDBUF,
&sndbuf, sizeof(sndbuf)) < 0) &sndbuf, sizeof(sndbuf)) < 0)
return -errno; goto err_with_errno;
if (setsockopt(handler->fd, SOL_SOCKET, SO_RCVBUF, if (setsockopt(handler->fd, SOL_SOCKET, SO_RCVBUF,
&rcvbuf,sizeof(rcvbuf)) < 0) &rcvbuf,sizeof(rcvbuf)) < 0)
return -errno; goto err_with_errno;
memset(&handler->local, 0, sizeof(handler->local)); memset(&handler->local, 0, sizeof(handler->local));
handler->local.nl_family = AF_NETLINK; handler->local.nl_family = AF_NETLINK;
...@@ -286,22 +287,31 @@ extern int netlink_open(struct nl_handler *handler, int protocol) ...@@ -286,22 +287,31 @@ extern int netlink_open(struct nl_handler *handler, int protocol)
if (bind(handler->fd, (struct sockaddr*)&handler->local, if (bind(handler->fd, (struct sockaddr*)&handler->local,
sizeof(handler->local)) < 0) sizeof(handler->local)) < 0)
return -errno; goto err_with_errno;
socklen = sizeof(handler->local); socklen = sizeof(handler->local);
if (getsockname(handler->fd, (struct sockaddr*)&handler->local, if (getsockname(handler->fd, (struct sockaddr*)&handler->local,
&socklen) < 0) &socklen) < 0)
return -errno; goto err_with_errno;
if (socklen != sizeof(handler->local)) if (socklen != sizeof(handler->local)) {
return -EINVAL; err = -EINVAL;
goto errclose;
}
if (handler->local.nl_family != AF_NETLINK) if (handler->local.nl_family != AF_NETLINK) {
return -EINVAL; err = -EINVAL;
goto errclose;
}
handler->seq = time(NULL); handler->seq = time(NULL);
return 0; return 0;
err_with_errno:
err = -errno;
errclose:
close(handler->fd);
return err;
} }
extern int netlink_close(struct nl_handler *handler) extern int netlink_close(struct nl_handler *handler)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment