1163953Srrs/*
2167598Srrs *  This program is free software; you can redistribute it and/or modify
3163953Srrs *  it under the terms of the GNU General Public License version 2.
4163953Srrs *
5163953Srrs *  This program is distributed in the hope that it will be useful,
6163953Srrs *  but WITHOUT ANY WARRANTY; without even the implied warranty of
7163953Srrs *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8163953Srrs *  GNU General Public License for more details.
9163953Srrs *
10163953Srrs * Test that munlock() set errno = EINVAL when addr is not a multiple of
11163953Srrs * {PAGESIZE} if the implementation requires that addr be a multiple of
12163953Srrs * {PAGESIZE}.
13163953Srrs */
14163953Srrs
15163953Srrs#include <sys/mman.h>
16163953Srrs#include <stdio.h>
17163953Srrs#include <unistd.h>
18163953Srrs#include <stdlib.h>
19163953Srrs#include <errno.h>
20163953Srrs#include "posixtest.h"
21163953Srrs
22163953Srrsint main() {
23163953Srrs        int result;
24163953Srrs        long page_size;
25163953Srrs        void *ptr, *notpage_ptr;
26163953Srrs
27163953Srrs        page_size = sysconf(_SC_PAGESIZE);
28163953Srrs        if(errno) {
29163953Srrs                perror("An error occurs when calling sysconf()");
30163953Srrs                return PTS_UNRESOLVED;
31163953Srrs        }
32163953Srrs
33163953Srrs	ptr = malloc(page_size);
34163953Srrs	if(ptr == NULL) {
35163953Srrs                printf("Can not allocate memory.\n");
36163953Srrs                return PTS_UNRESOLVED;
37163953Srrs        }
38163953Srrs
39167598Srrs	notpage_ptr = ((long)ptr % page_size) ? ptr : ptr+1;
40163953Srrs
41163953Srrs	result = munlock(notpage_ptr, page_size - 1);
42163953Srrs
43163953Srrs	if(result == 0){
44163953Srrs		printf("munlock() does not require that addr be a multiple of {PAGESIZE}.\nTest PASSED\n");
45163953Srrs		return PTS_PASS;
46163953Srrs	} else if(result == -1 && errno == EINVAL){
47163953Srrs		printf("munlock() requires that addr be a multiple of {PAGESIZE}.\nTest PASSED\n");
48163953Srrs		return PTS_PASS;
49163953Srrs	} else if(result != -1){
50163953Srrs		printf("munlock() returns a value of %i instead of 0 or 1.\n",
51163953Srrs		       result);
52163953Srrs		perror("munlock");
53163953Srrs		return PTS_FAIL;
54163953Srrs	}
55163953Srrs	perror("Unexpected error");
56163953Srrs	return PTS_UNRESOLVED;
57163953Srrs}
58163953Srrs