xref: /freebsd/contrib/libevent/test/test-fdleak.c (revision b50261e21f39a6c7249a49e7b60aa878c98512a8)
1c43e99fdSEd Maste /*
2c43e99fdSEd Maste  * Copyright (c) 2012 Ross Lagerwall <rosslagerwall@gmail.com>
3c43e99fdSEd Maste  *
4c43e99fdSEd Maste  * Redistribution and use in source and binary forms, with or without
5c43e99fdSEd Maste  * modification, are permitted provided that the following conditions
6c43e99fdSEd Maste  * are met:
7c43e99fdSEd Maste  * 1. Redistributions of source code must retain the above copyright
8c43e99fdSEd Maste  *    notice, this list of conditions and the following disclaimer.
9c43e99fdSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
10c43e99fdSEd Maste  *    notice, this list of conditions and the following disclaimer in the
11c43e99fdSEd Maste  *    documentation and/or other materials provided with the distribution.
12c43e99fdSEd Maste  * 3. The name of the author may not be used to endorse or promote products
13c43e99fdSEd Maste  *    derived from this software without specific prior written permission.
14c43e99fdSEd Maste  *
15c43e99fdSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16c43e99fdSEd Maste  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17c43e99fdSEd Maste  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18c43e99fdSEd Maste  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19c43e99fdSEd Maste  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20c43e99fdSEd Maste  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21c43e99fdSEd Maste  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22c43e99fdSEd Maste  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23c43e99fdSEd Maste  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24c43e99fdSEd Maste  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25c43e99fdSEd Maste  */
26c43e99fdSEd Maste 
27c43e99fdSEd Maste #include "event2/event-config.h"
28c43e99fdSEd Maste 
29c43e99fdSEd Maste #ifdef _WIN32
30c43e99fdSEd Maste #define WIN32_LEAN_AND_MEAN
31c43e99fdSEd Maste #include <windows.h>
32c43e99fdSEd Maste #endif
33c43e99fdSEd Maste #include <string.h>
34c43e99fdSEd Maste #include <stdlib.h>
35c43e99fdSEd Maste #include <errno.h>
36c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_TIME_H
37c43e99fdSEd Maste #include <sys/time.h>
38c43e99fdSEd Maste #endif
39c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_RESOURCE_H
40c43e99fdSEd Maste #include <sys/resource.h>
41c43e99fdSEd Maste #endif
42c43e99fdSEd Maste #ifdef EVENT__HAVE_NETINET_IN_H
43c43e99fdSEd Maste #include <netinet/in.h>
44c43e99fdSEd Maste #endif
45c43e99fdSEd Maste 
46c43e99fdSEd Maste #include "event2/event.h"
47c43e99fdSEd Maste #include "event2/bufferevent.h"
48c43e99fdSEd Maste #include "event2/buffer.h"
49c43e99fdSEd Maste #include "event2/listener.h"
50c43e99fdSEd Maste 
51c43e99fdSEd Maste /* Number of requests to make. Setting this too high might result in the machine
52c43e99fdSEd Maste    running out of ephemeral ports */
53c43e99fdSEd Maste #ifdef _WIN32
54c43e99fdSEd Maste #define MAX_REQUESTS 1000
55c43e99fdSEd Maste #else
56c43e99fdSEd Maste #define MAX_REQUESTS 4000
57c43e99fdSEd Maste #endif
58c43e99fdSEd Maste 
59c43e99fdSEd Maste /* Provide storage for the address, both for the server & the clients */
60c43e99fdSEd Maste static struct sockaddr_in saddr;
61c43e99fdSEd Maste 
62c43e99fdSEd Maste /* Number of sucessful requests so far */
63c43e99fdSEd Maste static int num_requests;
64c43e99fdSEd Maste 
65c43e99fdSEd Maste static void start_client(struct event_base *base);
66c43e99fdSEd Maste 
67c43e99fdSEd Maste static void
my_perror(const char * s)68c43e99fdSEd Maste my_perror(const char *s)
69c43e99fdSEd Maste {
70c43e99fdSEd Maste 	fprintf(stderr, "%s: %s",
71c43e99fdSEd Maste 	    s, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR()));
72c43e99fdSEd Maste }
73c43e99fdSEd Maste 
74c43e99fdSEd Maste /*
75c43e99fdSEd Maste ===============================================
76c43e99fdSEd Maste Server functions
77c43e99fdSEd Maste ===============================================
78c43e99fdSEd Maste */
79c43e99fdSEd Maste 
80c43e99fdSEd Maste /* Read a byte from the client and write it back */
81c43e99fdSEd Maste static void
server_read_cb(struct bufferevent * bev,void * ctx)82c43e99fdSEd Maste server_read_cb(struct bufferevent *bev, void *ctx)
83c43e99fdSEd Maste {
84c43e99fdSEd Maste 	while (evbuffer_get_length(bufferevent_get_input(bev))) {
85c43e99fdSEd Maste 		unsigned char tmp;
86c43e99fdSEd Maste 		bufferevent_read(bev, &tmp, 1);
87c43e99fdSEd Maste 		bufferevent_write(bev, &tmp, 1);
88c43e99fdSEd Maste 	}
89c43e99fdSEd Maste }
90c43e99fdSEd Maste 
91c43e99fdSEd Maste /* Wait for an EOF and then free the bufferevent */
92c43e99fdSEd Maste static void
server_event_cb(struct bufferevent * bev,short events,void * ctx)93c43e99fdSEd Maste server_event_cb(struct bufferevent *bev, short events, void *ctx)
94c43e99fdSEd Maste {
95c43e99fdSEd Maste 	if (events & BEV_EVENT_ERROR) {
96c43e99fdSEd Maste 		my_perror("Error from bufferevent");
97c43e99fdSEd Maste 		exit(1);
98*b50261e2SCy Schubert 	} else if (events & BEV_EVENT_EOF) {
99c43e99fdSEd Maste 		bufferevent_free(bev);
100*b50261e2SCy Schubert 		if (num_requests == MAX_REQUESTS) {
101*b50261e2SCy Schubert 			event_base_loopbreak(bufferevent_get_base(bev));
102*b50261e2SCy Schubert 		}
103c43e99fdSEd Maste 	}
104c43e99fdSEd Maste }
105c43e99fdSEd Maste 
106c43e99fdSEd Maste /* Accept a client socket and set it up to for reading & writing */
107c43e99fdSEd Maste static void
listener_accept_cb(struct evconnlistener * listener,evutil_socket_t sock,struct sockaddr * addr,int len,void * ptr)108c43e99fdSEd Maste listener_accept_cb(struct evconnlistener *listener, evutil_socket_t sock,
109c43e99fdSEd Maste                    struct sockaddr *addr, int len, void *ptr)
110c43e99fdSEd Maste {
111c43e99fdSEd Maste 	struct event_base *base = evconnlistener_get_base(listener);
112c43e99fdSEd Maste 	struct bufferevent *bev = bufferevent_socket_new(base, sock,
113c43e99fdSEd Maste 		BEV_OPT_CLOSE_ON_FREE);
114c43e99fdSEd Maste 	bufferevent_setcb(bev, server_read_cb, NULL, server_event_cb, NULL);
115c43e99fdSEd Maste 	bufferevent_enable(bev, EV_READ|EV_WRITE);
116c43e99fdSEd Maste }
117c43e99fdSEd Maste 
118c43e99fdSEd Maste /* Start the server listening on a random port and start the first client. */
119c43e99fdSEd Maste static void
start_loop(void)120c43e99fdSEd Maste start_loop(void)
121c43e99fdSEd Maste {
122c43e99fdSEd Maste 	struct event_base *base;
123c43e99fdSEd Maste 	struct evconnlistener *listener;
124c43e99fdSEd Maste 	struct sockaddr_storage ss;
125c43e99fdSEd Maste 	ev_socklen_t socklen = sizeof(ss);
126c43e99fdSEd Maste 	evutil_socket_t fd;
127c43e99fdSEd Maste 
128c43e99fdSEd Maste 	base = event_base_new();
129c43e99fdSEd Maste 	if (base == NULL) {
130c43e99fdSEd Maste 		puts("Could not open event base!");
131c43e99fdSEd Maste 		exit(1);
132c43e99fdSEd Maste 	}
133c43e99fdSEd Maste 
134c43e99fdSEd Maste 	listener = evconnlistener_new_bind(base, listener_accept_cb, NULL,
135c43e99fdSEd Maste 	    LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
136c43e99fdSEd Maste 	    -1, (struct sockaddr *)&saddr, sizeof(saddr));
137c43e99fdSEd Maste 	if (listener == NULL) {
138c43e99fdSEd Maste 		my_perror("Could not create listener!");
139c43e99fdSEd Maste 		exit(1);
140c43e99fdSEd Maste 	}
141c43e99fdSEd Maste 	fd = evconnlistener_get_fd(listener);
142c43e99fdSEd Maste 	if (fd < 0) {
143c43e99fdSEd Maste 		puts("Couldn't get fd from listener");
144c43e99fdSEd Maste 		exit(1);
145c43e99fdSEd Maste 	}
146c43e99fdSEd Maste 	if (getsockname(fd, (struct sockaddr *)&ss, &socklen) < 0) {
147c43e99fdSEd Maste 		my_perror("getsockname()");
148c43e99fdSEd Maste 		exit(1);
149c43e99fdSEd Maste 	}
150c43e99fdSEd Maste 	memcpy(&saddr, &ss, sizeof(saddr));
151c43e99fdSEd Maste 	if (saddr.sin_family != AF_INET) {
152c43e99fdSEd Maste 		puts("AF mismatch from getsockname().");
153c43e99fdSEd Maste 		exit(1);
154c43e99fdSEd Maste 	}
155c43e99fdSEd Maste 
156c43e99fdSEd Maste 	start_client(base);
157c43e99fdSEd Maste 
158c43e99fdSEd Maste 	event_base_dispatch(base);
159*b50261e2SCy Schubert 
160*b50261e2SCy Schubert 	evconnlistener_free(listener);
161*b50261e2SCy Schubert 	event_base_free(base);
162c43e99fdSEd Maste }
163c43e99fdSEd Maste 
164c43e99fdSEd Maste /*
165c43e99fdSEd Maste ===============================================
166c43e99fdSEd Maste Client functions
167c43e99fdSEd Maste ===============================================
168c43e99fdSEd Maste */
169c43e99fdSEd Maste 
170c43e99fdSEd Maste /* Check that the server sends back the same byte that the client sent.
171c43e99fdSEd Maste    If MAX_REQUESTS have been reached, exit. Otherwise, start another client. */
172c43e99fdSEd Maste static void
client_read_cb(struct bufferevent * bev,void * ctx)173c43e99fdSEd Maste client_read_cb(struct bufferevent *bev, void *ctx)
174c43e99fdSEd Maste {
175c43e99fdSEd Maste 	unsigned char tmp;
176c43e99fdSEd Maste 	struct event_base *base = bufferevent_get_base(bev);
177c43e99fdSEd Maste 
178c43e99fdSEd Maste 	bufferevent_read(bev, &tmp, 1);
179c43e99fdSEd Maste 	if (tmp != 'A') {
180c43e99fdSEd Maste 		puts("Incorrect data received!");
181c43e99fdSEd Maste 		exit(2);
182c43e99fdSEd Maste 	}
183c43e99fdSEd Maste 	bufferevent_free(bev);
184c43e99fdSEd Maste 
185c43e99fdSEd Maste 	num_requests++;
186*b50261e2SCy Schubert 	if (++num_requests < MAX_REQUESTS) {
187c43e99fdSEd Maste 		start_client(base);
188c43e99fdSEd Maste 	}
189c43e99fdSEd Maste }
190c43e99fdSEd Maste 
191c43e99fdSEd Maste /* Send a byte to the server. */
192c43e99fdSEd Maste static void
client_event_cb(struct bufferevent * bev,short events,void * ctx)193c43e99fdSEd Maste client_event_cb(struct bufferevent *bev, short events, void *ctx)
194c43e99fdSEd Maste {
195c43e99fdSEd Maste 	if (events & BEV_EVENT_CONNECTED) {
196c43e99fdSEd Maste 		unsigned char tmp = 'A';
197c43e99fdSEd Maste 		bufferevent_write(bev, &tmp, 1);
198c43e99fdSEd Maste 	} else if (events & BEV_EVENT_ERROR) {
199c43e99fdSEd Maste 		puts("Client socket got error!");
200c43e99fdSEd Maste 		exit(2);
201c43e99fdSEd Maste 	}
202c43e99fdSEd Maste 
203c43e99fdSEd Maste 	bufferevent_enable(bev, EV_READ);
204c43e99fdSEd Maste }
205c43e99fdSEd Maste 
206c43e99fdSEd Maste /* Open a client socket to connect to localhost on sin */
207c43e99fdSEd Maste static void
start_client(struct event_base * base)208c43e99fdSEd Maste start_client(struct event_base *base)
209c43e99fdSEd Maste {
210c43e99fdSEd Maste 	struct bufferevent *bev = bufferevent_socket_new(base, -1,
211c43e99fdSEd Maste                                                          BEV_OPT_CLOSE_ON_FREE);
212c43e99fdSEd Maste 	bufferevent_setcb(bev, client_read_cb, NULL, client_event_cb, NULL);
213c43e99fdSEd Maste 
214c43e99fdSEd Maste 	if (bufferevent_socket_connect(bev, (struct sockaddr *)&saddr,
215c43e99fdSEd Maste                                        sizeof(saddr)) < 0) {
216c43e99fdSEd Maste 		my_perror("Could not connect!");
217c43e99fdSEd Maste 		bufferevent_free(bev);
218c43e99fdSEd Maste 		exit(2);
219c43e99fdSEd Maste 	}
220c43e99fdSEd Maste }
221c43e99fdSEd Maste 
222c43e99fdSEd Maste int
main(int argc,char ** argv)223c43e99fdSEd Maste main(int argc, char **argv)
224c43e99fdSEd Maste {
225c43e99fdSEd Maste #ifdef EVENT__HAVE_SETRLIMIT
226c43e99fdSEd Maste 	/* Set the fd limit to a low value so that any fd leak is caught without
227c43e99fdSEd Maste 	making many requests. */
228c43e99fdSEd Maste 	struct rlimit rl;
229c43e99fdSEd Maste 	rl.rlim_cur = rl.rlim_max = 20;
230c43e99fdSEd Maste 	if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
231c43e99fdSEd Maste 		my_perror("setrlimit");
232c43e99fdSEd Maste 		exit(3);
233c43e99fdSEd Maste 	}
234c43e99fdSEd Maste #endif
235c43e99fdSEd Maste 
236c43e99fdSEd Maste #ifdef _WIN32
237c43e99fdSEd Maste 	WSADATA WSAData;
238c43e99fdSEd Maste 	WSAStartup(0x101, &WSAData);
239c43e99fdSEd Maste #endif
240c43e99fdSEd Maste 
241c43e99fdSEd Maste 	/* Set up an address, used by both client & server. */
242c43e99fdSEd Maste 	memset(&saddr, 0, sizeof(saddr));
243c43e99fdSEd Maste 	saddr.sin_family = AF_INET;
244c43e99fdSEd Maste 	saddr.sin_addr.s_addr = htonl(0x7f000001);
245c43e99fdSEd Maste 	saddr.sin_port = 0; /* Tell the implementation to pick a port. */
246c43e99fdSEd Maste 
247c43e99fdSEd Maste 	start_loop();
248c43e99fdSEd Maste 
249c43e99fdSEd Maste 	return 0;
250c43e99fdSEd Maste }
251c43e99fdSEd Maste 
252c43e99fdSEd Maste /* XXX why does this test cause so much latency sometimes (OSX 10.5)? */
253