Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * FD polling functions for SunOS event ports. |
| 3 | * |
| 4 | * Copyright 2018 Joyent, Inc. |
| 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 | #include <unistd.h> |
| 13 | #include <sys/time.h> |
| 14 | #include <sys/types.h> |
| 15 | |
| 16 | #include <poll.h> |
| 17 | #include <port.h> |
| 18 | #include <errno.h> |
| 19 | #include <syslog.h> |
| 20 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 21 | #include <haproxy/activity.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 22 | #include <haproxy/api.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 23 | #include <haproxy/clock.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 24 | #include <haproxy/fd.h> |
| 25 | #include <haproxy/global.h> |
Willy Tarreau | 3727a8a | 2020-06-04 17:37:26 +0200 | [diff] [blame] | 26 | #include <haproxy/signal.h> |
Willy Tarreau | 6dfab11 | 2021-09-30 17:53:22 +0200 | [diff] [blame] | 27 | #include <haproxy/task.h> |
Willy Tarreau | c2f7c58 | 2020-06-02 18:15:32 +0200 | [diff] [blame] | 28 | #include <haproxy/ticks.h> |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 29 | |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 30 | /* |
| 31 | * Private data: |
| 32 | */ |
| 33 | static int evports_fd[MAX_THREADS]; // per-thread evports_fd |
| 34 | static THREAD_LOCAL port_event_t *evports_evlist = NULL; |
| 35 | static THREAD_LOCAL int evports_evlist_max = 0; |
| 36 | |
| 37 | /* |
| 38 | * Convert the "state" member of "fdtab" into an event ports event mask. |
| 39 | */ |
| 40 | static inline int evports_state_to_events(int state) |
| 41 | { |
| 42 | int events = 0; |
| 43 | |
Willy Tarreau | 5bee3e2 | 2019-09-04 09:52:57 +0200 | [diff] [blame] | 44 | if (state & FD_EV_ACTIVE_W) |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 45 | events |= POLLOUT; |
Willy Tarreau | 5bee3e2 | 2019-09-04 09:52:57 +0200 | [diff] [blame] | 46 | if (state & FD_EV_ACTIVE_R) |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 47 | events |= POLLIN; |
| 48 | |
| 49 | return (events); |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Associate or dissociate this file descriptor with the event port, using the |
| 54 | * specified event mask. |
| 55 | */ |
| 56 | static inline void evports_resync_fd(int fd, int events) |
| 57 | { |
| 58 | if (events == 0) |
| 59 | port_dissociate(evports_fd[tid], PORT_SOURCE_FD, fd); |
| 60 | else |
| 61 | port_associate(evports_fd[tid], PORT_SOURCE_FD, fd, events, NULL); |
| 62 | } |
| 63 | |
| 64 | static void _update_fd(int fd) |
| 65 | { |
| 66 | int en; |
| 67 | int events; |
Willy Tarreau | 6302212 | 2022-07-06 10:37:31 +0200 | [diff] [blame] | 68 | ulong pr, ps; |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 69 | |
| 70 | en = fdtab[fd].state; |
Willy Tarreau | 6302212 | 2022-07-06 10:37:31 +0200 | [diff] [blame] | 71 | pr = _HA_ATOMIC_LOAD(&polled_mask[fd].poll_recv); |
| 72 | ps = _HA_ATOMIC_LOAD(&polled_mask[fd].poll_send); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 73 | |
Willy Tarreau | 3638d17 | 2022-07-07 08:23:03 +0200 | [diff] [blame] | 74 | if (!(fdtab[fd].thread_mask & ti->ltid_bit) || !(en & FD_EV_ACTIVE_RW)) { |
Willy Tarreau | 6302212 | 2022-07-06 10:37:31 +0200 | [diff] [blame] | 75 | if (!((pr | ps) & ti->ltid_bit)) { |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 76 | /* fd was not watched, it's still not */ |
| 77 | return; |
| 78 | } |
| 79 | /* fd totally removed from poll list */ |
| 80 | events = 0; |
Willy Tarreau | 6302212 | 2022-07-06 10:37:31 +0200 | [diff] [blame] | 81 | if (pr & ti->ltid_bit) |
| 82 | _HA_ATOMIC_AND(&polled_mask[fd].poll_recv, ~ti->ltid_bit); |
| 83 | if (ps & ti->ltid_bit) |
| 84 | _HA_ATOMIC_AND(&polled_mask[fd].poll_send, ~ti->ltid_bit); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 85 | } |
| 86 | else { |
| 87 | /* OK fd has to be monitored, it was either added or changed */ |
| 88 | events = evports_state_to_events(en); |
Willy Tarreau | 5bee3e2 | 2019-09-04 09:52:57 +0200 | [diff] [blame] | 89 | if (en & FD_EV_ACTIVE_R) { |
Willy Tarreau | 6302212 | 2022-07-06 10:37:31 +0200 | [diff] [blame] | 90 | if (!(pr & ti->ltid_bit)) |
| 91 | _HA_ATOMIC_OR(&polled_mask[fd].poll_recv, ti->ltid_bit); |
Olivier Houchard | 5305505 | 2019-07-25 14:00:18 +0000 | [diff] [blame] | 92 | } else { |
Willy Tarreau | 6302212 | 2022-07-06 10:37:31 +0200 | [diff] [blame] | 93 | if (pr & ti->ltid_bit) |
| 94 | _HA_ATOMIC_AND(&polled_mask[fd].poll_recv, ~ti->ltid_bit); |
Olivier Houchard | 5305505 | 2019-07-25 14:00:18 +0000 | [diff] [blame] | 95 | } |
Willy Tarreau | 5bee3e2 | 2019-09-04 09:52:57 +0200 | [diff] [blame] | 96 | if (en & FD_EV_ACTIVE_W) { |
Willy Tarreau | 6302212 | 2022-07-06 10:37:31 +0200 | [diff] [blame] | 97 | if (!(ps & ti->ltid_bit)) |
| 98 | _HA_ATOMIC_OR(&polled_mask[fd].poll_send, ti->ltid_bit); |
Olivier Houchard | 5305505 | 2019-07-25 14:00:18 +0000 | [diff] [blame] | 99 | } else { |
Willy Tarreau | 6302212 | 2022-07-06 10:37:31 +0200 | [diff] [blame] | 100 | if (ps & ti->ltid_bit) |
| 101 | _HA_ATOMIC_AND(&polled_mask[fd].poll_send, ~ti->ltid_bit); |
Olivier Houchard | 5305505 | 2019-07-25 14:00:18 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 104 | } |
| 105 | evports_resync_fd(fd, events); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Event Ports poller. This routine interacts with the file descriptor |
| 110 | * management data structures and routines; see the large block comment in |
| 111 | * "src/fd.c" for more information. |
| 112 | */ |
| 113 | |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 114 | static void _do_poll(struct poller *p, int exp, int wake) |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 115 | { |
| 116 | int i; |
| 117 | int wait_time; |
| 118 | struct timespec timeout_ts; |
| 119 | unsigned int nevlist; |
| 120 | int fd, old_fd; |
| 121 | int status; |
| 122 | |
| 123 | /* |
| 124 | * Scan the list of file descriptors with an updated status: |
| 125 | */ |
| 126 | for (i = 0; i < fd_nbupdt; i++) { |
| 127 | fd = fd_updt[i]; |
| 128 | |
Willy Tarreau | 1f947cb | 2022-07-09 23:55:43 +0200 | [diff] [blame] | 129 | if (!fd_grab_tgid(fd, tgid)) { |
| 130 | /* was reassigned */ |
Willy Tarreau | e406386 | 2020-06-17 20:35:33 +0200 | [diff] [blame] | 131 | activity[tid].poll_drop_fd++; |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 132 | continue; |
| 133 | } |
| 134 | |
Willy Tarreau | 1f947cb | 2022-07-09 23:55:43 +0200 | [diff] [blame] | 135 | _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~ti->ltid_bit); |
| 136 | |
| 137 | if (fdtab[fd].owner) |
| 138 | _update_fd(fd); |
| 139 | else |
| 140 | activity[tid].poll_drop_fd++; |
| 141 | |
| 142 | fd_drop_tgid(fd); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 143 | } |
| 144 | fd_nbupdt = 0; |
Willy Tarreau | 1f947cb | 2022-07-09 23:55:43 +0200 | [diff] [blame] | 145 | |
| 146 | /* Scan the shared update list */ |
Willy Tarreau | 35ee710 | 2022-07-08 11:33:43 +0200 | [diff] [blame] | 147 | for (old_fd = fd = update_list[tgid - 1].first; fd != -1; fd = fdtab[fd].update.next) { |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 148 | if (fd == -2) { |
| 149 | fd = old_fd; |
| 150 | continue; |
| 151 | } |
| 152 | else if (fd <= -3) |
| 153 | fd = -fd -4; |
| 154 | if (fd == -1) |
| 155 | break; |
Willy Tarreau | 1f947cb | 2022-07-09 23:55:43 +0200 | [diff] [blame] | 156 | |
| 157 | if (!fd_grab_tgid(fd, tgid)) { |
| 158 | /* was reassigned */ |
| 159 | activity[tid].poll_drop_fd++; |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 160 | continue; |
Willy Tarreau | 1f947cb | 2022-07-09 23:55:43 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Willy Tarreau | 6983426 | 2022-07-25 15:39:21 +0200 | [diff] [blame] | 163 | if (!(fdtab[fd].update_mask & ti->ltid_bit)) { |
| 164 | fd_drop_tgid(fd); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 165 | continue; |
Willy Tarreau | 6983426 | 2022-07-25 15:39:21 +0200 | [diff] [blame] | 166 | } |
Willy Tarreau | 1f947cb | 2022-07-09 23:55:43 +0200 | [diff] [blame] | 167 | |
| 168 | done_update_polling(fd); |
| 169 | |
| 170 | if (fdtab[fd].owner) |
| 171 | _update_fd(fd); |
| 172 | else |
| 173 | activity[tid].poll_drop_fd++; |
| 174 | |
| 175 | fd_drop_tgid(fd); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 178 | thread_idle_now(); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 179 | thread_harmless_now(); |
| 180 | |
Matthias Wirth | eea152e | 2022-09-09 10:21:00 +0200 | [diff] [blame] | 181 | /* Now let's wait for polled events. */ |
| 182 | wait_time = wake ? 0 : compute_poll_timeout(exp); |
Willy Tarreau | f9d5e10 | 2021-10-08 10:43:59 +0200 | [diff] [blame] | 183 | clock_entering_poll(); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 184 | |
| 185 | do { |
| 186 | int timeout = (global.tune.options & GTUNE_BUSY_POLLING) ? 0 : wait_time; |
| 187 | int interrupted = 0; |
| 188 | nevlist = 1; /* desired number of events to be retrieved */ |
| 189 | timeout_ts.tv_sec = (timeout / 1000); |
| 190 | timeout_ts.tv_nsec = (timeout % 1000) * 1000000; |
| 191 | |
| 192 | status = port_getn(evports_fd[tid], |
| 193 | evports_evlist, |
| 194 | evports_evlist_max, |
| 195 | &nevlist, /* updated to the number of events retrieved */ |
| 196 | &timeout_ts); |
| 197 | if (status != 0) { |
| 198 | int e = errno; |
| 199 | switch (e) { |
| 200 | case ETIME: |
| 201 | /* |
| 202 | * Though the manual page has not historically made it |
| 203 | * clear, port_getn() can return -1 with an errno of |
| 204 | * ETIME and still have returned some number of events. |
| 205 | */ |
| 206 | /* nevlist >= 0 */ |
| 207 | break; |
| 208 | default: |
| 209 | nevlist = 0; |
| 210 | interrupted = 1; |
| 211 | break; |
| 212 | } |
| 213 | } |
Willy Tarreau | 58b73f9 | 2022-09-21 08:11:38 +0200 | [diff] [blame] | 214 | clock_update_local_date(timeout, nevlist); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 215 | |
| 216 | if (nevlist || interrupted) |
| 217 | break; |
| 218 | if (timeout || !wait_time) |
| 219 | break; |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 220 | if (tick_isset(exp) && tick_is_expired(exp, now_ms)) |
| 221 | break; |
| 222 | } while(1); |
| 223 | |
Willy Tarreau | 58b73f9 | 2022-09-21 08:11:38 +0200 | [diff] [blame] | 224 | clock_update_global_date(); |
Willy Tarreau | 058b2c1 | 2022-06-22 15:21:34 +0200 | [diff] [blame] | 225 | fd_leaving_poll(wait_time, nevlist); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 226 | |
Willy Tarreau | e545153 | 2020-06-17 20:25:18 +0200 | [diff] [blame] | 227 | if (nevlist > 0) |
| 228 | activity[tid].poll_io++; |
| 229 | |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 230 | for (i = 0; i < nevlist; i++) { |
| 231 | unsigned int n = 0; |
| 232 | int events, rebind_events; |
Willy Tarreau | 200bd50 | 2021-07-29 16:57:19 +0200 | [diff] [blame] | 233 | int ret; |
| 234 | |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 235 | fd = evports_evlist[i].portev_object; |
| 236 | events = evports_evlist[i].portev_events; |
| 237 | |
Willy Tarreau | 38e8a1c | 2020-06-23 10:04:54 +0200 | [diff] [blame] | 238 | #ifdef DEBUG_FD |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 239 | _HA_ATOMIC_INC(&fdtab[fd].event_count); |
Willy Tarreau | 38e8a1c | 2020-06-23 10:04:54 +0200 | [diff] [blame] | 240 | #endif |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 241 | /* |
| 242 | * By virtue of receiving an event for this file descriptor, it |
| 243 | * is no longer associated with the port in question. Store |
| 244 | * the previous event mask so that we may reassociate after |
| 245 | * processing is complete. |
| 246 | */ |
| 247 | rebind_events = evports_state_to_events(fdtab[fd].state); |
| 248 | /* rebind_events != 0 */ |
| 249 | |
| 250 | /* |
| 251 | * Set bits based on the events we received from the port: |
| 252 | */ |
Emmanuel Hocdet | 7ceb96b | 2019-09-19 11:08:26 +0000 | [diff] [blame] | 253 | n = ((events & POLLIN) ? FD_EV_READY_R : 0) | |
| 254 | ((events & POLLOUT) ? FD_EV_READY_W : 0) | |
| 255 | ((events & POLLHUP) ? FD_EV_SHUT_RW : 0) | |
| 256 | ((events & POLLERR) ? FD_EV_ERR_RW : 0); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 257 | |
| 258 | /* |
| 259 | * Call connection processing callbacks. Note that it's |
| 260 | * possible for this processing to alter the required event |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 261 | * port association; i.e., the "state" member of the "fdtab" |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 262 | * entry. If it changes, the fd will be placed on the updated |
| 263 | * list for processing the next time we are called. |
| 264 | */ |
Willy Tarreau | 200bd50 | 2021-07-29 16:57:19 +0200 | [diff] [blame] | 265 | ret = fd_update_events(fd, n); |
| 266 | |
Willy Tarreau | b1093c6 | 2022-07-09 18:55:37 +0200 | [diff] [blame] | 267 | /* polling will be on this instance if the FD was migrated */ |
| 268 | if (ret == FD_UPDT_MIGRATED) |
Willy Tarreau | 200bd50 | 2021-07-29 16:57:19 +0200 | [diff] [blame] | 269 | continue; |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 270 | |
| 271 | /* |
| 272 | * This file descriptor was closed during the processing of |
| 273 | * polled events. No need to reassociate. |
| 274 | */ |
Willy Tarreau | 200bd50 | 2021-07-29 16:57:19 +0200 | [diff] [blame] | 275 | if (ret == FD_UPDT_CLOSED) |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 276 | continue; |
| 277 | |
| 278 | /* |
| 279 | * Reassociate with the port, using the same event mask as |
| 280 | * before. This call will not result in a dissociation as we |
| 281 | * asserted that _some_ events needed to be rebound above. |
| 282 | * |
| 283 | * Reassociating with the same mask allows us to mimic the |
| 284 | * level-triggered behaviour of poll(2). In the event that we |
| 285 | * are interested in the same events on the next turn of the |
| 286 | * loop, this represents no extra work. |
| 287 | * |
| 288 | * If this additional port_associate(3C) call becomes a |
| 289 | * performance problem, we would need to verify that we can |
| 290 | * correctly interact with the file descriptor cache and update |
| 291 | * list (see "src/fd.c") to avoid reassociating here, or to use |
| 292 | * a different events mask. |
| 293 | */ |
| 294 | evports_resync_fd(fd, rebind_events); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | static int init_evports_per_thread() |
| 299 | { |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 300 | evports_evlist_max = global.tune.maxpollevents; |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 301 | evports_evlist = calloc(evports_evlist_max, sizeof(*evports_evlist)); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 302 | if (evports_evlist == NULL) { |
| 303 | goto fail_alloc; |
| 304 | } |
| 305 | |
| 306 | if (MAX_THREADS > 1 && tid) { |
| 307 | if ((evports_fd[tid] = port_create()) == -1) { |
| 308 | goto fail_fd; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /* we may have to unregister some events initially registered on the |
| 313 | * original fd when it was alone, and/or to register events on the new |
| 314 | * fd for this thread. Let's just mark them as updated, the poller will |
| 315 | * do the rest. |
| 316 | */ |
Willy Tarreau | d95f18f | 2022-07-09 23:23:50 +0200 | [diff] [blame] | 317 | fd_reregister_all(tgid, ti->ltid_bit); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 318 | |
| 319 | return 1; |
| 320 | |
| 321 | fail_fd: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 322 | ha_free(&evports_evlist); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 323 | evports_evlist_max = 0; |
| 324 | fail_alloc: |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static void deinit_evports_per_thread() |
| 329 | { |
| 330 | if (MAX_THREADS > 1 && tid) |
| 331 | close(evports_fd[tid]); |
| 332 | |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 333 | ha_free(&evports_evlist); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 334 | evports_evlist_max = 0; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Initialisation of the event ports poller. |
| 339 | * Returns 0 in case of failure, non-zero in case of success. |
| 340 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 341 | static int _do_init(struct poller *p) |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 342 | { |
| 343 | p->private = NULL; |
| 344 | |
| 345 | if ((evports_fd[tid] = port_create()) == -1) { |
| 346 | goto fail; |
| 347 | } |
| 348 | |
| 349 | hap_register_per_thread_init(init_evports_per_thread); |
| 350 | hap_register_per_thread_deinit(deinit_evports_per_thread); |
| 351 | |
| 352 | return 1; |
| 353 | |
| 354 | fail: |
| 355 | p->pref = 0; |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Termination of the event ports poller. |
| 361 | * All resources are released and the poller is marked as inoperative. |
| 362 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 363 | static void _do_term(struct poller *p) |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 364 | { |
| 365 | if (evports_fd[tid] != -1) { |
| 366 | close(evports_fd[tid]); |
| 367 | evports_fd[tid] = -1; |
| 368 | } |
| 369 | |
| 370 | p->private = NULL; |
| 371 | p->pref = 0; |
| 372 | |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 373 | ha_free(&evports_evlist); |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 374 | evports_evlist_max = 0; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Run-time check to make sure we can allocate the resources needed for |
| 379 | * the poller to function correctly. |
| 380 | * Returns 1 on success, otherwise 0. |
| 381 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 382 | static int _do_test(struct poller *p) |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 383 | { |
| 384 | int fd; |
| 385 | |
| 386 | if ((fd = port_create()) == -1) { |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | close(fd); |
| 391 | return 1; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Close and recreate the event port after fork(). Returns 1 on success, |
| 396 | * otherwise 0. If this function fails, "_do_term()" must be called to |
| 397 | * clean up the poller. |
| 398 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 399 | static int _do_fork(struct poller *p) |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 400 | { |
| 401 | if (evports_fd[tid] != -1) { |
| 402 | close(evports_fd[tid]); |
| 403 | } |
| 404 | |
| 405 | if ((evports_fd[tid] = port_create()) == -1) { |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | return 1; |
| 410 | } |
| 411 | |
| 412 | /* |
Willy Tarreau | 740d749 | 2022-04-25 19:00:55 +0200 | [diff] [blame] | 413 | * Registers the poller. |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 414 | */ |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 415 | static void _do_register(void) |
| 416 | { |
| 417 | struct poller *p; |
| 418 | int i; |
| 419 | |
| 420 | if (nbpollers >= MAX_POLLERS) |
| 421 | return; |
| 422 | |
| 423 | for (i = 0; i < MAX_THREADS; i++) |
| 424 | evports_fd[i] = -1; |
| 425 | |
| 426 | p = &pollers[nbpollers++]; |
| 427 | |
| 428 | p->name = "evports"; |
| 429 | p->pref = 300; |
Willy Tarreau | 11ef083 | 2019-11-28 18:17:33 +0100 | [diff] [blame] | 430 | p->flags = HAP_POLL_F_ERRHUP; |
Emmanuel Hocdet | 0ba4f48 | 2019-04-08 16:53:32 +0000 | [diff] [blame] | 431 | p->private = NULL; |
| 432 | |
| 433 | p->clo = NULL; |
| 434 | p->test = _do_test; |
| 435 | p->init = _do_init; |
| 436 | p->term = _do_term; |
| 437 | p->poll = _do_poll; |
| 438 | p->fork = _do_fork; |
| 439 | } |
Willy Tarreau | 740d749 | 2022-04-25 19:00:55 +0200 | [diff] [blame] | 440 | |
| 441 | INITCALL0(STG_REGISTER, _do_register); |