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 |
| 8 | * it under the terms of the GNU General Public License version 2 |
| 9 | * as published by the Free Software Foundation |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <fcntl.h> |
| 22 | #include <osmocore/select.h> |
| 23 | #include <osmocore/linuxlist.h> |
| 24 | #include <osmocore/timer.h> |
| 25 | |
| 26 | #ifdef HAVE_SYS_SELECT_H |
| 27 | |
| 28 | static int maxfd = 0; |
| 29 | static LLIST_HEAD(bsc_fds); |
| 30 | static int unregistered_count; |
| 31 | |
| 32 | int bsc_register_fd(struct bsc_fd *fd) |
| 33 | { |
| 34 | int flags; |
| 35 | |
| 36 | /* make FD nonblocking */ |
| 37 | flags = fcntl(fd->fd, F_GETFL); |
| 38 | if (flags < 0) |
| 39 | return flags; |
| 40 | flags |= O_NONBLOCK; |
| 41 | flags = fcntl(fd->fd, F_SETFL, flags); |
| 42 | if (flags < 0) |
| 43 | return flags; |
| 44 | |
| 45 | /* Register FD */ |
| 46 | if (fd->fd > maxfd) |
| 47 | maxfd = fd->fd; |
| 48 | |
| 49 | llist_add_tail(&fd->list, &bsc_fds); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | void bsc_unregister_fd(struct bsc_fd *fd) |
| 55 | { |
| 56 | unregistered_count++; |
| 57 | llist_del(&fd->list); |
| 58 | } |
| 59 | |
| 60 | int bsc_select_main(int polling) |
| 61 | { |
| 62 | struct bsc_fd *ufd, *tmp; |
| 63 | fd_set readset, writeset, exceptset; |
| 64 | int work = 0, rc; |
| 65 | struct timeval no_time = {0, 0}; |
| 66 | |
| 67 | FD_ZERO(&readset); |
| 68 | FD_ZERO(&writeset); |
| 69 | FD_ZERO(&exceptset); |
| 70 | |
| 71 | /* prepare read and write fdsets */ |
| 72 | llist_for_each_entry(ufd, &bsc_fds, list) { |
| 73 | if (ufd->when & BSC_FD_READ) |
| 74 | FD_SET(ufd->fd, &readset); |
| 75 | |
| 76 | if (ufd->when & BSC_FD_WRITE) |
| 77 | FD_SET(ufd->fd, &writeset); |
| 78 | |
| 79 | if (ufd->when & BSC_FD_EXCEPT) |
| 80 | FD_SET(ufd->fd, &exceptset); |
| 81 | } |
| 82 | |
| 83 | bsc_timer_check(); |
| 84 | |
| 85 | if (!polling) |
| 86 | bsc_prepare_timers(); |
| 87 | rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : bsc_nearest_timer()); |
| 88 | if (rc < 0) |
| 89 | return 0; |
| 90 | |
| 91 | /* fire timers */ |
| 92 | bsc_update_timers(); |
| 93 | |
| 94 | /* call registered callback functions */ |
| 95 | restart: |
| 96 | unregistered_count = 0; |
| 97 | llist_for_each_entry_safe(ufd, tmp, &bsc_fds, list) { |
| 98 | int flags = 0; |
| 99 | |
| 100 | if (FD_ISSET(ufd->fd, &readset)) { |
| 101 | flags |= BSC_FD_READ; |
| 102 | FD_CLR(ufd->fd, &readset); |
| 103 | } |
| 104 | |
| 105 | if (FD_ISSET(ufd->fd, &writeset)) { |
| 106 | flags |= BSC_FD_WRITE; |
| 107 | FD_CLR(ufd->fd, &writeset); |
| 108 | } |
| 109 | |
| 110 | if (FD_ISSET(ufd->fd, &exceptset)) { |
| 111 | flags |= BSC_FD_EXCEPT; |
| 112 | FD_CLR(ufd->fd, &exceptset); |
| 113 | } |
| 114 | |
| 115 | if (flags) { |
| 116 | work = 1; |
| 117 | ufd->cb(ufd, flags); |
| 118 | } |
| 119 | /* ugly, ugly hack. If more than one filedescriptors were |
| 120 | * unregistered, they might have been consecutive and |
| 121 | * llist_for_each_entry_safe() is no longer safe */ |
| 122 | if (unregistered_count > 1) |
| 123 | goto restart; |
| 124 | } |
| 125 | return work; |
| 126 | } |
| 127 | |
| 128 | #endif /* _HAVE_SYS_SELECT_H */ |