6 * io/fdpass - IO helper for passing file descriptors across local sockets
8 * This code adds the ability to pass file descriptors to ccan/io.
10 * License: LGPL (v2.1 or any later version)
11 * Author: Rusty Russell <rusty@rustcorp.com.au>
14 * // Given "hello" outputs hello
15 * #include <ccan/io/fdpass/fdpass.h>
16 * #include <sys/types.h>
17 * #include <sys/socket.h>
23 * // Child reads stdin into the buffer, prints it out.
28 * static struct io_plan *read_more(struct io_conn *conn, struct buf *buf)
30 * printf("%.*s", (int)buf->used, buf->c);
31 * return io_read_partial(conn, buf->c, sizeof(buf->c), &buf->used,
35 * // Child has received fd, start reading loop.
36 * static struct io_plan *got_infd(struct io_conn *conn, int *infd)
38 * struct buf *buf = calloc(1, sizeof(*buf));
40 * io_new_conn(NULL, *infd, read_more, buf);
41 * return io_close(conn);
43 * // Child is receiving the fd to read into.
44 * static struct io_plan *recv_infd(struct io_conn *conn, int *infd)
46 * return io_recv_fd(conn, infd, got_infd, infd);
49 * // Gets passed fd (stdin), which it reads from.
50 * static void child(int sockfd)
54 * io_new_conn(NULL, sockfd, recv_infd, &infd);
55 * io_loop(NULL, NULL);
59 * static struct io_plan *send_stdin(struct io_conn *conn, void *unused)
61 * return io_send_fd(conn, STDIN_FILENO, io_close_cb, NULL);
64 * static void parent(int sockfd)
66 * io_new_conn(NULL, sockfd, send_stdin, NULL);
67 * io_loop(NULL, NULL);
75 * socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
82 int main(int argc, char *argv[])
84 /* Expect exactly one argument */
88 if (strcmp(argv[1], "depends") == 0) {
89 printf("ccan/fdpass\n");