All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] sendfile/sendfile06.c: cleanup and fix some bugs
@ 2014-05-15 10:32 Xiaoguang Wang
  2014-05-16  4:52 ` [LTP] [PATCH 1/2] lib: Add SAFE_KILL() Xiaoguang Wang
  2014-06-03 16:12 ` [LTP] [PATCH] sendfile/sendfile06.c: cleanup and fix some bugs chrubis
  0 siblings, 2 replies; 4+ messages in thread
From: Xiaoguang Wang @ 2014-05-15 10:32 UTC (permalink / raw)
  To: ltp-list

If we run sendfile06 with "-i large number" option, we may get
EMFILE error, this is because we don't close the 'sockfd' and
'out_fd' file descriptor in do_sendfile(), fix this bug.

We also modify the code in do_child(), let child do not loop
'TEST_LOOPING(lc)'times. This makes no sense, and if we run with
"-i large number", this will slow down the program.

Delete some useless comments.
Use SAFE_* macros.
Some cleanup.

Signed-off-by: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
---
 testcases/kernel/syscalls/sendfile/sendfile06.c | 182 +++++++++---------------
 1 file changed, 65 insertions(+), 117 deletions(-)

diff --git a/testcases/kernel/syscalls/sendfile/sendfile06.c b/testcases/kernel/syscalls/sendfile/sendfile06.c
index 8e29f27..738f8e0 100644
--- a/testcases/kernel/syscalls/sendfile/sendfile06.c
+++ b/testcases/kernel/syscalls/sendfile/sendfile06.c
@@ -19,28 +19,14 @@
  */
 
 /*
- * NAME
- *	sendfile06.c
- *
  * DESCRIPTION
  *	Testcase to test that sendfile(2) system call updates file
  *	position of in_fd correctly when passing NULL as offset.
  *
- * USAGE:  <for command-line>
- *  sendfile06 [-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
  *	11/2007 Copyed from sendfile02.c by Masatake YAMATO
- *
- * RESTRICTIONS
- *	NONE
  */
+
 #include <inttypes.h>
 #include <stdio.h>
 #include <errno.h>
@@ -53,26 +39,26 @@
 #include <sys/mman.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <string.h>
 #include "usctest.h"
 #include "test.h"
+#include "safe_macros.h"
 
-#ifndef OFF_T
-#define OFF_T off_t
-#endif /* Not def: OFF_T */
 
 TCID_DEFINE(sendfile06);
 
-char in_file[100];
-char out_file[100];
-int out_fd;
-pid_t child_pid;
-static int sockfd, s;
+#define IN_FILE		"infile"
+#define OUT_FILE	"outfile"
+
+static pid_t child_pid;
+static int sockfd;
 static struct sockaddr_in sin1;	/* shared between do_child and create_server */
+static struct stat sb;
 
-void cleanup(void);
-void do_child(void);
-void setup(void);
-int create_server(void);
+static void cleanup(void);
+static void do_child(void);
+static void setup(void);
+static int create_server(void);
 
 int TST_TOTAL = 1;
 
@@ -80,22 +66,15 @@ int TST_TOTAL = 1;
 static char *argv0;
 #endif
 
-void do_sendfile(void)
+static void do_sendfile(void)
 {
-	int in_fd;
-	struct stat sb;
+	int in_fd, out_fd;
 	off_t after_pos;
-	int wait_status;
 	int wait_stat;
 
 	out_fd = create_server();
 
-	if ((in_fd = open(in_file, O_RDONLY)) < 0) {
-		tst_brkm(TBROK, cleanup, "open failed: %d", errno);
-	}
-	if (stat(in_file, &sb) < 0) {
-		tst_brkm(TBROK, cleanup, "stat failed: %d", errno);
-	}
+	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) {
@@ -103,107 +82,79 @@ void do_sendfile(void)
 			 "lseek after invoking sendfile failed: %d", errno);
 	}
 
