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