Port: Replace fork() in test_ctf_writer
authorMichael Jeanson <mjeanson@efficios.com>
Tue, 15 Aug 2017 22:05:31 +0000 (18:05 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 21 Aug 2017 21:02:23 +0000 (17:02 -0400)
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
tests/lib/test_ctf_writer.c

index bf1ff958e0bd53bb726f9d9939e579a93ea60e90..0a09e2443fd74dfb4991df0c210c4b6a32ec9e59 100644 (file)
@@ -40,7 +40,6 @@
 #include <babeltrace/compat/stdio-internal.h>
 #include <string.h>
 #include <assert.h>
-#include <sys/wait.h>
 #include <fcntl.h>
 #include "tap/tap.h"
 #include <math.h>
@@ -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
This page took 0.02737 seconds and 4 git commands to generate.