On Fri, Apr 11, 2014 at 08:28:55AM -0400, Bruce Momjian wrote:
> Once this is applied I will work on changing the libpq socket type to
> use portable pgsocket, but I am not planning to backpatch that unless we
> find a bug.

Attached is a follow up patch which stores socket values in libpq as
pgsocket, rather than int, and maps it to -1 only for the PQsocket()
external return value.  In the interest of time, I will apply this later
today, and only to head as it does not fix a bug.

This is the last open item I was working on.

-- 
  Bruce Momjian  <br...@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + Everyone has their own god. +
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
new file mode 100644
index 51d4de4..90b944a
*** a/src/interfaces/libpq/fe-connect.c
--- b/src/interfaces/libpq/fe-connect.c
*************** pqDropConnection(PGconn *conn)
*** 398,406 ****
  	/* Drop any SSL state */
  	pqsecure_close(conn);
  	/* Close the socket itself */
! 	if (conn->sock >= 0)
  		closesocket(conn->sock);
! 	conn->sock = -1;
  	/* Discard any unread/unsent data */
  	conn->inStart = conn->inCursor = conn->inEnd = 0;
  	conn->outCount = 0;
--- 398,406 ----
  	/* Drop any SSL state */
  	pqsecure_close(conn);
  	/* Close the socket itself */
! 	if (conn->sock != PGINVALID_SOCKET)
  		closesocket(conn->sock);
! 	conn->sock = PGINVALID_SOCKET;
  	/* Discard any unread/unsent data */
  	conn->inStart = conn->inCursor = conn->inEnd = 0;
  	conn->outCount = 0;
