1 /*-------------------------------------------------------------------------
6 * Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 * This is a replacement version of pipe for Win32 which allows
9 * returned handles to be used in select(). Note that read/write calls
10 * must be replaced with recv/send.
15 *-------------------------------------------------------------------------
22 pgpipe(int handles
[2])
25 struct sockaddr_in serv_addr
;
26 int len
= sizeof(serv_addr
);
28 handles
[0] = handles
[1] = INVALID_SOCKET
;
30 if ((s
= socket(AF_INET
, SOCK_STREAM
, 0)) == INVALID_SOCKET
)
32 ereport(LOG
, (errmsg_internal("pgpipe failed to create socket: %ui", WSAGetLastError())));
36 memset((void *) &serv_addr
, 0, sizeof(serv_addr
));
37 serv_addr
.sin_family
= AF_INET
;
38 serv_addr
.sin_port
= htons(0);
39 serv_addr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
40 if (bind(s
, (SOCKADDR
*) & serv_addr
, len
) == SOCKET_ERROR
)
42 ereport(LOG
, (errmsg_internal("pgpipe failed to bind: %ui", WSAGetLastError())));
46 if (listen(s
, 1) == SOCKET_ERROR
)
48 ereport(LOG
, (errmsg_internal("pgpipe failed to listen: %ui", WSAGetLastError())));
52 if (getsockname(s
, (SOCKADDR
*) & serv_addr
, &len
) == SOCKET_ERROR
)
54 ereport(LOG
, (errmsg_internal("pgpipe failed to getsockname: %ui", WSAGetLastError())));
58 if ((handles
[1] = socket(PF_INET
, SOCK_STREAM
, 0)) == INVALID_SOCKET
)
60 ereport(LOG
, (errmsg_internal("pgpipe failed to create socket 2: %ui", WSAGetLastError())));
65 if (connect(handles
[1], (SOCKADDR
*) & serv_addr
, len
) == SOCKET_ERROR
)
67 ereport(LOG
, (errmsg_internal("pgpipe failed to connect socket: %ui", WSAGetLastError())));
71 if ((handles
[0] = accept(s
, (SOCKADDR
*) & serv_addr
, &len
)) == INVALID_SOCKET
)
73 ereport(LOG
, (errmsg_internal("pgpipe failed to accept socket: %ui", WSAGetLastError())));
74 closesocket(handles
[1]);
75 handles
[1] = INVALID_SOCKET
;
85 piperead(int s
, char *buf
, int len
)
87 int ret
= recv(s
, buf
, len
, 0);
89 if (ret
< 0 && WSAGetLastError() == WSAECONNRESET
)
90 /* EOF on the pipe! (win32 socket based implementation) */