From b3ef0fc8daa41e433f1919a4933a1cc047f59341 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 20 Feb 2016 22:30:11 +0100 Subject: Interpose `open` library function `open` is not as side effect free as I had imagined - i.e. if the flag `O_TRUNC` is passed it truncates the file contents alongside opening the file descriptor. In practice this is done by _emacs_ prior to writing the new file content and as such needs to be intercepted so we can start tracking the file before it is changed. Interposing `open` required some changes to make the library work without including `fcntl.h`. This header not only defines some of the flags we require to check if a library call actually is able to change files but also defines the `open` library function. While implementing this change I noticed that the function interpositions implemented in C++ actually need to be declared as `external "C"` so their names do not get wrangled during compilation. I suspect that this was previously implicitly done for e.g. `mmap` and `write` by the included C standard library headers. However this did not work for `open` which is why all function interpositions are now explicitly declared external. End result: _emacs_ file changes are now tracked correctly. --- src/main.cc | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'src/main.cc') diff --git a/src/main.cc b/src/main.cc index 8b6ca23..7e849fd 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,3 +1,7 @@ +#include +#include +#include + #include "actual.h" #include "utility/io.h" @@ -61,6 +65,14 @@ inline void track_write(const int fd) { } } +inline void track_write(const std::string& path) { + if ( enabled && utility::is_regular_file(path.c_str()) ) { + if ( !matcher->isMatching(path) ) { + tracker->track(path); + } + } +} + inline void track_rename( const std::string& old_path, const std::string& new_path) { if ( enabled ) { @@ -82,6 +94,28 @@ inline void track_remove(const std::string& path) { } } +extern "C" { + +int open(const char* path, int flags, mode_t mode) { + static actual::ptr actual_open{}; + + if ( !actual_open ) { + actual_open = actual::get_ptr("open"); + } + + // `open` may reset the file contents when used with the `O_TRUNC` flag. + // e.g. this is how _emacs_ clears the file content prior to writing the + // new content. + // Normally `O_TRUNC` is defined in `fcntl.h` which we can not include + // as it defines the very c-function we are currently _overriding_. + // + if ( flags & 01000 ) { + track_write(path); + } + + return actual_open(path, flags, mode); +} + ssize_t write(int fd, const void* buffer, size_t count) { static actual::ptr actual_write{}; @@ -163,7 +197,10 @@ int unlinkat(int dirfd, const char* path, int flags) { actual_unlinkat = actual::get_ptr("unlinkat"); } - if ( dirfd == AT_FDCWD ) { + // Normally `AT_FDCWD` is defined in `fcntl.h` which we can not include + // as it defines `open` which this library also aims to override. + // + if ( dirfd == -100 ) { track_remove(path); } else { track_remove(utility::get_file_path(dirfd) + path); @@ -171,3 +208,5 @@ int unlinkat(int dirfd, const char* path, int flags) { return actual_unlinkat(dirfd, path, flags); } + +} -- cgit v1.2.3