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 *
20 * 1. Create a shared memory object.
21 * 2. mmap the shared memory object into memory, setting MAP_PRIVATE.
22 * 3. Modify the mapped memory.
23 * 4. Fork a child process.
24 * 5. Child process mmap the same shared memory object into memory.
25 * 6. Check whether the change in step 3 is visible to the child.
26 *
27 */
28
29 #define _XOPEN_SOURCE 600
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <sys/mman.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37 #include <fcntl.h>
38 #include <string.h>
39 #include <errno.h>
40 #include "posixtest.h"
41
main(void)42 int main(void)
43 {
44 char tmpfname[256];
45 void *pa;
46 size_t size = 1024;
47 int fd;
48 pid_t child;
49 int exit_stat;
50
51 /* Create shared object */
52 snprintf(tmpfname, sizeof(tmpfname), "pts_mmap_7_3_%d", getpid());
53 shm_unlink(tmpfname);
54 fd = shm_open(tmpfname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
55 if (fd == -1) {
56 printf("Error at shm_open(): %s\n", strerror(errno));
57 return PTS_UNRESOLVED;
58 }
59 shm_unlink(tmpfname);
60
61 if (ftruncate(fd, size) == -1) {
62 printf("Error at ftruncate(): %s\n", strerror(errno));
63 return PTS_UNRESOLVED;
64 }
65
66 pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
67 if (pa == MAP_FAILED) {
68 printf("Error at mmap(): %s\n", strerror(errno));
69 return PTS_FAIL;
70 }
71
72 /* Write the mapped memory */
73 *(char *)pa = 'a';
74
75 child = fork();
76
77 switch (child) {
78 case 0:
79 /* Mmap again the same shared memory to child's memory */
80 pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,
81 0);
82 if (pa == MAP_FAILED) {
83 printf("Error at mmap(): %s\n", strerror(errno));
84 return PTS_FAIL;
85 }
86
87 if (*(char *)pa == 'a') {
88 printf("Set flag as MAP_PRIVATE, write reference will "
89 "change the underlying shared memory object\n");
90 return PTS_FAIL;
91 }
92
93 printf("Set flag as MAP_PRIVATE, write reference will "
94 "not change the underlying shared memory object\n"
95 "Test PASSED\n");
96 return PTS_PASS;
97 break;
98 case -1:
99 printf("Error at fork(): %s\n", strerror(errno));
100 return PTS_UNRESOLVED;
101 break;
102 default:
103 waitpid(child, &exit_stat, WUNTRACED);
104
105 close(fd);
106 munmap(pa, size);
107
108 if (WIFEXITED(exit_stat)) {
109 return WEXITSTATUS(exit_stat);
110 } else {
111 printf("Child has not exit properly\n");
112 return PTS_UNRESOLVED;
113 }
114 }
115 }
116