xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/stream_send_fd.c (revision ff6d749d9964a9b179f9760ce6992137cffbaefe)
1 /*	$NetBSD: stream_send_fd.c,v 1.1.1.2 2011/03/02 19:32:45 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	stream_send_fd 3
6 /* SUMMARY
7 /*	send file descriptor
8 /* SYNOPSIS
9 /*	#include <iostuff.h>
10 /*
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.
17 /*
18 /*	Arguments:
19 /* .IP fd
20 /*	File descriptor that connects the sending and receiving processes.
21 /* .IP sendfd
22 /*	The file descriptor to be sent.
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 /*--*/
35 
36 /* System library. */
37 
38 #include <sys_defs.h>			/* includes <sys/types.h> */
39 
40 #ifdef STREAM_CONNECTIONS
41 
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <errno.h>
46 #include <stropts.h>
47 
48 #endif
49 
50 /* Utility library. */
51 
52 #include <msg.h>
53 #include <iostuff.h>
54 
55 /* stream_send_fd - send file descriptor */
56 
stream_send_fd(int fd,int sendfd)57 int     stream_send_fd(int fd, int sendfd)
58 {
59 #ifdef STREAM_CONNECTIONS
60     const char *myname = "stream_send_fd";
61 
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
68 }
69 
70 #ifdef TEST
71 
72  /*
73   * Proof-of-concept program. Open a file and send the descriptor, presumably
74   * to the stream_recv_fd test program.
75   */
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>
82 
main(int argc,char ** argv)83 int     main(int argc, char **argv)
84 {
85     char   *transport;
86     char   *endpoint;
87     char   *path;
88     int     server_sock;
89     int     client_fd;
90 
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]);
95 
96     if (strcmp(transport, "stream") == 0) {
97 	server_sock = stream_connect(endpoint, BLOCKING, 0);
98     } else {
99 	msg_fatal("invalid transport name: %s", transport);
100     }
101     if (server_sock < 0)
102 	msg_fatal("connect %s:%s: %m", transport, endpoint);
103 
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);
113     }
114     exit(0);
115 }
116 
117 #endif
118