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