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