96
96
#include "utils/guc.h"
97
97
#include "utils/memutils.h"
98
98
99
+ /*
100
+ * Cope with the various platform-specific ways to spell TCP keepalive socket
101
+ * options. This doesn't cover Windows, which as usual does its own thing.
102
+ */
103
+ #if defined(TCP_KEEPIDLE )
104
+ /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
105
+ #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPIDLE
106
+ #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPIDLE"
107
+ #elif defined(TCP_KEEPALIVE_THRESHOLD )
108
+ /* TCP_KEEPALIVE_THRESHOLD is the name of this option on Solaris >= 11 */
109
+ #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE_THRESHOLD
110
+ #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE_THRESHOLD"
111
+ #elif defined(TCP_KEEPALIVE ) && defined(__darwin__ )
112
+ /* TCP_KEEPALIVE is the name of this option on macOS */
113
+ /* Caution: Solaris has this symbol but it means something different */
114
+ #define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE
115
+ #define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE"
116
+ #endif
117
+
99
118
/*
100
119
* Configuration options
101
120
*/
@@ -415,7 +434,8 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
415
434
{
416
435
ereport (LOG ,
417
436
(errcode_for_socket_access (),
418
- errmsg ("setsockopt(SO_REUSEADDR) failed: %m" )));
437
+ errmsg ("setsockopt(%s) failed: %m" ,
438
+ "SO_REUSEADDR" )));
419
439
closesocket (fd );
420
440
continue ;
421
441
}
@@ -430,7 +450,8 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
430
450
{
431
451
ereport (LOG ,
432
452
(errcode_for_socket_access (),
433
- errmsg ("setsockopt(IPV6_V6ONLY) failed: %m" )));
453
+ errmsg ("setsockopt(%s) failed: %m" ,
454
+ "IPV6_V6ONLY" )));
434
455
closesocket (fd );
435
456
continue ;
436
457
}
@@ -667,15 +688,15 @@ StreamConnection(pgsocket server_fd, Port *port)
667
688
if (setsockopt (port -> sock , IPPROTO_TCP , TCP_NODELAY ,
668
689
(char * ) & on , sizeof (on )) < 0 )
669
690
{
670
- elog (LOG , "setsockopt(TCP_NODELAY ) failed: %m" );
691
+ elog (LOG , "setsockopt(%s ) failed: %m" , "TCP_NODELAY " );
671
692
return STATUS_ERROR ;
672
693
}
673
694
#endif
674
695
on = 1 ;
675
696
if (setsockopt (port -> sock , SOL_SOCKET , SO_KEEPALIVE ,
676
697
(char * ) & on , sizeof (on )) < 0 )
677
698
{
678
- elog (LOG , "setsockopt(SO_KEEPALIVE ) failed: %m" );
699
+ elog (LOG , "setsockopt(%s ) failed: %m" , "SO_KEEPALIVE " );
679
700
return STATUS_ERROR ;
680
701
}
681
702
@@ -689,7 +710,7 @@ StreamConnection(pgsocket server_fd, Port *port)
689
710
if (setsockopt (port -> sock , SOL_SOCKET , SO_SNDBUF , (char * ) & on ,
690
711
sizeof (on )) < 0 )
691
712
{
692
- elog (LOG , "setsockopt(SO_SNDBUF ) failed: %m" );
713
+ elog (LOG , "setsockopt(%s ) failed: %m" , "SO_SNDBUF " );
693
714
return STATUS_ERROR ;
694
715
}
695
716
#endif
@@ -1593,7 +1614,7 @@ pq_setkeepaliveswin32(Port *port, int idle, int interval)
1593
1614
int
1594
1615
pq_getkeepalivesidle (Port * port )
1595
1616
{
1596
- #if defined(TCP_KEEPIDLE ) || defined(TCP_KEEPALIVE_THRESHOLD ) || defined( TCP_KEEPALIVE ) || defined( WIN32 )
1617
+ #if defined(PG_TCP_KEEPALIVE_IDLE ) || defined(SIO_KEEPALIVE_VALS )
1597
1618
if (port == NULL || IS_AF_UNIX (port -> laddr .addr .ss_family ))
1598
1619
return 0 ;
1599
1620
@@ -1605,34 +1626,13 @@ pq_getkeepalivesidle(Port *port)
1605
1626
#ifndef WIN32
1606
1627
ACCEPT_TYPE_ARG3 size = sizeof (port -> default_keepalives_idle );
1607
1628
1608
- #if defined(TCP_KEEPIDLE )
1609
- /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
1610
- if (getsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPIDLE ,
1611
- (char * ) & port -> default_keepalives_idle ,
1612
- & size ) < 0 )
1613
- {
1614
- elog (LOG , "getsockopt(TCP_KEEPIDLE) failed: %m" );
1615
- port -> default_keepalives_idle = -1 ; /* don't know */
1616
- }
1617
- #elif defined(TCP_KEEPALIVE_THRESHOLD )
1618
- /* TCP_KEEPALIVE_THRESHOLD is the name of this option on Solaris */
1619
- if (getsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPALIVE_THRESHOLD ,
1629
+ if (getsockopt (port -> sock , IPPROTO_TCP , PG_TCP_KEEPALIVE_IDLE ,
1620
1630
(char * ) & port -> default_keepalives_idle ,
1621
1631
& size ) < 0 )
1622
1632
{
1623
- elog (LOG , "getsockopt(TCP_KEEPALIVE_THRESHOLD ) failed: %m" );
1633
+ elog (LOG , "getsockopt(%s ) failed: %m" , PG_TCP_KEEPALIVE_IDLE_STR );
1624
1634
port -> default_keepalives_idle = -1 ; /* don't know */
1625
1635
}
1626
- #else /* must have TCP_KEEPALIVE */
1627
- /* TCP_KEEPALIVE is the name of this option on macOS */
1628
- if (getsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPALIVE ,
1629
- (char * ) & port -> default_keepalives_idle ,
1630
- & size ) < 0 )
1631
- {
1632
- elog (LOG , "getsockopt(TCP_KEEPALIVE) failed: %m" );
1633
- port -> default_keepalives_idle = -1 ; /* don't know */
1634
- }
1635
- #endif /* KEEPIDLE/KEEPALIVE_THRESHOLD/KEEPALIVE */
1636
1636
#else /* WIN32 */
1637
1637
/* We can't get the defaults on Windows, so return "don't know" */
1638
1638
port -> default_keepalives_idle = -1 ;
@@ -1652,7 +1652,7 @@ pq_setkeepalivesidle(int idle, Port *port)
1652
1652
return STATUS_OK ;
1653
1653
1654
1654
/* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
1655
- #if defined(TCP_KEEPIDLE ) || defined( TCP_KEEPALIVE_THRESHOLD ) || defined( TCP_KEEPALIVE ) || defined(SIO_KEEPALIVE_VALS )
1655
+ #if defined(PG_TCP_KEEPALIVE_IDLE ) || defined(SIO_KEEPALIVE_VALS )
1656
1656
if (idle == port -> keepalives_idle )
1657
1657
return STATUS_OK ;
1658
1658
@@ -1671,43 +1671,25 @@ pq_setkeepalivesidle(int idle, Port *port)
1671
1671
if (idle == 0 )
1672
1672
idle = port -> default_keepalives_idle ;
1673
1673
1674
- #if defined(TCP_KEEPIDLE )
1675
- /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
1676
- if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPIDLE ,
1677
- (char * ) & idle , sizeof (idle )) < 0 )
1678
- {
1679
- elog (LOG , "setsockopt(TCP_KEEPIDLE) failed: %m" );
1680
- return STATUS_ERROR ;
1681
- }
1682
- #elif defined(TCP_KEEPALIVE_THRESHOLD )
1683
- /* TCP_KEEPALIVE_THRESHOLD is the name of this option on Solaris */
1684
- if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPALIVE_THRESHOLD ,
1674
+ if (setsockopt (port -> sock , IPPROTO_TCP , PG_TCP_KEEPALIVE_IDLE ,
1685
1675
(char * ) & idle , sizeof (idle )) < 0 )
1686
1676
{
1687
- elog (LOG , "setsockopt(TCP_KEEPALIVE_THRESHOLD ) failed: %m" );
1677
+ elog (LOG , "setsockopt(%s ) failed: %m" , PG_TCP_KEEPALIVE_IDLE_STR );
1688
1678
return STATUS_ERROR ;
1689
1679
}
1690
- #else /* must have TCP_KEEPALIVE */
1691
- /* TCP_KEEPALIVE is the name of this option on macOS */
1692
- if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPALIVE ,
1693
- (char * ) & idle , sizeof (idle )) < 0 )
1694
- {
1695
- elog (LOG , "setsockopt(TCP_KEEPALIVE) failed: %m" );
1696
- return STATUS_ERROR ;
1697
- }
1698
- #endif
1699
1680
1700
1681
port -> keepalives_idle = idle ;
1701
1682
#else /* WIN32 */
1702
1683
return pq_setkeepaliveswin32 (port , idle , port -> keepalives_interval );
1703
1684
#endif
1704
- #else /* no way to set it */
1685
+ #else
1705
1686
if (idle != 0 )
1706
1687
{
1707
1688
elog (LOG , "setting the keepalive idle time is not supported" );
1708
1689
return STATUS_ERROR ;
1709
1690
}
1710
1691
#endif
1692
+
1711
1693
return STATUS_OK ;
1712
1694
}
1713
1695
@@ -1730,7 +1712,7 @@ pq_getkeepalivesinterval(Port *port)
1730
1712
(char * ) & port -> default_keepalives_interval ,
1731
1713
& size ) < 0 )
1732
1714
{
1733
- elog (LOG , "getsockopt(TCP_KEEPINTVL ) failed: %m" );
1715
+ elog (LOG , "getsockopt(%s ) failed: %m" , "TCP_KEEPINTVL " );
1734
1716
port -> default_keepalives_interval = -1 ; /* don't know */
1735
1717
}
1736
1718
#else
@@ -1773,7 +1755,7 @@ pq_setkeepalivesinterval(int interval, Port *port)
1773
1755
if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPINTVL ,
1774
1756
(char * ) & interval , sizeof (interval )) < 0 )
1775
1757
{
1776
- elog (LOG , "setsockopt(TCP_KEEPINTVL ) failed: %m" );
1758
+ elog (LOG , "setsockopt(%s ) failed: %m" , "TCP_KEEPINTVL " );
1777
1759
return STATUS_ERROR ;
1778
1760
}
1779
1761
@@ -1784,7 +1766,7 @@ pq_setkeepalivesinterval(int interval, Port *port)
1784
1766
#else
1785
1767
if (interval != 0 )
1786
1768
{
1787
- elog (LOG , "setsockopt(TCP_KEEPINTVL ) not supported" );
1769
+ elog (LOG , "setsockopt(%s ) not supported" , "TCP_KEEPINTVL " );
1788
1770
return STATUS_ERROR ;
1789
1771
}
1790
1772
#endif
@@ -1810,7 +1792,7 @@ pq_getkeepalivescount(Port *port)
1810
1792
(char * ) & port -> default_keepalives_count ,
1811
1793
& size ) < 0 )
1812
1794
{
1813
- elog (LOG , "getsockopt(TCP_KEEPCNT ) failed: %m" );
1795
+ elog (LOG , "getsockopt(%s ) failed: %m" , "TCP_KEEPCNT " );
1814
1796
port -> default_keepalives_count = -1 ; /* don't know */
1815
1797
}
1816
1798
}
@@ -1848,15 +1830,15 @@ pq_setkeepalivescount(int count, Port *port)
1848
1830
if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPCNT ,
1849
1831
(char * ) & count , sizeof (count )) < 0 )
1850
1832
{
1851
- elog (LOG , "setsockopt(TCP_KEEPCNT ) failed: %m" );
1833
+ elog (LOG , "setsockopt(%s ) failed: %m" , "TCP_KEEPCNT " );
1852
1834
return STATUS_ERROR ;
1853
1835
}
1854
1836
1855
1837
port -> keepalives_count = count ;
1856
1838
#else
1857
1839
if (count != 0 )
1858
1840
{
1859
- elog (LOG , "setsockopt(TCP_KEEPCNT ) not supported" );
1841
+ elog (LOG , "setsockopt(%s ) not supported" , "TCP_KEEPCNT " );
1860
1842
return STATUS_ERROR ;
1861
1843
}
1862
1844
#endif
0 commit comments