vsock/test: Introduce enable_so_linger() helper
authorMichal Luczaj <mhal@rbox.co>
Wed, 21 May 2025 23:18:24 +0000 (01:18 +0200)
committerPaolo Abeni <pabeni@redhat.com>
Tue, 27 May 2025 09:05:21 +0000 (11:05 +0200)
Add a helper function that sets SO_LINGER. Adapt the caller.

Signed-off-by: Michal Luczaj <mhal@rbox.co>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Link: https://patch.msgid.link/20250522-vsock-linger-v6-4-2ad00b0e447e@rbox.co
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
tools/testing/vsock/util.c
tools/testing/vsock/util.h
tools/testing/vsock/vsock_test.c

index 4427d45..0c7e9cb 100644 (file)
@@ -823,3 +823,16 @@ void enable_so_zerocopy_check(int fd)
        setsockopt_int_check(fd, SOL_SOCKET, SO_ZEROCOPY, 1,
                             "setsockopt SO_ZEROCOPY");
 }
+
+void enable_so_linger(int fd, int timeout)
+{
+       struct linger optval = {
+               .l_onoff = 1,
+               .l_linger = timeout
+       };
+
+       if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &optval, sizeof(optval))) {
+               perror("setsockopt(SO_LINGER)");
+               exit(EXIT_FAILURE);
+       }
+}
index 91f9df1..5e2db67 100644 (file)
@@ -80,4 +80,5 @@ void setsockopt_int_check(int fd, int level, int optname, int val,
 void setsockopt_timeval_check(int fd, int level, int optname,
                              struct timeval val, char const *errmsg);
 void enable_so_zerocopy_check(int fd);
+void enable_so_linger(int fd, int timeout);
 #endif /* UTIL_H */
index 9d3a77b..b3258d6 100644 (file)
@@ -1813,10 +1813,6 @@ static void test_stream_connect_retry_server(const struct test_opts *opts)
 
 static void test_stream_linger_client(const struct test_opts *opts)
 {
-       struct linger optval = {
-               .l_onoff = 1,
-               .l_linger = 1
-       };
        int fd;
 
        fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
@@ -1825,11 +1821,7 @@ static void test_stream_linger_client(const struct test_opts *opts)
                exit(EXIT_FAILURE);
        }
 
-       if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &optval, sizeof(optval))) {
-               perror("setsockopt(SO_LINGER)");
-               exit(EXIT_FAILURE);
-       }
-
+       enable_so_linger(fd, 1);
        close(fd);
 }