aboutsummaryrefslogtreecommitdiffstats
path: root/filter.c
diff options
context:
space:
mode:
authorGravatar Jason A. Donenfeld <Jason@zx2c4.com>2014-01-15 02:07:23 (JST)
committerGravatar Jason A. Donenfeld <Jason@zx2c4.com>2014-01-15 02:09:52 (JST)
commit6ca734da8fb246ad2272826331e0d56428b96fa1 (patch)
tree01abfea3df45135b1114996de29093b6eed7023a /filter.c
parentce56d89a2662549acd178292450798f5ffcd4bc6 (diff)
downloadcgit-6ca734da8fb246ad2272826331e0d56428b96fa1.zip
cgit-6ca734da8fb246ad2272826331e0d56428b96fa1.tar.gz
filter: allow returning exit code from filter
Filters can now indicate a status back to cgit by means of the exit code for exec, or the return value from close for Lua. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'filter.c')
-rw-r--r--filter.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/filter.c b/filter.c
index 4d4acaf..0cce7bb 100644
--- a/filter.c
+++ b/filter.c
@@ -106,7 +106,7 @@ static int open_exec_filter(struct cgit_filter *base, va_list ap)
106static int close_exec_filter(struct cgit_filter *base) 106static int close_exec_filter(struct cgit_filter *base)
107{ 107{
108 struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base; 108 struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
109 int i, exit_status; 109 int i, exit_status = 0;
110 110
111 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO), 111 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
112 "Unable to restore STDOUT"); 112 "Unable to restore STDOUT");
@@ -114,14 +114,14 @@ static int close_exec_filter(struct cgit_filter *base)
114 if (filter->pid < 0) 114 if (filter->pid < 0)
115 goto done; 115 goto done;
116 waitpid(filter->pid, &exit_status, 0); 116 waitpid(filter->pid, &exit_status, 0);
117 if (WIFEXITED(exit_status) && !WEXITSTATUS(exit_status)) 117 if (WIFEXITED(exit_status))
118 goto done; 118 goto done;
119 die("Subprocess %s exited abnormally", filter->cmd); 119 die("Subprocess %s exited abnormally", filter->cmd);
120 120
121done: 121done:
122 for (i = 0; i < filter->base.argument_count; i++) 122 for (i = 0; i < filter->base.argument_count; i++)
123 filter->argv[i + 1] = NULL; 123 filter->argv[i + 1] = NULL;
124 return 0; 124 return WEXITSTATUS(exit_status);
125 125
126} 126}
127 127
@@ -315,10 +315,14 @@ static int close_lua_filter(struct cgit_filter *base)
315 int ret = 0; 315 int ret = 0;
316 316
317 lua_getglobal(filter->lua_state, "filter_close"); 317 lua_getglobal(filter->lua_state, "filter_close");
318 if (lua_pcall(filter->lua_state, 0, 0, 0)) { 318 if (lua_pcall(filter->lua_state, 0, 1, 0)) {
319 error_lua_filter(filter); 319 error_lua_filter(filter);
320 ret = 1; 320 ret = -1;
321 } else {
322 ret = lua_tonumber(filter->lua_state, -1);
323 lua_pop(filter->lua_state, 1);
321 } 324 }
325
322 unhook_write(); 326 unhook_write();
323 return ret; 327 return ret;
324} 328}