aboutsummaryrefslogtreecommitdiff
path: root/src/actual_function.h
diff options
context:
space:
mode:
authorAdrian Kummerlaender2015-11-30 12:07:33 +0100
committerAdrian Kummerlaender2015-11-30 12:07:33 +0100
commit30d28c558015afe22822647cc3943e2afb14afde (patch)
treea65bc59f97810652197d75349c23b067bcceb90f /src/actual_function.h
parent62b2be7fbedc13ed40ed8f1327dded532b90ad55 (diff)
downloadchange-30d28c558015afe22822647cc3943e2afb14afde.tar
change-30d28c558015afe22822647cc3943e2afb14afde.tar.gz
change-30d28c558015afe22822647cc3943e2afb14afde.tar.bz2
change-30d28c558015afe22822647cc3943e2afb14afde.tar.lz
change-30d28c558015afe22822647cc3943e2afb14afde.tar.xz
change-30d28c558015afe22822647cc3943e2afb14afde.tar.zst
change-30d28c558015afe22822647cc3943e2afb14afde.zip
Extract actual function acquisition
The pointers to the actual function implementations are now fetched inside the `actual` namespace declared in the `actual_function.h` header. Fixed source of _noreturn_ related warning during compilation by adding the appropriate flag. Sadly this means that we can not use `std::function` in this context as it doesn't seem to carry these _c-like_ flags.
Diffstat (limited to 'src/actual_function.h')
-rw-r--r--src/actual_function.h60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/actual_function.h b/src/actual_function.h
new file mode 100644
index 0000000..0ffe0c2
--- /dev/null
+++ b/src/actual_function.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 <memory>
+#include <cstring>
+
+namespace {
+ template <class Result, typename... Arguments>
+ std::function<Result(Arguments...)> get_actual_function(
+ const std::string& symbol_name) {
+
+ Result (*real_function)(Arguments...) = nullptr;
+ const void* symbol_address = dlsym(RTLD_NEXT, symbol_name.c_str());
+
+ std::memcpy(&real_function, &symbol_address, sizeof(symbol_address));
+
+ return std::function<Result(Arguments...)>(real_function);
+ }
+}
+
+namespace actual {
+ static auto write = get_actual_function<
+ ssize_t,
+ int, const void*, size_t
+ >("write");
+
+ 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_