<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" target="_blank">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 | 2 +<br>
testcases/kernel/syscalls/pipe2/pipe2_02.c | 69 +++++++++++++++++++<br>
.../kernel/syscalls/pipe2/pipe2_02_child.c | 26 +++++++<br>
4 files changed, 98 insertions(+)<br>
create mode 100644 testcases/kernel/syscalls/pipe2/pipe2_02.c<br>
create mode 100644 testcases/kernel/syscalls/pipe2/pipe2_02_child.c<br>
<br>
diff --git a/runtest/syscalls b/runtest/syscalls<br>
index 79b671d50..44254d7da 100644<br>
--- a/runtest/syscalls<br>
+++ b/runtest/syscalls<br>
@@ -911,6 +911,7 @@ pipe12 pipe12<br>
pipe13 pipe13<br>
<br>
pipe2_01 pipe2_01<br>
+pipe2_02 pipe2_02<br>
<br>
pivot_root01 pivot_root01<br>
<br>
diff --git a/testcases/kernel/syscalls/pipe2/.gitignore b/testcases/kernel/syscalls/pipe2/.gitignore<br>
index 42350bbdc..786222de2 100644<br>
--- a/testcases/kernel/syscalls/pipe2/.gitignore<br>
+++ b/testcases/kernel/syscalls/pipe2/.gitignore<br>
@@ -1 +1,3 @@<br>
/pipe2_01<br>
+/pipe2_02<br>
+/pipe2_02_child<br>
diff --git a/testcases/kernel/syscalls/pipe2/pipe2_02.c b/testcases/kernel/syscalls/pipe2/pipe2_02.c<br>
new file mode 100644<br>
index 000000000..743d78c58<br>
--- /dev/null<br>
+++ b/testcases/kernel/syscalls/pipe2/pipe2_02.c<br>
@@ -0,0 +1,69 @@<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_CLOEXEC flag of pipe2.<br>
+ */<br>
+#define _GNU_SOURCE<br>
+#include <stdio.h><br>
+#include <unistd.h><br>
+#include <stdlib.h><br>
+#include "lapi/fcntl.h"<br>
+#include "tst_test.h"<br>
+<br>
+#define TESTBIN "pipe2_02_child"<br>
+static int fds[2];<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>
+}<br>
+<br>
+static void verify_pipe2(void)<br>
+{<br>
+ int pid, status;<br>
+ char buf[20];<br>
+<br>
+ SAFE_PIPE2(fds, O_CLOEXEC);<br>
+ sprintf(buf, "%d", fds[1]);<br>
+ pid = SAFE_FORK();<br>
+ if (pid == 0) {<br>
+ if (execlp(TESTBIN, TESTBIN, buf, NULL))<br>
+ exit(2);<br></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">Do we really need the if() condition and exit(2)? AFAIK, the exec() family of functions replaces the current process image with a new process and returns zero if succeeded.</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>
+ SAFE_WAIT(&status);<br>
+ if (WIFEXITED(status)) {<br>
+ switch (WEXITSTATUS(status)) {<br>
+ case 0:<br>
+ tst_res(TPASS, "test O_CLOEXEC for pipe2 success");<br>
+ break;<br>
+ case 1:<br>
+ tst_res(TFAIL, "test O_CLOEXEC for pipe2 failed");<br>
+ break;<br>
+ default:<br>
+ tst_brk(TBROK, "execlp() failed");<br>
+ }<br>
+ } else {<br>
+ tst_brk(TBROK, "%s exits with unexpected error", TESTBIN);<br>
+ }<br>
+ cleanup();<br>
+}<br>
+<br>
+static const char *const resfile[] = {<br>
+ TESTBIN,<br>
+ NULL,<br>
+};<br>
+<br>
+static struct tst_test test = {<br>
+ .resource_files = resfile,<br>
+ .cleanup = cleanup,<br>
+ .forks_child = 1,<br>
+ .needs_root = 1,<br>
+ .test_all = verify_pipe2,<br>
+};<br>
diff --git a/testcases/kernel/syscalls/pipe2/pipe2_02_child.c b/testcases/kernel/syscalls/pipe2/pipe2_02_child.c<br>
new file mode 100644<br>
index 000000000..d5ed68cf7<br>
--- /dev/null<br>
+++ b/testcases/kernel/syscalls/pipe2/pipe2_02_child.c<br>
@@ -0,0 +1,26 @@<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>
+#include <stdio.h><br>
+#include <stdlib.h><br>
+#include <string.h><br>
+#include <errno.h><br>
+#include <unistd.h><br>
+<br>
+int main(int argc, char **argv)<br>
+{<br>
+ int ret;<br>
+ int fd;<br>
+<br>
+ if (argc != 2) {<br>
+ fprintf(stderr, "Only two arguments: %s <fd>\n", argv[0]);<br>
+ exit(1);<br>
+ }<br>
+<br>
+ fd = atoi(argv[1]);<br>
+ ret = write(fd, "x", 1);<br>
+<br>
+ return ret != -1;</blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">To check if the return value equals -1 maybe not a good idea to confirm the 'fd' is closed. That only proof it failed to write "x" to the fd, we are not sure if that exists other errors.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">What about using the fcntl() in the fd status check?</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small"> if (fcntl(fd, F_GETFL) < 0 && errno == EBADF) </div><div class="gmail_default" style="font-size:small"> return 0;</div></div></div><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div>Regards,<br></div><div>Li Wang<br></div></div></div></div>