Skip to content

Commit b0bea38

Browse files
committed
Use FD_CLOEXEC on ListenSockets
It's good hygiene if e.g. an extension launches a subprogram when being loaded. We went through some effort to close them in the child process in EXEC_BACKEND mode, but it's better to not hand them down to the child process in the first place. We still need to close them after fork when !EXEC_BACKEND, but it's a little simpler. In the passing, LOG a message if closing the client connection or listen socket fails. Shouldn't happen, but if it does, would be nice to know. Reviewed-by: Tristan Partin, Andres Freund, Thomas Munro Discussion: https://www.postgresql.org/message-id/7a59b073-5b5b-151e-7ed3-8b01ff7ce9ef@iki.fi
1 parent d71e605 commit b0bea38

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/backend/libpq/pqcomm.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,9 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
458458
}
459459

460460
#ifndef WIN32
461+
/* Don't give the listen socket to any subprograms we execute. */
462+
if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
463+
elog(FATAL, "fcntl(F_SETFD) failed on socket: %m");
461464

462465
/*
463466
* Without the SO_REUSEADDR flag, a new postmaster can't be started
@@ -831,7 +834,8 @@ StreamConnection(pgsocket server_fd, Port *port)
831834
void
832835
StreamClose(pgsocket sock)
833836
{
834-
closesocket(sock);
837+
if (closesocket(sock) != 0)
838+
elog(LOG, "could not close client or listen socket: %m");
835839
}
836840

837841
/*

src/backend/postmaster/postmaster.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,6 @@ typedef struct
496496
Port port;
497497
InheritableSocket portsocket;
498498
char DataDir[MAXPGPATH];
499-
pgsocket ListenSocket[MAXLISTEN];
500499
int32 MyCancelKey;
501500
int MyPMChildSlot;
502501
#ifndef WIN32
@@ -2545,8 +2544,6 @@ ConnFree(Port *port)
25452544
void
25462545
ClosePostmasterPorts(bool am_syslogger)
25472546
{
2548-
int i;
2549-
25502547
/* Release resources held by the postmaster's WaitEventSet. */
25512548
if (pm_wait_set)
25522549
{
@@ -2573,15 +2570,20 @@ ClosePostmasterPorts(bool am_syslogger)
25732570
/*
25742571
* Close the postmaster's listen sockets. These aren't tracked by fd.c,
25752572
* so we don't call ReleaseExternalFD() here.
2573+
*
2574+
* The listen sockets are marked as FD_CLOEXEC, so this isn't needed in
2575+
* EXEC_BACKEND mode.
25762576
*/
2577-
for (i = 0; i < MAXLISTEN; i++)
2577+
#ifndef EXEC_BACKEND
2578+
for (int i = 0; i < MAXLISTEN; i++)
25782579
{
25792580
if (ListenSocket[i] != PGINVALID_SOCKET)
25802581
{
25812582
StreamClose(ListenSocket[i]);
25822583
ListenSocket[i] = PGINVALID_SOCKET;
25832584
}
25842585
}
2586+
#endif
25852587

25862588
/*
25872589
* If using syslogger, close the read side of the pipe. We don't bother
@@ -6038,8 +6040,6 @@ save_backend_variables(BackendParameters *param, Port *port,
60386040

60396041
strlcpy(param->DataDir, DataDir, MAXPGPATH);
60406042

6041-
memcpy(&param->ListenSocket, &ListenSocket, sizeof(ListenSocket));
6042-
60436043
param->MyCancelKey = MyCancelKey;
60446044
param->MyPMChildSlot = MyPMChildSlot;
60456045

@@ -6271,8 +6271,6 @@ restore_backend_variables(BackendParameters *param, Port *port)
62716271

62726272
SetDataDir(param->DataDir);
62736273

6274-
memcpy(&ListenSocket, &param->ListenSocket, sizeof(ListenSocket));
6275-
62766274
MyCancelKey = param->MyCancelKey;
62776275
MyPMChildSlot = param->MyPMChildSlot;
62786276

0 commit comments

Comments
 (0)