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  * The st_ctime and st_mtime fields of a file that is mapped with
10  * MAP_SHARED and PROT_WRITE shall be marked for update at some point
11  * in the interval between a write reference to the
12  * mapped region and the next call to msync() with MS_ASYNC or MS_SYNC
13  * for that portion of the file by any process.
14  * If there is no such call and if the underlying file is modified
15  * as a result of a write reference, then these fields shall be marked
16  * for update at some time after the write reference.
17  *
18  */
19 
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/wait.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <time.h>
32 #include "posixtest.h"
33 
main(void)34 int main(void)
35 {
36 	char tmpfname[256];
37 	ssize_t size = 1024;
38 	char data[size];
39 	void *pa;
40 	int fd;
41 	struct stat stat_buff;
42 
43 	time_t mtime1, mtime2, ctime1, ctime2;
44 
45 	char *ch;
46 
47 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_14_1_%d", getpid());
48 	unlink(tmpfname);
49 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
50 	if (fd == -1) {
51 		printf("Error at open(): %s\n", strerror(errno));
52 		return PTS_UNRESOLVED;
53 	}
54 
55 	memset(data, 'a', size);
56 	printf("Time before write(): %ld\n", time(NULL));
57 	if (write(fd, data, size) != size) {
58 		printf("Error at write(): %s\n", strerror(errno));
59 		unlink(tmpfname);
60 		return PTS_UNRESOLVED;
61 	}
62 	sleep(1);
63 	printf("Time before mmap(): %ld\n", time(NULL));
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 		unlink(tmpfname);
68 		return PTS_FAIL;
69 	}
70 	sleep(1);
71 	printf("Time before write reference: %ld\n", time(NULL));
72 	/* Before write reference */
73 	if (stat(tmpfname, &stat_buff) == -1) {
74 		printf("Error at 1st stat(): %s\n", strerror(errno));
75 		unlink(tmpfname);
76 		return PTS_UNRESOLVED;
77 	}
78 
79 	ctime1 = stat_buff.st_ctime;
80 	mtime1 = stat_buff.st_mtime;
81 
82 	ch = pa;
83 	*ch = 'b';
84 
85 	/* Wait a while in case the precision of the sa_time
86 	 * is not acurate enough to reflect the update
87 	 */
88 	sleep(1);
89 	printf("Time before msync(): %ld\n", time(NULL));
90 	msync(pa, size, MS_SYNC);
91 
92 	/* FIXME: Update the in-core meta data to the disk */
93 	fsync(fd);
94 
95 	if (stat(tmpfname, &stat_buff) == -1) {
96 		printf("Error at stat(): %s\n", strerror(errno));
97 		unlink(tmpfname);
98 		return PTS_UNRESOLVED;
99 	}
100 
101 	ctime2 = stat_buff.st_ctime;
102 	mtime2 = stat_buff.st_mtime;
103 
104 	printf("ctime1: %ld, ctime2: %ld\nmtime1: %ld, mtime2: %ld\n",
105 	       ctime1, ctime2, mtime1, mtime2);
106 	if (ctime2 == ctime1 || mtime2 == mtime1) {
107 		printf("Test FAILED: "
108 		       "st_ctime and st_mtime were not updated properly\n");
109 		unlink(tmpfname);
110 		return PTS_FAIL;
111 	}
112 
113 	munmap(pa, size);
114 	close(fd);
115 	unlink(tmpfname);
116 	printf("Test PASSED\n");
117 	return PTS_PASS;
118 }
119