int
pq_getkeepalivesidle(Port *port)
{
-#if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE) || defined(WIN32)
+#if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE_THRESHOLD) || defined(TCP_KEEPALIVE) || defined(WIN32)
if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
return 0;
#ifndef WIN32
ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_idle);
-#ifdef TCP_KEEPIDLE
+#if defined(TCP_KEEPIDLE)
+ /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPIDLE,
(char *) &port->default_keepalives_idle,
&size) < 0)
elog(LOG, "getsockopt(TCP_KEEPIDLE) failed: %m");
port->default_keepalives_idle = -1; /* don't know */
}
-#else
+#elif defined(TCP_KEEPALIVE_THRESHOLD)
+ /* TCP_KEEPALIVE_THRESHOLD is the name of this option on Solaris */
+ if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD,
+ (char *) &port->default_keepalives_idle,
+ &size) < 0)
+ {
+ elog(LOG, "getsockopt(TCP_KEEPALIVE_THRESHOLD) failed: %m");
+ port->default_keepalives_idle = -1; /* don't know */
+ }
+#else /* must have TCP_KEEPALIVE */
+ /* TCP_KEEPALIVE is the name of this option on macOS */
if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPALIVE,
(char *) &port->default_keepalives_idle,
&size) < 0)
elog(LOG, "getsockopt(TCP_KEEPALIVE) failed: %m");
port->default_keepalives_idle = -1; /* don't know */
}
-#endif /* TCP_KEEPIDLE */
+#endif /* KEEPIDLE/KEEPALIVE_THRESHOLD/KEEPALIVE */
#else /* WIN32 */
/* We can't get the defaults on Windows, so return "don't know" */
port->default_keepalives_idle = -1;
if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
return STATUS_OK;
-#if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE) || defined(SIO_KEEPALIVE_VALS)
+/* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
+#if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE_THRESHOLD) || defined(TCP_KEEPALIVE) || defined(SIO_KEEPALIVE_VALS)
if (idle == port->keepalives_idle)
return STATUS_OK;
if (idle == 0)
idle = port->default_keepalives_idle;
-#ifdef TCP_KEEPIDLE
+#if defined(TCP_KEEPIDLE)
+ /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPIDLE,
(char *) &idle, sizeof(idle)) < 0)
{
elog(LOG, "setsockopt(TCP_KEEPIDLE) failed: %m");
return STATUS_ERROR;
}
-#else
+#elif defined(TCP_KEEPALIVE_THRESHOLD)
+ /* TCP_KEEPALIVE_THRESHOLD is the name of this option on Solaris */
+ if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD,
+ (char *) &idle, sizeof(idle)) < 0)
+ {
+ elog(LOG, "setsockopt(TCP_KEEPALIVE_THRESHOLD) failed: %m");
+ return STATUS_ERROR;
+ }
+#else /* must have TCP_KEEPALIVE */
+ /* TCP_KEEPALIVE is the name of this option on macOS */
if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPALIVE,
(char *) &idle, sizeof(idle)) < 0)
{
#else /* WIN32 */
return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
#endif
-#else /* TCP_KEEPIDLE || SIO_KEEPALIVE_VALS */
+#else /* no way to set it */
if (idle != 0)
{
elog(LOG, "setting the keepalive idle time is not supported");
if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
return STATUS_OK;
-#if defined(TCP_KEEPINTVL) || defined (SIO_KEEPALIVE_VALS)
+#if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
if (interval == port->keepalives_interval)
return STATUS_OK;
if (idle < 0)
idle = 0;
-#ifdef TCP_KEEPIDLE
+#if defined(TCP_KEEPIDLE)
+ /* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
if (setsockopt(conn->sock, IPPROTO_TCP, TCP_KEEPIDLE,
(char *) &idle, sizeof(idle)) < 0)
{
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
return 0;
}
-#else
-#ifdef TCP_KEEPALIVE
- /* macOS uses TCP_KEEPALIVE rather than TCP_KEEPIDLE */
+#elif defined(TCP_KEEPALIVE_THRESHOLD)
+ /* TCP_KEEPALIVE_THRESHOLD is the name of this option on Solaris */
+ if (setsockopt(conn->sock, IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD,
+ (char *) &idle, sizeof(idle)) < 0)
+ {
+ char sebuf[256];
+
+ appendPQExpBuffer(&conn->errorMessage,
+ libpq_gettext("setsockopt(TCP_KEEPALIVE_THRESHOLD) failed: %s\n"),
+ SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
+ return 0;
+ }
+#elif defined(TCP_KEEPALIVE)
+ /* TCP_KEEPALIVE is the name of this option on macOS */
if (setsockopt(conn->sock, IPPROTO_TCP, TCP_KEEPALIVE,
(char *) &idle, sizeof(idle)) < 0)
{
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
return 0;
}
-#endif
#endif
return 1;
return 1;
}
-#else /* Win32 */
+#else /* WIN32 */
#ifdef SIO_KEEPALIVE_VALS
/*
* Enable keepalives and set the keepalive values on Win32,