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, setting MAP_SHARED.
20 * 2. Modify the mapped memory.
21 * 3. Call msync() to synchronize the modification.
22 * 4. munmap() this mapping.
23 * 5. mmap() the same file again into memory.
24 * 6. Check whether the modification has taken effect.
25 */
26
27#define _XOPEN_SOURCE 600
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
41int main(void)
42{
43	char tmpfname[256];
44	ssize_t size = 1024;
45	char data[size];
46	void *pa;
47	int fd;
48
49	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_7_1_%d", getpid());
50	unlink(tmpfname);
51	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
52	if (fd == -1) {
53		printf("Error at open(): %s\n", strerror(errno));
54		return PTS_UNRESOLVED;
55	}
56	unlink(tmpfname);
57
58	memset(data, 'a', size);
59	if (write(fd, data, size) != size) {
60		printf("Error at write(): %s\n", strerror(errno));
61		return PTS_UNRESOLVED;
62	}
63
64	pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
65	if (pa == MAP_FAILED) {
66		printf("Error at mmap(): %s\n", strerror(errno));
67		return PTS_FAIL;
68	}
69
70	*(char *)pa = 'b';
71
72	/* Flush changes back to the file */
73	if (msync(pa, size, MS_SYNC) != 0) {
74		printf("Error at msync(): %s\n", strerror(errno));
75		return PTS_UNRESOLVED;
76	}
77
78	munmap(pa, size);
79
80	/* Mmap again */
81	pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
82	if (pa == MAP_FAILED) {
83		printf("Error at 2nd mmap(): %s\n", strerror(errno));
84		return PTS_FAIL;
85	}
86
87	if (*(char *)pa == 'b') {
88		printf("Memory write with MAP_SHARED has changed "
89		       "the underlying file\n" "Test PASSED\n");
90
91		close(fd);
92		munmap(pa, size);
93
94		return PTS_PASS;
95	}
96
97	close(fd);
98	munmap(pa, size);
99
100	printf("Memory write with MAP_SHARED has not changed "
101	       "the underlying file\n" "Test FAILED\n");
102	return PTS_FAIL;
103}
104