#define _XOPEN_SOURCE 600 #include #include #include #include #include #include #include #include #include #include int main(void) { char tmpfname[256]; long page_size; void *data; char *pa; size_t len; int fd, i, fail = 0; page_size = sysconf(_SC_PAGE_SIZE); snprintf(tmpfname, sizeof(tmpfname), "/tmp/test"); /* Create file */ unlink(tmpfname); fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR); if (fd == -1) { printf("Error at open(): %s\n", strerror(errno)); return 1; } /* Fill it to the size of the page with 'a' */ data = malloc(page_size); memset(data, 'a', page_size); if (write(fd, data, page_size) != page_size) { printf("Error at write(): %s\n", strerror(errno)); return 1; } free(data); /* mmap half of the page */ pa = mmap(NULL, page_size/2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (pa == MAP_FAILED) { printf("Error at mmap(): %s\n", strerror(errno)); return 1; } for (i = 0; i < page_size; i++) { if (i > page_size/2 && pa[i] != 0) fail++; printf("%4i %2x\n", i, pa[i]); } close(fd); munmap(pa, len); if (fail) printf("FAILED: Page not zeroed\n"); else printf("SUCCEDED\n"); return 0; }