fdpass: new module.
[ccan] / ccan / fdpass / _info
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.h>
4
5 /**
6  * fdpass - routines to pass a file descriptor over a socket.
7  *
8  * This code handles all the hairy details of fd passing.
9  *
10  * License: CC0 (Public domain)
11  * Maintainer: Rusty Russell <rusty@rustcorp.com.au>
12  *
13  * Example:
14  *      // Outputs hello!
15  *      #include <ccan/fdpass/fdpass.h>
16  *      #include <sys/socket.h>
17  *      #include <sys/un.h>
18  *      #include <stdio.h>
19  *      #include <stdlib.h>
20  *      #include <unistd.h>
21  *      
22  *      static void child(int sockfd)
23  *      {
24  *              char buffer[6];
25  *              int newfd = fdpass_recv(sockfd);
26  *              read(newfd, buffer, sizeof(buffer));
27  *              printf("%.*s\n", (int)sizeof(buffer), buffer);
28  *              exit(0);
29  *      }
30  *      
31  *      static void parent(int sockfd)
32  *      {
33  *              int pfds[2];
34  *      
35  *              pipe(pfds);
36  *              fdpass_send(sockfd, pfds[0]);
37  *              close(pfds[0]);
38  *              write(pfds[1], "hello!", 6);
39  *              exit(0);
40  *      }
41  *      
42  *      int main(void)
43  *      {
44  *              int sv[2];
45  *      
46  *              socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
47  *              if (fork() == 0)
48  *                      child(sv[0]);
49  *              else
50  *                      parent(sv[1]);
51  *      }
52  */
53 int main(int argc, char *argv[])
54 {
55         /* Expect exactly one argument */
56         if (argc != 2)
57                 return 1;
58
59         if (strcmp(argv[1], "depends") == 0)
60                 return 0;
61
62         return 1;
63 }
64
65