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 mmap( ) function shall establish a mapping between a process's 8 * address space and a file, 9 * 10 * Test Step: 11 * 1. Create a tmp file; 12 * 2. mmap it to memory using mmap(); 13 * 14 */ 15 16#define _XOPEN_SOURCE 600 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 <fcntl.h> 25#include <string.h> 26#include <errno.h> 27#include "posixtest.h" 28 29#define TNAME "mmap/1-1.c" 30 31int main() 32{ 33 char tmpfname[256]; 34 char* data; 35 int total_size = 1024; 36 37 void *pa = NULL; 38 void *addr = NULL; 39 size_t len = total_size; 40 int prot = PROT_READ | PROT_WRITE; 41 int flag = MAP_SHARED; 42 int fd; 43 off_t off = 0; 44 45 char * ch; 46 47 data = (char *) malloc(total_size); 48 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_1_1_%ld", 49 (long)getpid()); 50 unlink(tmpfname); 51 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, 52 S_IRUSR | S_IWUSR); 53 if (fd == -1) 54 { 55 printf(TNAME " Error at open(): %s\n", 56 strerror(errno)); 57 exit(PTS_UNRESOLVED); 58 } 59 60 /* Make sure the file is removed when it is closed */ 61 unlink(tmpfname); 62 memset(data, 'a', total_size); 63 if (write(fd, data, total_size) != total_size) 64 { 65 printf(TNAME "Error at write(): %s\n", 66 strerror(errno)); 67 exit(PTS_UNRESOLVED); 68 } 69 free(data); 70 71 pa = mmap(addr, len, prot, flag, fd, off); 72 if (pa == MAP_FAILED) 73 { 74 printf("Test Fail: " TNAME " Error at mmap: %s\n", 75 strerror(errno)); 76 exit(PTS_FAIL); 77 } 78 79 ch = pa; 80 81 if(*ch != 'a') 82 { 83 printf ("Test Fail: " TNAME 84 " The file did not mapped to memory\n"); 85 exit(PTS_FAIL); 86 } 87 88 close(fd); 89 munmap(pa, len); 90 printf ("Test Pass\n"); 91 return PTS_PASS; 92} 93