Skip to content

Commit ee7fbb1

Browse files
committed
Add WIN32 pipe implementation that uses sockets.
Claudio Natoli
1 parent 0d2148a commit ee7fbb1

File tree

4 files changed

+77
-7
lines changed

4 files changed

+77
-7
lines changed

configure

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12227,7 +12227,8 @@ esac
1222712227
case $host_os in mingw*)
1222812228
LIBOBJS="$LIBOBJS dirmod.$ac_objext"
1222912229
LIBOBJS="$LIBOBJS copydir.$ac_objext"
12230-
LIBOBJS="$LIBOBJS gettimeofday.$ac_objext" ;;
12230+
LIBOBJS="$LIBOBJS gettimeofday.$ac_objext"
12231+
LIBOBJS="$LIBOBJS pipe.$ac_objext" ;;
1223112232
esac
1223212233

1223312234
if test "$with_readline" = yes; then

configure.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dnl Process this file with autoconf to produce a configure script.
2-
dnl $PostgreSQL: pgsql/configure.in,v 1.309 2003/12/23 18:40:52 tgl Exp $
2+
dnl $PostgreSQL: pgsql/configure.in,v 1.310 2004/01/09 04:58:09 momjian Exp $
33
dnl
44
dnl Developers, please strive to achieve this order:
55
dnl
@@ -924,7 +924,8 @@ esac
924924
case $host_os in mingw*)
925925
AC_LIBOBJ(dirmod)
926926
AC_LIBOBJ(copydir)
927-
AC_LIBOBJ(gettimeofday) ;;
927+
AC_LIBOBJ(gettimeofday)
928+
AC_LIBOBJ(pipe) ;;
928929
esac
929930

930931
if test "$with_readline" = yes; then

src/backend/postmaster/pgstat.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* Copyright (c) 2001-2003, PostgreSQL Global Development Group
1515
*
16-
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.51 2004/01/06 23:15:22 momjian Exp $
16+
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.52 2004/01/09 04:58:09 momjian Exp $
1717
* ----------
1818
*/
1919
#include "postgres.h"
@@ -135,6 +135,19 @@ static void pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len);
135135
static void pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len);
136136
static void pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len);
137137

138+
/*
139+
* WIN32 doesn't allow descriptors returned by pipe() to be used in select(),
140+
* so for that platform we use socket() instead of pipe().
141+
*/
142+
#ifndef WIN32
143+
#define pgpipe(a) pipe(a)
144+
#define piperead(a,b,c) read(a,b,c)
145+
#define pipewrite(a,b,c) write(a,b,c)
146+
#else
147+
/* pgpipe() is in /src/port */
148+
#define piperead(a,b,c) recv(a,b,c,0)
149+
#define pipewrite(a,b,c) send(a,b,c,0)
150+
#endif
138151

139152
/* ------------------------------------------------------------
140153
* Public functions called from postmaster follow
@@ -1380,7 +1393,7 @@ pgstat_main(PGSTAT_FORK_ARGS)
13801393
* two buffer processes competing to read from the UDP socket --- not
13811394
* good.
13821395
*/
1383-
if (pipe(pgStatPipe) < 0)
1396+
if (pgpipe(pgStatPipe) < 0)
13841397
{
13851398
ereport(LOG,
13861399
(errcode_for_socket_access(),
@@ -1595,7 +1608,7 @@ pgstat_mainChild(PGSTAT_FORK_ARGS)
15951608

15961609
while (nread < targetlen)
15971610
{
1598-
len = read(readPipe,
1611+
len = piperead(readPipe,
15991612
((char *) &msg) + nread,
16001613
targetlen - nread);
16011614
if (len < 0)
@@ -1920,7 +1933,7 @@ pgstat_recvbuffer(void)
19201933
if (xfr > msg_have)
19211934
xfr = msg_have;
19221935
Assert(xfr > 0);
1923-
len = write(writePipe, msgbuffer + msg_send, xfr);
1936+
len = pipewrite(writePipe, msgbuffer + msg_send, xfr);
19241937
if (len < 0)
19251938
{
19261939
if (errno == EINTR || errno == EAGAIN)

src/port/pipe.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* pipe.c
4+
* pipe()
5+
*
6+
* Copyright (c) 1996-2003, PostgreSQL Global Development Group
7+
*
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.
11+
*
12+
* IDENTIFICATION
13+
* $PostgreSQL: pgsql/src/port/pipe.c,v 1.1 2004/01/09 04:58:09 momjian Exp $
14+
*
15+
*-------------------------------------------------------------------------
16+
*/
17+
18+
#include "postgres.h"
19+
20+
int
21+
pgpipe(int handles[2])
22+
{
23+
SOCKET s;
24+
struct sockaddr_in serv_addr;
25+
int len = sizeof(serv_addr);
26+
27+
handles[0] = handles[1] = INVALID_SOCKET;
28+
29+
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
30+
return -1;
31+
32+
memset((void *) &serv_addr, 0, sizeof(serv_addr));
33+
serv_addr.sin_family = AF_INET;
34+
serv_addr.sin_port = htons(0);
35+
serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
36+
if (bind(s, (SOCKADDR *) & serv_addr, len) == SOCKET_ERROR ||
37+
listen(s, 1) == SOCKET_ERROR ||
38+
getsockname(s, (SOCKADDR *) & serv_addr, &len) == SOCKET_ERROR ||
39+
(handles[1] = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
40+
{
41+
closesocket(s);
42+
return -1;
43+
}
44+
45+
if (connect(handles[1], (SOCKADDR *) & serv_addr, len) == SOCKET_ERROR ||
46+
(handles[0] = accept(s, (SOCKADDR *) & serv_addr, &len)) == INVALID_SOCKET)
47+
{
48+
closesocket(handles[1]);
49+
handles[1] = INVALID_SOCKET;
50+
closesocket(s);
51+
return -1;
52+
}
53+
closesocket(s);
54+
return 0;
55+
}

0 commit comments

Comments
 (0)