Skip to contentSkip to conversation

Fix and convert sendfile{02, 06} to the new API

PATCH Superseded Xie Ziyao 3 participants 11 messages Jun 3, 2021 – Jun 21, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Xie Ziyao

author
#0 lore raw

cover Fix and convert sendfile{02, 06} to the new API

1. Convert sendfile{02, 06} to the new API with file descriptors instead of socket descriptors.
2. Remove the support for UCLINUX.
3. Remove redundant testcases {4, 22, 26} and {6, 20, 26} for sendfile02.
4. Remove unnecessary header files for sendfile{03, 04, 05, 07}.

Xie Ziyao (3):
syscalls/sendfile: Convert sendfile02 to the new API syscalls/sendfile: Convert sendfile06 to the new API syscalls/sendfile: Remove unnecessary header files

 .../kernel/syscalls/sendfile/sendfile02.c     | 323 ++++--------------
 .../kernel/syscalls/sendfile/sendfile03.c     |   3 -
 .../kernel/syscalls/sendfile/sendfile04.c     |   9 -
 .../kernel/syscalls/sendfile/sendfile05.c     |   9 -
 .../kernel/syscalls/sendfile/sendfile06.c     | 247 +++-----------
 .../kernel/syscalls/sendfile/sendfile07.c     |   9 -
 6 files changed, 108 insertions(+), 492 deletions(-)

--
2.17.1

author
#1 lore raw

PATCH 1/3 syscalls/sendfile: Convert sendfile02 to the new API

1. Convert sendfile02 to the new API with file descriptors instead of socket descriptors.
2. Remove the support for UCLINUX.
3. Remove redundant testcases {4, 22, 26} and {6, 20, 26}.

---
 .../kernel/syscalls/sendfile/sendfile02.c     | 323 ++++--------------
 1 file changed, 63 insertions(+), 260 deletions(-)
testcases/kernel/syscalls/sendfile/sendfile02.c +63 -260
index 729a9e123..820063681 100644
@@ -1,285 +1,88 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *
- *   Copyright (c) International Business Machines  Corp., 2001
- *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (c) International Business Machines  Corp., 2001
+ * 07/2001 Ported by Wayne Boyer
+ * 08/2002 Make it use a socket so it works with 2.5 kernel
+ * Copyright (c) 2021 Xie Ziyao <[email protected]>
  */
 
-/*
- * NAME
- *	sendfile02.c
- *
- * DESCRIPTION
- *	Testcase to test the basic functionality of the sendfile(2) system call.
- *
- * ALGORITHM
- *	1. call sendfile(2) with offset = 0
- *	2. call sendfile(2) with offset in the middle of the file
+/*\
+ * [Description]
  *
- * USAGE:  <for command-line>
- *  sendfile02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- *     where,
- *             -f   : Turn off functionality Testing.
- *             -i n : Execute test n times.
- *             -I x : Execute test for x seconds.
- *             -P x : Pause for x seconds between iterations.
- *             -t   : Turn on syscall timing.
- *
- * HISTORY
- *	07/2001 Ported by Wayne Boyer
- *	08/2002 Make it use a socket so it works with 2.5 kernel
- *
- * RESTRICTIONS
- *	NONE
+ * Test the basic functionality of the sendfile() system call,
+ * 1. Call sendfile() with offset = 0.
+ * 2. Call sendfile() with offset in the middle of the file.
  */
+
 #include <stdio.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <sys/sendfile.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <unistd.h>
 #include <inttypes.h>
-#include "test.h"
-#include "safe_macros.h"
-
-TCID_DEFINE(sendfile02);
-int TST_TOTAL = 4;
+#include <sys/sendfile.h>
 
-char in_file[100];
-char out_file[100];
-int out_fd;
-pid_t child_pid;
-static int sockfd, s;
-static struct sockaddr_in sin1;	/* shared between do_child and create_server */
+#include "tst_test.h"
 
