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