• 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  * 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 
33 #include "posixtest.h"
34 #include "tempfile.h"
35 
main(void)36 int main(void)
37 {
38 	char tmpfname[PATH_MAX];
39 	ssize_t size = 1024;
40 	char data[size];
41 	void *pa;
42 	int fd;
43 	struct stat stat_buff;
44 
45 	time_t mtime1, mtime2, ctime1, ctime2;
46 
47 	char *ch;
48 
49 	PTS_GET_TMP_FILENAME(tmpfname, "pts_mmap_14_1");
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 
57 	memset(data, 'a', size);
58 	printf("Time before write(): %ld\n", time(NULL));
59 	if (write(fd, data, size) != size) {
60 		printf("Error at write(): %s\n", strerror(errno));
61 		unlink(tmpfname);
62 		return PTS_UNRESOLVED;
63 	}
64 	sleep(1);
65 	printf("Time before mmap(): %ld\n", time(NULL));
66 	pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
67 	if (pa == MAP_FAILED) {
68 		printf("Error at mmap: %s\n", strerror(errno));
69 		unlink(tmpfname);
70 		return PTS_FAIL;
71 	}
72 	sleep(1);
73 	printf("Time before write reference: %ld\n", time(NULL));
74 	/* Before write reference */
75 	if (stat(tmpfname, &stat_buff) == -1) {
76 		printf("Error at 1st stat(): %s\n", strerror(errno));
77 		unlink(tmpfname);
78 		return PTS_UNRESOLVED;
79 	}
80 
81 	ctime1 = stat_buff.st_ctime;
82 	mtime1 = stat_buff.st_mtime;
83 
84 	ch = pa;
85 	*ch = 'b';
86 
87 	/* Wait a while in case the precision of the sa_time
88 	 * is not acurate enough to reflect the update
89 	 */
90 	sleep(1);
91 	printf("Time before msync(): %ld\n", time(NULL));
92 	msync(pa, size, MS_SYNC);
93 
94 	/* FIXME: Update the in-core meta data to the disk */
95 	fsync(fd);
96 
97 	if (stat(tmpfname, &stat_buff) == -1) {
98 		printf("Error at stat(): %s\n", strerror(errno));
99 		unlink(tmpfname);
100 		return PTS_UNRESOLVED;
101 	}
102 
103 	ctime2 = stat_buff.st_ctime;
104 	mtime2 = stat_buff.st_mtime;
105 
106 	printf("ctime1: %ld, ctime2: %ld\nmtime1: %ld, mtime2: %ld\n",
107 	       ctime1, ctime2, mtime1, mtime2);
108 	if (ctime2 == ctime1 || mtime2 == mtime1) {
109 		printf("Test FAILED: "
110 		       "st_ctime and st_mtime were not updated properly\n");
111 		unlink(tmpfname);
112 		return PTS_FAIL;
113 	}
114 
115 	munmap(pa, size);
116 	close(fd);
117 	unlink(tmpfname);
118 	printf("Test PASSED\n");
119 	return PTS_PASS;
120 }
121