12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
32b15cb3dSCy Schubert * Copyright (c) 2007-2013 Niels Provos and Nick Mathewson
42b15cb3dSCy Schubert *
52b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without
62b15cb3dSCy Schubert * modification, are permitted provided that the following conditions
72b15cb3dSCy Schubert * are met:
82b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright
92b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer.
102b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
112b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the
122b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution.
132b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products
142b15cb3dSCy Schubert * derived from this software without specific prior written permission.
152b15cb3dSCy Schubert *
162b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262b15cb3dSCy Schubert */
27*a466cc55SCy Schubert #include "../util-internal.h"
282b15cb3dSCy Schubert #include "event2/event-config.h"
292b15cb3dSCy Schubert
302b15cb3dSCy Schubert #ifdef _WIN32
312b15cb3dSCy Schubert #include <winsock2.h>
322b15cb3dSCy Schubert #else
332b15cb3dSCy Schubert #include <unistd.h>
342b15cb3dSCy Schubert #endif
352b15cb3dSCy Schubert #include <sys/types.h>
362b15cb3dSCy Schubert #include <sys/stat.h>
372b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H
382b15cb3dSCy Schubert #include <sys/time.h>
392b15cb3dSCy Schubert #endif
402b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_SOCKET_H
412b15cb3dSCy Schubert #include <sys/socket.h>
422b15cb3dSCy Schubert #endif
432b15cb3dSCy Schubert #include <fcntl.h>
442b15cb3dSCy Schubert #include <stdlib.h>
452b15cb3dSCy Schubert #include <stdio.h>
462b15cb3dSCy Schubert #include <string.h>
472b15cb3dSCy Schubert #include <errno.h>
482b15cb3dSCy Schubert
492b15cb3dSCy Schubert #include <event.h>
502b15cb3dSCy Schubert #include <evutil.h>
512b15cb3dSCy Schubert
522b15cb3dSCy Schubert struct timeval timeout = {3, 0};
532b15cb3dSCy Schubert
542b15cb3dSCy Schubert static void
closed_cb(evutil_socket_t fd,short event,void * arg)552b15cb3dSCy Schubert closed_cb(evutil_socket_t fd, short event, void *arg)
562b15cb3dSCy Schubert {
572b15cb3dSCy Schubert if (EV_TIMEOUT & event) {
582b15cb3dSCy Schubert printf("%s: Timeout!\n", __func__);
592b15cb3dSCy Schubert exit(1);
602b15cb3dSCy Schubert }
612b15cb3dSCy Schubert
622b15cb3dSCy Schubert if (EV_CLOSED & event) {
632b15cb3dSCy Schubert printf("%s: detected socket close with success\n", __func__);
642b15cb3dSCy Schubert return;
652b15cb3dSCy Schubert }
662b15cb3dSCy Schubert
672b15cb3dSCy Schubert printf("%s: unable to detect socket close\n", __func__);
682b15cb3dSCy Schubert exit(1);
692b15cb3dSCy Schubert }
702b15cb3dSCy Schubert
712b15cb3dSCy Schubert int
main(int argc,char ** argv)722b15cb3dSCy Schubert main(int argc, char **argv)
732b15cb3dSCy Schubert {
742b15cb3dSCy Schubert struct event_base *base;
752b15cb3dSCy Schubert struct event_config *cfg;
762b15cb3dSCy Schubert struct event *ev;
772b15cb3dSCy Schubert const char *test = "test string";
782b15cb3dSCy Schubert evutil_socket_t pair[2];
792b15cb3dSCy Schubert
802b15cb3dSCy Schubert /* Initialize the library and check if the backend
812b15cb3dSCy Schubert supports EV_FEATURE_EARLY_CLOSE
822b15cb3dSCy Schubert */
832b15cb3dSCy Schubert cfg = event_config_new();
842b15cb3dSCy Schubert event_config_require_features(cfg, EV_FEATURE_EARLY_CLOSE);
852b15cb3dSCy Schubert base = event_base_new_with_config(cfg);
862b15cb3dSCy Schubert event_config_free(cfg);
872b15cb3dSCy Schubert if (!base) {
882b15cb3dSCy Schubert /* Backend doesn't support EV_FEATURE_EARLY_CLOSE */
892b15cb3dSCy Schubert return 0;
902b15cb3dSCy Schubert }
912b15cb3dSCy Schubert
922b15cb3dSCy Schubert /* Create a pair of sockets */
932b15cb3dSCy Schubert if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
942b15cb3dSCy Schubert return (1);
952b15cb3dSCy Schubert
962b15cb3dSCy Schubert /* Send some data on socket 0 and immediately close it */
972b15cb3dSCy Schubert if (send(pair[0], test, (int)strlen(test)+1, 0) < 0)
982b15cb3dSCy Schubert return (1);
99*a466cc55SCy Schubert shutdown(pair[0], EVUTIL_SHUT_WR);
1002b15cb3dSCy Schubert
1012b15cb3dSCy Schubert /* Dispatch */
1022b15cb3dSCy Schubert ev = event_new(base, pair[1], EV_CLOSED | EV_TIMEOUT, closed_cb, event_self_cbarg());
1032b15cb3dSCy Schubert event_add(ev, &timeout);
1042b15cb3dSCy Schubert event_base_dispatch(base);
1052b15cb3dSCy Schubert
1062b15cb3dSCy Schubert /* Finalize library */
107*a466cc55SCy Schubert event_free(ev);
1082b15cb3dSCy Schubert event_base_free(base);
1092b15cb3dSCy Schubert return 0;
1102b15cb3dSCy Schubert }
1112b15cb3dSCy Schubert
112