29
29
* INTERFACE ROUTINES
30
30
*
31
31
* 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
35
34
* TouchSocketFiles - Protect socket files against /tmp cleaners
36
- * pq_init - initialize libpq at backend startup
35
+ * pq_init - initialize libpq at backend startup
37
36
* socket_comm_reset - reset libpq during error recovery
38
37
* socket_close - shutdown libpq at backend exit
39
38
*
@@ -168,13 +167,18 @@ WaitEventSet *FeBeWaitSet;
168
167
* pq_init - initialize libpq at backend startup
169
168
* --------------------------------
170
169
*/
171
- void
172
- pq_init (void )
170
+ Port *
171
+ pq_init (ClientSocket * client_sock )
173
172
{
174
- Port * port = MyProcPort ;
173
+ Port * port ;
175
174
int socket_pos PG_USED_FOR_ASSERTS_ONLY ;
176
175
int latch_pos PG_USED_FOR_ASSERTS_ONLY ;
177
176
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
+
178
182
/* fill in the server (local) address */
179
183
port -> laddr .salen = sizeof (port -> laddr .addr );
180
184
if (getsockname (port -> sock ,
@@ -310,6 +314,8 @@ pq_init(void)
310
314
*/
311
315
Assert (socket_pos == FeBeWaitSetSocketPos );
312
316
Assert (latch_pos == FeBeWaitSetLatchPos );
317
+
318
+ return port ;
313
319
}
314
320
315
321
/* --------------------------------
@@ -384,16 +390,13 @@ socket_close(int code, Datum arg)
384
390
385
391
386
392
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
+ * --------------------------------
392
396
*/
393
397
394
-
395
398
/*
396
- * StreamServerPort -- open a "listening" port to accept connections.
399
+ * ListenServerPort -- open a "listening" port to accept connections.
397
400
*
398
401
* family should be AF_UNIX or AF_UNSPEC; portNumber is the port number.
399
402
* For AF_UNIX ports, hostName should be NULL and unixSocketDir must be
@@ -408,7 +411,7 @@ socket_close(int code, Datum arg)
408
411
* RETURNS: STATUS_OK or STATUS_ERROR
409
412
*/
410
413
int
411
- StreamServerPort (int family , const char * hostName , unsigned short portNumber ,
414
+ ListenServerPort (int family , const char * hostName , unsigned short portNumber ,
412
415
const char * unixSocketDir ,
413
416
pgsocket ListenSockets [], int * NumListenSockets , int MaxListen )
414
417
{
@@ -774,22 +777,23 @@ Setup_AF_UNIX(const char *sock_path)
774
777
775
778
776
779
/*
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.
779
783
*
780
784
* ASSUME: that this doesn't need to be non-blocking because
781
785
* the Postmaster waits for the socket to be ready to accept().
782
786
*
783
787
* RETURNS: STATUS_OK or STATUS_ERROR
784
788
*/
785
789
int
786
- StreamConnection (pgsocket server_fd , Port * port )
790
+ AcceptConnection (pgsocket server_fd , ClientSocket * client_sock )
787
791
{
788
792
/* 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 )
793
797
{
794
798
ereport (LOG ,
795
799
(errcode_for_socket_access (),
@@ -809,23 +813,6 @@ StreamConnection(pgsocket server_fd, Port *port)
809
813
return STATUS_OK ;
810
814
}
811
815
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
-
829
816
/*
830
817
* TouchSocketFiles -- mark socket files as recently accessed
831
818
*
0 commit comments