xref: /freebsd/contrib/libevent/test/test-closed.c (revision b50261e21f39a6c7249a49e7b60aa878c98512a8)
1c43e99fdSEd Maste /*
2c43e99fdSEd Maste  * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
3c43e99fdSEd Maste  * Copyright (c) 2007-2013 Niels Provos and Nick Mathewson
4c43e99fdSEd Maste  *
5c43e99fdSEd Maste  * Redistribution and use in source and binary forms, with or without
6c43e99fdSEd Maste  * modification, are permitted provided that the following conditions
7c43e99fdSEd Maste  * are met:
8c43e99fdSEd Maste  * 1. Redistributions of source code must retain the above copyright
9c43e99fdSEd Maste  *    notice, this list of conditions and the following disclaimer.
10c43e99fdSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
11c43e99fdSEd Maste  *    notice, this list of conditions and the following disclaimer in the
12c43e99fdSEd Maste  *    documentation and/or other materials provided with the distribution.
13c43e99fdSEd Maste  * 3. The name of the author may not be used to endorse or promote products
14c43e99fdSEd Maste  *    derived from this software without specific prior written permission.
15c43e99fdSEd Maste  *
16c43e99fdSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17c43e99fdSEd Maste  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18c43e99fdSEd Maste  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19c43e99fdSEd Maste  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20c43e99fdSEd Maste  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21c43e99fdSEd Maste  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22c43e99fdSEd Maste  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23c43e99fdSEd Maste  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24c43e99fdSEd Maste  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25c43e99fdSEd Maste  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26c43e99fdSEd Maste  */
27c43e99fdSEd Maste #include "../util-internal.h"
28c43e99fdSEd Maste #include "event2/event-config.h"
29c43e99fdSEd Maste 
30c43e99fdSEd Maste #ifdef _WIN32
31c43e99fdSEd Maste #include <winsock2.h>
32c43e99fdSEd Maste #else
33c43e99fdSEd Maste #include <unistd.h>
34c43e99fdSEd Maste #endif
35c43e99fdSEd Maste #include <sys/types.h>
36c43e99fdSEd Maste #include <sys/stat.h>
37c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_TIME_H
38c43e99fdSEd Maste #include <sys/time.h>
39c43e99fdSEd Maste #endif
40c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_SOCKET_H
41c43e99fdSEd Maste #include <sys/socket.h>
42c43e99fdSEd Maste #endif
43c43e99fdSEd Maste #include <fcntl.h>
44c43e99fdSEd Maste #include <stdlib.h>
45c43e99fdSEd Maste #include <stdio.h>
46c43e99fdSEd Maste #include <string.h>
47c43e99fdSEd Maste #include <errno.h>
48c43e99fdSEd Maste 
49c43e99fdSEd Maste #include <event.h>
50c43e99fdSEd Maste #include <evutil.h>
51c43e99fdSEd Maste 
52c43e99fdSEd Maste struct timeval timeout = {3, 0};
53c43e99fdSEd Maste 
54c43e99fdSEd Maste static void
closed_cb(evutil_socket_t fd,short event,void * arg)55c43e99fdSEd Maste closed_cb(evutil_socket_t fd, short event, void *arg)
56c43e99fdSEd Maste {
57c43e99fdSEd Maste 	if (EV_TIMEOUT & event) {
58c43e99fdSEd Maste 		printf("%s: Timeout!\n", __func__);
59c43e99fdSEd Maste 		exit(1);
60c43e99fdSEd Maste 	}
61c43e99fdSEd Maste 
62c43e99fdSEd Maste 	if (EV_CLOSED & event) {
63c43e99fdSEd Maste 		printf("%s: detected socket close with success\n", __func__);
64c43e99fdSEd Maste 		return;
65c43e99fdSEd Maste 	}
66c43e99fdSEd Maste 
67c43e99fdSEd Maste 	printf("%s: unable to detect socket close\n", __func__);
68c43e99fdSEd Maste 	exit(1);
69c43e99fdSEd Maste }
70c43e99fdSEd Maste 
71c43e99fdSEd Maste int
main(int argc,char ** argv)72c43e99fdSEd Maste main(int argc, char **argv)
73c43e99fdSEd Maste {
74c43e99fdSEd Maste 	struct event_base *base;
75c43e99fdSEd Maste 	struct event_config *cfg;
76c43e99fdSEd Maste 	struct event *ev;
77c43e99fdSEd Maste 	const char *test = "test string";
78c43e99fdSEd Maste 	evutil_socket_t pair[2];
79c43e99fdSEd Maste 
80c43e99fdSEd Maste 	/* Initialize the library and check if the backend
81c43e99fdSEd Maste 	   supports EV_FEATURE_EARLY_CLOSE
82c43e99fdSEd Maste 	*/
83c43e99fdSEd Maste 	cfg = event_config_new();
84c43e99fdSEd Maste 	event_config_require_features(cfg, EV_FEATURE_EARLY_CLOSE);
85c43e99fdSEd Maste 	base = event_base_new_with_config(cfg);
86c43e99fdSEd Maste 	event_config_free(cfg);
87c43e99fdSEd Maste 	if (!base) {
88c43e99fdSEd Maste 		/* Backend doesn't support EV_FEATURE_EARLY_CLOSE */
89c43e99fdSEd Maste 		return 0;
90c43e99fdSEd Maste 	}
91c43e99fdSEd Maste 
92c43e99fdSEd Maste 	/* Create a pair of sockets */
93c43e99fdSEd Maste 	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
94c43e99fdSEd Maste 		return (1);
95c43e99fdSEd Maste 
96c43e99fdSEd Maste 	/* Send some data on socket 0 and immediately close it */
97c43e99fdSEd Maste 	if (send(pair[0], test, (int)strlen(test)+1, 0) < 0)
98c43e99fdSEd Maste 		return (1);
99c43e99fdSEd Maste 	shutdown(pair[0], EVUTIL_SHUT_WR);
100c43e99fdSEd Maste 
101c43e99fdSEd Maste 	/* Dispatch */
102c43e99fdSEd Maste 	ev = event_new(base, pair[1], EV_CLOSED | EV_TIMEOUT, closed_cb, event_self_cbarg());
103c43e99fdSEd Maste 	event_add(ev, &timeout);
104c43e99fdSEd Maste 	event_base_dispatch(base);
105c43e99fdSEd Maste 
106c43e99fdSEd Maste 	/* Finalize library */
107*b50261e2SCy Schubert 	event_free(ev);
108c43e99fdSEd Maste 	event_base_free(base);
109c43e99fdSEd Maste 	return 0;
110c43e99fdSEd Maste }
111c43e99fdSEd Maste 
112