Sync usage with man page.
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / stream_recv_fd.c
blob90f4277ab8cd15aaf113363327e2403e78e266fb
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* stream_recv_fd 3
6 /* SUMMARY
7 /* receive file descriptor
8 /* SYNOPSIS
9 /* #include <iostuff.h>
11 /* int stream_recv_fd(fd)
12 /* int fd;
13 /* DESCRIPTION
14 /* stream_recv_fd() receives a file descriptor via the specified
15 /* stream. The result value is the received descriptor.
17 /* Arguments:
18 /* .IP fd
19 /* File descriptor.
20 /* DIAGNOSTICS
21 /* stream_recv_fd() returns -1 upon failure.
22 /* LICENSE
23 /* .ad
24 /* .fi
25 /* The Secure Mailer license must be distributed with this software.
26 /* AUTHOR(S)
27 /* Wietse Venema
28 /* IBM T.J. Watson Research
29 /* P.O. Box 704
30 /* Yorktown Heights, NY 10598, USA
31 /*--*/
33 /* System library. */
35 #include <sys_defs.h> /* includes <sys/types.h> */
37 #ifdef STREAM_CONNECTIONS
39 #include <sys/stat.h>
40 #include <unistd.h>
41 #include <errno.h>
42 #include <stropts.h>
43 #include <fcntl.h>
45 #endif
47 /* Utility library. */
49 #include <msg.h>
50 #include <iostuff.h>
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)
64 return (-1);
65 return (fdinfo.fd);
66 #else
67 msg_fatal("stream connections are not implemented");
68 #endif
71 #ifdef TEST
74 * Proof-of-concept program. Receive a descriptor (presumably from the
75 * stream_send_fd test program) and copy its content until EOF.
77 #include <unistd.h>
78 #include <string.h>
79 #include <stdlib.h>
80 #include <split_at.h>
81 #include <listen.h>
83 int main(int argc, char **argv)
85 char *transport;
86 char *endpoint;
87 int listen_sock;
88 int client_sock;
89 int client_fd;
90 ssize_t read_count;
91 char buf[1024];
93 if (argc != 2
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);
100 } else {
101 msg_fatal("invalid transport name: %s", transport);
103 if (listen_sock < 0)
104 msg_fatal("listen %s:%s: %m", transport, endpoint);
106 client_sock = stream_accept(listen_sock);
107 if (client_sock < 0)
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);
114 if (read_count < 0)
115 msg_fatal("read: %m");
116 if (close(client_fd) != 0)
117 msg_fatal("close(%d): %m", client_fd);
119 exit(0);
122 #endif