diff mbox series

[07/11] selftests: nettest: Implement -k to fork after bind or listen

Message ID 6c035c083cedd0ccf06932344d33c867763d6762.1633520807.git.cdleonard@gmail.com (mailing list archive)
State New
Headers show
Series selftests: Improve nettest and net/fcnal-test.sh | expand

Commit Message

Leonard Crestez Oct. 6, 2021, 11:47 a.m. UTC
The fcnal-test.sh script launches the nettest server in the background
and sleeps for 1 second to ensure that it has time to get up.

Add an option to nettest to fork into the background as soon as packets
are accepted by the kernel, this means bind() for datagrams or listen()
for TCP.

Signed-off-by: Leonard Crestez <cdleonard@gmail.com>
---
 tools/testing/selftests/net/nettest.c | 34 ++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/tools/testing/selftests/net/nettest.c b/tools/testing/selftests/net/nettest.c
index bd6288302094..576d8bb4c94c 100644
--- a/tools/testing/selftests/net/nettest.c
+++ b/tools/testing/selftests/net/nettest.c
@@ -73,10 +73,11 @@  struct sock_args {
 	unsigned int has_local_ip:1,
 		     has_remote_ip:1,
 		     has_grp:1,
 		     has_expected_laddr:1,
 		     has_expected_raddr:1,
+		     fork_background:1,
 		     bind_test_only:1;
 
 	unsigned short port;
 
 	int type;      /* DGRAM, STREAM, RAW */
@@ -1388,10 +1389,29 @@  static int config_xfrm_policy(int sd, struct sock_args *args)
 	}
 
 	return 0;
 }
 
+static void handle_fork_background(struct sock_args *args)
+{
+	pid_t fork_result;
+	int result;
+
+	if (!args->fork_background)
+		return;
+
+	fork_result = fork();
+	if (fork_result)
+		exit(0);
+	result = setpgid(0, 0);
+	if (result) {
+		log_err_errno("Failed setpgid");
+		exit(1);
+	}
+	log_msg("server running in background\n");
+}
+
 static int lsock_init(struct sock_args *args)
 {
 	long flags;
 	int sd;
 
@@ -1422,10 +1442,12 @@  static int lsock_init(struct sock_args *args)
 	if (args->type == SOCK_STREAM && listen(sd, 1) < 0) {
 		log_err_errno("listen failed");
 		goto err;
 	}
 
+	handle_fork_background(args);
+
 	flags = fcntl(sd, F_GETFL);
 	if ((flags < 0) || (fcntl(sd, F_SETFL, flags|O_NONBLOCK) < 0)) {
 		log_err_errno("Failed to set non-blocking option");
 		goto err;
 	}
@@ -1819,11 +1841,11 @@  static int ipc_parent(int cpid, int fd, struct sock_args *args)
 
 	wait(&status);
 	return client_status;
 }
 
-#define GETOPT_STR  "sr:l:c:p:t:g:P:DRn:M:X:m:d:I:BN:O:SCi6xL:0:1:2:3:Fbq"
+#define GETOPT_STR  "sr:l:c:p:t:g:P:DRn:M:X:m:d:I:BN:O:SCi6xL:0:1:2:3:Fbqk"
 
 static void print_usage(char *prog)
 {
 	printf(
 	"usage: %s OPTS\n"
@@ -1866,10 +1888,11 @@  static void print_usage(char *prog)
 	"    -2 dev        Expected device name (or index) to receive packet\n"
 	"    -3 dev        Expected device name (or index) to receive packets - server mode\n"
 	"\n"
 	"    -b            Bind test only.\n"
 	"    -q            Be quiet. Run test without printing anything.\n"
+	"    -k            Fork server in background after bind or listen.\n"
 	, prog, DEFAULT_PORT);
 }
 
 int main(int argc, char *argv[])
 {
@@ -2017,10 +2040,13 @@  int main(int argc, char *argv[])
 			quiet = 1;
 			break;
 		case 'x':
 			args.use_xfrm = 1;
 			break;
+		case 'k':
+			args.fork_background = 1;
+			break;
 		default:
 			print_usage(argv[0]);
 			return 1;
 		}
 	}
@@ -2058,10 +2084,16 @@  int main(int argc, char *argv[])
 		fprintf(stderr,
 			"Local (server mode) or remote IP (client IP) required\n");
 		return 1;
 	}
 
+	if (args.fork_background && (both_mode || !server_mode)) {
+		fprintf(stderr,
+			"Fork after listen only supported for server mode\n");
+		return 1;
+	}
+
 	if (interactive) {
 		prog_timeout = 0;
 		msg = NULL;
 	}