Skip to contentSkip to conversation

splice02: Generate input in C

PATCH New Petr Vorel 2 participants 9 messages Mar 8, 2021 – Mar 9, 2021

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH 1/1 splice02: Generate input in C

instead relying on shell piping data into it.

Found on SLES JeOS (https://progress.opensuse.org/issues/77260).

Also add metadata docs.

---
Hi,

Hope using TST_CHECKPOINT_*() correctly. Not using tst_tag for such old kernel.

Kind regards,
Petr

 runtest/smoketest                           |  2 +-
 runtest/syscalls                            |  2 +-
 testcases/kernel/syscalls/splice/splice02.c | 67 ++++++++++++++++++++-
 3 files changed, 66 insertions(+), 5 deletions(-)
runtest/smoketest +1 -1
index 0c24fc1fa..ec0c088cb 100644
@@ -11,5 +11,5 @@ symlink01 symlink01
 stat04 symlink01 -T stat04
 utime01A symlink01 -T utime01
 rename01A symlink01 -T rename01
-splice02 seq 1 20 | splice02
+splice02 splice02 -n 20
 route4-change-dst route-change-dst.sh
runtest/syscalls +1 -1
index fe22ae5b6..355e71144 100644
@@ -1446,7 +1446,7 @@ socketpair02 socketpair02
 sockioctl01 sockioctl01
 
 splice01 splice01
-splice02 seq 1 20000 | splice02
+splice02 splice02 -n 20000
 splice03 splice03
 splice04 splice04
 splice05 splice05
testcases/kernel/syscalls/splice/splice02.c +64 -3

94 lines not on this page · raw message

Signature · 1 line
2.30.1
#1 lore raw

PATCH 1/1 splice02: Generate input in C

Hi!

runtest/smoketest symlink01 symlink01 · line 14

Quoted 10 lines
runtest/smoketest +1 -1
index 0c24fc1fa..ec0c088cb 100644
@@ -11,5 +11,5 @@ symlink01 symlink01
 stat04 symlink01 -T stat04
 utime01A symlink01 -T utime01
 rename01A symlink01 -T rename01
-splice02 seq 1 20 | splice02
+splice02 splice02 -n 20

I do wonder if this should be replaced with something that includes a shell pipe instead. It has been selected here to make sure we pass the command line correctly to a shell interpreter.

Maybe something as:

shell_test01 echo "SUCCESS" | cat

runtest/syscalls socketpair02 socketpair02 · line 1449

Quoted 11 lines
runtest/smoketest +0 -0
 route4-change-dst route-change-dst.sh
runtest/syscalls +1 -1
index fe22ae5b6..355e71144 100644
@@ -1446,7 +1446,7 @@ socketpair02 socketpair02
 sockioctl01 sockioctl01
 
 splice01 splice01
-splice02 seq 1 20000 | splice02
+splice02 splice02 -n 20000

Don't we default to 20000 in the test anyway? What is the point of passing the default value here?

Quoted 38 lines
runtest/syscalls +0 -0
 splice03 splice03
 splice04 splice04
 splice05 splice05
testcases/kernel/syscalls/splice/splice02.c +17 -1
index b579667b9..6e183ac7e 100644
@@ -1,9 +1,24 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) Jens Axboe <[email protected]>, 2009
- * http://lkml.org/lkml/2009/4/2/55
+ * Copyright (c) 2021 Petr Vorel <[email protected]>
  */
 
+/*\
+ * [DESCRIPTION]
+ * Original reproducer for kernel fix
+ * bf40d3435caf NFS: add support for splice writes
+ * from v2.6.31-rc1.
+ *
+ * http://lkml.org/lkml/2009/4/2/55
+ *
+ * [ALGORITHM]
+ * - create pipe
+ * - fork(), child replace stdin with pipe
+ * - parent write to pipe
+ * - child slice from pipe
+\*/
+
 #define _GNU_SOURCE
 
 #include <stdio.h>
@@ -15,11 +30,27 @@
 #include "lapi/splice.h"
 
 #define SPLICE_SIZE (64*1024)
+#define DEFAULT_NARG 20000

testcases/kernel/syscalls/splice/splice02.c line 42

Quoted 10 lines
testcases/kernel/syscalls/splice/splice02.c +8 -1
 
