]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/mainloop.c
remove unused variable
[mirror_lxc.git] / src / lxc / mainloop.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
8 *
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.
13 *
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.
18 *
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
22 */
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <sys/epoll.h>
29
30 #include "mainloop.h"
31
32 struct mainloop_handler {
33 lxc_mainloop_callback_t callback;
34 int fd;
35 void *data;
36 };
37
38 #define MAX_EVENTS 10
39
40 int lxc_mainloop(struct lxc_epoll_descr *descr)
41 {
42 int i, nfds;
43 struct mainloop_handler *handler;
44 struct epoll_event events[MAX_EVENTS];
45
46 for (;;) {
47
48 nfds = epoll_wait(descr->epfd, events, MAX_EVENTS, -1);
49 if (nfds < 0) {
50 if (errno == EINTR)
51 continue;
52 return -1;
53 }
54
55 for (i = 0; i < nfds; i++) {
56 handler =
57 (struct mainloop_handler *) events[i].data.ptr;
58
59 /* If the handler returns a positive value, exit
60 the mainloop */
61 if (handler->callback(handler->fd, handler->data,
62 descr) > 0)
63 return 0;
64 }
65
66 if (lxc_list_empty(&descr->handlers))
67 return 0;
68 }
69 }
70
71 int lxc_mainloop_add_handler(struct lxc_epoll_descr *descr, int fd,
72 lxc_mainloop_callback_t callback, void *data)
73 {
74 struct epoll_event ev;
75 struct mainloop_handler *handler;
76 struct lxc_list *item;
77
78 handler = malloc(sizeof(*handler));
79 if (!handler)
80 return -1;
81
82 handler->callback = callback;
83 handler->fd = fd;
84 handler->data = data;
85
86 ev.events = EPOLLIN;
87 ev.data.ptr = handler;
88
89 if (epoll_ctl(descr->epfd, EPOLL_CTL_ADD, fd, &ev) < 0)
90 goto out_free_handler;
91
92 item = malloc(sizeof(*item));
93 if (!item)
94 goto out_free_handler;
95
96 item->elem = handler;
97 lxc_list_add(&descr->handlers, item);
98 return 0;
99
100 out_free_handler:
101 free(handler);
102 return -1;
103 }
104
105 int lxc_mainloop_del_handler(struct lxc_epoll_descr *descr, int fd)
106 {
107 struct mainloop_handler *handler;
108 struct lxc_list *iterator;
109
110 lxc_list_for_each(iterator, &descr->handlers) {
111 handler = iterator->elem;
112
113 if (handler->fd == fd) {
114 /* found */
115 if (epoll_ctl(descr->epfd, EPOLL_CTL_DEL, fd, NULL))
116 return -1;
117
118 lxc_list_del(iterator);
119 free(iterator->elem);
120 free(iterator);
121 return 0;
122 }
123 }
124
125 return -1;
126 }
127
128 int lxc_mainloop_open(struct lxc_epoll_descr *descr)
129 {
130 /* hint value passed to epoll create */
131 descr->epfd = epoll_create(2);
132 if (descr->epfd < 0)
133 return -1;
134
135 lxc_list_init(&descr->handlers);
136 return 0;
137 }
138
139 int lxc_mainloop_close(struct lxc_epoll_descr *descr)
140 {
141 struct lxc_list *iterator, *next;
142
143 iterator = descr->handlers.next;
144 while (iterator != &descr->handlers) {
145 next = iterator->next;
146
147 lxc_list_del(iterator);
148 free(iterator->elem);
149 free(iterator);
150 iterator = next;
151 }
152
153 return close(descr->epfd);
154 }
155