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