1*657871a7Schristos /* $NetBSD: test-closed.c,v 1.1.1.2 2021/04/07 02:43:15 christos Exp $ */
2805a1ce9Schristos /*
3805a1ce9Schristos * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
4805a1ce9Schristos * Copyright (c) 2007-2013 Niels Provos and Nick Mathewson
5805a1ce9Schristos *
6805a1ce9Schristos * Redistribution and use in source and binary forms, with or without
7805a1ce9Schristos * modification, are permitted provided that the following conditions
8805a1ce9Schristos * are met:
9805a1ce9Schristos * 1. Redistributions of source code must retain the above copyright
10805a1ce9Schristos * notice, this list of conditions and the following disclaimer.
11805a1ce9Schristos * 2. Redistributions in binary form must reproduce the above copyright
12805a1ce9Schristos * notice, this list of conditions and the following disclaimer in the
13805a1ce9Schristos * documentation and/or other materials provided with the distribution.
14805a1ce9Schristos * 3. The name of the author may not be used to endorse or promote products
15805a1ce9Schristos * derived from this software without specific prior written permission.
16805a1ce9Schristos *
17805a1ce9Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18805a1ce9Schristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19805a1ce9Schristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20805a1ce9Schristos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21805a1ce9Schristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22805a1ce9Schristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23805a1ce9Schristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24805a1ce9Schristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25805a1ce9Schristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26805a1ce9Schristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27805a1ce9Schristos */
28805a1ce9Schristos #include "../util-internal.h"
29805a1ce9Schristos #include "event2/event-config.h"
30805a1ce9Schristos #include <sys/cdefs.h>
31*657871a7Schristos __RCSID("$NetBSD: test-closed.c,v 1.1.1.2 2021/04/07 02:43:15 christos Exp $");
32805a1ce9Schristos
33805a1ce9Schristos #ifdef _WIN32
34805a1ce9Schristos #include <winsock2.h>
35805a1ce9Schristos #else
36805a1ce9Schristos #include <unistd.h>
37805a1ce9Schristos #endif
38805a1ce9Schristos #include <sys/types.h>
39805a1ce9Schristos #include <sys/stat.h>
40805a1ce9Schristos #ifdef EVENT__HAVE_SYS_TIME_H
41805a1ce9Schristos #include <sys/time.h>
42805a1ce9Schristos #endif
43805a1ce9Schristos #ifdef EVENT__HAVE_SYS_SOCKET_H
44805a1ce9Schristos #include <sys/socket.h>
45805a1ce9Schristos #endif
46805a1ce9Schristos #include <fcntl.h>
47805a1ce9Schristos #include <stdlib.h>
48805a1ce9Schristos #include <stdio.h>
49805a1ce9Schristos #include <string.h>
50805a1ce9Schristos #include <errno.h>
51805a1ce9Schristos
52805a1ce9Schristos #include <event.h>
53805a1ce9Schristos #include <evutil.h>
54805a1ce9Schristos
55805a1ce9Schristos struct timeval timeout = {3, 0};
56805a1ce9Schristos
57805a1ce9Schristos static void
closed_cb(evutil_socket_t fd,short event,void * arg)58805a1ce9Schristos closed_cb(evutil_socket_t fd, short event, void *arg)
59805a1ce9Schristos {
60805a1ce9Schristos if (EV_TIMEOUT & event) {
61805a1ce9Schristos printf("%s: Timeout!\n", __func__);
62805a1ce9Schristos exit(1);
63805a1ce9Schristos }
64805a1ce9Schristos
65805a1ce9Schristos if (EV_CLOSED & event) {
66805a1ce9Schristos printf("%s: detected socket close with success\n", __func__);
67805a1ce9Schristos return;
68805a1ce9Schristos }
69805a1ce9Schristos
70805a1ce9Schristos printf("%s: unable to detect socket close\n", __func__);
71805a1ce9Schristos exit(1);
72805a1ce9Schristos }
73805a1ce9Schristos
74805a1ce9Schristos int
main(int argc,char ** argv)75805a1ce9Schristos main(int argc, char **argv)
76805a1ce9Schristos {
77805a1ce9Schristos struct event_base *base;
78805a1ce9Schristos struct event_config *cfg;
79805a1ce9Schristos struct event *ev;
80805a1ce9Schristos const char *test = "test string";
81805a1ce9Schristos evutil_socket_t pair[2];
82805a1ce9Schristos
83805a1ce9Schristos /* Initialize the library and check if the backend
84805a1ce9Schristos supports EV_FEATURE_EARLY_CLOSE
85805a1ce9Schristos */
86805a1ce9Schristos cfg = event_config_new();
87805a1ce9Schristos event_config_require_features(cfg, EV_FEATURE_EARLY_CLOSE);
88805a1ce9Schristos base = event_base_new_with_config(cfg);
89805a1ce9Schristos event_config_free(cfg);
90805a1ce9Schristos if (!base) {
91805a1ce9Schristos /* Backend doesn't support EV_FEATURE_EARLY_CLOSE */
92805a1ce9Schristos return 0;
93805a1ce9Schristos }
94805a1ce9Schristos
95805a1ce9Schristos /* Create a pair of sockets */
96805a1ce9Schristos if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
97805a1ce9Schristos return (1);
98805a1ce9Schristos
99805a1ce9Schristos /* Send some data on socket 0 and immediately close it */
100805a1ce9Schristos if (send(pair[0], test, (int)strlen(test)+1, 0) < 0)
101805a1ce9Schristos return (1);
102805a1ce9Schristos shutdown(pair[0], EVUTIL_SHUT_WR);
103805a1ce9Schristos
104805a1ce9Schristos /* Dispatch */
105805a1ce9Schristos ev = event_new(base, pair[1], EV_CLOSED | EV_TIMEOUT, closed_cb, event_self_cbarg());
106805a1ce9Schristos event_add(ev, &timeout);
107805a1ce9Schristos event_base_dispatch(base);
108805a1ce9Schristos
109805a1ce9Schristos /* Finalize library */
110*657871a7Schristos event_free(ev);
111805a1ce9Schristos event_base_free(base);
112805a1ce9Schristos return 0;
113805a1ce9Schristos }
114805a1ce9Schristos
115