<div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-size:small"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Apr 16, 2020 at 3:29 PM Yang Xu <<a href="mailto:xuyang2018.jy@cn.fujitsu.com">xuyang2018.jy@cn.fujitsu.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Signed-off-by: Yang Xu <<a href="mailto:xuyang2018.jy@cn.fujitsu.com" target="_blank">xuyang2018.jy@cn.fujitsu.com</a>><br>
---<br>
runtest/syscalls | 1 +<br>
testcases/kernel/syscalls/pipe2/.gitignore | 1 +<br>
testcases/kernel/syscalls/pipe2/pipe2_03.c | 157 +++++++++++++++++++++<br>
3 files changed, 159 insertions(+)<br>
create mode 100644 testcases/kernel/syscalls/pipe2/pipe2_03.c<br>
<br>
diff --git a/runtest/syscalls b/runtest/syscalls<br>
index 44254d7da..4d8ebc5a3 100644<br>
--- a/runtest/syscalls<br>
+++ b/runtest/syscalls<br>
@@ -912,6 +912,7 @@ pipe13 pipe13<br>
<br>
pipe2_01 pipe2_01<br>
pipe2_02 pipe2_02<br>
+pipe2_03 pipe2_03<br>
<br>
pivot_root01 pivot_root01<br>
<br>
diff --git a/testcases/kernel/syscalls/pipe2/.gitignore b/testcases/kernel/syscalls/pipe2/.gitignore<br>
index 786222de2..4cc5acaf1 100644<br>
--- a/testcases/kernel/syscalls/pipe2/.gitignore<br>
+++ b/testcases/kernel/syscalls/pipe2/.gitignore<br>
@@ -1,3 +1,4 @@<br>
/pipe2_01<br>
/pipe2_02<br>
/pipe2_02_child<br>
+/pipe2_03<br>
diff --git a/testcases/kernel/syscalls/pipe2/pipe2_03.c b/testcases/kernel/syscalls/pipe2/pipe2_03.c<br>
new file mode 100644<br>
index 000000000..0b5d37dd0<br>
--- /dev/null<br>
+++ b/testcases/kernel/syscalls/pipe2/pipe2_03.c<br>
@@ -0,0 +1,157 @@<br>
+// SPDX-License-Identifier: GPL-2.0-or-later<br>
+/*<br>
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.<br>
+ * Author: Yang Xu <<a href="mailto:xuyang2018.jy@cn.fujitsu.com" target="_blank">xuyang2018.jy@cn.fujitsu.com</a>><br>
+ *<br>
+ * This case is designed to test the basic functionality about the<br>
+ * O_DIRECT flag of pipe2.<br>
+ *<br>
+ * It includes three sub tests.<br>
+ * 1) Each write(2) to the pipe is dealt with as a separate packet, and<br>
+ * read(2)s from the pipe will read one packet at a time.<br>
+ * 2) Writes of greater than PIPE_BUF bytes (see pipe(7)) will be split<br>
+ * into multiple packet.<br>
+ * 3)If a read(2) specifies a buffer size that is smaller than the next<br>
+ * packet, then the requested number of bytes are read, and the excess<br>
+ * bytes in the packet are discarded.<br>
+ */<br>
+#define _GNU_SOURCE<br>
+#include <stdio.h><br>
+#include <unistd.h><br>
+#include <stdlib.h><br>
+#include <linux/limits.h><br>
+#include "lapi/fcntl.h"<br>
+#include "tst_test.h"<br>
+<br>
+static int fds[2], packet_num, pipe_size;<br>
+static char *wrbuf;<br>
+static char *rdbuf;<br>
+static void check_peer_rw(void);<br>
+static void check_split(void);<br>
+static void check_discard(void);<br>
+<br>
+static void (*test_func[])(void) = {check_peer_rw, check_split, check_discard};<br>
+<br>
+static void check_peer_rw(void)<br>
+{<br>
+ int i, pid;<br>
+<br>
+ SAFE_PIPE2(fds, O_DIRECT | O_NONBLOCK);<br>
+ for (i = 0; i < packet_num; i++)<br>
+ SAFE_WRITE(1, fds[1], "x", 1);<br>
+<br>
+ TEST(write(fds[1], "x", 1));<br>
+ if (TST_RET != -1) {<br>
+ tst_res(TFAIL, "write succeeded unexpectedly");<br>
+ } else {<br>
+ if (TST_ERR == EAGAIN)<br>
+ tst_res(TPASS, "Each write(2) uses a separate packet");<br>
+ else<br>
+ tst_res(TFAIL | TTERRNO, "write failed, expected EAGAIN but got");<br>
+ }<br>
+ pid = SAFE_FORK();<br>
+ if (!pid) {<br>
+ memset(rdbuf, 0, pipe_size);<br>
+ for (i = 0; i < packet_num; i++) {<br>
+ TEST(SAFE_READ(0, fds[0], rdbuf, pipe_size));<br>
+ if (TST_RET != 1)<br>
+ tst_res(TFAIL,<br>
+ "Each read(2) doesn't read a separate packet, return %ld", TST_RET);<br>
+ }<br>
+ tst_res(TPASS, "Each read(2) reads a separate packet");<br></blockquote><div><br></div><div class="gmail_default" style="font-size:small">I would add _exit(0); at the end of the child.</div><div class="gmail_default" style="font-size:small">+ _exit(0);</div><div class="gmail_default" style="font-size:small"><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ }<br></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">And move the tst_reap_children() from the main process to here.</div><br></div><div><div class="gmail_default" style="font-size:small">+ tst_reap_children();<br>+ SAFE_CLOSE(fds[0]);<br>+ SAFE_CLOSE(fds[1]);<br></div><div class="gmail_default" style="font-size:small"><br></div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+}<br>
+<br>
+static void check_split(void)<br>
+{<br>
+ int i, pid;<br>
+<br>
+ SAFE_PIPE2(fds, O_DIRECT);<br>
+ SAFE_WRITE(1, fds[1], wrbuf, PIPE_BUF * 2);<br>
+<br>
+ pid = SAFE_FORK();<br>
+ if (!pid) {<br>
+ memset(rdbuf, 0, pipe_size);<br>
+ for (i = 0; i < 2; i++) {<br>
+ TEST(SAFE_READ(0, fds[0], rdbuf, pipe_size));<br>
+ if (TST_RET != PIPE_BUF)<br>
+ tst_res(TFAIL,<br>
+ "write(higner than PIPE_BUF) split into multiple packet, return %ld", TST_RET);<br>
+ }<br>
+ tst_res(TPASS, "write(higner than PIPE_BUF) split into multiple packet");<br></blockquote><div> </div><div><span class="gmail_default" style="font-size:small"></span>+ _exit(0);<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ }<br></blockquote><div> </div><div><span class="gmail_default" style="font-size:small"></span>+ tst_reap_children();</div>+ SAFE_CLOSE(fds[0]);<br><div><span class="gmail_default" style="font-size:small">+ SAFE_CLOSE(fds[1]);</span></div><div><span class="gmail_default" style="font-size:small"></span> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+}<br>
+<br>
+static void check_discard(void)<br>
+{<br>
+ int pid;<br>
+ char tmp_buf[20];<br>
+ char tmp_secondbuf[20];<br>
+<br>
+ SAFE_PIPE2(fds, O_DIRECT);<br>
+ SAFE_WRITE(1, fds[1], wrbuf, PIPE_BUF);<br>
+ SAFE_WRITE(1, fds[1], "1", 1);<br>
+<br>
+ pid = SAFE_FORK();<br>
+ if (!pid) {<br>
+ TEST(SAFE_READ(0, fds[0], tmp_buf, 20));<br>
+ if (TST_RET != 20)<br>
+ tst_res(TFAIL,<br>
+ "the excess bytes in the packet isn't discarded by read, return %ld", TST_RET);<br>
+ TEST(SAFE_READ(0, fds[0], tmp_secondbuf, 20));<br>
+ if (TST_RET == 1) {<br>
+ if (!strcmp(tmp_secondbuf, "1"))<br>
+ tst_res(TPASS,<br>
+ "the excess bytes in the packet is discarded by read, only read 1");<br>
+ else<br>
+ tst_res(TFAIL,<br>
+ "the excess bytes in the packet is discarded by read, expect 1 got %s", tmp_secondbuf);<br>
+ }<br></blockquote><div> </div><div><span class="gmail_default" style="font-size:small"></span><span class="gmail_default"></span>+ _exit(0);</div><div><span class="gmail_default"></span> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ }<br></blockquote><div> </div><div><div class="gmail_default" style="font-size:small"><span class="gmail_default"></span>+ tst_reap_children();</div>+ SAFE_CLOSE(fds[0]);<br><div class="gmail_default" style="font-size:small">+ SAFE_CLOSE(fds[1]);</div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+}<br>
+<br>
+static void verify_pipe2(unsigned int n)<br>
+{<br>
+ int pid;<br>
+<br>
+ pid = SAFE_FORK();<br></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">Why we need fork() here? I don't see any necessary reason for twice fork in the main process.</div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ if (pid == 0) {<br>
+ (*test_func[n])();<br></blockquote><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ tst_reap_children();<br>
+ SAFE_CLOSE(fds[0]);<br>
+ SAFE_CLOSE(fds[1]);<br></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">I tend to move the cloese(fd) to the test process to nearby pipe2(). Otherwise, it causes an unclear error like:</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small"># ./pipe2_03<br>tst_test.c:1246: INFO: Timeout per run is 0h 05m 00s<br>pipe2_03.c:48: PASS: Each write(2) uses a separate packet<br>pipe2_03.c:61: PASS: Each read(2) reads a separate packet<br>pipe2_03.c:81: PASS: write(higner than PIPE_BUF) split into multiple packet<br>pipe2_03.c:108: FAIL: the excess bytes in the packet is discarded by read, expect 1 got 1%A<br></div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ }<br>
+ tst_reap_children();<br>
+}<br>
+<br>
+static void setup(void)<br>
+{<br>
+ SAFE_PIPE2(fds, O_DIRECT);<br>
+ pipe_size = SAFE_FCNTL(fds[1], F_GETPIPE_SZ);<br>
+ wrbuf = SAFE_MALLOC(PIPE_BUF * 2);<br>
+ rdbuf = SAFE_MALLOC(pipe_size);<br>
+ memset(wrbuf, 'x', PIPE_BUF * 2);<br>
+ packet_num = pipe_size / PIPE_BUF;<br>
+ SAFE_CLOSE(fds[0]);<br>
+ SAFE_CLOSE(fds[1]);<br>
+}<br>
+<br>
+static void cleanup(void)<br>
+{<br>
+ if (fds[0] > 0)<br>
+ SAFE_CLOSE(fds[0]);<br>
+ if (fds[1] > 0)<br>
+ SAFE_CLOSE(fds[1]);<br>
+ if (wrbuf)<br>
+ free(wrbuf);<br>
+ if (rdbuf)<br>
+ free(rdbuf);<br>
+}<br>
+<br>
+static struct tst_test test = {<br>
+ .setup = setup,<br>
+ .cleanup = cleanup,<br>
+ .forks_child = 1,<br>
+ .test = verify_pipe2,<br>
+ .tcnt = ARRAY_SIZE(test_func),<br>
+};<br>
-- <br>
2.23.0<br>
<br>
<br>
<br>
<br>
-- <br>
Mailing list info: <a href="https://lists.linux.it/listinfo/ltp" rel="noreferrer" target="_blank">https://lists.linux.it/listinfo/ltp</a><br>
<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div>Regards,<br></div><div>Li Wang<br></div></div></div></div>