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