]> git.ozlabs.org Git - ccan/blob - ccan/io/poll.c
ccan/io: test read after hangup.
[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
11 static size_t num_fds = 0, max_fds = 0, num_closing = 0, num_waiting = 0;
12 static struct pollfd *pollfds = NULL;
13 static struct fd **fds = NULL;
14 static struct timers timeouts;
15
16 static bool add_fd(struct fd *fd, short events)
17 {
18         if (num_fds + 1 > max_fds) {
19                 struct pollfd *newpollfds;
20                 struct fd **newfds;
21                 size_t num = max_fds ? max_fds * 2 : 8;
22
23                 newpollfds = realloc(pollfds, sizeof(*newpollfds) * num);
24                 if (!newpollfds)
25                         return false;
26                 pollfds = newpollfds;
27                 newfds = realloc(fds, sizeof(*newfds) * num);
28                 if (!newfds)
29                         return false;
30                 fds = newfds;
31                 max_fds = num;
32         }
33
34         pollfds[num_fds].events = events;
35         /* In case it's idle. */
36         if (!events)
37                 pollfds[num_fds].fd = -fd->fd;
38         else
39                 pollfds[num_fds].fd = fd->fd;
40         pollfds[num_fds].revents = 0; /* In case we're iterating now */
41         fds[num_fds] = fd;
42         fd->backend_info = num_fds;
43         num_fds++;
44         if (events)
45                 num_waiting++;
46
47         return true;
48 }
49
50 static void del_fd(struct fd *fd)
51 {
52         size_t n = fd->backend_info;
53
54         assert(n != -1);
55         assert(n < num_fds);
56         if (pollfds[n].events)
57                 num_waiting--;
58         if (n != num_fds - 1) {
59                 /* Move last one over us. */
60                 pollfds[n] = pollfds[num_fds-1];
61                 fds[n] = fds[num_fds-1];
62                 assert(fds[n]->backend_info == num_fds-1);
63                 fds[n]->backend_info = n;
64         } else if (num_fds == 1) {
65                 /* Free everything when no more fds. */
66                 free(pollfds);
67                 free(fds);
68                 pollfds = NULL;
69                 fds = NULL;
70                 max_fds = 0;
71         }
72         num_fds--;
73         fd->backend_info = -1;
74         close(fd->fd);
75 }
76
77 bool add_listener(struct io_listener *l)
78 {
79         if (!add_fd(&l->fd, POLLIN))
80                 return false;
81         return true;
82 }
83
84 void backend_plan_changed(struct io_conn *conn)
85 {
86         struct pollfd *pfd = &pollfds[conn->fd.backend_info];
87
88         if (pfd->events)
89                 num_waiting--;
90
91         pfd->events = conn->plan.pollflag;
92         if (conn->duplex) {
93                 int mask = conn->duplex->plan.pollflag;
94                 /* You can't *both* read/write. */
95                 assert(!mask || pfd->events != mask);
96                 pfd->events |= mask;
97         }
98         if (pfd->events) {
99                 num_waiting++;
100                 pfd->fd = conn->fd.fd;
101         } else
102                 pfd->fd = -conn->fd.fd;
103
104         if (!conn->plan.next)
105                 num_closing++;
106 }
107
108 bool add_conn(struct io_conn *c)
109 {
110         if (!add_fd(&c->fd, c->plan.pollflag))
111                 return false;
112         /* Immediate close is allowed. */
113         if (!c->plan.next)
114                 num_closing++;
115         return true;
116 }
117
118 bool add_duplex(struct io_conn *c)
119 {
120         c->fd.backend_info = c->duplex->fd.backend_info;
121         backend_plan_changed(c);
122         return true;
123 }
124
125 static void del_conn(struct io_conn *conn)
126 {
127         if (conn->finish)
128                 conn->finish(conn, conn->finish_arg);
129         if (timeout_active(conn))
130                 backend_del_timeout(conn);
131         free(conn->timeout);
132         if (conn->duplex) {
133                 /* In case fds[] pointed to the other one. */
134                 fds[conn->fd.backend_info] = &conn->duplex->fd;
135                 conn->duplex->duplex = NULL;
136         } else
137                 del_fd(&conn->fd);
138         num_closing--;
139 }
140
141 void del_listener(struct io_listener *l)
142 {
143         del_fd(&l->fd);
144 }
145
146 static void set_plan(struct io_conn *conn, struct io_plan plan)
147 {
148         conn->plan = plan;
149         backend_plan_changed(conn);
150 }
151
152 static void accept_conn(struct io_listener *l)
153 {
154         int fd = accept(l->fd.fd, NULL, NULL);
155
156         /* FIXME: What to do here? */
157         if (fd < 0)
158                 return;
159         l->init(fd, l->arg);
160 }
161
162 /* It's OK to miss some, as long as we make progress. */
163 static void finish_conns(void)
164 {
165         unsigned int i;
166
167         for (i = 0; !io_loop_return && i < num_fds; i++) {
168                 struct io_conn *c, *duplex;
169
170                 if (!num_closing)
171                         break;
172
173                 if (fds[i]->listener)
174                         continue;
175                 c = (void *)fds[i];
176                 for (duplex = c->duplex; c; c = duplex, duplex = NULL) {
177                         if (!c->plan.next) {
178                                 del_conn(c);
179                                 free(c);
180                                 i--;
181                         }
182                 }
183         }
184 }
185
186 void backend_add_timeout(struct io_conn *conn, struct timespec duration)
187 {
188         if (!timeouts.base)
189                 timers_init(&timeouts, time_now());
190         timer_add(&timeouts, &conn->timeout->timer,
191                   time_add(time_now(), duration));
192         conn->timeout->conn = conn;
193 }
194
195 void backend_del_timeout(struct io_conn *conn)
196 {
197         assert(conn->timeout->conn == conn);
198         timer_del(&timeouts, &conn->timeout->timer);
199         conn->timeout->conn = NULL;
200 }
201
202 /* This is the main loop. */
203 void *io_loop(void)
204 {
205         void *ret;
206
207         while (!io_loop_return) {
208                 int i, r, timeout = INT_MAX;
209                 struct timespec now;
210
211                 if (timeouts.base) {
212                         struct timespec first;
213                         struct list_head expired;
214                         struct io_timeout *t;
215
216                         now = time_now();
217
218                         /* Call functions for expired timers. */
219                         timers_expire(&timeouts, now, &expired);
220                         while ((t = list_pop(&expired, struct io_timeout, timer.list))) {
221                                 struct io_conn *conn = t->conn;
222                                 /* Clear, in case timer re-adds */
223                                 t->conn = NULL;
224                                 set_plan(conn, t->next(conn, t->next_arg));
225                         }
226
227                         /* Now figure out how long to wait for the next one. */
228                         if (timer_earliest(&timeouts, &first)) {
229                                 uint64_t f = time_to_msec(time_sub(first, now));
230                                 if (f < INT_MAX)
231                                         timeout = f;
232                         }
233                 }
234
235                 if (num_closing) {
236                         finish_conns();
237                         /* Could have started/finished more. */
238                         continue;
239                 }
240
241                 if (num_fds == 0)
242                         break;
243
244                 /* You can't tell them all to go to sleep! */
245                 assert(num_waiting);
246
247                 r = poll(pollfds, num_fds, timeout);
248                 if (r < 0)
249                         break;
250
251                 for (i = 0; i < num_fds && !io_loop_return; i++) {
252                         struct io_conn *c = (void *)fds[i];
253                         int events = pollfds[i].revents;
254
255                         if (r == 0)
256                                 break;
257
258                         if (fds[i]->listener) {
259                                 if (events & POLLIN) {
260                                         accept_conn((void *)c);
261                                         r--;
262                                 }
263                         } else if (events & (POLLIN|POLLOUT)) {
264                                 r--;
265                                 if (c->duplex) {
266                                         int mask = c->duplex->plan.pollflag;
267                                         if (events & mask) {
268                                                 io_ready(c->duplex);
269                                                 events &= ~mask;
270                                                 if (!(events&(POLLIN|POLLOUT)))
271                                                         continue;
272                                         }
273                                 }
274                                 io_ready(c);
275                         } else if (events & POLLHUP) {
276                                 r--;
277                                 set_plan(c, io_close(c, NULL));
278                                 if (c->duplex)
279                                         set_plan(c->duplex,
280                                                  io_close(c->duplex, NULL));
281                         }
282                 }
283         }
284
285         while (num_closing)
286                 finish_conns();
287
288         ret = io_loop_return;
289         io_loop_return = NULL;
290         return ret;
291 }