1*c54f35caSApple OSS Distributions /* compile: xcrun -sdk macosx.internal clang -ldarwintest -o test_file_leases file_leases.c -g -Weverything */
2*c54f35caSApple OSS Distributions
3*c54f35caSApple OSS Distributions #include <darwintest.h>
4*c54f35caSApple OSS Distributions #include <darwintest_utils.h>
5*c54f35caSApple OSS Distributions #include <darwintest_multiprocess.h>
6*c54f35caSApple OSS Distributions #include <copyfile.h>
7*c54f35caSApple OSS Distributions #include <errno.h>
8*c54f35caSApple OSS Distributions #include <fcntl.h>
9*c54f35caSApple OSS Distributions #include <stdint.h>
10*c54f35caSApple OSS Distributions #include <stdlib.h>
11*c54f35caSApple OSS Distributions #include <unistd.h>
12*c54f35caSApple OSS Distributions #include <sys/clonefile.h>
13*c54f35caSApple OSS Distributions #include <sys/event.h>
14*c54f35caSApple OSS Distributions #include <sys/resource.h>
15*c54f35caSApple OSS Distributions #include <sys/sysctl.h>
16*c54f35caSApple OSS Distributions #include <sys/xattr.h>
17*c54f35caSApple OSS Distributions
18*c54f35caSApple OSS Distributions #include "test_utils.h"
19*c54f35caSApple OSS Distributions
20*c54f35caSApple OSS Distributions
21*c54f35caSApple OSS Distributions T_GLOBAL_META(
22*c54f35caSApple OSS Distributions T_META_NAMESPACE("xnu.vfs.lease"),
23*c54f35caSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
24*c54f35caSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("vfs"),
25*c54f35caSApple OSS Distributions T_META_CHECK_LEAKS(false));
26*c54f35caSApple OSS Distributions
27*c54f35caSApple OSS Distributions #define TEST_LEASE_DIR "lease_dir"
28*c54f35caSApple OSS Distributions #define TEST_LEASE_FILE "lease_file"
29*c54f35caSApple OSS Distributions
30*c54f35caSApple OSS Distributions static char g_testfile[MAXPATHLEN];
31*c54f35caSApple OSS Distributions static char g_testdir[MAXPATHLEN];
32*c54f35caSApple OSS Distributions
33*c54f35caSApple OSS Distributions /*
34*c54f35caSApple OSS Distributions * This unit-test validates the behavior of file leasing (read and write leases)
35*c54f35caSApple OSS Distributions * by utilizing the file leasing API (fcntl's F_SETLEASE and F_GETLEASE
36*c54f35caSApple OSS Distributions * commands) provided by VFS.
37*c54f35caSApple OSS Distributions */
38*c54f35caSApple OSS Distributions
39*c54f35caSApple OSS Distributions
40*c54f35caSApple OSS Distributions static void
exit_cleanup(void)41*c54f35caSApple OSS Distributions exit_cleanup(void)
42*c54f35caSApple OSS Distributions {
43*c54f35caSApple OSS Distributions uint32_t val, new_val;
44*c54f35caSApple OSS Distributions size_t val_len, new_val_len;
45*c54f35caSApple OSS Distributions
46*c54f35caSApple OSS Distributions (void)remove(g_testfile);
47*c54f35caSApple OSS Distributions (void)rmdir(g_testdir);
48*c54f35caSApple OSS Distributions
49*c54f35caSApple OSS Distributions new_val = 60;
50*c54f35caSApple OSS Distributions new_val_len = val_len = sizeof(uint32_t);
51*c54f35caSApple OSS Distributions (void)sysctlbyname("vfs.lease.break_timeout", &val, &val_len,
52*c54f35caSApple OSS Distributions (void *)&new_val, new_val_len);
53*c54f35caSApple OSS Distributions }
54*c54f35caSApple OSS Distributions
55*c54f35caSApple OSS Distributions
56*c54f35caSApple OSS Distributions static void
create_test_file(void)57*c54f35caSApple OSS Distributions create_test_file(void)
58*c54f35caSApple OSS Distributions {
59*c54f35caSApple OSS Distributions const char *tmpdir = dt_tmpdir();
60*c54f35caSApple OSS Distributions int fd;
61*c54f35caSApple OSS Distributions
62*c54f35caSApple OSS Distributions T_SETUPBEGIN;
63*c54f35caSApple OSS Distributions
64*c54f35caSApple OSS Distributions /*
65*c54f35caSApple OSS Distributions * Make sure dataless file manipulation is enabled for this
66*c54f35caSApple OSS Distributions * process (children will inherit).
67*c54f35caSApple OSS Distributions *
68*c54f35caSApple OSS Distributions * See kpi_vfs.c:vfs_context_can_break_leases().
69*c54f35caSApple OSS Distributions */
70*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(
71*c54f35caSApple OSS Distributions setiopolicy_np(IOPOL_TYPE_VFS_MATERIALIZE_DATALESS_FILES,
72*c54f35caSApple OSS Distributions IOPOL_SCOPE_PROCESS, IOPOL_MATERIALIZE_DATALESS_FILES_ON),
73*c54f35caSApple OSS Distributions "Setup: ensuring dataless file materialization is enabled");
74*c54f35caSApple OSS Distributions
75*c54f35caSApple OSS Distributions atexit(exit_cleanup);
76*c54f35caSApple OSS Distributions
77*c54f35caSApple OSS Distributions snprintf(g_testdir, MAXPATHLEN, "%s/%s", tmpdir, TEST_LEASE_DIR);
78*c54f35caSApple OSS Distributions
79*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(mkdir(g_testdir, 0777),
80*c54f35caSApple OSS Distributions "Setup: creating test dir: %s", g_testdir);
81*c54f35caSApple OSS Distributions
82*c54f35caSApple OSS Distributions snprintf(g_testfile, MAXPATHLEN, "%s/%s", g_testdir, TEST_LEASE_FILE);
83*c54f35caSApple OSS Distributions
84*c54f35caSApple OSS Distributions T_WITH_ERRNO;
85*c54f35caSApple OSS Distributions fd = open(g_testfile, O_CREAT | O_RDWR, 0666);
86*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Create test fi1e: %s", g_testfile);
87*c54f35caSApple OSS Distributions
88*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close test file: %s", TEST_LEASE_FILE);
89*c54f35caSApple OSS Distributions
90*c54f35caSApple OSS Distributions T_SETUPEND;
91*c54f35caSApple OSS Distributions }
92*c54f35caSApple OSS Distributions
93*c54f35caSApple OSS Distributions #define HELPER_TIMEOUT_SECS 60
94*c54f35caSApple OSS Distributions #define MAX_HELPERS 10
95*c54f35caSApple OSS Distributions
96*c54f35caSApple OSS Distributions static void __attribute__((noreturn))
run_helpers(const char ** helper_test_names,int num_helpers)97*c54f35caSApple OSS Distributions run_helpers(const char **helper_test_names, int num_helpers)
98*c54f35caSApple OSS Distributions {
99*c54f35caSApple OSS Distributions dt_helper_t helpers[MAX_HELPERS];
100*c54f35caSApple OSS Distributions char *args[] = {g_testfile, g_testdir, NULL};
101*c54f35caSApple OSS Distributions int i;
102*c54f35caSApple OSS Distributions
103*c54f35caSApple OSS Distributions T_QUIET;
104*c54f35caSApple OSS Distributions T_ASSERT_LE(num_helpers, MAX_HELPERS, "too many helpers");
105*c54f35caSApple OSS Distributions
106*c54f35caSApple OSS Distributions for (i = 0; i < num_helpers; i++) {
107*c54f35caSApple OSS Distributions helpers[i] = dt_child_helper_args(helper_test_names[i], args);
108*c54f35caSApple OSS Distributions }
109*c54f35caSApple OSS Distributions dt_run_helpers(helpers, (size_t)num_helpers, HELPER_TIMEOUT_SECS);
110*c54f35caSApple OSS Distributions }
111*c54f35caSApple OSS Distributions
112*c54f35caSApple OSS Distributions T_HELPER_DECL(open_rdonly_acquire_read_lease_succeed, "Open file in O_RDONLY mode and acquire read lease succeeded")
113*c54f35caSApple OSS Distributions {
114*c54f35caSApple OSS Distributions char *testfile = argv[0];
115*c54f35caSApple OSS Distributions int err, fd;
116*c54f35caSApple OSS Distributions
117*c54f35caSApple OSS Distributions T_WITH_ERRNO;
118*c54f35caSApple OSS Distributions fd = open(testfile, O_RDONLY, 0666);
119*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDONLY: %s", testfile);
120*c54f35caSApple OSS Distributions
121*c54f35caSApple OSS Distributions T_WITH_ERRNO;
122*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_RDLCK);
123*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Acquire read lease: %s", testfile);
124*c54f35caSApple OSS Distributions
125*c54f35caSApple OSS Distributions T_WITH_ERRNO;
126*c54f35caSApple OSS Distributions err = fcntl(fd, F_GETLEASE);
127*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_RDLCK, "Retrieve lease: %s", testfile);
128*c54f35caSApple OSS Distributions
129*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close test file: %s", testfile);
130*c54f35caSApple OSS Distributions }
131*c54f35caSApple OSS Distributions
132*c54f35caSApple OSS Distributions T_HELPER_DECL(open_rdonly_acquire_read_lease_EAGAIN, "Open file in O_RDONLY mode and acquire read lease failed with EAGAIN")
133*c54f35caSApple OSS Distributions {
134*c54f35caSApple OSS Distributions char *testfile = argv[0];
135*c54f35caSApple OSS Distributions int err, fd;
136*c54f35caSApple OSS Distributions
137*c54f35caSApple OSS Distributions T_WITH_ERRNO;
138*c54f35caSApple OSS Distributions fd = open(testfile, O_RDONLY, 0666);
139*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDONLY: %s", testfile);
140*c54f35caSApple OSS Distributions
141*c54f35caSApple OSS Distributions T_WITH_ERRNO;
142*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_RDLCK);
143*c54f35caSApple OSS Distributions T_ASSERT_TRUE((err == -1) && (errno == EAGAIN), "Acquire read lease: %s",
144*c54f35caSApple OSS Distributions testfile);
145*c54f35caSApple OSS Distributions
146*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close test file: %s", testfile);
147*c54f35caSApple OSS Distributions }
148*c54f35caSApple OSS Distributions
149*c54f35caSApple OSS Distributions T_HELPER_DECL(open_rdwr_acquire_write_lease_EAGAIN, "Open file in O_RDWR mode and acquire write lease failed with EAGAIN")
150*c54f35caSApple OSS Distributions {
151*c54f35caSApple OSS Distributions char *testfile = argv[0];
152*c54f35caSApple OSS Distributions int err, fd;
153*c54f35caSApple OSS Distributions
154*c54f35caSApple OSS Distributions T_WITH_ERRNO;
155*c54f35caSApple OSS Distributions fd = open(testfile, O_RDWR, 0666);
156*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDWR: %s", testfile);
157*c54f35caSApple OSS Distributions
158*c54f35caSApple OSS Distributions T_WITH_ERRNO;
159*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_WRLCK);
160*c54f35caSApple OSS Distributions T_ASSERT_TRUE((err == -1) && (errno == EAGAIN), "Acquire write lease: %s",
161*c54f35caSApple OSS Distributions testfile);
162*c54f35caSApple OSS Distributions
163*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close test file: %s", testfile);
164*c54f35caSApple OSS Distributions }
165*c54f35caSApple OSS Distributions
166*c54f35caSApple OSS Distributions T_HELPER_DECL(open_rdonly_read_lease_release, "Open file in O_RDONLY mode, acquire read lease, and release lease upon NOTE_LEASE_RELEASE event")
167*c54f35caSApple OSS Distributions {
168*c54f35caSApple OSS Distributions struct kevent lease_kevent;
169*c54f35caSApple OSS Distributions struct timespec kevent_timeout;
170*c54f35caSApple OSS Distributions char *testfile = argv[0];
171*c54f35caSApple OSS Distributions int err, fd, kq;
172*c54f35caSApple OSS Distributions
173*c54f35caSApple OSS Distributions T_WITH_ERRNO;
174*c54f35caSApple OSS Distributions fd = open(testfile, O_RDONLY, 0666);
175*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDONLY: %s", testfile);
176*c54f35caSApple OSS Distributions
177*c54f35caSApple OSS Distributions T_WITH_ERRNO;
178*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_RDLCK);
179*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Acquire read lease: %s", testfile);
180*c54f35caSApple OSS Distributions
181*c54f35caSApple OSS Distributions kq = kqueue();
182*c54f35caSApple OSS Distributions T_ASSERT_NE(kq, -1, "Create kqueue");
183*c54f35caSApple OSS Distributions
184*c54f35caSApple OSS Distributions kevent_timeout.tv_sec = kevent_timeout.tv_nsec = 0;
185*c54f35caSApple OSS Distributions EV_SET(&lease_kevent, fd, EVFILT_VNODE, (EV_ADD | EV_ENABLE | EV_CLEAR),
186*c54f35caSApple OSS Distributions (NOTE_LEASE_DOWNGRADE | NOTE_LEASE_RELEASE), 0, (void *)testfile);
187*c54f35caSApple OSS Distributions err = kevent(kq, &lease_kevent, 1, NULL, 0, &kevent_timeout);
188*c54f35caSApple OSS Distributions T_ASSERT_GE(err, 0, "Register lease event on kq: %d", kq);
189*c54f35caSApple OSS Distributions
190*c54f35caSApple OSS Distributions kevent_timeout.tv_sec = 60;
191*c54f35caSApple OSS Distributions kevent_timeout.tv_nsec = 0;
192*c54f35caSApple OSS Distributions EV_SET(&lease_kevent, fd, EVFILT_VNODE, EV_CLEAR, 0, 0, 0);
193*c54f35caSApple OSS Distributions
194*c54f35caSApple OSS Distributions err = kevent(kq, NULL, 0, &lease_kevent, 1, &kevent_timeout);
195*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Listen for lease event on kq: %d", kq);
196*c54f35caSApple OSS Distributions
197*c54f35caSApple OSS Distributions if (err > 0) {
198*c54f35caSApple OSS Distributions T_ASSERT_EQ(lease_kevent.fflags, NOTE_LEASE_RELEASE,
199*c54f35caSApple OSS Distributions "Got lease event 0x%x", lease_kevent.fflags);
200*c54f35caSApple OSS Distributions
201*c54f35caSApple OSS Distributions T_WITH_ERRNO;
202*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_UNLCK);
203*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Release lease: %s", testfile);
204*c54f35caSApple OSS Distributions } else {
205*c54f35caSApple OSS Distributions T_FAIL("Timedout listening for lease event on kq: %d", kq);
206*c54f35caSApple OSS Distributions }
207*c54f35caSApple OSS Distributions }
208*c54f35caSApple OSS Distributions
209*c54f35caSApple OSS Distributions T_HELPER_DECL(open_rdonly_write_lease_downgrade, "Open file in O_RDONLY mode, acquire a write lease, and downgrade lease upon NOTE_LEASE_DOWNGRADE event")
210*c54f35caSApple OSS Distributions {
211*c54f35caSApple OSS Distributions struct kevent lease_kevent;
212*c54f35caSApple OSS Distributions struct timespec kevent_timeout;
213*c54f35caSApple OSS Distributions char *testfile = argv[0];
214*c54f35caSApple OSS Distributions int err, fd, kq;
215*c54f35caSApple OSS Distributions
216*c54f35caSApple OSS Distributions T_WITH_ERRNO;
217*c54f35caSApple OSS Distributions fd = open(testfile, O_RDONLY, 0666);
218*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDONLY: %s", testfile);
219*c54f35caSApple OSS Distributions
220*c54f35caSApple OSS Distributions T_WITH_ERRNO;
221*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_WRLCK);
222*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Acquire write lease: %s", testfile);
223*c54f35caSApple OSS Distributions
224*c54f35caSApple OSS Distributions kq = kqueue();
225*c54f35caSApple OSS Distributions T_ASSERT_NE(kq, -1, "Create kqueue");
226*c54f35caSApple OSS Distributions
227*c54f35caSApple OSS Distributions kevent_timeout.tv_sec = kevent_timeout.tv_nsec = 0;
228*c54f35caSApple OSS Distributions EV_SET(&lease_kevent, fd, EVFILT_VNODE, (EV_ADD | EV_ENABLE | EV_CLEAR),
229*c54f35caSApple OSS Distributions (NOTE_LEASE_DOWNGRADE | NOTE_LEASE_RELEASE), 0, (void *)testfile);
230*c54f35caSApple OSS Distributions err = kevent(kq, &lease_kevent, 1, NULL, 0, &kevent_timeout);
231*c54f35caSApple OSS Distributions T_ASSERT_GE(err, 0, "Register lease event on kq: %d", kq);
232*c54f35caSApple OSS Distributions
233*c54f35caSApple OSS Distributions kevent_timeout.tv_sec = 60;
234*c54f35caSApple OSS Distributions kevent_timeout.tv_nsec = 0;
235*c54f35caSApple OSS Distributions EV_SET(&lease_kevent, fd, EVFILT_VNODE, EV_CLEAR, 0, 0, 0);
236*c54f35caSApple OSS Distributions
237*c54f35caSApple OSS Distributions err = kevent(kq, NULL, 0, &lease_kevent, 1, &kevent_timeout);
238*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Listen for lease event on kq: %d", kq);
239*c54f35caSApple OSS Distributions
240*c54f35caSApple OSS Distributions if (err > 0) {
241*c54f35caSApple OSS Distributions T_ASSERT_EQ(lease_kevent.fflags, NOTE_LEASE_DOWNGRADE,
242*c54f35caSApple OSS Distributions "Got lease event 0x%x", lease_kevent.fflags);
243*c54f35caSApple OSS Distributions
244*c54f35caSApple OSS Distributions T_WITH_ERRNO;
245*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_RDLCK);
246*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Downgrade to read lease: %s", testfile);
247*c54f35caSApple OSS Distributions } else {
248*c54f35caSApple OSS Distributions T_FAIL("Timedout listening for lease event on kq: %d", kq);
249*c54f35caSApple OSS Distributions }
250*c54f35caSApple OSS Distributions }
251*c54f35caSApple OSS Distributions
252*c54f35caSApple OSS Distributions T_HELPER_DECL(open_rw_write_lease_downgrade, "Open file multiple times in O_RDWR mode, acquire a write lease, and downgrade lease upon NOTE_LEASE_DOWNGRADE event")
253*c54f35caSApple OSS Distributions {
254*c54f35caSApple OSS Distributions struct kevent lease_kevent;
255*c54f35caSApple OSS Distributions struct timespec kevent_timeout;
256*c54f35caSApple OSS Distributions char *testfile = argv[0];
257*c54f35caSApple OSS Distributions int err, rw_fd1, rw_fd2, fd, kq;
258*c54f35caSApple OSS Distributions
259*c54f35caSApple OSS Distributions T_WITH_ERRNO;
260*c54f35caSApple OSS Distributions rw_fd1 = open(testfile, O_RDWR, 0666);
261*c54f35caSApple OSS Distributions T_ASSERT_NE(rw_fd1, -1, "Open test fi1e in O_RDWR: %s", testfile);
262*c54f35caSApple OSS Distributions
263*c54f35caSApple OSS Distributions T_WITH_ERRNO;
264*c54f35caSApple OSS Distributions rw_fd2 = open(testfile, O_RDWR, 0666);
265*c54f35caSApple OSS Distributions T_ASSERT_NE(rw_fd2, -1, "Open test fi1e in O_RDWR: %s", testfile);
266*c54f35caSApple OSS Distributions
267*c54f35caSApple OSS Distributions T_WITH_ERRNO;
268*c54f35caSApple OSS Distributions fd = open(testfile, O_EVTONLY, 0666);
269*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_EVTONLY: %s", testfile);
270*c54f35caSApple OSS Distributions
271*c54f35caSApple OSS Distributions T_WITH_ERRNO;
272*c54f35caSApple OSS Distributions /* Pass in the expected open counts when placing a write lease. */
273*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_SETLEASE_ARG(F_WRLCK, 3));
274*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Acquire write lease: %s", testfile);
275*c54f35caSApple OSS Distributions
276*c54f35caSApple OSS Distributions kq = kqueue();
277*c54f35caSApple OSS Distributions T_ASSERT_NE(kq, -1, "Create kqueue");
278*c54f35caSApple OSS Distributions
279*c54f35caSApple OSS Distributions kevent_timeout.tv_sec = kevent_timeout.tv_nsec = 0;
280*c54f35caSApple OSS Distributions EV_SET(&lease_kevent, fd, EVFILT_VNODE, (EV_ADD | EV_ENABLE | EV_CLEAR),
281*c54f35caSApple OSS Distributions (NOTE_LEASE_DOWNGRADE | NOTE_LEASE_RELEASE), 0, (void *)testfile);
282*c54f35caSApple OSS Distributions err = kevent(kq, &lease_kevent, 1, NULL, 0, &kevent_timeout);
283*c54f35caSApple OSS Distributions T_ASSERT_GE(err, 0, "Register lease event on kq: %d", kq);
284*c54f35caSApple OSS Distributions
285*c54f35caSApple OSS Distributions kevent_timeout.tv_sec = 60;
286*c54f35caSApple OSS Distributions kevent_timeout.tv_nsec = 0;
287*c54f35caSApple OSS Distributions EV_SET(&lease_kevent, fd, EVFILT_VNODE, EV_CLEAR, 0, 0, 0);
288*c54f35caSApple OSS Distributions
289*c54f35caSApple OSS Distributions err = kevent(kq, NULL, 0, &lease_kevent, 1, &kevent_timeout);
290*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Listen for lease event on kq: %d", kq);
291*c54f35caSApple OSS Distributions
292*c54f35caSApple OSS Distributions if (err > 0) {
293*c54f35caSApple OSS Distributions T_ASSERT_EQ(lease_kevent.fflags, NOTE_LEASE_DOWNGRADE,
294*c54f35caSApple OSS Distributions "Got lease event 0x%x", lease_kevent.fflags);
295*c54f35caSApple OSS Distributions
296*c54f35caSApple OSS Distributions T_WITH_ERRNO;
297*c54f35caSApple OSS Distributions /* Pass in the expected write counts when placing a read lease. */
298*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_SETLEASE_ARG(F_RDLCK, 2));
299*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Downgrade to read lease: %s", testfile);
300*c54f35caSApple OSS Distributions } else {
301*c54f35caSApple OSS Distributions T_FAIL("Timedout listening for lease event on kq: %d", kq);
302*c54f35caSApple OSS Distributions }
303*c54f35caSApple OSS Distributions }
304*c54f35caSApple OSS Distributions
305*c54f35caSApple OSS Distributions T_HELPER_DECL(open_rdonly_read_lease_timedout, "Open file in O_RDONLY mode, acquire read lease, and hold lease beyond lease break timeout upon NOTE_LEASE_RELEASE event", T_META_ASROOT(true))
306*c54f35caSApple OSS Distributions {
307*c54f35caSApple OSS Distributions struct kevent lease_kevent;
308*c54f35caSApple OSS Distributions struct timespec kevent_timeout;
309*c54f35caSApple OSS Distributions uint32_t val, new_val;
310*c54f35caSApple OSS Distributions size_t val_len, new_val_len;
311*c54f35caSApple OSS Distributions char *testfile = argv[0];
312*c54f35caSApple OSS Distributions int err, fd, kq;
313*c54f35caSApple OSS Distributions
314*c54f35caSApple OSS Distributions if (!is_development_kernel()) {
315*c54f35caSApple OSS Distributions T_SKIP("Skipping test on release kernel");
316*c54f35caSApple OSS Distributions }
317*c54f35caSApple OSS Distributions
318*c54f35caSApple OSS Distributions T_WITH_ERRNO;
319*c54f35caSApple OSS Distributions fd = open(testfile, O_RDONLY, 0666);
320*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDONLY: %s", testfile);
321*c54f35caSApple OSS Distributions
322*c54f35caSApple OSS Distributions T_WITH_ERRNO;
323*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_RDLCK);
324*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Acquire read lease: %s", testfile);
325*c54f35caSApple OSS Distributions
326*c54f35caSApple OSS Distributions new_val = 10;
327*c54f35caSApple OSS Distributions new_val_len = val_len = sizeof(uint32_t);
328*c54f35caSApple OSS Distributions err = sysctlbyname("vfs.lease.break_timeout", (void *)&val, &val_len,
329*c54f35caSApple OSS Distributions (void *)&new_val, new_val_len);
330*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Change vfs.lease.break_timeout to %d secs", new_val);
331*c54f35caSApple OSS Distributions
332*c54f35caSApple OSS Distributions kq = kqueue();
333*c54f35caSApple OSS Distributions T_ASSERT_NE(kq, -1, "Create kqueue");
334*c54f35caSApple OSS Distributions
335*c54f35caSApple OSS Distributions kevent_timeout.tv_sec = kevent_timeout.tv_nsec = 0;
336*c54f35caSApple OSS Distributions EV_SET(&lease_kevent, fd, EVFILT_VNODE, (EV_ADD | EV_ENABLE | EV_CLEAR),
337*c54f35caSApple OSS Distributions (NOTE_LEASE_DOWNGRADE | NOTE_LEASE_RELEASE), 0, (void *)testfile);
338*c54f35caSApple OSS Distributions err = kevent(kq, &lease_kevent, 1, NULL, 0, &kevent_timeout);
339*c54f35caSApple OSS Distributions T_ASSERT_GE(err, 0, "Register lease event on kq: %d", kq);
340*c54f35caSApple OSS Distributions
341*c54f35caSApple OSS Distributions kevent_timeout.tv_sec = 30;
342*c54f35caSApple OSS Distributions kevent_timeout.tv_nsec = 0;
343*c54f35caSApple OSS Distributions EV_SET(&lease_kevent, fd, EVFILT_VNODE, EV_CLEAR, 0, 0, 0);
344*c54f35caSApple OSS Distributions
345*c54f35caSApple OSS Distributions err = kevent(kq, NULL, 0, &lease_kevent, 1, &kevent_timeout);
346*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Listen for lease event on kq: %d", kq);
347*c54f35caSApple OSS Distributions
348*c54f35caSApple OSS Distributions if (err > 0) {
349*c54f35caSApple OSS Distributions T_ASSERT_EQ(lease_kevent.fflags, NOTE_LEASE_RELEASE,
350*c54f35caSApple OSS Distributions "Got lease event 0x%x", lease_kevent.fflags);
351*c54f35caSApple OSS Distributions
352*c54f35caSApple OSS Distributions /* Sleep to force lease break timedout. */
353*c54f35caSApple OSS Distributions T_LOG("Sleep for %d secs to force lease break timedout", new_val + 5);
354*c54f35caSApple OSS Distributions sleep(new_val + 5);
355*c54f35caSApple OSS Distributions } else {
356*c54f35caSApple OSS Distributions T_FAIL("Timedout listening for lease event on kq: %d", kq);
357*c54f35caSApple OSS Distributions }
358*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Change vfs.lease.break_timeout to %d secs", new_val);
359*c54f35caSApple OSS Distributions }
360*c54f35caSApple OSS Distributions
361*c54f35caSApple OSS Distributions T_HELPER_DECL(open_rdonly_dir_read_lease, "Open directory in O_RDONLY mode, acquire read lease, and release lease upon NOTE_LEASE_RELEASE event")
362*c54f35caSApple OSS Distributions {
363*c54f35caSApple OSS Distributions struct kevent lease_kevent;
364*c54f35caSApple OSS Distributions struct timespec kevent_timeout;
365*c54f35caSApple OSS Distributions char *testdir = argv[1];
366*c54f35caSApple OSS Distributions int err, dir_fd, kq;
367*c54f35caSApple OSS Distributions
368*c54f35caSApple OSS Distributions T_WITH_ERRNO;
369*c54f35caSApple OSS Distributions dir_fd = open(testdir, O_RDONLY);
370*c54f35caSApple OSS Distributions T_ASSERT_NE(dir_fd, -1, "Open test dir in O_RDONLY: %s", testdir);
371*c54f35caSApple OSS Distributions
372*c54f35caSApple OSS Distributions kq = kqueue();
373*c54f35caSApple OSS Distributions T_ASSERT_NE(kq, -1, "Create kqueue");
374*c54f35caSApple OSS Distributions
375*c54f35caSApple OSS Distributions retry:
376*c54f35caSApple OSS Distributions T_WITH_ERRNO;
377*c54f35caSApple OSS Distributions err = fcntl(dir_fd, F_SETLEASE, F_RDLCK);
378*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Acquire read lease: %s", testdir);
379*c54f35caSApple OSS Distributions
380*c54f35caSApple OSS Distributions kevent_timeout.tv_sec = kevent_timeout.tv_nsec = 0;
381*c54f35caSApple OSS Distributions EV_SET(&lease_kevent, dir_fd, EVFILT_VNODE, (EV_ADD | EV_ENABLE | EV_CLEAR),
382*c54f35caSApple OSS Distributions (NOTE_LEASE_DOWNGRADE | NOTE_LEASE_RELEASE), 0, (void *)testdir);
383*c54f35caSApple OSS Distributions err = kevent(kq, &lease_kevent, 1, NULL, 0, &kevent_timeout);
384*c54f35caSApple OSS Distributions T_ASSERT_GE(err, 0, "Register lease event on kq: %d", kq);
385*c54f35caSApple OSS Distributions
386*c54f35caSApple OSS Distributions kevent_timeout.tv_sec = 30;
387*c54f35caSApple OSS Distributions kevent_timeout.tv_nsec = 0;
388*c54f35caSApple OSS Distributions EV_SET(&lease_kevent, dir_fd, EVFILT_VNODE, EV_CLEAR, 0, 0, 0);
389*c54f35caSApple OSS Distributions
390*c54f35caSApple OSS Distributions err = kevent(kq, NULL, 0, &lease_kevent, 1, &kevent_timeout);
391*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Listen for lease event on kq: %d", kq);
392*c54f35caSApple OSS Distributions
393*c54f35caSApple OSS Distributions if (err > 0) {
394*c54f35caSApple OSS Distributions T_ASSERT_EQ(lease_kevent.fflags, NOTE_LEASE_RELEASE,
395*c54f35caSApple OSS Distributions "Got lease event 0x%x", lease_kevent.fflags);
396*c54f35caSApple OSS Distributions
397*c54f35caSApple OSS Distributions T_WITH_ERRNO;
398*c54f35caSApple OSS Distributions err = fcntl(dir_fd, F_SETLEASE, F_UNLCK);
399*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Release lease: %s", testdir);
400*c54f35caSApple OSS Distributions
401*c54f35caSApple OSS Distributions /*
402*c54f35caSApple OSS Distributions * Retry until we got no more events (kevent timedout) which means
403*c54f35caSApple OSS Distributions * the other helper is done with all the tests.
404*c54f35caSApple OSS Distributions */
405*c54f35caSApple OSS Distributions goto retry;
406*c54f35caSApple OSS Distributions } else {
407*c54f35caSApple OSS Distributions T_FAIL("Timedout listening for lease event on kq: %d", kq);
408*c54f35caSApple OSS Distributions }
409*c54f35caSApple OSS Distributions }
410*c54f35caSApple OSS Distributions
411*c54f35caSApple OSS Distributions T_HELPER_DECL(open_rdwr, "Open file in O_RDWR mode")
412*c54f35caSApple OSS Distributions {
413*c54f35caSApple OSS Distributions char *testfile = argv[0];
414*c54f35caSApple OSS Distributions int fd;
415*c54f35caSApple OSS Distributions
416*c54f35caSApple OSS Distributions /* wait for the other helper to be in ready state */
417*c54f35caSApple OSS Distributions sleep(1);
418*c54f35caSApple OSS Distributions
419*c54f35caSApple OSS Distributions T_WITH_ERRNO;
420*c54f35caSApple OSS Distributions fd = open(testfile, O_RDWR, 0666);
421*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDWR: %s", testfile);
422*c54f35caSApple OSS Distributions }
423*c54f35caSApple OSS Distributions
424*c54f35caSApple OSS Distributions T_HELPER_DECL(open_rdonly, "Open file in O_RDONLY mode")
425*c54f35caSApple OSS Distributions {
426*c54f35caSApple OSS Distributions char *testfile = argv[0];
427*c54f35caSApple OSS Distributions int fd;
428*c54f35caSApple OSS Distributions
429*c54f35caSApple OSS Distributions /* wait for the other helper to be in ready state */
430*c54f35caSApple OSS Distributions sleep(1);
431*c54f35caSApple OSS Distributions
432*c54f35caSApple OSS Distributions T_WITH_ERRNO;
433*c54f35caSApple OSS Distributions fd = open(testfile, O_RDONLY, 0666);
434*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDONLY: %s", testfile);
435*c54f35caSApple OSS Distributions }
436*c54f35caSApple OSS Distributions
437*c54f35caSApple OSS Distributions T_HELPER_DECL(truncate, "Truncate file")
438*c54f35caSApple OSS Distributions {
439*c54f35caSApple OSS Distributions char *testfile = argv[0];
440*c54f35caSApple OSS Distributions int err;
441*c54f35caSApple OSS Distributions
442*c54f35caSApple OSS Distributions /* wait for the other helper to be in ready state */
443*c54f35caSApple OSS Distributions sleep(1);
444*c54f35caSApple OSS Distributions
445*c54f35caSApple OSS Distributions T_WITH_ERRNO;
446*c54f35caSApple OSS Distributions err = truncate(testfile, 0);
447*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Truncate test fi1e: %s", testfile);
448*c54f35caSApple OSS Distributions }
449*c54f35caSApple OSS Distributions
450*c54f35caSApple OSS Distributions T_HELPER_DECL(open_rdonly_request_read_range_lock, "Open file in O_RDONLY mode and request byte range lock")
451*c54f35caSApple OSS Distributions {
452*c54f35caSApple OSS Distributions struct flock lreq;
453*c54f35caSApple OSS Distributions char *testfile = argv[0];
454*c54f35caSApple OSS Distributions int err, fd;
455*c54f35caSApple OSS Distributions
456*c54f35caSApple OSS Distributions /* wait for the other helper to be in ready state */
457*c54f35caSApple OSS Distributions sleep(1);
458*c54f35caSApple OSS Distributions
459*c54f35caSApple OSS Distributions T_WITH_ERRNO;
460*c54f35caSApple OSS Distributions fd = open(testfile, O_RDONLY, 0666);
461*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDONLY: %s", testfile);
462*c54f35caSApple OSS Distributions
463*c54f35caSApple OSS Distributions T_WITH_ERRNO;
464*c54f35caSApple OSS Distributions lreq.l_start = 0;
465*c54f35caSApple OSS Distributions lreq.l_len = 0;
466*c54f35caSApple OSS Distributions lreq.l_type = F_RDLCK;
467*c54f35caSApple OSS Distributions lreq.l_whence = 0;
468*c54f35caSApple OSS Distributions
469*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLK, &lreq);
470*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Acquire read range lock on test fi1e: %s", testfile);
471*c54f35caSApple OSS Distributions
472*c54f35caSApple OSS Distributions T_WITH_ERRNO;
473*c54f35caSApple OSS Distributions lreq.l_start = 0;
474*c54f35caSApple OSS Distributions lreq.l_len = 0;
475*c54f35caSApple OSS Distributions lreq.l_type = F_UNLCK;
476*c54f35caSApple OSS Distributions lreq.l_whence = 0;
477*c54f35caSApple OSS Distributions
478*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLK, &lreq);
479*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Release read range lock on test fi1e: %s", testfile);
480*c54f35caSApple OSS Distributions }
481*c54f35caSApple OSS Distributions
482*c54f35caSApple OSS Distributions T_HELPER_DECL(file_syscalls, "Call file syscalls")
483*c54f35caSApple OSS Distributions {
484*c54f35caSApple OSS Distributions char destfile[MAXPATHLEN];
485*c54f35caSApple OSS Distributions struct attrlist attrlist;
486*c54f35caSApple OSS Distributions char *xattr_key = "com.apple.xattr_test";
487*c54f35caSApple OSS Distributions char xattr_val[] = "xattr_foo";
488*c54f35caSApple OSS Distributions char *testfile = argv[0];
489*c54f35caSApple OSS Distributions uint32_t flags;
490*c54f35caSApple OSS Distributions int err, fd;
491*c54f35caSApple OSS Distributions
492*c54f35caSApple OSS Distributions /* wait for the other helper to be in ready state */
493*c54f35caSApple OSS Distributions sleep(1);
494*c54f35caSApple OSS Distributions
495*c54f35caSApple OSS Distributions T_WITH_ERRNO;
496*c54f35caSApple OSS Distributions fd = open(testfile, O_RDWR | O_CREAT, 0666);
497*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDWR|O_CREAT: %s", testfile);
498*c54f35caSApple OSS Distributions sleep(1);
499*c54f35caSApple OSS Distributions
500*c54f35caSApple OSS Distributions /* Test ftruncate (fd needs to be opened with write mode) */
501*c54f35caSApple OSS Distributions T_WITH_ERRNO;
502*c54f35caSApple OSS Distributions err = ftruncate(fd, 0);
503*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "fdtruncate: %s", testfile);
504*c54f35caSApple OSS Distributions sleep(1);
505*c54f35caSApple OSS Distributions
506*c54f35caSApple OSS Distributions /* Test (p)write. */
507*c54f35caSApple OSS Distributions T_WITH_ERRNO;
508*c54f35caSApple OSS Distributions err = (int)write(fd, destfile, sizeof(destfile));
509*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "write: %s", testfile);
510*c54f35caSApple OSS Distributions sleep(1);
511*c54f35caSApple OSS Distributions
512*c54f35caSApple OSS Distributions T_WITH_ERRNO;
513*c54f35caSApple OSS Distributions err = (int)pwrite(fd, destfile, sizeof(destfile), sizeof(destfile));
514*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "write: %s", testfile);
515*c54f35caSApple OSS Distributions sleep(1);
516*c54f35caSApple OSS Distributions
517*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close test file: %s", testfile);
518*c54f35caSApple OSS Distributions
519*c54f35caSApple OSS Distributions T_WITH_ERRNO;
520*c54f35caSApple OSS Distributions fd = open(testfile, O_RDONLY, 0666);
521*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDONLY: %s", testfile);
522*c54f35caSApple OSS Distributions
523*c54f35caSApple OSS Distributions /* Test (f)chflags syscall */
524*c54f35caSApple OSS Distributions T_WITH_ERRNO;
525*c54f35caSApple OSS Distributions err = chflags(testfile, 0);
526*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "chflags: %s", testfile);
527*c54f35caSApple OSS Distributions sleep(1);
528*c54f35caSApple OSS Distributions
529*c54f35caSApple OSS Distributions T_WITH_ERRNO;
530*c54f35caSApple OSS Distributions err = fchflags(fd, 0);
531*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "fchflags: %s", testfile);
532*c54f35caSApple OSS Distributions sleep(1);
533*c54f35caSApple OSS Distributions
534*c54f35caSApple OSS Distributions /* Test (f)chmod syscall */
535*c54f35caSApple OSS Distributions T_WITH_ERRNO;
536*c54f35caSApple OSS Distributions err = chmod(testfile, S_IRWXU);
537*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "chmod: %s", testfile);
538*c54f35caSApple OSS Distributions sleep(1);
539*c54f35caSApple OSS Distributions
540*c54f35caSApple OSS Distributions T_WITH_ERRNO;
541*c54f35caSApple OSS Distributions err = fchmod(fd, S_IRWXU);
542*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "fchmod: %s", testfile);
543*c54f35caSApple OSS Distributions sleep(1);
544*c54f35caSApple OSS Distributions
545*c54f35caSApple OSS Distributions /* Test clonefile */
546*c54f35caSApple OSS Distributions snprintf(destfile, sizeof(destfile), "%s.%d", testfile, rand());
547*c54f35caSApple OSS Distributions T_WITH_ERRNO;
548*c54f35caSApple OSS Distributions err = clonefile(testfile, destfile, CLONE_NOFOLLOW);
549*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "clonefile src: %s dest: %s", testfile, destfile);
550*c54f35caSApple OSS Distributions sleep(1);
551*c54f35caSApple OSS Distributions
552*c54f35caSApple OSS Distributions /* Test copyfile */
553*c54f35caSApple OSS Distributions T_WITH_ERRNO;
554*c54f35caSApple OSS Distributions err = copyfile(testfile, destfile, NULL, COPYFILE_DATA | COPYFILE_STAT);
555*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "copyfile src: %s dest: %s", testfile, destfile);
556*c54f35caSApple OSS Distributions sleep(1);
557*c54f35caSApple OSS Distributions
558*c54f35caSApple OSS Distributions /* Test unlink */
559*c54f35caSApple OSS Distributions T_WITH_ERRNO;
560*c54f35caSApple OSS Distributions err = unlink(destfile);
561*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "unlink: %s", destfile);
562*c54f35caSApple OSS Distributions sleep(1);
563*c54f35caSApple OSS Distributions
564*c54f35caSApple OSS Distributions /* Test (f)setxattr and (f)removexattr */
565*c54f35caSApple OSS Distributions T_WITH_ERRNO;
566*c54f35caSApple OSS Distributions err = setxattr(testfile, xattr_key, &xattr_val[0], sizeof(xattr_val), 0, 0);
567*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "setxattr: %s", testfile);
568*c54f35caSApple OSS Distributions sleep(1);
569*c54f35caSApple OSS Distributions
570*c54f35caSApple OSS Distributions T_WITH_ERRNO;
571*c54f35caSApple OSS Distributions err = removexattr(testfile, xattr_key, 0);
572*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "removexattr: %s", testfile);
573*c54f35caSApple OSS Distributions sleep(1);
574*c54f35caSApple OSS Distributions
575*c54f35caSApple OSS Distributions T_WITH_ERRNO;
576*c54f35caSApple OSS Distributions err = fsetxattr(fd, xattr_key, &xattr_val[0], sizeof(xattr_val), 0, 0);
577*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "fsetxattr: %s", testfile);
578*c54f35caSApple OSS Distributions sleep(1);
579*c54f35caSApple OSS Distributions
580*c54f35caSApple OSS Distributions T_WITH_ERRNO;
581*c54f35caSApple OSS Distributions err = fremovexattr(fd, xattr_key, 0);
582*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "fremovexattr: %s", testfile);
583*c54f35caSApple OSS Distributions sleep(1);
584*c54f35caSApple OSS Distributions
585*c54f35caSApple OSS Distributions /* Test (f)setattrlist */
586*c54f35caSApple OSS Distributions flags = 0;
587*c54f35caSApple OSS Distributions memset(&attrlist, 0, sizeof(attrlist));
588*c54f35caSApple OSS Distributions attrlist.bitmapcount = ATTR_BIT_MAP_COUNT;
589*c54f35caSApple OSS Distributions attrlist.commonattr = (ATTR_CMN_FLAGS);
590*c54f35caSApple OSS Distributions
591*c54f35caSApple OSS Distributions T_WITH_ERRNO;
592*c54f35caSApple OSS Distributions err = setattrlist(testfile, &attrlist, &flags, sizeof(flags), 0);
593*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "setattrlist: %s", testfile);
594*c54f35caSApple OSS Distributions sleep(1);
595*c54f35caSApple OSS Distributions
596*c54f35caSApple OSS Distributions T_WITH_ERRNO;
597*c54f35caSApple OSS Distributions err = fsetattrlist(fd, &attrlist, &flags, sizeof(flags), 0);
598*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "fsetattrlist: %s", testfile);
599*c54f35caSApple OSS Distributions sleep(1);
600*c54f35caSApple OSS Distributions
601*c54f35caSApple OSS Distributions /* Test truncate */
602*c54f35caSApple OSS Distributions T_WITH_ERRNO;
603*c54f35caSApple OSS Distributions err = truncate(testfile, 0);
604*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "truncate: %s", testfile);
605*c54f35caSApple OSS Distributions sleep(1);
606*c54f35caSApple OSS Distributions
607*c54f35caSApple OSS Distributions /* Test (f)utimes */
608*c54f35caSApple OSS Distributions T_WITH_ERRNO;
609*c54f35caSApple OSS Distributions err = utimes(testfile, NULL);
610*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "utimes: %s", testfile);
611*c54f35caSApple OSS Distributions sleep(1);
612*c54f35caSApple OSS Distributions
613*c54f35caSApple OSS Distributions T_WITH_ERRNO;
614*c54f35caSApple OSS Distributions err = futimes(fd, NULL);
615*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "futimes: %s", testfile);
616*c54f35caSApple OSS Distributions
617*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close test file: %s", testfile);
618*c54f35caSApple OSS Distributions }
619*c54f35caSApple OSS Distributions
620*c54f35caSApple OSS Distributions T_HELPER_DECL(file_syscalls_2, "Call file syscalls (mknod)", T_META_ASROOT(true))
621*c54f35caSApple OSS Distributions {
622*c54f35caSApple OSS Distributions char destfile[MAXPATHLEN];
623*c54f35caSApple OSS Distributions char *testfile = argv[0];
624*c54f35caSApple OSS Distributions int err;
625*c54f35caSApple OSS Distributions
626*c54f35caSApple OSS Distributions snprintf(destfile, sizeof(destfile), "%s.%d", testfile, rand());
627*c54f35caSApple OSS Distributions
628*c54f35caSApple OSS Distributions /* wait for the other helper to be in ready state */
629*c54f35caSApple OSS Distributions sleep(1);
630*c54f35caSApple OSS Distributions
631*c54f35caSApple OSS Distributions /* Test mknod */
632*c54f35caSApple OSS Distributions T_WITH_ERRNO;
633*c54f35caSApple OSS Distributions err = mknod(destfile, (S_IFCHR | S_IRWXU), 0);
634*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "mknod: %s", destfile);
635*c54f35caSApple OSS Distributions sleep(1);
636*c54f35caSApple OSS Distributions
637*c54f35caSApple OSS Distributions /* Test unlink */
638*c54f35caSApple OSS Distributions T_WITH_ERRNO;
639*c54f35caSApple OSS Distributions err = unlink(destfile);
640*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "unlink: %s", destfile);
641*c54f35caSApple OSS Distributions }
642*c54f35caSApple OSS Distributions
643*c54f35caSApple OSS Distributions /*
644*c54f35caSApple OSS Distributions * Test acquire, downgrade, and release lease.
645*c54f35caSApple OSS Distributions * a. Process A opens the file in O_RDONLY mode
646*c54f35caSApple OSS Distributions * b. Process A acquires write lease
647*c54f35caSApple OSS Distributions * c. Process A downgrade from write to read lease
648*c54f35caSApple OSS Distributions * d. Process A release lease
649*c54f35caSApple OSS Distributions *
650*c54f35caSApple OSS Distributions * Result: Lease operations should succeed as expected.
651*c54f35caSApple OSS Distributions */
652*c54f35caSApple OSS Distributions T_DECL(acquire_downgrade_release, "Test acquire, downgrade and release lease", T_META_ENABLED(TARGET_OS_OSX))
653*c54f35caSApple OSS Distributions {
654*c54f35caSApple OSS Distributions int err, fd;
655*c54f35caSApple OSS Distributions
656*c54f35caSApple OSS Distributions create_test_file();
657*c54f35caSApple OSS Distributions
658*c54f35caSApple OSS Distributions T_WITH_ERRNO;
659*c54f35caSApple OSS Distributions fd = open(g_testfile, O_RDONLY, 0666);
660*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDONLY: %s", g_testfile);
661*c54f35caSApple OSS Distributions
662*c54f35caSApple OSS Distributions T_WITH_ERRNO;
663*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_WRLCK);
664*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Acquire write lease: %s", g_testfile);
665*c54f35caSApple OSS Distributions
666*c54f35caSApple OSS Distributions T_WITH_ERRNO;
667*c54f35caSApple OSS Distributions err = fcntl(fd, F_GETLEASE);
668*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_WRLCK, "Retrieve lease: %s", g_testfile);
669*c54f35caSApple OSS Distributions
670*c54f35caSApple OSS Distributions T_WITH_ERRNO;
671*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_RDLCK);
672*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Downgrade to read lease: %s", g_testfile);
673*c54f35caSApple OSS Distributions
674*c54f35caSApple OSS Distributions T_WITH_ERRNO;
675*c54f35caSApple OSS Distributions err = fcntl(fd, F_GETLEASE);
676*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_RDLCK, "Retrieve lease: %s", g_testfile);
677*c54f35caSApple OSS Distributions
678*c54f35caSApple OSS Distributions T_WITH_ERRNO;
679*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_UNLCK);
680*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Release lease: %s", g_testfile);
681*c54f35caSApple OSS Distributions
682*c54f35caSApple OSS Distributions T_WITH_ERRNO;
683*c54f35caSApple OSS Distributions err = fcntl(fd, F_GETLEASE);
684*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_UNLCK, "Retrieve lease: %s", g_testfile);
685*c54f35caSApple OSS Distributions
686*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close test file: %s", g_testfile);
687*c54f35caSApple OSS Distributions }
688*c54f35caSApple OSS Distributions
689*c54f35caSApple OSS Distributions /*
690*c54f35caSApple OSS Distributions * Test acquire lease failure due to open conflicts.
691*c54f35caSApple OSS Distributions * a. Process A opens the file in O_RDWR mode
692*c54f35caSApple OSS Distributions * b. Process B opens the file in O_RDONLY mode
693*c54f35caSApple OSS Distributions * c. Process B tries to acquire read lease
694*c54f35caSApple OSS Distributions *
695*c54f35caSApple OSS Distributions * Result: Process B should fail to acquire read lease with EAGAIN due to the
696*c54f35caSApple OSS Distributions * file has been opened with write mode (O_RDWR).
697*c54f35caSApple OSS Distributions */
698*c54f35caSApple OSS Distributions T_DECL(open_conflict_1, "Test acquire read lease failure due to open conflicts", T_META_ENABLED(TARGET_OS_OSX))
699*c54f35caSApple OSS Distributions {
700*c54f35caSApple OSS Distributions const char *helper_test_names[] = {"open_rdonly_acquire_read_lease_EAGAIN"};
701*c54f35caSApple OSS Distributions int fd;
702*c54f35caSApple OSS Distributions
703*c54f35caSApple OSS Distributions create_test_file();
704*c54f35caSApple OSS Distributions
705*c54f35caSApple OSS Distributions T_WITH_ERRNO;
706*c54f35caSApple OSS Distributions fd = open(g_testfile, O_RDWR, 0666);
707*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDWR: %s", g_testfile);
708*c54f35caSApple OSS Distributions
709*c54f35caSApple OSS Distributions run_helpers(helper_test_names, 1);
710*c54f35caSApple OSS Distributions
711*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close test file: %s", g_testfile);
712*c54f35caSApple OSS Distributions }
713*c54f35caSApple OSS Distributions
714*c54f35caSApple OSS Distributions /*
715*c54f35caSApple OSS Distributions * Test acquire lease failure due to open conflicts.
716*c54f35caSApple OSS Distributions * a. Process A opens the file in O_RDONLY mode
717*c54f35caSApple OSS Distributions * b. Process B opens the file in O_RDWR mode
718*c54f35caSApple OSS Distributions * c. Process B tries to acquire write lease
719*c54f35caSApple OSS Distributions *
720*c54f35caSApple OSS Distributions * Result: Process B should fail to acquire write lease with EAGAIN due to the
721*c54f35caSApple OSS Distributions * file has been opened elsewhere.
722*c54f35caSApple OSS Distributions */
723*c54f35caSApple OSS Distributions T_DECL(open_conflict_2, "Test acquire write lease failure due to open conflicts", T_META_ENABLED(TARGET_OS_OSX))
724*c54f35caSApple OSS Distributions {
725*c54f35caSApple OSS Distributions const char *helper_test_names[] = {"open_rdwr_acquire_write_lease_EAGAIN"};
726*c54f35caSApple OSS Distributions int fd;
727*c54f35caSApple OSS Distributions
728*c54f35caSApple OSS Distributions create_test_file();
729*c54f35caSApple OSS Distributions
730*c54f35caSApple OSS Distributions T_WITH_ERRNO;
731*c54f35caSApple OSS Distributions fd = open(g_testfile, O_RDONLY, 0666);
732*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDONLY: %s", g_testfile);
733*c54f35caSApple OSS Distributions
734*c54f35caSApple OSS Distributions run_helpers(helper_test_names, 1);
735*c54f35caSApple OSS Distributions
736*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close test file: %s", g_testfile);
737*c54f35caSApple OSS Distributions }
738*c54f35caSApple OSS Distributions
739*c54f35caSApple OSS Distributions /*
740*c54f35caSApple OSS Distributions * Test multiple processes put a read lease on the file.
741*c54f35caSApple OSS Distributions * a. Process A opens the file with O_RDONLY mode and place a read lease
742*c54f35caSApple OSS Distributions * b. Process B opens the file with O_RDONLY mode and place a read lease
743*c54f35caSApple OSS Distributions *
744*c54f35caSApple OSS Distributions * Result: Both processes should succeed in placing read lease on the file.
745*c54f35caSApple OSS Distributions */
746*c54f35caSApple OSS Distributions T_DECL(multiple_read_leases, "Test multiple processes put a read lease on the file", T_META_ENABLED(TARGET_OS_OSX))
747*c54f35caSApple OSS Distributions {
748*c54f35caSApple OSS Distributions const char *helper_test_names[] = {"open_rdonly_acquire_read_lease_succeed",
749*c54f35caSApple OSS Distributions "open_rdonly_acquire_read_lease_succeed"};
750*c54f35caSApple OSS Distributions
751*c54f35caSApple OSS Distributions create_test_file();
752*c54f35caSApple OSS Distributions run_helpers(helper_test_names, 2);
753*c54f35caSApple OSS Distributions }
754*c54f35caSApple OSS Distributions
755*c54f35caSApple OSS Distributions /*
756*c54f35caSApple OSS Distributions * Test acquire and release lease when there is no lease is in place.
757*c54f35caSApple OSS Distributions *
758*c54f35caSApple OSS Distributions * Result: Acquire lease should succeed with F_UNLCK (no lease).
759*c54f35caSApple OSS Distributions * Release lease should fail with ENOLCK.
760*c54f35caSApple OSS Distributions */
761*c54f35caSApple OSS Distributions T_DECL(acquire_release_no_lease, "Test acquire and release lease when there is no lease is in place", T_META_ENABLED(TARGET_OS_OSX))
762*c54f35caSApple OSS Distributions {
763*c54f35caSApple OSS Distributions int err, fd;
764*c54f35caSApple OSS Distributions
765*c54f35caSApple OSS Distributions create_test_file();
766*c54f35caSApple OSS Distributions
767*c54f35caSApple OSS Distributions T_WITH_ERRNO;
768*c54f35caSApple OSS Distributions fd = open(g_testfile, O_RDWR, 0666);
769*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDWR: %s", g_testfile);
770*c54f35caSApple OSS Distributions
771*c54f35caSApple OSS Distributions T_WITH_ERRNO;
772*c54f35caSApple OSS Distributions err = fcntl(fd, F_GETLEASE);
773*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_UNLCK, "Retrieve lease: %s", g_testfile);
774*c54f35caSApple OSS Distributions
775*c54f35caSApple OSS Distributions T_WITH_ERRNO;
776*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_UNLCK);
777*c54f35caSApple OSS Distributions T_ASSERT_TRUE((err == -1) && (errno == ENOLCK), "Release lease: %s",
778*c54f35caSApple OSS Distributions g_testfile);
779*c54f35caSApple OSS Distributions }
780*c54f35caSApple OSS Distributions
781*c54f35caSApple OSS Distributions /*
782*c54f35caSApple OSS Distributions * Test acquire, release and retrieve lease on non-regular file.
783*c54f35caSApple OSS Distributions *
784*c54f35caSApple OSS Distributions * Result: Acquire, release and retrieve lease should fail with EBADF.
785*c54f35caSApple OSS Distributions */
786*c54f35caSApple OSS Distributions T_DECL(acquire_release_retrieve_non_file, "Test acquire, release and retrieve lease on non-regular file", T_META_ENABLED(TARGET_OS_OSX))
787*c54f35caSApple OSS Distributions {
788*c54f35caSApple OSS Distributions int err, fd;
789*c54f35caSApple OSS Distributions
790*c54f35caSApple OSS Distributions T_WITH_ERRNO;
791*c54f35caSApple OSS Distributions fd = socket(AF_UNIX, SOCK_STREAM, 0);
792*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open socket");
793*c54f35caSApple OSS Distributions
794*c54f35caSApple OSS Distributions T_WITH_ERRNO;
795*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_RDLCK);
796*c54f35caSApple OSS Distributions T_ASSERT_TRUE((err == -1) && (errno == EBADF), "Acquire read lease on socket");
797*c54f35caSApple OSS Distributions
798*c54f35caSApple OSS Distributions T_WITH_ERRNO;
799*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_WRLCK);
800*c54f35caSApple OSS Distributions T_ASSERT_TRUE((err == -1) && (errno == EBADF), "Acquire write lease on socket");
801*c54f35caSApple OSS Distributions
802*c54f35caSApple OSS Distributions T_WITH_ERRNO;
803*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_UNLCK);
804*c54f35caSApple OSS Distributions T_ASSERT_TRUE((err == -1) && (errno == EBADF), "Release lease on socket");
805*c54f35caSApple OSS Distributions
806*c54f35caSApple OSS Distributions T_WITH_ERRNO;
807*c54f35caSApple OSS Distributions err = fcntl(fd, F_GETLEASE);
808*c54f35caSApple OSS Distributions T_ASSERT_TRUE((err == -1) && (errno == EBADF), "Retrieve lease on socket");
809*c54f35caSApple OSS Distributions
810*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close socket");
811*c54f35caSApple OSS Distributions }
812*c54f35caSApple OSS Distributions
813*c54f35caSApple OSS Distributions /*
814*c54f35caSApple OSS Distributions * Test retrieve and downgrade lease with duplicated fd created with dup(2).
815*c54f35caSApple OSS Distributions * a. Process A opens the file with O_RDONLY mode and place a write lease
816*c54f35caSApple OSS Distributions * b. Process A duplicates the existing file descriptor
817*c54f35caSApple OSS Distributions * c. Process A retrieves and downgrade lease with duplicated fd
818*c54f35caSApple OSS Distributions * d. Process A closes the original and duplicated fds to release lease.
819*c54f35caSApple OSS Distributions *
820*c54f35caSApple OSS Distributions * Result: Retrieve and downgrade with duplicated fd should succeed.
821*c54f35caSApple OSS Distributions * When all fds are closed, lease should be released implicity.
822*c54f35caSApple OSS Distributions */
823*c54f35caSApple OSS Distributions T_DECL(retrieve_downgrade_dup_fd, "Test retrieve and downgrade lease with duplicated fd created with dup()", T_META_ENABLED(TARGET_OS_OSX))
824*c54f35caSApple OSS Distributions {
825*c54f35caSApple OSS Distributions int err, dup_fd, fd;
826*c54f35caSApple OSS Distributions
827*c54f35caSApple OSS Distributions create_test_file();
828*c54f35caSApple OSS Distributions
829*c54f35caSApple OSS Distributions T_WITH_ERRNO;
830*c54f35caSApple OSS Distributions fd = open(g_testfile, O_RDONLY, 0666);
831*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDONLY: %s", g_testfile);
832*c54f35caSApple OSS Distributions
833*c54f35caSApple OSS Distributions T_WITH_ERRNO;
834*c54f35caSApple OSS Distributions err = fcntl(fd, F_GETLEASE);
835*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_UNLCK, "Retrieve lease: %s", g_testfile);
836*c54f35caSApple OSS Distributions
837*c54f35caSApple OSS Distributions T_WITH_ERRNO;
838*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_WRLCK);
839*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Acquire write lease: %s", g_testfile);
840*c54f35caSApple OSS Distributions
841*c54f35caSApple OSS Distributions T_WITH_ERRNO;
842*c54f35caSApple OSS Distributions dup_fd = dup(fd);
843*c54f35caSApple OSS Distributions T_ASSERT_NE(dup_fd, -1, "Duplicate existing fd: %d", fd);
844*c54f35caSApple OSS Distributions
845*c54f35caSApple OSS Distributions T_WITH_ERRNO;
846*c54f35caSApple OSS Distributions err = fcntl(dup_fd, F_GETLEASE);
847*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_WRLCK, "Retrieve lease with dup fd: %d", dup_fd);
848*c54f35caSApple OSS Distributions
849*c54f35caSApple OSS Distributions T_WITH_ERRNO;
850*c54f35caSApple OSS Distributions err = fcntl(dup_fd, F_SETLEASE, F_RDLCK);
851*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Downgrade to read lease with dup fd: %d", dup_fd);
852*c54f35caSApple OSS Distributions
853*c54f35caSApple OSS Distributions T_WITH_ERRNO;
854*c54f35caSApple OSS Distributions err = fcntl(dup_fd, F_GETLEASE);
855*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_RDLCK, "Retrieve lease with dup fd: %d", dup_fd);
856*c54f35caSApple OSS Distributions
857*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close original fd");
858*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(dup_fd), "Close duplicated fd");
859*c54f35caSApple OSS Distributions
860*c54f35caSApple OSS Distributions T_WITH_ERRNO;
861*c54f35caSApple OSS Distributions fd = open(g_testfile, O_RDONLY, 0666);
862*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDONLY: %s", g_testfile);
863*c54f35caSApple OSS Distributions
864*c54f35caSApple OSS Distributions T_WITH_ERRNO;
865*c54f35caSApple OSS Distributions err = fcntl(fd, F_GETLEASE);
866*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_UNLCK, "Retrieve lease: %s", g_testfile);
867*c54f35caSApple OSS Distributions
868*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close fd");
869*c54f35caSApple OSS Distributions }
870*c54f35caSApple OSS Distributions
871*c54f35caSApple OSS Distributions /*
872*c54f35caSApple OSS Distributions * Test retrieve and release lease with duplicated fd created with fork(2).
873*c54f35caSApple OSS Distributions * a. Process A opens the file with O_RDONLY mode and place a write lease
874*c54f35caSApple OSS Distributions * b. Process A forks to create a child process
875*c54f35caSApple OSS Distributions * c. Child process retrieves and releases lease with duplicated fd
876*c54f35caSApple OSS Distributions * d. Child process exits
877*c54f35caSApple OSS Distributions * e. Process A verifies the lease has been released
878*c54f35caSApple OSS Distributions *
879*c54f35caSApple OSS Distributions * Result: Retrieve and release with duplicated fd should succeed.
880*c54f35caSApple OSS Distributions * Child process should be able to release the leased placed by the
881*c54f35caSApple OSS Distributions * parent process.
882*c54f35caSApple OSS Distributions */
883*c54f35caSApple OSS Distributions T_DECL(retrieve_release_fork_fd, "Test retrieve and release lease with duplicated fd created with fork()", T_META_ENABLED(TARGET_OS_OSX))
884*c54f35caSApple OSS Distributions {
885*c54f35caSApple OSS Distributions pid_t child_pid;
886*c54f35caSApple OSS Distributions int err, fd;
887*c54f35caSApple OSS Distributions
888*c54f35caSApple OSS Distributions create_test_file();
889*c54f35caSApple OSS Distributions
890*c54f35caSApple OSS Distributions T_WITH_ERRNO;
891*c54f35caSApple OSS Distributions fd = open(g_testfile, O_RDONLY, 0666);
892*c54f35caSApple OSS Distributions T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDONLY: %s", g_testfile);
893*c54f35caSApple OSS Distributions
894*c54f35caSApple OSS Distributions T_WITH_ERRNO;
895*c54f35caSApple OSS Distributions err = fcntl(fd, F_GETLEASE);
896*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_UNLCK, "Retrieve lease: %s", g_testfile);
897*c54f35caSApple OSS Distributions
898*c54f35caSApple OSS Distributions T_WITH_ERRNO;
899*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_WRLCK);
900*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Acquire write lease: %s", g_testfile);
901*c54f35caSApple OSS Distributions
902*c54f35caSApple OSS Distributions child_pid = fork();
903*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(child_pid, "Fork process");
904*c54f35caSApple OSS Distributions
905*c54f35caSApple OSS Distributions if (child_pid == 0) {
906*c54f35caSApple OSS Distributions /* child process */
907*c54f35caSApple OSS Distributions err = fcntl(fd, F_GETLEASE);
908*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_WRLCK, "Retrieve lease with fork fd: %d", fd);
909*c54f35caSApple OSS Distributions
910*c54f35caSApple OSS Distributions T_WITH_ERRNO;
911*c54f35caSApple OSS Distributions err = fcntl(fd, F_SETLEASE, F_UNLCK);
912*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Release lease with fork fd: %d", fd);
913*c54f35caSApple OSS Distributions
914*c54f35caSApple OSS Distributions T_WITH_ERRNO;
915*c54f35caSApple OSS Distributions err = fcntl(fd, F_GETLEASE);
916*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_UNLCK, "Retrieve lease with fork fd: %d", fd);
917*c54f35caSApple OSS Distributions
918*c54f35caSApple OSS Distributions exit(0);
919*c54f35caSApple OSS Distributions } else {
920*c54f35caSApple OSS Distributions /* wait for child process to exit */
921*c54f35caSApple OSS Distributions if (dt_waitpid(child_pid, &err, NULL, 30) == false) {
922*c54f35caSApple OSS Distributions T_FAIL("dt_waitpid() failed on child pid %d", child_pid);
923*c54f35caSApple OSS Distributions }
924*c54f35caSApple OSS Distributions
925*c54f35caSApple OSS Distributions T_WITH_ERRNO;
926*c54f35caSApple OSS Distributions err = fcntl(fd, F_GETLEASE);
927*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_UNLCK, "Retrieve lease with parent fd: %d", fd);
928*c54f35caSApple OSS Distributions
929*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), "Close fd");
930*c54f35caSApple OSS Distributions }
931*c54f35caSApple OSS Distributions }
932*c54f35caSApple OSS Distributions
933*c54f35caSApple OSS Distributions /*
934*c54f35caSApple OSS Distributions * Test lease break release event.
935*c54f35caSApple OSS Distributions * a. Process A opens the file in O_RDONLY mode and place a read lease
936*c54f35caSApple OSS Distributions * b. Process B opens the file in O_RDWR mode and open syscall is blocked
937*c54f35caSApple OSS Distributions * c. Lease break release event is sent to Process A
938*c54f35caSApple OSS Distributions *
939*c54f35caSApple OSS Distributions * Result: Process A releases the lease and process B's open is unblocked
940*c54f35caSApple OSS Distributions */
941*c54f35caSApple OSS Distributions T_DECL(lease_break_release_1, "Test lease break release event when file is opened in O_RDWR mode", T_META_ENABLED(TARGET_OS_OSX))
942*c54f35caSApple OSS Distributions {
943*c54f35caSApple OSS Distributions const char *helper_test_names[] = {"open_rdonly_read_lease_release", "open_rdwr"};
944*c54f35caSApple OSS Distributions
945*c54f35caSApple OSS Distributions create_test_file();
946*c54f35caSApple OSS Distributions
947*c54f35caSApple OSS Distributions run_helpers(helper_test_names, 2);
948*c54f35caSApple OSS Distributions }
949*c54f35caSApple OSS Distributions
950*c54f35caSApple OSS Distributions /*
951*c54f35caSApple OSS Distributions * Test lease break release event.
952*c54f35caSApple OSS Distributions * a. Process A opens the file in O_RDONLY mode and place a read lease
953*c54f35caSApple OSS Distributions * b. Process B truncates the file and truncate syscall is blocked
954*c54f35caSApple OSS Distributions * c. Lease break release event is sent to Process A
955*c54f35caSApple OSS Distributions *
956*c54f35caSApple OSS Distributions * Result: Process A releases the lease and process B's truncate is unblocked.
957*c54f35caSApple OSS Distributions */
958*c54f35caSApple OSS Distributions T_DECL(lease_break_release_2, "Test lease break release event when file is truncated", T_META_ENABLED(TARGET_OS_OSX))
959*c54f35caSApple OSS Distributions {
960*c54f35caSApple OSS Distributions const char *helper_test_names[] = {"open_rdonly_read_lease_release", "truncate"};
961*c54f35caSApple OSS Distributions
962*c54f35caSApple OSS Distributions create_test_file();
963*c54f35caSApple OSS Distributions
964*c54f35caSApple OSS Distributions run_helpers(helper_test_names, 2);
965*c54f35caSApple OSS Distributions }
966*c54f35caSApple OSS Distributions
967*c54f35caSApple OSS Distributions /*
968*c54f35caSApple OSS Distributions * Test lease break release event.
969*c54f35caSApple OSS Distributions * a. Process A opens the file in O_RDONLY mode and place a read lease
970*c54f35caSApple OSS Distributions * b. Process B opens the file in O_RDONLY mode and requests byte range lock
971*c54f35caSApple OSS Distributions * via fcntl(F_SETLK or F_OFD_SETLK)
972*c54f35caSApple OSS Distributions * c. Lease break release event is sent to Process A
973*c54f35caSApple OSS Distributions *
974*c54f35caSApple OSS Distributions * Result: Process A releases the lease and process B's fcntl call is unblocked.
975*c54f35caSApple OSS Distributions */
976*c54f35caSApple OSS Distributions T_DECL(lease_break_release_3, "Test lease break release event when byte range lock is requested", T_META_ENABLED(TARGET_OS_OSX))
977*c54f35caSApple OSS Distributions {
978*c54f35caSApple OSS Distributions const char *helper_test_names[] = {"open_rdonly_read_lease_release", "open_rdonly_request_read_range_lock"};
979*c54f35caSApple OSS Distributions
980*c54f35caSApple OSS Distributions create_test_file();
981*c54f35caSApple OSS Distributions
982*c54f35caSApple OSS Distributions run_helpers(helper_test_names, 2);
983*c54f35caSApple OSS Distributions }
984*c54f35caSApple OSS Distributions
985*c54f35caSApple OSS Distributions /*
986*c54f35caSApple OSS Distributions * Test lease break release event.
987*c54f35caSApple OSS Distributions * a. Process A opens the file in O_RDONLY mode and place a read lease
988*c54f35caSApple OSS Distributions * a. Process B opens the file in O_RDONLY mode and place a read lease
989*c54f35caSApple OSS Distributions * b. Process C opens the file in O_RDWR mode and open syscall is blocked
990*c54f35caSApple OSS Distributions * c. Lease break release events are sent to Process A and B
991*c54f35caSApple OSS Distributions *
992*c54f35caSApple OSS Distributions * Result: Process A and B release the lease and process C's open is unblocked
993*c54f35caSApple OSS Distributions */
994*c54f35caSApple OSS Distributions T_DECL(lease_break_release_4, "Test multiple lease break release events", T_META_ENABLED(TARGET_OS_OSX))
995*c54f35caSApple OSS Distributions {
996*c54f35caSApple OSS Distributions const char *helper_test_names[] = {"open_rdonly_read_lease_release",
997*c54f35caSApple OSS Distributions "open_rdonly_read_lease_release", "open_rdwr"};
998*c54f35caSApple OSS Distributions
999*c54f35caSApple OSS Distributions create_test_file();
1000*c54f35caSApple OSS Distributions
1001*c54f35caSApple OSS Distributions run_helpers(helper_test_names, 3);
1002*c54f35caSApple OSS Distributions }
1003*c54f35caSApple OSS Distributions
1004*c54f35caSApple OSS Distributions /*
1005*c54f35caSApple OSS Distributions * Test lease break downgrade event.
1006*c54f35caSApple OSS Distributions * a. Process A opens the file in O_RDONLY mode and place a write lease
1007*c54f35caSApple OSS Distributions * b. Process B opens the file in O_RDONLY mode and open syscall is blocked
1008*c54f35caSApple OSS Distributions * c. Lease break downgrade event is sent to Process A
1009*c54f35caSApple OSS Distributions *
1010*c54f35caSApple OSS Distributions * Result: Process A downgrades the lease and process B's open is unblocked.
1011*c54f35caSApple OSS Distributions */
1012*c54f35caSApple OSS Distributions T_DECL(lease_break_downgrade_1, "Test lease break downgrade event with read-only opens", T_META_ENABLED(TARGET_OS_OSX))
1013*c54f35caSApple OSS Distributions {
1014*c54f35caSApple OSS Distributions const char *helper_test_names[] = {"open_rdonly_write_lease_downgrade", "open_rdonly"};
1015*c54f35caSApple OSS Distributions
1016*c54f35caSApple OSS Distributions create_test_file();
1017*c54f35caSApple OSS Distributions
1018*c54f35caSApple OSS Distributions run_helpers(helper_test_names, 2);
1019*c54f35caSApple OSS Distributions }
1020*c54f35caSApple OSS Distributions
1021*c54f35caSApple OSS Distributions /*
1022*c54f35caSApple OSS Distributions * Test lease break downgrade event.
1023*c54f35caSApple OSS Distributions * a. Process A opens the file multiple times in O_RDWR mode and place a
1024*c54f35caSApple OSS Distributions * write lease
1025*c54f35caSApple OSS Distributions * b. Process B opens the file in O_RDONLY mode and open syscall is blocked
1026*c54f35caSApple OSS Distributions * c. Lease break downgrade event is sent to Process A
1027*c54f35caSApple OSS Distributions *
1028*c54f35caSApple OSS Distributions * Result: Process A downgrades the lease and process B's open is unblocked.
1029*c54f35caSApple OSS Distributions */
1030*c54f35caSApple OSS Distributions T_DECL(lease_break_downgrade_2, "Test lease break downgrade event with multiple read-write opens", T_META_ENABLED(TARGET_OS_OSX))
1031*c54f35caSApple OSS Distributions {
1032*c54f35caSApple OSS Distributions const char *helper_test_names[] = {"open_rw_write_lease_downgrade", "open_rdonly"};
1033*c54f35caSApple OSS Distributions
1034*c54f35caSApple OSS Distributions create_test_file();
1035*c54f35caSApple OSS Distributions
1036*c54f35caSApple OSS Distributions run_helpers(helper_test_names, 2);
1037*c54f35caSApple OSS Distributions }
1038*c54f35caSApple OSS Distributions
1039*c54f35caSApple OSS Distributions /*
1040*c54f35caSApple OSS Distributions * Test lease break timedout
1041*c54f35caSApple OSS Distributions * a. Process A opens the file in O_RDONLY mode and place a read lease
1042*c54f35caSApple OSS Distributions * b. Process B opens the file in O_RDWR mode and open syscall is blocked
1043*c54f35caSApple OSS Distributions * c. Lease break release event is sent to Process A
1044*c54f35caSApple OSS Distributions * d. Lease is not release within sysctl's 'vfs.lease.break_timeout'
1045*c54f35caSApple OSS Distributions *
1046*c54f35caSApple OSS Distributions * Result: Kernel forcibly breaks the lease and process B's open is unblocked.
1047*c54f35caSApple OSS Distributions */
1048*c54f35caSApple OSS Distributions T_DECL(lease_break_timedout, "Test lease break timedout", T_META_ENABLED(TARGET_OS_OSX))
1049*c54f35caSApple OSS Distributions {
1050*c54f35caSApple OSS Distributions const char *helper_test_names[] = {"open_rdonly_read_lease_timedout", "open_rdwr"};
1051*c54f35caSApple OSS Distributions
1052*c54f35caSApple OSS Distributions create_test_file();
1053*c54f35caSApple OSS Distributions
1054*c54f35caSApple OSS Distributions run_helpers(helper_test_names, 2);
1055*c54f35caSApple OSS Distributions }
1056*c54f35caSApple OSS Distributions
1057*c54f35caSApple OSS Distributions /*
1058*c54f35caSApple OSS Distributions * Test acquire and release lease on directory.
1059*c54f35caSApple OSS Distributions * a. Process A opens the directory in O_RDONLY mode
1060*c54f35caSApple OSS Distributions * b. Process A acquires read lease
1061*c54f35caSApple OSS Distributions * d. Process A release lease
1062*c54f35caSApple OSS Distributions *
1063*c54f35caSApple OSS Distributions * Result: Lease operations should succeed as expected.
1064*c54f35caSApple OSS Distributions */
1065*c54f35caSApple OSS Distributions T_DECL(acquire_release_read_lease_dir, "Test acquire and release read lease", T_META_ENABLED(TARGET_OS_OSX))
1066*c54f35caSApple OSS Distributions {
1067*c54f35caSApple OSS Distributions int err, dir_fd;
1068*c54f35caSApple OSS Distributions
1069*c54f35caSApple OSS Distributions create_test_file();
1070*c54f35caSApple OSS Distributions
1071*c54f35caSApple OSS Distributions T_WITH_ERRNO;
1072*c54f35caSApple OSS Distributions dir_fd = open(g_testdir, O_RDONLY);
1073*c54f35caSApple OSS Distributions T_ASSERT_NE(dir_fd, -1, "Open test dir in O_RDONLY: %s", g_testdir);
1074*c54f35caSApple OSS Distributions
1075*c54f35caSApple OSS Distributions T_WITH_ERRNO;
1076*c54f35caSApple OSS Distributions err = fcntl(dir_fd, F_SETLEASE, F_RDLCK);
1077*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Acquire read lease: %s", g_testdir);
1078*c54f35caSApple OSS Distributions
1079*c54f35caSApple OSS Distributions T_WITH_ERRNO;
1080*c54f35caSApple OSS Distributions err = fcntl(dir_fd, F_GETLEASE);
1081*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_RDLCK, "Retrieve lease: %s", g_testdir);
1082*c54f35caSApple OSS Distributions
1083*c54f35caSApple OSS Distributions T_WITH_ERRNO;
1084*c54f35caSApple OSS Distributions err = fcntl(dir_fd, F_SETLEASE, F_UNLCK);
1085*c54f35caSApple OSS Distributions T_ASSERT_NE(err, -1, "Release lease: %s", g_testdir);
1086*c54f35caSApple OSS Distributions
1087*c54f35caSApple OSS Distributions T_WITH_ERRNO;
1088*c54f35caSApple OSS Distributions err = fcntl(dir_fd, F_GETLEASE);
1089*c54f35caSApple OSS Distributions T_ASSERT_EQ(err, F_UNLCK, "Retrieve lease: %s", g_testdir);
1090*c54f35caSApple OSS Distributions
1091*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(dir_fd), "Close test dir: %s", g_testdir);
1092*c54f35caSApple OSS Distributions }
1093*c54f35caSApple OSS Distributions
1094*c54f35caSApple OSS Distributions /*
1095*c54f35caSApple OSS Distributions * Test acquire write lease on directory.
1096*c54f35caSApple OSS Distributions *
1097*c54f35caSApple OSS Distributions * Result: Acquire write lease should fail with EBADF.
1098*c54f35caSApple OSS Distributions */
1099*c54f35caSApple OSS Distributions T_DECL(acquire_write_lease_dir, "Test acquire write lease on directory", T_META_ENABLED(TARGET_OS_OSX))
1100*c54f35caSApple OSS Distributions {
1101*c54f35caSApple OSS Distributions int err, dir_fd;
1102*c54f35caSApple OSS Distributions
1103*c54f35caSApple OSS Distributions create_test_file();
1104*c54f35caSApple OSS Distributions
1105*c54f35caSApple OSS Distributions T_WITH_ERRNO;
1106*c54f35caSApple OSS Distributions dir_fd = open(g_testdir, O_RDONLY);
1107*c54f35caSApple OSS Distributions T_ASSERT_NE(dir_fd, -1, "Open test dir in O_RDONLY: %s", g_testdir);
1108*c54f35caSApple OSS Distributions
1109*c54f35caSApple OSS Distributions T_WITH_ERRNO;
1110*c54f35caSApple OSS Distributions err = fcntl(dir_fd, F_SETLEASE, F_WRLCK);
1111*c54f35caSApple OSS Distributions T_ASSERT_TRUE((err == -1) && (errno == ENOTSUP), "Acquire write lease on directory: %s", g_testdir);
1112*c54f35caSApple OSS Distributions
1113*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(dir_fd), "Close test dir");
1114*c54f35caSApple OSS Distributions }
1115*c54f35caSApple OSS Distributions
1116*c54f35caSApple OSS Distributions /*
1117*c54f35caSApple OSS Distributions * Test lease break release event for directory read leasing.
1118*c54f35caSApple OSS Distributions * a. Process A opens the directory in O_RDONLY mode and place a read lease
1119*c54f35caSApple OSS Distributions * b. Process B performs various syscalls that can cause its directory contents
1120*c54f35caSApple OSS Distributions * (namespace) to change, modify contents or change attributes on the
1121*c54f35caSApple OSS Distributions * immediate files.
1122*c54f35caSApple OSS Distributions *
1123*c54f35caSApple OSS Distributions * Result: Process A releases the lease and process B's syscall is unblocked.
1124*c54f35caSApple OSS Distributions */
1125*c54f35caSApple OSS Distributions T_DECL(read_lease_dir_1, "Test directory read leasing and lease break events", T_META_ENABLED(TARGET_OS_OSX))
1126*c54f35caSApple OSS Distributions {
1127*c54f35caSApple OSS Distributions const char *helper_test_names[] = {"open_rdonly_dir_read_lease", "file_syscalls"};
1128*c54f35caSApple OSS Distributions
1129*c54f35caSApple OSS Distributions create_test_file();
1130*c54f35caSApple OSS Distributions
1131*c54f35caSApple OSS Distributions run_helpers(helper_test_names, 2);
1132*c54f35caSApple OSS Distributions }
1133*c54f35caSApple OSS Distributions
1134*c54f35caSApple OSS Distributions T_DECL(read_lease_dir_2, "Test directory read leasing and lease break events", T_META_ENABLED(TARGET_OS_OSX))
1135*c54f35caSApple OSS Distributions {
1136*c54f35caSApple OSS Distributions const char *helper_test_names[] = {"open_rdonly_dir_read_lease", "file_syscalls_2"};
1137*c54f35caSApple OSS Distributions
1138*c54f35caSApple OSS Distributions create_test_file();
1139*c54f35caSApple OSS Distributions
1140*c54f35caSApple OSS Distributions run_helpers(helper_test_names, 2);
1141*c54f35caSApple OSS Distributions }
1142