git: d19f34821d66 - main - stress2: Add a helper tool

From: Peter Holm <pho_at_FreeBSD.org>
Date: Sat, 24 Feb 2024 06:26:43 UTC
The branch main has been updated by pho:

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

commit d19f34821d661468486bdf4cacc9be795ac58780
Author:     Peter Holm <pho@FreeBSD.org>
AuthorDate: 2024-02-24 06:26:16 +0000
Commit:     Peter Holm <pho@FreeBSD.org>
CommitDate: 2024-02-24 06:26:16 +0000

    stress2: Add a helper tool
---
 tools/test/stress2/tools/serial.c | 44 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/tools/test/stress2/tools/serial.c b/tools/test/stress2/tools/serial.c
new file mode 100644
index 000000000000..8dcf9c5da3a8
--- /dev/null
+++ b/tools/test/stress2/tools/serial.c
@@ -0,0 +1,44 @@
+/* Fill a file with a sequence of byte values from 0 - 0xff */
+
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(int argc, char *argv[])
+{
+	size_t i, size;
+	int fd;
+	char *cp, *file;
+
+	if (argc != 3) {
+		fprintf(stderr, "Usage: %s <file> <file length in bytes>\n", argv[0]);
+		exit(1);
+	}
+	file = argv[1];
+	size = atol(argv[2]);
+
+	if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
+		err(1, "%s", file);
+
+	if (lseek(fd, size - 1, SEEK_SET) == -1)
+		err(1, "lseek error");
+
+	/* write a dummy byte at the last location */
+	if (write(fd, "\0", 1) != 1)
+		err(1, "write error");
+
+	if ((cp = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
+		err(1, "mmap()");
+
+	for (i = 0; i < size; i++)
+		cp[i] = i & 0xff;
+
+	if (munmap(cp, size) == -1)
+		err(1, "munmap");
+	close(fd);
+}