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
*/
@@ -742,15 +761,15 @@ StreamConnection(pgsocket server_fd, Port *port)
742
761
if (setsockopt (port -> sock , IPPROTO_TCP , TCP_NODELAY ,
743
762
(char * ) & on , sizeof (on )) < 0 )
744
763
{
745
- elog (LOG , "setsockopt(TCP_NODELAY ) failed: %m" );
764
+ elog (LOG , "setsockopt(%s ) failed: %m" , "TCP_NODELAY " );
746
765
return STATUS_ERROR ;
747
766
}
748
767
#endif
749
768
on = 1 ;
750
769
if (setsockopt (port -> sock , SOL_SOCKET , SO_KEEPALIVE ,
751
770
(char * ) & on , sizeof (on )) < 0 )
752
771
{
753
- elog (LOG , "setsockopt(SO_KEEPALIVE ) failed: %m" );
772
+ elog (LOG , "setsockopt(%s ) failed: %m" , "SO_KEEPALIVE " );
754
773
return STATUS_ERROR ;
755
774
}
756
775
@@ -781,7 +800,7 @@ StreamConnection(pgsocket server_fd, Port *port)
781
800
if (getsockopt (port -> sock , SOL_SOCKET , SO_SNDBUF , (char * ) & oldopt ,
782
801
& optlen ) < 0 )
783
802
{
784
- elog (LOG , "getsockopt(SO_SNDBUF ) failed: %m" );
803
+ elog (LOG , "getsockopt(%s ) failed: %m" , "SO_SNDBUF " );
785
804
return STATUS_ERROR ;
786
805
}
787
806
newopt = PQ_SEND_BUFFER_SIZE * 4 ;
@@ -790,7 +809,7 @@ StreamConnection(pgsocket server_fd, Port *port)
790
809
if (setsockopt (port -> sock , SOL_SOCKET , SO_SNDBUF , (char * ) & newopt ,
791
810
sizeof (newopt )) < 0 )
792
811
{
793
- elog (LOG , "setsockopt(SO_SNDBUF ) failed: %m" );
812
+ elog (LOG , "setsockopt(%s ) failed: %m" , "SO_SNDBUF " );
794
813
return STATUS_ERROR ;
795
814
}
796
815
}
@@ -1676,7 +1695,7 @@ pq_setkeepaliveswin32(Port *port, int idle, int interval)
1676
1695
int
1677
1696
pq_getkeepalivesidle (Port * port )
1678
1697
{
1679
- #if defined(TCP_KEEPIDLE ) || defined(TCP_KEEPALIVE_THRESHOLD ) || defined( TCP_KEEPALIVE ) || defined( WIN32 )
1698
+ #if defined(PG_TCP_KEEPALIVE_IDLE ) || defined(SIO_KEEPALIVE_VALS )
1680
1699
if (port == NULL || IS_AF_UNIX (port -> laddr .addr .ss_family ))
1681
1700
return 0 ;
1682
1701
@@ -1688,34 +1707,13 @@ pq_getkeepalivesidle(Port *port)
1688
1707
#ifndef WIN32
1689
1708
ACCEPT_TYPE_ARG3 size = sizeof (port -> default_keepalives_idle );
1690
1709
1691
- #if defined(TCP_KEEPIDLE )
1692
- /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
1693
- if (getsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPIDLE ,
1694
- (char * ) & port -> default_keepalives_idle ,
1695
- & size ) < 0 )
1696
- {
1697
- elog (LOG , "getsockopt(TCP_KEEPIDLE) failed: %m" );
1698
- port -> default_keepalives_idle = -1 ; /* don't know */
1699
- }
1700
- #elif defined(TCP_KEEPALIVE_THRESHOLD )
1701
- /* TCP_KEEPALIVE_THRESHOLD is the name of this option on Solaris */
1702
- if (getsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPALIVE_THRESHOLD ,
1710
+ if (getsockopt (port -> sock , IPPROTO_TCP , PG_TCP_KEEPALIVE_IDLE ,
1703
1711
(char * ) & port -> default_keepalives_idle ,
1704
1712
& size ) < 0 )
1705
1713
{
1706
- elog (LOG , "getsockopt(TCP_KEEPALIVE_THRESHOLD ) failed: %m" );
1714
+ elog (LOG , "getsockopt(%s ) failed: %m" , PG_TCP_KEEPALIVE_IDLE_STR );
1707
1715
port -> default_keepalives_idle = -1 ; /* don't know */
1708
1716
}
1709
- #else /* must have TCP_KEEPALIVE */
1710
- /* TCP_KEEPALIVE is the name of this option on macOS */
1711
- if (getsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPALIVE ,
1712
- (char * ) & port -> default_keepalives_idle ,
1713
- & size ) < 0 )
1714
- {
1715
- elog (LOG , "getsockopt(TCP_KEEPALIVE) failed: %m" );
1716
- port -> default_keepalives_idle = -1 ; /* don't know */
1717
- }
1718
- #endif /* KEEPIDLE/KEEPALIVE_THRESHOLD/KEEPALIVE */
1719
1717
#else /* WIN32 */
1720
1718
/* We can't get the defaults on Windows, so return "don't know" */
1721
1719
port -> default_keepalives_idle = -1 ;
@@ -1735,7 +1733,7 @@ pq_setkeepalivesidle(int idle, Port *port)
1735
1733
return STATUS_OK ;
1736
1734
1737
1735
/* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
1738
- #if defined(TCP_KEEPIDLE ) || defined( TCP_KEEPALIVE_THRESHOLD ) || defined( TCP_KEEPALIVE ) || defined(SIO_KEEPALIVE_VALS )
1736
+ #if defined(PG_TCP_KEEPALIVE_IDLE ) || defined(SIO_KEEPALIVE_VALS )
1739
1737
if (idle == port -> keepalives_idle )
1740
1738
return STATUS_OK ;
1741
1739
@@ -1754,43 +1752,25 @@ pq_setkeepalivesidle(int idle, Port *port)
1754
1752
if (idle == 0 )
1755
1753
idle = port -> default_keepalives_idle ;
1756
1754
1757
- #if defined(TCP_KEEPIDLE )
1758
- /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
1759
- if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPIDLE ,
1760
- (char * ) & idle , sizeof (idle )) < 0 )
1761
- {
1762
- elog (LOG , "setsockopt(TCP_KEEPIDLE) failed: %m" );
1763
- return STATUS_ERROR ;
1764
- }
1765
- #elif defined(TCP_KEEPALIVE_THRESHOLD )
1766
- /* TCP_KEEPALIVE_THRESHOLD is the name of this option on Solaris */
1767
- if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPALIVE_THRESHOLD ,
1755
+ if (setsockopt (port -> sock , IPPROTO_TCP , PG_TCP_KEEPALIVE_IDLE ,
1768
1756
(char * ) & idle , sizeof (idle )) < 0 )
1769
1757
{
1770
- elog (LOG , "setsockopt(TCP_KEEPALIVE_THRESHOLD ) failed: %m" );
1758
+ elog (LOG , "setsockopt(%s ) failed: %m" , PG_TCP_KEEPALIVE_IDLE_STR );
1771
1759
return STATUS_ERROR ;
1772
1760
}
1773
- #else /* must have TCP_KEEPALIVE */
1774
- /* TCP_KEEPALIVE is the name of this option on macOS */
1775
- if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPALIVE ,
1776
- (char * ) & idle , sizeof (idle )) < 0 )
1777
- {
1778
- elog (LOG , "setsockopt(TCP_KEEPALIVE) failed: %m" );
1779
- return STATUS_ERROR ;
1780
- }
1781
- #endif
1782
1761
1783
1762
port -> keepalives_idle = idle ;
1784
1763
#else /* WIN32 */
1785
1764
return pq_setkeepaliveswin32 (port , idle , port -> keepalives_interval );
1786
1765
#endif
1787
- #else /* no way to set it */
1766
+ #else
1788
1767
if (idle != 0 )
1789
1768
{
1790
1769
elog (LOG , "setting the keepalive idle time is not supported" );
1791
1770
return STATUS_ERROR ;
1792
1771
}
1793
1772
#endif
1773
+
1794
1774
return STATUS_OK ;
1795
1775
}
1796
1776
@@ -1813,7 +1793,7 @@ pq_getkeepalivesinterval(Port *port)
1813
1793
(char * ) & port -> default_keepalives_interval ,
1814
1794
& size ) < 0 )
1815
1795
{
1816
- elog (LOG , "getsockopt(TCP_KEEPINTVL ) failed: %m" );
1796
+ elog (LOG , "getsockopt(%s ) failed: %m" , "TCP_KEEPINTVL " );
1817
1797
port -> default_keepalives_interval = -1 ; /* don't know */
1818
1798
}
1819
1799
#else
@@ -1856,7 +1836,7 @@ pq_setkeepalivesinterval(int interval, Port *port)
1856
1836
if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPINTVL ,
1857
1837
(char * ) & interval , sizeof (interval )) < 0 )
1858
1838
{
1859
- elog (LOG , "setsockopt(TCP_KEEPINTVL ) failed: %m" );
1839
+ elog (LOG , "setsockopt(%s ) failed: %m" , "TCP_KEEPINTVL " );
1860
1840
return STATUS_ERROR ;
1861
1841
}
1862
1842
@@ -1867,7 +1847,7 @@ pq_setkeepalivesinterval(int interval, Port *port)
1867
1847
#else
1868
1848
if (interval != 0 )
1869
1849
{
1870
- elog (LOG , "setsockopt(TCP_KEEPINTVL ) not supported" );
1850
+ elog (LOG , "setsockopt(%s ) not supported" , "TCP_KEEPINTVL " );
1871
1851
return STATUS_ERROR ;
1872
1852
}
1873
1853
#endif
@@ -1893,7 +1873,7 @@ pq_getkeepalivescount(Port *port)
1893
1873
(char * ) & port -> default_keepalives_count ,
1894
1874
& size ) < 0 )
1895
1875
{
1896
- elog (LOG , "getsockopt(TCP_KEEPCNT ) failed: %m" );
1876
+ elog (LOG , "getsockopt(%s ) failed: %m" , "TCP_KEEPCNT " );
1897
1877
port -> default_keepalives_count = -1 ; /* don't know */
1898
1878
}
1899
1879
}
@@ -1931,15 +1911,15 @@ pq_setkeepalivescount(int count, Port *port)
1931
1911
if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPCNT ,
1932
1912
(char * ) & count , sizeof (count )) < 0 )
1933
1913
{
1934
- elog (LOG , "setsockopt(TCP_KEEPCNT ) failed: %m" );
1914
+ elog (LOG , "setsockopt(%s ) failed: %m" , "TCP_KEEPCNT " );
1935
1915
return STATUS_ERROR ;
1936
1916
}
1937
1917
1938
1918
port -> keepalives_count = count ;
1939
1919
#else
1940
1920
if (count != 0 )
1941
1921
{
1942
- elog (LOG , "setsockopt(TCP_KEEPCNT ) not supported" );
1922
+ elog (LOG , "setsockopt(%s ) not supported" , "TCP_KEEPCNT " );
1943
1923
return STATUS_ERROR ;
1944
1924
}
1945
1925
#endif
0 commit comments