1 /* 2 * Copyright (c) 2002, Intel Corporation. All rights reserved. 3 * Copyright (c) 2012, Cyril Hrubis <chrubis (at) suse.cz> 4 * 5 * This file is licensed under the GPL license. For the full content 6 * of this license, see the COPYING file at the top level of this 7 * source tree. 8 * 9 * The mmap() function shall establish a mapping 10 * between a process's address space 11 * and a shared memory object. 12 */ 13 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <unistd.h> 17 #include <sys/mman.h> 18 #include <sys/types.h> 19 #include <sys/stat.h> 20 #include <fcntl.h> 21 #include <string.h> 22 #include <errno.h> 23 #include "posixtest.h" 24 25 int main(void) 26 { 27 char tmpfname[256]; 28 void *pa; 29 size_t size = 1024 * 4 * 1024; 30 int fd; 31 32 snprintf(tmpfname, sizeof(tmpfname), "pts_mmap_1_2_%d", getpid()); 33 34 /* Create shared object */ 35 shm_unlink(tmpfname); 36 fd = shm_open(tmpfname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); 37 if (fd == -1) { 38 printf("Error at shm_open(): %s\n", strerror(errno)); 39 return PTS_UNRESOLVED; 40 } 41 shm_unlink(tmpfname); 42 if (ftruncate(fd, size) == -1) { 43 printf("Error at ftruncate(): %s\n", strerror(errno)); 44 return PTS_UNRESOLVED; 45 } 46 47 if (write(fd, "a", 1) != 1) { 48 printf("Error at write(): %s\n", strerror(errno)); 49 return PTS_UNRESOLVED; 50 } 51 52 pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 53 if (pa == MAP_FAILED) { 54 printf("Error at mmap: %s\n", strerror(errno)); 55 return PTS_FAIL; 56 } 57 58 if (*(char *)pa != 'a') { 59 printf("Test FAILED: The file was not mapped correctly.\n"); 60 return PTS_FAIL; 61 } 62 63 close(fd); 64 munmap(pa, size); 65 printf("Test PASSED\n"); 66 return PTS_PASS; 67 } 68