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 * 8 * If a mapping to be removed was private, any modifications 9 * made in this address range shall be discarded. 10 * 11 * Test Step: 12 * 1. mmap a file into memory. Set flag as MAP_PRIVATE; 13 * 2. Modify the mapped memory, and call msync to try to synchronize the change with 14 * the file; 15 * 3. munmap the mapped memory; 16 * 4. mmap the same file again into memory; 17 * 5. If the modification in step 2 appears in the mapped memory, then fail, 18 * otherwise pass. 19 */ 20 21 #define _XOPEN_SOURCE 600 22 23 #include <pthread.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <unistd.h> 27 #include <sys/mman.h> 28 #include <sys/types.h> 29 #include <sys/stat.h> 30 #include <sys/wait.h> 31 #include <fcntl.h> 32 #include <string.h> 33 #include <errno.h> 34 #include "posixtest.h" 35 36 #define TNAME "munmap/4-1.c" 37 38 int main(void) 39 { 40 int rc; 41 42 char tmpfname[256]; 43 char *data; 44 int total_size = 1024; 45 46 void *pa = NULL; 47 void *addr = NULL; 48 size_t size = total_size; 49 int flag; 50 int fd; 51 off_t off = 0; 52 int prot; 53 54 char *ch; 55 56 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_munmap_4_1_%d", 57 getpid()); 58 unlink(tmpfname); 59 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR); 60 if (fd == -1) { 61 printf(TNAME " Error at open(): %s\n", strerror(errno)); 62 exit(PTS_UNRESOLVED); 63 } 64 unlink(tmpfname); 65 66 data = malloc(total_size); 67 memset(data, 'a', total_size); 68 if (write(fd, data, total_size) != total_size) { 69 printf(TNAME "Error at write(): %s\n", strerror(errno)); 70 exit(PTS_UNRESOLVED); 71 } 72 free(data); 73 74 prot = PROT_READ | PROT_WRITE; 75 flag = MAP_PRIVATE; 76 pa = mmap(addr, size, prot, flag, fd, off); 77 if (pa == MAP_FAILED) { 78 printf("Test Fail: " TNAME " Error at mmap: %s\n", 79 strerror(errno)); 80 exit(PTS_FAIL); 81 } 82 83 ch = pa; 84 *ch = 'b'; 85 86 /* Flush changes back to the file */ 87 88 if ((rc = msync(pa, size, MS_SYNC)) != 0) { 89 printf(TNAME " Error at msync(): %s\n", strerror(rc)); 90 exit(PTS_UNRESOLVED); 91 } 92 93 munmap(pa, size); 94 95 /* Mmap again */ 96 97 pa = mmap(addr, size, prot, flag, fd, off); 98 if (pa == MAP_FAILED) { 99 printf("Test Fail: " TNAME " Error at 2nd mmap: %s\n", 100 strerror(errno)); 101 exit(PTS_FAIL); 102 } 103 104 ch = pa; 105 if (*ch == 'b') { 106 printf("Test FAIL\n"); 107 exit(PTS_FAIL); 108 } 109 110 close(fd); 111 printf("Write referece is discarded when setting MAP_RPIVATE\n"); 112 printf("Test PASSED\n"); 113 return PTS_PASS; 114 } 115