aboutsummaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/actual_function.h60
-rw-r--r--src/change_log.cc47
-rw-r--r--src/io.h6
-rw-r--r--src/utility.h6
4 files changed, 79 insertions, 40 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_
diff --git a/src/change_log.cc b/src/change_log.cc
index f07296e..8ff1475 100644
--- a/src/change_log.cc
+++ b/src/change_log.cc
@@ -2,32 +2,18 @@
#define _GNU_SOURCE
#endif
-#include <sys/mman.h>
-
-#include <memory>
#include <algorithm>
#include <functional>
#include <unordered_set>
#include "io.h"
#include "utility.h"
+#include "actual_function.h"
static std::unique_ptr<std::unordered_set<std::string>> tracked_files;
static std::unique_ptr<io::FileDescriptorGuard> fd_guard;
static std::unique_ptr<utility::Logger> logger;
-template <class Result, typename... Arguments>
-std::function<Result(Arguments...)> get_real_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);
-}
-
void init() __attribute__ ((constructor));
void init() {
tracked_files = std::make_unique<std::unordered_set<std::string>>();
@@ -43,9 +29,14 @@ void init() {
}
void exit(int status) {
+ __attribute__((__noreturn__)) void(*real_exit)(int) = nullptr;
+ const void* symbol_address = dlsym(RTLD_NEXT, "exit");
+
+ std::memcpy(&real_exit, &symbol_address, sizeof(symbol_address));
+
logger->append("exit");
- get_real_function<void, int>("exit")(status);
+ real_exit(status);
}
bool is_tracked_file(const std::string& file_name) {
@@ -57,8 +48,6 @@ bool track_file(const std::string& file_name) {
}
ssize_t write(int fd, const void* buffer, size_t count) {
- static auto real_write = get_real_function<ssize_t, int, const void*, size_t>("write");
-
if ( io::is_regular_file(fd) ) {
const std::string file_name{ io::get_file_name(fd) };
@@ -69,52 +58,42 @@ ssize_t write(int fd, const void* buffer, size_t count) {
}
}
- return real_write(fd, buffer, count);
+ return actual::write(fd, buffer, count);
}
int rename(const char* old_path, const char* new_path) {
- static auto real_rename = get_real_function<int, const char*, const char*>("rename");
-
if ( !is_tracked_file(old_path) ) {
track_file(old_path);
}
logger->append("renamed '" + std::string(old_path) + "' to '" + std::string(new_path) + "'");
- return real_rename(old_path, new_path);
+ return actual::rename(old_path, new_path);
}
int rmdir(const char* path) {
- static auto real_rmdir = get_real_function<int, const char*>("rmdir");
-
logger->append("removed directory '" + std::string(path) + "'");
- return real_rmdir(path);
+ return actual::rmdir(path);
}
int unlink(const char* path) {
- static auto real_unlink = get_real_function<int, const char*>("unlink");
-
logger->append("removed '" + std::string(path) + "'");
- return real_unlink(path);
+ return actual::unlink(path);
}
int unlinkat(int dirfd, const char* path, int flags) {
- static auto real_unlinkat = get_real_function<int, int, const char*, int>("unlinkat");
-
if ( dirfd == AT_FDCWD ) {
logger->append("removed '" + std::string(path) + "'");
} else {
logger->append("removed '" + io::get_file_name(dirfd) + path + "'");
}
- return real_unlinkat(dirfd, path, flags);
+ return actual::unlinkat(dirfd, path, flags);
}
void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) {
- static auto real_mmap = get_real_function<void*, void*, size_t, int, int, int, off_t>("mmap");
-
if ( ( prot & PROT_WRITE ) && io::is_regular_file(fd) ) {
const std::string file_name{ io::get_file_name(fd) };
@@ -125,5 +104,5 @@ void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset)
}
}
- return real_mmap(addr, length, prot, flags, fd, offset);
+ return actual::mmap(addr, length, prot, flags, fd, offset);
}
diff --git a/src/io.h b/src/io.h
index c36a5b0..676ebac 100644
--- a/src/io.h
+++ b/src/io.h
@@ -1,5 +1,5 @@
-#ifndef CHANGE_IO_H_
-#define CHANGE_IO_H_
+#ifndef CHANGE_SRC_IO_H_
+#define CHANGE_SRC_IO_H_
#include <unistd.h>
#include <dlfcn.h>
@@ -58,4 +58,4 @@ bool is_regular_file(int fd) {
}
-#endif // CHANGE_IO_H_
+#endif // CHANGE_SRC_IO_H_
diff --git a/src/utility.h b/src/utility.h
index 27ce8e9..96ee15c 100644
--- a/src/utility.h
+++ b/src/utility.h
@@ -1,5 +1,5 @@
-#ifndef CHANGE_UTILITY_H_
-#define CHANGE_UTILITY_H_
+#ifndef CHANGE_SRC_UTILITY_H_
+#define CHANGE_SRC_UTILITY_H_
#include <ext/stdio_filebuf.h>
@@ -25,4 +25,4 @@ class Logger {
}
-#endif // CHANGE_UTILITY_H_
+#endif // CHANGE_SRC_UTILITY_H_