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 | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 7 | * (C) 2000-2009 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * All Rights Reserverd. |
| 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. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
Jaroslav Škarvada | 2b82c1c | 2015-11-11 16:02:54 +0100 | [diff] [blame] | 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 25 | * MA 02110-1301, USA. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #include <fcntl.h> |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 29 | #include <stdio.h> |
Harald Welte | ea91a51 | 2017-07-13 14:28:30 +0200 | [diff] [blame] | 30 | #include <unistd.h> |
Harald Welte | 9e166e8 | 2014-03-10 17:28:33 +0100 | [diff] [blame] | 31 | #include <string.h> |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 32 | #include <stdbool.h> |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 33 | #include <errno.h> |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 34 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 35 | #include <osmocom/core/select.h> |
| 36 | #include <osmocom/core/linuxlist.h> |
| 37 | #include <osmocom/core/timer.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 38 | |
Harald Welte | 5484480 | 2010-02-20 22:23:08 +0100 | [diff] [blame] | 39 | #include "../config.h" |
| 40 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 41 | #ifdef HAVE_SYS_SELECT_H |
Harald Welte | 1d604d8 | 2017-05-17 15:32:35 +0100 | [diff] [blame] | 42 | #include <sys/select.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 43 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 44 | /*! \addtogroup select |
| 45 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 46 | * select() loop abstraction |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 47 | * |
| 48 | * \file select.c */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 49 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 50 | static int maxfd = 0; |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 51 | static LLIST_HEAD(osmo_fds); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 52 | static int unregistered_count; |
| 53 | |
Harald Welte | 6c0a0e6 | 2017-08-12 11:43:14 +0200 | [diff] [blame] | 54 | /*! Set up an osmo-fd. Will not register it. |
| 55 | * \param[inout] ofd Osmo FD to be set-up |
| 56 | * \param[in] fd OS-level file descriptor number |
| 57 | * \param[in] when bit-mask of BSC_FD_{READ,WRITE,EXECEPT} |
| 58 | * \param[in] cb Call-back function to be called |
| 59 | * \param[in] data Private context pointer |
| 60 | * \param[in] priv_nr Private number |
| 61 | */ |
| 62 | void osmo_fd_setup(struct osmo_fd *ofd, int fd, unsigned int when, |
| 63 | int (*cb)(struct osmo_fd *fd, unsigned int what), |
| 64 | void *data, unsigned int priv_nr) |
| 65 | { |
| 66 | ofd->fd = fd; |
| 67 | ofd->when = when; |
| 68 | ofd->cb = cb; |
| 69 | ofd->data = data; |
| 70 | ofd->priv_nr = priv_nr; |
| 71 | } |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 72 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 73 | /*! Check if a file descriptor is already registered |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 74 | * \param[in] fd osmocom file descriptor to be checked |
| 75 | * \returns true if registered; otherwise false |
| 76 | */ |
| 77 | bool osmo_fd_is_registered(struct osmo_fd *fd) |
| 78 | { |
| 79 | struct osmo_fd *entry; |
| 80 | llist_for_each_entry(entry, &osmo_fds, list) { |
| 81 | if (entry == fd) { |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return false; |
| 87 | } |
| 88 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 89 | /*! Register a new file descriptor with select loop abstraction |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 90 | * \param[in] fd osmocom file descriptor to be registered |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 91 | * \returns 0 on success; negative in case of error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 92 | */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 93 | int osmo_fd_register(struct osmo_fd *fd) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 94 | { |
| 95 | int flags; |
| 96 | |
| 97 | /* make FD nonblocking */ |
| 98 | flags = fcntl(fd->fd, F_GETFL); |
| 99 | if (flags < 0) |
| 100 | return flags; |
| 101 | flags |= O_NONBLOCK; |
| 102 | flags = fcntl(fd->fd, F_SETFL, flags); |
| 103 | if (flags < 0) |
| 104 | return flags; |
| 105 | |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 106 | /* set close-on-exec flag */ |
| 107 | flags = fcntl(fd->fd, F_GETFD); |
| 108 | if (flags < 0) |
| 109 | return flags; |
| 110 | flags |= FD_CLOEXEC; |
| 111 | flags = fcntl(fd->fd, F_SETFD, flags); |
| 112 | if (flags < 0) |
| 113 | return flags; |
| 114 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 115 | /* Register FD */ |
| 116 | if (fd->fd > maxfd) |
| 117 | maxfd = fd->fd; |
| 118 | |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 119 | #ifdef BSC_FD_CHECK |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 120 | if (osmo_fd_is_registered(fd)) { |
| 121 | fprintf(stderr, "Adding a osmo_fd that is already in the list.\n"); |
| 122 | return 0; |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 123 | } |
| 124 | #endif |
| 125 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 126 | llist_add_tail(&fd->list, &osmo_fds); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 131 | /*! Unregister a file descriptor from select loop abstraction |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 132 | * \param[in] fd osmocom file descriptor to be unregistered |
| 133 | */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 134 | void osmo_fd_unregister(struct osmo_fd *fd) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 135 | { |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 136 | /* Note: when fd is inside the osmo_fds list (not registered before) |
| 137 | * this function will crash! If in doubt, check file descriptor with |
| 138 | * osmo_fd_is_registered() */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 139 | unregistered_count++; |
| 140 | llist_del(&fd->list); |
| 141 | } |
| 142 | |
Harald Welte | ea91a51 | 2017-07-13 14:28:30 +0200 | [diff] [blame] | 143 | /*! Close a file descriptor, mark it as closed + unregister from select loop abstraction |
| 144 | * \param[in] fd osmocom file descriptor to be unregistered + closed |
| 145 | * |
| 146 | * If \a fd is registered, we unregister it from the select() loop |
| 147 | * abstraction. We then close the fd and set it to -1, as well as |
| 148 | * unsetting any 'when' flags */ |
| 149 | void osmo_fd_close(struct osmo_fd *fd) |
| 150 | { |
| 151 | if (osmo_fd_is_registered(fd)) |
| 152 | osmo_fd_unregister(fd); |
| 153 | if (fd->fd != -1) |
| 154 | close(fd->fd); |
| 155 | fd->fd = -1; |
| 156 | fd->when = 0; |
| 157 | } |
| 158 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 159 | /*! Populate the fd_sets and return the highest fd number |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 160 | * \param[in] _rset The readfds to populate |
| 161 | * \param[in] _wset The wrtiefds to populate |
| 162 | * \param[in] _eset The errorfds to populate |
| 163 | * |
| 164 | * \returns The highest file descriptor seen or 0 on an empty list |
| 165 | */ |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 166 | inline int osmo_fd_fill_fds(void *_rset, void *_wset, void *_eset) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 167 | { |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 168 | fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset; |
| 169 | struct osmo_fd *ufd; |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 170 | int highfd = 0; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 171 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 172 | llist_for_each_entry(ufd, &osmo_fds, list) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 173 | if (ufd->when & BSC_FD_READ) |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 174 | FD_SET(ufd->fd, readset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 175 | |
| 176 | if (ufd->when & BSC_FD_WRITE) |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 177 | FD_SET(ufd->fd, writeset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 178 | |
| 179 | if (ufd->when & BSC_FD_EXCEPT) |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 180 | FD_SET(ufd->fd, exceptset); |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 181 | |
| 182 | if (ufd->fd > highfd) |
| 183 | highfd = ufd->fd; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Holger Hans Peter Freyther | ff20641 | 2017-03-07 14:28:38 +0100 | [diff] [blame] | 186 | return highfd; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 187 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 188 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 189 | inline int osmo_fd_disp_fds(void *_rset, void *_wset, void *_eset) |
| 190 | { |
| 191 | struct osmo_fd *ufd, *tmp; |
| 192 | int work = 0; |
| 193 | fd_set *readset = _rset, *writeset = _wset, *exceptset = _eset; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 194 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 195 | restart: |
| 196 | unregistered_count = 0; |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 197 | llist_for_each_entry_safe(ufd, tmp, &osmo_fds, list) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 198 | int flags = 0; |
| 199 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 200 | if (FD_ISSET(ufd->fd, readset)) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 201 | flags |= BSC_FD_READ; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 202 | FD_CLR(ufd->fd, readset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 205 | if (FD_ISSET(ufd->fd, writeset)) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 206 | flags |= BSC_FD_WRITE; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 207 | FD_CLR(ufd->fd, writeset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 210 | if (FD_ISSET(ufd->fd, exceptset)) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 211 | flags |= BSC_FD_EXCEPT; |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 212 | FD_CLR(ufd->fd, exceptset); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | if (flags) { |
| 216 | work = 1; |
| 217 | ufd->cb(ufd, flags); |
| 218 | } |
Holger Hans Peter Freyther | 92e1e70 | 2014-04-09 17:24:00 +0200 | [diff] [blame] | 219 | /* ugly, ugly hack. If more than one filedescriptor was |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 220 | * unregistered, they might have been consecutive and |
| 221 | * llist_for_each_entry_safe() is no longer safe */ |
Holger Hans Peter Freyther | 23ba474 | 2010-04-11 17:33:19 +0200 | [diff] [blame] | 222 | /* this seems to happen with the last element of the list as well */ |
| 223 | if (unregistered_count >= 1) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 224 | goto restart; |
| 225 | } |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 226 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 227 | return work; |
| 228 | } |
| 229 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 230 | /*! select main loop integration |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 231 | * \param[in] polling should we pollonly (1) or block on select (0) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 232 | * \returns 0 if no fd handled; 1 if fd handled; negative in case of error |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 233 | */ |
| 234 | int osmo_select_main(int polling) |
| 235 | { |
| 236 | fd_set readset, writeset, exceptset; |
| 237 | int rc; |
| 238 | struct timeval no_time = {0, 0}; |
| 239 | |
| 240 | FD_ZERO(&readset); |
| 241 | FD_ZERO(&writeset); |
| 242 | FD_ZERO(&exceptset); |
| 243 | |
| 244 | /* prepare read and write fdsets */ |
| 245 | osmo_fd_fill_fds(&readset, &writeset, &exceptset); |
| 246 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 247 | if (!polling) |
| 248 | osmo_timers_prepare(); |
| 249 | rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : osmo_timers_nearest()); |
| 250 | if (rc < 0) |
| 251 | return 0; |
| 252 | |
| 253 | /* fire timers */ |
| 254 | osmo_timers_update(); |
| 255 | |
| 256 | /* call registered callback functions */ |
| 257 | return osmo_fd_disp_fds(&readset, &writeset, &exceptset); |
| 258 | } |
| 259 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 260 | /*! find an osmo_fd based on the integer fd |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 261 | * \param[in] fd file descriptor to use as search key |
| 262 | * \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] | 263 | struct osmo_fd *osmo_fd_get_by_fd(int fd) |
| 264 | { |
| 265 | struct osmo_fd *ofd; |
| 266 | |
| 267 | llist_for_each_entry(ofd, &osmo_fds, list) { |
| 268 | if (ofd->fd == fd) |
| 269 | return ofd; |
| 270 | } |
| 271 | return NULL; |
| 272 | } |
| 273 | |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 274 | #ifdef HAVE_SYS_TIMERFD_H |
| 275 | #include <sys/timerfd.h> |
| 276 | |
| 277 | /*! disable the osmocom-wrapped timerfd */ |
| 278 | int osmo_timerfd_disable(struct osmo_fd *ofd) |
| 279 | { |
| 280 | const struct itimerspec its_null = { |
| 281 | .it_value = { 0, 0 }, |
| 282 | .it_interval = { 0, 0 }, |
| 283 | }; |
| 284 | return timerfd_settime(ofd->fd, 0, &its_null, NULL); |
| 285 | } |
| 286 | |
| 287 | /*! schedule the osmcoom-wrapped timerfd to occur first at \a first, then periodically at \a interval |
| 288 | * \param[in] ofd Osmocom wrapped timerfd |
| 289 | * \param[in] first Relative time at which the timer should first execute (NULL = \a interval) |
| 290 | * \param[in] interval Time interval at which subsequent timer shall fire |
| 291 | * \returns 0 on success; negative on error */ |
| 292 | int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first, |
| 293 | const struct timespec *interval) |
| 294 | { |
| 295 | struct itimerspec its; |
| 296 | |
| 297 | if (ofd->fd < 0) |
| 298 | return -EINVAL; |
| 299 | |
| 300 | /* first expiration */ |
| 301 | if (first) |
| 302 | its.it_value = *first; |
| 303 | else |
| 304 | its.it_value = *interval; |
| 305 | /* repeating interval */ |
| 306 | its.it_interval = *interval; |
| 307 | |
| 308 | return timerfd_settime(ofd->fd, 0, &its, NULL); |
| 309 | } |
| 310 | |
| 311 | /*! setup osmocom-wrapped timerfd |
| 312 | * \param[inout] ofd Osmocom-wrapped timerfd on which to operate |
| 313 | * \param[in] cb Call-back function called when timerfd becomes readable |
| 314 | * \param[in] data Opaque data to be passed on to call-back |
| 315 | * \returns 0 on success; negative on error |
| 316 | * |
| 317 | * We simply initialize the data structures here, but do not yet |
| 318 | * schedule the timer. |
| 319 | */ |
| 320 | int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data) |
| 321 | { |
| 322 | ofd->cb = cb; |
| 323 | ofd->data = data; |
| 324 | ofd->when = BSC_FD_READ; |
| 325 | |
| 326 | if (ofd->fd < 0) { |
| 327 | ofd->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); |
| 328 | if (ofd->fd < 0) |
| 329 | return ofd->fd; |
| 330 | |
| 331 | osmo_fd_register(ofd); |
| 332 | } |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | #endif /* HAVE_SYS_TIMERFD_H */ |
| 337 | |
| 338 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 339 | /*! @} */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 340 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 341 | #endif /* _HAVE_SYS_SELECT_H */ |