Skip to content

Commit 04d1802

Browse files
mmhalNipaLocal
authored and
NipaLocal
committed
vsock/test: Expand linger test to ensure close() does not misbehave
There was an issue with SO_LINGER: instead of blocking until all queued messages for the socket have been successfully sent (or the linger timeout has been reached), close() would block until packets were handled by the peer. Add a check to alert on close() lingering when it should not. Signed-off-by: Michal Luczaj <[email protected]> Signed-off-by: NipaLocal <nipa@local>
1 parent 08dc446 commit 04d1802

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

tools/testing/vsock/vsock_test.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,13 +1788,16 @@ static void test_stream_connect_retry_server(const struct test_opts *opts)
17881788
close(fd);
17891789
}
17901790

1791+
#define LINGER_TIMEOUT 1 /* seconds */
1792+
17911793
static void test_stream_linger_client(const struct test_opts *opts)
17921794
{
17931795
struct linger optval = {
17941796
.l_onoff = 1,
1795-
.l_linger = 1
1797+
.l_linger = LINGER_TIMEOUT
17961798
};
1797-
int fd;
1799+
int bytes_unsent, fd;
1800+
time_t ts;
17981801

17991802
fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
18001803
if (fd < 0) {
@@ -1807,7 +1810,28 @@ static void test_stream_linger_client(const struct test_opts *opts)
18071810
exit(EXIT_FAILURE);
18081811
}
18091812

1813+
/* Byte left unread to expose any incorrect behaviour. */
1814+
send_byte(fd, 1, 0);
1815+
1816+
/* Reuse LINGER_TIMEOUT to wait for bytes_unsent == 0. */
1817+
timeout_begin(LINGER_TIMEOUT);
1818+
do {
1819+
if (ioctl(fd, SIOCOUTQ, &bytes_unsent) < 0) {
1820+
perror("ioctl(SIOCOUTQ)");
1821+
exit(EXIT_FAILURE);
1822+
}
1823+
timeout_check("ioctl(SIOCOUTQ) == 0");
1824+
} while (bytes_unsent != 0);
1825+
timeout_end();
1826+
1827+
ts = current_nsec();
18101828
close(fd);
1829+
if ((current_nsec() - ts) / NSEC_PER_SEC > 0) {
1830+
fprintf(stderr, "Unexpected lingering on close()\n");
1831+
exit(EXIT_FAILURE);
1832+
}
1833+
1834+
control_writeln("DONE");
18111835
}
18121836

18131837
static void test_stream_linger_server(const struct test_opts *opts)
@@ -1820,7 +1844,7 @@ static void test_stream_linger_server(const struct test_opts *opts)
18201844
exit(EXIT_FAILURE);
18211845
}
18221846

1823-
vsock_wait_remote_close(fd);
1847+
control_expectln("DONE");
18241848
close(fd);
18251849
}
18261850

0 commit comments

Comments
 (0)