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