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