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