-void cleanup(void);
-void do_child(void);
-void setup(void);
-int create_server(void);
+#define IN_FILE "in_file"
+#define OUT_FILE "out_file"
 
 struct test_case_t {
 	char *desc;
-	int offset;
-	int exp_retval;
-	int exp_updated_offset;
-} testcases[] = {
-	{
-	"Test sendfile(2) with offset = 0", 0, 26, 26}, {
-	"Test sendfile(2) with offset in the middle of file", 2, 24, 26}, {
-	"Test sendfile(2) with offset in the middle of file", 4, 22, 26}, {
-	"Test sendfile(2) with offset in the middle of file", 6, 20, 26}
+	off_t offset;
+	int64_t count;
+	int64_t exp_retval;
+	int64_t exp_updated_offset;
+} tc[] = {
+	{ "with offset = 0", 0, 26, 26, 26 },
+	{ "with offset = 2", 2, 24, 24, 26 },
 };
 
-#ifdef UCLINUX
-static char *argv0;
-#endif
-
-void do_sendfile(off_t offset, int i)
-{
-	int in_fd;
-	struct stat sb;
-	int wait_status;
-	int wait_stat;
-	off_t before_pos, after_pos;
-
-	out_fd = create_server();
-
-	if ((in_fd = open(in_file, O_RDONLY)) < 0) {
-		tst_brkm(TBROK, cleanup, "open failed: %d", errno);
-	}
-	SAFE_STAT(cleanup, in_file, &sb);
-
-	if ((before_pos = lseek(in_fd, 0, SEEK_CUR)) < 0) {
-		tst_brkm(TBROK, cleanup,
-			 "lseek before invoking sendfile failed: %d", errno);
-	}
-
-	TEST(sendfile(out_fd, in_fd, &offset, sb.st_size - offset));
-
-	if ((after_pos = lseek(in_fd, 0, SEEK_CUR)) < 0) {
-		tst_brkm(TBROK, cleanup,
-			 "lseek after invoking sendfile failed: %d", errno);
-	}
-
-	/* Close the sockets */
-	shutdown(sockfd, SHUT_RDWR);
-	shutdown(s, SHUT_RDWR);
-	if (TEST_RETURN != testcases[i].exp_retval) {
-		tst_resm(TFAIL, "sendfile(2) failed to return "
-			 "expected value, expected: %d, "
-			 "got: %ld", testcases[i].exp_retval,
-			 TEST_RETURN);
-		kill(child_pid, SIGKILL);
-	} else if (offset != testcases[i].exp_updated_offset) {
-		tst_resm(TFAIL, "sendfile(2) failed to update "
-			 "OFFSET parameter to expected value, "
-			 "expected: %d, got: %" PRId64,
-			 testcases[i].exp_updated_offset,
-			 (int64_t) offset);
-	} else if (before_pos != after_pos) {
-		tst_resm(TFAIL, "sendfile(2) updated the file position "
-			 " of in_fd unexpectedly, expected file position: %"
-			 PRId64 ", " " actual file position %" PRId64,
-			 (int64_t) before_pos, (int64_t) after_pos);
-	} else {
-		tst_resm(TPASS, "functionality of sendfile() is "
-			 "correct");
-		wait_status = waitpid(-1, &wait_stat, 0);
-	}
-
-	close(in_fd);
-}
-
-/*
- * do_child
- */
-void do_child(void)
-{
-	int lc;
-	socklen_t length;
-	char rbuf[4096];
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		length = sizeof(sin1);
-		recvfrom(sockfd, rbuf, 4096, 0, (struct sockaddr *)&sin1,
-			 &length);
-	}
-	exit(0);
-}
-
-/*
- * setup() - performs all ONE TIME setup for this test.
- */
-void setup(void)
+static void setup(void)
 {
 	int fd;
-	char buf[100];
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
+	char buf[27];
 
-	TEST_PAUSE;
-
-	/* make a temporary directory and cd to it */
-	tst_tmpdir();
-	sprintf(in_file, "in.%d", getpid());
-	if ((fd = creat(in_file, 00700)) < 0) {
-		tst_brkm(TBROK, cleanup, "creat failed in setup, errno: %d",
-			 errno);
-	}
+	fd = SAFE_CREAT(IN_FILE, 00700);
 	sprintf(buf, "abcdefghijklmnopqrstuvwxyz");
-	if (write(fd, buf, strlen(buf)) < 0) {
-		tst_brkm(TBROK, cleanup, "write failed, errno: %d", errno);
-	}
-	close(fd);
-	sprintf(out_file, "out.%d", getpid());
-}
-
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- *	       completion or premature exit.
- */
-void cleanup(void)
-{
-
-	close(out_fd);
-	/* delete the test directory created in setup() */
-	tst_rmdir();
+	SAFE_WRITE(1, fd, buf, strlen(buf));
+	SAFE_CLOSE(fd);
 
+	fd = SAFE_CREAT(OUT_FILE, 00700);
+	SAFE_CLOSE(fd);
 }
 
