Home | History | Annotate | Download | only in munmap
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * This file is licensed under the GPL license.  For the full content
      4  * of this license, see the COPYING file at the top level of this
      5  * source tree.
      6  *
      7  * The implementation shall require that addr be a multiple
      8  * of the page size {PAGESIZE}.
      9  *
     10  * Test step:
     11  * Try to call unmap, with addr NOT a multiple of page size.
     12  * Should get EINVAL.
     13  */
     14 
     15 #define _XOPEN_SOURCE 600
     16 
     17 #include <pthread.h>
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 #include <unistd.h>
     21 #include <sys/mman.h>
     22 #include <sys/types.h>
     23 #include <sys/stat.h>
     24 #include <sys/wait.h>
     25 #include <fcntl.h>
     26 #include <string.h>
     27 #include <errno.h>
     28 #include "posixtest.h"
     29 
     30 #define TNAME "munmap/3-1.c"
     31 
     32 int main(void)
     33 {
     34 	char tmpfname[256];
     35 	long file_size;
     36 
     37 	void *pa = NULL;
     38 	void *addr = NULL;
     39 	size_t len;
     40 	int flag;
     41 	int fd;
     42 	off_t off = 0;
     43 	int prot;
     44 
     45 	int page_size;
     46 
     47 	char *pa2;
     48 
     49 	page_size = sysconf(_SC_PAGE_SIZE);
     50 	file_size = 2 * page_size;
     51 
     52 	/* We hope to map 2 pages */
     53 	len = page_size + 1;
     54 
     55 	/* Create tmp file */
     56 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_munmap_1_1_%d",
     57 		 getpid());
     58 	unlink(tmpfname);
     59 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
     60 	if (fd == -1) {
     61 		printf(TNAME " Error at open(): %s\n", strerror(errno));
     62 		exit(PTS_UNRESOLVED);
     63 	}
     64 	unlink(tmpfname);
     65 
     66 	if (ftruncate(fd, file_size) == -1) {
     67 		printf("Error at ftruncate: %s\n", strerror(errno));
     68 		exit(PTS_UNRESOLVED);
     69 	}
     70 
     71 	flag = MAP_SHARED;
     72 	prot = PROT_READ | PROT_WRITE;
     73 	pa = mmap(addr, len, prot, flag, fd, off);
     74 	if (pa == MAP_FAILED) {
     75 		printf("Test Unresolved: " TNAME " Error at mmap: %s\n",
     76 		       strerror(errno));
     77 		exit(PTS_UNRESOLVED);
     78 	}
     79 
     80 	/* pa should be a multiple of page size */
     81 	pa2 = pa;
     82 
     83 	while (((unsigned long)pa2 % page_size) == 0)
     84 		pa2++;
     85 
     86 	close(fd);
     87 	if (munmap(pa2, len) == -1 && errno == EINVAL) {
     88 		printf("Got EINVAL\n");
     89 		printf("Test PASSED\n");
     90 		exit(PTS_PASS);
     91 	} else {
     92 		printf("Test FAILED: " TNAME " munmap returns: %s\n",
     93 		       strerror(errno));
     94 		exit(PTS_FAIL);
     95 	}
     96 }
     97