test-closed.c revision 275970
1275970Scy/* 2275970Scy * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu> 3275970Scy * Copyright (c) 2007-2013 Niels Provos and Nick Mathewson 4275970Scy * 5275970Scy * Redistribution and use in source and binary forms, with or without 6275970Scy * modification, are permitted provided that the following conditions 7275970Scy * are met: 8275970Scy * 1. Redistributions of source code must retain the above copyright 9275970Scy * notice, this list of conditions and the following disclaimer. 10275970Scy * 2. Redistributions in binary form must reproduce the above copyright 11275970Scy * notice, this list of conditions and the following disclaimer in the 12275970Scy * documentation and/or other materials provided with the distribution. 13275970Scy * 3. The name of the author may not be used to endorse or promote products 14275970Scy * derived from this software without specific prior written permission. 15275970Scy * 16275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17275970Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18275970Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19275970Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20275970Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22275970Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23275970Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24275970Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25275970Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26275970Scy */ 27275970Scy#include "event2/event-config.h" 28275970Scy 29275970Scy#ifdef _WIN32 30275970Scy#include <winsock2.h> 31275970Scy#else 32275970Scy#include <unistd.h> 33275970Scy#endif 34275970Scy#include <sys/types.h> 35275970Scy#include <sys/stat.h> 36275970Scy#ifdef EVENT__HAVE_SYS_TIME_H 37275970Scy#include <sys/time.h> 38275970Scy#endif 39275970Scy#ifdef EVENT__HAVE_SYS_SOCKET_H 40275970Scy#include <sys/socket.h> 41275970Scy#endif 42275970Scy#include <fcntl.h> 43275970Scy#include <stdlib.h> 44275970Scy#include <stdio.h> 45275970Scy#include <string.h> 46275970Scy#include <errno.h> 47275970Scy 48275970Scy#include <event.h> 49275970Scy#include <evutil.h> 50275970Scy 51275970Scy#ifdef EVENT____func__ 52275970Scy#define __func__ EVENT____func__ 53275970Scy#endif 54275970Scy 55275970Scystruct timeval timeout = {3, 0}; 56275970Scy 57275970Scystatic void 58275970Scyclosed_cb(evutil_socket_t fd, short event, void *arg) 59275970Scy{ 60275970Scy if (EV_TIMEOUT & event) { 61275970Scy printf("%s: Timeout!\n", __func__); 62275970Scy exit(1); 63275970Scy } 64275970Scy 65275970Scy if (EV_CLOSED & event) { 66275970Scy printf("%s: detected socket close with success\n", __func__); 67275970Scy return; 68275970Scy } 69275970Scy 70275970Scy printf("%s: unable to detect socket close\n", __func__); 71275970Scy exit(1); 72275970Scy} 73275970Scy 74275970Scy#ifndef SHUT_WR 75275970Scy#define SHUT_WR 1 76275970Scy#endif 77275970Scy 78275970Scyint 79275970Scymain(int argc, char **argv) 80275970Scy{ 81275970Scy struct event_base *base; 82275970Scy struct event_config *cfg; 83275970Scy struct event *ev; 84275970Scy const char *test = "test string"; 85275970Scy evutil_socket_t pair[2]; 86275970Scy 87275970Scy /* Initialize the library and check if the backend 88275970Scy supports EV_FEATURE_EARLY_CLOSE 89275970Scy */ 90275970Scy cfg = event_config_new(); 91275970Scy event_config_require_features(cfg, EV_FEATURE_EARLY_CLOSE); 92275970Scy base = event_base_new_with_config(cfg); 93275970Scy event_config_free(cfg); 94275970Scy if (!base) { 95275970Scy /* Backend doesn't support EV_FEATURE_EARLY_CLOSE */ 96275970Scy return 0; 97275970Scy } 98275970Scy 99275970Scy /* Create a pair of sockets */ 100275970Scy if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) 101275970Scy return (1); 102275970Scy 103275970Scy /* Send some data on socket 0 and immediately close it */ 104275970Scy if (send(pair[0], test, (int)strlen(test)+1, 0) < 0) 105275970Scy return (1); 106275970Scy shutdown(pair[0], SHUT_WR); 107275970Scy 108275970Scy /* Dispatch */ 109275970Scy ev = event_new(base, pair[1], EV_CLOSED | EV_TIMEOUT, closed_cb, event_self_cbarg()); 110275970Scy event_add(ev, &timeout); 111275970Scy event_base_dispatch(base); 112275970Scy 113275970Scy /* Finalize library */ 114275970Scy event_base_free(base); 115275970Scy return 0; 116275970Scy} 117275970Scy 118