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 * When the implementation selects a 8 * value for pa, it never places a mapping at address 0. 9 * 10 * Test step: 11 * This is not a good test. Cannot make sure (pa == 0) never happens. 12 * Repeat LOOP_NUM times mmap() and mnumap(), 13 * make sure pa will not equal 0. 14 */ 15 16#define _XOPEN_SOURCE 600 17 18#include <pthread.h> 19#include <stdio.h> 20#include <stdlib.h> 21#include <unistd.h> 22#include <sys/mman.h> 23#include <sys/types.h> 24#include <sys/stat.h> 25#include <sys/wait.h> 26#include <fcntl.h> 27#include <string.h> 28#include <errno.h> 29#include "posixtest.h" 30 31#define TNAME "mmap/10-1.c" 32 33#define LOOP_NUM 100000 34 35int main() 36{ 37 int rc; 38 unsigned long cnt; 39 40 char tmpfname[256]; 41 long total_size; 42 43 void *pa = NULL; 44 void *addr = NULL; 45 size_t size; 46 int flag; 47 int fd; 48 off_t off = 0; 49 int prot; 50 51 total_size = 1024; 52 size = total_size; 53 54 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_10_1_%ld", 55 (long)getpid()); 56 unlink(tmpfname); 57 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, 58 S_IRUSR | S_IWUSR); 59 if (fd == -1) 60 { 61 printf(TNAME " Error at open(): %s\n", 62 strerror(errno)); 63 exit(PTS_UNRESOLVED); 64 } 65 unlink(tmpfname); 66 if (ftruncate(fd, total_size) == -1) 67 { 68 printf(TNAME "Error at ftruncate(): %s\n", 69 strerror(errno)); 70 exit(PTS_UNRESOLVED); 71 } 72 73 flag = MAP_SHARED; 74 prot = PROT_READ | PROT_WRITE; 75 for (cnt = 0; cnt < LOOP_NUM; cnt++) 76 { 77 pa = mmap(addr, size, prot, flag, fd, off); 78 if (pa == MAP_FAILED) 79 { 80 printf ("Test Fail: " TNAME " Error at mmap: %s\n", 81 strerror(errno)); 82 exit(PTS_FAIL); 83 } 84 85 if (pa == 0) 86 { 87 printf("Test Fail " TNAME " mmap() map the file to 0 address " 88 "without setting MAP_FIXED\n"); 89 exit(PTS_FAIL); 90 } 91 rc = munmap (pa, size); 92 if (rc != 0) 93 { 94 printf(TNAME "Error at mnumap(): %s\n", 95 strerror(errno)); 96 exit(PTS_UNRESOLVED); 97 } 98 } 99 100 close (fd); 101 printf ("Test PASS\n"); 102 return PTS_PASS; 103} 104