tests: robustify create_nl_socket based tests against the race condition

There is a race condition between the moment a netlink socket is created
and the moment it is reported via SOCK_DIAG_BY_FAMILY interface.
Add one more operation on the socket created by create_nl_socket
to increase chances of winning the race.

* tests/create_nl_socket.c (create_nl_socket_ext): Add a getsockopt call
with the created socket.
This commit is contained in:
2017-07-04 22:31:08 +00:00
parent e70dae5954
commit bcc7daac6c

View File

@@ -37,10 +37,16 @@ create_nl_socket_ext(const int proto, const char *const name)
perror_msg_and_skip("socket AF_NETLINK %s", name);
const struct sockaddr_nl addr = { .nl_family = AF_NETLINK };
const socklen_t len = sizeof(addr);
socklen_t len = sizeof(addr);
if (bind(fd, (const struct sockaddr *) &addr, len))
perror_msg_and_skip("bind AF_NETLINK %s", name);
/* one more operation on this socket to win the race */
int listening;
len = sizeof(listening);
if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &listening, &len))
perror_msg_and_fail("getsockopt");
return fd;
}