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 #include <sys/mman.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include "posixtest.h"
23
24 #define BUF_SIZE 8
25 #define SHM_NAME "/posixtest_3-6"
26
main(void)27 int main(void)
28 {
29 void *page_ptr;
30 size_t page_size;
31 int result, fd;
32 void *foo;
33
34 page_size = sysconf(_SC_PAGESIZE);
35 if (errno) {
36 perror("An error occurs when calling sysconf()");
37 return PTS_UNRESOLVED;
38 }
39
40 fd = shm_open(SHM_NAME, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
41 if (fd == -1) {
42 perror("An error occurs when calling shm_open()");
43 return PTS_UNRESOLVED;
44 }
45
46 if (ftruncate(fd, BUF_SIZE) != 0) {
47 perror("An error occurs when calling ftruncate()");
48 shm_unlink(SHM_NAME);
49 return PTS_UNRESOLVED;
50 }
51
52 foo = mmap(NULL, BUF_SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
53 if (foo == MAP_FAILED) {
54 perror("An error occurs when calling mmap()");
55 shm_unlink(SHM_NAME);
56 return PTS_UNRESOLVED;
57 }
58
59 if (mlockall(MCL_CURRENT) == -1) {
60 if (errno == EPERM) {
61 printf
62 ("You don't have permission to lock your address space.\nTry to rerun this test as root.\n");
63 } else {
64 perror("An error occurs when calling mlockall()");
65 }
66 return PTS_UNRESOLVED;
67 }
68
69 page_ptr = (void *)((long)foo - ((long)foo % page_size));
70
71 result = msync(page_ptr, page_size, MS_SYNC | MS_INVALIDATE);
72 if (result == -1 && errno == EBUSY) {
73 printf("Test PASSED\n");
74 return PTS_PASS;
75 } else if (result == 0) {
76 printf
77 ("The shared memory pages of the process are not locked.\n");
78 return PTS_FAIL;
79 }
80 perror("Unexpected error");
81 return PTS_UNRESOLVED;
82 }
83