From 9961a9c09c6fc5afaa782d9bcdccdf700f0558ab Mon Sep 17 00:00:00 2001 From: pd-fkie <77979557+pd-fkie@users.noreply.github.com> Date: Thu, 12 Sep 2024 18:06:45 +0200 Subject: [PATCH] Added debug output to sendfile --- src/sendfile.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/sendfile.c b/src/sendfile.c index 1f53343..d6b713e 100644 --- a/src/sendfile.c +++ b/src/sendfile.c @@ -1,8 +1,10 @@ +#include #include "util.h" #include "desock.h" #include "syscall.h" #include "musl-features.h" +#include "hooks.h" VISIBLE ssize_t sendfile (int out_fd, int in_fd, off_t* ofs, size_t count) { @@ -11,6 +13,44 @@ ssize_t sendfile (int out_fd, int in_fd, off_t* ofs, size_t count) { } DEBUG_LOG("sendfile(%d, %d, %p, %lu) = %lu", out_fd, in_fd, ofs, count, count); + +#ifdef DEBUG + off_t old_offset = 0; + size_t consumed = 0; + char buf[512]; + + if (ofs) { + old_offset = lseek(in_fd, 0, SEEK_CUR); + + if (old_offset < 0) { + _error("Cannot lseek() in sendfile() in_fd"); + } + + if (lseek(in_fd, *ofs, SEEK_SET) < 0) { + _error("Invalid ofs in sendfile()"); + } + } + + while (consumed < count) { + size_t delta = count - consumed; + ssize_t r = syscall_cp(SYS_read, in_fd, buf, MIN(sizeof(buf), delta)); + + if (r < 0) { + _error("Cannot read from file in sendfile()"); + } + + hook_output(buf, (size_t) r); + + consumed += (size_t) r; + } + + if (ofs) { + if (lseek(in_fd, old_offset, SEEK_SET) < 0) { + _error("Cannot restore file offset in sendfile()"); + } + } +#endif + return count; } VERSION(sendfile)