-int create_server(void)
+static void run(unsigned int i)
 {
-	static int count = 0;
-	socklen_t slen = sizeof(sin1);
-
-	sockfd = socket(PF_INET, SOCK_DGRAM, 0);
-	if (sockfd < 0) {
-		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
-			 strerror(errno));
-		return -1;
-	}
-	sin1.sin_family = AF_INET;
-	sin1.sin_port = 0; /* pick random free port */
-	sin1.sin_addr.s_addr = INADDR_ANY;
-	count++;
-	if (bind(sockfd, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
-		tst_brkm(TBROK, cleanup, "call to bind() failed: %s",
-			 strerror(errno));
-		return -1;
-	}
-	SAFE_GETSOCKNAME(cleanup, sockfd, (struct sockaddr *)&sin1, &slen);
-
-	child_pid = FORK_OR_VFORK();
-	if (child_pid < 0) {
-		tst_brkm(TBROK, cleanup, "client/server fork failed: %s",
-			 strerror(errno));
-		return -1;
-	}
-	if (!child_pid) {	/* child */
-#ifdef UCLINUX
-		if (self_exec(argv0, "") < 0) {
-			tst_brkm(TBROK, cleanup, "self_exec failed");
-			return -1;
-
-		}
-#else
-		do_child();
-#endif
-	}
-
-	s = socket(PF_INET, SOCK_DGRAM, 0);
-	inet_aton("127.0.0.1", &sin1.sin_addr);
-	if (s < 0) {
-		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
-			 strerror(errno));
-		return -1;
-	}
-	SAFE_CONNECT(cleanup, s, (struct sockaddr *)&sin1, sizeof(sin1));
-	return s;
+	int in_fd = SAFE_OPEN(IN_FILE, O_RDONLY);
+	int out_fd = SAFE_OPEN(OUT_FILE, O_WRONLY);
+	off_t offset = tc[i].offset;
+	off_t before_pos, after_pos;
 
+	before_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR);
+
+	TEST(sendfile(out_fd, in_fd, &offset, tc[i].count));
+	after_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR);
+
+	if (TST_RET != tc[i].exp_retval)
+		tst_res(TFAIL, "sendfile() failed to return expected value, "
+			       "expected: %" PRId64 ", got: %ld",
+			tc[i].exp_retval, TST_RET);
+	else if (offset != tc[i].exp_updated_offset)
+		tst_res(TFAIL, "sendfile() failed to update OFFSET parameter to "
+			       "expected value, expected: %" PRId64 ", got: %" PRId64,
+			tc[i].exp_updated_offset, (int64_t)(offset));
+	else if (before_pos != after_pos)
+		tst_res(TFAIL, "sendfile() updated the file position of in_fd "
+			       "unexpectedly, expected file position: %" PRId64
+			       ", actual file position %" PRId64,
+			(int64_t)(before_pos), (int64_t)(after_pos));
+	else
+		tst_res(TPASS, "sendfile() with %s", tc[i].desc);
+
+	SAFE_CLOSE(in_fd);
+	SAFE_CLOSE(out_fd);
 }
 
