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