]> git.ozlabs.org Git - ccan/blob - ccan/io/fdpass/fdpass.c
io/fdpass: new module for async fd passing.
[ccan] / ccan / io / fdpass / fdpass.c
1 /* GNU LGPL version 2 (or later) - see LICENSE file for details */
2 #include <ccan/io/fdpass/fdpass.h>
3 #include <ccan/fdpass/fdpass.h>
4 #include <ccan/io/io_plan.h>
5 #include <errno.h>
6
7 static int do_fd_send(int fd, struct io_plan_arg *arg)
8 {
9         if (!fdpass_send(fd, arg->u1.s)) {
10                 /* In case ccan/io ever gets smart with non-blocking. */
11                 if (errno == EAGAIN || errno == EWOULDBLOCK)
12                         return 0;
13                 return -1;
14         }
15         return 1;
16 }
17
18 struct io_plan *io_send_fd_(struct io_conn *conn,
19                             int fd,
20                             struct io_plan *(*next)(struct io_conn *, void *),
21                             void *next_arg)
22 {
23         struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
24
25         arg->u1.s = fd;
26
27         return io_set_plan(conn, IO_OUT, do_fd_send, next, next_arg);
28 }
29
30 static int do_fd_recv(int fd, struct io_plan_arg *arg)
31 {
32         int fdin = fdpass_recv(fd);
33
34         if (fdin < 0) {
35                 /* In case ccan/io ever gets smart with non-blocking. */
36                 if (errno == EAGAIN || errno == EWOULDBLOCK)
37                         return 0;
38                 return -1;
39         }
40         *(int *)arg->u1.vp = fdin;
41         return 1;
42 }
43
44 struct io_plan *io_recv_fd_(struct io_conn *conn,
45                             int *fd,
46                             struct io_plan *(*next)(struct io_conn *, void *),
47                             void *next_arg)
48 {
49         struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
50
51         arg->u1.vp = fd;
52
53         return io_set_plan(conn, IO_IN, do_fd_recv, next, next_arg);
54 }