xref: /xnu-11215.41.3/tests/socket_poll_close_25786011.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1*33de042dSApple OSS Distributions #include <darwintest.h>
2*33de042dSApple OSS Distributions #include <poll.h>
3*33de042dSApple OSS Distributions #include <sys/socket.h>
4*33de042dSApple OSS Distributions #include <unistd.h>
5*33de042dSApple OSS Distributions 
6*33de042dSApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
7*33de042dSApple OSS Distributions 
8*33de042dSApple OSS Distributions T_DECL(socket_poll_close_25786011, "Tests an invalid poll call to a socket and then calling close.", T_META_LTEPHASE(LTE_POSTINIT), T_META_TAG_VM_PREFERRED)
9*33de042dSApple OSS Distributions {
10*33de042dSApple OSS Distributions 	int my_socket, ret;
11*33de042dSApple OSS Distributions 
12*33de042dSApple OSS Distributions 	my_socket = socket(PF_LOCAL, SOCK_STREAM, 0);
13*33de042dSApple OSS Distributions 	T_WITH_ERRNO; T_ASSERT_TRUE(my_socket > 0, "create socket");
14*33de042dSApple OSS Distributions 
15*33de042dSApple OSS Distributions 	/*
16*33de042dSApple OSS Distributions 	 * Setup a pollfd that we know will return an error when we try
17*33de042dSApple OSS Distributions 	 * to create a knote for it. We specify a BSD vnode specific event
18*33de042dSApple OSS Distributions 	 * for a socket.
19*33de042dSApple OSS Distributions 	 */
20*33de042dSApple OSS Distributions 	struct pollfd my_pollfd = {
21*33de042dSApple OSS Distributions 		.fd = my_socket,
22*33de042dSApple OSS Distributions 		.events = POLLEXTEND
23*33de042dSApple OSS Distributions 	};
24*33de042dSApple OSS Distributions 
25*33de042dSApple OSS Distributions 	/*
26*33de042dSApple OSS Distributions 	 * Previously the call to kevent_register() in the kernel from this call
27*33de042dSApple OSS Distributions 	 * would leak an iocount reference on the fileproc, which would cause any
28*33de042dSApple OSS Distributions 	 * subsequent calls to close() on the associated fd to block indefinitely.
29*33de042dSApple OSS Distributions 	 */
30*33de042dSApple OSS Distributions 	ret = poll(&my_pollfd, 1, 0);
31*33de042dSApple OSS Distributions 	T_WITH_ERRNO; T_ASSERT_TRUE(ret == 1, "poll returned %d", ret);
32*33de042dSApple OSS Distributions 
33*33de042dSApple OSS Distributions 	ret = close(my_socket);
34*33de042dSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ret, "close on socket with fd %d\n", my_socket);
35*33de042dSApple OSS Distributions 
36*33de042dSApple OSS Distributions 	T_PASS("socket_poll_close_25786011 PASSED");
37*33de042dSApple OSS Distributions }
38