/* * Copyright (C) 2010 John Crispin * Copyright (C) 2010 Steven Barth * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "uloop.h" #include "ulog.h" struct uloop_sock { int sock; void *priv; _uloop_sock_handler cb; }; static int epoll_fd = 0; static int uloop_loop = 1; static struct ubus_ctx *uloop_ubus = 0; void uloop_init(void) { epoll_fd = epoll_create(32); fcntl(epoll_fd, F_SETFD, fcntl(epoll_fd, F_GETFD) | FD_CLOEXEC); } static void uloop_ubus_cb(struct uloop_sock *u) { ubus_recv(uloop_ubus); } struct uloop_sock* uloop_sock_add(int sock, void *priv, _uloop_sock_handler cb) { struct epoll_event ev; struct uloop_sock *u = malloc(sizeof(struct uloop_sock)); u->sock = sock; u->priv = priv; u->cb = cb; memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP; ev.data.fd = sock; ev.data.ptr = u; epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev); return u; } void uloop_ubus_add(struct ubus_ctx *ctx) { if(!uloop_ubus) uloop_ubus = ctx; } void uloop_sock_delete(struct uloop_sock *u) { if(u->priv != uloop_ubus) free(u->priv); free(u); } static void uloop_handler_INT(int signo) { LOG("away we go\n"); uloop_loop = 0; } static void uloop_setup_signals(void) { struct sigaction s1, s2; memset(&s1, 0, sizeof(struct sigaction)); memset(&s2, 0, sizeof(struct sigaction)); s1.sa_handler = uloop_handler_INT; s1.sa_flags = 0; sigaction(SIGINT, &s1, NULL); s2.sa_handler = uloop_handler_INT; s2.sa_flags = 0; sigaction(SIGTERM, &s2, NULL); } int uloop(void (*cb)(void), int timeout) { #define MAX_EVENTS 10 struct uloop_sock *ubus_sock = 0; struct epoll_event events[MAX_EVENTS]; int nfds, n; time_t t = time(0); uloop_loop = 1; uloop_setup_signals(); while(uloop_loop) { if(uloop_ubus) if(ubus_connect(uloop_ubus) > 0) { int ubus_fd = ubus_get_sock(uloop_ubus); fcntl(ubus_fd, F_SETFD, fcntl(ubus_fd, F_GETFD) | FD_CLOEXEC); ubus_sock = uloop_sock_add(ubus_fd, uloop_ubus, uloop_ubus_cb); } nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, 1000); for(n = 0; n < nfds; ++n) { struct uloop_sock *u = (struct uloop_sock*)events[n].data.ptr; if(events[n].events & EPOLLRDHUP) { epoll_ctl(epoll_fd, EPOLL_CTL_DEL, u->sock, 0); uloop_sock_delete(u); } else if(events[n].events & EPOLLIN) { if(u->cb) u->cb(u); } } if(cb) { if(difftime(time(0), t) < timeout) continue; t = time(0); cb(); } } if(ubus_sock) uloop_sock_delete(ubus_sock); return 0; }