12fae26bdSAlan Somers /* 22fae26bdSAlan Somers * CDDL HEADER START 32fae26bdSAlan Somers * 42fae26bdSAlan Somers * The contents of this file are subject to the terms of the 52fae26bdSAlan Somers * Common Development and Distribution License (the "License"). 62fae26bdSAlan Somers * You may not use this file except in compliance with the License. 72fae26bdSAlan Somers * 82fae26bdSAlan Somers * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 92fae26bdSAlan Somers * or http://www.opensolaris.org/os/licensing. 102fae26bdSAlan Somers * See the License for the specific language governing permissions 112fae26bdSAlan Somers * and limitations under the License. 122fae26bdSAlan Somers * 132fae26bdSAlan Somers * When distributing Covered Code, include this CDDL HEADER in each 142fae26bdSAlan Somers * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 152fae26bdSAlan Somers * If applicable, add the following below this CDDL HEADER, with the 162fae26bdSAlan Somers * fields enclosed by brackets "[]" replaced with your own identifying 172fae26bdSAlan Somers * information: Portions Copyright [yyyy] [name of copyright owner] 182fae26bdSAlan Somers * 192fae26bdSAlan Somers * CDDL HEADER END 202fae26bdSAlan Somers * $FreeBSD$ 212fae26bdSAlan Somers */ 222fae26bdSAlan Somers 232fae26bdSAlan Somers /* 242fae26bdSAlan Somers * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 252fae26bdSAlan Somers * Use is subject to license terms. 262fae26bdSAlan Somers */ 272fae26bdSAlan Somers 282fae26bdSAlan Somers #pragma ident "@(#)readmmap.c 1.4 07/05/25 SMI" 292fae26bdSAlan Somers 302fae26bdSAlan Somers /* 312fae26bdSAlan Somers * -------------------------------------------------------------- 322fae26bdSAlan Somers * BugId 5047993 : Getting bad read data. 332fae26bdSAlan Somers * 342fae26bdSAlan Somers * Usage: readmmap <filename> 352fae26bdSAlan Somers * 362fae26bdSAlan Somers * where: 372fae26bdSAlan Somers * filename is an absolute path to the file name. 382fae26bdSAlan Somers * 392fae26bdSAlan Somers * Return values: 402fae26bdSAlan Somers * 1 : error 412fae26bdSAlan Somers * 0 : no errors 422fae26bdSAlan Somers * -------------------------------------------------------------- 432fae26bdSAlan Somers */ 442fae26bdSAlan Somers #include <stdio.h> 452fae26bdSAlan Somers #include <stdlib.h> 46*47be4845SDimitry Andric #include <time.h> 472fae26bdSAlan Somers #include <unistd.h> 482fae26bdSAlan Somers #include <fcntl.h> 492fae26bdSAlan Somers #include <errno.h> 502fae26bdSAlan Somers #include <sys/mman.h> 512fae26bdSAlan Somers 522fae26bdSAlan Somers int 532fae26bdSAlan Somers main(int argc, char **argv) 542fae26bdSAlan Somers { 552fae26bdSAlan Somers char *filename = "badfile"; 562fae26bdSAlan Somers size_t size = 4395; 572fae26bdSAlan Somers size_t idx = 0; 582fae26bdSAlan Somers char *buf = NULL; 592fae26bdSAlan Somers char *map = NULL; 602fae26bdSAlan Somers int fd = -1, bytes, retval = 0; 612fae26bdSAlan Somers unsigned seed; 622fae26bdSAlan Somers 632fae26bdSAlan Somers if (argc < 2 || optind == argc) { 642fae26bdSAlan Somers (void) fprintf(stderr, 652fae26bdSAlan Somers "usage: %s <file name>\n", argv[0]); 662fae26bdSAlan Somers exit(1); 672fae26bdSAlan Somers } 682fae26bdSAlan Somers 692fae26bdSAlan Somers if ((buf = calloc(1, size)) == NULL) { 702fae26bdSAlan Somers perror("calloc"); 712fae26bdSAlan Somers exit(1); 722fae26bdSAlan Somers } 732fae26bdSAlan Somers 742fae26bdSAlan Somers filename = argv[optind]; 752fae26bdSAlan Somers 762fae26bdSAlan Somers (void) remove(filename); 772fae26bdSAlan Somers 782fae26bdSAlan Somers fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666); 792fae26bdSAlan Somers if (fd == -1) { 802fae26bdSAlan Somers perror("open to create"); 812fae26bdSAlan Somers retval = 1; 822fae26bdSAlan Somers goto end; 832fae26bdSAlan Somers } 842fae26bdSAlan Somers 852fae26bdSAlan Somers bytes = write(fd, buf, size); 862fae26bdSAlan Somers if (bytes != size) { 87f27fb06cSEd Maste (void) printf("short write: %d != %zu\n", bytes, size); 882fae26bdSAlan Somers retval = 1; 892fae26bdSAlan Somers goto end; 902fae26bdSAlan Somers } 912fae26bdSAlan Somers 922fae26bdSAlan Somers map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); 932fae26bdSAlan Somers if (map == MAP_FAILED) { 942fae26bdSAlan Somers perror("mmap"); 952fae26bdSAlan Somers retval = 1; 962fae26bdSAlan Somers goto end; 972fae26bdSAlan Somers } 982fae26bdSAlan Somers seed = time(NULL); 992fae26bdSAlan Somers srandom(seed); 1002fae26bdSAlan Somers 1012fae26bdSAlan Somers idx = random() % size; 1022fae26bdSAlan Somers map[idx] = 1; 1032fae26bdSAlan Somers 1042fae26bdSAlan Somers if (msync(map, size, MS_SYNC) != 0) { 1052fae26bdSAlan Somers perror("msync"); 1062fae26bdSAlan Somers retval = 1; 1072fae26bdSAlan Somers goto end; 1082fae26bdSAlan Somers } 1092fae26bdSAlan Somers 1102fae26bdSAlan Somers if (munmap(map, size) != 0) { 1112fae26bdSAlan Somers perror("munmap"); 1122fae26bdSAlan Somers retval = 1; 1132fae26bdSAlan Somers goto end; 1142fae26bdSAlan Somers } 1152fae26bdSAlan Somers 1162fae26bdSAlan Somers bytes = pread(fd, buf, size, 0); 1172fae26bdSAlan Somers if (bytes != size) { 118f27fb06cSEd Maste (void) printf("short read: %d != %zu\n", bytes, size); 1192fae26bdSAlan Somers retval = 1; 1202fae26bdSAlan Somers goto end; 1212fae26bdSAlan Somers } 1222fae26bdSAlan Somers 1232fae26bdSAlan Somers if (buf[idx] != 1) { 1242fae26bdSAlan Somers (void) printf( 125f27fb06cSEd Maste "bad data from read! got buf[%zu]=%d, expected 1\n", 1262fae26bdSAlan Somers idx, buf[idx]); 1272fae26bdSAlan Somers retval = 1; 1282fae26bdSAlan Somers goto end; 1292fae26bdSAlan Somers } 1302fae26bdSAlan Somers 131f27fb06cSEd Maste (void) printf("good data from read: buf[%zu]=1\n", idx); 1322fae26bdSAlan Somers end: 1332fae26bdSAlan Somers if (fd != -1) { 1342fae26bdSAlan Somers (void) close(fd); 1352fae26bdSAlan Somers } 1362fae26bdSAlan Somers if (buf != NULL) { 1372fae26bdSAlan Somers free(buf); 1382fae26bdSAlan Somers } 1392fae26bdSAlan Somers 1402fae26bdSAlan Somers return (retval); 1412fae26bdSAlan Somers } 142