0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <errno.h>
0018 #include <sys/types.h>
0019 #include <sys/stat.h>
0020 #include <fcntl.h>
0021 #include <stdio.h>
0022 #include <unistd.h>
0023 #include <sys/mman.h>
0024 #include <stdlib.h>
0025
0026 static void pass(const char *fmt, unsigned long a, unsigned long b)
0027 {
0028 char name[64];
0029 char buf[64];
0030
0031 snprintf(name, sizeof(name), fmt, a, b);
0032 if (readlink(name, buf, sizeof(buf)) == -1)
0033 exit(1);
0034 }
0035
0036 static void fail(const char *fmt, unsigned long a, unsigned long b)
0037 {
0038 char name[64];
0039 char buf[64];
0040
0041 snprintf(name, sizeof(name), fmt, a, b);
0042 if (readlink(name, buf, sizeof(buf)) == -1 && errno == ENOENT)
0043 return;
0044 exit(1);
0045 }
0046
0047 int main(void)
0048 {
0049 const int PAGE_SIZE = sysconf(_SC_PAGESIZE);
0050
0051
0052
0053
0054 const unsigned long va_max = 1UL << 20;
0055 unsigned long va;
0056 void *p;
0057 int fd;
0058 unsigned long a, b;
0059
0060 fd = open("/dev/zero", O_RDONLY);
0061 if (fd == -1)
0062 return 1;
0063
0064 for (va = 0; va < va_max; va += PAGE_SIZE) {
0065 p = mmap((void *)va, PAGE_SIZE, PROT_NONE, MAP_PRIVATE|MAP_FILE|MAP_FIXED, fd, 0);
0066 if (p == (void *)va)
0067 break;
0068 }
0069 if (va == va_max) {
0070 fprintf(stderr, "error: mmap doesn't like you\n");
0071 return 1;
0072 }
0073
0074 a = (unsigned long)p;
0075 b = (unsigned long)p + PAGE_SIZE;
0076
0077 pass("/proc/self/map_files/%lx-%lx", a, b);
0078 fail("/proc/self/map_files/ %lx-%lx", a, b);
0079 fail("/proc/self/map_files/%lx -%lx", a, b);
0080 fail("/proc/self/map_files/%lx- %lx", a, b);
0081 fail("/proc/self/map_files/%lx-%lx ", a, b);
0082 fail("/proc/self/map_files/0%lx-%lx", a, b);
0083 fail("/proc/self/map_files/%lx-0%lx", a, b);
0084 if (sizeof(long) == 4) {
0085 fail("/proc/self/map_files/100000000%lx-%lx", a, b);
0086 fail("/proc/self/map_files/%lx-100000000%lx", a, b);
0087 } else if (sizeof(long) == 8) {
0088 fail("/proc/self/map_files/10000000000000000%lx-%lx", a, b);
0089 fail("/proc/self/map_files/%lx-10000000000000000%lx", a, b);
0090 } else
0091 return 1;
0092
0093 return 0;
0094 }