2929 * INTERFACE ROUTINES
3030 *
3131 * setup/teardown:
32- *StreamServerPort- Open postmaster's server port
33- *StreamConnection- Create new connection with client
34- *StreamClose- Close a client/backend connection
32+ *ListenServerPort- Open postmaster's server port
33+ *AcceptConnection- Accept new connection with client
3534 *TouchSocketFiles- Protect socket files against /tmp cleaners
36- *pq_init- initialize libpq at backend startup
35+ *pq_init - initialize libpq at backend startup
3736 *socket_comm_reset- reset libpq during error recovery
3837 *socket_close- shutdown libpq at backend exit
3938 *
@@ -168,13 +167,18 @@ WaitEventSet *FeBeWaitSet;
168167 *pq_init - initialize libpq at backend startup
169168 * --------------------------------
170169 */
171- void
172- pq_init (void )
170+ Port *
171+ pq_init (ClientSocket * client_sock )
173172{
174- Port * port = MyProcPort ;
173+ Port * port ;
175174int socket_pos PG_USED_FOR_ASSERTS_ONLY ;
176175int latch_pos PG_USED_FOR_ASSERTS_ONLY ;
177176
177+ /* allocate the Port struct and copy the ClientSocket contents to it */
178+ port = palloc0 (sizeof (Port ));
179+ port -> sock = client_sock -> sock ;
180+ port -> raddr = client_sock -> raddr ;
181+
178182/* fill in the server (local) address */
179183port -> laddr .salen = sizeof (port -> laddr .addr );
180184if (getsockname (port -> sock ,
@@ -310,6 +314,8 @@ pq_init(void)
310314 */
311315Assert (socket_pos == FeBeWaitSetSocketPos );
312316Assert (latch_pos == FeBeWaitSetLatchPos );
317+
318+ return port ;
313319}
314320
315321/* --------------------------------
@@ -384,16 +390,13 @@ socket_close(int code, Datum arg)
384390
385391
386392
387- /*
388- * Streams -- wrapper around Unix socket system calls
389- *
390- *
391- *Stream functions are used for vanilla TCP connection protocol.
393+ /* --------------------------------
394+ * Postmaster functions to handle sockets.
395+ * --------------------------------
392396 */
393397
394-
395398/*
396- *StreamServerPort -- open a "listening" port to accept connections.
399+ *ListenServerPort -- open a "listening" port to accept connections.
397400 *
398401 * family should be AF_UNIX or AF_UNSPEC; portNumber is the port number.
399402 * For AF_UNIX ports, hostName should be NULL and unixSocketDir must be
@@ -408,7 +411,7 @@ socket_close(int code, Datum arg)
408411 * RETURNS: STATUS_OK or STATUS_ERROR
409412 */
410413int
411- StreamServerPort (int family ,const char * hostName ,unsigned short portNumber ,
414+ ListenServerPort (int family ,const char * hostName ,unsigned short portNumber ,
412415const char * unixSocketDir ,
413416pgsocket ListenSockets [],int * NumListenSockets ,int MaxListen )
414417{
@@ -774,22 +777,23 @@ Setup_AF_UNIX(const char *sock_path)
774777
775778
776779/*
777- * StreamConnection -- create a new connection with client using
778- *server port. Set port->sock to the FD of the new connection.
780+ * AcceptConnection -- accept a new connection with client using
781+ *server port. Fills *client_sock with the FD and endpoint info
782+ *of the new connection.
779783 *
780784 * ASSUME: that this doesn't need to be non-blocking because
781785 *the Postmaster waits for the socket to be ready to accept().
782786 *
783787 * RETURNS: STATUS_OK or STATUS_ERROR
784788 */
785789int
786- StreamConnection (pgsocket server_fd ,Port * port )
790+ AcceptConnection (pgsocket server_fd ,ClientSocket * client_sock )
787791{
788792/* accept connection and fill in the client (remote) address */
789- port -> raddr .salen = sizeof (port -> raddr .addr );
790- if ((port -> sock = accept (server_fd ,
791- (struct sockaddr * )& port -> raddr .addr ,
792- & port -> raddr .salen ))== PGINVALID_SOCKET )
793+ client_sock -> raddr .salen = sizeof (client_sock -> raddr .addr );
794+ if ((client_sock -> sock = accept (server_fd ,
795+ (struct sockaddr * )& client_sock -> raddr .addr ,
796+ & client_sock -> raddr .salen ))== PGINVALID_SOCKET )
793797{
794798ereport (LOG ,
795799(errcode_for_socket_access (),
@@ -809,23 +813,6 @@ StreamConnection(pgsocket server_fd, Port *port)
809813return STATUS_OK ;
810814}
811815
812- /*
813- * StreamClose -- close a client/backend connection
814- *
815- * NOTE: this is NOT used to terminate a session; it is just used to release
816- * the file descriptor in a process that should no longer have the socket
817- * open. (For example, the postmaster calls this after passing ownership
818- * of the connection to a child process.) It is expected that someone else
819- * still has the socket open. So, we only want to close the descriptor,
820- * we do NOT want to send anything to the far end.
821- */
822- void
823- StreamClose (pgsocket sock )
824- {
825- if (closesocket (sock )!= 0 )
826- elog (LOG ,"could not close client or listen socket: %m" );
827- }
828-
829816/*
830817 * TouchSocketFiles -- mark socket files as recently accessed
831818 *