diff mbox series

[1/3] Add tst_validate_children() helper function

Message ID 20220913151907.26763-2-mdoucha@suse.cz
State Superseded
Headers show
Series Fix ADSP074 timeouts | expand

Commit Message

Martin Doucha Sept. 13, 2022, 3:19 p.m. UTC
The function waits for given number of child processes and validates
that they have all exited without error.

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 include/tst_test.h |  8 ++++++++
 lib/tst_res.c      | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

Comments

Cyril Hrubis Sept. 14, 2022, 9:12 a.m. UTC | #1
Hi!
> The function waits for given number of child processes and validates
> that they have all exited without error.
> 
> Signed-off-by: Martin Doucha <mdoucha@suse.cz>
> ---
>  include/tst_test.h |  8 ++++++++
>  lib/tst_res.c      | 37 +++++++++++++++++++++++++++++++++++++
>  2 files changed, 45 insertions(+)
> 
> diff --git a/include/tst_test.h b/include/tst_test.h
> index ac52f268c..69e649651 100644
> --- a/include/tst_test.h
> +++ b/include/tst_test.h
> @@ -362,6 +362,14 @@ void tst_set_max_runtime(int max_runtime);
>   */
>  char *tst_get_tmpdir(void);
>  
> +/*
> + * Validates exit status of child processes
> + */
> +int tst_validate_children_(const char *file, const int lineno,
> +	unsigned int count);
> +#define tst_validate_children(child_count) \
> +	tst_validate_children_(__FILE__, __LINE__, (child_count))
> +
>  #ifndef TST_NO_DEFAULT_MAIN
>  
>  static struct tst_test test;
> diff --git a/lib/tst_res.c b/lib/tst_res.c
> index e0896eb05..cac7484e7 100644
> --- a/lib/tst_res.c
> +++ b/lib/tst_res.c
> @@ -613,3 +613,40 @@ void tst_require_root(void)
>  	if (geteuid() != 0)
>  		tst_brkm(TCONF, NULL, "Test needs to be run as root");
>  }
> +
> +int tst_validate_children_(const char *file, const int lineno,
> +	unsigned int count)
> +{
> +	unsigned int i;
> +	int status;
> +
> +	for (i = 0; i < count; i++) {
> +		SAFE_WAITPID(NULL, -1, &status, 0);
> +
> +		if (WIFSIGNALED(status)) {
> +			tst_res_(file, lineno, TFAIL,
> +				"Child killed by signal %d %s",
> +				WTERMSIG(status), tst_strsig(WTERMSIG(status)));
> +			return 1;
> +		}
> +
> +		if (WCOREDUMP(status)) {
> +			tst_res_(file, lineno, TFAIL, "Child crashed");
> +			return 1;
> +		}

WCOREDUMP() is subset of WIFSIGNALED() and as such it will be never
triggered here.

> +		if (!WIFEXITED(status)) {
> +			tst_res_(file, lineno, TFAIL,
> +				"Unexpected child status");
> +			return 1;
> +		}
> +
> +		if (WEXITSTATUS(status)) {
> +			tst_res_(file, lineno, TFAIL, "Child returned error %d",
> +				WEXITSTATUS(status));
> +			return 1;
> +		}

And we do have tst_strstatus() that actually does all of this so why not
just:

	SAFE_WAITPID(NULL, -1, &status, 0);

	if (!WIFEXITED(status) || WEXITSTATUS(status)) {
		tst_res_(file, lineno, TFAIL, "Child %s", tst_strstatus(status);
		return 1;
	}
diff mbox series

Patch

diff --git a/include/tst_test.h b/include/tst_test.h
index ac52f268c..69e649651 100644
--- a/include/tst_test.h
+++ b/include/tst_test.h
@@ -362,6 +362,14 @@  void tst_set_max_runtime(int max_runtime);
  */
 char *tst_get_tmpdir(void);
 
+/*
+ * Validates exit status of child processes
+ */
+int tst_validate_children_(const char *file, const int lineno,
+	unsigned int count);
+#define tst_validate_children(child_count) \
+	tst_validate_children_(__FILE__, __LINE__, (child_count))
+
 #ifndef TST_NO_DEFAULT_MAIN
 
 static struct tst_test test;
diff --git a/lib/tst_res.c b/lib/tst_res.c
index e0896eb05..cac7484e7 100644
--- a/lib/tst_res.c
+++ b/lib/tst_res.c
@@ -613,3 +613,40 @@  void tst_require_root(void)
 	if (geteuid() != 0)
 		tst_brkm(TCONF, NULL, "Test needs to be run as root");
 }
+
+int tst_validate_children_(const char *file, const int lineno,
+	unsigned int count)
+{
+	unsigned int i;
+	int status;
+
+	for (i = 0; i < count; i++) {
+		SAFE_WAITPID(NULL, -1, &status, 0);
+
+		if (WIFSIGNALED(status)) {
+			tst_res_(file, lineno, TFAIL,
+				"Child killed by signal %d %s",
+				WTERMSIG(status), tst_strsig(WTERMSIG(status)));
+			return 1;
+		}
+
+		if (WCOREDUMP(status)) {
+			tst_res_(file, lineno, TFAIL, "Child crashed");
+			return 1;
+		}
+
+		if (!WIFEXITED(status)) {
+			tst_res_(file, lineno, TFAIL,
+				"Unexpected child status");
+			return 1;
+		}
+
+		if (WEXITSTATUS(status)) {
+			tst_res_(file, lineno, TFAIL, "Child returned error %d",
+				WEXITSTATUS(status));
+			return 1;
+		}
+	}
+
+	return 0;
+}