]> git.ozlabs.org Git - ccan/blob - ccan/io/poll.c
io: allow freeing of io_conn at any time.
[ccan] / ccan / io / poll.c
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #include "io.h"
3 #include "backend.h"
4 #include <assert.h>
5 #include <poll.h>
6 #include <stdlib.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <limits.h>
10 #include <errno.h>
11 #include <ccan/time/time.h>
12 #include <ccan/timer/timer.h>
13
14 static size_t num_fds = 0, max_fds = 0, num_waiting = 0;
15 static struct pollfd *pollfds = NULL;
16 static struct fd **fds = NULL;
17 static LIST_HEAD(closing);
18 static LIST_HEAD(always);
19 static struct timemono (*nowfn)(void) = time_mono;
20
21 struct timemono (*io_time_override(struct timemono (*now)(void)))(void)
22 {
23         struct timemono (*old)(void) = nowfn;
24         nowfn = now;
25         return old;
26 }
27
28 static bool add_fd(struct fd *fd, short events)
29 {
30         if (!max_fds) {
31                 assert(num_fds == 0);
32                 pollfds = tal_arr(NULL, struct pollfd, 8);
33                 if (!pollfds)
34                         return false;
35                 fds = tal_arr(pollfds, struct fd *, 8);
36                 if (!fds)
37                         return false;
38                 max_fds = 8;
39         }
40
41         if (num_fds + 1 > max_fds) {
42                 size_t num = max_fds * 2;
43
44                 if (!tal_resize(&pollfds, num))
45                         return false;
46                 if (!tal_resize(&fds, num))
47                         return false;
48                 max_fds = num;
49         }
50
51         pollfds[num_fds].events = events;
52         /* In case it's idle. */
53         if (!events)
54                 pollfds[num_fds].fd = -fd->fd;
55         else
56                 pollfds[num_fds].fd = fd->fd;
57         pollfds[num_fds].revents = 0; /* In case we're iterating now */
58         fds[num_fds] = fd;
59         fd->backend_info = num_fds;
60         num_fds++;
61         if (events)
62                 num_waiting++;
63
64         return true;
65 }
66
67 static void del_fd(struct fd *fd)
68 {
69         size_t n = fd->backend_info;
70
71         assert(n != -1);
72         assert(n < num_fds);
73         if (pollfds[n].events)
74                 num_waiting--;
75         if (n != num_fds - 1) {
76                 /* Move last one over us. */
77                 pollfds[n] = pollfds[num_fds-1];
78                 fds[n] = fds[num_fds-1];
79                 assert(fds[n]->backend_info == num_fds-1);
80                 fds[n]->backend_info = n;
81         } else if (num_fds == 1) {
82                 /* Free everything when no more fds. */
83                 pollfds = tal_free(pollfds);
84                 fds = NULL;
85                 max_fds = 0;
86         }
87         num_fds--;
88         fd->backend_info = -1;
89
90         /* Closing a local socket doesn't wake poll() because other end
91          * has them open.  See 2.6.  When should I use shutdown()?
92          * in http://www.faqs.org/faqs/unix-faq/socket/ */
93         shutdown(fd->fd, SHUT_RDWR);
94
95         close(fd->fd);
96 }
97
98 static void destroy_listener(struct io_listener *l)
99 {
100         close(l->fd.fd);
101         del_fd(&l->fd);
102 }
103
104 bool add_listener(struct io_listener *l)
105 {
106         if (!add_fd(&l->fd, POLLIN))
107                 return false;
108         tal_add_destructor(l, destroy_listener);
109         return true;
110 }
111
112 void remove_from_always(struct io_conn *conn)
113 {
114         list_del_init(&conn->always);
115 }
116
117 void backend_new_always(struct io_conn *conn)
118 {
119         /* In case it's already in always list. */
120         list_del(&conn->always);
121         list_add_tail(&always, &conn->always);
122 }
123
124 void backend_new_plan(struct io_conn *conn)
125 {
126         struct pollfd *pfd = &pollfds[conn->fd.backend_info];
127
128         if (pfd->events)
129                 num_waiting--;
130
131         pfd->events = 0;
132         if (conn->plan[IO_IN].status == IO_POLLING)
133                 pfd->events |= POLLIN;
134         if (conn->plan[IO_OUT].status == IO_POLLING)
135                 pfd->events |= POLLOUT;
136
137         if (pfd->events) {
138                 num_waiting++;
139                 pfd->fd = conn->fd.fd;
140         } else {
141                 pfd->fd = -conn->fd.fd;
142         }
143 }
144
145 void backend_wake(const void *wait)
146 {
147         unsigned int i;
148
149         for (i = 0; i < num_fds; i++) {
150                 struct io_conn *c;
151
152                 /* Ignore listeners */
153                 if (fds[i]->listener)
154                         continue;
155
156                 c = (void *)fds[i];
157                 if (c->plan[IO_IN].status == IO_WAITING
158                     && c->plan[IO_IN].arg.u1.const_vp == wait)
159                         io_do_wakeup(c, IO_IN);
160
161                 if (c->plan[IO_OUT].status == IO_WAITING
162                     && c->plan[IO_OUT].arg.u1.const_vp == wait)
163                         io_do_wakeup(c, IO_OUT);
164         }
165 }
166
167 static void destroy_conn(struct io_conn *conn)
168 {
169         int saved_errno = errno;
170
171         close(conn->fd.fd);
172         del_fd(&conn->fd);
173         /* In case it's on always list, remove it. */
174         list_del_init(&conn->always);
175
176         /* errno saved/restored by tal_free itself. */
177         if (conn->finish) {
178                 errno = saved_errno;
179                 conn->finish(conn, conn->finish_arg);
180         }
181 }
182
183 bool add_conn(struct io_conn *c)
184 {
185         if (!add_fd(&c->fd, 0))
186                 return false;
187         tal_add_destructor(c, destroy_conn);
188         return true;
189 }
190
191 static void accept_conn(struct io_listener *l)
192 {
193         int fd = accept(l->fd.fd, NULL, NULL);
194
195         /* FIXME: What to do here? */
196         if (fd < 0)
197                 return;
198
199         io_new_conn(l->ctx, fd, l->init, l->arg);
200 }
201
202 static bool handle_always(void)
203 {
204         bool ret = false;
205         struct io_conn *conn;
206
207         while ((conn = list_pop(&always, struct io_conn, always)) != NULL) {
208                 assert(conn->plan[IO_IN].status == IO_ALWAYS
209                        || conn->plan[IO_OUT].status == IO_ALWAYS);
210
211                 /* Re-initialize, for next time. */
212                 list_node_init(&conn->always);
213                 io_do_always(conn);
214                 ret = true;
215         }
216         return ret;
217 }
218
219 /* This is the main loop. */
220 void *io_loop(struct timers *timers, struct timer **expired)
221 {
222         void *ret;
223
224         /* if timers is NULL, expired must be.  If not, not. */
225         assert(!timers == !expired);
226
227         /* Make sure this is NULL if we exit for some other reason. */
228         if (expired)
229                 *expired = NULL;
230
231         while (!io_loop_return) {
232                 int i, r, ms_timeout = -1;
233
234                 if (handle_always()) {
235                         /* Could have started/finished more. */
236                         continue;
237                 }
238
239                 /* Everything closed? */
240                 if (num_fds == 0)
241                         break;
242
243                 /* You can't tell them all to go to sleep! */
244                 assert(num_waiting);
245
246                 if (timers) {
247                         struct timemono now, first;
248
249                         now = nowfn();
250
251                         /* Call functions for expired timers. */
252                         *expired = timers_expire(timers, now);
253                         if (*expired)
254                                 break;
255
256                         /* Now figure out how long to wait for the next one. */
257                         if (timer_earliest(timers, &first)) {
258                                 uint64_t next;
259                                 next = time_to_msec(timemono_between(first, now));
260                                 if (next < INT_MAX)
261                                         ms_timeout = next;
262                                 else
263                                         ms_timeout = INT_MAX;
264                         }
265                 }
266
267                 r = poll(pollfds, num_fds, ms_timeout);
268                 if (r < 0)
269                         break;
270
271                 for (i = 0; i < num_fds && !io_loop_return; i++) {
272                         struct io_conn *c = (void *)fds[i];
273                         int events = pollfds[i].revents;
274
275                         if (r == 0)
276                                 break;
277
278                         if (fds[i]->listener) {
279                                 if (events & POLLIN) {
280                                         accept_conn((void *)c);
281                                         r--;
282                                 }
283                         } else if (events & (POLLIN|POLLOUT)) {
284                                 r--;
285                                 io_ready(c, events);
286                         } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
287                                 r--;
288                                 errno = EBADF;
289                                 io_close(c);
290                         }
291                 }
292         }
293
294         ret = io_loop_return;
295         io_loop_return = NULL;
296
297         return ret;
298 }