From: Michael Jeanson Date: Tue, 15 Aug 2017 22:05:31 +0000 (-0400) Subject: Port: Replace fork() in test_ctf_writer X-Git-Tag: v2.0.0-pre4~123 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=1f4a376be5568b1da76c107b01d29c2f3aae2807 Port: Replace fork() in test_ctf_writer Signed-off-by: Michael Jeanson Signed-off-by: Jérémie Galarneau --- diff --git a/tests/lib/test_ctf_writer.c b/tests/lib/test_ctf_writer.c index bf1ff958..0a09e244 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