root/luci2/libubox/uloop.c @ 5845

Revision 5845, 3.3 KB (checked in by blogic, 3 years ago)

leak inside uloop

Line 
1/*
2 *   Copyright (C) 2010 John Crispin <blogic@openwrt.org>
3 *   Copyright (C) 2010 Steven Barth <steven@midlink.org>
4 *
5 *   This program is free software; you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation; either version 2 of the License, or
8 *   (at your option) any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program; if not, write to the Free Software
17 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18 *
19 */
20
21#include <unistd.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <errno.h>
25#include <poll.h>
26#include <string.h>
27#include <time.h>
28#include <fcntl.h>
29#include <signal.h>
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <sys/epoll.h>
33
34#include <libubus.h>
35#include "uloop.h"
36#include "ulog.h"
37
38struct uloop_sock
39{
40    int sock;
41    void *priv;
42    _uloop_sock_handler cb;
43};
44
45static int epoll_fd = 0;
46static int uloop_loop = 1;
47static struct ubus_ctx *uloop_ubus = 0;
48
49void uloop_init(void)
50{
51    epoll_fd = epoll_create(32);
52}
53
54static void uloop_ubus_cb(struct uloop_sock *u)
55{
56    ubus_recv(uloop_ubus);
57}
58
59struct uloop_sock* uloop_sock_add(int sock, void *priv, _uloop_sock_handler cb)
60{
61    struct epoll_event ev;
62    struct uloop_sock *u = malloc(sizeof(struct uloop_sock));
63    u->sock = sock;
64    u->priv = priv;
65    u->cb = cb;
66    memset(&ev, 0, sizeof(struct epoll_event));
67    ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
68    ev.data.fd = sock;
69    ev.data.ptr = u;
70    epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev);
71    return u;
72}
73
74void uloop_ubus_add(struct ubus_ctx *ctx)
75{
76    if(!uloop_ubus)
77        uloop_ubus = ctx;
78}
79
80void uloop_sock_delete(struct uloop_sock *u)
81{
82    if(u->priv != uloop_ubus)
83        free(u->priv);
84    free(u);
85}
86
87static void uloop_handler_INT(int signo)
88{
89    LOG("away we go\n");
90    uloop_loop = 0;
91}
92
93static void uloop_setup_signals(void)
94{
95    struct sigaction s1, s2;
96    memset(&s1, 0, sizeof(struct sigaction));
97    memset(&s2, 0, sizeof(struct sigaction));
98    s1.sa_handler = uloop_handler_INT;
99    s1.sa_flags = 0;
100    sigaction(SIGINT, &s1, NULL);
101    s2.sa_handler = uloop_handler_INT;
102    s2.sa_flags = 0;
103    sigaction(SIGTERM, &s2, NULL);
104}
105
106int uloop(void (*cb)(void), int timeout)
107{
108    #define MAX_EVENTS  10
109    struct uloop_sock *ubus_sock = 0;
110    struct epoll_event events[MAX_EVENTS];
111    int nfds, n;
112    time_t t = time(0);
113    uloop_loop = 1;
114    uloop_setup_signals();
115    while(uloop_loop)
116    {
117        if(uloop_ubus)
118            if(ubus_connect(uloop_ubus) > 0)
119                ubus_sock = uloop_sock_add(ubus_get_sock(uloop_ubus), uloop_ubus, uloop_ubus_cb);
120        nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, 1000);
121        for(n = 0; n < nfds; ++n)
122        {
123            struct uloop_sock *u = (struct uloop_sock*)events[n].data.ptr;
124            if(events[n].events & EPOLLRDHUP)
125            {
126                epoll_ctl(epoll_fd, EPOLL_CTL_DEL, u->sock, 0);
127                uloop_sock_delete(u);
128            } else if(events[n].events & EPOLLIN)
129            {
130                if(u->cb)
131                    u->cb(u);
132            }
133        }
134        if(cb)
135        {
136            if(difftime(time(0), t) < timeout)
137                continue;
138            t = time(0);
139            cb();
140        }
141    }
142    if(ubus_sock)
143        uloop_sock_delete(ubus_sock);
144    return 0;
145}
Note: See TracBrowser for help on using the browser.