parser/sdp: more suggestive debug message
[sip-router] / io_wait.h
index 0557c00..b7ae925 100644 (file)
--- a/io_wait.h
+++ b/io_wait.h
@@ -1,6 +1,6 @@
-/* 
+/*
  * $Id$
- * 
+ *
  * Copyright (C) 2005 iptelorg GmbH
  *
  * Permission to use, copy, modify, and distribute this software for any
@@ -31,9 +31,9 @@
  *                 this assumption)
  *     local_malloc (defaults to pkg_malloc)
  *     local_free   (defaults to pkg_free)
- *  
+ *
  */
-/* 
+/*
  * History:
  * --------
  *  2005-06-13  created by andrei
@@ -45,6 +45,7 @@
  *  2007-11-29  support for write (POLLOUT); added io_watch_chg() (andrei)
  *  2008-02-04  POLLRDHUP & EPOLLRDHUP support (automatically enabled if POLLIN
  *               is set) (andrei)
+ *  2010-06-17  re-enabled & enhanced the EV_ERROR for kqueue (andrei)
  */
 
 
@@ -78,8 +79,8 @@
 #endif
 #ifdef HAVE_SELECT
 /* needed on openbsd for select*/
-#include <sys/time.h> 
-#include <sys/types.h> 
+#include <sys/time.h>
+#include <sys/types.h>
 #include <unistd.h>
 /* needed according to POSIX for select*/
 #include <sys/select.h>
@@ -108,7 +109,7 @@ extern int _os_ver; /* os version number, needed to select bugs workarrounds */
 
 #if 0
 enum fd_types; /* this should be defined from the including file,
-                                 see tcp_main.c for an example, 
+                                 see tcp_main.c for an example,
                                  0 has a special meaning: not used/empty*/
 #endif
 
