]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/zfs-tests/cmd/readmmap/readmmap.c
Vendor import of openzfs master @ 184df27eef0abdc7ab2105b21257f753834b936b
[FreeBSD/FreeBSD.git] / tests / zfs-tests / cmd / readmmap / readmmap.c
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 #include <time.h>
48
49 int
50 main(int argc, char **argv)
51 {
52         char *filename = "badfile";
53         size_t size = 4395;
54         size_t idx = 0;
55         char *buf = NULL;
56         char *map = NULL;
57         int fd = -1, bytes, retval = 0;
58         unsigned seed;
59
60         if (argc < 2 || optind == argc) {
61                 (void) fprintf(stderr,
62                     "usage: %s <file name>\n", argv[0]);
63                 exit(1);
64         }
65
66         if ((buf = calloc(1, size)) == NULL) {
67                 perror("calloc");
68                 exit(1);
69         }
70
71         filename = argv[optind];
72
73         (void) remove(filename);
74
75         fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
76         if (fd == -1) {
77                 perror("open to create");
78                 retval = 1;
79                 goto end;
80         }
81
82         bytes = write(fd, buf, size);
83         if (bytes != size) {
84                 (void) printf("short write: %d != %zd\n", bytes, size);
85                 retval = 1;
86                 goto end;
87         }
88
89         map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
90         if (map == MAP_FAILED) {
91                 perror("mmap");
92                 retval = 1;
93                 goto end;
94         }
95         seed = time(NULL);
96         srandom(seed);
97
98         idx = random() % size;
99         map[idx] = 1;
100
101         if (msync(map, size, MS_SYNC) != 0) {
102                 perror("msync");
103                 retval = 1;
104                 goto end;
105         }
106
107         if (munmap(map, size) != 0) {
108                 perror("munmap");
109                 retval = 1;
110                 goto end;
111         }
112
113         bytes = pread(fd, buf, size, 0);
114         if (bytes != size) {
115                 (void) printf("short read: %d != %zd\n", bytes, size);
116                 retval = 1;
117                 goto end;
118         }
119
120         if (buf[idx] != 1) {
121                 (void) printf(
122                     "bad data from read!  got buf[%zd]=%d, expected 1\n",
123                     idx, buf[idx]);
124                 retval = 1;
125                 goto end;
126         }
127
128         (void) printf("good data from read: buf[%zd]=1\n", idx);
129 end:
130         if (fd != -1) {
131                 (void) close(fd);
132         }
133         if (buf != NULL) {
134                 free(buf);
135         }
136
137         return (retval);
138 }