Updated French and Turkish translation by gagar
[amule.git] / cmake / mmap-test.cpp
blob78d7bce6c52ef9e1502c826ed0d6188f1d3053a5
1 #include <sys/stat.h>
2 #include <stdlib.h>
3 #include <stddef.h>
4 #include <unistd.h>
6 /* malloc might have been renamed as rpl_malloc. */
7 #undef malloc
9 /* Thanks to Mike Haertel and Jim Avera for this test.
10 Here is a matrix of mmap possibilities:
11 mmap private not fixed
12 mmap private fixed at somewhere currently unmapped
13 mmap private fixed at somewhere already mapped
14 mmap shared not fixed
15 mmap shared fixed at somewhere currently unmapped
16 mmap shared fixed at somewhere already mapped
17 For private mappings, we should verify that changes cannot be read()
18 back from the file, nor mmap's back from the file at a different
19 address. (There have been systems where private was not correctly
20 implemented like the infamous i386 svr4.0, and systems where the
21 VM page cache was not coherent with the file system buffer cache
22 like early versions of FreeBSD and possibly contemporary NetBSD.)
23 For shared mappings, we should conversely verify that changes get
24 propagated back to all the places they're supposed to be.
26 Grep wants private fixed already mapped.
27 The main things grep needs to know about mmap are:
28 * does it exist and is it safe to write into the mmap'd area
29 * how to use it (BSD variants) */
31 #include <fcntl.h>
32 #include <sys/mman.h>
34 #if !defined STDC_HEADERS && !defined HAVE_STDLIB_H
35 char *malloc ();
36 #endif
38 /* This mess was copied from the GNU getpagesize.h. */
39 #ifndef HAVE_GETPAGESIZE
40 # ifdef _SC_PAGESIZE
41 # define getpagesize() sysconf(_SC_PAGESIZE)
42 # else /* no _SC_PAGESIZE */
43 # ifdef HAVE_SYS_PARAM_H
44 # include <sys/param.h>
45 # ifdef EXEC_PAGESIZE
46 # define getpagesize() EXEC_PAGESIZE
47 # else /* no EXEC_PAGESIZE */
48 # ifdef NBPG
49 # define getpagesize() NBPG * CLSIZE
50 # ifndef CLSIZE
51 # define CLSIZE 1
52 # endif /* no CLSIZE */
53 # else /* no NBPG */
54 # ifdef NBPC
55 # define getpagesize() NBPC
56 # else /* no NBPC */
57 # ifdef PAGESIZE
58 # define getpagesize() PAGESIZE
59 # endif /* PAGESIZE */
60 # endif /* no NBPC */
61 # endif /* no NBPG */
62 # endif /* no EXEC_PAGESIZE */
63 # else /* no HAVE_SYS_PARAM_H */
64 # define getpagesize() 8192 /* punt totally */
65 # endif /* no HAVE_SYS_PARAM_H */
66 # endif /* no _SC_PAGESIZE */
68 #endif /* no HAVE_GETPAGESIZE */
70 int
71 main ()
73 char *data, *data2, *data3;
74 int i, pagesize;
75 int fd, fd2;
77 pagesize = getpagesize ();
79 /* First, make a file with some known garbage in it. */
80 data = (char *) malloc (pagesize);
81 if (!data)
82 return 1;
83 for (i = 0; i < pagesize; ++i)
84 *(data + i) = rand ();
85 umask (0);
86 fd = creat ("conftest.mmap", 0600);
87 if (fd < 0)
88 return 2;
89 if (write (fd, data, pagesize) != pagesize)
90 return 3;
91 close (fd);
93 /* Next, check that the tail of a page is zero-filled. File must have
94 non-zero length, otherwise we risk SIGBUS for entire page. */
95 fd2 = open ("conftest.txt", O_RDWR | O_CREAT | O_TRUNC, 0600);
96 if (fd2 < 0)
97 return 4;
98 data2 = (char *) "";
99 if (write (fd2, data2, 1) != 1)
100 return 5;
101 data2 = (char *) mmap (0, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd2, 0L);
102 if (data2 == MAP_FAILED)
103 return 6;
104 for (i = 0; i < pagesize; ++i)
105 if (*(data2 + i))
106 return 7;
107 close (fd2);
108 if (munmap (data2, pagesize))
109 return 8;
111 /* Next, try to mmap the file at a fixed address which already has
112 something else allocated at it. If we can, also make sure that
113 we see the same garbage. */
114 fd = open ("conftest.mmap", O_RDWR);
115 if (fd < 0)
116 return 9;
117 if (data2 != mmap (data2, pagesize, PROT_READ | PROT_WRITE,
118 MAP_PRIVATE | MAP_FIXED, fd, 0L))
119 return 10;
120 for (i = 0; i < pagesize; ++i)
121 if (*(data + i) != *(data2 + i))
122 return 11;
124 /* Finally, make sure that changes to the mapped area do not
125 percolate back to the file as seen by read(). (This is a bug on
126 some variants of i386 svr4.0.) */
127 for (i = 0; i < pagesize; ++i)
128 *(data2 + i) = *(data2 + i) + 1;
129 data3 = (char *) malloc (pagesize);
130 if (!data3)
131 return 12;
132 if (read (fd, data3, pagesize) != pagesize)
133 return 13;
134 for (i = 0; i < pagesize; ++i)
135 if (*(data + i) != *(data3 + i))
136 return 14;
137 close (fd);
138 return 0;