From f5d4e7db0138222f043b512a3d08134d74fcc82a Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Tue, 15 Aug 2017 18:05:31 -0400 Subject: [PATCH] Port: Replace fork() in test_ctf_writer MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Michael Jeanson Signed-off-by: Jérémie Galarneau --- tests/lib/test_ctf_writer.c | 90 +++++++++---------------------------- 1 file changed, 22 insertions(+), 68 deletions(-) diff --git a/tests/lib/test_ctf_writer.c b/tests/lib/test_ctf_writer.c index bf1ff958e..0a09e2443 100644 --- a/tests/lib/test_ctf_writer.c +++ b/tests/lib/test_ctf_writer.c @@ -40,7 +40,6 @@ #include #include #include -#include #include #include "tap/tap.h" #include @@ -99,85 +98,40 @@ static void validate_trace(char *parser_path, char *trace_path) { int ret = 0; - gchar *babeltrace_output_path; - int babeltrace_output_fd = -1; + gchar *standard_error; + gint exit_status; + char *argv[] = {parser_path, trace_path, NULL}; - if (!trace_path) { + if (!parser_path || !trace_path) { ret = -1; goto result; } - babeltrace_output_fd = g_file_open_tmp("babeltrace_output_XXXXXX", - &babeltrace_output_path, NULL); - unlink(babeltrace_output_path); - g_free(babeltrace_output_path); - - if (babeltrace_output_fd == -1) { - diag("Failed to create a temporary file for trace parsing."); + if (!g_spawn_sync(NULL, + argv, + NULL, + G_SPAWN_STDOUT_TO_DEV_NULL, + NULL, + NULL, + NULL, + &standard_error, + &exit_status, + NULL)) { + diag("Failed to spawn babeltrace."); ret = -1; goto result; } - pid_t pid = fork(); - if (pid) { - int status = 0; - waitpid(pid, &status, 0); - ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1; - } else { - ret = dup2(babeltrace_output_fd, STDOUT_FILENO); - if (ret < 0) { - perror("# dup2 babeltrace_output_fd to STDOUT"); - goto result; - } - - ret = dup2(babeltrace_output_fd, STDERR_FILENO); - if (ret < 0) { - perror("# dup2 babeltrace_output_fd to STDERR"); - goto result; - } - - execl(parser_path, parser_path, trace_path, NULL); - perror("# Could not launch the babeltrace process"); - exit(-1); + if(!g_spawn_check_exit_status(exit_status, NULL)) { + diag("Babeltrace returned an error."); + diag_multiline(standard_error); + ret = -1; + goto result; } + result: ok(ret == 0, "Babeltrace could read the resulting trace"); - - if (ret && babeltrace_output_fd >= 0) { - char *line; - size_t len = METADATA_LINE_SIZE; - FILE *babeltrace_output_fp = NULL; - - babeltrace_output_fp = fdopen(babeltrace_output_fd, "r"); - if (!babeltrace_output_fp) { - perror("fdopen on babeltrace_output_fd"); - goto close_fp; - } - babeltrace_output_fd = -1; - - line = malloc(len); - if (!line) { - diag("malloc error"); - } - rewind(babeltrace_output_fp); - while (bt_getline(&line, &len, babeltrace_output_fp) > 0) { - diag("%s", line); - } - - free(line); -close_fp: - if (babeltrace_output_fp) { - if (fclose(babeltrace_output_fp)) { - diag("fclose error"); - } - } - } - - if (babeltrace_output_fd >= 0) { - if (close(babeltrace_output_fd)) { - diag("close error"); - } - } + g_free(standard_error); } static -- 2.34.1