From 87091cb1f1ed914e2ddca424fa28f94fdf8461d2 Mon Sep 17 00:00:00 2001
From: Magnus Hagander <magnus@hagander.net>
Date: Sun, 10 Jan 2010 14:16:08 +0000
Subject: [PATCH] Create typedef pgsocket for storing socket descriptors.

This silences some warnings on Win64. Not using the proper SOCKET datatype
was actually wrong on Win32 as well, but didn't cause any warnings there.

Also create define PGINVALID_SOCKET to indicate an invalid/non-existing
socket, instead of using a hardcoded -1 value.
---
 src/backend/libpq/auth.c            |  4 ++--
 src/backend/libpq/ip.c              |  6 +++---
 src/backend/libpq/pqcomm.c          | 20 ++++++++++----------
 src/backend/postmaster/pgstat.c     | 22 +++++++++++-----------
 src/backend/postmaster/postmaster.c | 28 ++++++++++++++--------------
 src/include/libpq/libpq-be.h        |  4 ++--
 src/include/libpq/libpq.h           |  8 ++++----
 src/include/port.h                  | 15 ++++++++++++---
 src/port/noblock.c                  |  6 +++---
 9 files changed, 61 insertions(+), 52 deletions(-)

diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c
index 4344b0bfaa0..e7d0dbb2985 100644
--- a/src/backend/libpq/auth.c
+++ b/src/backend/libpq/auth.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/libpq/auth.c,v 1.190 2010/01/02 16:57:45 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/libpq/auth.c,v 1.191 2010/01/10 14:16:07 mha Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1497,7 +1497,7 @@ ident_inet(const SockAddr remote_addr,
 		   const SockAddr local_addr,
 		   char *ident_user)
 {
-	int			sock_fd,		/* File descriptor for socket on which we talk
+	pgsocket	sock_fd,		/* File descriptor for socket on which we talk
 								 * to Ident */
 				rc;				/* Return code from a locally called function */
 	bool		ident_return;
diff --git a/src/backend/libpq/ip.c b/src/backend/libpq/ip.c
index b0085cf6bf9..778b9f9ea4a 100644
--- a/src/backend/libpq/ip.c
+++ b/src/backend/libpq/ip.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/libpq/ip.c,v 1.49 2010/01/02 16:57:45 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/libpq/ip.c,v 1.50 2010/01/10 14:16:07 mha Exp $
  *
  * This file and the IPV6 implementation were initially provided by
  * Nigel Kukard <nkukard@lbsd.net>, Linux Based Systems Design
@@ -656,9 +656,9 @@ pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
 	struct sockaddr *addr, *mask;
 	char *ptr, *buffer = NULL;
 	size_t n_buffer = 1024;
-	int sock, fd;
+	pgsocket sock, fd;
 #ifdef HAVE_IPV6
-	int sock6;
+	pgsocket sock6;
 #endif
 	int i, total;
 
diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c
index 8800fa6f213..b99c9da2ab5 100644
--- a/src/backend/libpq/pqcomm.c
+++ b/src/backend/libpq/pqcomm.c
@@ -30,7 +30,7 @@
  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- *	$PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.200 2010/01/02 16:57:45 momjian Exp $
+ *	$PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.201 2010/01/10 14:16:07 mha Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -199,9 +199,9 @@ pq_close(int code, Datum arg)
 		 * transport layer reports connection closure, and you can be sure the
 		 * backend has exited.
 		 *
-		 * We do set sock to -1 to prevent any further I/O, though.
+		 * We do set sock to PGINVALID_SOCKET to prevent any further I/O, though.
 		 */
-		MyProcPort->sock = -1;
+		MyProcPort->sock = PGINVALID_SOCKET;
 	}
 }
 
@@ -232,7 +232,7 @@ StreamDoUnlink(int code, Datum arg)
  * StreamServerPort -- open a "listening" port to accept connections.
  *
  * Successfully opened sockets are added to the ListenSocket[] array,
- * at the first position that isn't -1.
+ * at the first position that isn't PGINVALID_SOCKET.
  *
  * RETURNS: STATUS_OK or STATUS_ERROR
  */
