[LTP] [PATCH v3 3/7] lib/tst_safe_pidfd: Add SAFE_PIDFD_OPEN macro

Yang Xu xuyang2018.jy@fujitsu.com
Tue Feb 22 09:41:08 CET 2022


Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 include/tst_safe_pidfd.h | 19 +++++++++++++++++++
 lib/tst_safe_pidfd.c     | 28 ++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 include/tst_safe_pidfd.h
 create mode 100644 lib/tst_safe_pidfd.c

diff --git a/include/tst_safe_pidfd.h b/include/tst_safe_pidfd.h
new file mode 100644
index 000000000..5c37099bd
--- /dev/null
+++ b/include/tst_safe_pidfd.h
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2022 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
+ */
+
+#ifndef TST_SAFE_PIDFD_H__
+#define TST_SAFE_PIDFD_H__
+
+#include <unistd.h>
+#include "lapi/pidfd.h"
+
+int safe_pidfd_open(const char *file, const int lineno, pid_t pid,
+		    unsigned int flags);
+
+#define SAFE_PIDFD_OPEN(pid, flags) \
+	safe_pidfd_open(__FILE__, __LINE__, (pid), (flags))
+
+#endif /* TST_SAFE_PIDFD_H__ */
diff --git a/lib/tst_safe_pidfd.c b/lib/tst_safe_pidfd.c
new file mode 100644
index 000000000..c9fe5e1d0
--- /dev/null
+++ b/lib/tst_safe_pidfd.c
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2022 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
+ */
+
+#define TST_NO_DEFAULT_MAIN
+#include "tst_test.h"
+#include "tst_safe_pidfd.h"
+
+int safe_pidfd_open(const char *file, const int lineno, pid_t pid,
+		    unsigned int flags)
+{
+	int rval;
+
+	rval = pidfd_open(pid, flags);
+
+	if (rval == -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			 "pidfd_open(%i, %i) failed", pid, flags);
+	} else if (rval < 0) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			  "Invalid pidfd_open(%i, %i) return value %d",
+			  pid, flags, rval);
+	}
+
+	return rval;
+}
-- 
2.31.1



More information about the ltp mailing list