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