@@ -240,10 +240,10 @@ StreamDoUnlink(int code, Datum arg)
 int
 StreamServerPort(int family, char *hostName, unsigned short portNumber,
 				 char *unixSocketName,
-				 int ListenSocket[], int MaxListen)
+				 pgsocket ListenSocket[], int MaxListen)
 {
-	int			fd,
-				err;
+	pgsocket	fd;
+	int			err;
 	int			maxconn;
 	int			ret;
 	char		portNumberStr[32];
@@ -311,7 +311,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
 		/* See if there is still room to add 1 more socket. */
 		for (; listen_index < MaxListen; listen_index++)
 		{
-			if (ListenSocket[listen_index] == -1)
+			if (ListenSocket[listen_index] == PGINVALID_SOCKET)
 				break;
 		}
 		if (listen_index >= MaxListen)
@@ -570,7 +570,7 @@ Setup_AF_UNIX(void)
  * RETURNS: STATUS_OK or STATUS_ERROR
  */
 int
-StreamConnection(int server_fd, Port *port)
+StreamConnection(pgsocket server_fd, Port *port)
 {
 	/* accept connection and fill in the client (remote) address */
 	port->raddr.salen = sizeof(port->raddr.addr);
@@ -676,7 +676,7 @@ StreamConnection(int server_fd, Port *port)
  * we do NOT want to send anything to the far end.
  */
 void
-StreamClose(int sock)
+StreamClose(pgsocket sock)
 {
 	closesocket(sock);
 }
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index d5adaf6e109..e45cdaa8e8d 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -13,7 +13,7 @@
  *
  *	Copyright (c) 2001-2010, PostgreSQL Global Development Group
  *
- *	$PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.196 2010/01/02 16:57:50 momjian Exp $
+ *	$PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.197 2010/01/10 14:16:07 mha Exp $
  * ----------
  */
 #include "postgres.h"
@@ -130,7 +130,7 @@ PgStat_MsgBgWriter BgWriterStats;
  * Local data
  * ----------
  */
-NON_EXEC_STATIC int pgStatSock = -1;
+NON_EXEC_STATIC pgsocket pgStatSock = PGINVALID_SOCKET;
 
 static struct sockaddr_storage pgStatAddr;
 
@@ -369,7 +369,7 @@ pgstat_init(void)
 					(errcode_for_socket_access(),
 			  errmsg("could not bind socket for statistics collector: %m")));
 			closesocket(pgStatSock);
-			pgStatSock = -1;
+			pgStatSock = PGINVALID_SOCKET;
 			continue;
 		}
 
@@ -380,7 +380,7 @@ pgstat_init(void)
 					(errcode_for_socket_access(),
 					 errmsg("could not get address of socket for statistics collector: %m")));
 			closesocket(pgStatSock);
-			pgStatSock = -1;
+			pgStatSock = PGINVALID_SOCKET;
 			continue;
 		}
 
@@ -396,7 +396,7 @@ pgstat_init(void)
 					(errcode_for_socket_access(),
 			errmsg("could not connect socket for statistics collector: %m")));
 			closesocket(pgStatSock);
-			pgStatSock = -1;
+			pgStatSock = PGINVALID_SOCKET;
 			continue;
 		}
 
@@ -417,7 +417,7 @@ retry1:
 					(errcode_for_socket_access(),
 					 errmsg("could not send test message on socket for statistics collector: %m")));
 			closesocket(pgStatSock);
-			pgStatSock = -1;
+			pgStatSock = PGINVALID_SOCKET;
 			continue;
 		}
 
@@ -443,7 +443,7 @@ retry1:
 					(errcode_for_socket_access(),
 					 errmsg("select() failed in statistics collector: %m")));
 			closesocket(pgStatSock);
-			pgStatSock = -1;
+			pgStatSock = PGINVALID_SOCKET;
 			continue;
 		}
 		if (sel_res == 0 || !FD_ISSET(pgStatSock, &rset))
@@ -458,7 +458,7 @@ retry1:
 					(errcode(ERRCODE_CONNECTION_FAILURE),
 					 errmsg("test message did not get through on socket for statistics collector")));
 			closesocket(pgStatSock);
-			pgStatSock = -1;
+			pgStatSock = PGINVALID_SOCKET;
 			continue;
 		}
 
@@ -473,7 +473,7 @@ retry2:
 					(errcode_for_socket_access(),
 					 errmsg("could not receive test message on socket for statistics collector: %m")));
 			closesocket(pgStatSock);
-			pgStatSock = -1;
+			pgStatSock = PGINVALID_SOCKET;
 			continue;
 		}
 
@@ -483,7 +483,7 @@ retry2:
 					(errcode(ERRCODE_INTERNAL_ERROR),
 					 errmsg("incorrect test message transmission on socket for statistics collector")));
 			closesocket(pgStatSock);
