18 size_t ps = sysconf (_SC_PAGESIZE);
21 /* Create a file and put some data in it. */
25 printf ("Cannot create temporary file: %m\n");
30 for (c = 0; c < sizeof (buf); ++c)
31 buf[c] = '0' + (c % 10);
33 for (c = 0; c < (ps * 4) / sizeof (buf); ++c)
34 if (fwrite (buf, 1, sizeof (buf), fp) != sizeof (buf))
36 printf ("`fwrite' failed: %m\n");
40 assert (ps + 1000 < c * sizeof (buf));
42 /* First try something which is not allowed: map at an offset which is
43 not modulo the pagesize. */
44 ptr = mmap (NULL, 1000, PROT_READ, MAP_SHARED, fd, ps - 1);
45 if (ptr != MAP_FAILED)
47 puts ("mapping at offset with mod pagesize != 0 succeeded!");
50 else if (errno != EINVAL && errno != ENOSYS)
52 puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
56 /* Try the same for mmap64. */
57 ptr = mmap64 (NULL, 1000, PROT_READ, MAP_SHARED, fd, ps - 1);
58 if (ptr != MAP_FAILED)
60 puts ("mapping at offset with mod pagesize != 0 succeeded!");
63 else if (errno != EINVAL && errno != ENOSYS)
65 puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
69 /* And the same for private mapping. */
70 ptr = mmap (NULL, 1000, PROT_READ, MAP_PRIVATE, fd, ps - 1);
71 if (ptr != MAP_FAILED)
73 puts ("mapping at offset with mod pagesize != 0 succeeded!");
76 else if (errno != EINVAL && errno != ENOSYS)
78 puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
82 /* Try the same for mmap64. */
83 ptr = mmap64 (NULL, 1000, PROT_READ, MAP_PRIVATE, fd, ps - 1);
84 if (ptr != MAP_FAILED)
86 puts ("mapping at offset with mod pagesize != 0 succeeded!");
89 else if (errno != EINVAL && errno != ENOSYS)
91 puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
95 /* Get a valid address. */
96 mem = malloc (2 * ps);
99 /* Now we map at an address which is not mod pagesize. */
100 ptr = mmap (mem + 1, 1000, PROT_READ, MAP_SHARED | MAP_FIXED, fd, ps);
101 if (ptr != MAP_FAILED)
103 puts ("mapping at address with mod pagesize != 0 succeeded!");
106 else if (errno != EINVAL && errno != ENOSYS)
108 puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
112 /* Try the same for mmap64. */
113 ptr = mmap64 (mem + 1, 1000, PROT_READ, MAP_SHARED | MAP_FIXED, fd, ps);
114 if (ptr != MAP_FAILED)
116 puts ("mapping at address with mod pagesize != 0 succeeded!");
119 else if (errno != EINVAL && errno != ENOSYS)
121 puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
125 /* And again for MAP_PRIVATE. */
126 ptr = mmap (mem + 1, 1000, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ps);
127 if (ptr != MAP_FAILED)
129 puts ("mapping at address with mod pagesize != 0 succeeded!");
132 else if (errno != EINVAL && errno != ENOSYS)
134 puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
138 /* Try the same for mmap64. */
139 ptr = mmap64 (mem + 1, 1000, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ps);
140 if (ptr != MAP_FAILED)
142 puts ("mapping at address with mod pagesize != 0 succeeded!");
145 else if (errno != EINVAL && errno != ENOSYS)
147 puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
154 /* Now map the memory and see whether the content of the mapped area
156 ptr = mmap (NULL, 1000, PROT_READ, MAP_SHARED, fd, ps);
157 if (ptr == MAP_FAILED)
161 printf ("cannot mmap file: %m\n");
167 for (c = ps; c < ps + 1000; ++c)
168 if (ptr[c - ps] != '0' + (c % 10))
170 printf ("wrong data mapped at offset %zd\n", c);
175 /* And for mmap64. */
176 ptr = mmap64 (NULL, 1000, PROT_READ, MAP_SHARED, fd, ps);
177 if (ptr == MAP_FAILED)
181 printf ("cannot mmap file: %m\n");
187 for (c = ps; c < ps + 1000; ++c)
188 if (ptr[c - ps] != '0' + (c % 10))
190 printf ("wrong data mapped at offset %zd\n", c);
199 #define TEST_FUNCTION do_test ()
200 #include "../test-skeleton.c"