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 * MAP_SHARED and MAP_PRIVATE describe the disposition of write references 10 * to the memory object. If MAP_SHARED is specified, write references shall 11 * change the underlying object. If MAP_PRIVATE is specified, modifications 12 * to the mapped data by the calling process shall be visible only to the 13 * calling process and shall not change the underlying object. 14 * It is unspecified whether modifications to the underlying object done 15 * after the MAP_PRIVATE mapping is established are visible through 16 * the MAP_PRIVATE mapping. 17 * 18 * Test Steps: 19 * 1. mmap a file into memory. Set flag as MAP_PRIVATE. 20 * 2. Modify the mapped memory, and call msync to try to synchronize the change. 21 * 3. munmap the mapped memory. 22 * 4. mmap the same file again into memory. 23 * 5. If the modification in step 2 appears in the mapped memory, then fail, 24 * otherwise pass. 25 */ 26 27 #define _XOPEN_SOURCE 600 28 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <unistd.h> 32 #include <sys/mman.h> 33 #include <sys/types.h> 34 #include <sys/stat.h> 35 #include <sys/wait.h> 36 #include <fcntl.h> 37 #include <string.h> 38 #include <errno.h> 39 #include "posixtest.h" 40 41 int main(void) 42 { 43 char tmpfname[256]; 44 ssize_t size = 1024; 45 char data[size]; 46 void *pa; 47 int fd; 48 49 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_7_2_%d", getpid()); 50 unlink(tmpfname); 51 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR); 52 if (fd == -1) { 53 printf("Error at open(): %s\n", strerror(errno)); 54 return PTS_UNRESOLVED; 55 } 56 unlink(tmpfname); 57 58 memset(data, 'a', size); 59 if (write(fd, data, size) != size) { 60 printf("Error at write(): %s\n", strerror(errno)); 61 return PTS_UNRESOLVED; 62 } 63 64 pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); 65 if (pa == MAP_FAILED) { 66 printf("Error at mmap(): %s\n", strerror(errno)); 67 return PTS_FAIL; 68 } 69 70 *(char *)pa = 'b'; 71 72 /* Try to flush changes back to the file */ 73 if (msync(pa, size, MS_SYNC) != 0) { 74 printf("Error at msync(): %s\n", strerror(errno)); 75 return PTS_UNRESOLVED; 76 } 77 78 munmap(pa, size); 79 80 /* Mmap again */ 81 pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); 82 if (pa == MAP_FAILED) { 83 printf("Error at 2nd mmap(): %s\n", strerror(errno)); 84 return PTS_FAIL; 85 } 86 87 if (*(char *)pa == 'b') { 88 printf("Memory write with MAP_PRIVATE has changed " 89 "the underlying file\n" "Test FAILED\n"); 90 exit(PTS_FAIL); 91 } 92 93 close(fd); 94 printf("Memory write with MAP_PRIVATE has not changed " 95 "the underlying file\n" "Test PASSED\n"); 96 return PTS_PASS; 97 } 98