xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/stream_connect.c (revision 41fbaed053f8fbfdf9d2a4ee0a7386a3c83f8505)
1 /*	$NetBSD: stream_connect.c,v 1.1.1.1 2009/06/23 10:09:00 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	stream_connect 3
6 /* SUMMARY
7 /*	connect to stream listener
8 /* SYNOPSIS
9 /*	#include <connect.h>
10 /*
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.
18 /*
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 /*--*/
42 
43 /* System library. */
44 
45 #include <sys_defs.h>
46 
47 #ifdef STREAM_CONNECTIONS
48 
49 #include <sys/stat.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <errno.h>
53 #include <stropts.h>
54 
55 #endif
56 
57 /* Utility library. */
58 
59 #include <msg.h>
60 #include <connect.h>
61 
62 /* stream_connect - connect to stream listener */
63 
stream_connect(const char * path,int block_mode,int unused_timeout)64 int     stream_connect(const char *path, int block_mode, int unused_timeout)
65 {
66 #ifdef STREAM_CONNECTIONS
67     const char *myname = "stream_connect";
68     int     pair[2];
69     int     fifo;
70 
71     /*
72      * The requested file system object must exist, otherwise we can't reach
73      * the server.
74      */
75     if ((fifo = open(path, O_WRONLY | O_NONBLOCK, 0)) < 0)
76 	return (-1);
77 
78     /*
79      * This is for {unix,inet}_connect() compatibility.
80      */
81     if (block_mode == BLOCKING)
82 	non_blocking(fifo, BLOCKING);
83 
84     /*
85      * Create a pipe, and send one pipe end to the server.
86      */
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]);
92 
93     /*
94      * This is for {unix,inet}_connect() compatibility.
95      */
96     if (block_mode == NON_BLOCKING)
97 	non_blocking(pair[0], NON_BLOCKING);
98 
99     /*
100      * Cleanup.
101      */
102     close(fifo);
103 
104     /*
105      * Keep the other end of the pipe.
106      */
107     return (pair[0]);
108 #else
109     msg_fatal("stream connections are not implemented");
110 #endif
111 }
112