Clean up __exit_thread.
[glibc.git] / posix / tst-mmap.c
blobc03acf5e16fe560a15c2d8058614643b1dd0e877
1 #include <assert.h>
2 #include <errno.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <sys/mman.h>
9 int
10 main (void)
12 int result = 0;
13 FILE *fp;
14 size_t c;
15 char buf[1000];
16 int fd;
17 unsigned char *ptr;
18 size_t ps = sysconf (_SC_PAGESIZE);
19 void *mem;
21 /* Create a file and put some data in it. */
22 fp = tmpfile ();
23 if (fp == NULL)
25 printf ("Cannot create temporary file: %m\n");
26 return 1;
28 fd = fileno (fp);
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");
37 return 1;
39 fflush (fp);
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!");
48 result = 1;
50 else if (errno != EINVAL && errno != ENOSYS)
52 puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
53 result = 1;
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!");
61 result = 1;
63 else if (errno != EINVAL && errno != ENOSYS)
65 puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
66 result = 1;
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!");
74 result = 1;
76 else if (errno != EINVAL && errno != ENOSYS)
78 puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
79 result = 1;
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!");
87 result = 1;
89 else if (errno != EINVAL && errno != ENOSYS)
91 puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
92 result = 1;
95 /* Get a valid address. */
96 mem = malloc (2 * ps);
97 if (mem != NULL)
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!");
104 result = 1;
106 else if (errno != EINVAL && errno != ENOSYS)
108 puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
109 result = 1;
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!");
117 result = 1;
119 else if (errno != EINVAL && errno != ENOSYS)
121 puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
122 result = 1;
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!");
130 result = 1;
132 else if (errno != EINVAL && errno != ENOSYS)
134 puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
135 result = 1;
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!");
143 result = 1;
145 else if (errno != EINVAL && errno != ENOSYS)
147 puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
148 result = 1;
151 free (mem);
154 /* Now map the memory and see whether the content of the mapped area
155 is correct. */
156 ptr = mmap (NULL, 1000, PROT_READ, MAP_SHARED, fd, ps);
157 if (ptr == MAP_FAILED)
159 if (errno != ENOSYS)
161 printf ("cannot mmap file: %m\n");
162 result = 1;
165 else
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);
171 result = 1;
175 /* And for mmap64. */
176 ptr = mmap64 (NULL, 1000, PROT_READ, MAP_SHARED, fd, ps);
177 if (ptr == MAP_FAILED)
179 if (errno != ENOSYS)
181 printf ("cannot mmap file: %m\n");
182 result = 1;
185 else
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);
191 result = 1;
195 /* That's it. */
196 return result;