xref: /titanic_44/usr/src/test/zfs-tests/cmd/readmmap/readmmap.c (revision be468fc6d7266e97f1330ad9a761169925f5f3f1)
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  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * --------------------------------------------------------------
29  *	BugId 5047993 : Getting bad read data.
30  *
31  *	Usage: readmmap <filename>
32  *
33  *	where:
34  *		filename is an absolute path to the file name.
35  *
36  *	Return values:
37  *		1 : error
38  *		0 : no errors
39  * --------------------------------------------------------------
40  */
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <errno.h>
46 #include <sys/mman.h>
47 
48 int
49 main(int argc, char **argv)
50 {
51 	char *filename = "badfile";
52 	size_t size = 4395;
53 	size_t idx = 0;
54 	char *buf = NULL;
55 	char *map = NULL;
56 	int fd = -1, bytes, retval = 0;
57 	unsigned seed;
58 
59 	if (argc < 2 || optind == argc) {
60 		(void) fprintf(stderr,
61 		    "usage: %s <file name>\n", argv[0]);
62 		exit(1);
63 	}
64 
65 	if ((buf = calloc(1, size)) == NULL) {
66 		perror("calloc");
67 		exit(1);
68 	}
69 
70 	filename = argv[optind];
71 
72 	(void) remove(filename);
73 
74 	fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
75 	if (fd == -1) {
76 		perror("open to create");
77 		retval = 1;
78 		goto end;
79 	}
80 
81 	bytes = write(fd, buf, size);
82 	if (bytes != size) {
83 		(void) printf("short write: %d != %ud\n", bytes, size);
84 		retval = 1;
85 		goto end;
86 	}
87 
88 	map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
89 	if (map == MAP_FAILED) {
90 		perror("mmap");
91 		retval = 1;
92 		goto end;
93 	}
94 	seed = time(NULL);
95 	srandom(seed);
96 
97 	idx = random() % size;
98 	map[idx] = 1;
99 
100 	if (msync(map, size, MS_SYNC) != 0) {
101 		perror("msync");
102 		retval = 1;
103 		goto end;
104 	}
105 
106 	if (munmap(map, size) != 0) {
107 		perror("munmap");
108 		retval = 1;
109 		goto end;
110 	}
111 
112 	bytes = pread(fd, buf, size, 0);
113 	if (bytes != size) {
114 		(void) printf("short read: %d != %ud\n", bytes, size);
115 		retval = 1;
116 		goto end;
117 	}
118 
119 	if (buf[idx] != 1) {
120 		(void) printf(
121 		    "bad data from read!  got buf[%ud]=%d, expected 1\n",
122 		    idx, buf[idx]);
123 		retval = 1;
124 		goto end;
125 	}
126 
127 	(void) printf("good data from read: buf[%ud]=1\n", idx);
128 end:
129 	if (fd != -1) {
130 		(void) close(fd);
131 	}
132 	if (buf != NULL) {
133 		free(buf);
134 	}
135 
136 	return (retval);
137 }
138