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