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