diff mbox series

syscalls/accept4_01: don't hardcode port number for test

Message ID 6a56618714e577c437a489a6050e3d29c2236022.1658842322.git.jstancek@redhat.com
State Accepted
Headers show
Series syscalls/accept4_01: don't hardcode port number for test | expand

Commit Message

Jan Stancek July 26, 2022, 1:33 p.m. UTC
Leave it to bind() to pick a free port number.

Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
 testcases/kernel/syscalls/accept4/accept4_01.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

Comments

Petr Vorel July 26, 2022, 4:22 p.m. UTC | #1
> Leave it to bind() to pick a free port number.
IMHO more common use case => +1

Reviewed-by: Petr Vorel <pvorel@suse.cz>

Kind regards,
Petr
Jan Stancek Aug. 10, 2022, 8:40 a.m. UTC | #2
On Tue, Jul 26, 2022 at 6:23 PM Petr Vorel <pvorel@suse.cz> wrote:
>
> > Leave it to bind() to pick a free port number.
> IMHO more common use case => +1
>
> Reviewed-by: Petr Vorel <pvorel@suse.cz>

Thanks, pushed.

>
> Kind regards,
> Petr
>
diff mbox series

Patch

diff --git a/testcases/kernel/syscalls/accept4/accept4_01.c b/testcases/kernel/syscalls/accept4/accept4_01.c
index 58115ea43213..b2f785d0a6aa 100644
--- a/testcases/kernel/syscalls/accept4/accept4_01.c
+++ b/testcases/kernel/syscalls/accept4/accept4_01.c
@@ -22,8 +22,6 @@ 
 #include "lapi/fcntl.h"
 #include "lapi/syscalls.h"
 
-#define PORT_NUM 33333
-
 static const char *variant_desc[] = {
 	"libc accept4()",
 	"__NR_accept4 syscall",
@@ -54,7 +52,7 @@  static int create_listening_socket(void)
 	memset(&svaddr, 0, sizeof(struct sockaddr_in));
 	svaddr.sin_family = AF_INET;
 	svaddr.sin_addr.s_addr = htonl(INADDR_ANY);
-	svaddr.sin_port = htons(PORT_NUM);
+	svaddr.sin_port = 0;
 
 	lfd = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
 
@@ -68,14 +66,16 @@  static int create_listening_socket(void)
 
 static void setup(void)
 {
+	socklen_t slen = sizeof(*conn_addr);
+
 	tst_res(TINFO, "Testing variant: %s", variant_desc[tst_variant]);
 
+	listening_fd = create_listening_socket();
+
 	memset(conn_addr, 0, sizeof(*conn_addr));
-	conn_addr->sin_family = AF_INET;
+	SAFE_GETSOCKNAME(listening_fd, (struct sockaddr *)conn_addr, &slen);
 	conn_addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-	conn_addr->sin_port = htons(PORT_NUM);
-
-	listening_fd = create_listening_socket();
+	tst_res(TINFO, "server listening on: %d", ntohs(conn_addr->sin_port));
 }
 
 static void cleanup(void)