blob: 925f3395f3cd8c7bc678ff4c9309f1098f86188a [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* select filedescriptor handling, taken from:
2 * userspace logging daemon for the iptables ULOG target
3 * of the linux 2.4 netfilter subsystem.
4 *
Harald Weltedc55db92009-02-03 12:58:59 +00005 * (C) 2000-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte52b1f982008-12-23 20:25:15 +00006 *
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 Freyther5f755982008-12-27 09:42:59 +000024#include <openbsc/timer.h>
Harald Welte52b1f982008-12-23 20:25:15 +000025
26static int maxfd = 0;
27static LLIST_HEAD(bsc_fds);
28
29int bsc_register_fd(struct bsc_fd *fd)
30{
31 int flags;
32
33 /* make FD nonblocking */
34 flags = fcntl(fd->fd, F_GETFL);
35 if (flags < 0)
Harald Welte8470bf22008-12-25 23:28:35 +000036 return flags;
Harald Welte52b1f982008-12-23 20:25:15 +000037 flags |= O_NONBLOCK;
38 flags = fcntl(fd->fd, F_SETFL, flags);
39 if (flags < 0)
Harald Welte8470bf22008-12-25 23:28:35 +000040 return flags;
Harald Welte52b1f982008-12-23 20:25:15 +000041
42 /* Register FD */
43 if (fd->fd > maxfd)
44 maxfd = fd->fd;
45
46 llist_add_tail(&fd->list, &bsc_fds);
47
48 return 0;
49}
50
51void bsc_unregister_fd(struct bsc_fd *fd)
52{
53 llist_del(&fd->list);
54}
55
56int bsc_select_main()
57{
Holger Freytherc6880a42009-01-02 21:53:34 +000058 struct bsc_fd *ufd, *tmp;
Harald Welte52b1f982008-12-23 20:25:15 +000059 fd_set readset, writeset, exceptset;
60 int i;
61
62 FD_ZERO(&readset);
63 FD_ZERO(&writeset);
64 FD_ZERO(&exceptset);
65
66 /* prepare read and write fdsets */
67 llist_for_each_entry(ufd, &bsc_fds, list) {
68 if (ufd->when & BSC_FD_READ)
69 FD_SET(ufd->fd, &readset);
70
71 if (ufd->when & BSC_FD_WRITE)
72 FD_SET(ufd->fd, &writeset);
73
74 if (ufd->when & BSC_FD_EXCEPT)
75 FD_SET(ufd->fd, &exceptset);
76 }
77
Holger Freyther5f755982008-12-27 09:42:59 +000078 prepare_timers();
79 i = select(maxfd+1, &readset, &writeset, &exceptset, nearest_timer());
80 if (i < 0)
81 return i;
Harald Welte52b1f982008-12-23 20:25:15 +000082
Holger Freyther5f755982008-12-27 09:42:59 +000083 /* fire timers */
84 update_timers();
Harald Welte52b1f982008-12-23 20:25:15 +000085
Holger Freyther5f755982008-12-27 09:42:59 +000086 /* call registered callback functions */
Holger Freytherc6880a42009-01-02 21:53:34 +000087 llist_for_each_entry_safe(ufd, tmp, &bsc_fds, list) {
Harald Weltedc55db92009-02-03 12:58:59 +000088 int flags = 0;
Harald Welte52b1f982008-12-23 20:25:15 +000089
Harald Weltedc55db92009-02-03 12:58:59 +000090 if (FD_ISSET(ufd->fd, &readset))
91 flags |= BSC_FD_READ;
Harald Welte52b1f982008-12-23 20:25:15 +000092
Harald Weltedc55db92009-02-03 12:58:59 +000093 if (FD_ISSET(ufd->fd, &writeset))
94 flags |= BSC_FD_WRITE;
Holger Freyther5f755982008-12-27 09:42:59 +000095
Harald Weltedc55db92009-02-03 12:58:59 +000096 if (FD_ISSET(ufd->fd, &exceptset))
97 flags |= BSC_FD_EXCEPT;
Holger Freyther5f755982008-12-27 09:42:59 +000098
Harald Weltedc55db92009-02-03 12:58:59 +000099 if (flags)
100 ufd->cb(ufd, flags);
Harald Welte52b1f982008-12-23 20:25:15 +0000101 }
102 return i;
103}