aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/libpq/fe-connect.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2017-06-28 12:30:16 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2017-06-28 12:30:16 -0400
commit43c67e32fb2920233e680af7a7e979ebb762c7e8 (patch)
tree7c449b96171d07f83b186e51b42387879793473f /src/interfaces/libpq/fe-connect.c
parenta2de017b30157c99e33cbc6457140e062a5ca26f (diff)
downloadpostgresql-43c67e32fb2920233e680af7a7e979ebb762c7e8.tar.gz
postgresql-43c67e32fb2920233e680af7a7e979ebb762c7e8.zip
Second try at fixing tcp_keepalives_idle option on Solaris.
Buildfarm evidence shows that TCP_KEEPALIVE_THRESHOLD doesn't exist after all on Solaris < 11. This means we need to take positive action to prevent the TCP_KEEPALIVE code path from being taken on that platform. I've chosen to limit it with "&& defined(__darwin__)", since it's unclear that anyone else would follow Apple's precedent of spelling the symbol that way. Also, follow a suggestion from Michael Paquier of eliminating code duplication by defining a couple of intermediate symbols for the socket option. In passing, make some effort to reduce the number of translatable messages by replacing "setsockopt(foo) failed" with "setsockopt(%s) failed", etc, throughout the affected files. And update relevant documentation so that it doesn't claim to provide an exhaustive list of the possible socket option names. Like the previous commit (f0256c774), back-patch to all supported branches. Discussion: https://postgr.es/m/20170627163757.25161.528@wrigleys.postgresql.org
Diffstat (limited to 'src/interfaces/libpq/fe-connect.c')
-rw-r--r--src/interfaces/libpq/fe-connect.c60
1 files changed, 29 insertions, 31 deletions
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 443b4fb3e2b..e61ed7dc28c 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -100,6 +100,25 @@ static int ldapServiceLookup(const char *purl, PQconninfoOption *options,
#define ERRCODE_CANNOT_CONNECT_NOW "57P03"
/*
+ * Cope with the various platform-specific ways to spell TCP keepalive socket
+ * options. This doesn't cover Windows, which as usual does its own thing.
+ */
+#if defined(TCP_KEEPIDLE)
+/* TCP_KEEPIDLE is the name of this option on Linux and *BSD */
+#define PG_TCP_KEEPALIVE_IDLE TCP_KEEPIDLE
+#define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPIDLE"
+#elif defined(TCP_KEEPALIVE_THRESHOLD)
+/* TCP_KEEPALIVE_THRESHOLD is the name of this option on Solaris >= 11 */
+#define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE_THRESHOLD
+#define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE_THRESHOLD"
+#elif defined(TCP_KEEPALIVE) && defined(__darwin__)
+/* TCP_KEEPALIVE is the name of this option on macOS */
+/* Caution: Solaris has this symbol but it means something different */
+#define PG_TCP_KEEPALIVE_IDLE TCP_KEEPALIVE
+#define PG_TCP_KEEPALIVE_IDLE_STR "TCP_KEEPALIVE"
+#endif
+
+/*
* fall back options if they are not specified by arguments or defined
* by environment variables
*/
@@ -1280,39 +1299,15 @@ setKeepalivesIdle(PGconn *conn)
if (idle < 0)
idle = 0;
-#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)
- {
- char sebuf[256];
-
- appendPQExpBuffer(&conn->errorMessage,
- libpq_gettext("setsockopt(TCP_KEEPIDLE) failed: %s\n"),
- SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
- return 0;
- }
-#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,
+#ifdef PG_TCP_KEEPALIVE_IDLE
+ if (setsockopt(conn->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
(char *) &idle, sizeof(idle)) < 0)
{
char sebuf[256];
appendPQExpBuffer(&conn->errorMessage,
- libpq_gettext("setsockopt(TCP_KEEPALIVE) failed: %s\n"),
+ libpq_gettext("setsockopt(%s) failed: %s\n"),
+ PG_TCP_KEEPALIVE_IDLE_STR,
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
return 0;
}
@@ -1343,7 +1338,8 @@ setKeepalivesInterval(PGconn *conn)
char sebuf[256];
appendPQExpBuffer(&conn->errorMessage,
- libpq_gettext("setsockopt(TCP_KEEPINTVL) failed: %s\n"),
+ libpq_gettext("setsockopt(%s) failed: %s\n"),
+ "TCP_KEEPINTVL",
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
return 0;
}
@@ -1375,7 +1371,8 @@ setKeepalivesCount(PGconn *conn)
char sebuf[256];
appendPQExpBuffer(&conn->errorMessage,
- libpq_gettext("setsockopt(TCP_KEEPCNT) failed: %s\n"),
+ libpq_gettext("setsockopt(%s) failed: %s\n"),
+ "TCP_KEEPCNT",
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
return 0;
}
@@ -1841,7 +1838,8 @@ keep_going: /* We will come back to here until there is
(char *) &on, sizeof(on)) < 0)
{
appendPQExpBuffer(&conn->errorMessage,
- libpq_gettext("setsockopt(SO_KEEPALIVE) failed: %s\n"),
+ libpq_gettext("setsockopt(%s) failed: %s\n"),
+ "SO_KEEPALIVE",
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
err = 1;
}