-int main(int ac, char **av)
-{
-	int i;
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-#ifdef UCLINUX
-	argv0 = av[0];
-	maybe_run_child(&do_child, "");
-#endif
-
-	setup();
-
-	/*
-	 * The following loop checks looping state if -c option given
-	 */
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; ++i) {
-			do_sendfile(testcases[i].offset, i);
-		}
-	}
-	cleanup();
-
-	tst_exit();
-}
+static struct tst_test test = {
+		.needs_tmpdir = 1,
+		.setup = setup,
+		.test = run,
+		.tcnt = ARRAY_SIZE(tc),
+};

--
2.17.1

#2 lore raw

PATCH 1/3 syscalls/sendfile: Convert sendfile02 to the new API

Quoted 4 lines

1. Convert sendfile02 to the new API with file descriptors instead of socket descriptors.
2. Remove the support for UCLINUX.
3. Remove redundant testcases {4, 22, 26} and {6, 20, 26}.

Thanks, merged with diff below (fix docparse formatting, add OFFSET_DESC() macro, fix checkpatch.pl warning on comparison order).

Kind regards,
Petr

kernel/syscalls/sendfile/sendfile02.c +7 -4
index 820063681..ffd654885 100644
@@ -9,7 +9,8 @@
 /*\
  * [Description]
  *
- * Test the basic functionality of the sendfile() system call,
+ * Test the basic functionality of the sendfile() system call:
+ *
  * 1. Call sendfile() with offset = 0.
  * 2. Call sendfile() with offset in the middle of the file.
  */
@@ -23,6 +24,8 @@
 #define IN_FILE "in_file"
 #define OUT_FILE "out_file"
 
+#define OFFSET_DESC(x) .desc = "with offset = "#x, .offset = x
+
 struct test_case_t {
 	char *desc;
 	off_t offset;
@@ -30,8 +33,8 @@ struct test_case_t {
 	int64_t exp_retval;
 	int64_t exp_updated_offset;
 } tc[] = {
-	{ "with offset = 0", 0, 26, 26, 26 },
-	{ "with offset = 2", 2, 24, 24, 26 },
+	{ OFFSET_DESC(0), 26, 26, 26 },
+	{ OFFSET_DESC(2), 24, 24, 26 },
 };
 
 static void setup(void)
@@ -60,7 +63,7 @@ static void run(unsigned int i)
 	TEST(sendfile(out_fd, in_fd, &offset, tc[i].count));
 	after_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR);
 
-	if (TST_RET != tc[i].exp_retval)
+	if (tc[i].exp_retval != TST_RET)
 		tst_res(TFAIL, "sendfile() failed to return expected value, "
 			       "expected: %" PRId64 ", got: %ld",
 			tc[i].exp_retval, TST_RET);
author
#3 lore raw

PATCH 2/3 syscalls/sendfile: Convert sendfile06 to the new API

In reply to Xie Ziyao

1. Convert sendfile06 to the new API with file descriptors instead of socket descriptors.
2. Remove the support for UCLINUX.

---
 .../kernel/syscalls/sendfile/sendfile06.c     | 247 ++++--------------
 1 file changed, 45 insertions(+), 202 deletions(-)
testcases/kernel/syscalls/sendfile/sendfile06.c +45 -202
index abb67604f..964a71eae 100644
@@ -1,228 +1,71 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) International Business Machines  Corp., 2001
  * Copyright (c) Red Hat Inc., 2007
- *
- * This program is free software;  you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY;  without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program;  if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * 11/2007 Copyed from sendfile02.c by Masatake YAMATO
+ * Copyright (c) 2021 Xie Ziyao <[email protected]>
  */
 
