Merge commit '47ee693170d589f760c4a9c7a5c4ad0b289aa65d'
[osmocom-bb.git] / src / shared / libosmocore / src / select.c
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 <stdio.h>
23
24 #include <osmocore/select.h>
25 #include <osmocore/linuxlist.h>
26 #include <osmocore/timer.h>
27
28 #include "../config.h"
29
30 #ifdef HAVE_SYS_SELECT_H
31
32 static int maxfd = 0;
33 static LLIST_HEAD(bsc_fds);
34 static int unregistered_count;
35
36 int bsc_register_fd(struct bsc_fd *fd)
37 {
38         int flags;
39
40         /* make FD nonblocking */
41         flags = fcntl(fd->fd, F_GETFL);
42         if (flags < 0)
43                 return flags;
44         flags |= O_NONBLOCK;
45         flags = fcntl(fd->fd, F_SETFL, flags);
46         if (flags < 0)
47                 return flags;
48
49         /* Register FD */
50         if (fd->fd > maxfd)
51                 maxfd = fd->fd;
52
53 #ifdef BSC_FD_CHECK
54         struct bsc_fd *entry;
55         llist_for_each_entry(entry, &bsc_fds, list) {
56                 if (entry == fd) {
57                         fprintf(stderr, "Adding a bsc_fd that is already in the list.\n");
58                         return 0;
59                 }
60         }
61 #endif
62
63         llist_add_tail(&fd->list, &bsc_fds);
64
65         return 0;
66 }
67
68 void bsc_unregister_fd(struct bsc_fd *fd)
69 {
70         unregistered_count++;
71         llist_del(&fd->list);
72 }
73
74 int bsc_select_main(int polling)
75 {
76         struct bsc_fd *ufd, *tmp;
77         fd_set readset, writeset, exceptset;
78         int work = 0, rc;
79         struct timeval no_time = {0, 0};
80
81         FD_ZERO(&readset);
82         FD_ZERO(&writeset);
83         FD_ZERO(&exceptset);
84
85         /* prepare read and write fdsets */
86         llist_for_each_entry(ufd, &bsc_fds, list) {
87                 if (ufd->when & BSC_FD_READ)
88                         FD_SET(ufd->fd, &readset);
89
90                 if (ufd->when & BSC_FD_WRITE)
91                         FD_SET(ufd->fd, &writeset);
92
93                 if (ufd->when & BSC_FD_EXCEPT)
94                         FD_SET(ufd->fd, &exceptset);
95         }
96
97         bsc_timer_check();
98
99         if (!polling)
100                 bsc_prepare_timers();
101         rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : bsc_nearest_timer());
102         if (rc < 0)
103                 return 0;
104
105         /* fire timers */
106         bsc_update_timers();
107
108         /* call registered callback functions */
109 restart:
110         unregistered_count = 0;
111         llist_for_each_entry_safe(ufd, tmp, &bsc_fds, list) {
112                 int flags = 0;
113
114                 if (FD_ISSET(ufd->fd, &readset)) {
115                         flags |= BSC_FD_READ;
116                         FD_CLR(ufd->fd, &readset);
117                 }
118
119                 if (FD_ISSET(ufd->fd, &writeset)) {
120                         flags |= BSC_FD_WRITE;
121                         FD_CLR(ufd->fd, &writeset);
122                 }
123
124                 if (FD_ISSET(ufd->fd, &exceptset)) {
125                         flags |= BSC_FD_EXCEPT;
126                         FD_CLR(ufd->fd, &exceptset);
127                 }
128
129                 if (flags) {
130                         work = 1;
131                         ufd->cb(ufd, flags);
132                 }
133                 /* ugly, ugly hack. If more than one filedescriptors were
134                  * unregistered, they might have been consecutive and
135                  * llist_for_each_entry_safe() is no longer safe */
136                 /* this seems to happen with the last element of the list as well */
137                 if (unregistered_count >= 1)
138                         goto restart;
139         }
140         return work;
141 }
142
143 #endif /* _HAVE_SYS_SELECT_H */