No empty .Rs/.Re
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / stream_connect.c
blob07158c73e502fce07252d754be8c124cacd29b37
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* stream_connect 3
6 /* SUMMARY
7 /* connect to stream listener
8 /* SYNOPSIS
9 /* #include <connect.h>
11 /* int stream_connect(path, block_mode, timeout)
12 /* const char *path;
13 /* int block_mode;
14 /* int timeout;
15 /* DESCRIPTION
16 /* stream_connect() connects to a stream listener for the specified
17 /* pathname, and returns the resulting file descriptor.
19 /* Arguments:
20 /* .IP path
21 /* Null-terminated string with listener endpoint name.
22 /* .IP block_mode
23 /* Either NON_BLOCKING for a non-blocking stream, or BLOCKING for
24 /* blocking mode. However, a stream connection succeeds or fails
25 /* immediately.
26 /* .IP timeout
27 /* This argument is ignored; it is present for compatibility with
28 /* other interfaces. Stream connections succeed or fail immediately.
29 /* DIAGNOSTICS
30 /* The result is -1 in case the connection could not be made.
31 /* Fatal errors: other system call failures.
32 /* LICENSE
33 /* .ad
34 /* .fi
35 /* The Secure Mailer license must be distributed with this software.
36 /* AUTHOR(S)
37 /* Wietse Venema
38 /* IBM T.J. Watson Research
39 /* P.O. Box 704
40 /* Yorktown Heights, NY 10598, USA
41 /*--*/
43 /* System library. */
45 #include <sys_defs.h>
47 #ifdef STREAM_CONNECTIONS
49 #include <sys/stat.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <errno.h>
53 #include <stropts.h>
55 #endif
57 /* Utility library. */
59 #include <msg.h>
60 #include <connect.h>
62 /* stream_connect - connect to stream listener */
64 int stream_connect(const char *path, int block_mode, int unused_timeout)
66 #ifdef STREAM_CONNECTIONS
67 const char *myname = "stream_connect";
68 int pair[2];
69 int fifo;
72 * The requested file system object must exist, otherwise we can't reach
73 * the server.
75 if ((fifo = open(path, O_WRONLY | O_NONBLOCK, 0)) < 0)
76 return (-1);
79 * This is for {unix,inet}_connect() compatibility.
81 if (block_mode == BLOCKING)
82 non_blocking(fifo, BLOCKING);
85 * Create a pipe, and send one pipe end to the server.
87 if (pipe(pair) < 0)
88 msg_fatal("%s: pipe: %m", myname);
89 if (ioctl(fifo, I_SENDFD, pair[1]) < 0)
90 msg_fatal("%s: send file descriptor: %m", myname);
91 close(pair[1]);
94 * This is for {unix,inet}_connect() compatibility.
96 if (block_mode == NON_BLOCKING)
97 non_blocking(pair[0], NON_BLOCKING);
100 * Cleanup.
102 close(fifo);
105 * Keep the other end of the pipe.
107 return (pair[0]);
108 #else
109 msg_fatal("stream connections are not implemented");
110 #endif