[CRIU] [PATCH 2/3] zdtm: use {read,write}_data where applicable

Mike Rapoport rppt at linux.vnet.ibm.com
Thu Nov 30 14:46:19 MSK 2017


Reading and writing large buffers may result in short read/write. In cases
we expect the entire buffer to be transferred use {read,write}_data rather
than plain read/write syscalls.

Signed-off-by: Mike Rapoport <rppt at linux.vnet.ibm.com>
---
 test/zdtm/static/deleted_unix_sock.c |  4 ++--
 test/zdtm/static/fifo-ghost.c        |  7 ++-----
 test/zdtm/static/fifo.c              |  7 ++-----
 test/zdtm/static/fifo_ro.c           |  7 ++-----
 test/zdtm/static/file_attr.c         |  4 ++--
 test/zdtm/static/overmount_sock.c    |  4 ++--
 test/zdtm/static/socket-tcp.c        | 29 -----------------------------
 test/zdtm/static/unlink_fstat00.c    |  4 ++--
 test/zdtm/static/write_read00.c      |  4 ++--
 test/zdtm/static/write_read01.c      |  6 +++---
 test/zdtm/static/write_read02.c      |  8 ++++----
 test/zdtm/static/write_read10.c      |  4 ++--
 12 files changed, 25 insertions(+), 63 deletions(-)

diff --git a/test/zdtm/static/deleted_unix_sock.c b/test/zdtm/static/deleted_unix_sock.c
index bcc33f3..f02ec11 100644
--- a/test/zdtm/static/deleted_unix_sock.c
+++ b/test/zdtm/static/deleted_unix_sock.c
@@ -113,7 +113,7 @@ int main(int argc, char ** argv)
 
 		crc = ~0;
 		datagen(buf, sizeof(buf), &crc);
-		if (write(sock, buf, sizeof(buf)) != sizeof(buf)) {
+		if (write_data(sock, buf, sizeof(buf))) {
 			pr_perror("can't write to socket");
 			exit(errno);
 		}
@@ -161,7 +161,7 @@ int main(int argc, char ** argv)
 		goto out;
 	}
 
