]> git.ozlabs.org Git - ccan/blob - ccan/io/fdpass/_info
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / io / fdpass / _info
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.h>
4
5 /**
6  * io/fdpass - IO helper for passing file descriptors across local sockets
7  *
8  * This code adds the ability to pass file descriptors to ccan/io.
9  *
10  * License: LGPL (v2.1 or any later version)
11  * Author: Rusty Russell <rusty@rustcorp.com.au>
12  *
13  * Example:
14  *      // Given "hello" outputs hello
15  *      #include <ccan/io/fdpass/fdpass.h>
16  *      #include <sys/types.h>
17  *      #include <sys/socket.h>
18  *      #include <sys/un.h>
19  *      #include <stdio.h>
20  *      #include <stdlib.h>
21  *      #include <unistd.h>
22  *
23  *      // Child reads stdin into the buffer, prints it out.
24  *      struct buf {
25  *              size_t used;
26  *              char c[100];
27  *      };
28  *      static struct io_plan *read_more(struct io_conn *conn, struct buf *buf)
29  *      {
30  *              printf("%.*s", (int)buf->used, buf->c);
31  *              return io_read_partial(conn, buf->c, sizeof(buf->c), &buf->used,
32  *                                      read_more, buf);
33  *      }
34  *
35  *      // Clean up allocation so -fsanitize=address doesn't see leak!
36  *      static void free_buf(struct io_conn *c, struct buf *buf)
37  *      {
38  *              free(buf);
39  *      }
40  *
41  *      // Child has received fd, start reading loop.
42  *      static struct io_plan *got_infd(struct io_conn *conn, int *infd)
43  *      {
44  *              struct buf *buf = calloc(1, sizeof(*buf));
45  *              struct io_conn *new_conn;
46  *
47  *              new_conn = io_new_conn(NULL, *infd, read_more, buf);
48  *              io_set_finish(new_conn, free_buf, buf);
49  *              return io_close(conn);
50  *      }
51  *      // Child is receiving the fd to read into. 
52  *      static struct io_plan *recv_infd(struct io_conn *conn, int *infd)
53  *      {
54  *              return io_recv_fd(conn, infd, got_infd, infd);
55  *      }
56  *
57  *      // Gets passed fd (stdin), which it reads from.
58  *      static void child(int sockfd)
59  *      {
60  *              int infd;
61  *
62  *              io_new_conn(NULL, sockfd, recv_infd, &infd);
63  *              io_loop(NULL, NULL);
64  *              exit(0);
65  *      }
66  *
67  *      static struct io_plan *send_stdin(struct io_conn *conn, void *unused)
68  *      {
69  *              return io_send_fd(conn, STDIN_FILENO, false, io_close_cb, NULL);
70  *      }
71  *
72  *      static void parent(int sockfd)
73  *      {
74  *              io_new_conn(NULL, sockfd, send_stdin, NULL);
75  *              io_loop(NULL, NULL);
76  *              exit(0);
77  *      }
78  *      
79  *      int main(void)
80  *      {
81  *              int sv[2];
82  *      
83  *              socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
84  *              if (fork() == 0)
85  *                      child(sv[0]);
86  *              else
87  *                      parent(sv[1]);
88  *      }
89  */
90 int main(int argc, char *argv[])
91 {
92         /* Expect exactly one argument */
93         if (argc != 2)
94                 return 1;
95
96         if (strcmp(argv[1], "depends") == 0) {
97                 printf("ccan/fdpass\n");
98                 printf("ccan/io\n");
99                 return 0;
100         }
101
102         return 1;
103 }