From 0c4d76755b98bb597279a1930bf4c69eca7dde62 Mon Sep 17 00:00:00 2001 From: John Keeping Date: Thu, 13 Aug 2015 12:14:20 +0100 Subject: filter: don't use dlsym unnecessarily 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 --- filter.c | 78 ++++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/filter.c b/filter.c index c7037a3..949c931 100644 --- a/filter.c +++ b/filter.c @@ -8,17 +8,13 @@ #include "cgit.h" #include "html.h" -#include #ifndef NO_LUA +#include #include #include #include #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; -- cgit 1.4.1 d35f867e&follow=1'>Tweak filter regexesJune McEnroe 2019-11-14Filter multi-prefixJune McEnroe 2019-11-14Replace filters with regex replacesJune McEnroe 2019-11-13Refactor filterUserhostInNamesJune McEnroe 2019-11-13Factor out wordcpy for filtersJune McEnroe 2019-11-11Revert "Test getopt_config"June McEnroe 2019-11-11Test getopt_configJune McEnroe 2019-11-11Add userhost-in-names to manualJune McEnroe 2019-11-11Skip initial NAMES parametersJune McEnroe 2019-11-11Filter userhost-in-namesJune McEnroe 2019-11-10Compare words without copying in filtersJune McEnroe 2019-11-10Separate tags from all targetJune McEnroe 2019-11-10Filter invite-notifyJune McEnroe 2019-11-10Add capsicum note to READMEJune McEnroe 2019-11-10Filter extended-joinJune McEnroe 2019-11-10Expand client configuration documentation and list capabilitiesJune McEnroe 2019-11-10Request all supported caps from serverJune McEnroe 2019-11-10Filter ACCOUNT, AWAY, CHGHOST for incapable clientsJune McEnroe 2019-11-10Rename listen to localJune McEnroe 2019-11-09Remove extended-join and invite-notifyJune McEnroe 2019-11-09Maintain stateCaps and offer them to clientsJune McEnroe 2019-11-09Parse capabilitiesJune McEnroe 2019-11-09Avoid the reserved _A names with BIT macroJune McEnroe 2019-11-09Define macro for bit flag enumsJune McEnroe 2019-11-08Check that password is hashedJune McEnroe 2019-11-08Avoid calling getopt_long again after it returns -1June McEnroe 2019-11-08Only change AWAY status for registered clientsJune McEnroe 2019-11-07Just write the example normallyJune McEnroe 2019-11-07Include path in readlinkat errorJune McEnroe 2019-11-07Call clientConsume before clientRecvJune McEnroe 2019-11-06Use -l:filename in Linux.mkJune McEnroe 2019-11-06Fix compat.h for #defined strlcpyJune McEnroe 2019-11-06Allow unsetting LIBRESSL_PREFIXJune McEnroe 2019-11-06Document calico service configurationJune McEnroe 2019-11-06Document SASL EXTERNAL configuration in more detailJune McEnroe 2019-11-06Document pounce service configurationJune McEnroe 2019-11-06Mention Darwin and GNU/Linux in READMEJune McEnroe 2019-11-06Assume LibreSSL from brew on DarwinJune McEnroe 2019-11-06Remove -DNO_EXPLICIT_BZERO from Darwin.mkJune McEnroe 2019-11-06Don't install rc scripts or dirs on LinuxJune McEnroe