1275970Scy/*
2275970Scy * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
3275970Scy * Copyright (c) 2007-2012 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 <signal.h>
47275970Scy#include <errno.h>
48275970Scy
49275970Scy#include "event2/event.h"
50275970Scy#include "event2/event_struct.h"
51275970Scy#include "event2/event_compat.h"
52275970Scy#include "event2/util.h"
53275970Scy
54275970Scy#ifdef EVENT____func__
55275970Scy#define __func__ EVENT____func__
56275970Scy#endif
57275970Scy
58275970Scyevutil_socket_t pair[2];
59275970Scyint test_okay = 1;
60275970Scyint called = 0;
61275970Scy
62275970Scystatic void
63275970Scywrite_cb(evutil_socket_t fd, short event, void *arg)
64275970Scy{
65275970Scy	const char *test = "test string";
66275970Scy	int len;
67275970Scy
68275970Scy	len = send(fd, test, (int)strlen(test) + 1, 0);
69275970Scy
70275970Scy	printf("%s: write %d%s\n", __func__,
71275970Scy	    len, len ? "" : " - means EOF");
72275970Scy
73275970Scy	if (len > 0) {
74275970Scy		if (!called)
75275970Scy			event_add(arg, NULL);
76275970Scy		evutil_closesocket(pair[0]);
77275970Scy	} else if (called == 1)
78275970Scy		test_okay = 0;
79275970Scy
80275970Scy	called++;
81275970Scy}
82275970Scy
83275970Scyint
84275970Scymain(int argc, char **argv)
85275970Scy{
86275970Scy	struct event ev;
87275970Scy
88275970Scy#ifdef _WIN32
89275970Scy	WORD wVersionRequested;
90275970Scy	WSADATA wsaData;
91275970Scy
92275970Scy	wVersionRequested = MAKEWORD(2, 2);
93275970Scy
94275970Scy	(void) WSAStartup(wVersionRequested, &wsaData);
95275970Scy#endif
96275970Scy
97275970Scy#ifndef _WIN32
98275970Scy	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
99275970Scy		return (1);
100275970Scy#endif
101275970Scy
102275970Scy	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
103275970Scy		return (1);
104275970Scy
105275970Scy	/* Initalize the event library */
106275970Scy	event_init();
107275970Scy
108275970Scy	/* Initalize one event */
109275970Scy	event_set(&ev, pair[1], EV_WRITE, write_cb, &ev);
110275970Scy
111275970Scy	event_add(&ev, NULL);
112275970Scy
113275970Scy	event_dispatch();
114275970Scy
115275970Scy	return (test_okay);
116275970Scy}
117275970Scy
118