2017-06-20 02:35:06 +00:00
|
|
|
/*! \file select.c
|
|
|
|
* select filedescriptor handling.
|
|
|
|
* Taken from:
|
2010-02-20 19:34:29 +00:00
|
|
|
* userspace logging daemon for the iptables ULOG target
|
2017-06-20 02:35:06 +00:00
|
|
|
* of the linux 2.4 netfilter subsystem. */
|
|
|
|
/*
|
2010-02-20 19:34:29 +00:00
|
|
|
* (C) 2000-2009 by Harald Welte <laforge@gnumonks.org>
|
2017-11-12 16:00:26 +00:00
|
|
|
* All Rights Reserverd.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2010-02-20 19:34:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2010-10-31 12:56:45 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2010-02-20 19:34:29 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2015-11-11 15:02:54 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
2010-02-20 19:34:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
2010-08-05 22:48:43 +00:00
|
|
|
#include <stdio.h>
|
2017-07-13 12:28:30 +00:00
|
|
|
#include <unistd.h>
|
2014-03-10 16:28:33 +00:00
|
|
|
#include <string.h>
|
2016-12-09 13:07:18 +00:00
|
|
|
#include <stdbool.h>
|
2017-12-03 15:13:39 +00:00
|
|
|
#include <errno.h>
|
2010-08-05 22:48:43 +00:00
|
|
|
|
2011-03-22 15:36:13 +00:00
|
|
|
#include <osmocom/core/select.h>
|
|
|
|
#include <osmocom/core/linuxlist.h>
|
|
|
|
#include <osmocom/core/timer.h>
|
2019-02-21 15:37:10 +00:00
|
|
|
#include <osmocom/core/logging.h>
|
2019-03-18 16:17:43 +00:00
|
|
|
#include <osmocom/core/talloc.h>
|
|
|
|
#include <osmocom/core/utils.h>
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2010-02-20 21:23:08 +00:00
|
|
|
#include "../config.h"
|
|
|
|
|
2010-02-20 19:34:29 +00:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
2017-05-17 14:32:35 +00:00
|
|
|
#include <sys/select.h>
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2011-08-17 10:46:48 +00:00
|
|
|
/*! \addtogroup select
|
|
|
|
* @{
|
2017-06-19 22:17:59 +00:00
|
|
|
* select() loop abstraction
|
2017-06-20 02:35:06 +00:00
|
|
|
*
|
|
|
|
* \file select.c */
|
2011-08-17 10:46:48 +00:00
|
|
|
|
2019-04-06 11:46:40 +00:00
|
|
|
/* keep a set of file descriptors per-thread, so that each thread can have its own
|
|
|
|
* distinct set of file descriptors to interact with */
|
|
|
|
static __thread int maxfd = 0;
|
|
|
|
static __thread struct llist_head osmo_fds; /* TLS cannot use LLIST_HEAD() */
|
|
|
|
static __thread int unregistered_count;
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2017-08-12 09:43:14 +00:00
|
|
|
/*! Set up an osmo-fd. Will not register it.
|
|
|
|
* \param[inout] ofd Osmo FD to be set-up
|
|
|
|
* \param[in] fd OS-level file descriptor number
|
2019-03-20 09:26:39 +00:00
|
|
|
* \param[in] when bit-mask of OSMO_FD_{READ,WRITE,EXECEPT}
|
2017-08-12 09:43:14 +00:00
|
|
|
* \param[in] cb Call-back function to be called
|
|
|
|
* \param[in] data Private context pointer
|
|
|
|
* \param[in] priv_nr Private number
|
|
|
|
*/
|
|
|
|
void osmo_fd_setup(struct osmo_fd *ofd, int fd, unsigned int when,
|
|
|
|
int (*cb)(struct osmo_fd *fd, unsigned int what),
|
|
|
|
void *data, unsigned int priv_nr)
|
|
|
|
{
|
|
|
|
ofd->fd = fd;
|
|
|
|
ofd->when = when;
|
|
|
|
ofd->cb = cb;
|
|
|
|
ofd->data = data;
|
|
|
|
ofd->priv_nr = priv_nr;
|
|
|
|
}
|
2016-12-09 13:07:18 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Check if a file descriptor is already registered
|
2016-12-09 13:07:18 +00:00
|
|
|
* \param[in] fd osmocom file descriptor to be checked
|
|
|
|
* \returns true if registered; otherwise false
|
|
|
|
*/
|
|
|
|
bool osmo_fd_is_registered(struct osmo_fd *fd)
|
|
|
|
{
|
|
|
|
struct osmo_fd *entry;
|
|
|
|
llist_for_each_entry(entry, &osmo_fds, list) {
|
|
|
|
if (entry == fd) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Register a new file descriptor with select loop abstraction
|
2011-08-17 10:46:48 +00:00
|
|
|
* \param[in] fd osmocom file descriptor to be registered
|
2016-04-25 10:11:20 +00:00
|
|
|
* \returns 0 on success; negative in case of error
|
2011-08-17 10:46:48 +00:00
|
|
|
*/
|
2011-05-07 10:42:40 +00:00
|
|
|
int osmo_fd_register(struct osmo_fd *fd)
|
2010-02-20 19:34:29 +00:00
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
/* make FD nonblocking */
|
|
|
|
flags = fcntl(fd->fd, F_GETFL);
|
|
|
|
if (flags < 0)
|
|
|
|
return flags;
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
flags = fcntl(fd->fd, F_SETFL, flags);
|
|
|
|
if (flags < 0)
|
|
|
|
return flags;
|
|
|
|
|
2011-06-26 11:07:18 +00:00
|
|
|
/* set close-on-exec flag */
|
|
|
|
flags = fcntl(fd->fd, F_GETFD);
|
|
|
|
if (flags < 0)
|
|
|
|
return flags;
|
|
|
|
flags |= FD_CLOEXEC;
|
|
|
|
flags = fcntl(fd->fd, F_SETFD, flags);
|
|
|
|
if (flags < 0)
|
|
|
|
return flags;
|
|
|
|
|
2010-02-20 19:34:29 +00:00
|
|
|
/* Register FD */
|
|
|
|
if (fd->fd > maxfd)
|
|
|
|
maxfd = fd->fd;
|
|
|
|
|
2010-08-05 22:48:43 +00:00
|
|
|
#ifdef BSC_FD_CHECK
|
2016-12-09 13:07:18 +00:00
|
|
|
if (osmo_fd_is_registered(fd)) {
|
|
|
|
fprintf(stderr, "Adding a osmo_fd that is already in the list.\n");
|
|
|
|
return 0;
|
2010-08-05 22:48:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-05-07 10:42:40 +00:00
|
|
|
llist_add_tail(&fd->list, &osmo_fds);
|
2010-02-20 19:34:29 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Unregister a file descriptor from select loop abstraction
|
2011-08-17 10:46:48 +00:00
|
|
|
* \param[in] fd osmocom file descriptor to be unregistered
|
|
|
|
*/
|
2011-05-07 10:42:40 +00:00
|
|
|
void osmo_fd_unregister(struct osmo_fd *fd)
|
2010-02-20 19:34:29 +00:00
|
|
|
{
|
2016-12-09 13:07:18 +00:00
|
|
|
/* Note: when fd is inside the osmo_fds list (not registered before)
|
|
|
|
* this function will crash! If in doubt, check file descriptor with
|
|
|
|
* osmo_fd_is_registered() */
|
2010-02-20 19:34:29 +00:00
|
|
|
unregistered_count++;
|
|
|
|
llist_del(&fd->list);
|
|
|
|
}
|
|
|
|
|
2017-07-13 12:28:30 +00:00
|
|
|
/*! Close a file descriptor, mark it as closed + unregister from select loop abstraction
|
|
|
|
* \param[in] fd osmocom file descriptor to be unregistered + closed
|
|
|
|
*
|
|
|
|
* If \a fd is registered, we unregister it from the select() loop
|
|
|
|
* abstraction. We then close the fd and set it to -1, as well as
|
|
|
|
* unsetting any 'when' flags */
|
|
|
|
void osmo_fd_close(struct osmo_fd *fd)
|
|
|
|
{
|
|
|
|
if (osmo_fd_is_registered(fd))
|
|
|
|
osmo_fd_unregister(fd);
|
|
|
|
if (fd->fd != -1)
|
|
|
|
close(fd->fd);
|
|
|
|
fd->fd = -1;
|
|
|
|
fd->when = 0;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Populate the fd_sets and return the highest fd number
|
2017-03-07 13:28:38 +00:00
|
|
|
* \param[in] _rset The readfds to populate
|
|
|
|
* \param[in] _wset The wrtiefds to populate
|
|
|
|
* \param[in] _eset The errorfds to populate
|
|
|
|
*
|
|
|
|
* \returns The highest file descriptor seen or 0 on an empty list
|
|
|
|
*/
|
2016-03-21 08:55:05 +00:00
|
|
|
inline int osmo_fd_fill_fds(void *_rset, void *_wset, void *_eset)
|
2010-02-20 19:34:29 +00:00
|
|
|
{
|
2016-03-21 08:55:05 +00:00
|
|
|
fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset;
|
|
|
|
struct osmo_fd *ufd;
|
2017-03-07 13:28:38 +00:00
|
|
|
int highfd = 0;
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2011-05-07 10:42:40 +00:00
|
|
|
llist_for_each_entry(ufd, &osmo_fds, list) {
|
2019-03-20 09:26:39 +00:00
|
|
|
if (ufd->when & OSMO_FD_READ)
|
2016-03-21 08:55:05 +00:00
|
|
|
FD_SET(ufd->fd, readset);
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2019-03-20 09:26:39 +00:00
|
|
|
if (ufd->when & OSMO_FD_WRITE)
|
2016-03-21 08:55:05 +00:00
|
|
|
FD_SET(ufd->fd, writeset);
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2019-03-20 09:26:39 +00:00
|
|
|
if (ufd->when & OSMO_FD_EXCEPT)
|
2016-03-21 08:55:05 +00:00
|
|
|
FD_SET(ufd->fd, exceptset);
|
2017-03-07 13:28:38 +00:00
|
|
|
|
|
|
|
if (ufd->fd > highfd)
|
|
|
|
highfd = ufd->fd;
|
2010-02-20 19:34:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 13:28:38 +00:00
|
|
|
return highfd;
|
2016-03-21 08:55:05 +00:00
|
|
|
}
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2016-03-21 08:55:05 +00:00
|
|
|
inline int osmo_fd_disp_fds(void *_rset, void *_wset, void *_eset)
|
|
|
|
{
|
|
|
|
struct osmo_fd *ufd, *tmp;
|
|
|
|
int work = 0;
|
|
|
|
fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset;
|
2010-02-20 19:34:29 +00:00
|
|
|
|
|
|
|
restart:
|
|
|
|
unregistered_count = 0;
|
2011-05-07 10:42:40 +00:00
|
|
|
llist_for_each_entry_safe(ufd, tmp, &osmo_fds, list) {
|
2010-02-20 19:34:29 +00:00
|
|
|
int flags = 0;
|
|
|
|
|
2016-03-21 08:55:05 +00:00
|
|
|
if (FD_ISSET(ufd->fd, readset)) {
|
2019-03-20 09:26:39 +00:00
|
|
|
flags |= OSMO_FD_READ;
|
2016-03-21 08:55:05 +00:00
|
|
|
FD_CLR(ufd->fd, readset);
|
2010-02-20 19:34:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 08:55:05 +00:00
|
|
|
if (FD_ISSET(ufd->fd, writeset)) {
|
2019-03-20 09:26:39 +00:00
|
|
|
flags |= OSMO_FD_WRITE;
|
2016-03-21 08:55:05 +00:00
|
|
|
FD_CLR(ufd->fd, writeset);
|
2010-02-20 19:34:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 08:55:05 +00:00
|
|
|
if (FD_ISSET(ufd->fd, exceptset)) {
|
2019-03-20 09:26:39 +00:00
|
|
|
flags |= OSMO_FD_EXCEPT;
|
2016-03-21 08:55:05 +00:00
|
|
|
FD_CLR(ufd->fd, exceptset);
|
2010-02-20 19:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags) {
|
|
|
|
work = 1;
|
2019-02-21 15:37:10 +00:00
|
|
|
/* make sure to clear any log context before processing the next incoming message
|
|
|
|
* as part of some file descriptor callback. This effectively prevents "context
|
|
|
|
* leaking" from processing of one message into processing of the next message as part
|
|
|
|
* of one iteration through the list of file descriptors here. See OS#3813 */
|
|
|
|
log_reset_context();
|
2010-02-20 19:34:29 +00:00
|
|
|
ufd->cb(ufd, flags);
|
|
|
|
}
|
2014-04-09 15:24:00 +00:00
|
|
|
/* ugly, ugly hack. If more than one filedescriptor was
|
2010-02-20 19:34:29 +00:00
|
|
|
* unregistered, they might have been consecutive and
|
|
|
|
* llist_for_each_entry_safe() is no longer safe */
|
2010-04-11 15:33:19 +00:00
|
|
|
/* this seems to happen with the last element of the list as well */
|
|
|
|
if (unregistered_count >= 1)
|
2010-02-20 19:34:29 +00:00
|
|
|
goto restart;
|
|
|
|
}
|
2016-03-21 08:55:05 +00:00
|
|
|
|
2010-02-20 19:34:29 +00:00
|
|
|
return work;
|
|
|
|
}
|
|
|
|
|
2019-03-18 16:17:43 +00:00
|
|
|
static int _osmo_select_main(int polling)
|
2016-03-21 08:55:05 +00:00
|
|
|
{
|
|
|
|
fd_set readset, writeset, exceptset;
|
|
|
|
int rc;
|
|
|
|
struct timeval no_time = {0, 0};
|
|
|
|
|
|
|
|
FD_ZERO(&readset);
|
|
|
|
FD_ZERO(&writeset);
|
|
|
|
FD_ZERO(&exceptset);
|
|
|
|
|
|
|
|
/* prepare read and write fdsets */
|
|
|
|
osmo_fd_fill_fds(&readset, &writeset, &exceptset);
|
|
|
|
|
|
|
|
if (!polling)
|
|
|
|
osmo_timers_prepare();
|
|
|
|
rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : osmo_timers_nearest());
|
|
|
|
if (rc < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* fire timers */
|
|
|
|
osmo_timers_update();
|
|
|
|
|
2019-03-18 16:17:43 +00:00
|
|
|
OSMO_ASSERT(osmo_ctx->select);
|
|
|
|
|
2016-03-21 08:55:05 +00:00
|
|
|
/* call registered callback functions */
|
|
|
|
return osmo_fd_disp_fds(&readset, &writeset, &exceptset);
|
|
|
|
}
|
|
|
|
|
2019-03-18 16:17:43 +00:00
|
|
|
/*! select main loop integration
|
|
|
|
* \param[in] polling should we pollonly (1) or block on select (0)
|
|
|
|
* \returns 0 if no fd handled; 1 if fd handled; negative in case of error
|
|
|
|
*/
|
|
|
|
int osmo_select_main(int polling)
|
|
|
|
{
|
|
|
|
int rc = _osmo_select_main(polling);
|
|
|
|
#ifndef EMBEDDED
|
|
|
|
if (talloc_total_size(osmo_ctx->select) != 0) {
|
|
|
|
osmo_panic("You cannot use the 'select' volatile "
|
|
|
|
"context if you don't use osmo_select_main_ctx()!\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef EMBEDDED
|
|
|
|
/*! select main loop integration with temporary select-dispatch talloc context
|
|
|
|
* \param[in] polling should we pollonly (1) or block on select (0)
|
|
|
|
* \returns 0 if no fd handled; 1 if fd handled; negative in case of error
|
|
|
|
*/
|
|
|
|
int osmo_select_main_ctx(int polling)
|
|
|
|
{
|
|
|
|
int rc = _osmo_select_main(polling);
|
|
|
|
/* free all the children of the volatile 'select' scope context */
|
|
|
|
talloc_free_children(osmo_ctx->select);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! find an osmo_fd based on the integer fd
|
2016-04-25 10:11:20 +00:00
|
|
|
* \param[in] fd file descriptor to use as search key
|
|
|
|
* \returns \ref osmo_fd for \ref fd; NULL in case it doesn't exist */
|
2016-03-19 20:17:58 +00:00
|
|
|
struct osmo_fd *osmo_fd_get_by_fd(int fd)
|
|
|
|
{
|
|
|
|
struct osmo_fd *ofd;
|
|
|
|
|
|
|
|
llist_for_each_entry(ofd, &osmo_fds, list) {
|
|
|
|
if (ofd->fd == fd)
|
|
|
|
return ofd;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-06 11:46:40 +00:00
|
|
|
/*! initialize the osmocom select abstraction for the current thread */
|
|
|
|
void osmo_select_init(void)
|
|
|
|
{
|
|
|
|
INIT_LLIST_HEAD(&osmo_fds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ensure main thread always has pre-initialized osmo_fds */
|
|
|
|
static __attribute__((constructor)) void on_dso_load_select(void)
|
|
|
|
{
|
|
|
|
osmo_select_init();
|
|
|
|
}
|
|
|
|
|
2017-12-03 15:13:39 +00:00
|
|
|
#ifdef HAVE_SYS_TIMERFD_H
|
|
|
|
#include <sys/timerfd.h>
|
|
|
|
|
|
|
|
/*! disable the osmocom-wrapped timerfd */
|
|
|
|
int osmo_timerfd_disable(struct osmo_fd *ofd)
|
|
|
|
{
|
|
|
|
const struct itimerspec its_null = {
|
|
|
|
.it_value = { 0, 0 },
|
|
|
|
.it_interval = { 0, 0 },
|
|
|
|
};
|
|
|
|
return timerfd_settime(ofd->fd, 0, &its_null, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! schedule the osmcoom-wrapped timerfd to occur first at \a first, then periodically at \a interval
|
|
|
|
* \param[in] ofd Osmocom wrapped timerfd
|
|
|
|
* \param[in] first Relative time at which the timer should first execute (NULL = \a interval)
|
|
|
|
* \param[in] interval Time interval at which subsequent timer shall fire
|
|
|
|
* \returns 0 on success; negative on error */
|
|
|
|
int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first,
|
|
|
|
const struct timespec *interval)
|
|
|
|
{
|
|
|
|
struct itimerspec its;
|
|
|
|
|
|
|
|
if (ofd->fd < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* first expiration */
|
|
|
|
if (first)
|
|
|
|
its.it_value = *first;
|
|
|
|
else
|
|
|
|
its.it_value = *interval;
|
|
|
|
/* repeating interval */
|
|
|
|
its.it_interval = *interval;
|
|
|
|
|
|
|
|
return timerfd_settime(ofd->fd, 0, &its, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! setup osmocom-wrapped timerfd
|
|
|
|
* \param[inout] ofd Osmocom-wrapped timerfd on which to operate
|
|
|
|
* \param[in] cb Call-back function called when timerfd becomes readable
|
|
|
|
* \param[in] data Opaque data to be passed on to call-back
|
|
|
|
* \returns 0 on success; negative on error
|
|
|
|
*
|
|
|
|
* We simply initialize the data structures here, but do not yet
|
|
|
|
* schedule the timer.
|
|
|
|
*/
|
|
|
|
int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data)
|
|
|
|
{
|
|
|
|
ofd->cb = cb;
|
|
|
|
ofd->data = data;
|
2019-03-20 09:26:39 +00:00
|
|
|
ofd->when = OSMO_FD_READ;
|
2017-12-03 15:13:39 +00:00
|
|
|
|
|
|
|
if (ofd->fd < 0) {
|
2018-10-21 10:32:55 +00:00
|
|
|
int rc;
|
|
|
|
|
2017-12-03 15:13:39 +00:00
|
|
|
ofd->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
|
|
|
|
if (ofd->fd < 0)
|
|
|
|
return ofd->fd;
|
|
|
|
|
2018-10-21 10:32:55 +00:00
|
|
|
rc = osmo_fd_register(ofd);
|
|
|
|
if (rc < 0) {
|
|
|
|
close(ofd->fd);
|
|
|
|
ofd->fd = -1;
|
|
|
|
return rc;
|
|
|
|
}
|
2017-12-03 15:13:39 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_SYS_TIMERFD_H */
|
|
|
|
|
|
|
|
|
2012-04-18 19:53:23 +00:00
|
|
|
/*! @} */
|
2011-08-17 10:46:48 +00:00
|
|
|
|
2010-02-20 19:34:29 +00:00
|
|
|
#endif /* _HAVE_SYS_SELECT_H */
|