-static void splice_test(void)
+static char *narg;
+static int num = DEFAULT_NARG;
+static int pipe_fd[2];
+
+static void setup(void)
+{
+	if (tst_parse_int(narg, &num, 1, INT_MAX))
+		tst_brk(TBROK, "invalid number of input '%s'", narg);

^
That does not sound english

Maybe "Invalid number of writes" or "Invalid size" something along these lines.

testcases/kernel/syscalls/splice/splice02.c line 51

Quoted 9 lines
testcases/kernel/syscalls/splice/splice02.c +6 -0
+}
+
+static void do_child(void)
 {
 	int fd;
 
+	SAFE_CLOSE(pipe_fd[1]);
+	close(STDIN_FILENO);
+	SAFE_DUP2(pipe_fd[0], STDIN_FILENO);

dup2() closed the newfd, no need to close STDIN_FILENO here.

Quoted 17 lines
testcases/kernel/syscalls/splice/splice02.c +10 -0
+	TST_CHECKPOINT_WAIT(0);
 	fd = SAFE_OPEN("splice02-temp", O_WRONLY | O_CREAT | O_TRUNC, 0644);
 
 	TEST(splice(STDIN_FILENO, NULL, fd, NULL, SPLICE_SIZE, 0));
@@ -31,10 +62,40 @@ static void splice_test(void)
 	}
 
 	SAFE_CLOSE(fd);
+
+	exit(0);
+}
+
+static void run(void)
+{
+	int i;
+
+	SAFE_PIPE(pipe_fd);

testcases/kernel/syscalls/splice/splice02.c static void splice_test(void) · line 83

Quoted 10 lines
testcases/kernel/syscalls/splice/splice02.c +10 -0
+
+	if (SAFE_FORK())
+		do_child();
+
+	tst_res(TINFO, "writting %d times", num);
+
+	for (i = 0; i < num; i++)
+		SAFE_WRITE(1, pipe_fd[1], "x", 1);
+
+	TST_CHECKPOINT_WAKE(0);

I guess that the test will timeout if the -n parameter is greater than maximal pipe capacity, since the write would end up blocking.

Note that in the original test excess data was simply ignored.

If we wanted to increase the test coverage we could change the child to splice in a loop with a proper offset until all data are written. After that no synchronization would be required. Then we could check if we ended up with a right file size and if the content is correct as well.

Quoted 6 lines
testcases/kernel/syscalls/splice/splice02.c +4 -0
+	tst_reap_children();
+
+	SAFE_CLOSE(pipe_fd[0]);
+	SAFE_CLOSE(pipe_fd[1]);
 }
 

testcases/kernel/syscalls/splice/splice02.c static void splice_test(void) · line 98

Quoted 10 lines
testcases/kernel/syscalls/splice/splice02.c +6 -1
 static struct tst_test test = {
-	.test_all = splice_test,
+	.test_all = run,
+	.setup = setup,
+	.needs_checkpoints = 1,
 	.needs_tmpdir = 1,
+	.forks_child = 1,
 	.min_kver = "2.6.17",
+	.options = (struct tst_option[]) {
+		{"n:", &narg, "-n x     Number of input"},

Here as well.

Quoted 6 lines
testcases/kernel/syscalls/splice/splice02.c +2 -0
+		{}
+	},
 };
Signature · 1 line
2.30.1
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#2 lore raw

PATCH 1/1 splice02: Generate input in C

Hi Cyril,

thanks for your review!

Quoted 7 lines

I do wonder if this should be replaced with something that includes a shell pipe instead. It has been selected here to make sure we pass the command line correctly to a shell interpreter.

Maybe something as:

shell_test01 echo "SUCCESS" | cat

I guess you mean to add another test to cover shell pipe. Makes sense to me, but I'd wrap it to a test file, e.g. something like:

cat shell01.sh
#!/bin/sh
TST_TESTFUNC=do_test
TST_NEEDS_CMDS="cat"
. tst_test.sh

do_test()

{
	EXPECT_PASS [ "$(echo 'SUCCESS' | cat)" = "SUCCESS" ]
}

tst_run

Quoted 7 lines

runtest/syscalls socketpair02 socketpair02 · line 1449

Quoted 4 lines

+++ b/runtest/syscalls
splice01 splice01

runtest/syscalls +1 -1
-splice02 seq 1 20000 | splice02
+splice02 splice02 -n 20000

