Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file select.c |
| 2 | * select filedescriptor handling. |
| 3 | * Taken from: |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 4 | * userspace logging daemon for the iptables ULOG target |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 5 | * of the linux 2.4 netfilter subsystem. */ |
| 6 | /* |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 7 | * (C) 2000-2020 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 6dda35a | 2022-11-09 16:54:50 +0100 | [diff] [blame] | 8 | * All Rights Reserved. |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 9 | * |
| 10 | * SPDX-License-Identifier: GPL-2.0+ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9d92f0e | 2010-10-31 13:56:45 +0100 | [diff] [blame] | 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <fcntl.h> |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 24 | #include <stdio.h> |
Harald Welte | ea91a51 | 2017-07-13 14:28:30 +0200 | [diff] [blame] | 25 | #include <unistd.h> |
Harald Welte | 9e166e8 | 2014-03-10 17:28:33 +0100 | [diff] [blame] | 26 | #include <string.h> |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 27 | #include <stdbool.h> |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 28 | #include <errno.h> |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 29 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 30 | #include <osmocom/core/select.h> |
| 31 | #include <osmocom/core/linuxlist.h> |
| 32 | #include <osmocom/core/timer.h> |
Harald Welte | 52a8375 | 2019-02-21 16:37:10 +0100 | [diff] [blame] | 33 | #include <osmocom/core/logging.h> |
Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 34 | #include <osmocom/core/talloc.h> |
| 35 | #include <osmocom/core/utils.h> |
Philipp Maier | b1ef8f5 | 2021-12-06 16:31:02 +0100 | [diff] [blame] | 36 | #include <osmocom/core/stat_item.h> |
| 37 | #include <osmocom/core/stats_tcp.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 38 | |
Pau Espin Pedrol | 88955fb | 2023-01-18 18:54:00 +0100 | [diff] [blame] | 39 | #include "config.h" |
Harald Welte | 5484480 | 2010-02-20 22:23:08 +0100 | [diff] [blame] | 40 | |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 41 | #if defined(HAVE_SYS_SELECT_H) && defined(HAVE_POLL_H) |
Harald Welte | 1d604d8 | 2017-05-17 15:32:35 +0100 | [diff] [blame] | 42 | #include <sys/select.h> |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 43 | #include <poll.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 44 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 45 | /*! \addtogroup select |
| 46 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 47 | * select() loop abstraction |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 48 | * |
| 49 | * \file select.c */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 50 | |
Harald Welte | 7a010b1 | 2019-04-06 13:46:40 +0200 | [diff] [blame] | 51 | /* keep a set of file descriptors per-thread, so that each thread can have its own |
| 52 | * distinct set of file descriptors to interact with */ |
| 53 | static __thread int maxfd = 0; |
| 54 | static __thread struct llist_head osmo_fds; /* TLS cannot use LLIST_HEAD() */ |
| 55 | static __thread int unregistered_count; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 56 | |
Pau Espin Pedrol | c46a15d | 2023-03-09 17:37:15 +0100 | [diff] [blame] | 57 | /* Array of struct osmo_fd * (size "max_fd") ordered by "ofd->fd" */ |
| 58 | static __thread struct { |
| 59 | struct osmo_fd **table; |
| 60 | unsigned int size; |
| 61 | } osmo_fd_lookup; |
| 62 | |
| 63 | static void osmo_fd_lookup_table_extend(unsigned int new_max_fd) |
| 64 | { |
| 65 | unsigned int min_new_size; |
| 66 | unsigned int pw2; |
| 67 | unsigned int new_size; |
| 68 | |
| 69 | /* First calculate the minimally required new size of the array in bytes: */ |
| 70 | min_new_size = (new_max_fd + 1) * sizeof(struct osmo_fd *); |
| 71 | /* Now find the lower power of two of min_new_size (in bytes): */ |
| 72 | pw2 = 32 - __builtin_clz(min_new_size); |
| 73 | /* get next (upper side) power of 2: */ |
| 74 | pw2++; |
| 75 | OSMO_ASSERT(pw2 <= 31); /* Avoid shifting more than 31 bits */ |
| 76 | new_size = 1 << (pw2 + 1); |
| 77 | |
| 78 | /* Let's make it at least 1024B, to avoid reallocating quickly at startup */ |
| 79 | if (new_size < 1024) |
| 80 | new_size = 1024; |
| 81 | if (new_size > osmo_fd_lookup.size) { |
| 82 | uint8_t *ptr = talloc_realloc_size(OTC_GLOBAL, osmo_fd_lookup.table, new_size); |
| 83 | OSMO_ASSERT(ptr); |
| 84 | memset(ptr + osmo_fd_lookup.size, 0, new_size - osmo_fd_lookup.size); |
| 85 | osmo_fd_lookup.table = (struct osmo_fd **)ptr; |
| 86 | osmo_fd_lookup.size = new_size; |
| 87 | } |
| 88 | } |
| 89 | |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 90 | #ifndef FORCE_IO_SELECT |
| 91 | struct poll_state { |
| 92 | /* array of pollfd */ |
| 93 | struct pollfd *poll; |
| 94 | /* number of entries in pollfd allocated */ |
| 95 | unsigned int poll_size; |
| 96 | /* number of osmo_fd registered */ |
| 97 | unsigned int num_registered; |
| 98 | }; |
| 99 | static __thread struct poll_state g_poll; |
| 100 | #endif /* FORCE_IO_SELECT */ |
| 101 | |
Neels Hofmeyr | ac49bda | 2021-06-04 16:30:04 +0200 | [diff] [blame] | 102 | /*! See osmo_select_shutdown_request() */ |
| 103 | static int _osmo_select_shutdown_requested = 0; |
| 104 | /*! See osmo_select_shutdown_request() */ |
| 105 | static bool _osmo_select_shutdown_done = false; |
| 106 | |
Harald Welte | 6c0a0e6 | 2017-08-12 11:43:14 +0200 | [diff] [blame] | 107 | /*! Set up an osmo-fd. Will not register it. |
| 108 | * \param[inout] ofd Osmo FD to be set-up |
| 109 | * \param[in] fd OS-level file descriptor number |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 110 | * \param[in] when bit-mask of OSMO_FD_{READ,WRITE,EXECEPT} |
Harald Welte | 6c0a0e6 | 2017-08-12 11:43:14 +0200 | [diff] [blame] | 111 | * \param[in] cb Call-back function to be called |
| 112 | * \param[in] data Private context pointer |
| 113 | * \param[in] priv_nr Private number |
| 114 | */ |
| 115 | void osmo_fd_setup(struct osmo_fd *ofd, int fd, unsigned int when, |
| 116 | int (*cb)(struct osmo_fd *fd, unsigned int what), |
| 117 | void *data, unsigned int priv_nr) |
| 118 | { |
| 119 | ofd->fd = fd; |
| 120 | ofd->when = when; |
| 121 | ofd->cb = cb; |
| 122 | ofd->data = data; |
| 123 | ofd->priv_nr = priv_nr; |
| 124 | } |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 125 | |
Harald Welte | 7e65791 | 2020-10-18 22:07:21 +0200 | [diff] [blame] | 126 | /*! Update the 'when' field of osmo_fd. "ofd->when = (ofd->when & when_mask) | when". |
| 127 | * Use this function instead of directly modifying ofd->when, as the latter will be |
| 128 | * removed soon. */ |
| 129 | void osmo_fd_update_when(struct osmo_fd *ofd, unsigned int when_mask, unsigned int when) |
| 130 | { |
| 131 | ofd->when &= when_mask; |
| 132 | ofd->when |= when; |
| 133 | } |
| 134 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 135 | /*! Check if a file descriptor is already registered |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 136 | * \param[in] fd osmocom file descriptor to be checked |
| 137 | * \returns true if registered; otherwise false |
| 138 | */ |
| 139 | bool osmo_fd_is_registered(struct osmo_fd *fd) |
| 140 | { |
| 141 | struct osmo_fd *entry; |
| 142 | llist_for_each_entry(entry, &osmo_fds, list) { |
| 143 | if (entry == fd) { |
| 144 | return true; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return false; |
| 149 | } |
| 150 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 151 | /*! Register a new file descriptor with select loop abstraction |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 152 | * \param[in] fd osmocom file descriptor to be registered |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 153 | * \returns 0 on success; negative in case of error |
Pau Espin Pedrol | 153519f | 2023-03-14 12:01:37 +0100 | [diff] [blame] | 154 | * |
| 155 | * The API expects fd field of the struct osmo_fd to remain unchanged while |
| 156 | * registered, ie until osmo_fd_unregister() is called on it. |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 157 | */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 158 | int osmo_fd_register(struct osmo_fd *fd) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 159 | { |
| 160 | int flags; |
| 161 | |
| 162 | /* make FD nonblocking */ |
| 163 | flags = fcntl(fd->fd, F_GETFL); |
| 164 | if (flags < 0) |
| 165 | return flags; |
| 166 | flags |= O_NONBLOCK; |
| 167 | flags = fcntl(fd->fd, F_SETFL, flags); |
| 168 | if (flags < 0) |
| 169 | return flags; |
| 170 | |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 171 | /* set close-on-exec flag */ |
| 172 | flags = fcntl(fd->fd, F_GETFD); |
| 173 | if (flags < 0) |
| 174 | return flags; |
| 175 | flags |= FD_CLOEXEC; |
| 176 | flags = fcntl(fd->fd, F_SETFD, flags); |
| 177 | if (flags < 0) |
| 178 | return flags; |
| 179 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 180 | /* Register FD */ |
Pau Espin Pedrol | c46a15d | 2023-03-09 17:37:15 +0100 | [diff] [blame] | 181 | if (fd->fd > maxfd) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 182 | maxfd = fd->fd; |
Pau Espin Pedrol | c46a15d | 2023-03-09 17:37:15 +0100 | [diff] [blame] | 183 | osmo_fd_lookup_table_extend(maxfd); |
| 184 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 185 | |
Pau Espin Pedrol | d05def0 | 2020-05-09 19:24:21 +0200 | [diff] [blame] | 186 | #ifdef OSMO_FD_CHECK |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 187 | if (osmo_fd_is_registered(fd)) { |
| 188 | fprintf(stderr, "Adding a osmo_fd that is already in the list.\n"); |
| 189 | return 0; |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 190 | } |
| 191 | #endif |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 192 | #ifndef FORCE_IO_SELECT |
| 193 | if (g_poll.num_registered + 1 > g_poll.poll_size) { |
| 194 | struct pollfd *p; |
| 195 | unsigned int new_size = g_poll.poll_size ? g_poll.poll_size * 2 : 1024; |
| 196 | p = talloc_realloc(OTC_GLOBAL, g_poll.poll, struct pollfd, new_size); |
| 197 | if (!p) |
| 198 | return -ENOMEM; |
| 199 | memset(p + g_poll.poll_size, 0, new_size - g_poll.poll_size); |
| 200 | g_poll.poll = p; |
| 201 | g_poll.poll_size = new_size; |
| 202 | } |
| 203 | g_poll.num_registered++; |
| 204 | #endif /* FORCE_IO_SELECT */ |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 205 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 206 | llist_add_tail(&fd->list, &osmo_fds); |
Pau Espin Pedrol | c46a15d | 2023-03-09 17:37:15 +0100 | [diff] [blame] | 207 | osmo_fd_lookup.table[fd->fd] = fd; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 212 | /*! Unregister a file descriptor from select loop abstraction |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 213 | * \param[in] fd osmocom file descriptor to be unregistered |
Pau Espin Pedrol | 153519f | 2023-03-14 12:01:37 +0100 | [diff] [blame] | 214 | * |
Pau Espin Pedrol | 25729cf | 2023-03-14 12:06:27 +0100 | [diff] [blame] | 215 | * Caller is responsible for ensuring the fd is really registered before calling this API. |
Pau Espin Pedrol | 153519f | 2023-03-14 12:01:37 +0100 | [diff] [blame] | 216 | * This function must be called before changing the value of the fd field in |
| 217 | * the struct osmo_fd. |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 218 | */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 219 | void osmo_fd_unregister(struct osmo_fd *fd) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 220 | { |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 221 | /* Note: when fd is inside the osmo_fds list (not registered before) |
| 222 | * this function will crash! If in doubt, check file descriptor with |
| 223 | * osmo_fd_is_registered() */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 224 | unregistered_count++; |
| 225 | llist_del(&fd->list); |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 226 | #ifndef FORCE_IO_SELECT |
| 227 | g_poll.num_registered--; |
| 228 | #endif /* FORCE_IO_SELECT */ |
Philipp Maier | b1ef8f5 | 2021-12-06 16:31:02 +0100 | [diff] [blame] | 229 | |
Pau Espin Pedrol | 6fd7f41 | 2023-03-14 12:14:36 +0100 | [diff] [blame] | 230 | if (OSMO_UNLIKELY(fd->fd < 0 || fd->fd > maxfd)) { |
| 231 | /* Some old users used to incorrectly set fd = -1 *before* calling osmo_unregister(). |
| 232 | * Hence, in order to keep backward compatibility it's not possible to assert() here. |
| 233 | * Instead, print an error message since this is actually a bug in the API user. */ |
| 234 | #ifdef OSMO_FD_CHECK |
| 235 | osmo_panic("osmo_fd_unregister(fd=%u) out of expected range (0..%u), fix your code!!!\n", |
| 236 | fd->fd, maxfd); |
| 237 | #else |
| 238 | fprintf(stderr, "osmo_fd_unregister(fd=%u) out of expected range (0..%u), fix your code!!!\n", |
| 239 | fd->fd, maxfd); |
| 240 | return; |
| 241 | #endif |
| 242 | } |
| 243 | |
| 244 | osmo_fd_lookup.table[fd->fd] = NULL; |
Philipp Maier | b1ef8f5 | 2021-12-06 16:31:02 +0100 | [diff] [blame] | 245 | /* If existent, free any statistical data */ |
| 246 | osmo_stats_tcp_osmo_fd_unregister(fd); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Harald Welte | ea91a51 | 2017-07-13 14:28:30 +0200 | [diff] [blame] | 249 | /*! Close a file descriptor, mark it as closed + unregister from select loop abstraction |
| 250 | * \param[in] fd osmocom file descriptor to be unregistered + closed |
| 251 | * |
| 252 | * If \a fd is registered, we unregister it from the select() loop |
| 253 | * abstraction. We then close the fd and set it to -1, as well as |
| 254 | * unsetting any 'when' flags */ |
| 255 | void osmo_fd_close(struct osmo_fd *fd) |
| 256 | { |
| 257 | if (osmo_fd_is_registered(fd)) |
| 258 | osmo_fd_unregister(fd); |
| 259 | if (fd->fd != -1) |
| 260 | close(fd->fd); |
| 261 | fd->fd = -1; |
| 262 | fd->when = 0; |
| 263 | } |
| 264 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 265 | /*! Populate the fd_sets and return the highest fd number |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 266 | * \param[in] _rset The readfds to populate |
| 267 | * \param[in] _wset The wrtiefds to populate |
| 268 | * \param[in] _eset The errorfds to populate |
| 269 | * |
| 270 | * \returns The highest file descriptor seen or 0 on an empty list |
| 271 | */ |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 272 | inline int osmo_fd_fill_fds(void *_rset, void *_wset, void *_eset) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 273 | { |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 274 | fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset; |
| 275 | struct osmo_fd *ufd; |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 276 | int highfd = 0; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 277 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 278 | llist_for_each_entry(ufd, &osmo_fds, list) { |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 279 | if (ufd->when & OSMO_FD_READ) |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 280 | FD_SET(ufd->fd, readset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 281 | |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 282 | if (ufd->when & OSMO_FD_WRITE) |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 283 | FD_SET(ufd->fd, writeset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 284 | |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 285 | if (ufd->when & OSMO_FD_EXCEPT) |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 286 | FD_SET(ufd->fd, exceptset); |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 287 | |
| 288 | if (ufd->fd > highfd) |
| 289 | highfd = ufd->fd; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 292 | return highfd; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 293 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 294 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 295 | inline int osmo_fd_disp_fds(void *_rset, void *_wset, void *_eset) |
| 296 | { |
| 297 | struct osmo_fd *ufd, *tmp; |
| 298 | int work = 0; |
| 299 | fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 300 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 301 | restart: |
| 302 | unregistered_count = 0; |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 303 | llist_for_each_entry_safe(ufd, tmp, &osmo_fds, list) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 304 | int flags = 0; |
| 305 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 306 | if (FD_ISSET(ufd->fd, readset)) { |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 307 | flags |= OSMO_FD_READ; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 308 | FD_CLR(ufd->fd, readset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 311 | if (FD_ISSET(ufd->fd, writeset)) { |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 312 | flags |= OSMO_FD_WRITE; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 313 | FD_CLR(ufd->fd, writeset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 314 | } |
| 315 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 316 | if (FD_ISSET(ufd->fd, exceptset)) { |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 317 | flags |= OSMO_FD_EXCEPT; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 318 | FD_CLR(ufd->fd, exceptset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | if (flags) { |
| 322 | work = 1; |
Harald Welte | 52a8375 | 2019-02-21 16:37:10 +0100 | [diff] [blame] | 323 | /* make sure to clear any log context before processing the next incoming message |
| 324 | * as part of some file descriptor callback. This effectively prevents "context |
| 325 | * leaking" from processing of one message into processing of the next message as part |
| 326 | * of one iteration through the list of file descriptors here. See OS#3813 */ |
| 327 | log_reset_context(); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 328 | ufd->cb(ufd, flags); |
| 329 | } |
Holger Hans Peter Freyther | 92e1e70 | 2014-04-09 17:24:00 +0200 | [diff] [blame] | 330 | /* ugly, ugly hack. If more than one filedescriptor was |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 331 | * unregistered, they might have been consecutive and |
| 332 | * llist_for_each_entry_safe() is no longer safe */ |
Holger Hans Peter Freyther | 23ba474 | 2010-04-11 17:33:19 +0200 | [diff] [blame] | 333 | /* this seems to happen with the last element of the list as well */ |
| 334 | if (unregistered_count >= 1) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 335 | goto restart; |
| 336 | } |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 337 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 338 | return work; |
| 339 | } |
| 340 | |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 341 | |
| 342 | #ifndef FORCE_IO_SELECT |
| 343 | /* fill g_poll.poll and return the number of entries filled */ |
| 344 | static unsigned int poll_fill_fds(void) |
| 345 | { |
| 346 | struct osmo_fd *ufd; |
| 347 | unsigned int i = 0; |
| 348 | |
| 349 | llist_for_each_entry(ufd, &osmo_fds, list) { |
| 350 | struct pollfd *p; |
| 351 | |
| 352 | if (!ufd->when) |
| 353 | continue; |
| 354 | |
| 355 | p = &g_poll.poll[i++]; |
| 356 | |
| 357 | p->fd = ufd->fd; |
| 358 | p->events = 0; |
| 359 | p->revents = 0; |
| 360 | |
| 361 | /* use the same mapping as the Linux kernel does in fs/select.c */ |
| 362 | if (ufd->when & OSMO_FD_READ) |
| 363 | p->events |= POLLIN | POLLHUP | POLLERR; |
| 364 | |
| 365 | if (ufd->when & OSMO_FD_WRITE) |
| 366 | p->events |= POLLOUT | POLLERR; |
| 367 | |
| 368 | if (ufd->when & OSMO_FD_EXCEPT) |
| 369 | p->events |= POLLPRI; |
| 370 | |
| 371 | } |
| 372 | |
| 373 | return i; |
| 374 | } |
| 375 | |
| 376 | /* iterate over first n_fd entries of g_poll.poll + dispatch */ |
Harald Welte | 0b08d51 | 2022-01-09 12:03:12 +0100 | [diff] [blame] | 377 | static int poll_disp_fds(unsigned int n_fd) |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 378 | { |
| 379 | struct osmo_fd *ufd; |
| 380 | unsigned int i; |
| 381 | int work = 0; |
Neels Hofmeyr | ac49bda | 2021-06-04 16:30:04 +0200 | [diff] [blame] | 382 | int shutdown_pending_writes = 0; |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 383 | |
| 384 | for (i = 0; i < n_fd; i++) { |
| 385 | struct pollfd *p = &g_poll.poll[i]; |
| 386 | int flags = 0; |
| 387 | |
| 388 | if (!p->revents) |
| 389 | continue; |
| 390 | |
| 391 | ufd = osmo_fd_get_by_fd(p->fd); |
| 392 | if (!ufd) { |
| 393 | /* FD might have been unregistered meanwhile */ |
| 394 | continue; |
| 395 | } |
| 396 | /* use the same mapping as the Linux kernel does in fs/select.c */ |
| 397 | if (p->revents & (POLLIN | POLLHUP | POLLERR)) |
| 398 | flags |= OSMO_FD_READ; |
| 399 | if (p->revents & (POLLOUT | POLLERR)) |
| 400 | flags |= OSMO_FD_WRITE; |
| 401 | if (p->revents & POLLPRI) |
| 402 | flags |= OSMO_FD_EXCEPT; |
| 403 | |
| 404 | /* make sure we never report more than the user requested */ |
| 405 | flags &= ufd->when; |
| 406 | |
Neels Hofmeyr | ac49bda | 2021-06-04 16:30:04 +0200 | [diff] [blame] | 407 | if (_osmo_select_shutdown_requested > 0) { |
| 408 | if (ufd->when & OSMO_FD_WRITE) |
| 409 | shutdown_pending_writes++; |
| 410 | } |
| 411 | |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 412 | if (flags) { |
| 413 | work = 1; |
| 414 | /* make sure to clear any log context before processing the next incoming message |
| 415 | * as part of some file descriptor callback. This effectively prevents "context |
| 416 | * leaking" from processing of one message into processing of the next message as part |
| 417 | * of one iteration through the list of file descriptors here. See OS#3813 */ |
| 418 | log_reset_context(); |
| 419 | ufd->cb(ufd, flags); |
| 420 | } |
| 421 | } |
| 422 | |
Neels Hofmeyr | ac49bda | 2021-06-04 16:30:04 +0200 | [diff] [blame] | 423 | if (_osmo_select_shutdown_requested > 0 && !shutdown_pending_writes) |
| 424 | _osmo_select_shutdown_done = true; |
| 425 | |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 426 | return work; |
| 427 | } |
| 428 | |
| 429 | static int _osmo_select_main(int polling) |
| 430 | { |
| 431 | unsigned int n_poll; |
| 432 | int rc; |
Oliver Smith | d841bec | 2021-12-21 19:17:51 +0100 | [diff] [blame] | 433 | int timeout = 0; |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 434 | |
| 435 | /* prepare read and write fdsets */ |
| 436 | n_poll = poll_fill_fds(); |
| 437 | |
Oliver Smith | d841bec | 2021-12-21 19:17:51 +0100 | [diff] [blame] | 438 | if (!polling) { |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 439 | osmo_timers_prepare(); |
Oliver Smith | d841bec | 2021-12-21 19:17:51 +0100 | [diff] [blame] | 440 | timeout = osmo_timers_nearest_ms(); |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 441 | |
Oliver Smith | d841bec | 2021-12-21 19:17:51 +0100 | [diff] [blame] | 442 | if (_osmo_select_shutdown_requested && timeout == -1) |
| 443 | timeout = 0; |
| 444 | } |
| 445 | |
| 446 | rc = poll(g_poll.poll, n_poll, timeout); |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 447 | if (rc < 0) |
| 448 | return 0; |
| 449 | |
| 450 | /* fire timers */ |
Neels Hofmeyr | ac49bda | 2021-06-04 16:30:04 +0200 | [diff] [blame] | 451 | if (!_osmo_select_shutdown_requested) |
| 452 | osmo_timers_update(); |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 453 | |
| 454 | OSMO_ASSERT(osmo_ctx->select); |
| 455 | |
| 456 | /* call registered callback functions */ |
| 457 | return poll_disp_fds(n_poll); |
| 458 | } |
| 459 | #else /* FORCE_IO_SELECT */ |
| 460 | /* the old implementation based on select, used 2008-2020 */ |
Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 461 | static int _osmo_select_main(int polling) |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 462 | { |
| 463 | fd_set readset, writeset, exceptset; |
| 464 | int rc; |
| 465 | struct timeval no_time = {0, 0}; |
| 466 | |
| 467 | FD_ZERO(&readset); |
| 468 | FD_ZERO(&writeset); |
| 469 | FD_ZERO(&exceptset); |
| 470 | |
| 471 | /* prepare read and write fdsets */ |
| 472 | osmo_fd_fill_fds(&readset, &writeset, &exceptset); |
| 473 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 474 | if (!polling) |
| 475 | osmo_timers_prepare(); |
| 476 | rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : osmo_timers_nearest()); |
| 477 | if (rc < 0) |
| 478 | return 0; |
| 479 | |
| 480 | /* fire timers */ |
| 481 | osmo_timers_update(); |
| 482 | |
Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 483 | OSMO_ASSERT(osmo_ctx->select); |
| 484 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 485 | /* call registered callback functions */ |
| 486 | return osmo_fd_disp_fds(&readset, &writeset, &exceptset); |
| 487 | } |
Harald Welte | b904e42 | 2020-10-18 21:24:13 +0200 | [diff] [blame] | 488 | #endif /* FORCE_IO_SELECT */ |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 489 | |
Harald Welte | 2d90611 | 2019-03-18 17:17:43 +0100 | [diff] [blame] | 490 | /*! select main loop integration |
| 491 | * \param[in] polling should we pollonly (1) or block on select (0) |
| 492 | * \returns 0 if no fd handled; 1 if fd handled; negative in case of error |
| 493 | */ |
| 494 | int osmo_select_main(int polling) |
| 495 | { |
| 496 | int rc = _osmo_select_main(polling); |
| 497 | #ifndef EMBEDDED |
| 498 | if (talloc_total_size(osmo_ctx->select) != 0) { |
| 499 | osmo_panic("You cannot use the 'select' volatile " |
| 500 | "context if you don't use osmo_select_main_ctx()!\n"); |
| 501 | } |
| 502 | #endif |
| 503 | return rc; |
| 504 | } |
| 505 | |
| 506 | #ifndef EMBEDDED |
| 507 | /*! select main loop integration with temporary select-dispatch talloc context |
| 508 | * \param[in] polling should we pollonly (1) or block on select (0) |
| 509 | * \returns 0 if no fd handled; 1 if fd handled; negative in case of error |
| 510 | */ |
| 511 | int osmo_select_main_ctx(int polling) |
| 512 | { |
| 513 | int rc = _osmo_select_main(polling); |
| 514 | /* free all the children of the volatile 'select' scope context */ |
| 515 | talloc_free_children(osmo_ctx->select); |
| 516 | return rc; |
| 517 | } |
| 518 | #endif |
| 519 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 520 | /*! find an osmo_fd based on the integer fd |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 521 | * \param[in] fd file descriptor to use as search key |
| 522 | * \returns \ref osmo_fd for \ref fd; NULL in case it doesn't exist */ |
Harald Welte | 6c33ae2 | 2016-03-19 21:17:58 +0100 | [diff] [blame] | 523 | struct osmo_fd *osmo_fd_get_by_fd(int fd) |
| 524 | { |
arehbein | cd23806 | 2023-07-13 18:49:47 +0200 | [diff] [blame] | 525 | if (fd > maxfd || fd < 0) |
Pau Espin Pedrol | c46a15d | 2023-03-09 17:37:15 +0100 | [diff] [blame] | 526 | return NULL; |
| 527 | return osmo_fd_lookup.table[fd]; |
Harald Welte | 6c33ae2 | 2016-03-19 21:17:58 +0100 | [diff] [blame] | 528 | } |
| 529 | |
Harald Welte | 7a010b1 | 2019-04-06 13:46:40 +0200 | [diff] [blame] | 530 | /*! initialize the osmocom select abstraction for the current thread */ |
| 531 | void osmo_select_init(void) |
| 532 | { |
| 533 | INIT_LLIST_HEAD(&osmo_fds); |
Pau Espin Pedrol | c46a15d | 2023-03-09 17:37:15 +0100 | [diff] [blame] | 534 | osmo_fd_lookup_table_extend(0); |
Harald Welte | 7a010b1 | 2019-04-06 13:46:40 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Oliver Smith | b46cfba | 2023-03-14 15:05:03 +0100 | [diff] [blame] | 537 | /* ensure main thread always has pre-initialized osmo_fds |
| 538 | * priority 102: must run after on_dso_load_ctx */ |
| 539 | static __attribute__((constructor(102))) void on_dso_load_select(void) |
Harald Welte | 7a010b1 | 2019-04-06 13:46:40 +0200 | [diff] [blame] | 540 | { |
| 541 | osmo_select_init(); |
| 542 | } |
| 543 | |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 544 | #ifdef HAVE_SYS_TIMERFD_H |
| 545 | #include <sys/timerfd.h> |
| 546 | |
| 547 | /*! disable the osmocom-wrapped timerfd */ |
| 548 | int osmo_timerfd_disable(struct osmo_fd *ofd) |
| 549 | { |
| 550 | const struct itimerspec its_null = { |
| 551 | .it_value = { 0, 0 }, |
| 552 | .it_interval = { 0, 0 }, |
| 553 | }; |
| 554 | return timerfd_settime(ofd->fd, 0, &its_null, NULL); |
| 555 | } |
| 556 | |
Alexander Chemeris | e8ec214 | 2020-05-09 01:57:16 +0300 | [diff] [blame] | 557 | /*! schedule the osmocom-wrapped timerfd to occur first at \a first, then periodically at \a interval |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 558 | * \param[in] ofd Osmocom wrapped timerfd |
| 559 | * \param[in] first Relative time at which the timer should first execute (NULL = \a interval) |
| 560 | * \param[in] interval Time interval at which subsequent timer shall fire |
| 561 | * \returns 0 on success; negative on error */ |
| 562 | int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first, |
| 563 | const struct timespec *interval) |
| 564 | { |
| 565 | struct itimerspec its; |
| 566 | |
| 567 | if (ofd->fd < 0) |
| 568 | return -EINVAL; |
| 569 | |
| 570 | /* first expiration */ |
| 571 | if (first) |
| 572 | its.it_value = *first; |
| 573 | else |
| 574 | its.it_value = *interval; |
| 575 | /* repeating interval */ |
| 576 | its.it_interval = *interval; |
| 577 | |
| 578 | return timerfd_settime(ofd->fd, 0, &its, NULL); |
| 579 | } |
| 580 | |
| 581 | /*! setup osmocom-wrapped timerfd |
| 582 | * \param[inout] ofd Osmocom-wrapped timerfd on which to operate |
| 583 | * \param[in] cb Call-back function called when timerfd becomes readable |
| 584 | * \param[in] data Opaque data to be passed on to call-back |
| 585 | * \returns 0 on success; negative on error |
| 586 | * |
| 587 | * We simply initialize the data structures here, but do not yet |
| 588 | * schedule the timer. |
| 589 | */ |
| 590 | int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data) |
| 591 | { |
| 592 | ofd->cb = cb; |
| 593 | ofd->data = data; |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 594 | ofd->when = OSMO_FD_READ; |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 595 | |
| 596 | if (ofd->fd < 0) { |
Harald Welte | 37608f9 | 2018-10-21 12:32:55 +0200 | [diff] [blame] | 597 | int rc; |
| 598 | |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 599 | ofd->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); |
| 600 | if (ofd->fd < 0) |
| 601 | return ofd->fd; |
| 602 | |
Harald Welte | 37608f9 | 2018-10-21 12:32:55 +0200 | [diff] [blame] | 603 | rc = osmo_fd_register(ofd); |
| 604 | if (rc < 0) { |
Harald Welte | 100e44e | 2020-04-18 21:16:43 +0200 | [diff] [blame] | 605 | osmo_fd_unregister(ofd); |
Harald Welte | 37608f9 | 2018-10-21 12:32:55 +0200 | [diff] [blame] | 606 | close(ofd->fd); |
| 607 | ofd->fd = -1; |
| 608 | return rc; |
| 609 | } |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 610 | } |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | #endif /* HAVE_SYS_TIMERFD_H */ |
| 615 | |
Harald Welte | a70ac85 | 2020-04-17 19:20:01 +0200 | [diff] [blame] | 616 | #ifdef HAVE_SYS_SIGNALFD_H |
| 617 | #include <sys/signalfd.h> |
| 618 | |
| 619 | static int signalfd_callback(struct osmo_fd *ofd, unsigned int what) |
| 620 | { |
| 621 | struct osmo_signalfd *osfd = ofd->data; |
| 622 | struct signalfd_siginfo fdsi; |
| 623 | int rc; |
| 624 | |
| 625 | rc = read(ofd->fd, &fdsi, sizeof(fdsi)); |
| 626 | if (rc < 0) { |
| 627 | osmo_fd_unregister(ofd); |
| 628 | close(ofd->fd); |
| 629 | ofd->fd = -1; |
| 630 | return rc; |
| 631 | } |
| 632 | |
| 633 | osfd->cb(osfd, &fdsi); |
| 634 | |
| 635 | return 0; |
| 636 | }; |
| 637 | |
| 638 | /*! create a signalfd and register it with osmocom select loop. |
| 639 | * \param[in] ctx talloc context from which osmo_signalfd is to be allocated |
| 640 | * \param[in] set of signals to be accept via this file descriptor |
| 641 | * \param[in] cb call-back function to be called for each arriving signal |
| 642 | * \param[in] data opaque user-provided data to pass to callback |
| 643 | * \returns pointer to newly-allocated + registered osmo_signalfd; NULL on error */ |
| 644 | struct osmo_signalfd * |
| 645 | osmo_signalfd_setup(void *ctx, sigset_t set, osmo_signalfd_cb *cb, void *data) |
| 646 | { |
| 647 | struct osmo_signalfd *osfd = talloc_size(ctx, sizeof(*osfd)); |
| 648 | int fd, rc; |
| 649 | |
| 650 | if (!osfd) |
| 651 | return NULL; |
| 652 | |
| 653 | osfd->data = data; |
| 654 | osfd->sigset = set; |
| 655 | osfd->cb = cb; |
| 656 | |
| 657 | fd = signalfd(-1, &osfd->sigset, SFD_NONBLOCK); |
| 658 | if (fd < 0) { |
| 659 | talloc_free(osfd); |
| 660 | return NULL; |
| 661 | } |
| 662 | |
| 663 | osmo_fd_setup(&osfd->ofd, fd, OSMO_FD_READ, signalfd_callback, osfd, 0); |
| 664 | rc = osmo_fd_register(&osfd->ofd); |
| 665 | if (rc < 0) { |
| 666 | close(fd); |
| 667 | talloc_free(osfd); |
| 668 | return NULL; |
| 669 | } |
| 670 | |
| 671 | return osfd; |
| 672 | } |
| 673 | |
| 674 | #endif /* HAVE_SYS_SIGNALFD_H */ |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 675 | |
Neels Hofmeyr | ac49bda | 2021-06-04 16:30:04 +0200 | [diff] [blame] | 676 | /*! Request osmo_select_* to only service pending OSMO_FD_WRITE requests. Once all writes are done, |
| 677 | * osmo_select_shutdown_done() returns true. This allows for example to send all outbound packets before terminating the |
| 678 | * process. |
| 679 | * |
| 680 | * Usage example: |
| 681 | * |
| 682 | * static void signal_handler(int signum) |
| 683 | * { |
| 684 | * fprintf(stdout, "signal %u received\n", signum); |
| 685 | * |
| 686 | * switch (signum) { |
| 687 | * case SIGINT: |
| 688 | * case SIGTERM: |
| 689 | * // If the user hits Ctrl-C the third time, just terminate immediately. |
| 690 | * if (osmo_select_shutdown_requested() >= 2) |
| 691 | * exit(-1); |
| 692 | * // Request write-only mode in osmo_select_main_ctx() |
| 693 | * osmo_select_shutdown_request(); |
| 694 | * break; |
| 695 | * [...] |
| 696 | * } |
| 697 | * |
| 698 | * main() |
| 699 | * { |
| 700 | * signal(SIGINT, &signal_handler); |
| 701 | * signal(SIGTERM, &signal_handler); |
| 702 | * |
| 703 | * [...] |
| 704 | * |
| 705 | * // After the signal_handler issued osmo_select_shutdown_request(), osmo_select_shutdown_done() returns true |
| 706 | * // as soon as all write queues are empty. |
| 707 | * while (!osmo_select_shutdown_done()) { |
| 708 | * osmo_select_main_ctx(0); |
| 709 | * } |
| 710 | * } |
| 711 | */ |
Harald Welte | e61d459 | 2022-11-03 11:05:58 +0100 | [diff] [blame] | 712 | void osmo_select_shutdown_request(void) |
Neels Hofmeyr | ac49bda | 2021-06-04 16:30:04 +0200 | [diff] [blame] | 713 | { |
| 714 | _osmo_select_shutdown_requested++; |
| 715 | }; |
| 716 | |
| 717 | /*! Return the number of times osmo_select_shutdown_request() was called before. */ |
Harald Welte | e61d459 | 2022-11-03 11:05:58 +0100 | [diff] [blame] | 718 | int osmo_select_shutdown_requested(void) |
Neels Hofmeyr | ac49bda | 2021-06-04 16:30:04 +0200 | [diff] [blame] | 719 | { |
| 720 | return _osmo_select_shutdown_requested; |
| 721 | }; |
| 722 | |
| 723 | /*! Return true after osmo_select_shutdown_requested() was called, and after an osmo_select poll loop found no more |
| 724 | * pending OSMO_FD_WRITE on any registered socket. */ |
Harald Welte | e61d459 | 2022-11-03 11:05:58 +0100 | [diff] [blame] | 725 | bool osmo_select_shutdown_done(void) { |
Neels Hofmeyr | ac49bda | 2021-06-04 16:30:04 +0200 | [diff] [blame] | 726 | return _osmo_select_shutdown_done; |
| 727 | }; |
| 728 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 729 | /*! @} */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 730 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 731 | #endif /* _HAVE_SYS_SELECT_H */ |