[LTP] [PATCH v1] splices06.c: Add splice check on proc files

Wei Gao wegao@suse.com
Thu Aug 17 02:39:47 CEST 2023


Signed-off-by: Wei Gao <wegao@suse.com>
---
 testcases/kernel/syscalls/splice/splice06.c | 134 ++++++++++++++++++++
 1 file changed, 134 insertions(+)
 create mode 100644 testcases/kernel/syscalls/splice/splice06.c

diff --git a/testcases/kernel/syscalls/splice/splice06.c b/testcases/kernel/syscalls/splice/splice06.c
new file mode 100644
index 000000000..f14fd84c5
--- /dev/null
+++ b/testcases/kernel/syscalls/splice/splice06.c
@@ -0,0 +1,134 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2023 SUSE LLC <wegao@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * This test is cover splice() on proc files.
+ *
+ */
+
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <fcntl.h>
+
+#include "tst_test.h"
+#include "lapi/splice.h"
+
+#define TEST_BLOCK_SIZE 100
+#define NAME_SPACES_NUM 1024
+#define PROCFILE "/proc/sys/user/max_user_namespaces"
+#define TESTFILE1 "splice_testfile_1"
+#define TESTFILE2 "splice_testfile_2"
+
+static int fd_in, fd_out;
+static int origin_name_spaces_num;
+static char line[TEST_BLOCK_SIZE];
+
+static void splice_file(void)
+{
+	int pipes[2];
+	int ret;
+
+	SAFE_PIPE(pipes);
+
+	ret = splice(fd_in, NULL, pipes[1], NULL, TEST_BLOCK_SIZE, 0);
+	if (ret < 0)
+		tst_brk(TBROK | TERRNO, "splice(fd_in, pipe) failed");
+
+	ret = splice(pipes[0], NULL, fd_out, NULL, TEST_BLOCK_SIZE, 0);
+	if (ret < 0)
+		tst_brk(TBROK | TERRNO, "splice(pipe, fd_out) failed");
+
+	SAFE_CLOSE(fd_in);
+	SAFE_CLOSE(fd_out);
+	SAFE_CLOSE(pipes[0]);
+	SAFE_CLOSE(pipes[1]);
+}
+
+static void set_value(char file[], int num)
+{
+	int fd;
+	int len = snprintf(NULL, 0, "%d", num);
+
+	memset(line, '\0', sizeof(line));
+	sprintf(line, "%d", num);
+
+	fd = SAFE_OPEN(file, O_RDWR | O_CREAT | O_TRUNC, 0777);
+	SAFE_WRITE(SAFE_WRITE_ALL, fd, line, len);
+	SAFE_CLOSE(fd);
+}
+
+static int get_value(char file[])
+{
+	int fd, num, ret;
+
+	memset(line, '\0', sizeof(line));
+
+	fd = SAFE_OPEN(file, O_RDONLY);
+	SAFE_READ(0, fd, line, TEST_BLOCK_SIZE);
+	SAFE_CLOSE(fd);
+
+	ret = sscanf(line, "%d", &num);
+	if (ret == EOF)
+		tst_brk(TBROK | TERRNO, "sscanf error");
+
+	return num;
+}
+
+static void splice_test(void)
+{
+
+	int name_spaces_num_setting = get_value(PROCFILE);
+
+	fd_in = SAFE_OPEN(PROCFILE, O_RDONLY);
+	fd_out = SAFE_OPEN(TESTFILE2, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+	splice_file();
+
+	if (name_spaces_num_setting == get_value(TESTFILE2))
+		tst_res(TPASS, "Written data has been read back correctly");
+	else
+		tst_brk(TBROK | TERRNO, "Written data has been read back failed");
+
+	if (get_value(PROCFILE) != NAME_SPACES_NUM)
+		name_spaces_num_setting = NAME_SPACES_NUM;
+	else
+		name_spaces_num_setting = NAME_SPACES_NUM + 1;
+
+	set_value(TESTFILE1, name_spaces_num_setting);
+
+	fd_in = SAFE_OPEN(TESTFILE1, O_RDONLY, 0777);
+	fd_out = SAFE_OPEN(PROCFILE, O_WRONLY, 0777);
+
+	splice_file();
+
+	if (name_spaces_num_setting == get_value(PROCFILE))
+		tst_res(TPASS, "Written data has been written correctly");
+	else
+		tst_brk(TBROK | TERRNO, "Written data has been written failed");
+}
+
+static void setup(void)
+{
+	origin_name_spaces_num = get_value(PROCFILE);
+}
+
+static void cleanup(void)
+{
+	set_value(PROCFILE, origin_name_spaces_num);
+}
+
+static struct tst_test test = {
+	.min_kver = "5.11",
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = splice_test,
+	.needs_tmpdir = 1,
+};
-- 
2.35.3



More information about the ltp mailing list