casaroli opened a new pull request, #21944: URL: https://github.com/apache/nuttx/pull/21944
## Summary `sendfile()` can send some bytes to a full nonblocking output and then return -1 with `EAGAIN`. It also moves `*offset` past the bytes that it sent. This occurs in `copyfile()` (`fs/vfs/fs_sendfile.c`), the buffer copy that `sendfile()` uses for every output that is not a socket with `CONFIG_NET_SENDFILE`. A caller that continues from the count or from `*offset` after `EAGAIN` then sends some bytes twice or skips some. `copyfile()` now returns the count when some bytes went out, and the error only when no byte went out, as `write()` does. `*offset` moves by the count. Without an offset, the input file position moves back over the bytes that were read but not written. An input that cannot seek loses these bytes, as before. The `EINTR` retry of the loop does not change. ## Impact On the copy path, the result of `sendfile()` and `*offset` now agree with the bytes that went out. A call that sends all bytes, or no byte, gives the same result as before. The `CONFIG_NET_SENDFILE` path (`psock_sendfile()`), the headers and the options do not change. The code is common VFS code, the same in flat, protected and kernel builds. In-tree callers: `netutils/ftpc` and `netutils/netcat` in nuttx-apps call `sendfile()` again from `*offset` after `EAGAIN`; they now send the correct bytes. `netutils/webserver` makes one call and is not affected. ## Testing New cmocka cases in apache/nuttx-apps#NNNN: `test_nuttx_fs_sendfile03` and `test_nuttx_fs_sendfile04` send a file of 8 KiB to a nonblocking pipe that fills up, with and without an offset. The whole `cmocka_fs_test` run, with the test directory on tmpfs: | Configuration | NuttX master 61ff9b8b308 | this PR | |---|---|---| | `qemu-armv8a:nsh` (flat, arm64, QEMU HVF, GCC 14.2) | 46 of 48 pass: sendfile03 and sendfile04 fail, "sendfile sent 0 but the offset moved 1536" | 48 of 48 pass | | `rv-virt:nsh` (flat, rv32, QEMU TCG, GCC 14.3) | 46 of 48 pass: the same two fail | 48 of 48 pass | | `sim:nsh` (flat, x86_64 Linux, the NuttX CI image) | 41 of 43 pass: the same two fail | 43 of 43 pass | `qemu-armv8a:knsh` (kernel, arm64, QEMU HVF) with `CONFIG_PIPES` and `CONFIG_NET_TCP_WRITE_BUFFERS`: - A test program with the same pipe checks: master FAIL (each call puts 1024 bytes into the pipe and returns -1 with `EAGAIN`); this PR PASS. - A test program that sends a 4 MiB file to a nonblocking TCP socket over loopback and continues from `*offset` after `EAGAIN`: on master a call returns `EAGAIN` with `*offset` moved by 17408, and the client gets 220868720 bytes instead of 4194304. With this PR, 240 calls return a partial count and 240 return `EAGAIN`, `*offset` always moves by the count, and the client gets 4194304 bytes. ostest: status 0 on `qemu-armv8a:knsh` and `qemu-armv8a:nsh` (HVF) on master and with this PR; status 0 on `qemu-armv8a:nsh`, `rv-virt:nsh` and `sim:nsh` (TCG, CI image) with this PR. After the rebase on master 5d150b4109e, `qemu-armv8a:nsh` passes 48 of 48 again. Make builds only; CMake was not built. Not run: a protected build, real hardware, `CONFIG_NET_SENDFILE`. `tools/checkpatch.sh -c -u -m -g` passes. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
