7 /* receive file descriptor
9 /* #include <iostuff.h>
11 /* int stream_recv_fd(fd)
14 /* stream_recv_fd() receives a file descriptor via the specified
15 /* stream. The result value is the received descriptor.
21 /* stream_recv_fd() returns -1 upon failure.
25 /* The Secure Mailer license must be distributed with this software.
28 /* IBM T.J. Watson Research
30 /* Yorktown Heights, NY 10598, USA
35 #include <sys_defs.h> /* includes <sys/types.h> */
37 #ifdef STREAM_CONNECTIONS
47 /* Utility library. */
52 /* stream_recv_fd - receive file descriptor */
54 int stream_recv_fd(int fd
)
56 #ifdef STREAM_CONNECTIONS
57 struct strrecvfd fdinfo
;
60 * This will return EAGAIN on a non-blocking stream when someone else
61 * snatched the connection from us.
63 if (ioctl(fd
, I_RECVFD
, &fdinfo
) < 0)
67 msg_fatal("stream connections are not implemented");
74 * Proof-of-concept program. Receive a descriptor (presumably from the
75 * stream_send_fd test program) and copy its content until EOF.
83 int main(int argc
, char **argv
)
94 || (endpoint
= split_at(transport
= argv
[1], ':')) == 0
95 || *endpoint
== 0 || *transport
== 0)
96 msg_fatal("usage: %s transport:endpoint", argv
[0]);
98 if (strcmp(transport
, "stream") == 0) {
99 listen_sock
= stream_listen(endpoint
, BLOCKING
, 0);
101 msg_fatal("invalid transport name: %s", transport
);
104 msg_fatal("listen %s:%s: %m", transport
, endpoint
);
106 client_sock
= stream_accept(listen_sock
);
108 msg_fatal("stream_accept: %m");
110 while ((client_fd
= stream_recv_fd(client_sock
)) >= 0) {
111 msg_info("client_fd = %d", client_fd
);
112 while ((read_count
= read(client_fd
, buf
, sizeof(buf
))) > 0)
113 write(1, buf
, read_count
);
115 msg_fatal("read: %m");
116 if (close(client_fd
) != 0)
117 msg_fatal("close(%d): %m", client_fd
);