xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/stream_listen.c (revision e6ca80d43962864f49719de05ed892793630d07f)
1 /*	$NetBSD: stream_listen.c,v 1.1.1.2 2013/09/25 19:06:37 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	stream_listen 3
6 /* SUMMARY
7 /*	start stream listener
8 /* SYNOPSIS
9 /*	#include <listen.h>
10 /*
11 /*	int	stream_listen(path, backlog, block_mode)
12 /*	const char *path;
13 /*	int	backlog;
14 /*	int	block_mode;
15 /*
16 /*	int	stream_accept(fd)
17 /*	int	fd;
18 /* DESCRIPTION
19 /*	This module implements a substitute local IPC for systems that do
20 /*	not have properly-working UNIX-domain sockets.
21 /*
22 /*	stream_listen() creates a listener endpoint with the specified
23 /*	permissions, and returns a file descriptor to be used for accepting
24 /*	connections.
25 /*
26 /*	stream_accept() accepts a connection.
27 /*
28 /*	Arguments:
29 /* .IP path
30 /*	Null-terminated string with connection destination.
31 /* .IP backlog
32 /*	This argument exists for compatibility and is ignored.
33 /* .IP block_mode
34 /*	Either NON_BLOCKING or BLOCKING. This does not affect the
35 /*	mode of accepted connections.
36 /* .IP fd
37 /*	File descriptor returned by stream_listen().
38 /* DIAGNOSTICS
39 /*	Fatal errors: stream_listen() aborts upon any system call failure.
40 /*	stream_accept() leaves all error handling up to the caller.
41 /* LICENSE
42 /* .ad
43 /* .fi
44 /*	The Secure Mailer license must be distributed with this software.
45 /* AUTHOR(S)
46 /*	Wietse Venema
47 /*	IBM T.J. Watson Research
48 /*	P.O. Box 704
49 /*	Yorktown Heights, NY 10598, USA
50 /*--*/
51 
52 /* System interfaces. */
53 
54 #include <sys_defs.h>
55 
56 #ifdef STREAM_CONNECTIONS
57 
58 #include <sys/stat.h>
59 #include <unistd.h>
60 #include <errno.h>
61 #include <stropts.h>
62 #include <fcntl.h>
63 
64 #endif
65 
66 /* Utility library. */
67 
68 #include "msg.h"
69 #include "listen.h"
70 
71 /* stream_listen - create stream listener */
72 
stream_listen(const char * path,int unused_backlog,int block_mode)73 int     stream_listen(const char *path, int unused_backlog, int block_mode)
74 {
75 #ifdef STREAM_CONNECTIONS
76 
77     /*
78      * We can't specify a listen backlog, however, sending file descriptors
79      * across a FIFO gives us a backlog buffer of 460 on Solaris 2.4/SPARC.
80      */
81     return (fifo_listen(path, 0622, block_mode));
82 #else
83     msg_fatal("stream connections are not implemented");
84 #endif
85 }
86 
87 /* stream_accept - accept stream connection */
88 
stream_accept(int fd)89 int     stream_accept(int fd)
90 {
91 #ifdef STREAM_CONNECTIONS
92     struct strrecvfd fdinfo;
93 
94     /*
95      * This will return EAGAIN on a non-blocking stream when someone else
96      * snatched the connection from us.
97      */
98     if (ioctl(fd, I_RECVFD, &fdinfo) < 0)
99 	return (-1);
100     return (fdinfo.fd);
101 #else
102             msg_fatal("stream connections are not implemented");
103 #endif
104 }
105