[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 03/11] test-util-sockets: Clean up SocketAddress construction
From: |
Markus Armbruster |
Subject: |
[PATCH v2 03/11] test-util-sockets: Clean up SocketAddress construction |
Date: |
Mon, 2 Nov 2020 10:44:14 +0100 |
The thread functions build the SocketAddress from global variable
@abstract_sock_name and the tight flag passed as pointer
argument (either NULL or (gpointer)1). There is no need for such
hackery; simply pass the SocketAddress instead.
While there, dumb down g_rand_int_range() to g_random_int().
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
tests/test-util-sockets.c | 64 ++++++++++++++++-----------------------
1 file changed, 26 insertions(+), 38 deletions(-)
diff --git a/tests/test-util-sockets.c b/tests/test-util-sockets.c
index 9d317e73a6..a4792253ba 100644
--- a/tests/test-util-sockets.c
+++ b/tests/test-util-sockets.c
@@ -230,25 +230,15 @@ static void test_socket_fd_pass_num_nocli(void)
#endif
#ifdef __linux__
-static gchar *abstract_sock_name;
-
static gpointer unix_server_thread_func(gpointer user_data)
{
- SocketAddress addr;
- Error *err = NULL;
- int fd = -1;
- int connfd = -1;
+ SocketAddress *addr = user_data;
+ int fd;
+ int connfd;
struct sockaddr_un un;
socklen_t len = sizeof(un);
- addr.type = SOCKET_ADDRESS_TYPE_UNIX;
- addr.u.q_unix.path = abstract_sock_name;
- addr.u.q_unix.has_tight = true;
- addr.u.q_unix.tight = user_data != NULL;
- addr.u.q_unix.has_abstract = true;
- addr.u.q_unix.abstract = true;
-
- fd = socket_listen(&addr, 1, &err);
+ fd = socket_listen(addr, 1, &error_abort);
g_assert_cmpint(fd, >=, 0);
g_assert(fd_is_socket(fd));
@@ -257,69 +247,67 @@ static gpointer unix_server_thread_func(gpointer
user_data)
close(connfd);
close(fd);
-
return NULL;
}
static gpointer unix_client_thread_func(gpointer user_data)
{
- SocketAddress addr;
- Error *err = NULL;
- int fd = -1;
-
- addr.type = SOCKET_ADDRESS_TYPE_UNIX;
- addr.u.q_unix.path = abstract_sock_name;
- addr.u.q_unix.has_tight = true;
- addr.u.q_unix.tight = user_data != NULL;
- addr.u.q_unix.has_abstract = true;
- addr.u.q_unix.abstract = true;
-
- fd = socket_connect(&addr, &err);
+ SocketAddress *addr = user_data;
+ int fd;
+ fd = socket_connect(addr, &error_abort);
g_assert_cmpint(fd, >=, 0);
-
close(fd);
-
return NULL;
}
static void test_socket_unix_abstract_good(void)
{
- GRand *r = g_rand_new();
+ SocketAddress addr;
- abstract_sock_name = g_strdup_printf("unix-%d-%d", getpid(),
- g_rand_int_range(r, 100, 1000));
+ addr.type = SOCKET_ADDRESS_TYPE_UNIX;
+ addr.u.q_unix.path = g_strdup_printf("unix-%d-%u",
+ getpid(), g_random_int());
+ addr.u.q_unix.has_abstract = true;
+ addr.u.q_unix.abstract = true;
/* non tight socklen serv and cli */
+
+ addr.u.q_unix.has_tight = false;
+ addr.u.q_unix.tight = false;
+
GThread *serv = g_thread_new("abstract_unix_server",
unix_server_thread_func,
- NULL);
+ &addr);
sleep(1);
GThread *cli = g_thread_new("abstract_unix_client",
unix_client_thread_func,
- NULL);
+ &addr);
g_thread_join(cli);
g_thread_join(serv);
/* tight socklen serv and cli */
+
+ addr.u.q_unix.has_tight = true;
+ addr.u.q_unix.tight = true;
+
serv = g_thread_new("abstract_unix_server",
unix_server_thread_func,
- (gpointer)1);
+ &addr);
sleep(1);
cli = g_thread_new("abstract_unix_client",
unix_client_thread_func,
- (gpointer)1);
+ &addr);
g_thread_join(cli);
g_thread_join(serv);
- g_free(abstract_sock_name);
- g_rand_free(r);
+ g_free(addr.u.q_unix.path);
}
#endif
--
2.26.2
- [PATCH v2 00/11] sockets: Attempt to drain the abstract socket swamp, Markus Armbruster, 2020/11/02
- [PATCH v2 02/11] test-util-sockets: Correct to set has_abstract, has_tight, Markus Armbruster, 2020/11/02
- [PATCH v2 06/11] test-util-sockets: Test the complete abstract socket matrix, Markus Armbruster, 2020/11/02
- [PATCH v2 01/11] test-util-sockets: Plug file descriptor leak, Markus Armbruster, 2020/11/02
- [PATCH v2 08/11] sockets: Fix socket_sockaddr_to_address_unix() for abstract sockets, Markus Armbruster, 2020/11/02
- [PATCH v2 03/11] test-util-sockets: Clean up SocketAddress construction,
Markus Armbruster <=
- [PATCH v2 10/11] sockets: Bypass "replace empty @path" for abstract unix sockets, Markus Armbruster, 2020/11/02
- [PATCH v2 04/11] test-util-sockets: Factor out test_socket_unix_abstract_one(), Markus Armbruster, 2020/11/02
- [PATCH v2 07/11] sockets: Fix default of UnixSocketAddress member @tight, Markus Armbruster, 2020/11/02
- [PATCH v2 05/11] test-util-sockets: Synchronize properly, don't sleep(1), Markus Armbruster, 2020/11/02
- [PATCH v2 09/11] char-socket: Fix qemu_chr_socket_address() for abstract sockets, Markus Armbruster, 2020/11/02
[PATCH v2 11/11] sockets: Make abstract UnixSocketAddress depend on CONFIG_LINUX, Markus Armbruster, 2020/11/02