Skip to content

Commit 5806c31

Browse files
authored
Merge pull request #909 from dark-lbp/Implement-sendfile-syscall
Implement sendfile syscall
2 parents e22c58d + 73ada07 commit 5806c31

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

qiling/os/posix/syscall/sendfile.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,33 @@
66
from qiling import Qiling
77
from qiling.os.posix.const import NR_OPEN
88

9-
def ql_syscall_sendfile64(ql: Qiling, out_fd: int, in_fd: int, offest: int, count: int):
10-
if (0 <= out_fd < NR_OPEN and ql.os.fd[out_fd] != 0) and (0 <= in_fd < NR_OPEN and ql.os.fd[in_fd] != 0):
11-
ql.os.fd[in_fd].lseek(ql.unpack32(ql.mem.read(offest, 4)))
9+
10+
def ql_syscall_sendfile64(ql: Qiling, out_fd: int, in_fd: int, offset: int, count: int):
11+
# https://linux.die.net/man/2/sendfile64
12+
return ql_syscall_sendfile(ql, out_fd, in_fd, offset, count)
13+
14+
15+
def ql_syscall_sendfile(ql: Qiling, out_fd: int, in_fd: int, offset: int, count: int):
16+
# https://man7.org/linux/man-pages/man2/sendfile.2.html
17+
if 0 <= out_fd < NR_OPEN and 0 <= in_fd < NR_OPEN \
18+
and ql.os.fd[out_fd] != 0 and ql.os.fd[in_fd] != 0:
19+
20+
in_fd_pos = ql.os.fd[in_fd].tell()
21+
if offset:
22+
# Handle sendfile64 and sendfile offset_ptr
23+
offset = ql.unpack(ql.mem.read(offset, ql.pointersize))
24+
else:
25+
offset = in_fd_pos
26+
27+
ql.os.fd[in_fd].lseek(offset)
1228
buf = ql.os.fd[in_fd].read(count)
29+
if offset:
30+
current_offset = ql.os.fd[in_fd].tell()
31+
ql.mem.write(offset, ql.pack(current_offset))
32+
ql.os.fd[in_fd].lseek(in_fd_pos)
33+
1334
regreturn = ql.os.fd[out_fd].write(buf)
35+
1436
else:
1537
regreturn = -1
1638

0 commit comments

Comments
 (0)