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);
66 array_shrink(server->ibuf, 512);
67 array_shrink(server->obuf, 512);
70 static void server_delete(server_t **server)
73 buffer_wipe(&(*server)->ibuf);
74 buffer_wipe(&(*server)->obuf);
80 static server_t* server_acquire(void)
82 if (server_pool.len != 0) {
83 return array_elt(server_pool, --server_pool.len);
89 static void server_release(server_t *server)
92 array_add(server_pool, server);
95 static void server_shutdown(void)
97 array_deep_wipe(listeners, server_delete);
98 array_deep_wipe(server_pool, server_delete);
101 module_exit(server_shutdown);
103 int start_server(int port, start_listener_t starter, delete_client_t deleter)
105 struct sockaddr_in addr = {
106 .sin_family = AF_INET,
107 .sin_addr = { htonl(INADDR_LOOPBACK) },
113 addr.sin_port = htons(port);
114 sock = tcp_listen_nonblock((const struct sockaddr *)&addr, sizeof(addr));
127 tmp = server_acquire();
129 tmp->listener = true;
131 tmp->clear_data = deleter;
132 epoll_register(sock, EPOLLIN, tmp);
133 array_add(listeners, tmp);
137 static int start_client(server_t *server, start_client_t starter,
138 delete_client_t deleter)
144 sock = accept_nonblock(server->fd);
151 data = starter(server);
158 tmp = server_acquire();
161 tmp->clear_data = deleter;
162 epoll_register(sock, EPOLLIN, tmp);
166 event_t event_register(void *data)
169 if (pipe(fds) != 0) {
173 if (setnonblock(fds[0]) != 0) {
179 server_t *tmp = server_acquire();
184 epoll_register(fds[0], EPOLLIN, tmp);
188 bool event_fire(event_t event)
190 static const char *data = "";
191 return write(event->fd2, data, 1) == 0;
194 static bool event_cancel(event_t event)
198 ssize_t res = read(event->fd, buff, 32);
199 if (res == -1 && errno != EAGAIN && errno != EINTR) {
202 } else if (res == -1 && errno == EINTR) {
204 } else if (res != 32) {
210 int server_loop(start_client_t starter, delete_client_t deleter,
211 run_client_t runner, event_handler_t handler,
212 refresh_t refresh, void* config) {
213 info("entering processing loop");
215 struct epoll_event evts[1024];
218 if (sighup && refresh) {
220 info("refreshing...");
221 if (!refresh(config)) {
222 crit("error while refreshing configuration");
225 info("refresh done, processing loop restarts");
228 n = epoll_select(evts, countof(evts), -1);
230 if (errno != EAGAIN && errno != EINTR) {
231 UNIXERR("epoll_wait");
238 server_t *d = evts[n].data.ptr;
241 (void)start_client(d, starter, deleter);
243 } else if (d->event) {
244 if (!event_cancel(d)) {
249 if (!handler(d, config)) {
256 if (evts[n].events & EPOLLIN) {
257 if (runner(d, config) < 0) {
263 if ((evts[n].events & EPOLLOUT) && d->obuf.len) {
264 if (buffer_write(&d->obuf, d->fd) < 0) {
269 epoll_modify(d->fd, EPOLLIN, d);
274 info("exit requested");