pwtest: add pwtest_spawn for running external programs

This commit is contained in:
Pauli Virtanen 2022-03-05 11:57:40 +02:00 committed by Wim Taymans
parent df4f844daa
commit 581f6c7f1d
2 changed files with 33 additions and 0 deletions

View file

@ -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, ...)
{

View file

@ -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[]);
/**
* \}