Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [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 | * |
Harald Welte | dc55db9 | 2009-02-03 12:58:59 +0000 | [diff] [blame] | 5 | * (C) 2000-2009 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 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 <openbsc/select.h> |
| 23 | #include <openbsc/linuxlist.h> |
Holger Freyther | 5f75598 | 2008-12-27 09:42:59 +0000 | [diff] [blame] | 24 | #include <openbsc/timer.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 25 | |
| 26 | static int maxfd = 0; |
| 27 | static LLIST_HEAD(bsc_fds); |
Harald Welte | 709f2fd | 2009-07-28 13:32:00 +0200 | [diff] [blame] | 28 | static int unregistered_count; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 29 | |
| 30 | int bsc_register_fd(struct bsc_fd *fd) |
| 31 | { |
| 32 | int flags; |
| 33 | |
| 34 | /* make FD nonblocking */ |
| 35 | flags = fcntl(fd->fd, F_GETFL); |
| 36 | if (flags < 0) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 37 | return flags; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 38 | flags |= O_NONBLOCK; |
| 39 | flags = fcntl(fd->fd, F_SETFL, flags); |
| 40 | if (flags < 0) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 41 | return flags; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 42 | |
| 43 | /* Register FD */ |
| 44 | if (fd->fd > maxfd) |
| 45 | maxfd = fd->fd; |
| 46 | |
| 47 | llist_add_tail(&fd->list, &bsc_fds); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | void bsc_unregister_fd(struct bsc_fd *fd) |
| 53 | { |
Harald Welte | 709f2fd | 2009-07-28 13:32:00 +0200 | [diff] [blame] | 54 | unregistered_count++; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 55 | llist_del(&fd->list); |
| 56 | } |
| 57 | |
Harald Welte | 04d3c92 | 2009-05-23 06:07:04 +0000 | [diff] [blame] | 58 | int bsc_select_main(int polling) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 59 | { |
Holger Freyther | c6880a4 | 2009-01-02 21:53:34 +0000 | [diff] [blame] | 60 | struct bsc_fd *ufd, *tmp; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 61 | fd_set readset, writeset, exceptset; |
Harald Welte | 04d3c92 | 2009-05-23 06:07:04 +0000 | [diff] [blame] | 62 | int work = 0, rc; |
| 63 | struct timeval no_time = {0, 0}; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 64 | |
| 65 | FD_ZERO(&readset); |
| 66 | FD_ZERO(&writeset); |
| 67 | FD_ZERO(&exceptset); |
| 68 | |
| 69 | /* prepare read and write fdsets */ |
| 70 | llist_for_each_entry(ufd, &bsc_fds, list) { |
| 71 | if (ufd->when & BSC_FD_READ) |
| 72 | FD_SET(ufd->fd, &readset); |
| 73 | |
| 74 | if (ufd->when & BSC_FD_WRITE) |
| 75 | FD_SET(ufd->fd, &writeset); |
| 76 | |
| 77 | if (ufd->when & BSC_FD_EXCEPT) |
| 78 | FD_SET(ufd->fd, &exceptset); |
| 79 | } |
| 80 | |
Harald Welte (local) | fed176a | 2009-08-14 14:30:00 +0200 | [diff] [blame] | 81 | bsc_timer_check(); |
| 82 | |
Harald Welte | 04d3c92 | 2009-05-23 06:07:04 +0000 | [diff] [blame] | 83 | if (!polling) |
| 84 | bsc_prepare_timers(); |
| 85 | rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : bsc_nearest_timer()); |
| 86 | if (rc < 0) |
| 87 | return 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 88 | |
Holger Freyther | 5f75598 | 2008-12-27 09:42:59 +0000 | [diff] [blame] | 89 | /* fire timers */ |
Harald Welte | ff117a8 | 2009-05-23 05:22:08 +0000 | [diff] [blame] | 90 | bsc_update_timers(); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 91 | |
Holger Freyther | 5f75598 | 2008-12-27 09:42:59 +0000 | [diff] [blame] | 92 | /* call registered callback functions */ |
Harald Welte | 709f2fd | 2009-07-28 13:32:00 +0200 | [diff] [blame] | 93 | restart: |
| 94 | unregistered_count = 0; |
Holger Freyther | c6880a4 | 2009-01-02 21:53:34 +0000 | [diff] [blame] | 95 | llist_for_each_entry_safe(ufd, tmp, &bsc_fds, list) { |
Harald Welte | dc55db9 | 2009-02-03 12:58:59 +0000 | [diff] [blame] | 96 | int flags = 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 97 | |
Harald Welte | dc55db9 | 2009-02-03 12:58:59 +0000 | [diff] [blame] | 98 | if (FD_ISSET(ufd->fd, &readset)) |
| 99 | flags |= BSC_FD_READ; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 100 | |
Harald Welte | dc55db9 | 2009-02-03 12:58:59 +0000 | [diff] [blame] | 101 | if (FD_ISSET(ufd->fd, &writeset)) |
| 102 | flags |= BSC_FD_WRITE; |
Holger Freyther | 5f75598 | 2008-12-27 09:42:59 +0000 | [diff] [blame] | 103 | |
Harald Welte | dc55db9 | 2009-02-03 12:58:59 +0000 | [diff] [blame] | 104 | if (FD_ISSET(ufd->fd, &exceptset)) |
| 105 | flags |= BSC_FD_EXCEPT; |
Holger Freyther | 5f75598 | 2008-12-27 09:42:59 +0000 | [diff] [blame] | 106 | |
Harald Welte | 04d3c92 | 2009-05-23 06:07:04 +0000 | [diff] [blame] | 107 | if (flags) { |
| 108 | work = 1; |
Harald Welte | dc55db9 | 2009-02-03 12:58:59 +0000 | [diff] [blame] | 109 | ufd->cb(ufd, flags); |
Harald Welte | 04d3c92 | 2009-05-23 06:07:04 +0000 | [diff] [blame] | 110 | } |
Harald Welte | 709f2fd | 2009-07-28 13:32:00 +0200 | [diff] [blame] | 111 | /* ugly, ugly hack. If more than one filedescriptors were |
| 112 | * unregistered, they might have been consecutive and |
| 113 | * llist_for_each_entry_safe() is no longer safe */ |
| 114 | if (unregistered_count > 1) |
| 115 | goto restart; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 116 | } |
Harald Welte | 04d3c92 | 2009-05-23 06:07:04 +0000 | [diff] [blame] | 117 | return work; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 118 | } |