static void eventer_ports_impl_trigger(eventer_t e, int mask) { ev_lock_state_t lockstate; const char *cbname; struct timeval __now; int fd, oldmask, newmask; fd = e->fd; if(e != master_fds[fd].e) return; lockstate = acquire_master_fd(fd); if(lockstate == EV_ALREADY_OWNED) return; assert(lockstate == EV_OWNED); gettimeofday(&__now, NULL); oldmask = e->mask; cbname = eventer_name_for_callback(e->callback); noitLT(eventer_deb, &__now, "ports: fire on %d/%x to %s(%p)\n", fd, mask, cbname?cbname:"???", e->callback); EVENTER_CALLBACK_ENTRY((void *)e->callback, (char *)cbname, fd, e->mask, mask); newmask = e->callback(e, mask, e->closure, &__now); EVENTER_CALLBACK_RETURN((void *)e->callback, (char *)cbname, newmask); if(newmask) { alter_fd(e, newmask); /* Set our mask */ e->mask = newmask; noitLT(eventer_deb, &__now, "ports: complete on %d/(%x->%x) to %s(%p)\n", fd, mask, newmask, cbname?cbname:"???", e->callback); } else { noitLT(eventer_deb, &__now, "ports: complete on %d/none to %s(%p)\n", fd, cbname?cbname:"???", e->callback); /* * Long story long: * When integrating with a few external event systems, we find * it difficult to make their use of remove+add as an update * as it can be recurrent in a single handler call and you cannot * remove completely from the event system if you are going to * just update (otherwise the eventer_t in your call stack could * be stale). What we do is perform a superficial remove, marking * the mask as 0, but not eventer_remove_fd. Then on an add, if * we already have an event, we just update the mask (as we * have not yet returned to the eventer's loop. * This leaves us in a tricky situation when a remove is called * and the add doesn't roll in, we return 0 (mask == 0) and hit * this spot. We have intended to remove the event, but it still * resides at master_fds[fd].e -- even after we free it. * So, in the evnet that we return 0 and the event that * master_fds[fd].e == the event we're about to free... we NULL * it out. */ if(master_fds[fd].e == e) master_fds[fd].e = NULL; eventer_free(e); } release_master_fd(fd, lockstate); }
static int eventer_epoll_impl_loop() { struct epoll_event *epev; epev = malloc(sizeof(*epev) * maxfds); while(1) { struct timeval __now, __sleeptime; int fd_cnt = 0; __sleeptime = eventer_max_sleeptime; eventer_dispatch_timed(&__now, &__sleeptime); /* Handle recurrent events */ eventer_dispatch_recurrent(&__now); /* Now we move on to our fd-based events */ fd_cnt = epoll_wait(epoll_fd, epev, maxfds, __sleeptime.tv_sec * 1000 + __sleeptime.tv_usec / 1000); noitLT(eventer_deb, &__now, "debug: epoll_wait(%d, [], %d) => %d\n", epoll_fd, maxfds, fd_cnt); if(fd_cnt < 0) { noitLT(eventer_err, &__now, "epoll_wait: %s\n", strerror(errno)); } else { int idx; /* loop once to clear */ for(idx = 0; idx < fd_cnt; idx++) { struct epoll_event *ev; eventer_t e; int fd, mask = 0; ev = &epev[idx]; if(ev->events & (EPOLLIN | EPOLLPRI)) mask |= EVENTER_READ; if(ev->events & (EPOLLOUT)) mask |= EVENTER_WRITE; if(ev->events & (EPOLLERR|EPOLLHUP)) mask |= EVENTER_EXCEPTION; fd = ev->data.fd; e = master_fds[fd].e; /* It's possible that someone removed the event and freed it * before we got here. */ if(!e) continue; eventer_epoll_impl_trigger(e, mask); } } } /* NOTREACHED */ return 0; }
static void eventer_epoll_impl_trigger(eventer_t e, int mask) { struct timeval __now; int fd, newmask; const char *cbname; ev_lock_state_t lockstate; fd = e->fd; if(e != master_fds[fd].e) return; lockstate = acquire_master_fd(fd); if(lockstate == EV_ALREADY_OWNED) return; assert(lockstate == EV_OWNED); gettimeofday(&__now, NULL); cbname = eventer_name_for_callback_e(e->callback, e); noitLT(eventer_deb, &__now, "epoll: fire on %d/%x to %s(%p)\n", fd, mask, cbname?cbname:"???", e->callback); EVENTER_CALLBACK_ENTRY((void *)e->callback, (char *)cbname, fd, e->mask, mask); newmask = e->callback(e, mask, e->closure, &__now); EVENTER_CALLBACK_RETURN((void *)e->callback, (char *)cbname, newmask); if(newmask) { struct epoll_event _ev; memset(&_ev, 0, sizeof(_ev)); _ev.data.fd = fd; if(newmask & EVENTER_READ) _ev.events |= (EPOLLIN|EPOLLPRI); if(newmask & EVENTER_WRITE) _ev.events |= (EPOLLOUT); if(newmask & EVENTER_EXCEPTION) _ev.events |= (EPOLLERR|EPOLLHUP); if(master_fds[fd].e == NULL) { noitL(noit_debug, "eventer %s(%p) epoll asked to modify descheduled fd: %d\n", cbname?cbname:"???", e->callback, fd); } else { assert(epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &_ev) == 0); } /* Set our mask */ e->mask = newmask; } else { /* see kqueue implementation for details on the next line */ if(master_fds[fd].e == e) master_fds[fd].e = NULL; eventer_free(e); } release_master_fd(fd, lockstate); }
static int eventer_ports_impl_loop() { struct timeval __dyna_sleep = { 0, 0 }; while(1) { const char *cbname; struct timeval __now, __sleeptime; struct timespec __ports_sleeptime; unsigned int fd_cnt = 0; int ret; port_event_t pevents[MAX_PORT_EVENTS]; int max_timed_events_to_process; int newmask; if(compare_timeval(eventer_max_sleeptime, __dyna_sleep) < 0) __dyna_sleep = eventer_max_sleeptime; __sleeptime = __dyna_sleep; eventer_dispatch_timed(&__now, &__sleeptime); if(compare_timeval(__sleeptime, __dyna_sleep) > 0) __sleeptime = __dyna_sleep; /* Handle recurrent events */ eventer_dispatch_recurrent(&__now); /* Now we move on to our fd-based events */ __ports_sleeptime.tv_sec = __sleeptime.tv_sec; __ports_sleeptime.tv_nsec = __sleeptime.tv_usec * 1000; fd_cnt = 1; pevents[0].portev_source = 65535; /* This is impossible */ ret = port_getn(port_fd, pevents, MAX_PORT_EVENTS, &fd_cnt, &__ports_sleeptime); /* The timeout case is a tad complex with ports. -1/ETIME is clearly * a timeout. However, it i spossible that we got that and fd_cnt isn't * 0, which means we both timed out and got events... WTF? */ if(fd_cnt == 0 || (ret == -1 && errno == ETIME && pevents[0].portev_source == 65535)) add_timeval(__dyna_sleep, __dyna_increment, &__dyna_sleep); if(ret == -1 && (errno != ETIME && errno != EINTR)) noitLT(eventer_err, &__now, "port_getn: %s\n", strerror(errno)); if(ret < 0) noitLT(eventer_deb, &__now, "port_getn: %s\n", strerror(errno)); noitLT(eventer_deb, &__now, "debug: port_getn(%d, [], %d) => %d\n", port_fd, fd_cnt, ret); if(pevents[0].portev_source == 65535) { /* the impossible still remains, which means our fd_cnt _must_ be 0 */ fd_cnt = 0; } if(fd_cnt > 0) { int idx; /* Loop a last time to process */ __dyna_sleep.tv_sec = __dyna_sleep.tv_usec = 0; /* reset */ for(idx = 0; idx < fd_cnt; idx++) { port_event_t *pe; eventer_t e; int fd, oldmask, mask; pe = &pevents[idx]; if(pe->portev_source != PORT_SOURCE_FD) continue; fd = (int)pe->portev_object; assert((int)pe->portev_user == fd); e = master_fds[fd].e; mask = 0; if(pe->portev_events & (POLLIN | POLLHUP)) mask |= EVENTER_READ; if(pe->portev_events & (POLLOUT)) mask |= EVENTER_WRITE; if(pe->portev_events & (POLLERR | POLLHUP | POLLNVAL)) mask |= EVENTER_EXCEPTION; /* It's possible that someone removed the event and freed it * before we got here. */ eventer_ports_impl_trigger(e, mask); } } } /* NOTREACHED */ }
static int eventer_kqueue_impl_loop() { struct timeval __dyna_sleep = { 0, 0 }; KQUEUE_DECL; KQUEUE_SETUP; if(!kqs) { kqs = calloc(1, sizeof(*kqs)); kqs_init(kqs); } pthread_setspecific(kqueue_setup_key, kqs); while(1) { struct timeval __now, __sleeptime; struct timespec __kqueue_sleeptime; int fd_cnt = 0; if(compare_timeval(eventer_max_sleeptime, __dyna_sleep) < 0) __dyna_sleep = eventer_max_sleeptime; __sleeptime = __dyna_sleep; eventer_dispatch_timed(&__now, &__sleeptime); if(compare_timeval(__sleeptime, __dyna_sleep) > 0) __sleeptime = __dyna_sleep; /* Handle recurrent events */ eventer_dispatch_recurrent(&__now); /* If we're the master, we need to lock the master_kqs and make mods */ if(master_kqs->__ke_vec_used) { struct timespec __zerotime = { 0, 0 }; pthread_mutex_lock(&kqs_lock); fd_cnt = kevent(kqueue_fd, master_kqs->__ke_vec, master_kqs->__ke_vec_used, NULL, 0, &__zerotime); noitLT(eventer_deb, &__now, "debug: kevent(%d, [], %d) => %d\n", kqueue_fd, master_kqs->__ke_vec_used, fd_cnt); if(fd_cnt < 0) { noitLT(eventer_err, &__now, "kevent: %s\n", strerror(errno)); } master_kqs->__ke_vec_used = 0; pthread_mutex_unlock(&kqs_lock); } /* Now we move on to our fd-based events */ __kqueue_sleeptime.tv_sec = __sleeptime.tv_sec; __kqueue_sleeptime.tv_nsec = __sleeptime.tv_usec * 1000; fd_cnt = kevent(kqueue_fd, ke_vec, ke_vec_used, ke_vec, ke_vec_a, &__kqueue_sleeptime); noitLT(eventer_deb, &__now, "debug: kevent(%d, [], %d) => %d\n", kqueue_fd, ke_vec_used, fd_cnt); ke_vec_used = 0; if(fd_cnt < 0) { noitLT(eventer_err, &__now, "kevent: %s\n", strerror(errno)); } else if(fd_cnt == 0) { /* timeout */ add_timeval(__dyna_sleep, __dyna_increment, &__dyna_sleep); } else { int idx; __dyna_sleep.tv_sec = __dyna_sleep.tv_usec = 0; /* reset */ /* loop once to clear */ for(idx = 0; idx < fd_cnt; idx++) { struct kevent *ke; ke = &ke_vec[idx]; if(ke->flags & EV_ERROR) continue; masks[ke->ident] = 0; } /* Loop again to aggregate */ for(idx = 0; idx < fd_cnt; idx++) { struct kevent *ke; ke = &ke_vec[idx]; if(ke->flags & EV_ERROR) continue; if(ke->filter == EVFILT_READ) masks[ke->ident] |= EVENTER_READ; if(ke->filter == EVFILT_WRITE) masks[ke->ident] |= EVENTER_WRITE; } /* Loop a last time to process */ for(idx = 0; idx < fd_cnt; idx++) { struct kevent *ke; eventer_t e; int fd; ke = &ke_vec[idx]; if(ke->flags & EV_ERROR) { if(ke->data != EBADF && ke->data != ENOENT) noitLT(eventer_err, &__now, "error [%d]: %s\n", (int)ke->ident, strerror(ke->data)); continue; } assert((vpsized_int)ke->udata == (vpsized_int)ke->ident); fd = ke->ident; e = master_fds[fd].e; /* If we've seen this fd, don't callback twice */ if(!masks[fd]) continue; /* It's possible that someone removed the event and freed it * before we got here. */ if(e) eventer_kqueue_impl_trigger(e, masks[fd]); masks[fd] = 0; /* indicates we've processed this fd */ } } } /* NOTREACHED */ return 0; }
static void eventer_kqueue_impl_trigger(eventer_t e, int mask) { ev_lock_state_t lockstate; struct timeval __now; int oldmask, newmask; const char *cbname; int fd; fd = e->fd; if(e != master_fds[fd].e) return; lockstate = acquire_master_fd(fd); if(lockstate == EV_ALREADY_OWNED) return; assert(lockstate == EV_OWNED); gettimeofday(&__now, NULL); /* We're going to lie to ourselves. You'd think this should be: * oldmask = e->mask; However, we just fired with masks[fd], so * kqueue is clearly looking for all of the events in masks[fd]. * So, we combine them "just to be safe." */ oldmask = e->mask | masks[fd]; cbname = eventer_name_for_callback(e->callback); noitLT(eventer_deb, &__now, "kqueue: fire on %d/%x to %s(%p)\n", fd, masks[fd], cbname?cbname:"???", e->callback); newmask = e->callback(e, mask, e->closure, &__now); if(newmask) { /* toggle the read bits if needed */ if(newmask & (EVENTER_READ | EVENTER_EXCEPTION)) { if(!(oldmask & (EVENTER_READ | EVENTER_EXCEPTION))) ke_change(fd, EVFILT_READ, EV_ADD | EV_ENABLE, e); } else if(oldmask & (EVENTER_READ | EVENTER_EXCEPTION)) ke_change(fd, EVFILT_READ, EV_DELETE | EV_DISABLE, e); /* toggle the write bits if needed */ if(newmask & EVENTER_WRITE) { if(!(oldmask & EVENTER_WRITE)) ke_change(fd, EVFILT_WRITE, EV_ADD | EV_ENABLE, e); } else if(oldmask & EVENTER_WRITE) ke_change(fd, EVFILT_WRITE, EV_DELETE | EV_DISABLE, e); /* Set our mask */ e->mask = newmask; } else { /* * Long story long: * When integrating with a few external event systems, we find * it difficult to make their use of remove+add as an update * as it can be recurrent in a single handler call and you cannot * remove completely from the event system if you are going to * just update (otherwise the eventer_t in your call stack could * be stale). What we do is perform a superficial remove, marking * the mask as 0, but not eventer_remove_fd. Then on an add, if * we already have an event, we just update the mask (as we * have not yet returned to the eventer's loop. * This leaves us in a tricky situation when a remove is called * and the add doesn't roll in, we return 0 (mask == 0) and hit * this spot. We have intended to remove the event, but it still * resides at master_fds[fd].e -- even after we free it. * So, in the evnet that we return 0 and the event that * master_fds[fd].e == the event we're about to free... we NULL * it out. */ if(master_fds[fd].e == e) master_fds[fd].e = NULL; eventer_free(e); } release_master_fd(fd, lockstate); }
static int ping_icmp_handler(eventer_t e, int mask, void *closure, struct timeval *now) { noit_module_t *self = (noit_module_t *)closure; ping_icmp_data_t *ping_data; struct check_info *data; char packet[1500]; int packet_len = sizeof(packet); union { struct sockaddr_in in4; struct sockaddr_in6 in6; } from; unsigned int from_len; struct ip *ip = (struct ip *)packet; struct icmp *icp; struct ping_payload *payload; ping_data = noit_module_get_userdata(self); while(1) { struct ping_session_key k; int inlen, iphlen; void *vcheck; noit_check_t *check; struct timeval tt; from_len = sizeof(from); inlen = recvfrom(e->fd, packet, packet_len, 0, (struct sockaddr *)&from, &from_len); gettimeofday(now, NULL); /* set it, as we care about accuracy */ if(inlen < 0) { if(errno == EAGAIN || errno == EINTR) break; noitLT(nlerr, now, "ping_icmp recvfrom: %s\n", strerror(errno)); break; } iphlen = ip->ip_hl << 2; if((inlen-iphlen) != (sizeof(struct icmp)+sizeof(struct ping_payload))) { noitLT(nldeb, now, "ping_icmp bad size: %d+%d\n", iphlen, inlen-iphlen); continue; } icp = (struct icmp *)(packet + iphlen); payload = (struct ping_payload *)(icp + 1); if(icp->icmp_type != ICMP_ECHOREPLY) { continue; } if(icp->icmp_id != (((vpsized_uint)self) & 0xffff)) { noitLT(nlerr, now, "ping_icmp not sent from this instance (%d:%d) vs. %lu\n", icp->icmp_id, ntohs(icp->icmp_seq), (unsigned long)(((vpsized_uint)self) & 0xffff)); continue; } check = NULL; k.addr_of_check = payload->addr_of_check; uuid_copy(k.checkid, payload->checkid); if(noit_hash_retrieve(ping_data->in_flight, (const char *)&k, sizeof(k), &vcheck)) check = vcheck; /* make sure this check is from this generation! */ if(!check) { char uuid_str[37]; uuid_unparse_lower(payload->checkid, uuid_str); noitLT(nldeb, now, "ping_icmp response for unknown check '%s'\n", uuid_str); continue; } if(check->generation != payload->generation) { noitLT(nldeb, now, "ping_icmp response in generation gap\n"); continue; } data = (struct check_info *)check->closure; /* If there is no timeout_event, the check must have completed. * We have nothing to do. */ if(!data->timeout_event) continue; /* Sanity check the payload */ if(payload->check_no != data->check_no) continue; if(payload->check_pack_cnt != data->expected_count) continue; if(payload->check_pack_no < 0 || payload->check_pack_no >= data->expected_count) continue; sub_timeval(*now, payload->whence, &tt); data->turnaround[payload->check_pack_no] = (float)tt.tv_sec + (float)tt.tv_usec / 1000000.0; if(ping_icmp_is_complete(self, check)) { ping_icmp_log_results(self, check); eventer_remove(data->timeout_event); free(data->timeout_event->closure); eventer_free(data->timeout_event); data->timeout_event = NULL; check->flags &= ~NP_RUNNING; k.addr_of_check = check; uuid_copy(k.checkid, check->checkid); noit_hash_delete(ping_data->in_flight, (const char *)&k, sizeof(k), free, NULL); } } return EVENTER_READ; }