From bcc7daac6cac9fcabd8197073ba9bcf843a851a4 Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Tue, 4 Jul 2017 22:31:08 +0000 Subject: [PATCH] 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. --- tests/create_nl_socket.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/create_nl_socket.c b/tests/create_nl_socket.c index 064960fa..c2708d8f 100644 --- a/tests/create_nl_socket.c +++ b/tests/create_nl_socket.c @@ -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; } -- 2.50.1