kqueue: Deal with EINTR returns from kevent() by retrying.

When calling kevent() on upload queue overflow, or when
unregistering a file descriptor, or when handling ->notify_fd_sync(),
an EINTR return means that we should retry the call until it returns
something other than EINTR.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
poll: Deal with EINTR returns in ->notify_fd_sync().

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
select: Deal with EINTR returns in ->notify_fd_sync().

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_fd_pump: Deal with EINTR returns on read(2)/write(2)/splice(2).

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_event_raw: Include config.h.

So that HAVE_EVENTFD and HAVE_SYS_EVENTFD_H are passed through to us.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
port: Fix iv_port_notify_fd_sync() return type.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_fd_pump: Solaris 11 build fix.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_signal: Split out spinlock handling into spinlock.h.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_signal: Implement spinlocks by hand if pthread_spin_lock() unavailable.

Some platforms (such as OpenBSD 5.1) don't provide an implementation
of pthread_spin_lock() and friends, in which case we implement spinlocks
by hand, using gcc's __sync_*() intrinsics.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_inotify: Only install man pages if inotify support was detected.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_wait: Handle caller unregistration differently.

Perform detection of unregistration of the calling context via a
thread-local variable.  Aside from being cleaner, this also makes
the 'warning: dereferencing type-punned pointer will break
strict-aliasing rules' warning that we'd get on some platforms go
away.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
select: Save some space in struct iv_state.

As three of the four fd_set pointers stored in there can easily be
computed from the others.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
Introduce iv_fatal(), and route all fatal error conditions through it.

When ivykis detects a fatal error, we shouldn't unconditionally log
the error message to syslog like we do right now.

This commit introduces iv_fatal(), which is a printf-style varargs
function that takes a message for a fatal error, formats it, logs it,
and then aborts the current process.

By default, the message will be logged to syslog as before, unless
iv_set_fatal_msg_handler() was used to set up an alternative error
message log function, in which case it will be logged to that function
instead.

(Original idea from the Balabit ivykis tree.  Suggestion for marking
iv_fatal() with __attribute__((format(printf, 1, 2))) from Gergely
Nagy <algernon@balabit.hu>.)

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_fatal: Add a manual page and an example.

This is a little manual page for iv_fatal(3), and a trivial example
for it in iv_examples(3).  While we're at it, remove direct use of
syslog(2) in iv_examples(3), as that is now deprecated.

Signed-off-by: Gergely Nagy <algernon@balabit.hu>
Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
Pass in iv_poll_method::poll() timeout argument as a struct timespec.

Instead of converting the struct timespec that we get from
iv_get_soonest_timeout() into a millisecond count that some poll
methods then convert back to a struct timespec again.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
configure.ac: Don't test for stdint.h.

As we don't use it anywhere.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
Introduce __iv_list_steal_elements().

For moving all elements on a list to another list, to be used for
enforcing fairness in various parts of ivykis.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_task: Implement fairness by list splicing.

When iv_run_tasks() is called, we want it to only process tasks that
were registered before the function was called.  This is currently
done by inserting a sentinel element, but it's easier to just move
all tasks to a local list on entry to the function and then work off
the local list.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_event: Implement fairness by list splicing.

When iv_event_run_pending_events() is called, we want it to only
process events that were active before the function was called.  This
is currently done by inserting a sentinel element, but it's easier to
just move all events to a local list on entry to the function and
then work off the local list.  This simplifies the locking as well.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>
iv_wait: Implement fairness by list splicing.

When iv_wait_completion() is called, we want it to only process wait
events that were queued before the function was called.  We'll do
this in the same way as iv_task and iv_event do, by moving all wait
events to a local list on entry to the function and then working off
the local list.  This simplifies the locking as well.  If we exit
the function with wait events pending, iv_event guarantees that we'll
be called again at a later time.

Signed-off-by: Lennert Buytenhek <buytenh@wantstofly.org>