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
*/
@@ -719,15 +738,15 @@ StreamConnection(pgsocket server_fd, Port *port)
719
738
if (setsockopt (port -> sock , IPPROTO_TCP , TCP_NODELAY ,
720
739
(char * ) & on , sizeof (on )) < 0 )
721
740
{
722
- elog (LOG , "setsockopt(TCP_NODELAY ) failed: %m" );
741
+ elog (LOG , "setsockopt(%s ) failed: %m" , "TCP_NODELAY " );
723
742
return STATUS_ERROR ;
724
743
}
725
744
#endif
726
745
on = 1 ;
727
746
if (setsockopt (port -> sock , SOL_SOCKET , SO_KEEPALIVE ,
728
747
(char * ) & on , sizeof (on )) < 0 )
729
748
{
730
- elog (LOG , "setsockopt(SO_KEEPALIVE ) failed: %m" );
749
+ elog (LOG , "setsockopt(%s ) failed: %m" , "SO_KEEPALIVE " );
731
750
return STATUS_ERROR ;
732
751
}
733
752
@@ -758,7 +777,7 @@ StreamConnection(pgsocket server_fd, Port *port)
758
777
if (getsockopt (port -> sock , SOL_SOCKET , SO_SNDBUF , (char * ) & oldopt ,
759
778
& optlen ) < 0 )
760
779
{
761
- elog (LOG , "getsockopt(SO_SNDBUF ) failed: %m" );
780
+ elog (LOG , "getsockopt(%s ) failed: %m" , "SO_SNDBUF " );
762
781
return STATUS_ERROR ;
763
782
}
764
783
newopt = PQ_SEND_BUFFER_SIZE * 4 ;
@@ -767,7 +786,7 @@ StreamConnection(pgsocket server_fd, Port *port)
767
786
if (setsockopt (port -> sock , SOL_SOCKET , SO_SNDBUF , (char * ) & newopt ,
768
787
sizeof (newopt )) < 0 )
769
788
{
770
- elog (LOG , "setsockopt(SO_SNDBUF ) failed: %m" );
789
+ elog (LOG , "setsockopt(%s ) failed: %m" , "SO_SNDBUF " );
771
790
return STATUS_ERROR ;
772
791
}
773
792
}
@@ -1653,7 +1672,7 @@ pq_setkeepaliveswin32(Port *port, int idle, int interval)
1653
1672
int
1654
1673
pq_getkeepalivesidle (Port * port )
1655
1674
{
1656
- #if defined(TCP_KEEPIDLE ) || defined(TCP_KEEPALIVE_THRESHOLD ) || defined( TCP_KEEPALIVE ) || defined( WIN32 )
1675
+ #if defined(PG_TCP_KEEPALIVE_IDLE ) || defined(SIO_KEEPALIVE_VALS )
1657
1676
if (port == NULL || IS_AF_UNIX (port -> laddr .addr .ss_family ))
1658
1677
return 0 ;
1659
1678
@@ -1665,34 +1684,13 @@ pq_getkeepalivesidle(Port *port)
1665
1684
#ifndef WIN32
1666
1685
ACCEPT_TYPE_ARG3 size = sizeof (port -> default_keepalives_idle );
1667
1686
1668
- #if defined(TCP_KEEPIDLE )
1669
- /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
1670
- if (getsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPIDLE ,
1671
- (char * ) & port -> default_keepalives_idle ,
1672
- & size ) < 0 )
1673
- {
1674
- elog (LOG , "getsockopt(TCP_KEEPIDLE) failed: %m" );
1675
- port -> default_keepalives_idle = -1 ; /* don't know */
1676
- }
1677
- #elif defined(TCP_KEEPALIVE_THRESHOLD )
1678
- /* TCP_KEEPALIVE_THRESHOLD is the name of this option on Solaris */
1679
- if (getsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPALIVE_THRESHOLD ,
1687
+ if (getsockopt (port -> sock , IPPROTO_TCP , PG_TCP_KEEPALIVE_IDLE ,
1680
1688
(char * ) & port -> default_keepalives_idle ,
1681
1689
& size ) < 0 )
1682
1690
{
1683
- elog (LOG , "getsockopt(TCP_KEEPALIVE_THRESHOLD ) failed: %m" );
1691
+ elog (LOG , "getsockopt(%s ) failed: %m" , PG_TCP_KEEPALIVE_IDLE_STR );
1684
1692
port -> default_keepalives_idle = -1 ; /* don't know */
1685
1693
}
1686
- #else /* must have TCP_KEEPALIVE */
1687
- /* TCP_KEEPALIVE is the name of this option on macOS */
1688
- if (getsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPALIVE ,
1689
- (char * ) & port -> default_keepalives_idle ,
1690
- & size ) < 0 )
1691
- {
1692
- elog (LOG , "getsockopt(TCP_KEEPALIVE) failed: %m" );
1693
- port -> default_keepalives_idle = -1 ; /* don't know */
1694
- }
1695
- #endif /* KEEPIDLE/KEEPALIVE_THRESHOLD/KEEPALIVE */
1696
1694
#else /* WIN32 */
1697
1695
/* We can't get the defaults on Windows, so return "don't know" */
1698
1696
port -> default_keepalives_idle = -1 ;
@@ -1712,7 +1710,7 @@ pq_setkeepalivesidle(int idle, Port *port)
1712
1710
return STATUS_OK ;
1713
1711
1714
1712
/* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
1715
- #if defined(TCP_KEEPIDLE ) || defined( TCP_KEEPALIVE_THRESHOLD ) || defined( TCP_KEEPALIVE ) || defined(SIO_KEEPALIVE_VALS )
1713
+ #if defined(PG_TCP_KEEPALIVE_IDLE ) || defined(SIO_KEEPALIVE_VALS )
1716
1714
if (idle == port -> keepalives_idle )
1717
1715
return STATUS_OK ;
1718
1716
@@ -1731,43 +1729,25 @@ pq_setkeepalivesidle(int idle, Port *port)
1731
1729
if (idle == 0 )
1732
1730
idle = port -> default_keepalives_idle ;
1733
1731
1734
- #if defined(TCP_KEEPIDLE )
1735
- /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
1736
- if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPIDLE ,
1737
- (char * ) & idle , sizeof (idle )) < 0 )
1738
- {
1739
- elog (LOG , "setsockopt(TCP_KEEPIDLE) failed: %m" );
1740
- return STATUS_ERROR ;
1741
- }
1742
- #elif defined(TCP_KEEPALIVE_THRESHOLD )
1743
- /* TCP_KEEPALIVE_THRESHOLD is the name of this option on Solaris */
1744
- if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPALIVE_THRESHOLD ,
1732
+ if (setsockopt (port -> sock , IPPROTO_TCP , PG_TCP_KEEPALIVE_IDLE ,
1745
1733
(char * ) & idle , sizeof (idle )) < 0 )
1746
1734
{
1747
- elog (LOG , "setsockopt(TCP_KEEPALIVE_THRESHOLD ) failed: %m" );
1735
+ elog (LOG , "setsockopt(%s ) failed: %m" , PG_TCP_KEEPALIVE_IDLE_STR );
1748
1736
return STATUS_ERROR ;
1749
1737
}
1750
- #else /* must have TCP_KEEPALIVE */
1751
- /* TCP_KEEPALIVE is the name of this option on macOS */
1752
- if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPALIVE ,
1753
- (char * ) & idle , sizeof (idle )) < 0 )
1754
- {
1755
- elog (LOG , "setsockopt(TCP_KEEPALIVE) failed: %m" );
1756
- return STATUS_ERROR ;
1757
- }
1758
- #endif
1759
1738
1760
1739
port -> keepalives_idle = idle ;
1761
1740
#else /* WIN32 */
1762
1741
return pq_setkeepaliveswin32 (port , idle , port -> keepalives_interval );
1763
1742
#endif
1764
- #else /* no way to set it */
1743
+ #else
1765
1744
if (idle != 0 )
1766
1745
{
1767
1746
elog (LOG , "setting the keepalive idle time is not supported" );
1768
1747
return STATUS_ERROR ;
1769
1748
}
1770
1749
#endif
1750
+
1771
1751
return STATUS_OK ;
1772
1752
}
1773
1753
@@ -1790,7 +1770,7 @@ pq_getkeepalivesinterval(Port *port)
1790
1770
(char * ) & port -> default_keepalives_interval ,
1791
1771
& size ) < 0 )
1792
1772
{
1793
- elog (LOG , "getsockopt(TCP_KEEPINTVL ) failed: %m" );
1773
+ elog (LOG , "getsockopt(%s ) failed: %m" , "TCP_KEEPINTVL " );
1794
1774
port -> default_keepalives_interval = -1 ; /* don't know */
1795
1775
}
1796
1776
#else
@@ -1833,7 +1813,7 @@ pq_setkeepalivesinterval(int interval, Port *port)
1833
1813
if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPINTVL ,
1834
1814
(char * ) & interval , sizeof (interval )) < 0 )
1835
1815
{
1836
- elog (LOG , "setsockopt(TCP_KEEPINTVL ) failed: %m" );
1816
+ elog (LOG , "setsockopt(%s ) failed: %m" , "TCP_KEEPINTVL " );
1837
1817
return STATUS_ERROR ;
1838
1818
}
1839
1819
@@ -1844,7 +1824,7 @@ pq_setkeepalivesinterval(int interval, Port *port)
1844
1824
#else
1845
1825
if (interval != 0 )
1846
1826
{
1847
- elog (LOG , "setsockopt(TCP_KEEPINTVL ) not supported" );
1827
+ elog (LOG , "setsockopt(%s ) not supported" , "TCP_KEEPINTVL " );
1848
1828
return STATUS_ERROR ;
1849
1829
}
1850
1830
#endif
@@ -1870,7 +1850,7 @@ pq_getkeepalivescount(Port *port)
1870
1850
(char * ) & port -> default_keepalives_count ,
1871
1851
& size ) < 0 )
1872
1852
{
1873
- elog (LOG , "getsockopt(TCP_KEEPCNT ) failed: %m" );
1853
+ elog (LOG , "getsockopt(%s ) failed: %m" , "TCP_KEEPCNT " );
1874
1854
port -> default_keepalives_count = -1 ; /* don't know */
1875
1855
}
1876
1856
}
@@ -1908,15 +1888,15 @@ pq_setkeepalivescount(int count, Port *port)
1908
1888
if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPCNT ,
1909
1889
(char * ) & count , sizeof (count )) < 0 )
1910
1890
{
1911
- elog (LOG , "setsockopt(TCP_KEEPCNT ) failed: %m" );
1891
+ elog (LOG , "setsockopt(%s ) failed: %m" , "TCP_KEEPCNT " );
1912
1892
return STATUS_ERROR ;
1913
1893
}
1914
1894
1915
1895
port -> keepalives_count = count ;
1916
1896
#else
1917
1897
if (count != 0 )
1918
1898
{
1919
- elog (LOG , "setsockopt(TCP_KEEPCNT ) not supported" );
1899
+ elog (LOG , "setsockopt(%s ) not supported" , "TCP_KEEPCNT " );
1920
1900
return STATUS_ERROR ;
1921
1901
}
1922
1902
#endif
0 commit comments