From 581f6c7f1d94f287cb8718ad2bbf6153ac71c84d Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Sat, 5 Mar 2022 11:57:40 +0200 Subject: [PATCH] pwtest: add pwtest_spawn for running external programs --- test/pwtest.c | 28 ++++++++++++++++++++++++++++ test/pwtest.h | 5 +++++ 2 files changed, 33 insertions(+) diff --git a/test/pwtest.c b/test/pwtest.c index c711e2f37..f1604c094 100644 --- a/test/pwtest.c +++ b/test/pwtest.c @@ -407,6 +407,34 @@ pwtest_mkstemp(char path[PATH_MAX]) pwtest_error_with_msg("Unable to create temporary file: %s", strerror(errno)); } +int +pwtest_spawn(const char *file, char *const argv[]) +{ + int r; + int status = -1; + pid_t pid; + const int fail_code = 121; + + pid = fork(); + if (pid == 0) { + /* child process */ + setlinebuf(stderr); + setlinebuf(stdout); + execvp(file, (char **)argv); + exit(fail_code); + } else if (pid < 0) + pwtest_error_with_msg("Unable to fork: %s", strerror(errno)); + + r = waitpid(pid, &status, 0); + if (r <= 0) + pwtest_error_with_msg("waitpid failed: %s", strerror(errno)); + + if (WEXITSTATUS(status) == fail_code) + pwtest_error_with_msg("exec %s failed", file); + + return status; +} + void _pwtest_add(struct pwtest_context *ctx, struct pwtest_suite *suite, const char *funcname, const void *func, ...) { diff --git a/test/pwtest.h b/test/pwtest.h index 576399ead..9c0737523 100644 --- a/test/pwtest.h +++ b/test/pwtest.h @@ -558,6 +558,11 @@ pwtest_spa_plugin_try_load_interface(struct pwtest_spa_plugin *plugin, */ void pwtest_mkstemp(char path[PATH_MAX]); +/** + * Run a command and wait for it to return. + */ +int pwtest_spawn(const char *file, char *const argv[]); + /** * \}