aboutsummaryrefslogtreecommitdiff
path: root/src/actual.h
diff options
context:
space:
mode:
authorAdrian Kummerlaender2016-02-16 23:37:39 +0100
committerAdrian Kummerlaender2016-02-16 23:54:18 +0100
commite0b000a61f54db41286ededcc795318c79715d6e (patch)
treefc2d393a4945e74691d3391c9d35cbd75839d1ba /src/actual.h
parentf4353423207c451244ce9118b54ef3dcb28c7f89 (diff)
downloadchange-e0b000a61f54db41286ededcc795318c79715d6e.tar
change-e0b000a61f54db41286ededcc795318c79715d6e.tar.gz
change-e0b000a61f54db41286ededcc795318c79715d6e.tar.bz2
change-e0b000a61f54db41286ededcc795318c79715d6e.tar.lz
change-e0b000a61f54db41286ededcc795318c79715d6e.tar.xz
change-e0b000a61f54db41286ededcc795318c79715d6e.tar.zst
change-e0b000a61f54db41286ededcc795318c79715d6e.zip
Adapt `get_actual_function` template to accept newly introduced `function_ptr`
Diffstat (limited to 'src/actual.h')
-rw-r--r--src/actual.h60
1 files changed, 60 insertions, 0 deletions
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 <dlfcn.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+#include <memory>
+#include <cstring>
+
+namespace {
+ template <class Result, typename... Arguments>
+ using function_ptr = Result(*)(Arguments...);
+
+ template <typename FunctionPtr>
+ 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<ssize_t, int, const void*, size_t>
+ >("write");
+
+ static auto writev = get_actual_function<
+ function_ptr<ssize_t, int, const iovec*, int>
+ >("writev");
+
+ static auto rename = get_actual_function<
+ function_ptr<int, const char*, const char*>
+ >("rename");
+
+ static auto rmdir = get_actual_function<
+ function_ptr<int, const char*>
+ >("rmdir");
+
+ static auto unlink = get_actual_function<
+ function_ptr<int, const char*>
+ >("unlink");
+
+ static auto unlinkat = get_actual_function<
+ function_ptr<int, int, const char*, int>
+ >("unlinkat");
+
+ static auto mmap = get_actual_function<
+ function_ptr<void*, void*, size_t, int, int, int, off_t>
+ >("mmap");
+}
+
+#endif // CHANGE_SRC_ACTUAL_FUNCTION_H_