So, I'd really like win32 libevent to be a first class libevent
citizen. Right now, trunk doesn't even compile.
Attached is a first cut at trying to make win32 work better: the big
issues are (1) win32.c still assumes that we're still using a global
signalqueue rather than per-base queue as the new code implements.
(The 1.3 branch "solves" this issue by just disabling the signalqueue
on windows entirely) and (2) signal.c assumes that everybody has a
working sigaction() function (which Windows does not).
Also, I'm pretty sure you can't use read and write for windows
sockets: only send and recv.
This may not be as gloriously threadsafe as the current non-Windows
code: this is where I'd like guidance.
Remaining Win32 issues in trunk:
(1) The win32 socketpair implementation in Win32-Code is all wrong.
It returns a pair of linked win32 pipes. But select() only
accepts sockets, so socketpair() in misc.c can't possibly work
for libevent's needs.
Once the present patch is in, I'll grab the fake-socketpair code
from Tor, which has a compatible license, and which seems to
work on win32 just fine.
Also, it might make sense to expose this as event_socketpair()
or something similar, so that all the libraries using libevent
that want a socketpair can have one without reinventing the
rickety dubious wheel.
(2) Almost none of the files test compile under win32; in fact, they
seem to be disabled. I've make some progress here, but so many
of them require a working socketpair() that I'm going to hold
off until the above issues are solved.
Thoughts?
--
Nick Mathewson
Index: configure.in
===
--- configure.in(revision 440)
+++ configure.in(working copy)
@@ -42,7 +42,7 @@
dnl Checks for header files.
AC_HEADER_STDC
-AC_CHECK_HEADERS(fcntl.h stdarg.h inttypes.h stdint.h poll.h signal.h unistd.h
sys/epoll.h sys/time.h sys/queue.h sys/event.h sys/ioctl.h sys/select.h
sys/devpoll.h port.h netinet/in6.h)
+AC_CHECK_HEADERS(fcntl.h stdarg.h inttypes.h stdint.h poll.h signal.h unistd.h
sys/epoll.h sys/time.h sys/queue.h sys/event.h sys/ioctl.h sys/select.h
sys/devpoll.h port.h netinet/in6.h netdb.h)
if test "x$ac_cv_header_sys_queue_h" = "xyes"; then
AC_MSG_CHECKING(for TAILQ_FOREACH in sys/queue.h)
AC_EGREP_CPP(yes,
@@ -135,7 +135,7 @@
AC_HEADER_TIME
dnl Checks for library functions.
-AC_CHECK_FUNCS(gettimeofday vasprintf fcntl clock_gettime strtok_r strsep
getaddrinfo getnameinfo strlcpy inet_ntop)
+AC_CHECK_FUNCS(gettimeofday vasprintf fcntl clock_gettime strtok_r strsep
getaddrinfo getnameinfo strlcpy inet_ntop signal sigaction)
if test "x$ac_cv_func_clock_gettime" = "xyes"; then
AC_DEFINE(DNS_USE_CPU_CLOCK_FOR_ID, 1, [Define if clock_gettime is
available in libc])
@@ -333,6 +333,9 @@
AC_LIBOBJ(evport)
needsignal=yes
fi
+if test "x$bwin32" = "xtrue"; then
+ needsignal=yes
+fi
if test "x$needsignal" = "xyes" ; then
AC_LIBOBJ(signal)
fi
Index: signal.c
===
--- signal.c(revision 440)
+++ signal.c(working copy)
@@ -30,6 +30,12 @@
#include "config.h"
#endif
+#ifdef WIN32
+#define WIN32_LEAN_AND_MEAN
+#include
+#include
+#undef WIN32_LEAN_AND_MEAN
+#endif
#include
#include
#ifdef HAVE_SYS_TIME_H
@@ -38,7 +44,9 @@
#include
#endif
#include
+#ifdef HAVE_SYS_SOCKET_H
#include
+#endif
#include
#include
#include
@@ -67,7 +75,7 @@
struct event *ev = arg;
ssize_t n;
- n = read(fd, signals, sizeof(signals));
+ n = recv(fd, signals, sizeof(signals), 0);
if (n == -1)
event_err(1, "%s: read", __func__);
event_add(ev, NULL);
@@ -98,7 +106,14 @@
base->sig.evsignal_caught = 0;
memset(&base->sig.evsigcaught, 0, sizeof(sig_atomic_t)*NSIG);
+#ifdef WIN32
+ {
+ u_long nonblocking = 1;
+ ioctlsocket(base->sig.ev_signal_pair[0], FIONBIO, &nonblocking);
+ }
+#else
fcntl(base->sig.ev_signal_pair[0], F_SETFL, O_NONBLOCK);
+#endif
event_set(&base->sig.ev_signal, base->sig.ev_signal_pair[1], EV_READ,
evsignal_cb, &base->sig.ev_signal);
@@ -110,13 +125,16 @@
evsignal_add(struct event *ev)
{
int evsignal;
+#ifdef HAVE_SIGACTION
struct sigaction sa;
+#endif
struct event_base *base = ev->ev_base;
if (ev->ev_events & (EV_READ|EV_WRITE))
event_errx(1, "%s: EV_SIGNAL incompatible use", __func__);
evsignal = EVENT_SIGNAL(ev);
+#ifdef HAVE_SIGACTION
memset(&sa, 0, sizeof(sa));
sa.sa_handler = evsignal_handler;
sigfillset(&sa.sa_mask);
@@ -126,6 +144,11 @@
if (sigaction(evsignal, &sa, NULL) == -1)
return (-1);
+#else
+ evsignal_base = base;
+ if (signal(evsignal, evsig