From dca8ff54378495d1f08b863e431d2cbdb8acc688 Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 31 Oct 2026 14:28:22 -0700 Subject: Move the die wrappers into filter.c --- source/filter.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'source/filter.c') diff --git a/source/filter.c b/source/filter.c index c33e9bc..6cc5e80 100644 --- a/source/filter.c +++ b/source/filter.c @@ -25,6 +25,31 @@ // The exec filter holding stdout, for cgit_abort_filters to take it back. static struct cgit_exec_filter *running_exec; +/* + * Each of these returns its argument untouched when the syscall it wraps + * succeeded, and dies with the message and errno otherwise. + */ +static int die_unless_zero(int result, const char *msg) +{ + if (result != 0) + die_errno("%s", msg); + return result; +} + +static int die_unless_positive(int result, const char *msg) +{ + if (result <= 0) + die_errno("%s", msg); + return result; +} + +static int die_unless_non_negative(int result, const char *msg) +{ + if (result < 0) + die_errno("%s", msg); + return result; +} + static int open_exec_filter(struct cgit_filter *base, va_list ap) { struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base; @@ -34,12 +59,12 @@ static int open_exec_filter(struct cgit_filter *base, va_list ap) for (i = 0; i < filter->base.argument_count; i++) filter->argv[i + 1] = va_arg(ap, char *); - filter->old_stdout = cgit_die_unless_positive(dup(STDOUT_FILENO), "Unable to duplicate stdout"); + filter->old_stdout = die_unless_positive(dup(STDOUT_FILENO), "Unable to duplicate stdout"); // The child keeps the page's stdout, but not the descriptor the parent // needs it back from. fcntl(filter->old_stdout, F_SETFD, FD_CLOEXEC); - cgit_die_unless_zero(pipe(pipefd), "Unable to create pipe to subprocess"); - filter->pid = cgit_die_unless_non_negative(fork(), "Unable to create subprocess"); + die_unless_zero(pipe(pipefd), "Unable to create pipe to subprocess"); + filter->pid = die_unless_non_negative(fork(), "Unable to create subprocess"); if (filter->pid == 0) { close(pipefd[1]); if (dup2(pipefd[0], STDIN_FILENO) < 0) @@ -55,7 +80,7 @@ static int open_exec_filter(struct cgit_filter *base, va_list ap) _exit(EXEC_FAILED); } close(pipefd[0]); - cgit_die_unless_non_negative(dup2(pipefd[1], STDOUT_FILENO), "Unable to use pipe as stdout"); + die_unless_non_negative(dup2(pipefd[1], STDOUT_FILENO), "Unable to use pipe as stdout"); close(pipefd[1]); running_exec = filter; return 0; @@ -67,7 +92,7 @@ static int close_exec_filter(struct cgit_filter *base) int exit_status = 0; running_exec = NULL; - cgit_die_unless_non_negative(dup2(filter->old_stdout, STDOUT_FILENO), "Unable to restore stdout"); + die_unless_non_negative(dup2(filter->old_stdout, STDOUT_FILENO), "Unable to restore stdout"); close(filter->old_stdout); waitpid(filter->pid, &exit_status, 0); if (!WIFEXITED(exit_status)) -- cgit