blob: 026d4572ce43a0ac5a33ba903af69ab7ffa32079 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file select.c
2 * select filedescriptor handling.
3 * Taken from:
Harald Welteec8b4502010-02-20 20:34:29 +01004 * userspace logging daemon for the iptables ULOG target
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02005 * of the linux 2.4 netfilter subsystem. */
6/*
Harald Welteb904e422020-10-18 21:24:13 +02007 * (C) 2000-2020 by Harald Welte <laforge@gnumonks.org>
Harald Welte6dda35a2022-11-09 16:54:50 +01008 * All Rights Reserved.
Harald Weltee08da972017-11-13 01:00:26 +09009 *
10 * SPDX-License-Identifier: GPL-2.0+
Harald Welteec8b4502010-02-20 20:34:29 +010011 *
12 * This program is free software; you can redistribute it and/or modify
Harald Welte9d92f0e2010-10-31 13:56:45 +010013 * 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 Welteec8b4502010-02-20 20:34:29 +010016 *
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 Welteec8b4502010-02-20 20:34:29 +010021 */
22
23#include <fcntl.h>
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +080024#include <stdio.h>
Harald Welteea91a512017-07-13 14:28:30 +020025#include <unistd.h>
Harald Welte9e166e82014-03-10 17:28:33 +010026#include <string.h>
Philipp Maierb2888532016-12-09 14:07:18 +010027#include <stdbool.h>
Harald Welteea4d8932017-12-03 16:13:39 +010028#include <errno.h>
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +080029
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010030#include <osmocom/core/select.h>
31#include <osmocom/core/linuxlist.h>
32#include <osmocom/core/timer.h>
Harald Welte52a83752019-02-21 16:37:10 +010033#include <osmocom/core/logging.h>
Harald Welte2d906112019-03-18 17:17:43 +010034#include <osmocom/core/talloc.h>
35#include <osmocom/core/utils.h>
Philipp Maierb1ef8f52021-12-06 16:31:02 +010036#include <osmocom/core/stat_item.h>
37#include <osmocom/core/stats_tcp.h>
Harald Welteec8b4502010-02-20 20:34:29 +010038
Harald Welte54844802010-02-20 22:23:08 +010039#include "../config.h"
40
Harald Welteb904e422020-10-18 21:24:13 +020041#if defined(HAVE_SYS_SELECT_H) && defined(HAVE_POLL_H)
Harald Welte1d604d82017-05-17 15:32:35 +010042#include <sys/select.h>
Harald Welteb904e422020-10-18 21:24:13 +020043#include <poll.h>
Harald Welteec8b4502010-02-20 20:34:29 +010044
Harald Welteba6988b2011-08-17 12:46:48 +020045/*! \addtogroup select
46 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020047 * select() loop abstraction
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020048 *
49 * \file select.c */
Harald Welteba6988b2011-08-17 12:46:48 +020050
Harald Welte7a010b12019-04-06 13:46:40 +020051/* 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 */
53static __thread int maxfd = 0;
54static __thread struct llist_head osmo_fds; /* TLS cannot use LLIST_HEAD() */
55static __thread int unregistered_count;
Harald Welteec8b4502010-02-20 20:34:29 +010056
Harald Welteb904e422020-10-18 21:24:13 +020057#ifndef FORCE_IO_SELECT
58struct poll_state {
59 /* array of pollfd */
60 struct pollfd *poll;
61 /* number of entries in pollfd allocated */
62 unsigned int poll_size;
63 /* number of osmo_fd registered */
64 unsigned int num_registered;
65};
66static __thread struct poll_state g_poll;
67#endif /* FORCE_IO_SELECT */
68
Neels Hofmeyrac49bda2021-06-04 16:30:04 +020069/*! See osmo_select_shutdown_request() */
70static int _osmo_select_shutdown_requested = 0;
71/*! See osmo_select_shutdown_request() */
72static bool _osmo_select_shutdown_done = false;
73
Harald Welte6c0a0e62017-08-12 11:43:14 +020074/*! Set up an osmo-fd. Will not register it.
75 * \param[inout] ofd Osmo FD to be set-up
76 * \param[in] fd OS-level file descriptor number
Harald Welte16886992019-03-20 10:26:39 +010077 * \param[in] when bit-mask of OSMO_FD_{READ,WRITE,EXECEPT}
Harald Welte6c0a0e62017-08-12 11:43:14 +020078 * \param[in] cb Call-back function to be called
79 * \param[in] data Private context pointer
80 * \param[in] priv_nr Private number
81 */
82void osmo_fd_setup(struct osmo_fd *ofd, int fd, unsigned int when,
83 int (*cb)(struct osmo_fd *fd, unsigned int what),
84 void *data, unsigned int priv_nr)
85{
86 ofd->fd = fd;
87 ofd->when = when;
88 ofd->cb = cb;
89 ofd->data = data;
90 ofd->priv_nr = priv_nr;
91}
Philipp Maierb2888532016-12-09 14:07:18 +010092
Harald Welte7e657912020-10-18 22:07:21 +020093/*! Update the 'when' field of osmo_fd. "ofd->when = (ofd->when & when_mask) | when".
94 * Use this function instead of directly modifying ofd->when, as the latter will be
95 * removed soon. */
96void osmo_fd_update_when(struct osmo_fd *ofd, unsigned int when_mask, unsigned int when)
97{
98 ofd->when &= when_mask;
99 ofd->when |= when;
100}
101
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200102/*! Check if a file descriptor is already registered
Philipp Maierb2888532016-12-09 14:07:18 +0100103 * \param[in] fd osmocom file descriptor to be checked
104 * \returns true if registered; otherwise false
105 */
106bool osmo_fd_is_registered(struct osmo_fd *fd)
107{
108 struct osmo_fd *entry;
109 llist_for_each_entry(entry, &osmo_fds, list) {
110 if (entry == fd) {
111 return true;
112 }
113 }
114
115 return false;
116}
117
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200118/*! Register a new file descriptor with select loop abstraction
Harald Welteba6988b2011-08-17 12:46:48 +0200119 * \param[in] fd osmocom file descriptor to be registered
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200120 * \returns 0 on success; negative in case of error
Harald Welteba6988b2011-08-17 12:46:48 +0200121 */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200122int osmo_fd_register(struct osmo_fd *fd)
Harald Welteec8b4502010-02-20 20:34:29 +0100123{
124 int flags;
125
126 /* make FD nonblocking */
127 flags = fcntl(fd->fd, F_GETFL);
128 if (flags < 0)
129 return flags;
130 flags |= O_NONBLOCK;
131 flags = fcntl(fd->fd, F_SETFL, flags);
132 if (flags < 0)
133 return flags;
134
Harald Welte32e1f232011-06-26 13:07:18 +0200135 /* set close-on-exec flag */
136 flags = fcntl(fd->fd, F_GETFD);
137 if (flags < 0)
138 return flags;
139 flags |= FD_CLOEXEC;
140 flags = fcntl(fd->fd, F_SETFD, flags);
141 if (flags < 0)
142 return flags;
143
Harald Welteec8b4502010-02-20 20:34:29 +0100144 /* Register FD */
145 if (fd->fd > maxfd)
146 maxfd = fd->fd;
147
Pau Espin Pedrold05def02020-05-09 19:24:21 +0200148#ifdef OSMO_FD_CHECK
Philipp Maierb2888532016-12-09 14:07:18 +0100149 if (osmo_fd_is_registered(fd)) {
150 fprintf(stderr, "Adding a osmo_fd that is already in the list.\n");
151 return 0;
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +0800152 }
153#endif
Harald Welteb904e422020-10-18 21:24:13 +0200154#ifndef FORCE_IO_SELECT
155 if (g_poll.num_registered + 1 > g_poll.poll_size) {
156 struct pollfd *p;
157 unsigned int new_size = g_poll.poll_size ? g_poll.poll_size * 2 : 1024;
158 p = talloc_realloc(OTC_GLOBAL, g_poll.poll, struct pollfd, new_size);
159 if (!p)
160 return -ENOMEM;
161 memset(p + g_poll.poll_size, 0, new_size - g_poll.poll_size);
162 g_poll.poll = p;
163 g_poll.poll_size = new_size;
164 }
165 g_poll.num_registered++;
166#endif /* FORCE_IO_SELECT */
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +0800167
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200168 llist_add_tail(&fd->list, &osmo_fds);
Harald Welteec8b4502010-02-20 20:34:29 +0100169
170 return 0;
171}
172
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200173/*! Unregister a file descriptor from select loop abstraction
Harald Welteba6988b2011-08-17 12:46:48 +0200174 * \param[in] fd osmocom file descriptor to be unregistered
175 */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200176void osmo_fd_unregister(struct osmo_fd *fd)
Harald Welteec8b4502010-02-20 20:34:29 +0100177{
Philipp Maierb2888532016-12-09 14:07:18 +0100178 /* Note: when fd is inside the osmo_fds list (not registered before)
179 * this function will crash! If in doubt, check file descriptor with
180 * osmo_fd_is_registered() */
Harald Welteec8b4502010-02-20 20:34:29 +0100181 unregistered_count++;
182 llist_del(&fd->list);
Harald Welteb904e422020-10-18 21:24:13 +0200183#ifndef FORCE_IO_SELECT
184 g_poll.num_registered--;
185#endif /* FORCE_IO_SELECT */
Philipp Maierb1ef8f52021-12-06 16:31:02 +0100186
187 /* If existent, free any statistical data */
188 osmo_stats_tcp_osmo_fd_unregister(fd);
Harald Welteec8b4502010-02-20 20:34:29 +0100189}
190
Harald Welteea91a512017-07-13 14:28:30 +0200191/*! Close a file descriptor, mark it as closed + unregister from select loop abstraction
192 * \param[in] fd osmocom file descriptor to be unregistered + closed
193 *
194 * If \a fd is registered, we unregister it from the select() loop
195 * abstraction. We then close the fd and set it to -1, as well as
196 * unsetting any 'when' flags */
197void osmo_fd_close(struct osmo_fd *fd)
198{
199 if (osmo_fd_is_registered(fd))
200 osmo_fd_unregister(fd);
201 if (fd->fd != -1)
202 close(fd->fd);
203 fd->fd = -1;
204 fd->when = 0;
205}
206
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200207/*! Populate the fd_sets and return the highest fd number
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100208 * \param[in] _rset The readfds to populate
209 * \param[in] _wset The wrtiefds to populate
210 * \param[in] _eset The errorfds to populate
211 *
212 * \returns The highest file descriptor seen or 0 on an empty list
213 */
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100214inline int osmo_fd_fill_fds(void *_rset, void *_wset, void *_eset)
Harald Welteec8b4502010-02-20 20:34:29 +0100215{
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100216 fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset;
217 struct osmo_fd *ufd;
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100218 int highfd = 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100219
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200220 llist_for_each_entry(ufd, &osmo_fds, list) {
Harald Welte16886992019-03-20 10:26:39 +0100221 if (ufd->when & OSMO_FD_READ)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100222 FD_SET(ufd->fd, readset);
Harald Welteec8b4502010-02-20 20:34:29 +0100223
Harald Welte16886992019-03-20 10:26:39 +0100224 if (ufd->when & OSMO_FD_WRITE)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100225 FD_SET(ufd->fd, writeset);
Harald Welteec8b4502010-02-20 20:34:29 +0100226
Harald Welte16886992019-03-20 10:26:39 +0100227 if (ufd->when & OSMO_FD_EXCEPT)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100228 FD_SET(ufd->fd, exceptset);
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100229
230 if (ufd->fd > highfd)
231 highfd = ufd->fd;
Harald Welteec8b4502010-02-20 20:34:29 +0100232 }
233
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100234 return highfd;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100235}
Harald Welteec8b4502010-02-20 20:34:29 +0100236
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100237inline int osmo_fd_disp_fds(void *_rset, void *_wset, void *_eset)
238{
239 struct osmo_fd *ufd, *tmp;
240 int work = 0;
241 fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset;
Harald Welteec8b4502010-02-20 20:34:29 +0100242
Harald Welteec8b4502010-02-20 20:34:29 +0100243restart:
244 unregistered_count = 0;
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200245 llist_for_each_entry_safe(ufd, tmp, &osmo_fds, list) {
Harald Welteec8b4502010-02-20 20:34:29 +0100246 int flags = 0;
247
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100248 if (FD_ISSET(ufd->fd, readset)) {
Harald Welte16886992019-03-20 10:26:39 +0100249 flags |= OSMO_FD_READ;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100250 FD_CLR(ufd->fd, readset);
Harald Welteec8b4502010-02-20 20:34:29 +0100251 }
252
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100253 if (FD_ISSET(ufd->fd, writeset)) {
Harald Welte16886992019-03-20 10:26:39 +0100254 flags |= OSMO_FD_WRITE;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100255 FD_CLR(ufd->fd, writeset);
Harald Welteec8b4502010-02-20 20:34:29 +0100256 }
257
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100258 if (FD_ISSET(ufd->fd, exceptset)) {
Harald Welte16886992019-03-20 10:26:39 +0100259 flags |= OSMO_FD_EXCEPT;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100260 FD_CLR(ufd->fd, exceptset);
Harald Welteec8b4502010-02-20 20:34:29 +0100261 }
262
263 if (flags) {
264 work = 1;
Harald Welte52a83752019-02-21 16:37:10 +0100265 /* make sure to clear any log context before processing the next incoming message
266 * as part of some file descriptor callback. This effectively prevents "context
267 * leaking" from processing of one message into processing of the next message as part
268 * of one iteration through the list of file descriptors here. See OS#3813 */
269 log_reset_context();
Harald Welteec8b4502010-02-20 20:34:29 +0100270 ufd->cb(ufd, flags);
271 }
Holger Hans Peter Freyther92e1e702014-04-09 17:24:00 +0200272 /* ugly, ugly hack. If more than one filedescriptor was
Harald Welteec8b4502010-02-20 20:34:29 +0100273 * unregistered, they might have been consecutive and
274 * llist_for_each_entry_safe() is no longer safe */
Holger Hans Peter Freyther23ba4742010-04-11 17:33:19 +0200275 /* this seems to happen with the last element of the list as well */
276 if (unregistered_count >= 1)
Harald Welteec8b4502010-02-20 20:34:29 +0100277 goto restart;
278 }
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100279
Harald Welteec8b4502010-02-20 20:34:29 +0100280 return work;
281}
282
Harald Welteb904e422020-10-18 21:24:13 +0200283
284#ifndef FORCE_IO_SELECT
285/* fill g_poll.poll and return the number of entries filled */
286static unsigned int poll_fill_fds(void)
287{
288 struct osmo_fd *ufd;
289 unsigned int i = 0;
290
291 llist_for_each_entry(ufd, &osmo_fds, list) {
292 struct pollfd *p;
293
294 if (!ufd->when)
295 continue;
296
297 p = &g_poll.poll[i++];
298
299 p->fd = ufd->fd;
300 p->events = 0;
301 p->revents = 0;
302
303 /* use the same mapping as the Linux kernel does in fs/select.c */
304 if (ufd->when & OSMO_FD_READ)
305 p->events |= POLLIN | POLLHUP | POLLERR;
306
307 if (ufd->when & OSMO_FD_WRITE)
308 p->events |= POLLOUT | POLLERR;
309
310 if (ufd->when & OSMO_FD_EXCEPT)
311 p->events |= POLLPRI;
312
313 }
314
315 return i;
316}
317
318/* iterate over first n_fd entries of g_poll.poll + dispatch */
Harald Welte0b08d512022-01-09 12:03:12 +0100319static int poll_disp_fds(unsigned int n_fd)
Harald Welteb904e422020-10-18 21:24:13 +0200320{
321 struct osmo_fd *ufd;
322 unsigned int i;
323 int work = 0;
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200324 int shutdown_pending_writes = 0;
Harald Welteb904e422020-10-18 21:24:13 +0200325
326 for (i = 0; i < n_fd; i++) {
327 struct pollfd *p = &g_poll.poll[i];
328 int flags = 0;
329
330 if (!p->revents)
331 continue;
332
333 ufd = osmo_fd_get_by_fd(p->fd);
334 if (!ufd) {
335 /* FD might have been unregistered meanwhile */
336 continue;
337 }
338 /* use the same mapping as the Linux kernel does in fs/select.c */
339 if (p->revents & (POLLIN | POLLHUP | POLLERR))
340 flags |= OSMO_FD_READ;
341 if (p->revents & (POLLOUT | POLLERR))
342 flags |= OSMO_FD_WRITE;
343 if (p->revents & POLLPRI)
344 flags |= OSMO_FD_EXCEPT;
345
346 /* make sure we never report more than the user requested */
347 flags &= ufd->when;
348
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200349 if (_osmo_select_shutdown_requested > 0) {
350 if (ufd->when & OSMO_FD_WRITE)
351 shutdown_pending_writes++;
352 }
353
Harald Welteb904e422020-10-18 21:24:13 +0200354 if (flags) {
355 work = 1;
356 /* make sure to clear any log context before processing the next incoming message
357 * as part of some file descriptor callback. This effectively prevents "context
358 * leaking" from processing of one message into processing of the next message as part
359 * of one iteration through the list of file descriptors here. See OS#3813 */
360 log_reset_context();
361 ufd->cb(ufd, flags);
362 }
363 }
364
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200365 if (_osmo_select_shutdown_requested > 0 && !shutdown_pending_writes)
366 _osmo_select_shutdown_done = true;
367
Harald Welteb904e422020-10-18 21:24:13 +0200368 return work;
369}
370
371static int _osmo_select_main(int polling)
372{
373 unsigned int n_poll;
374 int rc;
Oliver Smithd841bec2021-12-21 19:17:51 +0100375 int timeout = 0;
Harald Welteb904e422020-10-18 21:24:13 +0200376
377 /* prepare read and write fdsets */
378 n_poll = poll_fill_fds();
379
Oliver Smithd841bec2021-12-21 19:17:51 +0100380 if (!polling) {
Harald Welteb904e422020-10-18 21:24:13 +0200381 osmo_timers_prepare();
Oliver Smithd841bec2021-12-21 19:17:51 +0100382 timeout = osmo_timers_nearest_ms();
Harald Welteb904e422020-10-18 21:24:13 +0200383
Oliver Smithd841bec2021-12-21 19:17:51 +0100384 if (_osmo_select_shutdown_requested && timeout == -1)
385 timeout = 0;
386 }
387
388 rc = poll(g_poll.poll, n_poll, timeout);
Harald Welteb904e422020-10-18 21:24:13 +0200389 if (rc < 0)
390 return 0;
391
392 /* fire timers */
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200393 if (!_osmo_select_shutdown_requested)
394 osmo_timers_update();
Harald Welteb904e422020-10-18 21:24:13 +0200395
396 OSMO_ASSERT(osmo_ctx->select);
397
398 /* call registered callback functions */
399 return poll_disp_fds(n_poll);
400}
401#else /* FORCE_IO_SELECT */
402/* the old implementation based on select, used 2008-2020 */
Harald Welte2d906112019-03-18 17:17:43 +0100403static int _osmo_select_main(int polling)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100404{
405 fd_set readset, writeset, exceptset;
406 int rc;
407 struct timeval no_time = {0, 0};
408
409 FD_ZERO(&readset);
410 FD_ZERO(&writeset);
411 FD_ZERO(&exceptset);
412
413 /* prepare read and write fdsets */
414 osmo_fd_fill_fds(&readset, &writeset, &exceptset);
415
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100416 if (!polling)
417 osmo_timers_prepare();
418 rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : osmo_timers_nearest());
419 if (rc < 0)
420 return 0;
421
422 /* fire timers */
423 osmo_timers_update();
424
Harald Welte2d906112019-03-18 17:17:43 +0100425 OSMO_ASSERT(osmo_ctx->select);
426
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100427 /* call registered callback functions */
428 return osmo_fd_disp_fds(&readset, &writeset, &exceptset);
429}
Harald Welteb904e422020-10-18 21:24:13 +0200430#endif /* FORCE_IO_SELECT */
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100431
Harald Welte2d906112019-03-18 17:17:43 +0100432/*! select main loop integration
433 * \param[in] polling should we pollonly (1) or block on select (0)
434 * \returns 0 if no fd handled; 1 if fd handled; negative in case of error
435 */
436int osmo_select_main(int polling)
437{
438 int rc = _osmo_select_main(polling);
439#ifndef EMBEDDED
440 if (talloc_total_size(osmo_ctx->select) != 0) {
441 osmo_panic("You cannot use the 'select' volatile "
442 "context if you don't use osmo_select_main_ctx()!\n");
443 }
444#endif
445 return rc;
446}
447
448#ifndef EMBEDDED
449/*! select main loop integration with temporary select-dispatch talloc context
450 * \param[in] polling should we pollonly (1) or block on select (0)
451 * \returns 0 if no fd handled; 1 if fd handled; negative in case of error
452 */
453int osmo_select_main_ctx(int polling)
454{
455 int rc = _osmo_select_main(polling);
456 /* free all the children of the volatile 'select' scope context */
457 talloc_free_children(osmo_ctx->select);
458 return rc;
459}
460#endif
461
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200462/*! find an osmo_fd based on the integer fd
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200463 * \param[in] fd file descriptor to use as search key
464 * \returns \ref osmo_fd for \ref fd; NULL in case it doesn't exist */
Harald Welte6c33ae22016-03-19 21:17:58 +0100465struct osmo_fd *osmo_fd_get_by_fd(int fd)
466{
467 struct osmo_fd *ofd;
468
469 llist_for_each_entry(ofd, &osmo_fds, list) {
470 if (ofd->fd == fd)
471 return ofd;
472 }
473 return NULL;
474}
475
Harald Welte7a010b12019-04-06 13:46:40 +0200476/*! initialize the osmocom select abstraction for the current thread */
477void osmo_select_init(void)
478{
479 INIT_LLIST_HEAD(&osmo_fds);
480}
481
482/* ensure main thread always has pre-initialized osmo_fds */
483static __attribute__((constructor)) void on_dso_load_select(void)
484{
485 osmo_select_init();
486}
487
Harald Welteea4d8932017-12-03 16:13:39 +0100488#ifdef HAVE_SYS_TIMERFD_H
489#include <sys/timerfd.h>
490
491/*! disable the osmocom-wrapped timerfd */
492int osmo_timerfd_disable(struct osmo_fd *ofd)
493{
494 const struct itimerspec its_null = {
495 .it_value = { 0, 0 },
496 .it_interval = { 0, 0 },
497 };
498 return timerfd_settime(ofd->fd, 0, &its_null, NULL);
499}
500
Alexander Chemerise8ec2142020-05-09 01:57:16 +0300501/*! schedule the osmocom-wrapped timerfd to occur first at \a first, then periodically at \a interval
Harald Welteea4d8932017-12-03 16:13:39 +0100502 * \param[in] ofd Osmocom wrapped timerfd
503 * \param[in] first Relative time at which the timer should first execute (NULL = \a interval)
504 * \param[in] interval Time interval at which subsequent timer shall fire
505 * \returns 0 on success; negative on error */
506int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first,
507 const struct timespec *interval)
508{
509 struct itimerspec its;
510
511 if (ofd->fd < 0)
512 return -EINVAL;
513
514 /* first expiration */
515 if (first)
516 its.it_value = *first;
517 else
518 its.it_value = *interval;
519 /* repeating interval */
520 its.it_interval = *interval;
521
522 return timerfd_settime(ofd->fd, 0, &its, NULL);
523}
524
525/*! setup osmocom-wrapped timerfd
526 * \param[inout] ofd Osmocom-wrapped timerfd on which to operate
527 * \param[in] cb Call-back function called when timerfd becomes readable
528 * \param[in] data Opaque data to be passed on to call-back
529 * \returns 0 on success; negative on error
530 *
531 * We simply initialize the data structures here, but do not yet
532 * schedule the timer.
533 */
534int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data)
535{
536 ofd->cb = cb;
537 ofd->data = data;
Harald Welte16886992019-03-20 10:26:39 +0100538 ofd->when = OSMO_FD_READ;
Harald Welteea4d8932017-12-03 16:13:39 +0100539
540 if (ofd->fd < 0) {
Harald Welte37608f92018-10-21 12:32:55 +0200541 int rc;
542
Harald Welteea4d8932017-12-03 16:13:39 +0100543 ofd->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
544 if (ofd->fd < 0)
545 return ofd->fd;
546
Harald Welte37608f92018-10-21 12:32:55 +0200547 rc = osmo_fd_register(ofd);
548 if (rc < 0) {
Harald Welte100e44e2020-04-18 21:16:43 +0200549 osmo_fd_unregister(ofd);
Harald Welte37608f92018-10-21 12:32:55 +0200550 close(ofd->fd);
551 ofd->fd = -1;
552 return rc;
553 }
Harald Welteea4d8932017-12-03 16:13:39 +0100554 }
555 return 0;
556}
557
558#endif /* HAVE_SYS_TIMERFD_H */
559
Harald Weltea70ac852020-04-17 19:20:01 +0200560#ifdef HAVE_SYS_SIGNALFD_H
561#include <sys/signalfd.h>
562
563static int signalfd_callback(struct osmo_fd *ofd, unsigned int what)
564{
565 struct osmo_signalfd *osfd = ofd->data;
566 struct signalfd_siginfo fdsi;
567 int rc;
568
569 rc = read(ofd->fd, &fdsi, sizeof(fdsi));
570 if (rc < 0) {
571 osmo_fd_unregister(ofd);
572 close(ofd->fd);
573 ofd->fd = -1;
574 return rc;
575 }
576
577 osfd->cb(osfd, &fdsi);
578
579 return 0;
580};
581
582/*! create a signalfd and register it with osmocom select loop.
583 * \param[in] ctx talloc context from which osmo_signalfd is to be allocated
584 * \param[in] set of signals to be accept via this file descriptor
585 * \param[in] cb call-back function to be called for each arriving signal
586 * \param[in] data opaque user-provided data to pass to callback
587 * \returns pointer to newly-allocated + registered osmo_signalfd; NULL on error */
588struct osmo_signalfd *
589osmo_signalfd_setup(void *ctx, sigset_t set, osmo_signalfd_cb *cb, void *data)
590{
591 struct osmo_signalfd *osfd = talloc_size(ctx, sizeof(*osfd));
592 int fd, rc;
593
594 if (!osfd)
595 return NULL;
596
597 osfd->data = data;
598 osfd->sigset = set;
599 osfd->cb = cb;
600
601 fd = signalfd(-1, &osfd->sigset, SFD_NONBLOCK);
602 if (fd < 0) {
603 talloc_free(osfd);
604 return NULL;
605 }
606
607 osmo_fd_setup(&osfd->ofd, fd, OSMO_FD_READ, signalfd_callback, osfd, 0);
608 rc = osmo_fd_register(&osfd->ofd);
609 if (rc < 0) {
610 close(fd);
611 talloc_free(osfd);
612 return NULL;
613 }
614
615 return osfd;
616}
617
618#endif /* HAVE_SYS_SIGNALFD_H */
Harald Welteea4d8932017-12-03 16:13:39 +0100619
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200620/*! Request osmo_select_* to only service pending OSMO_FD_WRITE requests. Once all writes are done,
621 * osmo_select_shutdown_done() returns true. This allows for example to send all outbound packets before terminating the
622 * process.
623 *
624 * Usage example:
625 *
626 * static void signal_handler(int signum)
627 * {
628 * fprintf(stdout, "signal %u received\n", signum);
629 *
630 * switch (signum) {
631 * case SIGINT:
632 * case SIGTERM:
633 * // If the user hits Ctrl-C the third time, just terminate immediately.
634 * if (osmo_select_shutdown_requested() >= 2)
635 * exit(-1);
636 * // Request write-only mode in osmo_select_main_ctx()
637 * osmo_select_shutdown_request();
638 * break;
639 * [...]
640 * }
641 *
642 * main()
643 * {
644 * signal(SIGINT, &signal_handler);
645 * signal(SIGTERM, &signal_handler);
646 *
647 * [...]
648 *
649 * // After the signal_handler issued osmo_select_shutdown_request(), osmo_select_shutdown_done() returns true
650 * // as soon as all write queues are empty.
651 * while (!osmo_select_shutdown_done()) {
652 * osmo_select_main_ctx(0);
653 * }
654 * }
655 */
Harald Weltee61d4592022-11-03 11:05:58 +0100656void osmo_select_shutdown_request(void)
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200657{
658 _osmo_select_shutdown_requested++;
659};
660
661/*! Return the number of times osmo_select_shutdown_request() was called before. */
Harald Weltee61d4592022-11-03 11:05:58 +0100662int osmo_select_shutdown_requested(void)
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200663{
664 return _osmo_select_shutdown_requested;
665};
666
667/*! Return true after osmo_select_shutdown_requested() was called, and after an osmo_select poll loop found no more
668 * pending OSMO_FD_WRITE on any registered socket. */
Harald Weltee61d4592022-11-03 11:05:58 +0100669bool osmo_select_shutdown_done(void) {
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200670 return _osmo_select_shutdown_done;
671};
672
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200673/*! @} */
Harald Welteba6988b2011-08-17 12:46:48 +0200674
Harald Welteec8b4502010-02-20 20:34:29 +0100675#endif /* _HAVE_SYS_SELECT_H */