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
22int main() {
23        int result;
24        long page_size;
25        void *ptr, *notpage_ptr;
26
27        page_size = sysconf(_SC_PAGESIZE);
28        if(errno) {
29                perror("An error occurs when calling sysconf()");
30                return PTS_UNRESOLVED;
31        }
32
33	ptr = malloc(page_size);
34	if(ptr == NULL) {
35                printf("Can not allocate memory.\n");
36                return PTS_UNRESOLVED;
37        }
38
39	notpage_ptr = ((long)ptr % page_size) ? ptr : ptr+1;
40
41	result = mlock(notpage_ptr, page_size - 1);
42
43	if(result == 0){
44		printf("mlock() does not require that addr be a multiple of {PAGESIZE}.\nTest PASSED\n");
45		return PTS_PASS;
46	} else if(result == -1 && errno == EINVAL){
47		printf("mlock() requires that addr be a multiple of {PAGESIZE}.\nTest PASSED\n");
48		return PTS_PASS;
49	} else if(errno == EPERM) {
50		printf("You don't have permission to lock your address space.\nTry to rerun this test as root.\n");
51		return PTS_UNRESOLVED;
52	} else if(result != -1){
53		printf("mlock() returns a value of %i instead of 0 or 1.\n",
54		       result);
55		perror("mlock");
56		return PTS_FAIL;
57	}
58	perror("Unexpected error");
59	return PTS_UNRESOLVED;
60}
61