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 mapped files 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#include <sys/mman.h>
16#include <stdio.h>
17#include <unistd.h>
18#include <errno.h>
19#include <fcntl.h>
20#include "posixtest.h"
21
22
23int main() {
24	void *page_ptr;
25	size_t page_size;
26	int result, fd;
27	void *foo;
28
29	page_size = sysconf(_SC_PAGESIZE);
30	if(errno) {
31		perror("An error occurs when calling sysconf()");
32		return PTS_UNRESOLVED;
33	}
34
35	fd = open("conformance/interfaces/mlockall/3-7.c", O_RDONLY);
36	if(fd == -1) {
37		perror("An error occurs when calling open()");
38		return PTS_UNRESOLVED;
39	}
40
41	foo = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
42		if(foo == MAP_FAILED) {
43		perror("An error occurs when calling mmap()");
44		return PTS_UNRESOLVED;
45	}
46
47	if(mlockall(MCL_CURRENT) == -1) {
48		if(errno == EPERM){
49			printf("You don't have permission to lock your address space.\nTry to rerun this test as root.\n");
50		} else {
51			perror("An error occurs when calling mlockall()");
52		}
53		return PTS_UNRESOLVED;
54	}
55
56	page_ptr = (void*) ( (long)foo - ((long)foo % page_size) );
57
58	result = msync(page_ptr, page_size, MS_SYNC|MS_INVALIDATE);
59	if(result == -1 && errno == EBUSY) {
60		printf("Test PASSED\n");
61		return PTS_PASS;
62	} else if(result == 0) {
63		printf("The mapped files pages of the process are not locked.\n");
64		return PTS_FAIL;
65	}
66	perror("Unexpected error");
67	return PTS_UNRESOLVED;
68}
69
70