]> git.ozlabs.org Git - ccan/blob - ccan/io/io.c
12b2489023d94077d5562db876ab9004caae3dd4
[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 #include <ccan/container_of/container_of.h>
14
15 void *io_loop_return;
16
17 struct io_plan io_conn_freed;
18
19 struct io_listener *io_new_listener_(const tal_t *ctx, int fd,
20                                      struct io_plan *(*init)(struct io_conn *,
21                                                              void *),
22                                      void *arg)
23 {
24         struct io_listener *l = tal(ctx, struct io_listener);
25         if (!l)
26                 return NULL;
27
28         l->fd.listener = true;
29         l->fd.fd = fd;
30         l->init = init;
31         l->arg = arg;
32         l->ctx = ctx;
33         if (!add_listener(l))
34                 return tal_free(l);
35         return l;
36 }
37
38 void io_close_listener(struct io_listener *l)
39 {
40         tal_free(l);
41 }
42
43 static struct io_plan *io_never_called(struct io_conn *conn, void *arg)
44 {
45         abort();
46 }
47
48 /* Returns false if conn was freed. */
49 static bool next_plan(struct io_conn *conn, struct io_plan *plan)
50 {
51         struct io_plan *(*next)(struct io_conn *, void *arg);
52
53         next = plan->next;
54
55         plan->status = IO_UNSET;
56         plan->io = NULL;
57         plan->next = io_never_called;
58
59         plan = next(conn, plan->next_arg);
60
61         if (plan == &io_conn_freed)
62                 return false;
63
64         /* It should have set a plan inside this conn (or duplex) */
65         assert(plan == &conn->plan[IO_IN]
66                || plan == &conn->plan[IO_OUT]
67                || plan == &conn->plan[2]);
68         assert(conn->plan[IO_IN].status != IO_UNSET
69                || conn->plan[IO_OUT].status != IO_UNSET);
70
71         backend_new_plan(conn);
72         return true;
73 }
74
75 static void set_blocking(int fd, bool block)
76 {
77         int flags = fcntl(fd, F_GETFL);
78
79         if (block)
80                 flags &= ~O_NONBLOCK;
81         else
82                 flags |= O_NONBLOCK;
83
84         fcntl(fd, F_SETFL, flags);
85 }
86
87 struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
88                              struct io_plan *(*init)(struct io_conn *, void *),
89                              void *arg)
90 {
91         struct io_conn *conn = tal(ctx, struct io_conn);
92
93         if (!conn)
94                 return NULL;
95
96         conn->fd.listener = false;
97         conn->fd.fd = fd;
98         conn->finish = NULL;
99         conn->finish_arg = NULL;
100         list_node_init(&conn->always);
101
102         if (!add_conn(conn))
103                 return tal_free(conn);
104
105         /* Keep our I/O async. */
106         set_blocking(fd, false);
107
108         /* We start with out doing nothing, and in doing our init. */
109         conn->plan[IO_OUT].status = IO_UNSET;
110
111         conn->plan[IO_IN].next = init;
112         conn->plan[IO_IN].next_arg = arg;
113         if (!next_plan(conn, &conn->plan[IO_IN]))
114                 return NULL;
115
116         return conn;
117 }
118
119 void io_set_finish_(struct io_conn *conn,
120                     void (*finish)(struct io_conn *, void *),
121                     void *arg)
122 {
123         conn->finish = finish;
124         conn->finish_arg = arg;
125 }
126
127 struct io_plan_arg *io_plan_arg(struct io_conn *conn, enum io_direction dir)
128 {
129         assert(conn->plan[dir].status == IO_UNSET);
130
131         conn->plan[dir].status = IO_POLLING;
132         return &conn->plan[dir].arg;
133 }
134
135 static struct io_plan *set_always(struct io_conn *conn,
136                                   enum io_direction dir,
137                                   struct io_plan *(*next)(struct io_conn *,
138                                                           void *),
139                                   void *arg)
140 {
141         struct io_plan *plan = &conn->plan[dir];
142
143         plan->status = IO_ALWAYS;
144         backend_new_always(conn);
145         return io_set_plan(conn, dir, NULL, next, arg);
146 }
147
148 static struct io_plan *io_always_dir(struct io_conn *conn,
149                                      enum io_direction dir,
150                                      struct io_plan *(*next)(struct io_conn *,
151                                                              void *),
152                                      void *arg)
153 {
154         return set_always(conn, dir, next, arg);
155 }
156
157 struct io_plan *io_always_(struct io_conn *conn,
158                            struct io_plan *(*next)(struct io_conn *, void *),
159                            void *arg)
160 {
161         return io_always_dir(conn, IO_IN, next, arg);
162 }
163
164 struct io_plan *io_out_always_(struct io_conn *conn,
165                                struct io_plan *(*next)(struct io_conn *,
166                                                        void *),
167                                void *arg)
168 {
169         return io_always_dir(conn, IO_OUT, next, arg);
170 }
171
172 static int do_write(int fd, struct io_plan_arg *arg)
173 {
174         ssize_t ret = write(fd, arg->u1.cp, arg->u2.s);
175         if (ret < 0)
176                 return -1;
177
178         arg->u1.cp += ret;
179         arg->u2.s -= ret;
180         return arg->u2.s == 0;
181 }
182
183 /* Queue some data to be written. */
184 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
185                           struct io_plan *(*next)(struct io_conn *, void *),
186                           void *next_arg)
187 {
188         struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
189
190         if (len == 0)
191                 return set_always(conn, IO_OUT, next, next_arg);
192
193         arg->u1.const_vp = data;
194         arg->u2.s = len;
195
196         return io_set_plan(conn, IO_OUT, do_write, next, next_arg);
197 }
198
199 static int do_read(int fd, struct io_plan_arg *arg)
200 {
201         ssize_t ret = read(fd, arg->u1.cp, arg->u2.s);
202         if (ret <= 0)
203                 return -1;
204
205         arg->u1.cp += ret;
206         arg->u2.s -= ret;
207         return arg->u2.s == 0;
208 }
209
210 /* Queue a request to read into a buffer. */
211 struct io_plan *io_read_(struct io_conn *conn,
212                          void *data, size_t len,
213                          struct io_plan *(*next)(struct io_conn *, void *),
214                          void *next_arg)
215 {
216         struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
217
218         if (len == 0)
219                 return set_always(conn, IO_IN, next, next_arg);
220
221         arg->u1.cp = data;
222         arg->u2.s = len;
223
224         return io_set_plan(conn, IO_IN, do_read, next, next_arg);
225 }
226
227 static int do_read_partial(int fd, struct io_plan_arg *arg)
228 {
229         ssize_t ret = read(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
230         if (ret <= 0)
231                 return -1;
232
233         *(size_t *)arg->u2.vp = ret;
234         return 1;
235 }
236
237 /* Queue a partial request to read into a buffer. */
238 struct io_plan *io_read_partial_(struct io_conn *conn,
239                                  void *data, size_t maxlen, size_t *len,
240                                  struct io_plan *(*next)(struct io_conn *,
241                                                          void *),
242                                  void *next_arg)
243 {
244         struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
245
246         if (maxlen == 0)
247                 return set_always(conn, IO_IN, next, next_arg);
248
249         arg->u1.cp = data;
250         /* We store the max len in here temporarily. */
251         *len = maxlen;
252         arg->u2.vp = len;
253
254         return io_set_plan(conn, IO_IN, do_read_partial, next, next_arg);
255 }
256
257 static int do_write_partial(int fd, struct io_plan_arg *arg)
258 {
259         ssize_t ret = write(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
260         if (ret < 0)
261                 return -1;
262
263         *(size_t *)arg->u2.vp = ret;
264         return 1;
265 }
266
267 /* Queue a partial write request. */
268 struct io_plan *io_write_partial_(struct io_conn *conn,
269                                   const void *data, size_t maxlen, size_t *len,
270                                   struct io_plan *(*next)(struct io_conn *,
271                                                           void*),
272                                   void *next_arg)
273 {
274         struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
275
276         if (maxlen == 0)
277                 return set_always(conn, IO_OUT, next, next_arg);
278
279         arg->u1.const_vp = data;
280         /* We store the max len in here temporarily. */
281         *len = maxlen;
282         arg->u2.vp = len;
283
284         return io_set_plan(conn, IO_OUT, do_write_partial, next, next_arg);
285 }
286
287 static int do_connect(int fd, struct io_plan_arg *arg)
288 {
289         int err, ret;
290         socklen_t len = sizeof(err);
291
292         /* Has async connect finished? */
293         ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
294         if (ret < 0)
295                 return -1;
296
297         if (err == 0) {
298                 return 1;
299         } else if (err == EINPROGRESS)
300                 return 0;
301
302         errno = err;
303         return -1;
304 }
305
306 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
307                             struct io_plan *(*next)(struct io_conn *, void *),
308                             void *next_arg)
309 {
310         int fd = io_conn_fd(conn);
311
312         /* We don't actually need the arg, but we need it polling. */
313         io_plan_arg(conn, IO_OUT);
314
315         /* Note that io_new_conn() will make fd O_NONBLOCK */
316
317         /* Immediate connect can happen. */
318         if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0)
319                 return set_always(conn, IO_OUT, next, next_arg);
320
321         if (errno != EINPROGRESS)
322                 return io_close(conn);
323
324         return io_set_plan(conn, IO_OUT, do_connect, next, next_arg);
325 }
326
327 static struct io_plan *io_wait_dir(struct io_conn *conn,
328                                    const void *wait,
329                                    enum io_direction dir,
330                                    struct io_plan *(*next)(struct io_conn *,
331                                                            void *),
332                                    void *next_arg)
333 {
334         struct io_plan_arg *arg = io_plan_arg(conn, dir);
335         arg->u1.const_vp = wait;
336
337         conn->plan[dir].status = IO_WAITING;
338
339         return io_set_plan(conn, dir, NULL, next, next_arg);
340 }
341
342 struct io_plan *io_wait_(struct io_conn *conn,
343                          const void *wait,
344                          struct io_plan *(*next)(struct io_conn *, void *),
345                          void *next_arg)
346 {
347         return io_wait_dir(conn, wait, IO_IN, next, next_arg);
348 }
349
350 struct io_plan *io_out_wait_(struct io_conn *conn,
351                              const void *wait,
352                              struct io_plan *(*next)(struct io_conn *, void *),
353                              void *next_arg)
354 {
355         return io_wait_dir(conn, wait, IO_OUT, next, next_arg);
356 }
357
358 void io_wake(const void *wait)
359 {
360         backend_wake(wait);
361 }
362
363 /* Returns false if this has been freed. */
364 static bool do_plan(struct io_conn *conn, struct io_plan *plan)
365 {
366         /* We shouldn't have polled for this event if this wasn't true! */
367         assert(plan->status == IO_POLLING);
368
369         switch (plan->io(conn->fd.fd, &plan->arg)) {
370         case -1:
371                 io_close(conn);
372                 return false;
373         case 0:
374                 return true;
375         case 1:
376                 return next_plan(conn, plan);
377         default:
378                 /* IO should only return -1, 0 or 1 */
379                 abort();
380         }
381 }
382
383 void io_ready(struct io_conn *conn, int pollflags)
384 {
385         if (pollflags & POLLIN)
386                 if (!do_plan(conn, &conn->plan[IO_IN]))
387                         return;
388
389         if (pollflags & POLLOUT)
390                 do_plan(conn, &conn->plan[IO_OUT]);
391 }
392
393 void io_do_always(struct io_conn *conn)
394 {
395         /* There's a corner case where the in next_plan wakes up the
396          * out, placing it in IO_ALWAYS and we end up processing it immediately,
397          * only to leave it in the always list.
398          *
399          * Yet we can't just process one, in case they are both supposed
400          * to be done, so grab state beforehand.
401          */
402         bool always_out = (conn->plan[IO_OUT].status == IO_ALWAYS);
403
404         if (conn->plan[IO_IN].status == IO_ALWAYS)
405                 if (!next_plan(conn, &conn->plan[IO_IN]))
406                         return;
407
408         if (always_out) {
409                 /* You can't *unalways* a conn (except by freeing, in which
410                  * case next_plan() returned false */
411                 assert(conn->plan[IO_OUT].status == IO_ALWAYS);
412                 next_plan(conn, &conn->plan[IO_OUT]);
413         }
414 }
415
416 void io_do_wakeup(struct io_conn *conn, enum io_direction dir)
417 {
418         struct io_plan *plan = &conn->plan[dir];
419
420         assert(plan->status == IO_WAITING);
421
422         set_always(conn, dir, plan->next, plan->next_arg);
423 }
424
425 /* Close the connection, we're done. */
426 struct io_plan *io_close(struct io_conn *conn)
427 {
428         tal_free(conn);
429         return &io_conn_freed;
430 }
431
432 struct io_plan *io_close_cb(struct io_conn *conn, void *next_arg)
433 {
434         return io_close(conn);
435 }
436
437 struct io_plan *io_close_taken_fd(struct io_conn *conn)
438 {
439         set_blocking(conn->fd.fd, true);
440
441         cleanup_conn_without_close(conn);
442         return io_close(conn);
443 }
444
445 /* Exit the loop, returning this (non-NULL) arg. */
446 void io_break(const void *ret)
447 {
448         assert(ret);
449         io_loop_return = (void *)ret;
450 }
451
452 struct io_plan *io_never(struct io_conn *conn, void *unused)
453 {
454         return io_always(conn, io_never_called, NULL);
455 }
456
457 int io_conn_fd(const struct io_conn *conn)
458 {
459         return conn->fd.fd;
460 }
461
462 struct io_plan *io_duplex(struct io_conn *conn,
463                           struct io_plan *in_plan, struct io_plan *out_plan)
464 {
465         assert(conn == container_of(in_plan, struct io_conn, plan[IO_IN]));
466         /* in_plan must be conn->plan[IO_IN], out_plan must be [IO_OUT] */
467         assert(out_plan == in_plan + 1);
468         return out_plan + 1;
469 }
470
471 struct io_plan *io_halfclose(struct io_conn *conn)
472 {
473         /* Both unset?  OK. */
474         if (conn->plan[IO_IN].status == IO_UNSET
475             && conn->plan[IO_OUT].status == IO_UNSET)
476                 return io_close(conn);
477
478         /* We leave this unset then. */
479         if (conn->plan[IO_IN].status == IO_UNSET)
480                 return &conn->plan[IO_IN];
481         else
482                 return &conn->plan[IO_OUT];
483 }
484
485 struct io_plan *io_set_plan(struct io_conn *conn, enum io_direction dir,
486                             int (*io)(int fd, struct io_plan_arg *arg),
487                             struct io_plan *(*next)(struct io_conn *, void *),
488                             void *next_arg)
489 {
490         struct io_plan *plan = &conn->plan[dir];
491
492         plan->io = io;
493         plan->next = next;
494         plan->next_arg = next_arg;
495         assert(next != NULL);
496
497         return plan;
498 }
499
500 bool io_flush_sync(struct io_conn *conn)
501 {
502         struct io_plan *plan = &conn->plan[IO_OUT];
503         bool ok;
504
505         /* Not writing?  Nothing to do. */
506         if (plan->status != IO_POLLING)
507                 return true;
508
509         /* Synchronous please. */
510         set_blocking(io_conn_fd(conn), true);
511
512 again:
513         switch (plan->io(conn->fd.fd, &plan->arg)) {
514         case -1:
515                 ok = false;
516                 break;
517         /* Incomplete, try again. */
518         case 0:
519                 goto again;
520         case 1:
521                 ok = true;
522                 /* In case they come back. */
523                 set_always(conn, IO_OUT, plan->next, plan->next_arg);
524                 break;
525         default:
526                 /* IO should only return -1, 0 or 1 */
527                 abort();
528         }
529
530         set_blocking(io_conn_fd(conn), false);
531         return ok;
532 }