1*eabc0478Schristos /* $NetBSD: test-weof.c,v 1.6 2024/08/18 20:47:24 christos Exp $ */ 28585484eSchristos 38585484eSchristos /* 48585484eSchristos * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu> 58585484eSchristos * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 68585484eSchristos * 78585484eSchristos * Redistribution and use in source and binary forms, with or without 88585484eSchristos * modification, are permitted provided that the following conditions 98585484eSchristos * are met: 108585484eSchristos * 1. Redistributions of source code must retain the above copyright 118585484eSchristos * notice, this list of conditions and the following disclaimer. 128585484eSchristos * 2. Redistributions in binary form must reproduce the above copyright 138585484eSchristos * notice, this list of conditions and the following disclaimer in the 148585484eSchristos * documentation and/or other materials provided with the distribution. 158585484eSchristos * 3. The name of the author may not be used to endorse or promote products 168585484eSchristos * derived from this software without specific prior written permission. 178585484eSchristos * 188585484eSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 198585484eSchristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 208585484eSchristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 218585484eSchristos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 228585484eSchristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 238585484eSchristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 248585484eSchristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 258585484eSchristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 268585484eSchristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 278585484eSchristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 288585484eSchristos */ 29*eabc0478Schristos #include "../util-internal.h" 308585484eSchristos #include "event2/event-config.h" 318585484eSchristos 328585484eSchristos #ifdef _WIN32 338585484eSchristos #include <winsock2.h> 348585484eSchristos #else 358585484eSchristos #include <unistd.h> 368585484eSchristos #endif 378585484eSchristos #include <sys/types.h> 388585484eSchristos #include <sys/stat.h> 398585484eSchristos #ifdef EVENT__HAVE_SYS_TIME_H 408585484eSchristos #include <sys/time.h> 418585484eSchristos #endif 428585484eSchristos #ifdef EVENT__HAVE_SYS_SOCKET_H 438585484eSchristos #include <sys/socket.h> 448585484eSchristos #endif 458585484eSchristos #include <fcntl.h> 468585484eSchristos #include <stdlib.h> 478585484eSchristos #include <stdio.h> 488585484eSchristos #include <string.h> 498585484eSchristos #include <signal.h> 508585484eSchristos #include <errno.h> 518585484eSchristos 528585484eSchristos #include "event2/event.h" 538585484eSchristos #include "event2/event_struct.h" 548585484eSchristos #include "event2/event_compat.h" 558585484eSchristos #include "event2/util.h" 568585484eSchristos 578585484eSchristos evutil_socket_t pair[2]; 588585484eSchristos int test_okay = 1; 598585484eSchristos int called = 0; 608585484eSchristos 618585484eSchristos static void 628585484eSchristos write_cb(evutil_socket_t fd, short event, void *arg) 638585484eSchristos { 648585484eSchristos const char *test = "test string"; 658585484eSchristos int len; 668585484eSchristos 678585484eSchristos len = send(fd, test, (int)strlen(test) + 1, 0); 688585484eSchristos 698585484eSchristos printf("%s: write %d%s\n", __func__, 708585484eSchristos len, len ? "" : " - means EOF"); 718585484eSchristos 728585484eSchristos if (len > 0) { 738585484eSchristos if (!called) 748585484eSchristos event_add(arg, NULL); 758585484eSchristos evutil_closesocket(pair[0]); 768585484eSchristos } else if (called == 1) 778585484eSchristos test_okay = 0; 788585484eSchristos 798585484eSchristos called++; 808585484eSchristos } 818585484eSchristos 828585484eSchristos int 838585484eSchristos main(int argc, char **argv) 848585484eSchristos { 858585484eSchristos struct event ev; 868585484eSchristos 878585484eSchristos #ifdef _WIN32 888585484eSchristos WORD wVersionRequested; 898585484eSchristos WSADATA wsaData; 908585484eSchristos 918585484eSchristos wVersionRequested = MAKEWORD(2, 2); 928585484eSchristos 938585484eSchristos (void) WSAStartup(wVersionRequested, &wsaData); 948585484eSchristos #endif 958585484eSchristos 968585484eSchristos #ifndef _WIN32 978585484eSchristos if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) 988585484eSchristos return (1); 998585484eSchristos #endif 1008585484eSchristos 1018585484eSchristos if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) 1028585484eSchristos return (1); 1038585484eSchristos 104*eabc0478Schristos /* Initialize the event library */ 1058585484eSchristos event_init(); 1068585484eSchristos 107*eabc0478Schristos /* Initialize one event */ 1088585484eSchristos event_set(&ev, pair[1], EV_WRITE, write_cb, &ev); 1098585484eSchristos 1108585484eSchristos event_add(&ev, NULL); 1118585484eSchristos 1128585484eSchristos event_dispatch(); 1138585484eSchristos 1148585484eSchristos return (test_okay); 1158585484eSchristos } 1168585484eSchristos 117