selftests/bpf: test_progs: fix client/server race in tcp_rtt
[linux-2.6-block.git] / tools / testing / selftests / bpf / prog_tests / tcp_rtt.c
index fdc0b3614a9e13afb97708297899364ef9e8cb9d..a82da555b1b0217af5c1d84faaeb8b08835c006e 100644 (file)
@@ -203,14 +203,24 @@ static int start_server(void)
        return fd;
 }
 
+static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
+
 static void *server_thread(void *arg)
 {
        struct sockaddr_storage addr;
        socklen_t len = sizeof(addr);
        int fd = *(int *)arg;
        int client_fd;
+       int err;
+
+       err = listen(fd, 1);
+
+       pthread_mutex_lock(&server_started_mtx);
+       pthread_cond_signal(&server_started);
+       pthread_mutex_unlock(&server_started_mtx);
 
-       if (CHECK_FAIL(listen(fd, 1)) < 0) {
+       if (CHECK_FAIL(err < 0)) {
                perror("Failed to listed on socket");
                return NULL;
        }
@@ -248,7 +258,14 @@ void test_tcp_rtt(void)
        if (CHECK_FAIL(server_fd < 0))
                goto close_cgroup_fd;
 
-       pthread_create(&tid, NULL, server_thread, (void *)&server_fd);
+       if (CHECK_FAIL(pthread_create(&tid, NULL, server_thread,
+                                     (void *)&server_fd)))
+               goto close_cgroup_fd;
+
+       pthread_mutex_lock(&server_started_mtx);
+       pthread_cond_wait(&server_started, &server_started_mtx);
+       pthread_mutex_unlock(&server_started_mtx);
+
        CHECK_FAIL(run_test(cgroup_fd, server_fd));
        close(server_fd);
 close_cgroup_fd: