Commit 4937f6c6 authored by Ramon Nou's avatar Ramon Nou Committed by Ramon Nou
Browse files

thread_local to avoid log reentrance

parent 2af74746
Loading
Loading
Loading
Loading
+13 −23
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ namespace {

thread_local bool reentrance_guard_flag;
thread_local gkfs::syscall::info saved_syscall_info;
thread_local bool avoid_logging;

constexpr void
save_current_syscall_info(gkfs::syscall::info info) {
    saved_syscall_info = info;
@@ -102,14 +102,10 @@ hook_internal(long syscall_number, long arg0, long arg1, long arg2, long arg3,
                                                arg3, arg4, arg5};
#endif

    if(!avoid_logging) {
        avoid_logging = true;
    LOG(SYSCALL,
        gkfs::syscall::from_internal_code | gkfs::syscall::to_hook |
                gkfs::syscall::not_executed,
        syscall_number, args);
        avoid_logging = false;
    }

    switch(syscall_number) {
#ifdef SYS_open
@@ -445,14 +441,11 @@ hook(long syscall_number, long arg0, long arg1, long arg2, long arg3, long arg4,
                                                arg3, arg4, arg5};
#endif

    if(!avoid_logging) {
        avoid_logging = true;
    LOG(SYSCALL,
        gkfs::syscall::from_external_code | gkfs::syscall::to_hook |
                gkfs::syscall::not_executed,
        syscall_number, args);
        avoid_logging = false;
    }

    switch(syscall_number) {

        case SYS_execve:
@@ -871,12 +864,9 @@ hook_forwarded_syscall(long syscall_number, long arg0, long arg1, long arg2,
                                                arg3, arg4, arg5};
#endif

    if(!avoid_logging) {
        avoid_logging = true;
    LOG(SYSCALL, ::get_current_syscall_info() | gkfs::syscall::executed,
        syscall_number, args, result);
        avoid_logging = false;
    }

    ::reset_current_syscall_info();
}

+8 −0
Original line number Diff line number Diff line
@@ -46,6 +46,9 @@ extern "C" {

namespace fs = std::filesystem;

namespace {
[[maybe_unused]] thread_local bool avoid_logging;
}
namespace {
enum class split_str_mode {
    is_any_of,
@@ -490,6 +493,10 @@ logger::log_syscall(syscall::info info, const long syscall_number,

print_syscall:

    if(::avoid_logging) {
        return;
    }
    ::avoid_logging = true;
    fmt::basic_memory_buffer<char, max_buffer_size> buffer;

    detail::format_timestamp_to(buffer, timezone_);
@@ -504,6 +511,7 @@ print_syscall:
    fmt::format_to(std::back_inserter(buffer), "\n");

    ::syscall_no_intercept(SYS_write, log_fd_, buffer.data(), buffer.size());
    ::avoid_logging = false;
}

} // namespace gkfs::log