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