Postfix3.3.1
stream_send_fd.c
[詳解]
1 /*++
2 /* NAME
3 /* stream_send_fd 3
4 /* SUMMARY
5 /* send file descriptor
6 /* SYNOPSIS
7 /* #include <iostuff.h>
8 /*
9 /* int stream_send_fd(fd, sendfd)
10 /* int fd;
11 /* int sendfd;
12 /* DESCRIPTION
13 /* stream_send_fd() sends a file descriptor over the specified
14 /* stream.
15 /*
16 /* Arguments:
17 /* .IP fd
18 /* File descriptor that connects the sending and receiving processes.
19 /* .IP sendfd
20 /* The file descriptor to be sent.
21 /* DIAGNOSTICS
22 /* stream_send_fd() returns -1 upon failure.
23 /* LICENSE
24 /* .ad
25 /* .fi
26 /* The Secure Mailer license must be distributed with this software.
27 /* AUTHOR(S)
28 /* Wietse Venema
29 /* IBM T.J. Watson Research
30 /* P.O. Box 704
31 /* Yorktown Heights, NY 10598, USA
32 /*--*/
33 
34 /* System library. */
35 
36 #include <sys_defs.h> /* includes <sys/types.h> */
37 
38 #ifdef STREAM_CONNECTIONS
39 
40 #include <sys/stat.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <errno.h>
44 #include <stropts.h>
45 
46 #endif
47 
48 /* Utility library. */
49 
50 #include <msg.h>
51 #include <iostuff.h>
52 
53 /* stream_send_fd - send file descriptor */
54 
55 int stream_send_fd(int fd, int sendfd)
56 {
57 #ifdef STREAM_CONNECTIONS
58  const char *myname = "stream_send_fd";
59 
60  if (ioctl(fd, I_SENDFD, sendfd) < 0)
61  msg_fatal("%s: send file descriptor %d: %m", myname, sendfd);
62  return (0);
63 #else
64  msg_fatal("stream connections are not implemented");
65 #endif
66 }
67 
68 #ifdef TEST
69 
70  /*
71  * Proof-of-concept program. Open a file and send the descriptor, presumably
72  * to the stream_recv_fd test program.
73  */
74 #include <unistd.h>
75 #include <fcntl.h>
76 #include <string.h>
77 #include <stdlib.h>
78 #include <split_at.h>
79 #include <connect.h>
80 
81 int main(int argc, char **argv)
82 {
83  char *transport;
84  char *endpoint;
85  char *path;
86  int server_sock;
87  int client_fd;
88 
89  if (argc < 3
90  || (endpoint = split_at(transport = argv[1], ':')) == 0
91  || *endpoint == 0 || *transport == 0)
92  msg_fatal("usage: %s transport:endpoint file...", argv[0]);
93 
94  if (strcmp(transport, "stream") == 0) {
95  server_sock = stream_connect(endpoint, BLOCKING, 0);
96  } else {
97  msg_fatal("invalid transport name: %s", transport);
98  }
99  if (server_sock < 0)
100  msg_fatal("connect %s:%s: %m", transport, endpoint);
101 
102  argv += 2;
103  while ((path = *argv++) != 0) {
104  if ((client_fd = open(path, O_RDONLY, 0)) < 0)
105  msg_fatal("open %s: %m", path);
106  msg_info("path=%s client_fd=%d", path, client_fd);
107  if (stream_send_fd(server_sock, client_fd) < 0)
108  msg_fatal("send file descriptor: %m");
109  if (close(client_fd) != 0)
110  msg_fatal("close(%d): %m", client_fd);
111  }
112  exit(0);
113 }
114 
115 #endif
int main(int argc, char **argv)
Definition: anvil.c:1010
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
int stream_connect(const char *, int, int)
int stream_send_fd(int fd, int sendfd)
char * split_at(char *string, int delimiter)
Definition: split_at.c:53
#define BLOCKING
Definition: iostuff.h:48
void msg_info(const char *fmt,...)
Definition: msg.c:199