[LTP] [PATCH v2 2/2] syscalls/pidfd_getfd02: Add basic error test
Yang Xu
xuyang2018.jy@fujitsu.com
Fri Feb 18 11:12:12 CET 2022
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
runtest/syscalls | 1 +
.../kernel/syscalls/pidfd_getfd/.gitignore | 1 +
.../syscalls/pidfd_getfd/pidfd_getfd02.c | 104 ++++++++++++++++++
3 files changed, 106 insertions(+)
create mode 100644 testcases/kernel/syscalls/pidfd_getfd/pidfd_getfd02.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 6155712cc..436cc2a0a 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -952,6 +952,7 @@ personality01 personality01
personality02 personality02
pidfd_getfd01 pidfd_getfd01
+pidfd_getfd02 pidfd_getfd02
pidfd_open01 pidfd_open01
pidfd_open02 pidfd_open02
diff --git a/testcases/kernel/syscalls/pidfd_getfd/.gitignore b/testcases/kernel/syscalls/pidfd_getfd/.gitignore
index 9328942ac..8ef904202 100644
--- a/testcases/kernel/syscalls/pidfd_getfd/.gitignore
+++ b/testcases/kernel/syscalls/pidfd_getfd/.gitignore
@@ -1 +1,2 @@
/pidfd_getfd01
+/pidfd_getfd02
diff --git a/testcases/kernel/syscalls/pidfd_getfd/pidfd_getfd02.c b/testcases/kernel/syscalls/pidfd_getfd/pidfd_getfd02.c
new file mode 100644
index 000000000..ef11de913
--- /dev/null
+++ b/testcases/kernel/syscalls/pidfd_getfd/pidfd_getfd02.c
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2022 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Tests basic error handling of the pidfd_open syscall.
+ *
+ * - EBADF pidfd is not a valid PID file descriptor
+ * - EBADF targetfd is not an open file descriptor in the process referred
+ * to by pidfd
+ * - EINVAL flags is not 0
+ * - ESRCH the process referred to by pidfd does not exist (it has terminated
+ * and been waited on)
+ * - EPERM the calling process doesn't have PTRACE_MODE_ATTACH_REALCREDS permissions
+ * over the process referred to by pidfd
+ */
+
+#include <stdlib.h>
+#include <pwd.h>
+#include "tst_test.h"
+#include "lapi/pidfd_open.h"
+#include "lapi/pidfd_getfd.h"
+
+static int valid_pidfd, invalid_pidfd = -1;
+static uid_t uid;
+
+static struct tcase {
+ char *name;
+ int *pidfd;
+ int targetfd;
+ int flags;
+ int exp_errno;
+} tcases[] = {
+ {"invalid pidfd", &invalid_pidfd, 0, 0, EBADF},
+ {"invalid targetfd", &valid_pidfd, -1, 0, EBADF},
+ {"invalid flags", &valid_pidfd, 0, 1, EINVAL},
+ {"the process referred to by pidfd doesn't exist", NULL, 0, 0, ESRCH},
+ {"lack of required permission", &valid_pidfd, 0, 0, EPERM},
+};
+
+static void setup(void)
+{
+ pidfd_open_supported();
+ pidfd_getfd_supported();
+
+ struct passwd *pw;
+
+ pw = SAFE_GETPWNAM("nobody");
+ uid = pw->pw_uid;
+
+ TST_EXP_FD_SILENT(pidfd_open(getpid(), 0), "pidfd_open");
+ if (!TST_PASS)
+ tst_brk(TFAIL | TTERRNO, "pidfd_open failed");
+ valid_pidfd = TST_RET;
+}
+
+static void run(unsigned int n)
+{
+ struct tcase *tc = &tcases[n];
+ int pid;
+
+ if (tc->exp_errno == EPERM) {
+ pid = SAFE_FORK();
+ if (!pid) {
+ SAFE_SETUID(uid);
+ TST_EXP_FAIL2(pidfd_getfd(valid_pidfd, tc->targetfd, tc->flags),
+ tc->exp_errno, "pidfd_getfd(%d, %d, %d) with %s",
+ valid_pidfd, tc->targetfd, tc->flags, tc->name);
+ TST_CHECKPOINT_WAKE(0);
+ exit(0);
+ }
+ TST_CHECKPOINT_WAIT(0);
+ SAFE_WAIT(NULL);
+ } else if (tc->exp_errno == ESRCH) {
+ pid = SAFE_FORK();
+ if (!pid) {
+ TST_CHECKPOINT_WAIT(0);
+ exit(0);
+ }
+ TST_EXP_FD_SILENT(pidfd_open(pid, 0), "pidfd_open");
+ TST_CHECKPOINT_WAKE(0);
+ SAFE_WAIT(NULL);
+ TST_EXP_FAIL2(pidfd_getfd(TST_RET, tc->targetfd, tc->flags),
+ tc->exp_errno, "pidfd_getfd(%d, %d, %d) with %s",
+ (int)TST_RET, tc->targetfd, tc->flags, tc->name);
+ } else {
+ TST_EXP_FAIL2(pidfd_getfd(*tc->pidfd, tc->targetfd, tc->flags),
+ tc->exp_errno, "pidfd_getfd(%d, %d, %d) with %s",
+ *tc->pidfd, tc->targetfd, tc->flags, tc->name);
+ }
+}
+
+static struct tst_test test = {
+ .tcnt = ARRAY_SIZE(tcases),
+ .test = run,
+ .setup = setup,
+ .needs_root = 1,
+ .forks_child = 1,
+ .needs_checkpoints = 1,
+};
--
2.23.0
More information about the ltp
mailing list