Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * FD polling functions for linux epoll() |
| 3 | * |
| 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <unistd.h> |
| 14 | #include <sys/time.h> |
| 15 | #include <sys/types.h> |
| 16 | |
| 17 | #include <common/compat.h> |
| 18 | #include <common/config.h> |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 19 | #include <common/standard.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 20 | #include <common/time.h> |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 21 | #include <common/tools.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 22 | |
| 23 | #include <types/fd.h> |
| 24 | #include <types/global.h> |
| 25 | |
| 26 | #include <proto/fd.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 27 | #include <proto/task.h> |
| 28 | |
| 29 | #if defined(USE_MY_EPOLL) |
Willy Tarreau | 69801b8 | 2007-04-09 15:28:51 +0200 | [diff] [blame] | 30 | #include <common/epoll.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 31 | #include <errno.h> |
| 32 | #include <sys/syscall.h> |
Willy Tarreau | b40d420 | 2007-04-15 23:57:41 +0200 | [diff] [blame] | 33 | static _syscall1 (int, epoll_create, int, size); |
| 34 | static _syscall4 (int, epoll_ctl, int, epfd, int, op, int, fd, struct epoll_event *, event); |
| 35 | static _syscall4 (int, epoll_wait, int, epfd, struct epoll_event *, events, int, maxevents, int, timeout); |
Willy Tarreau | 69801b8 | 2007-04-09 15:28:51 +0200 | [diff] [blame] | 36 | #else |
| 37 | #include <sys/epoll.h> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 38 | #endif |
| 39 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 40 | /* This is what we store in a list. It consists in old values and fds to detect changes. */ |
| 41 | struct fd_chg { |
| 42 | unsigned int prev:2; // previous state mask. New one is in fd_evts. |
| 43 | unsigned int fd:30; // file descriptor |
| 44 | }; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 45 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 46 | static int nbchanges = 0; // number of changes pending |
| 47 | static struct fd_chg *chg_list = NULL; // list of changes |
| 48 | static struct fd_chg **chg_ptr = NULL; // per-fd changes |
| 49 | |
| 50 | /* Each 32-bit word contains 2-bit descriptors of the latest state for 16 FDs : |
| 51 | * desc = (u32 >> (2*fd)) & 3 |
| 52 | * desc = 0 : FD not set |
| 53 | * 1 : WRITE not set, READ set |
| 54 | * 2 : WRITE set, READ not set |
| 55 | * 3 : WRITE set, READ set |
| 56 | */ |
| 57 | |
| 58 | static uint32_t *fd_evts; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 59 | |
| 60 | /* private data */ |
| 61 | static struct epoll_event *epoll_events; |
| 62 | static int epoll_fd; |
| 63 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 64 | /* This structure may be used for any purpose. Warning! do not use it in |
| 65 | * recursive functions ! |
| 66 | */ |
| 67 | static struct epoll_event ev; |
| 68 | |
| 69 | /* converts a direction to a single bitmask. |
| 70 | * 0 => 1 |
| 71 | * 1 => 2 |
| 72 | */ |
| 73 | #define DIR2MSK(dir) ((dir) + 1) |
| 74 | |
| 75 | /* converts an FD to an fd_evts offset and to a bit shift */ |
| 76 | #define FD2OFS(fd) ((uint32_t)(fd) >> 4) |
| 77 | #define FD2BIT(fd) (((uint32_t)(fd) & 0xF) << 1) |
| 78 | #define FD2MSK(fd) (3 << FD2BIT(fd)) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 79 | |
| 80 | /* |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 81 | * Returns non-zero if direction <dir> is already set for <fd>. |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 82 | */ |
Willy Tarreau | 63455a9 | 2007-04-09 15:34:49 +0200 | [diff] [blame] | 83 | REGPRM2 static int __fd_is_set(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 84 | { |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 85 | return (fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(dir); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 88 | /* |
| 89 | * Adds, mods or deletes <fd> according to current status and to new desired |
| 90 | * mask <dmask> : |
| 91 | * |
| 92 | * 0 = nothing |
| 93 | * 1 = EPOLLIN |
| 94 | * 2 = EPOLLOUT |
| 95 | * 3 = EPOLLIN | EPOLLOUT |
| 96 | * |
| 97 | */ |
| 98 | static int dmsk2event[4] = { 0, EPOLLIN, EPOLLOUT, EPOLLIN | EPOLLOUT }; |
| 99 | |
| 100 | |
| 101 | REGPRM2 static void fd_flush_changes() |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 102 | { |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 103 | uint32_t ofs; |
| 104 | int opcode; |
| 105 | int prev, next; |
| 106 | int chg, fd; |
| 107 | |
| 108 | for (chg = 0; chg < nbchanges; chg++) { |
| 109 | prev = chg_list[chg].prev; |
| 110 | fd = chg_list[chg].fd; |
| 111 | chg_ptr[fd] = NULL; |
| 112 | |
| 113 | ofs = FD2OFS(fd); |
| 114 | next = (fd_evts[ofs] >> FD2BIT(fd)) & 3; |
| 115 | |
| 116 | if (prev == next) |
| 117 | /* if the value was unchanged, do nothing */ |
| 118 | continue; |
| 119 | |
| 120 | ev.events = dmsk2event[next]; |
| 121 | ev.data.fd = fd; |
| 122 | |
| 123 | if (prev) { |
| 124 | if (!next) { |
| 125 | /* we want to delete it now */ |
| 126 | opcode = EPOLL_CTL_DEL; |
| 127 | } else { |
| 128 | /* we want to switch it */ |
| 129 | opcode = EPOLL_CTL_MOD; |
| 130 | } |
| 131 | } else { |
| 132 | /* the FD did not exist, let's add it */ |
| 133 | opcode = EPOLL_CTL_ADD; |
| 134 | } |
| 135 | |
| 136 | epoll_ctl(epoll_fd, opcode, fd, &ev); |
| 137 | } |
| 138 | nbchanges = 0; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 141 | REGPRM2 static void alloc_chg_list(const int fd, int old_evt) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 142 | { |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 143 | struct fd_chg *ptr; |
| 144 | |
Willy Tarreau | 70bcfb7 | 2008-01-27 02:21:53 +0100 | [diff] [blame] | 145 | if (unlikely(chg_ptr[fd] != NULL)) |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 146 | return; |
| 147 | |
| 148 | #if LIMIT_NUMBER_OF_CHANGES |
| 149 | if (nbchanges > 2) |
| 150 | fd_flush_changes(); |
| 151 | #endif |
| 152 | |
| 153 | ptr = &chg_list[nbchanges++]; |
| 154 | chg_ptr[fd] = ptr; |
| 155 | ptr->fd = fd; |
| 156 | ptr->prev = old_evt; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 159 | REGPRM2 static int __fd_set(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 160 | { |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 161 | uint32_t ofs = FD2OFS(fd); |
| 162 | uint32_t dmsk = DIR2MSK(dir); |
| 163 | uint32_t old_evt; |
| 164 | |
| 165 | old_evt = fd_evts[ofs] >> FD2BIT(fd); |
| 166 | old_evt &= 3; |
| 167 | if (unlikely(old_evt & dmsk)) |
| 168 | return 0; |
| 169 | |
| 170 | alloc_chg_list(fd, old_evt); |
| 171 | dmsk <<= FD2BIT(fd); |
| 172 | fd_evts[ofs] |= dmsk; |
| 173 | return 1; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 176 | REGPRM2 static int __fd_clr(const int fd, int dir) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 177 | { |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 178 | uint32_t ofs = FD2OFS(fd); |
| 179 | uint32_t dmsk = DIR2MSK(dir); |
| 180 | uint32_t old_evt; |
| 181 | |
| 182 | old_evt = fd_evts[ofs] >> FD2BIT(fd); |
| 183 | old_evt &= 3; |
| 184 | if (unlikely(!(old_evt & dmsk))) |
| 185 | return 0; |
| 186 | |
| 187 | alloc_chg_list(fd, old_evt); |
| 188 | dmsk <<= FD2BIT(fd); |
| 189 | fd_evts[ofs] &= ~dmsk; |
| 190 | return 1; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 193 | REGPRM1 static void __fd_rem(int fd) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 194 | { |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 195 | uint32_t ofs = FD2OFS(fd); |
| 196 | |
| 197 | if (unlikely(!((fd_evts[ofs] >> FD2BIT(fd)) & 3))) |
| 198 | return; |
| 199 | |
| 200 | alloc_chg_list(fd, 0); |
| 201 | fd_evts[ofs] &= ~FD2MSK(fd); |
| 202 | return; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 205 | /* |
| 206 | * On valid epoll() implementations, a call to close() automatically removes |
| 207 | * the fds. This means that the FD will appear as previously unset. |
| 208 | */ |
| 209 | REGPRM1 static void __fd_clo(int fd) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 210 | { |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 211 | struct fd_chg *ptr; |
| 212 | fd_evts[FD2OFS(fd)] &= ~FD2MSK(fd); |
| 213 | ptr = chg_ptr[fd]; |
| 214 | if (ptr) { |
| 215 | ptr->prev = 0; |
| 216 | chg_ptr[fd] = NULL; |
| 217 | } |
| 218 | return; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 221 | /* |
| 222 | * epoll() poller |
| 223 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 224 | REGPRM2 static void _do_poll(struct poller *p, struct timeval *exp) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 225 | { |
| 226 | int status; |
| 227 | int fd; |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 228 | int count; |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 229 | int wait_time; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 230 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 231 | if (likely(nbchanges)) |
| 232 | fd_flush_changes(); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 233 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 234 | /* now let's wait for events */ |
Willy Tarreau | bdefc51 | 2007-05-14 02:02:04 +0200 | [diff] [blame] | 235 | if (tv_iseternity(exp)) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 236 | wait_time = -1; |
Willy Tarreau | bdefc51 | 2007-05-14 02:02:04 +0200 | [diff] [blame] | 237 | else if (tv_isge(&now, exp)) |
| 238 | wait_time = 0; |
| 239 | else |
| 240 | wait_time = __tv_ms_elapsed(&now, exp) + 1; |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 241 | |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 242 | fd = MIN(maxfd, global.tune.maxpollevents); |
| 243 | status = epoll_wait(epoll_fd, epoll_events, fd, wait_time); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 244 | tv_now(&now); |
| 245 | |
| 246 | for (count = 0; count < status; count++) { |
| 247 | fd = epoll_events[count].data.fd; |
| 248 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 249 | if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_RD)) { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 250 | if (fdtab[fd].state == FD_STCLOSE) |
| 251 | continue; |
| 252 | if (epoll_events[count].events & ( EPOLLIN | EPOLLERR | EPOLLHUP )) |
| 253 | fdtab[fd].cb[DIR_RD].f(fd); |
| 254 | } |
| 255 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 256 | if ((fd_evts[FD2OFS(fd)] >> FD2BIT(fd)) & DIR2MSK(DIR_WR)) { |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 257 | if (fdtab[fd].state == FD_STCLOSE) |
| 258 | continue; |
| 259 | if (epoll_events[count].events & ( EPOLLOUT | EPOLLERR | EPOLLHUP )) |
| 260 | fdtab[fd].cb[DIR_WR].f(fd); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /* |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 266 | * Initialization of the epoll() poller. |
| 267 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 268 | * disables the poller by setting its pref to 0. |
| 269 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 270 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 271 | { |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 272 | __label__ fail_chg_ptr, fail_chg_list, fail_fdevt, fail_ee, fail_fd; |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 273 | int fd_set_bytes; |
| 274 | |
| 275 | p->private = NULL; |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 276 | fd_set_bytes = 4 * (global.maxsock + 15) / 16; |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 277 | |
| 278 | epoll_fd = epoll_create(global.maxsock + 1); |
| 279 | if (epoll_fd < 0) |
| 280 | goto fail_fd; |
| 281 | |
| 282 | epoll_events = (struct epoll_event*) |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 283 | calloc(1, sizeof(struct epoll_event) * global.tune.maxpollevents); |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 284 | |
| 285 | if (epoll_events == NULL) |
| 286 | goto fail_ee; |
| 287 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 288 | if ((fd_evts = (uint32_t *)calloc(1, fd_set_bytes)) == NULL) |
| 289 | goto fail_fdevt; |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 290 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 291 | chg_list = (struct fd_chg *)calloc(1, sizeof(struct fd_chg) * global.maxsock); |
| 292 | if (chg_list == NULL) |
| 293 | goto fail_chg_list; |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 294 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 295 | chg_ptr = (struct fd_chg **)calloc(1, sizeof(struct fd_chg*) * global.maxsock); |
| 296 | if (chg_ptr == NULL) |
| 297 | goto fail_chg_ptr; |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 298 | |
| 299 | return 1; |
| 300 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 301 | fail_chg_ptr: |
| 302 | free(chg_list); |
| 303 | fail_chg_list: |
| 304 | free(fd_evts); |
| 305 | fail_fdevt: |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 306 | free(epoll_events); |
| 307 | fail_ee: |
| 308 | close(epoll_fd); |
| 309 | epoll_fd = 0; |
| 310 | fail_fd: |
| 311 | p->pref = 0; |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Termination of the epoll() poller. |
| 317 | * Memory is released and the poller is marked as unselectable. |
| 318 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 319 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 320 | { |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 321 | fd_flush_changes(); |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 322 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 323 | if (chg_ptr) |
| 324 | free(chg_ptr); |
| 325 | if (chg_list) |
| 326 | free(chg_list); |
| 327 | if (fd_evts) |
| 328 | free(fd_evts); |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 329 | if (epoll_events) |
| 330 | free(epoll_events); |
| 331 | |
| 332 | close(epoll_fd); |
| 333 | epoll_fd = 0; |
| 334 | |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 335 | chg_ptr = NULL; |
| 336 | chg_list = NULL; |
| 337 | fd_evts = NULL; |
| 338 | epoll_events = NULL; |
| 339 | |
Willy Tarreau | e54e917 | 2007-04-09 09:23:31 +0200 | [diff] [blame] | 340 | p->private = NULL; |
| 341 | p->pref = 0; |
| 342 | } |
| 343 | |
| 344 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 345 | * Check that the poller works. |
| 346 | * Returns 1 if OK, otherwise 0. |
| 347 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 348 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 349 | { |
| 350 | int fd; |
| 351 | |
| 352 | fd = epoll_create(global.maxsock + 1); |
| 353 | if (fd < 0) |
| 354 | return 0; |
| 355 | close(fd); |
| 356 | return 1; |
| 357 | } |
| 358 | |
| 359 | /* |
Willy Tarreau | fb8983f | 2007-06-03 16:40:44 +0200 | [diff] [blame] | 360 | * Recreate the epoll file descriptor after a fork(). Returns 1 if OK, |
| 361 | * otherwise 0. It will ensure that all processes will not share their |
| 362 | * epoll_fd. Some side effects were encountered because of this, such |
| 363 | * as epoll_wait() returning an FD which was previously deleted. |
| 364 | */ |
| 365 | REGPRM1 static int _do_fork(struct poller *p) |
| 366 | { |
| 367 | close(epoll_fd); |
| 368 | epoll_fd = epoll_create(global.maxsock + 1); |
| 369 | if (epoll_fd < 0) |
| 370 | return 0; |
| 371 | return 1; |
| 372 | } |
| 373 | |
| 374 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 375 | * It is a constructor, which means that it will automatically be called before |
| 376 | * main(). This is GCC-specific but it works at least since 2.95. |
| 377 | * Special care must be taken so that it does not need any uninitialized data. |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 378 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 379 | __attribute__((constructor)) |
| 380 | static void _do_register(void) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 381 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 382 | struct poller *p; |
| 383 | |
| 384 | if (nbpollers >= MAX_POLLERS) |
| 385 | return; |
| 386 | p = &pollers[nbpollers++]; |
| 387 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 388 | p->name = "epoll"; |
| 389 | p->pref = 300; |
| 390 | p->private = NULL; |
| 391 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 392 | p->test = _do_test; |
| 393 | p->init = _do_init; |
| 394 | p->term = _do_term; |
| 395 | p->poll = _do_poll; |
Willy Tarreau | fb8983f | 2007-06-03 16:40:44 +0200 | [diff] [blame] | 396 | p->fork = _do_fork; |
Willy Tarreau | 58094f2 | 2007-04-10 01:33:20 +0200 | [diff] [blame] | 397 | |
| 398 | p->is_set = __fd_is_set; |
| 399 | p->cond_s = p->set = __fd_set; |
| 400 | p->cond_c = p->clr = __fd_clr; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 401 | p->rem = __fd_rem; |
| 402 | p->clo = __fd_clo; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | |
| 406 | /* |
| 407 | * Local variables: |
| 408 | * c-indent-level: 8 |
| 409 | * c-basic-offset: 8 |
| 410 | * End: |
| 411 | */ |