From e0b000a61f54db41286ededcc795318c79715d6e Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Tue, 16 Feb 2016 23:37:39 +0100 Subject: Adapt `get_actual_function` template to accept newly introduced `function_ptr` --- src/actual.h | 60 +++++++++++++++++++++++++++++++++++++++++++++ src/actual_function.h | 68 --------------------------------------------------- 2 files changed, 60 insertions(+), 68 deletions(-) create mode 100644 src/actual.h delete mode 100644 src/actual_function.h diff --git a/src/actual.h b/src/actual.h new file mode 100644 index 0000000..af64ec3 --- /dev/null +++ b/src/actual.h @@ -0,0 +1,60 @@ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#ifndef CHANGE_SRC_ACTUAL_FUNCTION_H_ +#define CHANGE_SRC_ACTUAL_FUNCTION_H_ + +#include +#include +#include + +#include +#include + +namespace { + template + using function_ptr = Result(*)(Arguments...); + + template + FunctionPtr get_actual_function(const std::string& symbol_name) { + const void* symbol_address{ dlsym(RTLD_NEXT, symbol_name.c_str()) }; + + FunctionPtr actual_function{}; + std::memcpy(&actual_function, &symbol_address, sizeof(symbol_address)); + + return actual_function; + } +} + +namespace actual { + static auto write = get_actual_function< + function_ptr + >("write"); + + static auto writev = get_actual_function< + function_ptr + >("writev"); + + static auto rename = get_actual_function< + function_ptr + >("rename"); + + static auto rmdir = get_actual_function< + function_ptr + >("rmdir"); + + static auto unlink = get_actual_function< + function_ptr + >("unlink"); + + static auto unlinkat = get_actual_function< + function_ptr + >("unlinkat"); + + static auto mmap = get_actual_function< + function_ptr + >("mmap"); +} + +#endif // CHANGE_SRC_ACTUAL_FUNCTION_H_ diff --git a/src/actual_function.h b/src/actual_function.h deleted file mode 100644 index ff69a13..0000000 --- a/src/actual_function.h +++ /dev/null @@ -1,68 +0,0 @@ -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - -#ifndef CHANGE_SRC_ACTUAL_FUNCTION_H_ -#define CHANGE_SRC_ACTUAL_FUNCTION_H_ - -#include -#include -#include - -#include -#include - -namespace { - template - using function_ptr = Result(*)(Arguments...); - - template - function_ptr get_actual_function( - const std::string& symbol_name) { - const void* symbol_address{ dlsym(RTLD_NEXT, symbol_name.c_str()) }; - - function_ptr actual_function{}; - std::memcpy(&actual_function, &symbol_address, sizeof(symbol_address)); - - return actual_function; - } -} - -namespace actual { - static auto write = get_actual_function< - ssize_t, - int, const void*, size_t - >("write"); - - static auto writev = get_actual_function< - ssize_t, - int, const iovec*, int - >("writev"); - - static auto rename = get_actual_function< - int, - const char*, const char* - >("rename"); - - static auto rmdir = get_actual_function< - int, - const char* - >("rmdir"); - - static auto unlink = get_actual_function< - int, - const char* - >("unlink"); - - static auto unlinkat = get_actual_function< - int, - int, const char*, int - >("unlinkat"); - - static auto mmap = get_actual_function< - void*, - void*, size_t, int, int, int, off_t - >("mmap"); -} - -#endif // CHANGE_SRC_ACTUAL_FUNCTION_H_ -- cgit v1.2.3