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