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