Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 1 | /* filter.c: filter framework functions |
| 2 | * |
| 3 | * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> |
| 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
| 9 | #include "cgit.h" |
| 10 | #include <sys/types.h> |
| 11 | #include <sys/wait.h> |
| 12 | #include <unistd.h> |
| 13 | #include <string.h> |
| 14 | #include <stdlib.h> |
| 15 | |
John Keeping | 7bd90b8 | 2014-01-12 17:13:52 +0000 | [diff] [blame] | 16 | static int open_exec_filter(struct cgit_filter *base, va_list ap) |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 17 | { |
John Keeping | 7bd90b8 | 2014-01-12 17:13:52 +0000 | [diff] [blame] | 18 | struct cgit_exec_filter *filter = (struct cgit_exec_filter *) base; |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 19 | int i; |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 20 | |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 21 | for (i = 0; i < filter->extra_args; i++) |
| 22 | filter->argv[i+1] = va_arg(ap, char *); |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 23 | |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 24 | filter->old_stdout = chk_positive(dup(STDOUT_FILENO), |
| 25 | "Unable to duplicate STDOUT"); |
| 26 | chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess"); |
| 27 | filter->pid = chk_non_negative(fork(), "Unable to create subprocess"); |
| 28 | if (filter->pid == 0) { |
| 29 | close(filter->pipe_fh[1]); |
| 30 | chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO), |
| 31 | "Unable to use pipe as STDIN"); |
| 32 | execvp(filter->cmd, filter->argv); |
| 33 | die_errno("Unable to exec subprocess %s", filter->cmd); |
| 34 | } |
| 35 | close(filter->pipe_fh[0]); |
| 36 | chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO), |
| 37 | "Unable to use pipe as STDOUT"); |
| 38 | close(filter->pipe_fh[1]); |
| 39 | return 0; |
| 40 | } |
| 41 | |
John Keeping | 7bd90b8 | 2014-01-12 17:13:52 +0000 | [diff] [blame] | 42 | static int close_exec_filter(struct cgit_filter *base) |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 43 | { |
John Keeping | 7bd90b8 | 2014-01-12 17:13:52 +0000 | [diff] [blame] | 44 | struct cgit_exec_filter *filter = (struct cgit_exec_filter *) base; |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 45 | int i, exit_status; |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 46 | |
| 47 | chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO), |
| 48 | "Unable to restore STDOUT"); |
| 49 | close(filter->old_stdout); |
| 50 | if (filter->pid < 0) |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 51 | goto done; |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 52 | waitpid(filter->pid, &exit_status, 0); |
| 53 | if (WIFEXITED(exit_status) && !WEXITSTATUS(exit_status)) |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 54 | goto done; |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 55 | die("Subprocess %s exited abnormally", filter->cmd); |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 56 | |
| 57 | done: |
| 58 | for (i = 0; i < filter->extra_args; i++) |
| 59 | filter->argv[i+1] = NULL; |
| 60 | return 0; |
| 61 | |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 62 | } |
| 63 | |
John Keeping | 7bd90b8 | 2014-01-12 17:13:52 +0000 | [diff] [blame] | 64 | static void fprintf_exec_filter(struct cgit_filter *base, FILE *f, const char *prefix) |
John Keeping | 632efb2 | 2014-01-12 17:13:51 +0000 | [diff] [blame] | 65 | { |
John Keeping | 7bd90b8 | 2014-01-12 17:13:52 +0000 | [diff] [blame] | 66 | struct cgit_exec_filter *filter = (struct cgit_exec_filter *) base; |
John Keeping | 4bb87cb | 2014-01-12 17:13:53 +0000 | [diff] [blame^] | 67 | fprintf(f, "%sexec:%s\n", prefix, filter->cmd); |
John Keeping | 632efb2 | 2014-01-12 17:13:51 +0000 | [diff] [blame] | 68 | } |
| 69 | |
John Keeping | 7bd90b8 | 2014-01-12 17:13:52 +0000 | [diff] [blame] | 70 | int cgit_open_filter(struct cgit_filter *filter, ...) |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 71 | { |
John Keeping | 7bd90b8 | 2014-01-12 17:13:52 +0000 | [diff] [blame] | 72 | int result; |
| 73 | va_list ap; |
| 74 | va_start(ap, filter); |
| 75 | result = filter->open(filter, ap); |
| 76 | va_end(ap); |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | int cgit_close_filter(struct cgit_filter *filter) |
| 81 | { |
| 82 | return filter->close(filter); |
| 83 | } |
| 84 | |
| 85 | void cgit_fprintf_filter(struct cgit_filter *filter, FILE *f, const char *prefix) |
| 86 | { |
| 87 | filter->fprintf(filter, f, prefix); |
| 88 | } |
| 89 | |
| 90 | void cgit_exec_filter_init(struct cgit_exec_filter *filter, char *cmd, char **argv) |
| 91 | { |
| 92 | memset(filter, 0, sizeof(*filter)); |
| 93 | filter->base.open = open_exec_filter; |
| 94 | filter->base.close = close_exec_filter; |
| 95 | filter->base.fprintf = fprintf_exec_filter; |
| 96 | filter->cmd = cmd; |
| 97 | filter->argv = argv; |
| 98 | } |
| 99 | |
| 100 | static struct cgit_filter *new_exec_filter(const char *cmd, filter_type filtertype) |
| 101 | { |
| 102 | struct cgit_exec_filter *f; |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 103 | int args_size = 0; |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 104 | |
John Keeping | 7bd90b8 | 2014-01-12 17:13:52 +0000 | [diff] [blame] | 105 | f = xmalloc(sizeof(*f)); |
| 106 | /* We leave argv for now and assign it below. */ |
| 107 | cgit_exec_filter_init(f, xstrdup(cmd), NULL); |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 108 | |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 109 | switch (filtertype) { |
| 110 | case SOURCE: |
| 111 | case ABOUT: |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 112 | f->extra_args = 1; |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 113 | break; |
| 114 | |
| 115 | case COMMIT: |
| 116 | default: |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 117 | f->extra_args = 0; |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 118 | break; |
| 119 | } |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 120 | |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 121 | args_size = (2 + f->extra_args) * sizeof(char *); |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 122 | f->argv = xmalloc(args_size); |
| 123 | memset(f->argv, 0, args_size); |
| 124 | f->argv[0] = f->cmd; |
John Keeping | 7bd90b8 | 2014-01-12 17:13:52 +0000 | [diff] [blame] | 125 | return &f->base; |
| 126 | } |
| 127 | |
John Keeping | 4bb87cb | 2014-01-12 17:13:53 +0000 | [diff] [blame^] | 128 | static const struct { |
| 129 | const char *prefix; |
| 130 | struct cgit_filter *(*ctor)(const char *cmd, filter_type filtertype); |
| 131 | } filter_specs[] = { |
| 132 | { "exec", new_exec_filter }, |
| 133 | }; |
| 134 | |
John Keeping | 7bd90b8 | 2014-01-12 17:13:52 +0000 | [diff] [blame] | 135 | struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype) |
| 136 | { |
John Keeping | 4bb87cb | 2014-01-12 17:13:53 +0000 | [diff] [blame^] | 137 | char *colon; |
| 138 | int i; |
| 139 | size_t len; |
John Keeping | 7bd90b8 | 2014-01-12 17:13:52 +0000 | [diff] [blame] | 140 | if (!cmd || !cmd[0]) |
| 141 | return NULL; |
| 142 | |
John Keeping | 4bb87cb | 2014-01-12 17:13:53 +0000 | [diff] [blame^] | 143 | colon = strchr(cmd, ':'); |
| 144 | len = colon - cmd; |
| 145 | /* |
| 146 | * In case we're running on Windows, don't allow a single letter before |
| 147 | * the colon. |
| 148 | */ |
| 149 | if (len == 1) |
| 150 | colon = NULL; |
| 151 | |
| 152 | /* If no prefix is given, exec filter is the default. */ |
| 153 | if (!colon) |
| 154 | return new_exec_filter(cmd, filtertype); |
| 155 | |
| 156 | for (i = 0; i < ARRAY_SIZE(filter_specs); i++) { |
| 157 | if (len == strlen(filter_specs[i].prefix) && |
| 158 | !strncmp(filter_specs[i].prefix, cmd, len)) |
| 159 | return filter_specs[i].ctor(colon + 1, filtertype); |
| 160 | } |
| 161 | |
| 162 | die("Invalid filter type: %.*s", (int) len, cmd); |
Jason A. Donenfeld | 3eae406 | 2014-01-10 05:19:05 +0100 | [diff] [blame] | 163 | } |