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_SHARED.
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 #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 #include "posixtest.h"
39
main(void)40 int main(void)
41 {
42 char tmpfname[256];
43 int shm_fd;
44 void *pa;
45 size_t size = 1024;
46
47 pid_t child;
48
49 int exit_stat;
50
51 /* Create shared object */
52 snprintf(tmpfname, sizeof(tmpfname), "pts_mmap_7_4_%d", getpid());
53 shm_unlink(tmpfname);
54 shm_fd =
55 shm_open(tmpfname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
56 if (shm_fd == -1) {
57 printf("Error at shm_open(): %s\n", strerror(errno));
58 return PTS_UNRESOLVED;
59 }
60 shm_unlink(tmpfname);
61
62 /* Set the size of the shared memory object */
63 if (ftruncate(shm_fd, size) == -1) {
64 printf("Error at ftruncate(): %s\n", strerror(errno));
65 return PTS_UNRESOLVED;
66 }
67
68 pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
69 if (pa == MAP_FAILED) {
70 printf("Error at mmap: %s\n", strerror(errno));
71 return PTS_FAIL;
72 }
73
74 /* Write the mapped memory */
75 *(char *)pa = 'a';
76
77 child = fork();
78 switch (child) {
79 case 0:
80 /* Mmap again the same shared memory to child's memory */
81 pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
82 shm_fd, 0);
83 if (pa == MAP_FAILED) {
84 printf("child: Error at mmap: %s\n", strerror(errno));
85 return PTS_FAIL;
86 }
87
88 if (*(char *)pa == 'a') {
89 printf("Test PASSED\n");
90 return PTS_PASS;
91 }
92
93 printf("mmap with MAP_SHARED failed to propagate change "
94 "into the child\n");
95 return PTS_FAIL;
96 case -1:
97 printf("Error at fork(): %s\n", strerror(errno));
98 return PTS_UNRESOLVED;
99
100 default:
101 waitpid(child, &exit_stat, WUNTRACED);
102
103 munmap(pa, size);
104
105 if (WIFEXITED(exit_stat)) {
106 return WEXITSTATUS(exit_stat);
107 } else {
108 printf("Child exited abnormally\n");
109 return PTS_UNRESOLVED;
110 }
111 }
112 }
113