]> git.ozlabs.org Git - ccan/blob - ccan/io/io.h
io: fix maybe-uninitialized warning in test (-O2)
[ccan] / ccan / io / io.h
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #ifndef CCAN_IO_H
3 #define CCAN_IO_H
4 #include <ccan/tal/tal.h>
5 #include <ccan/typesafe_cb/typesafe_cb.h>
6 #include <stdbool.h>
7 #include <unistd.h>
8
9 struct timers;
10 struct timer;
11 struct list_head;
12
13 /**
14  * struct io_plan - a plan for input or output.
15  *
16  * Each io_conn has zero to two of these active at any time.
17  */
18 struct io_plan;
19
20 /**
21  * struct io_conn - a connection associated with an fd.
22  */
23 struct io_conn;
24
25 /**
26  * io_new_conn - create a new connection.
27  * @ctx: the context to tal from (or NULL)
28  * @fd: the file descriptor.
29  * @init: the function to call for a new connection
30  * @arg: the argument to @init.
31  *
32  * This creates a connection which owns @fd, it then calls
33  * @init to initialize the connection, which sets up an io_plan.
34  *
35  * Returns NULL on error (and sets errno).
36  *
37  * Example:
38  * // Dumb init function to print string and tell conn to close.
39  * static struct io_plan *conn_init(struct io_conn *conn, const char *msg)
40  * {
41  *      printf("Created conn %p: %s", conn, msg);
42  *      return io_close(conn);
43  * }
44  *
45  * static void create_self_closing_pipe(void)
46  * {
47  *      int fd[2];
48  *      struct io_conn *conn;
49  *
50  *      if (pipe(fd) != 0)
51  *              exit(1);
52  *      conn = io_new_conn(NULL, fd[0], conn_init, (const char *)"hi!");
53  *      if (!conn)
54  *              exit(1);
55  * }
56  */
57 #define io_new_conn(ctx, fd, init, arg)                                 \
58         io_new_conn_((ctx), (fd),                                       \
59                      typesafe_cb_preargs(struct io_plan *, void *,      \
60                                          (init), (arg),                 \
61                                          struct io_conn *conn),         \
62                      (void *)(arg))
63
64 struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
65                              struct io_plan *(*init)(struct io_conn *, void *),
66                              void *arg);
67
68 /**
69  * io_set_finish - set finish function on a connection.
70  * @conn: the connection.
71  * @finish: the function to call when it's closed or fails.
72  * @arg: the argument to @finish.
73  *
74  * @finish will be called when an I/O operation fails, or you call
75  * io_close() on the connection.  errno will be set to the value
76  * after the failed I/O, or at the call to io_close().  The fd
77  * will be closed before @finish is called.
78  *
79  * Example:
80  * static void finish(struct io_conn *conn, const char *msg)
81  * {
82  *      // errno is not 0 after success, so this is a bit useless.
83  *      printf("Conn %p closed with errno %i (%s)\n", conn, errno, msg);
84  * }
85  *
86  * // Dumb init function to print string and tell conn to close.
87  * static struct io_plan *conn_init(struct io_conn *conn, const char *msg)
88  * {
89  *      io_set_finish(conn, finish, msg);
90  *      return io_close(conn);
91  * }
92  */
93 #define io_set_finish(conn, finish, arg)                                \
94         io_set_finish_((conn),                                          \
95                        typesafe_cb_preargs(void, void *,                \
96                                            (finish), (arg),             \
97                                            struct io_conn *),           \
98                        (void *)(arg))
99 void io_set_finish_(struct io_conn *conn,
100                     void (*finish)(struct io_conn *, void *),
101                     void *arg);
102
103
104 /**
105  * io_new_listener - create a new accepting listener.
106  * @ctx: the context to tal from (or NULL)
107  * @fd: the file descriptor.
108  * @init: the function to call for a new connection
109  * @arg: the argument to @init.
110  *
111  * When @fd becomes readable, we accept(), create a new connection,
112  * (tal'ocated off @ctx) and pass that to init().
113  *
114  * Returns NULL on error (and sets errno).
115  *
116  * Example:
117  * #include <sys/types.h>
118  * #include <sys/socket.h>
119  * #include <netdb.h>
120  *
121  * ...
122  *
123  * // Set up a listening socket, return it.
124  * static struct io_listener *do_listen(const char *port)
125  * {
126  *      struct addrinfo *addrinfo, hints;
127  *      int fd, on = 1;
128  *
129  *      memset(&hints, 0, sizeof(hints));
130  *      hints.ai_family = AF_UNSPEC;
131  *      hints.ai_socktype = SOCK_STREAM;
132  *      hints.ai_flags = AI_PASSIVE;
133  *      hints.ai_protocol = 0;
134  *
135  *      if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
136  *              return NULL;
137  *
138  *      fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
139  *                  addrinfo->ai_protocol);
140  *      if (fd < 0)
141  *              return NULL;
142  *
143  *      freeaddrinfo(addrinfo);
144  *      setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
145  *      if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
146  *              close(fd);
147  *              return NULL;
148  *      }
149  *      if (listen(fd, 1) != 0) {
150  *              close(fd);
151  *              return NULL;
152  *      }
153  *      return io_new_listener(NULL, fd, conn_init, (const char *)"listened!");
154  * }
155  */
156 #define io_new_listener(ctx, fd, init, arg)                             \
157         io_new_listener_((ctx), (fd),                                   \
158                          typesafe_cb_preargs(struct io_plan *, void *,  \
159                                              (init), (arg),             \
160                                              struct io_conn *conn),     \
161                          (void *)(arg))
162 struct io_listener *io_new_listener_(const tal_t *ctx, int fd,
163                                      struct io_plan *(*init)(struct io_conn *,
164                                                              void *),
165                                      void *arg);
166
167 /**
168  * io_close_listener - delete a listener.
169  * @listener: the listener returned from io_new_listener.
170  *
171  * This closes the fd and frees @listener.
172  *
173  * Example:
174  * ...
175  *      struct io_listener *l = do_listen("8111");
176  *      if (l) {
177  *              io_loop(NULL, NULL);
178  *              io_close_listener(l);
179  *      }
180  */
181 void io_close_listener(struct io_listener *listener);
182
183 /**
184  * io_write - output plan to write data.
185  * @conn: the connection that plan is for.
186  * @data: the data buffer.
187  * @len: the length to write.
188  * @next: function to call output is done.
189  * @arg: @next argument
190  *
191  * This updates the output plan, to write out a data buffer.  Once it's all
192  * written, the @next function will be called: on an error, the finish
193  * function is called instead.
194  *
195  * Note that the I/O may actually be done immediately.
196  *
197  * Example:
198  * static struct io_plan *write_to_conn(struct io_conn *conn, const char *msg)
199  * {
200  *      // Write message, then close.
201  *      return io_write(conn, msg, strlen(msg), io_close_cb, NULL);
202  * }
203  */
204 #define io_write(conn, data, len, next, arg)                            \
205         io_write_((conn), (data), (len),                                \
206                   typesafe_cb_preargs(struct io_plan *, void *,         \
207                                       (next), (arg), struct io_conn *), \
208                   (arg))
209 struct io_plan *io_write_(struct io_conn *conn,
210                           const void *data, size_t len,
211                           struct io_plan *(*next)(struct io_conn *, void *),
212                           void *arg);
213
214 /**
215  * io_read - input plan to read data.
216  * @conn: the connection that plan is for.
217  * @data: the data buffer.
218  * @len: the length to read.
219  * @next: function to call once input is done.
220  * @arg: @next argument
221  *
222  * This creates a plan to read data into a buffer.  Once it's all
223  * read, the @next function will be called: on an error, the finish
224  * function is called instead.
225  *
226  * Note that the I/O may actually be done immediately.
227  *
228  * Example:
229  * static struct io_plan *read_from_conn(struct io_conn *conn, char *buf)
230  * {
231  *      // Read message, then close.
232  *      return io_read(conn, buf, 12, io_close_cb, NULL);
233  * }
234  */
235 #define io_read(conn, data, len, next, arg)                             \
236         io_read_((conn), (data), (len),                                 \
237                  typesafe_cb_preargs(struct io_plan *, void *,          \
238                                      (next), (arg), struct io_conn *),  \
239                  (arg))
240 struct io_plan *io_read_(struct io_conn *conn,
241                          void *data, size_t len,
242                          struct io_plan *(*next)(struct io_conn *, void *),
243                          void *arg);
244
245
246 /**
247  * io_read_partial - input plan to read some data.
248  * @conn: the connection that plan is for.
249  * @data: the data buffer.
250  * @maxlen: the maximum length to read
251  * @lenp: set to the length actually read.
252  * @next: function to call once input is done.
253  * @arg: @next argument
254  *
255  * This creates a plan to read data into a buffer.  Once any data is
256  * read, @len is updated and the @next function will be called: on an
257  * error, the finish function is called instead.
258  *
259  * Note that the I/O may actually be done immediately.
260  *
261  * Example:
262  * struct buf {
263  *      size_t len;
264  *      char buf[12];
265  * };
266  *
267  * static struct io_plan *dump(struct io_conn *conn, struct buf *b)
268  * {
269  *      printf("Partial read: '%*s'\n", (int)b->len, b->buf);
270  *      free(b);
271  *      return io_close(conn);
272  * }
273  *
274  * static struct io_plan *read_part(struct io_conn *conn, struct buf *b)
275  * {
276  *      // Read message, then dump and close.
277  *      return io_read_partial(conn, b->buf, sizeof(b->buf), &b->len, dump, b);
278  * }
279  */
280 #define io_read_partial(conn, data, maxlen, lenp, next, arg)            \
281         io_read_partial_((conn), (data), (maxlen), (lenp),              \
282                          typesafe_cb_preargs(struct io_plan *, void *,  \
283                                              (next), (arg),             \
284                                              struct io_conn *),         \
285                          (arg))
286 struct io_plan *io_read_partial_(struct io_conn *conn,
287                                  void *data, size_t maxlen, size_t *lenp,
288                                  struct io_plan *(*next)(struct io_conn *,
289                                                          void *),
290                                  void *arg);
291
292 /**
293  * io_write_partial - output plan to write some data.
294  * @conn: the connection that plan is for.
295  * @data: the data buffer.
296  * @maxlen: the maximum length to write
297  * @lenp: set to the length actually written.
298  * @next: function to call once output is done.
299  * @arg: @next argument
300  *
301  * This creates a plan to write data from a buffer.   Once any data is
302  * written, @len is updated and the @next function will be called: on an
303  * error, the finish function is called instead.
304  *
305  * Note that the I/O may actually be done immediately.
306  *
307  * Example:
308  * struct buf {
309  *      size_t len;
310  *      char buf[12];
311  * };
312  *
313  * static struct io_plan *show_partial(struct io_conn *conn, struct buf *b)
314  * {
315  *      printf("Only wrote: '%*s'\n", (int)b->len, b->buf);
316  *      free(b);
317  *      return io_close(conn);
318  * }
319  *
320  * static struct io_plan *write_part(struct io_conn *conn, struct buf *b)
321  * {
322  *      // Write message, then dump and close.
323  *      strcpy(b->buf, "Hello world");
324  *      return io_write_partial(conn, b->buf, strlen(b->buf),
325  *                              &b->len, show_partial, b);
326  * }
327  */
328 #define io_write_partial(conn, data, maxlen, lenp, next, arg)           \
329         io_write_partial_((conn), (data), (maxlen), (lenp),             \
330                           typesafe_cb_preargs(struct io_plan *, void *, \
331                                               (next), (arg),            \
332                                               struct io_conn *),        \
333                           (arg))
334 struct io_plan *io_write_partial_(struct io_conn *conn,
335                                   const void *data, size_t maxlen, size_t *lenp,
336                                   struct io_plan *(*next)(struct io_conn *,
337                                                           void*),
338                                   void *arg);
339
340 /**
341  * io_always - plan to immediately call next callback
342  * @conn: the connection that plan is for.
343  * @next: function to call.
344  * @arg: @next argument
345  *
346  * Sometimes it's neater to plan a callback rather than call it directly;
347  * for example, if you only need to read data for one path and not another.
348  *
349  * Example:
350  * static struct io_plan *init_conn_with_nothing(struct io_conn *conn,
351  *                                               void *unused)
352  * {
353  *      // Silly example: close on next time around loop.
354  *      return io_always(conn, io_close_cb, NULL);
355  * }
356  */
357 #define io_always(conn, next, arg)                                      \
358         io_always_((conn), typesafe_cb_preargs(struct io_plan *, void *, \
359                                                (next), (arg),           \
360                                                struct io_conn *),       \
361                    (arg))
362
363 struct io_plan *io_always_(struct io_conn *conn,
364                            struct io_plan *(*next)(struct io_conn *, void *),
365                            void *arg);
366
367 /**
368  * io_out_always - output plan to immediately call next callback
369  * @conn: the connection that plan is for.
370  * @next: function to call.
371  * @arg: @next argument
372  *
373  * This is a variant of io_always() which uses the output plan; it only
374  * matters if you are using io_duplex, and thus have two plans running at
375  * once.
376  */
377 #define io_out_always(conn, next, arg)                                  \
378         io_out_always_((conn), typesafe_cb_preargs(struct io_plan *, void *, \
379                                                    (next), (arg),       \
380                                                    struct io_conn *),   \
381                        (arg))
382
383 struct io_plan *io_out_always_(struct io_conn *conn,
384                                struct io_plan *(*next)(struct io_conn *,
385                                                        void *),
386                                void *arg);
387
388 /**
389  * io_connect - create an asynchronous connection to a listening socket.
390  * @conn: the connection that plan is for.
391  * @addr: where to connect.
392  * @init: function to call once it's connected
393  * @arg: @init argument
394  *
395  * This initiates a connection, and creates a plan for
396  * (asynchronously) completing it.  Once complete, the @init function
397  * will be called.
398  *
399  * Example:
400  * #include <sys/types.h>
401  * #include <sys/socket.h>
402  * #include <netdb.h>
403  *
404  * // Write, then close socket.
405  * static struct io_plan *init_connect(struct io_conn *conn,
406  *                                     struct addrinfo *addrinfo)
407  * {
408  *      return io_connect(conn, addrinfo, io_close_cb, NULL);
409  * }
410  *
411  * ...
412  *
413  *      int fd;
414  *      struct addrinfo *addrinfo;
415  *
416  *      fd = socket(AF_INET, SOCK_STREAM, 0);
417  *      getaddrinfo("localhost", "8111", NULL, &addrinfo);
418  *      io_new_conn(NULL, fd, init_connect, addrinfo);
419  */
420 struct addrinfo;
421 #define io_connect(conn, addr, next, arg)                               \
422         io_connect_((conn), (addr),                                     \
423                     typesafe_cb_preargs(struct io_plan *, void *,       \
424                                         (next), (arg),                  \
425                                         struct io_conn *),              \
426                     (arg))
427
428 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
429                             struct io_plan *(*next)(struct io_conn *, void *),
430                             void *arg);
431
432 /**
433  * io_duplex - set plans for both input and output.
434  * @conn: the connection that plan is for.
435  * @in: the input plan
436  * @out: the output plan
437  *
438  * Most plans are either for input or output; io_duplex creates a plan
439  * which does both.  This is often used in the init function to create
440  * two independent streams, though it can be used once on any connection.
441  *
442  * Note that if either plan closes the connection, it will be closed.
443  *
444  * Example:
445  * struct buf {
446  *      char in[100];
447  *      char out[100];
448  * };
449  *
450  * static struct io_plan *read_and_write(struct io_conn *conn, struct buf *b)
451  * {
452  *      return io_duplex(conn,
453  *                       io_read(conn, b->in, sizeof(b->in), io_close_cb, b),
454  *                       io_write(conn, b->out, sizeof(b->out), io_close_cb,b));
455  * }
456  */
457 #define io_duplex(conn, in_plan, out_plan) \
458         (io_duplex_prepare(conn), io_duplex_(in_plan, out_plan))
459
460 struct io_plan *io_duplex_(struct io_plan *in_plan, struct io_plan *out_plan);
461 void io_duplex_prepare(struct io_conn *conn);
462
463 /**
464  * io_halfclose - close half of an io_duplex connection.
465  * @conn: the connection that plan is for.
466  *
467  * It's common to want to close a duplex connection after both input and
468  * output plans have completed.  If either calls io_close() the connection
469  * closes immediately.  Instead, io_halfclose() needs to be called twice.
470  *
471  * Example:
472  * struct buf {
473  *      char in[100];
474  *      char out[100];
475  * };
476  *
477  * static struct io_plan *finish(struct io_conn *conn, struct buf *b)
478  * {
479  *      return io_halfclose(conn);
480  * }
481  *
482  * static struct io_plan *read_and_write(struct io_conn *conn, struct buf *b)
483  * {
484  *      return io_duplex(conn,
485  *                       io_read(conn, b->in, sizeof(b->in), finish, b),
486  *                       io_write(conn, b->out, sizeof(b->out), finish, b));
487  * }
488  */
489 struct io_plan *io_halfclose(struct io_conn *conn);
490
491 /**
492  * io_wait - leave a plan idle until something wakes us.
493  * @conn: the connection that plan is for.
494  * @waitaddr: the address to wait on.
495  * @next: function to call after waiting.
496  * @arg: @next argument
497  *
498  * This leaves the input or output idle: io_wake(@waitaddr) will be
499  * called later to restart the connection.
500  *
501  * Example:
502  * // Silly example to wait then close.
503  * static struct io_plan *wait(struct io_conn *conn, void *b)
504  * {
505  *      return io_wait(conn, b, io_close_cb, NULL);
506  * }
507  */
508 #define io_wait(conn, waitaddr, next, arg)                              \
509         io_wait_((conn), (waitaddr),                                    \
510                  typesafe_cb_preargs(struct io_plan *, void *,          \
511                                      (next), (arg),                     \
512                                      struct io_conn *),                 \
513                  (arg))
514
515 struct io_plan *io_wait_(struct io_conn *conn,
516                          const void *wait,
517                          struct io_plan *(*next)(struct io_conn *, void *),
518                          void *arg);
519
520
521 /**
522  * io_out_wait - leave the output plan idle until something wakes us.
523  * @conn: the connection that plan is for.
524  * @waitaddr: the address to wait on.
525  * @next: function to call after waiting.
526  * @arg: @next argument
527  *
528  * io_wait() makes the input plan idle: if you're not using io_duplex it
529  * doesn't matter which plan is waiting.  Otherwise, you may need to use
530  * io_out_wait() instead, to specify explicitly that the output plan is
531  * waiting.
532  */
533 #define io_out_wait(conn, waitaddr, next, arg)                          \
534         io_out_wait_((conn), (waitaddr),                                \
535                      typesafe_cb_preargs(struct io_plan *, void *,      \
536                                          (next), (arg),                 \
537                                          struct io_conn *),             \
538                      (arg))
539
540 struct io_plan *io_out_wait_(struct io_conn *conn,
541                              const void *wait,
542                              struct io_plan *(*next)(struct io_conn *, void *),
543                              void *arg);
544
545 /**
546  * io_wake - wake up any connections waiting on @wait
547  * @waitaddr: the address to trigger.
548  *
549  * All io_conns who have returned io_wait() on @waitaddr will move on
550  * to their next callback.
551  *
552  * Example:
553  * static struct io_plan *wake_it(struct io_conn *conn, void *b)
554  * {
555  *      io_wake(b);
556  *      return io_close(conn);
557  * }
558  */
559 void io_wake(const void *wait);
560
561 /**
562  * io_break - return from io_loop()
563  * @ret: non-NULL value to return from io_loop().
564  *
565  * This breaks out of the io_loop.  As soon as the current function
566  * returns, any io_close()'d connections will have their finish
567  * callbacks called, then io_loop() with return with @ret.
568  *
569  * If io_loop() is called again, then @plan will be carried out.
570  *
571  * Example:
572  *      static struct io_plan *fail_on_timeout(struct io_conn *conn, char *msg)
573  *      {
574  *              io_break(msg);
575  *              return io_close(conn);
576  *      }
577  */
578 void io_break(const void *ret);
579
580 /**
581  * io_never - assert if callback is called.
582  * @conn: the connection that plan is for.
583  * @unused: an unused parameter to make this suitable for use as a callback.
584  *
585  * Sometimes you want to make it clear that a callback should never happen
586  * (eg. for io_break).  This will assert() if called.
587  *
588  * Example:
589  * static struct io_plan *break_out(struct io_conn *conn, void *unused)
590  * {
591  *      io_break(conn);
592  *      // We won't ever return from io_break
593  *      return io_never(conn, NULL);
594  * }
595  */
596 struct io_plan *io_never(struct io_conn *conn, void *unused);
597
598 /* FIXME: io_recvfrom/io_sendto */
599
600 /**
601  * io_close - plan to close a connection.
602  * @conn: the connection to close.
603  *
604  * On return to io_loop, the connection will be closed.  It doesn't have
605  * to be the current connection and it doesn't need to be idle.  No more
606  * IO or callbacks will occur.
607  *
608  * You can close a connection twice without harmful effects.
609  *
610  * Example:
611  * static struct io_plan *close_on_timeout(struct io_conn *conn, const char *msg)
612  * {
613  *      printf("closing: %s\n", msg);
614  *      return io_close(conn);
615  * }
616  */
617 struct io_plan *io_close(struct io_conn *conn);
618
619 /**
620  * io_close_cb - helper callback to close a connection.
621  * @conn: the connection.
622  *
623  * This schedules a connection to be closed; designed to be used as
624  * a callback function.
625  *
626  * Example:
627  *      #define close_on_timeout io_close_cb
628  */
629 struct io_plan *io_close_cb(struct io_conn *, void *unused);
630
631 /**
632  * io_loop - process fds until all closed on io_break.
633  * @timers - timers which are waiting to go off (or NULL for none)
634  * @expired - an expired timer (can be NULL if @timers is)
635  *
636  * This is the core loop; it exits with the io_break() arg, or NULL if
637  * all connections and listeners are closed, or with @expired set to an
638  * expired timer (if @timers isn't NULL).
639  *
640  * Example:
641  *      io_loop(NULL, NULL);
642  */
643 void *io_loop(struct timers *timers, struct timer **expired);
644
645 /**
646  * io_conn_fd - get the fd from a connection.
647  * @conn: the connection.
648  *
649  * Sometimes useful, eg for getsockname().
650  */
651 int io_conn_fd(const struct io_conn *conn);
652
653 /**
654  * io_time_override - override the normal call for time.
655  * @nowfn: the function to call.
656  *
657  * io usually uses time_now() internally, but this forces it
658  * to use your function (eg. for debugging).  Returns the old
659  * one.
660  */
661 struct timeabs (*io_time_override(struct timeabs (*now)(void)))(void);
662
663 /**
664  * io_set_debug - set synchronous mode on a connection.
665  * @conn: the connection.
666  * @debug: whether to enable or disable debug.
667  *
668  * Once @debug is true on a connection, all I/O is done synchronously
669  * as soon as it is set, until it is unset or @conn is closed.  This
670  * makes it easy to debug what's happening with a connection, but note
671  * that other connections are starved while this is being done.
672  *
673  * See also: io_debug_complete()
674  *
675  * Example:
676  * // Dumb init function to set debug and tell conn to close.
677  * static struct io_plan *conn_init(struct io_conn *conn, const char *msg)
678  * {
679  *      io_set_debug(conn, true);
680  *      return io_close(conn);
681  * }
682  */
683 void io_set_debug(struct io_conn *conn, bool debug);
684
685 /**
686  * io_debug_complete - empty function called when conn is closing/waiting.
687  * @conn: the connection.
688  *
689  * This is for putting a breakpoint onto, when debugging.  It is called
690  * when a conn with io_set_debug() true can no longer be synchronous:
691  * 1) It is io_close()'d
692  * 2) It enters io_wait() (sychronous debug will resume after io_wake())
693  * 3) io_break() is called (sychronous debug will resume after io_loop())
694  */
695 void io_debug_complete(struct io_conn *conn);
696 #endif /* CCAN_IO_H */