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/actual_function.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/change_log.cc | 47 +++++++++++----------------------------- src/io.h | 6 +++--- src/utility.h | 6 +++--- 4 files changed, 79 insertions(+), 40 deletions(-) create mode 100644 src/actual_function.h (limited to 'src') 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 +#include + +#include +#include + +namespace { + template + std::function 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(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 - -#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); } 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 #include @@ -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 @@ -25,4 +25,4 @@ class Logger { } -#endif // CHANGE_UTILITY_H_ +#endif // CHANGE_SRC_UTILITY_H_ -- cgit v1.2.3