Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | /* select filedescriptor handling, taken from: |
| 2 | * userspace logging daemon for the iptables ULOG target |
| 3 | * of the linux 2.4 netfilter subsystem. |
| 4 | * |
| 5 | * (C) 2000-2009 by Harald Welte <laforge@gnumonks.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9d92f0e | 2010-10-31 13:56:45 +0100 | [diff] [blame] | 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include <fcntl.h> |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 23 | #include <stdio.h> |
Harald Welte | 9e166e8 | 2014-03-10 17:28:33 +0100 | [diff] [blame] | 24 | #include <string.h> |
| 25 | #include <sys/select.h> |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 26 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 27 | #include <osmocom/core/select.h> |
| 28 | #include <osmocom/core/linuxlist.h> |
| 29 | #include <osmocom/core/timer.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 30 | |
Harald Welte | 5484480 | 2010-02-20 22:23:08 +0100 | [diff] [blame] | 31 | #include "../config.h" |
| 32 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 33 | #ifdef HAVE_SYS_SELECT_H |
| 34 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 35 | /*! \addtogroup select |
| 36 | * @{ |
| 37 | */ |
| 38 | |
| 39 | /*! \file select.c |
| 40 | * \brief select loop abstraction |
| 41 | */ |
| 42 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 43 | static int maxfd = 0; |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 44 | static LLIST_HEAD(osmo_fds); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 45 | static int unregistered_count; |
| 46 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 47 | /*! \brief Register a new file descriptor with select loop abstraction |
| 48 | * \param[in] fd osmocom file descriptor to be registered |
| 49 | */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 50 | int osmo_fd_register(struct osmo_fd *fd) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 51 | { |
| 52 | int flags; |
| 53 | |
| 54 | /* make FD nonblocking */ |
| 55 | flags = fcntl(fd->fd, F_GETFL); |
| 56 | if (flags < 0) |
| 57 | return flags; |
| 58 | flags |= O_NONBLOCK; |
| 59 | flags = fcntl(fd->fd, F_SETFL, flags); |
| 60 | if (flags < 0) |
| 61 | return flags; |
| 62 | |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 63 | /* set close-on-exec flag */ |
| 64 | flags = fcntl(fd->fd, F_GETFD); |
| 65 | if (flags < 0) |
| 66 | return flags; |
| 67 | flags |= FD_CLOEXEC; |
| 68 | flags = fcntl(fd->fd, F_SETFD, flags); |
| 69 | if (flags < 0) |
| 70 | return flags; |
| 71 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 72 | /* Register FD */ |
| 73 | if (fd->fd > maxfd) |
| 74 | maxfd = fd->fd; |
| 75 | |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 76 | #ifdef BSC_FD_CHECK |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 77 | struct osmo_fd *entry; |
| 78 | llist_for_each_entry(entry, &osmo_fds, list) { |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 79 | if (entry == fd) { |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 80 | fprintf(stderr, "Adding a osmo_fd that is already in the list.\n"); |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | } |
| 84 | #endif |
| 85 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 86 | llist_add_tail(&fd->list, &osmo_fds); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 91 | /*! \brief Unregister a file descriptor from select loop abstraction |
| 92 | * \param[in] fd osmocom file descriptor to be unregistered |
| 93 | */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 94 | void osmo_fd_unregister(struct osmo_fd *fd) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 95 | { |
| 96 | unregistered_count++; |
| 97 | llist_del(&fd->list); |
| 98 | } |
| 99 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 100 | /*! \brief select main loop integration |
| 101 | * \param[in] polling should we pollonly (1) or block on select (0) |
| 102 | */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 103 | int osmo_select_main(int polling) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 104 | { |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 105 | struct osmo_fd *ufd, *tmp; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 106 | fd_set readset, writeset, exceptset; |
| 107 | int work = 0, rc; |
| 108 | struct timeval no_time = {0, 0}; |
| 109 | |
| 110 | FD_ZERO(&readset); |
| 111 | FD_ZERO(&writeset); |
| 112 | FD_ZERO(&exceptset); |
| 113 | |
| 114 | /* prepare read and write fdsets */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 115 | llist_for_each_entry(ufd, &osmo_fds, list) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 116 | if (ufd->when & BSC_FD_READ) |
| 117 | FD_SET(ufd->fd, &readset); |
| 118 | |
| 119 | if (ufd->when & BSC_FD_WRITE) |
| 120 | FD_SET(ufd->fd, &writeset); |
| 121 | |
| 122 | if (ufd->when & BSC_FD_EXCEPT) |
| 123 | FD_SET(ufd->fd, &exceptset); |
| 124 | } |
| 125 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 126 | osmo_timers_check(); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 127 | |
| 128 | if (!polling) |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 129 | osmo_timers_prepare(); |
| 130 | rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : osmo_timers_nearest()); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 131 | if (rc < 0) |
| 132 | return 0; |
| 133 | |
| 134 | /* fire timers */ |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 135 | osmo_timers_update(); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 136 | |
| 137 | /* call registered callback functions */ |
| 138 | restart: |
| 139 | unregistered_count = 0; |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 140 | llist_for_each_entry_safe(ufd, tmp, &osmo_fds, list) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 141 | int flags = 0; |
| 142 | |
| 143 | if (FD_ISSET(ufd->fd, &readset)) { |
| 144 | flags |= BSC_FD_READ; |
| 145 | FD_CLR(ufd->fd, &readset); |
| 146 | } |
| 147 | |
| 148 | if (FD_ISSET(ufd->fd, &writeset)) { |
| 149 | flags |= BSC_FD_WRITE; |
| 150 | FD_CLR(ufd->fd, &writeset); |
| 151 | } |
| 152 | |
| 153 | if (FD_ISSET(ufd->fd, &exceptset)) { |
| 154 | flags |= BSC_FD_EXCEPT; |
| 155 | FD_CLR(ufd->fd, &exceptset); |
| 156 | } |
| 157 | |
| 158 | if (flags) { |
| 159 | work = 1; |
| 160 | ufd->cb(ufd, flags); |
| 161 | } |
Holger Hans Peter Freyther | 92e1e70 | 2014-04-09 17:24:00 +0200 | [diff] [blame] | 162 | /* ugly, ugly hack. If more than one filedescriptor was |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 163 | * unregistered, they might have been consecutive and |
| 164 | * llist_for_each_entry_safe() is no longer safe */ |
Holger Hans Peter Freyther | 23ba474 | 2010-04-11 17:33:19 +0200 | [diff] [blame] | 165 | /* this seems to happen with the last element of the list as well */ |
| 166 | if (unregistered_count >= 1) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 167 | goto restart; |
| 168 | } |
| 169 | return work; |
| 170 | } |
| 171 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 172 | /*! @} */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 173 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 174 | #endif /* _HAVE_SYS_SELECT_H */ |