-/*
- * DESCRIPTION
- *	Testcase to test that sendfile(2) system call updates file
- *	position of in_fd correctly when passing NULL as offset.
+/*\
+ * [Description]
  *
- * HISTORY
- *	11/2007 Copyed from sendfile02.c by Masatake YAMATO
+ * Test that sendfile() system call updates file position of in_fd correctly
+ * when passing NULL as offset.
  */
 
-#include <inttypes.h>
 #include <stdio.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/stat.h>
+#include <inttypes.h>
 #include <sys/sendfile.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
-#include <sys/mman.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <string.h>
-#include "test.h"
-#include "safe_macros.h"
 
-TCID_DEFINE(sendfile06);
+#include "tst_test.h"
 
-#define IN_FILE		"infile"
-#define OUT_FILE	"outfile"
+#define IN_FILE "in_file"
+#define OUT_FILE "out_file"
 
-static pid_t child_pid;
-static int sockfd;
-static struct sockaddr_in sin1;
 static struct stat sb;
 
-static void cleanup(void);
-static void do_child(void);
-static void setup(void);
-static int create_server(void);
-
-int TST_TOTAL = 1;
-
-#ifdef UCLINUX
-static char *argv0;
-#endif
-
-static void do_sendfile(void)
-{
-	int in_fd, out_fd;
-	off_t after_pos;
-	int wait_stat;
-
-	out_fd = create_server();
-
-	in_fd = SAFE_OPEN(cleanup, IN_FILE, O_RDONLY);
-
-	TEST(sendfile(out_fd, in_fd, NULL, sb.st_size));
-	if ((after_pos = lseek(in_fd, 0, SEEK_CUR)) < 0) {
-		tst_brkm(TBROK, cleanup,
-			 "lseek after invoking sendfile failed: %d", errno);
-	}
-
-	/* Close the sockets */
-	shutdown(sockfd, SHUT_RDWR);
-	shutdown(out_fd, SHUT_RDWR);
-	if (TEST_RETURN != sb.st_size) {
-		tst_resm(TFAIL, "sendfile(2) failed to return "
-			 "expected value, expected: %" PRId64 ", "
-			 "got: %ld", (int64_t) sb.st_size, TEST_RETURN);
-		SAFE_KILL(cleanup, child_pid, SIGKILL);
-	} else if (after_pos != sb.st_size) {
-		tst_resm(TFAIL, "sendfile(2) failed to update "
-			 " the file position of in_fd, "
-			 "expected file position: %" PRId64 ", "
-			 "actual file position %" PRId64,
-			 (int64_t) sb.st_size, (int64_t) after_pos);
-		SAFE_KILL(cleanup, child_pid, SIGKILL);
-	} else {
-		tst_resm(TPASS, "functionality of sendfile() is "
-			 "correct");
-		waitpid(-1, &wait_stat, 0);
-	}
-
-	SAFE_CLOSE(cleanup, in_fd);
-	SAFE_CLOSE(cleanup, out_fd);
-	SAFE_CLOSE(cleanup, sockfd);
-}
-
-static void do_child(void)
-{
-	socklen_t length = sizeof(sin1);
-	char rbuf[4096];
-	ssize_t ret, bytes_total_received = 0;
-
-	while (bytes_total_received < sb.st_size) {
-		ret = recvfrom(sockfd, rbuf, 4096, 0, (struct sockaddr *)&sin1,
-			       &length);
-		if (ret < 0) {
-			fprintf(stderr, "child process recvfrom failed: %s\n",
-				strerror(errno));
-			exit(1);
-		}
-		bytes_total_received += ret;
-	}
-
-	exit(0);
-}
-
 static void setup(void)
 {
 	int fd;
+	char buf[27];
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
+	fd = SAFE_CREAT(IN_FILE, 00700);
+	sprintf(buf, "abcdefghijklmnopqrstuvwxyz");
+	SAFE_WRITE(1, fd, buf, strlen(buf));
+	SAFE_FSTAT(fd, &sb);
+	SAFE_CLOSE(fd);
 
-	tst_tmpdir();
-
-	fd = SAFE_CREAT(cleanup, IN_FILE, 0600);
-
-	SAFE_WRITE(cleanup, 1, fd, "abcdefghijklmnopqrstuvwxyz", 26);
-
-	SAFE_FSTAT(cleanup, fd, &sb);
-
-	SAFE_CLOSE(cleanup, fd);
-}
-
-static void cleanup(void)
-{
-	tst_rmdir();
+	fd = SAFE_CREAT(OUT_FILE, 00700);
+	SAFE_CLOSE(fd);
 }
 
