This may happen when passing connected socket to nl_cache_mngr_alloc().
Now, nl_connect() will return error trying to connect already connected socket.
Also, dont call close(-1) if socket() fails.
* Creates a netlink socket using the specified protocol, binds the socket
* and issues a connection attempt.
*
+ * This function fail if socket is already connected.
+ *
* @note SOCK_CLOEXEC is set on the socket if available.
*
* @return 0 on success or a negative error code.
flags |= SOCK_CLOEXEC;
#endif
+ if (sk->s_fd != -1)
+ return -NLE_BAD_SOCK;
+
sk->s_fd = socket(AF_NETLINK, SOCK_RAW | flags, protocol);
if (sk->s_fd < 0) {
err = -nl_syserr2nlerr(errno);
return 0;
errout:
- close(sk->s_fd);
- sk->s_fd = -1;
+ if (sk->s_fd != -1) {
+ close(sk->s_fd);
+ sk->s_fd = -1;
+ }
return err;
}