12c28215423293e443469a07ae7011135d058b671Garrett Cooper/* 20dc076565f772bb1953209fb69ea150b494aaa40robbiew * Copyright (c) 2002, Intel Corporation. All rights reserved. 30dc076565f772bb1953209fb69ea150b494aaa40robbiew * This file is licensed under the GPL license. For the full content 42c28215423293e443469a07ae7011135d058b671Garrett Cooper * of this license, see the COPYING file at the top level of this 50dc076565f772bb1953209fb69ea150b494aaa40robbiew * source tree. 60dc076565f772bb1953209fb69ea150b494aaa40robbiew * 734faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe * If there are no mappings in the specified address range, then munmap() has 834faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe * no effect. To get a valid address range which is safe to call munmap() on 934faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe * we first map some arbitrary memory allowing the OS to select the address 1034faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe * then unmap it. We then call munmap() on the same address again to perform 1134faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe * the test. 120dc076565f772bb1953209fb69ea150b494aaa40robbiew * 130dc076565f772bb1953209fb69ea150b494aaa40robbiew */ 140dc076565f772bb1953209fb69ea150b494aaa40robbiew 150dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <pthread.h> 160dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdio.h> 170dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdlib.h> 180dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <unistd.h> 190dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <sys/mman.h> 200dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <sys/types.h> 210dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <sys/stat.h> 220dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <sys/wait.h> 230dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <fcntl.h> 240dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <string.h> 250dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <errno.h> 260dc076565f772bb1953209fb69ea150b494aaa40robbiew#include "posixtest.h" 272c28215423293e443469a07ae7011135d058b671Garrett Cooper 280dc076565f772bb1953209fb69ea150b494aaa40robbiew#define TNAME "munmap/2-1.c" 290dc076565f772bb1953209fb69ea150b494aaa40robbiew 304ca2bbdcd3003f3c8df4e6129e9c7b2bd1514f87Cyril Hrubisint main(void) 310dc076565f772bb1953209fb69ea150b494aaa40robbiew{ 3234faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe int rc, fd, map_size; 3334faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe void *map_addr; 3434faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe 3534faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe map_size = sysconf(_SC_PAGESIZE); 3634faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe fd = open("/dev/zero", O_RDWR); 3734faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe if (fd == -1) { 3834faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe printf("Failed to open /dev/zero: %s (%d)\n", 3934faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe strerror(errno), 4034faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe errno); 4134faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe return PTS_UNRESOLVED; 4234faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe } 4334faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe 4434faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe map_addr = mmap(NULL, map_size, PROT_NONE, MAP_PRIVATE, fd, 0); 4534faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe if (map_addr == MAP_FAILED) { 4634faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe printf("Failed to map memory: %s (%d)\n", 4734faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe strerror(errno), 4834faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe errno); 4934faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe close(fd); 5034faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe return PTS_UNRESOLVED; 5134faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe } 52354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao 5334faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe close(fd); 54354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao 5534faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe rc = munmap(map_addr, map_size); 5634faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe if (rc != 0) { 5734faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe printf("Failed to unmap memory: %s (%d)\n", 5834faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe strerror(errno), 5934faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe errno); 6034faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe close(fd); 6134faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe return PTS_UNRESOLVED; 62354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } 63354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao 6434faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe rc = munmap(map_addr, map_size); 6534faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe if (rc != 0) { 6634faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe printf("Test FAILED " TNAME " Error at munmap(): %s (%d)\n", 6734faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe strerror(errno), 6834faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe errno); 6934faca03b646a40f5a844e9456eadc6f3412f8a3Richard Palethorpe return PTS_FAIL; 70354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao } 71354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao 72354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao printf("Test PASSED\n"); 73354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao return PTS_PASS; 74ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman} 75