Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * FD polling functions for FreeBSD kqueue() |
| 3 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 4 | * Copyright 2000-2014 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +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 | * |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 11 | */ |
| 12 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 13 | #include <unistd.h> |
| 14 | #include <sys/time.h> |
| 15 | #include <sys/types.h> |
| 16 | |
| 17 | #include <sys/event.h> |
| 18 | #include <sys/time.h> |
| 19 | |
| 20 | #include <common/compat.h> |
| 21 | #include <common/config.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 22 | #include <common/ticks.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 23 | #include <common/time.h> |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 24 | #include <common/tools.h> |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 25 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 26 | #include <types/global.h> |
| 27 | |
| 28 | #include <proto/fd.h> |
Willy Tarreau | 10146c9 | 2015-04-13 20:44:19 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 30 | |
| 31 | /* private data */ |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 32 | static int kqueue_fd[MAX_THREADS]; // per-thread kqueue_fd |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 33 | static THREAD_LOCAL struct kevent *kev = NULL; |
Olivier Houchard | ebaba75 | 2018-04-16 13:24:48 +0200 | [diff] [blame] | 34 | static struct kevent *kev_out = NULL; // Trash buffer for kevent() to write the eventlist in |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 35 | |
PiBa-NL | c55b88e | 2018-05-10 01:01:28 +0200 | [diff] [blame] | 36 | static int _update_fd(int fd, int start) |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 37 | { |
| 38 | int en; |
PiBa-NL | c55b88e | 2018-05-10 01:01:28 +0200 | [diff] [blame] | 39 | int changes = start; |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 40 | |
| 41 | en = fdtab[fd].state; |
| 42 | |
| 43 | if (!(fdtab[fd].thread_mask & tid_bit) || !(en & FD_EV_POLLED_RW)) { |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 44 | if (!(polled_mask[fd] & tid_bit)) { |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 45 | /* fd was not watched, it's still not */ |
| 46 | return 0; |
| 47 | } |
| 48 | /* fd totally removed from poll list */ |
| 49 | EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); |
| 50 | EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 51 | HA_ATOMIC_AND(&polled_mask[fd], ~tid_bit); |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 52 | } |
| 53 | else { |
| 54 | /* OK fd has to be monitored, it was either added or changed */ |
| 55 | |
| 56 | if (en & FD_EV_POLLED_R) |
| 57 | EV_SET(&kev[changes++], fd, EVFILT_READ, EV_ADD, 0, 0, NULL); |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 58 | else if (polled_mask[fd] & tid_bit) |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 59 | EV_SET(&kev[changes++], fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); |
| 60 | |
| 61 | if (en & FD_EV_POLLED_W) |
| 62 | EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL); |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 63 | else if (polled_mask[fd] & tid_bit) |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 64 | EV_SET(&kev[changes++], fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); |
| 65 | |
Olivier Houchard | cb92f5c | 2018-04-26 14:23:07 +0200 | [diff] [blame] | 66 | HA_ATOMIC_OR(&polled_mask[fd], tid_bit); |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 67 | } |
| 68 | return changes; |
| 69 | } |
| 70 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 71 | /* |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 72 | * kqueue() poller |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 73 | */ |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 74 | REGPRM2 static void _do_poll(struct poller *p, int exp) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 75 | { |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 76 | int status; |
| 77 | int count, fd, delta_ms; |
| 78 | struct timespec timeout; |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 79 | int updt_idx; |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 80 | int changes = 0; |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 81 | int old_fd; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 82 | |
Olivier Houchard | ebaba75 | 2018-04-16 13:24:48 +0200 | [diff] [blame] | 83 | timeout.tv_sec = 0; |
| 84 | timeout.tv_nsec = 0; |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 85 | /* first, scan the update list to find changes */ |
| 86 | for (updt_idx = 0; updt_idx < fd_nbupdt; updt_idx++) { |
| 87 | fd = fd_updt[updt_idx]; |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 88 | |
Olivier Houchard | 8ef1a6b | 2018-04-03 19:06:18 +0200 | [diff] [blame] | 89 | HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit); |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 90 | if (!fdtab[fd].owner) { |
| 91 | activity[tid].poll_drop++; |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 92 | continue; |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 93 | } |
PiBa-NL | c55b88e | 2018-05-10 01:01:28 +0200 | [diff] [blame] | 94 | changes = _update_fd(fd, changes); |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 95 | } |
| 96 | /* Scan the global update list */ |
| 97 | for (old_fd = fd = update_list.first; fd != -1; fd = fdtab[fd].update.next) { |
| 98 | if (fd == -2) { |
| 99 | fd = old_fd; |
| 100 | continue; |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 101 | } |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 102 | else if (fd <= -3) |
| 103 | fd = -fd -4; |
| 104 | if (fd == -1) |
| 105 | break; |
| 106 | if (fdtab[fd].update_mask & tid_bit) |
| 107 | done_update_polling(fd); |
| 108 | else |
| 109 | continue; |
| 110 | if (!fdtab[fd].owner) |
| 111 | continue; |
PiBa-NL | c55b88e | 2018-05-10 01:01:28 +0200 | [diff] [blame] | 112 | changes = _update_fd(fd, changes); |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 113 | } |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 114 | |
Olivier Houchard | ebaba75 | 2018-04-16 13:24:48 +0200 | [diff] [blame] | 115 | if (changes) { |
| 116 | #ifdef EV_RECEIPT |
| 117 | kev[0].flags |= EV_RECEIPT; |
| 118 | #else |
| 119 | /* If EV_RECEIPT isn't defined, just add an invalid entry, |
| 120 | * so that we get an error and kevent() stops before scanning |
| 121 | * the kqueue. |
| 122 | */ |
| 123 | EV_SET(&kev[changes++], -1, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); |
| 124 | #endif |
| 125 | kevent(kqueue_fd[tid], kev, changes, kev_out, changes, &timeout); |
| 126 | } |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 127 | fd_nbupdt = 0; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 128 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 129 | delta_ms = 0; |
Willy Tarreau | 79b8a62 | 2007-05-14 03:15:46 +0200 | [diff] [blame] | 130 | |
Willy Tarreau | 10146c9 | 2015-04-13 20:44:19 +0200 | [diff] [blame] | 131 | if (!exp) { |
| 132 | delta_ms = MAX_DELAY_MS; |
| 133 | timeout.tv_sec = (MAX_DELAY_MS / 1000); |
| 134 | timeout.tv_nsec = (MAX_DELAY_MS % 1000) * 1000000; |
| 135 | } |
| 136 | else if (!tick_is_expired(exp, now_ms)) { |
| 137 | delta_ms = TICKS_TO_MS(tick_remain(now_ms, exp)) + 1; |
| 138 | if (delta_ms > MAX_DELAY_MS) |
| 139 | delta_ms = MAX_DELAY_MS; |
| 140 | timeout.tv_sec = (delta_ms / 1000); |
| 141 | timeout.tv_nsec = (delta_ms % 1000) * 1000000; |
Willy Tarreau | cd5ce2a | 2007-04-09 16:25:46 +0200 | [diff] [blame] | 142 | } |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 143 | else |
| 144 | activity[tid].poll_exp++; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 145 | |
Willy Tarreau | ce036bc | 2018-01-29 14:58:02 +0100 | [diff] [blame] | 146 | fd = global.tune.maxpollevents; |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 147 | gettimeofday(&before_poll, NULL); |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 148 | status = kevent(kqueue_fd[tid], // int kq |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 149 | NULL, // const struct kevent *changelist |
| 150 | 0, // int nchanges |
| 151 | kev, // struct kevent *eventlist |
Willy Tarreau | 1db3771 | 2007-06-03 17:16:49 +0200 | [diff] [blame] | 152 | fd, // int nevents |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 153 | &timeout); // const struct timespec *timeout |
| 154 | tv_update_date(delta_ms, status); |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 155 | measure_idle(); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 156 | |
| 157 | for (count = 0; count < status; count++) { |
Christopher Faulet | ab62f51 | 2017-08-30 10:34:36 +0200 | [diff] [blame] | 158 | unsigned int n = 0; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 159 | fd = kev[count].ident; |
Willy Tarreau | 9845e75 | 2012-07-06 11:44:28 +0200 | [diff] [blame] | 160 | |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 161 | if (!fdtab[fd].owner) { |
| 162 | activity[tid].poll_dead++; |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 163 | continue; |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | if (!(fdtab[fd].thread_mask & tid_bit)) { |
| 167 | activity[tid].poll_skip++; |
| 168 | continue; |
| 169 | } |
Willy Tarreau | 076be25 | 2012-07-06 16:02:29 +0200 | [diff] [blame] | 170 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 171 | if (kev[count].filter == EVFILT_READ) { |
Willy Tarreau | fa06176 | 2017-03-13 20:49:56 +0100 | [diff] [blame] | 172 | if (kev[count].data) |
Christopher Faulet | ab62f51 | 2017-08-30 10:34:36 +0200 | [diff] [blame] | 173 | n |= FD_POLL_IN; |
Willy Tarreau | 19c4ab9 | 2017-03-13 20:36:48 +0100 | [diff] [blame] | 174 | if (kev[count].flags & EV_EOF) |
Christopher Faulet | ab62f51 | 2017-08-30 10:34:36 +0200 | [diff] [blame] | 175 | n |= FD_POLL_HUP; |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 176 | } |
| 177 | else if (kev[count].filter == EVFILT_WRITE) { |
Christopher Faulet | ab62f51 | 2017-08-30 10:34:36 +0200 | [diff] [blame] | 178 | n |= FD_POLL_OUT; |
Willy Tarreau | 19c4ab9 | 2017-03-13 20:36:48 +0100 | [diff] [blame] | 179 | if (kev[count].flags & EV_EOF) |
Christopher Faulet | ab62f51 | 2017-08-30 10:34:36 +0200 | [diff] [blame] | 180 | n |= FD_POLL_ERR; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 181 | } |
Willy Tarreau | 4a22627 | 2012-11-11 20:49:49 +0100 | [diff] [blame] | 182 | |
Christopher Faulet | ab62f51 | 2017-08-30 10:34:36 +0200 | [diff] [blame] | 183 | fd_update_events(fd, n); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 187 | |
| 188 | static int init_kqueue_per_thread() |
| 189 | { |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 190 | int fd; |
| 191 | |
Olivier Houchard | ebaba75 | 2018-04-16 13:24:48 +0200 | [diff] [blame] | 192 | /* we can have up to two events per fd, so allocate enough to store |
| 193 | * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined, |
| 194 | * so that we can add an invalid entry and get an error, to avoid |
| 195 | * scanning the kqueue uselessly. |
| 196 | */ |
| 197 | kev = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1)); |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 198 | if (kev == NULL) |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 199 | goto fail_alloc; |
| 200 | |
Christopher Faulet | 727c89b | 2018-01-25 16:40:35 +0100 | [diff] [blame] | 201 | if (MAX_THREADS > 1 && tid) { |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 202 | kqueue_fd[tid] = kqueue(); |
| 203 | if (kqueue_fd[tid] < 0) |
| 204 | goto fail_fd; |
| 205 | } |
| 206 | |
| 207 | /* we may have to unregister some events initially registered on the |
| 208 | * original fd when it was alone, and/or to register events on the new |
| 209 | * fd for this thread. Let's just mark them as updated, the poller will |
| 210 | * do the rest. |
| 211 | */ |
Willy Tarreau | ce036bc | 2018-01-29 14:58:02 +0100 | [diff] [blame] | 212 | for (fd = 0; fd < global.maxsock; fd++) |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 213 | updt_fd_polling(fd); |
| 214 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 215 | return 1; |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 216 | fail_fd: |
| 217 | free(kev); |
| 218 | fail_alloc: |
| 219 | return 0; |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static void deinit_kqueue_per_thread() |
| 223 | { |
Christopher Faulet | 727c89b | 2018-01-25 16:40:35 +0100 | [diff] [blame] | 224 | if (MAX_THREADS > 1 && tid) |
Christopher Faulet | 13b007d | 2018-01-25 16:32:18 +0100 | [diff] [blame] | 225 | close(kqueue_fd[tid]); |
| 226 | |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 227 | free(kev); |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 228 | kev = NULL; |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 231 | /* |
| 232 | * Initialization of the kqueue() poller. |
| 233 | * Returns 0 in case of failure, non-zero in case of success. If it fails, it |
| 234 | * disables the poller by setting its pref to 0. |
| 235 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 236 | REGPRM1 static int _do_init(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 237 | { |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 238 | p->private = NULL; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 239 | |
Olivier Houchard | ebaba75 | 2018-04-16 13:24:48 +0200 | [diff] [blame] | 240 | /* we can have up to two events per fd, so allocate enough to store |
| 241 | * 2*fd event, and an extra one, in case EV_RECEIPT isn't defined, |
| 242 | * so that we can add an invalid entry and get an error, to avoid |
| 243 | * scanning the kqueue uselessly. |
| 244 | */ |
| 245 | kev_out = calloc(1, sizeof(struct kevent) * (2 * global.maxsock + 1)); |
| 246 | if (!kev_out) |
| 247 | goto fail_alloc; |
| 248 | |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 249 | kqueue_fd[tid] = kqueue(); |
| 250 | if (kqueue_fd[tid] < 0) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 251 | goto fail_fd; |
| 252 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 253 | hap_register_per_thread_init(init_kqueue_per_thread); |
| 254 | hap_register_per_thread_deinit(deinit_kqueue_per_thread); |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 255 | return 1; |
| 256 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 257 | fail_fd: |
Olivier Houchard | ebaba75 | 2018-04-16 13:24:48 +0200 | [diff] [blame] | 258 | free(kev_out); |
| 259 | kev_out = NULL; |
| 260 | fail_alloc: |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 261 | p->pref = 0; |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Termination of the kqueue() poller. |
| 267 | * Memory is released and the poller is marked as unselectable. |
| 268 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 269 | REGPRM1 static void _do_term(struct poller *p) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 270 | { |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 271 | if (kqueue_fd[tid] >= 0) { |
| 272 | close(kqueue_fd[tid]); |
| 273 | kqueue_fd[tid] = -1; |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 274 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 275 | |
| 276 | p->private = NULL; |
| 277 | p->pref = 0; |
Olivier Houchard | ebaba75 | 2018-04-16 13:24:48 +0200 | [diff] [blame] | 278 | if (kev_out) { |
| 279 | free(kev_out); |
| 280 | kev_out = NULL; |
| 281 | } |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 285 | * Check that the poller works. |
| 286 | * Returns 1 if OK, otherwise 0. |
| 287 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 288 | REGPRM1 static int _do_test(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 289 | { |
| 290 | int fd; |
| 291 | |
| 292 | fd = kqueue(); |
| 293 | if (fd < 0) |
| 294 | return 0; |
| 295 | close(fd); |
| 296 | return 1; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Recreate the kqueue file descriptor after a fork(). Returns 1 if OK, |
| 301 | * otherwise 0. Note that some pollers need to be reopened after a fork() |
| 302 | * (such as kqueue), and some others may fail to do so in a chroot. |
| 303 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 304 | REGPRM1 static int _do_fork(struct poller *p) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 305 | { |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 306 | kqueue_fd[tid] = kqueue(); |
| 307 | if (kqueue_fd[tid] < 0) |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 308 | return 0; |
| 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | /* |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 313 | * It is a constructor, which means that it will automatically be called before |
| 314 | * main(). This is GCC-specific but it works at least since 2.95. |
| 315 | * Special care must be taken so that it does not need any uninitialized data. |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 316 | */ |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 317 | __attribute__((constructor)) |
| 318 | static void _do_register(void) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 319 | { |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 320 | struct poller *p; |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 321 | int i; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 322 | |
| 323 | if (nbpollers >= MAX_POLLERS) |
| 324 | return; |
Willy Tarreau | d79e79b | 2009-05-10 10:18:54 +0200 | [diff] [blame] | 325 | |
Willy Tarreau | 7a2364d | 2018-01-19 08:56:14 +0100 | [diff] [blame] | 326 | for (i = 0; i < MAX_THREADS; i++) |
| 327 | kqueue_fd[i] = -1; |
| 328 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 329 | p = &pollers[nbpollers++]; |
| 330 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 331 | p->name = "kqueue"; |
| 332 | p->pref = 300; |
Willy Tarreau | 19c4ab9 | 2017-03-13 20:36:48 +0100 | [diff] [blame] | 333 | p->flags = HAP_POLL_F_RDHUP; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 334 | p->private = NULL; |
| 335 | |
Willy Tarreau | 70c6fd8 | 2012-11-11 21:02:34 +0100 | [diff] [blame] | 336 | p->clo = NULL; |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 337 | p->test = _do_test; |
| 338 | p->init = _do_init; |
| 339 | p->term = _do_term; |
| 340 | p->poll = _do_poll; |
| 341 | p->fork = _do_fork; |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | |
| 345 | /* |
| 346 | * Local variables: |
| 347 | * c-indent-level: 8 |
| 348 | * c-basic-offset: 8 |
| 349 | * End: |
| 350 | */ |