From 30d28c558015afe22822647cc3943e2afb14afde Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Mon, 30 Nov 2015 12:07:33 +0100 Subject: 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. --- src/change_log.cc | 47 +++++++++++++---------------------------------- 1 file changed, 13 insertions(+), 34 deletions(-) (limited to 'src/change_log.cc') 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 - -#include #include #include #include #include "io.h" #include "utility.h" +#include "actual_function.h" static std::unique_ptr> tracked_files; static std::unique_ptr fd_guard; static std::unique_ptr logger; -template -std::function 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(real_function); -} - void init() __attribute__ ((constructor)); void init() { tracked_files = std::make_unique>(); @@ -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("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("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("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("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("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("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("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); } -- cgit v1.2.3