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