-			pgStatSock = -1;
+			pgStatSock = PGINVALID_SOCKET;
 			continue;
 		}
 
@@ -521,7 +521,7 @@ startup_failed:
 
 	if (pgStatSock >= 0)
 		closesocket(pgStatSock);
-	pgStatSock = -1;
+	pgStatSock = PGINVALID_SOCKET;
 
 	/*
 	 * Adjust GUC variables to suppress useless activity, and for debugging
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index ca79a2c3751..94672be0c09 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -37,7 +37,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.599 2010/01/02 16:57:50 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.600 2010/01/10 14:16:08 mha Exp $
  *
  * NOTES
  *
@@ -172,7 +172,7 @@ int			ReservedBackends;
 
 /* The socket(s) we're listening to. */
 #define MAXLISTEN	64
-static int	ListenSocket[MAXLISTEN];
+static pgsocket ListenSocket[MAXLISTEN];
 
 /*
  * Set by the -o option
@@ -382,7 +382,7 @@ static pid_t internal_forkexec(int argc, char *argv[], Port *port);
 #ifdef WIN32
 typedef struct
 {
-	SOCKET		origsocket;		/* Original socket value, or -1 if not a
+	SOCKET		origsocket;		/* Original socket value, or PGINVALID_SOCKET if not a
 								 * socket */
 	WSAPROTOCOL_INFO wsainfo;
 } InheritableSocket;
@@ -400,7 +400,7 @@ typedef struct
 	Port		port;
 	InheritableSocket portsocket;
 	char		DataDir[MAXPGPATH];
-	int			ListenSocket[MAXLISTEN];
+	pgsocket	ListenSocket[MAXLISTEN];
 	long		MyCancelKey;
 	int			MyPMChildSlot;
 #ifndef WIN32
@@ -807,7 +807,7 @@ PostmasterMain(int argc, char *argv[])
 	 * Establish input sockets.
 	 */
 	for (i = 0; i < MAXLISTEN; i++)
