]> granicus.if.org Git - cgit/commitdiff
filter: don't use dlsym unnecessarily
authorJohn Keeping <john@keeping.me.uk>
Thu, 13 Aug 2015 11:14:20 +0000 (12:14 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Thu, 13 Aug 2015 13:39:06 +0000 (15:39 +0200)
We only need to hook write() if Lua filter's are in use.  If support has
been disabled, remove the dependency on dlsym().

Signed-off-by: John Keeping <john@keeping.me.uk>
filter.c

index c7037a30ab82c4901b616dd056de94997a071f46..949c931af1f67cb48df4554d6b1d7ca87d897ce2 100644 (file)
--- a/filter.c
+++ b/filter.c
@@ -8,17 +8,13 @@
 
 #include "cgit.h"
 #include "html.h"
-#include <dlfcn.h>
 #ifndef NO_LUA
+#include <dlfcn.h>
 #include <lua.h>
 #include <lualib.h>
 #include <lauxlib.h>
 #endif
 
-static ssize_t (*libc_write)(int fd, const void *buf, size_t count);
-static ssize_t (*filter_write)(struct cgit_filter *base, const void *buf, size_t count) = NULL;
-static struct cgit_filter *current_write_filter = NULL;
-
 static inline void reap_filter(struct cgit_filter *filter)
 {
        if (filter && filter->cleanup)
@@ -43,37 +39,6 @@ void cgit_cleanup_filters(void)
        }
 }
 
-void cgit_init_filters(void)
-{
-       libc_write = dlsym(RTLD_NEXT, "write");
-       if (!libc_write)
-               die("Could not locate libc's write function");
-}
-
-ssize_t write(int fd, const void *buf, size_t count)
-{
-       if (fd != STDOUT_FILENO || !filter_write)
-               return libc_write(fd, buf, count);
-       return filter_write(current_write_filter, buf, count);
-}
-
-static inline void hook_write(struct cgit_filter *filter, ssize_t (*new_write)(struct cgit_filter *base, const void *buf, size_t count))
-{
-       /* We want to avoid buggy nested patterns. */
-       assert(filter_write == NULL);
-       assert(current_write_filter == NULL);
-       current_write_filter = filter;
-       filter_write = new_write;
-}
-
-static inline void unhook_write(void)
-{
-       assert(filter_write != NULL);
-       assert(current_write_filter != NULL);
-       filter_write = NULL;
-       current_write_filter = NULL;
-}
-
 static int open_exec_filter(struct cgit_filter *base, va_list ap)
 {
        struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
@@ -170,7 +135,48 @@ void cgit_exec_filter_init(struct cgit_exec_filter *filter, char *cmd, char **ar
        filter->base.argument_count = 0;
 }
 
+#ifdef NO_LUA
+void cgit_init_filters(void)
+{
+}
+#endif
+
 #ifndef NO_LUA
+static ssize_t (*libc_write)(int fd, const void *buf, size_t count);
+static ssize_t (*filter_write)(struct cgit_filter *base, const void *buf, size_t count) = NULL;
+static struct cgit_filter *current_write_filter = NULL;
+
+void cgit_init_filters(void)
+{
+       libc_write = dlsym(RTLD_NEXT, "write");
+       if (!libc_write)
+               die("Could not locate libc's write function");
+}
+
+ssize_t write(int fd, const void *buf, size_t count)
+{
+       if (fd != STDOUT_FILENO || !filter_write)
+               return libc_write(fd, buf, count);
+       return filter_write(current_write_filter, buf, count);
+}
+
+static inline void hook_write(struct cgit_filter *filter, ssize_t (*new_write)(struct cgit_filter *base, const void *buf, size_t count))
+{
+       /* We want to avoid buggy nested patterns. */
+       assert(filter_write == NULL);
+       assert(current_write_filter == NULL);
+       current_write_filter = filter;
+       filter_write = new_write;
+}
+
+static inline void unhook_write(void)
+{
+       assert(filter_write != NULL);
+       assert(current_write_filter != NULL);
+       filter_write = NULL;
+       current_write_filter = NULL;
+}
+
 struct lua_filter {
        struct cgit_filter base;
        char *script_file;