1275970Scy/* 2275970Scy * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson 3275970Scy * 4275970Scy * Redistribution and use in source and binary forms, with or without 5275970Scy * modification, are permitted provided that the following conditions 6275970Scy * are met: 7275970Scy * 1. Redistributions of source code must retain the above copyright 8275970Scy * notice, this list of conditions and the following disclaimer. 9275970Scy * 2. Redistributions in binary form must reproduce the above copyright 10275970Scy * notice, this list of conditions and the following disclaimer in the 11275970Scy * documentation and/or other materials provided with the distribution. 12275970Scy * 3. The name of the author may not be used to endorse or promote products 13275970Scy * derived from this software without specific prior written permission. 14275970Scy * 15275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16275970Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17275970Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18275970Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19275970Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21275970Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22275970Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23275970Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24275970Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25275970Scy */ 26275970Scy#include "../util-internal.h" 27275970Scy#include "event2/event-config.h" 28275970Scy 29275970Scy#ifdef _WIN32 30275970Scy#include <winsock2.h> 31275970Scy#endif 32275970Scy#include <sys/types.h> 33275970Scy#include <sys/stat.h> 34275970Scy#ifdef EVENT__HAVE_SYS_SOCKET_H 35275970Scy#include <sys/socket.h> 36275970Scy#endif 37275970Scy#include <fcntl.h> 38275970Scy#include <stdlib.h> 39275970Scy#include <stdio.h> 40275970Scy#include <string.h> 41275970Scy#ifndef _WIN32 42275970Scy#include <sys/time.h> 43275970Scy#include <unistd.h> 44275970Scy#endif 45275970Scy#include <errno.h> 46275970Scy 47275970Scy#include "event2/event.h" 48275970Scy#include "event2/util.h" 49275970Scy 50275970Scy#include "regress.h" 51275970Scy 52275970Scystatic int was_et = 0; 53275970Scy 54275970Scystatic void 55275970Scyread_cb(evutil_socket_t fd, short event, void *arg) 56275970Scy{ 57275970Scy char buf; 58275970Scy int len; 59275970Scy 60275970Scy len = recv(fd, &buf, sizeof(buf), 0); 61275970Scy 62275970Scy called++; 63275970Scy if (event & EV_ET) 64275970Scy was_et = 1; 65275970Scy 66275970Scy if (!len) 67275970Scy event_del(arg); 68275970Scy} 69275970Scy 70275970Scy#ifndef SHUT_WR 71275970Scy#define SHUT_WR 1 72275970Scy#endif 73275970Scy 74275970Scy#ifdef _WIN32 75275970Scy#define LOCAL_SOCKETPAIR_AF AF_INET 76275970Scy#else 77275970Scy#define LOCAL_SOCKETPAIR_AF AF_UNIX 78275970Scy#endif 79275970Scy 80275970Scystatic void 81275970Scytest_edgetriggered(void *et) 82275970Scy{ 83275970Scy struct event *ev = NULL; 84275970Scy struct event_base *base = NULL; 85275970Scy const char *test = "test string"; 86275970Scy evutil_socket_t pair[2] = {-1,-1}; 87275970Scy int supports_et; 88275970Scy 89275970Scy /* On Linux 3.2.1 (at least, as patched by Fedora and tested by Nick), 90275970Scy * doing a "recv" on an AF_UNIX socket resets the readability of the 91275970Scy * socket, even though there is no state change, so we don't actually 92275970Scy * get edge-triggered behavior. Yuck! Linux 3.1.9 didn't have this 93275970Scy * problem. 94275970Scy */ 95275970Scy#ifdef __linux__ 96275970Scy if (evutil_ersatz_socketpair_(AF_INET, SOCK_STREAM, 0, pair) == -1) { 97275970Scy tt_abort_perror("socketpair"); 98275970Scy } 99275970Scy#else 100275970Scy if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pair) == -1) { 101275970Scy tt_abort_perror("socketpair"); 102275970Scy } 103275970Scy#endif 104275970Scy 105275970Scy called = was_et = 0; 106275970Scy 107275970Scy tt_int_op(send(pair[0], test, (int)strlen(test)+1, 0), >, 0); 108275970Scy shutdown(pair[0], SHUT_WR); 109275970Scy 110275970Scy /* Initalize the event library */ 111275970Scy base = event_base_new(); 112275970Scy 113275970Scy if (!strcmp(event_base_get_method(base), "epoll") || 114275970Scy !strcmp(event_base_get_method(base), "epoll (with changelist)") || 115275970Scy !strcmp(event_base_get_method(base), "kqueue")) 116275970Scy supports_et = 1; 117275970Scy else 118275970Scy supports_et = 0; 119275970Scy 120275970Scy TT_BLATHER(("Checking for edge-triggered events with %s, which should %s" 121275970Scy "support edge-triggering", event_base_get_method(base), 122275970Scy supports_et?"":"not ")); 123275970Scy 124275970Scy /* Initalize one event */ 125275970Scy ev = event_new(base, pair[1], EV_READ|EV_ET|EV_PERSIST, read_cb, &ev); 126275970Scy 127275970Scy event_add(ev, NULL); 128275970Scy 129275970Scy /* We're going to call the dispatch function twice. The first invocation 130275970Scy * will read a single byte from pair[1] in either case. If we're edge 131275970Scy * triggered, we'll only see the event once (since we only see transitions 132275970Scy * from no data to data), so the second invocation of event_base_loop will 133275970Scy * do nothing. If we're level triggered, the second invocation of 134275970Scy * event_base_loop will also activate the event (because there's still 135275970Scy * data to read). */ 136275970Scy event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE); 137275970Scy event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE); 138275970Scy 139275970Scy if (supports_et) { 140275970Scy tt_int_op(called, ==, 1); 141275970Scy tt_assert(was_et); 142275970Scy } else { 143275970Scy tt_int_op(called, ==, 2); 144275970Scy tt_assert(!was_et); 145275970Scy } 146275970Scy 147275970Scy end: 148275970Scy if (ev) { 149275970Scy event_del(ev); 150275970Scy event_free(ev); 151275970Scy } 152275970Scy if (base) 153275970Scy event_base_free(base); 154275970Scy evutil_closesocket(pair[0]); 155275970Scy evutil_closesocket(pair[1]); 156275970Scy} 157275970Scy 158275970Scystatic void 159275970Scytest_edgetriggered_mix_error(void *data_) 160275970Scy{ 161275970Scy struct basic_test_data *data = data_; 162275970Scy struct event_base *base = NULL; 163275970Scy struct event *ev_et=NULL, *ev_lt=NULL; 164275970Scy 165275970Scy#ifdef EVENT__DISABLE_DEBUG_MODE 166275970Scy if (1) 167275970Scy tt_skip(); 168275970Scy#endif 169275970Scy 170275970Scy if (!libevent_tests_running_in_debug_mode) 171275970Scy event_enable_debug_mode(); 172275970Scy 173275970Scy base = event_base_new(); 174275970Scy 175275970Scy /* try mixing edge-triggered and level-triggered to make sure it fails*/ 176275970Scy ev_et = event_new(base, data->pair[0], EV_READ|EV_ET, read_cb, ev_et); 177275970Scy tt_assert(ev_et); 178275970Scy ev_lt = event_new(base, data->pair[0], EV_READ, read_cb, ev_lt); 179275970Scy tt_assert(ev_lt); 180275970Scy 181275970Scy /* Add edge-triggered, then level-triggered. Get an error. */ 182275970Scy tt_int_op(0, ==, event_add(ev_et, NULL)); 183275970Scy tt_int_op(-1, ==, event_add(ev_lt, NULL)); 184275970Scy tt_int_op(EV_READ, ==, event_pending(ev_et, EV_READ, NULL)); 185275970Scy tt_int_op(0, ==, event_pending(ev_lt, EV_READ, NULL)); 186275970Scy 187275970Scy tt_int_op(0, ==, event_del(ev_et)); 188275970Scy /* Add level-triggered, then edge-triggered. Get an error. */ 189275970Scy tt_int_op(0, ==, event_add(ev_lt, NULL)); 190275970Scy tt_int_op(-1, ==, event_add(ev_et, NULL)); 191275970Scy tt_int_op(EV_READ, ==, event_pending(ev_lt, EV_READ, NULL)); 192275970Scy tt_int_op(0, ==, event_pending(ev_et, EV_READ, NULL)); 193275970Scy 194275970Scyend: 195275970Scy if (ev_et) 196275970Scy event_free(ev_et); 197275970Scy if (ev_lt) 198275970Scy event_free(ev_lt); 199275970Scy if (base) 200275970Scy event_base_free(base); 201275970Scy} 202275970Scy 203275970Scystruct testcase_t edgetriggered_testcases[] = { 204275970Scy { "et", test_edgetriggered, TT_FORK, NULL, NULL }, 205275970Scy { "et_mix_error", test_edgetriggered_mix_error, 206275970Scy TT_FORK|TT_NEED_SOCKETPAIR|TT_NO_LOGS, &basic_setup, NULL }, 207275970Scy END_OF_TESTCASES 208275970Scy}; 209