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