-	if (read(sock, buf, sizeof(buf)) != sizeof(buf)) {
+	if (read_data(sock, buf, sizeof(buf))) {
 		fail("can't read %s: %m\n", filename);
 		goto out;
 	}
diff --git a/test/zdtm/static/fifo-ghost.c b/test/zdtm/static/fifo-ghost.c
index 94a8c57..f5e11cf 100644
--- a/test/zdtm/static/fifo-ghost.c
+++ b/test/zdtm/static/fifo-ghost.c
@@ -20,7 +20,6 @@ int main(int argc, char **argv)
 	mode_t mode = S_IFIFO | 0700;
 	uint8_t buf[256];
 	uint32_t crc;
-	int ret;
 
 	test_init(argc, argv);
 
@@ -43,8 +42,7 @@ int main(int argc, char **argv)
 
 	crc = ~0;
 	datagen(buf, sizeof(buf), &crc);
-	ret = write(fd, buf, sizeof(buf));
-	if (ret != sizeof(buf)) {
+	if (write_data(fd, buf, sizeof(buf))) {
 		pr_perror("write() failed");
 		return 1;
 	}
@@ -59,8 +57,7 @@ int main(int argc, char **argv)
 	test_daemon();
 	test_waitsig();
 
-	ret = read(fd_ro, buf, sizeof(buf));
-	if (ret != sizeof(buf)) {
+	if (read_data(fd_ro, buf, sizeof(buf))) {
 		pr_perror("read() failed");
 		return 1;
 	}
diff --git a/test/zdtm/static/fifo.c b/test/zdtm/static/fifo.c
index c8437d9..ab5674a 100644
--- a/test/zdtm/static/fifo.c
+++ b/test/zdtm/static/fifo.c
@@ -23,7 +23,6 @@ int main(int argc, char **argv)
 	mode_t mode = S_IFIFO | 0700;
 	uint8_t buf[BUF_SIZE];
 	uint32_t crc;
-	int ret;;
 
 	test_init(argc, argv);
 
@@ -40,8 +39,7 @@ int main(int argc, char **argv)
 
 	crc = ~0;
 	datagen(buf, BUF_SIZE, &crc);
-	ret = write(fd, buf, BUF_SIZE);
-	if (ret != BUF_SIZE) {
+	if (write_data(fd, buf, BUF_SIZE)) {
 		pr_perror("write() failed");
 		return 1;
 	}
@@ -49,8 +47,7 @@ int main(int argc, char **argv)
 	test_daemon();
 	test_waitsig();
 
-	ret = read(fd, buf, BUF_SIZE);
-	if (ret != BUF_SIZE) {
+	if (read_data(fd, buf, BUF_SIZE)) {
 		pr_perror("read() failed");
 		return 1;
 	}
diff --git a/test/zdtm/static/fifo_ro.c b/test/zdtm/static/fifo_ro.c
index 63c4d29..ea32329 100644
--- a/test/zdtm/static/fifo_ro.c
+++ b/test/zdtm/static/fifo_ro.c
@@ -23,7 +23,6 @@ int main(int argc, char **argv)
 	mode_t mode = S_IFIFO | 0700;
 	uint8_t buf[BUF_SIZE];
 	uint32_t crc;
-	int ret;;
 
 	test_init(argc, argv);
 
@@ -46,8 +45,7 @@ int main(int argc, char **argv)
 
 	crc = ~0;
 	datagen(buf, BUF_SIZE, &crc);
-	ret = write(fd, buf, BUF_SIZE);
-	if (ret != BUF_SIZE) {
+	if (write_data(fd, buf, BUF_SIZE)) {
 		pr_perror("write() failed");
 		return 1;
 	}
@@ -57,8 +55,7 @@ int main(int argc, char **argv)
 	test_daemon();
 	test_waitsig();
 
-	ret = read(fd_ro, buf, BUF_SIZE);
-	if (ret != BUF_SIZE) {
+	if (read_data(fd_ro, buf, BUF_SIZE)) {
 		pr_perror("read() failed");
 		return 1;
 	}
diff --git a/test/zdtm/static/file_attr.c b/test/zdtm/static/file_attr.c
index eb1c2a2..d9e3e78 100644
--- a/test/zdtm/static/file_attr.c
+++ b/test/zdtm/static/file_attr.c
@@ -44,7 +44,7 @@ int main(int argc, char ** argv)
 
 	crc = ~0;
 	datagen(buf, sizeof(buf), &crc);
-	if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
+	if (write_data(fd, buf, sizeof(buf))) {
 		pr_perror("can't write to %s", filename);
 		exit(1);
 	}
@@ -76,7 +76,7 @@ int main(int argc, char ** argv)
 		goto out;
 	}
 
-	if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
+	if (read_data(fd, buf, sizeof(buf))) {
 		fail("can't read %s: %m\n", filename);
 		goto out;
 	}
diff --git a/test/zdtm/static/overmount_sock.c b/test/zdtm/static/overmount_sock.c
index 94e4c7e..39b714d 100644
--- a/test/zdtm/static/overmount_sock.c
+++ b/test/zdtm/static/overmount_sock.c
@@ -122,7 +122,7 @@ int main(int argc, char ** argv)
 
 		crc = ~0;
 		datagen(buf, sizeof(buf), &crc);
-		if (write(sock, buf, sizeof(buf)) != sizeof(buf))
+		if (write_data(sock, buf, sizeof(buf)))
 			_exit(errno);
 
 		close(sock);
@@ -169,7 +169,7 @@ int main(int argc, char ** argv)
 		goto out;
 	}
 
-	if (read(sock, buf, sizeof(buf)) != sizeof(buf)) {
+	if (read_data(sock, buf, sizeof(buf))) {
 		fail("can't read %s: %m\n", path);
 		goto out;
 	}
diff --git a/test/zdtm/static/socket-tcp.c b/test/zdtm/static/socket-tcp.c
index 6b30fc4..2be0072 100644
--- a/test/zdtm/static/socket-tcp.c
+++ b/test/zdtm/static/socket-tcp.c
@@ -28,35 +28,6 @@ static int port = 8880;
 
 #define BUF_SIZE 4096
 
-int read_data(int fd, unsigned char *buf, int size)
-{
-	int cur = 0;
-	int ret;
-	while (cur != size) {
-		ret = read(fd, buf + cur, size - cur);
-		if (ret <= 0)
-			return -1;
-		cur += ret;
-	}
-
-	return 0;
-}
-
-int write_data(int fd, const unsigned char *buf, int size)
-{
-	int cur = 0;
-	int ret;
-
-	while (cur != size) {
-		ret = write(fd, buf + cur, size - cur);
-		if (ret <= 0)
-			return -1;
-		cur += ret;
-	}
-
-	return 0;
-}
-
 int main(int argc, char **argv)
 {
 	unsigned char buf[BUF_SIZE];
diff --git a/test/zdtm/static/unlink_fstat00.c b/test/zdtm/static/unlink_fstat00.c
index 88a8647..0004857 100644
--- a/test/zdtm/static/unlink_fstat00.c
+++ b/test/zdtm/static/unlink_fstat00.c
@@ -70,7 +70,7 @@ int main(int argc, char ** argv)
 
 	crc = ~0;
 	datagen(buf, sizeof(buf), &crc);
-	if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
+	if (write_data(fd, buf, sizeof(buf))) {
 		pr_perror("can't write %s", filename);
 		goto failed;
 	}
@@ -137,7 +137,7 @@ int main(int argc, char ** argv)
 		pr_perror("can't reposition to 0");
 		goto failed;
 	}
-	if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
+	if (read_data(fd, buf, sizeof(buf))) {
 		fail("can't read %s: %m\n", filename);
 		goto failed;
 	}
diff --git a/test/zdtm/static/write_read00.c b/test/zdtm/static/write_read00.c
index 1648e35..c214eac 100644
--- a/test/zdtm/static/write_read00.c
+++ b/test/zdtm/static/write_read00.c
@@ -27,7 +27,7 @@ int main(int argc, char ** argv)
 
 	crc = ~0;
 	datagen(buf, sizeof(buf), &crc);
-	if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
+	if (write_data(fd, buf, sizeof(buf))) {
 		pr_perror("can't write %s", filename);
 		exit(1);
 	}
@@ -43,7 +43,7 @@ int main(int argc, char ** argv)
 		exit(1);
 	}
 
-	if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
+	if (read_data(fd, buf, sizeof(buf))) {
 		fail("can't read %s: %m\n", filename);
 		goto out;
 	}
diff --git a/test/zdtm/static/write_read01.c b/test/zdtm/static/write_read01.c
index 0d41767..6ff1d07 100644
--- a/test/zdtm/static/write_read01.c
+++ b/test/zdtm/static/write_read01.c
@@ -28,7 +28,7 @@ int main(int argc, char ** argv)
 
 	crc = ~0;
 	datagen(buf, sizeof(buf), &crc);
-	if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
+	if (write_data(fd, buf, sizeof(buf))) {
 		pr_perror("can't write %s", filename);
 		exit(1);
 	}
@@ -42,7 +42,7 @@ int main(int argc, char ** argv)
 	}
 
 	len = sizeof(buf) / 2;
-	if (read(fd, buf, len) != len) {
+	if (read_data(fd, buf, len)) {
 		pr_perror("can't read %s", filename);
 		exit(1);
 	}
@@ -51,7 +51,7 @@ int main(int argc, char ** argv)
 	test_waitsig();
 
 	/* recover reading */
-	if (read(fd, buf + len, sizeof(buf) - len) != (sizeof(buf) - len)) {
+	if (read_data(fd, buf + len, sizeof(buf) - len)) {
 		fail("can't read %s: %m\n", filename);
 		goto out;
 	}
diff --git a/test/zdtm/static/write_read02.c b/test/zdtm/static/write_read02.c
index 0a80558..1936511 100644
--- a/test/zdtm/static/write_read02.c
+++ b/test/zdtm/static/write_read02.c
@@ -34,14 +34,14 @@ int main(int argc, char ** argv)
 	// create standard file
 	sprintf(str, "standard_%s", filename);
 	fd1 = open(str, O_WRONLY | O_CREAT | O_TRUNC, 0644);
-	if (write(fd1, buf, full_len) != full_len) {
+	if (write_data(fd1, buf, full_len)) {
 		pr_perror("can't write %s", str);
 		exit(1);
 	}
 	close(fd1);
 
 	len = sizeof(buf) / 2;
-	if (write(fd, buf, len) != len) {
+	if (write_data(fd, buf, len)) {
 		pr_perror("can't write %s", filename);
 		exit(1);
 	}
@@ -49,7 +49,7 @@ int main(int argc, char ** argv)
 	test_daemon();
 	test_waitsig();
 
-	if (write(fd, buf + len, sizeof(buf) - len) != (sizeof(buf) - len)) {
+	if (write_data(fd, buf + len, sizeof(buf) - len)) {
 		fail("can't write %s: %m\n", filename);
 		goto out;
 	}
@@ -62,7 +62,7 @@ int main(int argc, char ** argv)
 		return 1;
 	}
 
-	if (read(fd, buf, full_len) != full_len) {
+	if (read_data(fd, buf, full_len)) {
 		fail("can't read %s: %m\n", filename);
 		return 1;
 	}
diff --git a/test/zdtm/static/write_read10.c b/test/zdtm/static/write_read10.c
index 4e8a67e..4d41303 100644
--- a/test/zdtm/static/write_read10.c
+++ b/test/zdtm/static/write_read10.c
@@ -58,7 +58,7 @@ int main(int argc, char ** argv)
 
 		crc = ~0;
 		datagen(buf, sizeof(buf), &crc);
-		if (write(child_fd, buf, sizeof(buf)) != sizeof(buf))
+		if (write_data(child_fd, buf, sizeof(buf)))
 			_exit(errno);
 
 		close(child_fd);
@@ -98,7 +98,7 @@ int main(int argc, char ** argv)
 		goto out;
 	}
 
-	if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
+	if (read_data(fd, buf, sizeof(buf))) {
 		fail("can't read %s: %m\n", filename);
 		goto out;
 	}
-- 
2.7.4



More information about the CRIU mailing list