@@ -1676,7 +1676,7 @@ pq_setkeepaliveswin32(Port *port, int idle, int interval)
16761676int
16771677pq_getkeepalivesidle (Port * port )
16781678{
1679- #if defined(TCP_KEEPIDLE ) || defined(TCP_KEEPALIVE ) || defined(WIN32 )
1679+ #if defined(TCP_KEEPIDLE ) || defined(TCP_KEEPALIVE_THRESHOLD ) || defined( TCP_KEEPALIVE ) || defined(WIN32 )
16801680 if (port == NULL || IS_AF_UNIX (port -> laddr .addr .ss_family ))
16811681 return 0 ;
16821682
@@ -1688,23 +1688,34 @@ pq_getkeepalivesidle(Port *port)
16881688#ifndef WIN32
16891689 ACCEPT_TYPE_ARG3 size = sizeof (port -> default_keepalives_idle );
16901690
1691- #ifdef TCP_KEEPIDLE
1691+ #if defined(TCP_KEEPIDLE )
1692+ /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
16921693 if (getsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPIDLE ,
16931694 (char * ) & port -> default_keepalives_idle ,
16941695 & size ) < 0 )
16951696 {
16961697 elog (LOG , "getsockopt(TCP_KEEPIDLE) failed: %m" );
16971698 port -> default_keepalives_idle = -1 ; /* don't know */
16981699 }
1699- #else
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 ,
1703+ (char * ) & port -> default_keepalives_idle ,
1704+ & size ) < 0 )
1705+ {
1706+ elog (LOG , "getsockopt(TCP_KEEPALIVE_THRESHOLD) failed: %m" );
1707+ port -> default_keepalives_idle = -1 ; /* don't know */
1708+ }
1709+ #else /* must have TCP_KEEPALIVE */
1710+ /* TCP_KEEPALIVE is the name of this option on macOS */
17001711 if (getsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPALIVE ,
17011712 (char * ) & port -> default_keepalives_idle ,
17021713 & size ) < 0 )
17031714 {
17041715 elog (LOG , "getsockopt(TCP_KEEPALIVE) failed: %m" );
17051716 port -> default_keepalives_idle = -1 ; /* don't know */
17061717 }
1707- #endif /* TCP_KEEPIDLE */
1718+ #endif /* KEEPIDLE/KEEPALIVE_THRESHOLD/KEEPALIVE */
17081719#else /* WIN32 */
17091720 /* We can't get the defaults on Windows, so return "don't know" */
17101721 port -> default_keepalives_idle = -1 ;
@@ -1723,7 +1734,8 @@ pq_setkeepalivesidle(int idle, Port *port)
17231734 if (port == NULL || IS_AF_UNIX (port -> laddr .addr .ss_family ))
17241735 return STATUS_OK ;
17251736
1726- #if defined(TCP_KEEPIDLE ) || defined(TCP_KEEPALIVE ) || defined(SIO_KEEPALIVE_VALS )
1737+ /* 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 )
17271739 if (idle == port -> keepalives_idle )
17281740 return STATUS_OK ;
17291741
@@ -1742,14 +1754,24 @@ pq_setkeepalivesidle(int idle, Port *port)
17421754 if (idle == 0 )
17431755 idle = port -> default_keepalives_idle ;
17441756
1745- #ifdef TCP_KEEPIDLE
1757+ #if defined(TCP_KEEPIDLE )
1758+ /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
17461759 if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPIDLE ,
17471760 (char * ) & idle , sizeof (idle )) < 0 )
17481761 {
17491762 elog (LOG , "setsockopt(TCP_KEEPIDLE) failed: %m" );
17501763 return STATUS_ERROR ;
17511764 }
1752- #else
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 ,
1768+ (char * ) & idle , sizeof (idle )) < 0 )
1769+ {
1770+ elog (LOG , "setsockopt(TCP_KEEPALIVE_THRESHOLD) failed: %m" );
1771+ return STATUS_ERROR ;
1772+ }
1773+ #else /* must have TCP_KEEPALIVE */
1774+ /* TCP_KEEPALIVE is the name of this option on macOS */
17531775 if (setsockopt (port -> sock , IPPROTO_TCP , TCP_KEEPALIVE ,
17541776 (char * ) & idle , sizeof (idle )) < 0 )
17551777 {
@@ -1762,7 +1784,7 @@ pq_setkeepalivesidle(int idle, Port *port)
17621784#else /* WIN32 */
17631785 return pq_setkeepaliveswin32 (port , idle , port -> keepalives_interval );
17641786#endif
1765- #else /* TCP_KEEPIDLE || SIO_KEEPALIVE_VALS */
1787+ #else /* no way to set it */
17661788 if (idle != 0 )
17671789 {
17681790 elog (LOG , "setting the keepalive idle time is not supported" );
@@ -1812,7 +1834,7 @@ pq_setkeepalivesinterval(int interval, Port *port)
18121834 if (port == NULL || IS_AF_UNIX (port -> laddr .addr .ss_family ))
18131835 return STATUS_OK ;
18141836
1815- #if defined(TCP_KEEPINTVL ) || defined (SIO_KEEPALIVE_VALS )
1837+ #if defined(TCP_KEEPINTVL ) || defined(SIO_KEEPALIVE_VALS )
18161838 if (interval == port -> keepalives_interval )
18171839 return STATUS_OK ;
18181840
0 commit comments