Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tests/sys/cddl/zfs/bin/readmmap.c
39537 views
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
* --------------------------------------------------------------
30
* BugId 5047993 : Getting bad read data.
31
*
32
* Usage: readmmap <filename>
33
*
34
* where:
35
* filename is an absolute path to the file name.
36
*
37
* Return values:
38
* 1 : error
39
* 0 : no errors
40
* --------------------------------------------------------------
41
*/
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include <time.h>
45
#include <unistd.h>
46
#include <fcntl.h>
47
#include <errno.h>
48
#include <sys/mman.h>
49
50
int
51
main(int argc, char **argv)
52
{
53
char *filename = "badfile";
54
size_t size = 4395;
55
size_t idx = 0;
56
char *buf = NULL;
57
char *map = NULL;
58
int fd = -1, bytes, retval = 0;
59
unsigned seed;
60
61
if (argc < 2 || optind == argc) {
62
(void) fprintf(stderr,
63
"usage: %s <file name>\n", argv[0]);
64
exit(1);
65
}
66
67
if ((buf = calloc(1, size)) == NULL) {
68
perror("calloc");
69
exit(1);
70
}
71
72
filename = argv[optind];
73
74
(void) remove(filename);
75
76
fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
77
if (fd == -1) {
78
perror("open to create");
79
retval = 1;
80
goto end;
81
}
82
83
bytes = write(fd, buf, size);
84
if (bytes != size) {
85
(void) printf("short write: %d != %zu\n", bytes, size);
86
retval = 1;
87
goto end;
88
}
89
90
map = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
91
if (map == MAP_FAILED) {
92
perror("mmap");
93
retval = 1;
94
goto end;
95
}
96
seed = time(NULL);
97
srandom(seed);
98
99
idx = random() % size;
100
map[idx] = 1;
101
102
if (msync(map, size, MS_SYNC) != 0) {
103
perror("msync");
104
retval = 1;
105
goto end;
106
}
107
108
if (munmap(map, size) != 0) {
109
perror("munmap");
110
retval = 1;
111
goto end;
112
}
113
114
bytes = pread(fd, buf, size, 0);
115
if (bytes != size) {
116
(void) printf("short read: %d != %zu\n", bytes, size);
117
retval = 1;
118
goto end;
119
}
120
121
if (buf[idx] != 1) {
122
(void) printf(
123
"bad data from read! got buf[%zu]=%d, expected 1\n",
124
idx, buf[idx]);
125
retval = 1;
126
goto end;
127
}
128
129
(void) printf("good data from read: buf[%zu]=1\n", idx);
130
end:
131
if (fd != -1) {
132
(void) close(fd);
133
}
134
if (buf != NULL) {
135
free(buf);
136
}
137
138
return (retval);
139
}
140
141