* [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
* [LTP] [PATCH 1/2] lib: Add SAFE_KILL()
2014-05-15 10:32 [LTP] [PATCH] sendfile/sendfile06.c: cleanup and fix some bugs Xiaoguang Wang
@ 2014-05-16 4:52 ` 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
1 sibling, 1 reply; 4+ messages in thread
From: Xiaoguang Wang @ 2014-05-16 4:52 UTC (permalink / raw)
To: ltp-list
Signed-off-by: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
---
include/safe_macros.h | 5 +++++
lib/safe_macros.c | 16 ++++++++++++++++
2 files changed, 21 insertions(+)
diff --git a/include/safe_macros.h b/include/safe_macros.h
index 9e3ba98..c79dadf 100644
--- a/include/safe_macros.h
+++ b/include/safe_macros.h
@@ -231,5 +231,10 @@ pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
#define SAFE_WAITPID(cleanup_fn, pid, status, opts) \
safe_waitpid(__FILE__, __LINE__, (cleanup_fn), (pid), (status), (opts))
+int safe_kill(const char *file, const int lineno, void (cleanup_fn)(void),
+ pid_t pid, int sig);
+#define SAFE_KILL(cleanup_fn, pid, sig) \
+ safe_kill(__FILE__, __LINE__, (cleanup_fn), (pid), (sig))
+
#endif /* __SAFE_MACROS_H__ */
#endif /* __TEST_H__ */
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index 48a837c..81c66b5 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -659,3 +659,19 @@ pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
return rval;
}
+
+int safe_kill(const char *file, const int lineno, void (cleanup_fn)(void),
+ pid_t pid, int sig)
+{
+ int rval;
+
+ rval = kill(pid, sig);
+
+ if (rval == -1) {
+ tst_brkm(TBROK | TERRNO, cleanup_fn,
+ "%s:%d: kill(%d,%s) failed",
+ file, lineno, pid, tst_strsig(sig));
+ }
+
+ return rval;
+}
--
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
* Re: [LTP] [PATCH 1/2] lib: Add SAFE_KILL()
2014-05-16 4:52 ` [LTP] [PATCH 1/2] lib: Add SAFE_KILL() Xiaoguang Wang
@ 2014-05-16 4:55 ` Xiaoguang Wang
0 siblings, 0 replies; 4+ messages in thread
From: Xiaoguang Wang @ 2014-05-16 4:55 UTC (permalink / raw)
To: LTP
Hi,
I forgot to send this patch first, please review this patch first, thanks.
Regards,
Xiaoguang Wang
On 05/16/2014 12:52 PM, Xiaoguang Wang wrote:
> Signed-off-by: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
> ---
> include/safe_macros.h | 5 +++++
> lib/safe_macros.c | 16 ++++++++++++++++
> 2 files changed, 21 insertions(+)
>
> diff --git a/include/safe_macros.h b/include/safe_macros.h
> index 9e3ba98..c79dadf 100644
> --- a/include/safe_macros.h
> +++ b/include/safe_macros.h
> @@ -231,5 +231,10 @@ pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
> #define SAFE_WAITPID(cleanup_fn, pid, status, opts) \
> safe_waitpid(__FILE__, __LINE__, (cleanup_fn), (pid), (status), (opts))
>
> +int safe_kill(const char *file, const int lineno, void (cleanup_fn)(void),
> + pid_t pid, int sig);
> +#define SAFE_KILL(cleanup_fn, pid, sig) \
> + safe_kill(__FILE__, __LINE__, (cleanup_fn), (pid), (sig))
> +
> #endif /* __SAFE_MACROS_H__ */
> #endif /* __TEST_H__ */
> diff --git a/lib/safe_macros.c b/lib/safe_macros.c
> index 48a837c..81c66b5 100644
> --- a/lib/safe_macros.c
> +++ b/lib/safe_macros.c
> @@ -659,3 +659,19 @@ pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
>
> return rval;
> }
> +
> +int safe_kill(const char *file, const int lineno, void (cleanup_fn)(void),
> + pid_t pid, int sig)
> +{
> + int rval;
> +
> + rval = kill(pid, sig);
> +
> + if (rval == -1) {
> + tst_brkm(TBROK | TERRNO, cleanup_fn,
> + "%s:%d: kill(%d,%s) failed",
> + file, lineno, pid, tst_strsig(sig));
> + }
> +
> + return rval;
> +}
------------------------------------------------------------------------------
"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 [flat|nested] 4+ messages in thread
* Re: [LTP] [PATCH] sendfile/sendfile06.c: cleanup and fix some bugs
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-06-03 16:12 ` chrubis
1 sibling, 0 replies; 4+ messages in thread
From: chrubis @ 2014-06-03 16:12 UTC (permalink / raw)
To: Xiaoguang Wang; +Cc: ltp-list
Hi!
Both pushed, thanks.
(I've had to merge the changes to sendfile06 by hand due to changes in
parse_opts() and removal of STD_FUNCTIONAL_TEST)
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/NeoTech
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox