1 /* 2 * This program is free software; you can redistribute it and/or modify 3 * it under the terms of the GNU General Public License version 2. 4 * 5 * This program is distributed in the hope that it will be useful, 6 * but WITHOUT ANY WARRANTY; without even the implied warranty of 7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 8 * GNU General Public License for more details. 9 * 10 * Test that mlockall lock the shared memory pages currently mapped into the 11 * address space of the process when MCL_CURRENT is set. 12 * 13 * This test use msync to check that the page is locked. 14 */ 15 16 /* ftruncate was formerly an XOPEN extension. We define _XOPEN_SOURCE here to 17 avoid warning if the implementation does not program ftruncate as a base 18 interface */ 19 #define _XOPEN_SOURCE 600 20 21 #include <sys/mman.h> 22 #include <sys/stat.h> 23 #include <fcntl.h> 24 #include <stdio.h> 25 #include <unistd.h> 26 #include <errno.h> 27 #include "posixtest.h" 28 29 #define BUF_SIZE 8 30 #define SHM_NAME "/posixtest_3-6" 31 32 int main(void) 33 { 34 void *page_ptr; 35 size_t page_size; 36 int result, fd; 37 void *foo; 38 39 page_size = sysconf(_SC_PAGESIZE); 40 if (errno) { 41 perror("An error occurs when calling sysconf()"); 42 return PTS_UNRESOLVED; 43 } 44 45 fd = shm_open(SHM_NAME, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); 46 if (fd == -1) { 47 perror("An error occurs when calling shm_open()"); 48 return PTS_UNRESOLVED; 49 } 50 51 if (ftruncate(fd, BUF_SIZE) != 0) { 52 perror("An error occurs when calling ftruncate()"); 53 shm_unlink(SHM_NAME); 54 return PTS_UNRESOLVED; 55 } 56 57 foo = mmap(NULL, BUF_SIZE, PROT_WRITE, MAP_SHARED, fd, 0); 58 if (foo == MAP_FAILED) { 59 perror("An error occurs when calling mmap()"); 60 shm_unlink(SHM_NAME); 61 return PTS_UNRESOLVED; 62 } 63 64 if (mlockall(MCL_CURRENT) == -1) { 65 if (errno == EPERM) { 66 printf 67 ("You don't have permission to lock your address space.\nTry to rerun this test as root.\n"); 68 } else { 69 perror("An error occurs when calling mlockall()"); 70 } 71 return PTS_UNRESOLVED; 72 } 73 74 page_ptr = (void *)((long)foo - ((long)foo % page_size)); 75 76 result = msync(page_ptr, page_size, MS_SYNC | MS_INVALIDATE); 77 if (result == -1 && errno == EBUSY) { 78 printf("Test PASSED\n"); 79 return PTS_PASS; 80 } else if (result == 0) { 81 printf 82 ("The shared memory pages of the process are not locked.\n"); 83 return PTS_FAIL; 84 } 85 perror("Unexpected error"); 86 return PTS_UNRESOLVED; 87 } 88