~ubuntu-branches/ubuntu/raring/postfix/raring

« back to all changes in this revision

Viewing changes to src/util/stream_send_fd.c

Tags: upstream-2.2.6
ImportĀ upstreamĀ versionĀ 2.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
19
/* .IP sendfd
 
20
/*      Another file descriptor.
 
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
    char   *myname = "stream_send_fd";
 
58
 
 
59
#ifdef STREAM_CONNECTIONS
 
60
    if (ioctl(fd, I_SENDFD, sendfd) < 0)
 
61
        msg_fatal("%s: send file descriptor: %m", myname);
 
62
#else
 
63
    msg_fatal("stream connections are not implemented");
 
64
#endif
 
65
}
 
66
 
 
67
#ifdef TEST
 
68
 
 
69
 /*
 
70
  * Proof-of-concept program. Open a file and send the descriptor, presumably
 
71
  * to the stream_recv_fd test program.
 
72
  */
 
73
#include <unistd.h>
 
74
#include <fcntl.h>
 
75
#include <split_at.h>
 
76
#include <connect.h>
 
77
 
 
78
int     main(int argc, char **argv)
 
79
{
 
80
    char   *transport;
 
81
    char   *endpoint;
 
82
    char   *path;
 
83
    int     server_sock;
 
84
    int     client_fd;
 
85
 
 
86
    if (argc < 3
 
87
        || (endpoint = split_at(transport = argv[1], ':')) == 0
 
88
        || *endpoint == 0 || *transport == 0)
 
89
        msg_fatal("usage: %s transport:endpoint file...", argv[0]);
 
90
 
 
91
    if (strcmp(transport, "stream") == 0) {
 
92
        server_sock = stream_connect(endpoint, BLOCKING, 0);
 
93
    } else {
 
94
        msg_fatal("invalid transport name: %s", transport);
 
95
    }
 
96
    if (server_sock < 0)
 
97
        msg_fatal("connect %s:%s: %m", transport, endpoint);
 
98
 
 
99
    argv += 2;
 
100
    while ((path = *argv++) != 0) {
 
101
        if ((client_fd = open(path, O_RDONLY, 0)) < 0)
 
102
            msg_fatal("open %s: %m", path);
 
103
        msg_info("path=%s client_fd=%d", path, client_fd);
 
104
        if (stream_send_fd(server_sock, client_fd) < 0)
 
105
            msg_fatal("send file descriptor: %m");
 
106
        if (close(client_fd) != 0)
 
107
            msg_fatal("close(%d): %m", client_fd);
 
108
    }
 
109
    exit(0);
 
110
}
 
111
 
 
112
#endif