-	if (STD_FUNCTIONAL_TEST) {
-		/* Close the sockets */
-		shutdown(sockfd, SHUT_RDWR);
-		shutdown(s, 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);
-			kill(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);
-			kill(child_pid, SIGKILL);
-		} else {
-			tst_resm(TPASS, "functionality of sendfile() is "
-				 "correct");
-			wait_status = waitpid(-1, &wait_stat, 0);
-		}
+	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, "call succeeded");
-		/* Close the sockets */
-		shutdown(sockfd, SHUT_RDWR);
-		shutdown(s, SHUT_RDWR);
-		if (TEST_RETURN != sb.st_size) {
-			kill(child_pid, SIGKILL);
-		} else {
-			wait_status = waitpid(-1, &wait_stat, 0);
-		}
+		tst_resm(TPASS, "functionality of sendfile() is correct");
+		waitpid(-1, &wait_stat, 0);
 	}
 
-	close(in_fd);
+	SAFE_CLOSE(cleanup, in_fd);
+	SAFE_CLOSE(cleanup, out_fd);
+	SAFE_CLOSE(cleanup, sockfd);
 }
 
-/*
- * do_child
- */
-void do_child(void)
+static void do_child(void)
 {
-	int lc;
-	socklen_t length;
+	socklen_t length = sizeof(sin1);
 	char rbuf[4096];
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		length = sizeof(sin1);
-		recvfrom(sockfd, rbuf, 4096, 0, (struct sockaddr *)&sin1,
-			 &length);
+	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);
 }
 
-/*
- * 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);
 
 	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);
-	}
-	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());
+
+	fd = SAFE_CREAT(cleanup, IN_FILE, 0600);
+
+	SAFE_WRITE(cleanup, 1, fd, "abcdefghijklmnopqrstuvwxyz", 26);
+
+	SAFE_FSTAT(cleanup, fd, &sb);
+
+	SAFE_CLOSE(cleanup, fd);
 }
 
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- *	       completion or premature exit.
- */
-void cleanup(void)
+static void cleanup(void)
 {
-	/*
-	 * print timing stats if that option was specified.
-	 * print errno log if that option was specified.
-	 */
 	TEST_CLEANUP;
 
-	close(out_fd);
-	/* delete the test directory created in setup() */
 	tst_rmdir();
-
 }
 
-int create_server(void)
+static int create_server(void)
 {
-	static int count = 0;
+	int s;
 	socklen_t slen = sizeof(sin1);
 
 	sockfd = socket(PF_INET, SOCK_DGRAM, 0);
@@ -215,7 +166,7 @@ int create_server(void)
 	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));
@@ -230,6 +181,7 @@ int create_server(void)
 			 strerror(errno));
 		return -1;
 	}
+
 	if (!child_pid) {	/* child */
 #ifdef UCLINUX
 		if (self_exec(argv0, "") < 0) {
@@ -254,13 +206,12 @@ int create_server(void)
 			 strerror(errno));
 	}
 	return s;
-
 }
 
 int main(int ac, char **av)
 {
 	int lc;
-	char *msg;		/* parse_opts() return message */
+	char *msg;
 
 	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
 		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
@@ -272,15 +223,12 @@ int main(int ac, char **av)
 
 	setup();
 
-	/*
-	 * The following loop checks looping state if -c option given
-	 */
 	for (lc = 0; TEST_LOOPING(lc); lc++) {
 		tst_count = 0;
 
 		do_sendfile();
 	}
-	cleanup();
 
+	cleanup();
 	tst_exit();
 }
-- 
1.8.2.1


------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-06-03 16:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-15 10:32 [LTP] [PATCH] sendfile/sendfile06.c: cleanup and fix some bugs Xiaoguang Wang
2014-05-16  4:52 ` [LTP] [PATCH 1/2] lib: Add SAFE_KILL() Xiaoguang Wang
2014-05-16  4:55   ` Xiaoguang Wang
2014-06-03 16:12 ` [LTP] [PATCH] sendfile/sendfile06.c: cleanup and fix some bugs chrubis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.