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  * If there are no mappings in the specified address range, then munmap() has
      8  * no effect. To get a valid address range which is safe to call munmap() on
      9  * we first map some arbitrary memory allowing the OS to select the address
     10  * then unmap it. We then call munmap() on the same address again to perform
     11  * the test.
     12  *
     13  */
     14 
     15 #include <pthread.h>
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <unistd.h>
     19 #include <sys/mman.h>
     20 #include <sys/types.h>
     21 #include <sys/stat.h>
     22 #include <sys/wait.h>
     23 #include <fcntl.h>
     24 #include <string.h>
     25 #include <errno.h>
     26 #include "posixtest.h"
     27 
     28 #define TNAME "munmap/2-1.c"
     29 
     30 int main(void)
     31 {
     32 	int rc, fd, map_size;
     33 	void *map_addr;
     34 
     35 	map_size = sysconf(_SC_PAGESIZE);
     36 	fd = open("/dev/zero", O_RDWR);
     37 	if (fd == -1) {
     38 		printf("Failed to open /dev/zero: %s (%d)\n",
     39 		       strerror(errno),
     40 		       errno);
     41 		return PTS_UNRESOLVED;
     42 	}
     43 
     44 	map_addr = mmap(NULL, map_size, PROT_NONE, MAP_PRIVATE, fd, 0);
     45 	if (map_addr == MAP_FAILED) {
     46 		printf("Failed to map memory: %s (%d)\n",
     47 		       strerror(errno),
     48 		       errno);
     49 		close(fd);
     50 		return PTS_UNRESOLVED;
     51 	}
     52 
     53 	close(fd);
     54 
     55 	rc = munmap(map_addr, map_size);
     56 	if (rc != 0) {
     57 		printf("Failed to unmap memory: %s (%d)\n",
     58 		       strerror(errno),
     59 		       errno);
     60 		close(fd);
     61 		return PTS_UNRESOLVED;
     62 	}
     63 
     64 	rc = munmap(map_addr, map_size);
     65 	if (rc != 0) {
     66 		printf("Test FAILED " TNAME " Error at munmap(): %s (%d)\n",
     67 		       strerror(errno),
     68 		       errno);
     69 		return PTS_FAIL;
     70 	}
     71 
     72 	printf("Test PASSED\n");
     73 	return PTS_PASS;
     74 }
     75