From 9386c1f2519abd8b4351fc97419c41182389a1f6 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Tue, 9 Jul 2024 23:41:48 +0530 Subject: util/vpopen: support for reading stderr --- src/evanix.c | 6 +++--- src/jobs.c | 4 ++-- src/util.c | 14 ++++++++++---- 3 files changed, 15 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/evanix.c b/src/evanix.c index cd35782..b801e73 100644 --- a/src/evanix.c +++ b/src/evanix.c @@ -17,11 +17,11 @@ static const char usage[] = " -s, --system System to build for.\n" " -m, --max-build Max number of builds.\n" " -p, --pipelined Use evanix build pipeline.\n" - " -c, --close-stderr-exec Close stderr on exec.\n" + " -c, --close-unused-fd Close stderr on exec.\n" "\n"; struct evanix_opts_t evanix_opts = { - .close_stderr_exec = true, + .close_unused_fd = true, .isflake = false, .ispipelined = true, .isdryrun = false, @@ -164,7 +164,7 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - evanix_opts.close_stderr_exec = ret; + evanix_opts.close_unused_fd = ret; break; default: fprintf(stderr, diff --git a/src/jobs.c b/src/jobs.c index 096c97d..9ffe478 100644 --- a/src/jobs.c +++ b/src/jobs.c @@ -257,7 +257,7 @@ int job_read(FILE *stream, struct job **job) temp = cJSON_GetObjectItemCaseSensitive(root, "error"); if (cJSON_IsString(temp)) { - if (evanix_opts.close_stderr_exec) + if (evanix_opts.close_unused_fd) puts(temp->valuestring); ret = JOB_READ_EVAL_ERR; goto out_free; @@ -463,7 +463,7 @@ int jobs_init(FILE **stream, char *expr) args[argindex++] = NULL; /* the package is wrapProgram-ed with nix-eval-jobs */ - ret = vpopen(stream, XSTR(NIX_EVAL_JOBS_PATH), args); + ret = vpopen(stream, XSTR(NIX_EVAL_JOBS_PATH), args, VPOPEN_STDOUT); return ret; } diff --git a/src/util.c b/src/util.c index 5001e01..95e5341 100644 --- a/src/util.c +++ b/src/util.c @@ -42,7 +42,7 @@ out_free_line: return ret; } -int vpopen(FILE **stream, const char *file, char *const argv[]) +int vpopen(FILE **stream, const char *file, char *const argv[], vpopen_t type) { int fd[2], ret; int nullfd = -1; @@ -72,19 +72,25 @@ int vpopen(FILE **stream, const char *file, char *const argv[]) } close(fd[0]); - ret = dup2(fd[1], STDOUT_FILENO); + if (type == VPOPEN_STDOUT) + ret = dup2(fd[1], STDOUT_FILENO); + else + ret = dup2(fd[1], STDERR_FILENO); if (ret < 0) { print_err("%s", strerror(errno)); goto out_close_fd_1; } - if (evanix_opts.close_stderr_exec) { + if (evanix_opts.close_unused_fd) { nullfd = open("/dev/null", O_WRONLY); if (nullfd < 0) { print_err("%s", strerror(errno)); goto out_close_fd_1; } - ret = dup2(nullfd, STDERR_FILENO); + if (type == VPOPEN_STDOUT) + ret = dup2(nullfd, STDERR_FILENO); + else + ret = dup2(nullfd, STDOUT_FILENO); if (ret < 0) { print_err("%s", strerror(errno)); goto out_close_nullfd; -- cgit v1.2.3