]> jfr.im git - irc/evilnet/x3.git/blame - src/ioset-epoll.c
Couple of srvx updates.
[irc/evilnet/x3.git] / src / ioset-epoll.c
CommitLineData
1136f709 1/* ioset epoll_*() backend for srvx
2 * Copyright 2006 srvx Development Team
3 *
4 * This file is part of srvx.
5 *
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
be2c97a5 8 * the Free Software Foundation; either version 3 of the License, or
1136f709 9 * (at your option) any later version.
10 *
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.
15 *
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.
19 */
20
21#include "ioset-impl.h"
22#include "common.h"
23#include "log.h"
24
25#ifdef HAVE_SYS_EPOLL_H
26# include <sys/epoll.h>
27#endif
28
29#ifdef HAVE_SYS_SOCKET_H
30# include <sys/socket.h>
31#endif
32
33extern int clock_skew;
34static int epoll_fd;
35
36static int
37ioset_epoll_init(void)
38{
39 epoll_fd = epoll_create(1024);
40 if (epoll_fd < 0)
41 return 0;
42 return 1;
43}
44
45static int
46ioset_epoll_events(struct io_fd *fd)
47{
48 return EPOLLHUP
49 | EPOLLIN
50 | (fd_wants_writes(fd) ? EPOLLOUT : 0)
51 ;
52}
53
54static void
55ioset_epoll_add(struct io_fd *fd)
56{
57 struct epoll_event evt;
58 int res;
59
60 evt.events = ioset_epoll_events(fd);
61 evt.data.ptr = fd;
62 res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd->fd, &evt);
63 if (res < 0)
64 log_module(MAIN_LOG, LOG_ERROR, "Unable to add fd %d to epoll: %s", fd->fd, strerror(errno));
65}
66
67static void
68ioset_epoll_remove(struct io_fd *fd, int closed)
69{
70 static struct epoll_event evt;
71 if (!closed)
72 (void)epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd->fd, &evt);
73}
74
75static void
76ioset_epoll_update(struct io_fd *fd)
77{
78 struct epoll_event evt;
79 int res;
80
81 evt.events = ioset_epoll_events(fd);
82 evt.data.ptr = fd;
83 res = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd->fd, &evt);
84 if (res < 0)
85 log_module(MAIN_LOG, LOG_ERROR, "Unable to modify fd %d for epoll: %s", fd->fd, strerror(errno));
86}
87
88static void
89ioset_epoll_cleanup(void)
90{
91 close(epoll_fd);
92}
93
94static int
95ioset_epoll_loop(struct timeval *timeout)
96{
97 struct epoll_event evts[32];
98 int events;
99 int msec;
100 int res;
101 int ii;
102
103 msec = timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : -1;
104
105 res = epoll_wait(epoll_fd, evts, ArrayLength(evts), msec);
106 now = time(NULL) + clock_skew;
107 if (res < 0) {
108 if (errno != EINTR) {
109 log_module(MAIN_LOG, LOG_ERROR, "epoll_wait() error %d: %s", errno, strerror(errno));
110 close_socket();
111 }
112 return 1;
113 }
114
115 for (ii = 0; ii < res; ++ii) {
116 events = evts[ii].events;
117 ioset_events(evts[ii].data.ptr, (events & (EPOLLIN | EPOLLHUP)), (events & EPOLLOUT));
118 }
119
120 return 0;
121}
122
123struct io_engine io_engine_epoll = {
124 .name = "epoll",
125 .init = ioset_epoll_init,
126 .add = ioset_epoll_add,
127 .remove = ioset_epoll_remove,
128 .update = ioset_epoll_update,
129 .loop = ioset_epoll_loop,
130 .cleanup = ioset_epoll_cleanup,
131};