1 /******************************************************************************/
2 /* pfixtools: a collection of postfix related tools */
4 /* ________________________________________________________________________ */
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
10 /* 1. Redistributions of source code must retain the above copyright */
11 /* notice, this list of conditions and the following disclaimer. */
12 /* 2. Redistributions in binary form must reproduce the above copyright */
13 /* notice, this list of conditions and the following disclaimer in the */
14 /* documentation and/or other materials provided with the distribution. */
15 /* 3. The names of its contributors may not be used to endorse or promote */
16 /* products derived from this software without specific prior written */
19 /* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE */
21 /* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
22 /* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS */
23 /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR */
24 /* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF */
25 /* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS */
26 /* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN */
27 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) */
28 /* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF */
29 /* THE POSSIBILITY OF SUCH DAMAGE. */
30 /******************************************************************************/
33 * Copyright © 2008 Florent Bruneau
40 static PA(server_t) listeners = ARRAY_INIT;
41 static PA(server_t) server_pool = ARRAY_INIT;
43 static server_t* server_new(void)
45 server_t* server = p_new(server_t, 1);
51 static void server_wipe(server_t *server)
53 server->listener = server->event = false;
55 epoll_modify(server->fd, 0, NULL);
59 if (server->fd2 > 0) {
63 if (server->data && server->clear_data) {
64 server->clear_data(&server->data);
68 static void server_delete(server_t **server)
71 buffer_wipe(&(*server)->ibuf);
72 buffer_wipe(&(*server)->obuf);
78 static server_t* server_acquire(void)
80 if (server_pool.len != 0) {
81 return array_elt(server_pool, --server_pool.len);
87 static void server_release(server_t *server)
90 array_add(server_pool, server);
93 static void server_shutdown(void)
95 array_deep_wipe(listeners, server_delete);
96 array_deep_wipe(server_pool, server_delete);
99 module_exit(server_shutdown);
101 int start_server(int port, start_listener_t starter, delete_client_t deleter)
103 struct sockaddr_in addr = {
104 .sin_family = AF_INET,
105 .sin_addr = { htonl(INADDR_LOOPBACK) },
111 addr.sin_port = htons(port);
112 sock = tcp_listen_nonblock((const struct sockaddr *)&addr, sizeof(addr));
125 tmp = server_acquire();
127 tmp->listener = true;
129 tmp->clear_data = deleter;
130 epoll_register(sock, EPOLLIN, tmp);
131 array_add(listeners, tmp);
135 static int start_client(server_t *server, start_client_t starter,
136 delete_client_t deleter)
142 sock = accept_nonblock(server->fd);
149 data = starter(server);
156 tmp = server_acquire();
159 tmp->clear_data = deleter;
160 epoll_register(sock, EPOLLIN, tmp);
164 event_t event_register(int fd, void *data)
168 if (pipe(fds) != 0) {
172 if (setnonblock(fds[0]) != 0) {
179 server_t *tmp = server_acquire();
181 tmp->fd = fd == -1 ? fds[0] : fd;
182 tmp->fd2 = fd == -1 ? fds[1] : -1;
184 epoll_register(fds[0], EPOLLIN, tmp);
188 bool event_fire(event_t event)
190 static const char *data = "";
191 if (event->fd2 == -1) {
194 return write(event->fd2, data, 1) == 0;
197 static bool event_cancel(event_t event)
201 ssize_t res = read(event->fd, buff, 32);
202 if (res == -1 && errno != EAGAIN && errno != EINTR) {
205 } else if (res == -1 && errno == EINTR) {
207 } else if (res != 32) {
213 int server_loop(start_client_t starter, delete_client_t deleter,
214 run_client_t runner, event_handler_t handler,
215 refresh_t refresh, void* config)
217 info("entering processing loop");
219 struct epoll_event evts[1024];
222 if (sighup && refresh) {
224 info("refreshing...");
225 if (!refresh(config)) {
226 crit("error while refreshing configuration");
229 info("refresh done, processing loop restarts");
232 n = epoll_select(evts, countof(evts), -1);
234 if (errno != EAGAIN && errno != EINTR) {
235 UNIXERR("epoll_wait");
242 server_t *d = evts[n].data.ptr;
245 (void)start_client(d, starter, deleter);
247 } else if (d->event) {
248 if (!event_cancel(d)) {
253 if (!handler(d, config)) {
260 if (evts[n].events & EPOLLIN) {
261 if (runner(d, config) < 0) {
267 if ((evts[n].events & EPOLLOUT) && d->obuf.len) {
268 if (buffer_write(&d->obuf, d->fd) < 0) {
273 epoll_modify(d->fd, EPOLLIN, d);
278 info("exit requested");