1 /*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * Copyright (c) 2012, Cyril Hrubis <chrubis@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
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <sys/mman.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/wait.h>
35 #include <fcntl.h>
36 #include <string.h>
37 #include <errno.h>
38
39 #include "posixtest.h"
40 #include "tempfile.h"
41
main(void)42 int main(void)
43 {
44 char tmpfname[PATH_MAX];
45 ssize_t size = 1024;
46 char data[size];
47 void *pa;
48 int fd;
49
50 PTS_GET_TMP_FILENAME(tmpfname, "pts_mmap_7_2");
51 unlink(tmpfname);
52 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
53 if (fd == -1) {
54 printf("Error at open(): %s\n", strerror(errno));
55 return PTS_UNRESOLVED;
56 }
57 unlink(tmpfname);
58
59 memset(data, 'a', size);
60 if (write(fd, data, size) != size) {
61 printf("Error at write(): %s\n", strerror(errno));
62 return PTS_UNRESOLVED;
63 }
64
65 pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
66 if (pa == MAP_FAILED) {
67 printf("Error at mmap(): %s\n", strerror(errno));
68 return PTS_FAIL;
69 }
70
71 *(char *)pa = 'b';
72
73 /* Try to flush changes back to the file */
74 if (msync(pa, size, MS_SYNC) != 0) {
75 printf("Error at msync(): %s\n", strerror(errno));
76 return PTS_UNRESOLVED;
77 }
78
79 munmap(pa, size);
80
81 /* Mmap again */
82 pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
83 if (pa == MAP_FAILED) {
84 printf("Error at 2nd mmap(): %s\n", strerror(errno));
85 return PTS_FAIL;
86 }
87
88 if (*(char *)pa == 'b') {
89 printf("Memory write with MAP_PRIVATE has changed "
90 "the underlying file\n" "Test FAILED\n");
91 exit(PTS_FAIL);
92 }
93
94 close(fd);
95 printf("Memory write with MAP_PRIVATE has not changed "
96 "the underlying file\n" "Test PASSED\n");
97 return PTS_PASS;
98 }
99