xref: /freebsd/tests/sys/cddl/zfs/bin/readmmap.c (revision 47be48457393ba6747cc3144272b1f4d8b9077fb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  * $FreeBSD$
21  */
22 
23 /*
24  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"@(#)readmmap.c	1.4	07/05/25 SMI"
29 
30 /*
31  * --------------------------------------------------------------
32  *	BugId 5047993 : Getting bad read data.
33  *
34  *	Usage: readmmap <filename>
35  *
36  *	where:
37  *		filename is an absolute path to the file name.
38  *
39  *	Return values:
40  *		1 : error
41  *		0 : no errors
42  * --------------------------------------------------------------
43  */
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <time.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49 #include <errno.h>
50 #include <sys/mman.h>
51 
52 int
53 main(int argc, char **argv)
54 {
55 	char *filename = "badfile";
56 	size_t size = 4395;
57 	size_t idx = 0;
58 	char *buf = NULL;
59 	char *map = NULL;
60 	int fd = -1, bytes, retval = 0;
61 	unsigned seed;
62 
63 	if (argc < 2 || optind == argc) {
64 		(void) fprintf(stderr,
65 		    "usage: %s <file name>\n", argv[0]);
66 		exit(1);
67 	}
68 
69 	if ((buf = calloc(1, size)) == NULL) {
70 		perror("calloc");
71 		exit(1);
72 	}
73 
74 	filename = argv[optind];
75 
76 	(void) remove(filename);
77 
78 	fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
79 	if (fd == -1) {
80 		perror("open to create");
81 		retval = 1;
82 		goto end;
83 	}
84 
85 	bytes = write(fd, buf, size);
86 	if (bytes != size) {
87 		(void) printf("short write: %d != %zu\n", bytes, size);
88 		retval = 1;
89 		goto end;
90 	}
91 
92 	map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
93 	if (map == MAP_FAILED) {
94 		perror("mmap");
95 		retval = 1;
96 		goto end;
97 	}
98 	seed = time(NULL);
99 	srandom(seed);
100 
101 	idx = random() % size;
102 	map[idx] = 1;
103 
104 	if (msync(map, size, MS_SYNC) != 0) {
105 		perror("msync");
106 		retval = 1;
107 		goto end;
108 	}
109 
110 	if (munmap(map, size) != 0) {
111 		perror("munmap");
112 		retval = 1;
113 		goto end;
114 	}
115 
116 	bytes = pread(fd, buf, size, 0);
117 	if (bytes != size) {
118 		(void) printf("short read: %d != %zu\n", bytes, size);
119 		retval = 1;
120 		goto end;
121 	}
122 
123 	if (buf[idx] != 1) {
124 		(void) printf(
125 		    "bad data from read!  got buf[%zu]=%d, expected 1\n",
126 		    idx, buf[idx]);
127 		retval = 1;
128 		goto end;
129 	}
130 
131 	(void) printf("good data from read: buf[%zu]=1\n", idx);
132 end:
133 	if (fd != -1) {
134 		(void) close(fd);
135 	}
136 	if (buf != NULL) {
137 		free(buf);
138 	}
139 
140 	return (retval);
141 }
142