git: 9ac518adf03e - main - stress2: Update test to ensure propper cleanup of fifo files

Peter Holm pho at FreeBSD.org
Mon Sep 20 07:39:11 UTC 2021


The branch main has been updated by pho:

URL: https://cgit.FreeBSD.org/src/commit/?id=9ac518adf03e371aa3e8499f84d995663bd4e0c1

commit 9ac518adf03e371aa3e8499f84d995663bd4e0c1
Author:     Peter Holm <pho at FreeBSD.org>
AuthorDate: 2021-09-20 07:36:27 +0000
Commit:     Peter Holm <pho at FreeBSD.org>
CommitDate: 2021-09-20 07:36:27 +0000

    stress2: Update test to ensure propper cleanup of fifo files
---
 tools/test/stress2/testcases/mkfifo/mkfifo.c | 67 +++++++++++-----------------
 1 file changed, 25 insertions(+), 42 deletions(-)

diff --git a/tools/test/stress2/testcases/mkfifo/mkfifo.c b/tools/test/stress2/testcases/mkfifo/mkfifo.c
index caeb5e45310b..fd4a68b7e0f2 100644
--- a/tools/test/stress2/testcases/mkfifo/mkfifo.c
+++ b/tools/test/stress2/testcases/mkfifo/mkfifo.c
@@ -26,82 +26,63 @@
  */
 
 #include <sys/param.h>
-#include <sys/types.h>
 #include <sys/stat.h>
-#include <stdio.h>
+#include <sys/wait.h>
+
+#include <err.h>
 #include <fcntl.h>
-#include <stdlib.h>
-#include <unistd.h>
 #include <signal.h>
-#include <sys/wait.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
-#include <err.h>
+#include <unistd.h>
 
 #include "stress.h"
 
 static char path[MAXPATHLEN+1];
-#define NB (1 * 1024 * 1024)
-
-static int bufsize;
-static int freespace;
-
-static void
-handler2(int i __unused)
-{
-	_exit(0);
-}
+static int bufsize, freespace;
 
 static void
 reader(void) {
-	int fd;
-	int i, n, *buf;
+	fd_set set;
+	struct timeval tv;
+	int *buf, fd, n;
 
 	setproctitle("reader");
-	signal(SIGALRM, handler2);
-	alarm(60);
-	if ((fd = open(path, O_RDWR, 0600)) < 0) {
-		unlink(path);
+	if ((fd = open(path, O_RDONLY | O_NONBLOCK)) < 0)
 		err(1, "open(%s)", path);
-	}
 	if ((buf = malloc(bufsize)) == NULL)
 			err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
-	for (i = 0; i < NB; i+= bufsize) {
+	n = 0;
+	FD_ZERO(&set);
+	FD_SET(fd, &set);
+	tv.tv_sec = 10;
+	tv.tv_usec = 0;
+	if (select(fd + 1, &set, NULL, NULL, &tv) == 1) {
 		if ((n = read(fd, buf, bufsize)) < 0)
 			err(1, "read(), %s:%d", __FILE__, __LINE__);
-		if (n == 0) break;
 	}
 	close(fd);
 	free(buf);
-	return;
 }
 
 static void
 writer(void) {
-	int i, *buf;
-	int fd;
-
-	signal(SIGALRM, handler2);
-	alarm(60);
+	int *buf, fd;
 
 	setproctitle("writer");
-	if ((fd = open(path, O_RDWR, 0600)) < 0) {
+	if ((fd = open(path, O_WRONLY)) < 0) {
 		unlink(path);
 		err(1, "open(%s)", path);
 	}
 	if ((buf = malloc(bufsize)) == NULL)
 			err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
-	for (i = 0; i < bufsize / (int)sizeof(int); i++)
-		buf[i] = i;
-
-	for (i = 0; i < NB; i+= bufsize) {
-		if (write(fd, buf, bufsize) < 0) {
-			err(1, "write(%d), %s:%d", fd,
-					__FILE__, __LINE__);
-		}
-	}
+	memset(buf, 0, bufsize);
+
+	if (write(fd, buf, bufsize) < 0)
+		err(1, "write(%d), %s:%d", fd, __FILE__, __LINE__);
 	close(fd);
 	free(buf);
-	return;
 }
 
 int
@@ -134,6 +115,7 @@ setup(int nb)
 	if (!freespace)
 		_exit(0);
 	bufsize = 2 << random_int(2, 12);
+
 	return (0);
 }
 
@@ -179,5 +161,6 @@ test(void)
 		err(1, "fork(), %s:%d",  __FILE__, __LINE__);
 
 	unlink(path);
+
 	return (0);
 }


More information about the dev-commits-src-main mailing list