]> git.ozlabs.org Git - ccan/blob - ccan/io/io.c
ccan/io: duplex support.
[ccan] / ccan / io / io.c
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #include "io.h"
3 #include "backend.h"
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netdb.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <assert.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13
14 void *io_loop_return;
15
16 struct io_listener *io_new_listener_(const tal_t *ctx, int fd,
17                                      struct io_plan *(*init)(struct io_conn *,
18                                                              void *),
19                                      void *arg)
20 {
21         struct io_listener *l = tal(ctx, struct io_listener);
22         if (!l)
23                 return NULL;
24
25         l->fd.listener = true;
26         l->fd.fd = fd;
27         l->init = init;
28         l->arg = arg;
29         l->ctx = ctx;
30         if (!add_listener(l))
31                 return tal_free(l);
32         return l;
33 }
34
35 void io_close_listener(struct io_listener *l)
36 {
37         close(l->fd.fd);
38         del_listener(l);
39         tal_free(l);
40 }
41
42 static struct io_plan *io_never_called(struct io_conn *conn, void *arg)
43 {
44         abort();
45 }
46
47 static void next_plan(struct io_conn *conn, struct io_plan *plan)
48 {
49         struct io_plan *(*next)(struct io_conn *, void *arg);
50
51         next = plan->next;
52
53         plan->status = IO_UNSET;
54         plan->io = NULL;
55         plan->next = io_never_called;
56
57         plan = next(conn, plan->next_arg);
58
59         /* It should have set a plan inside this conn (or duplex) */
60         assert(plan == &conn->plan[IO_IN]
61                || plan == &conn->plan[IO_OUT]
62                || plan == &conn->plan[2]);
63         assert(conn->plan[IO_IN].status != IO_UNSET
64                || conn->plan[IO_OUT].status != IO_UNSET);
65
66         backend_new_plan(conn);
67 }
68
69 struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
70                              struct io_plan *(*init)(struct io_conn *, void *),
71                              void *arg)
72 {
73         struct io_conn *conn = tal(ctx, struct io_conn);
74
75         if (!conn)
76                 return NULL;
77
78         conn->fd.listener = false;
79         conn->fd.fd = fd;
80         conn->finish = NULL;
81         conn->finish_arg = NULL;
82         conn->list = NULL;
83
84         if (!add_conn(conn))
85                 return tal_free(conn);
86
87         /* We start with out doing nothing, and in doing our init. */
88         conn->plan[IO_OUT].status = IO_UNSET;
89
90         conn->plan[IO_IN].next = init;
91         conn->plan[IO_IN].next_arg = arg;
92         next_plan(conn, &conn->plan[IO_IN]);
93
94         return conn;
95 }
96
97 void io_set_finish_(struct io_conn *conn,
98                     void (*finish)(struct io_conn *, void *),
99                     void *arg)
100 {
101         conn->finish = finish;
102         conn->finish_arg = arg;
103 }
104
105 struct io_plan *io_get_plan(struct io_conn *conn, enum io_direction dir)
106 {
107         assert(conn->plan[dir].status == IO_UNSET);
108
109         conn->plan[dir].status = IO_POLLING;
110         return &conn->plan[dir];
111 }
112
113 static struct io_plan *set_always(struct io_conn *conn,
114                                   struct io_plan *plan,
115                                   struct io_plan *(*next)(struct io_conn *,
116                                                           void *),
117                                   void *arg)
118 {
119         plan->next = next;
120         plan->next_arg = arg;
121         plan->status = IO_ALWAYS;
122
123         backend_new_always(conn);
124         return plan;
125 }
126
127 struct io_plan *io_always_(struct io_conn *conn,
128                            struct io_plan *(*next)(struct io_conn *, void *),
129                            void *arg)
130 {
131         struct io_plan *plan;
132
133         /* If we're duplex, we want this on the current plan.  Otherwise,
134          * doesn't matter. */
135         if (conn->plan[IO_IN].status == IO_UNSET)
136                 plan = io_get_plan(conn, IO_IN);
137         else
138                 plan = io_get_plan(conn, IO_OUT);
139
140         assert(next);
141         set_always(conn, plan, next, arg);
142
143         return plan;
144 }
145
146 static int do_write(int fd, struct io_plan *plan)
147 {
148         ssize_t ret = write(fd, plan->u1.cp, plan->u2.s);
149         if (ret < 0)
150                 return -1;
151
152         plan->u1.cp += ret;
153         plan->u2.s -= ret;
154         return plan->u2.s == 0;
155 }
156
157 /* Queue some data to be written. */
158 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
159                           struct io_plan *(*next)(struct io_conn *, void *),
160                           void *arg)
161 {
162         struct io_plan *plan = io_get_plan(conn, IO_OUT);
163
164         assert(next);
165
166         if (len == 0)
167                 return set_always(conn, plan, next, arg);
168
169         plan->u1.const_vp = data;
170         plan->u2.s = len;
171         plan->io = do_write;
172         plan->next = next;
173         plan->next_arg = arg;
174
175         return plan;
176 }
177
178 static int do_read(int fd, struct io_plan *plan)
179 {
180         ssize_t ret = read(fd, plan->u1.cp, plan->u2.s);
181         if (ret <= 0)
182                 return -1;
183
184         plan->u1.cp += ret;
185         plan->u2.s -= ret;
186         return plan->u2.s == 0;
187 }
188
189 /* Queue a request to read into a buffer. */
190 struct io_plan *io_read_(struct io_conn *conn,
191                          void *data, size_t len,
192                          struct io_plan *(*next)(struct io_conn *, void *),
193                          void *arg)
194 {
195         struct io_plan *plan = io_get_plan(conn, IO_IN);
196
197         assert(next);
198
199         if (len == 0)
200                 return set_always(conn, plan, next, arg);
201
202         plan->u1.cp = data;
203         plan->u2.s = len;
204         plan->io = do_read;
205         plan->next = next;
206         plan->next_arg = arg;
207
208         return plan;
209 }
210
211 static int do_read_partial(int fd, struct io_plan *plan)
212 {
213         ssize_t ret = read(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
214         if (ret <= 0)
215                 return -1;
216
217         *(size_t *)plan->u2.vp = ret;
218         return 1;
219 }
220
221 /* Queue a partial request to read into a buffer. */
222 struct io_plan *io_read_partial_(struct io_conn *conn,
223                                  void *data, size_t maxlen, size_t *len,
224                                  struct io_plan *(*next)(struct io_conn *,
225                                                          void *),
226                                  void *arg)
227 {
228         struct io_plan *plan = io_get_plan(conn, IO_IN);
229
230         assert(next);
231
232         if (maxlen == 0)
233                 return set_always(conn, plan, next, arg);
234
235         plan->u1.cp = data;
236         /* We store the max len in here temporarily. */
237         *len = maxlen;
238         plan->u2.vp = len;
239         plan->io = do_read_partial;
240         plan->next = next;
241         plan->next_arg = arg;
242
243         return plan;
244 }
245
246 static int do_write_partial(int fd, struct io_plan *plan)
247 {
248         ssize_t ret = write(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
249         if (ret < 0)
250                 return -1;
251
252         *(size_t *)plan->u2.vp = ret;
253         return 1;
254 }
255
256 /* Queue a partial write request. */
257 struct io_plan *io_write_partial_(struct io_conn *conn,
258                                   const void *data, size_t maxlen, size_t *len,
259                                   struct io_plan *(*next)(struct io_conn *,
260                                                           void*),
261                                   void *arg)
262 {
263         struct io_plan *plan = io_get_plan(conn, IO_OUT);
264
265         assert(next);
266
267         if (maxlen == 0)
268                 return set_always(conn, plan, next, arg);
269
270         plan->u1.const_vp = data;
271         /* We store the max len in here temporarily. */
272         *len = maxlen;
273         plan->u2.vp = len;
274         plan->io = do_write_partial;
275         plan->next = next;
276         plan->next_arg = arg;
277
278         return plan;
279 }
280
281 static int do_connect(int fd, struct io_plan *plan)
282 {
283         int err, ret;
284         socklen_t len = sizeof(err);
285
286         /* Has async connect finished? */
287         ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
288         if (ret < 0)
289                 return -1;
290
291         if (err == 0) {
292                 /* Restore blocking if it was initially. */
293                 fcntl(fd, F_SETFL, plan->u1.s);
294                 return 1;
295         } else if (err == EINPROGRESS)
296                 return 0;
297
298         errno = err;
299         return -1;
300 }
301
302 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
303                             struct io_plan *(*next)(struct io_conn *, void *),
304                             void *arg)
305 {
306         struct io_plan *plan = io_get_plan(conn, IO_IN);
307         int fd = io_conn_fd(conn);
308
309         assert(next);
310
311         /* Save old flags, set nonblock if not already. */
312         plan->u1.s = fcntl(fd, F_GETFL);
313         fcntl(fd, F_SETFL, plan->u1.s | O_NONBLOCK);
314
315         /* Immediate connect can happen. */
316         if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0)
317                 return set_always(conn, plan, next, arg);
318
319         if (errno != EINPROGRESS)
320                 return io_close(conn);
321
322         plan->next = next;
323         plan->next_arg = arg;
324         plan->io = do_connect;
325
326         return plan;
327 }
328
329 struct io_plan *io_wait_(struct io_conn *conn,
330                          const void *wait,
331                          struct io_plan *(*next)(struct io_conn *, void *),
332                          void *arg)
333 {
334         struct io_plan *plan;
335
336         /* If we're duplex, we want this on the current plan.  Otherwise,
337          * doesn't matter. */
338         if (conn->plan[IO_IN].status == IO_UNSET)
339                 plan = io_get_plan(conn, IO_IN);
340         else
341                 plan = io_get_plan(conn, IO_OUT);
342
343         assert(next);
344
345         plan->next = next;
346         plan->next_arg = arg;
347         plan->u1.const_vp = wait;
348         plan->status = IO_WAITING;
349
350         return plan;
351 }
352
353 void io_wake(const void *wait)
354 {
355         backend_wake(wait);
356 }
357
358 static void do_plan(struct io_conn *conn, struct io_plan *plan)
359 {
360         /* Someone else might have called io_close() on us. */
361         if (plan->status == IO_CLOSING)
362                 return;
363
364         /* We shouldn't have polled for this event if this wasn't true! */
365         assert(plan->status == IO_POLLING);
366
367         switch (plan->io(conn->fd.fd, plan)) {
368         case -1:
369                 io_close(conn);
370                 break;
371         case 0:
372                 break;
373         case 1:
374                 next_plan(conn, plan);
375                 break;
376         default:
377                 /* IO should only return -1, 0 or 1 */
378                 abort();
379         }
380 }
381
382 void io_ready(struct io_conn *conn, int pollflags)
383 {
384         if (pollflags & POLLIN)
385                 do_plan(conn, &conn->plan[IO_IN]);
386
387         if (pollflags & POLLOUT)
388                 do_plan(conn, &conn->plan[IO_OUT]);
389 }
390
391 void io_do_always(struct io_conn *conn)
392 {
393         if (conn->plan[IO_IN].status == IO_ALWAYS)
394                 next_plan(conn, &conn->plan[IO_IN]);
395
396         if (conn->plan[IO_OUT].status == IO_ALWAYS)
397                 next_plan(conn, &conn->plan[IO_OUT]);
398 }
399
400 void io_do_wakeup(struct io_conn *conn, struct io_plan *plan)
401 {
402         assert(plan->status == IO_WAITING);
403         next_plan(conn, plan);
404 }
405
406 /* Close the connection, we're done. */
407 struct io_plan *io_close(struct io_conn *conn)
408 {
409         /* Already closing?  Don't close twice. */
410         if (conn->plan[IO_IN].status == IO_CLOSING)
411                 return &conn->plan[IO_IN];
412
413         conn->plan[IO_IN].status = conn->plan[IO_OUT].status = IO_CLOSING;
414         conn->plan[IO_IN].u1.s = errno;
415         backend_new_closing(conn);
416
417         return &conn->plan[IO_IN];
418 }
419
420 struct io_plan *io_close_cb(struct io_conn *conn, void *arg)
421 {
422         return io_close(conn);
423 }
424
425 /* Exit the loop, returning this (non-NULL) arg. */
426 void io_break(const void *ret)
427 {
428         assert(ret);
429         io_loop_return = (void *)ret;
430 }
431
432 struct io_plan *io_never(struct io_conn *conn, void *unused)
433 {
434         return io_always(conn, io_never_called, NULL);
435 }
436
437 int io_conn_fd(const struct io_conn *conn)
438 {
439         return conn->fd.fd;
440 }
441
442 struct io_plan *io_duplex(struct io_plan *in_plan, struct io_plan *out_plan)
443 {
444         /* in_plan must be conn->plan[IO_IN], out_plan must be [IO_OUT] */
445         assert(out_plan == in_plan + 1);
446
447         return out_plan + 1;
448 }