2 * Simple test for validating mtime on a file create and set via utime.
15 void usage(char *prog)
17 fprintf(stderr, "usage: %s <filename> [-s <filename>]\n", prog);
21 int main(int argc, char *argv[])
23 long before_mknod, after_mknod;
24 const char *prog = argv[0];
25 const char *filename = argv[1];
35 while ((c = getopt(argc, argv, "s:")) != -1) {
44 if (optind + 1 > argc)
47 /* Adjust the before time back one second, because the kernel's
48 * CURRENT_TIME (lockless clock reading, used to set inode times)
49 * may drift against the do_gettimeofday() time (TSC-corrected and
50 * locked clock reading, used to return timestamps to user space).
51 * This means that the mknod time could be a second older than the
52 * before time, even for a local filesystem such as ext3.
54 before_mknod = time(0) - 1;
55 rc = mknod(filename, 0700, S_IFREG);
56 after_mknod = time(0);
57 if (rc && errno != EEXIST) {
58 fprintf(stderr, "%s: mknod(%s) failed: rc %d: %s\n",
59 prog, filename, errno, strerror(errno));
62 rc = stat(filename, &st);
64 fprintf(stderr, "%s: stat(%s) failed: rc %d: %s\n",
65 prog, filename, errno, strerror(errno));
69 if (st.st_mtime < before_mknod || st.st_mtime > after_mknod) {
71 "%s: bad mknod times %lu <= %lu <= %lu false\n",
72 prog, before_mknod, st.st_mtime, after_mknod);
76 printf("%s: good mknod times %lu%s <= %lu <= %lu for %s\n",
77 prog, before_mknod, before_mknod == st.st_mtime ? "*":"",
78 st.st_mtime, after_mknod, filename);
82 rc = stat(secname, &st2);
84 fprintf(stderr, "%s: stat(%s) failed: rc %d: "
85 "%s\n", prog, secname, errno,
90 if (st2.st_mtime < before_mknod ||
91 st2.st_mtime > after_mknod) {
92 fprintf(stderr, "%s: bad mknod times %lu <= %lu"
93 " <= %lu false\n", prog, before_mknod,
94 st2.st_mtime, after_mknod);
98 printf("%s: good mknod times %lu%s <= %lu <= %lu "
99 "for %s\n", prog, before_mknod,
100 before_mknod == st.st_mtime ? "*":"",
101 st2.st_mtime, after_mknod, secname);
106 rc = utime(filename, &utb);
108 fprintf(stderr, "%s: utime(%s) failed: rc %d: %s\n",
109 prog, filename, errno, strerror(errno));
113 rc = stat(filename, &st);
115 fprintf(stderr, "%s: second stat(%s) failed: rc %d: %s\n",
116 prog, filename, errno, strerror(errno));
120 if (st.st_mtime != utb.modtime ) {
121 fprintf(stderr, "%s: bad utime mtime %lu should be %lu\n",
122 prog, st.st_mtime, utb.modtime);
126 if (st.st_atime != utb.actime ) {
127 fprintf(stderr, "%s: bad utime atime %lu should be %lu\n",
128 prog, st.st_atime, utb.actime);
132 printf("%s: good utime mtimes %lu, atime %lu\n",
133 prog, utb.modtime, utb.actime);
138 /* Checking that times in past get updated on another client. */
139 rc = stat(secname, &st2);
141 fprintf(stderr, "%s: second stat(%s) failed: rc %d: %s\n",
142 prog, secname, errno, strerror(errno));
146 if (st2.st_mtime != st.st_mtime) {
147 fprintf(stderr, "%s: not synced mtime between clients: %lu "
148 "should be %lu\n", prog, st2.st_mtime, st.st_mtime);
152 if (st2.st_ctime != st.st_ctime) {
153 fprintf(stderr, "%s: not synced ctime between clients: %lu "
154 " should be %lu\n", prog, st2.st_ctime, st.st_ctime);
158 printf("%s: updated times for %s\n", prog, secname);