• 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_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 #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 	void *pa;
45 	size_t size = 1024;
46 	int fd;
47 	pid_t child;
48 	int exit_stat;
49 
50 	/* Create shared object */
51 	snprintf(tmpfname, sizeof(tmpfname), "pts_mmap_7_3_%d", getpid());
52 	shm_unlink(tmpfname);
53 	fd = shm_open(tmpfname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
54 	if (fd == -1) {
55 		printf("Error at shm_open(): %s\n", strerror(errno));
56 		return PTS_UNRESOLVED;
57 	}
58 	shm_unlink(tmpfname);
59 
60 	if (ftruncate(fd, size) == -1) {
61 		printf("Error at ftruncate(): %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 	/* Write the mapped memory */
72 	*(char *)pa = 'a';
73 
74 	child = fork();
75 
76 	switch (child) {
77 	case 0:
78 		/* Mmap again the same shared memory to child's memory */
79 		pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,
80 			  0);
81 		if (pa == MAP_FAILED) {
82 			printf("Error at mmap(): %s\n", strerror(errno));
83 			return PTS_FAIL;
84 		}
85 
86 		if (*(char *)pa == 'a') {
87 			printf("Set flag as MAP_PRIVATE, write reference will "
88 			       "change the underlying shared memory object\n");
89 			return PTS_FAIL;
90 		}
91 
92 		printf("Set flag as MAP_PRIVATE, write reference will "
93 		       "not change the underlying shared memory object\n"
94 		       "Test PASSED\n");
95 		return PTS_PASS;
96 		break;
97 	case -1:
98 		printf("Error at fork(): %s\n", strerror(errno));
99 		return PTS_UNRESOLVED;
100 		break;
101 	default:
102 		waitpid(child, &exit_stat, WUNTRACED);
103 
104 		close(fd);
105 		munmap(pa, size);
106 
107 		if (WIFEXITED(exit_stat)) {
108 			return WEXITSTATUS(exit_stat);
109 		} else {
110 			printf("Child has not exit properly\n");
111 			return PTS_UNRESOLVED;
112 		}
113 	}
114 }
115