Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/readmmap.c
48529 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* 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.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright 2007 Sun Microsystems, Inc. All rights reserved.24* Use is subject to license terms.25*/2627/*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 : error38* 0 : no errors39* --------------------------------------------------------------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#include <sys/types.h>48#include <time.h>4950int51main(int argc, char **argv)52{53const char *filename = "badfile";54size_t size = 4395;55size_t idx = 0;56char *buf = NULL;57char *map = NULL;58int fd = -1, bytes, retval = 0;59uint_t seed;6061if (argc < 2 || optind == argc) {62(void) fprintf(stderr,63"usage: %s <file name>\n", argv[0]);64exit(1);65}6667if ((buf = calloc(1, size)) == NULL) {68perror("calloc");69exit(1);70}7172filename = argv[optind];7374(void) remove(filename);7576fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);77if (fd == -1) {78perror("open to create");79retval = 1;80goto end;81}8283bytes = write(fd, buf, size);84if (bytes != size) {85(void) printf("short write: %d != %zd\n", bytes, size);86retval = 1;87goto end;88}8990map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);91if (map == MAP_FAILED) {92perror("mmap");93retval = 1;94goto end;95}96seed = (uint_t)time(NULL);97srandom(seed);9899idx = random() % size;100map[idx] = 1;101102if (msync(map, size, MS_SYNC) != 0) {103perror("msync");104retval = 1;105goto end;106}107108if (munmap(map, size) != 0) {109perror("munmap");110retval = 1;111goto end;112}113114bytes = pread(fd, buf, size, 0);115if (bytes != size) {116(void) printf("short read: %d != %zd\n", bytes, size);117retval = 1;118goto end;119}120121if (buf[idx] != 1) {122(void) printf(123"bad data from read! got buf[%zd]=%d, expected 1\n",124idx, buf[idx]);125retval = 1;126goto end;127}128129(void) printf("good data from read: buf[%zd]=1\n", idx);130end:131if (fd != -1) {132(void) close(fd);133}134if (buf != NULL) {135free(buf);136}137138return (retval);139}140141142