-		ListenSocket[i] = -1;
+		ListenSocket[i] = PGINVALID_SOCKET;
 
 	if (ListenAddresses)
 	{
@@ -860,7 +860,7 @@ PostmasterMain(int argc, char *argv[])
 
 #ifdef USE_BONJOUR
 	/* Register for Bonjour only if we opened TCP socket(s) */
-	if (enable_bonjour && ListenSocket[0] != -1)
+	if (enable_bonjour && ListenSocket[0] != PGINVALID_SOCKET)
 	{
 		DNSServiceErrorType err;
 
@@ -908,7 +908,7 @@ PostmasterMain(int argc, char *argv[])
 	/*
 	 * check that we have some socket to listen on
 	 */
-	if (ListenSocket[0] == -1)
+	if (ListenSocket[0] == PGINVALID_SOCKET)
 		ereport(FATAL,
 				(errmsg("no socket created for listening")));
 
@@ -1392,7 +1392,7 @@ ServerLoop(void)
 
 			for (i = 0; i < MAXLISTEN; i++)
 			{
-				if (ListenSocket[i] == -1)
+				if (ListenSocket[i] == PGINVALID_SOCKET)
 					break;
 				if (FD_ISSET(ListenSocket[i], &rmask))
 				{
@@ -1493,7 +1493,7 @@ initMasks(fd_set *rmask)
 	{
 		int			fd = ListenSocket[i];
 
-		if (fd == -1)
+		if (fd == PGINVALID_SOCKET)
 			break;
 		FD_SET		(fd, rmask);
 
@@ -2002,10 +2002,10 @@ ClosePostmasterPorts(bool am_syslogger)
 	/* Close the listen sockets */
 	for (i = 0; i < MAXLISTEN; i++)
 	{
-		if (ListenSocket[i] != -1)
+		if (ListenSocket[i] != PGINVALID_SOCKET)
 		{
 			StreamClose(ListenSocket[i]);
-			ListenSocket[i] = -1;
+			ListenSocket[i] = PGINVALID_SOCKET;
 		}
 	}
 
@@ -4408,7 +4408,7 @@ extern slock_t *ProcStructLock;
 extern PROC_HDR *ProcGlobal;
 extern PGPROC *AuxiliaryProcs;
 extern PMSignalData *PMSignalState;
-extern int	pgStatSock;
+extern pgsocket pgStatSock;
 
 #ifndef WIN32
 #define write_inheritable_socket(dest, src, childpid) ((*(dest) = (src)), true)
@@ -4522,7 +4522,7 @@ static bool
 write_inheritable_socket(InheritableSocket *dest, SOCKET src, pid_t childpid)
 {
 	dest->origsocket = src;
-	if (src != 0 && src != -1)
+	if (src != 0 && src != PGINVALID_SOCKET)
 	{
 		/* Actual socket */
 		if (WSADuplicateSocket(src, childpid, &dest->wsainfo) != 0)
@@ -4544,7 +4544,7 @@ read_inheritable_socket(SOCKET *dest, InheritableSocket *src)
 {
 	SOCKET		s;
 
-	if (src->origsocket == -1 || src->origsocket == 0)
+	if (src->origsocket == PGINVALID_SOCKET || src->origsocket == 0)
 	{
 		/* Not a real socket! */
 		*dest = src->origsocket;
diff --git a/src/include/libpq/libpq-be.h b/src/include/libpq/libpq-be.h
index c714add81a3..1d358715288 100644
--- a/src/include/libpq/libpq-be.h
+++ b/src/include/libpq/libpq-be.h
@@ -11,7 +11,7 @@
  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/libpq/libpq-be.h,v 1.72 2010/01/02 16:58:04 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/libpq/libpq-be.h,v 1.73 2010/01/10 14:16:08 mha Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -103,7 +103,7 @@ typedef struct
 
 typedef struct Port
 {
-	int			sock;			/* File descriptor */
+	pgsocket	sock;			/* File descriptor */
 	ProtocolVersion proto;		/* FE/BE protocol version */
 	SockAddr	laddr;			/* local addr (postmaster) */
 	SockAddr	raddr;			/* remote addr (client) */
diff --git a/src/include/libpq/libpq.h b/src/include/libpq/libpq.h
index 314ce69bce4..c9c7e0d7640 100644
--- a/src/include/libpq/libpq.h
+++ b/src/include/libpq/libpq.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/libpq/libpq.h,v 1.72 2010/01/02 16:58:04 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/libpq/libpq.h,v 1.73 2010/01/10 14:16:08 mha Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -45,10 +45,10 @@ typedef struct
  * prototypes for functions in pqcomm.c
  */
 extern int StreamServerPort(int family, char *hostName,
-		 unsigned short portNumber, char *unixSocketName, int ListenSocket[],
+		 unsigned short portNumber, char *unixSocketName, pgsocket ListenSocket[],
 				 int MaxListen);
-extern int	StreamConnection(int server_fd, Port *port);
-extern void StreamClose(int sock);
+extern int	StreamConnection(pgsocket server_fd, Port *port);
+extern void StreamClose(pgsocket sock);
 extern void TouchSocketFile(void);
 extern void pq_init(void);
 extern void pq_comm_reset(void);
diff --git a/src/include/port.h b/src/include/port.h
index b5c22cb0c4c..2a4f9b4818a 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/port.h,v 1.128 2010/01/02 16:58:00 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/port.h,v 1.129 2010/01/10 14:16:08 mha Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -17,9 +17,18 @@
 #include <netdb.h>
 #include <pwd.h>
 
+/* socket has a different definition on WIN32 */
+#ifndef WIN32
+typedef int pgsocket;
+#define PGINVALID_SOCKET -1
+#else
+typedef SOCKET pgsocket;
+#define PGINVALID_SOCKET INVALID_SOCKET
+#endif
+
 /* non-blocking */
-extern bool pg_set_noblock(int sock);
-extern bool pg_set_block(int sock);
+extern bool pg_set_noblock(pgsocket sock);
+extern bool pg_set_block(pgsocket sock);
 
 /* Portable path handling for Unix/Win32 (in path.c) */
 
diff --git a/src/port/noblock.c b/src/port/noblock.c
index 5349e9e097f..ace3bfdb915 100644
--- a/src/port/noblock.c
+++ b/src/port/noblock.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/noblock.c,v 1.14 2010/01/02 16:58:13 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/port/noblock.c,v 1.15 2010/01/10 14:16:08 mha Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -18,7 +18,7 @@
 
 
 bool
-pg_set_noblock(int sock)
+pg_set_noblock(pgsocket sock)
 {
 #if !defined(WIN32)
 	return (fcntl(sock, F_SETFL, O_NONBLOCK) != -1);
@@ -32,7 +32,7 @@ pg_set_noblock(int sock)
 
 
 bool
-pg_set_block(int sock)
+pg_set_block(pgsocket sock)
 {
 #if !defined(WIN32)
 	int			flags;
-- 
GitLab