@@ -146,8 +147,10 @@ struct io_wait_handler{
        enum poll_types poll_method;
        int flags;
        struct fd_map* fd_hash;
-       int fd_no; /*  current index used in fd_array and the passed size for 
-                                  ep_array & kq_array*/
+       int fd_no; /*  current index used in fd_array and the passed size for
+                                  ep_array (for kq_array at least
+                                   max(twice the size, kq_changes_size) should be
+                                  be passed). */
        int max_fd_no; /* maximum fd no, is also the size of fd_array,
                                                       fd_hash  and ep_array*/
        /* common stuff for POLL, SIGIO_RT and SELECT
@@ -169,6 +172,7 @@ struct io_wait_handler{
        struct kevent* kq_array;   /* used for the eventlist*/
        struct kevent* kq_changes; /* used for the changelist */
        size_t kq_nchanges;
+       size_t kq_array_size;   /* array size */
        size_t kq_changes_size; /* size of the changes array */
 #endif
 #ifdef HAVE_DEVPOLL
@@ -218,7 +222,7 @@ static inline struct fd_map* hash_fd_map(   io_wait_h* h,
  *          events - combinations of POLLIN, POLLOUT, POLLERR & POLLHUP
  *          idx    - index in the fd_array (or -1 if not known)
  * return: -1 on error
- *          0 on EAGAIN or when by some other way it is known that no more 
+ *          0 on EAGAIN or when by some other way it is known that no more
  *            io events are queued on the fd (the receive buffer is empty).
  *            Usefull to detect when there are no more io events queued for
  *            sigio_rt, epoll_et, kqueue.
@@ -242,10 +246,11 @@ int handle_io(struct fd_map* fm, short events, int idx);
  *       and EVFILT_WRITE, EV_ADD for the same fd).
  * returns: -1 on error, 0 on success
  */
-static inline int kq_ev_change(io_wait_h* h, int fd, int filter, int flag, 
+static inline int kq_ev_change(io_wait_h* h, int fd, int filter, int flag,
                                                                void* data)
 {
        int n;
+       int r;
        struct timespec tspec;
 
        if (h->kq_nchanges>=h->kq_changes_size){
@@ -256,11 +261,35 @@ static inline int kq_ev_change(io_wait_h* h, int fd, int filter, int flag,
                tspec.tv_nsec=0;
 again:
                n=kevent(h->kq_fd, h->kq_changes, h->kq_nchanges, 0, 0, &tspec);
-               if (n==-1){
-                       if (errno==EINTR) goto again;
-                       LOG(L_ERR, "ERROR: io_watch_add: kevent flush changes "
-                                               " failed: %s [%d]\n", strerror(errno), errno);
-                       return -1;
+               if (unlikely(n == -1)){
+                       if (unlikely(errno == EINTR)) goto again;
+                       else {
+                               /* for a detailed explanation of what follows see
+                                  io_wait_loop_kqueue EV_ERROR case */
+                               if (unlikely(!(errno == EBADF || errno == ENOENT)))
+                                       BUG("kq_ev_change: kevent flush changes failed"
+                                                       " (unexpected error): %s [%d]\n",
+                                                       strerror(errno), errno);
+                                       /* ignore error even if it's not a EBADF/ENOENT */
+                               /* one of the file descriptors is bad, probably already
+                                  closed => try to apply changes one-by-one */
+                               for (r = 0; r < h->kq_nchanges; r++) {
+retry2:
+                                       n = kevent(h->kq_fd, &h->kq_changes[r], 1, 0, 0, &tspec);
+                                       if (n==-1) {
+                                               if (unlikely(errno == EINTR))
+                                                       goto retry2;
+                                       /* for a detailed explanation of what follows see
+                                               io_wait_loop_kqueue EV_ERROR case */
+                                               if (unlikely(!(errno == EBADF || errno == ENOENT)))
+                                                       BUG("kq_ev_change: kevent flush changes failed:"
+                                                                       " (unexpected error) %s [%d] (%d/%lu)\n",
+                                                                               strerror(errno), errno,
+                                                                               r, (unsigned long)h->kq_nchanges);
+                                               continue; /* skip over it */
+                                       }
+                               }
+                       }
                }
                h->kq_nchanges=0; /* changes array is empty */
        }
@@ -395,7 +424,7 @@ inline static int io_watch_add(     io_wait_h* h,
 #ifdef HAVE_SIGIO_RT
                case POLL_SIGIO_RT:
                        fd_array_setup(events);
-                       /* re-set O_ASYNC might be needed, if not done from 
+                       /* re-set O_ASYNC might be needed, if not done from
                         * io_watch_del (or if somebody wants to add a fd which has
                         * already O_ASYNC/F_SETSIG set on a duplicate)
                         */
@@ -472,7 +501,7 @@ again2:
                case POLL_KQUEUE:
                        if (likely( events & POLLIN)){
                                if (unlikely(kq_ev_change(h, fd, EVFILT_READ, EV_ADD, e)==-1))
-                               goto error;
+                                       goto error;
                        }
                        if (unlikely( events & POLLOUT)){
                                if (unlikely(kq_ev_change(h, fd, EVFILT_WRITE, EV_ADD, e)==-1))
@@ -480,8 +509,8 @@ again2:
                                        if (likely(events & POLLIN)){
                                                kq_ev_change(h, fd, EVFILT_READ, EV_DELETE, 0);
                                        }
+                                       goto error;
                                }
-                               goto error;
                        }
                        break;
 #endif
@@ -516,7 +545,7 @@ again_devpoll:
                pf.events=events;
 check_io_again:
                n=0;
-               while(e->type && ((n=poll(&pf, 1, 0))>0) && 
+               while(e->type && ((n=poll(&pf, 1, 0))>0) &&
                                (handle_io(e, pf.revents, idx)>0) &&
                                (pf.revents & (e->events|POLLERR|POLLHUP)));
                if (unlikely(e->type && (n==-1))){
@@ -531,20 +560,20 @@ error:
        if (e) unhash_fd_map(e);
        return -1;
 #undef fd_array_setup
-#undef set_fd_flags 
+#undef set_fd_flags
 }
 
 
 
 #define IO_FD_CLOSING 16
-/* parameters:    h - handler 
+/* parameters:    h - handler
  *               fd - file descriptor
  *            index - index in the fd_array if known, -1 if not
  *                    (if index==-1 fd_array will be searched for the
- *                     corresponding fd* entry -- slower but unavoidable in 
+ *                     corresponding fd* entry -- slower but unavoidable in
  *                     some cases). index is not used (no fd_array) for epoll,
  *                     /dev/poll and kqueue
- *            flags - optimization flags, e.g. IO_FD_CLOSING, the fd was 
+ *            flags - optimization flags, e.g. IO_FD_CLOSING, the fd was
  *                    or will shortly be closed, in some cases we can avoid
  *                    extra remove operations (e.g.: epoll, kqueue, sigio)
  * returns 0 if ok, -1 on error */
@@ -600,7 +629,6 @@ inline static int io_watch_del(io_wait_h* h, int fd, int idx, int flags)
                goto error;
        }
        events=e->events;
-       unhash_fd_map(e);
        
        switch(h->poll_method){
                case POLL_POLL:
@@ -614,13 +642,12 @@ inline static int io_watch_del(io_wait_h* h, int fd, int idx, int flags)
                                FD_CLR(fd, &h->master_wset);
                        if (unlikely(h->max_fd_select && (h->max_fd_select==fd)))
                                /* we don't know the prev. max, so we just decrement it */
-                               h->max_fd_select--; 
+                               h->max_fd_select--;
                        fix_fd_array;
                        break;
 #endif
 #ifdef HAVE_SIGIO_RT
                case POLL_SIGIO_RT:
-                       fix_fd_array;
                        /* the O_ASYNC flag must be reset all the time, the fd
                         *  can be changed only if  O_ASYNC is reset (if not and
                         *  the fd is a duplicate, you will get signals from the dup. fd
@@ -629,17 +656,18 @@ inline static int io_watch_del(io_wait_h* h, int fd, int idx, int flags)
                         */
                        /*if (!(flags & IO_FD_CLOSING)){*/
                                /* reset ASYNC */
-                               fd_flags=fcntl(fd, F_GETFL); 
-                               if (unlikely(fd_flags==-1)){ 
-                                       LOG(L_ERR, "ERROR: io_watch_del: fnctl: GETFL failed:" 
-                                                       " %s [%d]\n", strerror(errno), errno); 
-                                       goto error; 
-                               } 
-                               if (unlikely(fcntl(fd, F_SETFL, fd_flags&(~O_ASYNC))==-1)){ 
-                                       LOG(L_ERR, "ERROR: io_watch_del: fnctl: SETFL" 
-                                                               " failed: %s [%d]\n", strerror(errno), errno); 
-                                       goto error; 
-                               } 
+                               fd_flags=fcntl(fd, F_GETFL);
+                               if (unlikely(fd_flags==-1)){
+                                       LOG(L_ERR, "ERROR: io_watch_del: fnctl: GETFL failed:"
+                                                       " %s [%d]\n", strerror(errno), errno);
+                                       goto error;
+                               }
+                               if (unlikely(fcntl(fd, F_SETFL, fd_flags&(~O_ASYNC))==-1)){
+                                       LOG(L_ERR, "ERROR: io_watch_del: fnctl: SETFL"
+                                                               " failed: %s [%d]\n", strerror(errno), errno);
+                                       goto error;
+                               }
+                       fix_fd_array; /* only on success */
                        break;
 #endif
 #ifdef HAVE_EPOLL
@@ -648,7 +676,7 @@ inline static int io_watch_del(io_wait_h* h, int fd, int idx, int flags)
                        /* epoll doesn't seem to automatically remove sockets,
                         * if the socket is a duplicate/moved and the original
                         * is still open. The fd is removed from the epoll set
-                        * only when the original (and all the  copies?) is/are 
+                        * only when the original (and all the  copies?) is/are
                         * closed. This is probably a bug in epoll. --andrei */
 #ifdef EPOLL_NO_CLOSE_BUG
                        if (!(flags & IO_FD_CLOSING)){
@@ -698,7 +726,7 @@ again_devpoll:
                                if (write(h->dpoll_fd, &pfd, sizeof(pfd))==-1){
                                        if (errno==EINTR) goto again_devpoll;
                                        LOG(L_ERR, "ERROR: io_watch_del: removing fd from "
-                                                               "/dev/poll failed: %s [%d]\n", 
+                                                               "/dev/poll failed: %s [%d]\n",
                                                                strerror(errno), errno);
                                        goto error;
                                }
@@ -706,10 +734,11 @@ again_devpoll:
 #endif
                default:
                        LOG(L_CRIT, "BUG: io_watch_del: no support for poll method "
-                                       " %s (%d)\n", poll_method_str[h->poll_method], 
+                                       " %s (%d)\n", poll_method_str[h->poll_method],
                                        h->poll_method);
                        goto error;
        }
+       unhash_fd_map(e); /* only on success */
        h->fd_no--;
        return 0;
 error:
@@ -719,12 +748,12 @@ error:
 
 
 
-/* parameters:    h - handler 
+/* parameters:    h - handler
  *               fd - file descriptor
  *           events - new events to watch for
  *              idx - index in the fd_array if known, -1 if not
  *                    (if index==-1 fd_array will be searched for the
- *                     corresponding fd* entry -- slower but unavoidable in 
+ *                     corresponding fd* entry -- slower but unavoidable in
  *                     some cases). index is not used (no fd_array) for epoll,
  *                     /dev/poll and kqueue
  * returns 0 if ok, -1 on error */
@@ -781,14 +810,16 @@ inline static int io_watch_chg(io_wait_h* h, int fd, short events, int idx )
        
        add_events=events & ~e->events;
        del_events=e->events & ~events;
-       e->events=events;
        switch(h->poll_method){
                case POLL_POLL:
 #ifdef POLLRDHUP
-                       /* listen to POLLRDHUP by default (if POLLIN) */
-                       events|=((int)!(events & POLLIN) - 1) & POLLRDHUP;
-#endif /* POLLRDHUP */
+                       fd_array_chg(events |
+                                                       /* listen to POLLRDHUP by default (if POLLIN) */
+                                                       (((int)!(events & POLLIN) - 1) & POLLRDHUP)
+                                               );
+#else /* POLLRDHUP */
                        fd_array_chg(events);
+#endif /* POLLRDHUP */
                        break;
 #ifdef HAVE_SELECT
                case POLL_SELECT:
@@ -882,7 +913,7 @@ again_devpoll1:
                                if (unlikely(write(h->dpoll_fd, &pfd, sizeof(pfd))==-1)){
                                        if (errno==EINTR) goto again_devpoll1;
                                        LOG(L_ERR, "ERROR: io_watch_chg: removing fd from "
-                                                               "/dev/poll failed: %s [%d]\n", 
+                                                               "/dev/poll failed: %s [%d]\n",
                                                                strerror(errno), errno);
                                        goto error;
                                }
@@ -892,18 +923,21 @@ again_devpoll2:
                                if (unlikely(write(h->dpoll_fd, &pfd, sizeof(pfd))==-1)){
                                        if (errno==EINTR) goto again_devpoll2;
                                        LOG(L_ERR, "ERROR: io_watch_chg: re-adding fd to "
-                                                               "/dev/poll failed: %s [%d]\n", 
+                                                               "/dev/poll failed: %s [%d]\n",
                                                                strerror(errno), errno);
+                                       /* error re-adding the fd => mark it as removed/unhash */
+                                       unhash_fd_map(e);
                                        goto error;
                                }
                                break;
 #endif
                default:
                        LOG(L_CRIT, "BUG: io_watch_chg: no support for poll method "
-                                       " %s (%d)\n", poll_method_str[h->poll_method], 
+                                       " %s (%d)\n", poll_method_str[h->poll_method],
                                        h->poll_method);
                        goto error;
        }
+       e->events=events; /* only on success */
        return 0;
 error:
        return -1;
@@ -912,7 +946,7 @@ error:
 
 
 
-/* io_wait_loop_x style function 
+/* io_wait_loop_x style function.
  * wait for io using poll()
  * params: h      - io_wait handle
  *         t      - timeout in s
@@ -953,11 +987,11 @@ again:
                                /* repeat handle_io if repeat, fd still watched (not deleted
                                 *  inside handle_io), handle_io returns that there's still
                                 *  IO and the fd is still watched for the triggering event */
-                               while(fm->type && 
+                               while(fm->type &&
                                                (handle_io(fm, h->fd_array[r].revents, r) > 0) &&
                                                repeat && ((fm->events|POLLERR|POLLHUP) &
                                                                                                        h->fd_array[r].revents));
-                               r=h->crt_fd_array_idx; /* can change due to io_watch_del(fd) 
+                               r=h->crt_fd_array_idx; /* can change due to io_watch_del(fd)
                                                                                  array shifting */
                        }
                }
@@ -1002,9 +1036,9 @@ again:
                        if (unlikely(revents)){
                                h->crt_fd_array_idx=r;
                                fm=get_fd_map(h, h->fd_array[r].fd);
-                               while(fm->type && (fm->events & revents) && 
+                               while(fm->type && (fm->events & revents) &&
                                                (handle_io(fm, revents, r)>0) && repeat);
-                               r=h->crt_fd_array_idx; /* can change due to io_watch_del(fd) 
+                               r=h->crt_fd_array_idx; /* can change due to io_watch_del(fd)
                                                                                  array shifting */
                                n--;
                        }
@@ -1028,7 +1062,7 @@ again:
                        if (errno==EINTR) goto again; /* signal, ignore it */
                        else{
                                LOG(L_ERR, "ERROR:io_wait_loop_epoll: "
-                                               "epoll_wait(%d, %p, %d, %d): %s [%d]\n", 
+                                               "epoll_wait(%d, %p, %d, %d): %s [%d]\n",
                                                h->epfd, h->ep_array, h->fd_no, t*1000,
                                                strerror(errno), errno);
                                goto error;
@@ -1054,7 +1088,7 @@ again:
                                        ;
                        if (likely(revents)){
                                fm=(struct fd_map*)h->ep_array[r].data.ptr;
-                               while(fm->type && ((fm->events|POLLERR|POLLHUP) & revents) && 
+                               while(fm->type && ((fm->events|POLLERR|POLLHUP) & revents) &&
                                                (handle_io(fm, revents, -1)>0) && repeat);
                        }else{
                                LOG(L_ERR, "ERROR:io_wait_loop_epoll: unexpected event %x"
@@ -1075,55 +1109,124 @@ inline static int io_wait_loop_kqueue(io_wait_h* h, int t, int repeat)
        int n, r;
        struct timespec tspec;
        struct fd_map* fm;
+       int orig_changes;
+       int apply_changes;
        int revents;
        
        tspec.tv_sec=t;
        tspec.tv_nsec=0;
+       orig_changes=h->kq_nchanges;
+       apply_changes=orig_changes;
+       do {
 again:
-               n=kevent(h->kq_fd, h->kq_changes, h->kq_nchanges,  h->kq_array,
-                                       h->fd_no, &tspec);
+               n=kevent(h->kq_fd, h->kq_changes, apply_changes,  h->kq_array,
+                                       h->kq_array_size, &tspec);
                if (unlikely(n==-1)){
-                       if (errno==EINTR) goto again; /* signal, ignore it */
-                       else{
-                               LOG(L_ERR, "ERROR: io_wait_loop_kqueue: kevent:"
+                       if (unlikely(errno==EINTR)) goto again; /* signal, ignore it */
+                       else {
+                               /* for a detailed explanation of what follows see below
+                                  the EV_ERROR case */
+                               if (unlikely(!(errno==EBADF || errno==ENOENT)))
+                                       BUG("io_wait_loop_kqueue: kevent: unexpected error"
                                                " %s [%d]\n", strerror(errno), errno);
-                               goto error;
+                               /* some of the FDs in kq_changes are bad (already closed)
+                                  and there is not enough space in kq_array to return all
+                                  of them back */
+                               apply_changes = h->kq_array_size;
+                               goto again;
                        }
                }
-               h->kq_nchanges=0; /* reset changes array */
+               /* remove applied changes */
+               h->kq_nchanges -= apply_changes;
+               if (unlikely(apply_changes < orig_changes)) {
+                       orig_changes -= apply_changes;
+                       memmove(&h->kq_changes[0], &h->kq_changes[apply_changes],
+                                                                       sizeof(h->kq_changes[0])*h->kq_nchanges);
+                       apply_changes = (orig_changes < h->kq_array_size) ? orig_changes :
+                                                               h->kq_array_size;
+               } else {
+                       orig_changes = 0;
+                       apply_changes = 0;
+               }
                for (r=0; r<n; r++){
 #ifdef EXTRA_DEBUG
                        DBG("DBG: kqueue: event %d/%d: fd=%d, udata=%lx, flags=0x%x\n",
                                        r, n, h->kq_array[r].ident, (long)h->kq_array[r].udata,
                                        h->kq_array[r].flags);
 #endif
-#if 0
-                       if (unlikely(h->kq_array[r].flags & EV_ERROR)){
-                               /* error in changes: we ignore it, it can be caused by
-                                  trying to remove an already closed fd: race between
-                                  adding something to the changes array, close() and
-                                  applying the changes */
-                               LOG(L_INFO, "INFO: io_wait_loop_kqueue: kevent error on "
-                                                       "fd %ld: %s [%ld]\n", h->kq_array[r].ident,
+                       if (unlikely((h->kq_array[r].flags & EV_ERROR) ||
+                                                        h->kq_array[r].udata == 0)){
+                               /* error in changes: we ignore it if it has to do with a
+                                  bad fd or update==0. It can be caused by trying to remove an
+                                  already closed fd: race between adding something to the
+                                  changes array, close() and applying the changes (EBADF).
+                                  E.g. for ser tcp: tcp_main sends a fd to child for reading
+                                   => deletes it from the watched fds => the changes array
+                                       will contain an EV_DELETE for it. Before the changes
+                                       are applied (they are at the end of the main io_wait loop,
+                                       after all the fd events were processed), a CON_ERR sent
+                                       to tcp_main by a sender (send fail) is processed and causes
+                                       the fd to be closed. When the changes are applied =>
+                                       error for the EV_DELETE attempt of a closed fd.
+                                       Something similar can happen when a fd is scheduled
+                                       for removal, is close()'ed before being removed and
+                                       re-opened(a new sock. get the same fd). When the
+                                       watched fd changes will be applied the fd will be valid
+                                       (so no EBADF), but it's not already watch => ENOENT.
+                                       We report a BUG for the other errors (there's nothing
+                                       constructive we can do if we get an error we don't know
+                                       how to handle), but apart from that we ignore it in the
+                                       idea that it is better apply the rest of the changes,
+                                       rather then dropping all of them.
+                               */
+                               /*
+                                       example EV_ERROR for trying to delete a read watched fd,
+                                       that was already closed:
+                                       {
+                                               ident = 63,  [fd]
+                                               filter = -1, [EVFILT_READ]
+                                               flags = 16384, [EV_ERROR]
+                                               fflags = 0,
+                                               data = 9, [errno = EBADF]
+                                               udata = 0x0
+                                       }
+                               */
+                               if (h->kq_array[r].data != EBADF &&
+                                               h->kq_array[r].data != ENOENT)
+                                       BUG("io_wait_loop_kqueue: kevent unexpected error on "
+                                                       "fd %ld udata %lx: %s [%ld]\n",
+                                                       (long)h->kq_array[r].ident,
+                                                       (long)h->kq_array[r].udata,
                                                        strerror(h->kq_array[r].data),
                                                        (long)h->kq_array[r].data);
-                       }else{ 
-#endif
+                       }else{
                                fm=(struct fd_map*)h->kq_array[r].udata;
                                if (likely(h->kq_array[r].filter==EVFILT_READ)){
-                                       revents=POLLIN | 
-                                               (((int)!(h->kq_array[r].flags & EV_EOF)-1)&POLLHUP);
-                                       while(fm->type && (fm->events & revents) && 
+                                       revents=POLLIN |
+                                               (((int)!(h->kq_array[r].flags & EV_EOF)-1)&POLLHUP) |
+                                               (((int)!((h->kq_array[r].flags & EV_EOF) &&
+                                                                       h->kq_array[r].fflags != 0) - 1)&POLLERR);
+                                       while(fm->type && (fm->events & revents) &&
                                                        (handle_io(fm, revents, -1)>0) && repeat);
                                }else if (h->kq_array[r].filter==EVFILT_WRITE){
-                                       revents=POLLOUT | 
-                                               (((int)!(h->kq_array[r].flags & EV_EOF)-1)&POLLHUP);
-                                       while(fm->type && (fm->events & revents) && 
+                                       revents=POLLOUT |
+                                               (((int)!(h->kq_array[r].flags & EV_EOF)-1)&POLLHUP) |
+                                               (((int)!((h->kq_array[r].flags & EV_EOF) &&
+                                                                       h->kq_array[r].fflags != 0) - 1)&POLLERR);
+                                       while(fm->type && (fm->events & revents) &&
                                                        (handle_io(fm, revents, -1)>0) && repeat);
+                               }else{
+                                       BUG("io_wait_loop_kqueue: unknown filter: kqueue: event "
+                                                       "%d/%d: fd=%d, filter=%d, flags=0x%x, fflags=0x%x,"
+                                                       " data=%lx, udata=%lx\n",
+                                       r, n, (int)h->kq_array[r].ident, (int)h->kq_array[r].filter,
+                                       h->kq_array[r].flags, h->kq_array[r].fflags,
+                                       (unsigned long)h->kq_array[r].data,
+                                       (unsigned long)h->kq_array[r].udata);
                                }
-                       /*} */
+                       }
                }
-error:
+       } while(unlikely(orig_changes));
        return n;
 }
 #endif
@@ -1207,14 +1310,14 @@ again:
                         *  POLLIN|POLLRDNORM|POLLMSG (=POLL_MSG),
                         *  POLLERR (=POLL_ERR),
                         *  POLLPRI|POLLRDBAND (=POLL_PRI),
-                        *  POLLHUP|POLLERR (=POLL_HUP) 
+                        *  POLLHUP|POLLERR (=POLL_HUP)
                         *  [linux 2.6.22 fs/fcntl.c:447]
                         */
 #ifdef EXTRA_DEBUG
                        DBG("io_wait_loop_sigio_rt: siginfo: signal=%d (%d),"
                                        " si_code=%d, si_band=0x%x,"
                                        " si_fd=%d\n",
-                                       siginfo.si_signo, n, siginfo.si_code, 
+                                       siginfo.si_signo, n, siginfo.si_code,
                                        (unsigned)sigio_band,
                                        sigio_fd);
 #endif
@@ -1227,7 +1330,7 @@ again:
                                /* fix revents==POLLPRI case */
                                revents |= (!(revents & POLLPRI)-1) & POLLIN;
                                /* we can have queued signals generated by fds not watched
-                                * any more, or by fds in transition, to a child 
+                                * any more, or by fds in transition, to a child
                                 * => ignore them */
                                if (fm->type && ((fm->events|POLLERR|POLLHUP) & revents))
                                        handle_io(fm, revents, -1);
@@ -1243,7 +1346,7 @@ again:
                        }
                }
        }else{
-               /* signal queue overflow 
+               /* signal queue overflow
                 * TODO: increase signal queue size: 2.4x /proc/.., 2.6x -rlimits */
                LOG(L_WARN, "WARNING: io_wait_loop_sigio_rt: signal queue overflowed"
                                        "- falling back to poll\n");