[LTP] [PATCH 2/6] API: Add tst_reap_child

Richard Palethorpe rpalethorpe@suse.com
Thu Jan 27 07:12:21 CET 2022


Add a simple way to wait for a specific child process. This makes
sense when you want to wait for a child while others continue to run
in the background.

Signed-off-by: Richard Palethorpe <rpalethorpe@suse.com>
---
 include/tst_test.h |  6 ++++++
 lib/tst_test.c     | 22 ++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/include/tst_test.h b/include/tst_test.h
index 450ddf086..8faf19141 100644
--- a/include/tst_test.h
+++ b/include/tst_test.h
@@ -106,6 +106,12 @@ pid_t safe_fork(const char *filename, unsigned int lineno);
  */
 void tst_reap_children(void);
 
+/*
+ * Wait for one child and exit with TBROK if it returns a non-zero
+ * exit status
+ */
+void tst_reap_child(pid_t child);
+
 struct tst_option {
 	char *optstr;
 	char **arg;
diff --git a/lib/tst_test.c b/lib/tst_test.c
index 844756fbd..156a1e4b3 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -416,6 +416,28 @@ void tst_reap_children(void)
 	}
 }
 
+void tst_reap_child(const pid_t pid)
+{
+	int status;
+
+	for (;;) {
+		const pid_t ret_pid = waitpid(pid, &status, 0);
+
+		if (ret_pid > 0) {
+			check_child_status(ret_pid, status);
+			continue;
+		}
+
+		if (errno == ECHILD)
+			break;
+
+		if (errno == EINTR)
+			continue;
+
+		tst_brk(TBROK | TERRNO, "waitpid(%d, ...) failed", pid);
+	}
+}
+
 
 pid_t safe_fork(const char *filename, unsigned int lineno)
 {
-- 
2.34.1



More information about the ltp mailing list