]> granicus.if.org Git - strace/commitdiff
tests: robustify create_nl_socket based tests against the race condition
authorDmitry V. Levin <ldv@altlinux.org>
Tue, 4 Jul 2017 22:31:08 +0000 (22:31 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Tue, 4 Jul 2017 22:31:08 +0000 (22:31 +0000)
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

index 064960fa5947e47589d5ea51588b169c1819c52f..c2708d8f4e2ddce863a9e2f6193237c89a71ea7f 100644 (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;
 }