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> |
| 24 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 25 | #include <osmocom/core/select.h> |
| 26 | #include <osmocom/core/linuxlist.h> |
| 27 | #include <osmocom/core/timer.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 28 | |
Harald Welte | 5484480 | 2010-02-20 22:23:08 +0100 | [diff] [blame] | 29 | #include "../config.h" |
| 30 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 31 | #ifdef HAVE_SYS_SELECT_H |
| 32 | |
| 33 | static int maxfd = 0; |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 34 | static LLIST_HEAD(osmo_fds); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 35 | static int unregistered_count; |
| 36 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 37 | int osmo_fd_register(struct osmo_fd *fd) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 38 | { |
| 39 | int flags; |
| 40 | |
| 41 | /* make FD nonblocking */ |
| 42 | flags = fcntl(fd->fd, F_GETFL); |
| 43 | if (flags < 0) |
| 44 | return flags; |
| 45 | flags |= O_NONBLOCK; |
| 46 | flags = fcntl(fd->fd, F_SETFL, flags); |
| 47 | if (flags < 0) |
| 48 | return flags; |
| 49 | |
| 50 | /* Register FD */ |
| 51 | if (fd->fd > maxfd) |
| 52 | maxfd = fd->fd; |
| 53 | |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 54 | #ifdef BSC_FD_CHECK |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 55 | struct osmo_fd *entry; |
| 56 | llist_for_each_entry(entry, &osmo_fds, list) { |
Holger Hans Peter Freyther | 4355831 | 2010-08-06 06:48:43 +0800 | [diff] [blame] | 57 | if (entry == fd) { |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 58 | 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] | 59 | return 0; |
| 60 | } |
| 61 | } |
| 62 | #endif |
| 63 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 64 | llist_add_tail(&fd->list, &osmo_fds); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 69 | void osmo_fd_unregister(struct osmo_fd *fd) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 70 | { |
| 71 | unregistered_count++; |
| 72 | llist_del(&fd->list); |
| 73 | } |
| 74 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 75 | int osmo_select_main(int polling) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 76 | { |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 77 | struct osmo_fd *ufd, *tmp; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 78 | fd_set readset, writeset, exceptset; |
| 79 | int work = 0, rc; |
| 80 | struct timeval no_time = {0, 0}; |
| 81 | |
| 82 | FD_ZERO(&readset); |
| 83 | FD_ZERO(&writeset); |
| 84 | FD_ZERO(&exceptset); |
| 85 | |
| 86 | /* prepare read and write fdsets */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 87 | llist_for_each_entry(ufd, &osmo_fds, list) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 88 | if (ufd->when & BSC_FD_READ) |
| 89 | FD_SET(ufd->fd, &readset); |
| 90 | |
| 91 | if (ufd->when & BSC_FD_WRITE) |
| 92 | FD_SET(ufd->fd, &writeset); |
| 93 | |
| 94 | if (ufd->when & BSC_FD_EXCEPT) |
| 95 | FD_SET(ufd->fd, &exceptset); |
| 96 | } |
| 97 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 98 | osmo_timers_check(); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 99 | |
| 100 | if (!polling) |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 101 | osmo_timers_prepare(); |
| 102 | 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] | 103 | if (rc < 0) |
| 104 | return 0; |
| 105 | |
| 106 | /* fire timers */ |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 107 | osmo_timers_update(); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 108 | |
| 109 | /* call registered callback functions */ |
| 110 | restart: |
| 111 | unregistered_count = 0; |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 112 | llist_for_each_entry_safe(ufd, tmp, &osmo_fds, list) { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 113 | int flags = 0; |
| 114 | |
| 115 | if (FD_ISSET(ufd->fd, &readset)) { |
| 116 | flags |= BSC_FD_READ; |
| 117 | FD_CLR(ufd->fd, &readset); |
| 118 | } |
| 119 | |
| 120 | if (FD_ISSET(ufd->fd, &writeset)) { |
| 121 | flags |= BSC_FD_WRITE; |
| 122 | FD_CLR(ufd->fd, &writeset); |
| 123 | } |
| 124 | |
| 125 | if (FD_ISSET(ufd->fd, &exceptset)) { |
| 126 | flags |= BSC_FD_EXCEPT; |
| 127 | FD_CLR(ufd->fd, &exceptset); |
| 128 | } |
| 129 | |
| 130 | if (flags) { |
| 131 | work = 1; |
| 132 | ufd->cb(ufd, flags); |
| 133 | } |
| 134 | /* ugly, ugly hack. If more than one filedescriptors were |
| 135 | * unregistered, they might have been consecutive and |
| 136 | * llist_for_each_entry_safe() is no longer safe */ |
Holger Hans Peter Freyther | 23ba474 | 2010-04-11 17:33:19 +0200 | [diff] [blame] | 137 | /* this seems to happen with the last element of the list as well */ |
| 138 | if (unregistered_count >= 1) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 139 | goto restart; |
| 140 | } |
| 141 | return work; |
| 142 | } |
| 143 | |
| 144 | #endif /* _HAVE_SYS_SELECT_H */ |