Fix pg_dump bug in the database-level collation patch. "datcollate" and
[PostgreSQL.git] / src / port / pipe.c
blob6ad9ad928d06fdc8876ef3c03a4fce958a4e239b
1 /*-------------------------------------------------------------------------
3 * pipe.c
4 * pipe()
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.
12 * IDENTIFICATION
13 * $PostgreSQL$
15 *-------------------------------------------------------------------------
18 #include "postgres.h"
20 #ifdef WIN32
21 int
22 pgpipe(int handles[2])
24 SOCKET s;
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())));
33 return -1;
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())));
43 closesocket(s);
44 return -1;
46 if (listen(s, 1) == SOCKET_ERROR)
48 ereport(LOG, (errmsg_internal("pgpipe failed to listen: %ui", WSAGetLastError())));
49 closesocket(s);
50 return -1;
52 if (getsockname(s, (SOCKADDR *) & serv_addr, &len) == SOCKET_ERROR)
54 ereport(LOG, (errmsg_internal("pgpipe failed to getsockname: %ui", WSAGetLastError())));
55 closesocket(s);
56 return -1;
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())));
61 closesocket(s);
62 return -1;
65 if (connect(handles[1], (SOCKADDR *) & serv_addr, len) == SOCKET_ERROR)
67 ereport(LOG, (errmsg_internal("pgpipe failed to connect socket: %ui", WSAGetLastError())));
68 closesocket(s);
69 return -1;
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;
76 closesocket(s);
77 return -1;
79 closesocket(s);
80 return 0;
84 int
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) */
91 ret = 0;
92 return ret;
95 #endif