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 unsigned int PAGE_SIZE = sysconf(_SC_PAGESIZE);
0050 void *p;
0051 int fd;
0052 unsigned long a, b;
0053
0054 fd = open("/dev/zero", O_RDONLY);
0055 if (fd == -1)
0056 return 1;
0057
0058 p = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_PRIVATE|MAP_FILE, fd, 0);
0059 if (p == MAP_FAILED)
0060 return 1;
0061
0062 a = (unsigned long)p;
0063 b = (unsigned long)p + PAGE_SIZE;
0064
0065 pass("/proc/self/map_files/%lx-%lx", a, b);
0066 fail("/proc/self/map_files/ %lx-%lx", a, b);
0067 fail("/proc/self/map_files/%lx -%lx", a, b);
0068 fail("/proc/self/map_files/%lx- %lx", a, b);
0069 fail("/proc/self/map_files/%lx-%lx ", a, b);
0070 fail("/proc/self/map_files/0%lx-%lx", a, b);
0071 fail("/proc/self/map_files/%lx-0%lx", a, b);
0072 if (sizeof(long) == 4) {
0073 fail("/proc/self/map_files/100000000%lx-%lx", a, b);
0074 fail("/proc/self/map_files/%lx-100000000%lx", a, b);
0075 } else if (sizeof(long) == 8) {
0076 fail("/proc/self/map_files/10000000000000000%lx-%lx", a, b);
0077 fail("/proc/self/map_files/%lx-10000000000000000%lx", a, b);
0078 } else
0079 return 1;
0080
0081 return 0;
0082 }