diff options
Diffstat (limited to 'src/interfaces/libpq/fe-connect.c')
-rw-r--r-- | src/interfaces/libpq/fe-connect.c | 60 |
1 files changed, 29 insertions, 31 deletions
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index e32c42b4b6b..2f7b4060df0 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -95,6 +95,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 */ @@ -1470,39 +1489,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; } @@ -1533,7 +1528,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; } @@ -1565,7 +1561,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; } @@ -2123,7 +2120,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; } |