diff --git a/freebsd/netinet/libalias/alias_sctp.c b/freebsd/netinet/libalias/alias_sctp.c index 67e47540..08d8b4d7 100644 --- a/freebsd/netinet/libalias/alias_sctp.c +++ b/freebsd/netinet/libalias/alias_sctp.c @@ -185,7 +185,7 @@ static MALLOC_DEFINE(M_SCTPNAT, "sctpnat", "sctp nat dbs"); /* Use kernel allocator. */ #ifdef _SYS_MALLOC_H_ #define sn_malloc(x) malloc(x, M_SCTPNAT, M_NOWAIT|M_ZERO) -#define sn_calloc(n,x) sn_malloc(x * n) +#define sn_calloc(n,x) sn_malloc((x) * (n)) #define sn_free(x) free(x, M_SCTPNAT) #endif// #ifdef _SYS_MALLOC_H_ diff --git a/lib/ff_epoll.c b/lib/ff_epoll.c index 3e236b8b..96e7a9e7 100644 --- a/lib/ff_epoll.c +++ b/lib/ff_epoll.c @@ -27,40 +27,79 @@ ff_epoll_create(int size __attribute__((__unused__))) return ff_kqueue(); } -int +int ff_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { - struct kevent kev[3]; + /* + * Since kqueue uses EVFILT_READ and EVFILT_WRITE filters to + * handle read/write events, so we need two kevents. + */ + const int changes = 2; + struct kevent kev[changes]; + int flags = 0; + int read_flags, write_flags; - if (!event && op != EPOLL_CTL_DEL) { + if ((!event && op != EPOLL_CTL_DEL) || + (op != EPOLL_CTL_ADD && + op != EPOLL_CTL_MOD && + op != EPOLL_CTL_DEL)) { errno = EINVAL; return -1; } - if (op == EPOLL_CTL_ADD){ - EV_SET(&kev[0], fd, EVFILT_READ, - EV_ADD | (event->events & EPOLLIN ? 0 : EV_DISABLE), 0, 0, NULL); - EV_SET(&kev[1], fd, EVFILT_WRITE, - EV_ADD | (event->events & EPOLLOUT ? 0 : EV_DISABLE), 0, 0, NULL); - EV_SET(&kev[2], fd, EVFILT_USER, EV_ADD, - event->events & EPOLLRDHUP ? 1 : 0, 0, NULL); - } else if (op == EPOLL_CTL_DEL) { + /* + * EPOLL_CTL_DEL doesn't need to care for event->events. + */ + if (op == EPOLL_CTL_DEL) { EV_SET(&kev[0], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); EV_SET(&kev[1], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); - EV_SET(&kev[2], fd, EVFILT_USER, EV_DELETE, 0, 0, NULL); - } else if (op == EPOLL_CTL_MOD) { - EV_SET(&kev[0], fd, EVFILT_READ, - event->events & EPOLLIN ? EV_ENABLE : EV_DISABLE, 0, 0, NULL); - EV_SET(&kev[1], fd, EVFILT_WRITE, - event->events & EPOLLOUT ? EV_ENABLE : EV_DISABLE, 0, 0, NULL); - EV_SET(&kev[2], fd, EVFILT_USER, 0, - NOTE_FFCOPY | (event->events & EPOLLRDHUP ? 1 : 0), 0, NULL); - } else { - errno = EINVAL; - return -1; + + return ff_kevent(epfd, kev, changes, NULL, 0, NULL); } - return ff_kevent(epfd, kev, 3, NULL, 0, NULL); + /* + * FIXME: + * + * Kqueue doesn't have edge-triggered mode that exactly + * same with epoll, the most similar way is setting EV_CLEAR + * or EV_DISPATCH flag, but there are still some differences. + * + * EV_CLEAR:after the event is retrieved by the user, + * its state is reset. + * EV_DISPATCH: disable the event source immediately + * after delivery of an event. + * + * Here we use EV_CLEAR temporarily. + * + */ + if (event->events & EPOLLET) { + flags |= EV_CLEAR; + } + + if (event->events & EPOLLONESHOT) { + flags |= EV_ONESHOT; + } + + if (op == EPOLL_CTL_ADD) { + flags |= EV_ADD; + } + + read_flags = write_flags = flags | EV_DISABLE; + + if (event->events & EPOLLIN) { + read_flags &= ~EV_DISABLE; + read_flags |= EV_ENABLE; + } + + if (event->events & EPOLLOUT) { + write_flags &= ~EV_DISABLE; + write_flags |= EV_ENABLE; + } + + EV_SET(&kev[0], fd, EVFILT_READ, read_flags, 0, 0, NULL); + EV_SET(&kev[1], fd, EVFILT_WRITE, write_flags, 0, 0, NULL); + + return ff_kevent(epfd, kev, changes, NULL, 0, NULL); } static void