• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 mlock() set errno = EINVAL when addr is not a multiple of
11  * {PAGESIZE} if the implementation requires that addr be a multiple of
12  * {PAGESIZE}.
13  */
14 
15 #include <sys/mman.h>
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <errno.h>
20 #include "posixtest.h"
21 
main(void)22 int main(void)
23 {
24 	int result;
25 	long page_size;
26 	void *ptr, *notpage_ptr;
27 
28 	page_size = sysconf(_SC_PAGESIZE);
29 	if (errno) {
30 		perror("An error occurs when calling sysconf()");
31 		return PTS_UNRESOLVED;
32 	}
33 
34 	ptr = malloc(page_size);
35 	if (ptr == NULL) {
36 		printf("Can not allocate memory.\n");
37 		return PTS_UNRESOLVED;
38 	}
39 
40 	notpage_ptr = ((long)ptr % page_size) ? ptr : ptr + 1;
41 
42 	result = mlock(notpage_ptr, page_size - 1);
43 
44 	if (result == 0) {
45 		printf
46 		    ("mlock() does not require that addr be a multiple of {PAGESIZE}.\nTest PASSED\n");
47 		return PTS_PASS;
48 	} else if (result == -1 && errno == EINVAL) {
49 		printf
50 		    ("mlock() requires that addr be a multiple of {PAGESIZE}.\nTest PASSED\n");
51 		return PTS_PASS;
52 	} else if (errno == EPERM) {
53 		printf
54 		    ("You don't have permission to lock your address space.\nTry to rerun this test as root.\n");
55 		return PTS_UNRESOLVED;
56 	} else if (result != -1) {
57 		printf("mlock() returns a value of %i instead of 0 or 1.\n",
58 		       result);
59 		perror("mlock");
60 		return PTS_FAIL;
61 	}
62 	perror("Unexpected error");
63 	return PTS_UNRESOLVED;
64 }
65