summaryrefslogtreecommitdiffabout
path: root/shared.c
Unidiff
Diffstat (limited to 'shared.c') (more/less context) (show whitespace changes)
-rw-r--r--shared.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/shared.c b/shared.c
index cce0af4..911a55a 100644
--- a/shared.c
+++ b/shared.c
@@ -64,2 +64,5 @@ struct cgit_repo *cgit_add_repo(const char *url)
64 ret->mtime = -1; 64 ret->mtime = -1;
65 ret->about_filter = ctx.cfg.about_filter;
66 ret->commit_filter = ctx.cfg.commit_filter;
67 ret->source_filter = ctx.cfg.source_filter;
65 return ret; 68 return ret;
@@ -357 +360,36 @@ int cgit_parse_snapshots_mask(const char *str)
357} 360}
361
362int cgit_open_filter(struct cgit_filter *filter)
363{
364
365 filter->old_stdout = chk_positive(dup(STDOUT_FILENO),
366 "Unable to duplicate STDOUT");
367 chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess");
368 filter->pid = chk_non_negative(fork(), "Unable to create subprocess");
369 if (filter->pid == 0) {
370 close(filter->pipe_fh[1]);
371 chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
372 "Unable to use pipe as STDIN");
373 execvp(filter->cmd, filter->argv);
374 die("Unable to exec subprocess %s: %s (%d)", filter->cmd,
375 strerror(errno), errno);
376 }
377 close(filter->pipe_fh[0]);
378 chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO),
379 "Unable to use pipe as STDOUT");
380 close(filter->pipe_fh[1]);
381 return 0;
382}
383
384int cgit_close_filter(struct cgit_filter *filter)
385{
386 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
387 "Unable to restore STDOUT");
388 close(filter->old_stdout);
389 if (filter->pid < 0)
390 return 0;
391 waitpid(filter->pid, &filter->exitstatus, 0);
392 if (WIFEXITED(filter->exitstatus) && !WEXITSTATUS(filter->exitstatus))
393 return 0;
394 die("Subprocess %s exited abnormally", filter->cmd);
395}