-static int create_server(void)
+static void run(void)
 {
-	int s;
-	socklen_t slen = sizeof(sin1);
-
-	sockfd = socket(PF_INET, SOCK_DGRAM, 0);
-	if (sockfd < 0) {
-		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
-			 strerror(errno));
-		return -1;
-	}
-	sin1.sin_family = AF_INET;
-	sin1.sin_port = 0; /* pick random free port */
-	sin1.sin_addr.s_addr = INADDR_ANY;
-
-	if (bind(sockfd, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
-		tst_brkm(TBROK, cleanup, "call to bind() failed: %s",
-			 strerror(errno));
-		return -1;
-	}
-	SAFE_GETSOCKNAME(cleanup, sockfd, (struct sockaddr *)&sin1, &slen);
-
-	child_pid = FORK_OR_VFORK();
-	if (child_pid < 0) {
-		tst_brkm(TBROK, cleanup, "client/server fork failed: %s",
-			 strerror(errno));
-		return -1;
-	}
-
-	if (!child_pid) {
-#ifdef UCLINUX
-		if (self_exec(argv0, "") < 0) {
-			tst_brkm(TBROK, cleanup, "self_exec failed");
-			return -1;
-
-		}
-#else
-		do_child();
-#endif
-	}
-
-	s = socket(PF_INET, SOCK_DGRAM, 0);
-	inet_aton("127.0.0.1", &sin1.sin_addr);
-
-	if (s < 0) {
-		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
-			 strerror(errno));
-		return -1;
-	}
-
-	SAFE_CONNECT(cleanup, s, (struct sockaddr *)&sin1, sizeof(sin1));
+	off_t after_pos;
+	int in_fd = SAFE_OPEN(IN_FILE, O_RDONLY);
+	int out_fd = SAFE_OPEN(OUT_FILE, O_WRONLY);
 
-	return s;
+	TEST(sendfile(out_fd, in_fd, NULL, sb.st_size));
+	after_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR);
+
+	if (TST_RET != sb.st_size)
+		tst_res(TFAIL, "sendfile() failed to return expected value, "
+			       "expected: %" PRId64 ", got: %ld",
+			sb.st_size, TST_RET);
+	else if (after_pos != sb.st_size)
+		tst_res(TFAIL, "sendfile() updated the file position of in_fd "
+			       "unexpectedly, expected file position: %" PRId64
+			       ", actual file position %" PRId64,
+			(int64_t)(sb.st_size), (int64_t)(after_pos));
+	else
+		tst_res(TPASS, "sendfile() with offset=NULL");
+
+	SAFE_CLOSE(in_fd);
+	SAFE_CLOSE(out_fd);
 }
 
-int main(int ac, char **av)
-{
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-#ifdef UCLINUX
-	argv0 = av[0];
-	maybe_run_child(&do_child, "");
-#endif
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		do_sendfile();
-	}
-
-	cleanup();
-	tst_exit();
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.test_all = run,
+};

--
2.17.1

#4 lore raw

PATCH 2/3 syscalls/sendfile: Convert sendfile06 to the new API

