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.
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.
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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * --------------------------------------------------------------
29 * BugId 5047993 : Getting bad read data.
31 * Usage: readmmap <filename>
34 * filename is an absolute path to the file name.
39 * --------------------------------------------------------------
50 main(int argc, char **argv)
52 char *filename = "badfile";
57 int fd = -1, bytes, retval = 0;
60 if (argc < 2 || optind == argc) {
61 (void) fprintf(stderr,
62 "usage: %s <file name>\n", argv[0]);
66 if ((buf = calloc(1, size)) == NULL) {
71 filename = argv[optind];
73 (void) remove(filename);
75 fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
77 perror("open to create");
82 bytes = write(fd, buf, size);
84 (void) printf("short write: %d != %zd\n", bytes, size);
89 map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
90 if (map == MAP_FAILED) {
98 idx = random() % size;
101 if (msync(map, size, MS_SYNC) != 0) {
107 if (munmap(map, size) != 0) {
113 bytes = pread(fd, buf, size, 0);
115 (void) printf("short read: %d != %zd\n", bytes, size);
122 "bad data from read! got buf[%zd]=%d, expected 1\n",
128 (void) printf("good data from read: buf[%zd]=1\n", idx);