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 #include <sys/uio.h>
17 #include <strings.h>
18 #include <limits.h>
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <errno.h>
23 #include <err.h>
24
25 #define ONE_GIG ((off_t)1024 * 1024 * 1024)
26
27 #define DATA_LEN (sizeof ("data"))
28
29 /*
30 * Some simple testing of the read/writev() family: specifically we're checking
31 * IOV_MAX == 1024, and that a large-file compiled 32-bit binary can correctly
32 * access certain offsets.
33 */
34
35 int
36 main(int argc, char *argv[])
37 {
38 char path[] = "/var/tmp/writev_test.XXXXXX";
39 char data[(IOV_MAX + 1) * DATA_LEN] = "";
40 struct iovec iov[IOV_MAX + 1];
41
42 if (IOV_MAX < 1024)
43 errx(EXIT_FAILURE, "IOV_MAX != 1024");
44
45 int fd = mkstemp(path);
46
47 if (fd == -1)
48 err(EXIT_FAILURE, "failed to create file");
49
50 int ret = ftruncate(fd, ONE_GIG * 8);
51
52 if (ret != 0)
53 err(EXIT_FAILURE, "failed to truncate file");
54
55 for (int i = 0; i < IOV_MAX + 1; i++) {
56 (void) strcpy(data + i * DATA_LEN, "data");
57 iov[i].iov_base = data + i * 5;
58 iov[i].iov_len = DATA_LEN;
59 }
60
61 ssize_t written = writev(fd, iov, IOV_MAX + 1);
62
63 if (written != -1 || errno != EINVAL)
64 errx(EXIT_FAILURE, "writev(IOV_MAX + 1) didn't fail properly");
65
66 written = writev(fd, iov, IOV_MAX);
67
68 if (written == -1)
69 err(EXIT_FAILURE, "writev failed");
70
71 bzero(data, sizeof (data));
72
73 ssize_t read = preadv(fd, iov, IOV_MAX, 0);
74
75 if (read != DATA_LEN * IOV_MAX)
76 err(EXIT_FAILURE, "preadv failed");
77
78 for (int i = 0; i < IOV_MAX; i++) {
79 if (strcmp(data + i * DATA_LEN, "data") != 0)
80 errx(EXIT_FAILURE, "bad read at 0x%lx", i * DATA_LEN);
81 }
82
83 /*
84 * Now test various "interesting" offsets.
85 */
86
87 for (off_t off = 0; off < ONE_GIG * 8; off += ONE_GIG) {
88 if ((written = pwritev(fd, iov, 1, off)) != DATA_LEN)
89 err(EXIT_FAILURE, "pwritev(0x%lx) failed", off);
90 }
91
92 for (off_t off = 0; off < ONE_GIG * 8; off += ONE_GIG) {
93 if ((read = preadv(fd, iov, 1, off)) != DATA_LEN)
94 err(EXIT_FAILURE, "preadv(0x%lx) failed", off);
95 if (strcmp(data, "data") != 0)
96 errx(EXIT_FAILURE, "bad read at 0x%lx", off);
97 }
98
99 (void) unlink(path);
100
101 return (EXIT_SUCCESS);
102 }