1 #include <darwintest.h>
2 #include <darwintest_utils.h>
3 #include <stdlib.h>
4 #include <sys/stat.h>
5 #include <sys/syslimits.h>
6 #include <sys/time.h>
7 #include <time.h>
8 #include <unistd.h>
9
10 T_GLOBAL_META(T_META_CHECK_LEAKS(false));
11
12 T_DECL(settimeofday, "check setting and getting time of day",
13 T_META_ASROOT(true), T_META_TAG_VM_PREFERRED)
14 {
15 struct timeval origtime = {};
16 struct timezone origtz = {};
17 int ret = gettimeofday(&origtime, &origtz);
18 T_ASSERT_POSIX_SUCCESS(ret, "get current time with gettimeofday(2)");
19
20 #if TARGET_OS_BRIDGE
21 /*
22 * bridgeOS is not allowed to set the time -- only the macOS side can.
23 */
24 T_SKIP("bridgeOS is not allowed to call settimeofday(2)");
25 #endif /* TARGET_OS_BRIDGE */
26
27 struct timeval newtime = {};
28 newtime = origtime;
29 newtime.tv_sec -= 60;
30 ret = settimeofday(&newtime, NULL);
31 T_ASSERT_POSIX_SUCCESS(ret,
32 "set time back 60 seconds with settimeofday(2)");
33
34 ret = gettimeofday(&newtime, NULL);
35 T_ASSERT_POSIX_SUCCESS(ret, "get new time with gettimeofday(2)");
36
37 T_ASSERT_GT(origtime.tv_sec, newtime.tv_sec,
38 "new time should be before original time");
39
40 newtime = origtime;
41 newtime.tv_sec += 1;
42 ret = settimeofday(&newtime, NULL);
43 T_ASSERT_POSIX_SUCCESS(ret,
44 "set time close to original value with gettimeofday(2)");
45 }
46
47 static char tmppath[PATH_MAX] = "";
48
49 static void
cleanup_tmpfile(void)50 cleanup_tmpfile(void)
51 {
52 if (tmppath[0] != '\0') {
53 unlink(tmppath);
54 }
55 }
56
57 static int
create_tmpfile(void)58 create_tmpfile(void)
59 {
60 const char *tmpdir = dt_tmpdir();
61 strlcat(tmppath, tmpdir ? tmpdir : "/tmp", sizeof(tmppath));
62 strlcat(tmppath, "/xnu.futimes.XXXXX", sizeof(tmppath));
63 T_LOG("creating temporary file at %s", tmppath);
64 int fd = mkstemp(tmppath);
65 T_ASSERT_POSIX_SUCCESS(fd, "create temporary file");
66 T_ATEND(cleanup_tmpfile);
67 return fd;
68 }
69
70 T_DECL(futimes, "check that futimes updates file times",
71 T_META_RUN_CONCURRENTLY(true), T_META_TAG_VM_PREFERRED)
72 {
73 int tmpfd = create_tmpfile();
74
75 struct stat stbuf = {};
76 int ret = fstat(tmpfd, &stbuf);
77 T_ASSERT_POSIX_SUCCESS(ret, "get file metadata with fstat(2)");
78 struct timeval amtimes[2] = {};
79 TIMESPEC_TO_TIMEVAL(&amtimes[0], &stbuf.st_atimespec);
80 TIMESPEC_TO_TIMEVAL(&amtimes[1], &stbuf.st_mtimespec);
81
82 amtimes[0].tv_sec -= 120;
83 amtimes[1].tv_sec -= 120;
84
85 ret = futimes(tmpfd, amtimes);
86 T_ASSERT_POSIX_SUCCESS(ret, "update file times with utimes(2)");
87
88 ret = fstat(tmpfd, &stbuf);
89 T_ASSERT_POSIX_SUCCESS(ret, "get file metadata after update with fstat(2)");
90 struct timeval newamtimes[2] = {};
91 TIMESPEC_TO_TIMEVAL(&newamtimes[0], &stbuf.st_atimespec);
92 TIMESPEC_TO_TIMEVAL(&newamtimes[1], &stbuf.st_mtimespec);
93
94 /*
95 * Reading the metadata shouldn't count as an access.
96 */
97 T_ASSERT_EQ(amtimes[0].tv_sec, newamtimes[0].tv_sec,
98 "access time matches what was set");
99 T_ASSERT_EQ(amtimes[1].tv_sec, newamtimes[1].tv_sec,
100 "modification time matches what was set");
101 }
102