xref: /illumos-gate/usr/src/test/os-tests/tests/writev.c (revision 1d0ec46fafb49266ae79840a692bb48af60ade70)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2020 Joyent, Inc.
14  */
15 
16 /*
17  * Some simple testing of the read/writev() family: specifically we're checking
18  * IOV_MAX == 1024, and that a large-file compiled 32-bit binary can correctly
19  * access certain offsets.
20  */
21 
22 #include <sys/uio.h>
23 #include <strings.h>
24 #include <limits.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <err.h>
30 
31 #define	ONE_GIG ((off_t)1024 * 1024 * 1024)
32 
33 #define	DATA_LEN (sizeof ("data"))
34 
35 char path[] = "/var/tmp/writev_test.XXXXXX";
36 
37 static void
38 cleanup(void)
39 {
40 	(void) unlink(path);
41 }
42 
43 int
44 main(int argc, char *argv[])
45 {
46 	char data[(IOV_MAX + 1) * DATA_LEN] = "";
47 	struct iovec iov[IOV_MAX + 1];
48 
49 	if (IOV_MAX != 1024)
50 		errx(EXIT_FAILURE, "IOV_MAX != 1024");
51 
52 	int fd = mkstemp(path);
53 
54 	if (fd == -1)
55 		err(EXIT_FAILURE, "failed to create file");
56 
57 	(void) atexit(cleanup);
58 
59 	int ret = ftruncate(fd, ONE_GIG * 8);
60 
61 	if (ret != 0)
62 		err(EXIT_FAILURE, "failed to truncate file");
63 
64 	for (int i = 0; i < IOV_MAX + 1; i++) {
65 		(void) strcpy(data + i * DATA_LEN, "data");
66 		iov[i].iov_base = data + i * 5;
67 		iov[i].iov_len = DATA_LEN;
68 	}
69 
70 	ssize_t written = writev(fd, iov, IOV_MAX + 1);
71 
72 	if (written != -1 || errno != EINVAL)
73 		errx(EXIT_FAILURE, "writev(IOV_MAX + 1) didn't fail properly");
74 
75 	written = writev(fd, iov, IOV_MAX);
76 
77 	if (written == -1)
78 		err(EXIT_FAILURE, "writev failed");
79 
80 	bzero(data, sizeof (data));
81 
82 	ssize_t read = preadv(fd, iov, IOV_MAX, 0);
83 
84 	if (read != DATA_LEN * IOV_MAX)
85 		err(EXIT_FAILURE, "preadv failed");
86 
87 	for (int i = 0; i < IOV_MAX; i++) {
88 		if (strcmp(data + i * DATA_LEN, "data") != 0)
89 			errx(EXIT_FAILURE, "bad read at 0x%zx", i * DATA_LEN);
90 	}
91 
92 	/*
93 	 * Now test various "interesting" offsets.
94 	 */
95 
96 	for (off_t off = 0; off < ONE_GIG * 8; off += ONE_GIG) {
97 		if ((written = pwritev(fd, iov, 1, off)) != DATA_LEN)
98 			err(EXIT_FAILURE, "pwritev(0x%" PRIx64 ") failed", off);
99 	}
100 
101 	for (off_t off = 0; off < ONE_GIG * 8; off += ONE_GIG) {
102 		if ((read = preadv(fd, iov, 1, off)) != DATA_LEN)
103 			err(EXIT_FAILURE, "preadv(0x%" PRIx64 ") failed", off);
104 		if (strcmp(data, "data") != 0)
105 			errx(EXIT_FAILURE, "bad read at 0x%" PRIx64, off);
106 	}
107 
108 	return (EXIT_SUCCESS);
109 }
110