Skip to content

Commit

Permalink
Added debug output to sendfile
Browse files Browse the repository at this point in the history
  • Loading branch information
pd-fkie committed Sep 12, 2024
1 parent 01b5bee commit 9961a9c
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/sendfile.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <unistd.h>

#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) {
Expand All @@ -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)
Expand Down

0 comments on commit 9961a9c

Please sign in to comment.