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