blob: c11f3a511b47b28018871ebf18ef6b010a15c0fe [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);
Harald Welte709f2fd2009-07-28 13:32:00 +020028static int unregistered_count;
Harald Welte52b1f982008-12-23 20:25:15 +000029
30int 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 Welte8470bf22008-12-25 23:28:35 +000037 return flags;
Harald Welte52b1f982008-12-23 20:25:15 +000038 flags |= O_NONBLOCK;
39 flags = fcntl(fd->fd, F_SETFL, flags);
40 if (flags < 0)
Harald Welte8470bf22008-12-25 23:28:35 +000041 return flags;
Harald Welte52b1f982008-12-23 20:25:15 +000042
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
52void bsc_unregister_fd(struct bsc_fd *fd)
53{
Harald Welte709f2fd2009-07-28 13:32:00 +020054 unregistered_count++;
Harald Welte52b1f982008-12-23 20:25:15 +000055 llist_del(&fd->list);
56}
57
Harald Welte04d3c922009-05-23 06:07:04 +000058int bsc_select_main(int polling)
Harald Welte52b1f982008-12-23 20:25:15 +000059{
Holger Freytherc6880a42009-01-02 21:53:34 +000060 struct bsc_fd *ufd, *tmp;
Harald Welte52b1f982008-12-23 20:25:15 +000061 fd_set readset, writeset, exceptset;
Harald Welte04d3c922009-05-23 06:07:04 +000062 int work = 0, rc;
63 struct timeval no_time = {0, 0};
Harald Welte52b1f982008-12-23 20:25:15 +000064
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)fed176a2009-08-14 14:30:00 +020081 bsc_timer_check();
82
Harald Welte04d3c922009-05-23 06:07:04 +000083 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 Welte52b1f982008-12-23 20:25:15 +000088
Holger Freyther5f755982008-12-27 09:42:59 +000089 /* fire timers */
Harald Welteff117a82009-05-23 05:22:08 +000090 bsc_update_timers();
Harald Welte52b1f982008-12-23 20:25:15 +000091
Holger Freyther5f755982008-12-27 09:42:59 +000092 /* call registered callback functions */
Harald Welte709f2fd2009-07-28 13:32:00 +020093restart:
94 unregistered_count = 0;
Holger Freytherc6880a42009-01-02 21:53:34 +000095 llist_for_each_entry_safe(ufd, tmp, &bsc_fds, list) {
Harald Weltedc55db92009-02-03 12:58:59 +000096 int flags = 0;
Harald Welte52b1f982008-12-23 20:25:15 +000097
Harald Weltedc55db92009-02-03 12:58:59 +000098 if (FD_ISSET(ufd->fd, &readset))
99 flags |= BSC_FD_READ;
Harald Welte52b1f982008-12-23 20:25:15 +0000100
Harald Weltedc55db92009-02-03 12:58:59 +0000101 if (FD_ISSET(ufd->fd, &writeset))
102 flags |= BSC_FD_WRITE;
Holger Freyther5f755982008-12-27 09:42:59 +0000103
Harald Weltedc55db92009-02-03 12:58:59 +0000104 if (FD_ISSET(ufd->fd, &exceptset))
105 flags |= BSC_FD_EXCEPT;
Holger Freyther5f755982008-12-27 09:42:59 +0000106
Harald Welte04d3c922009-05-23 06:07:04 +0000107 if (flags) {
108 work = 1;
Harald Weltedc55db92009-02-03 12:58:59 +0000109 ufd->cb(ufd, flags);
Harald Welte04d3c922009-05-23 06:07:04 +0000110 }
Harald Welte709f2fd2009-07-28 13:32:00 +0200111 /* 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 Welte52b1f982008-12-23 20:25:15 +0000116 }
Harald Welte04d3c922009-05-23 06:07:04 +0000117 return work;
Harald Welte52b1f982008-12-23 20:25:15 +0000118}