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