Hi Ziyao,

Quoted 2 lines

1. Convert sendfile06 to the new API with file descriptors instead of socket descriptors.

I wonder if this is ok, suppose yes, but better if other check.

Quoted 1 line

2. Remove the support for UCLINUX.

LGTM.

...

testcases/kernel/syscalls/sendfile/sendfile06.c line 51

Quoted 4 lines
testcases/kernel/syscalls/sendfile/sendfile06.c +4 -0
+	TEST(sendfile(out_fd, in_fd, NULL, sb.st_size));
+	after_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR);
+
+	if (TST_RET != sb.st_size)

nit: checkpatch.pl complains, it should be if (sb.st_size != TST_RET) (can be fixed during merge)

Kind regards,
Petr

author
#5 lore raw

PATCH 2/3 syscalls/sendfile: Convert sendfile06 to the new API

Hi, Petr,

Quoted 4 lines
Quoted 3 lines

1. Convert sendfile06 to the new API with file descriptors instead of socket descriptors.

I wonder if this is ok, suppose yes, but better if other check.

I noticed that it is described in man 2 sendfile as follows:

In Linux kernels before 2.6.33, out_fd must refer to a socket. Since Linux 2.6.33 it can be any file.

And if we need to cover the case where out_fd is a socket, I suggest adding it to sendfile02.c, which tests the basic functionality of the sendfile(), just like sendfile07.c:

in_fd = SAFE_OPEN("in_file", O_RDONLY);

SAFE_SOCKETPAIR(PF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, p); out_fd = p[1];

Quoted 6 lines
Quoted 2 lines

2. Remove the support for UCLINUX.

LGTM.

Quoted 7 lines

...

testcases/kernel/syscalls/sendfile/sendfile06.c line 51

Quoted 4 lines
testcases/kernel/syscalls/sendfile/sendfile06.c +4 -0
+	TEST(sendfile(out_fd, in_fd, NULL, sb.st_size));
+	after_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR);
+
+	if (TST_RET != sb.st_size)

nit: checkpatch.pl complains, it should be if (sb.st_size != TST_RET) (can be fixed during merge)

Oh yes, sometimes forgot to use checkpatch.pl to check my code files. Thanks for your tips, Petr.

Kind Regards,
Ziyao

#6 lore raw

PATCH 2/3 syscalls/sendfile: Convert sendfile06 to the new API

Hi Ziyao,

Quoted 9 lines

Hi, Petr,

Quoted 3 lines
Quoted 2 lines

1. Convert sendfile06 to the new API with file descriptors instead of socket descriptors.

I wonder if this is ok, suppose yes, but better if other check.

I noticed that it is described in man 2 sendfile as follows:

In Linux kernels before 2.6.33, out_fd must refer to a socket. Since Linux 2.6.33 it can be any file.

+1

Quoted 8 lines

And if we need to cover the case where out_fd is a socket, I suggest adding it to sendfile02.c, which tests the basic functionality of the sendfile(), just like sendfile07.c:

in_fd = SAFE_OPEN("in_file", O_RDONLY);

SAFE_SOCKETPAIR(PF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, p); out_fd = p[1];

Sounds reasonable, but given we test for compilation with 3.10 [1], we can forget on 2.6.33. We haven't decided the minimal version, maybe we should state that anything < 3.0 can be removed (it's probably to early to declare 3.10 as minimal supported version).

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

2. Remove the support for UCLINUX.

LGTM.

...

Quoted 8 lines
Quoted 6 lines

testcases/kernel/syscalls/sendfile/sendfile06.c line 51

Quoted 4 lines
testcases/kernel/syscalls/sendfile/sendfile06.c +4 -0
+	TEST(sendfile(out_fd, in_fd, NULL, sb.st_size));
+	after_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR);
+
+	if (TST_RET != sb.st_size)

nit: checkpatch.pl complains, it should be if (sb.st_size != TST_RET) (can be fixed during merge)

