xref: /xnu-11215.81.4/tests/fd_guarded.c (revision d4514f0bc1d3f944c22d92e68b646ac3fb40d452)
1*d4514f0bSApple OSS Distributions #include <darwintest.h>
2*d4514f0bSApple OSS Distributions #include <darwintest_utils.h>
3*d4514f0bSApple OSS Distributions #include <dispatch/dispatch.h>
4*d4514f0bSApple OSS Distributions #include <sys/guarded.h>
5*d4514f0bSApple OSS Distributions #include <sys/types.h>
6*d4514f0bSApple OSS Distributions #include <sys/uio.h>
7*d4514f0bSApple OSS Distributions #include <unistd.h>
8*d4514f0bSApple OSS Distributions #include <fcntl.h>
9*d4514f0bSApple OSS Distributions 
10*d4514f0bSApple OSS Distributions T_GLOBAL_META(
11*d4514f0bSApple OSS Distributions 	T_META_NAMESPACE("xnu.fd"),
12*d4514f0bSApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true));
13*d4514f0bSApple OSS Distributions 
14*d4514f0bSApple OSS Distributions T_DECL(fd_guard_monitored, "Test that we can guard fds in kevent", T_META_TAG_VM_PREFERRED)
15*d4514f0bSApple OSS Distributions {
16*d4514f0bSApple OSS Distributions 	static int pfd[2];
17*d4514f0bSApple OSS Distributions 	static dispatch_source_t ds;
18*d4514f0bSApple OSS Distributions 	guardid_t guard = (uintptr_t)&pfd;
19*d4514f0bSApple OSS Distributions 
20*d4514f0bSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(pipe(pfd), "pipe");
21*d4514f0bSApple OSS Distributions 
22*d4514f0bSApple OSS Distributions 	ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ,
23*d4514f0bSApple OSS Distributions 	    (uintptr_t)pfd[0], 0, NULL);
24*d4514f0bSApple OSS Distributions 	dispatch_source_set_event_handler(ds, ^{ });
25*d4514f0bSApple OSS Distributions 	dispatch_activate(ds);
26*d4514f0bSApple OSS Distributions 
27*d4514f0bSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(change_fdguard_np(pfd[0], NULL, 0,
28*d4514f0bSApple OSS Distributions 	    &guard, GUARD_DUP | GUARD_CLOSE, NULL), "change_fdguard_np");
29*d4514f0bSApple OSS Distributions }
30