Don't we default to 20000 in the test anyway? What is the point of passing the default value here?

+1

...

Quoted 9 lines

testcases/kernel/syscalls/splice/splice02.c line 42

Quoted 4 lines
testcases/kernel/syscalls/splice/splice02.c +4 -0
+static void setup(void)
+{
+	if (tst_parse_int(narg, &num, 1, INT_MAX))
+		tst_brk(TBROK, "invalid number of input '%s'", narg);

^
That does not sound english

Maybe "Invalid number of writes" or "Invalid size" something along these lines.

+1 (before it was invalid number of input lines, but then I removed \n).

...

Quoted 5 lines

testcases/kernel/syscalls/splice/splice02.c line 51

Quoted 3 lines
testcases/kernel/syscalls/splice/splice02.c +3 -0
+	SAFE_CLOSE(pipe_fd[1]);
+	close(STDIN_FILENO);
+	SAFE_DUP2(pipe_fd[0], STDIN_FILENO);

dup2() closed the newfd, no need to close STDIN_FILENO here.

+1.

...

Quoted 10 lines
Quoted 10 lines
testcases/kernel/syscalls/splice/splice02.c +10 -0
+static void run(void)
+{
+	int i;
+
+	SAFE_PIPE(pipe_fd);
+
+	if (SAFE_FORK())
+		do_child();
+
+	tst_res(TINFO, "writting %d times", num);
Quoted 8 lines

testcases/kernel/syscalls/splice/splice02.c static void splice_test(void) · line 83

Quoted 5 lines
testcases/kernel/syscalls/splice/splice02.c +5 -0
+
+	for (i = 0; i < num; i++)
+		SAFE_WRITE(1, pipe_fd[1], "x", 1);
+
+	TST_CHECKPOINT_WAKE(0);

I guess that the test will timeout if the -n parameter is greater than maximal pipe capacity, since the write would end up blocking.

Yes. I could use fcntl(pipe_fd[1], F_GETPIPE_SZ)
(16 pages, i.e. 65536 on my system).
But with changes you suggest below we don't have to bother about F_GETPIPE_SZ. And I guess having more for regular test (to block) does not give more test coverage, right?

Quoted 1 line

Note that in the original test excess data was simply ignored.

I noticed that as well, considered it as a bug. But probably it was enough for the original reproducer.

Quoted 4 lines

If we wanted to increase the test coverage we could change the child to splice in a loop with a proper offset until all data are written. After that no synchronization would be required. Then we could check if we ended up with a right file size and if the content is correct as well.

Sounds good, I'll try.

Quoted 4 lines

testcases/kernel/syscalls/splice/splice02.c static void splice_test(void) · line 98

Quoted 2 lines
testcases/kernel/syscalls/splice/splice02.c +2 -0
+	.options = (struct tst_option[]) {
+		{"n:", &narg, "-n x     Number of input"},

Here as well.

+1

Kind regards,
Petr

#3 lore raw

PATCH 1/1 splice02: Generate input in C

Hi!

Quoted 17 lines
Quoted 7 lines

I do wonder if this should be replaced with something that includes a shell pipe instead. It has been selected here to make sure we pass the command line correctly to a shell interpreter.

Maybe something as:

shell_test01 echo "SUCCESS" | cat

I guess you mean to add another test to cover shell pipe. Makes sense to me, but I'd wrap it to a test file, e.g. something like:

cat shell01.sh
#!/bin/sh
TST_TESTFUNC=do_test
TST_NEEDS_CMDS="cat"
. tst_test.sh

Quoted 6 lines

do_test()

{
	EXPECT_PASS [ "$(echo 'SUCCESS' | cat)" = "SUCCESS" ]
}

tst_run

That would not work, the pipe is supposed to be in the runtest file.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#4 lore raw

PATCH 1/1 splice02: Generate input in C

Hi Cyril,

Quoted 18 lines

Hi!

Quoted 16 lines
Quoted 7 lines

I do wonder if this should be replaced with something that includes a shell pipe instead. It has been selected here to make sure we pass the command line correctly to a shell interpreter.

Maybe something as:

shell_test01 echo "SUCCESS" | cat

I guess you mean to add another test to cover shell pipe. Makes sense to me, but I'd wrap it to a test file, e.g. something like:

cat shell01.sh
#!/bin/sh
TST_TESTFUNC=do_test
TST_NEEDS_CMDS="cat"
. tst_test.sh

Quoted 8 lines
Quoted 6 lines

do_test()

{
	EXPECT_PASS [ "$(echo 'SUCCESS' | cat)" = "SUCCESS" ]
}

tst_run

That would not work, the pipe is supposed to be in the runtest file.

Do you want to test that runtest is working with pipe?
I considered anything but shell script with getopt parameters a bit strange and thought it'd be removed in new shell runner. But obviously you want to keep it.

But in case of failure script don't detect it. e.g.:

echo "SUCCESS" | cat /asdf
cat: /asdf: No such file or directory

=> there is no TFAIL/TBROK/TCONF. Not sure if all users check exit status (which they should now, because that is the only common thing so far).

Kind regards,
Petr

#5 lore raw

PATCH 1/1 splice02: Generate input in C

Hi!

Quoted 2 lines
Quoted 1 line

That would not work, the pipe is supposed to be in the runtest file.

Do you want to test that runtest is working with pipe?

Yes, that was the original idea behind adding the splice test there.

Quoted 2 lines

I considered anything but shell script with getopt parameters a bit strange and thought it'd be removed in new shell runner. But obviously you want to keep it.

We will have to keep the functionality for the time being.

Quoted 7 lines

But in case of failure script don't detect it. e.g.:

echo "SUCCESS" | cat /asdf
cat: /asdf: No such file or directory

=> there is no TFAIL/TBROK/TCONF. Not sure if all users check exit status (which they should now, because that is the only common thing so far).

Well yes, I guess that we can do something as:

echo "SUCCESS" | grep -q "SUCCESS"

Which at least returns 1 if the grep fails.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#6 lore raw

PATCH 1/1 splice02: Generate input in C

Quoted 16 lines

Hi!

Quoted 2 lines
Quoted 1 line

That would not work, the pipe is supposed to be in the runtest file.

Do you want to test that runtest is working with pipe?

Yes, that was the original idea behind adding the splice test there.

Quoted 2 lines

I considered anything but shell script with getopt parameters a bit strange and thought it'd be removed in new shell runner. But obviously you want to keep it.

We will have to keep the functionality for the time being.

Quoted 4 lines

But in case of failure script don't detect it. e.g.:

echo "SUCCESS" | cat /asdf
cat: /asdf: No such file or directory

Quoted 8 lines
Quoted 2 lines

=> there is no TFAIL/TBROK/TCONF. Not sure if all users check exit status (which they should now, because that is the only common thing so far).

Well yes, I guess that we can do something as:

echo "SUCCESS" | grep -q "SUCCESS"

Which at least returns 1 if the grep fails.

Yep, that's better than using cat.

Although, IMHO it should be possible to do something like:

shell_test01 echo "SUCCESS" | shell_pipe01.sh

cat shell_pipe01.sh
...
do_test()

{
	tst_res TINFO "expecting SUCCESS string passed from stdin"

	read line
	EXPECT_PASS [ "$line" = "SUCCESS" ]
}

We'd use standard LTP interface with tst_test.sh. WDYT?

Kind regards,
Petr

#7 lore raw

PATCH 1/1 splice02: Generate input in C

Hi!

Quoted 14 lines
Quoted 5 lines

Well yes, I guess that we can do something as:

echo "SUCCESS" | grep -q "SUCCESS"

Which at least returns 1 if the grep fails.

Yep, that's better than using cat.

Although, IMHO it should be possible to do something like:

shell_test01 echo "SUCCESS" | shell_pipe01.sh

cat shell_pipe01.sh
...
do_test()

Quoted 8 lines
{
	tst_res TINFO "expecting SUCCESS string passed from stdin"

	read line
	EXPECT_PASS [ "$line" = "SUCCESS" ]
}

We'd use standard LTP interface with tst_test.sh. WDYT?

Well we have to figure out where to put the dummy test, but other than that no complaints. Also please put that change in a separate commit.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#8 lore raw

PATCH 1/1 splice02: Generate input in C

Quoted 4 lines
Quoted 1 line

We'd use standard LTP interface with tst_test.sh. WDYT?

Well we have to figure out where to put the dummy test, but other than that no complaints. Also please put that change in a separate commit.

Sure! It will be in v2.
Place: it could be in new shell directory: either in testcases/misc/shell/ or testcases/shell/.

Kind regards,
Petr