]> git.ozlabs.org Git - ccan/blob - ccan/io/fdpass/fdpass.h
366ff3521606e23e182ed5073b4378d12fdc98dd
[ccan] / ccan / io / fdpass / fdpass.h
1 /* GNU LGPL version 2 (or later) - see LICENSE file for details */
2 #ifndef CCAN_IO_FDPASS_H
3 #define CCAN_IO_FDPASS_H
4 #include <ccan/io/io.h>
5
6 /**
7  * io_send_fd - output plan to send a file descriptor
8  * @conn: the connection that plan is for.
9  * @fd: the file descriptor to pass.
10  * @next: function to call output is done.
11  * @arg: @next argument
12  *
13  * This updates the output plan, to write out a file descriptor.  This
14  * usually only works over an AF_LOCAL (ie. Unix domain) socket.  Once
15  * that's sent, the @next function will be called: on an error, the
16  * finish function is called instead.
17  *
18  * Note that the I/O may actually be done immediately, and the other end
19  * of the socket must use io_recv_fd: if it does a normal read, the file
20  * descriptor will be lost.
21  *
22  * Example:
23  * static struct io_plan *fd_to_conn(struct io_conn *conn, int fd)
24  * {
25  *      // Write fd, then close.
26  *      return io_send_fd(conn, fd, io_close_cb, NULL);
27  * }
28  */
29 #define io_send_fd(conn, fd, next, arg)                                 \
30         io_send_fd_((conn), (fd),                                       \
31                     typesafe_cb_preargs(struct io_plan *, void *,       \
32                                         (next), (arg), struct io_conn *), \
33                     (arg))
34 struct io_plan *io_send_fd_(struct io_conn *conn,
35                             int fd,
36                             struct io_plan *(*next)(struct io_conn *, void *),
37                             void *arg);
38
39 /**
40  * io_recv_fd - input plan to receive a file descriptor
41  * @conn: the connection that plan is for.
42  * @fd: a pointer to where to place to file descriptor
43  * @next: function to call once input is done.
44  * @arg: @next argument
45  *
46  * This creates a plan to receive a file descriptor, as sent by
47  * io_send_fd.  Once it's all read, the @next function will be called:
48  * on an error, the finish function is called instead.
49  *
50  * Note that the I/O may actually be done immediately.
51  *
52  * Example:
53  * static struct io_plan *read_from_conn(struct io_conn *conn, int *fdp)
54  * {
55  *      // Read message, then close.
56  *      return io_recv_fd(conn, fdp, io_close_cb, NULL);
57  * }
58  */
59 #define io_recv_fd(conn, fd, next, arg)                                 \
60         io_recv_fd_((conn), (fd),                                       \
61                     typesafe_cb_preargs(struct io_plan *, void *,       \
62                                         (next), (arg), struct io_conn *), \
63                     (arg))
64 struct io_plan *io_recv_fd_(struct io_conn *conn,
65                             int *fd,
66                             struct io_plan *(*next)(struct io_conn *, void *),
67                             void *arg);
68 #endif /* CCAN_IO_FDPASS_H */