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