Skip to contentSkip to conversation

syscalls/pidfd_open01.c: Add check for close-on-exec flag

PATCH New Xiao Yang 4 participants 4 messages Jul 20, 2020

lore
Share & tools
lore

Xiao Yang

author
#0 lore raw

PATCH v4 1/1 syscalls/pidfd_open01.c: Add check for close-on-exec flag

pidfd_open(2) will set close-on-exec flag on the file descriptor as it manpage states, so check close-on-exec flag by fcntl(2).

---

1) We don't care if the test uses the TEST() macro so just keep it.
2) Use bare fcntl() instead of SAFE_FCNTL() so that file descriptor can be closed when fcntl(F_GETFD) fails.

 .../kernel/syscalls/pidfd_open/pidfd_open01.c  | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/pidfd_open/pidfd_open01.c +16 -2
index 93bb86687..f40e9b624 100644
@@ -3,21 +3,35 @@
  * Copyright (c) 2020 Viresh Kumar <[email protected]>
  *
  * Description:
- * Basic pidfd_open() test, fetches the PID of the current process and tries to
- * get its file descriptor.
+ * Basic pidfd_open() test:
+ * 1) Fetch the PID of the current process and try to get its file descriptor.
+ * 2) Check that the close-on-exec flag is set on the file descriptor.
  */
+
+#include <unistd.h>
+#include <fcntl.h>
 #include "tst_test.h"
 #include "lapi/pidfd_open.h"
 
 static void run(void)
 {
+	int flag;
+
 	TEST(pidfd_open(getpid(), 0));
 
 	if (TST_RET == -1)
 		tst_brk(TFAIL | TTERRNO, "pidfd_open(getpid(), 0) failed");
 
+	flag = fcntl(TST_RET, F_GETFD);
+
 	SAFE_CLOSE(TST_RET);
 
+	if (flag == -1)
+		tst_brk(TFAIL | TERRNO, "fcntl(F_GETFD) failed");
+
+	if (!(flag & FD_CLOEXEC))
+		tst_brk(TFAIL, "pidfd_open(getpid(), 0) didn't set close-on-exec flag");
+
 	tst_res(TPASS, "pidfd_open(getpid(), 0) passed");
 }
 
Signature · 1 line
2.21.0
#2 lore raw

PATCH v4 1/1 syscalls/pidfd_open01.c: Add check for close-on-exec flag

In reply to Xiao Yang
Quoted 4 lines · Xiao Yang

pidfd_open(2) will set close-on-exec flag on the file descriptor as it manpage states, so check close-on-exec flag by fcntl(2).

Quoted 8 lines

---

1) We don't care if the test uses the TEST() macro so just keep it.
2) Use bare fcntl() instead of SAFE_FCNTL() so that file descriptor can be closed when fcntl(F_GETFD) fails.

 .../kernel/syscalls/pidfd_open/pidfd_open01.c  | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
Signature · 1 line
viresh
#3 lore raw

PATCH v4 1/1 syscalls/pidfd_open01.c: Add check for close-on-exec flag

Hi Petr, Viresh

Thanks a lot for your review, pushed. :-)

Thanks,

Xiao Yang

Quoted 15 lines · Viresh Kumar
Quoted 4 lines · Xiao Yang

pidfd_open(2) will set close-on-exec flag on the file descriptor as it manpage states, so check close-on-exec flag by fcntl(2).

Quoted 8 lines

---

1) We don't care if the test uses the TEST() macro so just keep it.
2) Use bare fcntl() instead of SAFE_FCNTL() so that file descriptor can be closed when fcntl(F_GETFD) fails.

  .../kernel/syscalls/pidfd_open/pidfd_open01.c  | 18 ++++++++++++++++--
  1 file changed, 16 insertions(+), 2 deletions(-)