@@ -227,7 +227,8 @@ int ReservedConnections;
227
227
228
228
/* The socket(s) we're listening to. */
229
229
#define MAXLISTEN 64
230
- static pgsocket ListenSocket [MAXLISTEN ];
230
+ static int NumListenSockets = 0 ;
231
+ static pgsocket * ListenSockets = NULL ;
231
232
232
233
/* still more option variables */
233
234
bool EnableSSL = false;
@@ -588,7 +589,6 @@ PostmasterMain(int argc, char *argv[])
588
589
int status ;
589
590
char * userDoption = NULL ;
590
591
bool listen_addr_saved = false;
591
- int i ;
592
592
char * output_config_variable = NULL ;
593
593
594
594
InitProcessGlobals ();
@@ -1142,17 +1142,6 @@ PostmasterMain(int argc, char *argv[])
1142
1142
errmsg ("could not remove file \"%s\": %m" ,
1143
1143
LOG_METAINFO_DATAFILE )));
1144
1144
1145
- /*
1146
- * Initialize input sockets.
1147
- *
1148
- * Mark them all closed, and set up an on_proc_exit function that's
1149
- * charged with closing the sockets again at postmaster shutdown.
1150
- */
1151
- for (i = 0 ; i < MAXLISTEN ; i ++ )
1152
- ListenSocket [i ] = PGINVALID_SOCKET ;
1153
-
1154
- on_proc_exit (CloseServerPorts , 0 );
1155
-
1156
1145
/*
1157
1146
* If enabled, start up syslogger collection subprocess
1158
1147
*/
@@ -1187,7 +1176,13 @@ PostmasterMain(int argc, char *argv[])
1187
1176
1188
1177
/*
1189
1178
* Establish input sockets.
1179
+ *
1180
+ * First set up an on_proc_exit function that's charged with closing the
1181
+ * sockets again at postmaster shutdown.
1190
1182
*/
1183
+ ListenSockets = palloc (MAXLISTEN * sizeof (pgsocket ));
1184
+ on_proc_exit (CloseServerPorts , 0 );
1185
+
1191
1186
if (ListenAddresses )
1192
1187
{
1193
1188
char * rawstring ;
@@ -1216,12 +1211,16 @@ PostmasterMain(int argc, char *argv[])
1216
1211
status = StreamServerPort (AF_UNSPEC , NULL ,
1217
1212
(unsigned short ) PostPortNumber ,
1218
1213
NULL ,
1219
- ListenSocket , MAXLISTEN );
1214
+ ListenSockets ,
1215
+ & NumListenSockets ,
1216
+ MAXLISTEN );
1220
1217
else
1221
1218
status = StreamServerPort (AF_UNSPEC , curhost ,
1222
1219
(unsigned short ) PostPortNumber ,
1223
1220
NULL ,
1224
- ListenSocket , MAXLISTEN );
1221
+ ListenSockets ,
1222
+ & NumListenSockets ,
1223
+ MAXLISTEN );
1225
1224
1226
1225
if (status == STATUS_OK )
1227
1226
{
@@ -1249,7 +1248,7 @@ PostmasterMain(int argc, char *argv[])
1249
1248
1250
1249
#ifdef USE_BONJOUR
1251
1250
/* Register for Bonjour only if we opened TCP socket(s) */
1252
- if (enable_bonjour && ListenSocket [ 0 ] != PGINVALID_SOCKET )
1251
+ if (enable_bonjour && NumListenSockets > 0 )
1253
1252
{
1254
1253
DNSServiceErrorType err ;
1255
1254
@@ -1313,7 +1312,9 @@ PostmasterMain(int argc, char *argv[])
1313
1312
status = StreamServerPort (AF_UNIX , NULL ,
1314
1313
(unsigned short ) PostPortNumber ,
1315
1314
socketdir ,
1316
- ListenSocket , MAXLISTEN );
1315
+ ListenSockets ,
1316
+ & NumListenSockets ,
1317
+ MAXLISTEN );
1317
1318
1318
1319
if (status == STATUS_OK )
1319
1320
{
@@ -1339,7 +1340,7 @@ PostmasterMain(int argc, char *argv[])
1339
1340
/*
1340
1341
* check that we have some socket to listen on
1341
1342
*/
1342
- if (ListenSocket [ 0 ] == PGINVALID_SOCKET )
1343
+ if (NumListenSockets == 0 )
1343
1344
ereport (FATAL ,
1344
1345
(errmsg ("no socket created for listening" )));
1345
1346
@@ -1487,14 +1488,9 @@ CloseServerPorts(int status, Datum arg)
1487
1488
* before we remove the postmaster.pid lockfile; otherwise there's a race
1488
1489
* condition if a new postmaster wants to re-use the TCP port number.
1489
1490
*/
1490
- for (i = 0 ; i < MAXLISTEN ; i ++ )
1491
- {
1492
- if (ListenSocket [i ] != PGINVALID_SOCKET )
1493
- {
1494
- StreamClose (ListenSocket [i ]);
1495
- ListenSocket [i ] = PGINVALID_SOCKET ;
1496
- }
1497
- }
1491
+ for (i = 0 ; i < NumListenSockets ; i ++ )
1492
+ StreamClose (ListenSockets [i ]);
1493
+ NumListenSockets = 0 ;
1498
1494
1499
1495
/*
1500
1496
* Next, remove any filesystem entries for Unix sockets. To avoid race
@@ -1695,29 +1691,19 @@ DetermineSleepTime(void)
1695
1691
static void
1696
1692
ConfigurePostmasterWaitSet (bool accept_connections )
1697
1693
{
1698
- int nsockets ;
1699
-
1700
1694
if (pm_wait_set )
1701
1695
FreeWaitEventSet (pm_wait_set );
1702
1696
pm_wait_set = NULL ;
1703
1697
1704
- /* How many server sockets do we need to wait for? */
1705
- nsockets = 0 ;
1706
- if (accept_connections )
1707
- {
1708
- while (nsockets < MAXLISTEN &&
1709
- ListenSocket [nsockets ] != PGINVALID_SOCKET )
1710
- ++ nsockets ;
1711
- }
1712
-
1713
- pm_wait_set = CreateWaitEventSet (CurrentMemoryContext , 1 + nsockets );
1698
+ pm_wait_set = CreateWaitEventSet (CurrentMemoryContext ,
1699
+ accept_connections ? (1 + NumListenSockets ) : 1 );
1714
1700
AddWaitEventToSet (pm_wait_set , WL_LATCH_SET , PGINVALID_SOCKET , MyLatch ,
1715
1701
NULL );
1716
1702
1717
1703
if (accept_connections )
1718
1704
{
1719
- for (int i = 0 ; i < nsockets ; i ++ )
1720
- AddWaitEventToSet (pm_wait_set , WL_SOCKET_ACCEPT , ListenSocket [i ],
1705
+ for (int i = 0 ; i < NumListenSockets ; i ++ )
1706
+ AddWaitEventToSet (pm_wait_set , WL_SOCKET_ACCEPT , ListenSockets [i ],
1721
1707
NULL , NULL );
1722
1708
}
1723
1709
}
@@ -2579,14 +2565,11 @@ ClosePostmasterPorts(bool am_syslogger)
2579
2565
* EXEC_BACKEND mode.
2580
2566
*/
2581
2567
#ifndef EXEC_BACKEND
2582
- for (int i = 0 ; i < MAXLISTEN ; i ++ )
2583
- {
2584
- if (ListenSocket [i ] != PGINVALID_SOCKET )
2585
- {
2586
- StreamClose (ListenSocket [i ]);
2587
- ListenSocket [i ] = PGINVALID_SOCKET ;
2588
- }
2589
- }
2568
+ for (int i = 0 ; i < NumListenSockets ; i ++ )
2569
+ StreamClose (ListenSockets [i ]);
2570
+ NumListenSockets = 0 ;
2571
+ pfree (ListenSockets );
2572
+ ListenSockets = NULL ;
2590
2573
#endif
2591
2574
2592
2575
/*
0 commit comments