summarylogtreecommitdiffstats
path: root/fd-passing-libmilter.patch
blob: 01d35f5c39af7ecddedc322b1ec1f78b92e81c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Description: systemd-like socket activation support for libmilter
Author: Mikhail Gusarov <dottedmag@debian.org
--- a/libmilter/docs/smfi_setconn.html
+++ b/libmilter/docs/smfi_setconn.html
@@ -44,6 +44,7 @@ Set the socket through which this filter
 	<LI><CODE>{unix|local}:/path/to/file</CODE> -- A named pipe.
 	<LI><CODE>inet:port@{hostname|ip-address}</CODE> -- An IPV4 socket.
 	<LI><CODE>inet6:port@{hostname|ip-address}</CODE> -- An IPV6 socket.
+	<LI><CODE>fd:number</CODE> -- Pre-opened file descriptor.
 	</UL>
 	</TD></TR>
     </TABLE>
--- a/libmilter/listener.c
+++ b/libmilter/listener.c
@@ -197,6 +197,11 @@
 			L_socksize = sizeof addr.sin6;
 		}
 #endif /* NETINET6 */
+		else if (strcasecmp(p, "fd") == 0)
+		{
+			addr.sa.sa_family = AF_UNSPEC;
+			L_socksize = sizeof (_SOCK_ADDR);
+		}
 		else
 		{
 			smi_log(SMI_LOG_ERR, "%s: unknown socket type %s",
@@ -443,7 +448,21 @@
 	}
 #endif /* NETINET || NETINET6 */

-	sock = socket(addr.sa.sa_family, SOCK_STREAM, 0);
+	if (addr.sa.sa_family == AF_UNSPEC)
+	{
+		char *end;
+		sock = strtol(colon, &end, 10);
+		if (*end != '\0' || sock < 0)
+		{
+			smi_log(SMI_LOG_ERR, "%s: expected positive integer as fd, got %s", name, colon);
+			return INVALID_SOCKET;
+		}
+	}
+	else
+	{
+		sock = socket(addr.sa.sa_family, SOCK_STREAM, 0);
+	}
+
 	if (!ValidSocket(sock))
 	{
 		smi_log(SMI_LOG_ERR,
@@ -466,6 +485,7 @@
 #if NETUNIX
 	    addr.sa.sa_family != AF_UNIX &&
 #endif
+	    addr.sa.sa_family != AF_UNSPEC &&
 	    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &sockopt,
 		       sizeof(sockopt)) == -1)
 	{
@@ -511,7 +531,8 @@
 	}
 #endif /* NETUNIX */

-	if (bind(sock, &addr.sa, L_socksize) < 0)
+	if (addr.sa.sa_family != AF_UNSPEC &&
+	    bind(sock, &addr.sa, L_socksize) < 0)
 	{
 		smi_log(SMI_LOG_ERR,
 			"%s: Unable to bind to port %s: %s",
@@ -818,7 +839,7 @@
 #ifdef BSD4_4_SOCKADDR
 		     cliaddr.sa.sa_len == 0 ||
 #endif
-		     cliaddr.sa.sa_family != L_family))
+		     (L_family != AF_UNSPEC && cliaddr.sa.sa_family != L_family)))
 		{
 			(void) closesocket(connfd);
 			connfd = INVALID_SOCKET;