*************** keep_going:						/* We will come back to
*** 1631,1654 ****
  						   addr_cur->ai_addrlen);
  					conn->raddr.salen = addr_cur->ai_addrlen;
  
! 					/* Open a socket */
! 					{
! 						/*
! 						 * While we use 'pgsocket' as the socket type in the
! 						 * backend, we use 'int' for libpq socket values.
! 						 * This requires us to map PGINVALID_SOCKET to -1
! 						 * on Windows.
! 						 * See http://msdn.microsoft.com/en-us/library/windows/desktop/ms740516%28v=vs.85%29.aspx
! 						 */
! 						pgsocket sock = socket(addr_cur->ai_family, SOCK_STREAM, 0);
! #ifdef WIN32
! 						if (sock == PGINVALID_SOCKET)
! 							conn->sock = -1;
! 						else
! #endif
! 							conn->sock = sock;
! 					}
! 					if (conn->sock == -1)
  					{
  						/*
  						 * ignore socket() failure if we have more addresses
--- 1631,1638 ----
  						   addr_cur->ai_addrlen);
  					conn->raddr.salen = addr_cur->ai_addrlen;
  
! 					conn->sock = socket(addr_cur->ai_family, SOCK_STREAM, 0);
! 					if (conn->sock == PGINVALID_SOCKET)
  					{
  						/*
  						 * ignore socket() failure if we have more addresses
*************** makeEmptyPGconn(void)
*** 2717,2723 ****
  	conn->client_encoding = PG_SQL_ASCII;
  	conn->std_strings = false;	/* unless server says differently */
  	conn->verbosity = PQERRORS_DEFAULT;
! 	conn->sock = -1;
  	conn->auth_req_received = false;
  	conn->password_needed = false;
  	conn->dot_pgpass_used = false;
--- 2701,2707 ----
  	conn->client_encoding = PG_SQL_ASCII;
  	conn->std_strings = false;	/* unless server says differently */
  	conn->verbosity = PQERRORS_DEFAULT;
! 	conn->sock = PGINVALID_SOCKET;
  	conn->auth_req_received = false;
  	conn->password_needed = false;
  	conn->dot_pgpass_used = false;
*************** closePGconn(PGconn *conn)
*** 2882,2888 ****
  	 * Note that the protocol doesn't allow us to send Terminate messages
  	 * during the startup phase.
  	 */
! 	if (conn->sock >= 0 && conn->status == CONNECTION_OK)
  	{
  		/*
  		 * Try to send "close connection" message to backend. Ignore any
--- 2866,2872 ----
  	 * Note that the protocol doesn't allow us to send Terminate messages
  	 * during the startup phase.
  	 */
! 	if (conn->sock != PGINVALID_SOCKET && conn->status == CONNECTION_OK)
  	{
  		/*
  		 * Try to send "close connection" message to backend. Ignore any
*************** PQgetCancel(PGconn *conn)
*** 3103,3109 ****
  	if (!conn)
  		return NULL;
  
! 	if (conn->sock < 0)
  		return NULL;
  
  	cancel = malloc(sizeof(PGcancel));
--- 3087,3093 ----
  	if (!conn)
  		return NULL;
  
! 	if (conn->sock == PGINVALID_SOCKET)
  		return NULL;
  
  	cancel = malloc(sizeof(PGcancel));
*************** PQrequestCancel(PGconn *conn)
*** 3284,3290 ****
  	if (!conn)
  		return FALSE;
  
! 	if (conn->sock < 0)
  	{
  		strlcpy(conn->errorMessage.data,
  				"PQrequestCancel() -- connection is not open\n",
--- 3268,3274 ----
  	if (!conn)
  		return FALSE;
  
! 	if (conn->sock == PGINVALID_SOCKET)
  	{
  		strlcpy(conn->errorMessage.data,
  				"PQrequestCancel() -- connection is not open\n",
*************** PQsocket(const PGconn *conn)
*** 5329,5335 ****
  {
  	if (!conn)
  		return -1;
! 	return conn->sock;
  }
  
  int
--- 5313,5319 ----
  {
  	if (!conn)
  		return -1;
! 	return (conn->sock != PGINVALID_SOCKET) ? conn->sock : -1;
  }
  
  int
diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c
new file mode 100644
index 8ccf6d3..50e4035
*** a/src/interfaces/libpq/fe-exec.c
--- b/src/interfaces/libpq/fe-exec.c
*************** PQfn(PGconn *conn,
*** 2549,2555 ****
  	/* clear the error string */
  	resetPQExpBuffer(&conn->errorMessage);
  
! 	if (conn->sock < 0 || conn->asyncStatus != PGASYNC_IDLE ||
  		conn->result != NULL)
  	{
  		printfPQExpBuffer(&conn->errorMessage,
--- 2549,2555 ----
  	/* clear the error string */
  	resetPQExpBuffer(&conn->errorMessage);
  
! 	if (conn->sock == PGINVALID_SOCKET || conn->asyncStatus != PGASYNC_IDLE ||
  		conn->result != NULL)
  	{
  		printfPQExpBuffer(&conn->errorMessage,
diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c
new file mode 100644
index a7afd42..cc487b2
*** a/src/interfaces/libpq/fe-misc.c
--- b/src/interfaces/libpq/fe-misc.c
*************** pqReadData(PGconn *conn)
*** 604,610 ****
  	int			someread = 0;
  	int			nread;
  
! 	if (conn->sock < 0)
  	{
  		printfPQExpBuffer(&conn->errorMessage,
  						  libpq_gettext("connection not open\n"));
--- 604,610 ----
  	int			someread = 0;
  	int			nread;
  
! 	if (conn->sock == PGINVALID_SOCKET)
  	{
  		printfPQExpBuffer(&conn->errorMessage,
  						  libpq_gettext("connection not open\n"));
*************** pqSendSome(PGconn *conn, int len)
*** 800,806 ****
  	int			remaining = conn->outCount;
  	int			result = 0;
  
! 	if (conn->sock < 0)
  	{
  		printfPQExpBuffer(&conn->errorMessage,
  						  libpq_gettext("connection not open\n"));
--- 800,806 ----
  	int			remaining = conn->outCount;
  	int			result = 0;
  
! 	if (conn->sock == PGINVALID_SOCKET)
  	{
  		printfPQExpBuffer(&conn->errorMessage,
  						  libpq_gettext("connection not open\n"));
*************** pqSocketCheck(PGconn *conn, int forRead,
*** 1011,1017 ****
  
  	if (!conn)
  		return -1;
! 	if (conn->sock < 0)
  	{
  		printfPQExpBuffer(&conn->errorMessage,
  						  libpq_gettext("socket not open\n"));
--- 1011,1017 ----
  
  	if (!conn)
  		return -1;
! 	if (conn->sock == PGINVALID_SOCKET)
  	{
  		printfPQExpBuffer(&conn->errorMessage,
  						  libpq_gettext("socket not open\n"));
diff --git a/src/interfaces/libpq/fe-protocol2.c b/src/interfaces/libpq/fe-protocol2.c
new file mode 100644
index f3fddaa..10510b5
*** a/src/interfaces/libpq/fe-protocol2.c
--- b/src/interfaces/libpq/fe-protocol2.c
*************** pqGetline2(PGconn *conn, char *s, int ma
*** 1211,1217 ****
  {
  	int			result = 1;		/* return value if buffer overflows */
  
! 	if (conn->sock < 0 ||
  		conn->asyncStatus != PGASYNC_COPY_OUT)
  	{
  		*s = '\0';
--- 1211,1217 ----
  {
  	int			result = 1;		/* return value if buffer overflows */
  
! 	if (conn->sock == PGINVALID_SOCKET ||
  		conn->asyncStatus != PGASYNC_COPY_OUT)
  	{
  		*s = '\0';
diff --git a/src/interfaces/libpq/fe-protocol3.c b/src/interfaces/libpq/fe-protocol3.c
new file mode 100644
index 47cd7f4..d895589
*** a/src/interfaces/libpq/fe-protocol3.c
--- b/src/interfaces/libpq/fe-protocol3.c
*************** pqGetline3(PGconn *conn, char *s, int ma
*** 1568,1574 ****
  {
  	int			status;
  
! 	if (conn->sock < 0 ||
  		(conn->asyncStatus != PGASYNC_COPY_OUT &&
  		 conn->asyncStatus != PGASYNC_COPY_BOTH) ||
  		conn->copy_is_binary)
--- 1568,1574 ----
  {
  	int			status;
  
! 	if (conn->sock == PGINVALID_SOCKET ||
  		(conn->asyncStatus != PGASYNC_COPY_OUT &&
  		 conn->asyncStatus != PGASYNC_COPY_BOTH) ||
  		conn->copy_is_binary)
diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h
new file mode 100644
index ee975d4..0725c17
*** a/src/interfaces/libpq/libpq-int.h
--- b/src/interfaces/libpq/libpq-int.h
*************** struct pg_conn
*** 365,371 ****
  
  	/* Connection data */
  	/* See PQconnectPoll() for how we use 'int' and not 'pgsocket'. */
! 	int			sock;			/* Unix FD for socket, -1 if not connected */
  	SockAddr	laddr;			/* Local address */
  	SockAddr	raddr;			/* Remote address */
  	ProtocolVersion pversion;	/* FE/BE protocol version in use */
--- 365,371 ----
  
  	/* Connection data */
  	/* See PQconnectPoll() for how we use 'int' and not 'pgsocket'. */
! 	pgsocket	sock;			/* FD for socket, PGINVALID_SOCKET if unconnected */
  	SockAddr	laddr;			/* Local address */
  	SockAddr	raddr;			/* Remote address */
  	ProtocolVersion pversion;	/* FE/BE protocol version in use */
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to