1 /* ioset.h - srvx event loop
2 * Copyright 2002-2004, 2006 srvx Development Team
4 * This file is part of srvx.
6 * srvx is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 #include "ioset-impl.h"
30 #ifdef HAVE_SYS_SOCKET_H
31 #include <sys/socket.h>
34 #define IS_EOL(CH) ((CH) == '\n')
36 extern int uplink_connect(void);
40 static struct io_engine *engine;
41 static struct io_fd *active_fd;
44 ioq_init(struct ioq *ioq, int size) {
45 ioq->buf = malloc(size);
46 ioq->get = ioq->put = 0;
51 ioq_put_avail(const struct ioq *ioq) {
52 /* Subtract 1 from ioq->get to be sure we don't fill the buffer
53 * and make it look empty even when there's data in it. */
54 if (ioq->put < ioq->get)
55 return ioq->get - ioq->put - 1;
56 else if (ioq->get == 0)
57 return ioq->size - ioq->put - 1;
59 return ioq->size - ioq->put;
63 ioq_get_avail(const struct ioq *ioq) {
64 return ((ioq->put < ioq->get) ? ioq->size : ioq->put) - ioq->get;
68 ioq_used(const struct ioq *ioq) {
69 return ((ioq->put < ioq->get) ? ioq->size : 0) + ioq->put - ioq->get;
73 ioq_grow(struct ioq *ioq) {
74 int new_size = ioq->size << 1;
75 char *new_buf = malloc(new_size);
76 int get_avail = ioq_get_avail(ioq);
77 memcpy(new_buf, ioq->buf + ioq->get, get_avail);
78 if (ioq->put < ioq->get)
79 memcpy(new_buf + get_avail, ioq->buf, ioq->put);
81 ioq->put = ioq_used(ioq);
85 return new_size - ioq->put;
88 extern struct io_engine io_engine_kqueue;
89 extern struct io_engine io_engine_epoll;
90 extern struct io_engine io_engine_win32;
91 extern struct io_engine io_engine_select;
96 if (engine) /* someone beat us to it */
100 if (!engine && io_engine_kqueue.init())
101 engine = &io_engine_kqueue;
105 if (!engine && io_engine_epoll.init())
106 engine = &io_engine_epoll;
110 if (!engine && io_engine_win32.init())
111 engine = &io_engine_win32;
115 /* we found one that works */
116 } else if (io_engine_select.init())
117 engine = &io_engine_select;
119 log_module(MAIN_LOG, LOG_FATAL, "No usable I/O engine found.");
120 log_module(MAIN_LOG, LOG_DEBUG, "Using %s I/O engine.", engine->name);
124 ioset_cleanup(void) {
134 log_module(MAIN_LOG, LOG_ERROR, "Somebody called ioset_add(%d) on a negative fd!", fd);
139 res = calloc(1, sizeof(*res));
143 ioq_init(&res->send, 1024);
144 ioq_init(&res->recv, 1024);
146 flags = fcntl(fd, F_GETFL);
147 fcntl(fd, F_SETFL, flags|O_NONBLOCK);
148 flags = fcntl(fd, F_GETFD);
149 fcntl(fd, F_SETFD, flags|FD_CLOEXEC);
151 /* I hope you're using the Win32 backend or something else that
152 * automatically marks the file descriptor non-blocking...
160 struct io_fd *ioset_listen(struct sockaddr *local, unsigned int sa_size, void *data, void (*accept_cb)(struct io_fd *listener, struct io_fd *new_connect))
167 fd = socket(local ? local->sa_family : PF_INET, SOCK_STREAM, 0);
169 log_module(MAIN_LOG, LOG_ERROR, "Unable to create listening socket: %s", strerror(errno));
173 if (local && sa_size) {
174 res = bind(fd, local, sa_size);
176 log_module(MAIN_LOG, LOG_ERROR, "Unable to bind listening socket %d: %s", fd, strerror(errno));
182 res = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
184 log_module(MAIN_LOG, LOG_WARNING, "Unable to mark listener address as re-usable: %s", strerror(errno));
190 log_module(MAIN_LOG, LOG_ERROR, "Unable to listen on socket %d: %s", fd, strerror(errno));
195 io_fd = ioset_add(fd);
200 io_fd->state = IO_LISTENING;
202 io_fd->accept_cb = accept_cb;
203 engine->update(io_fd);
208 ioset_connect(struct sockaddr *local, unsigned int sa_size, const char *peer, unsigned int port, int blocking, void *data, void (*connect_cb)(struct io_fd *fd, int error)) {
209 struct addrinfo hints;
212 struct io_fd *old_active;
217 memset(&hints, 0, sizeof(hints));
218 hints.ai_family = local ? local->sa_family : 0;
219 hints.ai_socktype = SOCK_STREAM;
220 snprintf(portnum, sizeof(portnum), "%u", port);
221 if (getaddrinfo(peer, portnum, &hints, &ai)) {
222 log_module(MAIN_LOG, LOG_ERROR, "getaddrinfo(%s, %s) failed.", peer, portnum);
227 if ((fd = socket(local->sa_family, SOCK_STREAM, 0)) < 0) {
228 log_module(MAIN_LOG, LOG_ERROR, "socket() for %s returned errno %d (%s)", peer, errno, strerror(errno));
232 if (bind(fd, local, sa_size) < 0) {
233 log_module(MAIN_LOG, LOG_ERROR, "bind() of socket for %s (fd %d) returned errno %d (%s). Will let operating system choose.", peer, fd, errno, strerror(errno));
236 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
237 log_module(MAIN_LOG, LOG_ERROR, "socket() for %s returned errno %d (%s).", peer, errno, strerror(errno));
244 res = connect(fd, ai->ai_addr, ai->ai_addrlen);
245 io_fd = ioset_add(fd);
247 io_fd = ioset_add(fd);
248 res = connect(fd, ai->ai_addr, ai->ai_addrlen);
255 io_fd->state = IO_CONNECTING;
257 io_fd->connect_cb = connect_cb;
260 case EINPROGRESS: /* only if !blocking */
261 engine->update(io_fd);
264 log_module(MAIN_LOG, LOG_ERROR, "connect(%s:%d) (fd %d) returned errno %d (%s).", peer, port, io_fd->fd, errno, strerror(errno));
265 /* then fall through */
268 ioset_close(io_fd, 1);
272 io_fd->state = IO_CONNECTED;
273 old_active = active_fd;
275 connect_cb(io_fd, ((res < 0) ? errno : 0));
277 engine->update(io_fd);
278 if (old_active != io_fd)
279 active_fd = old_active;
283 void ioset_update(struct io_fd *fd) {
288 ioset_try_write(struct io_fd *fd) {
292 req = ioq_get_avail(&fd->send);
293 res = write(fd->fd, fd->send.buf+fd->send.get, req);
299 log_module(MAIN_LOG, LOG_ERROR, "write() on fd %d error %d: %s", fd->fd, errno, strerror(errno));
303 if (fd->send.get == fd->send.size)
310 ioset_close(struct io_fd *fdp, int os_close) {
313 if (active_fd == fdp)
316 fdp->destroy_cb(fdp);
317 #if defined(HAVE_WSAEVENTSELECT)
318 /* This is one huge kludge. Sorry! */
319 if (fdp->send.get != fdp->send.put && (os_close & 2)) {
320 engine->remove(fdp, 0);
321 ioset_try_write(fdp);
322 /* it may need to send the beginning of the buffer now.. */
323 if (fdp->send.get != fdp->send.put)
324 ioset_try_write(fdp);
329 closesocket(fdp->fd);
331 if (fdp->send.get != fdp->send.put && (os_close & 2)) {
334 flags = fcntl(fdp->fd, F_GETFL);
335 fcntl(fdp->fd, F_SETFL, flags&~O_NONBLOCK);
336 ioset_try_write(fdp);
337 /* it may need to send the beginning of the buffer now.. */
338 if (fdp->send.get != fdp->send.put)
339 ioset_try_write(fdp);
345 engine->remove(fdp, os_close & 1);
351 ioset_accept(struct io_fd *listener)
353 struct io_fd *old_active;
354 struct io_fd *new_fd;
357 fd = accept(listener->fd, NULL, 0);
359 log_module(MAIN_LOG, LOG_ERROR, "Unable to accept new connection on listener %d: %s", listener->fd, strerror(errno));
363 new_fd = ioset_add(fd);
364 new_fd->state = IO_CONNECTED;
365 old_active = active_fd;
367 listener->accept_cb(listener, new_fd);
368 assert(active_fd == NULL || active_fd == new_fd);
369 if (active_fd == new_fd) {
370 if (new_fd->send.get != new_fd->send.put)
371 ioset_try_write(new_fd);
373 engine->update(new_fd);
375 active_fd = old_active;
379 ioset_find_line_length(struct io_fd *fd) {
380 unsigned int pos, max, len;
382 max = (fd->recv.put < fd->recv.get) ? fd->recv.size : fd->recv.put;
383 for (pos = fd->recv.get; pos < max; ++pos, ++len)
384 if (IS_EOL(fd->recv.buf[pos]))
385 return fd->line_len = len + 1;
386 if (fd->recv.put < fd->recv.get)
387 for (pos = 0; pos < fd->recv.put; ++pos, ++len)
388 if (IS_EOL(fd->recv.buf[pos]))
389 return fd->line_len = len + 1;
390 return fd->line_len = 0;
394 ioset_buffered_read(struct io_fd *fd) {
395 int put_avail, nbr, fdnum;
397 if (!(put_avail = ioq_put_avail(&fd->recv)))
398 put_avail = ioq_grow(&fd->recv);
399 nbr = read(fd->fd, fd->recv.buf + fd->recv.put, put_avail);
405 log_module(MAIN_LOG, LOG_ERROR, "Unexpected read() error %d on fd %d: %s", errno, fd->fd, strerror(errno));
406 /* Just flag it as EOF and call readable_cb() to notify the fd's owner. */
407 fd->state = IO_CLOSED;
412 } else if (nbr == 0) {
413 fd->state = IO_CLOSED;
418 if (fd->line_len == 0) {
420 for (pos = fd->recv.put; pos < fd->recv.put + nbr; ++pos) {
421 if (IS_EOL(fd->recv.buf[pos])) {
422 if (fd->recv.put < fd->recv.get)
423 fd->line_len = fd->recv.size + pos + 1 - fd->recv.get;
425 fd->line_len = pos + 1 - fd->recv.get;
431 if (fd->recv.put == fd->recv.size)
434 while (fd->line_len > 0) {
435 struct io_fd *old_active;
438 old_active = active_fd;
442 ioset_find_line_length(fd);
445 if (old_active != fd)
446 active_fd = old_active;
454 ioset_line_read(struct io_fd *fd, char *dest, int max) {
459 line_len = fd->line_len;
460 if ((fd->state == IO_CLOSED) && (!ioq_get_avail(&fd->recv) || (line_len < 0)))
466 avail = ioq_get_avail(&fd->recv);
468 memcpy(dest, fd->recv.buf + fd->recv.get, avail);
469 assert(fd->recv.get + avail == fd->recv.size);
475 memcpy(dest + done, fd->recv.buf + fd->recv.get, max - done);
476 fd->recv.get += max - done;
477 if (fd->recv.get == fd->recv.size)
480 ioset_find_line_length(fd);
485 ioset_events(struct io_fd *fd, int readable, int writable)
487 if (!fd || (!readable && !writable))
494 if (active_fd && readable)
498 assert(active_fd == NULL || active_fd == fd);
499 if (active_fd && readable) {
503 if (getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &rc, &arglen) < 0)
505 fd->state = IO_CLOSED;
507 fd->connect_cb(fd, rc);
508 } else if (active_fd && writable) {
509 fd->state = IO_CONNECTED;
511 fd->connect_cb(fd, 0);
516 /* and fall through */
518 assert(active_fd == NULL || active_fd == fd);
519 if (active_fd && readable) {
521 ioset_buffered_read(fd);
526 assert(active_fd == NULL || active_fd == fd);
527 if (active_fd && writable)
535 extern struct io_fd *socket_io_fd;
536 struct timeval timeout;
539 while (!quit_services) {
540 while (!socket_io_fd)
543 /* How long to sleep? (fill in select_timeout) */
544 wakey = timeq_next();
545 if ((wakey - now) < 0)
548 timeout.tv_sec = wakey - now;
551 if (engine->loop(&timeout))
554 /* Call any timeq events we need to call. */
561 extern char *services_config;
562 conf_read(services_config);
569 ioset_write(struct io_fd *fd, const char *buf, unsigned int nbw) {
571 while (ioq_used(&fd->send) + nbw >= fd->send.size)
573 avail = ioq_put_avail(&fd->send);
575 memcpy(fd->send.buf + fd->send.put, buf, avail);
580 memcpy(fd->send.buf + fd->send.put, buf, nbw);
582 if (fd->send.put == fd->send.size)
588 ioset_printf(struct io_fd *fd, const char *fmt, ...) {
594 res = vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
596 if (res > 0 && (size_t)res <= sizeof(tmpbuf))
597 ioset_write(fd, tmpbuf, res);
602 ioset_set_time(unsigned long new_now) {
603 clock_skew = new_now - time(NULL);