2
* lxc: linux Container library
4
* (C) Copyright IBM Corp. 2007, 2008
7
* Daniel Lezcano <dlezcano at fr.ibm.com>
9
* This library is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU Lesser General Public
11
* License as published by the Free Software Foundation; either
12
* version 2.1 of the License, or (at your option) any later version.
14
* This library is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
* Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this library; if not, write to the Free Software
21
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
#include <sys/epoll.h>
33
lxc_mainloop_callback_t callback;
38
int lxc_mainloop(struct lxc_epoll_descr *descr)
40
int i, nfds, triggered;
41
struct lxc_handler *handler;
47
nfds = epoll_wait(descr->epfd, descr->ev, descr->nfds, -1);
54
for (i = 0; i < descr->nfds; i++) {
56
if (!(descr->ev[i].events & EPOLLIN) &&
57
!(descr->ev[i].events & EPOLLHUP))
61
handler = (struct lxc_handler *)descr->ev[i].data.ptr;
63
/* If the handler returns a positive value, exit
65
if (handler->callback(handler->fd, handler->data,
69
if (triggered == nfds)
78
int lxc_mainloop_add_handler(struct lxc_epoll_descr *descr, int fd,
79
lxc_mainloop_callback_t callback, void *data)
81
struct epoll_event *ev;
82
struct lxc_handler *handler;
85
handler = malloc(sizeof(*handler));
89
handler->callback = callback;
93
ev = malloc(sizeof(*descr->ev) * (descr->nfds + 1));
98
memcpy(ev, descr->ev, sizeof(*descr->ev) * (descr->nfds));
103
descr->ev[descr->nfds].events = EPOLLIN;
104
descr->ev[descr->nfds].data.ptr = handler;
106
ret = epoll_ctl(descr->epfd, EPOLL_CTL_ADD, fd,
107
&descr->ev[descr->nfds]);
118
int lxc_mainloop_del_handler(struct lxc_epoll_descr *descr, int fd)
120
struct epoll_event *ev;
121
struct lxc_handler *handler;
124
for (i = 0; i < descr->nfds; i++) {
126
handler = descr->ev[i].data.ptr;
128
if (handler->fd != fd)
131
if (epoll_ctl(descr->epfd, EPOLL_CTL_DEL, fd, NULL))
134
ev = malloc(sizeof(*ev) * (descr->nfds - 1));
138
for (j = 0; j < descr->nfds; j++) {
141
ev[idx] = descr->ev[idx];
145
free(descr->ev[i].data.ptr);
156
int lxc_mainloop_open(int size, struct lxc_epoll_descr *descr)
161
descr->epfd = epoll_create(size);
168
int lxc_mainloop_close(struct lxc_epoll_descr *descr)
172
for (i = 0; i < descr->nfds; i++)
173
free(descr->ev[i].data.ptr);
176
return close(descr->epfd);