]> gitweb.ps.run Git - ps-cgit/blob - filter.c
filter: allow returning exit code from filter
[ps-cgit] / 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 "html.h"
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <dlfcn.h>
17 #include <errno.h>
18 #ifndef NO_LUA
19 #include <lua.h>
20 #include <lualib.h>
21 #include <lauxlib.h>
22 #endif
23
24 static ssize_t (*libc_write)(int fd, const void *buf, size_t count);
25 static ssize_t (*filter_write)(struct cgit_filter *base, const void *buf, size_t count) = NULL;
26 static struct cgit_filter *current_write_filter = NULL;
27
28 static inline void reap_filter(struct cgit_filter *filter)
29 {
30         if (filter && filter->cleanup)
31                 filter->cleanup(filter);
32 }
33
34 void cgit_cleanup_filters(void)
35 {
36         int i;
37         reap_filter(ctx.cfg.about_filter);
38         reap_filter(ctx.cfg.commit_filter);
39         reap_filter(ctx.cfg.source_filter);
40         reap_filter(ctx.cfg.email_filter);
41         for (i = 0; i < cgit_repolist.count; ++i) {
42                 reap_filter(cgit_repolist.repos[i].about_filter);
43                 reap_filter(cgit_repolist.repos[i].commit_filter);
44                 reap_filter(cgit_repolist.repos[i].source_filter);
45                 reap_filter(cgit_repolist.repos[i].email_filter);
46         }
47 }
48
49 void cgit_init_filters(void)
50 {
51         libc_write = dlsym(RTLD_NEXT, "write");
52         if (!libc_write)
53                 die("Could not locate libc's write function");
54 }
55
56 ssize_t write(int fd, const void *buf, size_t count)
57 {
58         if (fd != STDOUT_FILENO || !filter_write)
59                 return libc_write(fd, buf, count);
60         return filter_write(current_write_filter, buf, count);
61 }
62
63 static inline void hook_write(struct cgit_filter *filter, ssize_t (*new_write)(struct cgit_filter *base, const void *buf, size_t count))
64 {
65         /* We want to avoid buggy nested patterns. */
66         assert(filter_write == NULL);
67         assert(current_write_filter == NULL);
68         current_write_filter = filter;
69         filter_write = new_write;
70 }
71
72 static inline void unhook_write()
73 {
74         assert(filter_write != NULL);
75         assert(current_write_filter != NULL);
76         filter_write = NULL;
77         current_write_filter = NULL;
78 }
79
80 static int open_exec_filter(struct cgit_filter *base, va_list ap)
81 {
82         struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
83         int i;
84
85         for (i = 0; i < filter->base.argument_count; i++)
86                 filter->argv[i + 1] = va_arg(ap, char *);
87
88         filter->old_stdout = chk_positive(dup(STDOUT_FILENO),
89                 "Unable to duplicate STDOUT");
90         chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess");
91         filter->pid = chk_non_negative(fork(), "Unable to create subprocess");
92         if (filter->pid == 0) {
93                 close(filter->pipe_fh[1]);
94                 chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
95                         "Unable to use pipe as STDIN");
96                 execvp(filter->cmd, filter->argv);
97                 die_errno("Unable to exec subprocess %s", filter->cmd);
98         }
99         close(filter->pipe_fh[0]);
100         chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO),
101                 "Unable to use pipe as STDOUT");
102         close(filter->pipe_fh[1]);
103         return 0;
104 }
105
106 static int close_exec_filter(struct cgit_filter *base)
107 {
108         struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
109         int i, exit_status = 0;
110
111         chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
112                 "Unable to restore STDOUT");
113         close(filter->old_stdout);
114         if (filter->pid < 0)
115                 goto done;
116         waitpid(filter->pid, &exit_status, 0);
117         if (WIFEXITED(exit_status))
118                 goto done;
119         die("Subprocess %s exited abnormally", filter->cmd);
120
121 done:
122         for (i = 0; i < filter->base.argument_count; i++)
123                 filter->argv[i + 1] = NULL;
124         return WEXITSTATUS(exit_status);
125
126 }
127
128 static void fprintf_exec_filter(struct cgit_filter *base, FILE *f, const char *prefix)
129 {
130         struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
131         fprintf(f, "%sexec:%s\n", prefix, filter->cmd);
132 }
133
134 static void cleanup_exec_filter(struct cgit_filter *base)
135 {
136         struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
137         if (filter->argv) {
138                 free(filter->argv);
139                 filter->argv = NULL;
140         }
141         if (filter->cmd) {
142                 free(filter->cmd);
143                 filter->cmd = NULL;
144         }
145 }
146
147 static struct cgit_filter *new_exec_filter(const char *cmd, int argument_count)
148 {
149         struct cgit_exec_filter *f;
150         int args_size = 0;
151
152         f = xmalloc(sizeof(*f));
153         /* We leave argv for now and assign it below. */
154         cgit_exec_filter_init(f, xstrdup(cmd), NULL);
155         f->base.argument_count = argument_count;
156         args_size = (2 + argument_count) * sizeof(char *);
157         f->argv = xmalloc(args_size);
158         memset(f->argv, 0, args_size);
159         f->argv[0] = f->cmd;
160         return &f->base;
161 }
162
163 void cgit_exec_filter_init(struct cgit_exec_filter *filter, char *cmd, char **argv)
164 {
165         memset(filter, 0, sizeof(*filter));
166         filter->base.open = open_exec_filter;
167         filter->base.close = close_exec_filter;
168         filter->base.fprintf = fprintf_exec_filter;
169         filter->base.cleanup = cleanup_exec_filter;
170         filter->cmd = cmd;
171         filter->argv = argv;
172         /* The argument count for open_filter is zero by default, unless called from new_filter, above. */
173         filter->base.argument_count = 0;
174 }
175
176 #ifndef NO_LUA
177 struct lua_filter {
178         struct cgit_filter base;
179         char *script_file;
180         lua_State *lua_state;
181 };
182
183 static void error_lua_filter(struct lua_filter *filter)
184 {
185         die("Lua error in %s: %s", filter->script_file, lua_tostring(filter->lua_state, -1));
186         lua_pop(filter->lua_state, 1);
187 }
188
189 static ssize_t write_lua_filter(struct cgit_filter *base, const void *buf, size_t count)
190 {
191         struct lua_filter *filter = (struct lua_filter *)base;
192
193         lua_getglobal(filter->lua_state, "filter_write");
194         lua_pushlstring(filter->lua_state, buf, count);
195         if (lua_pcall(filter->lua_state, 1, 0, 0)) {
196                 error_lua_filter(filter);
197                 errno = EIO;
198                 return -1;
199         }
200         return count;
201 }
202
203 static inline int hook_lua_filter(lua_State *lua_state, void (*fn)(const char *txt))
204 {
205         const char *str;
206         ssize_t (*save_filter_write)(struct cgit_filter *base, const void *buf, size_t count);
207         struct cgit_filter *save_filter;
208
209         str = lua_tostring(lua_state, 1);
210         if (!str)
211                 return 0;
212         
213         save_filter_write = filter_write;
214         save_filter = current_write_filter;
215         unhook_write();
216         fn(str);
217         hook_write(save_filter, save_filter_write);
218
219         return 0;
220 }
221
222 static int html_lua_filter(lua_State *lua_state)
223 {
224         return hook_lua_filter(lua_state, html);
225 }
226
227 static int html_txt_lua_filter(lua_State *lua_state)
228 {
229         return hook_lua_filter(lua_state, html_txt);
230 }
231
232 static int html_attr_lua_filter(lua_State *lua_state)
233 {
234         return hook_lua_filter(lua_state, html_attr);
235 }
236
237 static int html_url_path_lua_filter(lua_State *lua_state)
238 {
239         return hook_lua_filter(lua_state, html_url_path);
240 }
241
242 static int html_url_arg_lua_filter(lua_State *lua_state)
243 {
244         return hook_lua_filter(lua_state, html_url_arg);
245 }
246
247 static void cleanup_lua_filter(struct cgit_filter *base)
248 {
249         struct lua_filter *filter = (struct lua_filter *)base;
250
251         if (!filter->lua_state)
252                 return;
253
254         lua_close(filter->lua_state);
255         filter->lua_state = NULL;
256         if (filter->script_file) {
257                 free(filter->script_file);
258                 filter->script_file = NULL;
259         }
260 }
261
262 static int init_lua_filter(struct lua_filter *filter)
263 {
264         if (filter->lua_state)
265                 return 0;
266
267         if (!(filter->lua_state = luaL_newstate()))
268                 return 1;
269
270         luaL_openlibs(filter->lua_state);
271
272         lua_pushcfunction(filter->lua_state, html_lua_filter);
273         lua_setglobal(filter->lua_state, "html");
274         lua_pushcfunction(filter->lua_state, html_txt_lua_filter);
275         lua_setglobal(filter->lua_state, "html_txt");
276         lua_pushcfunction(filter->lua_state, html_attr_lua_filter);
277         lua_setglobal(filter->lua_state, "html_attr");
278         lua_pushcfunction(filter->lua_state, html_url_path_lua_filter);
279         lua_setglobal(filter->lua_state, "html_url_path");
280         lua_pushcfunction(filter->lua_state, html_url_arg_lua_filter);
281         lua_setglobal(filter->lua_state, "html_url_arg");
282
283         if (luaL_dofile(filter->lua_state, filter->script_file)) {
284                 error_lua_filter(filter);
285                 lua_close(filter->lua_state);
286                 filter->lua_state = NULL;
287                 return 1;
288         }
289         return 0;
290 }
291
292 static int open_lua_filter(struct cgit_filter *base, va_list ap)
293 {
294         struct lua_filter *filter = (struct lua_filter *)base;
295         int i;
296
297         if (init_lua_filter(filter))
298                 return 1;
299
300         hook_write(base, write_lua_filter);
301
302         lua_getglobal(filter->lua_state, "filter_open");
303         for (i = 0; i < filter->base.argument_count; ++i)
304                 lua_pushstring(filter->lua_state, va_arg(ap, char *));
305         if (lua_pcall(filter->lua_state, filter->base.argument_count, 0, 0)) {
306                 error_lua_filter(filter);
307                 return 1;
308         }
309         return 0;
310 }
311
312 static int close_lua_filter(struct cgit_filter *base)
313 {
314         struct lua_filter *filter = (struct lua_filter *)base;
315         int ret = 0;
316
317         lua_getglobal(filter->lua_state, "filter_close");
318         if (lua_pcall(filter->lua_state, 0, 1, 0)) {
319                 error_lua_filter(filter);
320                 ret = -1;
321         } else {
322                 ret = lua_tonumber(filter->lua_state, -1);
323                 lua_pop(filter->lua_state, 1);
324         }
325
326         unhook_write();
327         return ret;
328 }
329
330 static void fprintf_lua_filter(struct cgit_filter *base, FILE *f, const char *prefix)
331 {
332         struct lua_filter *filter = (struct lua_filter *)base;
333         fprintf(f, "%slua:%s\n", prefix, filter->script_file);
334 }
335
336
337 static struct cgit_filter *new_lua_filter(const char *cmd, int argument_count)
338 {
339         struct lua_filter *filter;
340
341         filter = xmalloc(sizeof(*filter));
342         memset(filter, 0, sizeof(*filter));
343         filter->base.open = open_lua_filter;
344         filter->base.close = close_lua_filter;
345         filter->base.fprintf = fprintf_lua_filter;
346         filter->base.cleanup = cleanup_lua_filter;
347         filter->base.argument_count = argument_count;
348         filter->script_file = xstrdup(cmd);
349
350         return &filter->base;
351 }
352
353 #endif
354
355
356 int cgit_open_filter(struct cgit_filter *filter, ...)
357 {
358         int result;
359         va_list ap;
360         if (!filter)
361                 return 0;
362         va_start(ap, filter);
363         result = filter->open(filter, ap);
364         va_end(ap);
365         return result;
366 }
367
368 int cgit_close_filter(struct cgit_filter *filter)
369 {
370         if (!filter)
371                 return 0;
372         return filter->close(filter);
373 }
374
375 void cgit_fprintf_filter(struct cgit_filter *filter, FILE *f, const char *prefix)
376 {
377         filter->fprintf(filter, f, prefix);
378 }
379
380
381
382 static const struct {
383         const char *prefix;
384         struct cgit_filter *(*ctor)(const char *cmd, int argument_count);
385 } filter_specs[] = {
386         { "exec", new_exec_filter },
387 #ifndef NO_LUA
388         { "lua", new_lua_filter },
389 #endif
390 };
391
392 struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype)
393 {
394         char *colon;
395         int i;
396         size_t len;
397         int argument_count;
398
399         if (!cmd || !cmd[0])
400                 return NULL;
401
402         colon = strchr(cmd, ':');
403         len = colon - cmd;
404         /*
405          * In case we're running on Windows, don't allow a single letter before
406          * the colon.
407          */
408         if (len == 1)
409                 colon = NULL;
410
411         switch (filtertype) {
412                 case EMAIL:
413                         argument_count = 2;
414                         break;
415
416                 case SOURCE:
417                 case ABOUT:
418                         argument_count = 1;
419                         break;
420
421                 case COMMIT:
422                 default:
423                         argument_count = 0;
424                         break;
425         }
426
427         /* If no prefix is given, exec filter is the default. */
428         if (!colon)
429                 return new_exec_filter(cmd, argument_count);
430
431         for (i = 0; i < ARRAY_SIZE(filter_specs); i++) {
432                 if (len == strlen(filter_specs[i].prefix) &&
433                     !strncmp(filter_specs[i].prefix, cmd, len))
434                         return filter_specs[i].ctor(colon + 1, argument_count);
435         }
436
437         die("Invalid filter type: %.*s", (int) len, cmd);
438 }