Skip to content

feat(libkernel): implement sendfile - #3568

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
DariusHQ:feat/libkernel-sendfile
Oct 11, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
DariusHQ:feat/libkernel-sendfile

Conversation

@DariusHQ

Copy link
Copy Markdown
Contributor

What

sendfile (YA0r4LCkfeY) in libkernel. Nothing in the tree implements it yet.

PS5MC (PPSA99420 1.2, VivaLaVent/PS5MC) stops at it at start-up on main 2220effbe (nothing since touches it), Windows, catalog build with eboot.bin and sce_module/libc.prx unwrapped as #2605 requires (tracked in #1578):

FAIL: unresolved ELF import YA0r4LCkfeY from libkernel.prx

With this branch it goes on to wcstok (8ngzWNZzFJU):

FAIL: unresolved ELF import 8ngzWNZzFJU from libSceLibcInternal.prx

Its only call site is in libmicrohttpd (Kodi's web server, connection.c), which sends file responses with it.

It follows FreeBSD 11's kern_sendfile.c:

  • headers, then the file from offset (to its end when nbytes is 0, only headers and trailers past the end), then trailers;
  • the file offset does not move;
  • on a non-blocking socket it fails with EAGAIN after a partial send, with *sbytes saying how far it got;
  • a negative offset (EINVAL), more than 1024 header or trailer entries (EINVAL) and an unreadable file (EBADF) fail before anything is sent and leave *sbytes alone;
  • a socket as the file and a socket that is not a stream (EINVAL), an unconnected socket (ENOTCONN) and a descriptor that is not a socket (EBADF, as for send) fail with *sbytes set to 0, as FreeBSD writes it once the file is open.

The file is read with sceKernelPread in 64 KiB blocks and sent with send, so flags are ignored. TechnicalDebt lists where that differs from FreeBSD: any descriptor sceKernelPread reads is accepted as the file, ENOTSOCK is EBADF, SF_NODISKIO never fails with EBUSY, and SF_SYNC returns once the data is queued.

Tested

Windows 11 (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build with BUILD_TESTING=ON on main a11c8f7f7 with four branches merged together: this one and the %#.0o, ' flag, positional-argument and sendfile branches opened alongside it: ctest 833/837 passed. The four failures fail the same way on main here:

guest_sendfile, on a socketpair, checks:

  • a range with two headers and a trailer, and *sbytes;
  • the rest of a 200 000-byte file from an offset, read by another thread, more than one block;
  • nbytes past the end of the file, an offset past the end (headers and trailers only), null header pointer with a count, null sbytes;
  • that the file offset stays 0;
  • every error above, with *sbytes;
  • EAGAIN from a non-blocking socket fed a 32 MiB file with no reader, and that the bytes *sbytes reports arrived intact.

It passed 10/10 runs. Mutation check, all caught:

  • a negative offset reaching the read;
  • no limit on header and trailer entries;
  • headers or trailers not sent;
  • *sbytes written for an unreadable file;
  • a datagram socket accepted;
  • the connection not checked before sending;
  • nbytes ignored;
  • ESPIPE instead of EINVAL for a socket as the file;
  • the partial count lost on EAGAIN.

tools/import_audit.py on PS5MC relinked with --windows --registry: absent imports go from 28 to 27.

Not run on Linux (no Linux host here). The code is shared; on Linux it sends with MSG_NOSIGNAL, so a closed peer gives EPIPE rather than a host SIGPIPE.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

PS5MC (PPSA99420) stops at start-up on sendfile. It follows FreeBSD 11:
headers, the file range and trailers go to a connected stream socket, the
file offset does not move, a non-blocking socket fails with EAGAIN after
a partial send, and *sbytes reports what was sent. The checks and the
point where *sbytes is written follow kern_sendfile.c.
@github-actions

Copy link
Copy Markdown

Progress report (a11c8f7 → bad1903)

📈 System libraries: 85.82% (+0.01%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel sendfile_nid_postfix

@babyhuey

Copy link
Copy Markdown
Contributor

Checked on Linux (Ubuntu 24.04, GCC 13) and Windows 11 (MinGW-w64 GCC 15.2 from WinLibs), Release, with this PR (bad1903) merged onto main a11c8f7.

  • guest_sendfile passes 5/5 on both (--repeat until-fail:5); no warnings in the changed files on Windows.
  • PS5MC (PPSA99420) gets past sendfile on both. On main Windows stops at FAIL: unresolved ELF import YA0r4LCkfeY (sendfile) from libkernel.prx; with this PR it stops at FAIL: unresolved ELF import 8ngzWNZzFJU (wcstok) from libSceLibcInternal.prx (feat(libc): implement wcstok #3583). Linux gives the same: undefined symbol: 8ngzWNZzFJU.

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#3840 docs/dev/TechnicalDebt.md L605-606

@adriadam10

Copy link
Copy Markdown
Collaborator

Checked on Linux with the PR merged onto current main: builds, guest_sendfile passes 5/5, and it doesn't link without the change. Reviewed the header/trailer limits, the *sbytes handling on each error path, the EAGAIN partial count and the TechnicalDebt entry against FreeBSD 11 kern_sendfile.c. Windows runs by @babyhuey and your own cover the MinGW side. Merging.

@adriadam10
adriadam10 merged commit 57ba0e7 into boykopovar:main Oct 11, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants