[CRIU] [PATCH 3/3] zdtm: move common code to the library
Mike Rapoport
rppt at linux.vnet.ibm.com
Thu Nov 30 14:46:20 MSK 2017
The fill_sock_buf and clean_sk_buff functions used by several tests.
Move them to the library code.
Signed-off-by: Mike Rapoport <rppt at linux.vnet.ibm.com>
---
test/zdtm/lib/streamutil.c | 69 ++++++++++++++++++++++++++++++++
test/zdtm/lib/zdtmtst.h | 4 +-
test/zdtm/static/socket-tcp-close-wait.c | 68 +------------------------------
test/zdtm/static/socket-tcp-closing.c | 67 +------------------------------
test/zdtm/static/socket-tcp-fin-wait1.c | 65 +-----------------------------
test/zdtm/static/socket-tcpbuf.c | 62 ----------------------------
6 files changed, 77 insertions(+), 258 deletions(-)
diff --git a/test/zdtm/lib/streamutil.c b/test/zdtm/lib/streamutil.c
index 4ebe05c..681d5a0 100644
--- a/test/zdtm/lib/streamutil.c
+++ b/test/zdtm/lib/streamutil.c
@@ -69,3 +69,72 @@ int write_data(int fd, const unsigned char *buf, int size)
return 0;
}
+
+int fill_sock_buf(int fd)
+{
+ int flags;
+ int size;
+ int ret;
+
+ flags = fcntl(fd, F_GETFL, 0);
+ if (flags == -1) {
+ pr_err("Can't get flags");
+ return -1;
+ }
+ if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
+ pr_err("Can't set flags");
+ return -1;
+ }
+
+ size = 0;
+ while (1) {
+ char zdtm[] = "zdtm test packet";
+ ret = write(fd, zdtm, sizeof(zdtm));
+ if (ret == -1) {
+ if (errno == EAGAIN)
+ break;
+ pr_err("write");
+ return -1;
+ }
+ size += ret;
+ }
+
+ if (fcntl(fd, F_SETFL, flags) == -1) {
+ pr_err("Can't set flags");
+ return -1;
+ }
+
+ test_msg("snd_size = %d\n", size);
+
+ return size;
+}
+
+#define BUF_SIZE 4096
+
+int clean_sk_buf(int fd, int limit)
+{
+ int size, ret;
+ char buf[BUF_SIZE];
+
+ size = 0;
+ while (1) {
+ ret = read(fd, buf, sizeof(buf));
+ if (ret == -1) {
+ pr_err("read");
+ return -11;
+ }
+
+ if (ret == 0)
+ break;
+
+ size += ret;
+
+ if (limit && size >= limit)
+ break;
+
+ }
+
+ test_msg("rcv_size = %d\n", size);
+
+ return size;
+}
diff --git a/test/zdtm/lib/zdtmtst.h b/test/zdtm/lib/zdtmtst.h
index 737ac77..a3ee2ec 100644
--- a/test/zdtm/lib/zdtmtst.h
+++ b/test/zdtm/lib/zdtmtst.h
@@ -57,6 +57,8 @@ extern int set_nonblock(int fd, int on);
extern int pipe_in2out(int infd, int outfd, uint8_t *buffer, int length);
extern int read_data(int fd, unsigned char *buf, int len);
extern int write_data(int fd, const unsigned char *buf, int len);
+extern int fill_sock_buf(int fd);
+extern int clean_sk_buf(int fd, int limit);
/* command line args */
struct long_opt {
@@ -109,7 +111,7 @@ extern int write_pidfile(int pid);
#define __stringify(x) __stringify_1(x)
/*
- * Macro to define stack alignment.
+ * Macro to define stack alignment.
* aarch64 requires stack to be aligned to 16 bytes.
*/
#define __stack_aligned__ __attribute__((aligned(16)))
diff --git a/test/zdtm/static/socket-tcp-close-wait.c b/test/zdtm/static/socket-tcp-close-wait.c
index 3e3462b..e57fec6 100644
--- a/test/zdtm/static/socket-tcp-close-wait.c
+++ b/test/zdtm/static/socket-tcp-close-wait.c
@@ -28,70 +28,6 @@ const char *test_author = "Andrey Vagin <avagin at parallels.com";
static int port = 8880;
#define BUF_SIZE 4096
-
-int fill_sock_buf(int fd)
-{
- int flags;
- int size;
- int ret;
-
- flags = fcntl(fd, F_GETFL, 0);
- if (flags == -1) {
- pr_err("Can't get flags");
- return -1;
- }
- if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
- pr_err("Can't set flags");
- return -1;
- }
-
- size = 0;
- while (1) {
- char zdtm[] = "zdtm test packet";
- ret = write(fd, zdtm, sizeof(zdtm));
- if (ret == -1) {
- if (errno == EAGAIN)
- break;
- pr_err("write");
- return -1;
- }
- size += ret;
- }
-
- if (fcntl(fd, F_SETFL, flags) == -1) {
- pr_err("Can't set flags");
- return -1;
- }
-
- test_msg("snd_size = %d\n", size);
-
- return size;
-}
-
-static int clean_sk_buf(int fd)
-{
- int size, ret;
- char buf[BUF_SIZE];
-
- size = 0;
- while (1) {
- ret = read(fd, buf, sizeof(buf));
- if (ret == -1) {
- pr_err("read");
- return -11;
- }
-
- if (ret == 0)
- break;
-
- size += ret;
- }
-
- test_msg("rcv_size = %d\n", size);
-
- return size;
-}
-
#define TEST_MSG "Hello World!"
int main(int argc, char **argv)
@@ -165,7 +101,7 @@ int main(int argc, char **argv)
}
#ifdef ZDTM_TCP_LAST_ACK
- size = clean_sk_buf(fd);
+ size = clean_sk_buf(fd, 0);
if (size < 0)
return 1;
#else
@@ -264,7 +200,7 @@ int main(int argc, char **argv)
}
#endif
- rcv_size = clean_sk_buf(fd);
+ rcv_size = clean_sk_buf(fd, 0);
if (ret != rcv_size) {
fail("The child sent %d bytes, but the parent received %d bytes\n", ret, rcv_size);
diff --git a/test/zdtm/static/socket-tcp-closing.c b/test/zdtm/static/socket-tcp-closing.c
index 23d49cc..4e4d324 100644
--- a/test/zdtm/static/socket-tcp-closing.c
+++ b/test/zdtm/static/socket-tcp-closing.c
@@ -29,69 +29,6 @@ static int port = 8880;
#define BUF_SIZE 4096
-int fill_sock_buf(int fd)
-{
- int flags;
- int size;
- int ret;
-
- flags = fcntl(fd, F_GETFL, 0);
- if (flags == -1) {
- pr_err("Can't get flags");
- return -1;
- }
- if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
- pr_err("Can't set flags");
- return -1;
- }
-
- size = 0;
- while (1) {
- char zdtm[] = "zdtm test packet";
- ret = write(fd, zdtm, sizeof(zdtm));
- if (ret == -1) {
- if (errno == EAGAIN)
- break;
- pr_err("write");
- return -1;
- }
- size += ret;
- }
-
- if (fcntl(fd, F_SETFL, flags) == -1) {
- pr_err("Can't set flags");
- return -1;
- }
-
- test_msg("snd_size = %d\n", size);
-
- return size;
-}
-
-static int clean_sk_buf(int fd)
-{
- int size, ret;
- char buf[BUF_SIZE];
-
- size = 0;
- while (1) {
- ret = read(fd, buf, sizeof(buf));
- if (ret == -1) {
- pr_err("read");
- return -11;
- }
-
- if (ret == 0)
- break;
-
- size += ret;
- }
-
- test_msg("rcv_size = %d\n", size);
-
- return size;
-}
-
int main(int argc, char **argv)
{
char *newns = getenv("ZDTM_NEWNS");
@@ -152,7 +89,7 @@ int main(int argc, char **argv)
return 1;
}
- size = clean_sk_buf(fd);
+ size = clean_sk_buf(fd, 0);
if (size < 0)
return 1;
@@ -206,7 +143,7 @@ int main(int argc, char **argv)
return 1;
}
- rcv_size = clean_sk_buf(fd);
+ rcv_size = clean_sk_buf(fd, 0);
if (ret != rcv_size) {
fail("The child sent %d bytes, but the parent received %d bytes\n", ret, rcv_size);
diff --git a/test/zdtm/static/socket-tcp-fin-wait1.c b/test/zdtm/static/socket-tcp-fin-wait1.c
index 6c7cc93..286078c 100644
--- a/test/zdtm/static/socket-tcp-fin-wait1.c
+++ b/test/zdtm/static/socket-tcp-fin-wait1.c
@@ -30,69 +30,6 @@ static int port = 8880;
#define TEST_MSG "Hello World!"
#define BUF_SIZE 4096
-int fill_sock_buf(int fd)
-{
- int flags;
- int size;
- int ret;
-
- flags = fcntl(fd, F_GETFL, 0);
- if (flags == -1) {
- pr_err("Can't get flags");
- return -1;
- }
- if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
- pr_err("Can't set flags");
- return -1;
- }
-
- size = 0;
- while (1) {
- char zdtm[] = "zdtm test packet";
- ret = write(fd, zdtm, sizeof(zdtm));
- if (ret == -1) {
- if (errno == EAGAIN)
- break;
- pr_err("write");
- return -1;
- }
- size += ret;
- }
-
- if (fcntl(fd, F_SETFL, flags) == -1) {
- pr_err("Can't set flags");
- return -1;
- }
-
- test_msg("snd_size = %d\n", size);
-
- return size;
-}
-
-static int clean_sk_buf(int fd)
-{
- int size, ret;
- char buf[BUF_SIZE];
-
- size = 0;
- while (1) {
- ret = read(fd, buf, sizeof(buf));
- if (ret == -1) {
- pr_err("read");
- return -11;
- }
-
- if (ret == 0)
- break;
-
- size += ret;
- }
-
- test_msg("rcv_size = %d\n", size);
-
- return size;
-}
-
int main(int argc, char **argv)
{
char *newns = getenv("ZDTM_NEWNS");
@@ -151,7 +88,7 @@ int main(int argc, char **argv)
return 1;
}
- size = clean_sk_buf(fd);
+ size = clean_sk_buf(fd, 0);
if (size < 0)
return 1;
diff --git a/test/zdtm/static/socket-tcpbuf.c b/test/zdtm/static/socket-tcpbuf.c
index f61f6a5..f7d8b69 100644
--- a/test/zdtm/static/socket-tcpbuf.c
+++ b/test/zdtm/static/socket-tcpbuf.c
@@ -41,68 +41,6 @@ static void write_safe(int fd, void *buf, size_t size)
}
}
-static int fill_sock_buf(int fd)
-{
- int flags;
- int size;
- int ret;
-
- flags = fcntl(fd, F_GETFL, 0);
- if (flags == -1) {
- pr_perror("Can't get flags");
- return -1;
- }
- if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
- pr_perror("Can't set flags");
- return -1;
- }
-
- size = 0;
- while (1) {
- char zdtm[] = "zdtm test packet";
- ret = write(fd, zdtm, sizeof(zdtm));
- if (ret == -1) {
- if (errno == EAGAIN)
- break;
- pr_perror("write");
- return -1;
- }
- size += ret;
- }
-
- if (fcntl(fd, F_SETFL, flags) == -1) {
- pr_perror("Can't set flags");
- return -1;
- }
-
- return size;
-}
-
-static int clean_sk_buf(int fd, int limit)
-{
- int size, ret;
- char buf[BUF_SIZE];
-
- size = 0;
- while (1) {
- ret = read(fd, buf, sizeof(buf));
- if (ret == -1) {
- pr_perror("read");
- return -11;
- }
-
- if (ret == 0)
- break;
-
- size += ret;
-
- if (limit && size >= limit)
- break;
- }
-
- return size;
-}
-
int main(int argc, char **argv)
{
int fd, fd_s, ctl_fd;
--
2.7.4
More information about the CRIU
mailing list