1 /* $NetBSD: test-closed.c,v 1.6 2024/08/18 20:47:23 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 "../util-internal.h" 30 #include "event2/event-config.h" 31 32 #ifdef _WIN32 33 #include <winsock2.h> 34 #else 35 #include <unistd.h> 36 #endif 37 #include <sys/types.h> 38 #include <sys/stat.h> 39 #ifdef EVENT__HAVE_SYS_TIME_H 40 #include <sys/time.h> 41 #endif 42 #ifdef EVENT__HAVE_SYS_SOCKET_H 43 #include <sys/socket.h> 44 #endif 45 #include <fcntl.h> 46 #include <stdlib.h> 47 #include <stdio.h> 48 #include <string.h> 49 #include <errno.h> 50 51 #include <event.h> 52 #include <evutil.h> 53 54 struct timeval timeout = {3, 0}; 55 56 static void 57 closed_cb(evutil_socket_t fd, short event, void *arg) 58 { 59 if (EV_TIMEOUT & event) { 60 printf("%s: Timeout!\n", __func__); 61 exit(1); 62 } 63 64 if (EV_CLOSED & event) { 65 printf("%s: detected socket close with success\n", __func__); 66 return; 67 } 68 69 printf("%s: unable to detect socket close\n", __func__); 70 exit(1); 71 } 72 73 int 74 main(int argc, char **argv) 75 { 76 struct event_base *base; 77 struct event_config *cfg; 78 struct event *ev; 79 const char *test = "test string"; 80 evutil_socket_t pair[2]; 81 82 /* Initialize the library and check if the backend 83 supports EV_FEATURE_EARLY_CLOSE 84 */ 85 cfg = event_config_new(); 86 event_config_require_features(cfg, EV_FEATURE_EARLY_CLOSE); 87 base = event_base_new_with_config(cfg); 88 event_config_free(cfg); 89 if (!base) { 90 /* Backend doesn't support EV_FEATURE_EARLY_CLOSE */ 91 return 0; 92 } 93 94 /* Create a pair of sockets */ 95 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) 96 return (1); 97 98 /* Send some data on socket 0 and immediately close it */ 99 if (send(pair[0], test, (int)strlen(test)+1, 0) < 0) 100 return (1); 101 shutdown(pair[0], EVUTIL_SHUT_WR); 102 103 /* Dispatch */ 104 ev = event_new(base, pair[1], EV_CLOSED | EV_TIMEOUT, closed_cb, event_self_cbarg()); 105 event_add(ev, &timeout); 106 event_base_dispatch(base); 107 108 /* Finalize library */ 109 event_free(ev); 110 event_base_free(base); 111 return 0; 112 } 113 114