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