Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / stream_send_fd.c
blob3b906e01cc01fae3e9e6558949f4199b8587baaa
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* stream_send_fd 3
6 /* SUMMARY
7 /* send file descriptor
8 /* SYNOPSIS
9 /* #include <iostuff.h>
11 /* int stream_send_fd(fd, sendfd)
12 /* int fd;
13 /* int sendfd;
14 /* DESCRIPTION
15 /* stream_send_fd() sends a file descriptor over the specified
16 /* stream.
18 /* Arguments:
19 /* .IP fd
20 /* File descriptor.
21 /* .IP sendfd
22 /* Another file descriptor.
23 /* DIAGNOSTICS
24 /* stream_send_fd() returns -1 upon failure.
25 /* LICENSE
26 /* .ad
27 /* .fi
28 /* The Secure Mailer license must be distributed with this software.
29 /* AUTHOR(S)
30 /* Wietse Venema
31 /* IBM T.J. Watson Research
32 /* P.O. Box 704
33 /* Yorktown Heights, NY 10598, USA
34 /*--*/
36 /* System library. */
38 #include <sys_defs.h> /* includes <sys/types.h> */
40 #ifdef STREAM_CONNECTIONS
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <errno.h>
46 #include <stropts.h>
48 #endif
50 /* Utility library. */
52 #include <msg.h>
53 #include <iostuff.h>
55 /* stream_send_fd - send file descriptor */
57 int stream_send_fd(int fd, int sendfd)
59 #ifdef STREAM_CONNECTIONS
60 const char *myname = "stream_send_fd";
62 if (ioctl(fd, I_SENDFD, sendfd) < 0)
63 msg_fatal("%s: send file descriptor %d: %m", myname, sendfd);
64 return (0);
65 #else
66 msg_fatal("stream connections are not implemented");
67 #endif
70 #ifdef TEST
73 * Proof-of-concept program. Open a file and send the descriptor, presumably
74 * to the stream_recv_fd test program.
76 #include <unistd.h>
77 #include <fcntl.h>
78 #include <string.h>
79 #include <stdlib.h>
80 #include <split_at.h>
81 #include <connect.h>
83 int main(int argc, char **argv)
85 char *transport;
86 char *endpoint;
87 char *path;
88 int server_sock;
89 int client_fd;
91 if (argc < 3
92 || (endpoint = split_at(transport = argv[1], ':')) == 0
93 || *endpoint == 0 || *transport == 0)
94 msg_fatal("usage: %s transport:endpoint file...", argv[0]);
96 if (strcmp(transport, "stream") == 0) {
97 server_sock = stream_connect(endpoint, BLOCKING, 0);
98 } else {
99 msg_fatal("invalid transport name: %s", transport);
101 if (server_sock < 0)
102 msg_fatal("connect %s:%s: %m", transport, endpoint);
104 argv += 2;
105 while ((path = *argv++) != 0) {
106 if ((client_fd = open(path, O_RDONLY, 0)) < 0)
107 msg_fatal("open %s: %m", path);
108 msg_info("path=%s client_fd=%d", path, client_fd);
109 if (stream_send_fd(server_sock, client_fd) < 0)
110 msg_fatal("send file descriptor: %m");
111 if (close(client_fd) != 0)
112 msg_fatal("close(%d): %m", client_fd);
114 exit(0);
117 #endif