]> git.cameronkatri.com Git - cgit.git/blob - filter.c
filter: basic write hooking infrastructure
[cgit.git] / filter.c
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 #include <dlfcn.h>
16
17 static ssize_t (*libc_write)(int fd, const void *buf, size_t count);
18 static ssize_t (*filter_write)(struct cgit_filter *base, const void *buf, size_t count) = NULL;
19 static struct cgit_filter *current_write_filter = NULL;
20
21 static inline void reap_filter(struct cgit_filter *filter)
22 {
23 if (filter && filter->cleanup)
24 filter->cleanup(filter);
25 }
26
27 void cgit_cleanup_filters(void)
28 {
29 int i;
30 reap_filter(ctx.cfg.about_filter);
31 reap_filter(ctx.cfg.commit_filter);
32 reap_filter(ctx.cfg.source_filter);
33 for (i = 0; i < cgit_repolist.count; ++i) {
34 reap_filter(cgit_repolist.repos[i].about_filter);
35 reap_filter(cgit_repolist.repos[i].commit_filter);
36 reap_filter(cgit_repolist.repos[i].source_filter);
37 }
38 }
39
40 void cgit_init_filters(void)
41 {
42 libc_write = dlsym(RTLD_NEXT, "write");
43 if (!libc_write)
44 die("Could not locate libc's write function");
45 }
46
47 ssize_t write(int fd, const void *buf, size_t count)
48 {
49 if (fd != STDOUT_FILENO || !filter_write)
50 return libc_write(fd, buf, count);
51 return filter_write(current_write_filter, buf, count);
52 }
53
54 static inline void hook_write(struct cgit_filter *filter, ssize_t (*new_write)(struct cgit_filter *base, const void *buf, size_t count))
55 {
56 /* We want to avoid buggy nested patterns. */
57 assert(filter_write == NULL);
58 assert(current_write_filter == NULL);
59 current_write_filter = filter;
60 filter_write = new_write;
61 }
62
63 static inline void unhook_write()
64 {
65 assert(filter_write != NULL);
66 assert(current_write_filter != NULL);
67 filter_write = NULL;
68 current_write_filter = NULL;
69 }
70
71 static int open_exec_filter(struct cgit_filter *base, va_list ap)
72 {
73 struct cgit_exec_filter *filter = (struct cgit_exec_filter *) base;
74 int i;
75
76 for (i = 0; i < filter->base.argument_count; i++)
77 filter->argv[i+1] = va_arg(ap, char *);
78
79 filter->old_stdout = chk_positive(dup(STDOUT_FILENO),
80 "Unable to duplicate STDOUT");
81 chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess");
82 filter->pid = chk_non_negative(fork(), "Unable to create subprocess");
83 if (filter->pid == 0) {
84 close(filter->pipe_fh[1]);
85 chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
86 "Unable to use pipe as STDIN");
87 execvp(filter->cmd, filter->argv);
88 die_errno("Unable to exec subprocess %s", filter->cmd);
89 }
90 close(filter->pipe_fh[0]);
91 chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO),
92 "Unable to use pipe as STDOUT");
93 close(filter->pipe_fh[1]);
94 return 0;
95 }
96
97 static int close_exec_filter(struct cgit_filter *base)
98 {
99 struct cgit_exec_filter *filter = (struct cgit_exec_filter *) base;
100 int i, exit_status;
101
102 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
103 "Unable to restore STDOUT");
104 close(filter->old_stdout);
105 if (filter->pid < 0)
106 goto done;
107 waitpid(filter->pid, &exit_status, 0);
108 if (WIFEXITED(exit_status) && !WEXITSTATUS(exit_status))
109 goto done;
110 die("Subprocess %s exited abnormally", filter->cmd);
111
112 done:
113 for (i = 0; i < filter->base.argument_count; i++)
114 filter->argv[i+1] = NULL;
115 return 0;
116
117 }
118
119 static void fprintf_exec_filter(struct cgit_filter *base, FILE *f, const char *prefix)
120 {
121 struct cgit_exec_filter *filter = (struct cgit_exec_filter *) base;
122 fprintf(f, "%sexec:%s\n", prefix, filter->cmd);
123 }
124
125 static void cleanup_exec_filter(struct cgit_filter *base)
126 {
127 struct cgit_exec_filter *filter = (struct cgit_exec_filter *) base;
128 if (filter->argv) {
129 free(filter->argv);
130 filter->argv = NULL;
131 }
132 if (filter->cmd) {
133 free(filter->cmd);
134 filter->cmd = NULL;
135 }
136 }
137
138 static struct cgit_filter *new_exec_filter(const char *cmd, int argument_count)
139 {
140 struct cgit_exec_filter *f;
141 int args_size = 0;
142
143 f = xmalloc(sizeof(*f));
144 /* We leave argv for now and assign it below. */
145 cgit_exec_filter_init(f, xstrdup(cmd), NULL);
146 f->base.argument_count = argument_count;
147 args_size = (2 + argument_count) * sizeof(char *);
148 f->argv = xmalloc(args_size);
149 memset(f->argv, 0, args_size);
150 f->argv[0] = f->cmd;
151 return &f->base;
152 }
153
154 void cgit_exec_filter_init(struct cgit_exec_filter *filter, char *cmd, char **argv)
155 {
156 memset(filter, 0, sizeof(*filter));
157 filter->base.open = open_exec_filter;
158 filter->base.close = close_exec_filter;
159 filter->base.fprintf = fprintf_exec_filter;
160 filter->base.cleanup = cleanup_exec_filter;
161 filter->cmd = cmd;
162 filter->argv = argv;
163 /* The argument count for open_filter is zero by default, unless called from new_filter, above. */
164 filter->base.argument_count = 0;
165 }
166
167 int cgit_open_filter(struct cgit_filter *filter, ...)
168 {
169 int result;
170 va_list ap;
171 va_start(ap, filter);
172 result = filter->open(filter, ap);
173 va_end(ap);
174 return result;
175 }
176
177 int cgit_close_filter(struct cgit_filter *filter)
178 {
179 return filter->close(filter);
180 }
181
182 void cgit_fprintf_filter(struct cgit_filter *filter, FILE *f, const char *prefix)
183 {
184 filter->fprintf(filter, f, prefix);
185 }
186
187
188
189 static const struct {
190 const char *prefix;
191 struct cgit_filter *(*ctor)(const char *cmd, int argument_count);
192 } filter_specs[] = {
193 { "exec", new_exec_filter },
194 };
195
196 struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype)
197 {
198 char *colon;
199 int i;
200 size_t len;
201 int argument_count;
202
203 if (!cmd || !cmd[0])
204 return NULL;
205
206 colon = strchr(cmd, ':');
207 len = colon - cmd;
208 /*
209 * In case we're running on Windows, don't allow a single letter before
210 * the colon.
211 */
212 if (len == 1)
213 colon = NULL;
214
215 switch (filtertype) {
216 case SOURCE:
217 case ABOUT:
218 argument_count = 1;
219 break;
220
221 case COMMIT:
222 default:
223 argument_count = 0;
224 break;
225 }
226
227 /* If no prefix is given, exec filter is the default. */
228 if (!colon)
229 return new_exec_filter(cmd, argument_count);
230
231 for (i = 0; i < ARRAY_SIZE(filter_specs); i++) {
232 if (len == strlen(filter_specs[i].prefix) &&
233 !strncmp(filter_specs[i].prefix, cmd, len))
234 return filter_specs[i].ctor(colon + 1, argument_count);
235 }
236
237 die("Invalid filter type: %.*s", (int) len, cmd);
238 }