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