-static
-void validate_metadata(char *parser_path, char *metadata_path)
-{
- int ret = 0;
- char parser_output_path[] = "/tmp/parser_output_XXXXXX";
- int parser_output_fd = -1, metadata_fd = -1;
-
- if (!metadata_path) {
- ret = -1;
- goto result;
- }
-
- parser_output_fd = mkstemp(parser_output_path);
- metadata_fd = open(metadata_path, O_RDONLY);
-
- unlink(parser_output_path);
-
- if (parser_output_fd == -1 || metadata_fd == -1) {
- diag("Failed create temporary files for metadata parsing.");
- ret = -1;
- goto result;
- }
-
- pid_t pid = fork();
- if (pid) {
- int status = 0;
- waitpid(pid, &status, 0);
- ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
- } else {
- /* ctf-parser-test expects a metadata string on stdin. */
- ret = dup2(metadata_fd, STDIN_FILENO);
- if (ret < 0) {
- perror("# dup2 metadata_fd to STDIN");
- goto result;
- }
-
- ret = dup2(parser_output_fd, STDOUT_FILENO);
- if (ret < 0) {
- perror("# dup2 parser_output_fd to STDOUT");
- goto result;
- }
-
- ret = dup2(parser_output_fd, STDERR_FILENO);
- if (ret < 0) {
- perror("# dup2 parser_output_fd to STDERR");
- goto result;
- }
-
- execl(parser_path, "ctf-parser-test", NULL);
- perror("# Could not launch the ctf metadata parser process");
- exit(-1);
- }
-result:
- ok(ret == 0, "Metadata string is valid");
-
- if (ret && metadata_fd >= 0 && parser_output_fd >= 0) {
- char *line;
- size_t len = METADATA_LINE_SIZE;
- FILE *metadata_fp = NULL, *parser_output_fp = NULL;
-
- metadata_fp = fdopen(metadata_fd, "r");
- if (!metadata_fp) {
- perror("fdopen on metadata_fd");
- goto close_fp;
- }
- metadata_fd = -1;
-
- parser_output_fp = fdopen(parser_output_fd, "r");
- if (!parser_output_fp) {
- perror("fdopen on parser_output_fd");
- goto close_fp;
- }
- parser_output_fd = -1;
-
- line = malloc(len);
- if (!line) {
- diag("malloc failure");
- }
-
- rewind(metadata_fp);
-
- /* Output the metadata and parser output as diagnostic */
- while (bt_getline(&line, &len, metadata_fp) > 0) {
- fprintf(stderr, "# %s", line);
- }
-
- rewind(parser_output_fp);
- while (bt_getline(&line, &len, parser_output_fp) > 0) {
- fprintf(stderr, "# %s", line);
- }
-
- free(line);
-close_fp:
- if (metadata_fp) {
- if (fclose(metadata_fp)) {
- diag("fclose failure");
- }
- }
- if (parser_output_fp) {
- if (fclose(parser_output_fp)) {
- diag("fclose failure");
- }
- }
- }
-
- if (parser_output_fd >= 0) {
- if (close(parser_output_fd)) {
- diag("close error");
- }
- }
- if (metadata_fd >= 0) {
- if (close(metadata_fd)) {
- diag("close error");
- }
- }
-}
-