• 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 munlock() 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 = munlock(notpage_ptr, page_size - 1);
43 
44 	if (result == 0) {
45 		printf
46 		    ("munlock() 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 		    ("munlock() requires that addr be a multiple of {PAGESIZE}.\nTest PASSED\n");
51 		return PTS_PASS;
52 	} else if (result != -1) {
53 		printf("munlock() returns a value of %i instead of 0 or 1.\n",
54 		       result);
55 		perror("munlock");
56 		return PTS_FAIL;
57 	}
58 	perror("Unexpected error");
59 	return PTS_UNRESOLVED;
60 }
61