Oh yes, sometimes forgot to use checkpatch.pl to check my code files. Thanks for your tips, Petr.

np, it's really a detail.

Kind regards,
Petr

Quoted 2 lines

Kind Regards,
Ziyao

[1] https://github.com/linux-test-project/ltp/wiki/Supported-kernel,-libc,-toolchain-versions

#7 lore raw

PATCH 2/3 syscalls/sendfile: Convert sendfile06 to the new API

In reply to Petr Vorel

Hi!

Quoted 3 lines
Quoted 2 lines

1. Convert sendfile06 to the new API with file descriptors instead of socket descriptors.

I wonder if this is ok, suppose yes, but better if other check.

In kernels before 2.6.33 out_fd had to be a socket. Kernel 2.6.33 went out somewhere in 2010 which should be more than old enough.

Signature · 2 lines
Cyril Hrubis
[email protected]
#8 lore raw

PATCH 2/3 syscalls/sendfile: Convert sendfile06 to the new API

Hi all,

Quoted 7 lines

Hi!

Quoted 3 lines
Quoted 2 lines

1. Convert sendfile06 to the new API with file descriptors instead of socket descriptors.

I wonder if this is ok, suppose yes, but better if other check.

In kernels before 2.6.33 out_fd had to be a socket. Kernel 2.6.33 went out somewhere in 2010 which should be more than old enough.

Yes, Ziyao also explained that. Thus merged.
I forgot to mention the reason at commit message, fortunately unlike me other people aren't lazy to search in man page :).

Thanks!

Kind regards,
Petr

author
#9 lore raw

PATCH 3/3 syscalls/sendfile: Remove unnecessary header files

In reply to Xie Ziyao

Remove unnecessary header files for sendfile{03, 04, 05, 07}

---
 testcases/kernel/syscalls/sendfile/sendfile03.c | 3 ---
 testcases/kernel/syscalls/sendfile/sendfile04.c | 9 ---------
 testcases/kernel/syscalls/sendfile/sendfile05.c | 9 ---------
 testcases/kernel/syscalls/sendfile/sendfile07.c | 9 ---------
 4 files changed, 30 deletions(-)
testcases/kernel/syscalls/sendfile/sendfile03.c +0 -3
index 8783a6c34..3b93dc62e 100644
@@ -18,9 +18,6 @@
  * - out_fd opened with O_RDONLY
  */
 
-#include <stdio.h>
-#include <errno.h>
-#include <fcntl.h>
 #include <sys/sendfile.h>
 
 #include "tst_test.h"
testcases/kernel/syscalls/sendfile/sendfile04.c +0 -9
index 67d004c74..97e79eadd 100644
@@ -18,16 +18,7 @@
  * - a protected buffer is created by mmap with specifying protection
  */
 
-#include <stdio.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/stat.h>
 #include <sys/sendfile.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/mman.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 
 #include "tst_test.h"
 
testcases/kernel/syscalls/sendfile/sendfile05.c +0 -9
index 99c91f660..684536396 100644
@@ -16,16 +16,7 @@
  * Call sendfile with offset = -1.
  */
 
-#include <stdio.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/stat.h>
 #include <sys/sendfile.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/mman.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 
 #include "tst_test.h"
 
testcases/kernel/syscalls/sendfile/sendfile07.c +0 -9
index e8bf022a7..c75dea768 100644
@@ -12,16 +12,7 @@
  * when passing full out_fd opened with O_NONBLOCK.
  */
 
-#include <stdio.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/stat.h>
 #include <sys/sendfile.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/mman.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 
 #include "tst_test.h"
 

--
2.17.1

#10 lore raw

PATCH 3/3 syscalls/sendfile: Remove unnecessary header files

Hi Ziyao,

Quoted 1 line

Remove unnecessary header files for sendfile{03, 04, 05, 07}

Obviously correct, merged!

Kind regards,
Petr