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