blob: 590c3db647ffb2d2fb2c5df2e10d04bedf39fa9f [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 Weltee08da972017-11-13 01:00:26 +09008 * All Rights Reserverd.
9 *
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>
Harald Welteec8b4502010-02-20 20:34:29 +010036
Harald Welte54844802010-02-20 22:23:08 +010037#include "../config.h"
38
Harald Welteb904e422020-10-18 21:24:13 +020039#if defined(HAVE_SYS_SELECT_H) && defined(HAVE_POLL_H)
Harald Welte1d604d82017-05-17 15:32:35 +010040#include <sys/select.h>
Harald Welteb904e422020-10-18 21:24:13 +020041#include <poll.h>
Harald Welteec8b4502010-02-20 20:34:29 +010042
Harald Welteba6988b2011-08-17 12:46:48 +020043/*! \addtogroup select
44 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020045 * select() loop abstraction
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020046 *
47 * \file select.c */
Harald Welteba6988b2011-08-17 12:46:48 +020048
Harald Welte7a010b12019-04-06 13:46:40 +020049/* keep a set of file descriptors per-thread, so that each thread can have its own
50 * distinct set of file descriptors to interact with */
51static __thread int maxfd = 0;
52static __thread struct llist_head osmo_fds; /* TLS cannot use LLIST_HEAD() */
53static __thread int unregistered_count;
Harald Welteec8b4502010-02-20 20:34:29 +010054
Harald Welteb904e422020-10-18 21:24:13 +020055#ifndef FORCE_IO_SELECT
56struct poll_state {
57 /* array of pollfd */
58 struct pollfd *poll;
59 /* number of entries in pollfd allocated */
60 unsigned int poll_size;
61 /* number of osmo_fd registered */
62 unsigned int num_registered;
63};
64static __thread struct poll_state g_poll;
65#endif /* FORCE_IO_SELECT */
66
Neels Hofmeyrac49bda2021-06-04 16:30:04 +020067/*! See osmo_select_shutdown_request() */
68static int _osmo_select_shutdown_requested = 0;
69/*! See osmo_select_shutdown_request() */
70static bool _osmo_select_shutdown_done = false;
71
Harald Welte6c0a0e62017-08-12 11:43:14 +020072/*! Set up an osmo-fd. Will not register it.
73 * \param[inout] ofd Osmo FD to be set-up
74 * \param[in] fd OS-level file descriptor number
Harald Welte16886992019-03-20 10:26:39 +010075 * \param[in] when bit-mask of OSMO_FD_{READ,WRITE,EXECEPT}
Harald Welte6c0a0e62017-08-12 11:43:14 +020076 * \param[in] cb Call-back function to be called
77 * \param[in] data Private context pointer
78 * \param[in] priv_nr Private number
79 */
80void osmo_fd_setup(struct osmo_fd *ofd, int fd, unsigned int when,
81 int (*cb)(struct osmo_fd *fd, unsigned int what),
82 void *data, unsigned int priv_nr)
83{
84 ofd->fd = fd;
85 ofd->when = when;
86 ofd->cb = cb;
87 ofd->data = data;
88 ofd->priv_nr = priv_nr;
89}
Philipp Maierb2888532016-12-09 14:07:18 +010090
Harald Welte7e657912020-10-18 22:07:21 +020091/*! Update the 'when' field of osmo_fd. "ofd->when = (ofd->when & when_mask) | when".
92 * Use this function instead of directly modifying ofd->when, as the latter will be
93 * removed soon. */
94void osmo_fd_update_when(struct osmo_fd *ofd, unsigned int when_mask, unsigned int when)
95{
96 ofd->when &= when_mask;
97 ofd->when |= when;
98}
99
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200100/*! Check if a file descriptor is already registered
Philipp Maierb2888532016-12-09 14:07:18 +0100101 * \param[in] fd osmocom file descriptor to be checked
102 * \returns true if registered; otherwise false
103 */
104bool osmo_fd_is_registered(struct osmo_fd *fd)
105{
106 struct osmo_fd *entry;
107 llist_for_each_entry(entry, &osmo_fds, list) {
108 if (entry == fd) {
109 return true;
110 }
111 }
112
113 return false;
114}
115
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200116/*! Register a new file descriptor with select loop abstraction
Harald Welteba6988b2011-08-17 12:46:48 +0200117 * \param[in] fd osmocom file descriptor to be registered
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200118 * \returns 0 on success; negative in case of error
Harald Welteba6988b2011-08-17 12:46:48 +0200119 */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200120int osmo_fd_register(struct osmo_fd *fd)
Harald Welteec8b4502010-02-20 20:34:29 +0100121{
122 int flags;
123
124 /* make FD nonblocking */
125 flags = fcntl(fd->fd, F_GETFL);
126 if (flags < 0)
127 return flags;
128 flags |= O_NONBLOCK;
129 flags = fcntl(fd->fd, F_SETFL, flags);
130 if (flags < 0)
131 return flags;
132
Harald Welte32e1f232011-06-26 13:07:18 +0200133 /* set close-on-exec flag */
134 flags = fcntl(fd->fd, F_GETFD);
135 if (flags < 0)
136 return flags;
137 flags |= FD_CLOEXEC;
138 flags = fcntl(fd->fd, F_SETFD, flags);
139 if (flags < 0)
140 return flags;
141
Harald Welteec8b4502010-02-20 20:34:29 +0100142 /* Register FD */
143 if (fd->fd > maxfd)
144 maxfd = fd->fd;
145
Pau Espin Pedrold05def02020-05-09 19:24:21 +0200146#ifdef OSMO_FD_CHECK
Philipp Maierb2888532016-12-09 14:07:18 +0100147 if (osmo_fd_is_registered(fd)) {
148 fprintf(stderr, "Adding a osmo_fd that is already in the list.\n");
149 return 0;
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +0800150 }
151#endif
Harald Welteb904e422020-10-18 21:24:13 +0200152#ifndef FORCE_IO_SELECT
153 if (g_poll.num_registered + 1 > g_poll.poll_size) {
154 struct pollfd *p;
155 unsigned int new_size = g_poll.poll_size ? g_poll.poll_size * 2 : 1024;
156 p = talloc_realloc(OTC_GLOBAL, g_poll.poll, struct pollfd, new_size);
157 if (!p)
158 return -ENOMEM;
159 memset(p + g_poll.poll_size, 0, new_size - g_poll.poll_size);
160 g_poll.poll = p;
161 g_poll.poll_size = new_size;
162 }
163 g_poll.num_registered++;
164#endif /* FORCE_IO_SELECT */
Holger Hans Peter Freyther43558312010-08-06 06:48:43 +0800165
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200166 llist_add_tail(&fd->list, &osmo_fds);
Harald Welteec8b4502010-02-20 20:34:29 +0100167
168 return 0;
169}
170
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200171/*! Unregister a file descriptor from select loop abstraction
Harald Welteba6988b2011-08-17 12:46:48 +0200172 * \param[in] fd osmocom file descriptor to be unregistered
173 */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200174void osmo_fd_unregister(struct osmo_fd *fd)
Harald Welteec8b4502010-02-20 20:34:29 +0100175{
Philipp Maierb2888532016-12-09 14:07:18 +0100176 /* Note: when fd is inside the osmo_fds list (not registered before)
177 * this function will crash! If in doubt, check file descriptor with
178 * osmo_fd_is_registered() */
Harald Welteec8b4502010-02-20 20:34:29 +0100179 unregistered_count++;
180 llist_del(&fd->list);
Harald Welteb904e422020-10-18 21:24:13 +0200181#ifndef FORCE_IO_SELECT
182 g_poll.num_registered--;
183#endif /* FORCE_IO_SELECT */
Harald Welteec8b4502010-02-20 20:34:29 +0100184}
185
Harald Welteea91a512017-07-13 14:28:30 +0200186/*! Close a file descriptor, mark it as closed + unregister from select loop abstraction
187 * \param[in] fd osmocom file descriptor to be unregistered + closed
188 *
189 * If \a fd is registered, we unregister it from the select() loop
190 * abstraction. We then close the fd and set it to -1, as well as
191 * unsetting any 'when' flags */
192void osmo_fd_close(struct osmo_fd *fd)
193{
194 if (osmo_fd_is_registered(fd))
195 osmo_fd_unregister(fd);
196 if (fd->fd != -1)
197 close(fd->fd);
198 fd->fd = -1;
199 fd->when = 0;
200}
201
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200202/*! Populate the fd_sets and return the highest fd number
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100203 * \param[in] _rset The readfds to populate
204 * \param[in] _wset The wrtiefds to populate
205 * \param[in] _eset The errorfds to populate
206 *
207 * \returns The highest file descriptor seen or 0 on an empty list
208 */
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100209inline int osmo_fd_fill_fds(void *_rset, void *_wset, void *_eset)
Harald Welteec8b4502010-02-20 20:34:29 +0100210{
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100211 fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset;
212 struct osmo_fd *ufd;
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100213 int highfd = 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100214
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200215 llist_for_each_entry(ufd, &osmo_fds, list) {
Harald Welte16886992019-03-20 10:26:39 +0100216 if (ufd->when & OSMO_FD_READ)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100217 FD_SET(ufd->fd, readset);
Harald Welteec8b4502010-02-20 20:34:29 +0100218
Harald Welte16886992019-03-20 10:26:39 +0100219 if (ufd->when & OSMO_FD_WRITE)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100220 FD_SET(ufd->fd, writeset);
Harald Welteec8b4502010-02-20 20:34:29 +0100221
Harald Welte16886992019-03-20 10:26:39 +0100222 if (ufd->when & OSMO_FD_EXCEPT)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100223 FD_SET(ufd->fd, exceptset);
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100224
225 if (ufd->fd > highfd)
226 highfd = ufd->fd;
Harald Welteec8b4502010-02-20 20:34:29 +0100227 }
228
Holger Hans Peter Freytherff206412017-03-07 14:28:38 +0100229 return highfd;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100230}
Harald Welteec8b4502010-02-20 20:34:29 +0100231
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100232inline int osmo_fd_disp_fds(void *_rset, void *_wset, void *_eset)
233{
234 struct osmo_fd *ufd, *tmp;
235 int work = 0;
236 fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset;
Harald Welteec8b4502010-02-20 20:34:29 +0100237
Harald Welteec8b4502010-02-20 20:34:29 +0100238restart:
239 unregistered_count = 0;
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200240 llist_for_each_entry_safe(ufd, tmp, &osmo_fds, list) {
Harald Welteec8b4502010-02-20 20:34:29 +0100241 int flags = 0;
242
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100243 if (FD_ISSET(ufd->fd, readset)) {
Harald Welte16886992019-03-20 10:26:39 +0100244 flags |= OSMO_FD_READ;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100245 FD_CLR(ufd->fd, readset);
Harald Welteec8b4502010-02-20 20:34:29 +0100246 }
247
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100248 if (FD_ISSET(ufd->fd, writeset)) {
Harald Welte16886992019-03-20 10:26:39 +0100249 flags |= OSMO_FD_WRITE;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100250 FD_CLR(ufd->fd, writeset);
Harald Welteec8b4502010-02-20 20:34:29 +0100251 }
252
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100253 if (FD_ISSET(ufd->fd, exceptset)) {
Harald Welte16886992019-03-20 10:26:39 +0100254 flags |= OSMO_FD_EXCEPT;
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100255 FD_CLR(ufd->fd, exceptset);
Harald Welteec8b4502010-02-20 20:34:29 +0100256 }
257
258 if (flags) {
259 work = 1;
Harald Welte52a83752019-02-21 16:37:10 +0100260 /* make sure to clear any log context before processing the next incoming message
261 * as part of some file descriptor callback. This effectively prevents "context
262 * leaking" from processing of one message into processing of the next message as part
263 * of one iteration through the list of file descriptors here. See OS#3813 */
264 log_reset_context();
Harald Welteec8b4502010-02-20 20:34:29 +0100265 ufd->cb(ufd, flags);
266 }
Holger Hans Peter Freyther92e1e702014-04-09 17:24:00 +0200267 /* ugly, ugly hack. If more than one filedescriptor was
Harald Welteec8b4502010-02-20 20:34:29 +0100268 * unregistered, they might have been consecutive and
269 * llist_for_each_entry_safe() is no longer safe */
Holger Hans Peter Freyther23ba4742010-04-11 17:33:19 +0200270 /* this seems to happen with the last element of the list as well */
271 if (unregistered_count >= 1)
Harald Welteec8b4502010-02-20 20:34:29 +0100272 goto restart;
273 }
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100274
Harald Welteec8b4502010-02-20 20:34:29 +0100275 return work;
276}
277
Harald Welteb904e422020-10-18 21:24:13 +0200278
279#ifndef FORCE_IO_SELECT
280/* fill g_poll.poll and return the number of entries filled */
281static unsigned int poll_fill_fds(void)
282{
283 struct osmo_fd *ufd;
284 unsigned int i = 0;
285
286 llist_for_each_entry(ufd, &osmo_fds, list) {
287 struct pollfd *p;
288
289 if (!ufd->when)
290 continue;
291
292 p = &g_poll.poll[i++];
293
294 p->fd = ufd->fd;
295 p->events = 0;
296 p->revents = 0;
297
298 /* use the same mapping as the Linux kernel does in fs/select.c */
299 if (ufd->when & OSMO_FD_READ)
300 p->events |= POLLIN | POLLHUP | POLLERR;
301
302 if (ufd->when & OSMO_FD_WRITE)
303 p->events |= POLLOUT | POLLERR;
304
305 if (ufd->when & OSMO_FD_EXCEPT)
306 p->events |= POLLPRI;
307
308 }
309
310 return i;
311}
312
313/* iterate over first n_fd entries of g_poll.poll + dispatch */
314static int poll_disp_fds(int n_fd)
315{
316 struct osmo_fd *ufd;
317 unsigned int i;
318 int work = 0;
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200319 int shutdown_pending_writes = 0;
Harald Welteb904e422020-10-18 21:24:13 +0200320
321 for (i = 0; i < n_fd; i++) {
322 struct pollfd *p = &g_poll.poll[i];
323 int flags = 0;
324
325 if (!p->revents)
326 continue;
327
328 ufd = osmo_fd_get_by_fd(p->fd);
329 if (!ufd) {
330 /* FD might have been unregistered meanwhile */
331 continue;
332 }
333 /* use the same mapping as the Linux kernel does in fs/select.c */
334 if (p->revents & (POLLIN | POLLHUP | POLLERR))
335 flags |= OSMO_FD_READ;
336 if (p->revents & (POLLOUT | POLLERR))
337 flags |= OSMO_FD_WRITE;
338 if (p->revents & POLLPRI)
339 flags |= OSMO_FD_EXCEPT;
340
341 /* make sure we never report more than the user requested */
342 flags &= ufd->when;
343
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200344 if (_osmo_select_shutdown_requested > 0) {
345 if (ufd->when & OSMO_FD_WRITE)
346 shutdown_pending_writes++;
347 }
348
Harald Welteb904e422020-10-18 21:24:13 +0200349 if (flags) {
350 work = 1;
351 /* make sure to clear any log context before processing the next incoming message
352 * as part of some file descriptor callback. This effectively prevents "context
353 * leaking" from processing of one message into processing of the next message as part
354 * of one iteration through the list of file descriptors here. See OS#3813 */
355 log_reset_context();
356 ufd->cb(ufd, flags);
357 }
358 }
359
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200360 if (_osmo_select_shutdown_requested > 0 && !shutdown_pending_writes)
361 _osmo_select_shutdown_done = true;
362
Harald Welteb904e422020-10-18 21:24:13 +0200363 return work;
364}
365
366static int _osmo_select_main(int polling)
367{
368 unsigned int n_poll;
369 int rc;
370
371 /* prepare read and write fdsets */
372 n_poll = poll_fill_fds();
373
374 if (!polling)
375 osmo_timers_prepare();
376
377 rc = poll(g_poll.poll, n_poll, polling ? 0 : osmo_timers_nearest_ms());
378 if (rc < 0)
379 return 0;
380
381 /* fire timers */
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200382 if (!_osmo_select_shutdown_requested)
383 osmo_timers_update();
Harald Welteb904e422020-10-18 21:24:13 +0200384
385 OSMO_ASSERT(osmo_ctx->select);
386
387 /* call registered callback functions */
388 return poll_disp_fds(n_poll);
389}
390#else /* FORCE_IO_SELECT */
391/* the old implementation based on select, used 2008-2020 */
Harald Welte2d906112019-03-18 17:17:43 +0100392static int _osmo_select_main(int polling)
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100393{
394 fd_set readset, writeset, exceptset;
395 int rc;
396 struct timeval no_time = {0, 0};
397
398 FD_ZERO(&readset);
399 FD_ZERO(&writeset);
400 FD_ZERO(&exceptset);
401
402 /* prepare read and write fdsets */
403 osmo_fd_fill_fds(&readset, &writeset, &exceptset);
404
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100405 if (!polling)
406 osmo_timers_prepare();
407 rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : osmo_timers_nearest());
408 if (rc < 0)
409 return 0;
410
411 /* fire timers */
412 osmo_timers_update();
413
Harald Welte2d906112019-03-18 17:17:43 +0100414 OSMO_ASSERT(osmo_ctx->select);
415
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100416 /* call registered callback functions */
417 return osmo_fd_disp_fds(&readset, &writeset, &exceptset);
418}
Harald Welteb904e422020-10-18 21:24:13 +0200419#endif /* FORCE_IO_SELECT */
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +0100420
Harald Welte2d906112019-03-18 17:17:43 +0100421/*! select main loop integration
422 * \param[in] polling should we pollonly (1) or block on select (0)
423 * \returns 0 if no fd handled; 1 if fd handled; negative in case of error
424 */
425int osmo_select_main(int polling)
426{
427 int rc = _osmo_select_main(polling);
428#ifndef EMBEDDED
429 if (talloc_total_size(osmo_ctx->select) != 0) {
430 osmo_panic("You cannot use the 'select' volatile "
431 "context if you don't use osmo_select_main_ctx()!\n");
432 }
433#endif
434 return rc;
435}
436
437#ifndef EMBEDDED
438/*! select main loop integration with temporary select-dispatch talloc context
439 * \param[in] polling should we pollonly (1) or block on select (0)
440 * \returns 0 if no fd handled; 1 if fd handled; negative in case of error
441 */
442int osmo_select_main_ctx(int polling)
443{
444 int rc = _osmo_select_main(polling);
445 /* free all the children of the volatile 'select' scope context */
446 talloc_free_children(osmo_ctx->select);
447 return rc;
448}
449#endif
450
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200451/*! find an osmo_fd based on the integer fd
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200452 * \param[in] fd file descriptor to use as search key
453 * \returns \ref osmo_fd for \ref fd; NULL in case it doesn't exist */
Harald Welte6c33ae22016-03-19 21:17:58 +0100454struct osmo_fd *osmo_fd_get_by_fd(int fd)
455{
456 struct osmo_fd *ofd;
457
458 llist_for_each_entry(ofd, &osmo_fds, list) {
459 if (ofd->fd == fd)
460 return ofd;
461 }
462 return NULL;
463}
464
Harald Welte7a010b12019-04-06 13:46:40 +0200465/*! initialize the osmocom select abstraction for the current thread */
466void osmo_select_init(void)
467{
468 INIT_LLIST_HEAD(&osmo_fds);
469}
470
471/* ensure main thread always has pre-initialized osmo_fds */
472static __attribute__((constructor)) void on_dso_load_select(void)
473{
474 osmo_select_init();
475}
476
Harald Welteea4d8932017-12-03 16:13:39 +0100477#ifdef HAVE_SYS_TIMERFD_H
478#include <sys/timerfd.h>
479
480/*! disable the osmocom-wrapped timerfd */
481int osmo_timerfd_disable(struct osmo_fd *ofd)
482{
483 const struct itimerspec its_null = {
484 .it_value = { 0, 0 },
485 .it_interval = { 0, 0 },
486 };
487 return timerfd_settime(ofd->fd, 0, &its_null, NULL);
488}
489
Alexander Chemerise8ec2142020-05-09 01:57:16 +0300490/*! schedule the osmocom-wrapped timerfd to occur first at \a first, then periodically at \a interval
Harald Welteea4d8932017-12-03 16:13:39 +0100491 * \param[in] ofd Osmocom wrapped timerfd
492 * \param[in] first Relative time at which the timer should first execute (NULL = \a interval)
493 * \param[in] interval Time interval at which subsequent timer shall fire
494 * \returns 0 on success; negative on error */
495int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first,
496 const struct timespec *interval)
497{
498 struct itimerspec its;
499
500 if (ofd->fd < 0)
501 return -EINVAL;
502
503 /* first expiration */
504 if (first)
505 its.it_value = *first;
506 else
507 its.it_value = *interval;
508 /* repeating interval */
509 its.it_interval = *interval;
510
511 return timerfd_settime(ofd->fd, 0, &its, NULL);
512}
513
514/*! setup osmocom-wrapped timerfd
515 * \param[inout] ofd Osmocom-wrapped timerfd on which to operate
516 * \param[in] cb Call-back function called when timerfd becomes readable
517 * \param[in] data Opaque data to be passed on to call-back
518 * \returns 0 on success; negative on error
519 *
520 * We simply initialize the data structures here, but do not yet
521 * schedule the timer.
522 */
523int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data)
524{
525 ofd->cb = cb;
526 ofd->data = data;
Harald Welte16886992019-03-20 10:26:39 +0100527 ofd->when = OSMO_FD_READ;
Harald Welteea4d8932017-12-03 16:13:39 +0100528
529 if (ofd->fd < 0) {
Harald Welte37608f92018-10-21 12:32:55 +0200530 int rc;
531
Harald Welteea4d8932017-12-03 16:13:39 +0100532 ofd->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
533 if (ofd->fd < 0)
534 return ofd->fd;
535
Harald Welte37608f92018-10-21 12:32:55 +0200536 rc = osmo_fd_register(ofd);
537 if (rc < 0) {
Harald Welte100e44e2020-04-18 21:16:43 +0200538 osmo_fd_unregister(ofd);
Harald Welte37608f92018-10-21 12:32:55 +0200539 close(ofd->fd);
540 ofd->fd = -1;
541 return rc;
542 }
Harald Welteea4d8932017-12-03 16:13:39 +0100543 }
544 return 0;
545}
546
547#endif /* HAVE_SYS_TIMERFD_H */
548
Harald Weltea70ac852020-04-17 19:20:01 +0200549#ifdef HAVE_SYS_SIGNALFD_H
550#include <sys/signalfd.h>
551
552static int signalfd_callback(struct osmo_fd *ofd, unsigned int what)
553{
554 struct osmo_signalfd *osfd = ofd->data;
555 struct signalfd_siginfo fdsi;
556 int rc;
557
558 rc = read(ofd->fd, &fdsi, sizeof(fdsi));
559 if (rc < 0) {
560 osmo_fd_unregister(ofd);
561 close(ofd->fd);
562 ofd->fd = -1;
563 return rc;
564 }
565
566 osfd->cb(osfd, &fdsi);
567
568 return 0;
569};
570
571/*! create a signalfd and register it with osmocom select loop.
572 * \param[in] ctx talloc context from which osmo_signalfd is to be allocated
573 * \param[in] set of signals to be accept via this file descriptor
574 * \param[in] cb call-back function to be called for each arriving signal
575 * \param[in] data opaque user-provided data to pass to callback
576 * \returns pointer to newly-allocated + registered osmo_signalfd; NULL on error */
577struct osmo_signalfd *
578osmo_signalfd_setup(void *ctx, sigset_t set, osmo_signalfd_cb *cb, void *data)
579{
580 struct osmo_signalfd *osfd = talloc_size(ctx, sizeof(*osfd));
581 int fd, rc;
582
583 if (!osfd)
584 return NULL;
585
586 osfd->data = data;
587 osfd->sigset = set;
588 osfd->cb = cb;
589
590 fd = signalfd(-1, &osfd->sigset, SFD_NONBLOCK);
591 if (fd < 0) {
592 talloc_free(osfd);
593 return NULL;
594 }
595
596 osmo_fd_setup(&osfd->ofd, fd, OSMO_FD_READ, signalfd_callback, osfd, 0);
597 rc = osmo_fd_register(&osfd->ofd);
598 if (rc < 0) {
599 close(fd);
600 talloc_free(osfd);
601 return NULL;
602 }
603
604 return osfd;
605}
606
607#endif /* HAVE_SYS_SIGNALFD_H */
Harald Welteea4d8932017-12-03 16:13:39 +0100608
Neels Hofmeyrac49bda2021-06-04 16:30:04 +0200609/*! Request osmo_select_* to only service pending OSMO_FD_WRITE requests. Once all writes are done,
610 * osmo_select_shutdown_done() returns true. This allows for example to send all outbound packets before terminating the
611 * process.
612 *
613 * Usage example:
614 *
615 * static void signal_handler(int signum)
616 * {
617 * fprintf(stdout, "signal %u received\n", signum);
618 *
619 * switch (signum) {
620 * case SIGINT:
621 * case SIGTERM:
622 * // If the user hits Ctrl-C the third time, just terminate immediately.
623 * if (osmo_select_shutdown_requested() >= 2)
624 * exit(-1);
625 * // Request write-only mode in osmo_select_main_ctx()
626 * osmo_select_shutdown_request();
627 * break;
628 * [...]
629 * }
630 *
631 * main()
632 * {
633 * signal(SIGINT, &signal_handler);
634 * signal(SIGTERM, &signal_handler);
635 *
636 * [...]
637 *
638 * // After the signal_handler issued osmo_select_shutdown_request(), osmo_select_shutdown_done() returns true
639 * // as soon as all write queues are empty.
640 * while (!osmo_select_shutdown_done()) {
641 * osmo_select_main_ctx(0);
642 * }
643 * }
644 */
645void osmo_select_shutdown_request()
646{
647 _osmo_select_shutdown_requested++;
648};
649
650/*! Return the number of times osmo_select_shutdown_request() was called before. */
651int osmo_select_shutdown_requested()
652{
653 return _osmo_select_shutdown_requested;
654};
655
656/*! Return true after osmo_select_shutdown_requested() was called, and after an osmo_select poll loop found no more
657 * pending OSMO_FD_WRITE on any registered socket. */
658bool osmo_select_shutdown_done() {
659 return _osmo_select_shutdown_done;
660};
661
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200662/*! @} */
Harald Welteba6988b2011-08-17 12:46:48 +0200663
Harald Welteec8b4502010-02-20 20:34:29 +0100664#endif /* _HAVE_SYS_SELECT_H */