Nick Mathewson [Sun, 9 May 2010 03:29:29 +0000 (23:29 -0400)]
Fix some autoconf issues on OpenBSD
Issue 1: autoconf gets accept when a header works properly with cpp
but not with cc. This was true of the sys/sysctl.h header on
openbsd. The fix: include sys/param.h (if present) when testing for
sys/sysctl.h
Issue 2: Somehow, autoconf's macro generation code is messed up on
some versions of openbsd (including mine, and other people's too) so
that instead of SIZEOF_VOID_P, it makes SIZEOF_VOID__.
evutil/util.h now works around that.
Nick Mathewson [Sun, 9 May 2010 02:21:52 +0000 (22:21 -0400)]
Define _REENTRANT as needed on Solaris, elsewhere
It turns out that _REENTRANT isn't only needed to make certain
functions visible; we also need it to make pthreads work properly
some places (like Solaris, where forgetting _REENTRANT basically
means that all threads are sharing the same errno). Fortunately,
our ACX_PTHREAD() configure macro already gives us a PTHREAD_CFLAG
variable, so all we have to do is use it.
Nick Mathewson [Sat, 8 May 2010 23:56:25 +0000 (19:56 -0400)]
Fix test.sh on freebsd
It turns out that in all conformant shells, "unset FOO" removes FOO
both from the shell's variables and from the exported environment.
(I've tested this on msys, opensolaris, linux, osx, and freebsd.)
And in nearly every shell I can find, "unset FOO; export FOO" does
the same as unset FOO... except in my FreeBSD VM, where the "export
FOO" sets the exported value of FOO equal to "". This broke test.sh
for us.
Nick Mathewson [Sat, 8 May 2010 22:00:26 +0000 (18:00 -0400)]
Make test.sh support mingw/msys on win32
This required:
- Adding another WIN32 section in test.sh
- not running "touch /dev/null"
- calling WSAStartup in all the test binaries
- Fixing a dumb windows-only bug in test-time.c
Nick Mathewson [Sat, 8 May 2010 23:16:47 +0000 (19:16 -0400)]
Fix another nasty solaris getaddrinfo() behavior
Everybody else thinks that when you getaddrinfo() on an ip address
and don't specify the protocol and the socktype, it should give you
multiple answers , one for each protocol/socktype implementation.
OpenSolaris takes a funny view of RFC3493, and leaves the results set
to 0.
This patch post-processes the getaddrinfo() results for consistency.
Nick Mathewson [Wed, 21 Apr 2010 15:57:55 +0000 (11:57 -0400)]
Fix getaddrinfo with protocol unset on Solaris 9. Found by Dagobert Michelsen
Apparently when you call Solaris 9's getaddrinfo(), it likes to leave
ai_protocol unset in the result. This is no way to behave, if I'm
reading RFC3493 right.
This patch makes us check for a getaddrinfo() that's broken in this way,
and work around it by trying to infer socktype and protocol from one
another.
Nick Mathewson [Sat, 8 May 2010 22:09:27 +0000 (18:09 -0400)]
Numerous opensolaris compilation fixes
For future note, opensolaris doesn't have sys/sysctl.h, doesn't like
comparing iov_buf to a chain_space_ptr without a cast, and is (predictably)
unforgiving of dumb syntax errors.
Also, we had accidentally broken the devpoll backend test in configure.in
Nick Mathewson [Sat, 8 May 2010 23:11:50 +0000 (19:11 -0400)]
Make test for bufferevent_connect_hostname system-neutral
Previously, the be5_outcome field for the dns error would be set to
something dependent on our system resolver. It turns out that you
can't rely on nameservers to really give you an NEXIST answer for
xyz.example.com nowadays: too many of them are annoyingly broken and
like to redirect you to their locked-in portals. This patch changes
the bufferevent_connect_hostname test so that it makes sure that the
dns_error of be5_outcome is "whatever you would get from resolving
the target hostname"
Nick Mathewson [Sat, 8 May 2010 23:09:09 +0000 (19:09 -0400)]
Make unit test for add_file able to tell "error" from "done"
Importantly, we don't actually want to call evbuffer_write() when
the buffer is empty. This makes it an error to ever get a -1 return
value from evbuffer_add_file(), which makes it safe for us to test
the return value.
Nick Mathewson [Thu, 6 May 2010 18:37:23 +0000 (14:37 -0400)]
Only specify -no-undefined on mingw
It turns out that commit 3cbca8661f broke building with shared
libraries on OSX. Since -no-undefined is only necessary on platforms
like win32, only use it there.
There may be a better fix for this. Should fix bug 2997775.
Nick Mathewson [Thu, 6 May 2010 17:26:05 +0000 (13:26 -0400)]
Stop distributing and installing manpages: they were too inaccurate
It would be great to have the manpages come back some time, perhaps
from a refactoring of my asciidoc book, but for now the existing
manpages were the single worst, most incomplete, and most misleading
libevent documentation we had. (Less misleading: the doxygen output,
the header files, and my reference book.)
Nick Mathewson [Tue, 4 May 2010 17:27:36 +0000 (13:27 -0400)]
Rename current_base symbol to event_global_current_base_
The "current_base" symbol was never actually declared in an exported
header; it's hideously deprecated, and it was the one remaining
exported symbol (fwict) that was prefixed with neither ev nor
bufferevent nor _ev nor _bufferevent.
codesearch.google.com turns up no actual attempts to use our
current_base from outside libevent.
Nick Mathewson [Tue, 4 May 2010 16:57:40 +0000 (12:57 -0400)]
Fix symbol conflict between mm_*() macros and libmm
Our mm_malloc, mm_calloc, etc functions were all exported, since C
hasn't got a nice portable way to say "we want to use this function
inside our library but not export it to others". But they apparently
conflict with anything else that calls its symbols mm_*, as libmm does.
This patch renames the mm_*() functions to event_mm_*_(, and defines
maros in mm_internal so that all the code we have that uses mm_*()
will still work. New code should also prefer the mm_*() macro names.
Nick Mathewson [Mon, 3 May 2010 17:00:00 +0000 (13:00 -0400)]
Try /proc on Linux as entropy fallback; use sysctl as last resort
It turns out that the happy fun Linux kernel is deprecating sysctl,
and using sysctl to fetch entropy will spew messages in the kernel
logs. Let's not do that. Instead, let's call sysctl for our
entropy only when all other means fail.
Additionally, let's add another means, and try
/proc/sys/kernel/random/uuid if /dev/urandom fails.
Every current BSD system providing TAILQ_* macros define
TAILQ_FOREACH_REVERSE in this order:
TAILQ_FOREACH_REVERSE(var, head, field, headname)
However, libevent defines it in another order:
TAILQ_FOREACH_REVERSE(var, head, headname, field)
Here's a trivial patch to have libevent compatible with stock queue.h headers.
-Frank.
[From sourceforge patch 2995179. codesearch.google.com confirms that
the only people defining TAILQ_FOREACH_REVERSE our way are people
using it in a compatibility header like us. Did we copy this from
OpenSSH or something?]
Nick Mathewson [Wed, 28 Apr 2010 19:16:32 +0000 (15:16 -0400)]
Remove redundant checks for lock!=NULL before calling EVLOCK_LOCK
The EVLOCK_LOCK and EVLOCK_UNLOCK macros already check to make sure
that the lock is present before messing with it, so there's no point
in checking the lock before calling them.
A good compiler should be able to simplify code like
if (lock) {
if (lock)
acquire(lock);
}
, but why count on it?
Nick Mathewson [Wed, 28 Apr 2010 16:03:08 +0000 (12:03 -0400)]
Catch attempts to enable debug_mode too late
Debug mode needs to be enabled before any event is setup or any
event_base is created. Otherwise, we will not have recorded when events
were first setup or added, and so it will look like a bug later when we
delete or free them.
I have already confused myself because of this requirement, so let's
make Libevent catch it for the next poor forgetful developer like me.
Nick Mathewson [Wed, 28 Apr 2010 15:51:56 +0000 (11:51 -0400)]
Make debug mode catch mixed ET and non-ET events on an fd
Of the backends that support edge-triggered IO, most (all?) do not
support attempts to mix edge-triggered and level-triggered IO on the
same FD. With debugging mode enabled, we now detect and refuse attempts
to add a level-triggered IO event to an fd that already has an
edge-triggered IO event, and vice versa.
Nick Mathewson [Fri, 23 Apr 2010 20:08:09 +0000 (16:08 -0400)]
Fix a couple of bugs in the BSD sysctl arc4seed logic
Of course, FreeBSD has its own arc4random() implementation, so this should
never actually be needed. Still, it's good to paint the underside of the
wagon.
Nick Mathewson [Thu, 4 Mar 2010 06:13:51 +0000 (01:13 -0500)]
Seed the RNG using sysctl() as well as /dev/urandom
William Ahern points out that if the user has chrooted, they might not
have a working /dev/urandom. Linux and many of the BSDs, however,
define a sysctl interface to their kernel random number generators.
This patch takes a belt-and-suspenders approach and tries to do use the
sysctl _and_ the /dev/urandom approach if both are present. When using
the sysctl approach, it tries to bulletproof itself by checking to make
sure that the buffers are actually set by the sysctl calls.
Nick Mathewson [Fri, 23 Apr 2010 18:42:25 +0000 (14:42 -0400)]
Make evdns logging threadsafe
The old logging code was littered with places where we stored messages in
static char[] fields. This is fine in a single-threaded program, but if you
ever tried to log evdns messages from two threads at once, you'd hit a race.
This patch also refactors evdns's debug_ntop function into a more useful
evutil_sockaddr_port_format() function, with unit tests.
When searching is enabled, evdns may make multiple requests before
calling the user callback with the result. This is a problem because
the same evdns_request handle is not retained for each search request,
so the user cannot reliably cancel the request.
This patch attempts to ensure that evdns_request persists accross
search requests.
Clean up properly when adding a signal handler fails.
Previously, when a signation() or signal() call failed, we would free
the element we added to sh_old, but not actually clear the pointer.
This would leave a dangling pointer in sh_old that could cause a
crash later.
The EVUTIL_CLOSESOCKET() macro required you to include unistd.h in your
source for POSIX. We might as well turn it into a function: an extra
function call is going to be cheap in comparison with the system call.
We retain the EVUTIL_CLOSESOCKET() macro as an alias for the new
evutil_closesocket() function.
Add void* arguments to request_new and reply_new evrpc hooks
This makes evprc setup more extensible, and helps with Shuo Chen's
work on implementing Google protocol buffers rpc on top of Libevent 2
evrpc.
This patch breaks binary compatibility with previous versions of
Libevent, since it changes struct evrpc and the signature of
evrpc_register_generic(). Since all compliant code should be calling
evrpc_register_generic via EVRPC_REGISTER, it shouldn't break source
compatibility.
Nick Mathewson [Tue, 13 Apr 2010 02:24:54 +0000 (22:24 -0400)]
Add evbuffer_copyout to copy data from an evbuffer without draining
The evbuffer_remove() function copies data from the front of an
evbuffer into an array of char, and removes the data from the buffer.
This function behaves the same, but does not remove the data. This
behavior can be handy for lots of protocols, where you want the
evbuffer to accumulate data until a complete record has arrived.
Lots of people have asked for a function more or less like this, and
though it isn't too hard to code one from evbuffer_peek(), it is
apparently annoying to do it in every app you write. The
evbuffer_peek() function is significantly faster, but it requires that
the user be able to handle data in separate extents.
This patch also reimplements evbufer_remove() as evbuffer_copyout()
followed by evbuffer_drain(). I am reasonably confident that this
won't be a performance hit: the memcpy() overhead should dominate the
cost of walking the list an extra time.
Nick Mathewson [Tue, 30 Mar 2010 20:47:37 +0000 (16:47 -0400)]
Rewrite evbuffer_expand and its users
The previous evbuffer_expand was not only incorrect; it was
inefficient too. On all questions of time vs memory tradeoffs, it
chose to burn time in order to avoid wasting memory. The new code
tries to be a little more balanced: it only resizes an existing chain
when doing so doesn't require too much copying, and when failing to do
so would waste a lot of the chain's space.
This patch also rewrites evbuffer_chain_insert to work properly with
last_with_datap, and adds a few convenience functions to buffer.c.
Nick Mathewson [Sat, 27 Mar 2010 04:09:25 +0000 (00:09 -0400)]
Make the no_iovecs case of write_atmost compile
Apparently nobody had tested it before on a system that had sendfile.
Why would you have sendfile and not writev? Perhaps you're trying to
test the no-iovecs code to make sure it still works.
Nick Mathewson [Sat, 27 Mar 2010 03:18:40 +0000 (23:18 -0400)]
Replace last_with_data with a slightly smarter version
To implement evbuffer_expand() properly, you need to be able to
replace the last chunk that has data, which means that we need to keep
track of the the next pointer pointing to the last_with_data chunk,
not the last_with_data chunk itself.
Nick Mathewson [Wed, 31 Mar 2010 16:29:26 +0000 (12:29 -0400)]
Fix critical bug in evbuffer_write when writev is not available
evbuffer_pullup() returns NULL if you try to pull up more bytes than
are there. But evbuffer_write_atmost would sometimes ask for more
bytes to be pulled up than it had, get a NULL, and fail.
Nick Mathewson [Fri, 26 Mar 2010 18:51:39 +0000 (14:51 -0400)]
Make evbuffer_prepend handle empty buffers better
If the first chunk of a buffer is empty, and we're told to prepend to
the buffer, we should be willing to use the entire first chunk.
Instead, we were dependent on the value of chunk->misalign.
Nick Mathewson [Fri, 26 Mar 2010 18:30:14 +0000 (14:30 -0400)]
Increase MIN_BUFFER_SIZE to 512 (1024 on 64-bit)
This constant decides the smallest (and typical) size of each evbuffer
chain. Since this number includes sizeof(evbuffer_chain) overhead,
the old value (256) was just too low: on 64-bit platforms, it would
spend nearly 20% of the allocations on overhead. The new values mean
that we'll be spending closer to 5% of evbuffer allocations on overhead.
It would be nice to get this number even lower if we can.
Nick Mathewson [Fri, 26 Mar 2010 17:46:29 +0000 (13:46 -0400)]
Remove a needless min_heap_shift_up_() call
Previously, every call to min_heap_shift_down_() would invoke
min_heap_shift_up_() at the end. This used to be necessary in the
first version of the minheap code, since min_heap_erase() would call
min_heap_shift_down_() unconditionally. But when patch 8b7a3b36763
from Marko Kreen fixed min_heap_erase() to be more sensible, we left
the weird behavior of min_heap_shift_down_() in place.
Fortunately, "cui" noticed this and reported it on Niels's blog.
Trond Norbye [Tue, 23 Mar 2010 17:27:10 +0000 (13:27 -0400)]
Never test for select() on windows
On 64-bit windows, configure actually _finds_ select when it tests for
it, and due to the ordering of the io implementations in event.c it is
chosen over the win32select implementation.
This modification skips the test for select on win32 (we don't want
that anyway, because Windows has its own), causing my windows box to
get the win32select implementation.
Nick Mathewson [Sun, 21 Mar 2010 17:28:48 +0000 (13:28 -0400)]
Detect and refuse reentrant event_base_loop() calls
Calling event_base_loop on a base from inside a callback invoked by
that same base, or from two threads at once, has long been a way to
get exceedingly hard-to-diagnose errors. This patch adds code to
detect such reentrant invocatinos, and exit quickly with a warning
that should explain what went wrong.
Nick Mathewson [Tue, 16 Mar 2010 17:37:15 +0000 (13:37 -0400)]
Make 'main/many_events' test 70 fds, not 64.
This is mainly intended to ensure that we don't get hung up on
the 64-handle limit that lots of O(n) Windows functions (but FWICT
not select) like to enforce.
Nick Mathewson [Sat, 13 Mar 2010 06:04:30 +0000 (01:04 -0500)]
Avoid an (untriggerable so far) crash bug in bufferevent_free()
We were saying
mm_free(bufev - bufev->be_ops->mem_offset);
when we should have said
mm_free(((char*)bufev) - bufev->be_ops->mem_offset);
In other words, if mem_offset had ever been nonzero, then instead of
backing up mem_offset bytes to find the thing we were supposed to free, we
would have backed up mem_offset*sizeof(struct bufferevent) bytes, and freed
something completely crazy.
Spotted thanks to a conversation with Jardel Weyrich
Nick Mathewson [Sat, 13 Mar 2010 05:53:54 +0000 (00:53 -0500)]
Free evdns_base->req_heads on evdns_base_free
It looks like when we moved from one big inflight-requests list to an
n-heads structure, we didn't make evdns_base_free() free the array of
heads. This patch should fix that.
Nick Mathewson [Fri, 12 Mar 2010 23:35:15 +0000 (18:35 -0500)]
Avoid a spurious close(-1) on Linux
On Linux, we use only one fd to do main-thread signaling (since we have
eventfd()), so we don't need to close th_notify_fd[1] as we would if we were
using a socketpair.