Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb0bea38

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

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

‎src/backend/libpq/pqcomm.c

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

460460
#ifndefWIN32
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(pgsocketsock)
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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,6 @@ typedef struct
496496
Portport;
497497
InheritableSocketportsocket;
498498
charDataDir[MAXPGPATH];
499-
pgsocketListenSocket[MAXLISTEN];
500499
int32MyCancelKey;
501500
intMyPMChildSlot;
502501
#ifndefWIN32
@@ -2545,8 +2544,6 @@ ConnFree(Port *port)
25452544
void
25462545
ClosePostmasterPorts(boolam_syslogger)
25472546
{
2548-
inti;
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+
#ifndefEXEC_BACKEND
2578+
for (inti=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)

[8]ページ先頭

©2009-2025 Movatter.jp