diff mbox series

[2/6] API: Add tst_reap_child

Message ID 20220127061225.23368-3-rpalethorpe@suse.com
State Accepted
Headers show
Series Add memcontrol03 and declarative CG API | expand

Commit Message

Richard Palethorpe Jan. 27, 2022, 6:12 a.m. UTC
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 mbox series

Patch

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)
 {