[PATCH 2/2] selftests/splice: cover zero-count sendfile to a full pipe

From: Mehtab Zafar

Date: Sat Nov 28 2026 - 08:42:58 EST


Add blocking and nonblocking cases for sendfile() with count zero and
a full output pipe. Both calls must return zero without waiting for pipe
space.

This regression test was developed with AI assistance.

Signed-off-by: Mehtab Zafar <hello@xxxxxxx>
---
The selftest passed 0/2 cases on baseline and 2/2 on the patched kernel
in each of three fresh x86_64 KVM boots per kernel. The static test binary
was built with warnings treated as errors; git diff --check was clean.
With the generic FILE_PATH_CHANGES message ignored, final strict
checkpatch reported no errors, warnings, or checks. Its final run also
logged an spdxcheck.py traceback because Python ply was missing; the
source and SPDX line were unchanged from the earlier successful SPDX
validation.

tools/testing/selftests/splice/.gitignore | 1 +
tools/testing/selftests/splice/Makefile | 1 +
.../testing/selftests/splice/sendfile_zero.c | 93 +++++++++++++++++++
3 files changed, 95 insertions(+)
cre
ate mode 100644 tools/testing/selftests/splice/sendfile_zero.c

diff --git a/tools/testing/selftests/splice/.gitignore b/tools/testing/selftests/splice/.gitignore
index be8266f5d04c..0b7414d3bbf1 100644
--- a/tools/testing/selftests/splice/.gitignore
+++ b/tools/testing/selftests/splice/.gitignore
@@ -1,3 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
default_file_splice_read
+sendfile_zero
splice_read
diff --git a/tools/testing/selftests/splice/Makefile b/tools/testing/selftests/splice/Makefile
index 541cd826d5a5..b77c12767846 100644
--- a/tools/testing/selftests/splice/Makefile
+++ b/tools/testing/selftests/splice/Makefile
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-2.0
TEST_PROGS := default_file_splice_read.sh short_splice_read.sh
+TEST_GEN_PROGS := sendfile_zero
TEST_GEN_PROGS_EXTENDED := default_file_splice_read splice_read

include ../lib.mk
diff --git a/tools/testing/selftests/splice/sendfile_zero.c b/tools/testing/selftests/splice/sendfile_zero
.c
new file mode 100644
index 000000000000..b597420be687
--- /dev/null
+++ b/tools/testing/selftests/splice/sendfile_zero.c
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/sendfile.h>
+#include <unistd.h>
+
+#include "../kselftest_harness.h"
+
+FIXTURE(sendfile_zero) {
+ int input_fd;
+ int pipefd[2];
+};
+
+FIXTURE_SETUP(sendfile_zero)
+{
+ char path[] = "/tmp/sendfile_zero.XXXXXX";
+ char buf[4096] = {};
+ ssize_t ret;
+
+ self->input_fd = -1;
+ self->pipefd[0] = -1;
+ self->pipefd[1] = -1;
+
+ self->input_fd = mkstemp(path);
+ ASSERT_GE(self->input_fd, 0);
+ ASSERT_EQ(0, unlink(path));
+ ASSERT_EQ(1, write(self->input_fd, "x", 1));
+ ASSERT_EQ(0, lseek(self->input_fd, 0, SEEK_SET));
+
+ ASSERT_EQ(0, pipe2(self->pipefd, O_NONBLOCK));
+ while ((ret = write(self->pipefd[1], buf, sizeof(buf))) > 0)
+ ;

+ ASSERT_EQ(-1, ret);
+ ASSERT_EQ(EAGAIN, errno);
+}
+
+FIXTURE_TEARDOWN(sendfile_zero)
+{
+ if (self->pipefd[0] >= 0)
+ close(self->pipefd[0]);
+ if (self->pipefd[1] >= 0)
+ close(self->pipefd[1]);
+ if (self->input_fd >= 0)
+ close(self->input_fd);
+}
+
+TEST_F(sendfile_zero, nonblocking_full_pipe)
+{
+ off_t offset = 0;
+ ssize_t ret;
+
+ ret = sendfile(self->pipefd[1], self->input_fd, &offset, 0);
+ ASSERT_EQ(0, ret) TH_LOG("sendfile: %s", strerror(errno));
+ ASSERT_EQ(0, offset);
+}
+
+static void alarm_handler(int signal)
+{
+ (void)signal;
+}
+
+TEST_F(sendfile_zero, blocking_full_pipe)
+{
+ struct sigaction action = {
+ .sa_handler = alarm_handler,
+ };
+ struct sigaction old_action;
+ off_t offset = 0;
+ ssize_t ret;
+ int error;
+ int flags;
+
+ flags = fcntl(self->pipefd[1], F_GETFL);
+ ASSERT_GE(flags, 0);
+ ASSERT_EQ(0, fcntl(self->pipefd[1], F_SETFL,
+ flags & ~O_NONBLOCK));
+ ASSERT_EQ(0, sigaction(SIGALRM, &action, &
old_action));
+
+ alarm(1);
+ ret = sendfile(self->pipefd[1], self->input_fd, &offset, 0);
+ error = errno;
+ alarm(0);
+ ASSERT_EQ(0, sigaction(SIGALRM, &old_action, NULL));
+
+ ASSERT_EQ(0, ret) TH_LOG("sendfile: %s", strerror(error));
+ ASSERT_EQ(0, offset);
+}
+
+TEST_HARNESS_MAIN
--
2.34.1

Attachment: signature.asc
Description: OpenPGP digital signature